forked from Wavyzz/dolibarr
* Changes to follow mvc logic * Reworked admin page and form to add lines * Reworked adding line logic * Adding options when editing subtotal lines * Fix translations * Fixed errors/displays and started pdf * Color for subtotals pdf lines * FIX display of subtotal totht * Added pdf azur for propal * Fix duplicate translation * Added subtotal support for facture pdf * Added subtotal support for commande pdf * Improve UI/translations * Restored old pdf * Info to warn user for unsupported pdf * Added title lines VAT rate and discount support This is meant for future feature wich is block mass changing vat rate and discount percentage * Working on block apply vat and discount * Added buttons for block actions * Handle editing vat and discount for subtotal lines * Editing vat and discount for subtotal lines bloc working * Added possibility to move by block * Updated adding and updating a subtotal line * Improved vat and discount block update * Improvement for block vat/discount and line edition Editing a title line edits the corresponding subtotal line * Improvement for moving by block * Bad tile or st line placement managing * Improved bad title or st line placement managing * Adding subtotal line improved Adding a subtotal line adds it right under its corresponding title * Improved deleting subtotal line Added possibility to choose if you want to delete the corresponding subtotal line when deleting a title line * Preventing too high level titles to be created * Create and update line errors managing * Improved bad title or st line placement managing * Improved st line creation * improve headers * fix bad block placement managing * fix adding st line not working if duplicates titles * fix translations * Fix block update * FIX special chars bug If special char like " ' " was used in title it could be converted to special char entity. * Prepare for pdf options Pdf options like page break befor title should be shown as a picto if activated on a subtotal line on a document * Added option managing * Improved subtotal options and PDF integration * Code refactor * Action name/Error managment/PDF refreshing When adding or updating a subtotal line * Reformat * Reworked subtotal options database managment * Changed access to special code * Remove unecessary call to php trait * Changed definition of subtotals special code constant * Reworked align on PDF * Removed unecessary function and improved error managment * Typo fix and removed treated todos * Post typo changed for to better match subtotals names * Disabled edit if status is not draft * Changed way subtotals options are stored * Added view managing when creating a document form an other * Improved creating document from an other Can check subtotals lines with table head checkbox and removed highlight class for better UI. * Make include of subtotals tpl more clear * Manage centered or justified case If user chose to center or justify, we don't change nothing * Improved pdf * Removed unecessary code block * optimisation * Code sniffer fix * Code sniffer fix * Code sniffer fix * Code sniffer fix * Code sniffer fix and added missing translation * Fix php code sniffer * Reload page when setup saved on subtotals admin page * Fix php code sniffer * pjan fix * phan fix * phan fix * phan fix * phan fix * phan fix * phan fix * phan fix * phan fix * phan fix * phan fix * phan fix * php warning fix * php warning fix * php phan fix * php phan fix * php phan fix * Fix bug admin page not loading because of const not defined * php phan fix * php phan fix * FIX subtotals admin page display * php phan fix * FIX php phan * Fix bugs and langs * Fix bug pdf align * Replace include by require * Add headers to avoid refreshing and adding unwanted lines * Fix phan * Add GETPOST check for security * Fix phan * Fix phan * Fix phan * Subtotal option when creating a document from another * Fix phan * Add field subtotal options for subtotal lines * Fix phan * Fix phan * Fix phan * Update to follow mvc * Fix php phan * Fix php phan * Fix phpstan/phan * Fix phpstan * Fix phpstan * Fix phpstan * Fix phpstan * Fix phpstan * Update to switch to extraparams * Update to switch to extraparams * Retrieve extraparams from db to objectline * Modified last things to switch to extraparams * Cleaning unnecessary code lines * Fix php-stan * Section subtotal in extraparams to differentiate if needed for further devs * Fix phan * Keep extraparams when creating from another object * Change default value to false when creating a subtotal line * Fix clone would not keep extraparams in new object * Fix dark subtotal line background color Fix when a subtotal background color is too dark and edit pencil or delete trash could not be seen * Fix typo * Fix typo * Added subtotals for facturerec * Fix precommit * Added extraparams when creating rec from fac and other way * Fix phan * Fix objectline null * Desactivating block vat / discount update for facturerec * reformating code * Added expeditions for subtotal * Save extraparams for shipping lines * Display of subtotals lines in shipments * Display when creating facture from shipments * Improve display of lines and invoice creation from shipments * Fix error if missing line rang * Deleted duplicate * Added deletion of subtotal line in shipping documents * Not including subtotal lines if there is no product line in between * Update get subtotal lines in shipment docs to disable * Delete possibility to edit subtotal lines in shipments * Handle pdf for shipment * Handle conf stock or shipment supporting services * Fix precommit * Fix duplicate name creating bug * Fix bug where id could be changed by the line id and would create bug * Deleted subtotal lines when STOCK_SUPPORTS_SERVICES is enabled and block would only have service lines * Disable shipments in admin modules Shimpements subtotals lines are only created from commands * Fixing phan and stan * Deleted unused template and phan fix * Fix phan * Fix phan * Fix phan * FIX: phan * Fix template bug If document was not in subtotal scope and would use a template used by subtotal it would create an error. * Fix php-stan * Fix bad display when modules and confs were activated * Fix php codesniffer * Excluding subtotal lines when mass updating * Fix shipments service lines exluded + showing subtotal line with specific configs * Fix line display with situation invoices + bug block line update * Added ODT managment This works with invoices but has not been tested with other documents * Fix bug when editing VAT/discount by block * Fix error raised when subtotal line added Subtotal line had no fk_product and raised the error but we want this line to have no fk product * Fix precommit * Added ODT template for documents that uses subtotals This template can be usefull to understand how to create an odt template using subtotals module * Fix phan * Fix phan * fix: buttons showing in bad document status * clean: unwanted commited files * add: table examples to use with subtotals on ODT templates * fix: Unwanted print of value when creating a document from another * feat: renaming for better understanding * feat: Improved templates for documents related to subtotals --------- Co-authored-by: Marc de Lima Lucio <68746600+marc-dll@users.noreply.github.com> Co-authored-by: Laurent Destailleur <eldy@destailleur.fr>
808 lines
20 KiB
PHP
808 lines
20 KiB
PHP
<?php
|
|
/* Copyright (C) 2003-2008 Rodolphe Quiedeville <rodolphe@quiedeville.org>
|
|
* Copyright (C) 2005-2012 Regis Houssin <regis.houssin@inodbox.com>
|
|
* Copyright (C) 2007 Franky Van Liedekerke <franky.van.liedekerke@telenet.be>
|
|
* Copyright (C) 2006-2012 Laurent Destailleur <eldy@users.sourceforge.net>
|
|
* Copyright (C) 2011-2020 Juanjo Menent <jmenent@2byte.es>
|
|
* Copyright (C) 2013 Florian Henry <florian.henry@open-concept.pro>
|
|
* Copyright (C) 2014 Cedric GROSS <c.gross@kreiz-it.fr>
|
|
* Copyright (C) 2014-2015 Marcos García <marcosgdf@gmail.com>
|
|
* Copyright (C) 2014-2017 Francis Appels <francis.appels@yahoo.com>
|
|
* Copyright (C) 2015 Claudio Aschieri <c.aschieri@19.coop>
|
|
* Copyright (C) 2016-2024 Ferran Marcet <fmarcet@2byte.es>
|
|
* Copyright (C) 2018 Nicolas ZABOURI <info@inovea-conseil.com>
|
|
* Copyright (C) 2018-2025 Frédéric France <frederic.france@free.fr>
|
|
* Copyright (C) 2020 Lenin Rivas <lenin@leninrivas.com>
|
|
* Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
|
|
*
|
|
* This program is free software; you can redistribute it and/or modify
|
|
* it under the terms of the GNU General Public License as published by
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
* (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
/**
|
|
* \file htdocs/expedition/class/expedition.class.php
|
|
* \ingroup expedition
|
|
* \brief File of class managing the shipments
|
|
*/
|
|
|
|
require_once DOL_DOCUMENT_ROOT."/core/class/commonobjectline.class.php";
|
|
require_once DOL_DOCUMENT_ROOT.'/expedition/class/expeditionlinebatch.class.php';
|
|
|
|
/**
|
|
* Class to manage lines of shipment
|
|
*/
|
|
class ExpeditionLigne extends CommonObjectLine
|
|
{
|
|
/**
|
|
* @var string ID to identify managed object
|
|
*/
|
|
public $element = 'expeditiondet';
|
|
|
|
/**
|
|
* @var string Name of table without prefix where object is stored
|
|
*/
|
|
public $table_element = 'expeditiondet';
|
|
|
|
/**
|
|
* @see CommonObjectLine
|
|
*/
|
|
public $parent_element = 'expedition';
|
|
|
|
/**
|
|
* @see CommonObjectLine
|
|
*/
|
|
public $fk_parent_attribute = 'fk_expedition';
|
|
|
|
/**
|
|
* Id of the line. Duplicate of $id.
|
|
*
|
|
* @var int
|
|
* @deprecated
|
|
*/
|
|
public $line_id; // deprecated
|
|
|
|
/**
|
|
* @var int ID Duplicate of origin_id (using origin_id is better)
|
|
*/
|
|
public $fk_element;
|
|
|
|
/**
|
|
* @var int ID Duplicate of fk_element
|
|
*/
|
|
public $origin_id;
|
|
|
|
/**
|
|
* @var int ID Duplicate of origin_line_id
|
|
*/
|
|
public $fk_elementdet;
|
|
|
|
/**
|
|
* @var int ID Duplicate of fk_elementdet
|
|
*/
|
|
public $origin_line_id;
|
|
|
|
/**
|
|
* @var int Id of parent line for children of virtual product
|
|
*/
|
|
public $fk_parent;
|
|
|
|
/**
|
|
* @var string Type of object the fk_element refers to. Example: 'order'.
|
|
*/
|
|
public $element_type;
|
|
|
|
|
|
/**
|
|
* Code of object line that is origin of the shipment line.
|
|
*
|
|
* @var string
|
|
* @deprecated Use instead origin_type = element_type to guess the line of origin of the shipment line.
|
|
*/
|
|
public $fk_origin; // Example: 'orderline'
|
|
|
|
/**
|
|
* @var int Id of shipment
|
|
*/
|
|
public $fk_expedition;
|
|
|
|
/**
|
|
* @var DoliDB Database handler.
|
|
*/
|
|
public $db;
|
|
|
|
/**
|
|
* @var float qty asked From llx_expeditiondet
|
|
*/
|
|
public $qty;
|
|
|
|
/**
|
|
* @var float qty shipped
|
|
*/
|
|
public $qty_shipped;
|
|
|
|
/**
|
|
* @var int Id of product
|
|
*/
|
|
public $fk_product;
|
|
|
|
/**
|
|
* Detail of lot and qty = array(id in llx_expeditiondet_batch, fk_expeditiondet, batch, qty, fk_origin_stock)
|
|
* We can use this to know warehouse planned to be used for each lot.
|
|
* @var stdClass|ExpeditionLineBatch[]
|
|
*/
|
|
public $detail_batch;
|
|
|
|
/**
|
|
* Virtual products : array of total of quantities group product id and warehouse id ([id_product][id_warehouse] -> qty (int|float))
|
|
* @var array<int, array<int, int|float>>
|
|
*/
|
|
public $detail_children;
|
|
|
|
/** detail of warehouses and qty
|
|
* We can use this to know warehouse when there is no lot.
|
|
* @var stdClass[]
|
|
*/
|
|
public $details_entrepot;
|
|
|
|
|
|
/**
|
|
* @var int Id of warehouse
|
|
*/
|
|
public $entrepot_id;
|
|
|
|
|
|
/**
|
|
* @var float qty asked From llx_commandedet or llx_propaldet
|
|
*/
|
|
public $qty_asked;
|
|
|
|
/**
|
|
* @var string
|
|
* @deprecated
|
|
* @see $product_ref
|
|
*/
|
|
public $ref;
|
|
|
|
/**
|
|
* @var string product ref
|
|
*/
|
|
public $product_ref;
|
|
|
|
/**
|
|
* @var string
|
|
* @deprecated
|
|
* @see $product_label
|
|
*/
|
|
public $libelle;
|
|
|
|
/**
|
|
* @var string product label
|
|
*/
|
|
public $product_label;
|
|
|
|
/**
|
|
* @var string product description
|
|
* @deprecated
|
|
* @see $product_desc
|
|
*/
|
|
public $desc;
|
|
|
|
/**
|
|
* @var string product description
|
|
*/
|
|
public $product_desc;
|
|
|
|
/**
|
|
* Type of the product. 0 for product, 1 for service
|
|
* @var int
|
|
*/
|
|
public $product_type = 0;
|
|
|
|
/**
|
|
* @var int rang of line
|
|
*/
|
|
public $rang;
|
|
|
|
/**
|
|
* @var float weight
|
|
*/
|
|
public $weight;
|
|
|
|
/**
|
|
* @var string|int
|
|
*/
|
|
public $weight_units;
|
|
|
|
/**
|
|
* @var float length
|
|
*/
|
|
public $length;
|
|
|
|
/**
|
|
* @var string|int
|
|
*/
|
|
public $length_units;
|
|
|
|
/**
|
|
* @var float width
|
|
*/
|
|
public $width;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
public $width_units;
|
|
|
|
/**
|
|
* @var float height
|
|
*/
|
|
public $height;
|
|
|
|
/**
|
|
* @var int
|
|
*/
|
|
public $height_units;
|
|
|
|
/**
|
|
* @var float surface
|
|
*/
|
|
public $surface;
|
|
|
|
/**
|
|
* @var string|int
|
|
*/
|
|
public $surface_units;
|
|
|
|
/**
|
|
* @var float volume
|
|
*/
|
|
public $volume;
|
|
|
|
/**
|
|
* @var string|int
|
|
*/
|
|
public $volume_units;
|
|
|
|
/**
|
|
* 0=This service or product is not managed in stock, 1=This service or product is managed in stock
|
|
*
|
|
* @var int
|
|
*/
|
|
public $stockable_product = 1;
|
|
|
|
/**
|
|
* @var float|string
|
|
*/
|
|
public $remise_percent;
|
|
|
|
/**
|
|
* @var float|string
|
|
*/
|
|
public $tva_tx;
|
|
|
|
/**
|
|
* @var float total without tax
|
|
*/
|
|
public $total_ht;
|
|
|
|
/**
|
|
* @var float total with tax
|
|
*/
|
|
public $total_ttc;
|
|
|
|
/**
|
|
* @var float total vat
|
|
*/
|
|
public $total_tva;
|
|
|
|
/**
|
|
* @var float total localtax 1
|
|
*/
|
|
public $total_localtax1;
|
|
|
|
/**
|
|
* @var float total localtax 2
|
|
*/
|
|
public $total_localtax2;
|
|
|
|
|
|
/**
|
|
* Constructor
|
|
*
|
|
* @param DoliDB $db Database handler
|
|
*/
|
|
public function __construct($db)
|
|
{
|
|
$this->db = $db;
|
|
}
|
|
|
|
/**
|
|
* Load line expedition
|
|
*
|
|
* @param int $rowid Id line order
|
|
* @return int Return integer <0 if KO, >0 if OK
|
|
*/
|
|
public function fetch($rowid)
|
|
{
|
|
$sql = 'SELECT ed.rowid, ed.fk_expedition, ed.fk_entrepot, ed.fk_elementdet, ed.element_type, ed.qty, ed.rang, ed.extraparams';
|
|
$sql .= ' FROM '.MAIN_DB_PREFIX.$this->table_element.' as ed';
|
|
$sql .= ' WHERE ed.rowid = '.((int) $rowid);
|
|
$result = $this->db->query($sql);
|
|
if ($result) {
|
|
$objp = $this->db->fetch_object($result);
|
|
$this->id = $objp->rowid;
|
|
$this->fk_expedition = $objp->fk_expedition;
|
|
$this->entrepot_id = $objp->fk_entrepot;
|
|
$this->fk_elementdet = $objp->fk_elementdet;
|
|
$this->element_type = $objp->element_type;
|
|
$this->qty = $objp->qty;
|
|
$this->rang = $objp->rang;
|
|
|
|
$this->extraparams = !empty($objp->extraparams) ? (array) json_decode($objp->extraparams, true) : array();
|
|
|
|
$this->db->free($result);
|
|
|
|
return 1;
|
|
} else {
|
|
$this->errors[] = $this->db->lasterror();
|
|
$this->error = $this->db->lasterror();
|
|
return -1;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Insert line into database
|
|
*
|
|
* @param User $user User that modify
|
|
* @param int $notrigger 1 = disable triggers
|
|
* @return int Return integer <0 if KO, line id >0 if OK
|
|
*/
|
|
public function insert($user, $notrigger = 0)
|
|
{
|
|
global $langs;
|
|
$error = 0;
|
|
|
|
$skip_check_parameters = false;
|
|
|
|
// Handling parent line subtotal line
|
|
if (!empty($this->element_type)
|
|
&& !empty($this->fk_elementdet)
|
|
&& $this->element_type == 'commande') {
|
|
$objectsrc_line = new OrderLine($this->db);
|
|
$objectsrc_line->fetch($this->fk_elementdet);
|
|
$skip_check_parameters = $objectsrc_line->special_code == SUBTOTALS_SPECIAL_CODE;
|
|
}
|
|
|
|
// Check parameters
|
|
if ((empty($this->fk_expedition)
|
|
|| empty($this->fk_product) // product id is mandatory
|
|
|| (empty($this->fk_elementdet) && empty($this->fk_parent)) // at least origin line id of parent line id is set
|
|
|| !is_numeric($this->qty))
|
|
&& !$skip_check_parameters) {
|
|
$langs->load('errors');
|
|
$this->errors[] = $langs->trans('ErrorMandatoryParametersNotProvided');
|
|
return -1;
|
|
}
|
|
|
|
$this->db->begin();
|
|
|
|
if (empty($this->rang)) {
|
|
$this->rang = 0;
|
|
}
|
|
|
|
// Rank to use
|
|
$ranktouse = $this->rang;
|
|
if ($ranktouse == -1) {
|
|
$rangmax = $this->line_max($this->fk_expedition);
|
|
$ranktouse = $rangmax + 1;
|
|
}
|
|
|
|
$sql = "INSERT INTO ".MAIN_DB_PREFIX."expeditiondet (";
|
|
$sql .= "fk_expedition";
|
|
$sql .= ", fk_entrepot";
|
|
$sql .= ", fk_elementdet";
|
|
$sql .= ", fk_parent";
|
|
$sql .= ", fk_product";
|
|
$sql .= ", element_type";
|
|
$sql .= ", qty";
|
|
$sql .= ", rang";
|
|
$sql .= ") VALUES (";
|
|
$sql .= $this->fk_expedition;
|
|
$sql .= ", ".(empty($this->entrepot_id) ? 'NULL' : $this->entrepot_id);
|
|
$sql .= ", ".(empty($this->fk_elementdet) ? 'NULL' : $this->fk_elementdet);
|
|
$sql .= ", ".(empty($this->fk_parent) ? 'NULL' : $this->fk_parent);
|
|
$sql .= ", ".(empty($this->fk_product) ? 'NULL' : $this->fk_product);
|
|
$sql .= ", '".(empty($this->element_type) ? 'order' : $this->db->escape($this->element_type))."'";
|
|
$sql .= ", ".price2num($this->qty, 'MS');
|
|
$sql .= ", ".((int) $ranktouse);
|
|
$sql .= ")";
|
|
|
|
dol_syslog(get_class($this)."::insert", LOG_DEBUG);
|
|
$resql = $this->db->query($sql);
|
|
if ($resql) {
|
|
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."expeditiondet");
|
|
|
|
if (!$error) {
|
|
$result = $this->insertExtraFields();
|
|
if ($result < 0) {
|
|
$error++;
|
|
}
|
|
}
|
|
|
|
if (!$error && !$notrigger) {
|
|
// Call trigger
|
|
$result = $this->call_trigger('LINESHIPPING_INSERT', $user);
|
|
if ($result < 0) {
|
|
$error++;
|
|
}
|
|
// End call triggers
|
|
}
|
|
|
|
if ($error) {
|
|
foreach ($this->errors as $errmsg) {
|
|
dol_syslog(__METHOD__.' '.$errmsg, LOG_ERR);
|
|
$this->error .= ($this->error ? ', '.$errmsg : $errmsg);
|
|
}
|
|
}
|
|
} else {
|
|
$error++;
|
|
}
|
|
|
|
if ($error) {
|
|
$this->db->rollback();
|
|
return -1;
|
|
} else {
|
|
$this->db->commit();
|
|
return $this->id;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Find all children
|
|
*
|
|
* @param int $line_id Line id
|
|
* @param stdClass[] $list List of sub-lines for a virtual product line (array of object with attributes : rowid, fk_product, fk_parent, qty, fk_warehouse, batch, eatby, sellby, iskit, incdec)
|
|
* @param int $mode [=0] array of lines ids, 1 array of line object for dispatcher
|
|
* @return int Return integer <0 if KO else >0 if OK
|
|
*/
|
|
public function findAllChild($line_id, &$list = array(), $mode = 0)
|
|
{
|
|
if ($line_id > 0) {
|
|
// find all child
|
|
$sql = "SELECT ed.rowid as child_line_id";
|
|
if ($mode == 1) {
|
|
$sql .= ", ed.fk_product";
|
|
$sql .= ", ed.fk_parent";
|
|
$sql .= ", " . $this->db->ifsql('eb.rowid IS NULL', 'ed.qty', 'eb.qty') . " as qty";
|
|
$sql .= ", " . $this->db->ifsql('eb.rowid IS NULL', 'ed.fk_entrepot', 'eb.fk_warehouse') . " as fk_warehouse";
|
|
$sql .= ", eb.batch, eb.eatby, eb.sellby";
|
|
}
|
|
$sql .= " FROM " . $this->db->prefix() . $this->table_element . " as ed";
|
|
$sql .= " LEFT JOIN " . $this->db->prefix() . "expeditiondet_batch as eb ON eb.fk_expeditiondet = " . ((int) $line_id);
|
|
$sql .= " WHERE ed.fk_parent = " . ((int) $line_id);
|
|
$sql .= $this->db->order('ed.fk_product,ed.rowid', 'ASC,ASC');
|
|
|
|
$resql = $this->db->query($sql);
|
|
if ($resql) {
|
|
while ($obj = $this->db->fetch_object($resql)) {
|
|
$child_line_id = (int) $obj->child_line_id;
|
|
if (!isset($list[$line_id])) {
|
|
$list[$line_id] = array();
|
|
}
|
|
|
|
if ($mode == 0) {
|
|
$list[$line_id][] = $child_line_id;
|
|
} elseif ($mode == 1) {
|
|
$line_obj = new stdClass();
|
|
$line_obj->rowid = $child_line_id;
|
|
$line_obj->fk_product = $obj->fk_product;
|
|
$line_obj->fk_parent = $obj->fk_parent;
|
|
$line_obj->qty = $obj->qty;
|
|
$line_obj->fk_warehouse = $obj->fk_warehouse;
|
|
$line_obj->batch = $obj->batch;
|
|
$line_obj->eatby = $obj->eatby;
|
|
$line_obj->sellby = $obj->sellby;
|
|
$line_obj->iskit = 0;
|
|
$line_obj->incdec = 0;
|
|
$list[$line_id][] = $line_obj;
|
|
}
|
|
|
|
$this->findAllChild($child_line_id, $list, $mode);
|
|
}
|
|
$this->db->free($resql);
|
|
} else {
|
|
$this->error = $this->db->lasterror();
|
|
$this->errors[] = $this->error;
|
|
dol_syslog(__METHOD__.' '.$this->error, LOG_ERR);
|
|
}
|
|
}
|
|
|
|
return 1;
|
|
}
|
|
|
|
/**
|
|
* Delete shipment line.
|
|
*
|
|
* @param User $user User that modify
|
|
* @param int $notrigger 0=launch triggers after, 1=disable triggers
|
|
* @return int >0 if OK, <0 if KO
|
|
*/
|
|
public function delete($user = null, $notrigger = 0)
|
|
{
|
|
$error = 0;
|
|
|
|
$this->db->begin();
|
|
|
|
// virtual products : delete all children and batch
|
|
if (getDolGlobalInt('PRODUIT_SOUSPRODUITS') && !($this->fk_parent > 0)) {
|
|
// find all children
|
|
$line_id_list = array();
|
|
$result = $this->findAllChild($this->id, $line_id_list);
|
|
if ($result) {
|
|
$child_line_id_list = array_reverse($line_id_list, true);
|
|
foreach ($child_line_id_list as $child_line_id_arr) {
|
|
foreach ($child_line_id_arr as $child_line_id) {
|
|
// delete batch expedition line
|
|
if (isModEnabled('productbatch')) {
|
|
$sql = "DELETE FROM " . $this->db->prefix() . "expeditiondet_batch";
|
|
$sql .= " WHERE fk_expeditiondet = " . ((int) $child_line_id);
|
|
if (!$this->db->query($sql)) {
|
|
$error++;
|
|
$this->errors[] = $this->db->lasterror() . " - sql=$sql";
|
|
}
|
|
}
|
|
|
|
$sql = "DELETE FROM " . $this->db->prefix() . "expeditiondet";
|
|
$sql .= " WHERE rowid = " . ((int) $child_line_id);
|
|
if (!$this->db->query($sql)) {
|
|
$error++;
|
|
$this->errors[] = $this->db->lasterror() . " - sql=$sql";
|
|
}
|
|
|
|
if ($error) {
|
|
break;
|
|
}
|
|
}
|
|
if ($error) {
|
|
break;
|
|
}
|
|
}
|
|
} else {
|
|
$error++;
|
|
}
|
|
}
|
|
|
|
if (!$error) {
|
|
// delete batch expedition line
|
|
if (isModEnabled('productbatch')) {
|
|
$sql = "DELETE FROM ".$this->db->prefix()."expeditiondet_batch";
|
|
$sql .= " WHERE fk_expeditiondet = ".((int) $this->id);
|
|
|
|
if (!$this->db->query($sql)) {
|
|
$this->errors[] = $this->db->lasterror()." - sql=$sql";
|
|
$error++;
|
|
}
|
|
}
|
|
|
|
$sql = "DELETE FROM ".$this->db->prefix()."expeditiondet";
|
|
$sql .= " WHERE rowid = ".((int) $this->id);
|
|
|
|
if (!$error && $this->db->query($sql)) {
|
|
// Remove extrafields
|
|
if (!$error) {
|
|
$result = $this->deleteExtraFields();
|
|
if ($result < 0) {
|
|
$this->errors[] = $this->error;
|
|
$error++;
|
|
}
|
|
}
|
|
if (!$error && !$notrigger) {
|
|
// Call trigger
|
|
$result = $this->call_trigger('LINESHIPPING_DELETE', $user);
|
|
if ($result < 0) {
|
|
$this->errors[] = $this->error;
|
|
$error++;
|
|
}
|
|
// End call triggers
|
|
}
|
|
} else {
|
|
$this->errors[] = $this->db->lasterror()." - sql=$sql";
|
|
$error++;
|
|
}
|
|
}
|
|
|
|
if (!$error) {
|
|
$this->db->commit();
|
|
return 1;
|
|
} else {
|
|
foreach ($this->errors as $errmsg) {
|
|
dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
|
|
$this->error .= ($this->error ? ', '.$errmsg : $errmsg);
|
|
}
|
|
$this->db->rollback();
|
|
return -1 * $error;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Update a line in database
|
|
*
|
|
* @param User $user User that modify
|
|
* @param int $notrigger 1 = disable triggers
|
|
* @return int Return integer < 0 if KO, > 0 if OK
|
|
*/
|
|
public function update($user = null, $notrigger = 0)
|
|
{
|
|
global $langs;
|
|
$error = 0;
|
|
|
|
dol_syslog(get_class($this)."::update id=$this->id, entrepot_id=$this->entrepot_id, product_id=$this->fk_product, qty=$this->qty");
|
|
|
|
$this->db->begin();
|
|
|
|
// Clean parameters
|
|
if (empty($this->qty)) {
|
|
$this->qty = 0;
|
|
}
|
|
$qty = price2num($this->qty);
|
|
$remainingQty = 0;
|
|
$batch = null;
|
|
$batch_id = 0;
|
|
$expedition_batch_id = 0;
|
|
if (is_array($this->detail_batch)) { // array of ExpeditionLineBatch
|
|
if (count($this->detail_batch) > 1) {
|
|
dol_syslog(get_class($this).'::update only possible for one batch', LOG_ERR);
|
|
$this->errors[] = 'ErrorBadParameters';
|
|
$error++;
|
|
} else {
|
|
$batch = $this->detail_batch[0]->batch;
|
|
$batch_id = $this->detail_batch[0]->fk_origin_stock;
|
|
$expedition_batch_id = $this->detail_batch[0]->id;
|
|
if ($this->entrepot_id != $this->detail_batch[0]->entrepot_id) {
|
|
dol_syslog(get_class($this).'::update only possible for batch of same warehouse', LOG_ERR);
|
|
$this->errors[] = 'ErrorBadParameters';
|
|
$error++;
|
|
}
|
|
$qty = price2num($this->detail_batch[0]->qty);
|
|
}
|
|
} elseif (!empty($this->detail_batch)) {
|
|
$batch = $this->detail_batch->batch;
|
|
$batch_id = $this->detail_batch->fk_origin_stock;
|
|
$expedition_batch_id = $this->detail_batch->id;
|
|
if ($this->entrepot_id != $this->detail_batch->entrepot_id) {
|
|
dol_syslog(get_class($this).'::update only possible for batch of same warehouse', LOG_ERR);
|
|
$this->errors[] = 'ErrorBadParameters';
|
|
$error++;
|
|
}
|
|
$qty = price2num($this->detail_batch->qty);
|
|
}
|
|
|
|
// check parameters
|
|
if (!isset($this->id) || !isset($this->entrepot_id)) {
|
|
dol_syslog(get_class($this).'::update missing line id and/or warehouse id', LOG_ERR);
|
|
$langs->load('errors');
|
|
$this->errors[] = $langs->trans('ErrorMandatoryParametersNotProvided');
|
|
$error++;
|
|
return -1;
|
|
}
|
|
|
|
// update lot
|
|
|
|
if (!empty($batch) && isModEnabled('productbatch')) {
|
|
$batch_id_str = $batch_id ?? 'null';
|
|
dol_syslog(get_class($this)."::update expedition batch id=$expedition_batch_id, batch_id=$batch_id_str, batch=$batch");
|
|
|
|
if (empty($batch_id) || empty($this->fk_product)) {
|
|
dol_syslog(get_class($this).'::update missing fk_origin_stock (batch_id) and/or fk_product', LOG_ERR);
|
|
$langs->load('errors');
|
|
$this->errors[] = $langs->trans('ErrorMandatoryParametersNotProvided');
|
|
$error++;
|
|
}
|
|
|
|
// fetch remaining lot qty
|
|
$shipmentlinebatch = new ExpeditionLineBatch($this->db);
|
|
$lotArray = $shipmentlinebatch->fetchAll($this->id);
|
|
if (!$error && $lotArray < 0) {
|
|
$this->errors[] = $this->db->lasterror()." - ExpeditionLineBatch::fetchAll";
|
|
$error++;
|
|
} else {
|
|
// calculate new total line qty
|
|
foreach ($lotArray as $lot) {
|
|
if ($expedition_batch_id != $lot->id) {
|
|
$remainingQty += $lot->qty;
|
|
}
|
|
}
|
|
$qty += $remainingQty;
|
|
|
|
//fetch lot details
|
|
|
|
// fetch from product_lot
|
|
require_once DOL_DOCUMENT_ROOT.'/product/stock/class/productlot.class.php';
|
|
$lot = new Productlot($this->db);
|
|
if ($lot->fetch(0, $this->fk_product, $batch) < 0) {
|
|
$this->errors = array_merge($this->errors, $lot->errors);
|
|
$error++;
|
|
}
|
|
if (!$error && !empty($expedition_batch_id)) {
|
|
// delete lot expedition line
|
|
$sql = "DELETE FROM ".MAIN_DB_PREFIX."expeditiondet_batch";
|
|
$sql .= " WHERE fk_expeditiondet = ".((int) $this->id);
|
|
$sql .= " AND rowid = ".((int) $expedition_batch_id);
|
|
|
|
if (!$this->db->query($sql)) {
|
|
$this->errors[] = $this->db->lasterror()." - sql=$sql";
|
|
$error++;
|
|
}
|
|
}
|
|
if (!$error && $this->detail_batch->qty > 0) {
|
|
// create lot expedition line
|
|
if (isset($lot->id)) {
|
|
$shipmentLot = new ExpeditionLineBatch($this->db);
|
|
$shipmentLot->batch = $lot->batch;
|
|
$shipmentLot->eatby = $lot->eatby;
|
|
$shipmentLot->sellby = $lot->sellby;
|
|
$shipmentLot->fk_warehouse = $this->detail_batch->entrepot_id;
|
|
$shipmentLot->qty = $this->detail_batch->qty;
|
|
$shipmentLot->fk_origin_stock = (int) $batch_id;
|
|
if ($shipmentLot->create($this->id) < 0) {
|
|
$this->errors = $shipmentLot->errors;
|
|
$error++;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
if (!$error) {
|
|
// update line
|
|
$sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
|
|
$sql .= " fk_entrepot = ".($this->entrepot_id > 0 ? $this->entrepot_id : 'null');
|
|
$sql .= " , qty = ".((float) price2num($qty, 'MS'));
|
|
$sql .= " WHERE rowid = ".((int) $this->id);
|
|
|
|
if (!$this->db->query($sql)) {
|
|
$this->errors[] = $this->db->lasterror()." - sql=$sql";
|
|
$error++;
|
|
}
|
|
}
|
|
|
|
if (!$error) {
|
|
$result = $this->insertExtraFields();
|
|
if ($result < 0) {
|
|
$this->errors[] = $this->error;
|
|
$error++;
|
|
}
|
|
}
|
|
|
|
if (!$error && !$notrigger) {
|
|
// Call trigger
|
|
$result = $this->call_trigger('LINESHIPPING_MODIFY', $user);
|
|
if ($result < 0) {
|
|
$this->errors[] = $this->error;
|
|
$error++;
|
|
}
|
|
// End call triggers
|
|
}
|
|
if (!$error) {
|
|
$this->db->commit();
|
|
return 1;
|
|
} else {
|
|
foreach ($this->errors as $errmsg) {
|
|
dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
|
|
$this->error .= ($this->error ? ', '.$errmsg : $errmsg);
|
|
}
|
|
$this->db->rollback();
|
|
return -1 * $error;
|
|
}
|
|
}
|
|
}
|