30 require
'../../main.inc.php';
31 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
32 require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/bookkeeping.class.php';
33 require_once DOL_DOCUMENT_ROOT.
'/core/class/html.formaccounting.class.php';
34 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
35 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
36 require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/accountingjournal.class.php';
37 require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/accountingaccount.class.php';
40 $langs->loadLangs(array(
"accountancy",
"bills",
"compta"));
42 $action =
GETPOST(
'action',
'aZ09');
43 $cancel =
GETPOST(
'cancel',
'aZ09');
45 $optioncss =
GETPOST(
'optioncss',
'aZ');
48 $mode = $mode = $action ==
'create' ?
"_tmp" :
GETPOST(
'mode',
'aZ09');
49 $piece_num =
GETPOST(
"piece_num",
'int');
54 $accountingaccount_number =
GETPOST(
'accountingaccount_number',
'alphanohtml');
55 $accountingaccount->fetch(
null, $accountingaccount_number,
true);
56 $accountingaccount_label = $accountingaccount->label;
58 $journal_code =
GETPOST(
'code_journal',
'alpha') ?
GETPOST(
'code_journal',
'alpha') :
"NULL";
59 $accountingjournal->fetch(
null, $journal_code);
60 $journal_label = $accountingjournal->label;
62 $next_num_mvt = (int)
GETPOST(
'next_num_mvt',
'alpha');
63 $doc_ref = (string)
GETPOST(
'doc_ref',
'alpha');
64 $doc_date = (string)
GETPOST(
'doc_date',
'alpha');
67 $subledger_account =
GETPOST(
'subledger_account',
'alphanohtml');
68 if ($subledger_account == -1) {
69 $subledger_account =
null;
71 $subledger_label =
GETPOST(
'subledger_label',
'alphanohtml');
73 $label_operation =
GETPOST(
'label_operation',
'alphanohtml');
77 $save =
GETPOST(
'save',
'alpha');
81 $valid =
GETPOST(
'validate',
'alpha');
85 $update =
GETPOST(
'update',
'alpha');
86 if (!empty($update)) {
87 $action =
'confirm_update';
96 if ($user->socid > 0) {
99 if (!$user->hasRight(
'accounting',
'mouvements',
'lire')) {
109 header(
"Location: ".DOL_URL_ROOT.
'/accountancy/bookkeeping/list.php');
113 if ($action ==
"confirm_update") {
116 if ((floatval($debit) != 0.0) && (floatval($credit) != 0.0)) {
121 if (empty($accountingaccount_number) || $accountingaccount_number ==
'-1') {
123 setEventMessages($langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
"AccountAccountingShort")),
null,
'errors');
130 $result = $object->fetch($id,
null, $mode);
135 $object->numero_compte = $accountingaccount_number;
136 $object->subledger_account = $subledger_account;
137 $object->subledger_label = $subledger_label;
138 $object->label_compte = $accountingaccount_label;
139 $object->label_operation = $label_operation;
140 $object->debit = $debit;
141 $object->credit = $credit;
143 if (floatval($debit) != 0.0) {
144 $object->montant = $debit;
145 $object->amount = $debit;
148 if (floatval($credit) != 0.0) {
149 $object->montant = $credit;
150 $object->amount = $credit;
154 $result = $object->update($user,
false, $mode);
158 if ($mode !=
'_tmp') {
169 } elseif ($action ==
'add' || $action ==
'valid') {
172 if (array_sum($debit) != array_sum($credit)) {
176 foreach ($accountingaccount_number as $key => $value) {
177 $accountingaccount->fetch(
null, $accountingaccount_number[$key],
true);
178 $accountingaccount_label[$key] = $accountingaccount->label[$key];
181 if ($key < 1 && (empty($accountingaccount_number[$key]) || $accountingaccount_number[$key] ==
'-1') || (floatval($debit[$key]) == 0.0) && (floatval($credit[$key]) == 0.0)) {
185 if ((floatval($debit[$key]) != 0.0) && (floatval($credit[$key]) != 0.0)) {
191 if (empty($accountingaccount_number[$key]) || $accountingaccount_number[$key] ==
'-1') {
193 setEventMessages($langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
"AccountAccountingShort")),
null,
'errors');
199 $object->numero_compte = $accountingaccount_number[$key];
200 $object->subledger_account = $subledger_account[$key];
201 $object->subledger_label = $subledger_label[$key];
202 $object->label_compte = $accountingaccount_label[$key];
203 $object->label_operation = $label_operation[$key];
204 $object->debit =
price2num($debit[$key]);
205 $object->credit =
price2num($credit[$key]);
206 $object->doc_date = $doc_date;
207 $object->doc_type = (string)
GETPOST(
'doc_type',
'alpha');
208 $object->piece_num = $piece_num;
209 $object->doc_ref = $doc_ref;
210 $object->code_journal = $journal_code;
211 $object->journal_label = $journal_label;
215 if (floatval($debit[$key]) != 0.0) {
216 $object->montant = $object->debit;
217 $object->amount = $object->debit;
221 if (floatval($credit[$key]) != 0.0) {
222 $object->montant = $object->credit;
223 $object->amount = $object->credit;
227 $result = $object->createStd($user,
false, $mode);
235 if ($mode !=
'_tmp') {
241 $action = $action ==
'add' ?
'' : $action ;
243 } elseif ($action ==
"confirm_delete") {
246 $result = $object->fetch($id,
null, $mode);
247 $piece_num = $object->piece_num;
252 $result = $object->delete($user,
false, $mode);
258 } elseif ($action ==
'create') {
263 $next_num_mvt = !empty($next_num_mvt) ? $next_num_mvt : $object->getNextNumMvt(
'_tmp');
264 $doc_ref = !empty($doc_ref) ? $doc_ref : $next_num_mvt;
266 if (empty($doc_date)) {
268 $_POST[
'doc_dateday'] = $tmp_date[
'mday'];
269 $_POST[
'doc_datemonth'] = $tmp_date[
'mon'];
270 $_POST[
'doc_dateyear'] = $tmp_date[
'year'];
274 if (!$journal_code || $journal_code ==
'-1') {
275 setEventMessages($langs->trans(
'ErrorFieldRequired', $langs->transnoentitiesnoconv(
"Journal")),
null,
'errors');
279 if (empty($doc_ref)) {
280 setEventMessages($langs->trans(
"ErrorFieldRequired", $langs->transnoentitiesnoconv(
"Piece")),
null,
'errors');
286 $object->label_compte =
'';
290 $object->doc_type =
GETPOST(
'doc_type',
'alpha');
291 $object->piece_num = $next_num_mvt;
292 $object->doc_ref = $doc_ref;
293 $object->code_journal = $journal_code;
294 $object->journal_label = $journal_label;
296 $object->fk_docdet = 0;
297 $object->montant = 0;
300 $result = $object->createStd($user, 0, $mode);
304 if ($mode !=
'_tmp') {
309 $piece_num = $object->piece_num;
314 if ($action ==
'setdate') {
316 $result = $object->updateByMvt($piece_num,
'doc_date', $db->idate($datedoc), $mode);
320 if ($mode !=
'_tmp') {
327 if ($action ==
'setjournal') {
328 $result = $object->updateByMvt($piece_num,
'code_journal', $journal_code, $mode);
329 $result = $object->updateByMvt($piece_num,
'journal_label', $journal_label, $mode);
333 if ($mode !=
'_tmp') {
340 if ($action ==
'setdocref') {
342 $result = $object->updateByMvt($piece_num,
'doc_ref', $refdoc, $mode);
346 if ($mode !=
'_tmp') {
354 if ($action ==
'valid') {
355 $result = $object->transformTransaction(0, $piece_num);
359 header(
"Location: list.php?sortfield=t.piece_num&sortorder=asc");
369 $html =
new Form($db);
372 $title = $langs->trans($mode ==
"_tmp" ?
"CreateMvts":
"UpdateMvts");
377 if ($action ==
'delete') {
378 $formconfirm = $html->formconfirm($_SERVER[
"PHP_SELF"].
'?id='.$id.
'&mode='.$mode, $langs->trans(
'DeleteMvt'), $langs->trans(
'ConfirmDeleteMvt', $langs->transnoentitiesnoconv(
"RegistrationInAccounting")),
'confirm_delete',
'', 0, 1);
384 $result = $object->fetchPerMvt($piece_num, $mode);
389 if (!empty($object->piece_num)) {
390 $backlink =
'<a href="'.DOL_URL_ROOT.
'/accountancy/bookkeeping/list.php?restore_lastsearch_values=1">'.$langs->trans(
'BackToList').
'</a>';
392 print
load_fiche_titre($langs->trans($mode ==
"_tmp" ?
"CreateMvts":
"UpdateMvts"), $backlink);
394 print
'<form action="'.$_SERVER[
"PHP_SELF"].
'?piece_num='.$object->piece_num.
'" method="post">';
if ($optioncss !=
'') {
395 print
'<input type="hidden" name="optioncss" value="'.$optioncss.
'" />';
399 $head[$h][0] = $_SERVER[
'PHP_SELF'].
'?piece_num='.$object->piece_num.($mode ?
'&mode='.$mode :
'');
400 $head[$h][1] = $langs->trans(
"Transaction");
401 $head[$h][2] =
'transaction';
408 print
'<div class="fichecenter">';
409 print
'<div class="fichehalfleft">';
411 print
'<div class="underbanner clearboth"></div>';
412 print
'<table class="border tableforfield" width="100%">';
420 print
'<td class="titlefieldcreate fieldrequired">'.$langs->trans(
"Docdate").
'</td>';
422 print $html->selectDate($doc_date,
'doc_date',
'',
'',
'',
"create_mvt", 1, 1);
427 print
'<td class="fieldrequired">'.$langs->trans(
"Codejournal").
'</td>';
428 print
'<td>'.$formaccounting->select_journal($journal_code,
'code_journal', 0, 0, 1, 1).
'</td>';
432 print
'<td class="fieldrequired">'.$langs->trans(
"Piece").
'</td>';
433 print
'<td><input type="text" class="minwidth200" name="doc_ref" value="'.$doc_ref.
'" /></td>';
447 print
'<div class="fichehalfright">';
449 print
'<div class="underbanner clearboth"></div>';
450 print
'<table class="border tableforfield centpercent">';
453 if (!empty($object->doc_type)) {
455 print
'<td class="titlefield">'.$langs->trans(
"Doctype").
'</td>';
456 print
'<td>'.$object->doc_type.
'</td>';
462 print
'<td class="titlefield">'.$langs->trans(
"DateCreation").
'</td>';
464 print $object->date_creation ?
dol_print_date($object->date_creation,
'day') :
' ';
469 if ($mode !=
"_tmp") {
472 print
'<td class="titlefield">'.$langs->trans(
"DateExport").
'</td>';
474 print $object->date_export ?
dol_print_date($object->date_export,
'dayhour') :
' ';
480 print
'<td class="titlefield">'.$langs->trans(
"DateValidation").
'</td>';
482 print $object->date_validation ?
dol_print_date($object->date_validation,
'dayhour') :
' ';
530 print
'<div class="clearboth"></div>';
534 $result = $object->fetchAllPerMvt($piece_num, $mode);
541 if ($optioncss !=
'') {
542 print
'<input type="hidden" name="optioncss" value="'.$optioncss.
'" />';
545 print
'<input type="hidden" name="token" value="'.newToken().
'" />';
546 print
'<input type="hidden" name="doc_type" value="'.$object->doc_type.
'" />'.
"\n";
547 print
'<input type="hidden" name="fk_doc" value="'.$object->fk_doc.
'" />'.
"\n";
548 print
'<input type="hidden" name="fk_docdet" value="'.$object->fk_docdet.
'" />'.
"\n";
549 print
'<input type="hidden" name="mode" value="'.$mode.
'" />'.
"\n";
551 if (count($object->linesmvt) > 0) {
552 print
'<div class="div-table-responsive-no-min">';
553 print
'<table class="noborder centpercent">';
559 if ($mode !=
"_tmp") {
562 print
'<td class="titlefield">' . $langs->trans(
"DateExport") .
'</td>';
564 print $object->date_export ?
dol_print_date($object->date_export,
'dayhour') :
' ';
570 print
'<td class="titlefield">' . $langs->trans(
"DateValidation") .
'</td>';
572 print $object->date_validation ?
dol_print_date($object->date_validation,
'dayhour') :
' ';
577 print
'<tr class="liste_titre">';
584 if (empty($object->date_validation)) {
593 if ($mode ==
"_tmp") {
594 array_shift($object->linesmvt);
599 $object->linesmvt[] = $line;
602 if (empty($object->linesmvt[1])) {
604 $object->linesmvt[] = $line;
607 $count_line = count($object->linesmvt);
609 foreach ($object->linesmvt as $key => $line) {
611 print
'<tr class="oddeven" data-lineid="'.((int) $line->id).
'">';
612 $total_debit += $line->debit;
613 $total_credit += $line->credit;
615 if ($action ==
'update' && $line->id == $id) {
616 print
'<!-- td columns in edit mode -->';
618 print $formaccounting->select_account((
GETPOSTISSET(
"accountingaccount_number") ?
GETPOST(
"accountingaccount_number",
"alpha") : $line->numero_compte),
'accountingaccount_number', 1, array(), 1, 1,
'');
625 if (!empty($conf->global->ACCOUNTANCY_COMBO_FOR_AUX)) {
626 print $formaccounting->select_auxaccount((
GETPOSTISSET(
"subledger_account") ?
GETPOST(
"subledger_account",
"alpha") : $line->subledger_account),
'subledger_account', 1,
'maxwidth250',
'',
'subledger_label');
628 print
'<input type="text" class="maxwidth150" name="subledger_account" value="'.(GETPOSTISSET(
"subledger_account") ?
GETPOST(
"subledger_account",
"alpha") : $line->subledger_account).
'" placeholder="'.
dol_escape_htmltag($langs->trans(
"SubledgerAccount")).
'" />';
631 print
'<br><input type="text" class="maxwidth150" name="subledger_label" value="'.(GETPOSTISSET(
"subledger_label") ?
GETPOST(
"subledger_label",
"alpha") : $line->subledger_label).
'" placeholder="'.
dol_escape_htmltag($langs->trans(
"SubledgerAccountLabel")).
'" />';
633 print
'<td><input type="text" class="minwidth200" name="label_operation" value="'.(GETPOSTISSET(
"label_operation") ?
GETPOST(
"label_operation",
"alpha") : $line->label_operation).
'" /></td>';
634 print
'<td class="right"><input type="text" size="6" class="right" name="debit" value="'.(GETPOSTISSET(
"debit") ?
GETPOST(
"debit",
"alpha") :
price($line->debit)).
'" /></td>';
635 print
'<td class="right"><input type="text" size="6" class="right" name="credit" value="'.(GETPOSTISSET(
"credit") ?
GETPOST(
"credit",
"alpha") :
price($line->credit)).
'" /></td>';
637 print
'<input type="hidden" name="id" value="'.$line->id.
'" />'.
"\n";
638 print
'<input type="submit" class="button" name="update" value="'.$langs->trans(
"Update").
'" />';
640 } elseif (empty($line->numero_compte) || (empty($line->debit) && empty($line->credit))) {
641 if ($action ==
"" || $action ==
'add') {
642 print
'<!-- td columns in add mode -->';
644 print $formaccounting->select_account((is_array($accountingaccount_number) ? $accountingaccount_number[$key] : $accountingaccount_number ),
'accountingaccount_number['.$key.
']', 1, array(), 1, 1,
'');
651 if (!empty($conf->global->ACCOUNTANCY_COMBO_FOR_AUX)) {
652 print $formaccounting->select_auxaccount((is_array($subledger_account) ? $subledger_account[$key] : $subledger_account ),
'subledger_account['.$key.
']', 1,
'maxwidth250',
'',
'subledger_label');
654 print
'<input type="text" class="maxwidth150" name="subledger_account['.$key.
']" value="' . (is_array($subledger_account) ? $subledger_account[$key] : $subledger_account ) .
'" placeholder="' .
dol_escape_htmltag($langs->trans(
"SubledgerAccount")) .
'" />';
656 print
'<br><input type="text" class="maxwidth150" name="subledger_label['.$key.
']" value="' . (is_array($subledger_label) ? $subledger_label[$key] : $subledger_label ) .
'" placeholder="' .
dol_escape_htmltag($langs->trans(
"SubledgerAccountLabel")) .
'" />';
658 print
'<td><input type="text" class="minwidth200" name="label_operation['.$key.
']" value="' . (is_array($label_operation) ? $label_operation[$key] : $label_operation ) .
'"/></td>';
659 print
'<td class="right"><input type="text" size="6" class="right" name="debit['.$key.
']" value="' . (is_array($debit) ? $debit[$key] : $debit ) .
'" /></td>';
660 print
'<td class="right"><input type="text" size="6" class="right" name="credit['.$key.
']" value="' . (is_array($credit) ? $credit[$key] : $credit ) .
'" /></td>';
662 if ($num_line === $count_line) {
663 print
'<td><input type="submit" class="button small" name="save" value="' . $langs->trans(
"Add") .
'" /></td>';
665 print
'<td class="right"></td>';
669 print
'<!-- td columns in display mode -->';
670 $resultfetch = $accountingaccount->fetch(
null, $line->numero_compte,
true);
672 if ($resultfetch > 0) {
673 print $accountingaccount->getNomUrl(0, 1, 1,
'', 0);
675 print $line->numero_compte.
' <span class="warning">('.$langs->trans(
"AccountRemovedFromCurrentChartOfAccount").
')</span>';
678 print
'<td>'.length_accounta($line->subledger_account);
679 if ($line->subledger_label) {
680 print
' - <span class="opacitymedium">'.$line->subledger_label.
'</span>';
683 print
'<td>'.$line->label_operation.
'</td>';
684 print
'<td class="right nowraponall amount">'.($line->debit != 0 ?
price($line->debit) :
'').
'</td>';
685 print
'<td class="right nowraponall amount">'.($line->credit != 0 ?
price($line->credit) :
'').
'</td>';
687 print
'<td class="center nowraponall">';
688 if (empty($line->date_export) && empty($line->date_validation)) {
689 print
'<a class="editfielda reposition" href="' . $_SERVER[
"PHP_SELF"] .
'?action=update&id=' . $line->id .
'&piece_num=' . urlencode($line->piece_num) .
'&mode=' . urlencode($mode) .
'&token=' . urlencode(
newToken()) .
'">';
690 print
img_edit(
'', 0,
'class="marginrightonly"');
693 print
'<a class="editfielda nohover cursornotallowed reposition disabled" href="#" title="'.dol_escape_htmltag($langs->trans(
"ForbiddenTransactionAlreadyExported")).
'">';
694 print
img_edit($langs->trans(
"ForbiddenTransactionAlreadyExported"), 0,
'class="marginrightonly"');
698 if (empty($line->date_validation)) {
699 $actiontodelete =
'delete';
700 if ($mode ==
'_tmp' || $action !=
'delmouv') {
701 $actiontodelete =
'confirm_delete';
704 print
'<a href="' . $_SERVER[
"PHP_SELF"] .
'?action=' . $actiontodelete .
'&id=' . $line->id .
'&piece_num=' . urlencode($line->piece_num) .
'&mode=' . urlencode($mode) .
'&token=' . urlencode(
newToken()) .
'">';
708 print
'<a class="editfielda nohover cursornotallowed disabled" href="#" title="'.dol_escape_htmltag($langs->trans(
"ForbiddenTransactionAlreadyExported")).
'">';
709 print
img_delete($langs->trans(
"ForbiddenTransactionAlreadyValidated"));
718 $total_debit =
price2num($total_debit,
'MT');
719 $total_credit =
price2num($total_credit,
'MT');
721 if ($total_debit != $total_credit) {
722 setEventMessages(
null, array($langs->trans(
'MvtNotCorrectlyBalanced', $total_debit, $total_credit)),
'warnings');
728 if ($mode ==
'_tmp' && $action ==
'') {
730 print
'<div class="center">';
731 if ($total_debit == $total_credit) {
732 print
'<input type="submit" class="button" name="validate" value="' . $langs->trans(
"ValidTransaction") .
'" />';
734 print
'<input type="submit" class="button" disabled="disabled" href="#" title="'.dol_escape_htmltag($langs->trans(
"MvtNotCorrectlyBalanced", $debit, $credit)).
'" value="'.
dol_escape_htmltag($langs->trans(
"ValidTransaction")).
'" />';
738 print
'<a class="button button-cancel" href="'.DOL_URL_ROOT.
'/accountancy/bookkeeping/list.php">'.$langs->trans(
"Cancel").
'</a>';