cashcontrol.class.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  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 <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/compta/cashcontrol/class/cashcontrol.class.php
  22. * \ingroup cashdesk|takepos
  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 = 'cash-register';
  50. /**
  51. * 'type' field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]', 'sellist:TableName:LabelFieldName[:KeyFieldName[:KeyFieldParent[:Filter]]]', 'varchar(x)', 'double(24,8)', 'real', 'price', 'text', 'text:none', 'html', 'date', 'datetime', 'timestamp', 'duration', 'mail', 'phone', 'url', 'password')
  52. * Note: Filter can be a string like "(t.ref:like:'SO-%') or (t.date_creation:<:'20160101') or (t.nature:is:NULL)"
  53. * 'label' the translation key.
  54. * 'picto' is code of a picto to show before value in forms
  55. * 'enabled' is a condition when the field must be managed (Example: 1 or '$conf->global->MY_SETUP_PARAM)
  56. * 'position' is the sort order of field.
  57. * 'notnull' is set to 1 if not null in database. Set to -1 if we must set data to null if empty ('' or 0).
  58. * 'visible' says if field is visible in list (Examples: 0=Not visible, 1=Visible on list and create/update/view forms, 2=Visible on list only, 3=Visible on create/update/view form only (not list), 4=Visible on list and update/view form only (not create). 5=Visible on list and view only (not create/not update). Using a negative value means field is not shown by default on list but can be selected for viewing)
  59. * 'noteditable' says if field is not editable (1 or 0)
  60. * 'default' is a default value for creation (can still be overwrote by the Setup of Default Values if field is editable in creation form). Note: If default is set to '(PROV)' and field is 'ref', the default value will be set to '(PROVid)' where id is rowid when a new record is created.
  61. * 'index' if we want an index in database.
  62. * 'foreignkey'=>'tablename.field' if the field is a foreign key (it is recommanded to name the field fk_...).
  63. * 'searchall' is 1 if we want to search in this field when making a search from the quick search button.
  64. * 'isameasure' must be set to 1 if you want to have a total on list for this field. Field type must be summable like integer or double(24,8).
  65. * 'css' and 'cssview' and 'csslist' is the CSS style to use on field. 'css' is used in creation and update. 'cssview' is used in view mode. 'csslist' is used for columns in lists. For example: 'maxwidth200', 'wordbreak', 'tdoverflowmax200'
  66. * 'help' is a 'TranslationString' to use to show a tooltip on field. You can also use 'TranslationString:keyfortooltiponlick' for a tooltip on click.
  67. * 'showoncombobox' if value of the field must be visible into the label of the combobox that list record
  68. * 'disabled' is 1 if we want to have the field locked by a 'disabled' attribute. In most cases, this is never set into the definition of $fields into class, but is set dynamically by some part of code.
  69. * 'arrayofkeyval' to set list of value if type is a list of predefined values. For example: array("0"=>"Draft","1"=>"Active","-1"=>"Cancel")
  70. * 'autofocusoncreate' to have field having the focus on a create form. Only 1 field should have this property set to 1.
  71. * 'comment' is not used. You can store here any text of your choice. It is not used by application.
  72. *
  73. * Note: To have value dynamic, you can set value to 0 in definition and edit the value on the fly into the constructor.
  74. */
  75. public $fields = array(
  76. 'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>10),
  77. 'entity' =>array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'notnull'=>1, 'position'=>15),
  78. 'ref' =>array('type'=>'varchar(64)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>18),
  79. 'posmodule' =>array('type'=>'varchar(30)', 'label'=>'Module', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>19),
  80. 'posnumber' =>array('type'=>'varchar(30)', 'label'=>'Terminal', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>20, 'css'=>'center'),
  81. 'label' =>array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>0, 'position'=>24),
  82. 'opening' =>array('type'=>'price', 'label'=>'Opening', 'enabled'=>1, 'visible'=>1, 'position'=>25, 'csslist'=>'amount'),
  83. 'cash' =>array('type'=>'price', 'label'=>'Cash', 'enabled'=>1, 'visible'=>1, 'position'=>30, 'csslist'=>'amount'),
  84. 'cheque' =>array('type'=>'price', 'label'=>'Cheque', 'enabled'=>1, 'visible'=>1, 'position'=>33, 'csslist'=>'amount'),
  85. 'card' =>array('type'=>'price', 'label'=>'CreditCard', 'enabled'=>1, 'visible'=>1, 'position'=>36, 'csslist'=>'amount'),
  86. 'year_close' =>array('type'=>'integer', 'label'=>'Year close', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>50, 'css'=>'center'),
  87. 'month_close' =>array('type'=>'integer', 'label'=>'Month close', 'enabled'=>1, 'visible'=>1, 'position'=>55, 'css'=>'center'),
  88. 'day_close' =>array('type'=>'integer', 'label'=>'Day close', 'enabled'=>1, 'visible'=>1, 'position'=>60, 'css'=>'center'),
  89. 'date_creation' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>500),
  90. 'date_valid' =>array('type'=>'datetime', 'label'=>'DateValidation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>502),
  91. 'tms' =>array('type'=>'timestamp', 'label'=>'Tms', 'enabled'=>1, 'visible'=>0, 'notnull'=>1, 'position'=>505),
  92. 'fk_user_creat' =>array('type'=>'integer:User', 'label'=>'UserCreation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>600),
  93. 'fk_user_valid' =>array('type'=>'integer:User', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>602),
  94. 'import_key' =>array('type'=>'varchar(14)', 'label'=>'Import key', 'enabled'=>1, 'visible'=>0, 'position'=>700),
  95. 'status' => array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'position'=>1000, 'notnull'=>1, 'index'=>1, 'arrayofkeyval'=>array('0'=>'Draft', '1'=>'Validated')),
  96. );
  97. /**
  98. * @var int Object Id
  99. */
  100. public $id;
  101. public $opening;
  102. public $status;
  103. public $year_close;
  104. public $month_close;
  105. public $day_close;
  106. public $posmodule;
  107. public $posnumber;
  108. public $cash;
  109. public $cheque;
  110. public $card;
  111. /**
  112. * @var integer|string date_creation
  113. */
  114. public $date_creation;
  115. public $fk_user_creat;
  116. /**
  117. * @var integer|string $date_modification
  118. */
  119. public $date_modification;
  120. /**
  121. * @var integer|string $date_valid
  122. */
  123. public $date_valid;
  124. public $fk_user_valid;
  125. const STATUS_DRAFT = 0;
  126. const STATUS_VALIDATED = 1;
  127. const STATUS_CLOSED = 1; // For the moment CLOSED = VALIDATED
  128. /**
  129. * Constructor
  130. *
  131. * @param DoliDB $db Database handler
  132. */
  133. public function __construct(DoliDB $db)
  134. {
  135. $this->db = $db;
  136. }
  137. /**
  138. * Create in database
  139. *
  140. * @param User $user User that create
  141. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  142. * @return int <0 if KO, Id of created object if OK
  143. */
  144. public function create(User $user, $notrigger = 0)
  145. {
  146. global $conf;
  147. $error = 0;
  148. // Clean data
  149. if (empty($this->cash)) {
  150. $this->cash = 0;
  151. }
  152. if (empty($this->cheque)) {
  153. $this->cheque = 0;
  154. }
  155. if (empty($this->card)) {
  156. $this->card = 0;
  157. }
  158. // Insert request
  159. $sql = "INSERT INTO ".MAIN_DB_PREFIX."pos_cash_fence (";
  160. $sql .= "entity";
  161. //$sql .= ", ref";
  162. $sql .= ", opening";
  163. $sql .= ", status";
  164. $sql .= ", date_creation";
  165. $sql .= ", posmodule";
  166. $sql .= ", posnumber";
  167. $sql .= ", day_close";
  168. $sql .= ", month_close";
  169. $sql .= ", year_close";
  170. $sql .= ", cash";
  171. $sql .= ", cheque";
  172. $sql .= ", card";
  173. $sql .= ") VALUES (";
  174. //$sql .= "'(PROV)', ";
  175. $sql .= ((int) $conf->entity);
  176. $sql .= ", ".(is_numeric($this->opening) ? price2num($this->opening, 'MT') : 0);
  177. $sql .= ", 0"; // Draft by default
  178. $sql .= ", '".$this->db->idate(dol_now())."'";
  179. $sql .= ", '".$this->db->escape($this->posmodule)."'";
  180. $sql .= ", '".$this->db->escape($this->posnumber)."'";
  181. $sql .= ", ".($this->day_close > 0 ? $this->day_close : "null");
  182. $sql .= ", ".($this->month_close > 0 ? $this->month_close : "null");
  183. $sql .= ", ".((int) $this->year_close);
  184. $sql .= ", ".price2num($this->cash, 'MT');
  185. $sql .= ", ".price2num($this->cheque, 'MT');
  186. $sql .= ", ".price2num($this->card, 'MT');
  187. $sql .= ")";
  188. $this->db->begin();
  189. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  190. $resql = $this->db->query($sql);
  191. if (!$resql) {
  192. $error++;
  193. $this->errors[] = "Error ".$this->db->lasterror();
  194. }
  195. if (!$error) {
  196. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."pos_cash_fence");
  197. $sql = 'UPDATE '.MAIN_DB_PREFIX.'pos_cash_fence SET ref = rowid where rowid = '.((int) $this->id);
  198. $this->db->query($sql);
  199. }
  200. // Commit or rollback
  201. if ($error) {
  202. foreach ($this->errors as $errmsg) {
  203. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  204. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  205. }
  206. $this->db->rollback();
  207. return -1 * $error;
  208. } else {
  209. $this->db->commit();
  210. return $this->id;
  211. }
  212. }
  213. /**
  214. * Validate cash fence
  215. *
  216. * @param User $user User
  217. * @param number $notrigger No trigger
  218. * @return int <0 if KO, >0 if OK
  219. */
  220. public function valid(User $user, $notrigger = 0)
  221. {
  222. global $conf, $langs;
  223. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  224. $error = 0;
  225. // Protection
  226. if ($this->status == self::STATUS_VALIDATED) {
  227. dol_syslog(get_class($this)."::valid action abandonned: already validated", LOG_WARNING);
  228. return 0;
  229. }
  230. /*
  231. $posmodule = $this->posmodule;
  232. if (!empty($user->rights->$posmodule->use))
  233. {
  234. $this->error='NotEnoughPermissions';
  235. dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
  236. return -1;
  237. }
  238. */
  239. $now = dol_now();
  240. // Update request
  241. $sql = "UPDATE ".MAIN_DB_PREFIX."pos_cash_fence";
  242. $sql .= " SET status = ".self::STATUS_VALIDATED.",";
  243. $sql .= " date_valid='".$this->db->idate($now)."',";
  244. $sql .= " fk_user_valid = ".$user->id;
  245. $sql .= " WHERE rowid=".((int) $this->id);
  246. $this->db->begin();
  247. dol_syslog(get_class($this)."::close", LOG_DEBUG);
  248. $resql = $this->db->query($sql);
  249. if (!$resql) {
  250. $error++;
  251. $this->errors[] = "Error ".$this->db->lasterror();
  252. }
  253. if (!$error) {
  254. $this->status = self::STATUS_VALIDATED;
  255. $this->date_valid = $now;
  256. $this->fk_user_valid = $user->id;
  257. }
  258. if (!$error && !$notrigger) {
  259. // Call trigger
  260. $result = $this->call_trigger('CASHCONTROL_VALIDATE', $user);
  261. if ($result < 0) {
  262. $error++;
  263. }
  264. // End call triggers
  265. }
  266. // Commit or rollback
  267. if ($error) {
  268. foreach ($this->errors as $errmsg) {
  269. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  270. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  271. }
  272. $this->db->rollback();
  273. return -1 * $error;
  274. } else {
  275. $this->db->commit();
  276. return $this->id;
  277. }
  278. }
  279. /**
  280. * Load object in memory from the database
  281. *
  282. * @param int $id Id object
  283. * @param string $ref Ref
  284. * @return int <0 if KO, 0 if not found, >0 if OK
  285. */
  286. public function fetch($id, $ref = null)
  287. {
  288. $result = $this->fetchCommon($id, $ref);
  289. //if ($result > 0 && !empty($this->table_element_line)) $this->fetchLines();
  290. return $result;
  291. }
  292. /**
  293. * Update object into database
  294. *
  295. * @param User $user User that modifies
  296. * @param bool $notrigger false=launch triggers after, true=disable triggers
  297. * @return int <0 if KO, >0 if OK
  298. */
  299. public function update(User $user, $notrigger = false)
  300. {
  301. return $this->updateCommon($user, $notrigger);
  302. }
  303. /**
  304. * Delete object in database
  305. *
  306. * @param User $user User that deletes
  307. * @param bool $notrigger false=launch triggers after, true=disable triggers
  308. * @return int <0 if KO, >0 if OK
  309. */
  310. public function delete(User $user, $notrigger = false)
  311. {
  312. return $this->deleteCommon($user, $notrigger);
  313. //return $this->deleteCommon($user, $notrigger, 1);
  314. }
  315. /**
  316. * Return label of the status
  317. *
  318. * @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
  319. * @return string Label of status
  320. */
  321. public function getLibStatut($mode = 0)
  322. {
  323. return $this->LibStatut($this->status, $mode);
  324. }
  325. // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
  326. /**
  327. * Return the status
  328. *
  329. * @param int $status Id status
  330. * @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
  331. * @return string Label of status
  332. */
  333. public function LibStatut($status, $mode = 0)
  334. {
  335. // phpcs:enable
  336. if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
  337. global $langs;
  338. //$langs->load("mymodule");
  339. $this->labelStatus[0] = $langs->transnoentitiesnoconv('Draft');
  340. $this->labelStatus[1] = $langs->transnoentitiesnoconv('Closed');
  341. $this->labelStatusShort[0] = $langs->transnoentitiesnoconv('Draft');
  342. $this->labelStatusShort[1] = $langs->transnoentitiesnoconv('Closed');
  343. }
  344. $statusType = 'status0';
  345. if ($status == self::STATUS_VALIDATED) {
  346. $statusType = 'status6';
  347. }
  348. return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
  349. }
  350. /**
  351. * Return clicable link of object (with eventually picto)
  352. *
  353. * @param int $withpicto Add picto into link
  354. * @param string $option On what the link point to ('nolink', ...)
  355. * @param int $notooltip 1=Disable tooltip
  356. * @param string $morecss Add more css on link
  357. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  358. * @return string String with URL
  359. */
  360. public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
  361. {
  362. global $conf, $langs, $hookmanager;
  363. if (!empty($conf->dol_no_mouse_hover)) {
  364. $notooltip = 1; // Force disable tooltips
  365. }
  366. $result = '';
  367. $newref = ($this->ref ? $this->ref : $this->id);
  368. $label = '<u>'.$langs->trans("CashControl").'</u>';
  369. $label .= '<br>';
  370. $label .= '<b>'.$langs->trans('Ref').':</b> '.($this->ref ? $this->ref : $this->id);
  371. $url = DOL_URL_ROOT.'/compta/cashcontrol/cashcontrol_card.php?id='.$this->id;
  372. if ($option != 'nolink') {
  373. // Add param to save lastsearch_values or not
  374. $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
  375. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
  376. $add_save_lastsearch_values = 1;
  377. }
  378. if ($add_save_lastsearch_values) {
  379. $url .= '&save_lastsearch_values=1';
  380. }
  381. }
  382. $linkclose = '';
  383. if (empty($notooltip)) {
  384. if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  385. $label = $langs->trans("ShowMyObject");
  386. $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
  387. }
  388. $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
  389. $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
  390. } else {
  391. $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
  392. }
  393. $linkstart = '<a href="'.$url.'"';
  394. $linkstart .= $linkclose.'>';
  395. $linkend = '</a>';
  396. $result .= $linkstart;
  397. if ($withpicto) {
  398. $result .= img_object(($notooltip ? '' : $label), ($this->picto ? $this->picto : 'generic'), ($notooltip ? (($withpicto != 2) ? 'class="paddingright"' : '') : 'class="'.(($withpicto != 2) ? 'paddingright ' : '').'classfortooltip"'), 0, 0, $notooltip ? 0 : 1);
  399. }
  400. if ($withpicto != 2) {
  401. $result .= $this->ref;
  402. }
  403. $result .= $linkend;
  404. //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
  405. global $action;
  406. $hookmanager->initHooks(array('cashfencedao'));
  407. $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
  408. $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  409. if ($reshook > 0) {
  410. $result = $hookmanager->resPrint;
  411. } else {
  412. $result .= $hookmanager->resPrint;
  413. }
  414. return $result;
  415. }
  416. }