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, fd.info_bits,";
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.
')' :
''));
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 $tva_npr = (($obj->info_bits & 1 == 1) ? 1 : 0);
238 $tabother[$obj->rowid][$compta_counterpart_tva_npr] += $obj->total_tva;
240 $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1;
241 $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2;
242 $tabcompany[$obj->rowid] = array(
244 'name' => $obj->name,
245 'code_fournisseur' => $obj->code_fournisseur,
246 'code_compta_fournisseur' => $compta_soc
255 $errorforinvoice = array();
258 foreach ($tabfac as $key => $val) {
259 $sql =
"SELECT COUNT(fd.rowid) as nb";
260 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn_det as fd";
261 $sql .=
" WHERE fd.product_type <= 2 AND fd.fk_code_ventilation <= 0";
262 $sql .=
" AND fd.total_ttc <> 0 AND fk_facture_fourn = ".((int) $key);
263 $resql = $db->query($sql);
265 $obj = $db->fetch_object(
$resql);
267 $errorforinvoice[$key] =
'somelinesarenotbound';
278 if ($action ==
'writebookkeeping') {
282 $companystatic =
new Societe($db);
286 $accountingaccountsupplier->fetch(
null, $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER,
true);
288 foreach ($tabfac as $key => $val) {
296 $companystatic->id = $tabcompany[$key][
'id'];
297 $companystatic->name = $tabcompany[$key][
'name'];
298 $companystatic->code_compta_fournisseur = $tabcompany[$key][
'code_compta_fournisseur'];
299 $companystatic->code_fournisseur = $tabcompany[$key][
'code_fournisseur'];
300 $companystatic->fournisseur = 1;
302 $invoicestatic->id = $key;
303 $invoicestatic->ref = (string) $val[
"refsologest"];
304 $invoicestatic->ref_supplier = $val[
"refsuppliersologest"];
305 $invoicestatic->type = $val[
"type"];
306 $invoicestatic->description = html_entity_decode(
dol_trunc($val[
"description"], 32));
307 $invoicestatic->close_code = $val[
"close_code"];
312 $replacedinvoice = 0;
313 if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED) {
314 $replacedinvoice = 1;
315 $alreadydispatched = $invoicestatic->getVentilExportCompta();
316 if ($alreadydispatched) {
317 $replacedinvoice = 2;
322 if ($replacedinvoice == 1) {
328 if ($errorforinvoice[$key] ==
'somelinesarenotbound') {
331 setEventMessages($langs->trans(
'ErrorInvoiceContainsLinesNotYetBounded', $val[
'ref']),
null,
'errors');
335 if (!$errorforline) {
336 foreach ($tabttc[$key] as $k => $mt) {
338 $bookkeeping->doc_date = $val[
"date"];
339 $bookkeeping->date_lim_reglement = $val[
"datereg"];
340 $bookkeeping->doc_ref = $val[
"refsologest"];
341 $bookkeeping->date_creation = $now;
342 $bookkeeping->doc_type =
'supplier_invoice';
343 $bookkeeping->fk_doc = $key;
344 $bookkeeping->fk_docdet = 0;
345 $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
347 $bookkeeping->subledger_account = $tabcompany[$key][
'code_compta_fournisseur'];
348 $bookkeeping->subledger_label = $tabcompany[$key][
'name'];
350 $bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER;
351 $bookkeeping->label_compte = $accountingaccountsupplier->label;
353 $bookkeeping->label_operation =
dol_trunc($companystatic->name, 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"SubledgerAccount");
354 $bookkeeping->montant = $mt;
355 $bookkeeping->sens = ($mt >= 0) ?
'C' :
'D';
356 $bookkeeping->debit = ($mt <= 0) ? -$mt : 0;
357 $bookkeeping->credit = ($mt > 0) ? $mt : 0;
358 $bookkeeping->code_journal = $journal;
359 $bookkeeping->journal_label = $langs->transnoentities($journal_label);
360 $bookkeeping->fk_user_author = $user->id;
361 $bookkeeping->entity = $conf->entity;
363 $totaldebit += $bookkeeping->debit;
364 $totalcredit += $bookkeeping->credit;
366 $result = $bookkeeping->create($user);
368 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
371 $errorforinvoice[$key] =
'alreadyjournalized';
376 $errorforinvoice[$key] =
'other';
381 require_once DOL_DOCUMENT_ROOT .
'/accountancy/class/lettering.class.php';
384 $nb_lettering = $lettering_static->bookkeepingLettering(array($bookkeeping->id));
391 if (!$errorforline) {
392 foreach ($tabht[$key] as $k => $mt) {
393 $resultfetch = $accountingaccount->fetch(
null, $k,
true);
394 $label_account = $accountingaccount->label;
397 if ($resultfetch > 0) {
399 $bookkeeping->doc_date = $val[
"date"];
400 $bookkeeping->date_lim_reglement = $val[
"datereg"];
401 $bookkeeping->doc_ref = $val[
"refsologest"];
402 $bookkeeping->date_creation = $now;
403 $bookkeeping->doc_type =
'supplier_invoice';
404 $bookkeeping->fk_doc = $key;
405 $bookkeeping->fk_docdet = 0;
406 $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
408 if (!empty($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER_USE_AUXILIARY_ON_DEPOSIT)) {
410 $bookkeeping->subledger_account = $tabcompany[$key][
'code_compta'];
411 $bookkeeping->subledger_label = $tabcompany[$key][
'name'];
413 $bookkeeping->subledger_account =
'';
414 $bookkeeping->subledger_label =
'';
417 $bookkeeping->subledger_account =
'';
418 $bookkeeping->subledger_label =
'';
421 $bookkeeping->numero_compte = $k;
422 $bookkeeping->label_compte = $label_account;
424 $bookkeeping->label_operation =
dol_trunc($companystatic->name, 16).
' - '.$invoicestatic->ref_supplier.
' - '.$label_account;
425 $bookkeeping->montant = $mt;
426 $bookkeeping->sens = ($mt < 0) ?
'C' :
'D';
427 $bookkeeping->debit = ($mt > 0) ? $mt : 0;
428 $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
429 $bookkeeping->code_journal = $journal;
430 $bookkeeping->journal_label = $langs->transnoentities($journal_label);
431 $bookkeeping->fk_user_author = $user->id;
432 $bookkeeping->entity = $conf->entity;
434 $totaldebit += $bookkeeping->debit;
435 $totalcredit += $bookkeeping->credit;
437 $result = $bookkeeping->create($user);
439 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
442 $errorforinvoice[$key] =
'alreadyjournalized';
447 $errorforinvoice[$key] =
'other';
457 if (!$errorforline) {
458 $listoftax = array(0, 1, 2);
459 foreach ($listoftax as $numtax) {
460 $arrayofvat = $tabtva;
462 $arrayofvat = $tablocaltax1;
465 $arrayofvat = $tablocaltax2;
468 foreach ($arrayofvat[$key] as $k => $mt) {
470 $accountingaccount->fetch(
null, $k,
true);
471 $label_account = $accountingaccount->label;
474 $bookkeeping->doc_date = $val[
"date"];
475 $bookkeeping->date_lim_reglement = $val[
"datereg"];
476 $bookkeeping->doc_ref = $val[
"refsologest"];
477 $bookkeeping->date_creation = $now;
478 $bookkeeping->doc_type =
'supplier_invoice';
479 $bookkeeping->fk_doc = $key;
480 $bookkeeping->fk_docdet = 0;
481 $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
483 $bookkeeping->subledger_account =
'';
484 $bookkeeping->subledger_label =
'';
486 $bookkeeping->numero_compte = $k;
487 $bookkeeping->label_compte = $label_account;
489 $bookkeeping->label_operation =
dol_trunc($companystatic->name, 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"VAT").
' '.join(
', ', $def_tva[$key][$k]).
' %'.($numtax ?
' - Localtax '.$numtax :
'');
490 $bookkeeping->montant = $mt;
491 $bookkeeping->sens = ($mt < 0) ?
'C' :
'D';
492 $bookkeeping->debit = ($mt > 0) ? $mt : 0;
493 $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
494 $bookkeeping->code_journal = $journal;
495 $bookkeeping->journal_label = $langs->transnoentities($journal_label);
496 $bookkeeping->fk_user_author = $user->id;
497 $bookkeeping->entity = $conf->entity;
499 $totaldebit += $bookkeeping->debit;
500 $totalcredit += $bookkeeping->credit;
502 $result = $bookkeeping->create($user);
504 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
507 $errorforinvoice[$key] =
'alreadyjournalized';
512 $errorforinvoice[$key] =
'other';
523 if (!$errorforline && is_array($tabother[$key])) {
524 foreach ($tabother[$key] as $k => $mt) {
527 $bookkeeping->doc_date = $val[
"date"];
528 $bookkeeping->date_lim_reglement = $val[
"datereg"];
529 $bookkeeping->doc_ref = $val[
"refsologest"];
530 $bookkeeping->date_creation = $now;
531 $bookkeeping->doc_type =
'supplier_invoice';
532 $bookkeeping->fk_doc = $key;
533 $bookkeeping->fk_docdet = 0;
534 $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
536 $bookkeeping->subledger_account =
'';
537 $bookkeeping->subledger_label =
'';
539 $bookkeeping->numero_compte = $k;
541 $bookkeeping->label_operation =
dol_trunc($companystatic->name, 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"VAT").
' NPR';
542 $bookkeeping->montant = $mt;
543 $bookkeeping->sens = ($mt < 0) ?
'C' :
'D';
544 $bookkeeping->debit = ($mt > 0) ? $mt : 0;
545 $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
546 $bookkeeping->code_journal = $journal;
547 $bookkeeping->journal_label = $langs->transnoentities($journal_label);
548 $bookkeeping->fk_user_author = $user->id;
549 $bookkeeping->entity = $conf->entity;
551 $totaldebit += $bookkeeping->debit;
552 $totalcredit += $bookkeeping->credit;
554 $result = $bookkeeping->create($user);
556 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
559 $errorforinvoice[$key] =
'alreadyjournalized';
564 $errorforinvoice[$key] =
'other';
573 if (!$errorforline && (
price2num($totaldebit,
'MT') !=
price2num($totalcredit,
'MT'))) {
576 $errorforinvoice[$key] =
'amountsnotbalanced';
577 setEventMessages(
'Try to insert a non balanced transaction in book for '.$invoicestatic->ref.
'. Canceled. Surely a bug.',
null,
'errors');
580 if (!$errorforline) {
586 setEventMessages($langs->trans(
"ErrorTooManyErrorsProcessStopped"),
null,
'errors');
594 if (empty($error) && count($tabpay) > 0) {
596 } elseif (count($tabpay) == $error) {
599 setEventMessages($langs->trans(
"GeneralLedgerSomeRecordWasNotRecorded"),
null,
'warnings');
605 if (count($tabpay) != $error) {
606 $param =
'id_journal='.$id_journal;
607 $param .=
'&date_startday='.$date_startday;
608 $param .=
'&date_startmonth='.$date_startmonth;
609 $param .=
'&date_startyear='.$date_startyear;
610 $param .=
'&date_endday='.$date_endday;
611 $param .=
'&date_endmonth='.$date_endmonth;
612 $param .=
'&date_endyear='.$date_endyear;
613 $param .=
'&in_bookkeeping='.$in_bookkeeping;
614 header(
"Location: ".$_SERVER[
'PHP_SELF'].($param ?
'?'.$param :
''));
626 if ($action ==
'exportcsv') {
627 $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
629 $filename =
'journal';
630 $type_export =
'journal';
631 include DOL_DOCUMENT_ROOT.
'/accountancy/tpl/export_journal.tpl.php';
636 foreach ($tabfac as $key => $val) {
637 $companystatic->id = $tabcompany[$key][
'id'];
638 $companystatic->name = $tabcompany[$key][
'name'];
639 $companystatic->code_compta_fournisseur = $tabcompany[$key][
'code_compta_fournisseur'];
640 $companystatic->code_fournisseur = $tabcompany[$key][
'code_fournisseur'];
641 $companystatic->fournisseur = 1;
643 $invoicestatic->id = $key;
644 $invoicestatic->ref = $val[
"refsologest"];
645 $invoicestatic->ref_supplier = $val[
"refsuppliersologest"];
646 $invoicestatic->type = $val[
"type"];
647 $invoicestatic->description =
dol_trunc(html_entity_decode($val[
"description"]), 32);
648 $invoicestatic->close_code = $val[
"close_code"];
653 $replacedinvoice = 0;
654 if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED) {
655 $replacedinvoice = 1;
656 $alreadydispatched = $invoicestatic->getVentilExportCompta();
657 if ($alreadydispatched) {
658 $replacedinvoice = 2;
663 if ($replacedinvoice == 1) {
668 foreach ($tabttc[$key] as $k => $mt) {
670 print
'"'.$key.
'"'.$sep;
671 print
'"'.$date.
'"'.$sep;
672 print
'"'.$val[
"refsologest"].
'"'.$sep;
673 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 32)).
'"'.$sep;
674 print
'"'.length_accounta(html_entity_decode($k)).
'"'.$sep;
675 print
'"'.length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER).
'"'.$sep;
676 print
'"'.length_accounta(html_entity_decode($k)).
'"'.$sep;
677 print
'"'.$langs->trans(
"Thirdparty").
'"'.$sep;
678 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 16)).
' - '.$val[
"refsuppliersologest"].
' - '.$langs->trans(
"Thirdparty").
'"'.$sep;
679 print
'"'.($mt < 0 ?
price(-$mt) :
'').
'"'.$sep;
680 print
'"'.($mt >= 0 ?
price($mt) :
'').
'"'.$sep;
681 print
'"'.$journal.
'"';
687 foreach ($tabht[$key] as $k => $mt) {
689 $accountingaccount->fetch(
null, $k,
true);
691 print
'"'.$key.
'"'.$sep;
692 print
'"'.$date.
'"'.$sep;
693 print
'"'.$val[
"refsologest"].
'"'.$sep;
694 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 32)).
'"'.$sep;
695 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
696 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
698 print
'"'.utf8_decode(
dol_trunc($accountingaccount->label, 32)).
'"'.$sep;
699 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 16)).
' - '.$val[
"refsuppliersologest"].
' - '.
dol_trunc($accountingaccount->label, 32).
'"'.$sep;
700 print
'"'.($mt >= 0 ?
price($mt) :
'').
'"'.$sep;
701 print
'"'.($mt < 0 ?
price(-$mt) :
'').
'"'.$sep;
702 print
'"'.$journal.
'"';
708 $listoftax = array(0, 1, 2);
709 foreach ($listoftax as $numtax) {
710 $arrayofvat = $tabtva;
712 $arrayofvat = $tablocaltax1;
715 $arrayofvat = $tablocaltax2;
718 foreach ($arrayofvat[$key] as $k => $mt) {
720 print
'"'.$key.
'"'.$sep;
721 print
'"'.$date.
'"'.$sep;
722 print
'"'.$val[
"refsologest"].
'"'.$sep;
723 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 32)).
'"'.$sep;
724 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
725 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
727 print
'"'.$langs->trans(
"VAT").
' - '.join(
', ', $def_tva[$key][$k]).
' %"'.$sep;
728 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 16)).
' - '.$val[
"refsuppliersologest"].
' - '.$langs->trans(
"VAT").join(
', ', $def_tva[$key][$k]).
' %'.($numtax ?
' - Localtax '.$numtax :
'').
'"'.$sep;
729 print
'"'.($mt >= 0 ?
price($mt) :
'').
'"'.$sep;
730 print
'"'.($mt < 0 ?
price(-$mt) :
'').
'"'.$sep;
731 print
'"'.$journal.
'"';
737 if (is_array($tabother[$key])) {
738 foreach ($tabother[$key] as $k => $mt) {
740 print
'"'.$key.
'"'.$sep;
741 print
'"'.$date.
'"'.$sep;
742 print
'"'.$val[
"refsologest"].
'"'.$sep;
743 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 32)).
'"'.$sep;
744 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
745 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
746 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
747 print
'"'.$langs->trans(
"Thirdparty").
'"'.$sep;
748 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 16)).
' - '.$val[
"refsuppliersologest"].
' - '.$langs->trans(
"VAT").
' NPR"'.$sep;
749 print
'"'.($mt < 0 ?
price(-$mt) :
'').
'"'.$sep;
750 print
'"'.($mt >= 0 ?
price($mt) :
'').
'"'.$sep;
751 print
'"'.$journal.
'"';
760 if (empty($action) || $action ==
'view') {
761 $title = $langs->trans(
"GenerationOfAccountingEntries").
' - '.$accountingjournalstatic->getNomUrl(0, 2, 1,
'', 1);
770 $description = $langs->trans(
"DescJournalOnlyBindedVisible").
'<br>';
771 if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
772 $description .= $langs->trans(
"DepositsAreNotIncluded");
774 $description .= $langs->trans(
"DepositsAreIncluded");
777 $listofchoices = array(
'notyet'=>$langs->trans(
"NotYetInGeneralLedger"),
'already'=>$langs->trans(
"AlreadyInGeneralLedger"));
778 $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);
779 $period .=
' - '.$langs->trans(
"JournalizationInLedgerStatus").
' '.
$form->selectarray(
'in_bookkeeping', $listofchoices, $in_bookkeeping, 1);
781 $varlink =
'id_journal='.$id_journal;
783 journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array(
'action' =>
''),
'', $varlink);
786 if (($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER ==
"") || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER ==
'-1') {
787 print
'<br><div class="warning">'.img_warning().
' '.$langs->trans(
"SomeMandatoryStepsOfSetupWereNotDone");
788 $desc =
' : '.$langs->trans(
"AccountancyAreaDescMisc", 4,
'{link}');
789 $desc = str_replace(
'{link}',
'<strong>'.$langs->transnoentitiesnoconv(
"MenuAccountancy").
'-'.$langs->transnoentitiesnoconv(
"Setup").
"-".$langs->transnoentitiesnoconv(
"MenuDefaultAccounts").
'</strong>', $desc);
793 print
'<div class="tabsAction tabsActionNoBottom centerimp">';
794 if (!empty($conf->global->ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL) && $in_bookkeeping ==
'notyet') {
795 print
'<input type="button" class="butAction" name="exportcsv" value="'.$langs->trans(
"ExportDraftJournal").
'" onclick="launch_export();" />';
797 if (($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER ==
"") || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER ==
'-1') {
798 print
'<input type="button" class="butActionRefused classfortooltip" title="'.dol_escape_htmltag($langs->trans(
"SomeMandatoryStepsOfSetupWereNotDone")).
'" value="'.$langs->trans(
"WriteBookKeeping").
'" />';
800 if ($in_bookkeeping ==
'notyet') {
801 print
'<input type="button" class="butAction" name="writebookkeeping" value="'.$langs->trans(
"WriteBookKeeping").
'" onclick="writebookkeeping();" />';
803 print
'<a href="#" class="butActionRefused classfortooltip" name="writebookkeeping">'.$langs->trans(
"WriteBookKeeping").
'</a>';
810 <script type="text/javascript">
811 function launch_export() {
812 $("div.fiche form input[name=\"action\"]").val("exportcsv");
813 $("div.fiche form input[type=\"submit\"]").click();
814 $("div.fiche form input[name=\"action\"]").val("");
816 function writebookkeeping() {
817 console.log("click on writebookkeeping");
818 $("div.fiche form input[name=\"action\"]").val("writebookkeeping");
819 $("div.fiche form input[type=\"submit\"]").click();
820 $("div.fiche form input[name=\"action\"]").val("");
829 print
'<div class="div-table-responsive">';
830 print
"<table class=\"noborder\" width=\"100%\">";
831 print
"<tr class=\"liste_titre\">";
832 print
"<td>".$langs->trans(
"Date").
"</td>";
833 print
"<td>".$langs->trans(
"Piece").
' ('.$langs->trans(
"InvoiceRef").
")</td>";
834 print
"<td>".$langs->trans(
"AccountAccounting").
"</td>";
835 print
"<td>".$langs->trans(
"SubledgerAccount").
"</td>";
836 print
"<td>".$langs->trans(
"LabelOperation").
"</td>";
837 print
'<td class="center">'.$langs->trans(
"AccountingDebit").
"</td>";
838 print
'<td class="center">'.$langs->trans(
"AccountingCredit").
"</td>";
846 foreach ($tabfac as $key => $val) {
847 $companystatic->id = $tabcompany[$key][
'id'];
848 $companystatic->name = $tabcompany[$key][
'name'];
849 $companystatic->code_compta_fournisseur = $tabcompany[$key][
'code_compta_fournisseur'];
850 $companystatic->code_fournisseur = $tabcompany[$key][
'code_fournisseur'];
851 $companystatic->fournisseur = 1;
853 $invoicestatic->id = $key;
854 $invoicestatic->ref = $val[
"refsologest"];
855 $invoicestatic->ref_supplier = $val[
"refsuppliersologest"];
856 $invoicestatic->type = $val[
"type"];
857 $invoicestatic->description =
dol_trunc(html_entity_decode($val[
"description"]), 32);
858 $invoicestatic->close_code = $val[
"close_code"];
863 $replacedinvoice = 0;
864 if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED) {
865 $replacedinvoice = 1;
866 $alreadydispatched = $invoicestatic->getVentilExportCompta();
867 if ($alreadydispatched) {
868 $replacedinvoice = 2;
873 if ($replacedinvoice == 1) {
874 print
'<tr class="oddeven">';
875 print
"<!-- Replaced invoice -->";
876 print
"<td>".$date.
"</td>";
877 print
"<td><strike>".$invoicestatic->getNomUrl(1).
"</strike></td>";
880 print $langs->trans(
"Replaced");
887 print
'<td class="right"></td>';
888 print
'<td class="right"></td>';
894 if ($errorforinvoice[$key] ==
'somelinesarenotbound') {
895 print
'<tr class="oddeven">';
896 print
"<!-- Some lines are not bound -->";
897 print
"<td>".$date.
"</td>";
898 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
901 print
'<span class="error">'.$langs->trans(
'ErrorInvoiceContainsLinesNotYetBoundedShort', $val[
'ref']).
'</span>';
908 print
'<td class="right"></td>';
909 print
'<td class="right"></td>';
916 foreach ($tabttc[$key] as $k => $mt) {
917 print
'<tr class="oddeven">';
918 print
"<!-- Thirdparty -->";
919 print
"<td>".$date.
"</td>";
920 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
923 $accountoshow =
length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER);
924 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
925 print
'<span class="error">'.$langs->trans(
"MainAccountForSuppliersNotDefined").
'</span>';
933 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
934 print
'<span class="error">'.$langs->trans(
"ThirdpartyAccountNotDefined").
'</span>';
939 print
"<td>".$companystatic->getNomUrl(0,
'supplier', 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"SubledgerAccount").
"</td>";
940 print
'<td class="right nowraponall amount">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
941 print
'<td class="right nowraponall amount">'.($mt >= 0 ?
price($mt) :
'').
"</td>";
948 foreach ($tabht[$key] as $k => $mt) {
950 $accountingaccount->fetch(
null, $k,
true);
952 print
'<tr class="oddeven">';
953 print
"<!-- Product -->";
954 print
"<td>".$date.
"</td>";
955 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
959 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
960 print
'<span class="error">'.$langs->trans(
"ProductAccountNotDefined").
'</span>';
967 if (!empty($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER_USE_AUXILIARY_ON_DEPOSIT)) {
971 } elseif (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
972 print
'<span class="error">' . $langs->trans(
"ThirdpartyAccountNotDefined") .
'</span>';
975 $companystatic->id = $tabcompany[$key][
'id'];
976 $companystatic->name = $tabcompany[$key][
'name'];
977 print
"<td>".$companystatic->getNomUrl(0,
'supplier', 16).
' - '.$invoicestatic->ref_supplier.
' - '.$accountingaccount->label.
"</td>";
978 print
'<td class="right nowraponall amount">'.($mt >= 0 ?
price($mt) :
'').
"</td>";
979 print
'<td class="right nowraponall amount">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
986 $listoftax = array(0, 1, 2);
987 foreach ($listoftax as $numtax) {
988 $arrayofvat = $tabtva;
990 $arrayofvat = $tablocaltax1;
993 $arrayofvat = $tablocaltax2;
996 foreach ($arrayofvat[$key] as $k => $mt) {
998 print
'<tr class="oddeven">';
999 print
"<!-- VAT -->";
1000 print
"<td>".$date.
"</td>";
1001 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
1005 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
1006 print
'<span class="error">'.$langs->trans(
"VATAccountNotDefined").
' ('.$langs->trans(
"Purchase").
')</span>';
1008 print $accountoshow;
1015 print $companystatic->getNomUrl(0,
'supplier', 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"VAT").
' '.join(
', ', $def_tva[$key][$k]).
' %'.($numtax ?
' - Localtax '.$numtax :
'');
1017 print
'<td class="right nowraponall amount">'.($mt >= 0 ?
price($mt) :
'').
"</td>";
1018 print
'<td class="right nowraponall amount">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
1027 if (is_array($tabother[$key])) {
1028 foreach ($tabother[$key] as $k => $mt) {
1030 print
'<tr class="oddeven">';
1031 print
'<!-- VAT counterpart NPR -->';
1032 print
"<td>".$date.
"</td>";
1033 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
1037 if ($accountoshow ==
'' || $accountoshow ==
'NotDefined') {
1038 print
'<span class="error">'.$langs->trans(
"VATAccountNotDefined").
' ('.$langs->trans(
"NPR counterpart").
'). Set ACCOUNTING_COUNTERPART_VAT_NPR to the subvention account</span>';
1040 print $accountoshow;
1046 print
"<td>".$companystatic->getNomUrl(0,
'supplier', 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"VAT").
" NPR (counterpart)</td>";
1047 print
'<td class="right nowraponall amount">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
1048 print
'<td class="right nowraponall amount">'.($mt >= 0 ?
price($mt) :
'').
"</td>";
1058 print
'<tr class="oddeven"><td colspan="7"><span class="opacitymedium">'.$langs->trans(
"NoRecordFound").
'</span></td></tr>';