dolibarr 24.0.0-beta
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 * Copyright (C) 2024-2025 MDW <mdeweerd@users.noreply.github.com>
7 * Copyright (C) 2024-2025 Frédéric France <frederic.france@free.fr>
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
13 *
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
18 *
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <https://www.gnu.org/licenses/>.
21 */
22
33{
37 public $element = 'cashcontrol';
38
42 public $table_element = 'pos_cash_fence';
43
47 public $picto = 'cash-register';
48
74 public $fields = array(
75 'rowid' => array('type' => 'integer', 'label' => 'TechnicalID', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 10),
76 'entity' => array('type' => 'integer', 'label' => 'Entity', 'enabled' => 1, 'visible' => 0, 'notnull' => 1, 'position' => 15),
77 'ref' => array('type' => 'varchar(64)', 'label' => 'Ref', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 18),
78 'posmodule' => array('type' => 'varchar(30)', 'label' => 'Module', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 19),
79 'posnumber' => array('type' => 'varchar(30)', 'label' => 'Terminal', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 20, 'csslist' => 'width50 center'),
80 'label' => array('type' => 'varchar(255)', 'label' => 'Label', 'enabled' => 1, 'visible' => 0, 'position' => 24),
81 'opening' => array('type' => 'price', 'label' => 'Opening', 'enabled' => 1, 'visible' => -1, 'position' => 25, 'csslist' => 'amount'),
82 'cash' => array('type' => 'price', 'label' => 'Cash', 'enabled' => 1, 'visible' => 1, 'position' => 30, 'csslist' => 'amount'),
83 'cheque' => array('type' => 'price', 'label' => 'Cheque', 'enabled' => 1, 'visible' => 1, 'position' => 33, 'csslist' => 'amount'),
84 'card' => array('type' => 'price', 'label' => 'CreditCard', 'enabled' => 1, 'visible' => 1, 'position' => 36, 'csslist' => 'amount'),
85 'cash_declared' => array('type' => 'price', 'label' => 'CashDeclared', 'enabled' => 1, 'visible' => -1, 'position' => 40, 'csslist' => 'amount'),
86 'cheque_declared' => array('type' => 'price', 'label' => 'ChequeDeclared', 'enabled' => 1, 'visible' => -1, 'position' => 41, 'csslist' => 'amount'),
87 'card_declared' => array('type' => 'price', 'label' => 'CreditCardDeclared', 'enabled' => 1, 'visible' => -1, 'position' => 42, 'csslist' => 'amount'),
88 'cash_lifetime' => array('type' => 'price', 'label' => 'CashLifetime', 'enabled' => 1, 'visible' => -1, 'position' => 45, 'csslist' => 'amount'),
89 'cheque_lifetime' => array('type' => 'price', 'label' => 'ChequeLifetime', 'enabled' => 1, 'visible' => -1, 'position' => 46, 'csslist' => 'amount'),
90 'card_lifetime' => array('type' => 'price', 'label' => 'CardLifetime', 'enabled' => 1, 'visible' => -1, 'position' => 47, 'csslist' => 'amount'),
91 'lifetime_start' => array('type' => 'datetime', 'label' => 'LifetimeStartDate', 'enabled' => 1, 'visible' => 0, 'position' => 48, 'csslist' => 'center'),
92 'year_close' => array('type' => 'integer', 'label' => 'Year', 'enabled' => 1, 'visible' => 1, 'notnull' => 1, 'position' => 50, 'css' => 'center'),
93 'month_close' => array('type' => 'integer', 'label' => 'Month', 'enabled' => 1, 'visible' => 1, 'position' => 55, 'css' => 'center'),
94 'day_close' => array('type' => 'integer', 'label' => 'Day', 'enabled' => 1, 'visible' => 1, 'position' => 60, 'css' => 'center'),
95 'hour_close' => array('type' => 'integer', 'label' => 'Hour', 'enabled' => 1, 'visible' => -1, 'position' => 62, 'css' => 'center'),
96 'min_close' => array('type' => 'integer', 'label' => 'Min', 'enabled' => 1, 'visible' => -1, 'position' => 63, 'css' => 'center'),
97 'sec_close' => array('type' => 'integer', 'label' => 'Sec', 'enabled' => 1, 'visible' => -1, 'position' => 64, 'css' => 'center'),
98 'date_creation' => array('type' => 'datetime', 'label' => 'DateCreation', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 500),
99 'date_valid' => array('type' => 'datetime', 'label' => 'DateValidation', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 502),
100 'tms' => array('type' => 'timestamp', 'label' => 'Tms', 'enabled' => 1, 'visible' => 0, 'notnull' => 1, 'position' => 505),
101 'fk_user_creat' => array('type' => 'integer:User', 'label' => 'UserCreation', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 600),
102 'fk_user_valid' => array('type' => 'integer:User', 'label' => 'UserValidation', 'enabled' => 1, 'visible' => -1, 'notnull' => 1, 'position' => 602),
103 'import_key' => array('type' => 'varchar(14)', 'label' => 'Import key', 'enabled' => 1, 'visible' => 0, 'position' => 700),
104 'status' => array('type' => 'integer', 'label' => 'Status', 'enabled' => 1, 'visible' => 1, 'position' => 1000, 'notnull' => 1, 'index' => 1, 'arrayofkeyval' => array('0' => 'Draft', '1' => 'Closed')),
105 );
106
110 public $id;
111
115 public $label;
116
120 public $opening;
121
125 public $status;
126
130 public $year_close;
131
135 public $month_close;
136
140 public $day_close;
141
145 public $hour_close;
146
150 public $min_close;
151
155 public $sec_close;
156
160 public $posmodule;
161
165 public $posnumber;
166
170 public $cash;
171
175 public $cheque;
176
180 public $card;
181
185 public $cash_declared;
186
190 public $cheque_declared;
191
195 public $card_declared;
196
200 public $cash_lifetime;
201
205 public $cheque_lifetime;
206
210 public $card_lifetime;
211
215 public $lifetime_start;
216
220 public $fk_user_creat;
221
225 public $date_valid;
226
230 public $fk_user_valid;
231
232
233 const STATUS_DRAFT = 0;
234 const STATUS_VALIDATED = 1;
235 const STATUS_CLOSED = 1; // For the moment CLOSED = VALIDATED
236
237
243 public function __construct(DoliDB $db)
244 {
245 $this->db = $db;
246
247 $this->ismultientitymanaged = 1;
248 $this->isextrafieldmanaged = 0;
249 }
250
251
259 public function create(User $user, $notrigger = 0)
260 {
261 global $conf;
262
263 $error = 0;
264
265 // Clean data
266 if (empty($this->cash)) {
267 $this->cash = 0;
268 }
269 if (empty($this->cheque)) {
270 $this->cheque = 0;
271 }
272 if (empty($this->card)) {
273 $this->card = 0;
274 }
275
276 // Insert request
277 $sql = "INSERT INTO ".MAIN_DB_PREFIX."pos_cash_fence (";
278 $sql .= "entity";
279 //$sql .= ", ref";
280 $sql .= ", opening";
281 $sql .= ", status";
282 $sql .= ", date_creation";
283 $sql .= ", posmodule";
284 $sql .= ", posnumber";
285 $sql .= ", day_close";
286 $sql .= ", month_close";
287 $sql .= ", year_close";
288 $sql .= ", hour_close";
289 $sql .= ", min_close";
290 $sql .= ", sec_close";
291 $sql .= ", cash";
292 $sql .= ", cheque";
293 $sql .= ", card";
294 $sql .= ", fk_user_creat";
295 $sql .= ") VALUES (";
296 //$sql .= "'(PROV)', ";
297 $sql .= ((int) $conf->entity);
298 $sql .= ", ".(is_numeric($this->opening) ? price2num($this->opening, 'MT') : 0);
299 $sql .= ", 0"; // Draft by default
300 $sql .= ", '".$this->db->idate(dol_now())."'";
301 $sql .= ", '".$this->db->escape($this->posmodule)."'";
302 $sql .= ", '".$this->db->escape($this->posnumber)."'";
303 $sql .= ", ".($this->day_close > 0 ? $this->day_close : "null");
304 $sql .= ", ".($this->month_close > 0 ? $this->month_close : "null");
305 $sql .= ", ".((int) $this->year_close);
306 $sql .= ", ".(isset($this->hour_close) ? (int) $this->hour_close : 23);
307 $sql .= ", ".(isset($this->min_close) ? (int) $this->min_close : 59);
308 $sql .= ", ".(isset($this->sec_close) ? (int) $this->sec_close : 59);
309 $sql .= ", ".price2num($this->cash, 'MT');
310 $sql .= ", ".price2num($this->cheque, 'MT');
311 $sql .= ", ".price2num($this->card, 'MT');
312 $sql .= ", ".((int) $user->id);
313 $sql .= ")";
314
315 $this->db->begin();
316
317 dol_syslog(get_class($this)."::create", LOG_DEBUG);
318 $resql = $this->db->query($sql);
319 if (!$resql) {
320 $error++;
321 $this->errors[] = "Error ".$this->db->lasterror();
322 }
323
324 if (!$error) {
325 $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."pos_cash_fence");
326
327 $sql = 'UPDATE '.MAIN_DB_PREFIX.'pos_cash_fence SET ref = rowid where rowid = '.((int) $this->id);
328 $this->db->query($sql);
329 }
330
331 // Commit or rollback
332 if ($error) {
333 foreach ($this->errors as $errmsg) {
334 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
335 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
336 }
337 $this->db->rollback();
338 return -1 * $error;
339 } else {
340 $this->db->commit();
341 return $this->id;
342 }
343 }
344
352 public function valid(User $user, $notrigger = 0)
353 {
354 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
355
356 $error = 0;
357
358 // Protection
359 if ($this->status == self::STATUS_VALIDATED) {
360 dol_syslog(get_class($this)."::valid action abandoned: already validated", LOG_WARNING);
361 return 0;
362 }
363
364 $now = dol_now();
365
366 // Update request
367 $sql = "UPDATE ".MAIN_DB_PREFIX."pos_cash_fence";
368 $sql .= " SET status = ".((int) self::STATUS_VALIDATED).",";
369 $sql .= " date_valid='".$this->db->idate($now)."',";
370 $sql .= " fk_user_valid = ".((int) $user->id);
371 $sql .= " WHERE rowid=".((int) $this->id);
372
373 $this->db->begin();
374
375 dol_syslog(get_class($this)."::close", LOG_DEBUG);
376 $resql = $this->db->query($sql);
377 if (!$resql) {
378 $error++;
379 $this->errors[] = "Error ".$this->db->lasterror();
380 }
381
382 if (!$error) {
383 $this->status = self::STATUS_VALIDATED;
384 $this->date_valid = $now;
385 $this->fk_user_valid = $user->id;
386 }
387
388 if (!$error && !$notrigger) {
389 // Call trigger
390 $result = $this->call_trigger('CASHCONTROL_VALIDATE', $user);
391 if ($result < 0) {
392 $error++;
393 }
394 // End call triggers
395 }
396
397 // Commit or rollback
398 if ($error) {
399 foreach ($this->errors as $errmsg) {
400 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
401 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
402 }
403 $this->db->rollback();
404 return -1 * $error;
405 } else {
406 $this->db->commit();
407 return $this->id;
408 }
409 }
410
411
419 public function close(User $user, $notrigger = 0)
420 {
421 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
422
423 $error = 0;
424
425 // Protection
426 if ($this->status == self::STATUS_CLOSED) {
427 $this->error = get_class($this)."::valid action abandoned: already validated";
428 dol_syslog($this->error, LOG_WARNING);
429 return 0;
430 }
431
432 $now = dol_now();
433
434 // Update request
435 $sql = "UPDATE ".MAIN_DB_PREFIX."pos_cash_fence";
436 $sql .= " SET status = ".((int) self::STATUS_CLOSED).",";
437 $sql .= " date_valid = '".$this->db->idate($now)."',";
438 $sql .= " fk_user_valid = ".((int) $user->id);
439 $sql .= " WHERE rowid=".((int) $this->id);
440
441 $this->db->begin();
442
443 dol_syslog(get_class($this)."::close", LOG_DEBUG);
444 $resql = $this->db->query($sql);
445 if (!$resql) {
446 $error++;
447 $this->errors[] = "Error ".$this->db->lasterror();
448 }
449
450 if (!$error) {
451 $this->status = self::STATUS_CLOSED;
452 $this->date_valid = $now;
453 $this->fk_user_valid = $user->id;
454 }
455
456 if (!$error && !$notrigger) {
457 // Call trigger
458 $result = $this->call_trigger('CASHCONTROL_CLOSE', $user);
459 if ($result < 0) {
460 $error++;
461 }
462 // End call triggers
463 }
464
465 // Commit or rollback
466 if ($error) {
467 foreach ($this->errors as $errmsg) {
468 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
469 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
470 }
471 $this->db->rollback();
472 return -1 * $error;
473 } else {
474 $this->db->commit();
475 return $this->id;
476 }
477 }
478
486 public function fetch($id, $ref = null)
487 {
488 $result = $this->fetchCommon($id, $ref);
489 //if ($result > 0 && !empty($this->table_element_line)) $this->fetchLines();
490 return $result;
491 }
492
500 public function update(User $user, $notrigger = 0)
501 {
502 return $this->updateCommon($user, $notrigger);
503 }
504
512 public function delete(User $user, $notrigger = 0)
513 {
514 return $this->deleteCommon($user, $notrigger);
515 //return $this->deleteCommon($user, $notrigger, 1);
516 }
517
524 public function getLibStatut($mode = 0)
525 {
526 return $this->LibStatut($this->status, $mode);
527 }
528
529 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
537 public function LibStatut($status, $mode = 0)
538 {
539 // phpcs:enable
540 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
541 global $langs;
542 //$langs->load("mymodule");
543 $this->labelStatus[0] = $langs->transnoentitiesnoconv('Draft');
544 $this->labelStatus[1] = $langs->transnoentitiesnoconv('Closed');
545 $this->labelStatusShort[0] = $langs->transnoentitiesnoconv('Draft');
546 $this->labelStatusShort[1] = $langs->transnoentitiesnoconv('Closed');
547 }
548
549 $statusType = 'status0';
550 if ($status == self::STATUS_VALIDATED) {
551 $statusType = 'status6';
552 }
553
554 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
555 }
556
567 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
568 {
569 global $conf, $langs, $hookmanager;
570
571 if (!empty($conf->dol_no_mouse_hover)) {
572 $notooltip = 1; // Force disable tooltips
573 }
574
575 $result = '';
576
577 $newref = ($this->ref ? $this->ref : $this->id);
578
579 $label = '<u>'.$langs->trans("CashControl").'</u>';
580 $label .= '<br>';
581 $label .= '<b>'.$langs->trans('Ref').':</b> '.($this->ref ? $this->ref : $this->id);
582
583 $url = DOL_URL_ROOT.'/compta/cashcontrol/cashcontrol_card.php?id='.$this->id;
584
585 if ($option != 'nolink') {
586 // Add param to save lastsearch_values or not
587 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
588 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
589 $add_save_lastsearch_values = 1;
590 }
591 if ($add_save_lastsearch_values) {
592 $url .= '&save_lastsearch_values=1';
593 }
594 }
595
596 $linkclose = '';
597 if (empty($notooltip)) {
598 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
599 $label = $langs->trans("ShowMyObject");
600 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
601 }
602 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label).'"';
603 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
604 } else {
605 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
606 }
607
608 $linkstart = '<a href="'.$url.'"';
609 $linkstart .= $linkclose.'>';
610 $linkend = '</a>';
611
612 $result .= $linkstart;
613 if ($withpicto) {
614 $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);
615 }
616 if ($withpicto != 2) {
617 $result .= $this->ref;
618 }
619 $result .= $linkend;
620 //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
621
622 global $action;
623 $hookmanager->initHooks(array('cashfencedao'));
624 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
625 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
626 if ($reshook > 0) {
627 $result = $hookmanager->resPrint;
628 } else {
629 $result .= $hookmanager->resPrint;
630 }
631
632 return $result;
633 }
634
642 public function getKanbanView($option = '', $arraydata = null)
643 {
644 global $langs;
645
646 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
647
648 $return = '<div class="box-flex-item box-flex-grow-zero">';
649 $return .= '<div class="info-box info-box-sm">';
650 $return .= '<span class="info-box-icon bg-infobox-action">';
651 $return .= img_picto('', $this->picto);
652 //var_dump($this->fields['rowid']);exit;
653 $return .= '</span>';
654 $return .= '<div class="info-box-content">';
655 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1, '', 1) : $this->ref).'</span>';
656 if ($selected >= 0) {
657 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
658 }
659 if (property_exists($this, 'posmodule')) {
660 $return .= '<br><span class="opacitymedium">'.substr($langs->trans("Module/Application"), 0, 12).'</span> : <span class="info-box-label">'.$this->posmodule.'</span>';
661 }
662 if (property_exists($this, 'year_close')) {
663 $return .= '<br><span class="info-box-label opacitymedium" >'.$langs->trans("Year").'</span> : <span>'.$this->year_close.'</span>';
664 }
665 if (method_exists($this, 'getLibStatut')) {
666 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
667 }
668 $return .= '</div>';
669 $return .= '</div>';
670 $return .= '</div>';
671 return $return;
672 }
673}
$object ref
Definition info.php:90
Class to manage cash fence.
getKanbanView($option='', $arraydata=null)
Return clickable link of object (with eventually picto)
LibStatut($status, $mode=0)
Return the status.
valid(User $user, $notrigger=0)
Validate a cash register control.
update(User $user, $notrigger=0)
Update object into database.
getNomUrl($withpicto=0, $option='', $notooltip=0, $morecss='', $save_lastsearch_value=-1)
Return clickable link of object (with eventually picto)
$fields
'type' field format ('integer', 'integer:ObjectClass:PathToClass[:AddCreateButtonOrNot[:Filter]]',...
create(User $user, $notrigger=0)
Create in database.
getLibStatut($mode=0)
Return label of the status.
close(User $user, $notrigger=0)
Close a cash register control.
__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,...
updateCommon(User $user, $notrigger=0)
Update object into database.
fetchCommon($id, $ref=null, $morewhere='', $noextrafields=0)
Load object in memory from the database.
deleteCommon(User $user, $notrigger=0, $forcechilddeletion=0)
Delete object in database.
Class to manage Dolibarr database access.
Class to manage Dolibarr users.
print $langs trans("Ref").' m titre as m m statut as status
Or an array listing all the potential status of the object: array: int of the status => translated la...
Definition index.php:168
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $db
API class for accounts.
dol_now($mode='gmt')
Return date for now.
img_picto($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $alt='', $morecss='', $marginleftonlyshort=2, $allowothertags=array())
Show picto whatever it's its name (generic function)
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
img_object($titlealt, $picto, $moreatt='', $pictoisfullpath=0, $srconly=0, $notitle=0, $allowothertags=array())
Show a picto called object_picto (generic function)
getDolGlobalString($key, $default='')
Return a Dolibarr global constant string value.
dol_syslog($message, $level=LOG_INFO, $ident=0, $suffixinfilename='', $restricttologhandler='', $logcontext=null)
Write log message into outputs.