Miglioramenti strutturali

This commit is contained in:
Thomas Zilio 2018-08-09 10:14:26 +02:00
parent f2fe26c58b
commit 530ceaabf4
10 changed files with 99 additions and 112 deletions

View File

@ -56,8 +56,9 @@
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"": "src/", "": "src/",
"Modules\\": "modules/", "Modules\\Fatture\\": "modules/fatture/src",
"Plugins\\": "plugins/" "Modules\\Anagrafiche\\": "modules/anagrafiche/src",
"Plugins\\Fatturazione\\": "plugins/fatturazione/src"
}, },
"files": [ "files": [
"lib/functions.php", "lib/functions.php",

View File

@ -0,0 +1,17 @@
<?php
namespace Modules\Anagrafiche;
use Illuminate\Database\Eloquent\Model;
use Modules\Fatture\Fattura;
class Anagrafica extends Model
{
protected $table = 'an_anagrafiche';
protected $primaryKey = 'idanagrafica';
public function fatture()
{
return $this->hasMany(Fattura::class, 'idanagrafica');
}
}

View File

@ -2,6 +2,8 @@
include_once __DIR__.'/../../core.php'; include_once __DIR__.'/../../core.php';
use Modules\Fatture\Fattura;
// Necessaria per la funzione add_movimento_magazzino // Necessaria per la funzione add_movimento_magazzino
include_once Modules::filepath('Articoli', 'modutil.php'); include_once Modules::filepath('Articoli', 'modutil.php');
include_once Modules::filepath('Interventi', 'modutil.php'); include_once Modules::filepath('Interventi', 'modutil.php');
@ -20,49 +22,19 @@ switch (post('op')) {
case 'add': case 'add':
$idanagrafica = post('idanagrafica'); $idanagrafica = post('idanagrafica');
$data = post('data'); $data = post('data');
$dir = post('dir');
$idtipodocumento = post('idtipodocumento'); $idtipodocumento = post('idtipodocumento');
$id_segment = post('id_segment'); $id_segment = post('id_segment');
$numero = get_new_numerofattura($data);
if ($dir == 'entrata') { $fattura = Modules\Fatture\Fattura::create([
$numero_esterno = get_new_numerosecondariofattura($data); 'idanagrafica' => $idanagrafica,
$idconto = setting('Conto predefinito fatture di vendita'); 'data' => $data,
$conto = 'vendite'; 'id_segment' => $id_segment,
} else { 'idtipodocumento' => $idtipodocumento,
$numero_esterno = ''; ]);
$idconto = setting('Conto predefinito fatture di acquisto'); $id_record = $fattura->id;
$conto = 'acquisti';
}
$campo = ($dir == 'entrata') ? 'idpagamento_vendite' : 'idpagamento_acquisti';
// Tipo di pagamento + banca predefinite dall'anagrafica
$query = 'SELECT id, (SELECT idbanca_'.$conto.' FROM an_anagrafiche WHERE idanagrafica = '.prepare($idanagrafica).') AS idbanca FROM co_pagamenti WHERE id = (SELECT '.$campo.' AS pagamento FROM an_anagrafiche WHERE idanagrafica='.prepare($idanagrafica).')';
$rs = $dbo->fetchArray($query);
$idpagamento = $rs[0]['id'];
$idbanca = $rs[0]['idbanca'];
// Se la fattura è di vendita e non è stato associato un pagamento predefinito al cliente leggo il pagamento dalle impostazioni
if ($dir == 'entrata' && $idpagamento == '') {
$idpagamento = setting('Tipo di pagamento predefinito');
}
// Se non è impostata la banca dell'anagrafica, uso quella del pagamento.
if (empty($idbanca)) {
// Banca predefinita del pagamento
$query = 'SELECT id FROM co_banche WHERE id_pianodeiconti3 = (SELECT idconto_'.$conto.' FROM co_pagamenti WHERE id = '.prepare($idpagamento).')';
$rs = $dbo->fetchArray($query);
$idbanca = $rs[0]['id'];
}
$query = 'INSERT INTO co_documenti (numero, numero_esterno, idanagrafica, idconto, idtipodocumento, idpagamento, idbanca, data, idstatodocumento, idsede, id_segment) VALUES ('.prepare($numero).', '.prepare($numero_esterno).', '.prepare($idanagrafica).', '.prepare($idconto).', '.prepare($idtipodocumento).', '.prepare($idpagamento).', '.prepare($idbanca).', '.prepare($data).', (SELECT `id` FROM `co_statidocumento` WHERE `descrizione`=\'Bozza\'), (SELECT idsede_fatturazione FROM an_anagrafiche WHERE idanagrafica='.prepare($idanagrafica).'), '.prepare($id_segment).')';
$dbo->query($query);
$id_record = $dbo->lastInsertedID();
flash()->info(tr('Aggiunta fattura numero _NUM_!', [ flash()->info(tr('Aggiunta fattura numero _NUM_!', [
'_NUM_' => $numero, '_NUM_' => $fattura->numero,
])); ]));
break; break;

View File

@ -440,14 +440,11 @@ if (!empty($operations)) {
echo ' echo '
<script> <script>
$( ".btn-sm[data-toggle=\"tooltip\"]" ).each(function() { $( ".btn-sm[data-toggle=\"tooltip\"]" ).each(function() {
$(this).on("click", function(){ $(this).on("click", function(){
form = $("#edit-form");
form = $("#edit-form"); btn = $(this);
btn = $(this);
prev_html = btn.html(); prev_html = btn.html();
prev_class = btn.attr("class"); prev_class = btn.attr("class");
@ -457,14 +454,14 @@ $( ".btn-sm[data-toggle=\"tooltip\"]" ).each(function() {
btn.prop("disabled", true); btn.prop("disabled", true);
function restore_btn(btn, prev_html, prev_class){ function restore_btn(btn, prev_html, prev_class){
btn.attr("class", ""); btn.attr("class", "");
btn.addClass(prev_class); btn.addClass(prev_class);
btn.html(prev_html); btn.html(prev_html);
btn.prop("disabled", false); btn.prop("disabled", false);
} }
//Procedo al salvataggio solo se tutti i campi obbligatori sono compilati, altimenti mostro avviso // Procedo al salvataggio solo se tutti i campi obbligatori sono compilati, altimenti mostro avviso
if (form.parsley().isValid()){ if (form.parsley().isValid()){
content_was_modified = false; content_was_modified = false;
@ -492,23 +489,20 @@ $( ".btn-sm[data-toggle=\"tooltip\"]" ).each(function() {
} }
}); });
}else{ } else {
swal({ swal({
type: "error", type: "error",
text: "'.tr('Alcuni campi obbligatori non sono stati compilati correttamente.').'", text: "'.tr('Alcuni campi obbligatori non sono stati compilati correttamente.').'",
title: "'.tr('Errore').'", title: "'.tr('Errore').'",
onClose: hide_popup onClose: hide_popup
}).then(function () { });
}); function hide_popup(){
function hide_popup(){
$("#bs-popup").modal("hide"); $("#bs-popup").modal("hide");
session_set ("errors,0", 0, 1); session_set ("errors,0", 0, 1);
form.parsley().validate(); form.parsley().validate();
} }
restore_btn(btn, prev_html, prev_class); restore_btn(btn, prev_html, prev_class);
} }

View File

@ -9,6 +9,8 @@ if ($module['name'] == 'Fatture di vendita') {
} }
if (isset($id_record)) { if (isset($id_record)) {
$fattura = Modules\Fatture\Fattura::with('tipo', 'stato')->find($id_record);
$record = $dbo->fetchOne('SELECT *, co_tipidocumento.reversed AS is_reversed, co_documenti.idagente AS idagente_fattura, co_documenti.note, co_documenti.note_aggiuntive, co_documenti.idpagamento, co_documenti.id AS iddocumento, co_statidocumento.descrizione AS `stato`, co_tipidocumento.descrizione AS `descrizione_tipodoc`, (SELECT descrizione FROM co_ritenutaacconto WHERE id=idritenutaacconto) AS ritenutaacconto_desc, (SELECT descrizione FROM co_rivalsainps WHERE id=idrivalsainps) AS rivalsainps_desc FROM ((co_documenti LEFT OUTER JOIN co_statidocumento ON co_documenti.idstatodocumento=co_statidocumento.id) INNER JOIN an_anagrafiche ON co_documenti.idanagrafica=an_anagrafiche.idanagrafica) INNER JOIN co_tipidocumento ON co_documenti.idtipodocumento=co_tipidocumento.id WHERE co_tipidocumento.dir = '.prepare($dir).' AND co_documenti.id='.prepare($id_record)); $record = $dbo->fetchOne('SELECT *, co_tipidocumento.reversed AS is_reversed, co_documenti.idagente AS idagente_fattura, co_documenti.note, co_documenti.note_aggiuntive, co_documenti.idpagamento, co_documenti.id AS iddocumento, co_statidocumento.descrizione AS `stato`, co_tipidocumento.descrizione AS `descrizione_tipodoc`, (SELECT descrizione FROM co_ritenutaacconto WHERE id=idritenutaacconto) AS ritenutaacconto_desc, (SELECT descrizione FROM co_rivalsainps WHERE id=idrivalsainps) AS rivalsainps_desc FROM ((co_documenti LEFT OUTER JOIN co_statidocumento ON co_documenti.idstatodocumento=co_statidocumento.id) INNER JOIN an_anagrafiche ON co_documenti.idanagrafica=an_anagrafiche.idanagrafica) INNER JOIN co_tipidocumento ON co_documenti.idtipodocumento=co_tipidocumento.id WHERE co_tipidocumento.dir = '.prepare($dir).' AND co_documenti.id='.prepare($id_record));
$note_accredito = $dbo->fetchArray("SELECT co_documenti.id, IF(numero_esterno != '', numero_esterno, numero) AS numero, data FROM co_documenti JOIN co_tipidocumento ON co_documenti.idtipodocumento=co_tipidocumento.id WHERE reversed = 1 AND ref_documento=".prepare($id_record)); $note_accredito = $dbo->fetchArray("SELECT co_documenti.id, IF(numero_esterno != '', numero_esterno, numero) AS numero, data FROM co_documenti JOIN co_tipidocumento ON co_documenti.idtipodocumento=co_tipidocumento.id WHERE reversed = 1 AND ref_documento=".prepare($id_record));

View File

@ -4,6 +4,7 @@ namespace Modules\Fatture;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Util\Generator; use Util\Generator;
use Modules\Anagrafiche\Anagrafica;
class Fattura extends Model class Fattura extends Model
{ {
@ -207,13 +208,15 @@ class Fattura extends Model
*/ */
public function getImponibile() public function getImponibile()
{ {
if (isset($this->conti['imponibile'])) { if (!isset($this->conti['imponibile'])) {
return $this->conti['imponibile']; $result = database()->fetchOne('SELECT SUM(co_righe_documenti.subtotale - co_righe_documenti.sconto) AS imponibile FROM co_righe_documenti WHERE iddocumento = :id', [
':id' => $this->id,
]);
$this->conti['imponibile'] = $result['imponibile'];
} }
$result = database()->fetchOne('SELECT SUM(co_righe_documenti.subtotale - co_righe_documenti.sconto) AS imponibile FROM co_righe_documenti WHERE iddocumento = :id', [ return $this->conti['imponibile'];
':id' => $this->id,
]);
return $result['imponibile']; return $result['imponibile'];
} }
@ -225,29 +228,25 @@ class Fattura extends Model
*/ */
public function getTotale() public function getTotale()
{ {
if (isset($this->conti['totale'])) { if (!isset($this->conti['totale'])) {
return $this->conti['totale']; // Sommo l'iva di ogni riga al totale
$iva = $this->righe()->sum('iva');
$iva_rivalsainps = database()->fetchArray('SELECT SUM(rivalsainps / 100 * percentuale) AS iva_rivalsainps FROM co_righe_documenti INNER JOIN co_iva ON co_iva.id = co_righe_documenti.idiva WHERE iddocumento = :id', [
':id' => $this->id,
])['iva_rivalsainps'];
$totale = sum([
$this->getImponibile(),
$this->rivalsainps,
$iva,
$iva_rivalsainps,
]);
$this->conti['totale'] = $totale;
} }
$dbo = database(); return $this->conti['totale'];
// Sommo l'iva di ogni riga al totale
$iva = $dbo->fetchArray('SELECT SUM(iva) AS iva FROM co_righe_documenti WHERE iddocumento = :id', [
':id' => $this->id,
])['iva'];
$iva_rivalsainps = $dbo->fetchArray('SELECT SUM(rivalsainps / 100 * percentuale) AS iva_rivalsainps FROM co_righe_documenti INNER JOIN co_iva ON co_iva.id = co_righe_documenti.idiva WHERE iddocumento = :id', [
':id' => $this->id,
])['iva_rivalsainps'];
$totale = sum([
$this->getImponibile(),
$this->rivalsainps,
$iva,
$iva_rivalsainps,
]);
return $totale;
} }
/** /**
@ -257,17 +256,17 @@ class Fattura extends Model
*/ */
public function getNetto($iddocumento) public function getNetto($iddocumento)
{ {
if (isset($this->conti['netto'])) { if (!isset($this->conti['netto'])) {
return $this->conti['netto']; $netto = sum([
$this->getTotale(),
$this->bollo,
-$this->ritenutaacconto,
]);
$this->conti['netto'] = $netto;
} }
$netto = sum([ return $this->conti['netto'];
$this->getTotale(),
$this->bollo,
-$this->ritenutaacconto,
]);
return $netto;
} }
/** /**
@ -277,15 +276,11 @@ class Fattura extends Model
*/ */
public function getIvaDetraibile() public function getIvaDetraibile()
{ {
if (isset($this->conti['iva_detraibile'])) { if (!isset($this->conti['iva_detraibile'])) {
return $this->conti['iva_detraibile']; $this->conti['iva_detraibile'] = $this->righe()->sum('iva') - $this->getIvaIndetraibile();
} }
$result = database()->fetchOne('SELECT SUM(iva) - SUM(iva_indetraibile) AS iva_detraibile FROM co_righe_documenti WHERE iddocumento = :id', [ return $this->conti['iva_detraibile'];
':id' => $this->id,
]);
return $result['iva_detraibile'];
} }
/** /**
@ -295,15 +290,11 @@ class Fattura extends Model
*/ */
public function getIvaIndetraibile() public function getIvaIndetraibile()
{ {
if (isset($this->conti['iva_indetraibile'])) { if (!isset($this->conti['iva_indetraibile'])) {
return $this->conti['iva_indetraibile']; $this->conti['iva_indetraibile'] = $this->righe()->sum('iva_indetraibile');
} }
$result = database()->fetchOne('SELECT SUM(iva_indetraibile) AS iva_indetraibile FROM co_righe_documenti WHERE = :id', [ return $this->conti['iva_indetraibile'];
':id' => $this->id,
]);
return $result['iva_indetraibile'];
} }
/** /**
@ -313,7 +304,7 @@ class Fattura extends Model
*/ */
public function getNoteDiAccredito() public function getNoteDiAccredito()
{ {
return database()->fetchArray("SELECT co_documenti.id, IF(numero_esterno != '', numero_esterno, numero) AS numero, data FROM co_documenti WHERE idtipodocumento IN (SELECT id FROM co_tipidocumento WHERE reversed = 1) AND ref_documento = :id", [ return database()->fetchArray("SELECT co_documenti.id, IF(numero_esterno != '', numero_esterno, numero) AS numero, data FROM co_documenti WHERE idtipodocumento IN (SELECT id FROM co_tipidocumento WHERE reversed = 1) AND ref_documento = :id", [
':id' => $this->id, ':id' => $this->id,
]); ]);
} }
@ -328,6 +319,11 @@ class Fattura extends Model
return $this->getTipo()['reversed'] == 1; return $this->getTipo()['reversed'] == 1;
} }
public function anagrafica()
{
return $this->belongsTo(Anagrafica::class, 'idanagrafica');
}
public function tipo() public function tipo()
{ {
return $this->belongsTo(Tipo::class, 'idtipodocumento'); return $this->belongsTo(Tipo::class, 'idtipodocumento');

View File

@ -12,4 +12,9 @@ class Riga extends Model
{ {
return $this->belongsTo(Fattura::class, 'iddocumento'); return $this->belongsTo(Fattura::class, 'iddocumento');
} }
public function getImponibile()
{
return $this->subtotale - $this->sconto;
}
} }