dolibarr  19.0.0-dev
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 
82  public $fields = array(
83  'rowid' =>array('type'=>'integer', 'label'=>'TechnicalID', 'enabled'=>1, 'visible'=>-2, 'notnull'=>1, 'position'=>10),
84  'entity' =>array('type'=>'integer', 'label'=>'Entity', 'enabled'=>1, 'visible'=>0, 'notnull'=>1, 'position'=>15),
85  'ref' =>array('type'=>'varchar(64)', 'label'=>'Ref', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>18),
86  'posmodule' =>array('type'=>'varchar(30)', 'label'=>'Module', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>19),
87  'posnumber' =>array('type'=>'varchar(30)', 'label'=>'Terminal', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>20, 'css'=>'center'),
88  'label' =>array('type'=>'varchar(255)', 'label'=>'Label', 'enabled'=>1, 'visible'=>0, 'position'=>24),
89  'opening' =>array('type'=>'price', 'label'=>'Opening', 'enabled'=>1, 'visible'=>1, 'position'=>25, 'csslist'=>'amount'),
90  'cash' =>array('type'=>'price', 'label'=>'Cash', 'enabled'=>1, 'visible'=>1, 'position'=>30, 'csslist'=>'amount'),
91  'cheque' =>array('type'=>'price', 'label'=>'Cheque', 'enabled'=>1, 'visible'=>1, 'position'=>33, 'csslist'=>'amount'),
92  'card' =>array('type'=>'price', 'label'=>'CreditCard', 'enabled'=>1, 'visible'=>1, 'position'=>36, 'csslist'=>'amount'),
93  'year_close' =>array('type'=>'integer', 'label'=>'Year close', 'enabled'=>1, 'visible'=>1, 'notnull'=>1, 'position'=>50, 'css'=>'center'),
94  'month_close' =>array('type'=>'integer', 'label'=>'Month close', 'enabled'=>1, 'visible'=>1, 'position'=>55, 'css'=>'center'),
95  'day_close' =>array('type'=>'integer', 'label'=>'Day close', 'enabled'=>1, 'visible'=>1, 'position'=>60, 'css'=>'center'),
96  'date_creation' =>array('type'=>'datetime', 'label'=>'DateCreation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>500),
97  'date_valid' =>array('type'=>'datetime', 'label'=>'DateValidation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>502),
98  'tms' =>array('type'=>'timestamp', 'label'=>'Tms', 'enabled'=>1, 'visible'=>0, 'notnull'=>1, 'position'=>505),
99  'fk_user_creat' =>array('type'=>'integer:User', 'label'=>'UserCreation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>600),
100  'fk_user_valid' =>array('type'=>'integer:User', 'label'=>'UserValidation', 'enabled'=>1, 'visible'=>-1, 'notnull'=>1, 'position'=>602),
101  'import_key' =>array('type'=>'varchar(14)', 'label'=>'Import key', 'enabled'=>1, 'visible'=>0, 'position'=>700),
102  'status' => array('type'=>'integer', 'label'=>'Status', 'enabled'=>1, 'visible'=>1, 'position'=>1000, 'notnull'=>1, 'index'=>1, 'arrayofkeyval'=>array('0'=>'Draft', '1'=>'Validated')),
103  );
104 
108  public $id;
109  public $label;
110  public $opening;
111  public $status;
112  public $year_close;
113  public $month_close;
114  public $day_close;
115  public $posmodule;
116  public $posnumber;
117  public $cash;
118  public $cheque;
119  public $card;
120 
124  public $date_creation;
125  public $fk_user_creat;
126 
135  public $tms;
136 
140  public $date_valid;
141  public $fk_user_valid;
142 
143 
144  const STATUS_DRAFT = 0;
145  const STATUS_VALIDATED = 1;
146  const STATUS_CLOSED = 1; // For the moment CLOSED = VALIDATED
147 
148 
154  public function __construct(DoliDB $db)
155  {
156  $this->db = $db;
157  }
158 
159 
167  public function create(User $user, $notrigger = 0)
168  {
169  global $conf;
170 
171  $error = 0;
172 
173  // Clean data
174  if (empty($this->cash)) {
175  $this->cash = 0;
176  }
177  if (empty($this->cheque)) {
178  $this->cheque = 0;
179  }
180  if (empty($this->card)) {
181  $this->card = 0;
182  }
183 
184  // Insert request
185  $sql = "INSERT INTO ".MAIN_DB_PREFIX."pos_cash_fence (";
186  $sql .= "entity";
187  //$sql .= ", ref";
188  $sql .= ", opening";
189  $sql .= ", status";
190  $sql .= ", date_creation";
191  $sql .= ", posmodule";
192  $sql .= ", posnumber";
193  $sql .= ", day_close";
194  $sql .= ", month_close";
195  $sql .= ", year_close";
196  $sql .= ", cash";
197  $sql .= ", cheque";
198  $sql .= ", card";
199  $sql .= ") VALUES (";
200  //$sql .= "'(PROV)', ";
201  $sql .= ((int) $conf->entity);
202  $sql .= ", ".(is_numeric($this->opening) ? price2num($this->opening, 'MT') : 0);
203  $sql .= ", 0"; // Draft by default
204  $sql .= ", '".$this->db->idate(dol_now())."'";
205  $sql .= ", '".$this->db->escape($this->posmodule)."'";
206  $sql .= ", '".$this->db->escape($this->posnumber)."'";
207  $sql .= ", ".($this->day_close > 0 ? $this->day_close : "null");
208  $sql .= ", ".($this->month_close > 0 ? $this->month_close : "null");
209  $sql .= ", ".((int) $this->year_close);
210  $sql .= ", ".price2num($this->cash, 'MT');
211  $sql .= ", ".price2num($this->cheque, 'MT');
212  $sql .= ", ".price2num($this->card, 'MT');
213  $sql .= ")";
214 
215  $this->db->begin();
216 
217  dol_syslog(get_class($this)."::create", LOG_DEBUG);
218  $resql = $this->db->query($sql);
219  if (!$resql) {
220  $error++;
221  $this->errors[] = "Error ".$this->db->lasterror();
222  }
223 
224  if (!$error) {
225  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."pos_cash_fence");
226 
227  $sql = 'UPDATE '.MAIN_DB_PREFIX.'pos_cash_fence SET ref = rowid where rowid = '.((int) $this->id);
228  $this->db->query($sql);
229  }
230 
231  // Commit or rollback
232  if ($error) {
233  foreach ($this->errors as $errmsg) {
234  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
235  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
236  }
237  $this->db->rollback();
238  return -1 * $error;
239  } else {
240  $this->db->commit();
241  return $this->id;
242  }
243  }
244 
252  public function valid(User $user, $notrigger = 0)
253  {
254  global $conf, $langs;
255  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
256 
257  $error = 0;
258 
259  // Protection
260  if ($this->status == self::STATUS_VALIDATED) {
261  dol_syslog(get_class($this)."::valid action abandonned: already validated", LOG_WARNING);
262  return 0;
263  }
264 
265  /*
266  $posmodule = $this->posmodule;
267  if (!empty($user->rights->$posmodule->use))
268  {
269  $this->error='NotEnoughPermissions';
270  dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
271  return -1;
272  }
273  */
274 
275  $now = dol_now();
276 
277  // Update request
278  $sql = "UPDATE ".MAIN_DB_PREFIX."pos_cash_fence";
279  $sql .= " SET status = ".self::STATUS_VALIDATED.",";
280  $sql .= " date_valid='".$this->db->idate($now)."',";
281  $sql .= " fk_user_valid = ".$user->id;
282  $sql .= " WHERE rowid=".((int) $this->id);
283 
284  $this->db->begin();
285 
286  dol_syslog(get_class($this)."::close", LOG_DEBUG);
287  $resql = $this->db->query($sql);
288  if (!$resql) {
289  $error++;
290  $this->errors[] = "Error ".$this->db->lasterror();
291  }
292 
293  if (!$error) {
294  $this->status = self::STATUS_VALIDATED;
295  $this->date_valid = $now;
296  $this->fk_user_valid = $user->id;
297  }
298 
299  if (!$error && !$notrigger) {
300  // Call trigger
301  $result = $this->call_trigger('CASHCONTROL_VALIDATE', $user);
302  if ($result < 0) {
303  $error++;
304  }
305  // End call triggers
306  }
307 
308  // Commit or rollback
309  if ($error) {
310  foreach ($this->errors as $errmsg) {
311  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
312  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
313  }
314  $this->db->rollback();
315  return -1 * $error;
316  } else {
317  $this->db->commit();
318  return $this->id;
319  }
320  }
321 
322 
330  public function fetch($id, $ref = null)
331  {
332  $result = $this->fetchCommon($id, $ref);
333  //if ($result > 0 && !empty($this->table_element_line)) $this->fetchLines();
334  return $result;
335  }
336 
344  public function update(User $user, $notrigger = false)
345  {
346  return $this->updateCommon($user, $notrigger);
347  }
348 
356  public function delete(User $user, $notrigger = false)
357  {
358  return $this->deleteCommon($user, $notrigger);
359  //return $this->deleteCommon($user, $notrigger, 1);
360  }
361 
368  public function getLibStatut($mode = 0)
369  {
370  return $this->LibStatut($this->status, $mode);
371  }
372 
373  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
381  public function LibStatut($status, $mode = 0)
382  {
383  // phpcs:enable
384  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
385  global $langs;
386  //$langs->load("mymodule");
387  $this->labelStatus[0] = $langs->transnoentitiesnoconv('Draft');
388  $this->labelStatus[1] = $langs->transnoentitiesnoconv('Closed');
389  $this->labelStatusShort[0] = $langs->transnoentitiesnoconv('Draft');
390  $this->labelStatusShort[1] = $langs->transnoentitiesnoconv('Closed');
391  }
392 
393  $statusType = 'status0';
394  if ($status == self::STATUS_VALIDATED) {
395  $statusType = 'status6';
396  }
397 
398  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
399  }
400 
411  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
412  {
413  global $conf, $langs, $hookmanager;
414 
415  if (!empty($conf->dol_no_mouse_hover)) {
416  $notooltip = 1; // Force disable tooltips
417  }
418 
419  $result = '';
420 
421  $newref = ($this->ref ? $this->ref : $this->id);
422 
423  $label = '<u>'.$langs->trans("CashControl").'</u>';
424  $label .= '<br>';
425  $label .= '<b>'.$langs->trans('Ref').':</b> '.($this->ref ? $this->ref : $this->id);
426 
427  $url = DOL_URL_ROOT.'/compta/cashcontrol/cashcontrol_card.php?id='.$this->id;
428 
429  if ($option != 'nolink') {
430  // Add param to save lastsearch_values or not
431  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
432  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
433  $add_save_lastsearch_values = 1;
434  }
435  if ($add_save_lastsearch_values) {
436  $url .= '&save_lastsearch_values=1';
437  }
438  }
439 
440  $linkclose = '';
441  if (empty($notooltip)) {
442  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
443  $label = $langs->trans("ShowMyObject");
444  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
445  }
446  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
447  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
448  } else {
449  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
450  }
451 
452  $linkstart = '<a href="'.$url.'"';
453  $linkstart .= $linkclose.'>';
454  $linkend = '</a>';
455 
456  $result .= $linkstart;
457  if ($withpicto) {
458  $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);
459  }
460  if ($withpicto != 2) {
461  $result .= $this->ref;
462  }
463  $result .= $linkend;
464  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
465 
466  global $action;
467  $hookmanager->initHooks(array('cashfencedao'));
468  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
469  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
470  if ($reshook > 0) {
471  $result = $hookmanager->resPrint;
472  } else {
473  $result .= $hookmanager->resPrint;
474  }
475 
476  return $result;
477  }
478 
486  public function getKanbanView($option = '', $arraydata = null)
487  {
488  global $langs;
489 
490  $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
491 
492  $return = '<div class="box-flex-item box-flex-grow-zero">';
493  $return .= '<div class="info-box info-box-sm">';
494  $return .= '<span class="info-box-icon bg-infobox-action">';
495  $return .= img_picto('', $this->picto);
496  //var_dump($this->fields['rowid']);exit;
497  $return .= '</span>';
498  $return .= '<div class="info-box-content">';
499  $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1, 1) : $this->ref).'</span>';
500  $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
501  if (property_exists($this, 'posmodule')) {
502  $return .= '<br><span class="opacitymedium">'.substr($langs->trans("Module/Application"), 0, 12).'</span> : <span class="info-box-label">'.$this->posmodule.'</span>';
503  }
504  if (property_exists($this, 'year_close')) {
505  $return .= '<br><span class="info-box-label opacitymedium" >'.$langs->trans("Year").'</span> : <span>'.$this->year_close.'</span>';
506  }
507  if (method_exists($this, 'getLibStatut')) {
508  $return .= '<br><div class="info-box-status margintoponly">'.$this->getLibStatut(3).'</div>';
509  }
510  $return .= '</div>';
511  $return .= '</div>';
512  $return .= '</div>';
513  return $return;
514  }
515 }
$object ref
Definition: info.php:78
Class to manage cash fence.
getKanbanView($option='', $arraydata=null)
Return clicable link of object (with eventually picto)
LibStatut($status, $mode=0)
Return the status.
valid(User $user, $notrigger=0)
Validate cash fence.
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return clicable link of object (with eventually picto)
$fields
'type' field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]',...
create(User $user, $notrigger=0)
Create in database.
update(User $user, $notrigger=false)
Update object into database.
getLibStatut($mode=0)
Return label of the status.
__construct(DoliDB $db)
Constructor.
fetch($id, $ref=null)
Load object in memory from the database.
Parent class of all other business classes (invoices, contracts, proposals, orders,...
fetchCommon($id, $ref=null, $morewhere='')
Load object in memory from the database.
deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
Delete object in database.
updateCommon(User $user, $notrigger=false)
Update object into database.
call_trigger($triggerName, $user)
Call trigger based on this instance.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
Definition: user.class.php:48
if(isModEnabled('facture') && $user->hasRight('facture', 'lire')) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->hasRight("fournisseur", "facture", "lire"))||(isModEnabled('supplier_invoice') && $user->hasRight("supplier_invoice", "lire"))) if(isModEnabled('don') && $user->hasRight('don', 'lire')) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->hasRight("commande", "lire") &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $sql
Social contributions to pay.
Definition: index.php:746
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
dol_now($mode='auto')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2)
Show picto whatever it's its name (generic function)
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.