dolibarr  13.0.2
cashcontrol.class.php
Go to the documentation of this file.
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 
31 {
35  public $element = 'cashcontrol';
36 
40  public $table_element = 'pos_cash_fence';
41 
45  public $ismultientitymanaged = 1;
46 
50  public $isextrafieldmanaged = 0;
51 
55  public $picto = 'cash-register';
56 
57 
58  public $fields = array(
59  'rowid' =>array('type'=>'integer', 'label'=>'ID', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>10),
60  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'notnull'=>1, 'position'=>15),
61  'ref' =>array('type'=>'varchar(64)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>18),
62  'posmodule' =>array('type'=>'varchar(30)', 'label'=>'Module', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>19),
63  'posnumber' =>array('type'=>'varchar(30)', 'label'=>'Terminal', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>20, 'css'=>'center'),
64  'label' =>array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>0, 'position'=>24),
65  'opening' =>array('type'=>'price', 'label'=>'Opening', 'enabled'=>1, 'visible'=>1, 'position'=>25),
66  'cash' =>array('type'=>'price', 'label'=>'Cash', 'enabled'=>1, 'visible'=>1, 'position'=>30),
67  'cheque' =>array('type'=>'price', 'label'=>'Cheque', 'enabled'=>1, 'visible'=>1, 'position'=>33),
68  'card' =>array('type'=>'price', 'label'=>'CreditCard', 'enabled'=>1, 'visible'=>1, 'position'=>36),
69  'year_close' =>array('type'=>'integer', 'label'=>'Year close', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>50, 'css'=>'center'),
70  'month_close' =>array('type'=>'integer', 'label'=>'Month close', 'enabled'=>1, 'visible'=>1, 'position'=>55, 'css'=>'center'),
71  'day_close' =>array('type'=>'integer', 'label'=>'Day close', 'enabled'=>1, 'visible'=>1, 'position'=>60, 'css'=>'center'),
72  'date_valid' =>array('type'=>'datetime', 'label'=>'DateValidation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>490),
73  'date_creation' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>500),
74  'tms' =>array('type'=>'timestamp', 'label'=>'Tms', 'enabled'=>1, 'visible'=>0, 'notnull'=>1, 'position'=>505),
75  'import_key' =>array('type'=>'varchar(14)', 'label'=>'Import key', 'enabled'=>1, 'visible'=>0, 'position'=>510),
76  'status' => array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'position'=>1000, 'notnull'=>1, 'index'=>1, 'arrayofkeyval'=>array('0'=>'Brouillon', '1'=>'Validated')),
77  );
78 
82  public $id;
83  public $opening;
84  public $status;
85  public $year_close;
86  public $month_close;
87  public $day_close;
88  public $posmodule;
89  public $posnumber;
90  public $cash;
91  public $cheque;
92  public $card;
93 
97  public $date_valid;
98 
102  public $date_creation;
103 
107  public $date_modification;
108 
109  const STATUS_DRAFT = 0;
110  const STATUS_VALIDATED = 1;
111  const STATUS_CLOSED = 1; // For the moment CLOSED = VALIDATED
112 
113 
119  public function __construct(DoliDB $db)
120  {
121  $this->db = $db;
122  }
123 
124 
132  public function create(User $user, $notrigger = 0)
133  {
134  global $conf;
135 
136  $error = 0;
137 
138  // Clean data
139  if (empty($this->cash)) $this->cash = 0;
140  if (empty($this->cheque)) $this->cheque = 0;
141  if (empty($this->card)) $this->card = 0;
142 
143  // Insert request
144  $sql = "INSERT INTO ".MAIN_DB_PREFIX."pos_cash_fence (";
145  $sql .= "entity";
146  //$sql .= ", ref";
147  $sql .= ", opening";
148  $sql .= ", status";
149  $sql .= ", date_creation";
150  $sql .= ", posmodule";
151  $sql .= ", posnumber";
152  $sql .= ", day_close";
153  $sql .= ", month_close";
154  $sql .= ", year_close";
155  $sql .= ", cash";
156  $sql .= ", cheque";
157  $sql .= ", card";
158  $sql .= ") VALUES (";
159  //$sql .= "'(PROV)', ";
160  $sql .= $conf->entity;
161  $sql .= ", ".(is_numeric($this->opening) ? $this->opening : 0);
162  $sql .= ", 0"; // Draft by default
163  $sql .= ", '".$this->db->idate(dol_now())."'";
164  $sql .= ", '".$this->db->escape($this->posmodule)."'";
165  $sql .= ", '".$this->db->escape($this->posnumber)."'";
166  $sql .= ", ".($this->day_close > 0 ? $this->day_close : "null");
167  $sql .= ", ".($this->month_close > 0 ? $this->month_close : "null");
168  $sql .= ", ".$this->year_close;
169  $sql .= ", ".$this->cash;
170  $sql .= ", ".$this->cheque;
171  $sql .= ", ".$this->card;
172  $sql .= ")";
173 
174  $this->db->begin();
175 
176  dol_syslog(get_class($this)."::create", LOG_DEBUG);
177  $resql = $this->db->query($sql);
178  if (!$resql) {
179  $error++;
180  $this->errors[] = "Error ".$this->db->lasterror();
181  }
182 
183  if (!$error) {
184  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."pos_cash_fence");
185 
186  $sql = 'UPDATE '.MAIN_DB_PREFIX.'pos_cash_fence SET ref = rowid where rowid = '.$this->id;
187  $this->db->query($sql);
188  }
189 
190  // Commit or rollback
191  if ($error) {
192  foreach ($this->errors as $errmsg) {
193  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
194  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
195  }
196  $this->db->rollback();
197  return -1 * $error;
198  } else {
199  $this->db->commit();
200  return $this->id;
201  }
202  }
203 
211  public function valid(User $user, $notrigger = 0)
212  {
213  global $conf, $langs;
214  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
215 
216  $error = 0;
217 
218  // Protection
219  if ($this->status == self::STATUS_VALIDATED)
220  {
221  dol_syslog(get_class($this)."::valid action abandonned: already validated", LOG_WARNING);
222  return 0;
223  }
224 
225  /*
226  $posmodule = $this->posmodule;
227  if (! empty($user->rights->$posmodule->use))
228  {
229  $this->error='NotEnoughPermissions';
230  dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
231  return -1;
232  }
233  */
234 
235  $now = dol_now();
236 
237  // Update request
238  $sql = "UPDATE ".MAIN_DB_PREFIX."pos_cash_fence";
239  $sql .= " SET status = ".self::STATUS_VALIDATED.",";
240  $sql .= " date_valid='".$this->db->idate($now)."',";
241  $sql .= " fk_user_valid = ".$user->id;
242  $sql .= " WHERE rowid=".$this->id;
243 
244  $this->db->begin();
245 
246  dol_syslog(get_class($this)."::close", LOG_DEBUG);
247  $resql = $this->db->query($sql);
248  if (!$resql) {
249  $error++;
250  $this->errors[] = "Error ".$this->db->lasterror();
251  }
252 
253  if (!$error) {
254  $this->status = self::STATUS_VALIDATED;
255  $this->date_valid = $now;
256  $this->fk_user_valid = $user->id;
257  }
258 
259  if (!$error && !$notrigger)
260  {
261  // Call trigger
262  $result = $this->call_trigger('CASHCONTROL_VALIDATE', $user);
263  if ($result < 0) $error++;
264  // End call triggers
265  }
266 
267  // Commit or rollback
268  if ($error) {
269  foreach ($this->errors as $errmsg) {
270  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
271  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
272  }
273  $this->db->rollback();
274  return -1 * $error;
275  } else {
276  $this->db->commit();
277  return $this->id;
278  }
279  }
280 
281 
289  public function fetch($id, $ref = null)
290  {
291  $result = $this->fetchCommon($id, $ref);
292  //if ($result > 0 && !empty($this->table_element_line)) $this->fetchLines();
293  return $result;
294  }
295 
303  public function update(User $user, $notrigger = false)
304  {
305  return $this->updateCommon($user, $notrigger);
306  }
307 
315  public function delete(User $user, $notrigger = false)
316  {
317  return $this->deleteCommon($user, $notrigger);
318  //return $this->deleteCommon($user, $notrigger, 1);
319  }
320 
327  public function getLibStatut($mode = 0)
328  {
329  return $this->LibStatut($this->status, $mode);
330  }
331 
332  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
340  public function LibStatut($status, $mode = 0)
341  {
342  // phpcs:enable
343  if (empty($this->labelStatus) || empty($this->labelStatusShort))
344  {
345  global $langs;
346  //$langs->load("mymodule");
347  $this->labelStatus[0] = $langs->trans('Draft');
348  $this->labelStatus[1] = $langs->trans('Closed');
349  $this->labelStatusShort[0] = $langs->trans('Draft');
350  $this->labelStatusShort[1] = $langs->trans('Closed');
351  }
352 
353  $statusType = 'status0';
354  if ($status == self::STATUS_VALIDATED) $statusType = 'status6';
355 
356  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
357  }
358 
369  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
370  {
371  global $conf, $langs, $hookmanager;
372 
373  if (!empty($conf->dol_no_mouse_hover)) $notooltip = 1; // Force disable tooltips
374 
375  $result = '';
376 
377  $newref = ($this->ref ? $this->ref : $this->id);
378 
379  $label = '<u>'.$langs->trans("CashFence").'</u>';
380  $label .= '<br>';
381  $label .= '<b>'.$langs->trans('Ref').':</b> '.($this->ref ? $this->ref : $this->id);
382 
383  $url = DOL_URL_ROOT.'/compta/cashcontrol/cashcontrol_card.php?id='.$this->id;
384 
385  if ($option != 'nolink')
386  {
387  // Add param to save lastsearch_values or not
388  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
389  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) $add_save_lastsearch_values = 1;
390  if ($add_save_lastsearch_values) $url .= '&save_lastsearch_values=1';
391  }
392 
393  $linkclose = '';
394  if (empty($notooltip))
395  {
396  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER))
397  {
398  $label = $langs->trans("ShowMyObject");
399  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
400  }
401  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
402  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
403 
404  /*
405  $hookmanager->initHooks(array('myobjectdao'));
406  $parameters=array('id'=>$this->id);
407  $reshook=$hookmanager->executeHooks('getnomurltooltip',$parameters,$this,$action); // Note that $action and $object may have been modified by some hooks
408  if ($reshook > 0) $linkclose = $hookmanager->resPrint;
409  */
410  } else $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
411 
412  $linkstart = '<a href="'.$url.'"';
413  $linkstart .= $linkclose.'>';
414  $linkend = '</a>';
415 
416  $result .= $linkstart;
417  if ($withpicto) $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);
418  if ($withpicto != 2) $result .= $this->ref;
419  $result .= $linkend;
420  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
421 
422  global $action;
423  $hookmanager->initHooks(array('cashfencedao'));
424  $parameters = array('id'=>$this->id, 'getnomurl'=>$result);
425  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
426  if ($reshook > 0) {
427  $result = $hookmanager->resPrint;
428  } else {
429  $result .= $hookmanager->resPrint;
430  }
431 
432  return $result;
433  }
434 }
deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
Delete object in database.
dol_now($mode= 'auto')
Return date for now.
Class to manage cash fence.
Class to manage Dolibarr users.
Definition: user.class.php:44
Class to manage Dolibarr database access.
LibStatut($status, $mode=0)
Return the status.
create(User $user, $notrigger=0)
Create in database.
$conf db
API class for accounts.
Definition: inc.php:54
valid(User $user, $notrigger=0)
Validate cash fence.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename= '', $restricttologhandler= '', $logcontext=null)
Write log message into outputs.
update(User $user, $notrigger=false)
Update object into database.
img_object($titlealt, $picto, $moreatt= '', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
getNomUrl($withpicto=0, $option= '', $notooltip=0, $morecss= '', $save_lastsearch_value=-1)
Return clicable link of object (with eventually picto)
updateCommon(User $user, $notrigger=false)
Update object into database.
__construct(DoliDB $db)
Constructor.
fetch($id, $ref=null)
Load object in memory from the database.
print $_SERVER["PHP_SELF"]
Edit parameters.
call_trigger($triggerName, $user)
Call trigger based on this instance.
if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) if(!empty($conf->don->enabled)&&$user->rights->don->lire) if(!empty($conf->tax->enabled)&&$user->rights->tax->charges->lire) if(!empty($conf->facture->enabled)&&!empty($conf->commande->enabled)&&$user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) if(!empty($conf->facture->enabled)&&$user->rights->facture->lire) if((!empty($conf->fournisseur->enabled)&&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD)||!empty($conf->supplier_invoice->enabled))&&$user->rights->fournisseur->facture->lire) $resql
Social contributions to pay.
Definition: index.php:1232
dolGetStatus($statusLabel= '', $statusLabelShort= '', $html= '', $statusType= 'status0', $displayMode=0, $url= '', $params=array())
Output the badge of a status.
getLibStatut($mode=0)
Return label of the status.
Parent class of all other business classes (invoices, contracts, proposals, orders, ...)
fetchCommon($id, $ref=null, $morewhere= '')
Load object in memory from the database.