* Copyright (C) 2013-2017 Florian Henry * Copyright (C) 2013-2018 Alexandre Spangaro * Copyright (C) 2017 Laurent Destailleur * * 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/accountancy/bookkeeping/card.php * \ingroup Advanced accountancy * \brief Page to show book-entry */ require '../../main.inc.php'; require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php'; require_once DOL_DOCUMENT_ROOT . '/accountancy/class/bookkeeping.class.php'; require_once DOL_DOCUMENT_ROOT . '/core/class/html.formaccounting.class.php'; require_once DOL_DOCUMENT_ROOT . '/compta/facture/class/facture.class.php'; require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php'; require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingjournal.class.php'; // Load translation files required by the page $langs->loadLangs(array("accountancy", "bills", "compta")); $action = GETPOST('action','aZ09'); $id = GETPOST('id', 'int'); // id of record $mode = GETPOST('mode','aZ09'); // '' or 'tmp' $piece_num = GETPOST("piece_num",'int'); // id of transaction (several lines share the same transaction id) // Security check if ($user->societe_id > 0) { accessforbidden(); } $mesg = ''; $account_number = GETPOST('account_number','alphanohtml'); $subledger_account = GETPOST('subledger_account','alphanohtml'); if ($subledger_account == - 1) { $subledger_account = null; } $label_compte = GETPOST('label_compte','alphanohtml'); $label_operation= GETPOST('label_operation','alphanohtml'); $debit = price2num(GETPOST('debit','alpha')); $credit = price2num(GETPOST('credit','alpha')); $save = GETPOST('save','alpha'); if (! empty($save)) $action = 'add'; $update = GETPOST('update','alpha'); if (! empty($update)) $action = 'confirm_update'; $object = new BookKeeping($db); /* * Actions */ if ($action == "confirm_update") { $error = 0; if ((floatval($debit) != 0.0) && (floatval($credit) != 0.0)) { $error++; setEventMessages($langs->trans('ErrorDebitCredit'), null, 'errors'); $action='update'; } if (empty($account_number) || $account_number == '-1') { $error++; setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("AccountAccountingShort")), null, 'errors'); $action='update'; } if (! $error) { $object = new BookKeeping($db); $result = $object->fetch($id, null, $mode); if ($result < 0) { $error++; setEventMessages($object->error, $object->errors, 'errors'); } else { $object->numero_compte = $account_number; $object->subledger_account = $subledger_account; $object->label_compte = $label_compte; $object->label_operation= $label_operation; $object->debit = $debit; $object->credit = $credit; if (floatval($debit) != 0.0) { $object->montant = $debit; $object->sens = 'D'; } if (floatval($credit) != 0.0) { $object->montant = $credit; $object->sens = 'C'; } $result = $object->update($user, false, $mode); if ($result < 0) { setEventMessages($object->error, $object->errors, 'errors'); } else { if ($mode != '_tmp') { setEventMessages($langs->trans('RecordSaved'), null, 'mesgs'); } $debit = 0; $credit = 0; $action = ''; } } } } else if ($action == "add") { $error = 0; if ((floatval($debit) != 0.0) && (floatval($credit) != 0.0)) { $error++; setEventMessages($langs->trans('ErrorDebitCredit'), null, 'errors'); $action=''; } if (empty($account_number) || $account_number == '-1') { $error++; setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("AccountAccountingShort")), null, 'errors'); $action=''; } if (! $error) { $object = new BookKeeping($db); $object->numero_compte = $account_number; $object->subledger_account = $subledger_account; $object->label_compte = $label_compte; $object->label_operation= $label_operation; $object->debit = $debit; $object->credit = $credit; $object->doc_date = GETPOST('doc_date','alpha'); $object->doc_type = GETPOST('doc_type','alpha'); $object->piece_num = $piece_num; $object->doc_ref = GETPOST('doc_ref','alpha'); $object->code_journal = GETPOST('code_journal','alpha'); $object->fk_doc = GETPOST('fk_doc','alpha'); $object->fk_docdet = GETPOST('fk_docdet','alpha'); if (floatval($debit) != 0.0) { $object->montant = $debit; $object->sens = 'D'; } if (floatval($credit) != 0.0) { $object->montant = $credit; $object->sens = 'C'; } $result = $object->createStd($user, false, $mode); if ($result < 0) { setEventMessages($object->error, $object->errors, 'errors'); } else { if ($mode != '_tmp') { setEventMessages($langs->trans('RecordSaved'), null, 'mesgs'); } $debit = 0; $credit = 0; $action = ''; } } } else if ($action == "confirm_delete") { $object = new BookKeeping($db); $result = $object->fetch($id, null, $mode); $piece_num = $object->piece_num; if ($result < 0) { setEventMessages($object->error, $object->errors, 'errors'); } else { $result = $object->delete($user, false, $mode); if ($result < 0) { setEventMessages($object->error, $object->errors, 'errors'); } } $action = ''; } else if ($action == "confirm_create") { $error = 0; $object = new BookKeeping($db); if (! GETPOST('code_journal','alpha') || GETPOST('code_journal','alpha') == '-1') { setEventMessages($langs->trans('ErrorFieldRequired', $langs->transnoentitiesnoconv("Journal")), null, 'errors'); $action='create'; $error++; } if (! GETPOST('next_num_mvt')) { setEventMessages($langs->trans("ErrorFieldRequired", $langs->transnoentitiesnoconv("NumPiece")), null, 'errors'); $error++; } if (! $error) { $object->label_compte = ''; $object->debit = 0; $object->credit = 0; $object->doc_date = $date_start = dol_mktime(0, 0, 0, GETPOST('doc_datemonth','int'), GETPOST('doc_dateday','int'), GETPOST('doc_dateyear','int')); $object->doc_type = GETPOST('doc_type','alpha'); $object->piece_num = GETPOST('next_num_mvt','alpha'); $object->doc_ref = GETPOST('doc_ref','alpha'); $object->code_journal = GETPOST('code_journal','alpha'); $object->fk_doc = 0; $object->fk_docdet = 0; $object->montant = 0; $result = $object->createStd($user,0, $mode); if ($result < 0) { setEventMessages($object->error, $object->errors, 'errors'); } else { if ($mode != '_tmp') { setEventMessages($langs->trans('RecordSaved'), null, 'mesgs'); } $action = 'update'; $id=$object->id; $piece_num = $object->piece_num; } } } if ($action == 'setdate') { $datedoc = dol_mktime(0, 0, 0, GETPOST('doc_datemonth'), GETPOST('doc_dateday'), GETPOST('doc_dateyear')); $result = $object->updateByMvt($piece_num,'doc_date',$db->idate($datedoc),$mode); if ($result < 0) { setEventMessages($object->error, $object->errors, 'errors'); } else { if ($mode != '_tmp') { setEventMessages($langs->trans('RecordSaved'), null, 'mesgs'); } $action = ''; } } if ($action == 'setjournal') { $journaldoc = trim(GETPOST('code_journal','alpha')); $result = $object->updateByMvt($piece_num, 'code_journal', $journaldoc, $mode); if ($result < 0) { setEventMessages($object->error, $object->errors, 'errors'); } else { if ($mode != '_tmp') { setEventMessages($langs->trans('RecordSaved'), null, 'mesgs'); } $action = ''; } } if ($action == 'setdocref') { $refdoc = trim(GETPOST('doc_ref','alpha')); $result = $object->updateByMvt($piece_num,'doc_ref',$refdoc,$mode); if ($result < 0) { setEventMessages($object->error, $object->errors, 'errors'); } else { if ($mode != '_tmp') { setEventMessages($langs->trans('RecordSaved'), null, 'mesgs'); } $action = ''; } } // Validate transaction if ($action == 'valid') { $result = $object->transformTransaction(0,$piece_num); if ($result < 0) { setEventMessages($object->error, $object->errors, 'errors'); } else { header("Location: list.php?sortfield=t.piece_num&sortorder=asc"); exit; } } /* * View */ $html = new Form($db); $formaccounting = new FormAccounting($db); $accountjournal = new AccountingJournal($db); llxHeader('', $langs->trans("CreateMvts")); // Confirmation to delete the command if ($action == 'delete') { $formconfirm = $html->formconfirm($_SERVER["PHP_SELF"] . '?id=' . $id.'&mode='. $mode, $langs->trans('DeleteMvt'), $langs->trans('ConfirmDeleteMvt'), 'confirm_delete', '', 0, 1); print $formconfirm; } if ($action == 'create') { print load_fiche_titre($langs->trans("CreateMvts")); $object = new BookKeeping($db); $next_num_mvt = $object->getNextNumMvt('_tmp'); if (empty($next_num_mvt)) { dol_print_error('', 'Failed to get next piece number'); } print '
'; print '' . "\n"; print '' . "\n"; print '' . "\n"; dol_fiche_head(); print ''; /*print ''; print ''; print ''; print '';*/ print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; /* print ''; print ''; print ''; print ''; */ print '
' . $langs->trans("NumPiece") . '' . $next_num_mvt . '
' . $langs->trans("Docdate") . ''; print $html->select_date('', 'doc_date', '', '', '', "create_mvt", 1, 1); print '
' . $langs->trans("Codejournal") . '' . $formaccounting->select_journal(GETPOST('code_journal'),'code_journal',0,1,array(),1,1) . '
' . $langs->trans("Piece") . '
' . $langs->trans("Doctype") . '
'; dol_fiche_end(); print '
'; print ''; print '     '; print ''; print '
'; print '
'; } else { $object = new BookKeeping($db); $result = $object->fetchPerMvt($piece_num, $mode); if ($result < 0) { setEventMessages($object->error, $object->errors, 'errors'); } if (! empty($object->piece_num)) { $backlink = '' . $langs->trans('BackToList') . ''; print load_fiche_titre($langs->trans("UpdateMvts"), $backlink); $head=array(); $h=0; $head[$h][0] = $_SERVER['PHP_SELF'].'?piece_num='.$object->piece_num.($mode?'&mode='.$mode:''); $head[$h][1] = $langs->trans("Transaction"); $head[$h][2] = 'transaction'; $h++; dol_fiche_head($head, 'transaction', '', -1); //dol_banner_tab($object, '', $backlink); print '
'; print '
'; print '
'; print ''; // Account movement print ''; print ''; print ''; print ''; // Date print ''; print ''; // Journal print ''; print ''; // Ref document print ''; print ''; print '
' . $langs->trans("NumMvts") . '' . $object->piece_num . '
'; print ''; if ($action != 'editdate') print ''; print '
'; print $langs->trans('Docdate'); print 'piece_num .'&mode='. $mode .'">'.img_edit($langs->transnoentitiesnoconv('SetDate'),1).'
'; print '
'; if ($action == 'editdate') { print '
'; print ''; print ''; print ''; $form->select_date($object->doc_date ? $object->doc_date : - 1, 'doc_date', '', '', '', "setdate"); print ''; print '
'; } else { print $object->doc_date ? dol_print_date($object->doc_date, 'day') : ' '; } print '
'; print ''; if ($action != 'editjournal') print ''; print '
'; print $langs->trans('Codejournal'); print 'piece_num.'&mode='. $mode .'">'.img_edit($langs->transnoentitiesnoconv('Edit'),1).'
'; print '
'; if ($action == 'editjournal') { print '
'; print ''; print ''; print ''; print $formaccounting->select_journal($object->code_journal,'code_journal',0,0,array(),1,1); print ''; print '
'; } else { print $object->code_journal ; } print '
'; print ''; if ($action != 'editdocref') print ''; print '
'; print $langs->trans('Piece'); print 'piece_num.'&mode='. $mode .'">'.img_edit($langs->transnoentitiesnoconv('Edit'),1).'
'; print '
'; if ($action == 'editdocref') { print '
'; print ''; print ''; print ''; print ''; print ''; print '
'; } else { print $object->doc_ref ; } print '
'; print '
'; print '
'; print '
'; print ''; // Doc type if(! empty($object->doc_type)) { print ''; print ''; print ''; print ''; } // Date document creation print ''; print ''; print ''; print ''; // Validate /* print ''; print ''; print ''; print ''; */ // check data /* print ''; print ''; if ($object->doc_type == 'customer_invoice') { $sqlmid = 'SELECT rowid as ref'; $sqlmid .= " FROM ".MAIN_DB_PREFIX."facture as fac"; $sqlmid .= " WHERE fac.rowid=" . $object->fk_doc; dol_syslog("accountancy/bookkeeping/card.php::sqlmid=" . $sqlmid, LOG_DEBUG); $resultmid = $db->query($sqlmid); if ($resultmid) { $objmid = $db->fetch_object($resultmid); $invoicestatic = new Facture($db); $invoicestatic->fetch($objmid->ref); $ref=$langs->trans("Invoice").' '.$invoicestatic->getNomUrl(1); } else dol_print_error($db); } print ''; print ''; */ print "
' . $langs->trans("Doctype") . '' . $object->doc_type . '
' . $langs->trans("DateCreation") . ''; print $object->date_creation ? dol_print_date($object->date_creation, 'day') : ' '; print '
' . $langs->trans("Status") . ''; if (empty($object->validated)) { print ''; print img_picto($langs->trans("Disabled"), 'switch_off'); print ''; } else { print ''; print img_picto($langs->trans("Activated"), 'switch_on'); print ''; } print '
' . $langs->trans("Control") . '' . $ref .'
\n"; print '
'; dol_fiche_end(); print '
'; print '
'; $result = $object->fetchAllPerMvt($piece_num, $mode); if ($result < 0) { setEventMessages($object->error, $object->errors, 'errors'); } else { print load_fiche_titre($langs->trans("ListeMvts"), '', ''); print '
'; print '' . "\n"; print '' . "\n"; print '' . "\n"; print '' . "\n"; print '' . "\n"; print '' . "\n"; print '' . "\n"; print ""; if (count($object->linesmvt) > 0) { $total_debit = 0; $total_credit = 0; print ''; print_liste_field_titre("AccountAccountingShort"); print_liste_field_titre("SubledgerAccount"); print_liste_field_titre("LabelAccount"); print_liste_field_titre("LabelOperation"); print_liste_field_titre("Debit", "", "", "", "", 'align="right"'); print_liste_field_titre("Credit", "", "", "", "", 'align="right"'); print_liste_field_titre("Action", "", "", "", "", 'width="60" align="center"'); print "\n"; foreach ($object->linesmvt as $line) { print ''; $total_debit += $line->debit; $total_credit += $line->credit; if ($action == 'update' && $line->id == $id) { print ''; print ''; print ''; print ''; print ''; print ''; print ''; } else { print ''; print ''; print ''; print ''; print ''; print ''; print ''; } print "\n"; } $total_debit = price2num($total_debit); $total_credit = price2num($total_credit); if ($total_debit != $total_credit) { setEventMessages(null, array($langs->trans('MvtNotCorrectlyBalanced', $total_debit, $total_credit)), 'warnings'); } if ($action == "" || $action == 'add') { print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; print ''; } print '
'; print $formaccounting->select_account($line->numero_compte, 'account_number', 1, array (), 1, 1, ''); print ''; // TODO For the moment we keep a free input text instead of a combo. The select_auxaccount has problem because it does not // use setup of keypress to select thirdparty and this hang browser on large database. if (! empty($conf->global->ACCOUNTANCY_COMBO_FOR_AUX)) { print $formaccounting->select_auxaccount($line->subledger_account, 'subledger_account', 1); } else { print ''; } print ''; print '' . "\n"; print ''; print '' . length_accountg($line->numero_compte) . '' . length_accounta($line->subledger_account) . '' . $line->label_compte . '' . $line->label_operation. '' . price($line->debit) . '' . price($line->credit) . ''; print 'id . '&piece_num=' . $line->piece_num . '&mode='.$mode.'">'; print img_edit(); print '  '; $actiontodelete='delete'; if ($mode == '_tmp' || $action != 'delmouv') $actiontodelete='confirm_delete'; print ''; print img_delete(); print ''; print '
'; print $formaccounting->select_account($account_number, 'account_number', 1, array (), 1, 1, ''); print ''; // TODO For the moment we keep a fre input text instead of a combo. The select_auxaccount has problem because it does not // use setup of keypress to select thirdparty and this hang browser on large database. if (! empty($conf->global->ACCOUNTANCY_COMBO_FOR_AUX)) { print $formaccounting->select_auxaccount($subledger_account, 'subledger_account', 1); } else { print ''; } print '
'; if ($mode=='_tmp' && $action=='') { print '
'; print '
'; if ($total_debit == $total_credit) { print ''.$langs->trans("ValidTransaction").''; } else { print ''; } print '   '; print ''.$langs->trans("Cancel").''; print "
"; } print '
'; } } } else { print load_fiche_titre($langs->trans("NoRecords")); } } dol_fiche_end(); llxFooter(); $db->close();