2004-10-19 22:43:23 +02:00
< ? php
2012-10-31 09:50:20 +01:00
/* Copyright ( C ) 2003 - 2008 Rodolphe Quiedeville < rodolphe @ quiedeville . org >
2012-12-30 15:13:49 +01:00
* Copyright ( C ) 2005 - 2012 Regis Houssin < regis . houssin @ capnetworks . com >
2012-10-31 09:50:20 +01:00
* Copyright ( C ) 2007 Franky Van Liedekerke < franky . van . liedekerke @ telenet . be >
* Copyright ( C ) 2006 - 2012 Laurent Destailleur < eldy @ users . sourceforge . net >
2017-10-18 12:10:00 +02:00
* Copyright ( C ) 2011 - 2017 Juanjo Menent < jmenent @ 2 byte . es >
2013-06-20 18:37:23 +02:00
* Copyright ( C ) 2013 Florian Henry < florian . henry @ open - concept . pro >
2014-03-07 11:35:16 +01:00
* Copyright ( C ) 2014 Cedric GROSS < c . gross @ kreiz - it . fr >
2015-04-18 20:50:03 +02:00
* Copyright ( C ) 2014 - 2015 Marcos García < marcosgdf @ gmail . com >
2017-10-18 17:01:17 +02:00
* Copyright ( C ) 2014 - 2017 Francis Appels < francis . appels @ yahoo . com >
2016-02-15 01:37:23 +01:00
* Copyright ( C ) 2015 Claudio Aschieri < c . aschieri @ 19. coop >
2016-02-03 11:33:13 +01:00
* Copyright ( C ) 2016 Ferran Marcet < fmarcet @ 2 byte . es >
2018-06-15 09:47:28 +02:00
* Copyright ( C ) 2018 Nicolas ZABOURI < info @ inovea - conseil . com >
2003-11-13 18:36:45 +01:00
*
* 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
2013-01-16 15:36:08 +01:00
* the Free Software Foundation ; either version 3 of the License , or
2003-11-13 18:36:45 +01:00
* ( 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
2008-09-01 15:22:36 +02:00
* along with this program . If not , see < http :// www . gnu . org / licenses />.
2003-11-13 18:36:45 +01:00
*/
2005-07-09 03:16:31 +02:00
/**
2010-06-05 15:27:11 +02:00
* \file htdocs / expedition / class / expedition . class . php
2009-11-12 14:33:55 +01:00
* \ingroup expedition
* \brief Fichier de la classe de gestion des expeditions
2008-05-26 23:27:50 +02:00
*/
2004-08-14 15:05:12 +02:00
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/class/commonobject.class.php' ;
2015-12-16 16:18:29 +01:00
require_once DOL_DOCUMENT_ROOT . " /core/class/commonobjectline.class.php " ;
2012-09-15 10:01:35 +02:00
if ( ! empty ( $conf -> propal -> enabled )) require_once DOL_DOCUMENT_ROOT . '/comm/propal/class/propal.class.php' ;
if ( ! empty ( $conf -> commande -> enabled )) require_once DOL_DOCUMENT_ROOT . '/commande/class/commande.class.php' ;
2014-03-07 11:35:16 +01:00
if ( ! empty ( $conf -> productbatch -> enabled )) require_once DOL_DOCUMENT_ROOT . '/expedition/class/expeditionbatch.class.php' ;
2006-06-18 16:35:35 +02:00
2016-09-07 12:41:32 +02:00
2008-05-26 23:27:50 +02:00
/**
2012-03-14 14:00:20 +01:00
* Class to manage shipments
2008-05-26 23:27:50 +02:00
*/
2006-06-18 16:35:35 +02:00
class Expedition extends CommonObject
2003-11-13 18:36:45 +01:00
{
2011-09-20 19:19:46 +02:00
public $element = " shipping " ;
public $fk_element = " fk_expedition " ;
public $table_element = " expedition " ;
2015-12-16 16:18:29 +01:00
public $table_element_line = " expeditiondet " ;
2017-10-25 22:02:07 +02:00
public $ismultientitymanaged = 1 ; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
2017-10-07 13:09:31 +02:00
public $picto = 'sending' ;
2017-06-23 11:12:17 +02:00
2008-10-25 13:16:39 +02:00
var $socid ;
2010-03-13 16:52:30 +01:00
var $ref_customer ;
2011-05-26 17:33:33 +02:00
var $ref_int ;
2008-10-25 13:16:39 +02:00
var $brouillon ;
var $entrepot_id ;
2010-09-09 16:06:15 +02:00
var $lines = array ();
2011-06-19 21:47:09 +02:00
var $tracking_number ;
var $tracking_url ;
2012-03-13 17:28:55 +01:00
var $billed ;
2013-07-29 12:17:24 +02:00
var $model_pdf ;
2010-05-08 21:21:57 +02:00
2010-05-13 01:03:33 +02:00
var $trueWeight ;
var $weight_units ;
var $trueWidth ;
var $width_units ;
var $trueHeight ;
var $height_units ;
var $trueDepth ;
var $depth_units ;
// A denormalized value
var $trueSize ;
var $date_delivery ; // Date delivery planed
2015-04-23 23:21:06 +02:00
/**
* @ deprecated
* @ see date_shipping
*/
var $date ;
/**
* @ deprecated
* @ see date_shipping
*/
var $date_expedition ;
/**
* Effective delivery date
* @ var int
*/
public $date_shipping ;
2010-03-13 16:52:30 +01:00
var $date_creation ;
var $date_valid ;
2012-03-14 14:00:20 +01:00
2016-05-06 22:43:50 +02:00
var $meths ;
2013-03-17 14:28:57 +01:00
var $listmeths ; // List of carriers
2013-04-11 21:53:41 +02:00
2016-09-07 12:41:32 +02:00
2016-06-09 22:40:21 +02:00
const STATUS_DRAFT = 0 ;
const STATUS_VALIDATED = 1 ;
const STATUS_CLOSED = 2 ;
2016-09-07 12:41:32 +02:00
2008-10-25 13:16:39 +02:00
/**
2011-09-29 22:32:28 +02:00
* Constructor
*
2012-03-14 14:00:20 +01:00
* @ param DoliDB $db Database handler
2008-10-25 13:16:39 +02:00
*/
2012-07-30 17:17:33 +02:00
function __construct ( $db )
2008-10-25 13:16:39 +02:00
{
2018-02-15 15:27:06 +01:00
global $conf ;
2012-03-14 14:00:20 +01:00
$this -> db = $db ;
2010-12-15 08:12:28 +01:00
$this -> lines = array ();
2008-10-25 13:16:39 +02:00
$this -> products = array ();
2010-01-05 21:03:37 +01:00
2009-12-17 16:52:37 +01:00
// List of long language codes for status
2014-04-23 18:16:22 +02:00
$this -> statuts = array ();
2009-12-17 16:52:37 +01:00
$this -> statuts [ - 1 ] = 'StatusSendingCanceled' ;
$this -> statuts [ 0 ] = 'StatusSendingDraft' ;
$this -> statuts [ 1 ] = 'StatusSendingValidated' ;
2012-03-13 17:28:55 +01:00
$this -> statuts [ 2 ] = 'StatusSendingProcessed' ;
2018-02-15 15:27:06 +01:00
// List of short language codes for status
$this -> statutshorts = array ();
$this -> statutshorts [ - 1 ] = 'StatusSendingCanceledShort' ;
$this -> statutshorts [ 0 ] = 'StatusSendingDraftShort' ;
$this -> statutshorts [ 1 ] = 'StatusSendingValidatedShort' ;
$this -> statutshorts [ 2 ] = 'StatusSendingProcessedShort' ;
/* Status " billed " or not is managed by another field than status
if ( ! empty ( $conf -> global -> WORKFLOW_BILL_ON_SHIPMENT ))
{
$this -> statuts [ 2 ] = 'StatusSendingBilled' ;
$this -> statutshorts [ 2 ] = 'StatusSendingBilledShort' ;
} */
2008-10-25 13:16:39 +02:00
}
2011-02-20 23:53:59 +01:00
2011-09-29 22:32:28 +02:00
/**
2011-02-20 01:20:45 +01:00
* Return next contract ref
2011-09-29 22:32:28 +02:00
*
2014-12-20 15:42:03 +01:00
* @ param Societe $soc Thirdparty object
2011-09-29 22:32:28 +02:00
* @ return string Free reference for contract
2011-02-20 01:20:45 +01:00
*/
function getNextNumRef ( $soc )
{
2016-05-06 22:43:50 +02:00
global $langs , $conf ;
2011-02-20 01:20:45 +01:00
$langs -> load ( " sendings " );
2017-10-07 13:09:31 +02:00
if ( ! empty ( $conf -> global -> EXPEDITION_ADDON_NUMBER ))
{
2015-01-17 18:58:05 +01:00
$mybool = false ;
$file = $conf -> global -> EXPEDITION_ADDON_NUMBER . " .php " ;
$classname = $conf -> global -> EXPEDITION_ADDON_NUMBER ;
2017-10-07 13:09:31 +02:00
// Include file with class
$dirmodels = array_merge ( array ( '/' ), ( array ) $conf -> modules_parts [ 'models' ]);
2011-02-20 01:20:45 +01:00
2017-10-07 13:09:31 +02:00
foreach ( $dirmodels as $reldir ) {
2011-02-20 01:20:45 +01:00
2017-10-07 13:09:31 +02:00
$dir = dol_buildpath ( $reldir . " core/modules/expedition/ " );
2015-01-17 18:58:05 +01:00
2017-10-07 13:09:31 +02:00
// Load file with numbering class (if found)
$mybool |=@ include_once $dir . $file ;
}
2015-01-17 18:58:05 +01:00
2017-10-07 13:09:31 +02:00
if ( ! $mybool )
{
dol_print_error ( '' , " Failed to include file " . $file );
return '' ;
}
2008-10-25 13:16:39 +02:00
2011-02-20 23:53:59 +01:00
$obj = new $classname ();
$numref = " " ;
$numref = $obj -> getNextValue ( $soc , $this );
if ( $numref != " " )
{
return $numref ;
2011-02-20 01:20:45 +01:00
}
else
{
2016-05-06 22:43:50 +02:00
dol_print_error ( $this -> db , get_class ( $this ) . " ::getNextNumRef " . $obj -> error );
2011-02-20 01:20:45 +01:00
return " " ;
}
2017-10-07 13:09:31 +02:00
}
else
{
print $langs -> trans ( " Error " ) . " " . $langs -> trans ( " Error_EXPEDITION_ADDON_NUMBER_NotDefined " );
return " " ;
}
2011-02-20 01:20:45 +01:00
}
2011-02-20 23:53:59 +01:00
2008-10-25 13:16:39 +02:00
/**
2011-09-29 22:32:28 +02:00
* Create expedition en base
*
* @ param User $user Objet du user qui cree
2017-10-07 13:09:31 +02:00
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
2011-09-29 22:32:28 +02:00
* @ return int < 0 si erreur , id expedition creee si ok
2008-10-25 13:16:39 +02:00
*/
2015-12-16 16:18:29 +01:00
function create ( $user , $notrigger = 0 )
2008-10-25 13:16:39 +02:00
{
2016-05-06 22:43:50 +02:00
global $conf , $hookmanager ;
2012-07-29 12:54:19 +02:00
2012-03-23 18:19:50 +01:00
$now = dol_now ();
2013-08-07 15:08:30 +02:00
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/product/stock/class/mouvementstock.class.php' ;
2008-10-25 13:16:39 +02:00
$error = 0 ;
2009-02-03 03:39:28 +01:00
// Clean parameters
$this -> brouillon = 1 ;
2009-04-29 20:02:50 +02:00
$this -> tracking_number = dol_sanitizeFileName ( $this -> tracking_number );
2017-06-10 16:07:56 +02:00
if ( empty ( $this -> fk_project )) $this -> fk_project = 0 ;
2009-02-04 23:12:23 +01:00
2008-10-25 13:16:39 +02:00
$this -> user = $user ;
$this -> db -> begin ();
2009-04-30 18:28:53 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " expedition ( " ;
$sql .= " ref " ;
$sql .= " , entity " ;
2011-05-26 17:33:33 +02:00
$sql .= " , ref_customer " ;
$sql .= " , ref_int " ;
2009-04-30 18:28:53 +02:00
$sql .= " , date_creation " ;
$sql .= " , fk_user_author " ;
$sql .= " , date_expedition " ;
2010-03-13 16:52:30 +01:00
$sql .= " , date_delivery " ;
2009-04-30 18:28:53 +02:00
$sql .= " , fk_soc " ;
2017-05-19 08:50:10 +02:00
$sql .= " , fk_projet " ;
2010-07-29 14:37:09 +02:00
$sql .= " , fk_address " ;
2013-04-11 21:53:41 +02:00
$sql .= " , fk_shipping_method " ;
2009-04-30 18:28:53 +02:00
$sql .= " , tracking_number " ;
$sql .= " , weight " ;
$sql .= " , size " ;
$sql .= " , width " ;
$sql .= " , height " ;
$sql .= " , weight_units " ;
$sql .= " , size_units " ;
2013-04-09 17:18:07 +02:00
$sql .= " , note_private " ;
$sql .= " , note_public " ;
2013-07-29 12:17:24 +02:00
$sql .= " , model_pdf " ;
2015-02-23 09:16:14 +01:00
$sql .= " , fk_incoterms, location_incoterms " ;
2010-03-13 16:52:30 +01:00
$sql .= " ) VALUES ( " ;
2009-04-30 18:28:53 +02:00
$sql .= " '(PROV)' " ;
$sql .= " , " . $conf -> entity ;
2014-05-16 15:03:28 +02:00
$sql .= " , " . ( $this -> ref_customer ? " ' " . $this -> db -> escape ( $this -> ref_customer ) . " ' " : " null " );
$sql .= " , " . ( $this -> ref_int ? " ' " . $this -> db -> escape ( $this -> ref_int ) . " ' " : " null " );
2012-03-23 18:19:50 +01:00
$sql .= " , ' " . $this -> db -> idate ( $now ) . " ' " ;
2009-04-30 18:28:53 +02:00
$sql .= " , " . $user -> id ;
2010-05-13 01:03:33 +02:00
$sql .= " , " . ( $this -> date_expedition > 0 ? " ' " . $this -> db -> idate ( $this -> date_expedition ) . " ' " : " null " );
$sql .= " , " . ( $this -> date_delivery > 0 ? " ' " . $this -> db -> idate ( $this -> date_delivery ) . " ' " : " null " );
2009-04-30 18:28:53 +02:00
$sql .= " , " . $this -> socid ;
2017-05-19 08:50:10 +02:00
$sql .= " , " . $this -> fk_project ;
2009-07-08 12:02:57 +02:00
$sql .= " , " . ( $this -> fk_delivery_address > 0 ? $this -> fk_delivery_address : " null " );
2013-03-22 17:10:17 +01:00
$sql .= " , " . ( $this -> shipping_method_id > 0 ? $this -> shipping_method_id : " null " );
2011-02-24 19:11:12 +01:00
$sql .= " , ' " . $this -> db -> escape ( $this -> tracking_number ) . " ' " ;
2009-04-30 18:28:53 +02:00
$sql .= " , " . $this -> weight ;
2010-05-17 16:13:03 +02:00
$sql .= " , " . $this -> sizeS ; // TODO Should use this->trueDepth
$sql .= " , " . $this -> sizeW ; // TODO Should use this->trueWidth
$sql .= " , " . $this -> sizeH ; // TODO Should use this->trueHeight
2009-04-30 18:28:53 +02:00
$sql .= " , " . $this -> weight_units ;
$sql .= " , " . $this -> size_units ;
2013-04-09 17:18:07 +02:00
$sql .= " , " . ( ! empty ( $this -> note_private ) ? " ' " . $this -> db -> escape ( $this -> note_private ) . " ' " : " null " );
$sql .= " , " . ( ! empty ( $this -> note_public ) ? " ' " . $this -> db -> escape ( $this -> note_public ) . " ' " : " null " );
2013-07-29 12:17:24 +02:00
$sql .= " , " . ( ! empty ( $this -> model_pdf ) ? " ' " . $this -> db -> escape ( $this -> model_pdf ) . " ' " : " null " );
2017-10-07 13:09:31 +02:00
$sql .= " , " . ( int ) $this -> fk_incoterms ;
$sql .= " , ' " . $this -> db -> escape ( $this -> location_incoterms ) . " ' " ;
2009-01-29 21:56:14 +01:00
$sql .= " ) " ;
2008-10-25 13:16:39 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::create " , LOG_DEBUG );
2008-10-25 13:16:39 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> id = $this -> db -> last_insert_id ( MAIN_DB_PREFIX . " expedition " );
2009-04-30 18:28:53 +02:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " expedition " ;
$sql .= " SET ref = '(PROV " . $this -> id . " )' " ;
$sql .= " WHERE rowid = " . $this -> id ;
2010-01-05 21:03:37 +01:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::create " , LOG_DEBUG );
2008-10-25 13:16:39 +02:00
if ( $this -> db -> query ( $sql ))
2009-12-12 10:13:41 +01:00
{
// Insertion des lignes
2011-09-17 21:49:50 +02:00
$num = count ( $this -> lines );
for ( $i = 0 ; $i < $num ; $i ++ )
2009-12-12 10:13:41 +01:00
{
2015-02-16 20:14:16 +01:00
if ( ! isset ( $this -> lines [ $i ] -> detail_batch ))
{ // no batch management
2015-12-16 16:18:29 +01:00
if ( ! $this -> create_line ( $this -> lines [ $i ] -> entrepot_id , $this -> lines [ $i ] -> origin_line_id , $this -> lines [ $i ] -> qty , $this -> lines [ $i ] -> array_options ) > 0 )
2015-02-16 20:14:16 +01:00
{
$error ++ ;
}
2009-12-12 10:13:41 +01:00
}
2015-02-16 20:14:16 +01:00
else
{ // with batch management
2015-12-16 16:18:29 +01:00
if ( ! $this -> create_line_batch ( $this -> lines [ $i ], $this -> lines [ $i ] -> array_options ) > 0 )
2014-03-07 11:35:16 +01:00
{
$error ++ ;
}
}
2009-12-12 10:13:41 +01:00
}
2010-01-05 21:03:37 +01:00
2009-12-12 10:13:41 +01:00
if ( ! $error && $this -> id && $this -> origin_id )
{
$ret = $this -> add_object_linked ();
if ( ! $ret )
{
$error ++ ;
}
}
2016-09-07 12:41:32 +02:00
2018-04-10 12:03:01 +02:00
// Actions on extra fields
2018-08-12 16:29:26 +02:00
if ( ! $error && empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED ))
2015-12-16 16:18:29 +01:00
{
2018-04-10 12:03:01 +02:00
$result = $this -> insertExtraFields ();
if ( $result < 0 )
2015-12-16 16:18:29 +01:00
{
2018-04-10 12:03:01 +02:00
$error ++ ;
2015-12-16 16:18:29 +01:00
}
}
2010-01-05 21:03:37 +01:00
2015-12-16 16:18:29 +01:00
if ( ! $error && ! $notrigger )
2009-12-12 10:13:41 +01:00
{
2017-10-07 13:09:31 +02:00
// Call trigger
$result = $this -> call_trigger ( 'SHIPPING_CREATE' , $user );
if ( $result < 0 ) { $error ++ ; }
// End call triggers
2011-08-31 16:42:47 +02:00
2014-06-10 12:05:31 +02:00
if ( ! $error )
{
$this -> db -> commit ();
return $this -> id ;
}
else
{
foreach ( $this -> errors as $errmsg )
{
dol_syslog ( get_class ( $this ) . " ::create " . $errmsg , LOG_ERR );
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
}
$this -> db -> rollback ();
return - 1 * $error ;
}
2009-12-12 10:13:41 +01:00
}
else
{
$error ++ ;
$this -> error = $this -> db -> lasterror () . " - sql= $sql " ;
$this -> db -> rollback ();
return - 3 ;
}
}
else
{
$error ++ ;
$this -> error = $this -> db -> lasterror () . " - sql= $sql " ;
$this -> db -> rollback ();
return - 2 ;
}
2008-10-25 13:16:39 +02:00
}
else
{
$error ++ ;
$this -> error = $this -> db -> error () . " - sql= $sql " ;
$this -> db -> rollback ();
return - 1 ;
}
}
/**
2011-12-16 15:29:58 +01:00
* Create a expedition line
2012-01-04 23:56:10 +01:00
*
* @ param int $entrepot_id Id of warehouse
* @ param int $origin_line_id Id of source line
* @ param int $qty Quantity
2016-09-12 02:05:38 +02:00
* @ param array $array_options extrafields array
2016-09-07 14:07:29 +02:00
* @ return int < 0 if KO , line_id if OK
2008-10-25 13:16:39 +02:00
*/
2015-12-16 16:18:29 +01:00
function create_line ( $entrepot_id , $origin_line_id , $qty , $array_options = 0 )
2008-10-25 13:16:39 +02:00
{
2017-10-18 17:01:17 +02:00
$expeditionline = new ExpeditionLigne ( $this -> db );
$expeditionline -> fk_expedition = $this -> id ;
$expeditionline -> entrepot_id = $entrepot_id ;
$expeditionline -> fk_origin_line = $origin_line_id ;
$expeditionline -> qty = $qty ;
2017-10-31 17:20:29 +01:00
$expeditionline -> array_options = $array_options ;
2017-06-23 11:12:17 +02:00
2017-10-18 17:01:17 +02:00
if (( $lineId = $expeditionline -> insert ()) < 0 )
2015-12-16 16:18:29 +01:00
{
2018-03-04 12:35:32 +01:00
$this -> errors [] = $expeditionline -> error ;
2015-12-16 16:18:29 +01:00
}
2017-10-18 17:01:17 +02:00
return $lineId ;
2008-10-25 13:16:39 +02:00
}
2015-02-16 20:14:16 +01:00
2014-03-07 11:35:16 +01:00
/**
2015-08-14 23:53:59 +02:00
* Create the detail ( eat - by date ) of the expedition line
2014-03-07 11:35:16 +01:00
*
* @ param object $line_ext full line informations
2015-12-16 16:18:29 +01:00
* @ param array $array_options extrafields array
2014-03-07 11:35:16 +01:00
* @ return int < 0 if KO , > 0 if OK
*/
2015-12-16 16:18:29 +01:00
function create_line_batch ( $line_ext , $array_options = 0 )
2014-03-07 11:35:16 +01:00
{
$error = 0 ;
2015-12-18 18:00:31 +01:00
$stockLocationQty = array (); // associated array with batch qty in stock location
2016-09-07 12:41:32 +02:00
2015-12-18 18:00:31 +01:00
$tab = $line_ext -> detail_batch ;
// create stockLocation Qty array
foreach ( $tab as $detbatch )
2014-03-07 11:35:16 +01:00
{
2015-12-18 18:00:31 +01:00
if ( $detbatch -> entrepot_id )
{
2018-10-11 11:56:52 +02:00
$stockLocationQty [ $detbatch -> entrepot_id ] += $detbatch -> qty ;
2015-12-18 18:00:31 +01:00
}
2015-02-16 20:14:16 +01:00
}
2015-12-18 18:00:31 +01:00
// create shipment lines
foreach ( $stockLocationQty as $stockLocation => $qty )
2015-02-16 20:14:16 +01:00
{
2016-09-07 14:07:29 +02:00
if (( $line_id = $this -> create_line ( $stockLocation , $line_ext -> origin_line_id , $qty , $array_options )) < 0 )
2015-12-18 18:00:31 +01:00
{
$error ++ ;
}
else
2015-02-16 20:14:16 +01:00
{
2015-12-18 18:00:31 +01:00
// create shipment batch lines for stockLocation
foreach ( $tab as $detbatch )
2015-02-16 20:14:16 +01:00
{
2015-12-18 18:00:31 +01:00
if ( $detbatch -> entrepot_id == $stockLocation ){
if ( ! ( $detbatch -> create ( $line_id ) > 0 )) // Create an expeditionlinebatch
{
$error ++ ;
}
2016-09-07 12:41:32 +02:00
}
2014-03-07 11:35:16 +01:00
}
}
}
2015-02-16 20:14:16 +01:00
2014-03-07 11:35:16 +01:00
if ( ! $error ) return 1 ;
else return - 1 ;
}
2008-10-25 13:16:39 +02:00
/**
2011-05-26 17:33:33 +02:00
* Get object and lines from database
2011-09-29 22:32:28 +02:00
*
* @ param int $id Id of object to load
* @ param string $ref Ref of object
* @ param string $ref_ext External reference of object
2017-10-07 13:09:31 +02:00
* @ param string $ref_int Internal reference of other object
2015-02-16 19:14:34 +01:00
* @ return int > 0 if OK , 0 if not found , < 0 if KO
2008-10-25 13:16:39 +02:00
*/
2011-05-26 17:33:33 +02:00
function fetch ( $id , $ref = '' , $ref_ext = '' , $ref_int = '' )
2008-10-25 13:16:39 +02:00
{
global $conf ;
2011-05-26 17:33:33 +02:00
// Check parameters
if ( empty ( $id ) && empty ( $ref ) && empty ( $ref_ext ) && empty ( $ref_int )) return - 1 ;
2010-06-06 17:53:49 +02:00
2018-05-25 13:14:58 +02:00
$sql = " SELECT e.rowid, e.ref, e.fk_soc as socid, e.date_creation, e.ref_customer, e.ref_ext, e.ref_int, e.fk_user_author, e.fk_statut, e.fk_projet, e.billed " ;
2009-08-05 18:42:04 +02:00
$sql .= " , e.weight, e.weight_units, e.size, e.size_units, e.width, e.height " ;
2010-07-29 14:37:09 +02:00
$sql .= " , e.date_expedition as date_expedition, e.model_pdf, e.fk_address, e.date_delivery " ;
2013-04-09 12:03:49 +02:00
$sql .= " , e.fk_shipping_method, e.tracking_number " ;
2013-04-09 17:18:07 +02:00
$sql .= " , e.note_private, e.note_public " ;
2017-10-07 13:09:31 +02:00
$sql .= ', e.fk_incoterms, e.location_incoterms' ;
$sql .= ', i.libelle as libelle_incoterms' ;
2018-05-25 13:14:58 +02:00
$sql .= " , el.fk_source as origin_id, el.sourcetype as origin " ;
2008-10-25 13:16:39 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " expedition as e " ;
2017-05-12 16:55:11 +02:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " element_element as el ON el.fk_target = e.rowid AND el.targettype = ' " . $this -> db -> escape ( $this -> element ) . " ' " ;
2015-02-23 09:16:14 +01:00
$sql .= ' LEFT JOIN ' . MAIN_DB_PREFIX . 'c_incoterms as i ON e.fk_incoterms = i.rowid' ;
2017-05-30 18:50:54 +02:00
$sql .= " WHERE e.entity IN ( " . getEntity ( 'expedition' ) . " ) " ;
2011-05-26 17:33:33 +02:00
if ( $id ) $sql .= " AND e.rowid= " . $id ;
2017-10-07 13:09:31 +02:00
if ( $ref ) $sql .= " AND e.ref=' " . $this -> db -> escape ( $ref ) . " ' " ;
if ( $ref_ext ) $sql .= " AND e.ref_ext=' " . $this -> db -> escape ( $ref_ext ) . " ' " ;
if ( $ref_int ) $sql .= " AND e.ref_int=' " . $this -> db -> escape ( $ref_int ) . " ' " ;
2008-10-25 13:16:39 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch " , LOG_DEBUG );
2011-09-20 15:32:16 +02:00
$result = $this -> db -> query ( $sql );
2008-10-25 13:16:39 +02:00
if ( $result )
{
if ( $this -> db -> num_rows ( $result ))
2009-01-29 21:56:14 +01:00
{
$obj = $this -> db -> fetch_object ( $result );
$this -> id = $obj -> rowid ;
$this -> ref = $obj -> ref ;
$this -> socid = $obj -> socid ;
2010-03-13 16:52:30 +01:00
$this -> ref_customer = $obj -> ref_customer ;
2011-05-26 17:33:33 +02:00
$this -> ref_ext = $obj -> ref_ext ;
$this -> ref_int = $obj -> ref_int ;
2009-01-29 21:56:14 +01:00
$this -> statut = $obj -> fk_statut ;
$this -> user_author_id = $obj -> fk_user_author ;
2010-05-13 01:03:33 +02:00
$this -> date_creation = $this -> db -> jdate ( $obj -> date_creation );
2015-04-23 23:21:06 +02:00
$this -> date = $this -> db -> jdate ( $obj -> date_expedition ); // TODO deprecated
$this -> date_expedition = $this -> db -> jdate ( $obj -> date_expedition ); // TODO deprecated
2010-05-13 01:03:33 +02:00
$this -> date_shipping = $this -> db -> jdate ( $obj -> date_expedition ); // Date real
$this -> date_delivery = $this -> db -> jdate ( $obj -> date_delivery ); // Date planed
2010-07-29 14:37:09 +02:00
$this -> fk_delivery_address = $obj -> fk_address ;
2009-01-29 21:56:14 +01:00
$this -> modelpdf = $obj -> model_pdf ;
2013-04-11 21:53:41 +02:00
$this -> shipping_method_id = $obj -> fk_shipping_method ;
2009-01-29 21:56:14 +01:00
$this -> tracking_number = $obj -> tracking_number ;
2010-09-21 11:44:15 +02:00
$this -> origin = ( $obj -> origin ? $obj -> origin : 'commande' ); // For compatibility
2010-03-13 16:52:30 +01:00
$this -> origin_id = $obj -> origin_id ;
2017-10-12 21:19:24 +02:00
$this -> billed = $obj -> billed ;
2018-05-23 09:47:29 +02:00
$this -> fk_project = $obj -> fk_projet ;
2009-01-29 21:56:14 +01:00
$this -> trueWeight = $obj -> weight ;
$this -> weight_units = $obj -> weight_units ;
$this -> trueWidth = $obj -> width ;
$this -> width_units = $obj -> size_units ;
$this -> trueHeight = $obj -> height ;
$this -> height_units = $obj -> size_units ;
$this -> trueDepth = $obj -> size ;
$this -> depth_units = $obj -> size_units ;
2013-04-11 21:53:41 +02:00
2013-04-09 17:18:07 +02:00
$this -> note_public = $obj -> note_public ;
$this -> note_private = $obj -> note_private ;
2009-01-29 21:56:14 +01:00
// A denormalized value
$this -> trueSize = $obj -> size . " x " . $obj -> width . " x " . $obj -> height ;
$this -> size_units = $obj -> size_units ;
2015-02-23 09:16:14 +01:00
//Incoterms
$this -> fk_incoterms = $obj -> fk_incoterms ;
2015-04-07 02:44:30 +02:00
$this -> location_incoterms = $obj -> location_incoterms ;
2015-02-23 09:16:14 +01:00
$this -> libelle_incoterms = $obj -> libelle_incoterms ;
2015-04-07 02:44:30 +02:00
2009-01-29 21:56:14 +01:00
$this -> db -> free ( $result );
if ( $this -> statut == 0 ) $this -> brouillon = 1 ;
2011-06-19 21:47:09 +02:00
// Tracking url
$this -> GetUrlTrackingStatus ( $obj -> tracking_number );
2009-01-29 21:56:14 +01:00
2013-02-22 16:02:19 +01:00
/*
* Thirparty
*/
$result = $this -> fetch_thirdparty ();
2016-09-07 12:41:32 +02:00
2018-08-12 16:29:26 +02:00
// Retreive extrafields
2018-02-20 19:38:18 +01:00
$this -> fetch_optionals ();
2013-04-11 21:53:41 +02:00
2009-01-29 21:56:14 +01:00
/*
2010-03-13 16:52:30 +01:00
* Lines
2009-01-29 21:56:14 +01:00
*/
$result = $this -> fetch_lines ();
if ( $result < 0 )
{
return - 3 ;
}
return 1 ;
}
else
{
2015-02-16 19:14:34 +01:00
dol_syslog ( get_class ( $this ) . '::Fetch no expedition found' , LOG_ERR );
2015-12-24 14:27:12 +01:00
$this -> error = 'Delivery with id ' . $id . ' not found' ;
2015-02-16 19:14:34 +01:00
return 0 ;
2009-01-29 21:56:14 +01:00
}
2008-10-25 13:16:39 +02:00
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
/**
2011-11-08 00:03:19 +01:00
* Validate object and update stock if option enabled
2011-09-29 22:32:28 +02:00
*
2011-11-08 00:03:19 +01:00
* @ param User $user Object user that validate
2017-10-07 13:09:31 +02:00
* @ param int $notrigger 1 = Does not execute triggers , 0 = execute triggers
2011-11-08 00:03:19 +01:00
* @ return int < 0 if OK , > 0 if KO
2008-10-25 13:16:39 +02:00
*/
2014-10-27 03:24:45 +01:00
function valid ( $user , $notrigger = 0 )
2008-10-25 13:16:39 +02:00
{
2011-05-30 20:49:31 +02:00
global $conf , $langs ;
2011-08-31 16:42:47 +02:00
2017-10-07 13:09:31 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
2008-10-25 13:16:39 +02:00
2012-01-04 23:56:10 +01:00
dol_syslog ( get_class ( $this ) . " ::valid " );
2008-10-25 13:16:39 +02:00
2009-05-07 23:38:56 +02:00
// Protection
if ( $this -> statut )
{
2012-01-04 23:56:10 +01:00
dol_syslog ( get_class ( $this ) . " ::valid no draft status " , LOG_WARNING );
2009-05-07 23:38:56 +02:00
return 0 ;
}
2017-10-07 13:09:31 +02:00
if ( ! (( empty ( $conf -> global -> MAIN_USE_ADVANCED_PERMS ) && ! empty ( $user -> rights -> expedition -> creer ))
|| ( ! empty ( $conf -> global -> MAIN_USE_ADVANCED_PERMS ) && ! empty ( $user -> rights -> expedition -> shipping_advance -> validate ))))
2009-05-07 23:38:56 +02:00
{
$this -> error = 'Permission denied' ;
2012-01-04 23:56:10 +01:00
dol_syslog ( get_class ( $this ) . " ::valid " . $this -> error , LOG_ERR );
2009-05-07 23:38:56 +02:00
return - 1 ;
}
2008-10-25 13:16:39 +02:00
$this -> db -> begin ();
2010-06-17 22:24:11 +02:00
$error = 0 ;
2009-05-07 23:38:56 +02:00
// Define new ref
2011-02-20 01:20:45 +01:00
$soc = new Societe ( $this -> db );
$soc -> fetch ( $this -> socid );
2008-10-25 13:16:39 +02:00
2011-02-20 01:20:45 +01:00
// Class of company linked to order
$result = $soc -> set_as_client ();
// Define new ref
2015-10-06 16:38:27 +02:00
if ( ! $error && ( preg_match ( '/^[\(]?PROV/i' , $this -> ref ) || empty ( $this -> ref ))) // empty should not happened, but when it occurs, the test save life
2011-02-20 01:20:45 +01:00
{
2011-06-15 21:53:37 +02:00
$numref = $this -> getNextNumRef ( $soc );
2011-02-20 01:20:45 +01:00
}
else
{
2011-06-15 21:53:37 +02:00
$numref = " EXP " . $this -> id ;
2011-02-20 01:20:45 +01:00
}
2017-10-07 13:09:31 +02:00
$this -> newref = $numref ;
2011-02-20 23:53:59 +01:00
2010-05-18 09:22:58 +02:00
$now = dol_now ();
2009-05-07 23:38:56 +02:00
// Validate
2010-03-13 16:52:30 +01:00
$sql = " UPDATE " . MAIN_DB_PREFIX . " expedition SET " ;
2011-06-15 21:53:37 +02:00
$sql .= " ref=' " . $numref . " ' " ;
2009-05-07 23:38:56 +02:00
$sql .= " , fk_statut = 1 " ;
2010-05-18 09:22:58 +02:00
$sql .= " , date_valid = ' " . $this -> db -> idate ( $now ) . " ' " ;
2009-05-07 23:38:56 +02:00
$sql .= " , fk_user_valid = " . $user -> id ;
$sql .= " WHERE rowid = " . $this -> id ;
2008-10-25 13:16:39 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::valid update expedition " , LOG_DEBUG );
2009-05-07 23:38:56 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql )
{
2010-05-18 09:22:58 +02:00
$this -> error = $this -> db -> lasterror ();
2009-05-07 23:38:56 +02:00
$error ++ ;
}
2009-01-29 13:39:52 +01:00
2010-06-17 22:24:11 +02:00
// If stock increment is done on sending (recommanded choice)
2012-09-15 11:21:22 +02:00
if ( ! $error && ! empty ( $conf -> stock -> enabled ) && ! empty ( $conf -> global -> STOCK_CALCULATE_ON_SHIPMENT ))
2009-05-07 23:38:56 +02:00
{
2012-08-22 23:11:24 +02:00
require_once DOL_DOCUMENT_ROOT . '/product/stock/class/mouvementstock.class.php' ;
2011-08-31 16:42:47 +02:00
2011-05-30 20:49:31 +02:00
$langs -> load ( " agenda " );
2010-06-17 22:24:11 +02:00
// Loop on each product line to add a stock movement
2015-08-14 23:53:59 +02:00
$sql = " SELECT cd.fk_product, cd.subprice, " ;
$sql .= " ed.rowid, ed.qty, ed.fk_entrepot, " ;
2015-08-19 10:42:54 +02:00
$sql .= " edb.rowid as edbrowid, edb.eatby, edb.sellby, edb.batch, edb.qty as edbqty, edb.fk_origin_stock " ;
2011-11-08 00:03:19 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " commandedet as cd, " ;
$sql .= " " . MAIN_DB_PREFIX . " expeditiondet as ed " ;
2015-08-14 23:53:59 +02:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " expeditiondet_batch as edb on edb.fk_expeditiondet = ed.rowid " ;
2010-06-17 22:24:11 +02:00
$sql .= " WHERE ed.fk_expedition = " . $this -> id ;
$sql .= " AND cd.rowid = ed.fk_origin_line " ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::valid select details " , LOG_DEBUG );
2010-06-17 22:24:11 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2009-05-07 23:38:56 +02:00
{
2015-08-19 10:42:54 +02:00
$cpt = $this -> db -> num_rows ( $resql );
for ( $i = 0 ; $i < $cpt ; $i ++ )
2009-05-07 23:38:56 +02:00
{
2014-10-06 14:10:27 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2015-08-27 19:52:43 +02:00
if ( empty ( $obj -> edbrowid ))
{
$qty = $obj -> qty ;
}
else
{
$qty = $obj -> edbqty ;
}
if ( $qty <= 0 ) continue ;
2015-08-14 23:53:59 +02:00
dol_syslog ( get_class ( $this ) . " ::valid movement index " . $i . " ed.rowid= " . $obj -> rowid . " edb.rowid= " . $obj -> edbrowid );
2010-06-17 22:24:11 +02:00
2011-05-24 01:46:39 +02:00
//var_dump($this->lines[$i]);
$mouvS = new MouvementStock ( $this -> db );
2014-03-15 02:18:39 +01:00
$mouvS -> origin = & $this ;
2016-09-07 12:41:32 +02:00
2015-08-14 23:53:59 +02:00
if ( empty ( $obj -> edbrowid ))
{
// line without batch detail
2016-09-07 12:41:32 +02:00
2016-05-27 16:49:55 +02:00
// We decrement stock of product (and sub-products) -> update table llx_product_stock (key of this table is fk_product+fk_entrepot) and add a movement record.
2015-08-27 19:52:43 +02:00
$result = $mouvS -> livraison ( $user , $obj -> fk_product , $obj -> fk_entrepot , $qty , $obj -> subprice , $langs -> trans ( " ShipmentValidatedInDolibarr " , $numref ));
2015-08-19 10:42:54 +02:00
if ( $result < 0 ) {
2016-09-07 12:41:32 +02:00
$error ++ ;
$this -> errors [] = $mouvS -> error ;
2017-07-04 21:45:38 +02:00
$this -> errors = array_merge ( $this -> errors , $mouvS -> errors );
2016-05-17 01:10:11 +02:00
break ;
2015-08-15 11:21:50 +02:00
}
2015-08-14 23:53:59 +02:00
}
else
2015-02-16 20:14:16 +01:00
{
2015-08-14 23:53:59 +02:00
// line with batch detail
2016-09-07 12:41:32 +02:00
2016-05-27 16:49:55 +02:00
// We decrement stock of product (and sub-products) -> update table llx_product_stock (key of this table is fk_product+fk_entrepot) and add a movement record.
2017-10-07 13:09:31 +02:00
// Note: ->fk_origin_stock = id into table llx_product_batch (may be rename into llx_product_stock_batch in another version)
2016-09-28 00:22:29 +02:00
$result = $mouvS -> livraison ( $user , $obj -> fk_product , $obj -> fk_entrepot , $qty , $obj -> subprice , $langs -> trans ( " ShipmentValidatedInDolibarr " , $numref ), '' , $this -> db -> jdate ( $obj -> eatby ), $this -> db -> jdate ( $obj -> sellby ), $obj -> batch , $obj -> fk_origin_stock );
2015-08-14 23:53:59 +02:00
if ( $result < 0 ) {
2016-09-07 12:41:32 +02:00
$error ++ ;
$this -> errors [] = $mouvS -> error ;
2017-07-04 21:48:38 +02:00
$this -> errors = array_merge ( $this -> errors , $mouvS -> errors );
2016-05-17 01:10:11 +02:00
break ;
2014-03-07 11:35:16 +01:00
}
}
2009-05-07 23:38:56 +02:00
}
2010-06-17 22:24:11 +02:00
}
else
{
$this -> db -> rollback ();
$this -> error = $this -> db -> error ();
return - 2 ;
2009-05-07 23:38:56 +02:00
}
2016-09-07 12:41:32 +02:00
2016-05-27 16:49:55 +02:00
}
// Change status of order to "shipment in process"
$ret = $this -> setStatut ( Commande :: STATUS_SHIPMENTONPROCESS , $this -> origin_id , $this -> origin );
2016-10-30 16:34:46 +01:00
2017-10-07 13:09:31 +02:00
if ( ! $ret )
2016-05-27 16:49:55 +02:00
{
2017-10-07 13:09:31 +02:00
$error ++ ;
2009-05-07 23:38:56 +02:00
}
2016-09-07 12:41:32 +02:00
2014-10-27 03:24:45 +01:00
if ( ! $error && ! $notrigger )
{
2017-10-07 13:09:31 +02:00
// Call trigger
$result = $this -> call_trigger ( 'SHIPPING_VALIDATE' , $user );
if ( $result < 0 ) { $error ++ ; }
// End call triggers
2014-10-27 03:24:45 +01:00
}
2009-05-07 23:38:56 +02:00
if ( ! $error )
{
2017-10-07 13:09:31 +02:00
$this -> oldref = $this -> ref ;
2012-08-22 23:11:24 +02:00
// Rename directory if dir was a temporary ref
if ( preg_match ( '/^[\(]?PROV/i' , $this -> ref ))
{
// On renomme repertoire ($this->ref = ancienne ref, $numfa = nouvelle ref)
2013-11-05 15:29:33 +01:00
// in order not to lose the attached files
2012-08-22 23:11:24 +02:00
$oldref = dol_sanitizeFileName ( $this -> ref );
$newref = dol_sanitizeFileName ( $numref );
$dirsource = $conf -> expedition -> dir_output . '/sending/' . $oldref ;
$dirdest = $conf -> expedition -> dir_output . '/sending/' . $newref ;
if ( file_exists ( $dirsource ))
{
dol_syslog ( get_class ( $this ) . " ::valid rename dir " . $dirsource . " into " . $dirdest );
if ( @ rename ( $dirsource , $dirdest ))
{
2017-10-07 13:09:31 +02:00
dol_syslog ( " Rename ok " );
// Rename docs starting with $oldref with $newref
$listoffiles = dol_dir_list ( $conf -> expedition -> dir_output . '/sending/' . $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 );
}
2012-08-22 23:11:24 +02:00
}
}
2009-05-07 23:38:56 +02:00
}
}
2011-06-17 15:39:38 +02:00
// Set new ref and current status
2009-05-07 23:38:56 +02:00
if ( ! $error )
{
2011-06-15 21:53:37 +02:00
$this -> ref = $numref ;
2011-06-17 15:39:38 +02:00
$this -> statut = 1 ;
2009-05-07 23:38:56 +02:00
}
if ( ! $error )
{
$this -> db -> commit ();
return 1 ;
2008-10-25 13:16:39 +02:00
}
else
{
2010-05-18 09:22:58 +02:00
foreach ( $this -> errors as $errmsg )
{
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::valid " . $errmsg , LOG_ERR );
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2010-05-18 09:22:58 +02:00
}
2008-10-25 13:16:39 +02:00
$this -> db -> rollback ();
2010-05-18 09:22:58 +02:00
return - 1 * $error ;
2008-10-25 13:16:39 +02:00
}
}
/**
2016-01-13 20:41:32 +01:00
* Create a delivery receipt from a shipment
2011-09-29 22:32:28 +02:00
*
2016-01-13 20:41:32 +01:00
* @ param User $user User
2011-09-29 22:32:28 +02:00
* @ return int < 0 if KO , >= 0 if OK
2008-10-25 13:16:39 +02:00
*/
function create_delivery ( $user )
{
global $conf ;
if ( $conf -> livraison_bon -> enabled )
{
2012-11-03 21:14:23 +01:00
if ( $this -> statut == 1 || $this -> statut == 2 )
2009-08-05 18:42:04 +02:00
{
2009-11-12 14:33:55 +01:00
// Expedition validee
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/livraison/class/livraison.class.php' ;
2010-03-13 16:52:30 +01:00
$delivery = new Livraison ( $this -> db );
$result = $delivery -> create_from_sending ( $user , $this -> id );
2009-08-05 18:42:04 +02:00
if ( $result > 0 )
{
return $result ;
}
else
{
2010-03-13 16:52:30 +01:00
$this -> error = $delivery -> error ;
2009-08-05 18:42:04 +02:00
return $result ;
}
}
else return 0 ;
2008-10-25 13:16:39 +02:00
}
else return 0 ;
}
/**
2016-11-14 13:40:41 +01:00
* Add an expedition line .
2015-02-16 20:46:54 +01:00
* If STOCK_WAREHOUSE_NOT_REQUIRED_FOR_SHIPMENTS is set , you can add a shipment line , with no stock source defined
* If STOCK_MUST_BE_ENOUGH_FOR_SHIPMENT is not set , you can add a shipment line , even if not enough into stock
2012-01-04 23:56:10 +01:00
*
* @ param int $entrepot_id Id of warehouse
2015-02-16 20:46:54 +01:00
* @ param int $id Id of source line ( order line )
2012-01-04 23:56:10 +01:00
* @ param int $qty Quantity
2016-11-14 13:40:41 +01:00
* @ param array $array_options extrafields array
2012-01-04 23:56:10 +01:00
* @ return int < 0 if KO , > 0 if OK
2008-10-25 13:16:39 +02:00
*/
2015-12-16 16:18:29 +01:00
function addline ( $entrepot_id , $id , $qty , $array_options = 0 )
2008-10-25 13:16:39 +02:00
{
2014-03-15 14:59:27 +01:00
global $conf , $langs ;
2014-10-18 16:27:15 +02:00
2011-09-17 21:49:50 +02:00
$num = count ( $this -> lines );
2010-03-13 16:52:30 +01:00
$line = new ExpeditionLigne ( $this -> db );
2008-10-25 13:16:39 +02:00
2010-03-13 16:52:30 +01:00
$line -> entrepot_id = $entrepot_id ;
$line -> origin_line_id = $id ;
$line -> qty = $qty ;
2014-10-18 16:27:15 +02:00
2015-02-16 20:46:54 +01:00
$orderline = new OrderLine ( $this -> db );
$orderline -> fetch ( $id );
2014-10-18 16:27:15 +02:00
2015-06-03 21:21:20 +02:00
if ( ! empty ( $conf -> stock -> enabled ) && ! empty ( $orderline -> fk_product ))
2015-02-16 20:14:16 +01:00
{
2015-06-03 21:21:20 +02:00
$fk_product = $orderline -> fk_product ;
2015-02-16 20:46:54 +01:00
if ( ! ( $entrepot_id > 0 ) && empty ( $conf -> global -> STOCK_WAREHOUSE_NOT_REQUIRED_FOR_SHIPMENTS ))
{
2017-10-07 13:09:31 +02:00
$langs -> load ( " errors " );
2015-02-16 20:46:54 +01:00
$this -> error = $langs -> trans ( " ErrorWarehouseRequiredIntoShipmentLine " );
return - 1 ;
}
2014-10-18 16:27:15 +02:00
2016-02-14 22:20:42 +01:00
if ( $conf -> global -> STOCK_MUST_BE_ENOUGH_FOR_SHIPMENT )
2014-03-15 14:59:27 +01:00
{
2017-10-07 13:09:31 +02:00
// Check must be done for stock of product into warehouse if $entrepot_id defined
2014-03-15 14:59:27 +01:00
$product = new Product ( $this -> db );
$result = $product -> fetch ( $fk_product );
2014-10-18 16:27:15 +02:00
2016-02-14 22:20:42 +01:00
if ( $entrepot_id > 0 ) {
2016-12-05 18:14:03 +01:00
$product -> load_stock ( 'warehouseopen' );
2016-02-01 13:40:02 +01:00
$product_stock = $product -> stock_warehouse [ $entrepot_id ] -> real ;
2016-02-14 22:20:42 +01:00
}
2016-02-01 13:40:02 +01:00
else
$product_stock = $product -> stock_reel ;
2014-10-18 16:27:15 +02:00
2016-04-29 20:42:35 +02:00
$product_type = $product -> type ;
2016-02-14 22:20:42 +01:00
if ( $product_type == 0 && $product_stock < $qty )
2015-02-16 20:14:16 +01:00
{
2017-10-07 13:09:31 +02:00
$langs -> load ( " errors " );
$this -> error = $langs -> trans ( 'ErrorStockIsNotEnoughToAddProductOnShipment' , $product -> ref );
2014-03-15 14:59:27 +01:00
$this -> db -> rollback ();
return - 3 ;
}
}
}
2016-09-07 12:41:32 +02:00
2016-11-14 13:40:41 +01:00
// If product need a batch number, we should not have called this function but addline_batch instead.
if ( ! empty ( $conf -> productbatch -> enabled ) && ! empty ( $orderline -> fk_product ) && ! empty ( $orderline -> product_tobatch ))
{
2017-10-07 13:09:31 +02:00
$this -> error = 'ADDLINE_WAS_CALLED_INSTEAD_OF_ADDLINEBATCH' ;
return - 4 ;
2016-11-14 13:40:41 +01:00
}
2017-06-23 11:12:17 +02:00
2015-12-16 16:18:29 +01:00
// extrafields
if ( empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED ) && is_array ( $array_options ) && count ( $array_options ) > 0 ) // For avoid conflicts if trigger used
$line -> array_options = $array_options ;
2016-09-07 12:41:32 +02:00
2010-12-15 08:12:28 +01:00
$this -> lines [ $num ] = $line ;
2008-10-25 13:16:39 +02:00
}
2017-10-07 13:09:31 +02:00
/**
2014-03-07 11:35:16 +01:00
* Add a shipment line with batch record
*
* @ param array $dbatch Array of value ( key 'detail' -> Array , key 'qty' total quantity for line , key ix_l : original line index )
2015-12-16 16:18:29 +01:00
* @ param array $array_options extrafields array
2015-02-16 20:14:16 +01:00
* @ return int < 0 if KO , > 0 if OK
2014-03-07 11:35:16 +01:00
*/
2015-12-16 16:18:29 +01:00
function addline_batch ( $dbatch , $array_options = 0 )
2014-03-07 11:35:16 +01:00
{
2016-02-03 11:33:13 +01:00
global $conf , $langs ;
2016-02-15 01:37:23 +01:00
2014-03-07 11:35:16 +01:00
$num = count ( $this -> lines );
2015-02-16 20:14:16 +01:00
if ( $dbatch [ 'qty' ] > 0 )
{
2014-03-07 11:35:16 +01:00
$line = new ExpeditionLigne ( $this -> db );
$tab = array ();
2015-02-16 20:14:16 +01:00
foreach ( $dbatch [ 'detail' ] as $key => $value )
{
if ( $value [ 'q' ] > 0 )
{
2015-08-14 23:53:59 +02:00
// $value['q']=qty to move
// $value['id_batch']=id into llx_product_batch of record to move
2015-08-19 10:42:54 +02:00
//var_dump($value);
2015-08-20 14:36:32 +02:00
2017-10-07 13:09:31 +02:00
$linebatch = new ExpeditionLineBatch ( $this -> db );
2015-02-16 20:14:16 +01:00
$ret = $linebatch -> fetchFromStock ( $value [ 'id_batch' ]); // load serial, sellby, eatby
if ( $ret < 0 )
{
2014-03-07 11:35:16 +01:00
$this -> error = $linebatch -> error ;
return - 1 ;
}
2018-10-11 11:56:52 +02:00
$linebatch -> qty = $value [ 'q' ];
2014-03-07 11:35:16 +01:00
$tab [] = $linebatch ;
2015-02-16 20:51:05 +01:00
if ( $conf -> global -> STOCK_MUST_BE_ENOUGH_FOR_SHIPMENT )
{
2016-02-03 11:33:13 +01:00
require_once DOL_DOCUMENT_ROOT . '/product/class/productbatch.class.php' ;
$prod_batch = new Productbatch ( $this -> db );
$prod_batch -> fetch ( $value [ 'id_batch' ]);
2018-10-11 11:56:52 +02:00
if ( $prod_batch -> qty < $linebatch -> qty )
2016-02-03 11:33:13 +01:00
{
2017-10-07 13:09:31 +02:00
$langs -> load ( " errors " );
$this -> errors [] = $langs -> trans ( 'ErrorStockIsNotEnoughToAddProductOnShipment' , $prod_batch -> fk_product );
2016-02-03 11:33:13 +01:00
dol_syslog ( get_class ( $this ) . " ::addline_batch error=Product " . $prod_batch -> batch . " : " . $this -> errorsToString (), LOG_ERR );
$this -> db -> rollback ();
return - 1 ;
}
2015-02-16 20:51:05 +01:00
}
2016-09-07 12:41:32 +02:00
2015-08-14 23:53:59 +02:00
//var_dump($linebatch);
2014-03-07 11:35:16 +01:00
}
}
$line -> entrepot_id = $linebatch -> entrepot_id ;
$line -> origin_line_id = $dbatch [ 'ix_l' ];
$line -> qty = $dbatch [ 'qty' ];
$line -> detail_batch = $tab ;
2015-08-14 23:53:59 +02:00
2015-12-16 16:18:29 +01:00
// extrafields
if ( empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED ) && is_array ( $array_options ) && count ( $array_options ) > 0 ) // For avoid conflicts if trigger used
$line -> array_options = $array_options ;
2016-09-07 12:41:32 +02:00
2015-08-14 23:53:59 +02:00
//var_dump($line);
2014-03-07 11:35:16 +01:00
$this -> lines [ $num ] = $line ;
2016-02-03 11:33:13 +01:00
return 1 ;
2014-03-07 11:35:16 +01:00
}
}
2017-10-07 13:09:31 +02:00
/**
* Update database
*
* @ param User $user User that modify
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
* @ return int < 0 if KO , > 0 if OK
*/
function update ( $user = null , $notrigger = 0 )
{
global $conf ;
2010-05-17 16:13:03 +02:00
$error = 0 ;
// Clean parameters
if ( isset ( $this -> ref )) $this -> ref = trim ( $this -> ref );
if ( isset ( $this -> entity )) $this -> entity = trim ( $this -> entity );
if ( isset ( $this -> ref_customer )) $this -> ref_customer = trim ( $this -> ref_customer );
if ( isset ( $this -> socid )) $this -> socid = trim ( $this -> socid );
if ( isset ( $this -> fk_user_author )) $this -> fk_user_author = trim ( $this -> fk_user_author );
if ( isset ( $this -> fk_user_valid )) $this -> fk_user_valid = trim ( $this -> fk_user_valid );
2013-02-15 19:56:51 +01:00
if ( isset ( $this -> fk_delivery_address )) $this -> fk_delivery_address = trim ( $this -> fk_delivery_address );
2013-03-22 17:10:17 +01:00
if ( isset ( $this -> shipping_method_id )) $this -> shipping_method_id = trim ( $this -> shipping_method_id );
2010-05-17 16:13:03 +02:00
if ( isset ( $this -> tracking_number )) $this -> tracking_number = trim ( $this -> tracking_number );
2015-04-07 02:44:30 +02:00
if ( isset ( $this -> statut )) $this -> statut = ( int ) $this -> statut ;
2010-05-17 16:13:03 +02:00
if ( isset ( $this -> trueDepth )) $this -> trueDepth = trim ( $this -> trueDepth );
if ( isset ( $this -> trueWidth )) $this -> trueWidth = trim ( $this -> trueWidth );
if ( isset ( $this -> trueHeight )) $this -> trueHeight = trim ( $this -> trueHeight );
if ( isset ( $this -> size_units )) $this -> size_units = trim ( $this -> size_units );
if ( isset ( $this -> weight_units )) $this -> weight_units = trim ( $this -> weight_units );
if ( isset ( $this -> trueWeight )) $this -> weight = trim ( $this -> trueWeight );
2013-04-09 17:18:07 +02:00
if ( isset ( $this -> note_private )) $this -> note = trim ( $this -> note_private );
if ( isset ( $this -> note_public )) $this -> note = trim ( $this -> note_public );
2016-02-25 13:46:38 +01:00
if ( isset ( $this -> modelpdf )) $this -> modelpdf = trim ( $this -> modelpdf );
2010-05-17 16:13:03 +02:00
// Check parameters
// Put here code to add control on parameters values
2017-10-07 13:09:31 +02:00
// Update request
$sql = " UPDATE " . MAIN_DB_PREFIX . " expedition SET " ;
2010-05-17 16:13:03 +02:00
2010-08-24 16:42:18 +02:00
$sql .= " tms= " . ( dol_strlen ( $this -> tms ) != 0 ? " ' " . $this -> db -> idate ( $this -> tms ) . " ' " : 'null' ) . " , " ;
2011-02-24 19:11:12 +01:00
$sql .= " ref= " . ( isset ( $this -> ref ) ? " ' " . $this -> db -> escape ( $this -> ref ) . " ' " : " null " ) . " , " ;
$sql .= " ref_customer= " . ( isset ( $this -> ref_customer ) ? " ' " . $this -> db -> escape ( $this -> ref_customer ) . " ' " : " null " ) . " , " ;
2010-05-17 16:13:03 +02:00
$sql .= " fk_soc= " . ( isset ( $this -> socid ) ? $this -> socid : " null " ) . " , " ;
2010-08-24 16:42:18 +02:00
$sql .= " date_creation= " . ( dol_strlen ( $this -> date_creation ) != 0 ? " ' " . $this -> db -> idate ( $this -> date_creation ) . " ' " : 'null' ) . " , " ;
2010-05-17 16:13:03 +02:00
$sql .= " fk_user_author= " . ( isset ( $this -> fk_user_author ) ? $this -> fk_user_author : " null " ) . " , " ;
2010-08-24 16:42:18 +02:00
$sql .= " date_valid= " . ( dol_strlen ( $this -> date_valid ) != 0 ? " ' " . $this -> db -> idate ( $this -> date_valid ) . " ' " : 'null' ) . " , " ;
2010-05-17 16:13:03 +02:00
$sql .= " fk_user_valid= " . ( isset ( $this -> fk_user_valid ) ? $this -> fk_user_valid : " null " ) . " , " ;
2010-08-24 16:42:18 +02:00
$sql .= " date_expedition= " . ( dol_strlen ( $this -> date_expedition ) != 0 ? " ' " . $this -> db -> idate ( $this -> date_expedition ) . " ' " : 'null' ) . " , " ;
$sql .= " date_delivery= " . ( dol_strlen ( $this -> date_delivery ) != 0 ? " ' " . $this -> db -> idate ( $this -> date_delivery ) . " ' " : 'null' ) . " , " ;
2013-02-15 19:56:51 +01:00
$sql .= " fk_address= " . ( isset ( $this -> fk_delivery_address ) ? $this -> fk_delivery_address : " null " ) . " , " ;
2013-03-22 17:10:17 +01:00
$sql .= " fk_shipping_method= " . (( isset ( $this -> shipping_method_id ) && $this -> shipping_method_id > 0 ) ? $this -> shipping_method_id : " null " ) . " , " ;
2011-02-24 19:11:12 +01:00
$sql .= " tracking_number= " . ( isset ( $this -> tracking_number ) ? " ' " . $this -> db -> escape ( $this -> tracking_number ) . " ' " : " null " ) . " , " ;
2010-05-17 16:13:03 +02:00
$sql .= " fk_statut= " . ( isset ( $this -> statut ) ? $this -> statut : " null " ) . " , " ;
2018-05-21 16:22:06 +02:00
$sql .= " fk_projet= " . ( isset ( $this -> fk_project ) ? $this -> fk_project : " null " ) . " , " ;
2012-11-18 16:21:38 +01:00
$sql .= " height= " . (( $this -> trueHeight != '' ) ? $this -> trueHeight : " null " ) . " , " ;
$sql .= " width= " . (( $this -> trueWidth != '' ) ? $this -> trueWidth : " null " ) . " , " ;
2010-05-17 16:13:03 +02:00
$sql .= " size_units= " . ( isset ( $this -> size_units ) ? $this -> size_units : " null " ) . " , " ;
2012-11-18 16:21:38 +01:00
$sql .= " size= " . (( $this -> trueDepth != '' ) ? $this -> trueDepth : " null " ) . " , " ;
2010-05-17 16:13:03 +02:00
$sql .= " weight_units= " . ( isset ( $this -> weight_units ) ? $this -> weight_units : " null " ) . " , " ;
2012-11-18 16:21:38 +01:00
$sql .= " weight= " . (( $this -> trueWeight != '' ) ? $this -> trueWeight : " null " ) . " , " ;
2013-04-09 17:18:07 +02:00
$sql .= " note_private= " . ( isset ( $this -> note_private ) ? " ' " . $this -> db -> escape ( $this -> note_private ) . " ' " : " null " ) . " , " ;
$sql .= " note_public= " . ( isset ( $this -> note_public ) ? " ' " . $this -> db -> escape ( $this -> note_public ) . " ' " : " null " ) . " , " ;
2016-02-25 13:46:38 +01:00
$sql .= " model_pdf= " . ( isset ( $this -> modelpdf ) ? " ' " . $this -> db -> escape ( $this -> modelpdf ) . " ' " : " null " ) . " , " ;
2010-05-17 16:13:03 +02:00
$sql .= " entity= " . $conf -> entity ;
2017-10-07 13:09:31 +02:00
$sql .= " WHERE rowid= " . $this -> id ;
2010-05-17 16:13:03 +02:00
$this -> db -> begin ();
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::update " , LOG_DEBUG );
2017-10-07 13:09:31 +02:00
$resql = $this -> db -> query ( $sql );
if ( ! $resql ) { $error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror (); }
2010-05-17 16:13:03 +02:00
if ( ! $error )
{
if ( ! $notrigger )
{
2017-10-07 13:09:31 +02:00
// Call trigger
$result = $this -> call_trigger ( 'SHIPPING_MODIFY' , $user );
if ( $result < 0 ) { $error ++ ; }
// End call triggers
}
2010-05-17 16:13:03 +02:00
}
2017-10-07 13:09:31 +02:00
// Commit or rollback
2010-05-17 16:13:03 +02:00
if ( $error )
{
foreach ( $this -> errors as $errmsg )
{
2017-10-07 13:09:31 +02:00
dol_syslog ( get_class ( $this ) . " ::update " . $errmsg , LOG_ERR );
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
2010-05-17 16:13:03 +02:00
}
$this -> db -> rollback ();
return - 1 * $error ;
}
else
{
$this -> db -> commit ();
return 1 ;
}
2017-10-07 13:09:31 +02:00
}
2010-05-17 16:13:03 +02:00
2015-10-14 12:16:54 +02:00
/**
2014-04-26 17:27:59 +02:00
* Delete shipment .
2018-03-11 00:00:24 +01:00
* Warning , do not delete a shipment if a delivery is linked to ( with table llx_element_element )
2011-09-29 22:32:28 +02:00
*
2014-04-26 17:27:59 +02:00
* @ return int > 0 if OK , 0 if deletion done but failed to delete files , < 0 if KO
2008-10-25 13:16:39 +02:00
*/
function delete ()
{
2011-06-02 19:20:24 +02:00
global $conf , $langs , $user ;
2018-03-11 00:00:24 +01:00
2015-10-14 12:16:54 +02:00
require_once DOL_DOCUMENT_ROOT . '/core/lib/files.lib.php' ;
require_once DOL_DOCUMENT_ROOT . '/expedition/class/expeditionbatch.class.php' ;
2018-03-11 00:00:24 +01:00
2011-12-05 19:41:38 +01:00
$error = 0 ;
2015-10-14 12:16:54 +02:00
$this -> error = '' ;
2013-08-07 15:08:30 +02:00
2018-03-10 23:40:56 +01:00
$this -> db -> begin ();
2018-03-11 00:00:24 +01:00
// Add a protection to refuse deleting if shipment has at least one delivery
$this -> fetchObjectLinked ( $this -> id , 'shipping' , 0 , 'delivery' ); // Get deliveries linked to this shipment
if ( count ( $this -> linkedObjectsIds ) > 0 )
{
$this -> error = 'ErrorThereIsSomeDeliveries' ;
$error ++ ;
}
2018-04-10 12:03:01 +02:00
if ( ! $error )
2018-03-11 00:00:24 +01:00
{
2018-03-10 23:40:56 +01:00
if ( ! $notrigger )
{
// Call trigger
$result = $this -> call_trigger ( 'SHIPPING_DELETE' , $user );
if ( $result < 0 ) { $error ++ ; }
// End call triggers
}
}
2013-06-20 18:37:23 +02:00
// Stock control
2018-03-11 00:00:24 +01:00
if ( ! $error && $conf -> stock -> enabled && $conf -> global -> STOCK_CALCULATE_ON_SHIPMENT && $this -> statut > 0 )
2008-10-25 13:16:39 +02:00
{
2013-06-20 18:37:23 +02:00
require_once ( DOL_DOCUMENT_ROOT . " /product/stock/class/mouvementstock.class.php " );
2013-08-07 15:08:30 +02:00
2013-06-20 18:37:23 +02:00
$langs -> load ( " agenda " );
2013-08-07 15:08:30 +02:00
2013-06-20 18:37:23 +02:00
// Loop on each product line to add a stock movement
2015-10-14 12:16:54 +02:00
$sql = " SELECT cd.fk_product, cd.subprice, ed.qty, ed.fk_entrepot, ed.rowid as expeditiondet_id " ;
2013-06-20 18:37:23 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " commandedet as cd, " ;
$sql .= " " . MAIN_DB_PREFIX . " expeditiondet as ed " ;
$sql .= " WHERE ed.fk_expedition = " . $this -> id ;
$sql .= " AND cd.rowid = ed.fk_origin_line " ;
2013-08-07 15:08:30 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::delete select details " , LOG_DEBUG );
2013-06-20 18:37:23 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2008-10-25 23:18:53 +02:00
{
2013-06-20 18:37:23 +02:00
$cpt = $this -> db -> num_rows ( $resql );
for ( $i = 0 ; $i < $cpt ; $i ++ )
2009-05-07 23:38:56 +02:00
{
2013-06-20 18:37:23 +02:00
dol_syslog ( get_class ( $this ) . " ::delete movement index " . $i );
$obj = $this -> db -> fetch_object ( $resql );
2016-09-07 12:41:32 +02:00
2013-06-20 18:37:23 +02:00
$mouvS = new MouvementStock ( $this -> db );
2015-10-14 12:16:54 +02:00
// we do not log origin because it will be deleted
$mouvS -> origin = null ;
// get lot/serial
$lotArray = null ;
2016-09-07 12:41:32 +02:00
if ( $conf -> productbatch -> enabled )
2008-05-26 23:27:50 +02:00
{
2015-10-14 12:16:54 +02:00
$lotArray = ExpeditionLineBatch :: fetchAll ( $this -> db , $obj -> expeditiondet_id );
if ( ! is_array ( $lotArray ))
{
$error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror ();
}
2013-06-20 18:37:23 +02:00
}
2015-10-14 12:16:54 +02:00
if ( empty ( $lotArray )) {
// no lot/serial
// We increment stock of product (and sub-products)
// We use warehouse selected for each line
2015-11-05 22:09:54 +01:00
$result = $mouvS -> reception ( $user , $obj -> fk_product , $obj -> fk_entrepot , $obj -> qty , 0 , $langs -> trans ( " ShipmentDeletedInDolibarr " , $this -> ref )); // Price is set to 0, because we don't want to see WAP changed
2015-10-14 12:16:54 +02:00
if ( $result < 0 )
{
$error ++ ; $this -> errors = $this -> errors + $mouvS -> errors ;
break ;
}
}
2016-09-07 12:41:32 +02:00
else
2015-10-14 12:16:54 +02:00
{
// We increment stock of batches
// We use warehouse selected for each line
foreach ( $lotArray as $lot )
{
2018-10-11 11:56:52 +02:00
$result = $mouvS -> reception ( $user , $obj -> fk_product , $obj -> fk_entrepot , $lot -> qty , 0 , $langs -> trans ( " ShipmentDeletedInDolibarr " , $this -> ref ), $lot -> eatby , $lot -> sellby , $lot -> batch ); // Price is set to 0, because we don't want to see WAP changed
2015-10-14 12:16:54 +02:00
if ( $result < 0 )
{
$error ++ ; $this -> errors = $this -> errors + $mouvS -> errors ;
break ;
}
}
2015-12-14 21:38:26 +01:00
if ( $error ) break ; // break for loop incase of error
2016-09-07 12:41:32 +02:00
}
2013-06-20 18:37:23 +02:00
}
}
else
{
2015-10-14 12:16:54 +02:00
$error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror ();
2013-06-20 18:37:23 +02:00
}
}
2016-09-07 12:41:32 +02:00
2015-10-14 12:16:54 +02:00
// delete batch expedition line
if ( ! $error && $conf -> productbatch -> enabled )
{
if ( ExpeditionLineBatch :: deletefromexp ( $this -> db , $this -> id ) < 0 )
{
$error ++ ; $this -> errors [] = " Error " . $this -> db -> lasterror ();
}
}
2016-09-07 12:41:32 +02:00
2014-04-26 17:27:59 +02:00
if ( ! $error )
2013-06-20 18:37:23 +02:00
{
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " expeditiondet " ;
$sql .= " WHERE fk_expedition = " . $this -> id ;
2013-08-07 15:08:30 +02:00
2013-06-20 18:37:23 +02:00
if ( $this -> db -> query ( $sql ) )
{
// Delete linked object
$res = $this -> deleteObjectLinked ();
if ( $res < 0 ) $error ++ ;
2013-08-07 15:08:30 +02:00
2013-06-20 18:37:23 +02:00
if ( ! $error )
{
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " expedition " ;
$sql .= " WHERE rowid = " . $this -> id ;
2013-08-07 15:08:30 +02:00
2013-06-20 18:37:23 +02:00
if ( $this -> db -> query ( $sql ))
{
2016-04-01 16:59:08 +02:00
if ( ! empty ( $this -> origin ) && $this -> origin_id > 0 )
{
2017-10-07 13:09:31 +02:00
$this -> fetch_origin ();
$origin = $this -> origin ;
if ( $this -> $origin -> statut == Commande :: STATUS_SHIPMENTONPROCESS ) // If order source of shipment is "shipment in progress"
{
// Check if there is no more shipment. If not, we can move back status of order to "validated" instead of "shipment in progress"
$this -> $origin -> loadExpeditions ();
//var_dump($this->$origin->expeditions);exit;
if ( count ( $this -> $origin -> expeditions ) <= 0 )
{
$this -> $origin -> setStatut ( Commande :: STATUS_VALIDATED );
}
}
2016-04-01 16:59:08 +02:00
}
2016-09-07 12:41:32 +02:00
2015-10-14 12:16:54 +02:00
if ( ! $error )
{
2014-04-26 17:27:59 +02:00
$this -> db -> commit ();
// We delete PDFs
$ref = dol_sanitizeFileName ( $this -> ref );
if ( ! empty ( $conf -> expedition -> dir_output ))
2009-04-05 21:30:37 +02:00
{
2014-04-26 17:27:59 +02:00
$dir = $conf -> expedition -> dir_output . '/sending/' . $ref ;
$file = $dir . '/' . $ref . '.pdf' ;
if ( file_exists ( $file ))
2013-06-20 18:37:23 +02:00
{
2014-04-26 17:27:59 +02:00
if ( ! dol_delete_file ( $file ))
{
return 0 ;
}
2013-06-20 18:37:23 +02:00
}
2014-04-26 17:27:59 +02:00
if ( file_exists ( $dir ))
2013-06-20 18:37:23 +02:00
{
2015-04-02 11:44:43 +02:00
if ( ! dol_delete_dir_recursive ( $dir ))
2014-04-26 17:27:59 +02:00
{
$this -> error = $langs -> trans ( " ErrorCanNotDeleteDir " , $dir );
return 0 ;
}
2013-06-20 18:37:23 +02:00
}
2009-04-05 21:30:37 +02:00
}
2013-08-07 15:08:30 +02:00
2014-04-26 17:27:59 +02:00
return 1 ;
2015-10-14 12:16:54 +02:00
}
else
2014-04-26 17:27:59 +02:00
{
$this -> db -> rollback ();
return - 1 ;
}
2013-06-20 18:37:23 +02:00
}
else
{
$this -> error = $this -> db -> lasterror () . " - sql= $sql " ;
$this -> db -> rollback ();
return - 3 ;
2008-05-26 23:27:50 +02:00
}
}
2009-04-05 21:30:37 +02:00
else
{
2009-12-11 17:31:42 +01:00
$this -> error = $this -> db -> lasterror () . " - sql= $sql " ;
2009-04-05 21:30:37 +02:00
$this -> db -> rollback ();
2013-06-20 18:37:23 +02:00
return - 2 ;
2009-04-05 21:30:37 +02:00
}
2009-05-07 23:38:56 +02:00
}
2008-05-26 23:27:50 +02:00
else
{
2009-12-11 17:31:42 +01:00
$this -> error = $this -> db -> lasterror () . " - sql= $sql " ;
2008-05-26 23:27:50 +02:00
$this -> db -> rollback ();
2013-06-20 18:37:23 +02:00
return - 1 ;
2008-05-26 23:27:50 +02:00
}
}
2013-08-07 15:08:30 +02:00
else
2008-05-26 23:27:50 +02:00
{
$this -> db -> rollback ();
return - 1 ;
}
2013-08-07 15:08:30 +02:00
2008-05-26 23:27:50 +02:00
}
2009-11-12 14:33:55 +01:00
/**
2011-09-29 22:32:28 +02:00
* Load lines
2009-11-12 14:33:55 +01:00
*
2011-12-16 15:29:58 +01:00
* @ return int > 0 if OK , Otherwise if KO
2009-11-12 14:33:55 +01:00
*/
2007-05-16 00:47:58 +02:00
function fetch_lines ()
2006-08-26 16:13:37 +02:00
{
2015-12-17 19:37:24 +01:00
global $conf , $mysoc ;
2010-09-07 00:31:36 +02:00
// TODO: recuperer les champs du document associe a part
2016-06-20 02:34:00 +02:00
$sql = " SELECT cd.rowid, cd.fk_product, cd.label as custom_label, cd.description, cd.qty as qty_asked, cd.product_type " ;
2012-03-08 22:30:45 +01:00
$sql .= " , cd.total_ht, cd.total_localtax1, cd.total_localtax2, cd.total_ttc, cd.total_tva " ;
2017-11-13 14:02:17 +01:00
$sql .= " , cd.vat_src_code, cd.tva_tx, cd.localtax1_tx, cd.localtax2_tx, cd.localtax1_type, cd.localtax2_type, cd.info_bits, cd.price, cd.subprice, cd.remise_percent,cd.buy_price_ht as pa_ht " ;
2017-06-23 11:12:17 +02:00
$sql .= " , cd.fk_multicurrency, cd.multicurrency_code, cd.multicurrency_subprice, cd.multicurrency_total_ht, cd.multicurrency_total_tva, cd.multicurrency_total_ttc " ;
2015-08-14 23:53:59 +02:00
$sql .= " , ed.rowid as line_id, ed.qty as qty_shipped, ed.fk_origin_line, ed.fk_entrepot " ;
2012-11-18 16:21:38 +01:00
$sql .= " , p.ref as product_ref, p.label as product_label, p.fk_product_type " ;
2015-08-14 23:53:59 +02:00
$sql .= " , p.weight, p.weight_units, p.length, p.length_units, p.surface, p.surface_units, p.volume, p.volume_units, p.tobatch as product_tobatch " ;
2017-12-02 10:43:01 +01:00
$sql .= " FROM " . MAIN_DB_PREFIX . " expeditiondet as ed, " . MAIN_DB_PREFIX . " commandedet as cd " ;
2010-05-13 01:03:33 +02:00
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " product as p ON p.rowid = cd.fk_product " ;
2007-05-16 00:47:58 +02:00
$sql .= " WHERE ed.fk_expedition = " . $this -> id ;
2010-09-07 00:31:36 +02:00
$sql .= " AND ed.fk_origin_line = cd.rowid " ;
2014-10-03 12:20:02 +02:00
$sql .= " ORDER BY cd.rang, ed.fk_origin_line " ;
2008-05-26 23:27:50 +02:00
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::fetch_lines " , LOG_DEBUG );
2006-08-26 16:13:37 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2012-08-23 02:04:35 +02:00
include_once DOL_DOCUMENT_ROOT . '/core/lib/price.lib.php' ;
2012-03-14 14:00:20 +01:00
2006-08-26 16:13:37 +02:00
$num = $this -> db -> num_rows ( $resql );
$i = 0 ;
2014-10-03 12:20:02 +02:00
$lineindex = 0 ;
$originline = 0 ;
2012-03-14 14:00:20 +01:00
2012-07-02 19:30:37 +02:00
$this -> total_ht = 0 ;
$this -> total_tva = 0 ;
$this -> total_ttc = 0 ;
$this -> total_localtax1 = 0 ;
2012-03-08 22:30:45 +01:00
$this -> total_localtax2 = 0 ;
2012-03-14 14:00:20 +01:00
2006-08-26 16:13:37 +02:00
while ( $i < $num )
{
2014-10-18 16:27:15 +02:00
$obj = $this -> db -> fetch_object ( $resql );
2017-10-07 13:09:31 +02:00
if ( $originline == $obj -> fk_origin_line ) {
$line -> entrepot_id = 0 ; // entrepod_id in details_entrepot
$line -> qty_shipped += $obj -> qty_shipped ;
2014-10-03 12:20:02 +02:00
} else {
2017-10-07 13:09:31 +02:00
$line = new ExpeditionLigne ( $this -> db );
$line -> entrepot_id = $obj -> fk_entrepot ;
$line -> qty_shipped = $obj -> qty_shipped ;
2014-10-03 12:20:02 +02:00
}
2014-10-18 16:27:15 +02:00
2014-10-03 12:20:02 +02:00
$detail_entrepot = new stdClass ;
$detail_entrepot -> entrepot_id = $obj -> fk_entrepot ;
$detail_entrepot -> qty_shipped = $obj -> qty_shipped ;
2017-10-23 22:16:00 +02:00
$detail_entrepot -> line_id = $obj -> line_id ;
2014-10-03 12:20:02 +02:00
$line -> details_entrepot [] = $detail_entrepot ;
2014-10-18 16:27:15 +02:00
2017-10-07 13:09:31 +02:00
$line -> line_id = $obj -> line_id ;
$line -> rowid = $obj -> line_id ; // TODO deprecated
$line -> id = $obj -> line_id ;
2017-06-27 16:41:57 +02:00
2017-10-07 13:09:31 +02:00
$line -> fk_origin = 'orderline' ;
$line -> fk_origin_line = $obj -> fk_origin_line ;
$line -> origin_line_id = $obj -> fk_origin_line ; // TODO deprecated
2017-06-27 16:41:57 +02:00
$line -> fk_expedition = $this -> id ; // id of parent
2016-06-20 02:34:00 +02:00
$line -> product_type = $obj -> product_type ;
2010-03-13 16:52:30 +01:00
$line -> fk_product = $obj -> fk_product ;
$line -> fk_product_type = $obj -> fk_product_type ;
2011-05-29 15:15:37 +02:00
$line -> ref = $obj -> product_ref ; // TODO deprecated
2017-10-07 13:09:31 +02:00
$line -> product_ref = $obj -> product_ref ;
$line -> product_label = $obj -> product_label ;
2012-02-20 23:58:36 +01:00
$line -> libelle = $obj -> product_label ; // TODO deprecated
2015-08-14 23:53:59 +02:00
$line -> product_tobatch = $obj -> product_tobatch ;
2012-08-25 15:21:34 +02:00
$line -> label = $obj -> custom_label ;
2010-03-13 16:52:30 +01:00
$line -> description = $obj -> description ;
$line -> qty_asked = $obj -> qty_asked ;
$line -> weight = $obj -> weight ;
$line -> weight_units = $obj -> weight_units ;
2012-11-18 16:21:38 +01:00
$line -> length = $obj -> length ;
$line -> length_units = $obj -> length_units ;
$line -> surface = $obj -> surface ;
$line -> surface_units = $obj -> surface_units ;
2010-03-13 16:52:30 +01:00
$line -> volume = $obj -> volume ;
$line -> volume_units = $obj -> volume_units ;
2012-03-14 14:00:20 +01:00
2016-06-30 15:30:07 +02:00
$line -> pa_ht = $obj -> pa_ht ;
2016-09-07 12:41:32 +02:00
2017-10-18 12:10:00 +02:00
// Local taxes
2017-06-23 11:12:17 +02:00
$localtax_array = array ( 0 => $obj -> localtax1_type , 1 => $obj -> localtax1_tx , 2 => $obj -> localtax2_type , 3 => $obj -> localtax2_tx );
2017-10-18 12:10:00 +02:00
$localtax1_tx = get_localtax ( $obj -> tva_tx , 1 , $this -> thirdparty );
$localtax2_tx = get_localtax ( $obj -> tva_tx , 2 , $this -> thirdparty );
2017-06-23 11:12:17 +02:00
2013-02-17 18:40:09 +01:00
// For invoicing
2017-10-18 12:13:32 +02:00
$tabprice = calcul_price_total ( $obj -> qty_shipped , $obj -> subprice , $obj -> remise_percent , $obj -> tva_tx , $localtax1_tx , $localtax2_tx , 0 , 'HT' , $obj -> info_bits , $obj -> fk_product_type , $mysoc , $localtax_array ); // We force type to 0
2013-02-17 18:40:09 +01:00
$line -> desc = $obj -> description ; // We need ->desc because some code into CommonObject use desc (property defined for other elements)
2014-10-03 12:20:02 +02:00
$line -> qty = $line -> qty_shipped ;
2013-07-19 17:27:08 +02:00
$line -> total_ht = $tabprice [ 0 ];
$line -> total_localtax1 = $tabprice [ 9 ];
$line -> total_localtax2 = $tabprice [ 10 ];
$line -> total_ttc = $tabprice [ 2 ];
$line -> total_tva = $tabprice [ 1 ];
2017-06-23 11:12:17 +02:00
$line -> vat_src_code = $obj -> vat_src_code ;
2012-03-08 22:30:45 +01:00
$line -> tva_tx = $obj -> tva_tx ;
$line -> localtax1_tx = $obj -> localtax1_tx ;
$line -> localtax2_tx = $obj -> localtax2_tx ;
2017-11-13 14:02:17 +01:00
$line -> info_bits = $obj -> info_bits ;
2012-03-08 22:30:45 +01:00
$line -> price = $obj -> price ;
$line -> subprice = $obj -> subprice ;
$line -> remise_percent = $obj -> remise_percent ;
2012-03-14 14:00:20 +01:00
2012-07-02 19:30:37 +02:00
$this -> total_ht += $tabprice [ 0 ];
$this -> total_tva += $tabprice [ 1 ];
$this -> total_ttc += $tabprice [ 2 ];
$this -> total_localtax1 += $tabprice [ 9 ];
2012-03-08 22:30:45 +01:00
$this -> total_localtax2 += $tabprice [ 10 ];
2016-09-07 12:41:32 +02:00
2017-06-23 11:12:17 +02:00
// Multicurrency
$this -> fk_multicurrency = $obj -> fk_multicurrency ;
$this -> multicurrency_code = $obj -> multicurrency_code ;
$this -> multicurrency_subprice = $obj -> multicurrency_subprice ;
$this -> multicurrency_total_ht = $obj -> multicurrency_total_ht ;
$this -> multicurrency_total_tva = $obj -> multicurrency_total_tva ;
$this -> multicurrency_total_ttc = $obj -> multicurrency_total_ttc ;
2016-09-07 12:41:32 +02:00
if ( $originline != $obj -> fk_origin_line )
2015-12-18 18:00:31 +01:00
{
$line -> detail_batch = array ();
}
2017-12-02 10:43:01 +01:00
// Detail of batch
if ( ! empty ( $conf -> productbatch -> enabled ) && $obj -> line_id > 0 && $obj -> product_tobatch > 0 )
2015-07-13 11:22:18 +02:00
{
2017-10-07 13:09:31 +02:00
require_once DOL_DOCUMENT_ROOT . '/expedition/class/expeditionbatch.class.php' ;
2017-12-02 10:43:01 +01:00
$newdetailbatch = ExpeditionLineBatch :: fetchAll ( $this -> db , $obj -> line_id , $obj -> fk_product );
2017-10-07 13:09:31 +02:00
if ( is_array ( $newdetailbatch ))
{
if ( $originline != $obj -> fk_origin_line )
{
$line -> detail_batch = $newdetailbatch ;
}
else
2015-07-13 11:22:18 +02:00
{
2017-10-07 13:09:31 +02:00
$line -> detail_batch = array_merge ( $line -> detail_batch , $newdetailbatch );
}
}
2014-10-03 12:20:02 +02:00
}
2015-07-13 11:22:18 +02:00
if ( $originline != $obj -> fk_origin_line )
{
2017-10-07 13:09:31 +02:00
$this -> lines [ $lineindex ] = $line ;
$lineindex ++ ;
2015-07-13 11:22:18 +02:00
}
else
{
2017-10-07 13:09:31 +02:00
$line -> total_ht += $tabprice [ 0 ];
$line -> total_localtax1 += $tabprice [ 9 ];
$line -> total_localtax2 += $tabprice [ 10 ];
$line -> total_ttc += $tabprice [ 2 ];
$line -> total_tva += $tabprice [ 1 ];
2014-03-07 11:35:16 +01:00
}
2010-09-19 18:25:24 +02:00
2006-08-26 16:13:37 +02:00
$i ++ ;
2014-10-18 16:27:15 +02:00
$originline = $obj -> fk_origin_line ;
2006-08-26 16:13:37 +02:00
}
$this -> db -> free ( $resql );
2007-05-16 00:47:58 +02:00
return 1 ;
2006-08-26 16:13:37 +02:00
}
2007-05-16 00:47:58 +02:00
else
{
$this -> error = $this -> db -> error ();
2008-05-26 23:27:50 +02:00
return - 3 ;
}
}
2017-12-21 16:50:18 +01:00
/**
* Delete detail line
*
* @ param User $user User making deletion
* @ param int $lineid Id of line to delete
* @ return int > 0 if OK , < 0 if KO
*/
function deleteline ( $user , $lineid )
{
global $user ;
if ( $this -> statut == self :: STATUS_DRAFT )
{
$this -> db -> begin ();
$line = new ExpeditionLigne ( $this -> db );
// For triggers
$line -> fetch ( $lineid );
if ( $line -> delete ( $user ) > 0 )
{
//$this->update_price(1);
$this -> db -> commit ();
return 1 ;
}
else
{
$this -> db -> rollback ();
return - 1 ;
}
}
else
{
$this -> error = 'ErrorDeleteLineNotAllowedByObjectStatus' ;
return - 2 ;
}
}
2010-05-27 00:06:27 +02:00
/**
2017-10-07 13:09:31 +02:00
* Return clicable link of object ( with eventually picto )
*
* @ param int $withpicto Add picto into link
* @ param string $option Where the link point to
* @ param int $max Max length to show
* @ param int $short Use short labels
* @ param int $notooltip 1 = No tooltip
* @ param int $save_lastsearch_value - 1 = Auto , 0 = No save of lastsearch_values when clicking , 1 = Save lastsearch_values whenclicking
* @ return string String with URL
*/
2017-10-06 00:26:24 +02:00
function getNomUrl ( $withpicto = 0 , $option = '' , $max = 0 , $short = 0 , $notooltip = 0 , $save_lastsearch_value =- 1 )
2010-05-27 00:06:27 +02:00
{
global $langs ;
$result = '' ;
2017-10-07 13:09:31 +02:00
$label = '<u>' . $langs -> trans ( " ShowSending " ) . '</u>' ;
$label .= '<br><b>' . $langs -> trans ( 'Ref' ) . ':</b> ' . $this -> ref ;
$label .= '<br><b>' . $langs -> trans ( 'RefCustomer' ) . ':</b> ' . ( $this -> ref_customer ? $this -> ref_customer : $this -> ref_client );
2017-06-23 11:12:17 +02:00
2014-09-18 21:18:25 +02:00
$url = DOL_URL_ROOT . '/expedition/card.php?id=' . $this -> id ;
2011-08-31 16:42:47 +02:00
2011-05-26 17:33:33 +02:00
if ( $short ) return $url ;
2010-05-27 00:06:27 +02:00
2017-10-03 16:00:52 +02:00
if ( $option !== 'nolink' )
{
// Add param to save lastsearch_values or not
$add_save_lastsearch_values = ( $save_lastsearch_value == 1 ? 1 : 0 );
if ( $save_lastsearch_value == - 1 && preg_match ( '/list\.php/' , $_SERVER [ " PHP_SELF " ])) $add_save_lastsearch_values = 1 ;
if ( $add_save_lastsearch_values ) $url .= '&save_lastsearch_values=1' ;
}
2016-11-02 10:29:14 +01:00
$linkclose = '' ;
if ( empty ( $notooltip ))
{
2017-10-07 13:09:31 +02:00
if ( ! empty ( $conf -> global -> MAIN_OPTIMIZEFORTEXTBROWSER ))
{
$label = $langs -> trans ( " ShowSending " );
$linkclose .= ' alt="' . dol_escape_htmltag ( $label , 1 ) . '"' ;
}
$linkclose .= ' title="' . dol_escape_htmltag ( $label , 1 ) . '"' ;
$linkclose .= ' class="classfortooltip"' ;
2016-11-02 10:29:14 +01:00
}
2017-06-23 11:12:17 +02:00
2017-10-07 13:09:31 +02:00
$linkstart = '<a href="' . $url . '" title="' . dol_escape_htmltag ( $label , 1 ) . '" class="classfortooltip">' ;
2011-05-26 17:33:33 +02:00
$linkend = '</a>' ;
2010-05-27 00:06:27 +02:00
2017-11-02 15:03:09 +01:00
$result .= $linkstart ;
if ( $withpicto ) $result .= img_object (( $notooltip ? '' : $label ), $this -> picto , ( $notooltip ? (( $withpicto != 2 ) ? 'class="paddingright"' : '' ) : 'class="' . (( $withpicto != 2 ) ? 'paddingright ' : '' ) . 'classfortooltip"' ), 0 , 0 , $notooltip ? 0 : 1 );
if ( $withpicto != 2 ) $result .= $this -> ref ;
$result .= $linkend ;
2010-05-27 00:06:27 +02:00
return $result ;
}
2012-01-04 23:56:10 +01:00
2008-05-26 23:27:50 +02:00
/**
2017-10-07 13:09:31 +02:00
* Return status label
*
* @ param int $mode 0 = Long label , 1 = Short label , 2 = Picto + Short label , 3 = Picto , 4 = Picto + Long label , 5 = Short label + Picto
* @ return string Libelle
*/
2008-05-26 23:27:50 +02:00
function getLibStatut ( $mode = 0 )
{
return $this -> LibStatut ( $this -> statut , $mode );
2006-08-26 16:13:37 +02:00
}
2008-05-26 23:27:50 +02:00
2006-07-14 15:26:19 +02:00
/**
2011-09-29 22:32:28 +02:00
* Return label of a status
*
* @ param int $statut Id statut
* @ param int $mode 0 = Long label , 1 = Short label , 2 = Picto + Short label , 3 = Picto , 4 = Picto + Long label , 5 = Short label + Picto
* @ return string Label of status
2006-07-14 15:26:19 +02:00
*/
2008-05-26 23:27:50 +02:00
function LibStatut ( $statut , $mode )
{
2006-07-14 15:26:19 +02:00
global $langs ;
2008-05-26 23:27:50 +02:00
if ( $mode == 0 )
{
2009-12-17 16:52:37 +01:00
if ( $statut == 0 ) return $langs -> trans ( $this -> statuts [ $statut ]);
2018-02-15 15:27:06 +01:00
if ( $statut == 1 ) return $langs -> trans ( $this -> statuts [ $statut ]);
if ( $statut == 2 ) return $langs -> trans ( $this -> statuts [ $statut ]);
2006-07-14 15:26:19 +02:00
}
2008-05-26 23:27:50 +02:00
if ( $mode == 1 )
{
2018-02-15 15:27:06 +01:00
if ( $statut == 0 ) return $langs -> trans ( $this -> statutshorts [ $statut ]);
if ( $statut == 1 ) return $langs -> trans ( $this -> statutshorts [ $statut ]);
if ( $statut == 2 ) return $langs -> trans ( $this -> statutshorts [ $statut ]);
2011-05-26 17:33:33 +02:00
}
if ( $mode == 3 )
{
if ( $statut == 0 ) return img_picto ( $langs -> trans ( $this -> statuts [ $statut ]), 'statut0' );
if ( $statut == 1 ) return img_picto ( $langs -> trans ( $this -> statuts [ $statut ]), 'statut4' );
2018-02-15 15:27:06 +01:00
if ( $statut == 2 ) return img_picto ( $langs -> trans ( $this -> statuts [ $statut ]), 'statut6' );
2008-05-26 23:27:50 +02:00
}
if ( $mode == 4 )
{
2011-05-26 17:33:33 +02:00
if ( $statut == 0 ) return img_picto ( $langs -> trans ( $this -> statuts [ $statut ]), 'statut0' ) . ' ' . $langs -> trans ( $this -> statuts [ $statut ]);
if ( $statut == 1 ) return img_picto ( $langs -> trans ( $this -> statuts [ $statut ]), 'statut4' ) . ' ' . $langs -> trans ( $this -> statuts [ $statut ]);
2018-02-15 15:27:06 +01:00
if ( $statut == 2 ) return img_picto ( $langs -> trans ( $this -> statuts [ $statut ]), 'statut6' ) . ' ' . $langs -> trans ( $this -> statuts [ $statut ]);
2008-05-26 23:27:50 +02:00
}
if ( $mode == 5 )
{
2018-02-15 15:27:06 +01:00
if ( $statut == 0 ) return $langs -> trans ( $this -> statutshorts [ $statut ]) . ' ' . img_picto ( $langs -> trans ( $this -> statuts [ $statut ]), 'statut0' );
if ( $statut == 1 ) return $langs -> trans ( $this -> statutshorts [ $statut ]) . ' ' . img_picto ( $langs -> trans ( $this -> statuts [ $statut ]), 'statut4' );
if ( $statut == 2 ) return $langs -> trans ( $this -> statutshorts [ $statut ]) . ' ' . img_picto ( $langs -> trans ( $this -> statuts [ $statut ]), 'statut6' );
2008-05-17 03:52:24 +02:00
}
2008-05-26 23:27:50 +02:00
}
2006-08-26 15:47:10 +02:00
/**
2017-10-07 13:09:31 +02:00
* Initialise an instance with random values .
* Used to build previews or test instances .
* id must be 0 if object instance is a specimen .
*
* @ return void
2006-08-26 15:47:10 +02:00
*/
function initAsSpecimen ()
{
2016-05-06 22:43:50 +02:00
global $langs ;
2006-08-26 15:47:10 +02:00
2011-08-31 16:42:47 +02:00
$now = dol_now ();
2012-01-04 23:56:10 +01:00
dol_syslog ( get_class ( $this ) . " ::initAsSpecimen " );
2008-10-25 13:16:39 +02:00
2017-10-07 13:09:31 +02:00
// Load array of products prodids
2016-06-28 02:36:40 +02:00
$num_prods = 0 ;
2006-08-26 15:47:10 +02:00
$prodids = array ();
2009-04-30 18:28:53 +02:00
$sql = " SELECT rowid " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " product " ;
2017-05-30 18:50:54 +02:00
$sql .= " WHERE entity IN ( " . getEntity ( 'product' ) . " ) " ;
2006-08-26 15:47:10 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$num_prods = $this -> db -> num_rows ( $resql );
$i = 0 ;
while ( $i < $num_prods )
{
$i ++ ;
$row = $this -> db -> fetch_row ( $resql );
$prodids [ $i ] = $row [ 0 ];
}
}
2008-05-17 03:52:24 +02:00
$order = new Commande ( $this -> db );
$order -> initAsSpecimen ();
2008-05-26 23:27:50 +02:00
2009-11-12 14:33:55 +01:00
// Initialise parametres
2006-08-26 15:47:10 +02:00
$this -> id = 0 ;
2012-04-22 00:41:13 +02:00
$this -> ref = 'SPECIMEN' ;
2006-08-26 15:47:10 +02:00
$this -> specimen = 1 ;
2008-05-26 23:27:50 +02:00
$this -> statut = 1 ;
2011-08-31 16:42:47 +02:00
$this -> livraison_id = 0 ;
$this -> date = $now ;
$this -> date_creation = $now ;
$this -> date_valid = $now ;
$this -> date_delivery = $now ;
$this -> date_expedition = $now + 24 * 3600 ;
2008-05-26 23:27:50 +02:00
$this -> entrepot_id = 0 ;
2009-07-08 12:02:57 +02:00
$this -> fk_delivery_address = 0 ;
2010-09-19 18:25:24 +02:00
$this -> socid = 1 ;
2006-08-26 15:47:10 +02:00
2008-05-17 03:52:24 +02:00
$this -> commande_id = 0 ;
$this -> commande = $order ;
2008-05-26 23:27:50 +02:00
2017-10-07 13:09:31 +02:00
$this -> origin_id = 1 ;
$this -> origin = 'commande' ;
2013-04-11 21:53:41 +02:00
2017-10-07 13:09:31 +02:00
$this -> note_private = 'Private note' ;
$this -> note_public = 'Public note' ;
2010-09-19 18:25:24 +02:00
2006-08-26 15:47:10 +02:00
$nbp = 5 ;
$xnbp = 0 ;
while ( $xnbp < $nbp )
{
2010-12-15 08:12:28 +01:00
$line = new ExpeditionLigne ( $this -> db );
$line -> desc = $langs -> trans ( " Description " ) . " " . $xnbp ;
$line -> libelle = $langs -> trans ( " Description " ) . " " . $xnbp ;
$line -> qty = 10 ;
$line -> qty_asked = 5 ;
$line -> qty_shipped = 4 ;
$line -> fk_product = $this -> commande -> lines [ $xnbp ] -> fk_product ;
2009-01-29 13:39:52 +01:00
2010-12-15 08:12:28 +01:00
$this -> lines [] = $line ;
2006-08-26 15:47:10 +02:00
$xnbp ++ ;
}
2009-01-29 13:39:52 +01:00
2008-05-26 23:27:50 +02:00
}
2009-03-23 18:38:04 +01:00
2010-05-13 01:03:33 +02:00
/**
2011-12-16 15:29:58 +01:00
* Set the planned delivery date
2011-09-29 22:32:28 +02:00
*
2012-01-04 23:56:10 +01:00
* @ param User $user Objet utilisateur qui modifie
* @ param timestamp $date_livraison Date de livraison
* @ return int < 0 if KO , > 0 if OK
2010-05-13 01:03:33 +02:00
*/
function set_date_livraison ( $user , $date_livraison )
{
if ( $user -> rights -> expedition -> creer )
{
$sql = " UPDATE " . MAIN_DB_PREFIX . " expedition " ;
$sql .= " SET date_delivery = " . ( $date_livraison ? " ' " . $this -> db -> idate ( $date_livraison ) . " ' " : 'null' );
$sql .= " WHERE rowid = " . $this -> id ;
2014-06-12 11:31:53 +02:00
dol_syslog ( get_class ( $this ) . " ::set_date_livraison " , LOG_DEBUG );
2010-05-13 01:03:33 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> date_delivery = $date_livraison ;
return 1 ;
}
else
{
$this -> error = $this -> db -> error ();
return - 1 ;
}
}
else
{
return - 2 ;
}
}
2009-03-23 18:38:04 +01:00
/**
2011-12-16 15:29:58 +01:00
* Fetch deliveries method and return an array . Load array this -> meths ( rowid => label ) .
2011-09-29 22:32:28 +02:00
*
2011-12-16 15:29:58 +01:00
* @ return void
2008-10-25 23:18:53 +02:00
*/
2008-10-25 13:16:39 +02:00
function fetch_delivery_methods ()
{
2009-03-23 18:38:04 +01:00
global $langs ;
2016-05-06 22:43:50 +02:00
$this -> meths = array ();
2008-10-25 13:16:39 +02:00
2009-03-23 18:38:04 +01:00
$sql = " SELECT em.rowid, em.code, em.libelle " ;
2010-09-06 02:02:55 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " c_shipment_mode as em " ;
2009-03-23 18:38:04 +01:00
$sql .= " WHERE em.active = 1 " ;
$sql .= " ORDER BY em.libelle ASC " ;
2008-10-25 13:16:39 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
while ( $obj = $this -> db -> fetch_object ( $resql ))
2008-10-29 11:51:52 +01:00
{
2009-03-23 18:38:04 +01:00
$label = $langs -> trans ( 'SendingMethod' . $obj -> code );
$this -> meths [ $obj -> rowid ] = ( $label != 'SendingMethod' . $obj -> code ? $label : $obj -> libelle );
2008-10-29 11:51:52 +01:00
}
2008-10-25 13:16:39 +02:00
}
}
2009-01-29 13:39:52 +01:00
2017-10-07 13:09:31 +02:00
/**
* Fetch all deliveries method and return an array . Load array this -> listmeths .
*
* @ param id $id only this carrier , all if none
* @ return void
*/
function list_delivery_methods ( $id = '' )
{
global $langs ;
$this -> listmeths = array ();
$i = 0 ;
$sql = " SELECT em.rowid, em.code, em.libelle, em.description, em.tracking, em.active " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " c_shipment_mode as em " ;
if ( $id != '' ) $sql .= " WHERE em.rowid= " . $id ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
while ( $obj = $this -> db -> fetch_object ( $resql ))
{
$this -> listmeths [ $i ][ 'rowid' ] = $obj -> rowid ;
$this -> listmeths [ $i ][ 'code' ] = $obj -> code ;
$label = $langs -> trans ( 'SendingMethod' . $obj -> code );
$this -> listmeths [ $i ][ 'libelle' ] = ( $label != 'SendingMethod' . $obj -> code ? $label : $obj -> libelle );
$this -> listmeths [ $i ][ 'description' ] = $obj -> description ;
$this -> listmeths [ $i ][ 'tracking' ] = $obj -> tracking ;
$this -> listmeths [ $i ][ 'active' ] = $obj -> active ;
$i ++ ;
}
}
}
/**
* Update / create delivery method .
*
* @ param string $id id method to activate
*
* @ return void
*/
function update_delivery_method ( $id = '' )
{
if ( $id == '' )
{
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " c_shipment_mode (code, libelle, description, tracking) " ;
$sql .= " VALUES (' " . $this -> db -> escape ( $this -> update [ 'code' ]) . " ',' " . $this -> db -> escape ( $this -> update [ 'libelle' ]) . " ',' " . $this -> db -> escape ( $this -> update [ 'description' ]) . " ',' " . $this -> db -> escape ( $this -> update [ 'tracking' ]) . " ') " ;
$resql = $this -> db -> query ( $sql );
}
else
{
$sql = " UPDATE " . MAIN_DB_PREFIX . " c_shipment_mode SET " ;
$sql .= " code=' " . $this -> db -> escape ( $this -> update [ 'code' ]) . " ' " ;
$sql .= " ,libelle=' " . $this -> db -> escape ( $this -> update [ 'libelle' ]) . " ' " ;
$sql .= " ,description=' " . $this -> db -> escape ( $this -> update [ 'description' ]) . " ' " ;
$sql .= " ,tracking=' " . $this -> db -> escape ( $this -> update [ 'tracking' ]) . " ' " ;
$sql .= " WHERE rowid= " . $id ;
$resql = $this -> db -> query ( $sql );
}
if ( $resql < 0 ) dol_print_error ( $this -> db , '' );
}
/**
* Activate delivery method .
*
* @ param id $id id method to activate
*
* @ return void
*/
function activ_delivery_method ( $id )
{
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'c_shipment_mode SET active=1' ;
$sql .= ' WHERE rowid=' . $id ;
$resql = $this -> db -> query ( $sql );
}
/**
* DesActivate delivery method .
*
* @ param id $id id method to desactivate
*
* @ return void
*/
function disable_delivery_method ( $id )
{
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'c_shipment_mode SET active=0' ;
$sql .= ' WHERE rowid=' . $id ;
$resql = $this -> db -> query ( $sql );
}
2013-02-21 23:40:25 +01:00
2016-09-07 12:41:32 +02:00
2009-02-03 03:39:28 +01:00
/**
2015-01-23 18:33:33 +01:00
* Forge an set tracking url
2012-01-04 23:56:10 +01:00
*
* @ param string $value Value
* @ return void
2008-10-25 23:18:53 +02:00
*/
2011-05-25 18:53:39 +02:00
function GetUrlTrackingStatus ( $value = '' )
2008-10-25 13:16:39 +02:00
{
2013-03-22 17:10:17 +01:00
if ( ! empty ( $this -> shipping_method_id ))
2008-10-25 13:16:39 +02:00
{
2013-02-22 14:18:51 +01:00
$sql = " SELECT em.code, em.tracking " ;
2011-05-25 18:53:39 +02:00
$sql .= " FROM " . MAIN_DB_PREFIX . " c_shipment_mode as em " ;
2013-03-22 17:10:17 +01:00
$sql .= " WHERE em.rowid = " . $this -> shipping_method_id ;
2011-06-19 21:47:09 +02:00
2011-05-25 18:53:39 +02:00
$resql = $this -> db -> query ( $sql );
if ( $resql )
2008-10-29 13:04:37 +01:00
{
2011-05-25 18:53:39 +02:00
if ( $obj = $this -> db -> fetch_object ( $resql ))
{
2013-04-19 20:20:10 +02:00
$tracking = $obj -> tracking ;
2011-05-25 18:53:39 +02:00
}
2008-10-29 13:04:37 +01:00
}
2008-10-25 13:16:39 +02:00
}
2013-04-20 11:03:36 +02:00
if ( ! empty ( $tracking ) && ! empty ( $value ))
2013-04-19 20:20:10 +02:00
{
$url = str_replace ( '{TRACKID}' , $value , $tracking );
$this -> tracking_url = sprintf ( '<a target="_blank" href="%s">' . ( $value ? $value : 'url' ) . '</a>' , $url , $url );
}
else
{
$this -> tracking_url = $value ;
}
}
2012-03-14 14:00:20 +01:00
2012-07-02 19:30:37 +02:00
/**
2016-06-09 22:40:21 +02:00
* Classify the shipping as closed .
2012-07-02 19:30:37 +02:00
*
2016-06-09 22:40:21 +02:00
* @ return int < 0 if KO , > 0 if OK
2012-07-02 19:30:37 +02:00
*/
2016-04-26 13:27:32 +02:00
function setClosed ()
2012-07-02 19:30:37 +02:00
{
2016-04-25 18:54:28 +02:00
global $conf , $langs , $user ;
2012-07-02 19:30:37 +02:00
2016-05-23 09:16:47 +02:00
$error = 0 ;
2016-09-07 12:41:32 +02:00
2016-05-23 09:16:47 +02:00
$this -> db -> begin ();
2016-09-07 12:41:32 +02:00
2016-06-09 22:40:21 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'expedition SET fk_statut=' . self :: STATUS_CLOSED ;
2014-10-18 16:27:15 +02:00
$sql .= ' WHERE rowid = ' . $this -> id . ' AND fk_statut > 0' ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
2012-03-13 17:28:55 +01:00
{
2016-06-09 22:40:21 +02:00
// Set order billed if 100% of order is shipped (qty in shipment lines match qty in order lines)
if ( $this -> origin == 'commande' && $this -> origin_id > 0 )
{
$order = new Commande ( $this -> db );
$order -> fetch ( $this -> origin_id );
2016-09-07 12:41:32 +02:00
2016-06-09 22:40:21 +02:00
$order -> loadExpeditions ( self :: STATUS_CLOSED ); // Fill $order->expeditions = array(orderlineid => qty)
2016-09-07 12:41:32 +02:00
2016-06-09 22:40:21 +02:00
$shipments_match_order = 1 ;
foreach ( $order -> lines as $line )
{
$lineid = $line -> id ;
$qty = $line -> qty ;
2016-06-20 02:34:00 +02:00
if (( $line -> product_type == 0 || ! empty ( $conf -> global -> STOCK_SUPPORTS_SERVICES )) && $order -> expeditions [ $lineid ] != $qty )
2016-06-09 22:40:21 +02:00
{
$shipments_match_order = 0 ;
2016-06-20 02:34:00 +02:00
$text = 'Qty for order line id ' . $lineid . ' is ' . $qty . '. However in the shipments with status Expedition::STATUS_CLOSED=' . self :: STATUS_CLOSED . ' we have qty = ' . $order -> expeditions [ $lineid ] . ', so we can t close order' ;
2016-06-09 22:40:21 +02:00
dol_syslog ( $text );
break ;
}
}
if ( $shipments_match_order )
{
2016-09-07 12:41:32 +02:00
dol_syslog ( " Qty for the " . count ( $order -> lines ) . " lines of order have same value for shipments with status Expedition::STATUS_CLOSED= " . self :: STATUS_CLOSED . ', so we close order' );
2016-06-09 22:40:21 +02:00
$order -> cloture ( $user );
}
}
2016-09-07 12:41:32 +02:00
2016-06-09 22:40:21 +02:00
$this -> statut = self :: STATUS_CLOSED ;
2016-05-14 19:14:39 +02:00
2016-09-07 12:41:32 +02:00
2016-05-14 19:14:39 +02:00
// If stock increment is done on closing
if ( ! $error && ! empty ( $conf -> stock -> enabled ) && ! empty ( $conf -> global -> STOCK_CALCULATE_ON_SHIPMENT_CLOSE ))
2016-04-25 18:33:39 +02:00
{
require_once DOL_DOCUMENT_ROOT . '/product/stock/class/mouvementstock.class.php' ;
2016-09-07 12:41:32 +02:00
2016-04-25 18:33:39 +02:00
$langs -> load ( " agenda " );
2016-09-07 12:41:32 +02:00
2016-04-25 18:33:39 +02:00
// Loop on each product line to add a stock movement
2016-06-09 22:40:21 +02:00
// TODO possibilite d'expedier a partir d'une propale ou autre origine ?
2016-04-25 18:33:39 +02:00
$sql = " SELECT cd.fk_product, cd.subprice, " ;
$sql .= " ed.rowid, ed.qty, ed.fk_entrepot, " ;
$sql .= " edb.rowid as edbrowid, edb.eatby, edb.sellby, edb.batch, edb.qty as edbqty, edb.fk_origin_stock " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " commandedet as cd, " ;
$sql .= " " . MAIN_DB_PREFIX . " expeditiondet as ed " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " expeditiondet_batch as edb on edb.fk_expeditiondet = ed.rowid " ;
$sql .= " WHERE ed.fk_expedition = " . $this -> id ;
$sql .= " AND cd.rowid = ed.fk_origin_line " ;
2016-09-07 12:41:32 +02:00
2016-04-25 18:33:39 +02:00
dol_syslog ( get_class ( $this ) . " ::valid select details " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$cpt = $this -> db -> num_rows ( $resql );
for ( $i = 0 ; $i < $cpt ; $i ++ )
{
$obj = $this -> db -> fetch_object ( $resql );
if ( empty ( $obj -> edbrowid ))
{
$qty = $obj -> qty ;
}
else
{
$qty = $obj -> edbqty ;
}
if ( $qty <= 0 ) continue ;
dol_syslog ( get_class ( $this ) . " ::valid movement index " . $i . " ed.rowid= " . $obj -> rowid . " edb.rowid= " . $obj -> edbrowid );
2016-09-07 12:41:32 +02:00
2016-04-25 18:33:39 +02:00
$mouvS = new MouvementStock ( $this -> db );
$mouvS -> origin = & $this ;
2016-09-07 12:41:32 +02:00
2016-04-25 18:33:39 +02:00
if ( empty ( $obj -> edbrowid ))
{
// line without batch detail
2016-09-07 12:41:32 +02:00
2016-04-25 18:33:39 +02:00
// We decrement stock of product (and sub-products) -> update table llx_product_stock (key of this table is fk_product+fk_entrepot) and add a movement record
2016-05-14 19:22:27 +02:00
$result = $mouvS -> livraison ( $user , $obj -> fk_product , $obj -> fk_entrepot , $qty , $obj -> subprice , $langs -> trans ( " ShipmentClassifyClosedInDolibarr " , $numref ));
2016-04-25 18:33:39 +02:00
if ( $result < 0 ) {
2017-10-07 13:09:31 +02:00
$this -> error = $mouvS -> error ;
$this -> errors = $mouvS -> errors ;
2016-04-25 18:33:39 +02:00
$error ++ ; break ;
}
}
else
{
// line with batch detail
2016-09-07 12:41:32 +02:00
2016-04-25 18:33:39 +02:00
// We decrement stock of product (and sub-products) -> update table llx_product_stock (key of this table is fk_product+fk_entrepot) and add a movement record
2016-09-28 00:22:29 +02:00
$result = $mouvS -> livraison ( $user , $obj -> fk_product , $obj -> fk_entrepot , $qty , $obj -> subprice , $langs -> trans ( " ShipmentClassifyClosedInDolibarr " , $numref ), '' , $this -> db -> jdate ( $obj -> eatby ), $this -> db -> jdate ( $obj -> sellby ), $obj -> batch , $obj -> fk_origin_stock );
2016-04-25 18:33:39 +02:00
if ( $result < 0 ) {
2017-10-07 13:09:31 +02:00
$this -> error = $mouvS -> error ;
$this -> errors = $mouvS -> errors ;
$error ++ ; break ;
2016-04-25 18:33:39 +02:00
}
}
}
}
else
{
2016-06-02 11:23:49 +02:00
$this -> error = $this -> db -> lasterror ();
$error ++ ;
2016-04-25 18:33:39 +02:00
}
}
2016-09-07 12:41:32 +02:00
2016-05-20 20:03:29 +02:00
// Call trigger
2016-06-02 11:23:49 +02:00
if ( ! $error )
{
2017-10-07 13:09:31 +02:00
$result = $this -> call_trigger ( 'SHIPPING_CLOSED' , $user );
if ( $result < 0 ) {
$error ++ ;
}
2016-05-20 20:03:29 +02:00
}
2016-04-26 13:27:32 +02:00
}
else
{
dol_print_error ( $this -> db );
2017-10-07 13:09:31 +02:00
$error ++ ;
2016-05-23 09:16:47 +02:00
}
2016-09-07 12:41:32 +02:00
2016-06-02 11:23:49 +02:00
if ( ! $error )
{
2017-10-07 13:09:31 +02:00
$this -> db -> commit ();
return 1 ;
2016-04-26 13:27:32 +02:00
}
else
{
2017-10-07 13:09:31 +02:00
$this -> db -> rollback ();
return - 1 ;
2016-04-26 13:27:32 +02:00
}
}
/**
* Classify the shipping as invoiced ( used when WORKFLOW_BILL_ON_SHIPMENT is on )
*
* @ return int < 0 if ko , > 0 if ok
*/
function set_billed ()
{
2017-10-07 13:09:31 +02:00
global $user ;
2016-05-23 09:16:47 +02:00
$error = 0 ;
2016-09-07 12:41:32 +02:00
2016-05-23 09:16:47 +02:00
$this -> db -> begin ();
2016-04-26 13:27:32 +02:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'expedition SET fk_statut=2, billed=1' ; // TODO Update only billed
$sql .= ' WHERE rowid = ' . $this -> id . ' AND fk_statut > 0' ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
2014-10-18 16:27:15 +02:00
$this -> statut = 2 ;
$this -> billed = 1 ;
2016-09-07 12:41:32 +02:00
2016-05-20 20:03:29 +02:00
// Call trigger
$result = $this -> call_trigger ( 'SHIPPING_BILLED' , $user );
2016-09-07 12:41:32 +02:00
if ( $result < 0 ) {
2016-05-23 09:16:47 +02:00
$error ++ ;
2016-05-20 20:03:29 +02:00
}
2016-09-07 12:41:32 +02:00
2016-05-23 09:16:47 +02:00
} else {
$error ++ ;
$this -> errors [] = $this -> db -> lasterror ;
}
2016-09-07 12:41:32 +02:00
2016-05-23 09:16:47 +02:00
if ( empty ( $error )) {
$this -> db -> commit ();
2012-07-02 19:30:37 +02:00
return 1 ;
}
else
{
2016-05-23 09:16:47 +02:00
$this -> db -> rollback ();
2012-07-02 19:30:37 +02:00
return - 1 ;
}
}
2012-03-14 14:00:20 +01:00
2016-02-24 13:19:25 +01:00
/**
* Classify the shipping as validated / opened
*
2018-02-15 15:27:06 +01:00
* @ return int < 0 if KO , 0 if already open , > 0 if OK
2016-02-24 13:19:25 +01:00
*/
function reOpen ()
{
2016-04-25 18:54:28 +02:00
global $conf , $langs , $user ;
2016-02-24 13:19:25 +01:00
2016-05-23 09:16:47 +02:00
$error = 0 ;
2016-09-07 12:41:32 +02:00
2018-02-15 15:27:06 +01:00
// Protection. This avoid to move stock later when we should not
if ( $this -> statut == self :: STATUS_VALIDATED )
{
return 0 ;
}
2016-05-23 09:16:47 +02:00
$this -> db -> begin ();
2016-09-07 12:41:32 +02:00
2016-02-24 13:19:25 +01:00
$sql = 'UPDATE ' . MAIN_DB_PREFIX . 'expedition SET fk_statut=1' ;
$sql .= ' WHERE rowid = ' . $this -> id . ' AND fk_statut > 0' ;
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$this -> statut = 1 ;
$this -> billed = 0 ;
2016-09-07 12:41:32 +02:00
2016-05-14 19:22:27 +02:00
// If stock increment is done on closing
if ( ! $error && ! empty ( $conf -> stock -> enabled ) && ! empty ( $conf -> global -> STOCK_CALCULATE_ON_SHIPMENT_CLOSE ))
2016-04-25 18:54:28 +02:00
{
require_once DOL_DOCUMENT_ROOT . '/product/stock/class/mouvementstock.class.php' ;
2016-09-07 12:41:32 +02:00
2016-04-25 18:54:28 +02:00
$langs -> load ( " agenda " );
2016-09-07 12:41:32 +02:00
2016-04-25 18:54:28 +02:00
// Loop on each product line to add a stock movement
// TODO possibilite d'expedier a partir d'une propale ou autre origine
$sql = " SELECT cd.fk_product, cd.subprice, " ;
$sql .= " ed.rowid, ed.qty, ed.fk_entrepot, " ;
$sql .= " edb.rowid as edbrowid, edb.eatby, edb.sellby, edb.batch, edb.qty as edbqty, edb.fk_origin_stock " ;
$sql .= " FROM " . MAIN_DB_PREFIX . " commandedet as cd, " ;
$sql .= " " . MAIN_DB_PREFIX . " expeditiondet as ed " ;
$sql .= " LEFT JOIN " . MAIN_DB_PREFIX . " expeditiondet_batch as edb on edb.fk_expeditiondet = ed.rowid " ;
$sql .= " WHERE ed.fk_expedition = " . $this -> id ;
$sql .= " AND cd.rowid = ed.fk_origin_line " ;
2016-09-07 12:41:32 +02:00
2016-04-25 18:54:28 +02:00
dol_syslog ( get_class ( $this ) . " ::valid select details " , LOG_DEBUG );
$resql = $this -> db -> query ( $sql );
if ( $resql )
{
$cpt = $this -> db -> num_rows ( $resql );
for ( $i = 0 ; $i < $cpt ; $i ++ )
{
$obj = $this -> db -> fetch_object ( $resql );
if ( empty ( $obj -> edbrowid ))
{
$qty = $obj -> qty ;
}
else
{
$qty = $obj -> edbqty ;
}
if ( $qty <= 0 ) continue ;
dol_syslog ( get_class ( $this ) . " ::reopen expedition movement index " . $i . " ed.rowid= " . $obj -> rowid . " edb.rowid= " . $obj -> edbrowid );
2016-09-07 12:41:32 +02:00
2016-04-25 18:54:28 +02:00
//var_dump($this->lines[$i]);
$mouvS = new MouvementStock ( $this -> db );
$mouvS -> origin = & $this ;
2016-09-07 12:41:32 +02:00
2016-04-25 18:54:28 +02:00
if ( empty ( $obj -> edbrowid ))
{
// line without batch detail
2016-09-07 12:41:32 +02:00
2016-04-25 18:54:28 +02:00
// We decrement stock of product (and sub-products) -> update table llx_product_stock (key of this table is fk_product+fk_entrepot) and add a movement record
2016-05-14 19:22:27 +02:00
$result = $mouvS -> livraison ( $user , $obj -> fk_product , $obj -> fk_entrepot , - $qty , $obj -> subprice , $langs -> trans ( " ShipmentUnClassifyCloseddInDolibarr " , $numref ));
2016-04-25 18:54:28 +02:00
if ( $result < 0 ) {
2017-10-07 13:09:31 +02:00
$this -> error = $mouvS -> error ;
$this -> errors = $mouvS -> errors ;
2016-04-25 18:54:28 +02:00
$error ++ ; break ;
}
}
else
{
// line with batch detail
2016-09-07 12:41:32 +02:00
2016-04-25 18:54:28 +02:00
// We decrement stock of product (and sub-products) -> update table llx_product_stock (key of this table is fk_product+fk_entrepot) and add a movement record
2016-09-28 00:22:29 +02:00
$result = $mouvS -> livraison ( $user , $obj -> fk_product , $obj -> fk_entrepot , - $qty , $obj -> subprice , $langs -> trans ( " ShipmentUnClassifyCloseddInDolibarr " , $numref ), '' , $this -> db -> jdate ( $obj -> eatby ), $this -> db -> jdate ( $obj -> sellby ), $obj -> batch , $obj -> fk_origin_stock );
2016-04-25 18:54:28 +02:00
if ( $result < 0 ) {
2017-10-07 13:09:31 +02:00
$this -> error = $mouvS -> error ;
$this -> errors = $mouvS -> errors ;
$error ++ ; break ;
2016-04-25 18:54:28 +02:00
}
}
}
}
else
{
2016-06-02 11:23:49 +02:00
$this -> error = $this -> db -> lasterror ();
$error ++ ;
2016-04-25 18:54:28 +02:00
}
2016-05-20 20:03:29 +02:00
}
2016-09-07 12:41:32 +02:00
2016-06-02 11:23:49 +02:00
if ( ! $error )
{
2017-10-07 13:09:31 +02:00
// Call trigger
$result = $this -> call_trigger ( 'SHIPPING_REOPEN' , $user );
if ( $result < 0 ) {
$error ++ ;
}
2016-06-02 11:23:49 +02:00
}
2016-09-07 12:41:32 +02:00
2016-05-23 09:16:47 +02:00
} else {
$error ++ ;
2016-06-02 11:23:49 +02:00
$this -> errors [] = $this -> db -> lasterror ();
2016-05-23 09:16:47 +02:00
}
2016-09-07 12:41:32 +02:00
if ( ! $error )
2016-06-02 11:23:49 +02:00
{
2016-05-23 09:16:47 +02:00
$this -> db -> commit ();
2016-02-24 13:19:25 +01:00
return 1 ;
}
else
{
2016-05-23 09:16:47 +02:00
$this -> db -> rollback ();
2016-02-24 13:19:25 +01:00
return - 1 ;
}
}
2016-09-07 12:41:32 +02:00
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
/**
2015-05-07 13:28:03 +02:00
* Create a document onto disk according to template module .
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
*
2015-05-06 23:13:33 +02:00
* @ param string $modele Force the model to using ( '' to not force )
* @ param Translate $outputlangs object lang to use for translations
* @ param int $hidedetails Hide details of lines
* @ param int $hidedesc Hide description
* @ param int $hideref Hide ref
2018-06-15 09:47:28 +02:00
* @ param null | array $moreparams Array to provide more information
2015-05-06 23:13:33 +02:00
* @ return int 0 if KO , 1 if OK
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
*/
2018-06-15 09:47:28 +02:00
public function generateDocument ( $modele , $outputlangs , $hidedetails = 0 , $hidedesc = 0 , $hideref = 0 , $moreparams = null )
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
{
2016-05-06 22:43:50 +02:00
global $conf , $langs ;
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
$langs -> load ( " sendings " );
2017-01-16 21:16:05 +01:00
if ( ! dol_strlen ( $modele )) {
$modele = 'rouget' ;
if ( $this -> modelpdf ) {
$modele = $this -> modelpdf ;
} elseif ( ! empty ( $conf -> global -> EXPEDITION_ADDON_PDF )) {
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
$modele = $conf -> global -> EXPEDITION_ADDON_PDF ;
}
}
2014-09-21 18:16:14 +02:00
$modelpath = " core/modules/expedition/doc/ " ;
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
2014-09-22 00:07:10 +02:00
$this -> fetch_origin ();
2018-06-15 09:47:28 +02:00
return $this -> commonGenerateDocument ( $modelpath , $modele , $outputlangs , $hidedetails , $hidedesc , $hideref , $moreparams );
Created a method generateDocument for several classes
Which are: Commande, Contrat, Livraison, Facture, Projet, Propal, Task, Expedition, CommandeFournisseur, FactureFournisseur and therefore deprecated the following methods supplier_order_pdf_create, supplier_invoice_pdf_create, delivery_order_pdf_create, task_pdf_create, propale_pdf_create, project_pdf_create, facture_pdf_create, expedition_pdf_create, commande_pdf_create
2014-09-16 12:30:37 +02:00
}
2015-04-18 18:11:01 +02:00
/**
* Function used to replace a thirdparty id with another one .
*
* @ param DoliDB $db Database handler
* @ param int $origin_id Old thirdparty id
* @ param int $dest_id New thirdparty id
* @ return bool
*/
public static function replaceThirdparty ( DoliDB $db , $origin_id , $dest_id )
{
$tables = array (
'expedition'
);
return CommonObject :: commonReplaceThirdparty ( $db , $origin_id , $dest_id , $tables );
}
2003-11-13 18:36:45 +01:00
}
2006-11-11 01:46:15 +01:00
/**
2011-09-29 22:32:28 +02:00
* Classe de gestion des lignes de bons d ' expedition
2008-05-26 23:27:50 +02:00
*/
2015-12-16 16:18:29 +01:00
class ExpeditionLigne extends CommonObjectLine
2003-11-13 18:36:45 +01:00
{
2017-10-15 17:58:22 +02:00
public $element = 'expeditiondet' ;
public $table_element = 'expeditiondet' ;
public $fk_origin_line ;
/**
* Id of shipment
* @ var int
*/
public $fk_expedition ;
2017-11-02 12:38:36 +01:00
var $db ;
2008-05-26 23:27:50 +02:00
2006-05-30 15:49:03 +02:00
// From llx_expeditiondet
2006-06-23 21:05:33 +02:00
var $qty ;
2008-06-14 12:53:12 +02:00
var $qty_shipped ;
2006-06-23 21:05:33 +02:00
var $fk_product ;
2014-03-07 11:35:16 +01:00
var $detail_batch ;
2017-10-15 17:58:22 +02:00
/**
* Id of warehouse
* @ var int
*/
public $entrepot_id ;
2017-11-02 12:38:36 +01:00
2008-05-26 23:27:50 +02:00
2008-01-17 11:27:59 +01:00
// From llx_commandedet or llx_propaldet
2008-06-14 12:53:12 +02:00
var $qty_asked ;
2015-04-23 23:21:06 +02:00
public $product_ref ;
public $product_label ;
public $product_desc ;
2012-03-14 14:00:20 +01:00
2012-03-08 22:30:45 +01:00
// Invoicing
2012-07-02 19:30:37 +02:00
var $remise_percent ;
var $total_ht ; // Total net of tax
var $total_ttc ; // Total with tax
var $total_tva ; // Total VAT
var $total_localtax1 ; // Total Local tax 1
2012-03-08 22:30:45 +01:00
var $total_localtax2 ; // Total Local tax 2
2006-11-11 01:46:15 +01:00
2017-11-02 12:38:36 +01:00
2015-04-23 23:21:06 +02:00
// Deprecated
/**
* @ deprecated
* @ see fk_origin_line
*/
var $origin_line_id ;
/**
* @ deprecated
* @ see product_ref
*/
var $ref ;
/**
* @ deprecated
* @ see product_label
*/
var $libelle ;
2012-01-04 23:56:10 +01:00
2017-10-15 17:58:22 +02:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
function __construct ( $db )
{
$this -> db = $db ;
}
2017-10-30 12:39:06 +01:00
/**
* Load line expedition
*
* @ param int $rowid Id line order
* @ return int < 0 if KO , > 0 if OK
*/
function fetch ( $rowid )
{
$sql = 'SELECT ed.rowid, ed.fk_expedition, ed.fk_entrepot, ed.fk_origin_line, ed.qty, ed.rang' ;
$sql .= ' FROM ' . MAIN_DB_PREFIX . $this -> table_element . ' as ed' ;
$sql .= ' WHERE ed.rowid = ' . $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_origin_line = $objp -> fk_origin_line ;
$this -> qty = $objp -> qty ;
$this -> rang = $objp -> rang ;
$this -> db -> free ( $result );
return 1 ;
}
else
{
$this -> errors [] = $this -> db -> lasterror ();
$this -> error = $this -> db -> lasterror ();
return - 1 ;
}
}
2017-10-07 13:09:31 +02:00
/**
2017-10-15 17:58:22 +02:00
* Insert line into database
2017-10-07 13:09:31 +02:00
*
2017-10-18 17:01:17 +02:00
* @ param User $user User that modify
2017-10-15 17:58:22 +02:00
* @ param int $notrigger 1 = disable triggers
* @ return int < 0 if KO , line id > 0 if OK
2017-10-07 13:09:31 +02:00
*/
2017-10-15 17:58:22 +02:00
function insert ( $user = null , $notrigger = 0 )
2006-05-30 15:49:03 +02:00
{
2017-10-15 17:58:22 +02:00
global $langs , $conf ;
$error = 0 ;
// Check parameters
2018-03-04 12:35:32 +01:00
if ( empty ( $this -> fk_expedition ) || empty ( $this -> fk_origin_line ) || ! is_numeric ( $this -> qty ))
2017-10-15 17:58:22 +02:00
{
2018-03-04 12:35:32 +01:00
$this -> error = 'ErrorMandatoryParametersNotProvided' ;
2017-10-15 17:58:22 +02:00
return - 1 ;
}
2017-10-18 17:01:17 +02:00
// Clean parameters
if ( empty ( $this -> entrepot_id )) $this -> entrepot_id = 'null' ;
2017-10-15 17:58:22 +02:00
2017-10-18 17:01:17 +02:00
$this -> db -> begin ();
2017-10-15 17:58:22 +02:00
$sql = " INSERT INTO " . MAIN_DB_PREFIX . " expeditiondet ( " ;
$sql .= " fk_expedition " ;
$sql .= " , fk_entrepot " ;
$sql .= " , fk_origin_line " ;
$sql .= " , qty " ;
$sql .= " ) VALUES ( " ;
2017-10-18 17:01:17 +02:00
$sql .= $this -> fk_expedition ;
2017-10-15 17:58:22 +02:00
$sql .= " , " . $this -> entrepot_id ;
2017-10-18 17:01:17 +02:00
$sql .= " , " . $this -> fk_origin_line ;
2017-10-15 17:58:22 +02:00
$sql .= " , " . $this -> qty ;
$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 " );
2018-08-12 16:29:26 +02:00
if ( ! $error && empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED ))
2017-10-15 17:58:22 +02:00
{
$result = $this -> insertExtraFields ();
if ( $result < 0 )
{
$error ++ ;
}
}
2017-10-18 17:01:17 +02:00
if ( ! $error && ! $notrigger )
{
// Call trigger
$result = $this -> call_trigger ( 'LINESHIPPING_INSERT' , $user );
if ( $result < 0 )
{
$error ++ ;
}
// End call triggers
}
if ( ! $error ) {
$this -> db -> commit ();
return $this -> id ;
}
foreach ( $this -> errors as $errmsg )
{
dol_syslog ( get_class ( $this ) . " ::delete " . $errmsg , LOG_ERR );
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
}
2018-03-10 23:40:56 +01:00
2017-10-18 17:01:17 +02:00
$this -> db -> rollback ();
return - 1 * $error ;
2017-10-15 17:58:22 +02:00
}
else
{
$error ++ ;
}
2017-10-18 17:01:17 +02:00
}
2017-10-15 17:58:22 +02:00
/**
* Delete shipment line .
2017-11-02 12:38:36 +01:00
*
2017-10-18 17:01:17 +02:00
* @ param User $user User that modify
* @ param int $notrigger 0 = launch triggers after , 1 = disable triggers
2017-10-15 17:58:22 +02:00
* @ return int > 0 if OK , < 0 if KO
*/
2017-10-18 17:01:17 +02:00
function delete ( $user = null , $notrigger = 0 )
2017-10-15 17:58:22 +02:00
{
global $conf ;
2017-10-18 17:01:17 +02:00
$error = 0 ;
2017-10-15 17:58:22 +02:00
$this -> db -> begin ();
2017-11-02 12:38:36 +01:00
2017-10-15 17:58:22 +02:00
// delete batch expedition line
if ( $conf -> productbatch -> enabled )
{
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " expeditiondet_batch " ;
$sql .= " WHERE fk_expeditiondet = " . $this -> id ;
if ( ! $this -> db -> query ( $sql ))
{
$this -> errors [] = $this -> db -> lasterror () . " - sql= $sql " ;
2017-10-18 17:01:17 +02:00
$error ++ ;
2017-10-15 17:58:22 +02:00
}
}
2017-11-02 12:38:36 +01:00
2017-10-15 17:58:22 +02:00
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " expeditiondet " ;
$sql .= " WHERE rowid = " . $this -> id ;
2017-10-18 17:01:17 +02:00
if ( ! $error && $this -> db -> query ( $sql ))
2017-10-15 17:58:22 +02:00
{
// Remove extrafields
if ( empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED )) // For avoid conflicts if trigger used
{
$result = $this -> deleteExtraFields ();
if ( $result < 0 )
{
$this -> errors [] = $this -> error ;
2017-10-18 17:01:17 +02:00
$error ++ ;
2017-10-15 17:58:22 +02:00
}
2017-10-18 17:01:17 +02:00
}
2017-11-02 12:38:36 +01:00
if ( ! $error && ! $notrigger )
2017-10-18 17:01:17 +02:00
{
// Call trigger
$result = $this -> call_trigger ( 'LINESHIPPING_DELETE' , $user );
if ( $result < 0 )
2017-10-15 17:58:22 +02:00
{
2017-10-18 17:01:17 +02:00
$this -> errors [] = $this -> error ;
$error ++ ;
2017-10-15 17:58:22 +02:00
}
2017-10-18 17:01:17 +02:00
// End call triggers
2017-10-15 17:58:22 +02:00
}
}
else
{
$this -> errors [] = $this -> db -> lasterror () . " - sql= $sql " ;
2017-10-18 17:01:17 +02:00
$error ++ ;
}
if ( ! $error ) {
$this -> db -> commit ();
return 1 ;
}
2017-11-02 12:38:36 +01:00
else
2017-10-18 17:01:17 +02:00
{
foreach ( $this -> errors as $errmsg )
{
dol_syslog ( get_class ( $this ) . " ::delete " . $errmsg , LOG_ERR );
$this -> error .= ( $this -> error ? ', ' . $errmsg : $errmsg );
}
2017-10-15 17:58:22 +02:00
$this -> db -> rollback ();
2017-10-18 17:01:17 +02:00
return - 1 * $error ;
2017-10-15 17:58:22 +02:00
}
2006-05-30 15:49:03 +02:00
}
2017-11-02 12:38:36 +01:00
2017-10-15 17:58:22 +02:00
/**
* Update a line in database
2017-11-02 12:38:36 +01:00
*
2017-10-24 15:48:22 +02:00
* @ param User $user User that modify
* @ param int $notrigger 1 = disable triggers
2017-10-15 17:58:22 +02:00
* @ return int < 0 if KO , > 0 if OK
*/
2017-10-18 17:01:17 +02:00
function update ( $user = null , $notrigger = 0 )
{
global $conf ;
2017-11-02 12:38:36 +01:00
2017-10-18 17:01:17 +02:00
$error = 0 ;
2017-10-15 17:58:22 +02:00
2017-10-18 17:01:17 +02:00
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 = null ;
2017-10-23 22:16:00 +02:00
$expedition_batch_id = null ;
2017-11-02 12:38:36 +01:00
if ( is_array ( $this -> detail_batch )) // array of ExpeditionLineBatch
2017-10-18 17:01:17 +02:00
{
2017-11-02 12:38:36 +01:00
if ( count ( $this -> detail_batch ) > 1 )
2017-10-18 17:01:17 +02:00
{
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 ;
2017-10-23 22:16:00 +02:00
$expedition_batch_id = $this -> detail_batch [ 0 ] -> id ;
2017-10-30 12:39:06 +01:00
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 ++ ;
}
2018-10-11 11:56:52 +02:00
$qty = price2num ( $this -> detail_batch [ 0 ] -> qty );
2017-10-18 17:01:17 +02:00
}
}
2017-10-30 12:39:06 +01:00
else if ( ! empty ( $this -> detail_batch ))
2017-10-18 17:01:17 +02:00
{
$batch = $this -> detail_batch -> batch ;
$batch_id = $this -> detail_batch -> fk_origin_stock ;
2017-10-23 22:16:00 +02:00
$expedition_batch_id = $this -> detail_batch -> id ;
2017-10-30 12:39:06 +01:00
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 ++ ;
}
2018-10-11 11:56:52 +02:00
$qty = price2num ( $this -> detail_batch -> qty );
2017-10-30 12:39:06 +01:00
}
// 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 );
$this -> errors [] = 'ErrorMandatoryParametersNotProvided' ;
$error ++ ;
return - 1 ;
2017-10-18 17:01:17 +02:00
}
2017-10-15 17:58:22 +02:00
// update lot
2017-10-18 17:01:17 +02:00
if ( ! empty ( $batch ) && $conf -> productbatch -> enabled )
2017-10-15 17:58:22 +02:00
{
2017-12-01 14:33:06 +01:00
dol_syslog ( get_class ( $this ) . " ::update expedition batch id= $expedition_batch_id , batch_id= $batch_id , batch= $batch " );
2017-11-02 11:44:36 +01:00
if ( empty ( $batch_id ) || empty ( $this -> fk_product )) {
2017-10-15 17:58:22 +02:00
dol_syslog ( get_class ( $this ) . '::update missing fk_origin_stock (batch_id) and/or fk_product' , LOG_ERR );
2017-10-18 17:01:17 +02:00
$this -> errors [] = 'ErrorMandatoryParametersNotProvided' ;
$error ++ ;
2017-10-15 17:58:22 +02:00
}
2017-10-18 17:01:17 +02:00
2017-10-15 17:58:22 +02:00
// fetch remaining lot qty
require_once DOL_DOCUMENT_ROOT . '/expedition/class/expeditionbatch.class.php' ;
2017-11-02 11:44:36 +01:00
if ( ! $error && ( $lotArray = ExpeditionLineBatch :: fetchAll ( $this -> db , $this -> id )) < 0 )
2017-10-15 17:58:22 +02:00
{
$this -> errors [] = $this -> db -> lasterror () . " - ExpeditionLineBatch::fetchAll " ;
2017-10-18 17:01:17 +02:00
$error ++ ;
}
2017-11-02 12:38:36 +01:00
else
2017-10-15 17:58:22 +02:00
{
2017-10-30 12:39:06 +01:00
// caculate new total line qty
2017-11-02 12:38:36 +01:00
foreach ( $lotArray as $lot )
2017-10-15 17:58:22 +02:00
{
2017-11-02 12:38:36 +01:00
if ( $expedition_batch_id != $lot -> id )
2017-10-18 17:01:17 +02:00
{
2018-10-11 11:56:52 +02:00
$remainingQty += $lot -> qty ;
2017-10-18 17:01:17 +02:00
}
}
2017-10-30 12:39:06 +01:00
$qty += $remainingQty ;
2017-11-02 12:38:36 +01:00
2017-10-18 17:01:17 +02:00
//fetch lot details
2017-11-02 12:38:36 +01:00
2017-10-18 17:01:17 +02:00
// fetch from product_lot
require_once DOL_DOCUMENT_ROOT . '/product/stock/class/productlot.class.php' ;
$lot = new Productlot ( $this -> db );
2017-11-02 12:38:36 +01:00
if ( $lot -> fetch ( 0 , $this -> fk_product , $batch ) < 0 )
2017-10-18 17:01:17 +02:00
{
$this -> errors [] = $lot -> errors ;
$error ++ ;
}
2017-11-02 11:44:36 +01:00
if ( ! $error && ! empty ( $expedition_batch_id ))
2017-10-18 17:01:17 +02:00
{
// delete lot expedition line
$sql = " DELETE FROM " . MAIN_DB_PREFIX . " expeditiondet_batch " ;
$sql .= " WHERE fk_expeditiondet = " . $this -> id ;
2017-10-23 22:16:00 +02:00
$sql .= " AND rowid = " . $expedition_batch_id ;
2017-11-02 12:38:36 +01:00
2017-10-18 17:01:17 +02:00
if ( ! $this -> db -> query ( $sql ))
{
$this -> errors [] = $this -> db -> lasterror () . " - sql= $sql " ;
$error ++ ;
}
2017-11-02 11:44:36 +01:00
}
2018-10-11 11:56:52 +02:00
if ( ! $error && $this -> detail_batch -> qty > 0 )
2017-11-02 11:44:36 +01:00
{
// create lot expedition line
2017-12-01 14:33:06 +01:00
if ( isset ( $lot -> id ))
2017-10-18 17:01:17 +02:00
{
2017-11-02 11:44:36 +01:00
$shipmentLot = new ExpeditionLineBatch ( $this -> db );
$shipmentLot -> batch = $lot -> batch ;
$shipmentLot -> eatby = $lot -> eatby ;
$shipmentLot -> sellby = $lot -> sellby ;
$shipmentLot -> entrepot_id = $this -> detail_batch -> entrepot_id ;
2018-10-11 11:56:52 +02:00
$shipmentLot -> qty = $this -> detail_batch -> qty ;
2017-11-02 11:44:36 +01:00
$shipmentLot -> fk_origin_stock = $batch_id ;
2017-12-01 14:33:06 +01:00
if ( $shipmentLot -> create ( $this -> id ) < 0 )
2017-10-18 17:01:17 +02:00
{
2017-11-02 11:44:36 +01:00
$this -> errors [] = $shipmentLot -> errors ;
$error ++ ;
2017-10-18 17:01:17 +02:00
}
}
2017-10-15 17:58:22 +02:00
}
}
2017-10-18 17:01:17 +02:00
}
if ( ! $error )
{
// update line
$sql = " UPDATE " . MAIN_DB_PREFIX . $this -> table_element . " SET " ;
2017-12-01 14:33:06 +01:00
$sql .= " fk_entrepot = " . ( $this -> entrepot_id > 0 ? $this -> entrepot_id : 'null' );
2017-10-30 12:39:06 +01:00
$sql .= " , qty = " . $qty ;
2017-10-18 17:01:17 +02:00
$sql .= " WHERE rowid = " . $this -> id ;
2017-11-02 12:38:36 +01:00
if ( ! $this -> db -> query ( $sql ))
2017-10-15 17:58:22 +02:00
{
$this -> errors [] = $this -> db -> lasterror () . " - sql= $sql " ;
2017-10-18 17:01:17 +02:00
$error ++ ;
2017-10-15 17:58:22 +02:00
}
2018-04-10 12:03:01 +02:00
}
if ( ! $error )
{
if ( empty ( $conf -> global -> MAIN_EXTRAFIELDS_DISABLED )) // For avoid conflicts if trigger used
2017-10-18 17:01:17 +02:00
{
2018-04-10 12:03:01 +02:00
$result = $this -> insertExtraFields ();
if ( $result < 0 )
2017-10-15 17:58:22 +02:00
{
2018-04-10 12:03:01 +02:00
$this -> errors [] = $this -> error ;
$error ++ ;
2017-10-15 17:58:22 +02:00
}
}
}
2018-04-10 12:03:01 +02:00
2017-11-02 12:38:36 +01:00
if ( ! $error && ! $notrigger )
2017-10-15 17:58:22 +02:00
{
2017-10-18 17:01:17 +02:00
// Call trigger
$result = $this -> call_trigger ( 'LINESHIPPING_UPDATE' , $user );
2017-10-15 17:58:22 +02:00
if ( $result < 0 )
{
$this -> errors [] = $this -> error ;
2017-10-18 17:01:17 +02:00
$error ++ ;
2017-10-15 17:58:22 +02:00
}
2017-10-18 17:01:17 +02:00
// End call triggers
}
if ( ! $error ) {
2017-10-15 17:58:22 +02:00
$this -> db -> commit ();
return 1 ;
}
2017-11-02 12:38:36 +01:00
else
2017-10-18 17:01:17 +02:00
{
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 ;
}
2017-10-15 17:58:22 +02:00
}
2003-11-13 18:36:45 +01:00
}