improves the simplified payment

MAIN_JS_ON_PAYMENT must be set
This commit is contained in:
Cyrille de Lambert 2011-07-27 08:00:45 +00:00
parent 32597f0e9b
commit 26e315c785
4 changed files with 60 additions and 29 deletions

View File

@ -35,49 +35,74 @@ if (! defined('NOREQUIREHTML')) define('NOREQUIREHTML','1'); // If we don't nee
require('../main.inc.php'); require('../main.inc.php');
//print_r($_POST); $langs->Load('compta');
//init var
$amountPayment = $_POST['amountPayment'];
$amounts = $_POST['amounts']; // from text inputs : invoice amount payment (check required)
$remains = $_POST['remains']; // from dolibarr's object (no need to check)
$currentInvId = $_POST['imgClicked']; // from DOM elements : imgId (equals invoice id)
// Getting the posted keys=>values, sanitize the ones who are from text inputs // Getting the posted keys=>values, sanitize the ones who are from text inputs
// from text inputs : total amount // from text inputs : total amount
$amountPayment = price2num($_POST['amountPayment']); $amountPayment = $amountPayment!='' ? ( is_numeric(price2num($amountPayment)) ? price2num($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) : ''; // keep void if not a valid entry
// Checkamounts
foreach ($amounts as $key => $value) foreach ($amounts as $key => $value)
{ {
$value = price2num($value); $value = price2num($value);
if (!is_numeric($value)) unset($amounts[$key]); 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 // Treatment
$result = $amountPayment - array_sum($amounts); // Remaining amountPayment $result = $amountPayment != '' ? $amountPayment - array_sum($amounts) : $amountPayment + array_sum($amounts); // Remaining amountPayment
$toJsonArray = array(); $toJsonArray = array();
$totalRemaining = price2num(array_sum($remains));
$toJsonArray['label'] = $amountPayment == '' ? $langs->transnoentities('AmountToBeCharged') : $langs->transnoentities('RemainingAmountPayment');
if($currentInvId) // Here to breakdown if($currentInvId) // Here to breakdown
{ {
// Get the current amount (from form) and the corresponding remainToPay (from invoice) // Get the current amount (from form) and the corresponding remainToPay (from invoice)
$currentAmount = $amounts['amount_'.$currentInvId]; $currentAmount = $amounts['amount_'.$currentInvId];
$currentRemain = $remains['remain_'.$currentInvId]; $currentRemain = $remains['remain_'.$currentInvId];
// Reset the substraction for this amount // If amountPayment isn't filled, breakdown invoice amount, else breakdown from amountPayment
$result += price2num($currentAmount); if($amountPayment == '')
$currentAmount = 0;
if($result >= 0) // then we need to calculate the amount to breakdown
{ {
$amountToBreakdown = ($result - $currentRemain >= 0 ? // Check if current amount exists in amounts
$currentRemain : // Remain can be fully paid $amountExists = array_key_exists('amount_'.$currentInvId,$amounts);
$currentRemain + ($result - $currentRemain)); // Remain can only partially be paid if($amountExists)
$currentAmount = $amountToBreakdown; // In both cases, amount will take breakdown value {
$result -= $amountToBreakdown; // And canceled substraction has been replaced by breakdown $remainAmount = $currentRemain - $currentAmount; // To keep value between curRemain and curAmount
} // else there's no need to calc anything, just reset the field (result is still < 0) $result += $remainAmount; // result must be deduced by
$currentAmount += $remainAmount; // curAmount put to curRemain
}else
{
$currentAmount = $currentRemain;
$result += $currentRemain;
}
}else
{
// 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 $toJsonArray['amount_'.$currentInvId] = price2num($currentAmount).""; // Param will exist only if an img has been clicked
} }
// Encode to JSON to return // Encode to JSON to return
$toJsonArray['result'] = price2num($result).""; $toJsonArray['makeRed'] = $totalRemaining < price2num($result) || price2num($result) < 0 ? true : false;
echo json_encode($toJsonArray); // Printing the call's result $toJsonArray['result'] = price2num($result);
echo json_encode($toJsonArray); // Printing the call's result
?> ?>

View File

@ -24,7 +24,7 @@
* \file htdocs/compta/paiement.php * \file htdocs/compta/paiement.php
* \ingroup compta * \ingroup compta
* \brief Page to create a payment * \brief Page to create a payment
* \version $Id: paiement.php,v 1.111 2011/07/13 08:57:21 eldy Exp $ * \version $Id: paiement.php,v 1.112 2011/07/27 08:00:45 cdelambert Exp $
*/ */
require('../main.inc.php'); require('../main.inc.php');
@ -290,6 +290,7 @@ if ($action == 'create' || $action == 'confirm_paiement' || $action == 'add_paie
json["amountPayment"] = jQuery("#amountpayment").attr("value"); json["amountPayment"] = jQuery("#amountpayment").attr("value");
json["amounts"] = elemToJson(form.find("input[name*=\"amount_\"]")); json["amounts"] = elemToJson(form.find("input[name*=\"amount_\"]"));
json["remains"] = elemToJson(form.find("input[name*=\"remain_\"]")); json["remains"] = elemToJson(form.find("input[name*=\"remain_\"]"));
if(imgId != null)json["imgClicked"] = imgId; if(imgId != null)json["imgClicked"] = imgId;
jQuery.post("ajaxpayment.php", json, function(data) jQuery.post("ajaxpayment.php", json, function(data)
@ -300,13 +301,14 @@ if ($action == 'create' || $action == 'confirm_paiement' || $action == 'add_paie
for(var key in json) for(var key in json)
{ {
if(key == "result") { if(key == "result") {
jQuery("#"+key).text(json[key]); if(json["makeRed"]) {
if(json[key] < 0) {
jQuery("#"+key).css("color", "red"); jQuery("#"+key).css("color", "red");
} else { } else {
jQuery("#"+key).removeAttr("style"); jQuery("#"+key).removeAttr("style");
} }
json[key]=json["label"]+" "+json[key];
jQuery("#"+key).text(json[key]);
} else { } else {
form.find("input[name*=\""+key+"\"]").each(function() { form.find("input[name*=\""+key+"\"]").each(function() {
jQuery(this).attr("value", json[key]); jQuery(this).attr("value", json[key]);
@ -671,5 +673,5 @@ if (! GETPOST('action'))
$db->close(); $db->close();
llxFooter('$Date: 2011/07/13 08:57:21 $ - $Revision: 1.111 $'); llxFooter('$Date: 2011/07/27 08:00:45 $ - $Revision: 1.112 $');
?> ?>

View File

@ -13,6 +13,8 @@ OptionModeVirtualDesc=In this context, the turnover is calculated over invoices
FeatureIsSupportedInInOutModeOnly=Feature only available in CREDITS-DEBTS accountancy mode (See Accountancy module configuration) FeatureIsSupportedInInOutModeOnly=Feature only available in CREDITS-DEBTS accountancy mode (See Accountancy module configuration)
VATReportBuildWithOptionDefinedInModule=Amounts shown here are calculated using rules defined by Tax module setup. VATReportBuildWithOptionDefinedInModule=Amounts shown here are calculated using rules defined by Tax module setup.
Param=Setup Param=Setup
RemainingAmountPayment=Amount payment remaining :
AmountToBeCharged=Total amount to pay :
AccountsGeneral=Accounts AccountsGeneral=Accounts
Account=Account Account=Account
Accounts=Accounts Accounts=Accounts

View File

@ -13,6 +13,8 @@ OptionModeVirtualDesc=Dans ce mode, le CA est calculé sur la base des factures
FeatureIsSupportedInInOutModeOnly=Fonction disponible uniquement en mode compta CREANCES-DETTES (Voir configuration du module compta) FeatureIsSupportedInInOutModeOnly=Fonction disponible uniquement en mode compta CREANCES-DETTES (Voir configuration du module compta)
VATReportBuildWithOptionDefinedInModule=Les montants obtenus sont ceux calculés à partir des règles de calcul paramétrées pour le module Taxes. VATReportBuildWithOptionDefinedInModule=Les montants obtenus sont ceux calculés à partir des règles de calcul paramétrées pour le module Taxes.
Param=Paramétrage Param=Paramétrage
RemainingAmountPayment=Montant restant du paiement :
AmountToBeCharged=Montant total à payer :
AccountsGeneral=Comptes généraux AccountsGeneral=Comptes généraux
Account=Compte Account=Compte
Accounts=Comptes Accounts=Comptes