diff --git a/htdocs/compta/ajaxpayment.php b/htdocs/compta/ajaxpayment.php new file mode 100644 index 00000000000..e52efc22fac --- /dev/null +++ b/htdocs/compta/ajaxpayment.php @@ -0,0 +1,70 @@ + + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +/** + * \file htdocs/compta/ajaxpayment.php + * \brief File to return Ajax response on payment breakdown process + * \version ajaxpayment.php,v 1.0 + */ + +require('../main.inc.php'); + +// Getting the posted keys=>values, sanitize the ones who are from text inputs + // from text inputs : total amount +$amountPayment = price2num($_POST['amountPayment']); +$amountPayment = is_numeric($amountPayment)? $amountPayment : 0; // is a value + // from text inputs : invoice amount payment +$amounts = $_POST['amounts']; // is an array (need a foreach) +foreach ($amounts as $key => &$value) +{ + $value = price2num($value); + if(!is_numeric($value))unset($amounts[$key]); +} + // from dolibarr's object (no need to check) +$remains = $_POST['remains']; + // from DOM elements : imgId (equals invoice id) +$currentInvId = $_POST['imgClicked']; + + +// Treatment +$result = $amountPayment - array_sum($amounts); // Remaining amountPayment +$toJsonArray = array(); +if($currentInvId) // Here to breakdown +{ + // Get the current amount (from form) and the corresponding remainToPay (from invoice) + $currentAmount = $amounts['amount_'.$currentInvId]; + $currentRemain = $remains['remain_'.$currentInvId]; + + // Reset the substraction for this amount + $result += price2num($currentAmount); + $currentAmount = 0; + if($result >= 0) // then we need to calculate the amount to breakdown + { + $amountToBreakdown = ($result - $currentRemain >= 0 ? + $currentRemain : // Remain can be fully paid + $currentRemain + ($result - $currentRemain)); // Remain can only partially be paid + $currentAmount = $amountToBreakdown; // In both cases, amount will take breakdown value + $result -= $amountToBreakdown; // And canceled substraction has been replaced by breakdown + } // else there's no need to calc anything, just reset the field (result is still < 0) + $toJsonArray['amount_'.$currentInvId] = price2num($currentAmount).""; // Param will exist only if an img has been clicked +} +// Encode to JSON to return +$toJsonArray['result'] = price2num($result).""; +echo json_encode($toJsonArray); // Printing the call's result + +?> \ No newline at end of file diff --git a/htdocs/compta/paiement.php b/htdocs/compta/paiement.php index 96203d9c39f..9c37d7b3b22 100644 --- a/htdocs/compta/paiement.php +++ b/htdocs/compta/paiement.php @@ -24,7 +24,7 @@ * \file htdocs/compta/paiement.php * \ingroup compta * \brief Page to create a payment - * \version $Id$ + * \version $Id: paiement.php,v 1.107 2011/07/01 15:19:32 cdelambert Exp $ */ require('../main.inc.php'); @@ -253,7 +253,6 @@ if ($action == 'create' || $action == 'confirm_paiement' || $action == 'add_paie if (! empty($conf->global->PAYPAL_BANK_ACCOUNT)) $accountid=$conf->global->PAYPAL_BANK_ACCOUNT; $paymentnum=$facture->ref_int; } - if ($conf->use_javascript_ajax) { print "\n".''."\n"; + }); + function elemToJson(selector) + { + var subJson = {}; + jQuery.map(selector.serializeArray(), function(n,i) + { + subJson[n["name"]] = n["value"]; + }); + return subJson; + } + function callForResult(imgId) + { + var json = {}; + var form = jQuery("#payment_form"); + + json["amountPayment"] = jQuery("#amountpayment").attr("value"); + json["amounts"] = elemToJson(form.find("input[name*=\"amount_\"]")); + json["remains"] = elemToJson(form.find("input[name*=\"remain_\"]")); + if(imgId != null)json["imgClicked"] = imgId; + + jQuery.post("ajaxpayment.php", json, function(data) + { + json = jQuery.parseJSON(data); + + form.data(json); + + for(var key in json) + { + if(key == "result") + { + jQuery("#"+key).text(json[key]); + if(json[key] < 0) + jQuery("#"+key).css("color", "red"); + else + jQuery("#"+key).removeAttr("style"); + }else + { + form.find("input[name*=\""+key+"\"]").each(function() + { + jQuery(this).attr("value", json[key]); + }); + } + } + }); + + } + function callToBreakdown(imgSelector) + { + var form = jQuery("#payment_form"), imgId; + + imgId = imgSelector.attr("id"); + callForResult(imgId); + } + jQuery(document).ready(function () + { + + jQuery("#payment_form").find("img").click(function() + { + callToBreakdown(jQuery(this)); + }); + + jQuery("#payment_form").find("input[name*=\"amount_\"]").change(function() + { + callForResult(); + }); + + jQuery("#amountpayment").change(function() + { + callForResult(); + }); + }); + '."\n"; } print '
'; @@ -307,7 +367,7 @@ if ($action == 'create' || $action == 'confirm_paiement' || $action == 'add_paie print ''.$langs->trans('Comments').''; $rowspan=5; - if (! empty($conf->global->MAIN_JS_ON_PAYMENT)) $rowspan++; + if ($conf->use_javascript_ajax) $rowspan++; // Payment mode print ''.$langs->trans('PaymentMode').''; @@ -318,7 +378,7 @@ if ($action == 'create' || $action == 'confirm_paiement' || $action == 'add_paie print ''; // Payment amount - if (! empty($conf->global->MAIN_JS_ON_PAYMENT)) + if ($conf->use_javascript_ajax) { print ''.$langs->trans('AmountPayment').''; print ''; @@ -407,115 +467,7 @@ if ($action == 'create' || $action == 'confirm_paiement' || $action == 'add_paie print ''.$langs->trans('RemainderToPay').''; print ''.$langs->trans('PaymentAmount').''; print ' '; - print "\n"; - - // FIXME Does not work yet because : - // - Does not support non , and non . decimal separator. - // - Does not use dolibarr param rounding - // - Other minor bugs - if (! empty($conf->global->MAIN_JS_ON_PAYMENT)) - { - print ''; - } + print "\n"; $var=True; $total=0; @@ -563,8 +515,8 @@ if ($action == 'create' || $action == 'confirm_paiement' || $action == 'add_paie // Add remind amount $namef = 'amount_'.$objp->facid; + $nameRemain = 'remain_'.$objp->facid; - $namef = 'amount_'.$objp->facid; if ($action != 'add_paiement') { if (! empty($conf->global->MAIN_JS_ON_PAYMENT)) @@ -614,7 +566,7 @@ if ($action == 'create' || $action == 'confirm_paiement' || $action == 'add_paie if ($totalrecudeposits) print '+'.price($totalrecudeposits); print ''; print ''.price(price2num($total_ttc - $totalrecu - $totalrecucreditnote - $totalrecudeposits,'MT')).''; - print ''; + print ''; print ' '; print "\n"; } @@ -730,5 +682,5 @@ if (! GETPOST('action')) $db->close(); -llxFooter('$Date$ - $Revision$'); +llxFooter('$Date: 2011/07/01 15:19:32 $ - $Revision: 1.107 $'); ?>