purchasesjournal.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002
  1. <?php
  2. /* Copyright (C) 2007-2010 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2007-2010 Jean Heimburger <jean@tiaris.info>
  4. * Copyright (C) 2011 Juanjo Menent <jmenent@2byte.es>
  5. * Copyright (C) 2012 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2013-2017 Alexandre Spangaro <aspangaro@open-dsi.fr>
  7. * Copyright (C) 2013-2016 Olivier Geffroy <jeff@jeffinfo.com>
  8. * Copyright (C) 2013-2016 Florian Henry <florian.henry@open-concept.pro>
  9. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  10. * Copyright (C) 2018 Eric Seigne <eric.seigne@cap-rel.fr>
  11. *
  12. * This program is free software; you can redistribute it and/or modify
  13. * it under the terms of the GNU General Public License as published by
  14. * the Free Software Foundation; either version 3 of the License, or
  15. * (at your option) any later version.
  16. *
  17. * This program is distributed in the hope that it will be useful,
  18. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. * GNU General Public License for more details.
  21. *
  22. * You should have received a copy of the GNU General Public License
  23. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  24. */
  25. /**
  26. * \file htdocs/accountancy/journal/purchasesjournal.php
  27. * \ingroup Accountancy (Double entries)
  28. * \brief Page with purchases journal
  29. */
  30. require '../../main.inc.php';
  31. require_once DOL_DOCUMENT_ROOT . '/core/lib/report.lib.php';
  32. require_once DOL_DOCUMENT_ROOT . '/core/lib/date.lib.php';
  33. require_once DOL_DOCUMENT_ROOT . '/core/lib/accounting.lib.php';
  34. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingjournal.class.php';
  35. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingaccount.class.php';
  36. require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.facture.class.php';
  37. require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.class.php';
  38. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/bookkeeping.class.php';
  39. // Load translation files required by the page
  40. $langs->loadLangs(array("commercial", "compta","bills","other","accountancy","errors"));
  41. $id_journal = GETPOST('id_journal', 'int');
  42. $action = GETPOST('action', 'aZ09');
  43. $date_startmonth = GETPOST('date_startmonth');
  44. $date_startday = GETPOST('date_startday');
  45. $date_startyear = GETPOST('date_startyear');
  46. $date_endmonth = GETPOST('date_endmonth');
  47. $date_endday = GETPOST('date_endday');
  48. $date_endyear = GETPOST('date_endyear');
  49. $in_bookkeeping = GETPOST('in_bookkeeping');
  50. if ($in_bookkeeping == '') $in_bookkeeping = 'notyet';
  51. $now = dol_now();
  52. // Security check
  53. if ($user->societe_id > 0)
  54. accessforbidden();
  55. $hookmanager->initHooks(array('purchasesjournal'));
  56. $parameters=array();
  57. /*
  58. * Actions
  59. */
  60. $reshook=$hookmanager->executeHooks('doActions', $parameters, $user, $action); // Note that $action and $object may have been modified by some hooks
  61. $accountingaccount = new AccountingAccount($db);
  62. // Get informations of journal
  63. $accountingjournalstatic = new AccountingJournal($db);
  64. $accountingjournalstatic->fetch($id_journal);
  65. $journal = $accountingjournalstatic->code;
  66. $journal_label = $accountingjournalstatic->label;
  67. $year_current = strftime("%Y", dol_now());
  68. $pastmonth = strftime("%m", dol_now()) - 1;
  69. $pastmonthyear = $year_current;
  70. if ($pastmonth == 0) {
  71. $pastmonth = 12;
  72. $pastmonthyear --;
  73. }
  74. $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
  75. $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
  76. if (! GETPOSTISSET('date_startmonth') && (empty($date_start) || empty($date_end))) // We define date_start and date_end, only if we did not submit the form
  77. {
  78. $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false);
  79. $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
  80. }
  81. $sql = "SELECT f.rowid, f.ref as ref, f.type, f.datef as df, f.libelle,f.ref_supplier, f.date_lim_reglement as dlf, f.close_code,";
  82. $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,";
  83. $sql .= " s.rowid as socid, s.nom as name, s.fournisseur, s.code_client, s.code_fournisseur, s.code_compta, s.code_compta_fournisseur,";
  84. $sql .= " p.accountancy_code_buy , aa.rowid as fk_compte, aa.account_number as compte, aa.label as label_compte";
  85. $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn_det as fd";
  86. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product as p ON p.rowid = fd.fk_product";
  87. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = fd.fk_code_ventilation";
  88. $sql .= " JOIN " . MAIN_DB_PREFIX . "facture_fourn as f ON f.rowid = fd.fk_facture_fourn";
  89. $sql .= " JOIN " . MAIN_DB_PREFIX . "societe as s ON s.rowid = f.fk_soc";
  90. $sql .= " WHERE f.fk_statut > 0";
  91. $sql .= " AND fd.fk_code_ventilation > 0";
  92. $sql .= " AND f.entity IN (" . getEntity('facture_fourn', 0) . ")"; // We don't share object for accountancy
  93. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  94. $sql .= " AND f.type IN (" . FactureFournisseur::TYPE_STANDARD . "," . FactureFournisseur::TYPE_REPLACEMENT . "," . FactureFournisseur::TYPE_CREDIT_NOTE . "," . FactureFournisseur::TYPE_SITUATION . ")";
  95. } else {
  96. $sql .= " AND f.type IN (" . FactureFournisseur::TYPE_STANDARD . "," . FactureFournisseur::TYPE_REPLACEMENT . "," . FactureFournisseur::TYPE_CREDIT_NOTE . "," . FactureFournisseur::TYPE_DEPOSIT . "," . FactureFournisseur::TYPE_SITUATION . ")";
  97. }
  98. if ($date_start && $date_end)
  99. $sql .= " AND f.datef >= '" . $db->idate($date_start) . "' AND f.datef <= '" . $db->idate($date_end) . "'";
  100. // Already in bookkeeping or not
  101. if ($in_bookkeeping == 'already')
  102. {
  103. $sql .= " AND f.rowid IN (SELECT fk_doc FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab WHERE ab.doc_type='supplier_invoice')";
  104. }
  105. if ($in_bookkeeping == 'notyet')
  106. {
  107. $sql .= " AND f.rowid NOT IN (SELECT fk_doc FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab WHERE ab.doc_type='supplier_invoice')";
  108. }
  109. $sql .= " ORDER BY f.datef";
  110. dol_syslog('accountancy/journal/purchasesjournal.php', LOG_DEBUG);
  111. $result = $db->query($sql);
  112. if ($result) {
  113. $tabfac = array ();
  114. $tabht = array ();
  115. $tabtva = array ();
  116. $def_tva = array ();
  117. $tabttc = array ();
  118. $tablocaltax1 = array ();
  119. $tablocaltax2 = array ();
  120. $tabcompany = array ();
  121. $tabother = array();
  122. $num = $db->num_rows($result);
  123. // Variables
  124. $cptfour = ($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER != "") ? $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER : 'NotDefined';
  125. $cpttva = (! empty($conf->global->ACCOUNTING_VAT_BUY_ACCOUNT)) ? $conf->global->ACCOUNTING_VAT_BUY_ACCOUNT : 'NotDefined';
  126. $i = 0;
  127. while ( $i < $num ) {
  128. $obj = $db->fetch_object($result);
  129. // Controls
  130. $compta_soc = ($obj->code_compta_fournisseur != "") ? $obj->code_compta_fournisseur : $cptfour;
  131. $compta_prod = $obj->compte;
  132. if (empty($compta_prod)) {
  133. if ($obj->product_type == 0)
  134. $compta_prod = (! empty($conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT)) ? $conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT : 'NotDefined';
  135. else
  136. $compta_prod = (! empty($conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT)) ? $conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT : 'NotDefined';
  137. }
  138. $vatdata = getTaxesFromId($obj->tva_tx.($obj->vat_src_code?' ('.$obj->vat_src_code.')':''), $mysoc, $mysoc, 0);
  139. $compta_tva = (! empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $cpttva);
  140. $compta_localtax1 = (! empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $cpttva);
  141. $compta_localtax2 = (! empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $cpttva);
  142. $compta_counterpart_tva_npr = (! empty($conf->global->ACCOUNTING_COUNTERPART_VAT_NPR)) ? $conf->global->ACCOUNTING_COUNTERPART_VAT_NPR : 'NotDefined';
  143. // Define array to display all VAT rates that use this accounting account $compta_tva
  144. if (price2num($obj->tva_tx) || ! empty($obj->vat_src_code))
  145. {
  146. $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.')':''));
  147. }
  148. $line = new SupplierInvoiceLine($db);
  149. $line->fetch($obj->fdid);
  150. $tabfac[$obj->rowid]["date"] = $db->jdate($obj->df);
  151. $tabfac[$obj->rowid]["datereg"] = $db->jdate($obj->dlr);
  152. $tabfac[$obj->rowid]["ref"] = $obj->ref_supplier . ' (' . $obj->ref . ')';
  153. $tabfac[$obj->rowid]["refsologest"] = $obj->ref;
  154. $tabfac[$obj->rowid]["refsuppliersologest"] = $obj->ref_supplier;
  155. $tabfac[$obj->rowid]["type"] = $obj->type;
  156. $tabfac[$obj->rowid]["description"] = $obj->description;
  157. $tabfac[$obj->rowid]["close_code"] = $obj->close_code; // close_code = 'replaced' for replacement invoices (not used in most european countries)
  158. //$tabfac[$obj->rowid]["fk_facturefourndet"] = $obj->fdid;
  159. // Avoid warnings
  160. if (! isset($tabttc[$obj->rowid][$compta_soc])) $tabttc[$obj->rowid][$compta_soc] = 0;
  161. if (! isset($tabht[$obj->rowid][$compta_prod])) $tabht[$obj->rowid][$compta_prod] = 0;
  162. if (! isset($tabtva[$obj->rowid][$compta_tva])) $tabtva[$obj->rowid][$compta_tva] = 0;
  163. if (! isset($tablocaltax1[$obj->rowid][$compta_localtax1])) $tablocaltax1[$obj->rowid][$compta_localtax1] = 0;
  164. if (! isset($tablocaltax2[$obj->rowid][$compta_localtax2])) $tablocaltax2[$obj->rowid][$compta_localtax2] = 0;
  165. $tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc;
  166. $tabht[$obj->rowid][$compta_prod] += $obj->total_ht;
  167. $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva;
  168. if (! empty($line->tva_npr)) // Add an entry for counterpart
  169. {
  170. $tabother[$obj->rowid][$compta_counterpart_tva_npr] += $obj->total_tva;
  171. }
  172. $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1;
  173. $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2;
  174. $tabcompany[$obj->rowid] = array (
  175. 'id' => $obj->socid,
  176. 'name' => $obj->name,
  177. 'code_fournisseur' => $obj->code_fournisseur,
  178. 'code_compta_fournisseur' => $compta_soc
  179. );
  180. $i++;
  181. }
  182. } else {
  183. dol_print_error($db);
  184. }
  185. $errorforinvoice = array();
  186. // Loop in invoices to detect lines with not binding lines
  187. foreach ($tabfac as $key => $val) { // Loop on each invoice
  188. $sql = "SELECT COUNT(fd.rowid) as nb";
  189. $sql.= " FROM " . MAIN_DB_PREFIX . "facture_fourn_det as fd";
  190. $sql.= " WHERE fd.product_type <= 2 AND fd.fk_code_ventilation <= 0";
  191. $sql.= " AND fd.total_ttc <> 0 AND fk_facture_fourn = ".$key;
  192. $resql=$db->query($sql);
  193. if ($resql)
  194. {
  195. $obj = $db->fetch_object($resql);
  196. if ($obj->nb > 0)
  197. {
  198. $errorforinvoice[$key]='somelinesarenotbound';
  199. }
  200. }
  201. else dol_print_error($db);
  202. }
  203. //var_dump($errorforinvoice);exit;
  204. // Bookkeeping Write
  205. if ($action == 'writebookkeeping') {
  206. $now = dol_now();
  207. $error = 0;
  208. $companystatic = new Societe($db);
  209. $invoicestatic = new FactureFournisseur($db);
  210. foreach ($tabfac as $key => $val) { // Loop on each invoice
  211. $errorforline = 0;
  212. $totalcredit = 0;
  213. $totaldebit = 0;
  214. $db->begin();
  215. $companystatic->id = $tabcompany[$key]['id'];
  216. $companystatic->name = $tabcompany[$key]['name'];
  217. $companystatic->code_compta = $tabcompany[$key]['code_compta'];
  218. $companystatic->code_compta_fournisseur = $tabcompany[$key]['code_compta_fournisseur'];
  219. $companystatic->code_client = $tabcompany[$key]['code_client'];
  220. $companystatic->code_fournisseur = $tabcompany[$key]['code_fournisseur'];
  221. $companystatic->fournisseur = 1;
  222. $invoicestatic->id = $key;
  223. $invoicestatic->ref = (string) $val["refsologest"];
  224. $invoicestatic->ref_supplier = $val["refsuppliersologest"];
  225. $invoicestatic->type = $val["type"];
  226. $invoicestatic->description = html_entity_decode(dol_trunc($val["description"], 32));
  227. $invoicestatic->close_code = $val["close_code"];
  228. $date = dol_print_date($val["date"], 'day');
  229. // Is it a replaced invoice ? 0=not a replaced invoice, 1=replaced invoice not yet dispatched, 2=replaced invoice dispatched
  230. $replacedinvoice = 0;
  231. if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED)
  232. {
  233. $replacedinvoice = 1;
  234. $alreadydispatched = $invoicestatic->getVentilExportCompta(); // Test if replaced invoice already into bookkeeping.
  235. if ($alreadydispatched) $replacedinvoice = 2;
  236. }
  237. // If not already into bookkeeping, we won't add it. If yes, do nothing (should not happen because creating replacement not possible if invoice is accounted)
  238. if ($replacedinvoice == 1)
  239. {
  240. $db->rollback();
  241. continue;
  242. }
  243. // Error if some lines are not binded/ready to be journalized
  244. if ($errorforinvoice[$key] == 'somelinesarenotbound')
  245. {
  246. $error++;
  247. $errorforline++;
  248. setEventMessages($langs->trans('ErrorInvoiceContainsLinesNotYetBounded', $val['ref']), null, 'errors');
  249. }
  250. // Thirdparty
  251. if (! $errorforline)
  252. {
  253. foreach ($tabttc[$key] as $k => $mt) {
  254. //if ($mt) {
  255. $bookkeeping = new BookKeeping($db);
  256. $bookkeeping->doc_date = $val["date"];
  257. $bookkeeping->date_lim_reglement = $val["datereg"];
  258. $bookkeeping->doc_ref = $val["refsologest"];
  259. $bookkeeping->date_creation = $now;
  260. $bookkeeping->doc_type = 'supplier_invoice';
  261. $bookkeeping->fk_doc = $key;
  262. $bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add
  263. $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
  264. $bookkeeping->subledger_account = $tabcompany[$key]['code_compta_fournisseur'];
  265. $bookkeeping->subledger_label = $tabcompany[$key]['name'];
  266. $bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER;
  267. $accountingaccount->fetch(null, $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER, true);
  268. $bookkeeping->label_compte = $accountingaccount->label;
  269. $bookkeeping->label_operation = dol_trunc($companystatic->name, 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("SubledgerAccount");
  270. $bookkeeping->montant = $mt;
  271. $bookkeeping->sens = ($mt >= 0) ? 'C' : 'D';
  272. $bookkeeping->debit = ($mt <= 0) ? -$mt : 0;
  273. $bookkeeping->credit = ($mt > 0) ? $mt : 0;
  274. $bookkeeping->code_journal = $journal;
  275. $bookkeeping->journal_label = $journal_label;
  276. $bookkeeping->fk_user_author = $user->id;
  277. $bookkeeping->entity = $conf->entity;
  278. $totaldebit += $bookkeeping->debit;
  279. $totalcredit += $bookkeeping->credit;
  280. $result = $bookkeeping->create($user);
  281. if ($result < 0) {
  282. if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
  283. {
  284. $error++;
  285. $errorforline++;
  286. $errorforinvoice[$key]='alreadyjournalized';
  287. //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
  288. }
  289. else
  290. {
  291. $error++;
  292. $errorforline++;
  293. $errorforinvoice[$key]='other';
  294. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  295. }
  296. }
  297. //}
  298. }
  299. }
  300. // Product / Service
  301. if (! $errorforline)
  302. {
  303. foreach ($tabht[$key] as $k => $mt) {
  304. //if ($mt) {
  305. // get compte id and label
  306. if ($accountingaccount->fetch(null, $k, true)) {
  307. $bookkeeping = new BookKeeping($db);
  308. $bookkeeping->doc_date = $val["date"];
  309. $bookkeeping->date_lim_reglement = $val["datereg"];
  310. $bookkeeping->doc_ref = $val["refsologest"];
  311. $bookkeeping->date_creation = $now;
  312. $bookkeeping->doc_type = 'supplier_invoice';
  313. $bookkeeping->fk_doc = $key;
  314. $bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add
  315. $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
  316. $bookkeeping->subledger_account = '';
  317. $bookkeeping->subledger_label = '';
  318. $bookkeeping->numero_compte = $k;
  319. $bookkeeping->label_compte = $accountingaccount->label;
  320. $bookkeeping->label_operation = dol_trunc($companystatic->name, 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $accountingaccount->label;
  321. $bookkeeping->montant = $mt;
  322. $bookkeeping->sens = ($mt < 0) ? 'C' : 'D';
  323. $bookkeeping->debit = ($mt > 0) ? $mt : 0;
  324. $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
  325. $bookkeeping->code_journal = $journal;
  326. $bookkeeping->journal_label = $journal_label;
  327. $bookkeeping->fk_user_author = $user->id;
  328. $bookkeeping->entity = $conf->entity;
  329. $totaldebit += $bookkeeping->debit;
  330. $totalcredit += $bookkeeping->credit;
  331. $result = $bookkeeping->create($user);
  332. if ($result < 0) {
  333. if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
  334. {
  335. $error++;
  336. $errorforline++;
  337. $errorforinvoice[$key]='alreadyjournalized';
  338. //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
  339. }
  340. else
  341. {
  342. $error++;
  343. $errorforline++;
  344. $errorforinvoice[$key]='other';
  345. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  346. }
  347. }
  348. }
  349. //}
  350. }
  351. }
  352. // VAT
  353. // var_dump($tabtva);
  354. if (! $errorforline)
  355. {
  356. $listoftax=array(0, 1, 2);
  357. foreach($listoftax as $numtax)
  358. {
  359. $arrayofvat = $tabtva;
  360. if ($numtax == 1) $arrayofvat = $tablocaltax1;
  361. if ($numtax == 2) $arrayofvat = $tablocaltax2;
  362. foreach ($arrayofvat[$key] as $k => $mt) {
  363. if ($mt) {
  364. $bookkeeping = new BookKeeping($db);
  365. $bookkeeping->doc_date = $val["date"];
  366. $bookkeeping->date_lim_reglement = $val["datereg"];
  367. $bookkeeping->doc_ref = $val["refsologest"];
  368. $bookkeeping->date_creation = $now;
  369. $bookkeeping->doc_type = 'supplier_invoice';
  370. $bookkeeping->fk_doc = $key;
  371. $bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add
  372. $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
  373. $bookkeeping->subledger_account = '';
  374. $bookkeeping->subledger_label = '';
  375. $bookkeeping->numero_compte = $k;
  376. $accountingaccount->fetch($k, null, true);
  377. $bookkeeping->label_compte = $accountingaccount->label;
  378. $bookkeeping->label_operation = dol_trunc($companystatic->name, 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("VAT").' '.join(', ', $def_tva[$key][$k]) .' %' . ($numtax?' - Localtax '.$numtax:'');
  379. $bookkeeping->montant = $mt;
  380. $bookkeeping->sens = ($mt < 0) ? 'C' : 'D';
  381. $bookkeeping->debit = ($mt > 0) ? $mt : 0;
  382. $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
  383. $bookkeeping->code_journal = $journal;
  384. $bookkeeping->journal_label = $journal_label;
  385. $bookkeeping->fk_user_author = $user->id;
  386. $bookkeeping->entity = $conf->entity;
  387. $totaldebit += $bookkeeping->debit;
  388. $totalcredit += $bookkeeping->credit;
  389. $result = $bookkeeping->create($user);
  390. if ($result < 0) {
  391. if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
  392. {
  393. $error++;
  394. $errorforline++;
  395. $errorforinvoice[$key]='alreadyjournalized';
  396. //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
  397. }
  398. else
  399. {
  400. $error++;
  401. $errorforline++;
  402. $errorforinvoice[$key]='other';
  403. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  404. }
  405. }
  406. }
  407. }
  408. }
  409. }
  410. // Counterpart of VAT for VAT NPR
  411. // var_dump($tabother);
  412. if (! $errorforline && is_array($tabother[$key]))
  413. {
  414. foreach ($tabother[$key] as $k => $mt) {
  415. if ($mt) {
  416. $bookkeeping = new BookKeeping($db);
  417. $bookkeeping->doc_date = $val["date"];
  418. $bookkeeping->date_lim_reglement = $val["datereg"];
  419. $bookkeeping->doc_ref = $val["refsologest"];
  420. $bookkeeping->date_creation = $now;
  421. $bookkeeping->doc_type = 'supplier_invoice';
  422. $bookkeeping->fk_doc = $key;
  423. $bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add
  424. $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
  425. $bookkeeping->subledger_account = '';
  426. $bookkeeping->subledger_label = '';
  427. $bookkeeping->numero_compte = $k;
  428. $bookkeeping->label_operation = dol_trunc($companystatic->name, 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("VAT").' NPR';
  429. $bookkeeping->montant = $mt;
  430. $bookkeeping->sens = ($mt < 0) ? 'C' : 'D';
  431. $bookkeeping->debit = ($mt > 0) ? $mt : 0;
  432. $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
  433. $bookkeeping->code_journal = $journal;
  434. $bookkeeping->journal_label = $journal_label;
  435. $bookkeeping->fk_user_author = $user->id;
  436. $bookkeeping->entity = $conf->entity;
  437. $totaldebit += $bookkeeping->debit;
  438. $totalcredit += $bookkeeping->credit;
  439. $result = $bookkeeping->create($user);
  440. if ($result < 0) {
  441. if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
  442. {
  443. $error++;
  444. $errorforline++;
  445. $errorforinvoice[$key]='alreadyjournalized';
  446. //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
  447. }
  448. else
  449. {
  450. $error++;
  451. $errorforline++;
  452. $errorforinvoice[$key]='other';
  453. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  454. }
  455. }
  456. }
  457. }
  458. }
  459. // Protection against a bug on lines before
  460. if (! $errorforline && (price2num($totaldebit) != price2num($totalcredit)))
  461. {
  462. $error++;
  463. $errorforline++;
  464. $errorforinvoice[$key]='amountsnotbalanced';
  465. setEventMessages('Try to insert a non balanced transaction in book for '.$invoicestatic->ref.'. Canceled. Surely a bug.', null, 'errors');
  466. }
  467. if (! $errorforline)
  468. {
  469. $db->commit();
  470. }
  471. else
  472. {
  473. $db->rollback();
  474. if ($error >= 10)
  475. {
  476. setEventMessages($langs->trans("ErrorTooManyErrorsProcessStopped"), null, 'errors');
  477. break; // Break in the foreach
  478. }
  479. }
  480. }
  481. $tabpay = $tabfac;
  482. if (empty($error) && count($tabpay) > 0) {
  483. setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs');
  484. }
  485. elseif (count($tabpay) == $error)
  486. {
  487. setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings');
  488. }
  489. else
  490. {
  491. setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings');
  492. }
  493. $action='';
  494. // Must reload data, so we make a redirect
  495. if (count($tabpay) != $error)
  496. {
  497. $param='id_journal='.$id_journal;
  498. $param.='&date_startday='.$date_startday;
  499. $param.='&date_startmonth='.$date_startmonth;
  500. $param.='&date_startyear='.$date_startyear;
  501. $param.='&date_endday='.$date_endday;
  502. $param.='&date_endmonth='.$date_endmonth;
  503. $param.='&date_endyear='.$date_endyear;
  504. $param.='&in_bookkeeping='.$in_bookkeeping;
  505. header("Location: ".$_SERVER['PHP_SELF'].($param?'?'.$param:''));
  506. exit;
  507. }
  508. }
  509. /*
  510. * View
  511. */
  512. $form = new Form($db);
  513. // Export
  514. if ($action == 'exportcsv') { // ISO and not UTF8 !
  515. $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
  516. $filename = 'journal';
  517. $type_export = 'journal';
  518. include DOL_DOCUMENT_ROOT . '/accountancy/tpl/export_journal.tpl.php';
  519. $companystatic = new Fournisseur($db);
  520. $invoicestatic = new FactureFournisseur($db);
  521. foreach ($tabfac as $key => $val)
  522. {
  523. $companystatic->id = $tabcompany[$key]['id'];
  524. $companystatic->name = $tabcompany[$key]['name'];
  525. $companystatic->code_compta = $tabcompany[$key]['code_compta'];
  526. $companystatic->code_compta_fournisseur = $tabcompany[$key]['code_compta_fournisseur'];
  527. $companystatic->code_client = $tabcompany[$key]['code_client'];
  528. $companystatic->code_fournisseur = $tabcompany[$key]['code_fournisseur'];
  529. $companystatic->fournisseur = 1;
  530. $invoicestatic->id = $key;
  531. $invoicestatic->ref = $val["refsologest"];
  532. $invoicestatic->ref_supplier = $val["refsuppliersologest"];
  533. $invoicestatic->type = $val["type"];
  534. $invoicestatic->description = dol_trunc(html_entity_decode($val["description"]), 32);
  535. $invoicestatic->close_code = $val["close_code"];
  536. $date = dol_print_date($val["date"], 'day');
  537. // Is it a replaced invoice ? 0=not a replaced invoice, 1=replaced invoice not yet dispatched, 2=replaced invoice dispatched
  538. $replacedinvoice = 0;
  539. if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED)
  540. {
  541. $replacedinvoice = 1;
  542. $alreadydispatched = $invoicestatic->getVentilExportCompta(); // Test if replaced invoice already into bookkeeping.
  543. if ($alreadydispatched) $replacedinvoice = 2;
  544. }
  545. // If not already into bookkeeping, we won't add it. If yes, do nothing (should not happen because creating replacement not possible if invoice is accounted)
  546. if ($replacedinvoice == 1)
  547. {
  548. continue;
  549. }
  550. // Third party
  551. foreach ($tabttc[$key] as $k => $mt) {
  552. //if ($mt) {
  553. print '"' . $key . '"' . $sep;
  554. print '"' . $date . '"' . $sep;
  555. print '"' . $val["refsologest"] . '"' . $sep;
  556. print '"' . utf8_decode(dol_trunc($companystatic->name, 32)). '"' . $sep;
  557. print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
  558. print '"' . $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER . '"' . $sep;
  559. print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
  560. print '"' . $langs->trans("Thirdparty") . '"' . $sep;
  561. print '"' . utf8_decode(dol_trunc($companystatic->name, 16)) . ' - ' . $val["refsuppliersologest"] . ' - ' . $langs->trans("Thirdparty") . '"' . $sep;
  562. print '"' . ($mt < 0 ? price(- $mt) : '') . '"' . $sep;
  563. print '"' . ($mt >= 0 ? price($mt) : '') . '"'. $sep;
  564. print '"' . $journal . '"' ;
  565. print "\n";
  566. //}
  567. }
  568. // Product / Service
  569. foreach ($tabht[$key] as $k => $mt) {
  570. $accountingaccount = new AccountingAccount($db);
  571. $accountingaccount->fetch(null, $k, true);
  572. //if ($mt) {
  573. print '"' . $key . '"' . $sep;
  574. print '"' . $date . '"' . $sep;
  575. print '"' . $val["refsologest"] . '"' . $sep;
  576. print '"' . utf8_decode(dol_trunc($companystatic->name, 32)) . '"' . $sep;
  577. print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep;
  578. print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep;
  579. print '""' . $sep;
  580. print '"' . utf8_decode(dol_trunc($accountingaccount->label, 32)) . '"' . $sep;
  581. print '"' . utf8_decode(dol_trunc($companystatic->name, 16)) . ' - ' . $val["refsuppliersologest"] . ' - ' . dol_trunc($accountingaccount->label, 32) . '"' . $sep;
  582. print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep;
  583. print '"' . ($mt < 0 ? price(- $mt) : '') . '"'. $sep;
  584. print '"' . $journal . '"' ;
  585. print "\n";
  586. //}
  587. }
  588. // VAT
  589. $listoftax = array(0, 1, 2);
  590. foreach ($listoftax as $numtax) {
  591. $arrayofvat = $tabtva;
  592. if ($numtax == 1) $arrayofvat = $tablocaltax1;
  593. if ($numtax == 2) $arrayofvat = $tablocaltax2;
  594. foreach ($arrayofvat[$key] as $k => $mt) {
  595. if ($mt) {
  596. print '"' . $key . '"' . $sep;
  597. print '"' . $date . '"' . $sep;
  598. print '"' . $val["refsologest"] . '"' . $sep;
  599. print '"' . utf8_decode(dol_trunc($companystatic->name, 32)) . '"' . $sep;
  600. print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep;
  601. print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep;
  602. print '""' . $sep;
  603. print '"' . $langs->trans("VAT") . ' - ' . $def_tva[$key] . '"' . $sep;
  604. print '"' . utf8_decode(dol_trunc($companystatic->name, 16)) . ' - ' . $val["refsuppliersologest"] . ' - ' . $langs->trans("VAT") . join(', ', $def_tva[$key][$k]) .' %' . ($numtax?' - Localtax '.$numtax:'') . '"' . $sep;
  605. print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep;
  606. print '"' . ($mt < 0 ? price(- $mt) : '') . '"'. $sep;
  607. print '"' . $journal . '"' ;
  608. print "\n";
  609. }
  610. }
  611. // VAT counterpart for NPR
  612. if (is_array($tabother[$key]))
  613. {
  614. foreach ($tabother[$key] as $k => $mt) {
  615. if ($mt) {
  616. print '"' . $key . '"' . $sep;
  617. print '"' . $date . '"' . $sep;
  618. print '"' . $val["refsologest"] . '"' . $sep;
  619. print '"' . utf8_decode(dol_trunc($companystatic->name, 32)). '"' . $sep;
  620. print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
  621. print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
  622. print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
  623. print '"' . $langs->trans("Thirdparty") . '"' . $sep;
  624. print '"' . utf8_decode(dol_trunc($companystatic->name, 16)) . ' - ' . $val["refsuppliersologest"] . ' - ' . $langs->trans("VAT") . ' NPR"' . $sep;
  625. print '"' . ($mt < 0 ? price(- $mt) : '') . '"' . $sep;
  626. print '"' . ($mt >= 0 ? price($mt) : '') . '"'. $sep;
  627. print '"' . $journal . '"' ;
  628. print "\n";
  629. }
  630. }
  631. }
  632. }
  633. }
  634. }
  635. if (empty($action) || $action == 'view') {
  636. llxHeader('', $langs->trans("PurchasesJournal"));
  637. $nom = $langs->trans("PurchasesJournal") . ' | ' . $accountingjournalstatic->getNomUrl(0, 1, 1, '', 1);
  638. $nomlink = '';
  639. $periodlink = '';
  640. $exportlink = '';
  641. $builddate=dol_now();
  642. $description.= $langs->trans("DescJournalOnlyBindedVisible").'<br>';
  643. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  644. $description .= $langs->trans("DepositsAreNotIncluded");
  645. } else {
  646. $description .= $langs->trans("DepositsAreIncluded");
  647. }
  648. $listofchoices=array('notyet'=>$langs->trans("NotYetInGeneralLedger"), 'already'=>$langs->trans("AlreadyInGeneralLedger"));
  649. $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);
  650. $period .= ' - ' .$langs->trans("JournalizationInLedgerStatus").' '. $form->selectarray('in_bookkeeping', $listofchoices, $in_bookkeeping, 1);
  651. $varlink = 'id_journal=' . $id_journal;
  652. journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink);
  653. // Button to write into Ledger
  654. if (($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "") || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1') {
  655. print '<br>';
  656. print img_warning().' '.$langs->trans("SomeMandatoryStepsOfSetupWereNotDone");
  657. print ' : '.$langs->trans("AccountancyAreaDescMisc", 4, '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("MenuDefaultAccounts").'</strong>');
  658. }
  659. print '<div class="tabsAction tabsActionNoBottom">';
  660. if (! empty($conf->global->ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL)) print '<input type="button" class="butAction" name="exportcsv" value="' . $langs->trans("ExportDraftJournal") . '" onclick="launch_export();" />';
  661. if (($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "") || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1') {
  662. print '<input type="button" class="butActionRefused classfortooltip" title="'.dol_escape_htmltag($langs->trans("SomeMandatoryStepsOfSetupWereNotDone")).'" value="' . $langs->trans("WriteBookKeeping") . '" />';
  663. }
  664. else {
  665. if ($in_bookkeeping == 'notyet') print '<input type="button" class="butAction" name="writebookkeeping" value="' . $langs->trans("WriteBookKeeping") . '" onclick="writebookkeeping();" />';
  666. else print '<a href="#" class="butActionRefused classfortooltip" name="writebookkeeping">' . $langs->trans("WriteBookKeeping") . '</a>';
  667. }
  668. print '</div>';
  669. // TODO Avoid using js. We can use a direct link with $param
  670. print '
  671. <script type="text/javascript">
  672. function launch_export() {
  673. $("div.fiche form input[name=\"action\"]").val("exportcsv");
  674. $("div.fiche form input[type=\"submit\"]").click();
  675. $("div.fiche form input[name=\"action\"]").val("");
  676. }
  677. function writebookkeeping() {
  678. console.log("click on writebookkeeping");
  679. $("div.fiche form input[name=\"action\"]").val("writebookkeeping");
  680. $("div.fiche form input[type=\"submit\"]").click();
  681. $("div.fiche form input[name=\"action\"]").val("");
  682. }
  683. </script>';
  684. /*
  685. * Show result array
  686. */
  687. print '<br>';
  688. $i = 0;
  689. print '<div class="div-table-responsive">';
  690. print "<table class=\"noborder\" width=\"100%\">";
  691. print "<tr class=\"liste_titre\">";
  692. print "<td>" . $langs->trans("Date") . "</td>";
  693. print "<td>" . $langs->trans("Piece") . ' (' . $langs->trans("InvoiceRef") . ")</td>";
  694. print "<td>" . $langs->trans("AccountAccounting") . "</td>";
  695. print "<td>" . $langs->trans("SubledgerAccount") . "</td>";
  696. print "<td>" . $langs->trans("LabelOperation") . "</td>";
  697. print '<td class="center">' . $langs->trans("Debit") . "</td>";
  698. print '<td class="center">' . $langs->trans("Credit") . "</td>";
  699. print "</tr>\n";
  700. $r = '';
  701. $invoicestatic = new FactureFournisseur($db);
  702. $companystatic = new Fournisseur($db);
  703. foreach ($tabfac as $key => $val)
  704. {
  705. $companystatic->id = $tabcompany[$key]['id'];
  706. $companystatic->name = $tabcompany[$key]['name'];
  707. $companystatic->code_compta = $tabcompany[$key]['code_compta'];
  708. $companystatic->code_compta_fournisseur = $tabcompany[$key]['code_compta_fournisseur'];
  709. $companystatic->code_client = $tabcompany[$key]['code_client'];
  710. $companystatic->code_fournisseur = $tabcompany[$key]['code_fournisseur'];
  711. $companystatic->fournisseur = 1;
  712. $invoicestatic->id = $key;
  713. $invoicestatic->ref = $val["refsologest"];
  714. $invoicestatic->ref_supplier = $val["refsuppliersologest"];
  715. $invoicestatic->type = $val["type"];
  716. $invoicestatic->description = dol_trunc(html_entity_decode($val["description"]), 32);
  717. $invoicestatic->close_code = $val["close_code"];
  718. $date = dol_print_date($val["date"], 'day');
  719. // Is it a replaced invoice ? 0=not a replaced invoice, 1=replaced invoice not yet dispatched, 2=replaced invoice dispatched
  720. $replacedinvoice = 0;
  721. if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED)
  722. {
  723. $replacedinvoice = 1;
  724. $alreadydispatched = $invoicestatic->getVentilExportCompta(); // Test if replaced invoice already into bookkeeping.
  725. if ($alreadydispatched) $replacedinvoice = 2;
  726. }
  727. // If not already into bookkeeping, we won't add it, if yes, add the counterpart ???.
  728. if ($replacedinvoice == 1)
  729. {
  730. print '<tr class="oddeven">';
  731. print "<!-- Replaced invoice -->";
  732. print "<td>" . $date . "</td>";
  733. print "<td><strike>" . $invoicestatic->getNomUrl(1) . "</strike></td>";
  734. // Account
  735. print "<td>";
  736. print $langs->trans("Replaced");
  737. print '</td>';
  738. // Subledger account
  739. print "<td>";
  740. print '</td>';
  741. print "<td>";
  742. print "</td>";
  743. print '<td class="right"></td>';
  744. print '<td class="right"></td>';
  745. print "</tr>";
  746. continue;
  747. }
  748. if ($errorforinvoice[$key] == 'somelinesarenotbound')
  749. {
  750. print '<tr class="oddeven">';
  751. print "<!-- Some lines are not bound -->";
  752. print "<td>" . $date . "</td>";
  753. print "<td>" . $invoicestatic->getNomUrl(1) . "</td>";
  754. // Account
  755. print "<td>";
  756. print '<span class="error">'.$langs->trans('ErrorInvoiceContainsLinesNotYetBoundedShort', $val['ref']).'</span>';
  757. print '</td>';
  758. // Subledger account
  759. print "<td>";
  760. print '</td>';
  761. print "<td>";
  762. print "</td>";
  763. print '<td class="right"></td>';
  764. print '<td class="right"></td>';
  765. print "</tr>";
  766. }
  767. // Third party
  768. foreach ($tabttc[$key] as $k => $mt) {
  769. //if ($mt) {
  770. print '<tr class="oddeven">';
  771. print "<!-- Thirdparty -->";
  772. print "<td>" . $date . "</td>";
  773. print "<td>" . $invoicestatic->getNomUrl(1) . "</td>";
  774. // Account
  775. print "<td>";
  776. $accountoshow = length_accounta($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER);
  777. if (($accountoshow == "") || $accountoshow == 'NotDefined')
  778. {
  779. print '<span class="error">'.$langs->trans("MainAccountForSuppliersNotDefined").'</span>';
  780. }
  781. else print $accountoshow;
  782. print '</td>';
  783. // Subledger account
  784. print "<td>";
  785. $accountoshow = length_accounta($k);
  786. if (($accountoshow == "") || $accountoshow == 'NotDefined')
  787. {
  788. print '<span class="error">'.$langs->trans("ThirdpartyAccountNotDefined").'</span>';
  789. }
  790. else print $accountoshow;
  791. print '</td>';
  792. print "<td>" . $companystatic->getNomUrl(0, 'supplier', 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("SubledgerAccount") . "</td>";
  793. print '<td class="right nowraponall">'. ($mt < 0 ? price(- $mt) : '') . "</td>";
  794. print '<td class="right nowraponall">' . ($mt >= 0 ? price($mt) : '') . "</td>";
  795. print "</tr>";
  796. //}
  797. }
  798. // Product / Service
  799. foreach ($tabht[$key] as $k => $mt) {
  800. $accountingaccount = new AccountingAccount($db);
  801. $accountingaccount->fetch(null, $k, true);
  802. //if ($mt) {
  803. print '<tr class="oddeven">';
  804. print "<!-- Product -->";
  805. print "<td>" . $date . "</td>";
  806. print "<td>" . $invoicestatic->getNomUrl(1) . "</td>";
  807. // Account
  808. print "<td>";
  809. $accountoshow = length_accountg($k);
  810. if (($accountoshow == "") || $accountoshow == 'NotDefined')
  811. {
  812. print '<span class="error">'.$langs->trans("ProductAccountNotDefined").'</span>';
  813. }
  814. else print $accountoshow;
  815. print "</td>";
  816. // Subledger account
  817. print "<td>";
  818. print '</td>';
  819. $companystatic->id = $tabcompany[$key]['id'];
  820. $companystatic->name = $tabcompany[$key]['name'];
  821. print "<td>" . $companystatic->getNomUrl(0, 'supplier', 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $accountingaccount->label . "</td>";
  822. print '<td class="right nowraponall">' . ($mt >= 0 ? price($mt) : '') . "</td>";
  823. print '<td class="right nowraponall">' . ($mt < 0 ? price(- $mt) : '') . "</td>";
  824. print "</tr>";
  825. //}
  826. }
  827. // VAT
  828. $listoftax = array(0, 1, 2);
  829. foreach ($listoftax as $numtax) {
  830. $arrayofvat = $tabtva;
  831. if ($numtax == 1) $arrayofvat = $tablocaltax1;
  832. if ($numtax == 2) $arrayofvat = $tablocaltax2;
  833. foreach ($arrayofvat[$key] as $k => $mt) {
  834. if ($mt) {
  835. print '<tr class="oddeven">';
  836. print "<!-- VAT -->";
  837. print "<td>" . $date . "</td>";
  838. print "<td>" . $invoicestatic->getNomUrl(1) . "</td>";
  839. // Account
  840. print "<td>";
  841. $accountoshow = length_accountg($k);
  842. if (($accountoshow == "") || $accountoshow == 'NotDefined')
  843. {
  844. print '<span class="error">'.$langs->trans("VATAccountNotDefined").' ('.$langs->trans("Purchase").')'.'</span>';
  845. }
  846. else print $accountoshow;
  847. print "</td>";
  848. // Subledger account
  849. print "<td>";
  850. print '</td>';
  851. print "<td>";
  852. print $companystatic->getNomUrl(0, 'supplier', 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("VAT"). ' '.join(', ', $def_tva[$key][$k]).' %'.($numtax?' - Localtax '.$numtax:'');
  853. print "</td>";
  854. print '<td class="right nowraponall">' . ($mt >= 0 ? price($mt) : '') . "</td>";
  855. print '<td class="right nowraponall">' . ($mt < 0 ? price(- $mt) : '') . "</td>";
  856. print "</tr>";
  857. }
  858. }
  859. }
  860. // VAT counterpart for NPR
  861. if (is_array($tabother[$key]))
  862. {
  863. foreach ($tabother[$key] as $k => $mt) {
  864. if ($mt) {
  865. print '<tr class="oddeven">';
  866. print "<!-- VAT counterpart NPR -->";
  867. print "<td>" . $date . "</td>";
  868. print "<td>" . $invoicestatic->getNomUrl(1) . "</td>";
  869. // Account
  870. print "<td>";
  871. $accountoshow = length_accountg($k);
  872. if (($accountoshow == "") || $accountoshow == 'NotDefined')
  873. {
  874. print '<span class="error">'.$langs->trans("VATAccountNotDefined").' ('.$langs->trans("NPR counterpart").'). Set ACCOUNTING_COUNTERPART_VAT_NPR to the subvention account'.'</span>';
  875. }
  876. else print $accountoshow;
  877. print '</td>';
  878. // Subledger account
  879. print "<td>";
  880. print '</td>';
  881. print "<td>" . $companystatic->getNomUrl(0, 'supplier', 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("VAT") . " NPR (counterpart)</td>";
  882. print '<td class="right nowraponall">' . ($mt < 0 ? price(- $mt) : '') . "</td>";
  883. print '<td class="right nowraponall">' . ($mt >= 0 ? price($mt) : '') . "</td>";
  884. print "</tr>";
  885. }
  886. }
  887. }
  888. }
  889. print "</table>";
  890. print '</div>';
  891. // End of page
  892. llxFooter();
  893. }
  894. $db->close();