Merge pull request #6388 from atm-ph/new_deposit_with_multi_tva_management
New deposit with multi tva management
This commit is contained in:
commit
7ee6dc7fa6
@ -1052,61 +1052,100 @@ 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
|
else
|
||||||
{
|
{
|
||||||
$amountdeposit = 0;
|
if ($typeamount == 'amount')
|
||||||
|
|
||||||
if ($result > 0)
|
|
||||||
{
|
{
|
||||||
$totalamount = 0;
|
$amountdeposit[] = $valuedeposit;
|
||||||
$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 ++;
|
|
||||||
}
|
}
|
||||||
}
|
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; // Fixme : is it not for the customer ? Shouldn't we take total_ttc ?
|
||||||
|
}
|
||||||
|
|
||||||
$result = $object->addline(
|
if ($totalamount != 0) {
|
||||||
$langs->trans('Deposit'),
|
$amountdeposit[$lines[$i]->tva] = ($totalamount * $valuedeposit) / 100;
|
||||||
$amountdeposit, // subprice
|
} else {
|
||||||
1, // quantity
|
$amountdeposit[] = 0;
|
||||||
$lines[$i]->tva_tx, // vat rate
|
}
|
||||||
0, // localtax1_tx
|
} else {
|
||||||
0, // localtax2_tx
|
setEventMessages($srcobject->error, $srcobject->errors, 'errors');
|
||||||
(empty($conf->global->INVOICE_PRODUCTID_DEPOSIT)?0:$conf->global->INVOICE_PRODUCTID_DEPOSIT), // fk_product
|
$error ++;
|
||||||
0, // remise_percent
|
}
|
||||||
0, // date_start
|
}
|
||||||
0, // date_end
|
|
||||||
0, $lines[$i]->info_bits, // info_bits
|
$amount_ttc_diff = $amountdeposit[0];
|
||||||
0, // info_bits
|
}
|
||||||
'HT',
|
|
||||||
0,
|
foreach ($amountdeposit as $tva => $amount)
|
||||||
0, // product_type
|
{
|
||||||
1,
|
$result = $object->addline(
|
||||||
$lines[$i]->special_code,
|
$langs->trans('Deposit'),
|
||||||
$object->origin,
|
$amount, // subprice
|
||||||
0,
|
1, // quantity
|
||||||
0,
|
$tva, // vat rate
|
||||||
0,
|
0, // localtax1_tx
|
||||||
0,
|
0, // localtax2_tx
|
||||||
$langs->trans('Deposit')
|
(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
|
else
|
||||||
{
|
{
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user