From 6ff8e846ada48e9100a3bf9ed02cc040414efe57 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Wed, 30 Oct 2024 17:46:13 +0100 Subject: [PATCH] Debug v21 --- htdocs/compta/bank/various_payment/card.php | 79 ++++++++++------- htdocs/compta/bank/various_payment/list.php | 9 +- htdocs/compta/paiement.php | 12 ++- .../compta/paiement/class/paiement.class.php | 86 +++++++++++++++---- htdocs/langs/en_US/errors.lang | 1 + 5 files changed, 133 insertions(+), 54 deletions(-) diff --git a/htdocs/compta/bank/various_payment/card.php b/htdocs/compta/bank/various_payment/card.php index ac590eba25c..cace48002da 100644 --- a/htdocs/compta/bank/various_payment/card.php +++ b/htdocs/compta/bank/various_payment/card.php @@ -52,7 +52,7 @@ $backtopage = GETPOST('backtopage', 'alpha'); $accountid = GETPOSTINT("accountid") > 0 ? GETPOSTINT("accountid") : 0; $label = GETPOST("label", "alpha"); $sens = GETPOSTINT("sens"); -$amount = GETPOSTFLOAT("amount"); +$amount = GETPOST("amount"); $paymenttype = GETPOST("paymenttype", "aZ09"); $accountancy_code = GETPOST("accountancy_code", "alpha"); $projectid = GETPOSTINT('projectid') ? GETPOSTINT('projectid') : GETPOSTINT('fk_project'); @@ -118,12 +118,14 @@ if (empty($reshook)) { } $object->ref = ''; // TODO - $object->accountid = GETPOSTINT("accountid") > 0 ? GETPOSTINT("accountid") : 0; + $object->fk_account = GETPOSTINT("accountid") > 0 ? GETPOSTINT("accountid") : 0; + $object->accountid = $object->fk_account; $object->datev = $datev; $object->datep = $datep; $object->amount = GETPOSTFLOAT("amount"); $object->label = GETPOST("label", 'restricthtml'); - $object->note = GETPOST("note", 'restricthtml'); + $object->note_private = GETPOST("note", 'restricthtml'); + $object->note = $object->note_private; $object->type_payment = dol_getIdFromCode($db, GETPOST('paymenttype'), 'c_paiement', 'code', 'id', 1); $object->num_payment = GETPOST("num_payment", 'alpha'); $object->chqemetteur = GETPOST("chqemetteur", 'alpha'); @@ -131,7 +133,7 @@ if (empty($reshook)) { $object->fk_user_author = $user->id; $object->category_transaction = GETPOSTINT("category_transaction"); - $object->accountancy_code = GETPOST("accountancy_code") > 0 ? GETPOST("accountancy_code", "alpha") : ""; + $object->accountancy_code = (GETPOST("accountancy_code") != '-1' ? GETPOST("accountancy_code", "alpha") : ""); $object->subledger_account = $subledger_account; $object->sens = GETPOSTINT('sens'); @@ -168,6 +170,21 @@ if (empty($reshook)) { $error++; } + $bankaccount = new Account($db); + $bankaccount->fetch($object->fk_account); + + // Check currency + $currencyofpayment = $conf->currency; // The currency of various payment is not yet asked, so we suppose it is the main company currency + + //var_dump($currencyofpayment); var_dump($bankaccount->currency_code); + + if (isModEnabled('multicurrency') && $currencyofpayment != $bankaccount->currency_code) { + // TODO Support this feature the same way we do it for invoice payment + // using the $value_converted = MultiCurrency::getAmountConversionFromInvoiceRate($key, $value, $way); + setEventMessages($langs->trans("ErrorVariousPaymentOnBankAccountWithADifferentCurrencyNotYetSupported"), null, 'errors'); + $error++; + } + if (!$error) { $db->begin(); @@ -443,7 +460,7 @@ if ($action == 'create') { print ''; print $form->editfieldkey('BankAccount', 'selectaccountid', '', $object, 0, 'string', '', 1).''; print img_picto('', 'bank_account', 'class="pictofixedwidth"'); - print $form->select_comptes($accountid, "accountid", 0, '', 2, '', 0, '', 1); // Show list of main accounts (comptes courants) + print $form->select_comptes($accountid, "accountid", 0, '', 2, '', (isModEnabled('multicurrency') ? 1 : 0), '', 1); // Show list of main accounts (comptes courants) print ''; } @@ -473,6 +490,33 @@ if ($action == 'create') { print ''; } + // Project + if (isModEnabled('project')) { + $formproject = new FormProjets($db); + + // Associated project + $langs->load("projects"); + + print ''.$langs->trans("Project").''; + print img_picto('', 'project', 'class="pictofixedwidth"'); + print $formproject->select_projects(-1, $projectid, 'fk_project', 0, 0, 1, 1, 0, 0, 0, '', 1); + print ''; + } + + // Other attributes + $parameters = array(); + $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook + print $hookmanager->resPrint; + + // Category + if (is_array($options) && count($options) && $conf->categorie->enabled) { + print ''.$langs->trans("RubriquesTransactions").''; + print img_picto('', 'category').Form::selectarray('category_transaction', $options, GETPOST('category_transaction'), 1, 0, 0, '', 0, 0, 0, '', 'minwidth300', 1); + print ''; + } + + print '
'; + // Accountancy account if (isModEnabled('accounting')) { // TODO Remove the fieldrequired and allow instead to edit a various payment to enter accounting code @@ -510,31 +554,6 @@ if ($action == 'create') { print $form->selectarray('sens', $sensarray, $sens, 1, 0, 0, '', 0, 0, 0, '', 'minwidth100', 1); print ''; - // Project - if (isModEnabled('project')) { - $formproject = new FormProjets($db); - - // Associated project - $langs->load("projects"); - - print ''.$langs->trans("Project").''; - print img_picto('', 'bank_account', 'class="pictofixedwidth"'); - print $formproject->select_projects(-1, $projectid, 'fk_project', 0, 0, 1, 1, 0, 0, 0, '', 1); - print ''; - } - - // Other attributes - $parameters = array(); - $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook - print $hookmanager->resPrint; - - // Category - if (is_array($options) && count($options) && $conf->categorie->enabled) { - print ''.$langs->trans("RubriquesTransactions").''; - print img_picto('', 'category').Form::selectarray('category_transaction', $options, GETPOST('category_transaction'), 1, 0, 0, '', 0, 0, 0, '', 'minwidth300', 1); - print ''; - } - print ''; print dol_get_fiche_end(); diff --git a/htdocs/compta/bank/various_payment/list.php b/htdocs/compta/bank/various_payment/list.php index 454f1e66e39..740208ccf4c 100644 --- a/htdocs/compta/bank/various_payment/list.php +++ b/htdocs/compta/bank/various_payment/list.php @@ -780,12 +780,13 @@ while ($i < $imaxinloop) { // Type if ($arrayfields['type']['checked']) { - print ''; + $labeltoshow = ''; if ($obj->payment_code) { - print $langs->trans("PaymentTypeShort".$obj->payment_code); - print ' '; + $labeltoshow = $langs->transnoentitiesnoconv("PaymentTypeShort".$obj->payment_code).' '; } - print $obj->num_payment; + $labeltoshow .= $obj->num_payment; + print ''; + print $labeltoshow; print ''; if (!$i) { $totalarray['nbfield']++; diff --git a/htdocs/compta/paiement.php b/htdocs/compta/paiement.php index 0d7c69e7503..782df704003 100644 --- a/htdocs/compta/paiement.php +++ b/htdocs/compta/paiement.php @@ -282,12 +282,18 @@ if (empty($reshook)) { $error++; } } + /* + var_dump($paiement->amount); + var_dump($paiement->multicurrency_amount); + var_dump($paiement->multicurrency_currency); + */ if (!$error) { $label = '(CustomerInvoicePayment)'; if (GETPOST('type') == Facture::TYPE_CREDIT_NOTE) { $label = '(CustomerInvoicePaymentBack)'; // Refund of a credit note } + $result = $paiement->addPaymentToBank($user, 'payment', $label, GETPOSTINT('accountid'), GETPOST('chqemetteur'), GETPOST('chqbank')); if ($result < 0) { setEventMessages($paiement->error, $paiement->errors, 'errors'); @@ -512,7 +518,7 @@ if ($action == 'create' || $action == 'confirm_paiement' || $action == 'add_paie print ''; print img_picto('', 'bank_account', 'class="pictofixedwidth"'); - print $form->select_comptes($accountid, 'accountid', 0, '', 2, '', 0, 'widthcentpercentminusx maxwidth500', 1); + print $form->select_comptes($accountid, 'accountid', 0, '', 2, '', (isModEnabled('multicurrency') ? 1 : 0), 'widthcentpercentminusx maxwidth500', 1); print ''; } else { print ' '; @@ -745,10 +751,10 @@ if ($action == 'create' || $action == 'confirm_paiement' || $action == 'add_paie if (!empty($conf->use_javascript_ajax)) { print img_picto("Auto fill", 'rightarrow', "class='AutoFillAmount' data-rowname='".$namef."' data-value='".($sign * (float) $multicurrency_remaintopay)."'"); } - print ''; + print ''; print ''; } else { - print ''; + print ''; print ''; } } diff --git a/htdocs/compta/paiement/class/paiement.class.php b/htdocs/compta/paiement/class/paiement.class.php index 3230ce19918..93a2a9abf8a 100644 --- a/htdocs/compta/paiement/class/paiement.class.php +++ b/htdocs/compta/paiement/class/paiement.class.php @@ -98,10 +98,15 @@ class Paiement extends CommonObject public $amount; /** - * @var float Total amount of payment (in the currency of the bank account) + * @var float Total amount of payment (in the currency of the invoice/bank account) */ public $multicurrency_amount; + /** + * @var string The currency of the total of payment (so the currency of the invoice/bank account) + */ + public $multicurrency_currency; + /** * @var array array: invoice ID => amount for that invoice (in the main currency) */ @@ -290,8 +295,9 @@ class Paiement extends CommonObject /** * Create payment of invoices into database. - * Use this->amounts to have list of invoices for the payment. + * It uses this->amounts and ->multicurrency_amounts to get the list of detail of payment for each invoices for the payment. * For payment of a customer invoice, amounts are positive, for payment of credit note, amounts are negative + * This will set also ->amount and ->multicurrency_amount at end. * * @param User $user Object user * @param int $closepaidinvoices 1=Also close paid invoices to paid, 0=Do nothing more @@ -321,9 +327,10 @@ class Paiement extends CommonObject } $currencyofpayment = ''; + $currencyofinvoices = ''; $currencytxofpayment = ''; - foreach ($amounts as $key => $value) { // How payment is dispatched + foreach ($amounts as $key => $value) { // How payment is dispatched. $key is ID of invoice if (empty($value)) { continue; } @@ -335,18 +342,35 @@ class Paiement extends CommonObject $this->error = $langs->trans('FailedToFoundTheConversionRateForInvoice'); return -1; } - if (empty($currencyofpayment)) { - $currencyofpayment = isset($this->multicurrency_code[$key]) ? $this->multicurrency_code[$key] : ""; - } elseif ($currencyofpayment != $this->multicurrency_code[$key]) { + + // Set the currency of the invoice + $currencyofinvoiceforthisline = empty($this->multicurrency_code[$key]) ? $conf->currency : $this->multicurrency_code[$key]; + // If a payment was enter into section of foreign currency of invoice, we want to pay in the currency if invoice + $currencyofpaymentforthisline = empty($this->multicurrency_amounts[$key]) ? $conf->currency : $this->multicurrency_code[$key]; + + //var_dump("Invoice ID: ".$key.", amount in company cur:".$this->amounts[$key]." amount in invoice cur:".$this->multicurrency_amounts[$key]." => currencyofinvoice= ".$currencyofinvoiceforthisline." - currencyofpaymentforthisline =".$currencyofpaymentforthisline); + + if (empty($currencyofinvoices)) { + $currencyofinvoices = $currencyofinvoiceforthisline; + } elseif ($currencyofinvoices != $currencyofinvoiceforthisline) { // If we have invoices with different currencies in the payment, we stop here $this->error = 'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment'; return -1; } + + if (empty($currencyofpayment)) { + $currencyofpayment = $currencyofpaymentforthisline; + } elseif ($currencyofpayment != $currencyofpaymentforthisline) { + // If we have invoices with different currencies in the payment, we stop here + $this->error = 'ErrorYouTryToPayInvoicesWithDifferentCurrenciesInSamePayment'; + return -1; + } + if (empty($currencytxofpayment)) { $currencytxofpayment = isset($this->multicurrency_tx[$key]) ? $this->multicurrency_tx[$key] : ""; } - $totalamount_converted += $value_converted; + $totalamount_converted += $value_converted; // Total in currency of the invoice $amounts_to_update[$key] = price2num($value_converted, 'MT'); $newvalue = price2num($value, 'MT'); @@ -355,6 +379,9 @@ class Paiement extends CommonObject if (!empty($newvalue)) { $atleastonepaymentnotnull++; } + + //var_dump('currencytxofpayment = '.$currencytxofpayment." totalamount_converted =".$totalamount_converted); + //print '
'; } if (empty($currencyofpayment)) { // Should not happen. For the case the multicurrency_code was not saved into invoices @@ -367,14 +394,24 @@ class Paiement extends CommonObject $bankaccount = new Account($this->db); $bankaccount->fetch($this->fk_account); $bankcurrencycode = empty($bankaccount->currency_code) ? $conf->currency : $bankaccount->currency_code; - if ($currencyofpayment != $bankcurrencycode && $currencyofpayment != $conf->currency && $bankcurrencycode != $conf->currency) { - $langs->load("errors"); - $this->error = $langs->trans('ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofpayment, $bankcurrencycode); - return -1; + + if ($bankcurrencycode != $conf->currency) { + // If we try to pay on a bank with a different currency + if ($bankcurrencycode != $currencyofinvoices && $currencyofinvoices != $conf->currency) { + $langs->load("errors"); + $this->error = $langs->trans('ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofinvoices, $bankcurrencycode); + return -1; + } + if ($bankcurrencycode != $currencyofpayment && $currencyofpayment != $conf->currency) { + $langs->load("errors"); + $this->error = $langs->trans('ErrorYouTryToPayInvoicesInACurrencyFromBankWithAnotherCurrency', $currencyofpayment, $bankcurrencycode); + return -1; + } + } else { + // No problem in this case } } - $totalamount = (float) price2num($totalamount); $totalamount_converted = (float) price2num($totalamount_converted); @@ -603,9 +640,12 @@ class Paiement extends CommonObject } if (!$error) { + // Set some properties that may be used by other process after calling the create $this->amount = $total; $this->total = $total; // deprecated $this->multicurrency_amount = $mtotal; + $this->multicurrency_currency = $currencyofinvoices; + $this->db->commit(); return $this->id; } else { @@ -626,8 +666,6 @@ class Paiement extends CommonObject */ public function delete($user, $notrigger = 0) { - $error = 0; - $bank_line_id = $this->bank_line; $this->db->begin(); @@ -730,6 +768,9 @@ class Paiement extends CommonObject $error = 0; $bank_line_id = 0; + // Note: ->amount (amount in company currency) ant multicurrency_amount (amount in )was set by the ->create beforecalling this. + // The create had also checked that currency of payment is same than currency of bank account + if (isModEnabled("bank")) { if ($accountid <= 0) { $this->error = 'Bad value for parameter accountid='.$accountid; @@ -759,9 +800,20 @@ class Paiement extends CommonObject $totalamount = $this->total; // For backward compatibility } - // if dolibarr currency != bank currency then we received an amount in customer currency (currently I don't manage the case : my currency is USD, the customer currency is EUR and he paid me in GBP. Seems no sense for me) - if (isModEnabled('multicurrency') && $conf->currency != $acc->currency_code) { - $totalamount = $this->multicurrency_amount; // We will insert into llx_bank.amount in foreign currency + // this->amount is amount of payment in company currency + // this->multicurrency_amount of payment in other currency + // this->multicurrency_currency is the currency of the payment (may be same than the company one) + if ($this->multicurrency_currency == $conf->currency) { + if ($this->amount != $this->multicurrency_amount) { + // Add protection, should not happen + $error++; + $this->error = 'Payment in same currency than company but this->amount != this->multicurrency_amount'; + } + } + + // if company currency != bank currency, then we received an amount in customer currency (currently I don't manage the case : my currency is USD, the customer currency is EUR and he paid me in GBP. Seems no sense for me) + if ($conf->currency != $acc->currency_code) { + $totalamount = $this->multicurrency_amount; // We will insert into llx_bank.amount in foreign currency of invoice $totalamount_main_currency = $this->amount; // We will also save the amount in main currency into column llx_bank.amount_main_currency } diff --git a/htdocs/langs/en_US/errors.lang b/htdocs/langs/en_US/errors.lang index bcff62c8c80..94e55f82eda 100644 --- a/htdocs/langs/en_US/errors.lang +++ b/htdocs/langs/en_US/errors.lang @@ -344,6 +344,7 @@ ErrorStartHourIsNull=Start date field cannot be empty ErrorTooManyLinesToProcessPleaseUseAMoreSelectiveFilter=Too many lines to process. Please use a more selective filter. ErrorEmptyValueForQty=Quantity cannot be zero. ErrorQtyOrderedLessQtyShipped = The quantity ordered cannot be less than the quantity shipped. +ErrorVariousPaymentOnBankAccountWithADifferentCurrencyNotYetSupported=Error, creating a various payment on a bank account with a currency different than the currency of the company is not yet supported. # Warnings WarningParamUploadMaxFileSizeHigherThanPostMaxSize=Your PHP parameter upload_max_filesize (%s) is higher than PHP parameter post_max_size (%s). This is not a consistent setup. WarningPasswordSetWithNoAccount=A password was set for this member. However, no user account was created. So this password is stored but can't be used to login to Dolibarr. It may be used by an external module/interface but if you don't need to define any login nor password for a member, you can disable option "Manage a login for each member" from Member module setup. If you need to manage a login but don't need any password, you can keep this field empty to avoid this warning. Note: Email can also be used as a login if the member is linked to a user.