PageRenderTime 62ms CodeModel.GetById 31ms RepoModel.GetById 1ms app.codeStats 0ms

/htdocs/fourn/class/fournisseur.facture.class.php

https://bitbucket.org/speedealing/speedealing
PHP | 1476 lines | 1066 code | 159 blank | 251 comment | 153 complexity | 777954d881e77f4ed02e482bde88e39d 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) 2002-2004 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004-2012 Laurent Destailleur <eldy@users.sourceforge.net>
  4. * Copyright (C) 2004 Christophe Combelles <ccomb@free.fr>
  5. * Copyright (C) 2005 Marc Barilley <marc@ocebo.com>
  6. * Copyright (C) 2005-2012 Regis Houssin <regis.houssin@capnetworks.com>
  7. * Copyright (C) 2010-2011 Juanjo Menent <jmenent@2byte.es>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/fourn/class/fournisseur.facture.class.php
  24. * \ingroup fournisseur,facture
  25. * \brief File of class to manage suppliers invoices
  26. */
  27. include_once DOL_DOCUMENT_ROOT.'/core/class/commoninvoice.class.php';
  28. /**
  29. * \class FactureFournisseur
  30. * \brief Class to manage suppliers invoices
  31. */
  32. class FactureFournisseur extends CommonInvoice
  33. {
  34. public $element='invoice_supplier';
  35. public $table_element='facture_fourn';
  36. public $table_element_line='facture_fourn_det';
  37. public $fk_element='fk_facture_fourn';
  38. protected $ismultientitymanaged = 1; // 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  39. var $ref; // TODO deprecated
  40. var $product_ref;
  41. var $ref_supplier;
  42. var $socid;
  43. //! 0=Standard invoice, 1=Replacement invoice, 2=Credit note invoice, 3=Deposit invoice, 4=Proforma invoice
  44. var $type;
  45. //! 0=draft,
  46. //! 1=validated
  47. //! 2=classified paid partially (close_code='discount_vat','badcustomer') or completely (close_code=null),
  48. //! Also 2, should be 3=classified abandoned and no payment done (close_code='badcustomer','abandon' ou 'replaced')
  49. var $statut;
  50. //! 1 si facture payee COMPLETEMENT, 0 sinon (ce champ ne devrait plus servir car insuffisant)
  51. var $paye;
  52. var $author;
  53. var $libelle;
  54. var $datec; // Creation date
  55. var $tms; // Last update date
  56. var $date; // Invoice date
  57. var $date_echeance; // Max payment date
  58. var $amount;
  59. var $remise;
  60. var $tva;
  61. var $localtax1;
  62. var $localtax2;
  63. var $total_ht;
  64. var $total_tva;
  65. var $total_localtax1;
  66. var $total_localtax2;
  67. var $total_ttc;
  68. var $note; // deprecated
  69. var $note_private;
  70. var $note_public;
  71. var $propalid;
  72. var $lines;
  73. var $fournisseur; // deprecated
  74. var $thirdparty; // To store thirdparty
  75. var $extraparams=array();
  76. /**
  77. * Constructor
  78. *
  79. * @param DoliDB $db Database handler
  80. */
  81. function __construct($db = '')
  82. {
  83. $this->db = $db;
  84. $this->amount = 0;
  85. $this->remise = 0;
  86. $this->tva = 0;
  87. $this->total_localtax1 = 0;
  88. $this->total_localtax2 = 0;
  89. $this->total_ht = 0;
  90. $this->total_tva = 0;
  91. $this->total_ttc = 0;
  92. $this->propalid = 0;
  93. $this->products = array();
  94. $this->lines = array();
  95. }
  96. /**
  97. * Create supplier invoice into database
  98. *
  99. * @param User $user object utilisateur qui cree
  100. * @return int id facture si ok, < 0 si erreur
  101. */
  102. function create($user)
  103. {
  104. global $langs,$conf;
  105. $error=0;
  106. $now=dol_now();
  107. // Clear parameters
  108. if (empty($this->date)) $this->date=$now;
  109. $socid = $this->socid;
  110. $number = $this->ref_supplier?$this->ref_supplier:$this->ref;
  111. $amount = $this->amount;
  112. $remise = $this->remise;
  113. $this->db->begin();
  114. if (! $remise) $remise = 0 ;
  115. $totalht = ($amount - $remise);
  116. $sql = "INSERT INTO ".MAIN_DB_PREFIX."facture_fourn (";
  117. $sql.= "facnumber";
  118. $sql.= ", entity";
  119. $sql.= ", libelle";
  120. $sql.= ", fk_soc";
  121. $sql.= ", datec";
  122. $sql.= ", datef";
  123. $sql.= ", note";
  124. $sql.= ", note_public";
  125. $sql.= ", fk_user_author";
  126. $sql.= ", date_lim_reglement";
  127. $sql.= ")";
  128. $sql.= " VALUES (";
  129. $sql.= "'".$this->db->escape($number)."'";
  130. $sql.= ", ".$conf->entity;
  131. $sql.= ", '".$this->db->escape($this->libelle)."'";
  132. $sql.= ", ".$this->socid;
  133. $sql.= ", '".$this->db->idate($now)."'";
  134. $sql.= ", '".$this->db->idate($this->date)."'";
  135. $sql.= ", '".$this->db->escape($this->note)."'";
  136. $sql.= ", '".$this->db->escape($this->note_public)."'";
  137. $sql.= ", ".$user->id.",";
  138. $sql.= $this->date_echeance!=''?"'".$this->db->idate($this->date_echeance)."'":"null";
  139. $sql.= ")";
  140. dol_syslog("FactureFournisseur::create sql=".$sql, LOG_DEBUG);
  141. $resql=$this->db->query($sql);
  142. if ($resql)
  143. {
  144. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX.'facture_fourn');
  145. // Add object linked
  146. if (! $error && $this->id && ! empty($this->origin) && ! empty($this->origin_id))
  147. {
  148. $ret = $this->add_object_linked();
  149. if (! $ret)
  150. {
  151. dol_print_error($this->db);
  152. $error++;
  153. }
  154. }
  155. foreach ($this->lines as $i => $val)
  156. {
  157. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'facture_fourn_det (fk_facture_fourn)';
  158. $sql .= ' VALUES ('.$this->id.');';
  159. dol_syslog("FactureFournisseur::create sql=".$sql, LOG_DEBUG);
  160. $resql_insert=$this->db->query($sql);
  161. if ($resql_insert)
  162. {
  163. $idligne = $this->db->last_insert_id(MAIN_DB_PREFIX.'facture_fourn_det');
  164. $this->updateline(
  165. $idligne,
  166. $this->lines[$i]->description,
  167. $this->lines[$i]->pu_ht,
  168. $this->lines[$i]->tva_tx,
  169. $this->lines[$i]->localtax1_tx,
  170. $this->lines[$i]->localtax2_tx,
  171. $this->lines[$i]->qty,
  172. $this->lines[$i]->fk_product,
  173. 'HT',
  174. (! empty($this->lines[$i]->info_bits)?$this->lines[$i]->info_bits:''),
  175. $this->lines[$i]->product_type
  176. );
  177. }
  178. }
  179. // Update total price
  180. $result=$this->update_price();
  181. if ($result > 0)
  182. {
  183. // Appel des triggers
  184. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  185. $interface=new Interfaces($this->db);
  186. $result=$interface->run_triggers('BILL_SUPPLIER_CREATE',$this,$user,$langs,$conf);
  187. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  188. // Fin appel triggers
  189. if (! $error)
  190. {
  191. $this->db->commit();
  192. return $this->id;
  193. }
  194. else
  195. {
  196. $this->db->rollback();
  197. return -4;
  198. }
  199. }
  200. else
  201. {
  202. $this->error=$langs->trans('FailedToUpdatePrice');
  203. $this->db->rollback();
  204. return -3;
  205. }
  206. }
  207. else
  208. {
  209. if ($this->db->errno() == 'DB_ERROR_RECORD_ALREADY_EXISTS')
  210. {
  211. $this->error=$langs->trans('ErrorRefAlreadyExists');
  212. $this->db->rollback();
  213. return -1;
  214. }
  215. else
  216. {
  217. $this->error=$this->db->error();
  218. $this->db->rollback();
  219. return -2;
  220. }
  221. }
  222. }
  223. /**
  224. * Load object in memory from database
  225. *
  226. * @param int $id Id supplier invoice
  227. * @param string $ref Ref supplier invoice
  228. * @return int <0 if KO, >0 if OK, 0 if not found
  229. */
  230. function fetch($id='',$ref='')
  231. {
  232. global $langs;
  233. $sql = "SELECT";
  234. $sql.= " t.rowid,";
  235. $sql.= " t.facnumber,";
  236. $sql.= " t.entity,";
  237. $sql.= " t.type,";
  238. $sql.= " t.fk_soc,";
  239. $sql.= " t.datec,";
  240. $sql.= " t.datef,";
  241. $sql.= " t.tms,";
  242. $sql.= " t.libelle,";
  243. $sql.= " t.paye,";
  244. $sql.= " t.amount,";
  245. $sql.= " t.remise,";
  246. $sql.= " t.close_code,";
  247. $sql.= " t.close_note,";
  248. $sql.= " t.tva,";
  249. $sql.= " t.localtax1,";
  250. $sql.= " t.localtax2,";
  251. $sql.= " t.total,";
  252. $sql.= " t.total_ht,";
  253. $sql.= " t.total_tva,";
  254. $sql.= " t.total_ttc,";
  255. $sql.= " t.fk_statut,";
  256. $sql.= " t.fk_user_author,";
  257. $sql.= " t.fk_user_valid,";
  258. $sql.= " t.fk_facture_source,";
  259. $sql.= " t.fk_projet,";
  260. $sql.= " t.fk_cond_reglement,";
  261. $sql.= " t.date_lim_reglement,";
  262. $sql.= " t.note as note_private,";
  263. $sql.= " t.note_public,";
  264. $sql.= " t.model_pdf,";
  265. $sql.= " t.import_key,";
  266. $sql.= " t.extraparams,";
  267. $sql.= ' s.nom as socnom, s.rowid as socid';
  268. $sql.= ' FROM '.MAIN_DB_PREFIX.'facture_fourn as t,'.MAIN_DB_PREFIX.'societe as s';
  269. if ($id) $sql.= " WHERE t.rowid=".$id;
  270. if ($ref) $sql.= " WHERE t.rowid='".$this->db->escape($ref)."'"; // ref is id (facnumber is supplier ref)
  271. $sql.= ' AND t.fk_soc = s.rowid';
  272. dol_syslog(get_class($this)."::fetch sql=".$sql, LOG_DEBUG);
  273. $resql=$this->db->query($sql);
  274. if ($resql)
  275. {
  276. if ($this->db->num_rows($resql))
  277. {
  278. $obj = $this->db->fetch_object($resql);
  279. $this->id = $obj->rowid;
  280. $this->ref = $obj->rowid;
  281. $this->ref_supplier = $obj->facnumber;
  282. $this->facnumber = $obj->facnumber;
  283. $this->entity = $obj->entity;
  284. $this->type = empty($obj->type)?0:$obj->type;
  285. $this->fk_soc = $obj->fk_soc;
  286. $this->datec = $this->db->jdate($obj->datec);
  287. $this->date = $this->db->jdate($obj->datef);
  288. $this->datep = $this->db->jdate($obj->datef);
  289. $this->tms = $this->db->jdate($obj->tms);
  290. $this->libelle = $obj->libelle;
  291. $this->label = $obj->libelle;
  292. $this->paye = $obj->paye;
  293. $this->amount = $obj->amount;
  294. $this->remise = $obj->remise;
  295. $this->close_code = $obj->close_code;
  296. $this->close_note = $obj->close_note;
  297. $this->tva = $obj->tva;
  298. $this->total_localtax1 = $obj->localtax1;
  299. $this->total_localtax2 = $obj->localtax2;
  300. $this->total = $obj->total;
  301. $this->total_ht = $obj->total_ht;
  302. $this->total_tva = $obj->total_tva;
  303. $this->total_ttc = $obj->total_ttc;
  304. $this->fk_statut = $obj->fk_statut;
  305. $this->statut = $obj->fk_statut;
  306. $this->fk_user_author = $obj->fk_user_author;
  307. $this->author = $obj->fk_user_author;
  308. $this->fk_user_valid = $obj->fk_user_valid;
  309. $this->fk_facture_source = $obj->fk_facture_source;
  310. $this->fk_project = $obj->fk_projet;
  311. $this->fk_cond_reglement = $obj->fk_cond_reglement;
  312. $this->date_echeance = $this->db->jdate($obj->date_lim_reglement);
  313. $this->note = $obj->note_private; // deprecated
  314. $this->note_private = $obj->note_private;
  315. $this->note_public = $obj->note_public;
  316. $this->model_pdf = $obj->model_pdf;
  317. $this->import_key = $obj->import_key;
  318. $this->extraparams = (array) json_decode($obj->extraparams, true);
  319. $this->socid = $obj->socid;
  320. $this->socnom = $obj->socnom;
  321. $result=$this->fetch_lines();
  322. if ($result < 0)
  323. {
  324. $this->error=$this->db->error();
  325. dol_syslog(get_class($this).'::fetch Error '.$this->error, LOG_ERR);
  326. return -3;
  327. }
  328. }
  329. else
  330. {
  331. $this->error='Bill with id '.$id.' not found sql='.$sql;
  332. dol_syslog(get_class($this).'::fetch '.$this->error);
  333. return 0;
  334. }
  335. $this->db->free($resql);
  336. return 1;
  337. }
  338. else
  339. {
  340. $this->error="Error ".$this->db->lasterror();
  341. dol_syslog(get_class($this)."::fetch ".$this->error, LOG_ERR);
  342. return -1;
  343. }
  344. }
  345. /**
  346. * Load this->lines
  347. *
  348. * @return int 1 si ok, < 0 si erreur
  349. */
  350. function fetch_lines()
  351. {
  352. $sql = 'SELECT f.rowid, f.description, f.pu_ht, f.pu_ttc, f.qty, f.remise_percent, f.tva_tx, f.tva';
  353. $sql.= ', f.localtax1_tx, f.localtax2_tx, f.total_localtax1, f.total_localtax2 ';
  354. $sql.= ', f.total_ht, f.tva as total_tva, f.total_ttc, f.fk_product, f.product_type';
  355. $sql.= ', p.rowid as product_id, p.ref as product_ref, p.label as label, p.description as product_desc';
  356. $sql.= ' FROM '.MAIN_DB_PREFIX.'facture_fourn_det as f';
  357. $sql.= ' LEFT JOIN '.MAIN_DB_PREFIX.'product as p ON f.fk_product = p.rowid';
  358. $sql.= ' WHERE fk_facture_fourn='.$this->id;
  359. dol_syslog("FactureFournisseur::fetch_lines sql=".$sql, LOG_DEBUG);
  360. $resql_rows = $this->db->query($sql);
  361. if ($resql_rows)
  362. {
  363. $num_rows = $this->db->num_rows($resql_rows);
  364. if ($num_rows)
  365. {
  366. $i = 0;
  367. while ($i < $num_rows)
  368. {
  369. $obj = $this->db->fetch_object($resql_rows);
  370. $this->lines[$i] = new stdClass();
  371. $this->lines[$i]->rowid = $obj->rowid;
  372. $this->lines[$i]->description = $obj->description;
  373. $this->lines[$i]->ref = $obj->product_ref; // TODO deprecated
  374. $this->lines[$i]->product_ref = $obj->product_ref; // Internal reference
  375. //$this->lines[$i]->ref_fourn = $obj->ref_fourn; // Reference fournisseur du produit
  376. $this->lines[$i]->libelle = $obj->label; // Label du produit
  377. $this->lines[$i]->product_desc = $obj->product_desc; // Description du produit
  378. $this->lines[$i]->pu_ht = $obj->pu_ht;
  379. $this->lines[$i]->pu_ttc = $obj->pu_ttc;
  380. $this->lines[$i]->tva_tx = $obj->tva_tx;
  381. $this->lines[$i]->localtax1_tx = $obj->localtax1_tx;
  382. $this->lines[$i]->localtax2_tx = $obj->localtax2_tx;
  383. $this->lines[$i]->qty = $obj->qty;
  384. $this->lines[$i]->remise_percent = $obj->remise_percent;
  385. $this->lines[$i]->tva = $obj->tva;
  386. $this->lines[$i]->total_ht = $obj->total_ht;
  387. $this->lines[$i]->total_tva = $obj->total_tva;
  388. $this->lines[$i]->total_localtax1 = $obj->total_localtax1;
  389. $this->lines[$i]->total_localtax2 = $obj->total_localtax2;
  390. $this->lines[$i]->total_ttc = $obj->total_ttc;
  391. $this->lines[$i]->fk_product = $obj->fk_product;
  392. $this->lines[$i]->product_type = $obj->product_type;
  393. $i++;
  394. }
  395. }
  396. $this->db->free($resql_rows);
  397. return 1;
  398. }
  399. else
  400. {
  401. $this->error=$this->db->error();
  402. dol_syslog('FactureFournisseur::fetch_lines: Error '.$this->error,LOG_ERR);
  403. return -3;
  404. }
  405. }
  406. /**
  407. * Update database
  408. *
  409. * @param User $user User that modify
  410. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  411. * @return int <0 if KO, >0 if OK
  412. */
  413. function update($user=0, $notrigger=0)
  414. {
  415. global $conf, $langs;
  416. $error=0;
  417. // Clean parameters
  418. if (isset($this->ref_supplier)) $this->ref_supplier=trim($this->ref_supplier);
  419. if (isset($this->entity)) $this->entity=trim($this->entity);
  420. if (isset($this->type)) $this->type=trim($this->type);
  421. if (isset($this->fk_soc)) $this->fk_soc=trim($this->fk_soc);
  422. if (isset($this->libelle)) $this->libelle=trim($this->libelle);
  423. if (isset($this->paye)) $this->paye=trim($this->paye);
  424. if (isset($this->amount)) $this->amount=trim($this->amount);
  425. if (isset($this->remise)) $this->remise=trim($this->remise);
  426. if (isset($this->close_code)) $this->close_code=trim($this->close_code);
  427. if (isset($this->close_note)) $this->close_note=trim($this->close_note);
  428. if (isset($this->tva)) $this->tva=trim($this->tva);
  429. if (isset($this->localtax1)) $this->localtax1=trim($this->localtax1);
  430. if (isset($this->localtax2)) $this->localtax2=trim($this->localtax2);
  431. if (empty($this->total)) $this->total=0;
  432. if (empty($this->total_ht)) $this->total_ht=0;
  433. if (empty($this->total_tva)) $this->total_tva=0;
  434. // if (isset($this->total_localtax1)) $this->total_localtax1=trim($this->total_localtax1);
  435. // if (isset($this->total_localtax2)) $this->total_localtax2=trim($this->total_localtax2);
  436. if (isset($this->total_ttc)) $this->total_ttc=trim($this->total_ttc);
  437. if (isset($this->statut)) $this->statut=trim($this->statut);
  438. if (isset($this->author)) $this->author=trim($this->author);
  439. if (isset($this->fk_user_valid)) $this->fk_user_valid=trim($this->fk_user_valid);
  440. if (isset($this->fk_facture_source)) $this->fk_facture_source=trim($this->fk_facture_source);
  441. if (isset($this->fk_project)) $this->fk_project=trim($this->fk_project);
  442. if (isset($this->fk_cond_reglement)) $this->fk_cond_reglement=trim($this->fk_cond_reglement);
  443. if (isset($this->note)) $this->note=trim($this->note);
  444. if (isset($this->note_public)) $this->note_public=trim($this->note_public);
  445. if (isset($this->model_pdf)) $this->model_pdf=trim($this->model_pdf);
  446. if (isset($this->import_key)) $this->import_key=trim($this->import_key);
  447. // Check parameters
  448. // Put here code to add control on parameters values
  449. // Update request
  450. $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn SET";
  451. $sql.= " facnumber=".(isset($this->facnumber)?"'".$this->db->escape($this->facnumber)."'":"null").",";
  452. $sql.= " entity=".(isset($this->entity)?$this->entity:"null").",";
  453. $sql.= " type=".(isset($this->type)?$this->type:"null").",";
  454. $sql.= " fk_soc=".(isset($this->fk_soc)?$this->fk_soc:"null").",";
  455. $sql.= " datec=".(dol_strlen($this->datec)!=0 ? "'".$this->db->idate($this->datec)."'" : 'null').",";
  456. $sql.= " datef=".(dol_strlen($this->date)!=0 ? "'".$this->db->idate($this->date)."'" : 'null').",";
  457. if (dol_strlen($this->tms) != 0) $sql.= " tms=".(dol_strlen($this->tms)!=0 ? "'".$this->db->idate($this->tms)."'" : 'null').",";
  458. $sql.= " libelle=".(isset($this->label)?"'".$this->db->escape($this->label)."'":"null").",";
  459. $sql.= " paye=".(isset($this->paye)?$this->paye:"null").",";
  460. $sql.= " amount=".(isset($this->amount)?$this->amount:"null").",";
  461. $sql.= " remise=".(isset($this->remise)?$this->remise:"null").",";
  462. $sql.= " close_code=".(isset($this->close_code)?"'".$this->db->escape($this->close_code)."'":"null").",";
  463. $sql.= " close_note=".(isset($this->close_note)?"'".$this->db->escape($this->close_note)."'":"null").",";
  464. $sql.= " tva=".(isset($this->tva)?$this->tva:"null").",";
  465. $sql.= " localtax1=".(isset($this->localtax1)?$this->localtax1:"null").",";
  466. $sql.= " localtax2=".(isset($this->localtax2)?$this->localtax2:"null").",";
  467. $sql.= " total=".(isset($this->total)?$this->total:"null").",";
  468. $sql.= " total_ht=".(isset($this->total_ht)?$this->total_ht:"null").",";
  469. $sql.= " total_tva=".(isset($this->total_tva)?$this->total_tva:"null").",";
  470. $sql.= " total_ttc=".(isset($this->total_ttc)?$this->total_ttc:"null").",";
  471. $sql.= " fk_statut=".(isset($this->statut)?$this->statut:"null").",";
  472. $sql.= " fk_user_author=".(isset($this->author)?$this->author:"null").",";
  473. $sql.= " fk_user_valid=".(isset($this->fk_user_valid)?$this->fk_user_valid:"null").",";
  474. $sql.= " fk_facture_source=".(isset($this->fk_facture_source)?$this->fk_facture_source:"null").",";
  475. $sql.= " fk_projet=".(isset($this->fk_project)?$this->fk_project:"null").",";
  476. $sql.= " fk_cond_reglement=".(isset($this->fk_cond_reglement)?$this->fk_cond_reglement:"null").",";
  477. $sql.= " date_lim_reglement=".(dol_strlen($this->date_echeance)!=0 ? "'".$this->db->idate($this->date_echeance)."'" : 'null').",";
  478. $sql.= " note=".(isset($this->note)?"'".$this->db->escape($this->note)."'":"null").",";
  479. $sql.= " note_public=".(isset($this->note_public)?"'".$this->db->escape($this->note_public)."'":"null").",";
  480. $sql.= " model_pdf=".(isset($this->model_pdf)?"'".$this->db->escape($this->model_pdf)."'":"null").",";
  481. $sql.= " import_key=".(isset($this->import_key)?"'".$this->db->escape($this->import_key)."'":"null")."";
  482. $sql.= " WHERE rowid=".$this->id;
  483. $this->db->begin();
  484. dol_syslog(get_class($this)."::update sql=".$sql, LOG_DEBUG);
  485. $resql = $this->db->query($sql);
  486. if (! $resql) { $error++; $this->errors[]="Error ".$this->db->lasterror(); }
  487. if (! $error)
  488. {
  489. if (! $notrigger)
  490. {
  491. // Call triggers
  492. //include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  493. //$interface=new Interfaces($this->db);
  494. //$result=$interface->run_triggers('BILL_SUPPLIER_MODIFY',$this,$user,$langs,$conf);
  495. //if ($result < 0) { $error++; $this->errors=$interface->errors; }
  496. // End call triggers
  497. }
  498. }
  499. // Commit or rollback
  500. if ($error)
  501. {
  502. foreach($this->errors as $errmsg)
  503. {
  504. dol_syslog(get_class($this)."::update ".$errmsg, LOG_ERR);
  505. $this->error.=($this->error?', '.$errmsg:$errmsg);
  506. }
  507. $this->db->rollback();
  508. return -1*$error;
  509. }
  510. else
  511. {
  512. $this->db->commit();
  513. return 1;
  514. }
  515. }
  516. /**
  517. * Delete invoice from database
  518. *
  519. * @param int $rowid Id of invoice to delete
  520. * @return int <0 if KO, >0 if OK
  521. */
  522. function delete($rowid)
  523. {
  524. global $user,$langs,$conf;
  525. if (! $rowid) $rowid=$this->id;
  526. dol_syslog("FactureFournisseur::delete rowid=".$rowid, LOG_DEBUG);
  527. // TODO Test if there is at least on payment. If yes, refuse to delete.
  528. $error=0;
  529. $this->db->begin();
  530. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_fourn_det WHERE fk_facture_fourn = '.$rowid.';';
  531. dol_syslog(get_class($this)."::delete sql=".$sql, LOG_DEBUG);
  532. $resql = $this->db->query($sql);
  533. if ($resql)
  534. {
  535. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_fourn WHERE rowid = '.$rowid;
  536. dol_syslog(get_class($this)."::delete sql=".$sql, LOG_DEBUG);
  537. $resql2 = $this->db->query($sql);
  538. if (! $resql2) {
  539. $error++;
  540. }
  541. }
  542. else {
  543. $error++;
  544. }
  545. if (! $error)
  546. {
  547. // Appel des triggers
  548. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  549. $interface=new Interfaces($this->db);
  550. $result=$interface->run_triggers('INVOICE_SUPPLIER_DELETE',$this,$user,$langs,$conf);
  551. if ($result < 0) {
  552. $error++; $this->errors=$interface->errors;
  553. }
  554. // Fin appel triggers
  555. }
  556. if (! $error)
  557. {
  558. // We remove directory
  559. if ($conf->fournisseur->facture->dir_output)
  560. {
  561. $ref = dol_sanitizeFileName($this->ref);
  562. $dir = $conf->fournisseur->facture->dir_output.'/'.get_exdir($this->id, 2).$ref;
  563. $file = $dir . "/" . $ref . ".pdf";
  564. if (file_exists($file))
  565. {
  566. if (! dol_delete_file($file,0,0,0,$this)) // For triggers
  567. {
  568. $this->error='ErrorFailToDeleteFile';
  569. $error++;
  570. }
  571. }
  572. if (file_exists($dir))
  573. {
  574. $res=@dol_delete_dir_recursive($dir);
  575. if (! $res)
  576. {
  577. $this->error='ErrorFailToDeleteDir';
  578. $error++;
  579. }
  580. }
  581. }
  582. }
  583. if (! $error)
  584. {
  585. dol_syslog(get_class($this)."::delete $this->id by $user->id", LOG_DEBUG);
  586. $this->db->commit();
  587. return 1;
  588. }
  589. else
  590. {
  591. $this->error=$this->db->lasterror();
  592. dol_syslog(get_class($this)."::delete ".$this->error, LOG_ERR);
  593. $this->db->rollback();
  594. return -$error;
  595. }
  596. }
  597. /**
  598. * Set supplier ref
  599. *
  600. * @param User $user User that make change
  601. * @param string $ref_supplier Supplier ref
  602. * @return int <0 if KO, >0 if OK
  603. */
  604. function set_ref_supplier($user, $ref_supplier)
  605. {
  606. if ($user->rights->fournisseur->facture->creer)
  607. {
  608. $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture_fourn SET facnumber = '.(empty($ref_supplier) ? 'NULL' : '\''.$this->db->escape($ref_supplier).'\'');
  609. $sql.= ' WHERE rowid = '.$this->id;
  610. dol_syslog("FactureFournisseur::set_ref_supplier sql=".$sql);
  611. if ($this->db->query($sql))
  612. {
  613. $this->ref_supplier = $ref_supplier;
  614. return 1;
  615. }
  616. else
  617. {
  618. $this->error=$this->db->lasterror();
  619. dol_syslog('FactureFournisseur::set_ref_supplier '.$this->error.' - '.$sql, LOG_ERR);
  620. return -2;
  621. }
  622. }
  623. else
  624. {
  625. return -1;
  626. }
  627. }
  628. /**
  629. * Tag invoice as a payed invoice
  630. *
  631. * @param User $user Object user
  632. * @return int <0 si ko, >0 si ok
  633. */
  634. function set_paid($user)
  635. {
  636. global $conf,$langs;
  637. $error=0;
  638. $this->db->begin();
  639. $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture_fourn';
  640. $sql.= ' SET paye = 1, fk_statut=2';
  641. $sql.= ' WHERE rowid = '.$this->id;
  642. dol_syslog("FactureFournisseur::set_paid sql=".$sql);
  643. $resql = $this->db->query($sql);
  644. if ($resql)
  645. {
  646. // Appel des triggers
  647. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  648. $interface=new Interfaces($this->db);
  649. $result=$interface->run_triggers('BILL_SUPPLIER_PAYED',$this,$user,$langs,$conf);
  650. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  651. // Fin appel triggers
  652. }
  653. else
  654. {
  655. $error++;
  656. $this->error=$this->db->error();
  657. dol_print_error($this->db);
  658. }
  659. if (! $error)
  660. {
  661. $this->db->commit();
  662. return 1;
  663. }
  664. else
  665. {
  666. $this->db->rollback();
  667. return -1;
  668. }
  669. }
  670. /**
  671. * Tag la facture comme non payee completement + appel trigger BILL_UNPAYED
  672. * Fonction utilisee quand un paiement prelevement est refuse,
  673. * ou quand une facture annulee et reouverte.
  674. *
  675. * @param User $user Object user that change status
  676. * @return int <0 si ok, >0 si ok
  677. */
  678. function set_unpaid($user)
  679. {
  680. global $conf,$langs;
  681. $error=0;
  682. $this->db->begin();
  683. $sql = 'UPDATE '.MAIN_DB_PREFIX.'facture_fourn';
  684. $sql.= ' SET paye=0, fk_statut=1, close_code=null, close_note=null';
  685. $sql.= ' WHERE rowid = '.$this->id;
  686. dol_syslog("FactureFournisseur::set_unpaid sql=".$sql);
  687. $resql = $this->db->query($sql);
  688. if ($resql)
  689. {
  690. // Appel des triggers
  691. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  692. $interface=new Interfaces($this->db);
  693. $result=$interface->run_triggers('BILL_SUPPLIER_UNPAYED',$this,$user,$langs,$conf);
  694. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  695. // Fin appel triggers
  696. }
  697. else
  698. {
  699. $error++;
  700. $this->error=$this->db->lasterror();
  701. dol_syslog("FactureFournisseur::set_unpaid ".$this->error);
  702. }
  703. if (! $error)
  704. {
  705. $this->db->commit();
  706. return 1;
  707. }
  708. else
  709. {
  710. $this->db->rollback();
  711. return -1;
  712. }
  713. }
  714. /**
  715. * Tag invoice as validated + call trigger BILL_VALIDATE
  716. *
  717. * @param User $user Object user that validate
  718. * @param string $force_number Reference to force on invoice
  719. * @param int $idwarehouse Id of warehouse for stock change
  720. * @return int <0 if KO, =0 if nothing to do, >0 if OK
  721. */
  722. function validate($user, $force_number='', $idwarehouse=0)
  723. {
  724. global $conf,$langs;
  725. $error=0;
  726. // Protection
  727. if ($this->statut > 0) // This is to avoid to validate twice (avoid errors on logs and stock management)
  728. {
  729. dol_syslog(get_class($this)."::validate no draft status", LOG_WARNING);
  730. return 0;
  731. }
  732. // Check parameters
  733. if (preg_match('/^'.preg_quote($langs->trans("CopyOf").' ').'/', $this->ref_supplier))
  734. {
  735. $this->error=$langs->trans("ErrorFieldFormat",$langs->transnoentities("RefSupplier"));
  736. return -1;
  737. }
  738. $this->db->begin();
  739. // Define new ref
  740. if ($force_number)
  741. {
  742. $num = $force_number;
  743. }
  744. else if (preg_match('/^[\(]?PROV/i', $this->ref))
  745. {
  746. $num = $this->getNextNumRef($this->client);
  747. }
  748. else
  749. {
  750. $num = $this->ref;
  751. }
  752. $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn";
  753. $sql.= " SET fk_statut = 1, fk_user_valid = ".$user->id;
  754. $sql.= " WHERE rowid = ".$this->id;
  755. dol_syslog(get_class($this)."::validate sql=".$sql);
  756. $resql = $this->db->query($sql);
  757. if ($resql)
  758. {
  759. // Si on incrémente le produit principal et ses composants à la validation de facture fournisseur
  760. if (! $error && ! empty($conf->stock->enabled) && ! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL))
  761. {
  762. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
  763. $langs->load("agenda");
  764. $cpt=count($this->lines);
  765. for ($i = 0; $i < $cpt; $i++)
  766. {
  767. if ($this->lines[$i]->fk_product > 0)
  768. {
  769. $mouvP = new MouvementStock($this->db);
  770. // We increase stock for product
  771. $result=$mouvP->reception($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->pu_ht, $langs->trans("InvoiceValidatedInSpeedealing",$num));
  772. if ($result < 0) { $error++; }
  773. }
  774. }
  775. }
  776. if (! $error)
  777. {
  778. // Appel des triggers
  779. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  780. $interface=new Interfaces($this->db);
  781. $result=$interface->run_triggers('BILL_SUPPLIER_VALIDATE',$this,$user,$langs,$conf);
  782. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  783. // Fin appel triggers
  784. }
  785. if (! $error)
  786. {
  787. $this->db->commit();
  788. return 1;
  789. }
  790. else
  791. {
  792. $this->db->rollback();
  793. return -1;
  794. }
  795. }
  796. else
  797. {
  798. $this->error=$this->db->error();
  799. $this->db->rollback();
  800. return -1;
  801. }
  802. }
  803. /**
  804. * Set draft status
  805. *
  806. * @param User $user Object user that modify
  807. * @param int $idwarehouse Id warehouse to use for stock change.
  808. * @return int <0 if KO, >0 if OK
  809. */
  810. function set_draft($user, $idwarehouse=-1)
  811. {
  812. global $conf,$langs;
  813. $error=0;
  814. if ($this->statut == 0)
  815. {
  816. dol_syslog(get_class($this)."::set_draft already draft status", LOG_WARNING);
  817. return 0;
  818. }
  819. $this->db->begin();
  820. $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn";
  821. $sql.= " SET fk_statut = 0";
  822. $sql.= " WHERE rowid = ".$this->id;
  823. dol_syslog(get_class($this)."::set_draft sql=".$sql, LOG_DEBUG);
  824. $result=$this->db->query($sql);
  825. if ($result)
  826. {
  827. // Si on incremente le produit principal et ses composants a la validation de facture fournisseur, on decremente
  828. if ($result >= 0 && ! empty($conf->stock->enabled) && ! empty($conf->global->STOCK_CALCULATE_ON_SUPPLIER_BILL))
  829. {
  830. require_once DOL_DOCUMENT_ROOT.'/product/stock/class/mouvementstock.class.php';
  831. $langs->load("agenda");
  832. $cpt=count($this->lines);
  833. for ($i = 0; $i < $cpt; $i++)
  834. {
  835. if ($this->lines[$i]->fk_product > 0)
  836. {
  837. $mouvP = new MouvementStock($this->db);
  838. // We increase stock for product
  839. $result=$mouvP->livraison($user, $this->lines[$i]->fk_product, $idwarehouse, $this->lines[$i]->qty, $this->lines[$i]->subprice, $langs->trans("InvoiceBackToDraftInSpeedealing",$this->ref));
  840. }
  841. }
  842. }
  843. if ($error == 0)
  844. {
  845. $this->db->commit();
  846. return 1;
  847. }
  848. else
  849. {
  850. $this->db->rollback();
  851. return -1;
  852. }
  853. }
  854. else
  855. {
  856. $this->error=$this->db->error();
  857. $this->db->rollback();
  858. return -1;
  859. }
  860. }
  861. /**
  862. * Ajoute une ligne de facture (associe a aucun produit/service predefini)
  863. * Les parametres sont deja cense etre juste et avec valeurs finales a l'appel
  864. * de cette methode. Aussi, pour le taux tva, il doit deja avoir ete defini
  865. * par l'appelant par la methode get_default_tva(societe_vendeuse,societe_acheteuse,idprod)
  866. * et le desc doit deja avoir la bonne valeur (a l'appelant de gerer le multilangue).
  867. *
  868. * @param string $desc Description de la ligne
  869. * @param double $pu Prix unitaire (HT ou TTC selon price_base_type, > 0 even for credit note)
  870. * @param double $txtva Taux de tva force, sinon -1
  871. * @param double $txlocaltax1 LocalTax1 Rate
  872. * @param double $txlocaltax2 LocalTax2 Rate
  873. * @param double $qty Quantite
  874. * @param int $fk_product Id du produit/service predefini
  875. * @param double $remise_percent Pourcentage de remise de la ligne
  876. * @param date $date_start Date de debut de validite du service
  877. * @param date $date_end Date de fin de validite du service
  878. * @param string $ventil Code de ventilation comptable
  879. * @param int $info_bits Bits de type de lines
  880. * @param string $price_base_type HT ou TTC
  881. * @param int $type Type of line (0=product, 1=service)
  882. * @param int $rang Position of line
  883. * @param int $notrigger Disable triggers
  884. * @return int >0 if OK, <0 if KO
  885. */
  886. 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)
  887. {
  888. 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", LOG_DEBUG);
  889. include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
  890. // Clean parameters
  891. if (empty($remise_percent)) $remise_percent=0;
  892. if (empty($qty)) $qty=0;
  893. if (empty($info_bits)) $info_bits=0;
  894. if (empty($rang)) $rang=0;
  895. if (empty($ventil)) $ventil=0;
  896. if (empty($txtva)) $txtva=0;
  897. if (empty($txlocaltax1)) $txlocaltax1=0;
  898. if (empty($txlocaltax2)) $txlocaltax2=0;
  899. $remise_percent=price2num($remise_percent);
  900. $qty=price2num($qty);
  901. $pu=price2num($pu);
  902. $txtva=price2num($txtva);
  903. $txlocaltax1=price2num($txlocaltax1);
  904. $txlocaltax2=price2num($txlocaltax2);
  905. // Check parameters
  906. if ($type < 0) return -1;
  907. $this->db->begin();
  908. $sql = 'INSERT INTO '.MAIN_DB_PREFIX.'facture_fourn_det (fk_facture_fourn)';
  909. $sql.= ' VALUES ('.$this->id.')';
  910. dol_syslog("Fournisseur.facture::addline sql=".$sql);
  911. $resql = $this->db->query($sql);
  912. if ($resql)
  913. {
  914. $idligne = $this->db->last_insert_id(MAIN_DB_PREFIX.'facture_fourn_det');
  915. $result=$this->updateline($idligne, $desc, $pu, $txtva, $txlocaltax1, $txlocaltax2, $qty, $fk_product, $price_base_type, $info_bits, $type, $remise_percent, true);
  916. if ($result > 0)
  917. {
  918. $this->rowid = $idligne;
  919. if (! $notrigger)
  920. {
  921. global $conf, $langs, $user;
  922. // Appel des triggers
  923. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  924. $interface=new Interfaces($this->db);
  925. $result=$interface->run_triggers('LINEBILL_SUPPLIER_CREATE',$this,$user,$langs,$conf);
  926. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  927. // Fin appel triggers
  928. }
  929. $this->db->commit();
  930. return 1;
  931. }
  932. else
  933. {
  934. dol_syslog("Error error=".$this->error, LOG_ERR);
  935. $this->db->rollback();
  936. return -1;
  937. }
  938. }
  939. else
  940. {
  941. $this->error=$this->db->lasterror();
  942. $this->db->rollback();
  943. return -2;
  944. }
  945. }
  946. /**
  947. * Update a line detail into database
  948. *
  949. * @param int $id Id of line invoice
  950. * @param string $label Description of line
  951. * @param double $pu Prix unitaire (HT ou TTC selon price_base_type)
  952. * @param double $vatrate VAT Rate
  953. * @param double $txlocaltax1 LocalTax1 Rate
  954. * @param double $txlocaltax2 LocalTax2 Rate
  955. * @param double $qty Quantity
  956. * @param int $idproduct Id produit
  957. * @param double $price_base_type HT or TTC
  958. * @param int $info_bits Miscellanous informations of line
  959. * @param int $type Type of line (0=product, 1=service)
  960. * @param double $remise_percent Pourcentage de remise de la ligne
  961. * @param int $notrigger Disable triggers
  962. * @return int <0 if KO, >0 if OK
  963. */
  964. function updateline($id, $label, $pu, $vatrate, $txlocaltax1=0, $txlocaltax2=0, $qty=1, $idproduct=0, $price_base_type='HT', $info_bits=0, $type=0, $remise_percent=0, $notrigger=false)
  965. {
  966. dol_syslog(get_class($this)."::updateline $id,$label,$pu,$vatrate,$qty,$idproduct,$price_base_type,$info_bits,$type,$remise_percent", LOG_DEBUG);
  967. include_once DOL_DOCUMENT_ROOT.'/core/lib/price.lib.php';
  968. $pu = price2num($pu);
  969. $qty = price2num($qty);
  970. $remise_percent=price2num($remise_percent);
  971. // Check parameters
  972. if (! is_numeric($pu) || ! is_numeric($qty)) return -1;
  973. if ($type < 0) return -1;
  974. // Clean parameters
  975. if (empty($txlocaltax1)) $txlocaltax1=0;
  976. if (empty($txlocaltax2)) $txlocaltax2=0;
  977. $txlocaltax1=price2num($txlocaltax1);
  978. $txlocaltax2=price2num($txlocaltax2);
  979. // Calcul du total TTC et de la TVA pour la ligne a partir de
  980. // qty, pu, remise_percent et txtva
  981. // TRES IMPORTANT: C'est au moment de l'insertion ligne qu'on doit stocker
  982. // la part ht, tva et ttc, et ce au niveau de la ligne qui a son propre taux tva.
  983. $tabprice = calcul_price_total($qty, $pu, $remise_percent, $vatrate, $txlocaltax1, $txlocaltax2, 0, $price_base_type, $info_bits);
  984. $total_ht = $tabprice[0];
  985. $total_tva = $tabprice[1];
  986. $total_ttc = $tabprice[2];
  987. $pu_ht = $tabprice[3];
  988. $pu_tva = $tabprice[4];
  989. $pu_ttc = $tabprice[5];
  990. $total_localtax1 = $tabprice[9];
  991. $total_localtax2 = $tabprice[10];
  992. if ($idproduct)
  993. {
  994. $product=new Product($this->db);
  995. $result=$product->fetch($idproduct);
  996. $product_type = $product->type;
  997. }
  998. else
  999. {
  1000. $product_type = $type;
  1001. }
  1002. $sql = "UPDATE ".MAIN_DB_PREFIX."facture_fourn_det SET";
  1003. $sql.= " description ='".$this->db->escape($label)."'";
  1004. $sql.= ", pu_ht = ".price2num($pu_ht);
  1005. $sql.= ", pu_ttc = ".price2num($pu_ttc);
  1006. $sql.= ", qty = ".price2num($qty);
  1007. $sql.= ", remise_percent = ".price2num($remise_percent);
  1008. $sql.= ", tva_tx = ".price2num($vatrate);
  1009. $sql.= ", localtax1_tx = ".price2num($txlocaltax1);
  1010. $sql.= ", localtax2_tx = ".price2num($txlocaltax2);
  1011. $sql.= ", total_ht = ".price2num($total_ht);
  1012. $sql.= ", tva= ".price2num($total_tva);
  1013. $sql.= ", total_localtax1= ".price2num($total_localtax1);
  1014. $sql.= ", total_localtax2= ".price2num($total_localtax2);
  1015. $sql.= ", total_ttc = ".price2num($total_ttc);
  1016. if ($idproduct) $sql.= ", fk_product = ".$idproduct;
  1017. else $sql.= ", fk_product = null";
  1018. $sql.= ", product_type = ".$product_type;
  1019. $sql.= " WHERE rowid = ".$id;
  1020. dol_syslog("Fournisseur.facture::updateline sql=".$sql);
  1021. $resql=$this->db->query($sql);
  1022. if ($resql)
  1023. {
  1024. $this->rowid = $id;
  1025. if (! $notrigger)
  1026. {
  1027. global $conf, $langs, $user;
  1028. // Appel des triggers
  1029. include_once DOL_DOCUMENT_ROOT . '/core/class/interfaces.class.php';
  1030. $interface=new Interfaces($this->db);
  1031. $result=$interface->run_triggers('LINEBILL_SUPPLIER_UPDATE',$this,$user,$langs,$conf);
  1032. if ($result < 0) { $error++; $this->errors=$interface->errors; }
  1033. // Fin appel triggers
  1034. }
  1035. // Update total price into invoice record
  1036. $result=$this->update_price();
  1037. return $result;
  1038. }
  1039. else
  1040. {
  1041. $this->error=$this->db->lasterror();
  1042. dol_syslog("Fournisseur.facture::updateline error=".$this->error, LOG_ERR);
  1043. return -1;
  1044. }
  1045. }
  1046. /**
  1047. * Delete a detail line from database
  1048. *
  1049. * @param int $rowid Id of line to delete
  1050. * @return void
  1051. */
  1052. function deleteline($rowid)
  1053. {
  1054. // Supprime ligne
  1055. $sql = 'DELETE FROM '.MAIN_DB_PREFIX.'facture_fourn_det ';
  1056. $sql .= ' WHERE rowid = '.$rowid.';';
  1057. $resql = $this->db->query($sql);
  1058. if (! $resql)
  1059. {
  1060. dol_print_error($this->db);
  1061. }
  1062. // Mise a jour prix facture
  1063. $this->update_price();
  1064. return 1;
  1065. }
  1066. /**
  1067. * Charge les informations d'ordre info dans l'objet facture
  1068. *
  1069. * @param int $id Id de la facture a charger
  1070. * @return void
  1071. */
  1072. function info($id)
  1073. {
  1074. $sql = 'SELECT c.rowid, datec, tms as datem,';
  1075. $sql.= ' fk_user_author, fk_user_valid';
  1076. $sql.= ' FROM '.MAIN_DB_PREFIX.'facture_fourn as c';
  1077. $sql.= ' WHERE c.rowid = '.$id;
  1078. $result=$this->db->query($sql);
  1079. if ($result)
  1080. {
  1081. if ($this->db->num_rows($result))
  1082. {
  1083. $obj = $this->db->fetch_object($result);
  1084. $this->id = $obj->rowid;
  1085. if ($obj->fk_user_author)
  1086. {
  1087. $cuser = new User($this->db);
  1088. $cuser->fetch($obj->fk_user_author);
  1089. $this->user_creation = $cuser;
  1090. }
  1091. if ($obj->fk_user_valid)
  1092. {
  1093. $vuser = new User($this->db);
  1094. $vuser->fetch($obj->fk_user_valid);
  1095. $this->user_validation = $vuser;
  1096. }
  1097. $this->date_creation = $obj->datec;
  1098. $this->date_modification = $obj->datem;
  1099. //$this->date_validation = $obj->datev; Should be stored in log table
  1100. }
  1101. $this->db->free($result);
  1102. }
  1103. else
  1104. {
  1105. dol_print_error($this->db);
  1106. }
  1107. }
  1108. /**
  1109. * Load indicators for dashboard (this->nbtodo and this->nbtodolate)
  1110. *
  1111. * @param User $user Object user
  1112. * @return int <0 if KO, >0 if OK
  1113. */
  1114. function load_board($user)
  1115. {
  1116. global $conf, $user;
  1117. $now=dol_now();
  1118. $this->nbtodo=$this->nbtodolate=0;
  1119. $sql = 'SELECT ff.rowid, ff.date_lim_reglement as datefin';
  1120. $sql.= ' FROM '.MAIN_DB_PREFIX.'facture_fourn as ff';
  1121. if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  1122. $sql.= ' WHERE ff.paye=0';
  1123. $sql.= ' AND ff.fk_statut > 0';
  1124. $sql.= " AND ff.entity = ".$conf->entity;
  1125. if ($user->societe_id) $sql.=' AND ff.fk_soc = '.$user->societe_id;
  1126. if (!$user->rights->societe->client->voir && !$user->societe_id) $sql.= " AND ff.fk_soc = sc.fk_soc AND sc.fk_user = ".$user->id;
  1127. $resql=$this->db->query($sql);
  1128. if ($resql)
  1129. {
  1130. while ($obj=$this->db->fetch_object($resql))
  1131. {
  1132. $this->nbtodo++;
  1133. if ($this->db->jdate($obj->datefin) < ($now - $conf->facture->fournisseur->warning_delay)) $this->nbtodolate++;
  1134. }
  1135. $this->db->free($resql);
  1136. return 1;
  1137. }
  1138. else
  1139. {
  1140. dol_print_error($this->db);
  1141. $this->error=$this->db->error();
  1142. return -1;
  1143. }
  1144. }
  1145. /**
  1146. * Renvoie nom clicable (avec eventuellement le picto)
  1147. *
  1148. * @param int $withpicto 0=Pas de picto, 1=Inclut le picto dans le lien, 2=Picto seul
  1149. * @param string $option Sur quoi pointe le lien
  1150. * @param int $max Max length of shown ref
  1151. * @return string Chaine avec URL
  1152. */
  1153. function getNomUrl($withpicto=0,$option='',$max=0)
  1154. {
  1155. global $langs;
  1156. $result='';
  1157. if ($option == 'document')
  1158. {
  1159. $lien = '<a href="'.DOL_URL_ROOT.'/fourn/facture/document.php?facid='.$this->id.'">';
  1160. $lienfin='</a>';
  1161. }
  1162. else
  1163. {
  1164. $lien = '<a href="'.DOL_URL_ROOT.'/fourn/facture/fiche.php?facid='.$this->id.'">';
  1165. $lienfin='</a>';
  1166. }
  1167. $label=$langs->trans("ShowInvoice").': '.$this->ref;
  1168. if ($this->ref_supplier) $label.=' / '.$this->ref_supplier;
  1169. if ($withpicto) $result.=($lien.img_object($label,'bill').$lienfin.' ');
  1170. $result.=$lien.($max?dol_trunc($this->ref,$max):$this->ref).$lienfin;
  1171. return $result;
  1172. }
  1173. /**
  1174. * Initialise an instance with random values.
  1175. * Used to build previews or test instances.
  1176. * id must be 0 if object instance is a specimen.
  1177. *
  1178. * @return void
  1179. */
  1180. function initAsSpecimen()
  1181. {
  1182. global $langs,$conf;
  1183. include_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  1184. $now = dol_now();
  1185. // Charge tableau des produits prodids
  1186. $prodids = array();
  1187. $sql = "SELECT rowid";
  1188. $sql.= " FROM ".MAIN_DB_PREFIX."product";
  1189. $sql.= " WHERE entity IN (".getEntity('product', 1).")";
  1190. $resql = $this->db->query($sql);
  1191. if ($resql)
  1192. {
  1193. $num_prods = $this->db->num_rows($resql);
  1194. $i = 0;

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