* Copyright (C) 2018-2024 Frédéric France * Copyright (C) 2023 Laurent Destailleur * Copyright (C) 2023 Joachim Kueter * Copyright (C) 2024 MDW * * 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/bank/various_payment/card.php * \ingroup bank * \brief Page of various expenses */ // Load Dolibarr environment require '../../../main.inc.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/bank.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php'; require_once DOL_DOCUMENT_ROOT.'/core/lib/accounting.lib.php'; require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/paymentvarious.class.php'; require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/account.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formaccounting.class.php'; require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingaccount.class.php'; require_once DOL_DOCUMENT_ROOT.'/accountancy/class/accountingjournal.class.php'; if (isModEnabled('project')) { require_once DOL_DOCUMENT_ROOT.'/projet/class/project.class.php'; require_once DOL_DOCUMENT_ROOT.'/core/class/html.formprojet.class.php'; } /** * @var Conf $conf * @var DoliDB $db * @var HookManager $hookmanager * @var Translate $langs * @var User $user */ // Load translation files required by the page $langs->loadLangs(array("compta", "banks", "bills", "users", "accountancy", "categories")); // Get parameters $id = GETPOSTINT('id'); $action = GETPOST('action', 'alpha'); $confirm = GETPOST('confirm'); $cancel = GETPOST('cancel', 'aZ09'); $backtopage = GETPOST('backtopage', 'alpha'); $accountid = GETPOSTINT("accountid") > 0 ? GETPOSTINT("accountid") : 0; $label = GETPOST("label", "alpha"); $sens = GETPOSTINT("sens"); $amount = GETPOST("amount"); $paymenttype = GETPOST("paymenttype", "aZ09"); $accountancy_code = GETPOST("accountancy_code", "alpha"); $projectid = GETPOSTINT('projectid') ? GETPOSTINT('projectid') : GETPOSTINT('fk_project'); if (isModEnabled('accounting') && getDolGlobalString('ACCOUNTANCY_COMBO_FOR_AUX')) { $subledger_account = GETPOST("subledger_account", "alpha") > 0 ? GETPOST("subledger_account", "alpha") : ''; } else { $subledger_account = GETPOST("subledger_account", "alpha"); } // Security check $socid = GETPOSTINT("socid"); if ($user->socid) { $socid = $user->socid; } // Initialize a technical object to manage hooks of page. Note that conf->hooks_modules contains an array of hook context $hookmanager->initHooks(array('variouscard', 'globalcard')); $result = restrictedArea($user, 'banque', '', '', ''); $object = new PaymentVarious($db); $permissiontoadd = $user->hasRight('banque', 'modifier'); $permissiontodelete = $user->hasRight('banque', 'modifier'); /** * Actions */ $parameters = array(); $reshook = $hookmanager->executeHooks('doActions', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks if ($reshook < 0) { setEventMessages($hookmanager->error, $hookmanager->errors, 'errors'); } if (empty($reshook)) { if ($cancel) { if ($action != 'addlink' && $action != 'setaccountancy_code' && $action != 'setsubledger_account') { $urltogo = $backtopage ? $backtopage : dol_buildpath('/compta/bank/various_payment/list.php', 1); header("Location: ".$urltogo); exit; } if ($id > 0 || !empty($ref)) { $ret = $object->fetch($id, $ref); } $action = ''; } // Link to a project if ($action == 'classin' && $permissiontoadd) { $object->fetch($id); $object->setProject(GETPOSTINT('projectid')); } if ($action == 'add' && $permissiontoadd) { $error = 0; $datep = dol_mktime(12, 0, 0, GETPOSTINT("datepmonth"), GETPOSTINT("datepday"), GETPOSTINT("datepyear")); $datev = dol_mktime(12, 0, 0, GETPOSTINT("datevmonth"), GETPOSTINT("datevday"), GETPOSTINT("datevyear")); if (empty($datev)) { $datev = $datep; } $object->ref = ''; // TODO $object->fk_account = GETPOSTINT("accountid") > 0 ? GETPOSTINT("accountid") : 0; $object->accountid = $object->fk_account; $object->datev = $datev; $object->datep = $datep; $object->amount = GETPOSTFLOAT("amount"); $object->label = GETPOST("label", 'restricthtml'); $object->note_private = GETPOST("note", 'restricthtml'); $object->note = $object->note_private; $object->type_payment = dol_getIdFromCode($db, GETPOST('paymenttype'), 'c_paiement', 'code', 'id', 1); $object->num_payment = GETPOST("num_payment", 'alpha'); $object->chqemetteur = GETPOST("chqemetteur", 'alpha'); $object->chqbank = GETPOST("chqbank", 'alpha'); $object->fk_user_author = $user->id; $object->category_transaction = GETPOSTINT("category_transaction"); $object->accountancy_code = (GETPOST("accountancy_code") != '-1' ? GETPOST("accountancy_code", "alpha") : ""); $object->subledger_account = $subledger_account; $object->sens = GETPOSTINT('sens'); $object->fk_project = GETPOSTINT('fk_project'); if (empty($datep) || empty($datev)) { $langs->load('errors'); setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Date")), null, 'errors'); $error++; } if (empty($object->amount)) { $langs->load('errors'); setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Amount")), null, 'errors'); $error++; } if (isModEnabled("bank") && !$object->accountid > 0) { $langs->load('errors'); setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("BankAccount")), null, 'errors'); $error++; } if (empty($object->type_payment) || $object->type_payment < 0) { $langs->load('errors'); setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("PaymentMode")), null, 'errors'); $error++; } if (isModEnabled('accounting') && !$object->accountancy_code) { $langs->load('errors'); setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("AccountAccounting")), null, 'errors'); $error++; } if ($object->sens < 0) { $langs->load('errors'); setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("Sens")), null, 'errors'); $error++; } $bankaccount = new Account($db); $bankaccount->fetch($object->fk_account); // Check currency $currencyofpayment = $conf->currency; // The currency of various payment is not yet asked, so we suppose it is the main company currency //var_dump($currencyofpayment); var_dump($bankaccount->currency_code); if (isModEnabled('multicurrency') && $currencyofpayment != $bankaccount->currency_code) { // TODO Support this feature the same way we do it for invoice payment // using the $value_converted = MultiCurrency::getAmountConversionFromInvoiceRate($key, $value, $way); setEventMessages($langs->trans("ErrorVariousPaymentOnBankAccountWithADifferentCurrencyNotYetSupported"), null, 'errors'); $error++; } if (!$error) { $db->begin(); $ret = $object->create($user); if ($ret > 0) { $db->commit(); $urltogo = ($backtopage ? $backtopage : DOL_URL_ROOT.'/compta/bank/various_payment/list.php'); header("Location: ".$urltogo); exit; } else { $db->rollback(); setEventMessages($object->error, $object->errors, 'errors'); $action = "create"; } } $action = 'create'; } if ($action == 'confirm_delete' && $confirm == 'yes' && $permissiontodelete) { $result = $object->fetch($id); if ($object->rappro == 0) { $db->begin(); $ret = $object->delete($user); if ($ret > 0) { if ($object->fk_bank) { $accountline = new AccountLine($db); $result = $accountline->fetch($object->fk_bank); if ($result > 0) { $result = $accountline->delete($user); // $result may be 0 if not found (when bank entry was deleted manually and fk_bank point to nothing) } } else { $account_line = null; } if ($result >= 0) { $db->commit(); header("Location: ".DOL_URL_ROOT.'/compta/bank/various_payment/list.php'); exit; } else { $object->error = $accountline ? $accountline->error : 'No AccountLine'; $db->rollback(); setEventMessages($object->error, $object->errors, 'errors'); } } else { $db->rollback(); setEventMessages($object->error, $object->errors, 'errors'); } } else { setEventMessages('Error try do delete a line linked to a conciliated bank transaction', null, 'errors'); } } if ($action == 'setaccountancy_code' && $permissiontodelete) { $db->begin(); $result = $object->fetch($id); $object->accountancy_code = GETPOST('accountancy_code', 'alphanohtml'); $res = $object->update($user); if ($res > 0) { $db->commit(); } else { $db->rollback(); setEventMessages($object->error, $object->errors, 'errors'); } } if ($action == 'setsubledger_account' && $permissiontodelete) { $db->begin(); $result = $object->fetch($id); $object->subledger_account = $subledger_account; $res = $object->update($user); if ($res > 0) { $db->commit(); } else { $db->rollback(); setEventMessages($object->error, $object->errors, 'errors'); } } } // Action clone object if ($action == 'confirm_clone' && $confirm != 'yes') { // Test on permission not required $action = ''; } if ($action == 'confirm_clone' && $confirm == 'yes' && $permissiontoadd) { $db->begin(); $originalId = $id; $object->fetch($id); if ($object->id > 0) { unset($object->id); unset($object->ref); if (GETPOST('clone_label', 'alphanohtml')) { $object->label = GETPOST('clone_label', 'alphanohtml'); } else { $object->label = $langs->trans("CopyOf").' '.$object->label; } $newdatepayment = dol_mktime(0, 0, 0, GETPOSTINT('clone_date_paymentmonth'), GETPOSTINT('clone_date_paymentday'), GETPOSTINT('clone_date_paymentyear')); $newdatevalue = dol_mktime(0, 0, 0, GETPOSTINT('clone_date_valuemonth'), GETPOSTINT('clone_date_valueday'), GETPOSTINT('clone_date_valueyear')); if ($newdatepayment) { $object->datep = $newdatepayment; } if (!empty($newdatevalue)) { $object->datev = $newdatevalue; } else { $object->datev = $newdatepayment; } if (GETPOSTISSET("clone_sens")) { $object->sens = GETPOSTINT("clone_sens"); } // else { $object->sens = $object->sens; } if (GETPOSTISSET("clone_amount")) { $object->amount = GETPOSTFLOAT("clone_amount"); } else { $object->amount = (float) price2num($object->amount); } if ($object->check()) { $id = $object->create($user); if ($id > 0) { $db->commit(); $db->close(); header("Location: ".$_SERVER["PHP_SELF"]."?id=".$id); exit; } else { $id = $originalId; $db->rollback(); setEventMessages($object->error, $object->errors, 'errors'); } } else { $id = $originalId; $db->rollback(); setEventMessages($object->error, $object->errors, 'errors'); } } else { $db->rollback(); dol_print_error($db, $object->error); } } /* * View */ $form = new Form($db); if (isModEnabled('accounting')) { $formaccounting = new FormAccounting($db); } else { $formaccounting = null; } if (isModEnabled('project')) { $formproject = new FormProjets($db); } else { $formproject = null; } if ($id) { $object = new PaymentVarious($db); $result = $object->fetch($id); if ($result <= 0) { dol_print_error($db); exit; } } $title = $object->ref." - ".$langs->trans('Card'); if ($action == 'create') { $title = $langs->trans("NewVariousPayment"); } $help_url = 'EN:Module_Suppliers_Invoices|FR:Module_Fournisseurs_Factures|ES:Módulo_Facturas_de_proveedores|DE:Modul_Lieferantenrechnungen'; llxHeader('', $title, $help_url); $options = array(); // Load bank groups require_once DOL_DOCUMENT_ROOT.'/compta/bank/class/bankcateg.class.php'; $bankcateg = new BankCateg($db); $arrayofbankcategs = $bankcateg->fetchAll(); foreach ($arrayofbankcategs as $bankcategory) { $options[$bankcategory->id] = $bankcategory->label; } // Create mode if ($action == 'create') { // Update fields properties in realtime if (!empty($conf->use_javascript_ajax)) { print "\n".''."\n"; } print '
'; print ''; print ''; print ''; print load_fiche_titre($langs->trans("NewVariousPayment"), '', 'object_payment'); print dol_get_fiche_head([], ''); print ''; // Date payment print ''; // Date value for bank print ''; // Label print ''; // Amount print ''; // Bank if (isModEnabled("bank")) { print ''; } // Type payment print '\n"; print ''; // Number if (isModEnabled("bank")) { print ''; print ''."\n"; // Check transmitter print ''; print ''; // Bank name print ''; print ''; } // Project if (isModEnabled('project')) { $formproject = new FormProjets($db); // Associated project $langs->load("projects"); print ''; } // Other attributes $parameters = array(); $reshook = $hookmanager->executeHooks('formObjectOptions', $parameters, $object, $action); // Note that $action and $object may have been modified by hook print $hookmanager->resPrint; // Category if (is_array($options) && count($options) && $conf->categorie->enabled) { print ''; } print ''; // Accountancy account if (isModEnabled('accounting')) { /** @var FormAccounting $formaccounting */ // TODO Remove the fieldrequired and allow instead to edit a various payment to enter accounting code print ''; print ''; } else { // For external software print ''; print ''; } // Subledger account if (isModEnabled('accounting')) { /** @var FormAccounting $formaccounting */ print ''; print ''; } else { // For external software print ''; print ''; } // Sens print ''; print '
'; print $form->editfieldkey('DatePayment', 'datep', '', $object, 0, 'string', '', 1).''; print $form->selectDate((empty($datep) ? -1 : $datep), "datep", 0, 0, 0, 'add', 1, 1); print '
'; print $form->editfieldkey('DateValue', 'datev', '', $object, 0).''; print $form->selectDate((empty($datev) ? -1 : $datev), "datev", 0, 0, 0, 'add', 1, 1); print '
'; print $form->editfieldkey('Label', 'label', '', $object, 0, 'string', '', 1).''; print ''; print '
'; print $form->editfieldkey('Amount', 'amount', '', $object, 0, 'string', '', 1).''; print ''; print '
'; print $form->editfieldkey('BankAccount', 'selectaccountid', '', $object, 0, 'string', '', 1).''; print img_picto('', 'bank_account', 'class="pictofixedwidth"'); print $form->select_comptes($accountid, "accountid", 0, '', 2, '', (isModEnabled('multicurrency') ? 1 : 0), '', 1); // Show list of main accounts (comptes courants) print '
'.$langs->trans('PaymentMode').''; $form->select_types_paiements($paymenttype, 'paymenttype', '', 2); print "
'.$langs->trans("Project").''; print img_picto('', 'project', 'class="pictofixedwidth"'); print $formproject->select_projects(-1, $projectid, 'fk_project', 0, 0, 1, 1, 0, 0, 0, '', 1); print '
'.$langs->trans("RubriquesTransactions").''; print img_picto('', 'category').Form::selectarray('category_transaction', $options, GETPOST('category_transaction'), 1, 0, 0, '', 0, 0, 0, '', 'minwidth300', 1); print '

