* Copyright (C) 2004-2013 Laurent Destailleur * Copyright (C) 2005-2009 Regis Houssin * Copyright (C) 2013 Charles-Fr BENKE * Copyright (C) 2015 Jean-François Ferry * Copyright (C) 2016 Marcos García * Copyright (C) 2018 Andreu Bisquerra * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see . */ /** * \file htdocs/compta/cashcontrol/cashcontrol_card.php * \ingroup cashdesk|takepos * \brief Page to show a cash fence */ // Load Dolibarr environment require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; require_once DOL_DOCUMENT_ROOT.'/compta/cashcontrol/class/cashcontrol.class.php'; $langs->loadLangs(array("install", "cashdesk", "admin", "banks")); $id = GETPOST('id', 'int'); $ref = GETPOST('ref', 'alpha'); $action = GETPOST('action', 'aZ09'); $categid = GETPOST('categid'); $label = GETPOST("label"); $now = dol_now(); $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")); $limit = GETPOST('limit', 'int') ?GETPOST('limit', 'int') : $conf->liste_limit; $sortfield = GETPOST('sortfield', 'aZ09comma'); $sortorder = GETPOST('sortorder', 'aZ09comma'); $page = GETPOSTISSET('pageplusone') ? (GETPOST('pageplusone') - 1) : GETPOST("page", 'int'); if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1 $offset = $limit * $page; $pageprev = $page - 1; $pagenext = $page + 1; if (!$sortfield) { $sortfield = 'rowid'; } if (!$sortorder) { $sortorder = 'ASC'; } $contextpage = GETPOST('contextpage', 'aZ') ?GETPOST('contextpage', 'aZ') : 'thirdpartylist'; if ($contextpage == 'takepos') { $_GET['optioncss'] = 'print'; } $arrayofpaymentmode = array('cash'=>'Cash', 'cheque'=>'Cheque', 'card'=>'CreditCard'); $arrayofposavailable = array(); if (isModEnabled('cashdesk')) { $arrayofposavailable['cashdesk'] = $langs->trans('CashDesk').' (cashdesk)'; } if (isModEnabled('takepos')) { $arrayofposavailable['takepos'] = $langs->trans('TakePOS').' (takepos)'; } // TODO Add hook here to allow other POS to add themself $object = new CashControl($db); $extrafields = new ExtraFields($db); // fetch optionals attributes and labels $extrafields->fetch_name_optionals_label($object->table_element); // Initialize technical object to manage hooks of page. Note that conf->hooks_modules contains array of hook context $hookmanager->initHooks(array('cashcontrolcard', 'globalcard')); // Load object include DOL_DOCUMENT_ROOT.'/core/actions_fetchobject.inc.php'; // Must be include, not include_once. // Security check if ($user->socid > 0) { // Protection if external user //$socid = $user->socid; accessforbidden(); } if (!$user->hasRight("cashdesk", "run") && !$user->hasRight("takepos", "run")) { accessforbidden(); } /* * Actions */ $permissiontoadd = ($user->hasRight("cashdesk", "run") || $user->hasRight("takepos", "run")); $permissiontodelete = ($user->hasRight("cashdesk", "run") || $user->hasRight("takepos", "run")) || ($permissiontoadd && $object->status == 0); if (empty($backtopage)) { $backtopage = DOL_URL_ROOT.'/compta/cashcontrol/cashcontrol_card.php?id='.(!empty($id) && $id > 0 ? $id : '__ID__'); } $backurlforlist = DOL_URL_ROOT.'/compta/cashcontrol/cashcontrol_list.php'; $triggermodname = 'CACHCONTROL_MODIFY'; // Name of trigger action code to execute when we modify record if (empty($conf->global->CASHDESK_ID_BANKACCOUNT_CASH) && empty($conf->global->CASHDESK_ID_BANKACCOUNT_CASH1)) { setEventMessages($langs->trans("CashDesk")." - ".$langs->trans("NotConfigured"), null, 'errors'); } if (GETPOST('cancel', 'alpha')) { if ($action == 'valid') { $action = 'view'; } else { $action = 'create'; } } if ($action == "reopen") { $result = $object->setStatut($object::STATUS_DRAFT, null, '', 'CASHFENCE_REOPEN'); if ($result < 0) { setEventMessages($object->error, $object->errors, 'errors'); } $action = 'view'; } if ($action == "start") { $error = 0; if (!GETPOST('posmodule', 'alpha') || GETPOST('posmodule', 'alpha') == '-1') { setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Module")), null, 'errors'); $action = 'create'; $error++; } if (GETPOST('posnumber', 'alpha') == '') { 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") { if (GETPOST('opening', 'alpha') == '') { setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("InitialBankBalance")), null, 'errors'); $action = 'start'; $error++; } $error = 0; foreach ($arrayofpaymentmode as $key => $val) { $object->$key = price2num(GETPOST($key.'_amount', 'alpha')); } if (!$error) { $object->day_close = GETPOST('closeday', 'int'); $object->month_close = GETPOST('closemonth', 'int'); $object->year_close = GETPOST('closeyear', 'int'); $object->opening = price2num(GETPOST('opening', 'alpha')); $object->posmodule = GETPOST('posmodule', 'alpha'); $object->posnumber = GETPOST('posnumber', 'alpha'); $db->begin(); $id = $object->create($user); if ($id > 0) { $db->commit(); $action = "view"; } else { $db->rollback; $action = "view"; } } if ($contextpage == 'takepos') { print " "; exit; } } if ($action == "valid") { // validate = close $object->fetch($id); $db->begin(); /* $object->day_close = GETPOST('closeday', 'int'); $object->month_close = GETPOST('closemonth', 'int'); $object->year_close = GETPOST('closeyear', 'int'); */ $object->cash = price2num(GETPOST('cash_amount', 'alpha')); $object->card = price2num(GETPOST('card_amount', 'alpha')); $object->cheque = price2num(GETPOST('cheque_amount', 'alpha')); $result = $object->update($user); $result = $object->valid($user); if ($result <= 0) { setEventMessages($object->error, $object->errors, 'errors'); $db->rollback(); } else { setEventMessages($langs->trans("CashFenceDone"), null); $db->commit(); } if ($contextpage == 'takepos') { print " "; exit; } $action = "view"; } // Action to delete if ($action == 'confirm_delete' && !empty($permissiontodelete)) { $object->fetch($id); if (!($object->id > 0)) { dol_print_error('', 'Error, object must be fetched before being deleted'); exit; } $result = $object->delete($user); //var_dump($result); if ($result > 0) { // Delete OK setEventMessages("RecordDeleted", null, 'mesgs'); header("Location: ".$backurlforlist); exit; } else { if (!empty($object->errors)) { setEventMessages(null, $object->errors, 'errors'); } else { setEventMessages($object->error, null, 'errors'); } } } /* * View */ $form = new Form($db); $initialbalanceforterminal = array(); $theoricalamountforterminal = array(); $theoricalnbofinvoiceforterminal = array(); llxHeader('', $langs->trans("CashControl")); if ($action == "create" || $action == "start" || $action == 'close') { if ($action == 'close') { $posmodule = $object->posmodule; $terminalid = $object->posnumber; $terminaltouse = $terminalid; $syear = $object->year_close; $smonth = $object->month_close; $sday = $object->day_close; } elseif (GETPOST('posnumber', 'alpha') != '' && GETPOST('posnumber', 'alpha') != '' && GETPOST('posnumber', 'alpha') != '-1') { $posmodule = GETPOST('posmodule', 'alpha'); $terminalid = GETPOST('posnumber', 'alpha'); $terminaltouse = $terminalid; if ($terminaltouse == '1' && $posmodule == 'cashdesk') { $terminaltouse = ''; } if ($posmodule == 'cashdesk' && $terminaltouse != '' && $terminaltouse != '1') { $terminaltouse = ''; setEventMessages($langs->trans("OnlyTerminal1IsAvailableForCashDeskModule"), null, 'errors'); $error++; } } if ($terminalid != '') { // Calculate $initialbalanceforterminal for terminal 0 foreach ($arrayofpaymentmode as $key => $val) { if ($key != 'cash') { $initialbalanceforterminal[$terminalid][$key] = 0; continue; } // Get the bank account dedicated to this point of sale module/terminal $vartouse = 'CASHDESK_ID_BANKACCOUNT_CASH'.$terminaltouse; $bankid = $conf->global->$vartouse; if ($bankid > 0) { $sql = "SELECT SUM(amount) as total FROM ".MAIN_DB_PREFIX."bank"; $sql .= " WHERE fk_account = ".((int) $bankid); if ($syear && !$smonth) { $sql .= " AND dateo < '".$db->idate(dol_get_first_day($syear, 1))."'"; } elseif ($syear && $smonth && !$sday) { $sql .= " AND dateo < '".$db->idate(dol_get_first_day($syear, $smonth))."'"; } elseif ($syear && $smonth && $sday) { $sql .= " AND dateo < '".$db->idate(dol_mktime(0, 0, 0, $smonth, $sday, $syear))."'"; } else { setEventMessages($langs->trans('YearNotDefined'), null, 'errors'); } $resql = $db->query($sql); if ($resql) { $obj = $db->fetch_object($resql); if ($obj) { $initialbalanceforterminal[$terminalid][$key] = $obj->total; } } else { dol_print_error($db); } } else { setEventMessages($langs->trans("SetupOfTerminalNotComplete", $terminaltouse), null, 'errors'); $error++; } } // Calculate $theoricalamountforterminal foreach ($arrayofpaymentmode as $key => $val) { $sql = "SELECT SUM(pf.amount) as total, COUNT(*) as nb"; $sql .= " FROM ".MAIN_DB_PREFIX."paiement_facture as pf, ".MAIN_DB_PREFIX."facture as f, ".MAIN_DB_PREFIX."paiement as p, ".MAIN_DB_PREFIX."c_paiement as cp"; $sql .= " WHERE pf.fk_facture = f.rowid AND p.rowid = pf.fk_paiement AND cp.id = p.fk_paiement"; $sql .= " AND f.module_source = '".$db->escape($posmodule)."'"; $sql .= " AND f.pos_source = '".$db->escape($terminalid)."'"; $sql .= " AND f.paye = 1"; $sql .= " AND p.entity IN (".getEntity('facture').")"; if ($key == 'cash') { $sql .= " AND cp.code = 'LIQ'"; } elseif ($key == 'cheque') { $sql .= " AND cp.code = 'CHQ'"; } elseif ($key == 'card') { $sql .= " AND cp.code = 'CB'"; } else { dol_print_error('Value for key = '.$key.' not supported'); exit; } if ($syear && !$smonth) { $sql .= " AND datef BETWEEN '".$db->idate(dol_get_first_day($syear, 1))."' AND '".$db->idate(dol_get_last_day($syear, 12))."'"; } elseif ($syear && $smonth && !$sday) { $sql .= " AND datef BETWEEN '".$db->idate(dol_get_first_day($syear, $smonth))."' AND '".$db->idate(dol_get_last_day($syear, $smonth))."'"; } elseif ($syear && $smonth && $sday) { $sql .= " AND datef BETWEEN '".$db->idate(dol_mktime(0, 0, 0, $smonth, $sday, $syear))."' AND '".$db->idate(dol_mktime(23, 59, 59, $smonth, $sday, $syear))."'"; } else { setEventMessages($langs->trans('YearNotDefined'), null, 'errors'); } $resql = $db->query($sql); if ($resql) { $theoricalamountforterminal[$terminalid][$key] = $initialbalanceforterminal[$terminalid][$key]; $obj = $db->fetch_object($resql); if ($obj) { $theoricalamountforterminal[$terminalid][$key] = price2num($theoricalamountforterminal[$terminalid][$key] + $obj->total); $theoricalnbofinvoiceforterminal[$terminalid][$key] = $obj->nb; } } else { dol_print_error($db); } } } //var_dump($theoricalamountforterminal); var_dump($theoricalnbofinvoiceforterminal); if ($action != 'close') { print load_fiche_titre($langs->trans("CashControl")." - ".$langs->trans("New"), '', 'cash-register'); print '
'; print ''; if ($contextpage == 'takepos') { print ''; } if ($action == 'start' && GETPOST('posnumber', 'int') != '' && GETPOST('posnumber', 'int') != '' && GETPOST('posnumber', 'int') != '-1') { print ''; } elseif ($action == 'close') { print ''; print ''; } else { print ''; } print '
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print "\n"; $disabled = 0; $prefix = 'close'; print ''; print ''; print ''; // Year print ''; // Month print ''; // Day print ''; // Button Start print ''; print '
'.$langs->trans("Module").''.$langs->trans("Terminal").''.$langs->trans("Year").''.$langs->trans("Month").''.$langs->trans("Day").'
'.$form->selectarray('posmodule', $arrayofposavailable, GETPOST('posmodule', 'alpha'), (count($arrayofposavailable) > 1 ? 1 : 0)).''; $array = array(); $numterminals = max(1, $conf->global->TAKEPOS_NUM_TERMINALS); for ($i = 1; $i <= $numterminals; $i++) { $array[$i] = $i; } $selectedposnumber = 0; $showempty = 1; if ($conf->global->TAKEPOS_NUM_TERMINALS == '1') { $selectedposnumber = 1; $showempty = 0; } print $form->selectarray('posnumber', $array, GETPOSTISSET('posnumber') ?GETPOST('posnumber', 'int') : $selectedposnumber, $showempty); //print ''; print ''; $retstring = ''; for ($year = $syear - 10; $year < $syear + 10; $year++) { $retstring .= ''; } $retstring .= "\n"; print $retstring; print ''; $retstring = ''; $retstring .= ''; for ($month = 1; $month <= 12; $month++) { $retstring .= '"; } $retstring .= ""; print $retstring; print ''; $retstring = ''; $retstring .= ''; for ($day = 1; $day <= 31; $day++) { $retstring .= ''; } $retstring .= ""; print $retstring; print ''; if ($action == 'start' && GETPOST('posnumber') != '' && GETPOST('posnumber') != '' && GETPOST('posnumber') != '-1') { print ''; } else { print ''; } print '
'; print '
'; // Table to see/enter balance if (($action == 'start' && GETPOST('posnumber') != '' && GETPOST('posnumber') != '' && GETPOST('posnumber') != '-1') || $action == 'close') { $posmodule = GETPOST('posmodule', 'alpha'); $terminalid = GETPOST('posnumber', 'alpha'); print '
'; print '
'; print ''; print ''; print ''; print ''; /* print ''; */ print ''; print ''; print ''; print ''; print ''; /* $i = 0; foreach ($arrayofpaymentmode as $key => $val) { print ''; $i++; }*/ print ''; print ''; /*print ''; // Initial amount print ''; print ''; // Amount per payment type $i = 0; foreach ($arrayofpaymentmode as $key => $val) { print ''; $i++; } // Save print ''; print ''; */ print ''; // Initial amount print ''; print ''; // Amount per payment type /*$i = 0; foreach ($arrayofpaymentmode as $key => $val) { print ''; $i++; }*/ // Save print ''; print ''; print ''; print ''; // Initial amount print ''; // Amount per payment type /*$i = 0; foreach ($arrayofpaymentmode as $key => $val) { print ''; $i++; }*/ // Save print ''; print ''; print '
'.$langs->trans("InitialBankBalance"); //print '
'.$langs->trans("TheoricalAmount").'
'.$langs->trans("RealAmount"); print '
'; print $langs->trans("AmountAtEndOfPeriod"); print '
'.$langs->trans("Cash"); //print '
'.$langs->trans("TheoricalAmount").'
'.$langs->trans("RealAmount"); print '
'.$langs->trans($val); //print '
'.$langs->trans("TheoricalAmount").'
'.$langs->trans("RealAmount"); print '
'.$langs->trans("NbOfInvoices").''; print ''; print $theoricalnbofinvoiceforterminal[$terminalid][$key]; print '
'.$langs->trans("TheoricalAmount").''; print price($initialbalanceforterminal[$terminalid]['cash']).'
'; print '
'; print price($theoricalamountforterminal[$terminalid][$key]).'
'; print '
'.$langs->trans("RealAmount").''; print ''; print ''; print ''; print ''; print ''; if ($action == 'start') { print ''; } elseif ($action == 'close') { print ''; } print '
'; print '
'; } print '
'; } } if (empty($action) || $action == "view" || $action == "close") { $result = $object->fetch($id); if ($result <= 0) { print $langs->trans("ErrorRecordNotFound"); } else { $head = array(); $head[0][0] = DOL_URL_ROOT.'/compta/cashcontrol/cashcontrol_card.php?id='.$object->id; $head[0][1] = $langs->trans("CashControl"); $head[0][2] = 'cashcontrol'; print dol_get_fiche_head($head, 'cashcontrol', $langs->trans("CashControl"), -1, 'account'); $linkback = ''.$langs->trans("BackToList").''; $morehtmlref = '
'; $morehtmlref .= '
'; dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'rowid', $morehtmlref); print '
'; print '
'; print '
'; print ''; print ''; print '"; print '"; print ''; print '
'; print $langs->trans("Ref"); print ''; print $id; print '
'.$langs->trans("Module").''; print $object->posmodule; print "
'.$langs->trans("Terminal").''; print $object->posnumber; print "
'; print $langs->trans("Period"); print ''; print $object->year_close; print ($object->month_close ? "-" : "").$object->month_close; print ($object->day_close ? "-" : "").$object->day_close; print '
'; print '
'; print '
'; print '
'; print ''; print ''; print '"; foreach ($arrayofpaymentmode as $key => $val) { print '"; } print "
'; print $langs->trans("DateCreationShort"); print ''; print dol_print_date($object->date_creation, 'dayhour'); print '
'.$langs->trans("InitialBankBalance").' - '.$langs->trans("Cash").''; print ''.price($object->opening, 0, $langs, 1, -1, -1, $conf->currency).''; print "
'.$langs->trans($val).''; print ''.price($object->$key, 0, $langs, 1, -1, -1, $conf->currency).''; print "
\n"; print '
'; print '
'; print dol_get_fiche_end(); if ($action != 'close') { print '
'; print ''; if ($object->status == CashControl::STATUS_DRAFT) { print ''; print ''; } else { print ''; } print '
'; if ($contextpage != 'takepos') { print '
'; } } else { print '
'; print ''; if ($contextpage == 'takepos') { print ''; } if ($action == 'start' && GETPOST('posnumber', 'int') != '' && GETPOST('posnumber', 'int') != '' && GETPOST('posnumber', 'int') != '-1') { print ''; } elseif ($action == 'close') { print ''; print ''; } else { print ''; } /* print '
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print "\n"; $disabled = 1; $prefix = 'close'; print ''; print ''; print ''; print ''; // Year print ''; // Month print ''; // Day print ''; print ''; print '
'.$langs->trans("Module").''.$langs->trans("Terminal").''.$langs->trans("Year").''.$langs->trans("Month").''.$langs->trans("Day").'
'.$form->selectarray('posmodulebis', $arrayofposavailable, $object->posmodule, (count($arrayofposavailable) > 1 ? 1 : 0), 0, 0, '', 0, 0, $disabled).''; $array = array(); $numterminals = max(1, $conf->global->TAKEPOS_NUM_TERMINALS); for($i = 1; $i <= $numterminals; $i++) { $array[$i] = $i; } $selectedposnumber = $object->posnumber; $showempty = 1; //print $form->selectarray('posnumber', $array, GETPOSTISSET('posnumber') ?GETPOST('posnumber', 'int') : $selectedposnumber, $showempty, 0, 0, '', 0, 0, $disabled); print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print '
'; print '
'; */ // Table to see/enter balance if (($action == 'start' && GETPOST('posnumber') != '' && GETPOST('posnumber') != '' && GETPOST('posnumber') != '-1') || $action == 'close') { $posmodule = $object->posmodule; $terminalid = $object->posnumber; print '
'; print '
'; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; $i = 0; foreach ($arrayofpaymentmode as $key => $val) { print ''; $i++; } print ''; print ''; print ''; // Initial amount print ''; print ''; // Amount per payment type $i = 0; foreach ($arrayofpaymentmode as $key => $val) { print ''; $i++; } // Save print ''; print ''; print ''; // Initial amount print ''; print ''; // Amount per payment type $i = 0; foreach ($arrayofpaymentmode as $key => $val) { print ''; $i++; } // Save print ''; print ''; print ''; print ''; // Initial amount print ''; // Amount per payment type $i = 0; foreach ($arrayofpaymentmode as $key => $val) { print ''; $i++; } // Save print ''; print ''; print '
'.$langs->trans("InitialBankBalance"); //print '
'.$langs->trans("TheoricalAmount").'
'.$langs->trans("RealAmount"); print '
'; print $langs->trans("AmountAtEndOfPeriod"); print '
'.$langs->trans("Cash"); //print '
'.$langs->trans("TheoricalAmount").'
'.$langs->trans("RealAmount"); print '
'.$langs->trans($val); //print '
'.$langs->trans("TheoricalAmount").'
'.$langs->trans("RealAmount"); print '
'.$langs->trans("NbOfInvoices").''; print ''; print $theoricalnbofinvoiceforterminal[$terminalid][$key]; print '
'.$langs->trans("TheoricalAmount").''; print price($initialbalanceforterminal[$terminalid]['cash']).'
'; print '
'; if ($key == 'cash') { $deltaforcash = ($object->opening - $initialbalanceforterminal[$terminalid]['cash']); print price($theoricalamountforterminal[$terminalid][$key] + $deltaforcash).'
'; } else { print price($theoricalamountforterminal[$terminalid][$key]).'
'; } print '
'.$langs->trans("RealAmount").''; print ''; print ''; print ''; print ''; print ''; if ($action == 'start') { print ''; } elseif ($action == 'close') { print ''; } print '
'; print '
'; } print '
'; } } } // End of page llxFooter(); $db->close();