cashcontrol.class.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  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 = 'bank';
  50. public $fields=array(
  51. 'rowid' =>array('type'=>'integer', 'label'=>'ID', 'enabled'=>1, 'visible'=>0, '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. 'label' =>array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>0, 'position'=>20),
  55. 'opening' =>array('type'=>'double(24,8)', 'label'=>'Opening', 'enabled'=>1, 'visible'=>1, 'position'=>25),
  56. 'cash' =>array('type'=>'double(24,8)', 'label'=>'Cash', 'enabled'=>1, 'visible'=>1, 'position'=>30),
  57. //'card' =>array('type'=>'double(24,8)', 'label'=>'Card', 'enabled'=>1, 'visible'=>1, 'position'=>35),
  58. 'cheque' =>array('type'=>'double(24,8)', 'label'=>'Cheque', 'enabled'=>1, 'visible'=>1, 'position'=>35),
  59. 'day_close' =>array('type'=>'integer', 'label'=>'Day close', 'enabled'=>1, 'visible'=>1, 'position'=>50),
  60. 'month_close' =>array('type'=>'integer', 'label'=>'Month close', 'enabled'=>1, 'visible'=>1, 'position'=>55),
  61. 'year_close' =>array('type'=>'integer', 'label'=>'Year close', 'enabled'=>1, 'visible'=>1, 'notnul'=>1, 'position'=>60),
  62. 'posmodule' =>array('type'=>'varchar(30)', 'label'=>'Module', 'enabled'=>1, 'visible'=>1, 'notnul'=>1, 'position'=>65),
  63. 'posnumber' =>array('type'=>'varchar(30)', 'label'=>'CashDesk', 'enabled'=>1, 'visible'=>1, 'notnul'=>1, 'position'=>70),
  64. 'status' =>array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'position'=>80),
  65. 'date_creation' =>array('type'=>'datetime', 'label'=>'Date creation', '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. );
  69. public $id;
  70. public $opening;
  71. public $status;
  72. public $date_creation;
  73. public $year_close;
  74. public $month_close;
  75. public $day_close;
  76. public $posmodule;
  77. public $posnumber;
  78. /**
  79. * Constructor
  80. *
  81. * @param DoliDB $db Database handler
  82. */
  83. public function __construct(DoliDB $db)
  84. {
  85. $this->db = $db;
  86. }
  87. /**
  88. * Create in database
  89. *
  90. * @param User $user User that create
  91. * @param int $notrigger 0=launch triggers after, 1=disable triggers
  92. * @return int <0 if KO, Id of created object if OK
  93. */
  94. public function create(User $user, $notrigger = 0)
  95. {
  96. global $conf;
  97. $error = 0;
  98. // Insert request
  99. $sql = "INSERT INTO ".MAIN_DB_PREFIX."pos_cash_fence (";
  100. $sql .= "entity";
  101. //$sql .= ", ref";
  102. $sql .= ", opening";
  103. $sql .= ", status";
  104. $sql .= ", date_creation";
  105. $sql .= ", posmodule";
  106. $sql .= ", posnumber";
  107. $sql .= ") VALUES (";
  108. //$sql .= "'(PROV)', ";
  109. $sql .= $conf->entity;
  110. $sql .= ", ".$this->opening;
  111. $sql .= ",1";
  112. $sql .= ", '".$this->db->idate(dol_now())."'";
  113. $sql .= ", '".$this->posmodule."'";
  114. $sql .= ", '".$this->posnumber."'";
  115. $sql .= ")";
  116. $this->db->begin();
  117. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  118. $resql = $this->db->query($sql);
  119. if (!$resql) {
  120. $error++;
  121. $this->errors[] = "Error ".$this->db->lasterror();
  122. }
  123. if (!$error) {
  124. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."pos_cash_fence");
  125. }
  126. // Commit or rollback
  127. if ($error) {
  128. foreach ($this->errors as $errmsg) {
  129. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  130. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  131. }
  132. $this->db->rollback();
  133. return -1 * $error;
  134. } else {
  135. $this->db->commit();
  136. return $this->id;
  137. }
  138. }
  139. /**
  140. * close
  141. *
  142. * @param User $user User
  143. * @param number $notrigger No trigger
  144. * @return int <0 if KO, >0 if OK
  145. */
  146. public function close(User $user, $notrigger = 0)
  147. {
  148. global $conf;
  149. $error = 0;
  150. // Update request
  151. $sql = "UPDATE ".MAIN_DB_PREFIX."pos_cash_fence ";
  152. $sql.= "SET";
  153. $sql.= " day_close=DAYOFMONTH(NOW())";
  154. $sql.= ", month_close=MONTH(NOW())";
  155. $sql.= ", year_close=YEAR(NOW())";
  156. $sql.= ", status=2";
  157. $sql.= " where rowid=".$this->id;
  158. $this->db->begin();
  159. dol_syslog(get_class($this)."::create", LOG_DEBUG);
  160. $resql = $this->db->query($sql);
  161. if (!$resql) {
  162. $error++;
  163. $this->errors[] = "Error ".$this->db->lasterror();
  164. }
  165. if (!$error) {
  166. $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."pos_cash_fence");
  167. }
  168. // Commit or rollback
  169. if ($error) {
  170. foreach ($this->errors as $errmsg) {
  171. dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
  172. $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
  173. }
  174. $this->db->rollback();
  175. return -1 * $error;
  176. } else {
  177. $this->db->commit();
  178. return $this->id;
  179. }
  180. }
  181. /**
  182. * Load object in memory from database
  183. *
  184. * @param int $id Id object
  185. * @return int <0 if KO, >0 if OK
  186. */
  187. public function fetch($id)
  188. {
  189. global $conf;
  190. $sql = "SELECT";
  191. $sql .= " *";
  192. $sql .= " FROM ".MAIN_DB_PREFIX."pos_cash_fence";
  193. $sql .= " WHERE rowid = ".$id;
  194. $sql .= " AND entity = ".$conf->entity;
  195. dol_syslog(get_class($this)."::fetch", LOG_DEBUG);
  196. $resql = $this->db->query($sql);
  197. if ($resql) {
  198. if ($this->db->num_rows($resql)) {
  199. $obj = $this->db->fetch_object($resql);
  200. $this->ref = $obj->id;
  201. $this->label = $obj->label;
  202. $this->opening = $obj->opening;
  203. $this->status = $obj->status;
  204. $this->year_close = $obj->year_close;
  205. $this->month_close = $obj->month_close;
  206. $this->day_close = $obj->day_close;
  207. $this->posmodule = $obj->posmodule;
  208. $this->posnumber = $obj->posnumber;
  209. $this->date_creation = $obj->date_creation;
  210. $this->tms = $obj->tms;
  211. $this->id=$id;
  212. }
  213. $this->db->free($resql);
  214. return 1;
  215. } else {
  216. $this->error = "Error ".$this->db->lasterror();
  217. return -1;
  218. }
  219. }
  220. /**
  221. * Return clicable link of object (with eventually picto)
  222. *
  223. * @param int $withpicto Add picto into link
  224. * @param string $option Where point the link ('stock', 'composition', 'category', 'supplier', '')
  225. * @param int $maxlength Maxlength of ref
  226. * @param int $save_lastsearch_value -1=Auto, 0=No save of lastsearch_values when clicking, 1=Save lastsearch_values whenclicking
  227. * @param int $notooltip No tooltip
  228. * @return string String with URL
  229. */
  230. public function getNomUrl($withpicto=0, $option='', $maxlength=0, $save_lastsearch_value=-1, $notooltip=0)
  231. {
  232. global $conf, $langs, $hookmanager;
  233. include_once DOL_DOCUMENT_ROOT.'/core/lib/product.lib.php';
  234. $result='';
  235. $newref=($this->ref?$this->ref:$this->id);
  236. if ($maxlength) { $newref=dol_trunc($newref, $maxlength, 'middle'); }
  237. $label = '<u>' . $langs->trans("ShowCashFence") . '</u>';
  238. $label .= '<br><b>' . $langs->trans('ProductRef') . ':</b> ' . ($this->ref?$this->ref:$this->id);
  239. $linkclose='';
  240. if (empty($notooltip)) {
  241. if (! empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
  242. $label=$langs->trans("ShowCashFence");
  243. $linkclose.=' alt="'.dol_escape_htmltag($label, 1).'"';
  244. }
  245. $linkclose.= ' title="'.dol_escape_htmltag($label, 1, 1).'"';
  246. $linkclose.= ' class="classfortooltip"';
  247. /*
  248. $hookmanager->initHooks(array('productdao'));
  249. $parameters=array('id'=>$this->id);
  250. $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
  251. if ($reshook > 0) $linkclose = $hookmanager->resPrint;
  252. */
  253. }
  254. $url = DOL_URL_ROOT.'/compta/cashcontrol/cashcontrol_card.php?id='.$this->id;
  255. if ($option !== 'nolink') {
  256. // Add param to save lastsearch_values or not
  257. $add_save_lastsearch_values=($save_lastsearch_value == 1 ? 1 : 0);
  258. if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) { $add_save_lastsearch_values=1;
  259. }
  260. if ($add_save_lastsearch_values) { $url.='&save_lastsearch_values=1';
  261. }
  262. }
  263. $linkstart = '<a href="'.$url.'"';
  264. $linkstart.=$linkclose.'>';
  265. $linkend='</a>';
  266. $result.=$linkstart;
  267. if ($withpicto) {
  268. $result.=(img_object(($notooltip?'':$label), 'bank', ($notooltip?'class="paddingright"':'class="paddingright classfortooltip"'), 0, 0, $notooltip?0:1));
  269. }
  270. $result.= $newref;
  271. $result.= $linkend;
  272. global $action;
  273. $hookmanager->initHooks(array('cashfencedao'));
  274. $parameters=array('id'=>$this->id, 'getnomurl'=>$result);
  275. $reshook=$hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
  276. if ($reshook > 0) { $result = $hookmanager->resPrint;
  277. } else { $result .= $hookmanager->resPrint;
  278. }
  279. return $result;
  280. }
  281. }