Modify règlement

This commit is contained in:
cdelambert 2011-07-01 15:19:32 +00:00
parent 65861cd356
commit 7dfeda5023
2 changed files with 149 additions and 127 deletions

View File

@ -0,0 +1,70 @@
<?php
/* Copyright (C) 2011 Auguria <anthony.poiret@auguria.net>
*
* 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
?>

View File

@ -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".'<script type="text/javascript" language="javascript">';
@ -272,17 +271,78 @@ if ($action == 'create' || $action == 'confirm_paiement' || $action == 'add_paie
{
jQuery(\'.fieldrequireddyn\').removeClass(\'fieldrequired\');
}
});';
print 'jQuery("#amountpayment").keyup(function() {
alter_amount_payment();
});';
print 'jQuery("#amountpayment").change(function() {
alter_amount_payment();
});';
print 'alter_amount_payment();';
print '});';
print '</script>'."\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();
});
});
</script>'."\n";
}
print '<form name="add_paiement" action="'.$_SERVER["PHP_SELF"].'" method="POST">';
@ -307,7 +367,7 @@ if ($action == 'create' || $action == 'confirm_paiement' || $action == 'add_paie
print '<td>'.$langs->trans('Comments').'</td></tr>';
$rowspan=5;
if (! empty($conf->global->MAIN_JS_ON_PAYMENT)) $rowspan++;
if ($conf->use_javascript_ajax) $rowspan++;
// Payment mode
print '<tr><td><span class="fieldrequired">'.$langs->trans('PaymentMode').'</span></td><td>';
@ -318,7 +378,7 @@ if ($action == 'create' || $action == 'confirm_paiement' || $action == 'add_paie
print '</tr>';
// Payment amount
if (! empty($conf->global->MAIN_JS_ON_PAYMENT))
if ($conf->use_javascript_ajax)
{
print '<tr><td><span class="fieldrequired">'.$langs->trans('AmountPayment').'</span></td>';
print '<td>';
@ -407,115 +467,7 @@ if ($action == 'create' || $action == 'confirm_paiement' || $action == 'add_paie
print '<td align="right">'.$langs->trans('RemainderToPay').'</td>';
print '<td align="right">'.$langs->trans('PaymentAmount').'</td>';
print '<td align="right">&nbsp;</td>';
print "</tr>\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 '<script type="text/javascript" language="javascript">
// This Array is used to get and store Array containing (String format):
// [0]-> the $namef of the linked text field (in order to get and set ElementsById)
// [1]-> the linked invoice amount
// [2]-> the linked amount payed (in order to calculate the remaining payment amount)
var amountInput = new Array();
var totalAmount = 0;
var paymentRemaining = 0;
// This function is used to round floats to monetary format
function roundNumber(num,dec) {
var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
return result;
}
// This function is used to parse php Strings(floats with coma) to javascript floats (with stops)
function convertFloat(str)
{
return parseFloat(str.replace(/,/, \'.\').split(\' \').join(\'\'));
}
// This function is used to convert javascript floats (stored as String) to coma floats (as String too)
function toComaStr(str)
{
return str.replace(/\./, ",");
}
// This function is called onclick of the buttons linked to the invoices. It refreshes the linked payed amount
// contained by the corresponding array, and the text field linked to the corresponding invoice
function calcRemind(index)
{
invoiceAmount = convertFloat(amountInput[index][1]);
var payedAmount = amountInput[index][2] != 0 ? convertFloat(amountInput[index][2]) : 0;
paymentRemaining = paymentRemaining + payedAmount;
payedAmount = 0;
if(paymentRemaining - invoiceAmount < 0)
{
payedAmount = paymentRemaining;
paymentRemaining = 0;
}else
{
paymentRemaining -= invoiceAmount;
payedAmount = invoiceAmount;
}
if(payedAmount < 0)
{
paymentRemaining += payedAmount;
payedAmount = 0;
}
amountInput[index][2] = payedAmount.toString();
document.getElementById(amountInput[index][0]).value = toComaStr(roundNumber(payedAmount,2).toString());
changeSumAmount();
}
// This function is called when the payment\'s amount is change. It refreshes the array containing
// the amounts payed (from the text fields linked to invoices)
function alter_amount_payment()
{
totalAmount = document.add_paiement.amountpayment.value != 0 ? convertFloat(document.add_paiement.amountpayment.value) : 0;
totalAmount = isNaN(totalAmount) ? 0 : totalAmount;
paymentRemaining = totalAmount;
for(var ii=0; ii<amountInput.length; ii++)
{
if(isNaN(convertFloat(document.getElementById(amountInput[ii][0]).value)))
amountInput[ii][2] = 0;
else
paymentRemaining -= convertFloat(document.getElementById(amountInput[ii][0]).value);
}
changeSumAmount();
}
// This function is called when a change occurs on the text fields link to the invoices amount
// to pay
function changeAmount(index)
{
var oldAmount = amountInput[index][2];
var newAmount = isNaN(document.getElementById(amountInput[index][0]).value) ? 0 : document.getElementById(amountInput[index][0]).value;
paymentRemaining += oldAmount - newAmount ;
amountInput[index][2] = newAmount;
changeSumAmount();
}
// This function is called when any change occurs on the amounts objects to print the remaining
// amount of the payment to divide into invoices amount
function changeSumAmount(t)
{
var dispatchRemaining = totalAmount;
for(var ii = 0; ii < amountInput.length; ii++)
{
dispatchRemaining -= amountInput[ii][2] != 0 ? convertFloat(amountInput[ii][2]) : 0;
}
if(dispatchRemaining < 0)
document.getElementById("amount_sum_payment").style.setProperty("color","#ff0000","");
else
document.getElementById("amount_sum_payment").style.removeProperty("color");
document.getElementById("amount_sum_payment").innerHTML= "'.$langs->trans('RemainToDivide').' "+toComaStr(roundNumber(dispatchRemaining,2).toString());
}
</script>';
}
print "</tr>\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 '</b></td>';
print '<td align="right"><b>'.price(price2num($total_ttc - $totalrecu - $totalrecucreditnote - $totalrecudeposits,'MT')).'</b></td>';
print '<td align="right" id="amount_sum_payment" style="font-weight:bold;"></td>';
print '<td align="right" id="result" style="font-weight:bold;"></td>';
print '<td align="center">&nbsp;</td>';
print "</tr>\n";
}
@ -730,5 +682,5 @@ if (! GETPOST('action'))
$db->close();
llxFooter('$Date$ - $Revision$');
llxFooter('$Date: 2011/07/01 15:19:32 $ - $Revision: 1.107 $');
?>