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.
'/core/lib/company.lib.php';
36 require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/accountingjournal.class.php';
37 require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/accountingaccount.class.php';
38 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.facture.class.php';
39 require_once DOL_DOCUMENT_ROOT.
'/fourn/class/fournisseur.class.php';
40 require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/bookkeeping.class.php';
43 $langs->loadLangs(array(
"commercial",
"compta",
"bills",
"other",
"accountancy",
"errors"));
45 $id_journal =
GETPOST(
'id_journal',
'int');
46 $action =
GETPOST(
'action',
'aZ09');
48 $date_startmonth =
GETPOST(
'date_startmonth');
49 $date_startday =
GETPOST(
'date_startday');
50 $date_startyear =
GETPOST(
'date_startyear');
51 $date_endmonth =
GETPOST(
'date_endmonth');
52 $date_endday =
GETPOST(
'date_endday');
53 $date_endyear =
GETPOST(
'date_endyear');
54 $in_bookkeeping =
GETPOST(
'in_bookkeeping');
55 if ($in_bookkeeping ==
'') {
56 $in_bookkeeping =
'notyet';
61 $hookmanager->initHooks(array(
'purchasesjournal'));
62 $parameters = array();
68 if ($user->socid > 0) {
71 if (!$user->hasRight(
'accounting',
'mouvements',
'lire')) {
80 $reshook = $hookmanager->executeHooks(
'doActions', $parameters, $user, $action);
86 $accountingjournalstatic->fetch($id_journal);
87 $journal = $accountingjournalstatic->code;
88 $journal_label = $accountingjournalstatic->label;
90 $date_start =
dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
91 $date_end =
dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
93 if (empty($date_startmonth) || empty($date_endmonth)) {
96 $date_start = $dates[
'date_start'];
97 $date_end = $dates[
'date_end'];
98 $pastmonthyear = $dates[
'pastmonthyear'];
99 $pastmonth = $dates[
'pastmonth'];
102 if (!
GETPOSTISSET(
'date_startmonth') && (empty($date_start) || empty($date_end))) {
107 $sql =
"SELECT f.rowid, f.ref as ref, f.type, f.datef as df, f.libelle as label, f.ref_supplier, f.date_lim_reglement as dlr, f.close_code, f.vat_reverse_charge,";
108 $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,";
109 $sql .=
" p.default_vat_code AS product_buy_default_vat_code, p.tva_tx as product_buy_vat, p.localtax1_tx as product_buy_localvat1, p.localtax2_tx as product_buy_localvat2,";
110 $sql .=
" co.code as country_code, co.label as country_label,";
111 $sql .=
" s.rowid as socid, s.nom as name, s.fournisseur, s.code_client, s.code_fournisseur, s.fk_pays,";
113 $sql .=
" spe.accountancy_code_customer as code_compta,";
114 $sql .=
" spe.accountancy_code_supplier as code_compta_fournisseur,";
116 $sql .=
" s.code_compta as code_compta,";
117 $sql .=
" s.code_compta_fournisseur,";
120 $sql .=
" ppe.accountancy_code_buy,";
122 $sql .=
" p.accountancy_code_buy,";
124 $sql .=
" aa.rowid as fk_compte, aa.account_number as compte, aa.label as label_compte";
125 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn_det as fd";
126 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON p.rowid = fd.fk_product";
128 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"product_perentity as ppe ON ppe.fk_product = p.rowid AND ppe.entity = " . ((int) $conf->entity);
130 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"accounting_account as aa ON aa.rowid = fd.fk_code_ventilation";
131 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"facture_fourn as f ON f.rowid = fd.fk_facture_fourn";
132 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"societe as s ON s.rowid = f.fk_soc";
133 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"c_country as co ON co.rowid = s.fk_pays ";
135 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"societe_perentity as spe ON spe.fk_soc = s.rowid AND spe.entity = " . ((int) $conf->entity);
137 $sql .=
" WHERE f.fk_statut > 0";
138 $sql .=
" AND fd.fk_code_ventilation > 0";
139 $sql .=
" AND f.entity IN (".getEntity(
'facture_fourn', 0).
")";
145 if ($date_start && $date_end) {
146 $sql .=
" AND f.datef >= '".$db->idate($date_start).
"' AND f.datef <= '".$db->idate($date_end).
"'";
153 if ($in_bookkeeping ==
'already') {
154 $sql .=
" AND f.rowid IN (SELECT fk_doc FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping as ab WHERE ab.doc_type='supplier_invoice')";
156 if ($in_bookkeeping ==
'notyet') {
157 $sql .=
" AND f.rowid NOT IN (SELECT fk_doc FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping as ab WHERE ab.doc_type='supplier_invoice')";
159 $sql .=
" ORDER BY f.datef";
161 dol_syslog(
'accountancy/journal/purchasesjournal.php', LOG_DEBUG);
162 $result = $db->query(
$sql);
169 $tablocaltax1 = array();
170 $tablocaltax2 = array();
171 $tabcompany = array();
174 $tabrclocaltax1 = array();
175 $tabrclocaltax2 = array();
177 $num = $db->num_rows($result);
182 $rcctva =
getDolGlobalString(
'ACCOUNTING_VAT_BUY_REVERSE_CHARGES_CREDIT',
'NotDefined');
183 $rcdtva =
getDolGlobalString(
'ACCOUNTING_VAT_BUY_REVERSE_CHARGES_DEBIT',
'NotDefined');
188 $obj = $db->fetch_object($result);
191 $compta_soc = ($obj->code_compta_fournisseur !=
"") ? $obj->code_compta_fournisseur : $cptfour;
193 $compta_prod = $obj->compte;
194 if (empty($compta_prod)) {
195 if ($obj->product_type == 0) {
202 $vatdata =
getTaxesFromId($obj->tva_tx.($obj->vat_src_code ?
' ('.$obj->vat_src_code.
')' :
''), $mysoc, $mysoc, 0);
203 $compta_tva = (!empty($vatdata[
'accountancy_code_buy']) ? $vatdata[
'accountancy_code_buy'] : $cpttva);
204 $compta_localtax1 = (!empty($vatdata[
'accountancy_code_buy']) ? $vatdata[
'accountancy_code_buy'] : $cpttva);
205 $compta_localtax2 = (!empty($vatdata[
'accountancy_code_buy']) ? $vatdata[
'accountancy_code_buy'] : $cpttva);
206 $compta_counterpart_tva_npr =
getDolGlobalString(
'ACCOUNTING_COUNTERPART_VAT_NPR',
'NotDefined');
209 if (
price2num($obj->tva_tx) || !empty($obj->vat_src_code)) {
210 $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.
')' :
''));
216 $tabfac[$obj->rowid][
"date"] = $db->jdate($obj->df);
217 $tabfac[$obj->rowid][
"datereg"] = $db->jdate($obj->dlr);
218 $tabfac[$obj->rowid][
"ref"] = $obj->ref_supplier.
' ('.$obj->ref.
')';
219 $tabfac[$obj->rowid][
"refsologest"] = $obj->ref;
220 $tabfac[$obj->rowid][
"refsuppliersologest"] = $obj->ref_supplier;
221 $tabfac[$obj->rowid][
"type"] = $obj->type;
222 $tabfac[$obj->rowid][
"description"] = $obj->description;
223 $tabfac[$obj->rowid][
"close_code"] = $obj->close_code;
227 if (!isset($tabttc[$obj->rowid][$compta_soc])) {
228 $tabttc[$obj->rowid][$compta_soc] = 0;
230 if (!isset($tabht[$obj->rowid][$compta_prod])) {
231 $tabht[$obj->rowid][$compta_prod] = 0;
233 if (!isset($tabtva[$obj->rowid][$compta_tva])) {
234 $tabtva[$obj->rowid][$compta_tva] = 0;
236 if (!isset($tablocaltax1[$obj->rowid][$compta_localtax1])) {
237 $tablocaltax1[$obj->rowid][$compta_localtax1] = 0;
239 if (!isset($tablocaltax2[$obj->rowid][$compta_localtax2])) {
240 $tablocaltax2[$obj->rowid][$compta_localtax2] = 0;
244 if (($mysoc->country_code ==
'FR' ||
getDolGlobalString(
'ACCOUNTING_FORCE_ENABLE_VAT_REVERSE_CHARGE')) && $obj->vat_reverse_charge == 1 && in_array($obj->country_code, $country_code_in_EEC)) {
245 $rcvatdata =
getTaxesFromId($obj->product_buy_vat . ($obj->product_buy_default_vat_code ?
' (' . $obj->product_buy_default_vat_code .
')' :
''), $mysoc, $mysoc, 0);
246 $rcc_compta_tva = (!empty($vatdata[
'accountancy_code_vat_reverse_charge_credit']) ? $vatdata[
'accountancy_code_vat_reverse_charge_credit'] : $rcctva);
247 $rcd_compta_tva = (!empty($vatdata[
'accountancy_code_vat_reverse_charge_debit']) ? $vatdata[
'accountancy_code_vat_reverse_charge_debit'] : $rcdtva);
248 $rcc_compta_localtax1 = (!empty($vatdata[
'accountancy_code_vat_reverse_charge_credit']) ? $vatdata[
'accountancy_code_vat_reverse_charge_credit'] : $rcctva);
249 $rcd_compta_localtax1 = (!empty($vatdata[
'accountancy_code_vat_reverse_charge_debit']) ? $vatdata[
'accountancy_code_vat_reverse_charge_debit'] : $rcdtva);
250 $rcc_compta_localtax2 = (!empty($vatdata[
'accountancy_code_vat_reverse_charge_credit']) ? $vatdata[
'accountancy_code_vat_reverse_charge_credit'] : $rcctva);
251 $rcd_compta_localtax2 = (!empty($vatdata[
'accountancy_code_vat_reverse_charge_debit']) ? $vatdata[
'accountancy_code_vat_reverse_charge_debit'] : $rcdtva);
252 if (
price2num($obj->product_buy_vat) || !empty($obj->product_buy_default_vat_code)) {
253 $vat_key =
vatrate($obj->product_buy_vat) . ($obj->product_buy_default_vat_code ?
' (' . $obj->product_buy_default_vat_code .
')' :
'');
254 $val_value = $vat_key;
255 $def_tva[$obj->rowid][$rcc_compta_tva][$vat_key] = $val_value;
256 $def_tva[$obj->rowid][$rcd_compta_tva][$vat_key] = $val_value;
259 if (!isset($tabrctva[$obj->rowid][$rcc_compta_tva])) {
260 $tabrctva[$obj->rowid][$rcc_compta_tva] = 0;
262 if (!isset($tabrctva[$obj->rowid][$rcd_compta_tva])) {
263 $tabrctva[$obj->rowid][$rcd_compta_tva] = 0;
265 if (!isset($tabrclocaltax1[$obj->rowid][$rcc_compta_localtax1])) {
266 $tabrclocaltax1[$obj->rowid][$rcc_compta_localtax1] = 0;
268 if (!isset($tabrclocaltax1[$obj->rowid][$rcd_compta_localtax1])) {
269 $tabrclocaltax1[$obj->rowid][$rcd_compta_localtax1] = 0;
271 if (!isset($tabrclocaltax2[$obj->rowid][$rcc_compta_localtax2])) {
272 $tabrclocaltax2[$obj->rowid][$rcc_compta_localtax2] = 0;
274 if (!isset($tabrclocaltax2[$obj->rowid][$rcd_compta_localtax2])) {
275 $tabrclocaltax2[$obj->rowid][$rcd_compta_localtax2] = 0;
278 $rcvat = (double)
price2num($obj->total_ttc * $obj->product_buy_vat / 100,
'MT');
279 $rclocalvat1 = (double)
price2num($obj->total_ttc * $obj->product_buy_localvat1 / 100,
'MT');
280 $rclocalvat2 = (double)
price2num($obj->total_ttc * $obj->product_buy_localvat2 / 100,
'MT');
282 $tabrctva[$obj->rowid][$rcd_compta_tva] += $rcvat;
283 $tabrctva[$obj->rowid][$rcc_compta_tva] -= $rcvat;
284 $tabrclocaltax1[$obj->rowid][$rcd_compta_localtax1] += $rclocalvat1;
285 $tabrclocaltax1[$obj->rowid][$rcc_compta_localtax1] -= $rclocalvat1;
286 $tabrclocaltax2[$obj->rowid][$rcd_compta_localtax2] += $rclocalvat2;
287 $tabrclocaltax2[$obj->rowid][$rcc_compta_localtax2] -= $rclocalvat2;
290 $tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc;
291 $tabht[$obj->rowid][$compta_prod] += $obj->total_ht;
292 $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva;
293 $tva_npr = (($obj->info_bits & 1 == 1) ? 1 : 0);
295 $tabother[$obj->rowid][$compta_counterpart_tva_npr] += $obj->total_tva;
297 $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1;
298 $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2;
299 $tabcompany[$obj->rowid] = array(
301 'name' => $obj->name,
302 'code_fournisseur' => $obj->code_fournisseur,
303 'code_compta_fournisseur' => $compta_soc
312 $errorforinvoice = array();
315 foreach ($tabfac as $key => $val) {
316 $sql =
"SELECT COUNT(fd.rowid) as nb";
317 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facture_fourn_det as fd";
318 $sql .=
" WHERE fd.product_type <= 2 AND fd.fk_code_ventilation <= 0";
319 $sql .=
" AND fd.total_ttc <> 0 AND fk_facture_fourn = ".((int) $key);
320 $resql = $db->query(
$sql);
322 $obj = $db->fetch_object($resql);
324 $errorforinvoice[$key] =
'somelinesarenotbound';
335 if ($action ==
'writebookkeeping') {
339 $companystatic =
new Societe($db);
343 $accountingaccountsupplier->fetch(
null,
getDolGlobalString(
'ACCOUNTING_ACCOUNT_SUPPLIER'),
true);
345 foreach ($tabfac as $key => $val) {
353 $companystatic->id = $tabcompany[$key][
'id'];
354 $companystatic->name = $tabcompany[$key][
'name'];
355 $companystatic->code_compta_fournisseur = $tabcompany[$key][
'code_compta_fournisseur'];
356 $companystatic->code_fournisseur = $tabcompany[$key][
'code_fournisseur'];
357 $companystatic->fournisseur = 1;
359 $invoicestatic->id = $key;
360 $invoicestatic->ref = (string) $val[
"refsologest"];
361 $invoicestatic->ref_supplier = $val[
"refsuppliersologest"];
362 $invoicestatic->type = $val[
"type"];
363 $invoicestatic->description = html_entity_decode(
dol_trunc($val[
"description"], 32));
364 $invoicestatic->close_code = $val[
"close_code"];
369 $replacedinvoice = 0;
370 if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED) {
371 $replacedinvoice = 1;
372 $alreadydispatched = $invoicestatic->getVentilExportCompta();
373 if ($alreadydispatched) {
374 $replacedinvoice = 2;
379 if ($replacedinvoice == 1) {
385 if ($errorforinvoice[$key] ==
'somelinesarenotbound') {
388 setEventMessages($langs->trans(
'ErrorInvoiceContainsLinesNotYetBounded', $val[
'ref']),
null,
'errors');
392 if (!$errorforline) {
393 foreach ($tabttc[$key] as $k => $mt) {
395 $bookkeeping->doc_date = $val[
"date"];
396 $bookkeeping->date_lim_reglement = $val[
"datereg"];
397 $bookkeeping->doc_ref = $val[
"refsologest"];
398 $bookkeeping->date_creation = $now;
399 $bookkeeping->doc_type =
'supplier_invoice';
400 $bookkeeping->fk_doc = $key;
401 $bookkeeping->fk_docdet = 0;
402 $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
404 $bookkeeping->subledger_account = $tabcompany[$key][
'code_compta_fournisseur'];
405 $bookkeeping->subledger_label = $tabcompany[$key][
'name'];
408 $bookkeeping->label_compte = $accountingaccountsupplier->label;
410 $bookkeeping->label_operation =
dol_trunc($companystatic->name, 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"SubledgerAccount");
411 $bookkeeping->montant = $mt;
412 $bookkeeping->sens = ($mt >= 0) ?
'C' :
'D';
413 $bookkeeping->debit = ($mt <= 0) ? -$mt : 0;
414 $bookkeeping->credit = ($mt > 0) ? $mt : 0;
415 $bookkeeping->code_journal = $journal;
416 $bookkeeping->journal_label = $langs->transnoentities($journal_label);
417 $bookkeeping->fk_user_author = $user->id;
418 $bookkeeping->entity = $conf->entity;
420 $totaldebit += $bookkeeping->debit;
421 $totalcredit += $bookkeeping->credit;
423 $result = $bookkeeping->create($user);
425 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
428 $errorforinvoice[$key] =
'alreadyjournalized';
433 $errorforinvoice[$key] =
'other';
438 require_once DOL_DOCUMENT_ROOT .
'/accountancy/class/lettering.class.php';
441 $nb_lettering = $lettering_static->bookkeepingLettering(array($bookkeeping->id));
448 if (!$errorforline) {
449 foreach ($tabht[$key] as $k => $mt) {
450 $resultfetch = $accountingaccount->fetch(
null, $k,
true);
451 $label_account = $accountingaccount->label;
454 if ($resultfetch > 0) {
456 $bookkeeping->doc_date = $val[
"date"];
457 $bookkeeping->date_lim_reglement = $val[
"datereg"];
458 $bookkeeping->doc_ref = $val[
"refsologest"];
459 $bookkeeping->date_creation = $now;
460 $bookkeeping->doc_type =
'supplier_invoice';
461 $bookkeeping->fk_doc = $key;
462 $bookkeeping->fk_docdet = 0;
463 $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
467 $bookkeeping->subledger_account = $tabcompany[$key][
'code_compta'];
468 $bookkeeping->subledger_label = $tabcompany[$key][
'name'];
470 $bookkeeping->subledger_account =
'';
471 $bookkeeping->subledger_label =
'';
474 $bookkeeping->subledger_account =
'';
475 $bookkeeping->subledger_label =
'';
478 $bookkeeping->numero_compte = $k;
479 $bookkeeping->label_compte = $label_account;
481 $bookkeeping->label_operation =
dol_trunc($companystatic->name, 16).
' - '.$invoicestatic->ref_supplier.
' - '.$label_account;
482 $bookkeeping->montant = $mt;
483 $bookkeeping->sens = ($mt < 0) ?
'C' :
'D';
484 $bookkeeping->debit = ($mt > 0) ? $mt : 0;
485 $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
486 $bookkeeping->code_journal = $journal;
487 $bookkeeping->journal_label = $langs->transnoentities($journal_label);
488 $bookkeeping->fk_user_author = $user->id;
489 $bookkeeping->entity = $conf->entity;
491 $totaldebit += $bookkeeping->debit;
492 $totalcredit += $bookkeeping->credit;
494 $result = $bookkeeping->create($user);
496 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
499 $errorforinvoice[$key] =
'alreadyjournalized';
504 $errorforinvoice[$key] =
'other';
514 if (!$errorforline) {
515 $listoftax = array(0, 1, 2);
516 foreach ($listoftax as $numtax) {
517 $arrayofvat = $tabtva;
519 $arrayofvat = $tablocaltax1;
522 $arrayofvat = $tablocaltax2;
526 if ($mysoc->country_code ==
'FR' ||
getDolGlobalString(
'ACCOUNTING_FORCE_ENABLE_VAT_REVERSE_CHARGE')) {
528 foreach ($arrayofvat[$key] as $k => $mt) {
535 $arrayofvat = $tabrctva;
537 $arrayofvat = $tabrclocaltax1;
540 $arrayofvat = $tabrclocaltax2;
542 if (!is_array($arrayofvat[$key])) {
543 $arrayofvat[$key] = array();
548 foreach ($arrayofvat[$key] as $k => $mt) {
550 $accountingaccount->fetch(
null, $k,
true);
551 $label_account = $accountingaccount->label;
554 $bookkeeping->doc_date = $val[
"date"];
555 $bookkeeping->date_lim_reglement = $val[
"datereg"];
556 $bookkeeping->doc_ref = $val[
"refsologest"];
557 $bookkeeping->date_creation = $now;
558 $bookkeeping->doc_type =
'supplier_invoice';
559 $bookkeeping->fk_doc = $key;
560 $bookkeeping->fk_docdet = 0;
561 $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
563 $bookkeeping->subledger_account =
'';
564 $bookkeeping->subledger_label =
'';
566 $bookkeeping->numero_compte = $k;
567 $bookkeeping->label_compte = $label_account;
569 $bookkeeping->label_operation =
dol_trunc($companystatic->name, 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"VAT").
' '.join(
', ', $def_tva[$key][$k]).
' %'.($numtax ?
' - Localtax '.$numtax :
'');
570 $bookkeeping->montant = $mt;
571 $bookkeeping->sens = ($mt < 0) ?
'C' :
'D';
572 $bookkeeping->debit = ($mt > 0) ? $mt : 0;
573 $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
574 $bookkeeping->code_journal = $journal;
575 $bookkeeping->journal_label = $langs->transnoentities($journal_label);
576 $bookkeeping->fk_user_author = $user->id;
577 $bookkeeping->entity = $conf->entity;
579 $totaldebit += $bookkeeping->debit;
580 $totalcredit += $bookkeeping->credit;
582 $result = $bookkeeping->create($user);
584 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
587 $errorforinvoice[$key] =
'alreadyjournalized';
592 $errorforinvoice[$key] =
'other';
603 if (!$errorforline && is_array($tabother[$key])) {
604 foreach ($tabother[$key] as $k => $mt) {
607 $bookkeeping->doc_date = $val[
"date"];
608 $bookkeeping->date_lim_reglement = $val[
"datereg"];
609 $bookkeeping->doc_ref = $val[
"refsologest"];
610 $bookkeeping->date_creation = $now;
611 $bookkeeping->doc_type =
'supplier_invoice';
612 $bookkeeping->fk_doc = $key;
613 $bookkeeping->fk_docdet = 0;
614 $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
616 $bookkeeping->subledger_account =
'';
617 $bookkeeping->subledger_label =
'';
619 $bookkeeping->numero_compte = $k;
621 $bookkeeping->label_operation =
dol_trunc($companystatic->name, 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"VAT").
' NPR';
622 $bookkeeping->montant = $mt;
623 $bookkeeping->sens = ($mt < 0) ?
'C' :
'D';
624 $bookkeeping->debit = ($mt > 0) ? $mt : 0;
625 $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
626 $bookkeeping->code_journal = $journal;
627 $bookkeeping->journal_label = $langs->transnoentities($journal_label);
628 $bookkeeping->fk_user_author = $user->id;
629 $bookkeeping->entity = $conf->entity;
631 $totaldebit += $bookkeeping->debit;
632 $totalcredit += $bookkeeping->credit;
634 $result = $bookkeeping->create($user);
636 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
639 $errorforinvoice[$key] =
'alreadyjournalized';
644 $errorforinvoice[$key] =
'other';
653 if (!$errorforline && (
price2num($totaldebit,
'MT') !=
price2num($totalcredit,
'MT'))) {
656 $errorforinvoice[$key] =
'amountsnotbalanced';
657 setEventMessages(
'Try to insert a non balanced transaction in book for '.$invoicestatic->ref.
'. Canceled. Surely a bug.',
null,
'errors');
660 if (!$errorforline) {
666 setEventMessages($langs->trans(
"ErrorTooManyErrorsProcessStopped"),
null,
'errors');
674 if (empty($error) && count($tabpay) > 0) {
676 } elseif (count($tabpay) == $error) {
679 setEventMessages($langs->trans(
"GeneralLedgerSomeRecordWasNotRecorded"),
null,
'warnings');
685 if (count($tabpay) != $error) {
686 $param =
'id_journal='.$id_journal;
687 $param .=
'&date_startday='.$date_startday;
688 $param .=
'&date_startmonth='.$date_startmonth;
689 $param .=
'&date_startyear='.$date_startyear;
690 $param .=
'&date_endday='.$date_endday;
691 $param .=
'&date_endmonth='.$date_endmonth;
692 $param .=
'&date_endyear='.$date_endyear;
693 $param .=
'&in_bookkeeping='.$in_bookkeeping;
694 header(
"Location: ".$_SERVER[
'PHP_SELF'].($param ?
'?'.$param :
''));
706 if ($action ==
'exportcsv') {
709 $filename =
'journal';
710 $type_export =
'journal';
711 include DOL_DOCUMENT_ROOT.
'/accountancy/tpl/export_journal.tpl.php';
716 foreach ($tabfac as $key => $val) {
717 $companystatic->id = $tabcompany[$key][
'id'];
718 $companystatic->name = $tabcompany[$key][
'name'];
719 $companystatic->code_compta_fournisseur = $tabcompany[$key][
'code_compta_fournisseur'];
720 $companystatic->code_fournisseur = $tabcompany[$key][
'code_fournisseur'];
721 $companystatic->fournisseur = 1;
723 $invoicestatic->id = $key;
724 $invoicestatic->ref = $val[
"refsologest"];
725 $invoicestatic->ref_supplier = $val[
"refsuppliersologest"];
726 $invoicestatic->type = $val[
"type"];
727 $invoicestatic->description =
dol_trunc(html_entity_decode($val[
"description"]), 32);
728 $invoicestatic->close_code = $val[
"close_code"];
733 $replacedinvoice = 0;
734 if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED) {
735 $replacedinvoice = 1;
736 $alreadydispatched = $invoicestatic->getVentilExportCompta();
737 if ($alreadydispatched) {
738 $replacedinvoice = 2;
743 if ($replacedinvoice == 1) {
748 foreach ($tabttc[$key] as $k => $mt) {
750 print
'"'.$key.
'"'.$sep;
751 print
'"'.$date.
'"'.$sep;
752 print
'"'.$val[
"refsologest"].
'"'.$sep;
753 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 32)).
'"'.$sep;
754 print
'"'.length_accounta(html_entity_decode($k)).
'"'.$sep;
755 print
'"'.length_accountg(
getDolGlobalString(
'ACCOUNTING_ACCOUNT_SUPPLIER')).
'"'.$sep;
756 print
'"'.length_accounta(html_entity_decode($k)).
'"'.$sep;
757 print
'"'.$langs->trans(
"Thirdparty").
'"'.$sep;
758 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 16)).
' - '.$val[
"refsuppliersologest"].
' - '.$langs->trans(
"Thirdparty").
'"'.$sep;
759 print
'"'.($mt < 0 ?
price(-$mt) :
'').
'"'.$sep;
760 print
'"'.($mt >= 0 ?
price($mt) :
'').
'"'.$sep;
761 print
'"'.$journal.
'"';
767 foreach ($tabht[$key] as $k => $mt) {
769 $accountingaccount->fetch(
null, $k,
true);
771 print
'"'.$key.
'"'.$sep;
772 print
'"'.$date.
'"'.$sep;
773 print
'"'.$val[
"refsologest"].
'"'.$sep;
774 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 32)).
'"'.$sep;
775 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
776 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
778 print
'"'.utf8_decode(
dol_trunc($accountingaccount->label, 32)).
'"'.$sep;
779 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 16)).
' - '.$val[
"refsuppliersologest"].
' - '.
dol_trunc($accountingaccount->label, 32).
'"'.$sep;
780 print
'"'.($mt >= 0 ?
price($mt) :
'').
'"'.$sep;
781 print
'"'.($mt < 0 ?
price(-$mt) :
'').
'"'.$sep;
782 print
'"'.$journal.
'"';
788 $listoftax = array(0, 1, 2);
789 foreach ($listoftax as $numtax) {
790 $arrayofvat = $tabtva;
792 $arrayofvat = $tablocaltax1;
795 $arrayofvat = $tablocaltax2;
799 if ($mysoc->country_code ==
'FR' ||
getDolGlobalString(
'ACCOUNTING_FORCE_ENABLE_VAT_REVERSE_CHARGE')) {
801 foreach ($arrayofvat[$key] as $k => $mt) {
808 $arrayofvat = $tabrctva;
810 $arrayofvat = $tabrclocaltax1;
813 $arrayofvat = $tabrclocaltax2;
815 if (!is_array($arrayofvat[$key])) {
816 $arrayofvat[$key] = array();
821 foreach ($arrayofvat[$key] as $k => $mt) {
823 print
'"'.$key.
'"'.$sep;
824 print
'"'.$date.
'"'.$sep;
825 print
'"'.$val[
"refsologest"].
'"'.$sep;
826 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 32)).
'"'.$sep;
827 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
828 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
830 print
'"'.$langs->trans(
"VAT").
' - '.join(
', ', $def_tva[$key][$k]).
' %"'.$sep;
831 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 16)).
' - '.$val[
"refsuppliersologest"].
' - '.$langs->trans(
"VAT").join(
', ', $def_tva[$key][$k]).
' %'.($numtax ?
' - Localtax '.$numtax :
'').
'"'.$sep;
832 print
'"'.($mt >= 0 ?
price($mt) :
'').
'"'.$sep;
833 print
'"'.($mt < 0 ?
price(-$mt) :
'').
'"'.$sep;
834 print
'"'.$journal.
'"';
840 if (is_array($tabother[$key])) {
841 foreach ($tabother[$key] as $k => $mt) {
843 print
'"'.$key.
'"'.$sep;
844 print
'"'.$date.
'"'.$sep;
845 print
'"'.$val[
"refsologest"].
'"'.$sep;
846 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 32)).
'"'.$sep;
847 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
848 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
849 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
850 print
'"'.$langs->trans(
"Thirdparty").
'"'.$sep;
851 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 16)).
' - '.$val[
"refsuppliersologest"].
' - '.$langs->trans(
"VAT").
' NPR"'.$sep;
852 print
'"'.($mt < 0 ?
price(-$mt) :
'').
'"'.$sep;
853 print
'"'.($mt >= 0 ?
price($mt) :
'').
'"'.$sep;
854 print
'"'.$journal.
'"';
863 if (empty($action) || $action ==
'view') {
864 $title = $langs->trans(
"GenerationOfAccountingEntries").
' - '.$accountingjournalstatic->getNomUrl(0, 2, 1,
'', 1);
873 $description = $langs->trans(
"DescJournalOnlyBindedVisible").
'<br>';
875 $description .= $langs->trans(
"DepositsAreNotIncluded");
877 $description .= $langs->trans(
"DepositsAreIncluded");
880 $listofchoices = array(
'notyet'=>$langs->trans(
"NotYetInGeneralLedger"),
'already'=>$langs->trans(
"AlreadyInGeneralLedger"));
881 $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);
882 $period .=
' - '.$langs->trans(
"JournalizationInLedgerStatus").
' '.
$form->selectarray(
'in_bookkeeping', $listofchoices, $in_bookkeeping, 1);
884 $varlink =
'id_journal='.$id_journal;
886 journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array(
'action' =>
''),
'', $varlink);
889 $acctSupplierNotConfigured = in_array(
getDolGlobalString(
'ACCOUNTING_ACCOUNT_SUPPLIER'), [
'',
'-1']);
890 if ($acctSupplierNotConfigured) {
891 print
'<br><div class="warning">'.img_warning().
' '.$langs->trans(
"SomeMandatoryStepsOfSetupWereNotDone");
892 $desc =
' : '.$langs->trans(
"AccountancyAreaDescMisc", 4,
'{link}');
893 $desc = str_replace(
'{link}',
'<strong>'.$langs->transnoentitiesnoconv(
"MenuAccountancy").
'-'.$langs->transnoentitiesnoconv(
"Setup").
"-".$langs->transnoentitiesnoconv(
"MenuDefaultAccounts").
'</strong>', $desc);
897 print
'<div class="tabsAction tabsActionNoBottom centerimp">';
898 if (
getDolGlobalString(
'ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL') && $in_bookkeeping ==
'notyet') {
899 print
'<input type="button" class="butAction" name="exportcsv" value="'.$langs->trans(
"ExportDraftJournal").
'" onclick="launch_export();" />';
901 if ($acctSupplierNotConfigured) {
902 print
'<input type="button" class="butActionRefused classfortooltip" title="'.dol_escape_htmltag($langs->trans(
"SomeMandatoryStepsOfSetupWereNotDone")).
'" value="'.$langs->trans(
"WriteBookKeeping").
'" />';
904 if ($in_bookkeeping ==
'notyet') {
905 print
'<input type="button" class="butAction" name="writebookkeeping" value="'.$langs->trans(
"WriteBookKeeping").
'" onclick="writebookkeeping();" />';
907 print
'<a href="#" class="butActionRefused classfortooltip" name="writebookkeeping">'.$langs->trans(
"WriteBookKeeping").
'</a>';
914 <script type="text/javascript">
915 function launch_export() {
916 $("div.fiche form input[name=\"action\"]").val("exportcsv");
917 $("div.fiche form input[type=\"submit\"]").click();
918 $("div.fiche form input[name=\"action\"]").val("");
920 function writebookkeeping() {
921 console.log("click on writebookkeeping");
922 $("div.fiche form input[name=\"action\"]").val("writebookkeeping");
923 $("div.fiche form input[type=\"submit\"]").click();
924 $("div.fiche form input[name=\"action\"]").val("");
933 print
'<div class="div-table-responsive">';
934 print
"<table class=\"noborder\" width=\"100%\">";
935 print
"<tr class=\"liste_titre\">";
936 print
"<td>".$langs->trans(
"Date").
"</td>";
937 print
"<td>".$langs->trans(
"Piece").
' ('.$langs->trans(
"InvoiceRef").
")</td>";
938 print
"<td>".$langs->trans(
"AccountAccounting").
"</td>";
939 print
"<td>".$langs->trans(
"SubledgerAccount").
"</td>";
940 print
"<td>".$langs->trans(
"LabelOperation").
"</td>";
941 print
'<td class="center">'.$langs->trans(
"AccountingDebit").
"</td>";
942 print
'<td class="center">'.$langs->trans(
"AccountingCredit").
"</td>";
950 foreach ($tabfac as $key => $val) {
951 $companystatic->id = $tabcompany[$key][
'id'];
952 $companystatic->name = $tabcompany[$key][
'name'];
953 $companystatic->code_compta_fournisseur = $tabcompany[$key][
'code_compta_fournisseur'];
954 $companystatic->code_fournisseur = $tabcompany[$key][
'code_fournisseur'];
955 $companystatic->fournisseur = 1;
957 $invoicestatic->id = $key;
958 $invoicestatic->ref = $val[
"refsologest"];
959 $invoicestatic->ref_supplier = $val[
"refsuppliersologest"];
960 $invoicestatic->type = $val[
"type"];
961 $invoicestatic->description =
dol_trunc(html_entity_decode($val[
"description"]), 32);
962 $invoicestatic->close_code = $val[
"close_code"];
967 $replacedinvoice = 0;
968 if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED) {
969 $replacedinvoice = 1;
970 $alreadydispatched = $invoicestatic->getVentilExportCompta();
971 if ($alreadydispatched) {
972 $replacedinvoice = 2;
977 if ($replacedinvoice == 1) {
978 print
'<tr class="oddeven">';
979 print
"<!-- Replaced invoice -->";
980 print
"<td>".$date.
"</td>";
981 print
"<td><strike>".$invoicestatic->getNomUrl(1).
"</strike></td>";
984 print $langs->trans(
"Replaced");
991 print
'<td class="right"></td>';
992 print
'<td class="right"></td>';
998 if ($errorforinvoice[$key] ==
'somelinesarenotbound') {
999 print
'<tr class="oddeven">';
1000 print
"<!-- Some lines are not bound -->";
1001 print
"<td>".$date.
"</td>";
1002 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
1005 print
'<span class="error">'.$langs->trans(
'ErrorInvoiceContainsLinesNotYetBoundedShort', $val[
'ref']).
'</span>';
1012 print
'<td class="right"></td>';
1013 print
'<td class="right"></td>';
1020 foreach ($tabttc[$key] as $k => $mt) {
1021 print
'<tr class="oddeven">';
1022 print
"<!-- Thirdparty -->";
1023 print
"<td>".$date.
"</td>";
1024 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
1028 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
1029 print
'<span class="error">'.$langs->trans(
"MainAccountForSuppliersNotDefined").
'</span>';
1031 print $accountoshow;
1037 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
1038 print
'<span class="error">'.$langs->trans(
"ThirdpartyAccountNotDefined").
'</span>';
1040 print $accountoshow;
1043 print
"<td>".$companystatic->getNomUrl(0,
'supplier', 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"SubledgerAccount").
"</td>";
1044 print
'<td class="right nowraponall amount">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
1045 print
'<td class="right nowraponall amount">'.($mt >= 0 ?
price($mt) :
'').
"</td>";
1052 foreach ($tabht[$key] as $k => $mt) {
1054 $accountingaccount->fetch(
null, $k,
true);
1056 print
'<tr class="oddeven">';
1057 print
"<!-- Product -->";
1058 print
"<td>".$date.
"</td>";
1059 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
1063 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
1064 print
'<span class="error">'.$langs->trans(
"ProductAccountNotDefined").
'</span>';
1066 print $accountoshow;
1075 } elseif (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
1076 print
'<span class="error">' . $langs->trans(
"ThirdpartyAccountNotDefined") .
'</span>';
1079 $companystatic->id = $tabcompany[$key][
'id'];
1080 $companystatic->name = $tabcompany[$key][
'name'];
1081 print
"<td>".$companystatic->getNomUrl(0,
'supplier', 16).
' - '.$invoicestatic->ref_supplier.
' - '.$accountingaccount->label.
"</td>";
1082 print
'<td class="right nowraponall amount">'.($mt >= 0 ?
price($mt) :
'').
"</td>";
1083 print
'<td class="right nowraponall amount">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
1090 $listoftax = array(0, 1, 2);
1091 foreach ($listoftax as $numtax) {
1092 $arrayofvat = $tabtva;
1094 $arrayofvat = $tablocaltax1;
1097 $arrayofvat = $tablocaltax2;
1101 if ($mysoc->country_code ==
'FR' ||
getDolGlobalString(
'ACCOUNTING_FORCE_ENABLE_VAT_REVERSE_CHARGE')) {
1103 foreach ($arrayofvat[$key] as $k => $mt) {
1110 $arrayofvat = $tabrctva;
1112 $arrayofvat = $tabrclocaltax1;
1115 $arrayofvat = $tabrclocaltax2;
1117 if (!is_array($arrayofvat[$key])) {
1118 $arrayofvat[$key] = array();
1123 foreach ($arrayofvat[$key] as $k => $mt) {
1125 print
'<tr class="oddeven">';
1126 print
"<!-- VAT -->";
1127 print
"<td>".$date.
"</td>";
1128 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
1132 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
1133 print
'<span class="error">'.$langs->trans(
"VATAccountNotDefined").
' ('.$langs->trans(
"Purchase").
')</span>';
1135 print $accountoshow;
1142 print $companystatic->getNomUrl(0,
'supplier', 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"VAT").
' '.join(
', ', $def_tva[$key][$k]).
' %'.($numtax ?
' - Localtax '.$numtax :
'');
1144 print
'<td class="right nowraponall amount">'.($mt >= 0 ?
price($mt) :
'').
"</td>";
1145 print
'<td class="right nowraponall amount">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
1154 if (is_array($tabother[$key])) {
1155 foreach ($tabother[$key] as $k => $mt) {
1157 print
'<tr class="oddeven">';
1158 print
'<!-- VAT counterpart NPR -->';
1159 print
"<td>".$date.
"</td>";
1160 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
1164 if ($accountoshow ==
'' || $accountoshow ==
'NotDefined') {
1165 print
'<span class="error">'.$langs->trans(
"VATAccountNotDefined").
' ('.$langs->trans(
"NPR counterpart").
'). Set ACCOUNTING_COUNTERPART_VAT_NPR to the subvention account</span>';
1167 print $accountoshow;
1173 print
"<td>".$companystatic->getNomUrl(0,
'supplier', 16).
' - '.$invoicestatic->ref_supplier.
' - '.$langs->trans(
"VAT").
" NPR (counterpart)</td>";
1174 print
'<td class="right nowraponall amount">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
1175 print
'<td class="right nowraponall amount">'.($mt >= 0 ?
price($mt) :
'').
"</td>";
1185 print
'<tr class="oddeven"><td colspan="7"><span class="opacitymedium">'.$langs->trans(
"NoRecordFound").
'</span></td></tr>';