Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIX: vat management for credit notes (and negative quantities) #5380

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 7 additions & 7 deletions htdocs/compta/facture/class/facture.class.php
Expand Up @@ -2215,10 +2215,10 @@ function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1=0, $txlocaltax2=0, $f
$this->line->rang=$rangtouse;
$this->line->info_bits=$info_bits;
$this->line->fk_remise_except=$fk_remise_except;
$this->line->total_ht= (($this->type==self::TYPE_CREDIT_NOTE||$qty<0)?-abs($total_ht):$total_ht); // For credit note and if qty is negative, total is negative
$this->line->total_tva= $total_tva;
$this->line->total_localtax1=$total_localtax1;
$this->line->total_localtax2=$total_localtax2;
$this->line->total_ht=(($this->type==self::TYPE_CREDIT_NOTE||$qty<0)?-abs($total_ht):$total_ht); // For credit note and if qty is negative, total is negative
$this->line->total_tva=(($this->type==self::TYPE_CREDIT_NOTE||$qty<0)?-abs($total_tva):$total_tva); // For credit note and if qty is negative, total is negative
$this->line->total_localtax1=(($this->type==self::TYPE_CREDIT_NOTE||$qty<0)?-abs($total_localtax1):$total_localtax1); // For credit note and if qty is negative, total is negative
$this->line->total_localtax2=(($this->type==self::TYPE_CREDIT_NOTE||$qty<0)?-abs($total_localtax2):$total_localtax2); // For credit note and if qty is negative, total is negative
$this->line->localtax1_type = $localtaxes_type[0];
$this->line->localtax2_type = $localtaxes_type[2];
$this->line->total_ttc= (($this->type==self::TYPE_CREDIT_NOTE||$qty<0)?-abs($total_ttc):$total_ttc);
Expand Down Expand Up @@ -2386,9 +2386,9 @@ function updateline($rowid, $desc, $pu, $qty, $remise_percent, $date_start, $dat
$this->line->date_start = $date_start;
$this->line->date_end = $date_end;
$this->line->total_ht = (($this->type==self::TYPE_CREDIT_NOTE||$qty<0)?-abs($total_ht):$total_ht); // For credit note and if qty is negative, total is negative
$this->line->total_tva = $total_tva;
$this->line->total_localtax1 = $total_localtax1;
$this->line->total_localtax2 = $total_localtax2;
$this->line->total_tva = (($this->type==self::TYPE_CREDIT_NOTE||$qty<0)?-abs($total_tva):$total_tva); // For credit note and if qty is negative, total is negative
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix into function addline looks good to me. However this part seems to be here to manage corrupted data in base due to old bug ? Right ? If i am right, it should be fixed by a sql request and not into core code. For example by adding the sql that fix data into file repair.sql

$this->line->total_localtax1 = (($this->type==self::TYPE_CREDIT_NOTE||$qty<0)?-abs($total_localtax1):$total_localtax1); // For credit note and if qty is negative, total is negative
$this->line->total_localtax2 = (($this->type==self::TYPE_CREDIT_NOTE||$qty<0)?-abs($total_localtax2):$total_localtax2); // For credit note and if qty is negative, total is negative
$this->line->total_ttc = (($this->type==self::TYPE_CREDIT_NOTE||$qty<0)?-abs($total_ttc):$total_ttc);
$this->line->info_bits = $info_bits;
$this->line->special_code = $special_code;
Expand Down
24 changes: 12 additions & 12 deletions htdocs/core/lib/tax.lib.php
Expand Up @@ -329,9 +329,9 @@ function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction,
{
$oldrowid=$assoc['rowid'];
$list[$assoc['rate']]['totalht'] += $assoc['total_ht'];
$list[$assoc['rate']]['vat'] += $assoc['total_vat'];
$list[$assoc['rate']]['localtax1'] += $assoc['total_localtax1'];
$list[$assoc['rate']]['localtax2'] += $assoc['total_localtax2'];
$list[$assoc['rate']]['vat'] += ($assoc['total_ht'] < 0 && $assoc['total_vat'] > 0) ? -1 * $assoc['total_vat']:$assoc['total_vat'];
$list[$assoc['rate']]['localtax1'] += ($assoc['total_ht'] < 0 && $assoc['total_localtax1'] > 0) ? -1 * $assoc['total_localtax1']:$assoc['total_localtax1'];
$list[$assoc['rate']]['localtax2'] += ($assoc['total_ht'] < 0 && $assoc['total_localtax2'] > 0) ? -1 * $assoc['total_localtax2']:$assoc['total_localtax2'];
}
$list[$assoc['rate']]['dtotal_ttc'][] = $assoc['total_ttc'];
$list[$assoc['rate']]['dtype'][] = $assoc['dtype'];
Expand All @@ -348,9 +348,9 @@ function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction,
$list[$assoc['rate']]['descr'][] = $assoc['descr'];

$list[$assoc['rate']]['totalht_list'][] = $assoc['total_ht'];
$list[$assoc['rate']]['vat_list'][] = $assoc['total_vat'];
$list[$assoc['rate']]['localtax1_list'][] = $assoc['total_localtax1'];
$list[$assoc['rate']]['localtax2_list'][] = $assoc['total_localtax2'];
$list[$assoc['rate']]['vat_list'][] = ($assoc['total_ht'] < 0 && $assoc['total_vat'] > 0) ? -1 * $assoc['total_vat']:$assoc['total_vat'];
$list[$assoc['rate']]['localtax1_list'][] = ($assoc['total_ht'] < 0 && $assoc['total_localtax1'] > 0) ? -1 * $assoc['total_localtax1']:$assoc['total_localtax1'];
$list[$assoc['rate']]['localtax2_list'][] = ($assoc['total_ht'] < 0 && $assoc['total_localtax2'] > 0) ? -1 * $assoc['total_localtax2']:$assoc['total_localtax2'];

$list[$assoc['rate']]['pid'][] = $assoc['pid'];
$list[$assoc['rate']]['pref'][] = $assoc['pref'];
Expand Down Expand Up @@ -474,9 +474,9 @@ function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction,
{
$oldrowid=$assoc['rowid'];
$list[$assoc['rate']]['totalht'] += $assoc['total_ht'];
$list[$assoc['rate']]['vat'] += $assoc['total_vat'];
$list[$assoc['rate']]['localtax1'] += $assoc['total_localtax1'];
$list[$assoc['rate']]['localtax2'] += $assoc['total_localtax2'];
$list[$assoc['rate']]['vat'] += ($assoc['total_ht'] < 0 && $assoc['total_vat'] > 0) ? -1 * $assoc['total_vat']:$assoc['total_vat'];
$list[$assoc['rate']]['localtax1'] += ($assoc['total_ht'] < 0 && $assoc['total_localtax1'] > 0) ? -1 * $assoc['total_localtax1']:$assoc['total_localtax1'];
$list[$assoc['rate']]['localtax2'] += ($assoc['total_ht'] < 0 && $assoc['total_localtax2'] > 0) ? -1 * $assoc['total_localtax2']:$assoc['total_localtax2'];
}
$list[$assoc['rate']]['dtotal_ttc'][] = $assoc['total_ttc'];
$list[$assoc['rate']]['dtype'][] = $assoc['dtype'];
Expand All @@ -493,9 +493,9 @@ function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction,
$list[$assoc['rate']]['descr'][] = $assoc['descr'];

$list[$assoc['rate']]['totalht_list'][] = $assoc['total_ht'];
$list[$assoc['rate']]['vat_list'][] = $assoc['total_vat'];
$list[$assoc['rate']]['localtax1_list'][] = $assoc['total_localtax1'];
$list[$assoc['rate']]['localtax2_list'][] = $assoc['total_localtax2'];
$list[$assoc['rate']]['vat_list'][] = ($assoc['total_ht'] < 0 && $assoc['total_vat'] > 0) ? -1 * $assoc['total_vat']:$assoc['total_vat'];
$list[$assoc['rate']]['localtax1_list'][] = ($assoc['total_ht'] < 0 && $assoc['total_localtax1'] > 0) ? -1 * $assoc['total_localtax1']:$assoc['total_localtax1'];
$list[$assoc['rate']]['localtax2_list'][] = ($assoc['total_ht'] < 0 && $assoc['total_localtax2'] > 0) ? -1 * $assoc['total_localtax2']:$assoc['total_localtax2'];

$list[$assoc['rate']]['pid'][] = $assoc['pid'];
$list[$assoc['rate']]['pref'][] = $assoc['pref'];
Expand Down