Merge pull request #6388 from atm-ph/new_deposit_with_multi_tva_management

New deposit with multi tva management
This commit is contained in:
Laurent Destailleur 2017-02-24 16:16:33 +01:00 committed by GitHub
commit 7ee6dc7fa6

View File

@ -1052,14 +1052,35 @@ if (empty($reshook))
$typeamount = GETPOST('typedeposit', 'alpha'); $typeamount = GETPOST('typedeposit', 'alpha');
$valuedeposit = GETPOST('valuedeposit', 'int'); $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
{
if ($typeamount == 'amount')
{
$amountdeposit[] = $valuedeposit;
} }
else else
{ {
$amountdeposit = 0;
if ($result > 0) if ($result > 0)
{ {
$totalamount = 0; $totalamount = 0;
@ -1070,11 +1091,13 @@ if (empty($reshook))
$qualified=1; $qualified=1;
if (empty($lines[$i]->qty)) $qualified=0; // We discard qty=0, it is an option 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 (! 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) { if ($totalamount != 0) {
$amountdeposit = ($totalamount * $valuedeposit) / 100; $amountdeposit[$lines[$i]->tva] = ($totalamount * $valuedeposit) / 100;
} else {
$amountdeposit[] = 0;
} }
} else { } else {
setEventMessages($srcobject->error, $srcobject->errors, 'errors'); setEventMessages($srcobject->error, $srcobject->errors, 'errors');
@ -1082,11 +1105,16 @@ if (empty($reshook))
} }
} }
$amount_ttc_diff = $amountdeposit[0];
}
foreach ($amountdeposit as $tva => $amount)
{
$result = $object->addline( $result = $object->addline(
$langs->trans('Deposit'), $langs->trans('Deposit'),
$amountdeposit, // subprice $amount, // subprice
1, // quantity 1, // quantity
$lines[$i]->tva_tx, // vat rate $tva, // vat rate
0, // localtax1_tx 0, // localtax1_tx
0, // localtax2_tx 0, // localtax2_tx
(empty($conf->global->INVOICE_PRODUCTID_DEPOSIT)?0:$conf->global->INVOICE_PRODUCTID_DEPOSIT), // fk_product (empty($conf->global->INVOICE_PRODUCTID_DEPOSIT)?0:$conf->global->INVOICE_PRODUCTID_DEPOSIT), // fk_product
@ -1108,6 +1136,17 @@ if (empty($reshook))
$langs->trans('Deposit') $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 else
{ {
if ($result > 0) if ($result > 0)