30 require
'../../main.inc.php';
31 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
32 require_once DOL_DOCUMENT_ROOT.
'/compta/bank/class/account.class.php';
33 require_once DOL_DOCUMENT_ROOT.
'/compta/cashcontrol/class/cashcontrol.class.php';
35 $langs->loadLangs(array(
"install",
"cashdesk",
"admin",
"banks"));
39 $action =
GETPOST(
'action',
'aZ09');
48 $limit =
GETPOST(
'limit',
'int') ?
GETPOST(
'limit',
'int') : $conf->liste_limit;
49 $sortfield =
GETPOST(
'sortfield',
'aZ09comma');
50 $sortorder =
GETPOST(
'sortorder',
'aZ09comma');
52 if (empty($page) || $page == -1) {
55 $offset = $limit * $page;
56 $pageprev = $page - 1;
57 $pagenext = $page + 1;
64 $contextpage =
GETPOST(
'contextpage',
'aZ') ?
GETPOST(
'contextpage',
'aZ') :
'thirdpartylist';
66 if ($contextpage ==
'takepos') {
67 $_GET[
'optioncss'] =
'print';
70 $arrayofpaymentmode = array(
'cash'=>
'Cash',
'cheque'=>
'Cheque',
'card'=>
'CreditCard');
72 $arrayofposavailable = array();
74 $arrayofposavailable[
'cashdesk'] = $langs->trans(
'CashDesk').
' (cashdesk)';
77 $arrayofposavailable[
'takepos'] = $langs->trans(
'TakePOS').
' (takepos)';
85 $extrafields->fetch_name_optionals_label($object->table_element);
88 $hookmanager->initHooks(array(
'cashcontrolcard',
'globalcard'));
91 include DOL_DOCUMENT_ROOT.
'/core/actions_fetchobject.inc.php';
94 if ($user->socid > 0) {
98 if (!$user->rights->cashdesk->run && !$user->rights->takepos->run) {
107 $permissiontoadd = ($user->rights->cashdesk->run || $user->rights->takepos->run);
108 $permissiontodelete = ($user->rights->cashdesk->run || $user->rights->takepos->run) || ($permissiontoadd && $object->status == 0);
109 if (empty($backtopage)) {
110 $backtopage = DOL_URL_ROOT.
'/compta/cashcontrol/cashcontrol_card.php?id='.($id > 0 ? $id :
'__ID__');
112 $backurlforlist = DOL_URL_ROOT.
'/compta/cashcontrol/cashcontrol_list.php';
113 $triggermodname =
'CACHCONTROL_MODIFY';
115 if (empty($conf->global->CASHDESK_ID_BANKACCOUNT_CASH) && empty($conf->global->CASHDESK_ID_BANKACCOUNT_CASH1)) {
116 setEventMessages($langs->trans(
"CashDesk").
" - ".$langs->trans(
"NotConfigured"),
null,
'errors');
120 if (
GETPOST(
'cancel',
'alpha')) {
121 if ($action ==
'valid') {
128 if ($action ==
"reopen") {
129 $result = $object->setStatut($object::STATUS_DRAFT,
null,
'',
'CASHFENCE_REOPEN');
137 if ($action ==
"start") {
139 if (!
GETPOST(
'posmodule',
'alpha') ||
GETPOST(
'posmodule',
'alpha') ==
'-1') {
140 setEventMessages($langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Module")),
null,
'errors');
144 if (
GETPOST(
'posnumber',
'alpha') ==
'') {
145 setEventMessages($langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"CashDesk")),
null,
'errors');
149 if (!
GETPOST(
'closeyear',
'alpha') ||
GETPOST(
'closeyear',
'alpha') ==
'-1') {
150 setEventMessages($langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Year")),
null,
'errors');
154 } elseif ($action ==
"add") {
155 if (
GETPOST(
'opening',
'alpha') ==
'') {
156 setEventMessages($langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"InitialBankBalance")),
null,
'errors');
161 foreach ($arrayofpaymentmode as $key => $val) {
166 $object->day_close =
GETPOST(
'closeday',
'int');
167 $object->month_close =
GETPOST(
'closemonth',
'int');
168 $object->year_close =
GETPOST(
'closeyear',
'int');
171 $object->posmodule =
GETPOST(
'posmodule',
'alpha');
172 $object->posnumber =
GETPOST(
'posnumber',
'alpha');
176 $id = $object->create($user);
186 if ($contextpage ==
'takepos') {
189 parent.location.href='../../takepos/index.php?place='+parent.place;
195 if ($action ==
"valid") {
210 $result = $object->update($user);
212 $result = $object->valid($user);
222 if ($contextpage ==
'takepos') {
225 parent.location.href='../../takepos/index.php?place='+parent.place;
233 if ($action ==
'confirm_delete' && !empty($permissiontodelete)) {
236 if (!($object->id > 0)) {
237 dol_print_error(
'',
'Error, object must be fetched before being deleted');
241 $result = $object->delete($user);
246 header(
"Location: ".$backurlforlist);
249 if (!empty($object->errors)) {
264 $initialbalanceforterminal = array();
265 $theoricalamountforterminal = array();
266 $theoricalnbofinvoiceforterminal = array();
268 if ($action ==
"create" || $action ==
"start" || $action ==
'close') {
269 if ($action ==
'close') {
270 $posmodule = $object->posmodule;
271 $terminalid = $object->posnumber;
272 $terminaltouse = $terminalid;
274 $syear = $object->year_close;
275 $smonth = $object->month_close;
276 $sday = $object->day_close;
277 } elseif (
GETPOST(
'posnumber',
'alpha') !=
'' &&
GETPOST(
'posnumber',
'alpha') !=
'' &&
GETPOST(
'posnumber',
'alpha') !=
'-1') {
278 $posmodule =
GETPOST(
'posmodule',
'alpha');
279 $terminalid =
GETPOST(
'posnumber',
'alpha');
280 $terminaltouse = $terminalid;
282 if ($terminaltouse ==
'1' && $posmodule ==
'cashdesk') {
286 if ($posmodule ==
'cashdesk' && $terminaltouse !=
'' && $terminaltouse !=
'1') {
288 setEventMessages($langs->trans(
"OnlyTerminal1IsAvailableForCashDeskModule"),
null,
'errors');
293 if ($terminalid !=
'') {
295 foreach ($arrayofpaymentmode as $key => $val) {
296 if ($key !=
'cash') {
297 $initialbalanceforterminal[$terminalid][$key] = 0;
302 $vartouse =
'CASHDESK_ID_BANKACCOUNT_CASH'.$terminaltouse;
303 $bankid = $conf->global->$vartouse;
306 $sql =
"SELECT SUM(amount) as total FROM ".MAIN_DB_PREFIX.
"bank";
307 $sql .=
" WHERE fk_account = ".((int) $bankid);
308 if ($syear && !$smonth) {
310 } elseif ($syear && $smonth && !$sday) {
312 } elseif ($syear && $smonth && $sday) {
313 $sql .=
" AND dateo < '".$db->idate(
dol_mktime(0, 0, 0, $smonth, $sday, $syear)).
"'";
318 $resql = $db->query($sql);
320 $obj = $db->fetch_object(
$resql);
322 $initialbalanceforterminal[$terminalid][$key] = $obj->total;
328 setEventMessages($langs->trans(
"SetupOfTerminalNotComplete", $terminaltouse),
null,
'errors');
334 foreach ($arrayofpaymentmode as $key => $val) {
335 $sql =
"SELECT SUM(pf.amount) as total, COUNT(*) as nb";
336 $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";
337 $sql .=
" WHERE pf.fk_facture = f.rowid AND p.rowid = pf.fk_paiement AND cp.id = p.fk_paiement";
338 $sql .=
" AND f.module_source = '".$db->escape($posmodule).
"'";
339 $sql .=
" AND f.pos_source = '".$db->escape($terminalid).
"'";
340 $sql .=
" AND f.paye = 1";
341 $sql .=
" AND p.entity IN (".getEntity(
'facture').
")";
342 if ($key ==
'cash') {
343 $sql .=
" AND cp.code = 'LIQ'";
344 } elseif ($key ==
'cheque') {
345 $sql .=
" AND cp.code = 'CHQ'";
346 } elseif ($key ==
'card') {
347 $sql .=
" AND cp.code = 'CB'";
352 if ($syear && !$smonth) {
354 } elseif ($syear && $smonth && !$sday) {
356 } elseif ($syear && $smonth && $sday) {
357 $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)).
"'";
362 $resql = $db->query($sql);
364 $theoricalamountforterminal[$terminalid][$key] = $initialbalanceforterminal[$terminalid][$key];
366 $obj = $db->fetch_object(
$resql);
368 $theoricalamountforterminal[$terminalid][$key] =
price2num($theoricalamountforterminal[$terminalid][$key] + $obj->total);
369 $theoricalnbofinvoiceforterminal[$terminalid][$key] = $obj->nb;
378 if ($action !=
'close') {
379 llxHeader(
'', $langs->trans(
"NewCashFence"));
381 print
load_fiche_titre($langs->trans(
"CashControl").
" - ".$langs->trans(
"New"),
'',
'cash-register');
383 print
'<form method="POST" action="'.$_SERVER[
"PHP_SELF"].
'">';
384 print
'<input type="hidden" name="token" value="'.newToken().
'">';
385 if ($contextpage ==
'takepos') {
386 print
'<input type="hidden" name="contextpage" value="takepos">';
388 if ($action ==
'start' &&
GETPOST(
'posnumber',
'int') !=
'' &&
GETPOST(
'posnumber',
'int') !=
'' &&
GETPOST(
'posnumber',
'int') !=
'-1') {
389 print
'<input type="hidden" name="action" value="add">';
390 } elseif ($action ==
'close') {
391 print
'<input type="hidden" name="action" value="valid">';
392 print
'<input type="hidden" name="id" value="'.$id.
'">';
394 print
'<input type="hidden" name="action" value="start">';
397 print
'<div class="div-table-responsive-no-min">';
398 print
'<table class="noborder centpercent">';
399 print
'<tr class="liste_titre">';
400 print
'<td>'.$langs->trans(
"Module").
'</td>';
401 print
'<td>'.$langs->trans(
"Terminal").
'</td>';
402 print
'<td>'.$langs->trans(
"Year").
'</td>';
403 print
'<td>'.$langs->trans(
"Month").
'</td>';
404 print
'<td>'.$langs->trans(
"Day").
'</td>';
411 print
'<tr class="oddeven">';
412 print
'<td>'.$form->selectarray(
'posmodule', $arrayofposavailable,
GETPOST(
'posmodule',
'alpha'), (count($arrayofposavailable) > 1 ? 1 : 0)).
'</td>';
416 $numterminals = max(1, $conf->global->TAKEPOS_NUM_TERMINALS);
417 for ($i = 1; $i <= $numterminals; $i++) {
420 $selectedposnumber = 0;
422 if ($conf->global->TAKEPOS_NUM_TERMINALS ==
'1') {
423 $selectedposnumber = 1;
426 print
$form->selectarray(
'posnumber', $array,
GETPOSTISSET(
'posnumber') ?
GETPOST(
'posnumber',
'int') : $selectedposnumber, $showempty);
431 $retstring =
'<select'.($disabled ?
' disabled' :
'').
' class="flat valignmiddle maxwidth75imp" id="'.$prefix.
'year" name="'.$prefix.
'year">';
432 for ($year = $syear - 10; $year < $syear + 10; $year++) {
433 $retstring .=
'<option value="'.$year.
'"'.($year == $syear ?
' selected' :
'').
'>'.$year.
'</option>';
435 $retstring .=
"</select>\n";
440 $retstring =
'<select'.($disabled ?
' disabled' :
'').
' class="flat valignmiddle maxwidth75imp" id="'.$prefix.
'month" name="'.$prefix.
'month">';
441 $retstring .=
'<option value="0"></option>';
442 for ($month = 1; $month <= 12; $month++) {
443 $retstring .=
'<option value="'.$month.
'"'.($month == $smonth ?
' selected' :
'').
'>';
444 $retstring .=
dol_print_date(mktime(12, 0, 0, $month, 1, 2000),
"%b");
445 $retstring .=
"</option>";
447 $retstring .=
"</select>";
452 $retstring =
'<select'.($disabled ?
' disabled' :
'').
' class="flat valignmiddle maxwidth50imp" id="'.$prefix.
'day" name="'.$prefix.
'day">';
453 $retstring .=
'<option value="0" selected> </option>';
454 for ($day = 1; $day <= 31; $day++) {
455 $retstring .=
'<option value="'.$day.
'"'.($day == $sday ?
' selected' :
'').
'>'.$day.
'</option>';
457 $retstring .=
"</select>";
462 if ($action ==
'start' &&
GETPOST(
'posnumber') !=
'' &&
GETPOST(
'posnumber') !=
'' &&
GETPOST(
'posnumber') !=
'-1') {
465 print
'<input type="submit" name="add" class="button" value="'.$langs->trans(
"Start").
'">';
472 if (($action ==
'start' &&
GETPOST(
'posnumber') !=
'' &&
GETPOST(
'posnumber') !=
'' &&
GETPOST(
'posnumber') !=
'-1') || $action ==
'close') {
473 $posmodule =
GETPOST(
'posmodule',
'alpha');
474 $terminalid =
GETPOST(
'posnumber',
'alpha');
478 print
'<div class="div-table-responsive-no-min">';
479 print
'<table class="noborder centpercent">';
481 print
'<tr class="liste_titre">';
483 print
'<td class="center">'.$langs->trans(
"InitialBankBalance");
495 print
'<tr class="liste_titre">';
497 print
'<td class="center">'.$langs->trans(
"Cash");
533 print
'<td>'.$langs->trans(
"TheoricalAmount").
'</td>';
534 print
'<td class="center">';
535 print
price($initialbalanceforterminal[$terminalid][
'cash']).
'<br>';
551 print
'<td>'.$langs->trans(
"RealAmount").
'</td>';
553 print
'<td class="center">';
555 if ($action ==
'close') {
558 print
'name="opening" type="text" class="maxwidth100 center" value="';
559 if ($action ==
'close') {
561 print $object->opening;
579 print
'<td class="center">';
580 print
'<input type="submit" name="cancel" class="button button-cancel" value="'.$langs->trans(
"Cancel").
'">';
581 if ($action ==
'start') {
582 print
'<input type="submit" name="add" class="button button-save" value="'.$langs->trans(
"Save").
'">';
583 } elseif ($action ==
'close') {
584 print
'<input type="submit" name="valid" class="button" value="'.$langs->trans(
"Validate").
'">';
597 if (empty($action) || $action ==
"view" || $action ==
"close") {
598 $result = $object->fetch($id);
600 llxHeader(
'', $langs->trans(
"CashControl"));
603 print $langs->trans(
"ErrorRecordNotFound");
606 $head[0][0] = DOL_URL_ROOT.
'/compta/cashcontrol/cashcontrol_card.php?id='.$object->id;
607 $head[0][1] = $langs->trans(
"CashControl");
608 $head[0][2] =
'cashcontrol';
610 print
dol_get_fiche_head($head,
'cashcontrol', $langs->trans(
"CashControl"), -1,
'account');
612 $linkback =
'<a href="'.DOL_URL_ROOT.
'/compta/cashcontrol/cashcontrol_list.php?restore_lastsearch_values=1">'.$langs->trans(
"BackToList").
'</a>';
614 $morehtmlref =
'<div class="refidno">';
615 $morehtmlref .=
'</div>';
618 dol_banner_tab($object,
'id', $linkback, 1,
'rowid',
'rowid', $morehtmlref);
620 print
'<div class="fichecenter">';
621 print
'<div class="fichehalfleft">';
622 print
'<div class="underbanner clearboth"></div>';
623 print
'<table class="border tableforfield" width="100%">';
625 print
'<tr><td class="titlefield nowrap">';
626 print $langs->trans(
"Ref");
631 print
'<tr><td valign="middle">'.$langs->trans(
"Module").
'</td><td>';
632 print $object->posmodule;
635 print
'<tr><td valign="middle">'.$langs->trans(
"Terminal").
'</td><td>';
636 print $object->posnumber;
639 print
'<tr><td class="nowrap">';
640 print $langs->trans(
"Period");
642 print $object->year_close;
643 print ($object->month_close ?
"-" :
"").$object->month_close;
644 print ($object->day_close ?
"-" :
"").$object->day_close;
650 print
'<div class="fichehalfright">';
651 print
'<div class="underbanner clearboth"></div>';
653 print
'<table class="border tableforfield centpercent">';
655 print
'<tr><td class="titlefield nowrap">';
656 print $langs->trans(
"DateCreationShort");
661 print
'<tr><td valign="middle">'.$langs->trans(
"InitialBankBalance").
' - '.$langs->trans(
"Cash").
'</td><td>';
662 print
'<span class="amount">'.price($object->opening, 0, $langs, 1, -1, -1, $conf->currency).
'</span>';
664 foreach ($arrayofpaymentmode as $key => $val) {
665 print
'<tr><td valign="middle">'.$langs->trans($val).
'</td><td>';
666 print
'<span class="amount">'.price($object->$key, 0, $langs, 1, -1, -1, $conf->currency).
'</span>';
672 print
'</div></div>';
673 print
'<div style="clear:both"></div>';
677 if ($action !=
'close') {
678 print
'<div class="tabsAction">';
680 print
'<div class="inline-block divButAction"><a target="_blank" rel="noopener noreferrer" class="butAction" href="report.php?id='.((int) $id).
'">'.$langs->trans(
'PrintTicket').
'</a></div>';
682 if ($object->status == CashControl::STATUS_DRAFT) {
683 print
'<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER[
"PHP_SELF"].
'?id='.((int) $id).
'&action=close&token='.
newToken().
'&contextpage='.$contextpage.
'">'.$langs->trans(
'Close').
'</a></div>';
685 print
'<div class="inline-block divButAction"><a class="butActionDelete" href="'.$_SERVER[
"PHP_SELF"].
'?id='.((int) $id).
'&action=confirm_delete&token='.
newToken().
'">'.$langs->trans(
'Delete').
'</a></div>';
687 print
'<div class="inline-block divButAction"><a class="butAction" href="'.$_SERVER[
"PHP_SELF"].
'?id='.((int) $id).
'&action=reopen&token='.
newToken().
'">'.$langs->trans(
'ReOpen').
'</a></div>';
692 if ($contextpage !=
'takepos') {
693 print
'<center><iframe src="report.php?id='.$id.
'" width="60%" height="800"></iframe></center>';
696 print
'<form method="POST" action="'.$_SERVER[
"PHP_SELF"].
'" name="formclose">';
697 print
'<input type="hidden" name="token" value="'.newToken().
'">';
698 if ($contextpage ==
'takepos') {
699 print
'<input type="hidden" name="contextpage" value="takepos">';
701 if ($action ==
'start' &&
GETPOST(
'posnumber',
'int') !=
'' &&
GETPOST(
'posnumber',
'int') !=
'' &&
GETPOST(
'posnumber',
'int') !=
'-1') {
702 print
'<input type="hidden" name="action" value="add">';
703 } elseif ($action ==
'close') {
704 print
'<input type="hidden" name="action" value="valid">';
705 print
'<input type="hidden" name="id" value="'.$id.
'">';
707 print
'<input type="hidden" name="action" value="start">';
762 if (($action ==
'start' &&
GETPOST(
'posnumber') !=
'' &&
GETPOST(
'posnumber') !=
'' &&
GETPOST(
'posnumber') !=
'-1') || $action ==
'close') {
763 $posmodule = $object->posmodule;
764 $terminalid = $object->posnumber;
768 print
'<div class="div-table-responsive-no-min">';
769 print
'<table class="noborder centpercent">';
771 print
'<tr class="liste_titre">';
773 print
'<td class="center">'.$langs->trans(
"InitialBankBalance");
777 print
'<td align="center" class="hide0" colspan="'.count($arrayofpaymentmode).
'">';
778 print $langs->trans(
"AmountAtEndOfPeriod");
783 print
'<tr class="liste_titre">';
785 print
'<td class="center">'.$langs->trans(
"Cash");
789 foreach ($arrayofpaymentmode as $key => $val) {
790 print
'<td align="center"'.($i == 0 ?
' class="hide0"' :
'').
'>'.$langs->trans($val);
800 print
'<td>'.$langs->trans(
"NbOfInvoices").
'</td>';
801 print
'<td class="center">';
805 foreach ($arrayofpaymentmode as $key => $val) {
806 print
'<td align="center"'.($i == 0 ?
' class="hide0"' :
'').
'>';
807 print $theoricalnbofinvoiceforterminal[$terminalid][$key];
812 print
'<td align="center"></td>';
817 print
'<td>'.$langs->trans(
"TheoricalAmount").
'</td>';
818 print
'<td class="center">';
819 print
price($initialbalanceforterminal[$terminalid][
'cash']).
'<br>';
823 foreach ($arrayofpaymentmode as $key => $val) {
824 print
'<td align="center"'.($i == 0 ?
' class="hide0"' :
'').
'>';
825 if ($key ==
'cash') {
826 $deltaforcash = ($object->opening - $initialbalanceforterminal[$terminalid][
'cash']);
827 print
price($theoricalamountforterminal[$terminalid][$key] + $deltaforcash).
'<br>';
829 print
price($theoricalamountforterminal[$terminalid][$key]).
'<br>';
835 print
'<td align="center"></td>';
839 print
'<td>'.$langs->trans(
"RealAmount").
'</td>';
841 print
'<td class="center">';
843 if ($action ==
'close') {
846 print
'name="opening" type="text" class="maxwidth100 center" value="';
847 if ($action ==
'close') {
849 print $object->opening;
857 foreach ($arrayofpaymentmode as $key => $val) {
858 print
'<td align="center"'.($i == 0 ?
' class="hide0"' :
'').
'>';
860 if ($action ==
'start') {
863 print
'name="'.$key.
'_amount" type="text"'.($key ==
'cash' ?
' autofocus' :
'').
' class="maxwidth100 center" value="'.
GETPOST($key.
'_amount',
'alpha').
'">';
868 print
'<td class="center">';
869 print
'<input type="submit" name="cancel" class="button button-cancel" value="'.$langs->trans(
"Cancel").
'">';
870 if ($action ==
'start') {
871 print
'<input type="submit" name="add" class="button button-save" value="'.$langs->trans(
"Save").
'">';
872 } elseif ($action ==
'close') {
873 print
'<input type="submit" name="valid" class="button" value="'.$langs->trans(
"Close").
'">';