Create CommandeFournisseurLigne->insert

Create SupplierInvoiceLine->insert
This commit is contained in:
Sergio Sanchis Climent 2016-07-19 12:57:23 +02:00
parent d2209be6e3
commit 07b6c991ff
3 changed files with 710 additions and 312 deletions

View File

@ -50,89 +50,89 @@ class CommandeFournisseur extends CommonOrder
*/ */
protected $table_ref_field = 'ref'; protected $table_ref_field = 'ref';
var $id; public $id;
/** /**
* Supplier order reference * Supplier order reference
* @var string * @var string
*/ */
var $ref; public $ref;
var $ref_supplier; public $ref_supplier;
var $brouillon; public $brouillon;
var $statut; // 0=Draft -> 1=Validated -> 2=Approved -> 3=Process runing -> 4=Received partially -> 5=Received totally -> (reopen) 4=Received partially public $statut; // 0=Draft -> 1=Validated -> 2=Approved -> 3=Process runing -> 4=Received partially -> 5=Received totally -> (reopen) 4=Received partially
// -> 7=Canceled/Never received -> (reopen) 3=Process runing // -> 7=Canceled/Never received -> (reopen) 3=Process runing
// -> 6=Canceled -> (reopen) 2=Approved // -> 6=Canceled -> (reopen) 2=Approved
// -> 9=Refused -> (reopen) 1=Validated // -> 9=Refused -> (reopen) 1=Validated
// Note: billed or not is on another field "billed" // Note: billed or not is on another field "billed"
var $statuts; // List of status public $statuts; // List of status
var $socid; public $socid;
var $fourn_id; public $fourn_id;
var $date; public $date;
var $date_valid; public $date_valid;
var $date_approve; public $date_approve;
var $date_approve2; // Used when SUPPLIER_ORDER_DOUBLE_APPROVAL is set public $date_approve2; // Used when SUPPLIER_ORDER_DOUBLE_APPROVAL is set
var $date_commande; public $date_commande;
/** /**
* Delivery date * Delivery date
*/ */
var $date_livraison; public $date_livraison;
var $total_ht; public $total_ht;
var $total_tva; public $total_tva;
var $total_localtax1; // Total Local tax 1 public $total_localtax1; // Total Local tax 1
var $total_localtax2; // Total Local tax 2 public $total_localtax2; // Total Local tax 2
var $total_ttc; public $total_ttc;
var $source; public $source;
/** /**
* @deprecated * @deprecated
* @see note_private, note_public * @see note_private, note_public
*/ */
var $note; public $note;
public $note_private; public $note_private;
public $note_public; public $note_public;
var $model_pdf; public $model_pdf;
var $fk_project; public $fk_project;
var $cond_reglement_id; public $cond_reglement_id;
var $cond_reglement_code; public $cond_reglement_code;
var $fk_account; public $fk_account;
var $mode_reglement_id; public $mode_reglement_id;
var $mode_reglement_code; public $mode_reglement_code;
var $user_author_id; public $user_author_id;
var $user_valid_id; public $user_valid_id;
var $user_approve_id; public $user_approve_id;
var $user_approve_id2; // Used when SUPPLIER_ORDER_DOUBLE_APPROVAL is set public $user_approve_id2; // Used when SUPPLIER_ORDER_DOUBLE_APPROVAL is set
//Incoterms //Incoterms
var $fk_incoterms; public $fk_incoterms;
var $location_incoterms; public $location_incoterms;
var $libelle_incoterms; //Used into tooltip public $libelle_incoterms; //Used into tooltip
var $extraparams=array(); public $extraparams=array();
/** /**
* @var CommandeFournisseurLigne[] * @var CommandeFournisseurLigne[]
*/ */
public $lines = array(); public $lines = array();
//Add for supplier_proposal //Add for supplier_proposal
var $origin; public $origin;
var $origin_id; public $origin_id;
var $linked_objects=array(); public $linked_objects=array();
// Multicurrency // Multicurrency
var $fk_multicurrency; public $fk_multicurrency;
var $multicurrency_code; public $multicurrency_code;
var $multicurrency_tx; public $multicurrency_tx;
var $multicurrency_total_ht; public $multicurrency_total_ht;
var $multicurrency_total_tva; public $multicurrency_total_tva;
var $multicurrency_total_ttc; public $multicurrency_total_ttc;
/** /**
* Constructor * Constructor
* *
* @param DoliDB $db Database handler * @param DoliDB $db Database handler
*/ */
function __construct($db) public function __construct($db)
{ {
global $conf; global $conf;
@ -161,7 +161,7 @@ class CommandeFournisseur extends CommonOrder
* @param string $ref Ref of object * @param string $ref Ref of object
* @return int >0 if OK, <0 if KO, 0 if not found * @return int >0 if OK, <0 if KO, 0 if not found
*/ */
function fetch($id,$ref='') public function fetch($id,$ref='')
{ {
global $conf; global $conf;
@ -373,7 +373,7 @@ class CommandeFournisseur extends CommonOrder
* @param int $notrigger 1=Does not execute triggers, 0= execuete triggers * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
function valid($user,$idwarehouse=0,$notrigger=0) public function valid($user,$idwarehouse=0,$notrigger=0)
{ {
global $langs,$conf; global $langs,$conf;
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
@ -492,7 +492,7 @@ class CommandeFournisseur extends CommonOrder
* @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=short label + picto * @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 * @return string Label
*/ */
function getLibStatut($mode=0) public function getLibStatut($mode=0)
{ {
return $this->LibStatut($this->statut,$mode,$this->billed); return $this->LibStatut($this->statut,$mode,$this->billed);
} }
@ -584,7 +584,7 @@ class CommandeFournisseur extends CommonOrder
* @param string $option On what the link points * @param string $option On what the link points
* @return string Chain with URL * @return string Chain with URL
*/ */
function getNomUrl($withpicto=0,$option='') public function getNomUrl($withpicto=0,$option='')
{ {
global $langs, $conf; global $langs, $conf;
@ -620,7 +620,7 @@ class CommandeFournisseur extends CommonOrder
* @param Societe $soc company object * @param Societe $soc company object
* @return string free reference for the invoice * @return string free reference for the invoice
*/ */
function getNextNumRef($soc) public function getNextNumRef($soc)
{ {
global $db, $langs, $conf; global $db, $langs, $conf;
$langs->load("orders"); $langs->load("orders");
@ -674,8 +674,9 @@ class CommandeFournisseur extends CommonOrder
* @param User $user Object user making the change * @param User $user Object user making the change
* @return int <0 if KO, >0 if KO * @return int <0 if KO, >0 if KO
*/ */
function classifyBilled(User $user) public function classifyBilled(User $user)
{ {
$error=0;
$this->db->begin(); $this->db->begin();
$sql = 'UPDATE '.MAIN_DB_PREFIX.'commande_fournisseur SET billed = 1'; $sql = 'UPDATE '.MAIN_DB_PREFIX.'commande_fournisseur SET billed = 1';
@ -720,7 +721,7 @@ class CommandeFournisseur extends CommonOrder
* @param int $secondlevel 0=Standard approval, 1=Second level approval (used when option SUPPLIER_ORDER_DOUBLE_APPROVAL is set) * @param int $secondlevel 0=Standard approval, 1=Second level approval (used when option SUPPLIER_ORDER_DOUBLE_APPROVAL is set)
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
function approve($user, $idwarehouse=0, $secondlevel=0) public function approve($user, $idwarehouse=0, $secondlevel=0)
{ {
global $langs,$conf; global $langs,$conf;
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
@ -805,6 +806,7 @@ class CommandeFournisseur extends CommonOrder
// Product with reference // Product with reference
if ($this->lines[$i]->fk_product > 0) if ($this->lines[$i]->fk_product > 0)
{ {
$this->line = $this->lines[$i];
$mouvP = new MouvementStock($this->db); $mouvP = new MouvementStock($this->db);
$mouvP->origin = &$this; $mouvP->origin = &$this;
// We decrement stock of product (and sub-products) // We decrement stock of product (and sub-products)
@ -812,6 +814,7 @@ class CommandeFournisseur extends CommonOrder
if (! empty($this->lines[$i]->remise_percent) && empty($conf->global->STOCK_EXCLUDE_DISCOUNT_FOR_PMP)) $up_ht_disc=price2num($up_ht_disc * (100 - $this->lines[$i]->remise_percent) / 100, 'MU'); if (! empty($this->lines[$i]->remise_percent) && empty($conf->global->STOCK_EXCLUDE_DISCOUNT_FOR_PMP)) $up_ht_disc=price2num($up_ht_disc * (100 - $this->lines[$i]->remise_percent) / 100, 'MU');
$result=$mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans("OrderApprovedInDolibarr",$this->ref)); $result=$mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans("OrderApprovedInDolibarr",$this->ref));
if ($result < 0) { $error++; } if ($result < 0) { $error++; }
unset($this->line);
} }
} }
} }
@ -870,7 +873,7 @@ class CommandeFournisseur extends CommonOrder
* @param User $user User making action * @param User $user User making action
* @return int 0 if Ok, <0 if Ko * @return int 0 if Ok, <0 if Ko
*/ */
function refuse($user) public function refuse($user)
{ {
global $conf, $langs; global $conf, $langs;
@ -988,7 +991,7 @@ class CommandeFournisseur extends CommonOrder
* @param string $comment Comment * @param string $comment Comment
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
function commande($user, $date, $methode, $comment='') public function commande($user, $date, $methode, $comment='')
{ {
global $langs; global $langs;
dol_syslog(get_class($this)."::commande"); dol_syslog(get_class($this)."::commande");
@ -1046,7 +1049,7 @@ class CommandeFournisseur extends CommonOrder
* @param int $notrigger Disable all triggers * @param int $notrigger Disable all triggers
* @return int <0 if KO, Id of supplier order if OK * @return int <0 if KO, Id of supplier order if OK
*/ */
function create($user, $notrigger=0) public function create($user, $notrigger=0)
{ {
global $langs,$conf,$hookmanager; global $langs,$conf,$hookmanager;
@ -1217,7 +1220,7 @@ class CommandeFournisseur extends CommonOrder
* *
* @return int New id of clone * @return int New id of clone
*/ */
function createFromClone() public function createFromClone()
{ {
global $conf,$user,$langs,$hookmanager; global $conf,$user,$langs,$hookmanager;
@ -1304,7 +1307,7 @@ class CommandeFournisseur extends CommonOrder
* @param string $fk_unit Code of the unit to use. Null to use the default one * @param string $fk_unit Code of the unit to use. Null to use the default one
* @return int <=0 if KO, >0 if OK * @return int <=0 if KO, >0 if OK
*/ */
function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1=0.0, $txlocaltax2=0.0, $fk_product=0, $fk_prod_fourn_price=0, $fourn_ref='', $remise_percent=0.0, $price_base_type='HT', $pu_ttc=0.0, $type=0, $info_bits=0, $notrigger=false, $date_start=null, $date_end=null, $array_options=0, $fk_unit=null) public function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1=0.0, $txlocaltax2=0.0, $fk_product=0, $fk_prod_fourn_price=0, $fourn_ref='', $remise_percent=0.0, $price_base_type='HT', $pu_ttc=0.0, $type=0, $info_bits=0, $notrigger=false, $date_start=null, $date_end=null, $array_options=0, $fk_unit=null)
{ {
global $langs,$mysoc,$conf; global $langs,$mysoc,$conf;
@ -1420,77 +1423,80 @@ class CommandeFournisseur extends CommonOrder
$subprice = price2num($pu,'MU'); $subprice = price2num($pu,'MU');
// TODO We should use here $this->line=new CommandeFournisseurLigne($this->db); and $this->line->insert(); to work loke other object (proposal, order, invoice) // Insert line
$sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseurdet"; $this->line=new CommandeFournisseurLigne($this->db);
$sql.= " (fk_commande, label, description, date_start, date_end,";
$sql.= " fk_product, product_type,";
$sql.= " qty, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, ref,";
$sql.= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_unit,";
$sql.= " fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc";
$sql.= ")";
$sql.= " VALUES (".$this->id.", '" . $this->db->escape($label) . "','" . $this->db->escape($desc) . "',";
$sql.= " ".($date_start?"'".$this->db->idate($date_start)."'":"null").",";
$sql.= " ".($date_end?"'".$this->db->idate($date_end)."'":"null").",";
if ($fk_product) { $sql.= $fk_product.","; }
else { $sql.= "null,"; }
$sql.= "'".$product_type."',";
$sql.= "'".$qty."', ".$txtva.", ".$txlocaltax1.", ".$txlocaltax2;
$sql.= ", '".$localtax1_type."',"; $this->line->context = $this->context;
$sql.= " '".$localtax2_type."'";
$sql.= ", ".$remise_percent.",'".price2num($subprice,'MU')."','".$ref."',"; $this->line->fk_commande=$this->id;
$sql.= "'".price2num($total_ht)."',"; $this->line->label=$label;
$sql.= "'".price2num($total_tva)."',"; $this->line->desc=$desc;
$sql.= "'".price2num($total_localtax1)."',"; $this->line->qty=$qty;
$sql.= "'".price2num($total_localtax2)."',"; $this->line->tva_tx=$txtva;
$sql.= "'".price2num($total_ttc)."',"; $this->line->localtax1_tx=$txlocaltax1;
$sql.= ($fk_unit ? "'".$this->db->escape($fk_unit)."'":"null"); $this->line->localtax2_tx=$txlocaltax2;
$sql.= ", ".$this->fk_multicurrency; $this->line->localtax1_type = $localtaxes_type[0];
$sql.= ", '".$this->db->escape($this->multicurrency_code)."'"; $this->line->localtax2_type = $localtaxes_type[2];
$sql.= ", ".price2num($pu_ht * $this->multicurrency_tx); $this->line->fk_product=$fk_product;
$sql.= ", ".$multicurrency_total_ht; $this->line->product_type=$product_type;
$sql.= ", ".$multicurrency_total_tva; $this->line->remise_percent=$remise_percent;
$sql.= ", ".$multicurrency_total_ttc; $this->line->subprice=$pu_ht;
$sql.= ")"; $this->line->rang=$this->rang;
$this->line->info_bits=$info_bits;
$this->line->total_ht=$total_ht;
$this->line->total_tva=$total_tva;
$this->line->total_localtax1=$total_localtax1;
$this->line->total_localtax2=$total_localtax2;
$this->line->total_ttc=$total_ttc;
$this->line->product_type=$type;
$this->line->special_code=$this->special_code;
$this->line->origin=$this->origin;
$this->line->fk_unit=$fk_unit;
$resql=$this->db->query($sql); $this->line->date_start=$date_start;
//print $sql; $this->line->date_end=$date_end;
if ($resql)
{
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used // Multicurrency
{ $this->line->fk_multicurrency = $this->fk_multicurrency;
$linetmp = new CommandeFournisseurLigne($this->db); $this->line->multicurrency_code = $this->multicurrency_code;
$linetmp->id=$this->db->last_insert_id(MAIN_DB_PREFIX.'commande_fournisseurdet'); $this->line->multicurrency_subprice = price2num($pu_ht * $this->multicurrency_tx);
$linetmp->array_options = $array_options; $this->line->multicurrency_total_ht = $multicurrency_total_ht;
$result=$linetmp->insertExtraFields(); $this->line->multicurrency_total_tva = $multicurrency_total_tva;
if ($result < 0) $this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
{
$error++; $this->line->subprice=$pu;
} $this->line->price=$this->line->subprice;
$this->line->remise_percent=$remise_percent;
if (is_array($array_options) && count($array_options)>0) {
$this->line->array_options=$array_options;
} }
if (! $error && ! $notrigger) $result=$this->line->insert($notrigger);
if ($result > 0)
{ {
global $conf, $langs, $user; // Reorder if child line
// Call trigger if (! empty($fk_parent_line)) $this->line_order(true,'DESC');
$result=$this->call_trigger('LINEORDER_SUPPLIER_CREATE',$user);
if ($result < 0) // Mise a jour informations denormalisees au niveau de la commande meme
$result=$this->update_price(1,'auto'); // This method is designed to add line from user input so total calculation must be done using 'auto' mode.
if ($result > 0)
{
$this->db->commit();
return $this->line->id;
}
else
{ {
$this->db->rollback(); $this->db->rollback();
return -1; return -1;
} }
// End call triggers
}
$this->update_price('','auto');
$this->db->commit();
return 1;
} }
else else
{ {
$this->error=$this->db->error(); $this->error=$this->line->error;
dol_syslog(get_class($this)."::addline error=".$this->error, LOG_ERR);
$this->db->rollback(); $this->db->rollback();
return -1; return -1;
} }
@ -1514,7 +1520,7 @@ class CommandeFournisseur extends CommonOrder
* @param int $notrigger 1 = notrigger * @param int $notrigger 1 = notrigger
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
function dispatchProduct($user, $product, $qty, $entrepot, $price=0, $comment='', $eatby='', $sellby='', $batch='', $fk_commandefourndet=0, $notrigger=0) public function dispatchProduct($user, $product, $qty, $entrepot, $price=0, $comment='', $eatby='', $sellby='', $batch='', $fk_commandefourndet=0, $notrigger=0)
{ {
global $conf, $langs; global $conf, $langs;
@ -1616,9 +1622,10 @@ class CommandeFournisseur extends CommonOrder
* @param int $notrigger 1=Disable call to triggers * @param int $notrigger 1=Disable call to triggers
* @return <0 if KO, >0 if OK * @return <0 if KO, >0 if OK
*/ */
function deleteline($idline, $notrigger=0) public function deleteline($idline, $notrigger=0)
{ {
global $user,$langs,$conf; global $user,$langs,$conf;
$error = 0;
if ($this->statut != 0) if ($this->statut != 0)
{ {
@ -1671,7 +1678,7 @@ class CommandeFournisseur extends CommonOrder
* @param User $user Object user * @param User $user Object user
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
function delete($user='') public function delete($user='')
{ {
global $langs,$conf; global $langs,$conf;
require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
@ -1822,7 +1829,7 @@ class CommandeFournisseur extends CommonOrder
* @param int $status Filter on stats (-1 = no filter, 0 = lines draft to be approved, 1 = approved lines) * @param int $status Filter on stats (-1 = no filter, 0 = lines draft to be approved, 1 = approved lines)
* @return array Array of lines * @return array Array of lines
*/ */
function getDispachedLines($status=-1) public function getDispachedLines($status=-1)
{ {
$ret = array(); $ret = array();
@ -2048,7 +2055,7 @@ class CommandeFournisseur extends CommonOrder
* @param int $comclientid Id of customer order to use as template * @param int $comclientid Id of customer order to use as template
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
function updateFromCommandeClient($user, $idc, $comclientid) public function updateFromCommandeClient($user, $idc, $comclientid)
{ {
$comclient = new Commande($this->db); $comclient = new Commande($this->db);
$comclient->fetch($comclientid); $comclient->fetch($comclientid);
@ -2089,7 +2096,7 @@ class CommandeFournisseur extends CommonOrder
* @param int $status New status * @param int $status New status
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
function setStatus($user,$status) public function setStatus($user,$status)
{ {
global $conf,$langs; global $conf,$langs;
$error=0; $error=0;
@ -2147,12 +2154,14 @@ class CommandeFournisseur extends CommonOrder
* @param string $fk_unit Code of the unit to use. Null to use the default one * @param string $fk_unit Code of the unit to use. Null to use the default one
* @return int < 0 if error, > 0 if ok * @return int < 0 if error, > 0 if ok
*/ */
function updateline($rowid, $desc, $pu, $qty, $remise_percent, $txtva, $txlocaltax1=0, $txlocaltax2=0, $price_base_type='HT', $info_bits=0, $type=0, $notrigger=false, $date_start='', $date_end='', $array_options=0, $fk_unit=null) public function updateline($rowid, $desc, $pu, $qty, $remise_percent, $txtva, $txlocaltax1=0, $txlocaltax2=0, $price_base_type='HT', $info_bits=0, $type=0, $notrigger=false, $date_start='', $date_end='', $array_options=0, $fk_unit=null)
{ {
global $mysoc; global $mysoc, $conf;
dol_syslog(get_class($this)."::updateline $rowid, $desc, $pu, $qty, $remise_percent, $txtva, $price_base_type, $info_bits, $type, $fk_unit"); dol_syslog(get_class($this)."::updateline $rowid, $desc, $pu, $qty, $remise_percent, $txtva, $price_base_type, $info_bits, $type, $fk_unit");
include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
$error = 0;
if ($this->brouillon) if ($this->brouillon)
{ {
$this->db->begin(); $this->db->begin();
@ -2202,84 +2211,63 @@ class CommandeFournisseur extends CommonOrder
$subprice = price2num($pu,'MU'); $subprice = price2num($pu,'MU');
// Mise a jour ligne en base $this->line=new CommandeFournisseurLigne($this->db);
$sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseurdet SET"; $this->line->fetch($rowid);
$sql.= " description='".$this->db->escape($desc)."'";
$sql.= ",subprice='".price2num($subprice)."'"; $this->line->context = $this->context;
//$sql.= ",remise='".price2num($remise)."'";
$sql.= ",remise_percent='".price2num($remise_percent)."'"; $this->line->fk_commande=$this->id;
$sql.= ",tva_tx='".price2num($txtva)."'"; //$this->line->label=$label;
$sql.= ",localtax1_tx='".price2num($txlocaltax1)."'"; $this->line->desc=$desc;
$sql.= ",localtax2_tx='".price2num($txlocaltax2)."'"; $this->line->qty=$qty;
$sql.= ",localtax1_type='".$localtax1_type."'"; $this->line->tva_tx=$txtva;
$sql.= ",localtax2_type='".$localtax2_type."'"; $this->line->localtax1_tx=$txlocaltax1;
$sql.= ",qty='".price2num($qty)."'"; $this->line->localtax2_tx=$txlocaltax2;
$sql.= ",date_start=".(! empty($date_start)?"'".$this->db->idate($date_start)."'":"null"); $this->line->localtax1_type = $localtaxes_type[0];
$sql.= ",date_end=".(! empty($date_end)?"'".$this->db->idate($date_end)."'":"null"); $this->line->localtax2_type = $localtaxes_type[2];
$sql.= ",info_bits='".$info_bits."'"; $this->line->remise_percent=$remise_percent;
$sql.= ",total_ht='".price2num($total_ht)."'"; $this->line->subprice=$pu;
$sql.= ",total_tva='".price2num($total_tva)."'"; $this->line->rang=$this->rang;
$sql.= ",total_localtax1='".price2num($total_localtax1)."'"; $this->line->info_bits=$info_bits;
$sql.= ",total_localtax2='".price2num($total_localtax2)."'"; $this->line->total_ht=$total_ht;
$sql.= ",total_ttc='".price2num($total_ttc)."'"; $this->line->total_tva=$total_tva;
$sql.= ",product_type=".$type; $this->line->total_localtax1=$total_localtax1;
$sql.= ($fk_unit ? ",fk_unit='".$this->db->escape($fk_unit)."'":", fk_unit=null"); $this->line->total_localtax2=$total_localtax2;
$this->line->total_ttc=$total_ttc;
$this->line->product_type=$type;
$this->line->special_code=$this->special_code;
$this->line->origin=$this->origin;
$this->line->fk_unit=$fk_unit;
$this->line->date_start=$date_start;
$this->line->date_end=$date_end;
// Multicurrency // Multicurrency
$sql.= " , multicurrency_subprice=".price2num($subprice * $this->multicurrency_tx).""; $this->line->fk_multicurrency = $this->fk_multicurrency;
$sql.= " , multicurrency_total_ht=".price2num($multicurrency_total_ht).""; $this->line->multicurrency_code = $this->multicurrency_code;
$sql.= " , multicurrency_total_tva=".price2num($multicurrency_total_tva).""; $this->line->multicurrency_subprice = price2num($pu_ht * $this->multicurrency_tx);
$sql.= " , multicurrency_total_ttc=".price2num($multicurrency_total_ttc).""; $this->line->multicurrency_total_ht = $multicurrency_total_ht;
$this->line->multicurrency_total_tva = $multicurrency_total_tva;
$this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
$sql.= " WHERE rowid = ".$rowid; $this->line->subprice=$pu;
$this->line->price=$this->line->subprice;
dol_syslog(get_class($this)."::updateline", LOG_DEBUG); $this->line->remise_percent=$remise_percent;
$result = $this->db->query($sql);
if ($result > 0) if (is_array($array_options) && count($array_options)>0) {
{ $this->line->array_options=$array_options;
$this->rowid = $rowid;
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{
$tmpline = new CommandeFournisseurLigne($this->db);
$tmpline->id=$this->rowid;
$tmpline->array_options = $array_options;
$result=$tmpline->insertExtraFields();
if ($result < 0)
{
$error++;
}
} }
if (! $error && ! $notrigger) $result=$this->line->update($notrigger);
{
global $conf, $langs, $user;
// Call trigger
$result=$this->call_trigger('LINEORDER_SUPPLIER_UPDATE',$user);
if ($result < 0)
{
$this->db->rollback();
return -1;
}
// End call triggers
}
// Mise a jour info denormalisees au niveau facture // Mise a jour info denormalisees au niveau facture
if (! $error) if (! $error)
{ {
$this->update_price('','auto'); $this->update_price('','auto');
} }
if (! $error)
{
$this->db->commit();
return $result;
}
else
{
$this->db->rollback();
return -1;
}
}
else else
{ {
$this->error=$this->db->lasterror(); $this->error=$this->db->lasterror();
@ -2303,7 +2291,7 @@ class CommandeFournisseur extends CommonOrder
* *
* @return void * @return void
*/ */
function initAsSpecimen() public function initAsSpecimen()
{ {
global $user,$langs,$conf; global $user,$langs,$conf;
@ -2385,7 +2373,7 @@ class CommandeFournisseur extends CommonOrder
* @param int $id Id de la facture a charger * @param int $id Id de la facture a charger
* @return void * @return void
*/ */
function info($id) public function info($id)
{ {
$sql = 'SELECT c.rowid, date_creation as datec, tms as datem, date_valid as date_validation, date_approve as datea, date_approve2 as datea2,'; $sql = 'SELECT c.rowid, date_creation as datec, tms as datem, date_valid as date_validation, date_approve as datea, date_approve2 as datea2,';
$sql.= ' fk_user_author, fk_user_modif, fk_user_valid, fk_user_approve, fk_user_approve2'; $sql.= ' fk_user_author, fk_user_modif, fk_user_valid, fk_user_approve, fk_user_approve2';
@ -2523,7 +2511,7 @@ class CommandeFournisseur extends CommonOrder
* *
* @return string * @return string
*/ */
function getInputMethod() public function getInputMethod()
{ {
global $db, $langs; global $db, $langs;
@ -2593,7 +2581,7 @@ class CommandeFournisseur extends CommonOrder
* @param Translate $langs Language object * @param Translate $langs Language object
* @return Translated string * @return Translated string
*/ */
function getMaxDeliveryTimeDay($langs) public function getMaxDeliveryTimeDay($langs)
{ {
if (empty($this->lines)) return ''; if (empty($this->lines)) return '';
@ -2692,14 +2680,28 @@ class CommandeFournisseurLigne extends CommonOrderLine
public $element='commande_fournisseurdet'; public $element='commande_fournisseurdet';
public $table_element='commande_fournisseurdet'; public $table_element='commande_fournisseurdet';
public $oldline;
/**
* Id of parent order
* @var int
*/
public $fk_commande;
// From llx_commande_fournisseurdet
public $fk_parent_line;
public $fk_facture;
public $label;
public $rang = 0;
/** /**
* Unit price without taxes * Unit price without taxes
* @var float * @var float
*/ */
public $pu_ht; public $pu_ht;
var $date_start; public $date_start;
var $date_end; public $date_end;
// From llx_product_fournisseur_price // From llx_product_fournisseur_price
@ -2716,6 +2718,8 @@ class CommandeFournisseurLigne extends CommonOrderLine
* @var string * @var string
*/ */
public $ref_supplier; public $ref_supplier;
public $remise;
public $product_libelle;
/** /**
@ -2723,7 +2727,7 @@ class CommandeFournisseurLigne extends CommonOrderLine
* *
* @param DoliDB $db Database handler * @param DoliDB $db Database handler
*/ */
function __construct($db) public function __construct($db)
{ {
$this->db= $db; $this->db= $db;
} }
@ -2734,7 +2738,7 @@ class CommandeFournisseurLigne extends CommonOrderLine
* @param int $rowid Id line order * @param int $rowid Id line order
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
function fetch($rowid) public function fetch($rowid)
{ {
$sql = 'SELECT cd.rowid, cd.fk_commande, cd.fk_product, cd.product_type, cd.description, cd.qty, cd.tva_tx,'; $sql = 'SELECT cd.rowid, cd.fk_commande, cd.fk_product, cd.product_type, cd.description, cd.qty, cd.tva_tx,';
$sql.= ' cd.localtax1_tx, cd.localtax2_tx,'; $sql.= ' cd.localtax1_tx, cd.localtax2_tx,';
@ -2751,6 +2755,7 @@ class CommandeFournisseurLigne extends CommonOrderLine
{ {
$objp = $this->db->fetch_object($result); $objp = $this->db->fetch_object($result);
$this->rowid = $objp->rowid; $this->rowid = $objp->rowid;
$this->id = $objp->rowid;
$this->fk_commande = $objp->fk_commande; $this->fk_commande = $objp->fk_commande;
$this->desc = $objp->description; $this->desc = $objp->description;
$this->qty = $objp->qty; $this->qty = $objp->qty;
@ -2770,6 +2775,7 @@ class CommandeFournisseurLigne extends CommonOrderLine
$this->product_type = $objp->product_type; $this->product_type = $objp->product_type;
$this->ref = $objp->product_ref; $this->ref = $objp->product_ref;
$this->product_ref = $objp->product_ref;
$this->product_libelle = $objp->product_libelle; $this->product_libelle = $objp->product_libelle;
$this->product_desc = $objp->product_desc; $this->product_desc = $objp->product_desc;
@ -2792,7 +2798,7 @@ class CommandeFournisseurLigne extends CommonOrderLine
* *
* @return int <0 si ko, >0 si ok * @return int <0 si ko, >0 si ok
*/ */
function update_total() public function update_total()
{ {
$this->db->begin(); $this->db->begin();
@ -2820,5 +2826,209 @@ class CommandeFournisseurLigne extends CommonOrderLine
return -2; return -2;
} }
} }
/**
* Insert line into database
*
* @param int $notrigger 1 = disable triggers
* @return int <0 if KO, >0 if OK
*/
public function insert($notrigger=0)
{
global $conf, $user;
$error=0;
dol_syslog(get_class($this)."::insert rang=".$this->rang);
// Clean parameters
if (empty($this->tva_tx)) $this->tva_tx=0;
if (empty($this->localtax1_tx)) $this->localtax1_tx=0;
if (empty($this->localtax2_tx)) $this->localtax2_tx=0;
if (empty($this->localtax1_type)) $this->localtax1_type=0;
if (empty($this->localtax2_type)) $this->localtax2_type=0;
if (empty($this->total_localtax1)) $this->total_localtax1=0;
if (empty($this->total_localtax2)) $this->total_localtax2=0;
if (empty($this->rang)) $this->rang=0;
if (empty($this->remise)) $this->remise=0;
if (empty($this->remise_percent)) $this->remise_percent=0;
if (empty($this->info_bits)) $this->info_bits=0;
if (empty($this->special_code)) $this->special_code=0;
if (empty($this->fk_parent_line)) $this->fk_parent_line=0;
if (empty($this->pa_ht)) $this->pa_ht=0;
// Check parameters
if ($this->product_type < 0) return -1;
$this->db->begin();
// Insertion dans base de la ligne
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element;
$sql.= " (fk_commande, label, description, date_start, date_end,";
$sql.= " fk_product, product_type,";
$sql.= " qty, tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type, remise_percent, subprice, ref,";
$sql.= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc, fk_unit,";
$sql.= " fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc";
$sql.= ")";
$sql.= " VALUES (".$this->fk_commande.", '" . $this->db->escape($this->product_label) . "','" . $this->db->escape($this->desc) . "',";
$sql.= " ".($this->date_start?"'".$this->db->idate($this->date_start)."'":"null").",";
$sql.= " ".($this->date_end?"'".$this->db->idate($this->date_end)."'":"null").",";
if ($this->fk_product) { $sql.= $this->fk_product.","; }
else { $sql.= "null,"; }
$sql.= "'".$this->product_type."',";
$sql.= "'".$this->qty."', ".$this->tva_tx.", ".$this->localtax1_tx.", ".$this->localtax2_tx;
$sql.= ", '".$this->localtax1_type."',";
$sql.= " '".$this->localtax2_type."'";
$sql.= ", ".$this->remise_percent.",'".price2num($this->subprice,'MU')."','".$this->product_ref."',";
$sql.= "'".price2num($this->total_ht)."',";
$sql.= "'".price2num($this->total_tva)."',";
$sql.= "'".price2num($this->total_localtax1)."',";
$sql.= "'".price2num($this->total_localtax2)."',";
$sql.= "'".price2num($this->total_ttc)."',";
$sql.= ($this->fk_unit ? "'".$this->db->escape($this->fk_unit)."'":"null");
$sql.= ", ".$this->fk_multicurrency;
$sql.= ", '".$this->db->escape($this->multicurrency_code)."'";
$sql.= ", ".price2num($this->pu_ht * $this->multicurrency_tx);
$sql.= ", ".$this->multicurrency_total_ht;
$sql.= ", ".$this->multicurrency_total_tva;
$sql.= ", ".$this->multicurrency_total_ttc;
$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.$this->table_element);
$this->rowid =$this->id;
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{
$result=$this->insertExtraFields();
if ($result < 0)
{
$error++;
}
}
if (! $error && ! $notrigger)
{
// Call trigger
$result=$this->call_trigger('LINEORDER_INSERT',$user);
if ($result < 0) $error++;
// End call triggers
}
if (!$error) {
$this->db->commit();
return 1;
}
foreach($this->errors as $errmsg)
{
dol_syslog(get_class($this)."::delete ".$errmsg, LOG_ERR);
$this->errors[]=($this->errors?', '.$errmsg:$errmsg);
}
$this->db->rollback();
return -1*$error;
}
else
{
$this->errors[]=$this->db->error();
$this->db->rollback();
return -2;
}
}
/**
* Update the line object into db
*
* @param int $notrigger 1 = disable triggers
* @return int <0 si ko, >0 si ok
*/
public function update($notrigger=0)
{
global $conf,$user;
$error=0;
// Mise a jour ligne en base
$sql = "UPDATE ".MAIN_DB_PREFIX.$this->table_element." SET";
$sql.= " description='".$this->db->escape($this->desc)."'";
$sql.= ",subprice='".price2num($this->subprice)."'";
//$sql.= ",remise='".price2num($remise)."'";
$sql.= ",remise_percent='".price2num($this->remise_percent)."'";
$sql.= ",tva_tx='".price2num($this->tva_tx)."'";
$sql.= ",localtax1_tx='".price2num($this->total_localtax1)."'";
$sql.= ",localtax2_tx='".price2num($this->total_localtax2)."'";
$sql.= ",localtax1_type='".$this->localtax1_type."'";
$sql.= ",localtax2_type='".$this->localtax2_type."'";
$sql.= ",qty='".price2num($this->qty)."'";
$sql.= ",date_start=".(! empty($this->date_start)?"'".$this->db->idate($this->date_start)."'":"null");
$sql.= ",date_end=".(! empty($this->date_end)?"'".$this->db->idate($this->date_end)."'":"null");
$sql.= ",info_bits='".$this->info_bits."'";
$sql.= ",total_ht='".price2num($this->total_ht)."'";
$sql.= ",total_tva='".price2num($this->total_tva)."'";
$sql.= ",total_localtax1='".price2num($this->total_localtax1)."'";
$sql.= ",total_localtax2='".price2num($this->total_localtax2)."'";
$sql.= ",total_ttc='".price2num($this->total_ttc)."'";
$sql.= ",product_type=".$this->product_type;
$sql.= ($this->fk_unit ? ",fk_unit='".$this->db->escape($this->fk_unit)."'":", fk_unit=null");
// Multicurrency
$sql.= " , multicurrency_subprice=".price2num($this->subprice * $this->multicurrency_tx)."";
$sql.= " , multicurrency_total_ht=".price2num($this->multicurrency_total_ht)."";
$sql.= " , multicurrency_total_tva=".price2num($this->multicurrency_total_tva)."";
$sql.= " , multicurrency_total_ttc=".price2num($this->multicurrency_total_ttc)."";
$sql.= " WHERE rowid = ".$this->id;
dol_syslog(get_class($this)."::updateline", LOG_DEBUG);
$result = $this->db->query($sql);
if ($result > 0)
{
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{
$result=$this->insertExtraFields();
if ($result < 0)
{
$error++;
}
}
if (! $error && ! $notrigger)
{
global $user;
// Call trigger
$result=$this->call_trigger('LINEORDER_SUPPLIER_UPDATE',$user);
if ($result < 0)
{
$this->db->rollback();
return -1;
}
// End call triggers
}
if (! $error)
{
$this->db->commit();
return $result;
}
else
{
$this->db->rollback();
return -1;
}
}
else
{
$this->error=$this->db->lasterror();
$this->db->rollback();
return -1;
}
}
} }

View File

@ -50,57 +50,57 @@ class FactureFournisseur extends CommonInvoice
*/ */
protected $table_ref_field = 'ref'; protected $table_ref_field = 'ref';
var $rowid; public $rowid;
var $ref; public $ref;
var $product_ref; public $product_ref;
var $ref_supplier; public $ref_supplier;
var $socid; public $socid;
//Check constants for types //Check constants for types
var $type = self::TYPE_STANDARD; public $type = self::TYPE_STANDARD;
/** /**
* Supplier invoice status * Supplier invoice status
* @var int * @var int
* @see FactureFournisseur::STATUS_DRAFT, FactureFournisseur::STATUS_VALIDATED, FactureFournisseur::STATUS_PAID, FactureFournisseur::STATUS_ABANDONED * @see FactureFournisseur::STATUS_DRAFT, FactureFournisseur::STATUS_VALIDATED, FactureFournisseur::STATUS_PAID, FactureFournisseur::STATUS_ABANDONED
*/ */
var $statut; public $statut;
/** /**
* Set to 1 if the invoice is completely paid, otherwise is 0 * Set to 1 if the invoice is completely paid, otherwise is 0
* @var int * @var int
* @deprecated Use statuses stored in self::statut * @deprecated Use statuses stored in self::statut
*/ */
var $paye; public $paye;
var $author; public $author;
var $libelle; public $libelle;
var $datec; // Creation date public $datec; // Creation date
var $tms; // Last update date public $tms; // Last update date
var $date; // Invoice date public $date; // Invoice date
var $date_echeance; // Max payment date public $date_echeance; // Max payment date
var $amount; public $amount;
var $remise; public $remise;
var $tva; public $tva;
var $localtax1; public $localtax1;
var $localtax2; public $localtax2;
var $total_ht; public $total_ht;
var $total_tva; public $total_tva;
var $total_localtax1; public $total_localtax1;
var $total_localtax2; public $total_localtax2;
var $total_ttc; public $total_ttc;
/** /**
* @deprecated * @deprecated
* @see note_private, note_public * @see note_private, note_public
*/ */
var $note; public $note;
var $note_private; public $note_private;
var $note_public; public $note_public;
var $propalid; public $propalid;
var $cond_reglement_id; public $cond_reglement_id;
var $cond_reglement_code; public $cond_reglement_code;
var $fk_account; public $fk_account;
var $mode_reglement_id; public $mode_reglement_id;
var $mode_reglement_code; public $mode_reglement_code;
/** /**
* Invoice lines * Invoice lines
@ -110,29 +110,29 @@ class FactureFournisseur extends CommonInvoice
/** /**
* @deprecated * @deprecated
*/ */
var $fournisseur; public $fournisseur;
//Incorterms //Incorterms
var $fk_incoterms; public $fk_incoterms;
var $location_incoterms; public $location_incoterms;
var $libelle_incoterms; //Used into tooltip public $libelle_incoterms; //Used into tooltip
var $extraparams=array(); public $extraparams=array();
// Multicurrency // Multicurrency
var $fk_multicurrency; public $fk_multicurrency;
var $multicurrency_code; public $multicurrency_code;
var $multicurrency_tx; public $multicurrency_tx;
var $multicurrency_total_ht; public $multicurrency_total_ht;
var $multicurrency_total_tva; public $multicurrency_total_tva;
var $multicurrency_total_ttc; public $multicurrency_total_ttc;
/** /**
* Constructor * Constructor
* *
* @param DoliDB $db Database handler * @param DoliDB $db Database handler
*/ */
function __construct($db) public function __construct($db)
{ {
$this->db = $db; $this->db = $db;
@ -155,7 +155,7 @@ class FactureFournisseur extends CommonInvoice
* @param User $user object utilisateur qui cree * @param User $user object utilisateur qui cree
* @return int id facture si ok, < 0 si erreur * @return int id facture si ok, < 0 si erreur
*/ */
function create($user) public function create($user)
{ {
global $langs,$conf,$hookmanager; global $langs,$conf,$hookmanager;
@ -355,7 +355,7 @@ class FactureFournisseur extends CommonInvoice
* @param string $ref Ref supplier invoice * @param string $ref Ref supplier invoice
* @return int <0 if KO, >0 if OK, 0 if not found * @return int <0 if KO, >0 if OK, 0 if not found
*/ */
function fetch($id='',$ref='') public function fetch($id='',$ref='')
{ {
global $langs; global $langs;
@ -613,7 +613,7 @@ class FactureFournisseur extends CommonInvoice
* @param int $notrigger 0=launch triggers after, 1=disable triggers * @param int $notrigger 0=launch triggers after, 1=disable triggers
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
function update($user=null, $notrigger=0) public function update($user=null, $notrigger=0)
{ {
global $conf, $langs; global $conf, $langs;
$error=0; $error=0;
@ -741,7 +741,7 @@ class FactureFournisseur extends CommonInvoice
* @param int $rowid Id of invoice to delete * @param int $rowid Id of invoice to delete
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
function delete($rowid) public function delete($rowid)
{ {
global $user,$langs,$conf; global $user,$langs,$conf;
@ -955,7 +955,7 @@ class FactureFournisseur extends CommonInvoice
* @param int $notrigger 1=Does not execute triggers, 0= execuete triggers * @param int $notrigger 1=Does not execute triggers, 0= execuete triggers
* @return int <0 if KO, =0 if nothing to do, >0 if OK * @return int <0 if KO, =0 if nothing to do, >0 if OK
*/ */
function validate($user, $force_number='', $idwarehouse=0, $notrigger=0) public function validate($user, $force_number='', $idwarehouse=0, $notrigger=0)
{ {
global $conf,$langs; global $conf,$langs;
@ -1011,6 +1011,7 @@ class FactureFournisseur extends CommonInvoice
{ {
if ($this->lines[$i]->fk_product > 0) if ($this->lines[$i]->fk_product > 0)
{ {
$this->line = $this->lines[$i];
$mouvP = new MouvementStock($this->db); $mouvP = new MouvementStock($this->db);
$mouvP->origin = &$this; $mouvP->origin = &$this;
// We increase stock for product // We increase stock for product
@ -1018,6 +1019,7 @@ class FactureFournisseur extends CommonInvoice
if (! empty($this->lines[$i]->remise_percent) && empty($conf->global->STOCK_EXCLUDE_DISCOUNT_FOR_PMP)) $up_ht_disc=price2num($up_ht_disc * (100 - $this->lines[$i]->remise_percent) / 100, 'MU'); if (! empty($this->lines[$i]->remise_percent) && empty($conf->global->STOCK_EXCLUDE_DISCOUNT_FOR_PMP)) $up_ht_disc=price2num($up_ht_disc * (100 - $this->lines[$i]->remise_percent) / 100, 'MU');
$result=$mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans("InvoiceValidatedInDolibarr",$num)); $result=$mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $up_ht_disc, $langs->trans("InvoiceValidatedInDolibarr",$num));
if ($result < 0) { $error++; } if ($result < 0) { $error++; }
unset($this->line);
} }
} }
} }
@ -1194,14 +1196,16 @@ class FactureFournisseur extends CommonInvoice
* @param int $notrigger Disable triggers * @param int $notrigger Disable triggers
* @param array $array_options extrafields array * @param array $array_options extrafields array
* @param string $fk_unit Code of the unit to use. Null to use the default one * @param string $fk_unit Code of the unit to use. Null to use the default one
* @param int $origin_id id origin document
* @return int >0 if OK, <0 if KO * @return int >0 if OK, <0 if KO
* *
* FIXME Add field ref (that should be named ref_supplier) and label into update. For example can be filled when product line created from order. * FIXME Add field ref (that should be named ref_supplier) and label into update. For example can be filled when product line created from order.
*/ */
function addline($desc, $pu, $txtva, $txlocaltax1, $txlocaltax2, $qty, $fk_product=0, $remise_percent=0, $date_start='', $date_end='', $ventil=0, $info_bits='', $price_base_type='HT', $type=0, $rang=-1, $notrigger=false, $array_options=0, $fk_unit=null) public function addline($desc, $pu, $txtva, $txlocaltax1, $txlocaltax2, $qty, $fk_product=0, $remise_percent=0, $date_start='', $date_end='', $ventil=0, $info_bits='', $price_base_type='HT', $type=0, $rang=-1, $notrigger=false, $array_options=0, $fk_unit=null, $origin_id=0)
{ {
dol_syslog(get_class($this)."::addline $desc,$pu,$qty,$txtva,$fk_product,$remise_percent,$date_start,$date_end,$ventil,$info_bits,$price_base_type,$type,$fk_unit", LOG_DEBUG); dol_syslog(get_class($this)."::addline $desc,$pu,$qty,$txtva,$fk_product,$remise_percent,$date_start,$date_end,$ventil,$info_bits,$price_base_type,$type,$fk_unit", LOG_DEBUG);
include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php'; include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
global $mysoc;
// Clean parameters // Clean parameters
if (empty($remise_percent)) $remise_percent=0; if (empty($remise_percent)) $remise_percent=0;
@ -1220,52 +1224,92 @@ class FactureFournisseur extends CommonInvoice
$txlocaltax1=price2num($txlocaltax1); $txlocaltax1=price2num($txlocaltax1);
$txlocaltax2=price2num($txlocaltax2); $txlocaltax2=price2num($txlocaltax2);
$localtaxes_type=getLocalTaxesFromRate($txtva,0,$mysoc, $this->thirdparty);
$tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits, $type, $this->thirdparty, $localtaxes_type, 100, $this->multicurrency_tx);
$total_ht = $tabprice[0];
$total_tva = $tabprice[1];
$total_ttc = $tabprice[2];
$total_localtax1 = $tabprice[9];
$total_localtax2 = $tabprice[10];
// MultiCurrency
$multicurrency_total_ht = $tabprice[16];
$multicurrency_total_tva = $tabprice[17];
$multicurrency_total_ttc = $tabprice[18];
// Check parameters // Check parameters
if ($type < 0) return -1; if ($type < 0) return -1;
// Insert line
$this->line=new SupplierInvoiceLine($this->db);
$this->db->begin(); $this->line->context = $this->context;
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.'facture_fourn_det (fk_facture_fourn)'; $this->line->fk_facture_fourn=$this->id;
$sql.= ' VALUES ('.$this->id.')'; //$this->line->label=$label; // deprecated
dol_syslog(get_class($this)."::addline", LOG_DEBUG); $this->line->desc=$desc;
$this->line->qty= ($this->type==self::TYPE_CREDIT_NOTE?abs($qty):$qty); // For credit note, quantity is always positive and unit price negative
$this->line->tva_tx=$txtva;
$this->line->localtax1_tx=$txlocaltax1;
$this->line->localtax2_tx=$txlocaltax2;
$this->line->fk_product=$fk_product;
$this->line->product_type=$type;
$this->line->remise_percent=$remise_percent;
$this->line->subprice= ($this->type==self::TYPE_CREDIT_NOTE?-abs($pu):$pu); // For credit note, unit price always negative, always positive otherwise
$this->line->date_start=$date_start;
$this->line->date_end=$date_end;
$this->line->ventil=$ventil;
$this->line->rang=$rang;
$this->line->info_bits=$info_bits;
$this->line->total_ht= (($this->type==self::TYPE_CREDIT_NOTE||$qty<0)?-abs($total_ht):$total_ht); // For credit note and if qty is negative, total is negative
$this->line->total_tva= $total_tva;
$this->line->total_localtax1=$total_localtax1;
$this->line->total_localtax2=$total_localtax2;
$this->line->localtax1_type = $localtaxes_type[0];
$this->line->localtax2_type = $localtaxes_type[2];
$this->line->total_ttc= (($this->type==self::TYPE_CREDIT_NOTE||$qty<0)?-abs($total_ttc):$total_ttc);
$this->line->special_code=$this->special_code;
$this->line->fk_parent_line=$this->fk_parent_line;
$this->line->origin=$this->origin;
$this->line->origin_id=$origin_id;
$this->line->fk_unit=$fk_unit;
$resql = $this->db->query($sql); // Multicurrency
if ($resql) $this->line->fk_multicurrency = $this->fk_multicurrency;
{ $this->line->multicurrency_code = $this->multicurrency_code;
$idligne = $this->db->last_insert_id(MAIN_DB_PREFIX.'facture_fourn_det'); $this->line->multicurrency_subprice = price2num($this->line->subprice * $this->multicurrency_tx);
$this->line->multicurrency_total_ht = $multicurrency_total_ht;
$this->line->multicurrency_total_tva = $multicurrency_total_tva;
$this->line->multicurrency_total_ttc = $multicurrency_total_ttc;
$result=$this->updateline($idligne, $desc, $pu, $txtva, $txlocaltax1, $txlocaltax2, $qty, $fk_product, $price_base_type, $info_bits, $type, $remise_percent, true, '', '', $array_options, $fk_unit); if (is_array($array_options) && count($array_options)>0) {
$this->line->array_options=$array_options;
}
$result=$this->line->insert($notrigger);
if ($result > 0) if ($result > 0)
{ {
$this->rowid = $idligne; // Reorder if child line
if (! empty($fk_parent_line)) $this->line_order(true,'DESC');
if (! $notrigger) // Mise a jour informations denormalisees au niveau de la facture meme
$result=$this->update_price(1,'auto',0,$mysoc); // The addline method is designed to add line from user input so total calculation with update_price must be done using 'auto' mode.
if ($result > 0)
{ {
global $conf, $langs, $user;
// Call trigger
$result=$this->call_trigger('LINEBILL_SUPPLIER_CREATE',$user);
if ($result < 0)
{
$this->db->rollback();
return -1;
}
// End call triggers
}
$this->db->commit(); $this->db->commit();
return 1; return $this->line->id;
} }
else else
{ {
dol_syslog("Error after updateline error=".$this->error, LOG_ERR); $this->error=$this->db->error();
$this->db->rollback(); $this->db->rollback();
return -1; return -1;
} }
} }
else else
{ {
$this->error=$this->db->lasterror(); $this->error=$this->line->error;
$this->db->rollback(); $this->db->rollback();
return -2; return -2;
} }
@ -1293,7 +1337,7 @@ class FactureFournisseur extends CommonInvoice
* @param string $fk_unit Code of the unit to use. Null to use the default one * @param string $fk_unit Code of the unit to use. Null to use the default one
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
function updateline($id, $desc, $pu, $vatrate, $txlocaltax1=0, $txlocaltax2=0, $qty=1, $idproduct=0, $price_base_type='HT', $info_bits=0, $type=0, $remise_percent=0, $notrigger=false, $date_start='', $date_end='', $array_options=0, $fk_unit = null) public function updateline($id, $desc, $pu, $vatrate, $txlocaltax1=0, $txlocaltax2=0, $qty=1, $idproduct=0, $price_base_type='HT', $info_bits=0, $type=0, $remise_percent=0, $notrigger=false, $date_start='', $date_end='', $array_options=0, $fk_unit = null)
{ {
global $mysoc; global $mysoc;
dol_syslog(get_class($this)."::updateline $id,$desc,$pu,$vatrate,$qty,$idproduct,$price_base_type,$info_bits,$type,$remise_percent,$fk_unit", LOG_DEBUG); dol_syslog(get_class($this)."::updateline $id,$desc,$pu,$vatrate,$qty,$idproduct,$price_base_type,$info_bits,$type,$remise_percent,$fk_unit", LOG_DEBUG);
@ -1315,6 +1359,8 @@ class FactureFournisseur extends CommonInvoice
$txlocaltax1=price2num($txlocaltax1); $txlocaltax1=price2num($txlocaltax1);
$txlocaltax2=price2num($txlocaltax2); $txlocaltax2=price2num($txlocaltax2);
$localtaxes_type = array($txlocaltax1,$txlocaltax2);
// Calcul du total TTC et de la TVA pour la ligne a partir de // Calcul du total TTC et de la TVA pour la ligne a partir de
// qty, pu, remise_percent et txtva // qty, pu, remise_percent et txtva
// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
@ -1404,7 +1450,7 @@ class FactureFournisseur extends CommonInvoice
* @param int $notrigger 1=Does not execute triggers, 0= execute triggers * @param int $notrigger 1=Does not execute triggers, 0= execute triggers
* @return int <0 if KO, >0 if OK * @return int <0 if KO, >0 if OK
*/ */
function deleteline($rowid, $notrigger=0) public function deleteline($rowid, $notrigger=0)
{ {
if (!$rowid) { if (!$rowid) {
$rowid = $this->id; $rowid = $this->id;
@ -1434,7 +1480,7 @@ class FactureFournisseur extends CommonInvoice
* @param int $id Id de la facture a charger * @param int $id Id de la facture a charger
* @return void * @return void
*/ */
function info($id) public function info($id)
{ {
$sql = 'SELECT c.rowid, datec, tms as datem, '; $sql = 'SELECT c.rowid, datec, tms as datem, ';
$sql.= ' fk_user_author, fk_user_modif, fk_user_valid'; $sql.= ' fk_user_author, fk_user_modif, fk_user_valid';
@ -1543,9 +1589,9 @@ class FactureFournisseur extends CommonInvoice
* @param int $max Max length of shown ref * @param int $max Max length of shown ref
* @return string Chaine avec URL * @return string Chaine avec URL
*/ */
function getNomUrl($withpicto=0,$option='',$max=0) public function getNomUrl($withpicto=0,$option='',$max=0)
{ {
global $langs; global $langs, $conf;
$result=''; $result='';
$label = '<u>' . $langs->trans("ShowSupplierInvoice") . '</u>'; $label = '<u>' . $langs->trans("ShowSupplierInvoice") . '</u>';
@ -1587,7 +1633,7 @@ class FactureFournisseur extends CommonInvoice
* @param string $mode 'next' for next value or 'last' for last value * @param string $mode 'next' for next value or 'last' for last value
* @return string free ref or last ref * @return string free ref or last ref
*/ */
function getNextNumRef($soc,$mode='next') public function getNextNumRef($soc,$mode='next')
{ {
global $db, $langs, $conf; global $db, $langs, $conf;
$langs->load("orders"); $langs->load("orders");
@ -1640,7 +1686,7 @@ class FactureFournisseur extends CommonInvoice
* *
* @return void * @return void
*/ */
function initAsSpecimen() public function initAsSpecimen()
{ {
global $langs,$conf; global $langs,$conf;
include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php'; include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
@ -1780,7 +1826,7 @@ class FactureFournisseur extends CommonInvoice
* @param int $invertdetail Reverse sign of amounts for lines * @param int $invertdetail Reverse sign of amounts for lines
* @return int New id of clone * @return int New id of clone
*/ */
function createFromClone($fromid,$invertdetail=0) public function createFromClone($fromid,$invertdetail=0)
{ {
global $user,$langs; global $user,$langs;
@ -1942,7 +1988,7 @@ class SupplierInvoiceLine extends CommonObjectLine
public $element='facture_fourn_det'; public $element='facture_fourn_det';
public $table_element='facture_fourn_det'; public $table_element='facture_fourn_det';
var $oldline; public $oldline;
/** /**
* @deprecated * @deprecated
@ -1999,22 +2045,22 @@ class SupplierInvoiceLine extends CommonObjectLine
* Id of the corresponding supplier invoice * Id of the corresponding supplier invoice
* @var int * @var int
*/ */
var $fk_facture_fourn; public $fk_facture_fourn;
/** /**
* Product label * Product label
* This field may contains label of product (when invoice create from order) * This field may contains label of product (when invoice create from order)
* @var string * @var string
*/ */
var $label; public $label;
/** /**
* Description of the line * Description of the line
* @var string * @var string
*/ */
var $description; public $description;
var $skip_update_total; // Skip update price total for special lines public $skip_update_total; // Skip update price total for special lines
/** /**
* @var int Situation advance percentage * @var int Situation advance percentage
@ -2046,19 +2092,19 @@ class SupplierInvoiceLine extends CommonObjectLine
public $localtax2_type; public $localtax2_type;
// Multicurrency // Multicurrency
var $fk_multicurrency; public $fk_multicurrency;
var $multicurrency_code; public $multicurrency_code;
var $multicurrency_subprice; public $multicurrency_subprice;
var $multicurrency_total_ht; public $multicurrency_total_ht;
var $multicurrency_total_tva; public $multicurrency_total_tva;
var $multicurrency_total_ttc; public $multicurrency_total_ttc;
/** /**
* Constructor * Constructor
* *
* @param DoliDB $db Database handler * @param DoliDB $db Database handler
*/ */
function __construct($db) public function __construct($db)
{ {
$this->db= $db; $this->db= $db;
} }
@ -2292,5 +2338,146 @@ class SupplierInvoiceLine extends CommonObjectLine
$this->db->commit(); $this->db->commit();
return 1; return 1;
} }
/**
* Insert line into database
*
* @param int $notrigger 1 no triggers
* @return int <0 if KO, >0 if OK
*/
public function insert($notrigger=0)
{
global $user,$conf;
$error=0;
dol_syslog(get_class($this)."::insert rang=".$this->rang, LOG_DEBUG);
// Clean parameters
$this->desc=trim($this->desc);
if (empty($this->tva_tx)) $this->tva_tx=0;
if (empty($this->localtax1_tx)) $this->localtax1_tx=0;
if (empty($this->localtax2_tx)) $this->localtax2_tx=0;
if (empty($this->localtax1_type)) $this->localtax1_type=0;
if (empty($this->localtax2_type)) $this->localtax2_type=0;
if (empty($this->total_localtax1)) $this->total_localtax1=0;
if (empty($this->total_localtax2)) $this->total_localtax2=0;
if (empty($this->rang)) $this->rang=0;
if (empty($this->remise_percent)) $this->remise_percent=0;
if (empty($this->info_bits)) $this->info_bits=0;
if (empty($this->subprice)) $this->subprice=0;
if (empty($this->special_code)) $this->special_code=0;
if (empty($this->fk_parent_line)) $this->fk_parent_line=0;
if (! isset($this->situation_percent) || $this->situation_percent > 100 || (string) $this->situation_percent == '') $this->situation_percent = 100;
if (empty($this->pa_ht)) $this->pa_ht=0;
if (empty($this->multicurrency_subprice)) $this->multicurrency_subprice=0;
if (empty($this->multicurrency_total_ht)) $this->multicurrency_total_ht=0;
if (empty($this->multicurrency_total_tva)) $this->multicurrency_total_tva=0;
if (empty($this->multicurrency_total_ttc)) $this->multicurrency_total_ttc=0;
// Check parameters
if ($this->product_type < 0)
{
$this->error='ErrorProductTypeMustBe0orMore';
return -1;
}
if (! empty($this->fk_product))
{
// Check product exists
$result=Product::isExistingObject('product', $this->fk_product);
if ($result <= 0)
{
$this->error='ErrorProductIdDoesNotExists';
return -1;
}
}
$this->db->begin();
// Insertion dans base de la ligne
$sql = 'INSERT INTO '.MAIN_DB_PREFIX.$this->table_element;
$sql.= ' (fk_facture_fourn, fk_parent_line, label, description, qty,';
$sql.= ' tva_tx, localtax1_tx, localtax2_tx, localtax1_type, localtax2_type,';
$sql.= ' fk_product, product_type, remise_percent, pu_ht, pu_ttc,';
$sql.= ' date_start, date_end, fk_code_ventilation, rang, special_code,';
$sql.= ' info_bits, total_ht, tva, total_ttc, total_localtax1, total_localtax2, fk_unit';
$sql.= ', fk_multicurrency, multicurrency_code, multicurrency_subprice, multicurrency_total_ht, multicurrency_total_tva, multicurrency_total_ttc';
$sql.= ')';
$sql.= " VALUES (".$this->fk_facture_fourn.",";
$sql.= " ".($this->fk_parent_line>0?"'".$this->fk_parent_line."'":"null").",";
$sql.= " ".(! empty($this->label)?"'".$this->db->escape($this->label)."'":"null").",";
$sql.= " '".$this->db->escape($this->desc)."',";
$sql.= " ".price2num($this->qty).",";
$sql.= " ".price2num($this->tva_tx).",";
$sql.= " ".price2num($this->localtax1_tx).",";
$sql.= " ".price2num($this->localtax2_tx).",";
$sql.= " '".$this->localtax1_type."',";
$sql.= " '".$this->localtax2_type."',";
$sql.= ' '.(! empty($this->fk_product)?$this->fk_product:"null").',';
$sql.= " ".$this->product_type.",";
$sql.= " ".price2num($this->remise_percent).",";
$sql.= " ".price2num($this->subprice).",";
$sql.= " ".price2num($this->total_ttc/$this->qty).",";
$sql.= " ".(! empty($this->date_start)?"'".$this->db->idate($this->date_start)."'":"null").",";
$sql.= " ".(! empty($this->date_end)?"'".$this->db->idate($this->date_end)."'":"null").",";
$sql.= ' '.(!empty($this->fk_code_ventilation)?$this->fk_code_ventilation:0).',';
$sql.= ' '.$this->rang.',';
$sql.= ' '.$this->special_code.',';
$sql.= " '".$this->info_bits."',";
$sql.= " ".price2num($this->total_ht).",";
$sql.= " ".price2num($this->total_tva).",";
$sql.= " ".price2num($this->total_ttc).",";
$sql.= " ".price2num($this->total_localtax1).",";
$sql.= " ".price2num($this->total_localtax2);
$sql .= ", ".(!$this->fk_unit ? 'NULL' : $this->fk_unit);
$sql.= ", ".(int) $this->fk_multicurrency;
$sql.= ", '".$this->db->escape($this->multicurrency_code)."'";
$sql.= ", ".price2num($this->multicurrency_subprice);
$sql.= ", ".price2num($this->multicurrency_total_ht);
$sql.= ", ".price2num($this->multicurrency_total_tva);
$sql.= ", ".price2num($this->multicurrency_total_ttc);
$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.$this->table_element);
$this->rowid=$this->id;
if (empty($conf->global->MAIN_EXTRAFIELDS_DISABLED)) // For avoid conflicts if trigger used
{
$result=$this->insertExtraFields();
if ($result < 0)
{
$error++;
}
}
if (! $notrigger)
{
// Call trigger
$result=$this->call_trigger('LINEBILL_SUPPLIER_CREATE',$user);
if ($result < 0)
{
$this->db->rollback();
return -2;
}
// End call triggers
}
$this->db->commit();
return $this->id;
}
else
{
$this->error=$this->db->error();
$this->db->rollback();
return -2;
}
}
} }

View File

@ -476,7 +476,8 @@ if (empty($reshook))
$lines[$i]->rang, $lines[$i]->rang,
0, 0,
$lines[$i]->array_options, $lines[$i]->array_options,
$lines[$i]->fk_unit $lines[$i]->fk_unit,
$lines[$i]->id
); );
if ($result < 0) if ($result < 0)