FIX Can create a credit note on situation invoice if previous is also
credit note
This commit is contained in:
parent
180a0a4557
commit
4020d97362
@ -990,6 +990,11 @@ if (empty($reshook))
|
||||
}
|
||||
$id = $object->create($user);
|
||||
|
||||
// NOTE: Pb with situation invoice
|
||||
// NOTE: fields total on situation invoice are stored as cumulative values on total of lines (bad) but delta on invoice total
|
||||
// NOTE: fields total on credit note are stored as delta both on total of lines and on invoice total (good)
|
||||
// NOTE: fields situation_percent on situation invoice are stored as cumulative values on lines (bad)
|
||||
// NOTE: fields situation_percent on credit note are stored as delta on lines (good)
|
||||
if (GETPOST('invoiceAvoirWithLines', 'int')==1 && $id>0)
|
||||
{
|
||||
if (!empty($facture_source->lines))
|
||||
@ -1010,36 +1015,38 @@ if (empty($reshook))
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
if($facture_source->type == Facture::TYPE_SITUATION)
|
||||
if ($facture_source->type == Facture::TYPE_SITUATION)
|
||||
{
|
||||
$source_fk_prev_id = $line->fk_prev_id; // temporary storing situation invoice fk_prev_id
|
||||
$line->fk_prev_id = $line->id; // Credit note line need to be linked to the situation invoice it is create from
|
||||
$line->fk_prev_id = $line->id; // The new line of the new credit note we are creating must be linked to the situation invoice line it is created from
|
||||
|
||||
if(!empty($facture_source->tab_previous_situation_invoice))
|
||||
if (!empty($facture_source->tab_previous_situation_invoice))
|
||||
{
|
||||
// search the last invoice in cycle
|
||||
$lineIndex = count($facture_source->tab_previous_situation_invoice) - 1;
|
||||
// search the last standard invoice in cycle and the possible credit note between this last and facture_source
|
||||
// TODO Move this out of loop of $facture_source->lines
|
||||
$tab_jumped_credit_notes = array();
|
||||
$lineIndex = count($facture_source->tab_previous_situation_invoice) - 1;
|
||||
$searchPreviousInvoice = true;
|
||||
while( $searchPreviousInvoice )
|
||||
while ($searchPreviousInvoice)
|
||||
{
|
||||
if($facture_source->tab_previous_situation_invoice[$lineIndex]->type == Facture::TYPE_SITUATION || $lineIndex < 1)
|
||||
if ($facture_source->tab_previous_situation_invoice[$lineIndex]->type == Facture::TYPE_SITUATION || $lineIndex < 1)
|
||||
{
|
||||
$searchPreviousInvoice=false; // find, exit;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
if ($facture_source->tab_previous_situation_invoice[$lineIndex]->type == Facture::TYPE_CREDIT_NOTE) {
|
||||
$tab_jumped_credit_notes[$lineIndex] = $facture_source->tab_previous_situation_invoice[$lineIndex]->id;
|
||||
}
|
||||
$lineIndex--; // go to previous invoice in cycle
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
$maxPrevSituationPercent = 0;
|
||||
foreach($facture_source->tab_previous_situation_invoice[$lineIndex]->lines as $prevLine)
|
||||
foreach ($facture_source->tab_previous_situation_invoice[$lineIndex]->lines as $prevLine)
|
||||
{
|
||||
if($prevLine->id == $source_fk_prev_id)
|
||||
if ($prevLine->id == $source_fk_prev_id)
|
||||
{
|
||||
$maxPrevSituationPercent = max($maxPrevSituationPercent, $prevLine->situation_percent);
|
||||
|
||||
@ -1059,6 +1066,36 @@ if (empty($reshook))
|
||||
|
||||
// prorata
|
||||
$line->situation_percent = $maxPrevSituationPercent - $line->situation_percent;
|
||||
|
||||
//print 'New line based on invoice id '.$facture_source->tab_previous_situation_invoice[$lineIndex]->id.' fk_prev_id='.$source_fk_prev_id.' will be fk_prev_id='.$line->fk_prev_id.' '.$line->total_ht.' '.$line->situation_percent.'<br>';
|
||||
|
||||
// If there is some credit note between last situation invoice and invoice used for credit note generation (note: credit notes are stored as delta)
|
||||
$maxPrevSituationPercent = 0;
|
||||
foreach ($tab_jumped_credit_notes as $index => $creditnoteid) {
|
||||
foreach ($facture_source->tab_previous_situation_invoice[$index]->lines as $prevLine)
|
||||
{
|
||||
if ($prevLine->fk_prev_id == $source_fk_prev_id)
|
||||
{
|
||||
$maxPrevSituationPercent = $prevLine->situation_percent;
|
||||
|
||||
$line->total_ht -= $prevLine->total_ht;
|
||||
$line->total_tva -= $prevLine->total_tva;
|
||||
$line->total_ttc -= $prevLine->total_ttc;
|
||||
$line->total_localtax1 -= $prevLine->total_localtax1;
|
||||
$line->total_localtax2 -= $prevLine->total_localtax2;
|
||||
|
||||
$line->multicurrency_subprice -= $prevLine->multicurrency_subprice;
|
||||
$line->multicurrency_total_ht -= $prevLine->multicurrency_total_ht;
|
||||
$line->multicurrency_total_tva -= $prevLine->multicurrency_total_tva;
|
||||
$line->multicurrency_total_ttc -= $prevLine->multicurrency_total_ttc;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// prorata
|
||||
$line->situation_percent += $maxPrevSituationPercent;
|
||||
|
||||
//print 'New line based on invoice id '.$facture_source->tab_previous_situation_invoice[$lineIndex]->id.' fk_prev_id='.$source_fk_prev_id.' will be fk_prev_id='.$line->fk_prev_id.' '.$line->total_ht.' '.$line->situation_percent.'<br>';
|
||||
}
|
||||
}
|
||||
|
||||
@ -1078,7 +1115,7 @@ if (empty($reshook))
|
||||
$line->multicurrency_total_tva = -$line->multicurrency_total_tva;
|
||||
$line->multicurrency_total_ttc = -$line->multicurrency_total_ttc;
|
||||
|
||||
$result = $line->insert(0, 1); // When creating credit note with same lines than source, we must ignore error if discount alreayd linked
|
||||
$result = $line->insert(0, 1); // When creating credit note with same lines than source, we must ignore error if discount already linked
|
||||
|
||||
$object->lines[] = $line; // insert new line in current object
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user