'.$langs->trans("AccountAccounting").''; print $formaccounting->select_account($accountancy_code, 'accountancy_code', 1, array(), 1, 1); print '
'.$langs->trans("AccountAccounting").''; print '
'.$langs->trans("SubledgerAccount").''; if (getDolGlobalString('ACCOUNTANCY_COMBO_FOR_AUX')) { print $formaccounting->select_auxaccount($subledger_account, 'subledger_account', 1, ''); } else { print ''; } print '
'.$langs->trans("SubledgerAccount").''; print '
'; $labelsens = $form->textwithpicto($langs->trans('Sens'), $langs->trans("AccountingDirectionHelp")); print $form->editfieldkey($labelsens, 'sens', '', $object, 0, 'string', '', 1).''; $sensarray = array('0' => $langs->trans("Debit"), '1' => $langs->trans("Credit")); print $form->selectarray('sens', $sensarray, $sens, 1, 0, 0, '', 0, 0, 0, '', 'minwidth100', 1); print '
'; print dol_get_fiche_end(); print $form->buttonsSaveCancel(); print '
'; } // View in read or edit mode if ($id) { $alreadyaccounted = $object->getVentilExportCompta(); $head = various_payment_prepare_head($object); // Clone confirmation if ($action === 'clone') { $set_value_help = $form->textwithpicto('', $langs->trans($langs->trans("AccountingDirectionHelp"))); $sensarray = array('0' => $langs->trans("Debit"), '1' => $langs->trans("Credit")); $formquestion = array( array('type' => 'text', 'name' => 'clone_label', 'label' => $langs->trans("Label"), 'value' => $langs->trans("CopyOf").' '.$object->label), array('type' => 'date', 'tdclass' => 'fieldrequired', 'name' => 'clone_date_payment', 'label' => $langs->trans("DatePayment"), 'value' => -1), array('type' => 'date', 'name' => 'clone_date_value', 'label' => $langs->trans("DateValue"), 'value' => -1), array('type' => 'other', 'tdclass' => 'fieldrequired', 'name' => 'clone_accountid', 'label' => $langs->trans("BankAccount"), 'value' => $form->select_comptes($object->fk_account, "accountid", 0, '', 1, '', 0, 'minwidth200', 1)), array('type' => 'text', 'name' => 'clone_amount', 'label' => $langs->trans("Amount"), 'value' => price($object->amount)), array('type' => 'select', 'name' => 'clone_sens', 'label' => $langs->trans("Sens").' '.$set_value_help, 'values' => $sensarray, 'default' => $object->sens), ); print $form->formconfirm($_SERVER["PHP_SELF"].'?id='.$object->id, $langs->trans('ToClone'), $langs->trans('ConfirmCloneVariousPayment', $object->ref), 'confirm_clone', $formquestion, 'yes', 1, 350); } // Confirmation of the removal of the Various Payment if ($action == 'delete') { $text = $langs->trans('ConfirmDeleteVariousPayment'); print $form->formconfirm($_SERVER['PHP_SELF'].'?id='.$object->id, $langs->trans('DeleteVariousPayment'), $text, 'confirm_delete', '', '', 2); } print dol_get_fiche_head($head, 'card', $langs->trans("VariousPayment"), -1, $object->picto); $morehtmlref = '
'; // Project if (isModEnabled('project') && $formproject !== null) { $langs->load("projects"); //$morehtmlref .= '
'; if ($permissiontoadd) { $morehtmlref .= img_picto($langs->trans("Project"), 'project', 'class="pictofixedwidth"'); if ($action != 'classify') { $morehtmlref .= ''.img_edit($langs->transnoentitiesnoconv('SetProject')).' '; } if ($action == 'classify') { //$morehtmlref.=$form->form_project($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->socid, $object->fk_project, 'projectid', 0, 0, 1, 1); $morehtmlref .= '
'; $morehtmlref .= ''; $morehtmlref .= ''; $morehtmlref .= $formproject->select_projects(-1, $object->fk_project, 'projectid', 0, 0, 1, 1, 0, 0, 0, '', 1, 0, 'maxwidth500 widthcentpercentminusxx'); $morehtmlref .= ''; $morehtmlref .= '
'; } else { $morehtmlref .= $form->form_project($_SERVER['PHP_SELF'].'?id='.$object->id, (property_exists($object, 'socid') ? $object->socid : 0), $object->fk_project, ($action == 'classify' ? 'projectid' : 'none'), 0, 0, 0, 1, '', 'maxwidth300'); } } else { if (!empty($object->fk_project)) { $proj = new Project($db); $proj->fetch($object->fk_project); $morehtmlref .= $proj->getNomUrl(1); if ($proj->title) { $morehtmlref .= ' - '.dol_escape_htmltag($proj->title).''; } } } } $morehtmlref .= '
'; $linkback = ''.$langs->trans("BackToList").''; $morehtmlstatus = ''; dol_banner_tab($object, 'id', $linkback, 1, 'rowid', 'ref', $morehtmlref, '', 0, '', $morehtmlstatus); print '
'; print '
'; print ''; // Label print ''; // Payment date print ""; print ''; // Value date print ''; // Debit / Credit if ($object->sens == '1') { $sens = $langs->trans("Credit"); } else { $sens = $langs->trans("Debit"); } print ''; print ''; // Account of Chart of account $editvalue = ''; if (isModEnabled('accounting')) { /** @var FormAccounting $formaccounting */ print ''; } else { print ''; } // Subledger account print ''; $bankaccountnotfound = 0; if (isModEnabled('bank')) { print ''; print ''; print ''; print ''; } // Other attributes $parameters = array('socid' => $object->id); include DOL_DOCUMENT_ROOT.'/core/tpl/extrafields_view.tpl.php'; print '
'.$langs->trans("Label").''.$object->label.'
'.$langs->trans("DatePayment").''; print dol_print_date($object->datep, 'day'); print '
'.$langs->trans("DateValue").''; print dol_print_date($object->datev, 'day'); print '
'.$langs->trans("Sens").''.$sens.'
'.$langs->trans("Amount").''.price($object->amount, 0, $langs, 1, -1, -1, $conf->currency).'
'; print $form->editfieldkey('AccountAccounting', 'accountancy_code', $object->accountancy_code, $object, (!$alreadyaccounted && $permissiontoadd), 'string', '', 0); print ''; if ($action == 'editaccountancy_code' && (!$alreadyaccounted && $permissiontoadd)) { //print $form->editfieldval('AccountAccounting', 'accountancy_code', $object->accountancy_code, $object, (!$alreadyaccounted && $user->hasRight('banque', 'modifier')), 'string', '', 0); print $formaccounting->formAccountingAccount($_SERVER['PHP_SELF'].'?id='.$object->id, $object->accountancy_code, 'accountancy_code', 0, 1, '', 1); } else { $accountingaccount = new AccountingAccount($db); $accountingaccount->fetch(0, $object->accountancy_code, 1); print $accountingaccount->getNomUrl(0, 1, 1, '', 1); } print '
'; print $langs->trans("AccountAccounting"); print ''; print $object->accountancy_code; print '
'; print $form->editfieldkey('SubledgerAccount', 'subledger_account', $object->subledger_account, $object, (!$alreadyaccounted && $permissiontoadd), 'string', '', 0); print ''; if ($action == 'editsubledger_account' && (!$alreadyaccounted && $permissiontoadd)) { if (getDolGlobalString('ACCOUNTANCY_COMBO_FOR_AUX')) { /** @var FormAccounting $formaccounting */ print $formaccounting->formAccountingAccount($_SERVER['PHP_SELF'] . '?id=' . $object->id, $object->subledger_account, 'subledger_account', 1, 1, '', 1); } else { print $form->editfieldval('SubledgerAccount', 'subledger_account', $object->subledger_account, $object, (!$alreadyaccounted && $permissiontoadd), 'string', '', null, null, '', 1, 'lengthAccounta'); } } else { print length_accounta($object->subledger_account); } print '
'.$langs->trans('BankTransactionLine').''; if ($object->fk_bank > 0) { $bankline = new AccountLine($db); $result = $bankline->fetch($object->fk_bank); if ($result <= 0) { $bankaccountnotfound = 1; } else { print $bankline->getNomUrl(1, 0, 'showall'); } } else { $bankaccountnotfound = 1; print ''.$langs->trans("NoRecordfound").''; } print '
'; print '
'; print '
'; print dol_get_fiche_end(); /* * Action bar */ print '
'."\n"; // TODO // Add button modify // Clone if ($permissiontoadd) { print '"; } // Delete if (empty($object->rappro) || $bankaccountnotfound) { if ($permissiontoadd) { if ($alreadyaccounted) { print ''; } else { print ''; } } else { print ''; } } else { print ''; } print "
"; } // End of page llxFooter(); $db->close();