diff --git a/htdocs/compta/facture/class/facture.class.php b/htdocs/compta/facture/class/facture.class.php index b4ecbf104c5..30703c704ba 100644 --- a/htdocs/compta/facture/class/facture.class.php +++ b/htdocs/compta/facture/class/facture.class.php @@ -3414,9 +3414,14 @@ class Facture extends CommonInvoice $num = $this->ref; } - $this->newref = dol_sanitizeFileName($num); + if (!$num) { + $error++; + } else { + $this->oldref = $this->ref; + $this->newref = dol_sanitizeFileName($num); + } - if ($num) { + if (!$error) { $this->update_price(1); // Validate @@ -3434,132 +3439,125 @@ class Facture extends CommonInvoice $this->error = $this->db->lasterror(); $error++; } + } - // We check if the invoice was provisional - /* - if (!$error && (preg_match('/^[\(]?PROV/i', $this->ref))) { - // La verif qu'une remise n'est pas utilisee 2 fois est faite au moment de l'insertion de ligne - } - */ + if (!$error) { + // Define third party as a customer + $result = $this->thirdparty->setAsCustomer(); - if (!$error) { - // Define third party as a customer - $result = $this->thirdparty->setAsCustomer(); + // If active (STOCK_CALCULATE_ON_BILL), we decrement the main product and its components at invoice validation + if ($this->type != self::TYPE_DEPOSIT && $result >= 0 && isModEnabled('stock') && getDolGlobalString('STOCK_CALCULATE_ON_BILL') && $idwarehouse > 0) { + require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php'; + $langs->load("agenda"); - // If active (STOCK_CALCULATE_ON_BILL), we decrement the main product and its components at invoice validation - if ($this->type != self::TYPE_DEPOSIT && $result >= 0 && isModEnabled('stock') && getDolGlobalString('STOCK_CALCULATE_ON_BILL') && $idwarehouse > 0) { - require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php'; - $langs->load("agenda"); + // Loop on each line + $cpt = count($this->lines); + for ($i = 0; $i < $cpt; $i++) { + if ($this->lines[$i]->fk_product > 0) { + $mouvP = new MouvementStock($this->db); + $mouvP->origin = &$this; // deprecated + $mouvP->setOrigin($this->element, $this->id); - // Loop on each line - $cpt = count($this->lines); - for ($i = 0; $i < $cpt; $i++) { - if ($this->lines[$i]->fk_product > 0) { - $mouvP = new MouvementStock($this->db); - $mouvP->origin = &$this; // deprecated - $mouvP->setOrigin($this->element, $this->id); + // We decrease stock for product + if ($this->type == self::TYPE_CREDIT_NOTE) { + // TODO If warehouseid has been set into invoice line, we should use this value in priority + // $newidwarehouse = $this->lines[$i]->fk_warehouse ? $this->lines[$i]->fk_warehouse : $idwarehouse; + $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans("InvoiceValidatedInDolibarr", $num), '', '', $this->lines[$i]->batch); + if ($result < 0) { + $error++; + $this->error = $mouvP->error; + $this->errors = array_merge($this->errors, $mouvP->errors); + } + } else { + // TODO If warehouseid has been set into invoice line, we should use this value in priority + // $newidwarehouse = $this->lines[$i]->fk_warehouse ? $this->lines[$i]->fk_warehouse : $idwarehouse; - // We decrease stock for product - if ($this->type == self::TYPE_CREDIT_NOTE) { - // TODO If warehouseid has been set into invoice line, we should use this value in priority - // $newidwarehouse = $this->lines[$i]->fk_warehouse ? $this->lines[$i]->fk_warehouse : $idwarehouse; - $result = $mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, 0, $langs->trans("InvoiceValidatedInDolibarr", $num), '', '', $this->lines[$i]->batch); - if ($result < 0) { - $error++; - $this->error = $mouvP->error; - $this->errors = array_merge($this->errors, $mouvP->errors); - } - } else { - // TODO If warehouseid has been set into invoice line, we should use this value in priority - // $newidwarehouse = $this->lines[$i]->fk_warehouse ? $this->lines[$i]->fk_warehouse : $idwarehouse; + $is_batch_line = false; + if ($batch_rule > 0) { + $productStatic->fetch($this->lines[$i]->fk_product); + if ($productStatic->hasbatch() && is_object($productbatch)) { + $is_batch_line = true; + $product_qty_remain = $this->lines[$i]->qty; - $is_batch_line = false; - if ($batch_rule > 0) { - $productStatic->fetch($this->lines[$i]->fk_product); - if ($productStatic->hasbatch() && is_object($productbatch)) { - $is_batch_line = true; - $product_qty_remain = $this->lines[$i]->qty; + $sortfield = null; + $sortorder = null; + // find lot/serial by sellby (DLC) and eatby dates (DLUO) first + if ($batch_rule == Productbatch::BATCH_RULE_SELLBY_EATBY_DATES_FIRST) { + $sortfield = 'pl.sellby,pl.eatby,pb.qty,pl.rowid'; + $sortorder = 'ASC,ASC,ASC,ASC'; + } - $sortfield = null; - $sortorder = null; - // find lot/serial by sellby (DLC) and eatby dates (DLUO) first - if ($batch_rule == Productbatch::BATCH_RULE_SELLBY_EATBY_DATES_FIRST) { - $sortfield = 'pl.sellby,pl.eatby,pb.qty,pl.rowid'; - $sortorder = 'ASC,ASC,ASC,ASC'; - } + $resBatchList = $productbatch->findAllForProduct($productStatic->id, $idwarehouse, (getDolGlobalInt('STOCK_ALLOW_NEGATIVE_TRANSFER') ? null : 0), $sortfield, $sortorder); + if (!is_array($resBatchList)) { + $error++; + $this->error = $this->db->lasterror(); + } - $resBatchList = $productbatch->findAllForProduct($productStatic->id, $idwarehouse, (getDolGlobalInt('STOCK_ALLOW_NEGATIVE_TRANSFER') ? null : 0), $sortfield, $sortorder); - if (!is_array($resBatchList)) { + if (!$error) { + $batchList = $resBatchList; + if (empty($batchList)) { $error++; - $this->error = $this->db->lasterror(); + $langs->load('errors'); + $warehouseStatic->fetch($idwarehouse); + $this->error = $langs->trans('ErrorBatchNoFoundForProductInWarehouse', $productStatic->label, $warehouseStatic->ref); + dol_syslog(__METHOD__.' Error: '.$langs->transnoentitiesnoconv('ErrorBatchNoFoundForProductInWarehouse', $productStatic->label, $warehouseStatic->ref), LOG_ERR); } - if (!$error) { - $batchList = $resBatchList; - if (empty($batchList)) { - $error++; - $langs->load('errors'); - $warehouseStatic->fetch($idwarehouse); - $this->error = $langs->trans('ErrorBatchNoFoundForProductInWarehouse', $productStatic->label, $warehouseStatic->ref); - dol_syslog(__METHOD__.' Error: '.$langs->transnoentitiesnoconv('ErrorBatchNoFoundForProductInWarehouse', $productStatic->label, $warehouseStatic->ref), LOG_ERR); + foreach ($batchList as $batch) { + if ($batch->qty <= 0) { + continue; // try to decrement only batches have positive quantity first } - foreach ($batchList as $batch) { - if ($batch->qty <= 0) { - continue; // try to decrement only batches have positive quantity first - } + // enough quantity in this batch + if ($batch->qty >= $product_qty_remain) { + $product_batch_qty = $product_qty_remain; + } else { + // not enough (take all in batch) + $product_batch_qty = $batch->qty; + } + $result = $mouvP->livraison($user, $productStatic->id, $idwarehouse, $product_batch_qty, $this->lines[$i]->subprice, $langs->trans('InvoiceValidatedInDolibarr', $num), '', '', '', $batch->batch); + if ($result < 0) { + $error++; + $this->error = $mouvP->error; + $this->errors = array_merge($this->errors, $mouvP->errors); + break; + } - // enough quantity in this batch - if ($batch->qty >= $product_qty_remain) { - $product_batch_qty = $product_qty_remain; - } else { - // not enough (take all in batch) - $product_batch_qty = $batch->qty; - } - $result = $mouvP->livraison($user, $productStatic->id, $idwarehouse, $product_batch_qty, $this->lines[$i]->subprice, $langs->trans('InvoiceValidatedInDolibarr', $num), '', '', '', $batch->batch); + $product_qty_remain -= $product_batch_qty; + // all product quantity was decremented + if ($product_qty_remain <= 0) { + break; + } + } + + if (!$error && $product_qty_remain > 0) { + if (getDolGlobalInt('STOCK_ALLOW_NEGATIVE_TRANSFER')) { + // take in the first batch + $batch = $batchList[0]; + $result = $mouvP->livraison($user, $productStatic->id, $idwarehouse, $product_qty_remain, $this->lines[$i]->subprice, $langs->trans('InvoiceValidatedInDolibarr', $num), '', '', '', $batch->batch); if ($result < 0) { $error++; $this->error = $mouvP->error; $this->errors = array_merge($this->errors, $mouvP->errors); - break; - } - - $product_qty_remain -= $product_batch_qty; - // all product quantity was decremented - if ($product_qty_remain <= 0) { - break; - } - } - - if (!$error && $product_qty_remain > 0) { - if (getDolGlobalInt('STOCK_ALLOW_NEGATIVE_TRANSFER')) { - // take in the first batch - $batch = $batchList[0]; - $result = $mouvP->livraison($user, $productStatic->id, $idwarehouse, $product_qty_remain, $this->lines[$i]->subprice, $langs->trans('InvoiceValidatedInDolibarr', $num), '', '', '', $batch->batch); - if ($result < 0) { - $error++; - $this->error = $mouvP->error; - $this->errors = array_merge($this->errors, $mouvP->errors); - } - } else { - $error++; - $langs->load('errors'); - $warehouseStatic->fetch($idwarehouse); - $this->error = $langs->trans('ErrorBatchNoFoundEnoughQuantityForProductInWarehouse', $productStatic->label, $warehouseStatic->ref); - dol_syslog(__METHOD__.' Error: '.$langs->transnoentitiesnoconv('ErrorBatchNoFoundEnoughQuantityForProductInWarehouse', $productStatic->label, $warehouseStatic->ref), LOG_ERR); } + } else { + $error++; + $langs->load('errors'); + $warehouseStatic->fetch($idwarehouse); + $this->error = $langs->trans('ErrorBatchNoFoundEnoughQuantityForProductInWarehouse', $productStatic->label, $warehouseStatic->ref); + dol_syslog(__METHOD__.' Error: '.$langs->transnoentitiesnoconv('ErrorBatchNoFoundEnoughQuantityForProductInWarehouse', $productStatic->label, $warehouseStatic->ref), LOG_ERR); } } } } + } - if (!$is_batch_line) { // If stock move not yet processed - $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans("InvoiceValidatedInDolibarr", $num)); - if ($result < 0) { - $error++; - $this->error = $mouvP->error; - $this->errors = array_merge($this->errors, $mouvP->errors); - } + if (!$is_batch_line) { // If stock move not yet processed + $result = $mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans("InvoiceValidatedInDolibarr", $num)); + if ($result < 0) { + $error++; + $this->error = $mouvP->error; + $this->errors = array_merge($this->errors, $mouvP->errors); } } } @@ -3597,8 +3595,6 @@ class Facture extends CommonInvoice } if (!$error) { - $this->oldref = $this->ref; - // Rename directory if dir was a temporary ref if (preg_match('/^[\(]?PROV/i', $this->ref)) { // Now we rename also files into index @@ -3616,28 +3612,6 @@ class Facture extends CommonInvoice $error++; $this->error = $this->db->lasterror(); } - - // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments - $oldref = dol_sanitizeFileName($this->ref); - $newref = dol_sanitizeFileName($num); - $dirsource = $conf->facture->dir_output.'/'.$oldref; - $dirdest = $conf->facture->dir_output.'/'.$newref; - if (!$error && file_exists($dirsource)) { - dol_syslog(get_class($this)."::validate rename dir ".$dirsource." into ".$dirdest); - - if (@rename($dirsource, $dirdest)) { - dol_syslog("Rename ok"); - // Rename docs starting with $oldref with $newref - $listoffiles = dol_dir_list($conf->facture->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/')); - foreach ($listoffiles as $fileentry) { - $dirsource = $fileentry['name']; - $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource); - $dirsource = $fileentry['path'].'/'.$dirsource; - $dirdest = $fileentry['path'].'/'.$dirdest; - @rename($dirsource, $dirdest); - } - } - } } } @@ -3681,8 +3655,31 @@ class Facture extends CommonInvoice $this->setFinal($user); } } - } else { - $error++; + } + + // Rename directory if dir was a temporary ref + if (!$error && preg_match('/^[\(]?PROV/i', $this->ref)) { + // We rename directory ($this->ref = old ref, $num = new ref) in order not to lose the attachments + $oldref = dol_sanitizeFileName($this->ref); + $newref = dol_sanitizeFileName($num); + $dirsource = $conf->facture->dir_output.'/'.$oldref; + $dirdest = $conf->facture->dir_output.'/'.$newref; + if (!$error && file_exists($dirsource)) { + dol_syslog(get_class($this)."::validate rename dir ".$dirsource." into ".$dirdest); + + if (@rename($dirsource, $dirdest)) { + dol_syslog("Rename ok"); + // Rename docs starting with $oldref with $newref + $listoffiles = dol_dir_list($conf->facture->dir_output.'/'.$newref, 'files', 1, '^'.preg_quote($oldref, '/')); + foreach ($listoffiles as $fileentry) { + $dirsource = $fileentry['name']; + $dirdest = preg_replace('/^'.preg_quote($oldref, '/').'/', $newref, $dirsource); + $dirsource = $fileentry['path'].'/'.$dirsource; + $dirdest = $fileentry['path'].'/'.$dirdest; + @rename($dirsource, $dirdest); + } + } + } } if (!$error) { diff --git a/htdocs/core/lib/invoice.lib.php b/htdocs/core/lib/invoice.lib.php index 4228c0a499f..b2939e49e67 100644 --- a/htdocs/core/lib/invoice.lib.php +++ b/htdocs/core/lib/invoice.lib.php @@ -921,9 +921,9 @@ function getCustomerInvoiceLatestEditTable($maxCount = 5, $socid = 0) $filename = dol_sanitizeFileName($obj->ref); $filedir = $conf->propal->multidir_output[$obj->entity].'/'.$filename; - $result .= ''; + $result .= ''; - $result .= ''; + $result .= ''; $result .= ''; $result .= ''; @@ -936,7 +936,7 @@ function getCustomerInvoiceLatestEditTable($maxCount = 5, $socid = 0) $result .= ''; $result .= ''; - $result .= ''; + $result .= ''; $result .= ''; // Load amount of existing payment of invoice (needed for complete status) @@ -1197,7 +1197,7 @@ function getCustomerInvoiceUnpaidOpenTable($maxCount = 500, $socid = 0) print ''; - print '
'.$companystatic->getNomUrl(1, 'customer').''.dol_print_date($db->jdate($obj->datec), 'day').''.dol_print_date($db->jdate($obj->datec), 'day').''.price($obj->total_ttc).''; print $societestatic->getNomUrl(1, 'customer'); print ''; + print ''; print dol_print_date($db->jdate($obj->datelimite), 'day'); if ($tmpinvoice->hasDelay()) { print img_warning($langs->trans("Late"));