36 |
|
|
37 |
class FactureFournisseur |
class FactureFournisseur |
38 |
{ |
{ |
39 |
var $id; |
var $id; |
40 |
var $db; |
var $db; |
41 |
var $socid; |
var $socid; |
42 |
var $number; |
var $number; |
43 |
var $statut; |
var $statut; |
44 |
var $paye; |
var $paye; |
45 |
var $author; |
var $author; |
46 |
var $libelle; |
var $libelle; |
47 |
var $date; |
var $date; |
48 |
var $date_echeance; |
var $date_echeance; |
49 |
var $ref; |
var $ref; |
50 |
var $amount; |
var $amount; |
51 |
var $remise; |
var $remise; |
52 |
var $tva; |
var $tva; |
53 |
var $total_ht; |
var $total_ht; |
54 |
var $total_tva; |
var $total_tva; |
55 |
var $total_ttc; |
var $total_ttc; |
56 |
var $note; |
var $note; |
57 |
var $db_table; |
var $db_table; |
58 |
var $propalid; |
var $propalid; |
59 |
var $lignes; |
var $lignes; |
60 |
|
|
61 |
/** |
/** |
62 |
* \brief Constructeur de la classe |
* \brief Constructeur de la classe |
63 |
* \param DB handler accès base de données |
* \param DB handler accès base de données |
64 |
* \param soc_idp id societe ("" par defaut) |
* \param soc_idp id societe ("" par defaut) |
65 |
* \param facid id facture ("" par defaut) |
* \param facid id facture ("" par defaut) |
66 |
*/ |
*/ |
67 |
function FactureFournisseur($DB, $soc_idp="", $facid="") |
function FactureFournisseur($DB, $soc_idp="", $facid="") |
68 |
{ |
{ |
69 |
$this->db = $DB ; |
$this->db = $DB ; |
70 |
$this->socidp = $soc_idp; |
$this->socidp = $soc_idp; |
71 |
$this->products = array(); |
$this->products = array(); |
72 |
$this->db_table = MAIN_DB_PREFIX."facture"; |
$this->db_table = MAIN_DB_PREFIX."facture"; |
73 |
$this->amount = 0; |
$this->amount = 0; |
74 |
$this->remise = 0; |
$this->remise = 0; |
75 |
$this->tva = 0; |
$this->tva = 0; |
76 |
$this->total = 0; |
$this->total = 0; |
77 |
$this->propalid = 0; |
$this->propalid = 0; |
78 |
$this->id = $facid; |
$this->id = $facid; |
79 |
|
|
80 |
$this->lignes = array(); |
$this->lignes = array(); |
81 |
} |
} |
82 |
|
|
83 |
/** |
/** |
84 |
* \brief Création de la facture en base |
* \brief Création de la facture en base |
85 |
* \param user object utilisateur qui crée |
* \param user object utilisateur qui crée |
86 |
* \return int id facture si ok, < 0 si erreur |
* \return int id facture si ok, < 0 si erreur |
87 |
*/ |
*/ |
88 |
function create($user) |
function create($user) |
89 |
{ |
{ |
90 |
global $langs; |
global $langs; |
91 |
|
|
92 |
|
// Nettoyage parametres |
93 |
$socid = $this->socidp; |
$socid = $this->socidp; |
94 |
$number = sanitize_string(strtoupper($this->number)); |
$number = strtoupper($this->number); |
95 |
$amount = $this->amount; |
$amount = $this->amount; |
96 |
$remise = $this->remise; |
$remise = $this->remise; |
97 |
|
|
98 |
$this->db->begin(); |
$this->db->begin(); |
99 |
|
|
100 |
if (! $remise) $remise = 0 ; |
if (! $remise) $remise = 0 ; |
101 |
$totalht = ($amount - $remise); |
$totalht = ($amount - $remise); |
|
// NE ME SEMBLE PLUS JUSTIFIE ICI |
|
|
// $tva = tva($totalht); |
|
|
// $total = $totalht + $tva; |
|
102 |
|
|
103 |
$sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_fourn (facnumber, libelle, fk_soc, datec, datef, note, fk_user_author, date_lim_reglement) "; |
$sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_fourn (facnumber, libelle, fk_soc, datec, datef, note, fk_user_author, date_lim_reglement) "; |
104 |
$sql .= " VALUES ('".$number."','".addslashes($this->libelle)."',"; |
$sql .= " VALUES ('".addslashes($number)."','".addslashes($this->libelle)."',"; |
105 |
$sql .= $this->socid.", now(),'".$this->db->idate($this->date)."','".addslashes($this->note)."', ".$user->id.",'".$this->db->idate($this->date_echeance)."');"; |
$sql .= $this->socid.", now(),'".$this->db->idate($this->date)."','".addslashes($this->note)."', ".$user->id.",'".$this->db->idate($this->date_echeance)."');"; |
106 |
|
|
107 |
$resql=$this->db->query($sql); |
$resql=$this->db->query($sql); |
108 |
|
|
109 |
if ($resql) |
if ($resql) |
110 |
{ |
{ |
111 |
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_fourn"); |
$this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_fourn"); |
112 |
|
|
113 |
for ($i = 0 ; $i < sizeof($this->lignes) ; $i++) |
for ($i = 0 ; $i < sizeof($this->lignes) ; $i++) |
114 |
{ |
{ |
115 |
|
|
116 |
$sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_fourn_det (fk_facture_fourn)"; |
$sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_fourn_det (fk_facture_fourn)"; |
117 |
$sql .= " VALUES ($this->id);"; |
$sql .= " VALUES ($this->id);"; |
118 |
if ($this->db->query($sql)) |
if ($this->db->query($sql)) |
119 |
{ |
{ |
120 |
$idligne = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_fourn_det"); |
$idligne = $this->db->last_insert_id(MAIN_DB_PREFIX."facture_fourn_det"); |
121 |
|
|
122 |
$this->updateline($idligne, |
$this->updateline($idligne, |
123 |
$this->lignes[$i][0], |
$this->lignes[$i][0], |
124 |
$this->lignes[$i][1], |
$this->lignes[$i][1], |
125 |
$this->lignes[$i][2], |
$this->lignes[$i][2], |
126 |
$this->lignes[$i][3]); |
$this->lignes[$i][3]); |
127 |
} |
} |
128 |
} |
} |
129 |
|
|
130 |
// Mise à jour prix |
// Mise à jour prix |
131 |
if ($this->updateprice($this->id) > 0) |
if ($this->updateprice($this->id) > 0) |
132 |
|
{ |
133 |
|
$this->db->commit(); |
134 |
|
return $this->id; |
135 |
|
} |
136 |
|
else |
137 |
{ |
{ |
138 |
$this->db->commit(); |
$this->error=$langs->trans("FailedToUpdatePrice"); |
139 |
return $this->id; |
$this->db->rollback(); |
140 |
|
return -3; |
141 |
} |
} |
|
else |
|
|
{ |
|
|
$this->error=$langs->trans("FailedToUpdatePrice"); |
|
|
$this->db->rollback(); |
|
|
return -3; |
|
|
} |
|
142 |
} |
} |
143 |
else |
else |
144 |
{ |
{ |
147 |
$this->error=$langs->trans("ErrorBillRefAlreadyExists"); |
$this->error=$langs->trans("ErrorBillRefAlreadyExists"); |
148 |
$this->db->rollback(); |
$this->db->rollback(); |
149 |
return -1; |
return -1; |
150 |
} |
} |
151 |
else |
else |
152 |
{ |
{ |
153 |
$this->error=$this->db->error(); |
$this->error=$this->db->error(); |
154 |
$this->db->rollback(); |
$this->db->rollback(); |
155 |
return -2; |
return -2; |
156 |
} |
} |
157 |
} |
} |
158 |
} |
} |
159 |
|
|
160 |
/** |
/** |