cashcontrol.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  1. <?php
  2. /* Copyright (C) 2008 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2009 Regis Houssin <regis.houssin@capnetworks.com>
  4. * Copyright (C) 2016 Marcos García <marcosgdf@gmail.com>
  5. * Copyright (C) 2018 Andreu Bisquerra <jove@bisquerra.com>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file cashcontrol/class/cashcontrol.class.php
  22. * \ingroup bank
  23. * \brief This file is CRUD class file (Create/Read/Update/Delete) for cash fence table
  24. */
  25. /**
  26. * Class to manage cash fence
  27. */
  28. class CashControl extends CommonObject
  29. {
  30. /**
  31. * @var string ID to identify managed object
  32. */
  33. public $element = 'cashcontrol';
  34. /**
  35. * @var string Name of table without prefix where object is stored
  36. */
  37. public $table_element = 'pos_cash_fence';
  38. /**
  39. * @var int Does pos_cash_fence support multicompany module ? 0=No test on entity, 1=Test with field entity, 2=Test with link by societe
  40. */
  41. public $ismultientitymanaged = 1;
  42. /**
  43. * @var int Does pos_cash_fence support extrafields ? 0=No, 1=Yes
  44. */
  45. public $isextrafieldmanaged = 0;
  46. /**
  47. * @var string String with name of icon for pos_cash_fence. Must be the part after the 'object_' into object_pos_cash_fence.png
  48. */
  49. public $picto = 'account';
  50. public $fields=array(
  51. 'rowid' =>array('type'=>'integer', 'label'=>'ID', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>10),
  52. 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'notnull'=>1, 'position'=>15),
  53. 'ref' =>array('type'=>'varchar(64)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>18),
  54. 'posmodule' =>array('type'=>'varchar(30)', 'label'=>'Module', 'enabled'=>1, 'visible'=>1, 'notnul'=>1, 'position'=>19),
  55. 'posnumber' =>array('type'=>'varchar(30)', 'label'=>'CashDesk', 'enabled'=>1, 'visible'=>1, 'notnul'=>1, 'position'=>20),
  56. 'label' =>array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>0, 'position'=>24),
  57. 'opening' =>array('type'=>'double(24,8)', 'label'=>'Opening', 'enabled'=>1, 'visible'=>1, 'position'=>25),
  58. 'cash' =>array('type'=>'double(24,8)', 'label'=>'Cash', 'enabled'=>1, 'visible'=>1, 'position'=>30),
  59. 'cheque' =>array('type'=>'double(24,8)', 'label'=>'Cheque', 'enabled'=>1, 'visible'=>1, 'position'=>33),
  60. 'card' =>array('type'=>'double(24,8)', 'label'=>'CreditCard', 'enabled'=>1, 'visible'=>1, 'position'=>36),
  61. 'year_close' =>array('type'=>'integer', 'label'=>'Year close', 'enabled'=>1, 'visible'=>1, 'notnul'=>1, 'position'=>50),
  62. 'month_close' =>array('type'=>'integer', 'label'=>'Month close', 'enabled'=>1, 'visible'=>1, 'position'=>55),
  63. 'day_close' =>array('type'=>'integer', 'label'=>'Day close', 'enabled'=>1, 'visible'=>1, 'position'=>60),
  64. 'date_valid' =>array('type'=>'datetime', 'label'=>'DateValid', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>490),
  65. 'date_creation' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>500),
  66. 'tms' =>array('type'=>'timestamp', 'label'=>'Tms', 'enabled'=>1, 'visible'=>0, 'notnull'=>1, 'position'=>505),
  67. 'import_key' =>array('type'=>'varchar(14)', 'label'=>'Import key', 'enabled'=>1, 'visible'=>0, 'position'=>510),
  68. 'status' => array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'position'=>1000, 'notnull'=>1, 'index'=>1, 'arrayofkeyval'=>array('0'=>'Brouillon', '1'=>'Validated')),
  69. );
  70. public $id;
  71. public $opening;
  72. public $status;
  73. public $year_close;
  74. public $month_close;
  75. public $day_close;
  76. public $posmodule;
  77. public $posnumber;
  78. public $cash;
  79. public $cheque;
  80. public $card;
  81. public $date_valid;
  82. public $date_creation;
  83. public $date_modification;
  84. const STATUS_DRAFT = 0;
  85. const STATUS_VALIDATED = 1;
  86. /**
  87. * Constructor
  88. *
  89. * @param DoliDB $db Database handler
  90. */
  91. public function __construct(DoliDB $db)
  92. {
  93. $this->db = $db;
  94. }
  95. /**
  96. * Create in database
  97. *
  98. * @param User $user User that create
  99. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  100. * @return int <0 if KO, Id of created object if OK
  101. */
  102. public function create(User $user, $notrigger = 0)
  103. {
  104. global $conf;
  105. $error = 0;
  106. // Clean data
  107. if (empty($this->cash)) $this->cash=0;
  108. if (empty($this->cheque)) $this->cheque=0;
  109. if (empty($this->card)) $this->card=0;
  110. // Insert request
  111. $sql = "INSERT INTO ".MAIN_DB_PREFIX."pos_cash_fence (";
  112. $sql .= "entity";
  113. //$sql .= ", ref";
  114. $sql .= ", opening";
  115. $sql .= ", status";
  116. $sql .= ", date_creation";
  117. $sql .= ", posmodule";
  118. $sql .= ", posnumber";
  119. $sql .= ", day_close";
  120. $sql .= ", month_close";
  121. $sql .= ", year_close";
  122. $sql .= ", cash";
  123. $sql .= ", cheque";
  124. $sql .= ", card";
  125. $sql .= ") VALUES (";
  126. //$sql .= "'(PROV)', ";
  127. $sql .= $conf->entity;
  128. $sql .= ", ".(is_numeric($this->opening) ? $this->opening : 0);
  129. $sql .= ", 0"; // Draft by default
  130. $sql .= ", '".$this->db->idate(dol_now())."'";
  131. $sql .= ", '".$this->db->escape($this->posmodule)."'";
  132. $sql .= ", '".$this->db->escape($this->posnumber)."'";
  133. $sql .= ", ".($this->day_close > 0 ? $this->day_close : "null");
  134. $sql .= ", ".($this->month_close > 0 ? $this->month_close : "null");
  135. $sql .= ", ".$this->year_close;
  136. $sql .= ", ".$this->cash;
  137. $sql .= ", ".$this->cheque;
  138. $sql .= ", ".$this->card;
  139. $sql .= ")";
  140. $this->db->begin();
  141. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  142. $resql = $this->db->query($sql);
  143. if (!$resql) {
  144. $error++;
  145. $this->errors[] = "Error ".$this->db->lasterror();
  146. }
  147. if (!$error) {
  148. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."pos_cash_fence");
  149. $sql = 'UPDATE '.MAIN_DB_PREFIX.'pos_cash_fence SET ref = rowid where rowid = '.$this->id;
  150. $this->db->query($sql);
  151. }
  152. // Commit or rollback
  153. if ($error) {
  154. foreach ($this->errors as $errmsg) {
  155. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  156. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  157. }
  158. $this->db->rollback();
  159. return -1 * $error;
  160. } else {
  161. $this->db->commit();
  162. return $this->id;
  163. }
  164. }
  165. /**
  166. * Validate cash fence
  167. *
  168. * @param User $user User
  169. * @param number $notrigger No trigger
  170. * @return int <0 if KO, >0 if OK
  171. */
  172. public function valid(User $user, $notrigger = 0)
  173. {
  174. global $conf,$langs;
  175. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  176. $error = 0;
  177. // Protection
  178. if ($this->status == self::STATUS_VALIDATED)
  179. {
  180. dol_syslog(get_class($this)."::valid action abandonned: already validated", LOG_WARNING);
  181. return 0;
  182. }
  183. /*
  184. $posmodule = $this->posmodule;
  185. if (! empty($user->rights->$posmodule->use))
  186. {
  187. $this->error='NotEnoughPermissions';
  188. dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
  189. return -1;
  190. }
  191. */
  192. $now=dol_now();
  193. // Update request
  194. $sql = "UPDATE ".MAIN_DB_PREFIX."pos_cash_fence";
  195. $sql.= " SET status = ".self::STATUS_VALIDATED.",";
  196. $sql.= " date_valid='".$this->db->idate($now)."',";
  197. $sql.= " fk_user_valid = ".$user->id;
  198. $sql.= " WHERE rowid=".$this->id;
  199. $this->db->begin();
  200. dol_syslog(get_class($this)."::close", LOG_DEBUG);
  201. $resql = $this->db->query($sql);
  202. if (!$resql) {
  203. $error++;
  204. $this->errors[] = "Error ".$this->db->lasterror();
  205. }
  206. if (!$error) {
  207. $this->status = self::STATUS_VALIDATED;
  208. $this->date_valid = $now;
  209. $this->fk_user_valid = $user->id;
  210. }
  211. if (! $error && ! $notrigger)
  212. {
  213. // Call trigger
  214. $result=$this->call_trigger('CASHCONTROL_VALIDATE', $user);
  215. if ($result < 0) $error++;
  216. // End call triggers
  217. }
  218. // Commit or rollback
  219. if ($error) {
  220. foreach ($this->errors as $errmsg) {
  221. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  222. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  223. }
  224. $this->db->rollback();
  225. return -1 * $error;
  226. } else {
  227. $this->db->commit();
  228. return $this->id;
  229. }
  230. }
  231. /**
  232. * Load object in memory from the database
  233. *
  234. * @param int $id Id object
  235. * @param string $ref Ref
  236. * @return int <0 if KO, 0 if not found, >0 if OK
  237. */
  238. public function fetch($id, $ref = null)
  239. {
  240. $result = $this->fetchCommon($id, $ref);
  241. if ($result > 0 && ! empty($this->table_element_line)) $this->fetchLines();
  242. return $result;
  243. }
  244. /**
  245. * Delete object in database
  246. *
  247. * @param User $user User that deletes
  248. * @param bool $notrigger false=launch triggers after, true=disable triggers
  249. * @return int <0 if KO, >0 if OK
  250. */
  251. public function delete(User $user, $notrigger = false)
  252. {
  253. return $this->deleteCommon($user, $notrigger);
  254. //return $this->deleteCommon($user, $notrigger, 1);
  255. }
  256. /**
  257. * Return label of the status
  258. *
  259. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  260. * @return string Label of status
  261. */
  262. public function getLibStatut($mode=0)
  263. {
  264. return $this->LibStatut($this->status, $mode);
  265. }
  266. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  267. /**
  268. * Return the status
  269. *
  270. * @param int $status Id status
  271. * @param int $mode 0=long label, 1=short label, 2=Picto + short label, 3=Picto, 4=Picto + long label, 5=Short label + Picto, 6=Long label + Picto
  272. * @return string Label of status
  273. */
  274. public function LibStatut($status, $mode=0)
  275. {
  276. // phpcs:enable
  277. if (empty($this->labelstatus))
  278. {
  279. global $langs;
  280. //$langs->load("mymodule");
  281. $this->labelstatus[0] = $langs->trans('Draft');
  282. $this->labelstatus[1] = $langs->trans('Closed');
  283. }
  284. if ($mode == 0)
  285. {
  286. return $this->labelstatus[$status];
  287. }
  288. elseif ($mode == 1)
  289. {
  290. return $this->labelstatus[$status];
  291. }
  292. elseif ($mode == 2)
  293. {
  294. if ($status == 1) return img_picto($this->labelstatus[$status],'statut6', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelstatus[$status];
  295. elseif ($status == 0) return img_picto($this->labelstatus[$status],'statut0', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelstatus[$status];
  296. }
  297. elseif ($mode == 3)
  298. {
  299. if ($status == 1) return img_picto($this->labelstatus[$status],'statut6', '', false, 0, 0, '', 'valignmiddle');
  300. elseif ($status == 0) return img_picto($this->labelstatus[$status],'statut0', '', false, 0, 0, '', 'valignmiddle');
  301. }
  302. elseif ($mode == 4)
  303. {
  304. if ($status == 1) return img_picto($this->labelstatus[$status],'statut6', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelstatus[$status];
  305. elseif ($status == 0) return img_picto($this->labelstatus[$status],'statut0', '', false, 0, 0, '', 'valignmiddle').' '.$this->labelstatus[$status];
  306. }
  307. elseif ($mode == 5)
  308. {
  309. if ($status == 1) return $this->labelstatus[$status].' '.img_picto($this->labelstatus[$status],'statut6', '', false, 0, 0, '', 'valignmiddle');
  310. elseif ($status == 0) return $this->labelstatus[$status].' '.img_picto($this->labelstatus[$status],'statut0', '', false, 0, 0, '', 'valignmiddle');
  311. }
  312. elseif ($mode == 6)
  313. {
  314. if ($status == 1) return $this->labelstatus[$status].' '.img_picto($this->labelstatus[$status],'statut6', '', false, 0, 0, '', 'valignmiddle');
  315. elseif ($status == 0) return $this->labelstatus[$status].' '.img_picto($this->labelstatus[$status],'statut0', '', false, 0, 0, '', 'valignmiddle');
  316. }
  317. }
  318. /**
  319. * Return clicable link of object (with eventually picto)
  320. *
  321. * @param int $withpicto Add picto into link
  322. * @param string $option Where point the link ('stock', 'composition', 'category', 'supplier', '')
  323. * @param int $maxlength Maxlength of ref
  324. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  325. * @param int $notooltip No tooltip
  326. * @return string String with URL
  327. */
  328. public function getNomUrl($withpicto=0, $option='', $maxlength=0, $save_lastsearch_value=-1, $notooltip=0)
  329. {
  330. global $conf, $langs, $hookmanager;
  331. include_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
  332. $result='';
  333. $newref=($this->ref?$this->ref:$this->id);
  334. if ($maxlength) { $newref=dol_trunc($newref, $maxlength, 'middle'); }
  335. $label = '<u>' . $langs->trans("ShowCashFence") . '</u>';
  336. $label .= '<br><b>' . $langs->trans('ProductRef') . ':</b> ' . ($this->ref?$this->ref:$this->id);
  337. $linkclose='';
  338. if (empty($notooltip)) {
  339. if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  340. $label=$langs->trans("ShowCashFence");
  341. $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
  342. }
  343. $linkclose.= ' title="'.dol_escape_htmltag($label, 1, 1).'"';
  344. $linkclose.= ' class="classfortooltip"';
  345. /*
  346. $hookmanager->initHooks(array('productdao'));
  347. $parameters=array('id'=>$this->id);
  348. $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
  349. if ($reshook > 0) $linkclose = $hookmanager->resPrint;
  350. */
  351. }
  352. $url = DOL_URL_ROOT.'/compta/cashcontrol/cashcontrol_card.php?id='.$this->id;
  353. if ($option !== 'nolink') {
  354. // Add param to save lastsearch_values or not
  355. $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
  356. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) { $add_save_lastsearch_values=1;
  357. }
  358. if ($add_save_lastsearch_values) { $url.='&save_lastsearch_values=1';
  359. }
  360. }
  361. $linkstart = '<a href="'.$url.'"';
  362. $linkstart.=$linkclose.'>';
  363. $linkend='</a>';
  364. $result.=$linkstart;
  365. if ($withpicto) {
  366. $result.=(img_object(($notooltip?'':$label), 'bank', ($notooltip?'class="paddingright"':'class="paddingright classfortooltip"'), 0, 0, $notooltip?0:1));
  367. }
  368. $result.= $newref;
  369. $result.= $linkend;
  370. global $action;
  371. $hookmanager->initHooks(array('cashfencedao'));
  372. $parameters=array('id'=>$this->id, 'getnomurl'=>$result);
  373. $reshook=$hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  374. if ($reshook > 0) { $result = $hookmanager->resPrint;
  375. } else { $result .= $hookmanager->resPrint;
  376. }
  377. return $result;
  378. }
  379. }