diff --git a/invoice.php b/invoice.php index d8406eb..a84fc9d 100644 --- a/invoice.php +++ b/invoice.php @@ -32,6 +32,11 @@ $desc = GETPOST('desc'); $pay = GETPOST('pay'); +function fail($message){ + header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500); + die($message); +} + $sql="SELECT rowid FROM ".MAIN_DB_PREFIX."facture where facnumber='ProvPOS-$place'"; $resql = $db->query($sql); $row = $db->fetch_array ($resql); @@ -52,6 +57,26 @@ $now=dol_now(); $invoice = new Facture($db); $invoice->fetch($placeid); + if($invoice->total_ttc<0){ + $invoice->type= $invoice::TYPE_CREDIT_NOTE; + $sql="SELECT rowid FROM ".MAIN_DB_PREFIX."facture WHERE "; + $sql.="fk_soc = '".$invoice->socid."' "; + $sql.="AND type <> ".Facture::TYPE_CREDIT_NOTE." "; + $sql.="AND fk_statut >= ".$invoice::STATUS_VALIDATED." "; + $sql.="ORDER BY rowid DESC"; + $resql = $db->query($sql); + if($resql){ + $obj = $db->fetch_object($resql); + $fk_source=$obj->rowid; + if($fk_source == null){ + fail($langs->transnoentitiesnoconv("NoPreviousBillForCustomer")); + } + }else{ + fail($langs->transnoentitiesnoconv("NoPreviousBillForCustomer")); + } + $invoice->fk_facture_source=$fk_source; + $invoice->update($user); + } if (! empty($conf->stock->enabled) and $conf->global->CASHDESK_NO_DECREASE_STOCK!="1") $invoice->validate($user, '', $conf->global->CASHDESK_ID_WAREHOUSE); else $invoice->validate($user); // Add the payment diff --git a/langs/en_US/takepos.lang b/langs/en_US/takepos.lang index 1910389..0c21425 100644 --- a/langs/en_US/takepos.lang +++ b/langs/en_US/takepos.lang @@ -19,4 +19,5 @@ Floor=Floor AddTable=Add table Place=Place TakeboxNecesary='TakeBOX' application required -OrderPrinters=Order printers \ No newline at end of file +OrderPrinters=Order printers +NoPreviousBillForCustomer=Cannot do negative bill without previous bill for current customer diff --git a/langs/fr_FR/takepos.lang b/langs/fr_FR/takepos.lang index fb2a457..aeec68b 100644 --- a/langs/fr_FR/takepos.lang +++ b/langs/fr_FR/takepos.lang @@ -19,4 +19,5 @@ Floor=Étage AddTable=Ajouter une table Place=Endroit TakeboxNecesary='TakeBOX' Application requise -OrderPrinters=Commande imprimantes \ No newline at end of file +OrderPrinters=Commande imprimantes +NoPreviousBillForCustomer=Impossible de créer une facture négative sans une facture précédente pour le client courant diff --git a/pay.php b/pay.php index 5479968..105a5a3 100644 --- a/pay.php +++ b/pay.php @@ -47,7 +47,7 @@ function addreceived(price) $('#change1').html(received.toFixed(2)); if (received>total_ttc;?>) { - var change=parseFloat(received-total_ttc;?>); + var change=parseFloat(received-(total_ttc;?>)); $('#change2').html(change.toFixed(2)); } } @@ -60,9 +60,13 @@ function reset() } function Validate(payment){ - parent.$("#poslines").load("invoice.php?place=&action=valid&pay="+payment, function() { - parent.$("#poslines").scrollTop(parent.$("#poslines")[0].scrollHeight); - parent.$.colorbox.close(); + parent.$("#poslines").load("invoice.php?place=&action=valid&pay="+payment, function(response, status, xhr) { + if(status == "error"){ + alert(response); + }else{ + parent.$("#poslines").scrollTop(parent.$("#poslines")[0].scrollHeight); + parent.$.colorbox.close(); + } }); }