From 0f421dc2b4c0df9ca6f2fd12052f87570101fdd5 Mon Sep 17 00:00:00 2001 From: phf Date: Tue, 7 Feb 2017 11:09:18 +0100 Subject: [PATCH 1/3] New global conf MAIN_DEPOSIT_MULTI_TVA to manage multi tva on deposit --- htdocs/compta/facture.php | 133 ++++++++++++++++++++++++-------------- 1 file changed, 85 insertions(+), 48 deletions(-) diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php index 54c8bf1bac1..80296fecc82 100644 --- a/htdocs/compta/facture.php +++ b/htdocs/compta/facture.php @@ -1020,61 +1020,98 @@ if (empty($reshook)) $typeamount = GETPOST('typedeposit', 'alpha'); $valuedeposit = GETPOST('valuedeposit', 'int'); - if ($typeamount == 'amount') + $amountdeposit = array(); + if (!empty($conf->global->MAIN_DEPOSIT_MULTI_TVA)) { - $amountdeposit = $valuedeposit; + if ($typeamount == 'amount') $amount = $valuedeposit; + else $amount = $srcobject->total_ttc * ($valuedeposit / 100); + + $TTotalByTva = array(); + foreach ($srcobject->lines as &$line) + { + $TTotalByTva[$line->tva_tx] += $line->total_ttc ; + } + + $amount_to_diff = 0; + foreach ($TTotalByTva as $tva => &$total) + { + $coef = $total / $srcobject->total_ttc; // Calc coef + $am = $amount * $coef; + $amount_ttc_diff += $am; + $amountdeposit[$tva] += $am / (1 + $tva / 100); // Convert into HT for the addline + } } else { - $amountdeposit = 0; - - if ($result > 0) + if ($typeamount == 'amount') { - $totalamount = 0; - $lines = $srcobject->lines; - $numlines=count($lines); - for ($i=0; $i<$numlines; $i++) - { - $qualified=1; - if (empty($lines[$i]->qty)) $qualified=0; // We discard qty=0, it is an option - if (! empty($lines[$i]->special_code)) $qualified=0; // We discard special_code (frais port, ecotaxe, option, ...) - if ($qualified) $totalamount += $lines[$i]->total_ht; - } - - if ($totalamount != 0) { - $amountdeposit = ($totalamount * $valuedeposit) / 100; - } - } else { - setEventMessages($srcobject->error, $srcobject->errors, 'errors'); - $error ++; + $amountdeposit[] = $valuedeposit; } - } + else + { + if ($result > 0) + { + $totalamount = 0; + $lines = $srcobject->lines; + $numlines=count($lines); + for ($i=0; $i<$numlines; $i++) + { + $qualified=1; + if (empty($lines[$i]->qty)) $qualified=0; // We discard qty=0, it is an option + if (! empty($lines[$i]->special_code)) $qualified=0; // We discard special_code (frais port, ecotaxe, option, ...) + if ($qualified) $totalamount += $lines[$i]->total_ht; + } - $result = $object->addline( - $langs->trans('Deposit'), - $amountdeposit, // subprice - 1, // quantity - $lines[$i]->tva_tx, // vat rate - 0, // localtax1_tx - 0, // localtax2_tx - (empty($conf->global->INVOICE_PRODUCTID_DEPOSIT)?0:$conf->global->INVOICE_PRODUCTID_DEPOSIT), // fk_product - 0, // remise_percent - 0, // date_start - 0, // date_end - 0, $lines[$i]->info_bits, // info_bits - 0, // info_bits - 'HT', - 0, - 0, // product_type - 1, - $lines[$i]->special_code, - $object->origin, - 0, - 0, - 0, - 0, - $langs->trans('Deposit') - ); + if ($totalamount != 0) { + $amountdeposit[] = ($totalamount * $valuedeposit) / 100; + } + } else { + setEventMessages($srcobject->error, $srcobject->errors, 'errors'); + $error ++; + } + } + + $amount_ttc_diff = $amountdeposit[0]; + } + + foreach ($amountdeposit as $tva => $amount) + { + $result = $object->addline( + $langs->trans('Deposit'), + $amount, // subprice + 1, // quantity + $tva, // vat rate + 0, // localtax1_tx + 0, // localtax2_tx + (empty($conf->global->INVOICE_PRODUCTID_DEPOSIT)?0:$conf->global->INVOICE_PRODUCTID_DEPOSIT), // fk_product + 0, // remise_percent + 0, // date_start + 0, // date_end + 0, $lines[$i]->info_bits, // info_bits + 0, // info_bits + 'HT', + 0, + 0, // product_type + 1, + $lines[$i]->special_code, + $object->origin, + 0, + 0, + 0, + 0, + $langs->trans('Deposit') + ); + } + + $diff = $object->total_ttc - $amount_ttc_diff; + + if ($diff != 0) + { + $object->fetch_lines(); + $subprice_diff = $object->lines[0]->subprice - $diff / (1 + $object->lines[0]->tva_tx); + $object->updateline($object->lines[0]->id, $object->lines[0]->desc, $subprice_diff, $object->lines[0]->qty, $object->lines[0]->remise_percent, $object->lines[0]->date_start, $object->lines[0]->date_end, $object->lines[0]->tva_tx, 0, 0, 'HT', $object->lines[0]->info_bits, $object->lines[0]->product_type, 0, 0, 0, $object->lines[0]->pa_ht, $object->lines[0]->label, 0, array(), 100); + } + } else { From 6b20bad847130e1cace7abfcbb461ca14985438a Mon Sep 17 00:00:00 2001 From: phf Date: Tue, 7 Feb 2017 11:14:27 +0100 Subject: [PATCH 2/3] add fixme --- htdocs/compta/facture.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php index 80296fecc82..f431b1bf344 100644 --- a/htdocs/compta/facture.php +++ b/htdocs/compta/facture.php @@ -1059,7 +1059,7 @@ if (empty($reshook)) $qualified=1; if (empty($lines[$i]->qty)) $qualified=0; // We discard qty=0, it is an option if (! empty($lines[$i]->special_code)) $qualified=0; // We discard special_code (frais port, ecotaxe, option, ...) - if ($qualified) $totalamount += $lines[$i]->total_ht; + if ($qualified) $totalamount += $lines[$i]->total_ht; // Fixme : is it not for the customer ? Shouldn't we take total_ttc ? } if ($totalamount != 0) { From 579d3505998ce3a651d3d7647ff7c5c9442b14f9 Mon Sep 17 00:00:00 2001 From: phf Date: Thu, 9 Feb 2017 17:41:06 +0100 Subject: [PATCH 3/3] Fix keep the standard behaviour --- htdocs/compta/facture.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/htdocs/compta/facture.php b/htdocs/compta/facture.php index f431b1bf344..d4ce37ce408 100644 --- a/htdocs/compta/facture.php +++ b/htdocs/compta/facture.php @@ -1063,7 +1063,9 @@ if (empty($reshook)) } if ($totalamount != 0) { - $amountdeposit[] = ($totalamount * $valuedeposit) / 100; + $amountdeposit[$lines[$i]->tva] = ($totalamount * $valuedeposit) / 100; + } else { + $amountdeposit[] = 0; } } else { setEventMessages($srcobject->error, $srcobject->errors, 'errors');