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": {
"psr-4": {
"": "src/",
"Modules\\": "modules/",
"Plugins\\": "plugins/"
"Modules\\Fatture\\": "modules/fatture/src",
"Modules\\Anagrafiche\\": "modules/anagrafiche/src",
"Plugins\\Fatturazione\\": "plugins/fatturazione/src"
},
"files": [
"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';
use Modules\Fatture\Fattura;
// Necessaria per la funzione add_movimento_magazzino
include_once Modules::filepath('Articoli', 'modutil.php');
include_once Modules::filepath('Interventi', 'modutil.php');
@ -20,49 +22,19 @@ switch (post('op')) {
case 'add':
$idanagrafica = post('idanagrafica');
$data = post('data');
$dir = post('dir');
$idtipodocumento = post('idtipodocumento');
$id_segment = post('id_segment');
$numero = get_new_numerofattura($data);
if ($dir == 'entrata') {
$numero_esterno = get_new_numerosecondariofattura($data);
$idconto = setting('Conto predefinito fatture di vendita');
$conto = 'vendite';
} else {
$numero_esterno = '';
$idconto = setting('Conto predefinito fatture di acquisto');
$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();
$fattura = Modules\Fatture\Fattura::create([
'idanagrafica' => $idanagrafica,
'data' => $data,
'id_segment' => $id_segment,
'idtipodocumento' => $idtipodocumento,
]);
$id_record = $fattura->id;
flash()->info(tr('Aggiunta fattura numero _NUM_!', [
'_NUM_' => $numero,
'_NUM_' => $fattura->numero,
]));
break;

View File

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

View File

@ -9,6 +9,8 @@ if ($module['name'] == 'Fatture di vendita') {
}
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));
$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 Util\Generator;
use Modules\Anagrafiche\Anagrafica;
class Fattura extends Model
{
@ -207,13 +208,15 @@ class Fattura extends Model
*/
public function getImponibile()
{
if (isset($this->conti['imponibile'])) {
return $this->conti['imponibile'];
if (!isset($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', [
':id' => $this->id,
]);
return $this->conti['imponibile'];
return $result['imponibile'];
}
@ -225,29 +228,25 @@ class Fattura extends Model
*/
public function getTotale()
{
if (isset($this->conti['totale'])) {
return $this->conti['totale'];
if (!isset($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();
// 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;
return $this->conti['totale'];
}
/**
@ -257,17 +256,17 @@ class Fattura extends Model
*/
public function getNetto($iddocumento)
{
if (isset($this->conti['netto'])) {
return $this->conti['netto'];
if (!isset($this->conti['netto'])) {
$netto = sum([
$this->getTotale(),
$this->bollo,
-$this->ritenutaacconto,
]);
$this->conti['netto'] = $netto;
}
$netto = sum([
$this->getTotale(),
$this->bollo,
-$this->ritenutaacconto,
]);
return $netto;
return $this->conti['netto'];
}
/**
@ -277,15 +276,11 @@ class Fattura extends Model
*/
public function getIvaDetraibile()
{
if (isset($this->conti['iva_detraibile'])) {
return $this->conti['iva_detraibile'];
if (!isset($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', [
':id' => $this->id,
]);
return $result['iva_detraibile'];
return $this->conti['iva_detraibile'];
}
/**
@ -295,15 +290,11 @@ class Fattura extends Model
*/
public function getIvaIndetraibile()
{
if (isset($this->conti['iva_indetraibile'])) {
return $this->conti['iva_indetraibile'];
if (!isset($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', [
':id' => $this->id,
]);
return $result['iva_indetraibile'];
return $this->conti['iva_indetraibile'];
}
/**
@ -313,7 +304,7 @@ class Fattura extends Model
*/
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,
]);
}
@ -328,6 +319,11 @@ class Fattura extends Model
return $this->getTipo()['reversed'] == 1;
}
public function anagrafica()
{
return $this->belongsTo(Anagrafica::class, 'idanagrafica');
}
public function tipo()
{
return $this->belongsTo(Tipo::class, 'idtipodocumento');

View File

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