From ed81d35e38c22309c6ddfe5132fc849267c15ec9 Mon Sep 17 00:00:00 2001 From: David Beniamine Date: Fri, 22 Mar 2019 09:06:44 +0100 Subject: [PATCH] Allow negative bills --- htdocs/takepos/invoice.php | 34 ++++++++++++++++++++++++++++++++++ htdocs/takepos/pay.php | 14 +++++++++----- 2 files changed, 43 insertions(+), 5 deletions(-) diff --git a/htdocs/takepos/invoice.php b/htdocs/takepos/invoice.php index 45a65af3f94..f8a51849412 100644 --- a/htdocs/takepos/invoice.php +++ b/htdocs/takepos/invoice.php @@ -41,6 +41,20 @@ $idline = GETPOST('idline'); $desc = GETPOST('desc', 'alpha'); $pay = GETPOST('pay'); +/** + * Abort invoice creationg with a given error message + * + * @param string $message Message explaining the error to the user + * @return void + */ +function fail($message) +{ + header($_SERVER['SERVER_PROTOCOL'] . ' 500 Internal Server Error', true, 500); + die($message); +} + + + $placeid = 0; // $placeid is id of invoice $invoice = new Facture($db); @@ -62,6 +76,26 @@ if ($action == 'valid' && $user->rights->facture->creer) $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); diff --git a/htdocs/takepos/pay.php b/htdocs/takepos/pay.php index 33524b80a8c..7f8b300bb8b 100644 --- a/htdocs/takepos/pay.php +++ b/htdocs/takepos/pay.php @@ -66,7 +66,7 @@ $langs->loadLangs(array("main", "bills", "cashdesk")); $('#change1').html(parseFloat(received).toFixed(2)); if (parseFloat(received)>total_ttc;?>) { - var change=parseFloat(parseFloat(received)-total_ttc;?>); + var change=parseFloat(parseFloat(received)-(total_ttc;?>)); $('#change2').html(change.toFixed(2)); } } @@ -79,10 +79,14 @@ $langs->loadLangs(array("main", "bills", "cashdesk")); } 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(); + } + }); }