dolibarr 25.0.0-alpha
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-2026 MDW <mdeweerd@users.noreply.github.com>
7 * Copyright (C) 2024-2026 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 ? ((int) $this->day_close) : "null");
304 $sql .= ", ".($this->month_close > 0 ? ((int) $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 if (!$error && !$notrigger) {
332 // Call trigger
333 $result = $this->call_trigger('CASHCONTROL_CREATE', $user);
334 if ($result < 0) {
335 $error++;
336 }
337 // End call triggers
338 }
339
340 // Commit or rollback
341 if ($error) {
342 foreach ($this->errors as $errmsg) {
343 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
344 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
345 }
346 $this->db->rollback();
347 return -1 * $error;
348 } else {
349 $this->db->commit();
350 return $this->id;
351 }
352 }
353
361 public function valid(User $user, $notrigger = 0)
362 {
363 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
364
365 $error = 0;
366
367 // Protection
368 if ($this->status == self::STATUS_VALIDATED) {
369 dol_syslog(get_class($this)."::valid action abandoned: already validated", LOG_WARNING);
370 return 0;
371 }
372
373 $now = dol_now();
374
375 // Update request
376 $sql = "UPDATE ".MAIN_DB_PREFIX."pos_cash_fence";
377 $sql .= " SET status = ".((int) self::STATUS_VALIDATED).",";
378 $sql .= " date_valid='".$this->db->idate($now)."',";
379 $sql .= " fk_user_valid = ".((int) $user->id);
380 $sql .= " WHERE rowid=".((int) $this->id);
381
382 $this->db->begin();
383
384 dol_syslog(get_class($this)."::close", LOG_DEBUG);
385 $resql = $this->db->query($sql);
386 if (!$resql) {
387 $error++;
388 $this->errors[] = "Error ".$this->db->lasterror();
389 }
390
391 if (!$error) {
392 $this->status = self::STATUS_VALIDATED;
393 $this->date_valid = $now;
394 $this->fk_user_valid = $user->id;
395 }
396
397 if (!$error && !$notrigger) {
398 // Call trigger
399 $result = $this->call_trigger('CASHCONTROL_VALIDATE', $user);
400 if ($result < 0) {
401 $error++;
402 }
403 // End call triggers
404 }
405
406 // Commit or rollback
407 if ($error) {
408 foreach ($this->errors as $errmsg) {
409 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
410 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
411 }
412 $this->db->rollback();
413 return -1 * $error;
414 } else {
415 $this->db->commit();
416 return $this->id;
417 }
418 }
419
420
428 public function close(User $user, $notrigger = 0)
429 {
430 require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
431
432 $error = 0;
433
434 // Protection
435 if ($this->status == self::STATUS_CLOSED) {
436 $this->error = get_class($this)."::valid action abandoned: already validated";
437 dol_syslog($this->error, LOG_WARNING);
438 return 0;
439 }
440
441 $now = dol_now();
442
443 // Update request
444 $sql = "UPDATE ".MAIN_DB_PREFIX."pos_cash_fence";
445 $sql .= " SET status = ".((int) self::STATUS_CLOSED).",";
446 $sql .= " date_valid = '".$this->db->idate($now)."',";
447 $sql .= " fk_user_valid = ".((int) $user->id);
448 $sql .= " WHERE rowid=".((int) $this->id);
449
450 $this->db->begin();
451
452 dol_syslog(get_class($this)."::close", LOG_DEBUG);
453 $resql = $this->db->query($sql);
454 if (!$resql) {
455 $error++;
456 $this->errors[] = "Error ".$this->db->lasterror();
457 }
458
459 if (!$error) {
460 $this->status = self::STATUS_CLOSED;
461 $this->date_valid = $now;
462 $this->fk_user_valid = $user->id;
463 }
464
465 if (!$error && !$notrigger) {
466 // Call trigger
467 $result = $this->call_trigger('CASHCONTROL_CLOSE', $user);
468 if ($result < 0) {
469 $error++;
470 }
471 // End call triggers
472 }
473
474 // Commit or rollback
475 if ($error) {
476 foreach ($this->errors as $errmsg) {
477 dol_syslog(get_class($this)."::create ".$errmsg, LOG_ERR);
478 $this->error .= ($this->error ? ', '.$errmsg : $errmsg);
479 }
480 $this->db->rollback();
481 return -1 * $error;
482 } else {
483 $this->db->commit();
484 return $this->id;
485 }
486 }
487
495 public function fetch($id, $ref = null)
496 {
497 $result = $this->fetchCommon($id, $ref);
498 //if ($result > 0 && !empty($this->table_element_line)) $this->fetchLines();
499 return $result;
500 }
501
509 public function update(User $user, $notrigger = 0)
510 {
511 return $this->updateCommon($user, $notrigger);
512 }
513
521 public function delete(User $user, $notrigger = 0)
522 {
523 return $this->deleteCommon($user, $notrigger);
524 //return $this->deleteCommon($user, $notrigger, 1);
525 }
526
533 public function getLibStatut($mode = 0)
534 {
535 return $this->LibStatut($this->status, $mode);
536 }
537
538 // phpcs:disable PEAR.NamingConventions.ValidFunctionName.ScopeNotCamelCaps
546 public function LibStatut($status, $mode = 0)
547 {
548 // phpcs:enable
549 if (empty($this->labelStatus) || empty($this->labelStatusShort)) {
550 global $langs;
551 //$langs->load("mymodule");
552 $this->labelStatus[0] = $langs->transnoentitiesnoconv('Draft');
553 $this->labelStatus[1] = $langs->transnoentitiesnoconv('Closed');
554 $this->labelStatusShort[0] = $langs->transnoentitiesnoconv('Draft');
555 $this->labelStatusShort[1] = $langs->transnoentitiesnoconv('Closed');
556 }
557
558 $statusType = 'status0';
559 if ($status == self::STATUS_VALIDATED) {
560 $statusType = 'status6';
561 }
562
563 return dolGetStatus($this->labelStatus[$status], $this->labelStatusShort[$status], '', $statusType, $mode);
564 }
565
576 public function getNomUrl($withpicto = 0, $option = '', $notooltip = 0, $morecss = '', $save_lastsearch_value = -1)
577 {
578 global $conf, $langs, $hookmanager;
579
580 if (!empty($conf->dol_no_mouse_hover)) {
581 $notooltip = 1; // Force disable tooltips
582 }
583
584 $result = '';
585
586 $newref = ($this->ref ? $this->ref : $this->id);
587
588 $label = '<u>'.$langs->trans("CashControl").'</u>';
589 $label .= '<br>';
590 $label .= '<b>'.$langs->trans('Ref').':</b> '.($this->ref ? $this->ref : $this->id);
591
592 $url = DOL_URL_ROOT.'/compta/cashcontrol/cashcontrol_card.php?id='.$this->id;
593
594 if ($option != 'nolink') {
595 // Add param to save lastsearch_values or not
596 $add_save_lastsearch_values = ($save_lastsearch_value == 1 ? 1 : 0);
597 if ($save_lastsearch_value == -1 && isset($_SERVER["PHP_SELF"]) && preg_match('/list\.php/', $_SERVER["PHP_SELF"])) {
598 $add_save_lastsearch_values = 1;
599 }
600 if ($add_save_lastsearch_values) {
601 $url .= '&save_lastsearch_values=1';
602 }
603 }
604
605 $linkclose = '';
606 if (empty($notooltip)) {
607 if (getDolGlobalString('MAIN_OPTIMIZEFORTEXTBROWSER')) {
608 $label = $langs->trans("ShowMyObject");
609 $linkclose .= ' alt="'.dolPrintHTMLForAttribute($label).'"';
610 }
611 $linkclose .= ' title="'.dolPrintHTMLForAttribute($label).'"';
612 $linkclose .= ' class="classfortooltip'.($morecss ? ' '.$morecss : '').'"';
613 } else {
614 $linkclose = ($morecss ? ' class="'.$morecss.'"' : '');
615 }
616
617 $linkstart = '<a href="'.$url.'"';
618 $linkstart .= $linkclose.'>';
619 $linkend = '</a>';
620
621 $result .= $linkstart;
622 if ($withpicto) {
623 $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);
624 }
625 if ($withpicto != 2) {
626 $result .= $this->ref;
627 }
628 $result .= $linkend;
629 //if ($withpicto != 2) $result.=(($addlabel && $this->label) ? $sep . dol_trunc($this->label, ($addlabel > 1 ? $addlabel : 0)) : '');
630
631 global $action;
632 $hookmanager->initHooks(array('cashfencedao'));
633 $parameters = array('id' => $this->id, 'getnomurl' => &$result);
634 $reshook = $hookmanager->executeHooks('getNomUrl', $parameters, $this, $action); // Note that $action and $object may have been modified by some hooks
635 if ($reshook > 0) {
636 $result = $hookmanager->resPrint;
637 } else {
638 $result .= $hookmanager->resPrint;
639 }
640
641 return $result;
642 }
643
651 public function getKanbanView($option = '', $arraydata = null)
652 {
653 global $langs;
654
655 $selected = (empty($arraydata['selected']) ? 0 : $arraydata['selected']);
656
657 $return = '<div class="box-flex-item box-flex-grow-zero">';
658 $return .= '<div class="info-box info-box-sm">';
659 $return .= '<span class="info-box-icon bg-infobox-action">';
660 $return .= img_picto('', $this->picto);
661 //var_dump($this->fields['rowid']);exit;
662 $return .= '</span>';
663 $return .= '<div class="info-box-content">';
664 $return .= '<span class="info-box-ref inline-block tdoverflowmax150 valignmiddle">'.(method_exists($this, 'getNomUrl') ? $this->getNomUrl(1, '', 1) : $this->ref).'</span>';
665 if ($selected >= 0) {
666 $return .= '<input id="cb'.$this->id.'" class="flat checkforselect fright" type="checkbox" name="toselect[]" value="'.$this->id.'"'.($selected ? ' checked="checked"' : '').'>';
667 }
668 if (property_exists($this, 'posmodule')) {
669 $return .= '<br><span class="opacitymedium">'.substr($langs->trans("Module/Application"), 0, 12).'</span> : <span class="info-box-label">'.$this->posmodule.'</span>';
670 }
671 if (property_exists($this, 'year_close')) {
672 $return .= '<br><span class="info-box-label opacitymedium" >'.$langs->trans("Year").'</span> : <span>'.$this->year_close.'</span>';
673 }
674 if (method_exists($this, 'getLibStatut')) {
675 $return .= '<br><div class="info-box-status">'.$this->getLibStatut(3).'</div>';
676 }
677 $return .= '</div>';
678 $return .= '</div>';
679 $return .= '</div>';
680 return $return;
681 }
682}
$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.
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:169
if(!isModEnabled('ai')||!getDolGlobalString('AI_ASSISTANT_ENABLED')) global $conf
The main.inc.php has been included so the following variable are now defined:
dol_now($mode='gmt')
Return date for now.
price2num($amount, $rounding='', $option=0)
Function that return a number with universal decimal format (decimal separator is '.
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.