PageRenderTime 61ms CodeModel.GetById 22ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/fourn/class/fournisseur.commande.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 2017 lines | 1451 code | 224 blank | 342 comment | 223 complexity | 91f6f79d63e90e9d9c21f7224968c596 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT

Large files files are truncated, but you can click here to view the full file

  1. <?php
  2. /* Copyright (C) 2003-2006 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
  5. * Copyright (C) 2007 Franky Van Liedekerke <franky.van.liedekerke@telenet.be>
  6. * Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
  7. * Copyright (C) 2010-2012 Philippe Grand <philippe.grand@atoo-net.com>
  8. * Copyright (C) 2012 Marcos García <marcosgdf@gmail.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. /**
  24. * \file htdocs/fourn/class/fournisseur.commande.class.php
  25. * \ingroup fournisseur,commande
  26. * \brief File of class to manage suppliers orders
  27. */
  28. include_once DOL_DOCUMENT_ROOT.'/core/class/commonorder.class.php';
  29. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  30. /**
  31. * \class CommandeFournisseur
  32. * \brief Class to manage predefined suppliers products
  33. */
  34. class CommandeFournisseur extends CommonOrder
  35. {
  36. public $element='order_supplier';
  37. public $table_element='commande_fournisseur';
  38. public $table_element_line = 'commande_fournisseurdet';
  39. public $fk_element = 'fk_commande';
  40. protected $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  41. var $ref; // TODO deprecated
  42. var $product_ref;
  43. var $ref_supplier;
  44. var $brouillon;
  45. var $statut; // 0=Draft -> 1=Validated -> 2=Approved -> 3=Process runing -> 4=Received partially -> 5=Received totally -> (reopen) 4=Received partially
  46. // -> 7=Canceled/Never received -> (reopen) 3=Process runing
  47. // -> 6=Canceled -> (reopen) 2=Approved
  48. // -> 9=Refused -> (reopen) 1=Validated
  49. var $socid;
  50. var $fourn_id;
  51. var $date;
  52. var $date_valid;
  53. var $date_approve;
  54. var $date_commande;
  55. var $date_livraison; // Date livraison souhaitee
  56. var $total_ht;
  57. var $total_tva;
  58. var $total_localtax1; // Total Local tax 1
  59. var $total_localtax2; // Total Local tax 2
  60. var $total_ttc;
  61. var $source;
  62. var $note;
  63. var $note_public;
  64. var $model_pdf;
  65. var $fk_project;
  66. var $cond_reglement_id;
  67. var $cond_reglement_code;
  68. var $mode_reglement_id;
  69. var $mode_reglement_code;
  70. var $user_author_id;
  71. var $user_valid_id;
  72. var $user_approve_id;
  73. var $extraparams=array();
  74. /**
  75. * Constructor
  76. *
  77. * @param DoliDB $db Database handler
  78. */
  79. function __construct($db = '')
  80. {
  81. $this->db = $db;
  82. $this->products = array();
  83. $this->lines = array();
  84. // List of language codes for status
  85. $this->statuts[0] = 'StatusOrderDraft';
  86. $this->statuts[1] = 'StatusOrderValidated';
  87. $this->statuts[2] = 'StatusOrderApproved';
  88. $this->statuts[3] = 'StatusOrderOnProcess';
  89. $this->statuts[4] = 'StatusOrderReceivedPartially';
  90. $this->statuts[5] = 'StatusOrderReceivedAll';
  91. $this->statuts[6] = 'StatusOrderCanceled';
  92. $this->statuts[7] = 'StatusOrderCanceled';
  93. $this->statuts[9] = 'StatusOrderRefused';
  94. }
  95. /**
  96. * Get object and lines from database
  97. *
  98. * @param int $id Id of order to load
  99. * @param string $ref Ref of object
  100. * @return int >0 if OK, <0 if KO, 0 if not found
  101. */
  102. function fetch($id,$ref='')
  103. {
  104. global $conf;
  105. // Check parameters
  106. if (empty($id) && empty($ref)) return -1;
  107. $sql = "SELECT c.rowid, c.ref, ref_supplier, c.fk_soc, c.fk_statut, c.amount_ht, c.total_ht, c.total_ttc, c.tva,";
  108. $sql.= " c.localtax1, c.localtax2, ";
  109. $sql.= " c.date_creation, c.date_valid, c.date_approve,";
  110. $sql.= " c.fk_user_author, c.fk_user_valid, c.fk_user_approve,";
  111. $sql.= " c.date_commande as date_commande, c.date_livraison as date_livraison, c.fk_cond_reglement, c.fk_mode_reglement, c.fk_projet as fk_project, c.remise_percent, c.source, c.fk_input_method,";
  112. $sql.= " c.note as note_private, c.note_public, c.model_pdf, c.extraparams,";
  113. $sql.= " cm.libelle as methode_commande,";
  114. $sql.= " cr.code as cond_reglement_code, cr.libelle as cond_reglement_libelle,";
  115. $sql.= " p.code as mode_reglement_code, p.libelle as mode_reglement_libelle";
  116. $sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseur as c";
  117. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_payment_term as cr ON (c.fk_cond_reglement = cr.rowid)";
  118. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_paiement as p ON (c.fk_mode_reglement = p.id)";
  119. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."c_input_method as cm ON cm.rowid = c.fk_input_method";
  120. $sql.= " WHERE c.entity = ".$conf->entity;
  121. if ($ref) $sql.= " AND c.ref='".$ref."'";
  122. else $sql.= " AND c.rowid=".$id;
  123. dol_syslog(get_class($this)."::fetch sql=".$sql,LOG_DEBUG);
  124. $resql = $this->db->query($sql);
  125. if ($resql)
  126. {
  127. $obj = $this->db->fetch_object($resql);
  128. if (! $obj)
  129. {
  130. $this->error='Bill with id '.$id.' not found sql='.$sql;
  131. dol_syslog(get_class($this).'::fetch '.$this->error);
  132. return 0;
  133. }
  134. $this->id = $obj->rowid;
  135. $this->ref = $obj->ref;
  136. $this->ref_supplier = $obj->ref_supplier;
  137. $this->socid = $obj->fk_soc;
  138. $this->fourn_id = $obj->fk_soc;
  139. $this->statut = $obj->fk_statut;
  140. $this->user_author_id = $obj->fk_user_author;
  141. $this->user_valid_id = $obj->fk_user_valid;
  142. $this->user_approve_id = $obj->fk_user_approve;
  143. $this->total_ht = $obj->total_ht;
  144. $this->total_tva = $obj->tva;
  145. $this->total_localtax1 = $obj->localtax1;
  146. $this->total_localtax2 = $obj->localtax2;
  147. $this->total_ttc = $obj->total_ttc;
  148. $this->date = $this->db->jdate($obj->date_creation);
  149. $this->date_valid = $this->db->jdate($obj->date_valid);
  150. $this->date_approve = $this->db->jdate($obj->date_approve);
  151. $this->date_commande = $this->db->jdate($obj->date_commande); // date a laquelle la commande a ete transmise
  152. $this->date_livraison = $this->db->jdate($obj->date_livraison);
  153. $this->remise_percent = $obj->remise_percent;
  154. $this->methode_commande_id = $obj->fk_input_method;
  155. $this->methode_commande = $obj->methode_commande;
  156. $this->source = $obj->source;
  157. //$this->facturee = $obj->facture;
  158. $this->fk_project = $obj->fk_project;
  159. $this->cond_reglement_id = $obj->fk_cond_reglement;
  160. $this->cond_reglement_code = $obj->cond_reglement_code;
  161. $this->cond_reglement = $obj->cond_reglement_libelle;
  162. $this->cond_reglement_doc = $obj->cond_reglement_libelle;
  163. $this->mode_reglement_id = $obj->fk_mode_reglement;
  164. $this->mode_reglement_code = $obj->mode_reglement_code;
  165. $this->mode_reglement = $obj->mode_reglement_libelle;
  166. $this->note = $obj->note_private; // deprecated
  167. $this->note_private = $obj->note_private;
  168. $this->note_public = $obj->note_public;
  169. $this->modelpdf = $obj->model_pdf;
  170. $this->extraparams = (array) json_decode($obj->extraparams, true);
  171. $this->db->free($resql);
  172. if ($this->statut == 0) $this->brouillon = 1;
  173. $sql = "SELECT l.rowid, l.ref as ref_supplier, l.fk_product, l.product_type, l.label, l.description,";
  174. $sql.= " l.qty,";
  175. $sql.= " l.tva_tx, l.remise_percent, l.subprice,";
  176. $sql.= " l.localtax1_tx, l. localtax2_tx, l.total_localtax1, l.total_localtax2,";
  177. $sql.= " l.total_ht, l.total_tva, l.total_ttc,";
  178. $sql.= " p.rowid as product_id, p.ref as product_ref, p.label as product_label, p.description as product_desc";
  179. $sql.= " FROM ".MAIN_DB_PREFIX."commande_fournisseurdet as l";
  180. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON l.fk_product = p.rowid';
  181. $sql.= " WHERE l.fk_commande = ".$this->id;
  182. $sql.= " ORDER BY l.rowid";
  183. //print $sql;
  184. dol_syslog(get_class($this)."::fetch get lines sql=".$sql,LOG_DEBUG);
  185. $result = $this->db->query($sql);
  186. if ($result)
  187. {
  188. $num = $this->db->num_rows($result);
  189. $i = 0;
  190. while ($i < $num)
  191. {
  192. $objp = $this->db->fetch_object($result);
  193. $line = new CommandeFournisseurLigne($this->db);
  194. $line->id = $objp->rowid;
  195. $line->desc = $objp->description; // Description ligne
  196. $line->description = $objp->description; // Description ligne
  197. $line->qty = $objp->qty;
  198. $line->tva_tx = $objp->tva_tx;
  199. $line->localtax1_tx = $objp->localtax1_tx;
  200. $line->localtax2_tx = $objp->localtax2_tx;
  201. $line->subprice = $objp->subprice;
  202. $line->remise_percent = $objp->remise_percent;
  203. $line->total_ht = $objp->total_ht;
  204. $line->total_tva = $objp->total_tva;
  205. $line->total_localtax1 = $objp->total_localtax1;
  206. $line->total_localtax2 = $objp->total_localtax2;
  207. $line->total_ttc = $objp->total_ttc;
  208. $line->product_type = $objp->product_type;
  209. $line->fk_product = $objp->fk_product; // Id du produit
  210. $line->libelle = $objp->product_label; // TODO deprecated
  211. $line->product_label = $objp->product_label; // Label produit
  212. $line->product_desc = $objp->product_desc; // Description produit
  213. $line->ref = $objp->product_ref; // TODO deprecated
  214. $line->product_ref = $objp->product_ref; // Internal reference
  215. $line->ref_fourn = $objp->ref_supplier; // TODO deprecated
  216. $line->ref_supplier = $objp->ref_supplier; // Reference supplier
  217. $this->lines[$i] = $line;
  218. $i++;
  219. }
  220. $this->db->free($result);
  221. return 1;
  222. }
  223. else
  224. {
  225. $this->error=$this->db->error()." sql=".$sql;
  226. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  227. return -1;
  228. }
  229. }
  230. else
  231. {
  232. $this->error=$this->db->error()." sql=".$sql;
  233. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  234. return -1;
  235. }
  236. }
  237. /**
  238. * Add a line in log table
  239. *
  240. * @param User $user User making action
  241. * @param int $statut Status of order
  242. * @param date $datelog Date of change
  243. * @param string $comment Comment
  244. * @return int <0 if KO, >0 if OK
  245. */
  246. function log($user, $statut, $datelog, $comment='')
  247. {
  248. $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseur_log (datelog, fk_commande, fk_statut, fk_user, comment)";
  249. $sql.= " VALUES ('".$this->db->idate($datelog)."',".$this->id.", ".$statut.", ";
  250. $sql.= $user->id.", ";
  251. $sql.= ($comment?"'".$this->db->escape($comment)."'":'null');
  252. $sql.= ")";
  253. dol_syslog("FournisseurCommande::log sql=".$sql, LOG_DEBUG);
  254. if ( $this->db->query($sql) )
  255. {
  256. return 1;
  257. }
  258. else
  259. {
  260. $this->error=$this->db->lasterror();
  261. dol_syslog(get_class($this)."::log ".$this->error, LOG_ERR);
  262. return -1;
  263. }
  264. }
  265. /**
  266. * Validate an order
  267. *
  268. * @param User $user Validator User
  269. * @param int $idwarehouse Id of warehouse to use for stock decrease
  270. * @return int <0 if KO, >0 if OK
  271. */
  272. function valid($user,$idwarehouse=0)
  273. {
  274. global $langs,$conf;
  275. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  276. $error=0;
  277. dol_syslog(get_class($this)."::valid");
  278. $result = 0;
  279. if ($user->rights->fournisseur->commande->valider)
  280. {
  281. $this->db->begin();
  282. // Definition du nom de modele de numerotation de commande
  283. $soc = new Societe($this->db);
  284. $soc->fetch($this->fourn_id);
  285. // Check if object has a temporary ref
  286. if (preg_match('/^[\(]?PROV/i', $this->ref))
  287. {
  288. $num = $this->getNextNumRef($soc);
  289. }
  290. else
  291. {
  292. $num = $this->ref;
  293. }
  294. $sql = 'UPDATE '.MAIN_DB_PREFIX."commande_fournisseur";
  295. $sql.= " SET ref='".$num."',";
  296. $sql.= " fk_statut = 1,";
  297. $sql.= " date_valid='".$this->db->idate(dol_now())."',";
  298. $sql.= " fk_user_valid = ".$user->id;
  299. $sql.= " WHERE rowid = ".$this->id;
  300. $sql.= " AND fk_statut = 0";
  301. $resql=$this->db->query($sql);
  302. if (! $resql)
  303. {
  304. dol_syslog(get_class($this)."::valid Echec update - 10 - sql=".$sql, LOG_ERR);
  305. dol_print_error($this->db);
  306. $error++;
  307. }
  308. if (! $error)
  309. {
  310. $this->oldref='';
  311. // Rename directory if dir was a temporary ref
  312. if (preg_match('/^[\(]?PROV/i', $this->ref))
  313. {
  314. // On renomme repertoire ($this->ref = ancienne ref, $num = nouvelle ref)
  315. // afin de ne pas perdre les fichiers attaches
  316. $oldref = dol_sanitizeFileName($this->ref);
  317. $newref = dol_sanitizeFileName($num);
  318. $dirsource = $conf->fournisseur->dir_output.'/commande/'.$oldref;
  319. $dirdest = $conf->fournisseur->dir_output.'/commande/'.$newref;
  320. if (file_exists($dirsource))
  321. {
  322. dol_syslog(get_class($this)."::valid rename dir ".$dirsource." into ".$dirdest);
  323. if (@rename($dirsource, $dirdest))
  324. {
  325. $this->oldref = $oldref;
  326. dol_syslog("Rename ok");
  327. // Suppression ancien fichier PDF dans nouveau rep
  328. dol_delete_file($dirdest.'/'.$oldref.'*.*');
  329. }
  330. }
  331. }
  332. }
  333. if (! $error)
  334. {
  335. $result = 1;
  336. $this->log($user, 1, time()); // Statut 1
  337. $this->ref = $num;
  338. }
  339. if (! $error)
  340. {
  341. // Appel des triggers
  342. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  343. $interface=new Interfaces($this->db);
  344. $result=$interface->run_triggers('ORDER_SUPPLIER_VALIDATE',$this,$user,$langs,$conf);
  345. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  346. // Fin appel triggers
  347. }
  348. if (! $error)
  349. {
  350. $this->db->commit();
  351. return 1;
  352. }
  353. else
  354. {
  355. dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
  356. $this->db->rollback();
  357. $this->error=$this->db->lasterror();
  358. return -1;
  359. }
  360. }
  361. else
  362. {
  363. $this->error='Not Authorized';
  364. dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
  365. return -1;
  366. }
  367. }
  368. /**
  369. * Set draft status
  370. * TODO This method seems to be never called.
  371. *
  372. * @param User $user Object user that modify
  373. * @param int $idwarehouse Id warehouse to use for stock change.
  374. * @return int <0 if KO, >0 if OK
  375. */
  376. function set_draft($user, $idwarehouse=-1)
  377. {
  378. global $conf,$langs;
  379. $error=0;
  380. // Protection
  381. if ($this->statut == 0)
  382. {
  383. dol_syslog(get_class($this)."::set_draft already draft status", LOG_WARNING);
  384. return 0;
  385. }
  386. if (! $user->rights->fournisseur->commande->valider)
  387. {
  388. $this->error='Permission denied';
  389. return -1;
  390. }
  391. $this->db->begin();
  392. $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
  393. $sql.= " SET fk_statut = 0";
  394. $sql.= " WHERE rowid = ".$this->id;
  395. dol_syslog(get_class($this)."::set_draft sql=".$sql, LOG_DEBUG);
  396. if ($this->db->query($sql))
  397. {
  398. // If stock is incremented on validate order, we must redecrement it
  399. if (! empty($conf->stock->enabled) && ! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER))
  400. {
  401. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
  402. $num=count($this->lines);
  403. for ($i = 0; $i < $num; $i++)
  404. {
  405. if ($this->lines[$i]->fk_product > 0)
  406. {
  407. $mouvP = new MouvementStock($this->db);
  408. // We increment stock of product (and sub-products)
  409. $result=$mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans("OrderBackToDraftInSpeedealing",$this->ref));
  410. if ($result < 0) { $error++; }
  411. }
  412. }
  413. if (!$error)
  414. {
  415. $this->statut=0;
  416. $this->db->commit();
  417. return $result;
  418. }
  419. else
  420. {
  421. $this->error=$mouvP->error;
  422. $this->db->rollback();
  423. return $result;
  424. }
  425. }
  426. $this->statut=0;
  427. $this->db->commit();
  428. return 1;
  429. }
  430. else
  431. {
  432. $this->error=$this->db->error();
  433. $this->db->rollback();
  434. dol_syslog($this->error, LOG_ERR);
  435. return -1;
  436. }
  437. }
  438. /**
  439. * Return label of the status of object
  440. *
  441. * @param int $mode 0=Long label, 1=Short label, 2=Picto + Short label, 3=Picto, 4=Picto + Long label
  442. * @return string Label
  443. */
  444. function getLibStatut($mode=0)
  445. {
  446. return $this->LibStatut($this->statut,$mode);
  447. }
  448. /**
  449. * Return label of a status
  450. *
  451. * @param int $statut Id statut
  452. * @param int $mode 0=Long label, 1=Short label, 2=Picto + Short label, 3=Picto, 4=Picto + Long label, 5=Short label + Picto
  453. * @return string Label of status
  454. */
  455. function LibStatut($statut,$mode=0)
  456. {
  457. global $langs;
  458. $langs->load('orders');
  459. // List of language codes for status
  460. $statutshort[0] = 'StatusOrderDraftShort';
  461. $statutshort[1] = 'StatusOrderValidatedShort';
  462. $statutshort[2] = 'StatusOrderApprovedShort';
  463. $statutshort[3] = 'StatusOrderOnProcessShort';
  464. $statutshort[4] = 'StatusOrderReceivedPartiallyShort';
  465. $statutshort[5] = 'StatusOrderReceivedAllShort';
  466. $statutshort[6] = 'StatusOrderCanceledShort';
  467. $statutshort[7] = 'StatusOrderCanceledShort';
  468. $statutshort[9] = 'StatusOrderRefusedShort';
  469. if ($mode == 0)
  470. {
  471. return $langs->trans($this->statuts[$statut]);
  472. }
  473. if ($mode == 1)
  474. {
  475. return $langs->trans($statutshort[$statut]);
  476. }
  477. if ($mode == 2)
  478. {
  479. return $langs->trans($this->statuts[$statut]);
  480. }
  481. if ($mode == 3)
  482. {
  483. if ($statut==0) return img_picto($langs->trans($this->statuts[$statut]),'statut0');
  484. if ($statut==1) return img_picto($langs->trans($this->statuts[$statut]),'statut1');
  485. if ($statut==2) return img_picto($langs->trans($this->statuts[$statut]),'statut3');
  486. if ($statut==3) return img_picto($langs->trans($this->statuts[$statut]),'statut3');
  487. if ($statut==4) return img_picto($langs->trans($this->statuts[$statut]),'statut3');
  488. if ($statut==5) return img_picto($langs->trans($this->statuts[$statut]),'statut6');
  489. if ($statut==6 || $statut==7) return img_picto($langs->trans($this->statuts[$statut]),'statut5');
  490. if ($statut==9) return img_picto($langs->trans($this->statuts[$statut]),'statut5');
  491. }
  492. if ($mode == 4)
  493. {
  494. if ($statut==0) return img_picto($langs->trans($this->statuts[$statut]),'statut0').' '.$langs->trans($this->statuts[$statut]);
  495. if ($statut==1) return img_picto($langs->trans($this->statuts[$statut]),'statut1').' '.$langs->trans($this->statuts[$statut]);
  496. if ($statut==2) return img_picto($langs->trans($this->statuts[$statut]),'statut3').' '.$langs->trans($this->statuts[$statut]);
  497. if ($statut==3) return img_picto($langs->trans($this->statuts[$statut]),'statut3').' '.$langs->trans($this->statuts[$statut]);
  498. if ($statut==4) return img_picto($langs->trans($this->statuts[$statut]),'statut3').' '.$langs->trans($this->statuts[$statut]);
  499. if ($statut==5) return img_picto($langs->trans($this->statuts[$statut]),'statut6').' '.$langs->trans($this->statuts[$statut]);
  500. if ($statut==6 || $statut==7) return img_picto($langs->trans($this->statuts[$statut]),'statut5').' '.$langs->trans($this->statuts[$statut]);
  501. if ($statut==9) return img_picto($langs->trans($this->statuts[$statut]),'statut5').' '.$langs->trans($this->statuts[$statut]);
  502. }
  503. if ($mode == 5)
  504. {
  505. if ($statut==0) return $langs->trans($this->statuts[$statut]).' '.img_picto($langs->trans($this->statuts[$statut]),'statut0');
  506. if ($statut==1) return $langs->trans($this->statuts[$statut]).' '.img_picto($langs->trans($this->statuts[$statut]),'statut1');
  507. if ($statut==2) return $langs->trans($this->statuts[$statut]).' '.img_picto($langs->trans($this->statuts[$statut]),'statut3');
  508. if ($statut==3) return $langs->trans($this->statuts[$statut]).' '.img_picto($langs->trans($this->statuts[$statut]),'statut3');
  509. if ($statut==4) return $langs->trans($this->statuts[$statut]).' '.img_picto($langs->trans($this->statuts[$statut]),'statut3');
  510. if ($statut==5) return $langs->trans($this->statuts[$statut]).' '.img_picto($langs->trans($this->statuts[$statut]),'statut6');
  511. if ($statut==6 || $statut==7) return $langs->trans($this->statuts[$statut]).' '.img_picto($langs->trans($this->statuts[$statut]),'statut5');
  512. if ($statut==9) return $langs->trans($this->statuts[$statut]).' '.img_picto($langs->trans($this->statuts[$statut]),'statut5');
  513. }
  514. }
  515. /**
  516. * Renvoie nom clicable (avec eventuellement le picto)
  517. *
  518. * @param int $withpicto 0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
  519. * @param string $option Sur quoi pointe le lien
  520. * @return string Chaine avec URL
  521. */
  522. function getNomUrl($withpicto=0,$option='')
  523. {
  524. global $langs;
  525. $result='';
  526. $lien = '<a href="'.DOL_URL_ROOT.'/fourn/commande/fiche.php?id='.$this->id.'">';
  527. $lienfin='</a>';
  528. $picto='order';
  529. $label=$langs->trans("ShowOrder").': '.$this->ref;
  530. if ($withpicto) $result.=($lien.img_object($label,$picto).$lienfin);
  531. if ($withpicto && $withpicto != 2) $result.=' ';
  532. $result.=$lien.$this->ref.$lienfin;
  533. return $result;
  534. }
  535. /**
  536. * Renvoie la reference de commande suivante non utilisee en fonction du modele
  537. * de numerotation actif defini dans COMMANDE_SUPPLIER_ADDON
  538. *
  539. * @param Societe $soc objet societe
  540. * @return string reference libre pour la facture
  541. */
  542. function getNextNumRef($soc)
  543. {
  544. global $db, $langs, $conf;
  545. $langs->load("orders");
  546. $dir = DOL_DOCUMENT_ROOT .'/core/modules/supplier_order/';
  547. if (! empty($conf->global->COMMANDE_SUPPLIER_ADDON))
  548. {
  549. $file = $conf->global->COMMANDE_SUPPLIER_ADDON.'.php';
  550. if (is_readable($dir.'/'.$file))
  551. {
  552. // Definition du nom de modele de numerotation de commande fournisseur
  553. $modName=$conf->global->COMMANDE_SUPPLIER_ADDON;
  554. require_once $dir.'/'.$file;
  555. // Recuperation de la nouvelle reference
  556. $objMod = new $modName($this->db);
  557. $numref = "";
  558. $numref = $objMod->commande_get_num($soc,$this);
  559. if ( $numref != "")
  560. {
  561. return $numref;
  562. }
  563. else
  564. {
  565. dol_print_error($db, get_class($this)."::getNextNumRef ".$obj->error);
  566. return -1;
  567. }
  568. }
  569. else
  570. {
  571. print $langs->trans("Error")." ".$langs->trans("Error_FailedToLoad_COMMANDE_SUPPLIER_ADDON_File",$conf->global->COMMANDE_SUPPLIER_ADDON);
  572. return -2;
  573. }
  574. }
  575. else
  576. {
  577. print $langs->trans("Error")." ".$langs->trans("Error_COMMANDE_SUPPLIER_ADDON_NotDefined");
  578. return -3;
  579. }
  580. }
  581. /**
  582. * Accept an order
  583. *
  584. * @param User $user Object user
  585. * @param int $idwarehouse Id of warhouse for stock change
  586. * @return int <0 if KO, >0 if OK
  587. */
  588. function approve($user, $idwarehouse=0)
  589. {
  590. global $langs,$conf;
  591. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  592. $error=0;
  593. dol_syslog(get_class($this)."::approve");
  594. if ($user->rights->fournisseur->commande->approuver)
  595. {
  596. $this->db->begin();
  597. // Definition du nom de modele de numerotation de commande
  598. $soc = new Societe($this->db);
  599. $soc->fetch($this->fourn_id);
  600. // Check if object has a temporary ref
  601. if (preg_match('/^[\(]?PROV/i', $this->ref))
  602. {
  603. $num = $this->getNextNumRef($soc);
  604. }
  605. else
  606. {
  607. $num = $this->ref;
  608. }
  609. $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
  610. $sql.= " SET ref='".$this->db->escape($num)."',";
  611. $sql.= " fk_statut = 2,";
  612. $sql.= " date_approve='".$this->db->idate(dol_now())."',";
  613. $sql.= " fk_user_approve = ".$user->id;
  614. $sql.= " WHERE rowid = ".$this->id;
  615. $sql.= " AND fk_statut = 1";
  616. if ($this->db->query($sql))
  617. {
  618. $this->log($user, 2, time()); // Statut 2
  619. // If stock is incremented on validate order, we must increment it
  620. if (! $error && ! empty($conf->stock->enabled) && ! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_VALIDATE_ORDER))
  621. {
  622. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
  623. $langs->load("agenda");
  624. $cpt=count($this->lines);
  625. for ($i = 0; $i < $cpt; $i++)
  626. {
  627. // Product with reference
  628. if ($this->lines[$i]->fk_product > 0)
  629. {
  630. $mouvP = new MouvementStock($this->db);
  631. // We decrement stock of product (and sub-products)
  632. $result=$mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans("OrderApprovedInSpeedealing",$this->ref));
  633. if ($result < 0) { $error++; }
  634. }
  635. }
  636. }
  637. if (! $error)
  638. {
  639. // Appel des triggers
  640. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  641. $interface=new Interfaces($this->db);
  642. $result=$interface->run_triggers('ORDER_SUPPLIER_APPROVE',$this,$user,$langs,$conf);
  643. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  644. // Fin appel triggers
  645. }
  646. if (! $error)
  647. {
  648. $this->db->commit();
  649. return 1;
  650. }
  651. else
  652. {
  653. $this->db->rollback();
  654. $this->error=$this->db->lasterror();
  655. return -1;
  656. }
  657. }
  658. else
  659. {
  660. $this->db->rollback();
  661. $this->error=$this->db->lasterror();
  662. dol_syslog(get_class($this)."::approve Error ",$this->error, LOG_ERR);
  663. return -1;
  664. }
  665. }
  666. else
  667. {
  668. dol_syslog(get_class($this)."::approve Not Authorized", LOG_ERR);
  669. }
  670. return -1;
  671. }
  672. /**
  673. * Refuse an order
  674. *
  675. * @param User $user User making action
  676. * @return int 0 if Ok, <0 if Ko
  677. */
  678. function refuse($user)
  679. {
  680. global $conf, $langs;
  681. $error=0;
  682. dol_syslog(get_class($this)."::refuse");
  683. $result = 0;
  684. if ($user->rights->fournisseur->commande->approuver)
  685. {
  686. $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur SET fk_statut = 9";
  687. $sql .= " WHERE rowid = ".$this->id;
  688. if ($this->db->query($sql))
  689. {
  690. $result = 0;
  691. $this->log($user, 9, time());
  692. if ($error == 0)
  693. {
  694. // Appel des triggers
  695. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  696. $interface=new Interfaces($this->db);
  697. $result=$interface->run_triggers('ORDER_SUPPLIER_REFUSE',$this,$user,$langs,$conf);
  698. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  699. // Fin appel triggers
  700. }
  701. }
  702. else
  703. {
  704. dol_syslog(get_class($this)."::refuse Error -1");
  705. $result = -1;
  706. }
  707. }
  708. else
  709. {
  710. dol_syslog(get_class($this)."::refuse Not Authorized");
  711. }
  712. return $result ;
  713. }
  714. /**
  715. * Cancel an approved order.
  716. * L'annulation se fait apres l'approbation
  717. *
  718. * @param User $user User making action
  719. * @param int $idwarehouse Id warehouse to use for stock change (not used for supplier orders).
  720. * @return int >0 if Ok, <0 if Ko
  721. */
  722. function Cancel($user, $idwarehouse=-1)
  723. {
  724. global $langs,$conf;
  725. $error=0;
  726. //dol_syslog("CommandeFournisseur::Cancel");
  727. $result = 0;
  728. if ($user->rights->fournisseur->commande->commander)
  729. {
  730. $statut = 6;
  731. $this->db->begin();
  732. $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur SET fk_statut = ".$statut;
  733. $sql .= " WHERE rowid = ".$this->id;
  734. dol_syslog(get_class($this)."::cancel sql=".$sql);
  735. if ($this->db->query($sql))
  736. {
  737. $result = 0;
  738. $this->log($user, $statut, time());
  739. // Appel des triggers
  740. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  741. $interface=new Interfaces($this->db);
  742. $result=$interface->run_triggers('ORDER_SUPPLIER_CANCEL',$this,$user,$langs,$conf);
  743. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  744. // Fin appel triggers
  745. if ($error == 0)
  746. {
  747. $this->db->commit();
  748. return 1;
  749. }
  750. else
  751. {
  752. $this->db->rollback();
  753. $this->error=$this->db->lasterror();
  754. return -1;
  755. }
  756. }
  757. else
  758. {
  759. $this->db->rollback();
  760. $this->error=$this->db->lasterror();
  761. dol_syslog(get_class($this)."::cancel ".$this->error);
  762. return -1;
  763. }
  764. }
  765. else
  766. {
  767. dol_syslog(get_class($this)."::cancel Not Authorized");
  768. return -1;
  769. }
  770. }
  771. /**
  772. * Send a supplier order to supplier
  773. *
  774. * @param User $user User making change
  775. * @param date $date Date
  776. * @param int $methode Method
  777. * @param string $comment Comment
  778. * @return int <0 if KO, >0 if OK
  779. */
  780. function commande($user, $date, $methode, $comment='')
  781. {
  782. dol_syslog(get_class($this)."::commande");
  783. $result = 0;
  784. if ($user->rights->fournisseur->commande->commander)
  785. {
  786. $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur SET fk_statut = 3, fk_input_method=".$methode.",date_commande=".$this->db->idate("$date");
  787. $sql .= " WHERE rowid = ".$this->id;
  788. dol_syslog(get_class($this)."::commande sql=".$sql, LOG_DEBUG);
  789. if ($this->db->query($sql))
  790. {
  791. $result = 0;
  792. $this->log($user, 3, $date, $comment);
  793. }
  794. else
  795. {
  796. dol_syslog(get_class($this)."::cCommande Error -1", LOG_ERR);
  797. $result = -1;
  798. }
  799. }
  800. else
  801. {
  802. dol_syslog(get_class($this)."::commande User not Authorized", LOG_ERR);
  803. }
  804. return $result ;
  805. }
  806. /**
  807. * Create order with draft status
  808. *
  809. * @param User $user User making creation
  810. * @param int $notrigger Disable all triggers
  811. * @return int <0 if KO, Id of supplier order if OK
  812. */
  813. function create($user, $notrigger=0)
  814. {
  815. global $langs,$conf;
  816. $this->db->begin();
  817. $error=0;
  818. $now=dol_now();
  819. /* On positionne en mode brouillon la commande */
  820. $this->brouillon = 1;
  821. $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseur (";
  822. $sql.= "ref";
  823. $sql.= ", entity";
  824. $sql.= ", fk_soc";
  825. $sql.= ", date_creation";
  826. //$sql.= ", date_livraison";
  827. $sql.= ", fk_user_author";
  828. $sql.= ", fk_statut";
  829. $sql.= ", source";
  830. $sql.= ", model_pdf";
  831. //$sql.= ", fk_mode_reglement";
  832. $sql.= ") ";
  833. $sql.= " VALUES (";
  834. $sql.= "''";
  835. $sql.= ", ".$conf->entity;
  836. $sql.= ", ".$this->socid;
  837. $sql.= ", ".$this->db->idate($now);
  838. //$sql.= ", ".$this->db->idate($now);
  839. $sql.= ", ".$user->id;
  840. $sql.= ", 0";
  841. $sql.= ", 0";
  842. $sql.= ", '".$conf->global->COMMANDE_SUPPLIER_ADDON_PDF."'";
  843. //$sql.= ", ".$this->mode_reglement_id;
  844. $sql.= ")";
  845. dol_syslog(get_class($this)."::create sql=".$sql);
  846. if ($this->db->query($sql))
  847. {
  848. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."commande_fournisseur");
  849. $sql = "UPDATE ".MAIN_DB_PREFIX."commande_fournisseur";
  850. $sql.= " SET ref='(PROV".$this->id.")'";
  851. $sql.= " WHERE rowid=".$this->id;
  852. dol_syslog(get_class($this)."::create sql=".$sql);
  853. if ($this->db->query($sql))
  854. {
  855. // On logue creation pour historique
  856. $this->log($user, 0, time());
  857. if (! $notrigger)
  858. {
  859. // Appel des triggers
  860. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  861. $interface=new Interfaces($this->db);
  862. $result=$interface->run_triggers('ORDER_SUPPLIER_CREATE',$this,$user,$langs,$conf);
  863. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  864. // Fin appel triggers
  865. }
  866. $this->db->commit();
  867. return $this->id;
  868. }
  869. else
  870. {
  871. $this->error=$this->db->error();
  872. dol_syslog(get_class($this)."::create: Failed -2 - ".$this->error, LOG_ERR);
  873. $this->db->rollback();
  874. return -2;
  875. }
  876. }
  877. else
  878. {
  879. $this->error=$this->db->error();
  880. dol_syslog(get_class($this)."::create: Failed -1 - ".$this->error, LOG_ERR);
  881. $this->db->rollback();
  882. return -1;
  883. }
  884. }
  885. /**
  886. * Add order line
  887. *
  888. * @param string $desc Description
  889. * @param double $pu_ht Unit price
  890. * @param double $qty Quantity
  891. * @param double $txtva Taux tva
  892. * @param double $txlocaltax1 Localtax1 tax
  893. * @param double $txlocaltax2 Localtax2 tax
  894. * @param int $fk_product Id produit
  895. * @param int $fk_prod_fourn_price Id supplier price
  896. * @param string $fourn_ref Supplier reference
  897. * @param double $remise_percent Remise
  898. * @param string $price_base_type HT or TTC
  899. * @param double $pu_ttc Unit price TTC
  900. * @param int $type Type of line (0=product, 1=service)
  901. * @param int $info_bits More information
  902. * @param int $notrigger Disable triggers
  903. * @return int <=0 if KO, >0 if OK
  904. */
  905. function addline($desc, $pu_ht, $qty, $txtva, $txlocaltax1=0, $txlocaltax2=0, $fk_product=0, $fk_prod_fourn_price=0, $fourn_ref='', $remise_percent=0, $price_base_type='HT', $pu_ttc=0, $type=0, $info_bits=0, $notrigger=false)
  906. {
  907. global $langs,$mysoc;
  908. dol_syslog(get_class($this)."::addline $desc, $pu_ht, $qty, $txtva, $txlocaltax1, $txlocaltax2. $fk_product, $fk_prod_fourn_price, $fourn_ref, $remise_percent, $price_base_type, $pu_ttc, $type");
  909. include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
  910. // Clean parameters
  911. if (! $qty) $qty=1;
  912. if (! $info_bits) $info_bits=0;
  913. if (empty($txtva)) $txtva=0;
  914. if (empty($txlocaltax1)) $txlocaltax1=0;
  915. if (empty($txlocaltax2)) $txlocaltax2=0;
  916. $remise_percent=price2num($remise_percent);
  917. $qty=price2num($qty);
  918. $pu_ht=price2num($pu_ht);
  919. $pu_ttc=price2num($pu_ttc);
  920. $txtva = price2num($txtva);
  921. $txlocaltax1 = price2num($txlocaltax1);
  922. $txlocaltax2 = price2num($txlocaltax2);
  923. if ($price_base_type=='HT')
  924. {
  925. $pu=$pu_ht;
  926. }
  927. else
  928. {
  929. $pu=$pu_ttc;
  930. }
  931. $desc=trim($desc);
  932. // Check parameters
  933. if ($qty < 1 && ! $fk_product)
  934. {
  935. $this->error=$langs->trans("ErrorFieldRequired",$langs->trans("Product"));
  936. return -1;
  937. }
  938. if ($type < 0) return -1;
  939. if ($this->statut == 0)
  940. {
  941. $this->db->begin();
  942. if ($fk_product > 0)
  943. {
  944. $prod = new Product($this->db, $fk_product);
  945. if ($prod->fetch($fk_product) > 0)
  946. {
  947. $result=$prod->get_buyprice($fk_prod_fourn_price,$qty,$fk_product,$fourn_ref);
  948. if ($result > 0)
  949. {
  950. $label = $prod->libelle;
  951. $pu = $prod->fourn_pu;
  952. $ref = $prod->ref_fourn;
  953. $product_type = $prod->type;
  954. }
  955. if ($result == 0 || $result == -1)
  956. {
  957. $this->error="No price found for this quantity. Quantity may be too low ?";
  958. $this->db->rollback();
  959. dol_syslog(get_class($this)."::addline result=".$result." - ".$this->error, LOG_DEBUG);
  960. return -1;
  961. }
  962. if ($result < -1)
  963. {
  964. $this->error=$prod->error;
  965. $this->db->rollback();
  966. dol_syslog(get_class($this)."::addline result=".$result." - ".$this->error, LOG_ERR);
  967. return -1;
  968. }
  969. }
  970. else
  971. {
  972. $this->error=$this->db->error();
  973. return -1;
  974. }
  975. }
  976. else
  977. {
  978. $product_type = $type;
  979. }
  980. // Calcul du total TTC et de la TVA pour la ligne a partir de
  981. // qty, pu, remise_percent et txtva
  982. // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
  983. // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
  984. $tabprice = calcul_price_total($qty, $pu, $remise_percent, $txtva, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits);
  985. $total_ht = $tabprice[0];
  986. $total_tva = $tabprice[1];
  987. $total_ttc = $tabprice[2];
  988. $total_localtax1 = $tabprice[9];
  989. $total_localtax2 = $tabprice[10];
  990. $subprice = price2num($pu,'MU');
  991. // TODO A virer
  992. // Anciens indicateurs: $price, $remise (a ne plus utiliser)
  993. $remise = 0;
  994. if ($remise_percent > 0)
  995. {
  996. $remise = round(($pu * $remise_percent / 100), 2);
  997. }
  998. $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseurdet";
  999. $sql.= " (fk_commande,label, description,";
  1000. $sql.= " fk_product, product_type,";
  1001. $sql.= " qty, tva_tx, localtax1_tx, localtax2_tx, remise_percent, subprice, remise, ref,";
  1002. $sql.= " total_ht, total_tva, total_localtax1, total_localtax2, total_ttc";
  1003. $sql.= ")";
  1004. $sql.= " VALUES (".$this->id.", '" . $this->db->escape($label) . "','" . $this->db->escape($desc) . "',";
  1005. if ($fk_product) { $sql.= $fk_product.","; }
  1006. else { $sql.= "null,"; }
  1007. $sql.= "'".$product_type."',";
  1008. $sql.= "'".$qty."', ".$txtva.", ".$txlocaltax1.", ".$txlocaltax2.", ".$remise_percent.",'".price2num($subprice,'MU')."','".price2num($remise)."','".$ref."',";
  1009. $sql.= "'".price2num($total_ht)."',";
  1010. $sql.= "'".price2num($total_tva)."',";
  1011. $sql.= "'".price2num($total_localtax1)."',";
  1012. $sql.= "'".price2num($total_localtax2)."',";
  1013. $sql.= "'".price2num($total_ttc)."'";
  1014. $sql.= ")";
  1015. dol_syslog(get_class($this)."::addline sql=".$sql);
  1016. $resql=$this->db->query($sql);
  1017. //print $sql;
  1018. if ($resql)
  1019. {
  1020. $this->rowid = $this->db->last_insert_id(MAIN_DB_PREFIX.'commande_fournisseurdet');
  1021. if (! $notrigger)
  1022. {
  1023. global $conf, $langs, $user;
  1024. // Appel des triggers
  1025. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  1026. $interface=new Interfaces($this->db);
  1027. $result=$interface->run_triggers('LINEORDER_SUPPLIER_CREATE',$this,$user,$langs,$conf);
  1028. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  1029. // Fin appel triggers
  1030. }
  1031. $this->update_price();
  1032. $this->db->commit();
  1033. return 1;
  1034. }
  1035. else
  1036. {
  1037. $this->error=$this->db->error();
  1038. $this->db->rollback();
  1039. dol_syslog(get_class($this)."::addline ".$this->error, LOG_ERR);
  1040. return -1;
  1041. }
  1042. }
  1043. }
  1044. /**
  1045. * Add a product into a stock warehouse.
  1046. *
  1047. * @param User $user User object making change
  1048. * @param int $product Id of product to dispatch
  1049. * @param double $qty Qty to dispatch
  1050. * @param int $entrepot Id of warehouse to add product
  1051. * @param double $price Price for PMP value calculation
  1052. * @param string $comment Comment for stock movement
  1053. * @return int <0 if KO, >0 if OK
  1054. */
  1055. function DispatchProduct($user, $product, $qty, $entrepot, $price=0, $comment='')
  1056. {
  1057. global $conf;
  1058. $error = 0;
  1059. require_once DOL_DOCUMENT_ROOT .'/product/stock/class/mouvementstock.class.php';
  1060. // Check parameters
  1061. if ($entrepot <= 0 || $qty <= 0)
  1062. {
  1063. $this->error='BadValueForParameter';
  1064. return -1;
  1065. }
  1066. $now=dol_now();
  1067. if (($this->statut == 3 || $this->statut == 4 || $this->statut == 5))
  1068. {
  1069. $this->db->begin();
  1070. $sql = "INSERT INTO ".MAIN_DB_PREFIX."commande_fournisseur_dispatch ";
  1071. $sql.= " (fk_commande,fk_product, qty, fk_entrepot, fk_user, datec) VALUES ";
  1072. $sql.= " ('".$this->id."','".$product."','".$qty."',".($entrepot>0?"'".$entrepot."'":"null").",'".$user->id."','".$this->db->idate($now)."')";
  1073. dol_syslog(get_class($this)."::DispatchProduct sql=".$sql);
  1074. $resql = $this->db->query($sql);
  1075. if (! $resql)
  1076. {
  1077. $this->error=$this->db->lasterror();
  1078. $error++;
  1079. }
  1080. // Si module stock gere et que incrementation faite depuis un dispatching en stock
  1081. if (!$error && $entrepot > 0 && ! empty($conf->stock->enabled) && ! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_DISPATCH_ORDER))
  1082. {
  1083. $mouv = new MouvementStock($this->db);
  1084. if ($product > 0)
  1085. {
  1086. $result=$mouv->reception($user, $product, $entrepot, $qty, $price, $comment);
  1087. if ($result < 0)
  1088. {
  1089. $this->error=$mouv->error;
  1090. dol_syslog(get_class($this)."::DispatchProduct ".$this->error, LOG_ERR);
  1091. $error++;
  1092. }
  1093. }
  1094. }
  1095. if ($error == 0)
  1096. {
  1097. $this->db->commit();
  1098. return 1;
  1099. }
  1100. else
  1101. {
  1102. $this->db->rollback();
  1103. return -1;
  1104. }
  1105. }
  1106. else
  1107. {
  1108. $this->error='BadStatusForObject';
  1109. return -2;
  1110. }
  1111. }
  1112. /**
  1113. * Delete line
  1114. *
  1115. * @param int $idligne Id of line to delete
  1116. * @return 0 if Ok, <0 ik Ko
  1117. */
  1118. function deleteline($idligne)
  1119. {
  1120. if ($this->statut == 0)
  1121. {
  1122. $sql = "DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseurdet WHERE rowid = ".$idligne;
  1123. $resql=$this->db->query($sql);
  1124. dol_syslog(get_class($this)."::deleteline sql=".$sql);
  1125. if ($resql)
  1126. {
  1127. $result=$this->update_price();
  1128. return 0;
  1129. }
  1130. else
  1131. {
  1132. $this->error=$this->db->error();
  1133. return -1;
  1134. }
  1135. }
  1136. else
  1137. {
  1138. return -1;
  1139. }
  1140. }
  1141. /**
  1142. * Delete an order
  1143. *
  1144. * @param User $user Object user
  1145. * @return int <0 if KO, >0 if OK
  1146. */
  1147. function delete($user='')
  1148. {
  1149. global $langs,$conf;
  1150. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  1151. $error = 0;
  1152. $this->db->begin();
  1153. $sql = "DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseurdet WHERE fk_commande =". $this->id ;
  1154. dol_syslog(get_class($this)."::delete sql=".$sql, LOG_DEBUG);
  1155. if (! $this->db->query($sql) )
  1156. {
  1157. $error++;
  1158. }
  1159. $sql = "DELETE FROM ".MAIN_DB_PREFIX."commande_fournisseur WHERE rowid =".$this->id;
  1160. dol_syslog(get_class($this)."::delete sql=".$sql, LOG_DEBUG);
  1161. if ($resql = $this->db->query($sql) )
  1162. {
  1163. if ($this->db->affected_rows($resql) < 1)
  1164. {
  1165. $error++;
  1166. }
  1167. }
  1168. else
  1169. {
  1170. $error++;
  1171. }
  1172. if (! $error)
  1173. {
  1174. // Appel des trigge…

Large files files are truncated, but you can click here to view the full file