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),
'supplier_invoice');
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 $bookkeeping->subledger_account =
'';
407 $bookkeeping->subledger_label =
'';
409 $bookkeeping->numero_compte = $k;
410 $bookkeeping->label_compte = $label_account;
412 $bookkeeping->label_operation =
dol_trunc($companystatic->name, 16).
' - '.$invoicestatic->ref_supplier.
' - '.$label_account;
413 $bookkeeping->montant = $mt;
414 $bookkeeping->sens = ($mt < 0) ?
'C' :
'D';
415 $bookkeeping->debit = ($mt > 0) ? $mt : 0;
416 $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
417 $bookkeeping->code_journal = $journal;
418 $bookkeeping->journal_label = $langs->transnoentities($journal_label);
419 $bookkeeping->fk_user_author = $user->id;
420 $bookkeeping->entity = $conf->entity;
422 $totaldebit += $bookkeeping->debit;
423 $totalcredit += $bookkeeping->credit;
425 $result = $bookkeeping->create($user);
427 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
430 $errorforinvoice[$key] =
'alreadyjournalized';
435 $errorforinvoice[$key] =
'other';
445 if (!$errorforline) {
446 $listoftax = array(0, 1, 2);
447 foreach ($listoftax as $numtax) {
448 $arrayofvat = $tabtva;
450 $arrayofvat = $tablocaltax1;
453 $arrayofvat = $tablocaltax2;
456 foreach ($arrayofvat[$key] as $k => $mt) {
458 $accountingaccount->fetch(
null, $k,
true);
459 $label_account = $accountingaccount->label;
462 $bookkeeping->doc_date = $val[
"date"];
463 $bookkeeping->date_lim_reglement = $val[
"datereg"];
464 $bookkeeping->doc_ref = $val[
"refsologest"];
465 $bookkeeping->date_creation = $now;
466 $bookkeeping->doc_type =
'supplier_invoice';
467 $bookkeeping->fk_doc = $key;
468 $bookkeeping->fk_docdet = 0;
469 $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
471 $bookkeeping->subledger_account =
'';
472 $bookkeeping->subledger_label =
'';
474 $bookkeeping->numero_compte = $k;
475 $bookkeeping->label_compte = $label_account;
477 $bookkeeping->label_operation =
dol_trunc($companystatic->name, 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"VAT").
' '.join(
', ', $def_tva[$key][$k]).
' %'.($numtax ?
' - Localtax '.$numtax :
'');
478 $bookkeeping->montant = $mt;
479 $bookkeeping->sens = ($mt < 0) ?
'C' :
'D';
480 $bookkeeping->debit = ($mt > 0) ? $mt : 0;
481 $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
482 $bookkeeping->code_journal = $journal;
483 $bookkeeping->journal_label = $langs->transnoentities($journal_label);
484 $bookkeeping->fk_user_author = $user->id;
485 $bookkeeping->entity = $conf->entity;
487 $totaldebit += $bookkeeping->debit;
488 $totalcredit += $bookkeeping->credit;
490 $result = $bookkeeping->create($user);
492 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
495 $errorforinvoice[$key] =
'alreadyjournalized';
500 $errorforinvoice[$key] =
'other';
511 if (!$errorforline && is_array($tabother[$key])) {
512 foreach ($tabother[$key] as $k => $mt) {
515 $bookkeeping->doc_date = $val[
"date"];
516 $bookkeeping->date_lim_reglement = $val[
"datereg"];
517 $bookkeeping->doc_ref = $val[
"refsologest"];
518 $bookkeeping->date_creation = $now;
519 $bookkeeping->doc_type =
'supplier_invoice';
520 $bookkeeping->fk_doc = $key;
521 $bookkeeping->fk_docdet = 0;
522 $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
524 $bookkeeping->subledger_account =
'';
525 $bookkeeping->subledger_label =
'';
527 $bookkeeping->numero_compte = $k;
529 $bookkeeping->label_operation =
dol_trunc($companystatic->name, 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"VAT").
' NPR';
530 $bookkeeping->montant = $mt;
531 $bookkeeping->sens = ($mt < 0) ?
'C' :
'D';
532 $bookkeeping->debit = ($mt > 0) ? $mt : 0;
533 $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
534 $bookkeeping->code_journal = $journal;
535 $bookkeeping->journal_label = $langs->transnoentities($journal_label);
536 $bookkeeping->fk_user_author = $user->id;
537 $bookkeeping->entity = $conf->entity;
539 $totaldebit += $bookkeeping->debit;
540 $totalcredit += $bookkeeping->credit;
542 $result = $bookkeeping->create($user);
544 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
547 $errorforinvoice[$key] =
'alreadyjournalized';
552 $errorforinvoice[$key] =
'other';
561 if (!$errorforline && (
price2num($totaldebit,
'MT') !=
price2num($totalcredit,
'MT'))) {
564 $errorforinvoice[$key] =
'amountsnotbalanced';
565 setEventMessages(
'Try to insert a non balanced transaction in book for '.$invoicestatic->ref.
'. Canceled. Surely a bug.',
null,
'errors');
568 if (!$errorforline) {
574 setEventMessages($langs->trans(
"ErrorTooManyErrorsProcessStopped"),
null,
'errors');
582 if (empty($error) && count($tabpay) > 0) {
584 } elseif (count($tabpay) == $error) {
587 setEventMessages($langs->trans(
"GeneralLedgerSomeRecordWasNotRecorded"),
null,
'warnings');
593 if (count($tabpay) != $error) {
594 $param =
'id_journal='.$id_journal;
595 $param .=
'&date_startday='.$date_startday;
596 $param .=
'&date_startmonth='.$date_startmonth;
597 $param .=
'&date_startyear='.$date_startyear;
598 $param .=
'&date_endday='.$date_endday;
599 $param .=
'&date_endmonth='.$date_endmonth;
600 $param .=
'&date_endyear='.$date_endyear;
601 $param .=
'&in_bookkeeping='.$in_bookkeeping;
602 header(
"Location: ".$_SERVER[
'PHP_SELF'].($param ?
'?'.$param :
''));
614 if ($action ==
'exportcsv') {
615 $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
617 $filename =
'journal';
618 $type_export =
'journal';
619 include DOL_DOCUMENT_ROOT.
'/accountancy/tpl/export_journal.tpl.php';
624 foreach ($tabfac as $key => $val) {
625 $companystatic->id = $tabcompany[$key][
'id'];
626 $companystatic->name = $tabcompany[$key][
'name'];
627 $companystatic->code_compta_fournisseur = $tabcompany[$key][
'code_compta_fournisseur'];
628 $companystatic->code_fournisseur = $tabcompany[$key][
'code_fournisseur'];
629 $companystatic->fournisseur = 1;
631 $invoicestatic->id = $key;
632 $invoicestatic->ref = $val[
"refsologest"];
633 $invoicestatic->ref_supplier = $val[
"refsuppliersologest"];
634 $invoicestatic->type = $val[
"type"];
635 $invoicestatic->description =
dol_trunc(html_entity_decode($val[
"description"]), 32);
636 $invoicestatic->close_code = $val[
"close_code"];
641 $replacedinvoice = 0;
642 if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED) {
643 $replacedinvoice = 1;
644 $alreadydispatched = $invoicestatic->getVentilExportCompta();
645 if ($alreadydispatched) {
646 $replacedinvoice = 2;
651 if ($replacedinvoice == 1) {
656 foreach ($tabttc[$key] as $k => $mt) {
658 print
'"'.$key.
'"'.$sep;
659 print
'"'.$date.
'"'.$sep;
660 print
'"'.$val[
"refsologest"].
'"'.$sep;
661 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 32)).
'"'.$sep;
662 print
'"'.length_accounta(html_entity_decode($k)).
'"'.$sep;
663 print
'"'.length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER).
'"'.$sep;
664 print
'"'.length_accounta(html_entity_decode($k)).
'"'.$sep;
665 print
'"'.$langs->trans(
"Thirdparty").
'"'.$sep;
666 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 16)).
' - '.$val[
"refsuppliersologest"].
' - '.$langs->trans(
"Thirdparty").
'"'.$sep;
667 print
'"'.($mt < 0 ?
price(-$mt) :
'').
'"'.$sep;
668 print
'"'.($mt >= 0 ?
price($mt) :
'').
'"'.$sep;
669 print
'"'.$journal.
'"';
675 foreach ($tabht[$key] as $k => $mt) {
677 $accountingaccount->fetch(
null, $k,
true);
679 print
'"'.$key.
'"'.$sep;
680 print
'"'.$date.
'"'.$sep;
681 print
'"'.$val[
"refsologest"].
'"'.$sep;
682 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 32)).
'"'.$sep;
683 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
684 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
686 print
'"'.utf8_decode(
dol_trunc($accountingaccount->label, 32)).
'"'.$sep;
687 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 16)).
' - '.$val[
"refsuppliersologest"].
' - '.
dol_trunc($accountingaccount->label, 32).
'"'.$sep;
688 print
'"'.($mt >= 0 ?
price($mt) :
'').
'"'.$sep;
689 print
'"'.($mt < 0 ?
price(-$mt) :
'').
'"'.$sep;
690 print
'"'.$journal.
'"';
696 $listoftax = array(0, 1, 2);
697 foreach ($listoftax as $numtax) {
698 $arrayofvat = $tabtva;
700 $arrayofvat = $tablocaltax1;
703 $arrayofvat = $tablocaltax2;
706 foreach ($arrayofvat[$key] as $k => $mt) {
708 print
'"'.$key.
'"'.$sep;
709 print
'"'.$date.
'"'.$sep;
710 print
'"'.$val[
"refsologest"].
'"'.$sep;
711 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 32)).
'"'.$sep;
712 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
713 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
715 print
'"'.$langs->trans(
"VAT").
' - '.join(
', ', $def_tva[$key][$k]).
' %"'.$sep;
716 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 16)).
' - '.$val[
"refsuppliersologest"].
' - '.$langs->trans(
"VAT").join(
', ', $def_tva[$key][$k]).
' %'.($numtax ?
' - Localtax '.$numtax :
'').
'"'.$sep;
717 print
'"'.($mt >= 0 ?
price($mt) :
'').
'"'.$sep;
718 print
'"'.($mt < 0 ?
price(-$mt) :
'').
'"'.$sep;
719 print
'"'.$journal.
'"';
725 if (is_array($tabother[$key])) {
726 foreach ($tabother[$key] as $k => $mt) {
728 print
'"'.$key.
'"'.$sep;
729 print
'"'.$date.
'"'.$sep;
730 print
'"'.$val[
"refsologest"].
'"'.$sep;
731 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 32)).
'"'.$sep;
732 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
733 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
734 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
735 print
'"'.$langs->trans(
"Thirdparty").
'"'.$sep;
736 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 16)).
' - '.$val[
"refsuppliersologest"].
' - '.$langs->trans(
"VAT").
' NPR"'.$sep;
737 print
'"'.($mt < 0 ?
price(-$mt) :
'').
'"'.$sep;
738 print
'"'.($mt >= 0 ?
price($mt) :
'').
'"'.$sep;
739 print
'"'.$journal.
'"';
748 if (empty($action) || $action ==
'view') {
749 llxHeader(
'', $langs->trans(
"PurchasesJournal"));
751 $nom = $langs->trans(
"PurchasesJournal").
' | '.$accountingjournalstatic->getNomUrl(0, 1, 1,
'', 1);
756 $description = $langs->trans(
"DescJournalOnlyBindedVisible").
'<br>';
757 if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
758 $description .= $langs->trans(
"DepositsAreNotIncluded");
760 $description .= $langs->trans(
"DepositsAreIncluded");
763 $listofchoices = array(
'notyet'=>$langs->trans(
"NotYetInGeneralLedger"),
'already'=>$langs->trans(
"AlreadyInGeneralLedger"));
764 $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);
765 $period .=
' - '.$langs->trans(
"JournalizationInLedgerStatus").
' '.
$form->selectarray(
'in_bookkeeping', $listofchoices, $in_bookkeeping, 1);
767 $varlink =
'id_journal='.$id_journal;
769 journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array(
'action' =>
''),
'', $varlink);
772 if (($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER ==
"") || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER ==
'-1') {
773 print
'<br><div class="warning">'.img_warning().
' '.$langs->trans(
"SomeMandatoryStepsOfSetupWereNotDone");
774 $desc =
' : '.$langs->trans(
"AccountancyAreaDescMisc", 4,
'{link}');
775 $desc = str_replace(
'{link}',
'<strong>'.$langs->transnoentitiesnoconv(
"MenuAccountancy").
'-'.$langs->transnoentitiesnoconv(
"Setup").
"-".$langs->transnoentitiesnoconv(
"MenuDefaultAccounts").
'</strong>', $desc);
779 print
'<div class="tabsAction tabsActionNoBottom centerimp">';
780 if (!empty($conf->global->ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL) && $in_bookkeeping ==
'notyet') {
781 print
'<input type="button" class="butAction" name="exportcsv" value="'.$langs->trans(
"ExportDraftJournal").
'" onclick="launch_export();" />';
783 if (($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER ==
"") || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER ==
'-1') {
784 print
'<input type="button" class="butActionRefused classfortooltip" title="'.dol_escape_htmltag($langs->trans(
"SomeMandatoryStepsOfSetupWereNotDone")).
'" value="'.$langs->trans(
"WriteBookKeeping").
'" />';
786 if ($in_bookkeeping ==
'notyet') {
787 print
'<input type="button" class="butAction" name="writebookkeeping" value="'.$langs->trans(
"WriteBookKeeping").
'" onclick="writebookkeeping();" />';
789 print
'<a href="#" class="butActionRefused classfortooltip" name="writebookkeeping">'.$langs->trans(
"WriteBookKeeping").
'</a>';
796 <script type="text/javascript">
797 function launch_export() {
798 $("div.fiche form input[name=\"action\"]").val("exportcsv");
799 $("div.fiche form input[type=\"submit\"]").click();
800 $("div.fiche form input[name=\"action\"]").val("");
802 function writebookkeeping() {
803 console.log("click on writebookkeeping");
804 $("div.fiche form input[name=\"action\"]").val("writebookkeeping");
805 $("div.fiche form input[type=\"submit\"]").click();
806 $("div.fiche form input[name=\"action\"]").val("");
816 print
'<div class="div-table-responsive">';
817 print
"<table class=\"noborder\" width=\"100%\">";
818 print
"<tr class=\"liste_titre\">";
819 print
"<td>".$langs->trans(
"Date").
"</td>";
820 print
"<td>".$langs->trans(
"Piece").
' ('.$langs->trans(
"InvoiceRef").
")</td>";
821 print
"<td>".$langs->trans(
"AccountAccounting").
"</td>";
822 print
"<td>".$langs->trans(
"SubledgerAccount").
"</td>";
823 print
"<td>".$langs->trans(
"LabelOperation").
"</td>";
824 print
'<td class="center">'.$langs->trans(
"Debit").
"</td>";
825 print
'<td class="center">'.$langs->trans(
"Credit").
"</td>";
833 foreach ($tabfac as $key => $val) {
834 $companystatic->id = $tabcompany[$key][
'id'];
835 $companystatic->name = $tabcompany[$key][
'name'];
836 $companystatic->code_compta_fournisseur = $tabcompany[$key][
'code_compta_fournisseur'];
837 $companystatic->code_fournisseur = $tabcompany[$key][
'code_fournisseur'];
838 $companystatic->fournisseur = 1;
840 $invoicestatic->id = $key;
841 $invoicestatic->ref = $val[
"refsologest"];
842 $invoicestatic->ref_supplier = $val[
"refsuppliersologest"];
843 $invoicestatic->type = $val[
"type"];
844 $invoicestatic->description =
dol_trunc(html_entity_decode($val[
"description"]), 32);
845 $invoicestatic->close_code = $val[
"close_code"];
850 $replacedinvoice = 0;
851 if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED) {
852 $replacedinvoice = 1;
853 $alreadydispatched = $invoicestatic->getVentilExportCompta();
854 if ($alreadydispatched) {
855 $replacedinvoice = 2;
860 if ($replacedinvoice == 1) {
861 print
'<tr class="oddeven">';
862 print
"<!-- Replaced invoice -->";
863 print
"<td>".$date.
"</td>";
864 print
"<td><strike>".$invoicestatic->getNomUrl(1).
"</strike></td>";
867 print $langs->trans(
"Replaced");
874 print
'<td class="right"></td>';
875 print
'<td class="right"></td>';
880 if ($errorforinvoice[$key] ==
'somelinesarenotbound') {
881 print
'<tr class="oddeven">';
882 print
"<!-- Some lines are not bound -->";
883 print
"<td>".$date.
"</td>";
884 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
887 print
'<span class="error">'.$langs->trans(
'ErrorInvoiceContainsLinesNotYetBoundedShort', $val[
'ref']).
'</span>';
894 print
'<td class="right"></td>';
895 print
'<td class="right"></td>';
900 foreach ($tabttc[$key] as $k => $mt) {
901 print
'<tr class="oddeven">';
902 print
"<!-- Thirdparty -->";
903 print
"<td>".$date.
"</td>";
904 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
907 $accountoshow =
length_accountg($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER);
908 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
909 print
'<span class="error">'.$langs->trans(
"MainAccountForSuppliersNotDefined").
'</span>';
917 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
918 print
'<span class="error">'.$langs->trans(
"ThirdpartyAccountNotDefined").
'</span>';
923 print
"<td>".$companystatic->getNomUrl(0,
'supplier', 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"SubledgerAccount").
"</td>";
924 print
'<td class="right nowraponall amount">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
925 print
'<td class="right nowraponall amount">'.($mt >= 0 ?
price($mt) :
'').
"</td>";
930 foreach ($tabht[$key] as $k => $mt) {
932 $accountingaccount->fetch(
null, $k,
true);
934 print
'<tr class="oddeven">';
935 print
"<!-- Product -->";
936 print
"<td>".$date.
"</td>";
937 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
941 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
942 print
'<span class="error">'.$langs->trans(
"ProductAccountNotDefined").
'</span>';
950 $companystatic->id = $tabcompany[$key][
'id'];
951 $companystatic->name = $tabcompany[$key][
'name'];
952 print
"<td>".$companystatic->getNomUrl(0,
'supplier', 16).
' - '.$invoicestatic->ref_supplier.
' - '.$accountingaccount->label.
"</td>";
953 print
'<td class="right nowraponall amount">'.($mt >= 0 ?
price($mt) :
'').
"</td>";
954 print
'<td class="right nowraponall amount">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
959 $listoftax = array(0, 1, 2);
960 foreach ($listoftax as $numtax) {
961 $arrayofvat = $tabtva;
963 $arrayofvat = $tablocaltax1;
966 $arrayofvat = $tablocaltax2;
969 foreach ($arrayofvat[$key] as $k => $mt) {
971 print
'<tr class="oddeven">';
972 print
"<!-- VAT -->";
973 print
"<td>".$date.
"</td>";
974 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
978 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
979 print
'<span class="error">'.$langs->trans(
"VATAccountNotDefined").
' ('.$langs->trans(
"Purchase").
')</span>';
988 print $companystatic->getNomUrl(0,
'supplier', 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"VAT").
' '.join(
', ', $def_tva[$key][$k]).
' %'.($numtax ?
' - Localtax '.$numtax :
'');
990 print
'<td class="right nowraponall amount">'.($mt >= 0 ?
price($mt) :
'').
"</td>";
991 print
'<td class="right nowraponall amount">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
998 if (is_array($tabother[$key])) {
999 foreach ($tabother[$key] as $k => $mt) {
1001 print
'<tr class="oddeven">';
1002 print
'<!-- VAT counterpart NPR -->';
1003 print
"<td>".$date.
"</td>";
1004 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
1008 if ($accountoshow ==
'' || $accountoshow ==
'NotDefined') {
1009 print
'<span class="error">'.$langs->trans(
"VATAccountNotDefined").
' ('.$langs->trans(
"NPR counterpart").
'). Set ACCOUNTING_COUNTERPART_VAT_NPR to the subvention account</span>';
1011 print $accountoshow;
1017 print
"<td>".$companystatic->getNomUrl(0,
'supplier', 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"VAT").
" NPR (counterpart)</td>";
1018 print
'<td class="right nowraponall amount">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
1019 print
'<td class="right nowraponall amount">'.($mt >= 0 ?
price($mt) :
'').
"</td>";