From 3e0df45f30a155b8fc3af465634b587caff4fa96 Mon Sep 17 00:00:00 2001 From: Anthony Damhet <73399671+EchoLoGeek@users.noreply.github.com> Date: Thu, 24 Jul 2025 11:53:11 +0200 Subject: [PATCH] NEW | Discount split more than two parts (#34782) * Discount split more than two parts * fix spaces * remove set action split and type variables * Condition if javascript is disabled * Update remx.php --------- Co-authored-by: Laurent Destailleur --- htdocs/comm/remx.php | 415 ++++++++++++++++++++++++++++++++-- htdocs/langs/en_US/bills.lang | 4 + htdocs/langs/fr_FR/bills.lang | 4 + 3 files changed, 403 insertions(+), 20 deletions(-) diff --git a/htdocs/comm/remx.php b/htdocs/comm/remx.php index 24f2baf1c8a..b8552a13a5e 100644 --- a/htdocs/comm/remx.php +++ b/htdocs/comm/remx.php @@ -4,6 +4,7 @@ * Copyright (C) 2008 Raphael Bertrand (Resultic) * Copyright (C) 2019-2024 Frédéric France * Copyright (C) 2024-2025 MDW + * Copyright (C) 2025 Anthony Damhet * * 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 @@ -52,6 +53,7 @@ $id = GETPOSTINT('id'); $action = GETPOST('action', 'aZ09'); $backtopage = GETPOST('backtopage', 'alpha'); +$splitamounts = GETPOST('splitamounts', 'array'); // Security check $socid = GETPOSTINT('id') ? GETPOSTINT('id') : GETPOSTINT('socid'); @@ -68,8 +70,6 @@ $result = restrictedArea($user, 'societe', $id, '&societe', '', 'fk_soc', 'rowid $permissiontocreate = ($user->hasRight('societe', 'creer') || $user->hasRight('facture', 'creer')); - - /* * Actions */ @@ -79,6 +79,87 @@ if (GETPOST('cancel', 'alpha') && !empty($backtopage)) { exit; } +if ($action == 'confirm_split_more' && $permissiontocreate) { + $error = 0; + $remid = (GETPOSTINT("remid") ? GETPOSTINT("remid") : 0); + $discount = new DiscountAbsolute($db); + $res = $discount->fetch($remid); + if (!($res > 0)) { + $error++; + setEventMessages($langs->trans("ErrorFailedToLoadDiscount"), null, 'errors'); + } + if (empty($splitamounts)) { + $error++; + setEventMessages($langs->trans("TotalOfDiscountMustEqualsOriginal"), null, 'errors'); + } + if (!$error) { + $totalsplitted = 0; + foreach ($splitamounts as $key => $value) { + $totalsplitted += (float) $value; + } + if ($totalsplitted != (float) $discount->amount_ttc) { + $error++; + setEventMessages($langs->trans("TotalOfDiscountMustEqualsOriginal"), null, 'errors'); + } + } + + if (!$error) { + $db->begin(); + + foreach ($splitamounts as $key => $value) { + if ((float) $value == 0) { + continue; + } + // + $newdiscount = new DiscountAbsolute($db); + $newdiscount->fk_facture_source = $discount->fk_facture_source; + $newdiscount->fk_facture = $discount->fk_facture; + $newdiscount->fk_facture_line = $discount->fk_facture_line; + $newdiscount->fk_invoice_supplier_source = $discount->fk_invoice_supplier_source; + $newdiscount->fk_invoice_supplier = $discount->fk_invoice_supplier; + $newdiscount->fk_invoice_supplier_line = $discount->fk_invoice_supplier_line; + if ($discount->description == '(CREDIT_NOTE)' || $discount->description == '(DEPOSIT)') { + $newdiscount->description = $discount->description; + } else { + $newdiscount->description = $discount->description.' (1)'; + } + $newdiscount->fk_user = $discount->fk_user; + $newdiscount->fk_soc = $discount->fk_soc; + $newdiscount->socid = $discount->socid; + $newdiscount->discount_type = $discount->discount_type; + $newdiscount->datec = $discount->datec; + $newdiscount->tva_tx = $discount->tva_tx; + $newdiscount->vat_src_code = $discount->vat_src_code; + $newdiscount->amount_ttc = price2num($value); + $newdiscount->amount_ht = price2num($newdiscount->amount_ttc / (1 + $newdiscount->tva_tx / 100), 'MT'); + $newdiscount->amount_tva = price2num($newdiscount->amount_ttc - $newdiscount->amount_ht); + $newdiscount->multicurrency_amount_ttc = (float) $value * ($discount->multicurrency_amount_ttc / $discount->amount_ttc); + $newdiscount->multicurrency_amount_ht = price2num($newdiscount->multicurrency_amount_ttc / (1 + $newdiscount->tva_tx / 100), 'MT'); + $newdiscount->multicurrency_amount_tva = price2num($newdiscount->multicurrency_amount_ttc - $newdiscount->multicurrency_amount_ht); + + $newdiscountID = $newdiscount->create($user); + if (!$newdiscountID) { + $error++; + } + } + + if (!$error) { + $discount->fk_facture_source = 0; + $discount->fk_invoice_supplier_source = 0; + $res = $discount->delete($user); + if ($res > 0) { + $db->commit(); + header("Location: ".$_SERVER["PHP_SELF"].'?id='.$id.($backtopage ? '&backtopage='.urlencode($backtopage) : '')); // To avoid pb with back + exit; + } else { + $db->rollback(); + } + } else { + $db->rollback(); + } + } +} + if ($action == 'confirm_split' && GETPOST("confirm", "alpha") == 'yes' && $permissiontocreate) { $amount_ttc_1 = GETPOST('amount_ttc_1', 'alpha'); $amount_ttc_1 = price2num($amount_ttc_1); @@ -570,15 +651,162 @@ if ($socid > 0) { print ''; if (count($showconfirminfo)) { - $amount1 = price2num($showconfirminfo['amount_ttc'] / 2, 'MT'); - $amount2 = ($showconfirminfo['amount_ttc'] - (float) $amount1); - $formquestion = array( - 'text' => $langs->trans('TypeAmountOfEachNewDiscount'), - 0 => array('type' => 'text', 'name' => 'amount_ttc_1', 'label' => $langs->trans("AmountTTC").' 1', 'value' => $amount1, 'size' => '5'), - 1 => array('type' => 'text', 'name' => 'amount_ttc_2', 'label' => $langs->trans("AmountTTC").' 2', 'value' => $amount2, 'size' => '5') - ); - $langs->load("dict"); - print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&remid='.$showconfirminfo['rowid'].($backtopage ? '&backtopage='.urlencode($backtopage) : ''), $langs->trans('SplitDiscount'), $langs->trans('ConfirmSplitDiscount', price($showconfirminfo['amount_ttc']), $langs->transnoentities("Currency".$conf->currency)), 'confirm_split', $formquestion, '', 0); + if (getDolGlobalInt('DISCOUNT_SPLIT_MORE_THAN_TWO_PARTS') && !getDolGlobalInt('MAIN_DISABLE_JAVASCRIPT')) { + print '
'; + + // + print ''; + print ''; + + print '
'; + print ''; + print ''; + + // Title + print ''; + print ''; + print ''; + + print ''; + print ''; + print ''; + + // + if (empty($splitamounts)) { + $amount1 = price2num($showconfirminfo['amount_ttc'] / 2, 'MT'); + $amount2 = ($showconfirminfo['amount_ttc'] - (float) $amount1); + $remaintosplit = (float) $showconfirminfo['amount_ttc'] - ((float) $amount1 + (float) $amount2); + + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + } else { + $nbSplitLines = count($splitamounts); + $remaintosplit = $showconfirminfo['amount_ttc']; + foreach ($splitamounts as $numero => $value) { + $remaintosplit -= (float) $value; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + } + } + + print ''; + print ''; + print ''; + print ''; + print ''; + + print ''; + print ''; + print ''; + + // + print ''; + print ''; + print ''; + print ''; + print ''; + + print ''; + print ''; + print ''; + print ''; + print ''; + + print ''; + print '
'.img_picto('', 'split', '', 0, 0, 0, '', 'paddingright').' '.$langs->trans('SplitDiscountTitle').'
'.$langs->trans('TypeAmountOfEachNewDiscountSplit').'
'.$langs->trans('AmountTTC').' 1:
'.$langs->trans('AmountTTC').' 2:
'.$langs->trans('AmountTTC').' '.$numero.':
 
'.$langs->trans('RemainToSplit').':'.price($remaintosplit).' '.$conf->currency.'
'; + print '
'; + print '
'; + print '
'; + ?> + + $langs->trans('TypeAmountOfEachNewDiscount'), + 0 => array('type' => 'text', 'name' => 'amount_ttc_1', 'label' => $langs->trans("AmountTTC").' 1', 'value' => $amount1, 'size' => '5'), + 1 => array('type' => 'text', 'name' => 'amount_ttc_2', 'label' => $langs->trans("AmountTTC").' 2', 'value' => $amount2, 'size' => '5') + ); + $langs->load("dict"); + print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&remid='.$showconfirminfo['rowid'].($backtopage ? '&backtopage='.urlencode($backtopage) : ''), $langs->trans('SplitDiscount'), $langs->trans('ConfirmSplitDiscount', price($showconfirminfo['amount_ttc']), $langs->transnoentities("Currency".$conf->currency)), 'confirm_split', $formquestion, '', 0); + } } } else { dol_print_error($db); @@ -717,15 +945,162 @@ if ($socid > 0) { print ''; if (count($showconfirminfo)) { - $amount1 = price2num($showconfirminfo['amount_ttc'] / 2, 'MT'); - $amount2 = ($showconfirminfo['amount_ttc'] - (float) $amount1); - $formquestion = array( - 'text' => $langs->trans('TypeAmountOfEachNewDiscount'), - 0 => array('type' => 'text', 'name' => 'amount_ttc_1', 'label' => $langs->trans("AmountTTC").' 1', 'value' => $amount1, 'size' => '5'), - 1 => array('type' => 'text', 'name' => 'amount_ttc_2', 'label' => $langs->trans("AmountTTC").' 2', 'value' => $amount2, 'size' => '5') - ); - $langs->load("dict"); - print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&remid='.$showconfirminfo['rowid'].($backtopage ? '&backtopage='.urlencode($backtopage) : ''), $langs->trans('SplitDiscount'), $langs->trans('ConfirmSplitDiscount', price($showconfirminfo['amount_ttc']), $langs->transnoentities("Currency".$conf->currency)), 'confirm_split', $formquestion, 0, 0); + if (getDolGlobalInt('DISCOUNT_SPLIT_MORE_THAN_TWO_PARTS') && !getDolGlobalInt('MAIN_DISABLE_JAVASCRIPT')) { + print '
'; + + // + print ''; + print ''; + + print '
'; + print ''; + print ''; + + // Title + print ''; + print ''; + print ''; + + print ''; + print ''; + print ''; + + // + if (empty($splitamounts)) { + $amount1 = price2num($showconfirminfo['amount_ttc'] / 2, 'MT'); + $amount2 = ($showconfirminfo['amount_ttc'] - (float) $amount1); + $remaintosplit = (float) $showconfirminfo['amount_ttc'] - ((float) $amount1 + (float) $amount2); + + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + } else { + $nbSplitLines = count($splitamounts); + $remaintosplit = $showconfirminfo['amount_ttc']; + foreach ($splitamounts as $numero => $value) { + $remaintosplit -= (float) $value; + print ''; + print ''; + print ''; + print ''; + print ''; + print ''; + } + } + + print ''; + print ''; + print ''; + print ''; + print ''; + + print ''; + print ''; + print ''; + + // + print ''; + print ''; + print ''; + print ''; + print ''; + + print ''; + print ''; + print ''; + print ''; + print ''; + + print ''; + print '
'.img_picto('', 'split', '', 0, 0, 0, '', 'paddingright').' '.$langs->trans('SplitDiscountTitle').'
'.$langs->trans('TypeAmountOfEachNewDiscountSplit').'
'.$langs->trans('AmountTTC').' 1:
'.$langs->trans('AmountTTC').' 2:
'.$langs->trans('AmountTTC').' '.$numero.':
 
'.$langs->trans('RemainToSplit').':'.price($remaintosplit).' '.$conf->currency.'
'; + print '
'; + print '
'; + print '
'; + ?> + + $langs->trans('TypeAmountOfEachNewDiscount'), + 0 => array('type' => 'text', 'name' => 'amount_ttc_1', 'label' => $langs->trans("AmountTTC").' 1', 'value' => $amount1, 'size' => '5'), + 1 => array('type' => 'text', 'name' => 'amount_ttc_2', 'label' => $langs->trans("AmountTTC").' 2', 'value' => $amount2, 'size' => '5') + ); + $langs->load("dict"); + print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id.'&remid='.$showconfirminfo['rowid'].($backtopage ? '&backtopage='.urlencode($backtopage) : ''), $langs->trans('SplitDiscount'), $langs->trans('ConfirmSplitDiscount', price($showconfirminfo['amount_ttc']), $langs->transnoentities("Currency".$conf->currency)), 'confirm_split', $formquestion, 0, 0); + } } } else { dol_print_error($db); diff --git a/htdocs/langs/en_US/bills.lang b/htdocs/langs/en_US/bills.lang index 881ff1a9a48..8e225b86a50 100644 --- a/htdocs/langs/en_US/bills.lang +++ b/htdocs/langs/en_US/bills.lang @@ -694,3 +694,7 @@ ExtPaymentSite=External payment Site CantConvertToReducAnInvoiceOfThisType=Can't convert to discount an invoice of this type WithholdingTax=Withholding tax LabelWithholdingExist=The label for Withholding already exists +TotalOfDiscountMustEqualsOriginal=The total of new discounts must be equal to the original discount amount. +RemainToSplit=Remaining amount to be split +TypeAmountOfEachNewDiscountSplit=Input amount for each of each parts +SplitDiscountTitle=Split discount into several \ No newline at end of file diff --git a/htdocs/langs/fr_FR/bills.lang b/htdocs/langs/fr_FR/bills.lang index 43851381d03..a263748c1f1 100644 --- a/htdocs/langs/fr_FR/bills.lang +++ b/htdocs/langs/fr_FR/bills.lang @@ -692,3 +692,7 @@ LimitDepositInvoicePossibilityForConsistency=Limite de dépôt de type facture p ExtPaymentID=ID de paiement externe ExtPaymentSite=Site de paiement externe CantConvertToReducAnInvoiceOfThisType=Impossible de convertir en remise un facture de ce type +TotalOfDiscountMustEqualsOriginal=La somme des nouvelles remises doit être équivalente au montant de la réduction à scinder +RemainToSplit=Montant restant à scinder +TypeAmountOfEachNewDiscountSplit=Saisissez le montant de chacune des parts +SplitDiscountTitle=Scinder réduction en plusieurs \ No newline at end of file