prepare contract class for margin management
This commit is contained in:
parent
b372a8c5ba
commit
44134dec57
@ -5,6 +5,7 @@
|
||||
* Copyright (C) 2006 Andre Cianfarani <acianfa@free.fr>
|
||||
* Copyright (C) 2008 Raphael Bertrand <raphael.bertrand@resultic.fr>
|
||||
* Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
|
||||
* Copyright (C) 2013 Christophe Battarel <christophe.battarel@altairis.fr>
|
||||
*
|
||||
* 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
|
||||
@ -27,7 +28,7 @@
|
||||
*/
|
||||
|
||||
require_once DOL_DOCUMENT_ROOT.'/core/class/commonobject.class.php';
|
||||
|
||||
require_once(DOL_DOCUMENT_ROOT ."/margin/lib/margins.lib.php");
|
||||
|
||||
/**
|
||||
* Class to manage contracts
|
||||
@ -402,7 +403,7 @@ class Contrat extends CommonObject
|
||||
|
||||
// Selectionne les lignes contrats liees a un produit
|
||||
$sql = "SELECT p.label, p.description as product_desc, p.ref,";
|
||||
$sql.= " d.rowid, d.fk_contrat, d.statut, d.description, d.price_ht, d.tva_tx, d.localtax1_tx, d.localtax2_tx, d.qty, d.remise_percent, d.subprice,";
|
||||
$sql.= " d.rowid, d.fk_contrat, d.statut, d.description, d.price_ht, d.tva_tx, d.localtax1_tx, d.localtax2_tx, d.qty, d.remise_percent, d.subprice, d.fk_product_fournisseur_price as fk_fournprice, d.buy_price_ht as pa_ht,";
|
||||
$sql.= " d.total_ht,";
|
||||
$sql.= " d.total_tva,";
|
||||
$sql.= " d.total_localtax1,";
|
||||
@ -451,6 +452,10 @@ class Contrat extends CommonObject
|
||||
$line->fk_product = $objp->fk_product;
|
||||
$line->info_bits = $objp->info_bits;
|
||||
|
||||
$line->fk_fournprice = $objp->fk_fournprice;
|
||||
$marginInfos = getMarginInfos($objp->subprice, $objp->remise_percent, $objp->tva_tx, $objp->localtax1_tx, $objp->localtax2_tx, $line->fk_fournprice, $objp->pa_ht);
|
||||
$line->pa_ht = $marginInfos[0];
|
||||
|
||||
$line->fk_user_author = $objp->fk_user_author;
|
||||
$line->fk_user_ouverture= $objp->fk_user_ouverture;
|
||||
$line->fk_user_cloture = $objp->fk_user_cloture;
|
||||
@ -856,7 +861,7 @@ class Contrat extends CommonObject
|
||||
* @param int $info_bits Bits de type de lignes
|
||||
* @return int <0 si erreur, >0 si ok
|
||||
*/
|
||||
function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $remise_percent, $date_start, $date_end, $price_base_type='HT', $pu_ttc=0, $info_bits=0)
|
||||
function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2, $fk_product, $remise_percent, $date_start, $date_end, $price_base_type='HT', $pu_ttc=0, $info_bits=0, $fk_fournprice=null, $pa_ht = 0)
|
||||
{
|
||||
global $user, $langs, $conf;
|
||||
|
||||
@ -875,6 +880,7 @@ class Contrat extends CommonObject
|
||||
if (! $pu_ttc) $pu_ttc=0;
|
||||
$pu_ht=price2num($pu_ht);
|
||||
$pu_ttc=price2num($pu_ttc);
|
||||
$pa_ht=price2num($pa_ht);
|
||||
$txtva=price2num($txtva);
|
||||
$txlocaltax1=price2num($txlocaltax1);
|
||||
$txlocaltax2=price2num($txlocaltax2);
|
||||
@ -908,13 +914,21 @@ class Contrat extends CommonObject
|
||||
$price = $pu_ht - $remise;
|
||||
}
|
||||
|
||||
if (empty($pa_ht)) $pa_ht=0;
|
||||
|
||||
// si prix d'achat non renseigne et utilise pour calcul des marges alors prix achat = prix vente
|
||||
if ($pa_ht == 0) {
|
||||
if ($pu_ht > 0 && (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1))
|
||||
$pa_ht = $pu_ht * (1 - $remise_percent / 100);
|
||||
}
|
||||
|
||||
// Insertion dans la base
|
||||
$sql = "INSERT INTO ".MAIN_DB_PREFIX."contratdet";
|
||||
$sql.= " (fk_contrat, label, description, fk_product, qty, tva_tx,";
|
||||
$sql.= " localtax1_tx, localtax2_tx, remise_percent, subprice,";
|
||||
$sql.= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc,";
|
||||
$sql.= " info_bits,";
|
||||
$sql.= " price_ht, remise"; // TODO A virer
|
||||
$sql.= " price_ht, remise, fk_product_fournisseur_price, buy_price_ht";
|
||||
if ($date_start > 0) { $sql.= ",date_ouverture_prevue"; }
|
||||
if ($date_end > 0) { $sql.= ",date_fin_validite"; }
|
||||
$sql.= ") VALUES ($this->id, '', '" . $this->db->escape($desc) . "',";
|
||||
@ -926,7 +940,11 @@ class Contrat extends CommonObject
|
||||
$sql.= " ".price2num($remise_percent).",".price2num($pu_ht).",";
|
||||
$sql.= " ".price2num($total_ht).",".price2num($total_tva).",".price2num($total_localtax1).",".price2num($total_localtax2).",".price2num($total_ttc).",";
|
||||
$sql.= " '".$info_bits."',";
|
||||
$sql.= " ".price2num($price).",".price2num($remise); // TODO A virer
|
||||
$sql.= " ".price2num($price).",".price2num($remise).",";
|
||||
if (isset($fk_fournprice)) $sql.= ' '.$fk_fournprice.',';
|
||||
else $sql.= ' null,';
|
||||
if (isset($pa_ht)) $sql.= ' '.price2num($pa_ht);
|
||||
else $sql.= ' null';
|
||||
if ($date_start > 0) { $sql.= ",'".$this->db->idate($date_start)."'"; }
|
||||
if ($date_end > 0) { $sql.= ",'".$this->db->idate($date_end)."'"; }
|
||||
$sql.= ")";
|
||||
@ -983,7 +1001,7 @@ class Contrat extends CommonObject
|
||||
* @param int $info_bits Bits de type de lignes
|
||||
* @return int < 0 si erreur, > 0 si ok
|
||||
*/
|
||||
function updateline($rowid, $desc, $pu, $qty, $remise_percent, $date_start, $date_end, $tvatx, $localtax1tx=0, $localtax2tx=0, $date_debut_reel='', $date_fin_reel='', $price_base_type='HT', $info_bits=0)
|
||||
function updateline($rowid, $desc, $pu, $qty, $remise_percent, $date_start, $date_end, $tvatx, $localtax1tx=0, $localtax2tx=0, $date_debut_reel='', $date_fin_reel='', $price_base_type='HT', $info_bits=0, $fk_fournprice=null, $pa_ht = 0)
|
||||
{
|
||||
global $user, $conf, $langs;
|
||||
|
||||
@ -995,6 +1013,7 @@ class Contrat extends CommonObject
|
||||
$tvatx = price2num($tvatx);
|
||||
$localtax1tx = price2num($localtax1tx);
|
||||
$localtax2tx = price2num($localtax2tx);
|
||||
$pa_ht=price2num($pa_ht);
|
||||
|
||||
$subprice = $price;
|
||||
$remise = 0;
|
||||
@ -1016,7 +1035,7 @@ class Contrat extends CommonObject
|
||||
// qty, pu, remise_percent et txtva
|
||||
// TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
|
||||
// la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
|
||||
$tabprice=calcul_price_total($qty, $pu, $remise_percent, $txtva, $localtaxtx1, $txlocaltaxtx2, 0, $price_base_type, $info_bits, 1);
|
||||
$tabprice=calcul_price_total($qty, $pu, $remise_percent, $tvatx, $localtaxtx1, $txlocaltaxtx2, 0, $price_base_type, $info_bits, 1);
|
||||
$total_ht = $tabprice[0];
|
||||
$total_tva = $tabprice[1];
|
||||
$total_ttc = $tabprice[2];
|
||||
@ -1032,6 +1051,14 @@ class Contrat extends CommonObject
|
||||
$price = $pu_ht - $remise;
|
||||
}
|
||||
|
||||
if (empty($pa_ht)) $pa_ht=0;
|
||||
|
||||
// si prix d'achat non renseigne et utilise pour calcul des marges alors prix achat = prix vente
|
||||
if ($pa_ht == 0) {
|
||||
if ($pu_ht > 0 && (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1))
|
||||
$pa_ht = $pu_ht * (1 - $remise_percent / 100);
|
||||
}
|
||||
|
||||
$sql = "UPDATE ".MAIN_DB_PREFIX."contratdet set description='".$this->db->escape($desc)."'";
|
||||
$sql.= ",price_ht='" . price2num($price)."'";
|
||||
$sql.= ",subprice='" . price2num($subprice)."'";
|
||||
@ -1046,6 +1073,8 @@ class Contrat extends CommonObject
|
||||
$sql.= ", total_localtax1='".price2num($total_localtax1)."'";
|
||||
$sql.= ", total_localtax2='".price2num($total_localtax2)."'";
|
||||
$sql.= ", total_ttc='". price2num($total_ttc)."'";
|
||||
$sql.= ", fk_product_fournisseur_price='".$fk_fournprice."'";
|
||||
$sql.= ", buy_price_ht='".price2num($pa_ht)."'";
|
||||
if ($date_start > 0) { $sql.= ",date_ouverture_prevue='".$this->db->idate($date_start)."'"; }
|
||||
else { $sql.=",date_ouverture_prevue=null"; }
|
||||
if ($date_end > 0) { $sql.= ",date_fin_validite='".$this->db->idate($date_end)."'"; }
|
||||
@ -1610,6 +1639,9 @@ class ContratLigne
|
||||
var $total_localtax2;
|
||||
var $total_ttc;
|
||||
|
||||
var $fk_fournprice;
|
||||
var $pa_ht;
|
||||
|
||||
var $info_bits;
|
||||
var $fk_user_author;
|
||||
var $fk_user_ouverture;
|
||||
@ -1769,6 +1801,8 @@ class ContratLigne
|
||||
$sql.= " t.total_localtax1,";
|
||||
$sql.= " t.total_localtax2,";
|
||||
$sql.= " t.total_ttc,";
|
||||
$sql.= " t.fk_product_fournisseur_price as fk_fournprice,";
|
||||
$sql.= " t.buy_price_ht as pa_ht,";
|
||||
$sql.= " t.info_bits,";
|
||||
$sql.= " t.fk_user_author,";
|
||||
$sql.= " t.fk_user_ouverture,";
|
||||
@ -1819,6 +1853,9 @@ class ContratLigne
|
||||
$this->fk_user_ouverture = $obj->fk_user_ouverture;
|
||||
$this->fk_user_cloture = $obj->fk_user_cloture;
|
||||
$this->commentaire = $obj->commentaire;
|
||||
$this->fk_fournprice = $obj->fk_fournprice;
|
||||
$marginInfos = getMarginInfos($obj->subprice, $obj->remise_percent, $obj->tva_tx, $obj->localtax1_tx, $obj->localtax2_tx, $this->fk_fournprice, $obj->pa_ht);
|
||||
$this->pa_ht = $marginInfos[0];
|
||||
|
||||
}
|
||||
$this->db->free($resql);
|
||||
@ -1887,6 +1924,14 @@ class ContratLigne
|
||||
$this->total_localtax1= $tabprice[9];
|
||||
$this->total_localtax2= $tabprice[10];
|
||||
|
||||
if (empty($this->pa_ht)) $this->pa_ht=0;
|
||||
|
||||
// si prix d'achat non renseigné et utilisé pour calcul des marges alors prix achat = prix vente
|
||||
if ($this->pa_ht == 0) {
|
||||
if ($this->subprice > 0 && (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1))
|
||||
$this->pa_ht = $this->subprice * (1 - $this->remise_percent / 100);
|
||||
}
|
||||
|
||||
// Update request
|
||||
$sql = "UPDATE ".MAIN_DB_PREFIX."contratdet SET";
|
||||
$sql.= " fk_contrat='".$this->fk_contrat."',";
|
||||
@ -1913,6 +1958,8 @@ class ContratLigne
|
||||
$sql.= " total_localtax1='".$this->total_localtax1."',";
|
||||
$sql.= " total_localtax2='".$this->total_localtax2."',";
|
||||
$sql.= " total_ttc='".$this->total_ttc."',";
|
||||
$sql.= " fk_product_fournisseur_price='".$this->fk_fournprice."',";
|
||||
$sql.= " buy_price_ht='".price2num($this->pa_ht)."',";
|
||||
$sql.= " info_bits='".$this->info_bits."',";
|
||||
$sql.= " fk_user_author=".($this->fk_user_author >= 0?$this->fk_user_author:"NULL").",";
|
||||
$sql.= " fk_user_ouverture=".($this->fk_user_ouverture > 0?$this->fk_user_ouverture:"NULL").",";
|
||||
|
||||
Loading…
Reference in New Issue
Block a user