dolibarr  16.0.5
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 $opening;
110  public $status;
111  public $year_close;
112  public $month_close;
113  public $day_close;
114  public $posmodule;
115  public $posnumber;
116  public $cash;
117  public $cheque;
118  public $card;
119 
123  public $date_creation;
124  public $fk_user_creat;
125 
129  public $date_modification;
130 
134  public $date_valid;
135  public $fk_user_valid;
136 
137 
138  const STATUS_DRAFT = 0;
139  const STATUS_VALIDATED = 1;
140  const STATUS_CLOSED = 1; // For the moment CLOSED = VALIDATED
141 
142 
148  public function __construct(DoliDB $db)
149  {
150  $this->db = $db;
151  }
152 
153 
161  public function create(User $user, $notrigger = 0)
162  {
163  global $conf;
164 
165  $error = 0;
166 
167  // Clean data
168  if (empty($this->cash)) {
169  $this->cash = 0;
170  }
171  if (empty($this->cheque)) {
172  $this->cheque = 0;
173  }
174  if (empty($this->card)) {
175  $this->card = 0;
176  }
177 
178  // Insert request
179  $sql = "INSERT INTO ".MAIN_DB_PREFIX."pos_cash_fence (";
180  $sql .= "entity";
181  //$sql .= ", ref";
182  $sql .= ", opening";
183  $sql .= ", status";
184  $sql .= ", date_creation";
185  $sql .= ", posmodule";
186  $sql .= ", posnumber";
187  $sql .= ", day_close";
188  $sql .= ", month_close";
189  $sql .= ", year_close";
190  $sql .= ", cash";
191  $sql .= ", cheque";
192  $sql .= ", card";
193  $sql .= ") VALUES (";
194  //$sql .= "'(PROV)', ";
195  $sql .= ((int) $conf->entity);
196  $sql .= ", ".(is_numeric($this->opening) ? price2num($this->opening, 'MT') : 0);
197  $sql .= ", 0"; // Draft by default
198  $sql .= ", '".$this->db->idate(dol_now())."'";
199  $sql .= ", '".$this->db->escape($this->posmodule)."'";
200  $sql .= ", '".$this->db->escape($this->posnumber)."'";
201  $sql .= ", ".($this->day_close > 0 ? $this->day_close : "null");
202  $sql .= ", ".($this->month_close > 0 ? $this->month_close : "null");
203  $sql .= ", ".((int) $this->year_close);
204  $sql .= ", ".price2num($this->cash, 'MT');
205  $sql .= ", ".price2num($this->cheque, 'MT');
206  $sql .= ", ".price2num($this->card, 'MT');
207  $sql .= ")";
208 
209  $this->db->begin();
210 
211  dol_syslog(get_class($this)."::create", LOG_DEBUG);
212  $resql = $this->db->query($sql);
213  if (!$resql) {
214  $error++;
215  $this->errors[] = "Error ".$this->db->lasterror();
216  }
217 
218  if (!$error) {
219  $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."pos_cash_fence");
220 
221  $sql = 'UPDATE '.MAIN_DB_PREFIX.'pos_cash_fence SET ref = rowid where rowid = '.((int) $this->id);
222  $this->db->query($sql);
223  }
224 
225  // Commit or rollback
226  if ($error) {
227  foreach ($this->errors as $errmsg) {
228  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
229  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
230  }
231  $this->db->rollback();
232  return -1 * $error;
233  } else {
234  $this->db->commit();
235  return $this->id;
236  }
237  }
238 
246  public function valid(User $user, $notrigger = 0)
247  {
248  global $conf, $langs;
249  require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
250 
251  $error = 0;
252 
253  // Protection
254  if ($this->status == self::STATUS_VALIDATED) {
255  dol_syslog(get_class($this)."::valid action abandonned: already validated", LOG_WARNING);
256  return 0;
257  }
258 
259  /*
260  $posmodule = $this->posmodule;
261  if (! empty($user->rights->$posmodule->use))
262  {
263  $this->error='NotEnoughPermissions';
264  dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR);
265  return -1;
266  }
267  */
268 
269  $now = dol_now();
270 
271  // Update request
272  $sql = "UPDATE ".MAIN_DB_PREFIX."pos_cash_fence";
273  $sql .= " SET status = ".self::STATUS_VALIDATED.",";
274  $sql .= " date_valid='".$this->db->idate($now)."',";
275  $sql .= " fk_user_valid = ".$user->id;
276  $sql .= " WHERE rowid=".((int) $this->id);
277 
278  $this->db->begin();
279 
280  dol_syslog(get_class($this)."::close", LOG_DEBUG);
281  $resql = $this->db->query($sql);
282  if (!$resql) {
283  $error++;
284  $this->errors[] = "Error ".$this->db->lasterror();
285  }
286 
287  if (!$error) {
288  $this->status = self::STATUS_VALIDATED;
289  $this->date_valid = $now;
290  $this->fk_user_valid = $user->id;
291  }
292 
293  if (!$error && !$notrigger) {
294  // Call trigger
295  $result = $this->call_trigger('CASHCONTROL_VALIDATE', $user);
296  if ($result < 0) {
297  $error++;
298  }
299  // End call triggers
300  }
301 
302  // Commit or rollback
303  if ($error) {
304  foreach ($this->errors as $errmsg) {
305  dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
306  $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
307  }
308  $this->db->rollback();
309  return -1 * $error;
310  } else {
311  $this->db->commit();
312  return $this->id;
313  }
314  }
315 
316 
324  public function fetch($id, $ref = null)
325  {
326  $result = $this->fetchCommon($id, $ref);
327  //if ($result > 0 && !empty($this->table_element_line)) $this->fetchLines();
328  return $result;
329  }
330 
338  public function update(User $user, $notrigger = false)
339  {
340  return $this->updateCommon($user, $notrigger);
341  }
342 
350  public function delete(User $user, $notrigger = false)
351  {
352  return $this->deleteCommon($user, $notrigger);
353  //return $this->deleteCommon($user, $notrigger, 1);
354  }
355 
362  public function getLibStatut($mode = 0)
363  {
364  return $this->LibStatut($this->status, $mode);
365  }
366 
367  // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
375  public function LibStatut($status, $mode = 0)
376  {
377  // phpcs:enable
378  if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
379  global $langs;
380  //$langs->load("mymodule");
381  $this->labelStatus[0] = $langs->transnoentitiesnoconv('Draft');
382  $this->labelStatus[1] = $langs->transnoentitiesnoconv('Closed');
383  $this->labelStatusShort[0] = $langs->transnoentitiesnoconv('Draft');
384  $this->labelStatusShort[1] = $langs->transnoentitiesnoconv('Closed');
385  }
386 
387  $statusType = 'status0';
388  if ($status == self::STATUS_VALIDATED) {
389  $statusType = 'status6';
390  }
391 
392  return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
393  }
394 
405  public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
406  {
407  global $conf, $langs, $hookmanager;
408 
409  if (!empty($conf->dol_no_mouse_hover)) {
410  $notooltip = 1; // Force disable tooltips
411  }
412 
413  $result = '';
414 
415  $newref = ($this->ref ? $this->ref : $this->id);
416 
417  $label = '<u>'.$langs->trans("CashControl").'</u>';
418  $label .= '<br>';
419  $label .= '<b>'.$langs->trans('Ref').':</b> '.($this->ref ? $this->ref : $this->id);
420 
421  $url = DOL_URL_ROOT.'/compta/cashcontrol/cashcontrol_card.php?id='.$this->id;
422 
423  if ($option != 'nolink') {
424  // Add param to save lastsearch_values or not
425  $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
426  if ($save_lastsearch_value == -1 && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
427  $add_save_lastsearch_values = 1;
428  }
429  if ($add_save_lastsearch_values) {
430  $url .= '&save_lastsearch_values=1';
431  }
432  }
433 
434  $linkclose = '';
435  if (empty($notooltip)) {
436  if (!empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER)) {
437  $label = $langs->trans("ShowMyObject");
438  $linkclose .= ' alt="'.dol_escape_htmltag($label, 1).'"';
439  }
440  $linkclose .= ' title="'.dol_escape_htmltag($label, 1).'"';
441  $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
442  } else {
443  $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
444  }
445 
446  $linkstart = '<a href="'.$url.'"';
447  $linkstart .= $linkclose.'>';
448  $linkend = '</a>';
449 
450  $result .= $linkstart;
451  if ($withpicto) {
452  $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);
453  }
454  if ($withpicto != 2) {
455  $result .= $this->ref;
456  }
457  $result .= $linkend;
458  //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
459 
460  global $action;
461  $hookmanager->initHooks(array('cashfencedao'));
462  $parameters = array('id'=>$this->id, 'getnomurl' => &$result);
463  $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
464  if ($reshook > 0) {
465  $result = $hookmanager->resPrint;
466  } else {
467  $result .= $hookmanager->resPrint;
468  }
469 
470  return $result;
471  }
472 }
CommonObject\deleteCommon
deleteCommon(User $user, $notrigger=false, $forcechilddeletion=0)
Delete object in database.
Definition: commonobject.class.php:9406
db
$conf db
API class for accounts.
Definition: inc.php:41
CommonObject\fetchCommon
fetchCommon($id, $ref=null, $morewhere='')
Load object in memory from the database.
Definition: commonobject.class.php:9202
CashControl\valid
valid(User $user, $notrigger=0)
Validate cash fence.
Definition: cashcontrol.class.php:246
CashControl
Class to manage cash fence.
Definition: cashcontrol.class.php:30
DoliDB
Class to manage Dolibarr database access.
Definition: DoliDB.class.php:30
ref
$object ref
Definition: info.php:77
CommonObject
Parent class of all other business classes (invoices, contracts, proposals, orders,...
Definition: commonobject.class.php:44
price2num
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
Definition: functions.lib.php:5661
CashControl\$fields
$fields
'type' field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]',...
Definition: cashcontrol.class.php:82
CashControl\create
create(User $user, $notrigger=0)
Create in database.
Definition: cashcontrol.class.php:161
CashControl\update
update(User $user, $notrigger=false)
Update object into database.
Definition: cashcontrol.class.php:338
CashControl\getLibStatut
getLibStatut($mode=0)
Return label of the status.
Definition: cashcontrol.class.php:362
CashControl\LibStatut
LibStatut($status, $mode=0)
Return the status.
Definition: cashcontrol.class.php:375
dol_syslog
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.
Definition: functions.lib.php:1603
CommonObject\updateCommon
updateCommon(User $user, $notrigger=false)
Update object into database.
Definition: commonobject.class.php:9308
CashControl\getNomUrl
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return clicable link of object (with eventually picto)
Definition: cashcontrol.class.php:405
CashControl\fetch
fetch($id, $ref=null)
Load object in memory from the database.
Definition: cashcontrol.class.php:324
User
Class to manage Dolibarr users.
Definition: user.class.php:44
CashControl\__construct
__construct(DoliDB $db)
Constructor.
Definition: cashcontrol.class.php:148
dolGetStatus
dolGetStatus($statusLabel='', $statusLabelShort='', $html='', $statusType='status0', $displayMode=0, $url='', $params=array())
Output the badge of a status.
Definition: functions.lib.php:10338
img_object
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=false, $srconly=0, $notitle=0)
Show a picto called object_picto (generic function)
Definition: functions.lib.php:4211
dol_now
dol_now($mode='auto')
Return date for now.
Definition: functions.lib.php:2845
$resql
if(isModEnabled('facture') &&!empty($user->rights->facture->lire)) if((isModEnabled('fournisseur') &&empty($conf->global->MAIN_USE_NEW_SUPPLIERMOD) && $user->rights->fournisseur->facture->lire)||(isModEnabled('supplier_invoice') && $user->rights->supplier_invoice->lire)) if(isModEnabled('don') &&!empty($user->rights->don->lire)) if(isModEnabled('tax') &&!empty($user->rights->tax->charges->lire)) if(isModEnabled('facture') &&isModEnabled('commande') && $user->rights->commande->lire &&empty($conf->global->WORKFLOW_DISABLE_CREATE_INVOICE_FROM_ORDER)) $resql
Social contributions to pay.
Definition: index.php:742
CommonObject\call_trigger
call_trigger($triggerName, $user)
Call trigger based on this instance.
Definition: commonobject.class.php:5791