34 require
'../../main.inc.php';
35 require_once DOL_DOCUMENT_ROOT.
'/core/lib/report.lib.php';
36 require_once DOL_DOCUMENT_ROOT.
'/core/lib/date.lib.php';
37 require_once DOL_DOCUMENT_ROOT.
'/core/lib/accounting.lib.php';
38 require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/accountingjournal.class.php';
39 require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/accountingaccount.class.php';
40 require_once DOL_DOCUMENT_ROOT.
'/compta/facture/class/facture.class.php';
41 require_once DOL_DOCUMENT_ROOT.
'/societe/class/client.class.php';
42 require_once DOL_DOCUMENT_ROOT.
'/accountancy/class/bookkeeping.class.php';
45 $langs->loadLangs(array(
"commercial",
"compta",
"bills",
"other",
"accountancy",
"errors"));
47 $id_journal =
GETPOST(
'id_journal',
'int');
48 $action =
GETPOST(
'action',
'aZ09');
50 $date_startmonth =
GETPOST(
'date_startmonth');
51 $date_startday =
GETPOST(
'date_startday');
52 $date_startyear =
GETPOST(
'date_startyear');
53 $date_endmonth =
GETPOST(
'date_endmonth');
54 $date_endday =
GETPOST(
'date_endday');
55 $date_endyear =
GETPOST(
'date_endyear');
56 $in_bookkeeping =
GETPOST(
'in_bookkeeping');
57 if ($in_bookkeeping ==
'') {
58 $in_bookkeeping =
'notyet';
63 $hookmanager->initHooks(array(
'sellsjournal'));
64 $parameters = array();
70 if ($user->socid > 0) {
73 if (empty($user->rights->accounting->mouvements->lire)) {
82 $reshook = $hookmanager->executeHooks(
'doActions', $parameters, $user, $action);
88 $accountingjournalstatic->fetch($id_journal);
89 $journal = $accountingjournalstatic->code;
90 $journal_label = $accountingjournalstatic->label;
92 $date_start =
dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
93 $date_end =
dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
95 if (empty($date_startmonth) || empty($date_endmonth)) {
98 $date_start = $dates[
'date_start'];
99 $date_end = $dates[
'date_end'];
100 $pastmonthyear = $dates[
'pastmonthyear'];
101 $pastmonth = $dates[
'pastmonth'];
104 if (!
GETPOSTISSET(
'date_startmonth') && (empty($date_start) || empty($date_end))) {
109 $sql =
"SELECT f.rowid, f.ref, f.type, f.datef as df, f.ref_client, f.date_lim_reglement as dlr, f.close_code, f.retained_warranty,";
110 $sql .=
" fd.rowid as fdid, fd.description, fd.product_type, fd.total_ht, fd.total_tva, fd.total_localtax1, fd.total_localtax2, fd.tva_tx, fd.total_ttc, fd.situation_percent, fd.vat_src_code,";
111 $sql .=
" s.rowid as socid, s.nom as name, s.code_client, s.code_fournisseur,";
112 if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
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,";
119 $sql .=
" p.rowid as pid, p.ref as pref, aa.rowid as fk_compte, aa.account_number as compte, aa.label as label_compte,";
120 if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
121 $sql .=
" ppe.accountancy_code_sell";
123 $sql .=
" p.accountancy_code_sell";
125 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facturedet as fd";
126 $sql .=
" LEFT JOIN ".MAIN_DB_PREFIX.
"product as p ON p.rowid = fd.fk_product";
127 if (!empty($conf->global->MAIN_PRODUCT_PERENTITY_SHARED)) {
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 as f ON f.rowid = fd.fk_facture";
132 $sql .=
" JOIN ".MAIN_DB_PREFIX.
"societe as s ON s.rowid = f.fk_soc";
133 if (!empty($conf->global->MAIN_COMPANY_PERENTITY_SHARED)) {
134 $sql .=
" LEFT JOIN " . MAIN_DB_PREFIX .
"societe_perentity as spe ON spe.fk_soc = s.rowid AND spe.entity = " . ((int) $conf->entity);
136 $sql .=
" WHERE fd.fk_code_ventilation > 0";
137 $sql .=
" AND f.entity IN (".getEntity(
'invoice', 0).
')';
138 $sql .=
" AND f.fk_statut > 0";
139 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
144 $sql .=
" AND fd.product_type IN (0,1)";
145 if ($date_start && $date_end) {
146 $sql .=
" AND f.datef >= '".$db->idate($date_start).
"' AND f.datef <= '".$db->idate($date_end).
"'";
149 if (!empty($conf->global->ACCOUNTING_DATE_START_BINDING)) {
150 $sql .=
" AND f.datef >= '".$db->idate($conf->global->ACCOUNTING_DATE_START_BINDING).
"'";
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='customer_invoice')";
157 if ($in_bookkeeping ==
'notyet') {
158 $sql .=
" AND f.rowid NOT IN (SELECT fk_doc FROM ".MAIN_DB_PREFIX.
"accounting_bookkeeping as ab WHERE ab.doc_type='customer_invoice')";
161 $sql .=
" ORDER BY f.datef";
164 dol_syslog(
'accountancy/journal/sellsjournal.php', LOG_DEBUG);
165 $result = $db->query(
$sql);
171 $tabwarranty = array();
173 $tablocaltax1 = array();
174 $tablocaltax2 = array();
175 $tabcompany = array();
177 $num = $db->num_rows($result);
180 $cptcli = (($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER !=
"")) ? $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER :
'NotDefined';
181 $cpttva = (!empty($conf->global->ACCOUNTING_VAT_SOLD_ACCOUNT)) ? $conf->global->ACCOUNTING_VAT_SOLD_ACCOUNT :
'NotDefined';
185 $obj = $db->fetch_object($result);
188 $compta_soc = (!empty($obj->code_compta)) ? $obj->code_compta : $cptcli;
190 $compta_prod = $obj->compte;
191 if (empty($compta_prod)) {
192 if ($obj->product_type == 0) {
193 $compta_prod = (!empty($conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT)) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT :
'NotDefined';
195 $compta_prod = (!empty($conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT)) ? $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT :
'NotDefined';
199 $vatdata =
getTaxesFromId($obj->tva_tx.($obj->vat_src_code ?
' ('.$obj->vat_src_code.
')' :
''), $mysoc, $mysoc, 0);
200 $compta_tva = (!empty($vatdata[
'accountancy_code_sell']) ? $vatdata[
'accountancy_code_sell'] : $cpttva);
201 $compta_localtax1 = (!empty($vatdata[
'accountancy_code_sell']) ? $vatdata[
'accountancy_code_sell'] : $cpttva);
202 $compta_localtax2 = (!empty($vatdata[
'accountancy_code_sell']) ? $vatdata[
'accountancy_code_sell'] : $cpttva);
205 if (
price2num($obj->tva_tx) || !empty($obj->vat_src_code)) {
206 $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.
')' :
''));
210 $line->fetch($obj->fdid);
213 $prev_progress = $line->get_prev_progress($obj->rowid);
217 if ($obj->situation_percent == 0) {
218 $situation_ratio = 0;
220 $situation_ratio = ($obj->situation_percent - $prev_progress) / $obj->situation_percent;
223 $situation_ratio = 1;
227 $tabfac[$obj->rowid][
"date"] = $db->jdate($obj->df);
228 $tabfac[$obj->rowid][
"datereg"] = $db->jdate($obj->dlr);
229 $tabfac[$obj->rowid][
"ref"] = $obj->ref;
230 $tabfac[$obj->rowid][
"type"] = $obj->type;
231 $tabfac[$obj->rowid][
"description"] = $obj->label_compte;
232 $tabfac[$obj->rowid][
"close_code"] = $obj->close_code;
236 if (!isset($tabttc[$obj->rowid][$compta_soc])) {
237 $tabttc[$obj->rowid][$compta_soc] = 0;
239 if (!isset($tabht[$obj->rowid][$compta_prod])) {
240 $tabht[$obj->rowid][$compta_prod] = 0;
242 if (!isset($tabtva[$obj->rowid][$compta_tva])) {
243 $tabtva[$obj->rowid][$compta_tva] = 0;
245 if (!isset($tablocaltax1[$obj->rowid][$compta_localtax1])) {
246 $tablocaltax1[$obj->rowid][$compta_localtax1] = 0;
248 if (!isset($tablocaltax2[$obj->rowid][$compta_localtax2])) {
249 $tablocaltax2[$obj->rowid][$compta_localtax2] = 0;
252 $total_ttc = $obj->total_ttc * $situation_ratio;
253 if (!empty($conf->global->INVOICE_USE_RETAINED_WARRANTY) && $obj->retained_warranty > 0) {
254 $retained_warranty = (double)
price2num($total_ttc * $obj->retained_warranty / 100,
'MT');
255 $tabwarranty[$obj->rowid][$compta_soc] += $retained_warranty;
256 $total_ttc -= $retained_warranty;
258 $tabttc[$obj->rowid][$compta_soc] += $total_ttc;
259 $tabht[$obj->rowid][$compta_prod] += $obj->total_ht * $situation_ratio;
260 if (empty($line->tva_npr)) {
261 $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva * $situation_ratio;
263 $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1 * $situation_ratio;
264 $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2 * $situation_ratio;
265 $tabcompany[$obj->rowid] = array(
267 'name' => $obj->name,
268 'code_client' => $obj->code_client,
269 'code_compta' => $compta_soc
278 $errorforinvoice = array();
281 foreach ($tabfac as $key => $val) {
282 $sql =
"SELECT COUNT(fd.rowid) as nb";
283 $sql .=
" FROM ".MAIN_DB_PREFIX.
"facturedet as fd";
284 $sql .=
" WHERE fd.product_type <= 2 AND fd.fk_code_ventilation <= 0";
285 $sql .=
" AND fd.total_ttc <> 0 AND fk_facture = ".((int) $key);
286 $resql = $db->query(
$sql);
288 $obj = $db->fetch_object($resql);
290 $errorforinvoice[$key] =
'somelinesarenotbound';
300 if ($action ==
'writebookkeeping') {
304 $companystatic =
new Societe($db);
305 $invoicestatic =
new Facture($db);
308 $accountingaccountcustomer->fetch(
null, $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER,
true);
312 $accountingaccountcustomerwarranty->fetch(
null, $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER_RETAINED_WARRANTY,
true);
314 foreach ($tabfac as $key => $val) {
322 $companystatic->id = $tabcompany[$key][
'id'];
323 $companystatic->name = $tabcompany[$key][
'name'];
324 $companystatic->code_compta = $tabcompany[$key][
'code_compta'];
325 $companystatic->code_client = $tabcompany[$key][
'code_client'];
326 $companystatic->client = 3;
328 $invoicestatic->id = $key;
329 $invoicestatic->ref = (string) $val[
"ref"];
330 $invoicestatic->type = $val[
"type"];
331 $invoicestatic->close_code = $val[
"close_code"];
336 $replacedinvoice = 0;
337 if ($invoicestatic->close_code == Facture::CLOSECODE_REPLACED) {
338 $replacedinvoice = 1;
339 $alreadydispatched = $invoicestatic->getVentilExportCompta();
340 if ($alreadydispatched) {
341 $replacedinvoice = 2;
346 if ($replacedinvoice == 1) {
352 if ($errorforinvoice[$key] ==
'somelinesarenotbound') {
355 setEventMessages($langs->trans(
'ErrorInvoiceContainsLinesNotYetBounded', $val[
'ref']),
null,
'errors');
359 if (!$errorforline) {
360 foreach ($tabwarranty[$key] as $k => $mt) {
362 $bookkeeping->doc_date = $val[
"date"];
363 $bookkeeping->date_lim_reglement = $val[
"datereg"];
364 $bookkeeping->doc_ref = $val[
"ref"];
365 $bookkeeping->date_creation = $now;
366 $bookkeeping->doc_type =
'customer_invoice';
367 $bookkeeping->fk_doc = $key;
368 $bookkeeping->fk_docdet = 0;
369 $bookkeeping->thirdparty_code = $companystatic->code_client;
371 $bookkeeping->subledger_account = $tabcompany[$key][
'code_compta'];
372 $bookkeeping->subledger_label = $tabcompany[$key][
'name'];
374 $bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER_RETAINED_WARRANTY;
375 $bookkeeping->label_compte = $accountingaccountcustomerwarranty->label;
377 $bookkeeping->label_operation =
dol_trunc($companystatic->name, 16).
' - '.$invoicestatic->ref.
' - '.$langs->trans(
"Retainedwarranty");
378 $bookkeeping->montant = $mt;
379 $bookkeeping->sens = ($mt >= 0) ?
'D' :
'C';
380 $bookkeeping->debit = ($mt >= 0) ? $mt : 0;
381 $bookkeeping->credit = ($mt < 0) ? -$mt : 0;
382 $bookkeeping->code_journal = $journal;
383 $bookkeeping->journal_label = $langs->transnoentities($journal_label);
384 $bookkeeping->fk_user_author = $user->id;
385 $bookkeeping->entity = $conf->entity;
387 $totaldebit += $bookkeeping->debit;
388 $totalcredit += $bookkeeping->credit;
390 $result = $bookkeeping->create($user);
392 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
395 $errorforinvoice[$key] =
'alreadyjournalized';
400 $errorforinvoice[$key] =
'other';
408 if (!$errorforline) {
409 foreach ($tabttc[$key] as $k => $mt) {
411 $bookkeeping->doc_date = $val[
"date"];
412 $bookkeeping->date_lim_reglement = $val[
"datereg"];
413 $bookkeeping->doc_ref = $val[
"ref"];
414 $bookkeeping->date_creation = $now;
415 $bookkeeping->doc_type =
'customer_invoice';
416 $bookkeeping->fk_doc = $key;
417 $bookkeeping->fk_docdet = 0;
418 $bookkeeping->thirdparty_code = $companystatic->code_client;
420 $bookkeeping->subledger_account = $tabcompany[$key][
'code_compta'];
421 $bookkeeping->subledger_label = $tabcompany[$key][
'name'];
423 $bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER;
424 $bookkeeping->label_compte = $accountingaccountcustomer->label;
426 $bookkeeping->label_operation =
dol_trunc($companystatic->name, 16).
' - '.$invoicestatic->ref.
' - '.$langs->trans(
"SubledgerAccount");
427 $bookkeeping->montant = $mt;
428 $bookkeeping->sens = ($mt >= 0) ?
'D' :
'C';
429 $bookkeeping->debit = ($mt >= 0) ? $mt : 0;
430 $bookkeeping->credit = ($mt < 0) ? -$mt : 0;
431 $bookkeeping->code_journal = $journal;
432 $bookkeeping->journal_label = $langs->transnoentities($journal_label);
433 $bookkeeping->fk_user_author = $user->id;
434 $bookkeeping->entity = $conf->entity;
436 $totaldebit += $bookkeeping->debit;
437 $totalcredit += $bookkeeping->credit;
439 $result = $bookkeeping->create($user);
441 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
444 $errorforinvoice[$key] =
'alreadyjournalized';
449 $errorforinvoice[$key] =
'other';
454 require_once DOL_DOCUMENT_ROOT .
'/accountancy/class/lettering.class.php';
456 $nb_lettering = $lettering_static->bookkeepingLettering(array($bookkeeping->id));
463 if (!$errorforline) {
464 foreach ($tabht[$key] as $k => $mt) {
465 $resultfetch = $accountingaccount->fetch(
null, $k,
true);
466 $label_account = $accountingaccount->label;
469 if ($resultfetch > 0) {
471 $bookkeeping->doc_date = $val[
"date"];
472 $bookkeeping->date_lim_reglement = $val[
"datereg"];
473 $bookkeeping->doc_ref = $val[
"ref"];
474 $bookkeeping->date_creation = $now;
475 $bookkeeping->doc_type =
'customer_invoice';
476 $bookkeeping->fk_doc = $key;
477 $bookkeeping->fk_docdet = 0;
478 $bookkeeping->thirdparty_code = $companystatic->code_client;
480 if (!empty($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER_USE_AUXILIARY_ON_DEPOSIT)) {
482 $bookkeeping->subledger_account = $tabcompany[$key][
'code_compta'];
483 $bookkeeping->subledger_label = $tabcompany[$key][
'name'];
485 $bookkeeping->subledger_account =
'';
486 $bookkeeping->subledger_label =
'';
489 $bookkeeping->subledger_account =
'';
490 $bookkeeping->subledger_label =
'';
493 $bookkeeping->numero_compte = $k;
494 $bookkeeping->label_compte = $label_account;
496 $bookkeeping->label_operation =
dol_trunc($companystatic->name, 16).
' - '.$invoicestatic->ref.
' - '.$label_account;
497 $bookkeeping->montant = $mt;
498 $bookkeeping->sens = ($mt < 0) ?
'D' :
'C';
499 $bookkeeping->debit = ($mt < 0) ? -$mt : 0;
500 $bookkeeping->credit = ($mt >= 0) ? $mt : 0;
501 $bookkeeping->code_journal = $journal;
502 $bookkeeping->journal_label = $langs->transnoentities($journal_label);
503 $bookkeeping->fk_user_author = $user->id;
504 $bookkeeping->entity = $conf->entity;
506 $totaldebit += $bookkeeping->debit;
507 $totalcredit += $bookkeeping->credit;
509 $result = $bookkeeping->create($user);
511 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
514 $errorforinvoice[$key] =
'alreadyjournalized';
519 $errorforinvoice[$key] =
'other';
528 if (!$errorforline) {
529 $listoftax = array(0, 1, 2);
530 foreach ($listoftax as $numtax) {
531 $arrayofvat = $tabtva;
533 $arrayofvat = $tablocaltax1;
536 $arrayofvat = $tablocaltax2;
539 foreach ($arrayofvat[$key] as $k => $mt) {
541 $accountingaccount->fetch(
null, $k,
true);
542 $label_account = $accountingaccount->label;
545 $bookkeeping->doc_date = $val[
"date"];
546 $bookkeeping->date_lim_reglement = $val[
"datereg"];
547 $bookkeeping->doc_ref = $val[
"ref"];
548 $bookkeeping->date_creation = $now;
549 $bookkeeping->doc_type =
'customer_invoice';
550 $bookkeeping->fk_doc = $key;
551 $bookkeeping->fk_docdet = 0;
552 $bookkeeping->thirdparty_code = $companystatic->code_client;
554 $bookkeeping->subledger_account =
'';
555 $bookkeeping->subledger_label =
'';
557 $bookkeeping->numero_compte = $k;
558 $bookkeeping->label_compte = $label_account;
560 $bookkeeping->label_operation =
dol_trunc($companystatic->name, 16).
' - '.$invoicestatic->ref.
' - '.$langs->trans(
"VAT").
' '.join(
', ', $def_tva[$key][$k]).
' %'.($numtax ?
' - Localtax '.$numtax :
'');
561 $bookkeeping->montant = $mt;
562 $bookkeeping->sens = ($mt < 0) ?
'D' :
'C';
563 $bookkeeping->debit = ($mt < 0) ? -$mt : 0;
564 $bookkeeping->credit = ($mt >= 0) ? $mt : 0;
565 $bookkeeping->code_journal = $journal;
566 $bookkeeping->journal_label = $langs->transnoentities($journal_label);
567 $bookkeeping->fk_user_author = $user->id;
568 $bookkeeping->entity = $conf->entity;
570 $totaldebit += $bookkeeping->debit;
571 $totalcredit += $bookkeeping->credit;
573 $result = $bookkeeping->create($user);
575 if ($bookkeeping->error ==
'BookkeepingRecordAlreadyExists') {
578 $errorforinvoice[$key] =
'alreadyjournalized';
583 $errorforinvoice[$key] =
'other';
593 if (!$errorforline && (
price2num($totaldebit,
'MT') !=
price2num($totalcredit,
'MT'))) {
596 $errorforinvoice[$key] =
'amountsnotbalanced';
597 setEventMessages(
'Try to insert a non balanced transaction in book for '.$invoicestatic->ref.
'. Canceled. Surely a bug.',
null,
'errors');
600 if (!$errorforline) {
606 setEventMessages($langs->trans(
"ErrorTooManyErrorsProcessStopped"),
null,
'errors');
614 if (empty($error) && count($tabpay) > 0) {
616 } elseif (count($tabpay) == $error) {
619 setEventMessages($langs->trans(
"GeneralLedgerSomeRecordWasNotRecorded"),
null,
'warnings');
625 if (count($tabpay) != $error) {
626 $param =
'id_journal='.$id_journal;
627 $param .=
'&date_startday='.$date_startday;
628 $param .=
'&date_startmonth='.$date_startmonth;
629 $param .=
'&date_startyear='.$date_startyear;
630 $param .=
'&date_endday='.$date_endday;
631 $param .=
'&date_endmonth='.$date_endmonth;
632 $param .=
'&date_endyear='.$date_endyear;
633 $param .=
'&in_bookkeeping='.$in_bookkeeping;
634 header(
"Location: ".$_SERVER[
'PHP_SELF'].($param ?
'?'.$param :
''));
648 if ($action ==
'exportcsv') {
649 $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
651 $filename =
'journal';
652 $type_export =
'journal';
653 include DOL_DOCUMENT_ROOT.
'/accountancy/tpl/export_journal.tpl.php';
655 $companystatic =
new Client($db);
656 $invoicestatic =
new Facture($db);
658 foreach ($tabfac as $key => $val) {
659 $companystatic->id = $tabcompany[$key][
'id'];
660 $companystatic->name = $tabcompany[$key][
'name'];
661 $companystatic->code_compta = $tabcompany[$key][
'code_compta'];
662 $companystatic->code_client = $tabcompany[$key][
'code_client'];
663 $companystatic->client = 3;
665 $invoicestatic->id = $key;
666 $invoicestatic->ref = (string) $val[
"ref"];
667 $invoicestatic->type = $val[
"type"];
668 $invoicestatic->close_code = $val[
"close_code"];
673 $replacedinvoice = 0;
674 if ($invoicestatic->close_code == Facture::CLOSECODE_REPLACED) {
675 $replacedinvoice = 1;
676 $alreadydispatched = $invoicestatic->getVentilExportCompta();
677 if ($alreadydispatched) {
678 $replacedinvoice = 2;
683 if ($replacedinvoice == 1) {
688 foreach ($tabwarranty[$key] as $k => $mt) {
690 print
'"'.$key.
'"'.$sep;
691 print
'"'.$date.
'"'.$sep;
692 print
'"'.$val[
"ref"].
'"'.$sep;
693 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 32)).
'"'.$sep;
694 print
'"'.length_accounta(html_entity_decode($k)).
'"'.$sep;
695 print
'"'.length_accountg($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER_RETAINED_WARRANTY).
'"'.$sep;
696 print
'"'.length_accounta(html_entity_decode($k)).
'"'.$sep;
697 print
'"'.$langs->trans(
"Thirdparty").
'"'.$sep;
698 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 16)).
' - '.$invoicestatic->ref.
' - '.$langs->trans(
"Retainedwarranty").
'"'.$sep;
699 print
'"'.($mt >= 0 ?
price($mt) :
'').
'"'.$sep;
700 print
'"'.($mt < 0 ?
price(-$mt) :
'').
'"'.$sep;
701 print
'"'.$journal.
'"';
707 foreach ($tabttc[$key] as $k => $mt) {
709 print
'"'.$key.
'"'.$sep;
710 print
'"'.$date.
'"'.$sep;
711 print
'"'.$val[
"ref"].
'"'.$sep;
712 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 32)).
'"'.$sep;
713 print
'"'.length_accounta(html_entity_decode($k)).
'"'.$sep;
714 print
'"'.length_accountg($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER).
'"'.$sep;
715 print
'"'.length_accounta(html_entity_decode($k)).
'"'.$sep;
716 print
'"'.$langs->trans(
"Thirdparty").
'"'.$sep;
717 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 16)).
' - '.$invoicestatic->ref.
' - '.$langs->trans(
"Thirdparty").
'"'.$sep;
718 print
'"'.($mt >= 0 ?
price($mt) :
'').
'"'.$sep;
719 print
'"'.($mt < 0 ?
price(-$mt) :
'').
'"'.$sep;
720 print
'"'.$journal.
'"';
726 foreach ($tabht[$key] as $k => $mt) {
728 $accountingaccount->fetch(
null, $k,
true);
730 print
'"'.$key.
'"'.$sep;
731 print
'"'.$date.
'"'.$sep;
732 print
'"'.$val[
"ref"].
'"'.$sep;
733 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 32)).
'"'.$sep;
734 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
735 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
737 print
'"'.utf8_decode(
dol_trunc($accountingaccount->label, 32)).
'"'.$sep;
738 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 16)).
' - '.
dol_trunc($accountingaccount->label, 32).
'"'.$sep;
739 print
'"'.($mt < 0 ?
price(-$mt) :
'').
'"'.$sep;
740 print
'"'.($mt >= 0 ?
price($mt) :
'').
'"'.$sep;
741 print
'"'.$journal.
'"';
747 $listoftax = array(0, 1, 2);
748 foreach ($listoftax as $numtax) {
749 $arrayofvat = $tabtva;
751 $arrayofvat = $tablocaltax1;
754 $arrayofvat = $tablocaltax2;
757 foreach ($arrayofvat[$key] as $k => $mt) {
759 print
'"'.$key.
'"'.$sep;
760 print
'"'.$date.
'"'.$sep;
761 print
'"'.$val[
"ref"].
'"'.$sep;
762 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 32)).
'"'.$sep;
763 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
764 print
'"'.length_accountg(html_entity_decode($k)).
'"'.$sep;
766 print
'"'.$langs->trans(
"VAT").
' - '.join(
', ', $def_tva[$key][$k]).
' %"'.$sep;
767 print
'"'.utf8_decode(
dol_trunc($companystatic->name, 16)).
' - '.$invoicestatic->ref.
' - '.$langs->trans(
"VAT").join(
', ', $def_tva[$key][$k]).
' %'.($numtax ?
' - Localtax '.$numtax :
'').
'"'.$sep;
768 print
'"'.($mt < 0 ?
price(-$mt) :
'').
'"'.$sep;
769 print
'"'.($mt >= 0 ?
price($mt) :
'').
'"'.$sep;
770 print
'"'.$journal.
'"';
780 if (empty($action) || $action ==
'view') {
781 $title = $langs->trans(
"GenerationOfAccountingEntries").
' - '.$accountingjournalstatic->getNomUrl(0, 2, 1,
'', 1);
790 $description = $langs->trans(
"DescJournalOnlyBindedVisible").
'<br>';
791 if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
792 $description .= $langs->trans(
"DepositsAreNotIncluded");
794 $description .= $langs->trans(
"DepositsAreIncluded");
797 $listofchoices = array(
'notyet'=>$langs->trans(
"NotYetInGeneralLedger"),
'already'=>$langs->trans(
"AlreadyInGeneralLedger"));
798 $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);
799 $period .=
' - '.$langs->trans(
"JournalizationInLedgerStatus").
' '.
$form->selectarray(
'in_bookkeeping', $listofchoices, $in_bookkeeping, 1);
801 $varlink =
'id_journal='.$id_journal;
803 journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array(
'action' =>
''),
'', $varlink);
806 if (($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER ==
"") || $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER ==
'-1') {
807 print
'<br><div class="warning">'.img_warning().
' '.$langs->trans(
"SomeMandatoryStepsOfSetupWereNotDone");
808 $desc =
' : '.$langs->trans(
"AccountancyAreaDescMisc", 4,
'{link}');
809 $desc = str_replace(
'{link}',
'<strong>'.$langs->transnoentitiesnoconv(
"MenuAccountancy").
'-'.$langs->transnoentitiesnoconv(
"Setup").
"-".$langs->transnoentitiesnoconv(
"MenuDefaultAccounts").
'</strong>', $desc);
813 print
'<div class="tabsAction tabsActionNoBottom centerimp">';
814 if (!empty($conf->global->ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL) && $in_bookkeeping ==
'notyet') {
815 print
'<input type="button" class="butAction" name="exportcsv" value="'.$langs->trans(
"ExportDraftJournal").
'" onclick="launch_export();" />';
817 if (($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER ==
"") || $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER ==
'-1') {
818 print
'<input type="button" class="butActionRefused classfortooltip" title="'.dol_escape_htmltag($langs->trans(
"SomeMandatoryStepsOfSetupWereNotDone")).
'" value="'.$langs->trans(
"WriteBookKeeping").
'" />';
820 if ($in_bookkeeping ==
'notyet') {
821 print
'<input type="button" class="butAction" name="writebookkeeping" value="'.$langs->trans(
"WriteBookKeeping").
'" onclick="writebookkeeping();" />';
823 print
'<a href="#" class="butActionRefused classfortooltip" name="writebookkeeping">'.$langs->trans(
"WriteBookKeeping").
'</a>';
830 <script type="text/javascript">
831 function launch_export() {
832 $("div.fiche form input[name=\"action\"]").val("exportcsv");
833 $("div.fiche form input[type=\"submit\"]").click();
834 $("div.fiche form input[name=\"action\"]").val("");
836 function writebookkeeping() {
837 console.log("click on writebookkeeping");
838 $("div.fiche form input[name=\"action\"]").val("writebookkeeping");
839 $("div.fiche form input[type=\"submit\"]").click();
840 $("div.fiche form input[name=\"action\"]").val("");
850 print
'<div class="div-table-responsive">';
851 print
"<table class=\"noborder\" width=\"100%\">";
852 print
"<tr class=\"liste_titre\">";
853 print
"<td>".$langs->trans(
"Date").
"</td>";
854 print
"<td>".$langs->trans(
"Piece").
' ('.$langs->trans(
"InvoiceRef").
")</td>";
855 print
"<td>".$langs->trans(
"AccountAccounting").
"</td>";
856 print
"<td>".$langs->trans(
"SubledgerAccount").
"</td>";
857 print
"<td>".$langs->trans(
"LabelOperation").
"</td>";
858 print
'<td class="center">'.$langs->trans(
"AccountingDebit").
"</td>";
859 print
'<td class="center">'.$langs->trans(
"AccountingCredit").
"</td>";
864 $companystatic =
new Client($db);
865 $invoicestatic =
new Facture($db);
867 foreach ($tabfac as $key => $val) {
868 $companystatic->id = $tabcompany[$key][
'id'];
869 $companystatic->name = $tabcompany[$key][
'name'];
870 $companystatic->code_compta = $tabcompany[$key][
'code_compta'];
871 $companystatic->code_client = $tabcompany[$key][
'code_client'];
872 $companystatic->client = 3;
874 $invoicestatic->id = $key;
875 $invoicestatic->ref = (string) $val[
"ref"];
876 $invoicestatic->type = $val[
"type"];
877 $invoicestatic->close_code = $val[
"close_code"];
882 $replacedinvoice = 0;
883 if ($invoicestatic->close_code == Facture::CLOSECODE_REPLACED) {
884 $replacedinvoice = 1;
885 $alreadydispatched = $invoicestatic->getVentilExportCompta();
886 if ($alreadydispatched) {
887 $replacedinvoice = 2;
892 if ($replacedinvoice == 1) {
893 print
'<tr class="oddeven">';
894 print
"<!-- Replaced invoice -->";
895 print
"<td>".$date.
"</td>";
896 print
"<td><strike>".$invoicestatic->getNomUrl(1).
"</strike></td>";
899 print $langs->trans(
"Replaced");
906 print
'<td class="right"></td>';
907 print
'<td class="right"></td>';
912 if ($errorforinvoice[$key] ==
'somelinesarenotbound') {
913 print
'<tr class="oddeven">';
914 print
"<!-- Some lines are not bound -->";
915 print
"<td>".$date.
"</td>";
916 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
919 print
'<span class="error">'.$langs->trans(
'ErrorInvoiceContainsLinesNotYetBoundedShort', $val[
'ref']).
'</span>';
926 print
'<td class="right"></td>';
927 print
'<td class="right"></td>';
932 foreach ($tabwarranty[$key] as $k => $mt) {
933 print
'<tr class="oddeven">';
934 print
"<!-- Thirdparty warranty -->";
935 print
"<td>".$date.
"</td>";
936 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
939 $accountoshow =
length_accountg($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER_RETAINED_WARRANTY);
940 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
941 print
'<span class="error">'.$langs->trans(
"MainAccountForCustomersNotDefined").
'</span>';
949 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
950 print
'<span class="error">'.$langs->trans(
"ThirdpartyAccountNotDefined").
'</span>';
955 print
"<td>".$companystatic->getNomUrl(0,
'customer', 16).
' - '.$invoicestatic->ref.
' - '.$langs->trans(
"Retainedwarranty").
"</td>";
956 print
'<td class="right nowraponall amount">'.($mt >= 0 ?
price($mt) :
'').
"</td>";
957 print
'<td class="right nowraponall amount">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
962 foreach ($tabttc[$key] as $k => $mt) {
963 print
'<tr class="oddeven">';
964 print
"<!-- Thirdparty -->";
965 print
"<td>".$date.
"</td>";
966 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
969 $accountoshow =
length_accountg($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER);
970 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
971 print
'<span class="error">'.$langs->trans(
"MainAccountForCustomersNotDefined").
'</span>';
979 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
980 print
'<span class="error">'.$langs->trans(
"ThirdpartyAccountNotDefined").
'</span>';
985 print
"<td>".$companystatic->getNomUrl(0,
'customer', 16).
' - '.$invoicestatic->ref.
' - '.$langs->trans(
"SubledgerAccount").
"</td>";
986 print
'<td class="right nowraponall amount">'.($mt >= 0 ?
price($mt) :
'').
"</td>";
987 print
'<td class="right nowraponall amount">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
992 foreach ($tabht[$key] as $k => $mt) {
994 $accountingaccount->fetch(
null, $k,
true);
996 print
'<tr class="oddeven">';
997 print
"<!-- Product -->";
998 print
"<td>".$date.
"</td>";
999 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
1003 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
1004 print
'<span class="error">'.$langs->trans(
"ProductNotDefined").
'</span>';
1006 print $accountoshow;
1011 if (!empty($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER_USE_AUXILIARY_ON_DEPOSIT)) {
1015 } elseif (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
1016 print
'<span class="error">' . $langs->trans(
"ThirdpartyAccountNotDefined") .
'</span>';
1019 $companystatic->id = $tabcompany[$key][
'id'];
1020 $companystatic->name = $tabcompany[$key][
'name'];
1021 print
"<td>".$companystatic->getNomUrl(0,
'customer', 16).
' - '.$invoicestatic->ref.
' - '.$accountingaccount->label.
"</td>";
1022 print
'<td class="right nowraponall amount">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
1023 print
'<td class="right nowraponall amount">'.($mt >= 0 ?
price($mt) :
'').
"</td>";
1028 $listoftax = array(0, 1, 2);
1029 foreach ($listoftax as $numtax) {
1030 $arrayofvat = $tabtva;
1032 $arrayofvat = $tablocaltax1;
1035 $arrayofvat = $tablocaltax2;
1038 foreach ($arrayofvat[$key] as $k => $mt) {
1040 print
'<tr class="oddeven">';
1041 print
"<!-- VAT -->";
1042 print
"<td>".$date.
"</td>";
1043 print
"<td>".$invoicestatic->getNomUrl(1).
"</td>";
1047 if (($accountoshow ==
"") || $accountoshow ==
'NotDefined') {
1048 print
'<span class="error">'.$langs->trans(
"VATAccountNotDefined").
' ('.$langs->trans(
"Sale").
')</span>';
1050 print $accountoshow;
1056 print
"<td>".$companystatic->getNomUrl(0,
'customer', 16).
' - '.$invoicestatic->ref.
' - '.$langs->trans(
"VAT").
' '.join(
', ', $def_tva[$key][$k]).
' %'.($numtax ?
' - Localtax '.$numtax :
'');
1058 print
'<td class="right nowraponall amount">'.($mt < 0 ?
price(-$mt) :
'').
"</td>";
1059 print
'<td class="right nowraponall amount">'.($mt >= 0 ?
price($mt) :
'').
"</td>";