From e5e4ef7b8c41a3c51e937ba0a1dd36206a4116e0 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 16 Dec 2018 16:47:09 +0100 Subject: [PATCH 1/4] Work on cash fence --- .../compta/cashcontrol/cashcontrol_card.php | 156 ++++++++++++++---- .../cashcontrol/class/cashcontrol.class.php | 23 ++- htdocs/core/menus/standard/eldy.lib.php | 7 +- 3 files changed, 148 insertions(+), 38 deletions(-) diff --git a/htdocs/compta/cashcontrol/cashcontrol_card.php b/htdocs/compta/cashcontrol/cashcontrol_card.php index 9e288d4b9a6..cfb9d3de8bc 100644 --- a/htdocs/compta/cashcontrol/cashcontrol_card.php +++ b/htdocs/compta/cashcontrol/cashcontrol_card.php @@ -22,9 +22,9 @@ */ /** - * \file htdocs/compta/bank/categ.php - * \ingroup pos - * \brief Page ajout de categories bancaires + * \file htdocs/compta/cashcontrol/cashcontrol_card.php + * \ingroup cashdesk|takepos + * \brief Page to show a cash fence */ require '../../main.inc.php'; @@ -55,6 +55,15 @@ if (! $user->rights->cashdesk->use && ! $user->rights->takepos->use) accessforbidden(); } +$arrayofpaymentmode=array('cash'=>'Cash', 'cheque'=>'Cheque', 'card'=>'CreditCard'); + +$arrayofposavailable=array(); +if (! empty($conf->cashdesk->enabled)) $arrayofposavailable['cashdesk']=$langs->trans('CashDesk').' (cashdesk)'; +if (! empty($conf->takepos->enabled)) $arrayofposavailable['takepos']=$langs->trans('TakePOS').' (takepos)'; +// TODO Add hook here to allow other POS to add themself + +$cashcontrol= new CashControl($db); + /* @@ -70,21 +79,55 @@ if ($action=="start") $action='create'; $error++; } - if (GETPOST('opening','alpha') == '') + if (GETPOST('posnumber','alpha') == '') { - setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("InitialBankBalance")), null, 'errors'); + setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("CashDesk")), null, 'errors'); $action='create'; $error++; } + if (! GETPOST('closeyear','alpha') || GETPOST('closeyear','alpha') == '-1') + { + setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Year")), null, 'errors'); + $action='create'; + $error++; + } +} +elseif ($action=="add") +{ + $error=0; + if (GETPOST('opening','alpha') == '') + { + setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("InitialBankBalance")), null, 'errors'); + $action='start'; + $error++; + } + foreach($arrayofpaymentmode as $key=>$val) + { + if (GETPOST($key,'alpha') == '') + { + setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv($val)), null, 'errors'); + $action='start'; + $error++; + } + else + { + $cashcontrol->$key = price2num(GETPOST($key,'alpha')); + } + } + if (! $error) { - $cashcontrol= new CashControl($db); - $cashcontrol->opening=GETPOST('opening'); - if (GETPOST('posmodule')=='cashdesk') $cashcontrol->posmodule="cashdesk"; - else if (GETPOST('posmodule')=='takepos') $cashcontrol->posmodule="takepos"; - $cashcontrol->posnumber=GETPOST('posnumber'); + $cashcontrol->day_close = GETPOST('closeday', 'int'); + $cashcontrol->month_close = GETPOST('closemonth', 'int'); + $cashcontrol->year_close = GETPOST('closeyear', 'int'); + + $cashcontrol->opening=price2num(GETPOST('opening','alpha')); + $cashcontrol->posmodule=GETPOST('posmodule','alpha'); + $cashcontrol->posnumber=GETPOST('posnumber','alpha'); + $id=$cashcontrol->create($user); - $action="view"; + + $action="view"; } } @@ -96,18 +139,32 @@ if ($action=="close") $action="view"; } -if ($action=="create") +if ($action=="create" || $action=="start") { llxHeader(); - $arrayofposavailable=array(); - if (! empty($conf->cashdesk->enabled)) $arrayofposavailable['cashdesk']=$langs->trans('CashDesk').' (cashdesk)'; - if (! empty($conf->takepos->enabled)) $arrayofposavailable['takepos']=$langs->trans('TakePOS').' (takepos)'; - // TODO Add hook here to allow other POS to add themself + $initialbalanceforterminal=array(); + $theoricalamountforterminal=array(); + + if (GETPOST('posnumber') != '' && GETPOST('posnumber') != '' && GETPOST('posnumber') != '-1') + { + // Calculate $initialbalanceforterminal and $theoricalamountforterminal for terminal 0 + // TODO + + + } print load_fiche_titre($langs->trans("CashControl")." - ".$langs->trans("New"), '', 'title_bank.png'); - print '
'; - print ''; + + print ''; + if ($action == 'start' && GETPOST('posnumber') != '' && GETPOST('posnumber') != '' && GETPOST('posnumber') != '-1') + { + print ''; + } + else + { + print ''; + } print ''; print ''; print ''; @@ -115,22 +172,22 @@ if ($action=="create") print ''; print ''; print ''; - print ''; print ''; print "\n"; $now=dol_now(); - $syear = dol_print_date($now, "%Y"); - $smonth = dol_print_date($now, "%m"); - $sday = dol_print_date($now, "%d"); + $syear = (GETPOSTISSET('closeyear')?GETPOST('closeyear', 'int'):dol_print_date($now, "%Y")); + $smonth = (GETPOSTISSET('closemonth')?GETPOST('closemonth', 'int'):dol_print_date($now, "%m")); + $sday = (GETPOSTISSET('closeday')?GETPOST('closeday', 'int'):dol_print_date($now, "%d")); + $disabled=0; + $prefix='close'; print ''; - print ''; - print ''; + print ''; + print ''; // Year print ''; - print ''; - print ''; - print ''; - print '
'.$langs->trans("Module").''.$langs->trans("Year").''.$langs->trans("Month").''.$langs->trans("Day").''.$langs->trans("InitialBankBalance").'
'.$form->selectarray('posmodule', $arrayofposavailable, 1, (count($arrayofposavailable)>1?1:0)).''.$form->selectarray('posmodule', $arrayofposavailable, GETPOST('posmodule','alpha'), (count($arrayofposavailable)>1?1:0)).''; $retstring=''; - for ($year = $syear - 10; $year < $syear + 10 ; $year++) { $retstring.=''; @@ -141,6 +198,7 @@ if ($action=="create") // Month print ''; $retstring=''; + $retstring.=''; for ($month = 1 ; $month <= 12 ; $month++) { $retstring.=''; $retstring=''; - if ($emptydate || $set_time == -1) - { - $retstring.=''; - } + $retstring.=''; for ($day = 1 ; $day <= 31; $day++) { $retstring.=''; @@ -164,10 +219,45 @@ if ($action=="create") $retstring.=""; print $retstring; print '
'; + print ''; + if ($action == 'start' && GETPOST('posnumber') != '' && GETPOST('posnumber') != '' && GETPOST('posnumber') != '-1') + { + print ''; + } + else + { + print ''; + } + print ''; + print ''; + + if ($action == 'start' && GETPOST('posnumber') != '' && GETPOST('posnumber') != '' && GETPOST('posnumber') != '-1') + { + print ''; + print ''; + print ''; + foreach($arrayofpaymentmode as $key => $val) + { + print ''; + } + print ''; + print ''; + print ''; + // Initial amount + print ''; + foreach($arrayofpaymentmode as $key => $val) + { + print ''; + } + + print ''; + print ''; + print ''; + } + print ''; } if (empty($action) || $action=="view") diff --git a/htdocs/compta/cashcontrol/class/cashcontrol.class.php b/htdocs/compta/cashcontrol/class/cashcontrol.class.php index 0b10d49c4bf..8f788d29e15 100644 --- a/htdocs/compta/cashcontrol/class/cashcontrol.class.php +++ b/htdocs/compta/cashcontrol/class/cashcontrol.class.php @@ -83,7 +83,9 @@ class CashControl extends CommonObject public $day_close; public $posmodule; public $posnumber; - + public $cash; + public $cheque; + public $card; /** @@ -110,6 +112,11 @@ class CashControl extends CommonObject $error = 0; + // Clean data + if (empty($this->cash)) $this->cash=0; + if (empty($this->cheque)) $this->cheque=0; + if (empty($this->card)) $this->card=0; + // Insert request $sql = "INSERT INTO ".MAIN_DB_PREFIX."pos_cash_fence ("; $sql .= "entity"; @@ -119,14 +126,26 @@ class CashControl extends CommonObject $sql .= ", date_creation"; $sql .= ", posmodule"; $sql .= ", posnumber"; + $sql .= ", day_close"; + $sql .= ", month_close"; + $sql .= ", year_close"; + $sql .= ", cash"; + $sql .= ", cheque"; + $sql .= ", card"; $sql .= ") VALUES ("; //$sql .= "'(PROV)', "; $sql .= $conf->entity; - $sql .= ", ".$this->opening; + $sql .= ", ".($this->opening > 0 ? $this->opening : 0); $sql .= ", 0"; // Draft by default $sql .= ", '".$this->db->idate(dol_now())."'"; $sql .= ", '".$this->db->escape($this->posmodule)."'"; $sql .= ", '".$this->db->escape($this->posnumber)."'"; + $sql .= ", ".($this->day_close > 0 ? $this->day_close : "null"); + $sql .= ", ".($this->month_close > 0 ? $this->month_close : "null"); + $sql .= ", ".$this->year_close; + $sql .= ", ".$this->cash; + $sql .= ", ".$this->cheque; + $sql .= ", ".$this->card; $sql .= ")"; $this->db->begin(); diff --git a/htdocs/core/menus/standard/eldy.lib.php b/htdocs/core/menus/standard/eldy.lib.php index 7c736465761..32973e845c7 100644 --- a/htdocs/core/menus/standard/eldy.lib.php +++ b/htdocs/core/menus/standard/eldy.lib.php @@ -1248,9 +1248,10 @@ function print_left_eldy_menu($db,$menu_array_before,$menu_array_after,&$tabMenu // Cash Control if (! empty($conf->takepos->enabled) || ! empty($conf->cashdesk->enabled)) { - $newmenu->add("/compta/cashcontrol/cashcontrol_list.php?action=list",$langs->trans("POS"),0,1, '', $mainmenu, 'cashcontrol'); - $newmenu->add("/compta/cashcontrol/cashcontrol_card.php?action=create",$langs->trans("NewCashFence"),1,1); - $newmenu->add("/compta/cashcontrol/cashcontrol_list.php?action=list",$langs->trans("List"),1,1); + $permtomakecashfence = ($user->rights->cashdesk->use ||$user->rights->takepos->use); + $newmenu->add("/compta/cashcontrol/cashcontrol_list.php?action=list",$langs->trans("POS"),0,$permtomakecashfence, '', $mainmenu, 'cashcontrol'); + $newmenu->add("/compta/cashcontrol/cashcontrol_card.php?action=create",$langs->trans("NewCashFence"),1,$permtomakecashfence); + $newmenu->add("/compta/cashcontrol/cashcontrol_list.php?action=list",$langs->trans("List"),1,$permtomakecashfence); } } From ce2d67f22568761821e0fb31821c15953e7139cd Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 16 Dec 2018 17:45:51 +0100 Subject: [PATCH 2/4] Fix phpcs --- htdocs/compta/cashcontrol/cashcontrol_card.php | 2 -- 1 file changed, 2 deletions(-) diff --git a/htdocs/compta/cashcontrol/cashcontrol_card.php b/htdocs/compta/cashcontrol/cashcontrol_card.php index cfb9d3de8bc..4311a3348bc 100644 --- a/htdocs/compta/cashcontrol/cashcontrol_card.php +++ b/htdocs/compta/cashcontrol/cashcontrol_card.php @@ -150,8 +150,6 @@ if ($action=="create" || $action=="start") { // Calculate $initialbalanceforterminal and $theoricalamountforterminal for terminal 0 // TODO - - } print load_fiche_titre($langs->trans("CashControl")." - ".$langs->trans("New"), '', 'title_bank.png'); From 72c5943c5583590d9c84b5f3a83cc6a7407c3f6c Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Sun, 16 Dec 2018 17:53:23 +0100 Subject: [PATCH 3/4] Execute trigger so we can record line into unalterable log table --- .../cashcontrol/class/cashcontrol.class.php | 28 ++++++++++++------- 1 file changed, 18 insertions(+), 10 deletions(-) diff --git a/htdocs/compta/cashcontrol/class/cashcontrol.class.php b/htdocs/compta/cashcontrol/class/cashcontrol.class.php index 8f788d29e15..a49cb68e57a 100644 --- a/htdocs/compta/cashcontrol/class/cashcontrol.class.php +++ b/htdocs/compta/cashcontrol/class/cashcontrol.class.php @@ -87,6 +87,9 @@ class CashControl extends CommonObject public $cheque; public $card; + const STATUS_DRAFT = 0; + const STATUS_VALIDATE = 1; + /** * Constructor @@ -187,21 +190,16 @@ class CashControl extends CommonObject */ public function close(User $user, $notrigger = 0) { - global $conf; - $error = 0; // Update request - $sql = "UPDATE ".MAIN_DB_PREFIX."pos_cash_fence "; - $sql.= "SET"; - $sql.= " day_close=DAYOFMONTH(NOW())"; - $sql.= ", month_close=MONTH(NOW())"; - $sql.= ", year_close=YEAR(NOW())"; - $sql.= ", status=2"; - $sql.= " where rowid=".$this->id; + $sql = "UPDATE ".MAIN_DB_PREFIX."pos_cash_fence"; + $sql.= " SET status = ".self::STATUS_VALIDATED; + $sql.= " WHERE rowid=".$this->id; + $this->db->begin(); - dol_syslog(get_class($this)."::create", LOG_DEBUG); + dol_syslog(get_class($this)."::close", LOG_DEBUG); $resql = $this->db->query($sql); if (!$resql) { $error++; @@ -212,6 +210,16 @@ class CashControl extends CommonObject $this->id = $this->db->last_insert_id(MAIN_DB_PREFIX."pos_cash_fence"); } + if (! $error && ! $notrigger) + { + $this->context=array('operation'=>'close'); + + // Call trigger + $result=$this->call_trigger('CASHCONTROL_MODIFY', $user); + if ($result < 0) $error++; + // End call triggers + } + // Commit or rollback if ($error) { foreach ($this->errors as $errmsg) { From bf9ee18bed424dbc3f44af4f278235832e0c16c1 Mon Sep 17 00:00:00 2001 From: Laurent Destailleur Date: Mon, 17 Dec 2018 03:25:45 +0100 Subject: [PATCH 4/4] Standardize code. --- .../compta/cashcontrol/cashcontrol_card.php | 2 +- .../cashcontrol/class/cashcontrol.class.php | 34 ++++++++++++++++--- .../install/mysql/migration/8.0.0-9.0.0.sql | 1 + .../mysql/tables/llx_pos_cash_fence.sql | 1 + 4 files changed, 32 insertions(+), 6 deletions(-) diff --git a/htdocs/compta/cashcontrol/cashcontrol_card.php b/htdocs/compta/cashcontrol/cashcontrol_card.php index 4311a3348bc..e212f8ff75b 100644 --- a/htdocs/compta/cashcontrol/cashcontrol_card.php +++ b/htdocs/compta/cashcontrol/cashcontrol_card.php @@ -135,7 +135,7 @@ if ($action=="close") { $cashcontrol= new CashControl($db); $cashcontrol->id=$id; - $cashcontrol->close($user); + $cashcontrol->valid($user); $action="view"; } diff --git a/htdocs/compta/cashcontrol/class/cashcontrol.class.php b/htdocs/compta/cashcontrol/class/cashcontrol.class.php index a49cb68e57a..37f73f6712f 100644 --- a/htdocs/compta/cashcontrol/class/cashcontrol.class.php +++ b/htdocs/compta/cashcontrol/class/cashcontrol.class.php @@ -182,19 +182,43 @@ class CashControl extends CommonObject } /** - * close + * Validate cash fence * * @param User $user User * @param number $notrigger No trigger * @return int <0 if KO, >0 if OK */ - public function close(User $user, $notrigger = 0) + public function valid(User $user, $notrigger = 0) { + global $conf,$langs; + require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php'; + $error = 0; + // Protection + if ($this->status == self::STATUS_VALIDATED) + { + dol_syslog(get_class($this)."::valid action abandonned: already validated", LOG_WARNING); + return 0; + } + + /* + $posmodule = $this->posmodule; + if (! empty($user->rights->$posmodule->use)) + { + $this->error='NotEnoughPermissions'; + dol_syslog(get_class($this)."::valid ".$this->error, LOG_ERR); + return -1; + } + */ + + $now=dol_now(); + // Update request $sql = "UPDATE ".MAIN_DB_PREFIX."pos_cash_fence"; - $sql.= " SET status = ".self::STATUS_VALIDATED; + $sql.= " SET status = ".self::STATUS_VALIDATED.","; + $sql.= " date_valid='".$this->db->idate($now)."',"; + $sql.= " fk_user_valid = ".$user->id; $sql.= " WHERE rowid=".$this->id; $this->db->begin(); @@ -212,10 +236,10 @@ class CashControl extends CommonObject if (! $error && ! $notrigger) { - $this->context=array('operation'=>'close'); + $this->context=array('date_valid'=>$now); // Call trigger - $result=$this->call_trigger('CASHCONTROL_MODIFY', $user); + $result=$this->call_trigger('CASHCONTROL_VALIDATE', $user); if ($result < 0) $error++; // End call triggers } diff --git a/htdocs/install/mysql/migration/8.0.0-9.0.0.sql b/htdocs/install/mysql/migration/8.0.0-9.0.0.sql index 7f50d9fe0d5..28d080584d1 100644 --- a/htdocs/install/mysql/migration/8.0.0-9.0.0.sql +++ b/htdocs/install/mysql/migration/8.0.0-9.0.0.sql @@ -242,6 +242,7 @@ CREATE TABLE llx_pos_cash_fence( cheque double(24,8) default 0, status INTEGER, date_creation DATETIME NOT NULL, + date_valid DATETIME, day_close INTEGER, month_close INTEGER, year_close INTEGER, diff --git a/htdocs/install/mysql/tables/llx_pos_cash_fence.sql b/htdocs/install/mysql/tables/llx_pos_cash_fence.sql index 26a78d69f00..607060adab6 100644 --- a/htdocs/install/mysql/tables/llx_pos_cash_fence.sql +++ b/htdocs/install/mysql/tables/llx_pos_cash_fence.sql @@ -25,6 +25,7 @@ CREATE TABLE llx_pos_cash_fence( cheque double(24,8) default 0, status INTEGER, date_creation DATETIME NOT NULL, + date_valid DATETIME, day_close INTEGER, month_close INTEGER, year_close INTEGER,
'.$langs->trans("InitialBankBalance").''.$langs->trans($val).'
'.$langs->trans("TheoricalAmount").'
'.$langs->trans("RealAmount").'
'; + print price($theoricalamountforterminal[0][$key]).'
'; + print ''; + print '