2017-09-05 17:31:58 +02:00
< ? php
include_once __DIR__ . '/../../core.php' ;
2017-09-22 15:19:59 +02:00
$module = Modules :: get ( $id_module );
2017-09-05 17:31:58 +02:00
$data = [
'ddt' => [
'table' => 'dt_ddt' ,
'rows' => 'dt_righe_ddt' ,
'id' => 'idddt' ,
'condition' => '(id_riga_documento IS NOT NULL)' ,
],
'ord' => [
'table' => 'or_ordini' ,
'rows' => 'or_righe_ordini' ,
'id' => 'idordine' ,
'condition' => '(id_riga_ddt IS NOT NULL OR id_riga_documento IS NOT NULL)' ,
],
];
$documento = get ( 'documento' );
if ( $module [ 'name' ] == 'Ordini cliente' || $module [ 'name' ] == 'Ordini fornitore' ) {
$pos = 'ord' ;
$op = ( $documento == 'ddt' ) ? 'ddt_da_ordine' : 'fattura_da_ordine' ;
$head = tr ( 'Ordine numero _NUM_' );
} else {
$pos = 'ddt' ;
$op = 'fattura_da_ddt' ;
$head = tr ( 'Ddt numero _NUM_' );
}
$table = $data [ $pos ][ 'table' ];
$rows = $data [ $pos ][ 'rows' ];
$id = $data [ $pos ][ 'id' ];
$row = str_replace ( 'id' , 'id_riga_' , $id );
if ( $module [ 'name' ] == 'Ordini cliente' || $module [ 'name' ] == 'Ddt di vendita' ) {
$dir = 'entrata' ;
} else {
$dir = 'uscita' ;
}
if ( $module [ 'name' ] == 'Ordini cliente' ) {
$module_name = ( $documento == 'ddt' ) ? 'Ddt di vendita' : 'Fatture di vendita' ;
} elseif ( $module [ 'name' ] == 'Ordini fornitore' ) {
$module_name = ( $documento == 'ddt' ) ? 'Ddt di acquisto' : 'Fatture di acquisto' ;
} elseif ( $module [ 'name' ] == 'Ddt di acquisto' ) {
$module_name = 'Fatture di acquisto' ;
} elseif ( $module [ 'name' ] == 'Ddt di vendita' ) {
$module_name = 'Fatture di vendita' ;
}
$op = ! empty ( $get [ 'op' ]) ? $get [ 'op' ] : $op ;
$button = ( $documento == 'ddt' ) ? tr ( 'Crea ddt' ) : tr ( 'Crea fattura' );
$button = ! empty ( $get [ 'op' ]) ? tr ( 'Aggiungi' ) : $button ;
// Info documento
$rs = $dbo -> fetchArray ( 'SELECT * FROM ' . $table . ' WHERE id=' . prepare ( $id_record ));
$numero = ! empty ( $rs [ 0 ][ 'numero_esterno' ]) ? $rs [ 0 ][ 'numero_esterno' ] : $rs [ 0 ][ 'numero' ];
$idanagrafica = $rs [ 0 ][ 'idanagrafica' ];
$idpagamento = $rs [ 0 ][ 'idpagamento' ];
$idconto = $rs [ 0 ][ 'idconto' ];
/*
Form di inserimento riga documento
*/
echo '
< p > '.str_replace(' _NUM_ ', $numero, $head).' .</ p > ' ;
// Selezione articoli dell'ordine da portare nel ddt
$rs = $dbo -> fetchArray ( 'SELECT *, (qta - qta_evasa) AS qta_rimanente FROM ' . $table . ' INNER JOIN ' . $rows . ' ON ' . $table . '.id=' . $rows . '.' . $id . ' WHERE ' . $table . '.id=' . prepare ( $id_record ) . ' HAVING qta_rimanente > 0' );
if ( ! empty ( $rs )) {
echo '
2017-11-16 17:33:24 +01:00
< p > '.tr(' Seleziona le righe e le relative quantità da inserire nel documento ').' .</ p >
2017-09-05 17:31:58 +02:00
2017-09-22 15:19:59 +02:00
< form action = " '. $rootdir .'/editor.php?id_module='.Modules::get( $module_name )['id'].(!empty( $get['iddocumento'] ) ? '&id_record='. $get['iddocumento'] : '').' " method = " post " >
2017-09-05 17:31:58 +02:00
< input type = " hidden " name = " '. $id .' " value = " '. $id_record .' " >
< input type = " hidden " name = " idanagrafica " value = " '. $idanagrafica .' " >
< input type = " hidden " name = " idconto " value = " '. $idconto .' " >
< input type = " hidden " name = " idpagamento " value = " '. $idpagamento .' " >
< input type = " hidden " name = " iddocumento " value = " '. $id_record .' " >
< input type = " hidden " name = " op " value = " '. $op .' " >
< input type = " hidden " name = " backto " value = " record-edit " >
< input type = " hidden " name = " dir " value = " '. $dir .' " > ' ;
if ( empty ( $get [ 'op' ])) {
echo '
< div class = " row " >
2018-03-21 21:25:17 +01:00
< div class = " col-md-6 " >
2017-09-05 17:31:58 +02:00
{[ " type " : " date " , " label " : " '.tr('Data del documento').' " , " name " : " data " , " required " : 1 , " value " : " -now- " ]}
2018-03-30 01:57:00 +02:00
</ div > ' ;
2018-03-21 21:25:17 +01:00
2018-03-30 01:57:00 +02:00
if ( $module_name == 'Fatture di vendita' || $module_name == 'Fatture di acquisto' ){
echo '
2018-03-21 21:25:17 +01:00
< div class = " col-md-6 " >
2018-05-11 15:56:08 +02:00
{[ " type " : " select " , " label " : " '.tr('Sezionale').' " , " name " : " id_segment " , " required " : 1 , " values " : " query=SELECT id, name AS descrizione FROM zz_segments WHERE id_module='.prepare(Modules::get( $module_name )['id']).' ORDER BY name " , " value " : " '.Modules::get( $module_name )['id_segment'].' " ]}
2018-03-30 01:57:00 +02:00
</ div > ' ;
}
2018-05-11 15:56:08 +02:00
2018-03-30 01:57:00 +02:00
echo
'</div>' ;
2017-09-05 17:31:58 +02:00
}
echo '
< div class = " clearfix " ></ div >
< br >
< table class = " table table-striped table-hover table-condensed " >
< tr >
< th > '.tr(' Descrizione ').' </ th >
< th width = " 10% " > '.tr(' Q . tà ').' </ th >
< th width = " 15% " > '.tr(' Q . tà da evadere ').' </ th >
< th width = " 20% " > '.tr(' Subtot . ').' </ th >
< th width = " 20% " > '.tr(' Seriali ').' </ th >
</ tr > ' ;
$totale = 0.00 ;
foreach ( $rs as $i => $r ) {
// Descrizione
echo '
< tr >
< td >
2018-01-12 16:23:26 +01:00
2017-11-16 17:33:24 +01:00
< input type = " hidden " name = " abilita_serial['. $r['id'] .'] " value = " '. $r['abilita_serial'] .' " />
< input type = " hidden " id = " idarticolo_'. $i .' " name = " idarticolo['. $r['id'] .'] " value = " '. $r['idarticolo'] .' " />
< input type = " hidden " id = " descrizione_'. $i .' " name = " descrizione['. $r['id'] .'] " value = " '. $r['descrizione'] .' " /> ' ;
2017-09-05 17:31:58 +02:00
// Checkbox - da evadere?
echo '
2017-11-16 17:33:24 +01:00
< input type = " checkbox " checked = " checked " id = " checked_'. $i .' " name = " evadere['. $r['id'] .'] " value = " on " onclick = " ricalcola_subtotale_riga('. $i .'); " /> ' ;
2017-09-05 17:31:58 +02:00
echo nl2br ( $r [ 'descrizione' ]);
echo '
</ td > ' ;
// Q.tà rimanente
echo '
< td >
< input type = " hidden " id = " qtamax_'. $i .' " value = " '.( $r['qta'] - $r['qta_evasa'] ).' " />
2017-11-16 17:33:24 +01:00
< input type = " hidden " id = " um_'. $i .' " name = " um['. $r['id'] .'] " value = " '. $r['um'] .' " />
2017-09-05 17:31:58 +02:00
< p class = " text-center " > '.Translator::numberToLocale($r[' qta_rimanente ']).' </ p >
</ td > ' ;
// Q.tà da evadere
echo '
< td >
2018-01-12 16:23:26 +01:00
{[ " type " : " number " , " name " : " qta_da_evadere['. $r['id'] .'] " , " id " : " qta_'. $i .' " , " required " : 1 , " value " : " '. $r['qta_rimanente'] .' " , " extra " : " onkeyup= \" ricalcola_subtotale_riga('. $i .'); \" " , " decimals " : " qta " , " min-value " : " 0 " ]}
2017-09-05 17:31:58 +02:00
</ td > ' ;
// Subtotale
$subtotale = $r [ 'subtotale' ] / $r [ 'qta' ] * ( $r [ 'qta' ] - $r [ 'qta_evasa' ]);
$sconto = $r [ 'sconto' ] / $r [ 'qta' ] * ( $r [ 'qta' ] - $r [ 'qta_evasa' ]);
$iva = $r [ 'iva' ] / $r [ 'qta' ] * ( $r [ 'qta' ] - $r [ 'qta_evasa' ]);
echo '
< td >
2017-11-16 17:33:24 +01:00
< input type = " hidden " id = " subtot_'. $i .' " name = " subtot['. $r['id'] .'] " value = " '.( $r['subtotale'] / $r['qta'] ).' " />
< input type = " hidden " id = " sconto_'. $i .' " name = " sconto['. $r['id'] .'] " value = " '.( $r['sconto'] / $r['qta'] ).' " />
< input type = " hidden " id = " idiva_'. $i .' " name = " idiva['. $r['id'] .'] " value = " '. $r['idiva'] .' " />
< input type = " hidden " id = " iva_'. $i .' " name = " iva['. $r['id'] .'] " value = " '.( $r['iva'] / $r['qta'] ).' " />
2017-09-05 17:31:58 +02:00
< big id = " subtotale_'. $i .' " > '.Translator::numberToLocale($subtotale - $sconto + $iva).' & euro ; </ big >< br />
< small style = " color:#777; " id = " subtotaledettagli_'. $i .' " > '.Translator::numberToLocale($subtotale - $sconto).' + '.Translator::numberToLocale($iva).' </ small >
</ td > ' ;
// Seriali
echo '
< td > ' ;
if ( ! empty ( $r [ 'abilita_serial' ])) {
2017-09-13 09:33:45 +02:00
$values = $dbo -> fetchArray ( 'SELECT DISTINCT serial FROM mg_prodotti WHERE dir=\'' . $dir . '\' AND ' . $row . ' = \'' . $r [ 'id' ] . '\' AND serial IS NOT NULL AND serial NOT IN (SELECT serial FROM mg_prodotti WHERE serial IS NOT NULL AND dir=\'' . $dir . '\' AND ' . $data [ $pos ][ 'condition' ] . ')' );
2017-09-05 17:31:58 +02:00
echo '
2017-11-16 17:33:24 +01:00
{[ " type " : " select " , " name " : " serial['. $i .']['. $r['id'] .'] " , " id " : " serial_'. $i .' " , " multiple " : 1 , " values " : " query=SELECT DISTINCT serial AS id, serial AS descrizione FROM mg_prodotti WHERE dir= \ ''. $dir .' \ ' AND '. $row .' = \ ''. $r['id'] .' \ ' AND serial IS NOT NULL AND serial NOT IN (SELECT serial FROM mg_prodotti WHERE serial IS NOT NULL AND dir= \ ''. $dir .' \ ' AND '. $data[$pos] ['condition'].') " , " value " : " '.implode(',', array_column( $values , 'serial')).' " , " extra " : " data-maximum= \" '.intval( $r['qta_rimanente'] ).' \" " ]}
2017-09-05 17:31:58 +02:00
' ;
} else {
echo '-' ;
}
echo '
</ td >
</ tr > ' ;
$totale += $subtotale - $sconto + $iva ;
}
// Totale
echo '
< tr >
< td colspan = " 4 " align = " right " class = " text-right " >
< b > '.tr(' Totale ').' :</ b >
</ td >
< td class = " text-right " colspan = " 2 " >
< big id = " totale " > '.Translator::numberToLocale($totale).' & euro ; </ big >
</ td >
</ tr >
</ table > ' ;
echo '
<!-- PULSANTI -->
< div class = " row " >
< div class = " col-md-12 text-right " >
< button type = " submit " id = " submit_btn " class = " btn btn-primary pull-right " >
< i class = " fa fa-plus " ></ i > '.$button.'
</ button >
</ div >
</ div >
</ form > ' ;
} else {
echo '
< p > '.tr(' Non ci sono articoli da evadere ').' ...</ p > ' ;
}
echo '
< script src = " '. $rootdir .'/lib/init.js " ></ script > ' ;
?>
< script type = " text/javascript " >
function ricalcola_subtotale_riga ( r ){
2017-09-18 18:50:37 +02:00
subtot = $ ( " #subtot_ " + r ) . val ();
sconto = $ ( " #sconto_ " + r ) . val ();
iva = $ ( " #iva_ " + r ) . val ();
2017-09-05 17:31:58 +02:00
2017-09-18 18:50:37 +02:00
qtamax = $ ( " #qtamax_ " + r ) . val () ? $ ( " #qtamax_ " + r ) . val () : 0 ;
2017-09-05 17:31:58 +02:00
2017-09-18 18:50:37 +02:00
subtot = parseFloat ( subtot );
sconto = parseFloat ( sconto );
iva = parseFloat ( iva );
qtamax = parseFloat ( qtamax );
2017-09-05 17:31:58 +02:00
2017-09-18 18:50:37 +02:00
subtot = subtot - sconto ;
2017-09-05 17:31:58 +02:00
2017-09-18 18:50:37 +02:00
qta = $ ( " #qta_ " + r ) . val () . toEnglish ();
// Se inserisco una quantità da evadere maggiore di quella rimanente, la imposto al massimo possibile
if ( qta > qtamax ){
qta = qtamax ;
$ ( '#qta_' + r ) . val ( qta );
}
2017-09-05 17:31:58 +02:00
// Se tolgo la spunta della casella dell'evasione devo azzerare i conteggi
2017-09-18 18:50:37 +02:00
if ( isNaN ( qta ) || ! $ ( '#checked_' + r ) . is ( ':checked' )){
2017-09-05 17:31:58 +02:00
qta = 0 ;
}
2017-09-19 12:22:21 +02:00
$ ( " #serial_ " + r ) . selectClear ();
$ ( " #serial_ " + r ) . select2 ( " destroy " );
$ ( " #serial_ " + r ) . data ( 'maximum' , qta );
start_superselect ();
2017-09-19 16:50:01 +02:00
subtotale = ( subtot * qta + iva * qta ) . toLocale ();
2017-09-05 17:31:58 +02:00
$ ( " #subtotale_ " + r ) . html ( subtotale + " € " );
2017-09-19 16:50:01 +02:00
$ ( " #subtotaledettagli_ " + r ) . html (( subtot * qta ) . toLocale () + " + " + ( iva * qta ) . toLocale ());
2017-09-05 17:31:58 +02:00
ricalcola_totale ();
}
2018-05-11 15:56:08 +02:00
2017-09-05 17:31:58 +02:00
function ricalcola_totale (){
2018-03-30 01:57:00 +02:00
tot_qta = 0 ;
2017-09-05 17:31:58 +02:00
r = 0 ;
totale = 0.00 ;
$ ( 'input[id*=qta_]' ) . each ( function (){
qta = $ ( this ) . val () . toEnglish ();
if ( ! $ ( '#checked_' + r ) . is ( ':checked' ) || isNaN ( qta ) ){
qta = 0 ;
}
2017-09-18 18:50:37 +02:00
subtot = $ ( " #subtot_ " + r ) . val ();
sconto = $ ( " #sconto_ " + r ) . val ();
iva = $ ( " #iva_ " + r ) . val ();
2017-09-05 17:31:58 +02:00
2017-09-18 18:50:37 +02:00
subtot = parseFloat ( subtot );
sconto = parseFloat ( sconto );
iva = parseFloat ( iva );
2017-09-05 17:31:58 +02:00
2017-09-18 18:50:37 +02:00
subtot = subtot - sconto ;
2017-09-05 17:31:58 +02:00
totale += subtot * qta + iva * qta ;
r ++ ;
2018-03-30 01:57:00 +02:00
tot_qta += qta ;
2017-09-05 17:31:58 +02:00
});
2017-09-19 16:50:01 +02:00
$ ( '#totale' ) . html ( ( totale . toLocale ()) + " € " );
2018-05-11 15:56:08 +02:00
2018-03-30 01:57:00 +02:00
if ( tot_qta > 0 )
2017-09-05 17:31:58 +02:00
$ ( '#submit_btn' ) . show ();
2018-03-30 01:57:00 +02:00
else
$ ( '#submit_btn' ) . hide ();
2017-09-05 17:31:58 +02:00
}
</ script >