31 require
'../../main.inc.php';
32 require_once DOL_DOCUMENT_ROOT.
'/core/lib/report.lib.php';
33 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
34 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
35 require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/accountingjournal.class.php';
36 require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/accountingaccount.class.php';
37 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
38 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.class.php';
39 require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/bookkeeping.class.php';
42 $langs->loadLangs(array(
"commercial",
"compta",
"bills",
"other",
"accountancy",
"errors"));
44 $id_journal =
GETPOST(
'id_journal',
'int');
45 $action =
GETPOST(
'action',
'aZ09');
47 $date_startmonth =
GETPOST(
'date_startmonth');
48 $date_startday =
GETPOST(
'date_startday');
49 $date_startyear =
GETPOST(
'date_startyear');
50 $date_endmonth =
GETPOST(
'date_endmonth');
51 $date_endday =
GETPOST(
'date_endday');
52 $date_endyear =
GETPOST(
'date_endyear');
53 $in_bookkeeping =
GETPOST(
'in_bookkeeping');
54 if ($in_bookkeeping ==
'') {
55 $in_bookkeeping =
'notyet';
60 $hookmanager->initHooks(array(
'purchasesjournal'));
61 $parameters = array();
67 if ($user->socid > 0) {
70 if (empty($user->rights->accounting->mouvements->lire)) {
79 $reshook = $hookmanager->executeHooks(
'doActions', $parameters, $user, $action);
85 $accountingjournalstatic->fetch($id_journal);
86 $journal = $accountingjournalstatic->code;
87 $journal_label = $accountingjournalstatic->label;
89 $date_start =
dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
90 $date_end =
dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
92 if (empty($date_startmonth) || empty($date_endmonth)) {
95 $date_start = $dates[
'date_start'];
96 $date_end = $dates[
'date_end'];
97 $pastmonthyear = $dates[
'pastmonthyear'];
98 $pastmonth = $dates[
'pastmonth'];
101 if (!
GETPOSTISSET(
'date_startmonth') && (empty($date_start) || empty($date_end))) {
106 $sql =
"SELECT f.rowid, f.ref as ref, f.type, f.datef as df, f.libelle,f.ref_supplier, f.date_lim_reglement as dlr, f.close_code,";
107 $sql .=
" fd.rowid as fdid, fd.description, fd.product_type, fd.total_ht, fd.tva as total_tva, fd.total_localtax1, fd.total_localtax2, fd.tva_tx, fd.total_ttc, fd.vat_src_code,";
108 $sql .=
" s.rowid as socid, s.nom as name, s.fournisseur, s.code_client, s.code_fournisseur,";
109 if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
110 $sql .=
" spe.accountancy_code_customer as code_compta,";
111 $sql .=
" spe.accountancy_code_supplier as code_compta_fournisseur,";
113 $sql .=
" s.code_compta as code_compta,";
114 $sql .=
" s.code_compta_fournisseur,";
116 if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
117 $sql .=
" ppe.accountancy_code_buy,";
119 $sql .=
" p.accountancy_code_buy,";
121 $sql .=
" aa.rowid as fk_compte, aa.account_number as compte, aa.label as label_compte";
122 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn_det as fd";
123 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON p.rowid = fd.fk_product";
124 if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
125 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"product_perentity as ppe ON ppe.fk_product = p.rowid AND ppe.entity = " . ((int) $conf->entity);
127 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"accounting_account as aa ON aa.rowid = fd.fk_code_ventilation";
128 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"facture_fourn as f ON f.rowid = fd.fk_facture_fourn";
129 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"societe as s ON s.rowid = f.fk_soc";
130 if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
131 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"societe_perentity as spe ON spe.fk_soc = s.rowid AND spe.entity = " . ((int) $conf->entity);
133 $sql .=
" WHERE f.fk_statut > 0";
134 $sql .=
" AND fd.fk_code_ventilation > 0";
135 $sql .=
" AND f.entity IN (".getEntity(
'facture_fourn', 0).
")";
136 if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
141 if ($date_start && $date_end) {
142 $sql .=
" AND f.datef >= '".$db->idate($date_start).
"' AND f.datef <= '".$db->idate($date_end).
"'";
145 if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
146 $sql .=
" AND f.datef >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING).
"'";
149 if ($in_bookkeeping ==
'already') {
150 $sql .=
" AND f.rowid IN (SELECT fk_doc FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping as ab WHERE ab.doc_type='supplier_invoice')";
152 if ($in_bookkeeping ==
'notyet') {
153 $sql .=
" AND f.rowid NOT IN (SELECT fk_doc FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping as ab WHERE ab.doc_type='supplier_invoice')";
155 $sql .=
" ORDER BY f.datef";
157 dol_syslog(
'accountancy/journal/purchasesjournal.php', LOG_DEBUG);
158 $result = $db->query(
$sql);
165 $tablocaltax1 = array();
166 $tablocaltax2 = array();
167 $tabcompany = array();
170 $num = $db->num_rows($result);
173 $cptfour = ($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER !=
"") ? $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER :
'NotDefined';
174 $cpttva = (!empty($conf->global->ACCOUNTING_VAT_BUY_ACCOUNT)) ? $conf->global->ACCOUNTING_VAT_BUY_ACCOUNT :
'NotDefined';
178 $obj = $db->fetch_object($result);
181 $compta_soc = ($obj->code_compta_fournisseur !=
"") ? $obj->code_compta_fournisseur : $cptfour;
183 $compta_prod = $obj->compte;
184 if (empty($compta_prod)) {
185 if ($obj->product_type == 0) {
186 $compta_prod = (!empty($conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT)) ? $conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT :
'NotDefined';
188 $compta_prod = (!empty($conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT)) ? $conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT :
'NotDefined';
192 $vatdata =
getTaxesFromId($obj->tva_tx.($obj->vat_src_code ?
' ('.$obj->vat_src_code.
')' :
''), $mysoc, $mysoc, 0);
193 $compta_tva = (!empty($vatdata[
'accountancy_code_buy']) ? $vatdata[
'accountancy_code_buy'] : $cpttva);
194 $compta_localtax1 = (!empty($vatdata[
'accountancy_code_buy']) ? $vatdata[
'accountancy_code_buy'] : $cpttva);
195 $compta_localtax2 = (!empty($vatdata[
'accountancy_code_buy']) ? $vatdata[
'accountancy_code_buy'] : $cpttva);
196 $compta_counterpart_tva_npr = (!empty($conf->global->ACCOUNTING_COUNTERPART_VAT_NPR)) ? $conf->global->ACCOUNTING_COUNTERPART_VAT_NPR :
'NotDefined';
199 if (
price2num($obj->tva_tx) || !empty($obj->vat_src_code)) {
200 $def_tva[$obj->rowid][$compta_tva][
vatrate($obj->tva_tx).($obj->vat_src_code ?
' ('.$obj->vat_src_code.
')' :
'')] = (
vatrate($obj->tva_tx).($obj->vat_src_code ?
' ('.$obj->vat_src_code.
')' :
''));
204 $line->fetch($obj->fdid);
206 $tabfac[$obj->rowid][
"date"] = $db->jdate($obj->df);
207 $tabfac[$obj->rowid][
"datereg"] = $db->jdate($obj->dlr);
208 $tabfac[$obj->rowid][
"ref"] = $obj->ref_supplier.
' ('.$obj->ref.
')';
209 $tabfac[$obj->rowid][
"refsologest"] = $obj->ref;
210 $tabfac[$obj->rowid][
"refsuppliersologest"] = $obj->ref_supplier;
211 $tabfac[$obj->rowid][
"type"] = $obj->type;
212 $tabfac[$obj->rowid][
"description"] = $obj->description;
213 $tabfac[$obj->rowid][
"close_code"] = $obj->close_code;
217 if (!isset($tabttc[$obj->rowid][$compta_soc])) {
218 $tabttc[$obj->rowid][$compta_soc] = 0;
220 if (!isset($tabht[$obj->rowid][$compta_prod])) {
221 $tabht[$obj->rowid][$compta_prod] = 0;
223 if (!isset($tabtva[$obj->rowid][$compta_tva])) {
224 $tabtva[$obj->rowid][$compta_tva] = 0;
226 if (!isset($tablocaltax1[$obj->rowid][$compta_localtax1])) {
227 $tablocaltax1[$obj->rowid][$compta_localtax1] = 0;
229 if (!isset($tablocaltax2[$obj->rowid][$compta_localtax2])) {
230 $tablocaltax2[$obj->rowid][$compta_localtax2] = 0;
233 $tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc;
234 $tabht[$obj->rowid][$compta_prod] += $obj->total_ht;
235 $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva;
236 if (!empty($line->tva_npr)) {
237 $tabother[$obj->rowid][$compta_counterpart_tva_npr] += $obj->total_tva;
239 $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1;
240 $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2;
241 $tabcompany[$obj->rowid] = array(
243 'name' => $obj->name,
244 'code_fournisseur' => $obj->code_fournisseur,
245 'code_compta_fournisseur' => $compta_soc
254 $errorforinvoice = array();
257 foreach ($tabfac as $key => $val) {
258 $sql =
"SELECT COUNT(fd.rowid) as nb";
259 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn_det as fd";
260 $sql .=
" WHERE fd.product_type <= 2 AND fd.fk_code_ventilation <= 0";
261 $sql .=
" AND fd.total_ttc <> 0 AND fk_facture_fourn = ".((int) $key);
262 $resql = $db->query(
$sql);
264 $obj = $db->fetch_object($resql);
266 $errorforinvoice[$key] =
'somelinesarenotbound';
277 if ($action ==
'writebookkeeping') {
281 $companystatic =
new Societe($db);
285 $accountingaccountsupplier->fetch(
null, $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER,
true);
287 foreach ($tabfac as $key => $val) {
295 $companystatic->id = $tabcompany[$key][
'id'];
296 $companystatic->name = $tabcompany[$key][
'name'];
297 $companystatic->code_compta_fournisseur = $tabcompany[$key][
'code_compta_fournisseur'];
298 $companystatic->code_fournisseur = $tabcompany[$key][
'code_fournisseur'];
299 $companystatic->fournisseur = 1;
301 $invoicestatic->id = $key;
302 $invoicestatic->ref = (string) $val[
"refsologest"];
303 $invoicestatic->ref_supplier = $val[
"refsuppliersologest"];
304 $invoicestatic->type = $val[
"type"];
305 $invoicestatic->description = html_entity_decode(
dol_trunc($val[
"description"], 32));
306 $invoicestatic->close_code = $val[
"close_code"];
311 $replacedinvoice = 0;
312 if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED) {
313 $replacedinvoice = 1;
314 $alreadydispatched = $invoicestatic->getVentilExportCompta();
315 if ($alreadydispatched) {
316 $replacedinvoice = 2;
321 if ($replacedinvoice == 1) {
327 if ($errorforinvoice[$key] ==
'somelinesarenotbound') {
330 setEventMessages($langs->trans(
'ErrorInvoiceContainsLinesNotYetBounded', $val[
'ref']),
null,
'errors');
334 if (!$errorforline) {
335 foreach ($tabttc[$key] as $k => $mt) {
337 $bookkeeping->doc_date = $val[
"date"];
338 $bookkeeping->date_lim_reglement = $val[
"datereg"];
339 $bookkeeping->doc_ref = $val[
"refsologest"];
340 $bookkeeping->date_creation = $now;
341 $bookkeeping->doc_type =
'supplier_invoice';
342 $bookkeeping->fk_doc = $key;
343 $bookkeeping->fk_docdet = 0;
344 $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
346 $bookkeeping->subledger_account = $tabcompany[$key][
'code_compta_fournisseur'];
347 $bookkeeping->subledger_label = $tabcompany[$key][
'name'];
349 $bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER;
350 $bookkeeping->label_compte = $accountingaccountsupplier->label;
352 $bookkeeping->label_operation =
dol_trunc($companystatic->name, 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"SubledgerAccount");
353 $bookkeeping->montant = $mt;
354 $bookkeeping->sens = ($mt >= 0) ?
'C' :
'D';
355 $bookkeeping->debit = ($mt <= 0) ? -$mt : 0;
356 $bookkeeping->credit = ($mt > 0) ? $mt : 0;
357 $bookkeeping->code_journal = $journal;
358 $bookkeeping->journal_label = $langs->transnoentities($journal_label);
359 $bookkeeping->fk_user_author = $user->id;
360 $bookkeeping->entity = $conf->entity;
362 $totaldebit += $bookkeeping->debit;
363 $totalcredit += $bookkeeping->credit;
365 $result = $bookkeeping->create($user);
367 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
370 $errorforinvoice[$key] =
'alreadyjournalized';
375 $errorforinvoice[$key] =
'other';
380 require_once DOL_DOCUMENT_ROOT .
'/accountancy/class/lettering.class.php';
382 $nb_lettering = $lettering_static->bookkeepingLettering(array($bookkeeping->id));
389 if (!$errorforline) {
390 foreach ($tabht[$key] as $k => $mt) {
391 $resultfetch = $accountingaccount->fetch(
null, $k,
true);
392 $label_account = $accountingaccount->label;
395 if ($resultfetch > 0) {
397 $bookkeeping->doc_date = $val[
"date"];
398 $bookkeeping->date_lim_reglement = $val[
"datereg"];
399 $bookkeeping->doc_ref = $val[
"refsologest"];
400 $bookkeeping->date_creation = $now;
401 $bookkeeping->doc_type =
'supplier_invoice';
402 $bookkeeping->fk_doc = $key;
403 $bookkeeping->fk_docdet = 0;
404 $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
406 if (!empty($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER_USE_AUXILIARY_ON_DEPOSIT)) {
408 $bookkeeping->subledger_account = $tabcompany[$key][
'code_compta'];
409 $bookkeeping->subledger_label = $tabcompany[$key][
'name'];
411 $bookkeeping->subledger_account =
'';
412 $bookkeeping->subledger_label =
'';
415 $bookkeeping->subledger_account =
'';
416 $bookkeeping->subledger_label =
'';
419 $bookkeeping->numero_compte = $k;
420 $bookkeeping->label_compte = $label_account;
422 $bookkeeping->label_operation =
dol_trunc($companystatic->name, 16).
' - '.$invoicestatic->ref_supplier.
' - '.$label_account;
423 $bookkeeping->montant = $mt;
424 $bookkeeping->sens = ($mt < 0) ?
'C' :
'D';
425 $bookkeeping->debit = ($mt > 0) ? $mt : 0;
426 $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
427 $bookkeeping->code_journal = $journal;
428 $bookkeeping->journal_label = $langs->transnoentities($journal_label);
429 $bookkeeping->fk_user_author = $user->id;
430 $bookkeeping->entity = $conf->entity;
432 $totaldebit += $bookkeeping->debit;
433 $totalcredit += $bookkeeping->credit;
435 $result = $bookkeeping->create($user);
437 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
440 $errorforinvoice[$key] =
'alreadyjournalized';
445 $errorforinvoice[$key] =
'other';
455 if (!$errorforline) {
456 $listoftax = array(0, 1, 2);
457 foreach ($listoftax as $numtax) {
458 $arrayofvat = $tabtva;
460 $arrayofvat = $tablocaltax1;
463 $arrayofvat = $tablocaltax2;
466 foreach ($arrayofvat[$key] as $k => $mt) {
468 $accountingaccount->fetch(
null, $k,
true);
469 $label_account = $accountingaccount->label;
472 $bookkeeping->doc_date = $val[
"date"];
473 $bookkeeping->date_lim_reglement = $val[
"datereg"];
474 $bookkeeping->doc_ref = $val[
"refsologest"];
475 $bookkeeping->date_creation = $now;
476 $bookkeeping->doc_type =
'supplier_invoice';
477 $bookkeeping->fk_doc = $key;
478 $bookkeeping->fk_docdet = 0;
479 $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
481 $bookkeeping->subledger_account =
'';
482 $bookkeeping->subledger_label =
'';
484 $bookkeeping->numero_compte = $k;
485 $bookkeeping->label_compte = $label_account;
487 $bookkeeping->label_operation =
dol_trunc($companystatic->name, 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"VAT").
' '.join(
', ', $def_tva[$key][$k]).
' %'.($numtax ?
' - Localtax '.$numtax :
'');
488 $bookkeeping->montant = $mt;
489 $bookkeeping->sens = ($mt < 0) ?
'C' :
'D';
490 $bookkeeping->debit = ($mt > 0) ? $mt : 0;
491 $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
492 $bookkeeping->code_journal = $journal;
493 $bookkeeping->journal_label = $langs->transnoentities($journal_label);
494 $bookkeeping->fk_user_author = $user->id;
495 $bookkeeping->entity = $conf->entity;
497 $totaldebit += $bookkeeping->debit;
498 $totalcredit += $bookkeeping->credit;
500 $result = $bookkeeping->create($user);
502 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
505 $errorforinvoice[$key] =
'alreadyjournalized';
510 $errorforinvoice[$key] =
'other';
521 if (!$errorforline && is_array($tabother[$key])) {
522 foreach ($tabother[$key] as $k => $mt) {
525 $bookkeeping->doc_date = $val[
"date"];
526 $bookkeeping->date_lim_reglement = $val[
"datereg"];
527 $bookkeeping->doc_ref = $val[
"refsologest"];
528 $bookkeeping->date_creation = $now;
529 $bookkeeping->doc_type =
'supplier_invoice';
530 $bookkeeping->fk_doc = $key;
531 $bookkeeping->fk_docdet = 0;
532 $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
534 $bookkeeping->subledger_account =
'';
535 $bookkeeping->subledger_label =
'';
537 $bookkeeping->numero_compte = $k;
539 $bookkeeping->label_operation =
dol_trunc($companystatic->name, 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"VAT").
' NPR';
540 $bookkeeping->montant = $mt;
541 $bookkeeping->sens = ($mt < 0) ?
'C' :
'D';
542 $bookkeeping->debit = ($mt > 0) ? $mt : 0;
543 $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
544 $bookkeeping->code_journal = $journal;
545 $bookkeeping->journal_label = $langs->transnoentities($journal_label);
546 $bookkeeping->fk_user_author = $user->id;
547 $bookkeeping->entity = $conf->entity;
549 $totaldebit += $bookkeeping->debit;
550 $totalcredit += $bookkeeping->credit;
552 $result = $bookkeeping->create($user);
554 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
557 $errorforinvoice[$key] =
'alreadyjournalized';
562 $errorforinvoice[$key] =
'other';
571 if (!$errorforline && (
price2num($totaldebit,
'MT') !=
price2num($totalcredit,
'MT'))) {
574 $errorforinvoice[$key] =
'amountsnotbalanced';
575 setEventMessages(
'Try to insert a non balanced transaction in book for '.$invoicestatic->ref.
'. Canceled. Surely a bug.',
null,
'errors');
578 if (!$errorforline) {
584 setEventMessages($langs->trans(
"ErrorTooManyErrorsProcessStopped"),
null,
'errors');
592 if (empty($error) && count($tabpay) > 0) {
594 } elseif (count($tabpay) == $error) {
597 setEventMessages($langs->trans(
"GeneralLedgerSomeRecordWasNotRecorded"),
null,
'warnings');
603 if (count($tabpay) != $error) {
604 $param =
'id_journal='.$id_journal;
605 $param .=
'&date_startday='.$date_startday;
606 $param .=
'&date_startmonth='.$date_startmonth;
607 $param .=
'&date_startyear='.$date_startyear;
608 $param .=
'&date_endday='.$date_endday;
609 $param .=
'&date_endmonth='.$date_endmonth;
610 $param .=
'&date_endyear='.$date_endyear;
611 $param .=
'&in_bookkeeping='.$in_bookkeeping;
612 header(
"Location: ".$_SERVER[
'PHP_SELF'].($param ?
'?'.$param :
''));
624 if ($action ==
'exportcsv') {
625 $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
627 $filename =
'journal';
628 $type_export =
'journal';
629 include DOL_DOCUMENT_ROOT.
'/accountancy/tpl/export_journal.tpl.php';
634 foreach ($tabfac as $key => $val) {
635 $companystatic->id = $tabcompany[$key][
'id'];
636 $companystatic->name = $tabcompany[$key][
'name'];
637 $companystatic->code_compta_fournisseur = $tabcompany[$key][
'code_compta_fournisseur'];
638 $companystatic->code_fournisseur = $tabcompany[$key][
'code_fournisseur'];
639 $companystatic->fournisseur = 1;
641 $invoicestatic->id = $key;
642 $invoicestatic->ref = $val[
"refsologest"];
643 $invoicestatic->ref_supplier = $val[
"refsuppliersologest"];
644 $invoicestatic->type = $val[
"type"];
645 $invoicestatic->description =
dol_trunc(html_entity_decode($val[
"description"]), 32);
646 $invoicestatic->close_code = $val[
"close_code"];
651 $replacedinvoice = 0;
652 if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED) {
653 $replacedinvoice = 1;
654 $alreadydispatched = $invoicestatic->getVentilExportCompta();
655 if ($alreadydispatched) {
656 $replacedinvoice = 2;
661 if ($replacedinvoice == 1) {
666 foreach ($tabttc[$key] as $k => $mt) {
668 print
'"'.$key.
'"'.$sep;
669 print
'"'.$date.
'"'.$sep;
670 print
'"'.$val[
"refsologest"].
'"'.$sep;
671 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 32)).
'"'.$sep;
672 print
'"'.length_accounta(html_entity_decode($k)).
'"'.$sep;
673 print
'"'.length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER).
'"'.$sep;
674 print
'"'.length_accounta(html_entity_decode($k)).
'"'.$sep;
675 print
'"'.$langs->trans(
"Thirdparty").
'"'.$sep;
676 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 16)).
' - '.$val[
"refsuppliersologest"].
' - '.$langs->trans(
"Thirdparty").
'"'.$sep;
677 print
'"'.($mt < 0 ?
price(-$mt) :
'').
'"'.$sep;
678 print
'"'.($mt >= 0 ?
price($mt) :
'').
'"'.$sep;
679 print
'"'.$journal.
'"';
685 foreach ($tabht[$key] as $k => $mt) {
687 $accountingaccount->fetch(
null, $k,
true);
689 print
'"'.$key.
'"'.$sep;
690 print
'"'.$date.
'"'.$sep;
691 print
'"'.$val[
"refsologest"].
'"'.$sep;
692 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 32)).
'"'.$sep;
693 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
694 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
696 print
'"'.utf8_decode(
dol_trunc($accountingaccount->label, 32)).
'"'.$sep;
697 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 16)).
' - '.$val[
"refsuppliersologest"].
' - '.
dol_trunc($accountingaccount->label, 32).
'"'.$sep;
698 print
'"'.($mt >= 0 ?
price($mt) :
'').
'"'.$sep;
699 print
'"'.($mt < 0 ?
price(-$mt) :
'').
'"'.$sep;
700 print
'"'.$journal.
'"';
706 $listoftax = array(0, 1, 2);
707 foreach ($listoftax as $numtax) {
708 $arrayofvat = $tabtva;
710 $arrayofvat = $tablocaltax1;
713 $arrayofvat = $tablocaltax2;
716 foreach ($arrayofvat[$key] as $k => $mt) {
718 print
'"'.$key.
'"'.$sep;
719 print
'"'.$date.
'"'.$sep;
720 print
'"'.$val[
"refsologest"].
'"'.$sep;
721 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 32)).
'"'.$sep;
722 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
723 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
725 print
'"'.$langs->trans(
"VAT").
' - '.join(
', ', $def_tva[$key][$k]).
' %"'.$sep;
726 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 16)).
' - '.$val[
"refsuppliersologest"].
' - '.$langs->trans(
"VAT").join(
', ', $def_tva[$key][$k]).
' %'.($numtax ?
' - Localtax '.$numtax :
'').
'"'.$sep;
727 print
'"'.($mt >= 0 ?
price($mt) :
'').
'"'.$sep;
728 print
'"'.($mt < 0 ?
price(-$mt) :
'').
'"'.$sep;
729 print
'"'.$journal.
'"';
735 if (is_array($tabother[$key])) {
736 foreach ($tabother[$key] as $k => $mt) {
738 print
'"'.$key.
'"'.$sep;
739 print
'"'.$date.
'"'.$sep;
740 print
'"'.$val[
"refsologest"].
'"'.$sep;
741 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 32)).
'"'.$sep;
742 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
743 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
744 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
745 print
'"'.$langs->trans(
"Thirdparty").
'"'.$sep;
746 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 16)).
' - '.$val[
"refsuppliersologest"].
' - '.$langs->trans(
"VAT").
' NPR"'.$sep;
747 print
'"'.($mt < 0 ?
price(-$mt) :
'').
'"'.$sep;
748 print
'"'.($mt >= 0 ?
price($mt) :
'').
'"'.$sep;
749 print
'"'.$journal.
'"';
758 if (empty($action) || $action ==
'view') {
759 $title = $langs->trans(
"GenerationOfAccountingEntries").
' - '.$accountingjournalstatic->getNomUrl(0, 2, 1,
'', 1);
768 $description = $langs->trans(
"DescJournalOnlyBindedVisible").
'<br>';
769 if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
770 $description .= $langs->trans(
"DepositsAreNotIncluded");
772 $description .= $langs->trans(
"DepositsAreIncluded");
775 $listofchoices = array(
'notyet'=>$langs->trans(
"NotYetInGeneralLedger"),
'already'=>$langs->trans(
"AlreadyInGeneralLedger"));
776 $period =
$form->selectDate($date_start ? $date_start : -1,
'date_start', 0, 0, 0,
'', 1, 0).
' - '.
$form->selectDate($date_end ? $date_end : -1,
'date_end', 0, 0, 0,
'', 1, 0);
777 $period .=
' - '.$langs->trans(
"JournalizationInLedgerStatus").
' '.
$form->selectarray(
'in_bookkeeping', $listofchoices, $in_bookkeeping, 1);
779 $varlink =
'id_journal='.$id_journal;
781 journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array(
'action' =>
''),
'', $varlink);
784 if (($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER ==
"") || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER ==
'-1') {
785 print
'<br><div class="warning">'.img_warning().
' '.$langs->trans(
"SomeMandatoryStepsOfSetupWereNotDone");
786 $desc =
' : '.$langs->trans(
"AccountancyAreaDescMisc", 4,
'{link}');
787 $desc = str_replace(
'{link}',
'<strong>'.$langs->transnoentitiesnoconv(
"MenuAccountancy").
'-'.$langs->transnoentitiesnoconv(
"Setup").
"-".$langs->transnoentitiesnoconv(
"MenuDefaultAccounts").
'</strong>', $desc);
791 print
'<div class="tabsAction tabsActionNoBottom centerimp">';
792 if (!empty($conf->global->ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL) && $in_bookkeeping ==
'notyet') {
793 print
'<input type="button" class="butAction" name="exportcsv" value="'.$langs->trans(
"ExportDraftJournal").
'" onclick="launch_export();" />';
795 if (($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER ==
"") || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER ==
'-1') {
796 print
'<input type="button" class="butActionRefused classfortooltip" title="'.dol_escape_htmltag($langs->trans(
"SomeMandatoryStepsOfSetupWereNotDone")).
'" value="'.$langs->trans(
"WriteBookKeeping").
'" />';
798 if ($in_bookkeeping ==
'notyet') {
799 print
'<input type="button" class="butAction" name="writebookkeeping" value="'.$langs->trans(
"WriteBookKeeping").
'" onclick="writebookkeeping();" />';
801 print
'<a href="#" class="butActionRefused classfortooltip" name="writebookkeeping">'.$langs->trans(
"WriteBookKeeping").
'</a>';
808 <script type="text/javascript">
809 function launch_export() {
810 $("div.fiche form input[name=\"action\"]").val("exportcsv");
811 $("div.fiche form input[type=\"submit\"]").click();
812 $("div.fiche form input[name=\"action\"]").val("");
814 function writebookkeeping() {
815 console.log("click on writebookkeeping");
816 $("div.fiche form input[name=\"action\"]").val("writebookkeeping");
817 $("div.fiche form input[type=\"submit\"]").click();
818 $("div.fiche form input[name=\"action\"]").val("");
828 print
'<div class="div-table-responsive">';
829 print
"<table class=\"noborder\" width=\"100%\">";
830 print
"<tr class=\"liste_titre\">";
831 print
"<td>".$langs->trans(
"Date").
"</td>";
832 print
"<td>".$langs->trans(
"Piece").
' ('.$langs->trans(
"InvoiceRef").
")</td>";
833 print
"<td>".$langs->trans(
"AccountAccounting").
"</td>";
834 print
"<td>".$langs->trans(
"SubledgerAccount").
"</td>";
835 print
"<td>".$langs->trans(
"LabelOperation").
"</td>";
836 print
'<td class="center">'.$langs->trans(
"AccountingDebit").
"</td>";
837 print
'<td class="center">'.$langs->trans(
"AccountingCredit").
"</td>";
845 foreach ($tabfac as $key => $val) {
846 $companystatic->id = $tabcompany[$key][
'id'];
847 $companystatic->name = $tabcompany[$key][
'name'];
848 $companystatic->code_compta_fournisseur = $tabcompany[$key][
'code_compta_fournisseur'];
849 $companystatic->code_fournisseur = $tabcompany[$key][
'code_fournisseur'];
850 $companystatic->fournisseur = 1;
852 $invoicestatic->id = $key;
853 $invoicestatic->ref = $val[
"refsologest"];
854 $invoicestatic->ref_supplier = $val[
"refsuppliersologest"];
855 $invoicestatic->type = $val[
"type"];
856 $invoicestatic->description =
dol_trunc(html_entity_decode($val[
"description"]), 32);
857 $invoicestatic->close_code = $val[
"close_code"];
862 $replacedinvoice = 0;
863 if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED) {
864 $replacedinvoice = 1;
865 $alreadydispatched = $invoicestatic->getVentilExportCompta();
866 if ($alreadydispatched) {
867 $replacedinvoice = 2;
872 if ($replacedinvoice == 1) {
873 print
'<tr class="oddeven">';
874 print
"<!-- Replaced invoice -->";
875 print
"<td>".$date.
"</td>";
876 print
"<td><strike>".$invoicestatic->getNomUrl(1).
"</strike></td>";
879 print $langs->trans(
"Replaced");
886 print
'<td class="right"></td>';
887 print
'<td class="right"></td>';
892 if ($errorforinvoice[$key] ==
'somelinesarenotbound') {
893 print
'<tr class="oddeven">';
894 print
"<!-- Some lines are not bound -->";
895 print
"<td>".$date.
"</td>";
896 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
899 print
'<span class="error">'.$langs->trans(
'ErrorInvoiceContainsLinesNotYetBoundedShort', $val[
'ref']).
'</span>';
906 print
'<td class="right"></td>';
907 print
'<td class="right"></td>';
912 foreach ($tabttc[$key] as $k => $mt) {
913 print
'<tr class="oddeven">';
914 print
"<!-- Thirdparty -->";
915 print
"<td>".$date.
"</td>";
916 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
919 $accountoshow =
length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER);
920 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
921 print
'<span class="error">'.$langs->trans(
"MainAccountForSuppliersNotDefined").
'</span>';
929 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
930 print
'<span class="error">'.$langs->trans(
"ThirdpartyAccountNotDefined").
'</span>';
935 print
"<td>".$companystatic->getNomUrl(0,
'supplier', 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"SubledgerAccount").
"</td>";
936 print
'<td class="right nowraponall amount">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
937 print
'<td class="right nowraponall amount">'.($mt >= 0 ?
price($mt) :
'').
"</td>";
942 foreach ($tabht[$key] as $k => $mt) {
944 $accountingaccount->fetch(
null, $k,
true);
946 print
'<tr class="oddeven">';
947 print
"<!-- Product -->";
948 print
"<td>".$date.
"</td>";
949 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
953 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
954 print
'<span class="error">'.$langs->trans(
"ProductAccountNotDefined").
'</span>';
961 if (!empty($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER_USE_AUXILIARY_ON_DEPOSIT)) {
965 } elseif (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
966 print
'<span class="error">' . $langs->trans(
"ThirdpartyAccountNotDefined") .
'</span>';
969 $companystatic->id = $tabcompany[$key][
'id'];
970 $companystatic->name = $tabcompany[$key][
'name'];
971 print
"<td>".$companystatic->getNomUrl(0,
'supplier', 16).
' - '.$invoicestatic->ref_supplier.
' - '.$accountingaccount->label.
"</td>";
972 print
'<td class="right nowraponall amount">'.($mt >= 0 ?
price($mt) :
'').
"</td>";
973 print
'<td class="right nowraponall amount">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
978 $listoftax = array(0, 1, 2);
979 foreach ($listoftax as $numtax) {
980 $arrayofvat = $tabtva;
982 $arrayofvat = $tablocaltax1;
985 $arrayofvat = $tablocaltax2;
988 foreach ($arrayofvat[$key] as $k => $mt) {
990 print
'<tr class="oddeven">';
991 print
"<!-- VAT -->";
992 print
"<td>".$date.
"</td>";
993 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
997 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
998 print
'<span class="error">'.$langs->trans(
"VATAccountNotDefined").
' ('.$langs->trans(
"Purchase").
')</span>';
1000 print $accountoshow;
1007 print $companystatic->getNomUrl(0,
'supplier', 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"VAT").
' '.join(
', ', $def_tva[$key][$k]).
' %'.($numtax ?
' - Localtax '.$numtax :
'');
1009 print
'<td class="right nowraponall amount">'.($mt >= 0 ?
price($mt) :
'').
"</td>";
1010 print
'<td class="right nowraponall amount">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
1017 if (is_array($tabother[$key])) {
1018 foreach ($tabother[$key] as $k => $mt) {
1020 print
'<tr class="oddeven">';
1021 print
'<!-- VAT counterpart NPR -->';
1022 print
"<td>".$date.
"</td>";
1023 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
1027 if ($accountoshow ==
'' || $accountoshow ==
'NotDefined') {
1028 print
'<span class="error">'.$langs->trans(
"VATAccountNotDefined").
' ('.$langs->trans(
"NPR counterpart").
'). Set ACCOUNTING_COUNTERPART_VAT_NPR to the subvention account</span>';
1030 print $accountoshow;
1036 print
"<td>".$companystatic->getNomUrl(0,
'supplier', 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"VAT").
" NPR (counterpart)</td>";
1037 print
'<td class="right nowraponall amount">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
1038 print
'<td class="right nowraponall amount">'.($mt >= 0 ?
price($mt) :
'').
"</td>";