purchasesjournal.php 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  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->socid > 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. $bookkeeping = new BookKeeping($db);
  255. $bookkeeping->doc_date = $val["date"];
  256. $bookkeeping->date_lim_reglement = $val["datereg"];
  257. $bookkeeping->doc_ref = $val["refsologest"];
  258. $bookkeeping->date_creation = $now;
  259. $bookkeeping->doc_type = 'supplier_invoice';
  260. $bookkeeping->fk_doc = $key;
  261. $bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add
  262. $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
  263. $bookkeeping->subledger_account = $tabcompany[$key]['code_compta_fournisseur'];
  264. $bookkeeping->subledger_label = $tabcompany[$key]['name'];
  265. $bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER;
  266. $accountingaccount->fetch(null, $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER, true);
  267. $bookkeeping->label_compte = $accountingaccount->label;
  268. $bookkeeping->label_operation = dol_trunc($companystatic->name, 16).' - '.$invoicestatic->ref_supplier.' - '.$langs->trans("SubledgerAccount");
  269. $bookkeeping->montant = $mt;
  270. $bookkeeping->sens = ($mt >= 0) ? 'C' : 'D';
  271. $bookkeeping->debit = ($mt <= 0) ? -$mt : 0;
  272. $bookkeeping->credit = ($mt > 0) ? $mt : 0;
  273. $bookkeeping->code_journal = $journal;
  274. $bookkeeping->journal_label = $journal_label;
  275. $bookkeeping->fk_user_author = $user->id;
  276. $bookkeeping->entity = $conf->entity;
  277. $totaldebit += $bookkeeping->debit;
  278. $totalcredit += $bookkeeping->credit;
  279. $result = $bookkeeping->create($user);
  280. if ($result < 0) {
  281. if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
  282. {
  283. $error++;
  284. $errorforline++;
  285. $errorforinvoice[$key] = 'alreadyjournalized';
  286. //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
  287. }
  288. else
  289. {
  290. $error++;
  291. $errorforline++;
  292. $errorforinvoice[$key] = 'other';
  293. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  294. }
  295. }
  296. }
  297. }
  298. // Product / Service
  299. if (!$errorforline)
  300. {
  301. foreach ($tabht[$key] as $k => $mt) {
  302. // get compte id and label
  303. if ($accountingaccount->fetch(null, $k, true)) {
  304. $bookkeeping = new BookKeeping($db);
  305. $bookkeeping->doc_date = $val["date"];
  306. $bookkeeping->date_lim_reglement = $val["datereg"];
  307. $bookkeeping->doc_ref = $val["refsologest"];
  308. $bookkeeping->date_creation = $now;
  309. $bookkeeping->doc_type = 'supplier_invoice';
  310. $bookkeeping->fk_doc = $key;
  311. $bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add
  312. $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
  313. $bookkeeping->subledger_account = '';
  314. $bookkeeping->subledger_label = '';
  315. $bookkeeping->numero_compte = $k;
  316. $bookkeeping->label_compte = $accountingaccount->label;
  317. $bookkeeping->label_operation = dol_trunc($companystatic->name, 16).' - '.$invoicestatic->ref_supplier.' - '.$accountingaccount->label;
  318. $bookkeeping->montant = $mt;
  319. $bookkeeping->sens = ($mt < 0) ? 'C' : 'D';
  320. $bookkeeping->debit = ($mt > 0) ? $mt : 0;
  321. $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
  322. $bookkeeping->code_journal = $journal;
  323. $bookkeeping->journal_label = $journal_label;
  324. $bookkeeping->fk_user_author = $user->id;
  325. $bookkeeping->entity = $conf->entity;
  326. $totaldebit += $bookkeeping->debit;
  327. $totalcredit += $bookkeeping->credit;
  328. $result = $bookkeeping->create($user);
  329. if ($result < 0) {
  330. if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
  331. {
  332. $error++;
  333. $errorforline++;
  334. $errorforinvoice[$key] = 'alreadyjournalized';
  335. //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
  336. }
  337. else
  338. {
  339. $error++;
  340. $errorforline++;
  341. $errorforinvoice[$key] = 'other';
  342. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  343. }
  344. }
  345. }
  346. }
  347. }
  348. // VAT
  349. // var_dump($tabtva);
  350. if (!$errorforline)
  351. {
  352. $listoftax = array(0, 1, 2);
  353. foreach ($listoftax as $numtax)
  354. {
  355. $arrayofvat = $tabtva;
  356. if ($numtax == 1) $arrayofvat = $tablocaltax1;
  357. if ($numtax == 2) $arrayofvat = $tablocaltax2;
  358. foreach ($arrayofvat[$key] as $k => $mt) {
  359. if ($mt) {
  360. $bookkeeping = new BookKeeping($db);
  361. $bookkeeping->doc_date = $val["date"];
  362. $bookkeeping->date_lim_reglement = $val["datereg"];
  363. $bookkeeping->doc_ref = $val["refsologest"];
  364. $bookkeeping->date_creation = $now;
  365. $bookkeeping->doc_type = 'supplier_invoice';
  366. $bookkeeping->fk_doc = $key;
  367. $bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add
  368. $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
  369. $bookkeeping->subledger_account = '';
  370. $bookkeeping->subledger_label = '';
  371. $bookkeeping->numero_compte = $k;
  372. $accountingaccount->fetch($k, null, true);
  373. $bookkeeping->label_compte = $accountingaccount->label;
  374. $bookkeeping->label_operation = dol_trunc($companystatic->name, 16).' - '.$invoicestatic->ref_supplier.' - '.$langs->trans("VAT").' '.join(', ', $def_tva[$key][$k]).' %'.($numtax ? ' - Localtax '.$numtax : '');
  375. $bookkeeping->montant = $mt;
  376. $bookkeeping->sens = ($mt < 0) ? 'C' : 'D';
  377. $bookkeeping->debit = ($mt > 0) ? $mt : 0;
  378. $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
  379. $bookkeeping->code_journal = $journal;
  380. $bookkeeping->journal_label = $journal_label;
  381. $bookkeeping->fk_user_author = $user->id;
  382. $bookkeeping->entity = $conf->entity;
  383. $totaldebit += $bookkeeping->debit;
  384. $totalcredit += $bookkeeping->credit;
  385. $result = $bookkeeping->create($user);
  386. if ($result < 0) {
  387. if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
  388. {
  389. $error++;
  390. $errorforline++;
  391. $errorforinvoice[$key] = 'alreadyjournalized';
  392. //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
  393. }
  394. else
  395. {
  396. $error++;
  397. $errorforline++;
  398. $errorforinvoice[$key] = 'other';
  399. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  400. }
  401. }
  402. }
  403. }
  404. }
  405. }
  406. // Counterpart of VAT for VAT NPR
  407. // var_dump($tabother);
  408. if (!$errorforline && is_array($tabother[$key]))
  409. {
  410. foreach ($tabother[$key] as $k => $mt) {
  411. if ($mt) {
  412. $bookkeeping = new BookKeeping($db);
  413. $bookkeeping->doc_date = $val["date"];
  414. $bookkeeping->date_lim_reglement = $val["datereg"];
  415. $bookkeeping->doc_ref = $val["refsologest"];
  416. $bookkeeping->date_creation = $now;
  417. $bookkeeping->doc_type = 'supplier_invoice';
  418. $bookkeeping->fk_doc = $key;
  419. $bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add
  420. $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
  421. $bookkeeping->subledger_account = '';
  422. $bookkeeping->subledger_label = '';
  423. $bookkeeping->numero_compte = $k;
  424. $bookkeeping->label_operation = dol_trunc($companystatic->name, 16).' - '.$invoicestatic->ref_supplier.' - '.$langs->trans("VAT").' NPR';
  425. $bookkeeping->montant = $mt;
  426. $bookkeeping->sens = ($mt < 0) ? 'C' : 'D';
  427. $bookkeeping->debit = ($mt > 0) ? $mt : 0;
  428. $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
  429. $bookkeeping->code_journal = $journal;
  430. $bookkeeping->journal_label = $journal_label;
  431. $bookkeeping->fk_user_author = $user->id;
  432. $bookkeeping->entity = $conf->entity;
  433. $totaldebit += $bookkeeping->debit;
  434. $totalcredit += $bookkeeping->credit;
  435. $result = $bookkeeping->create($user);
  436. if ($result < 0) {
  437. if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
  438. {
  439. $error++;
  440. $errorforline++;
  441. $errorforinvoice[$key] = 'alreadyjournalized';
  442. //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
  443. }
  444. else
  445. {
  446. $error++;
  447. $errorforline++;
  448. $errorforinvoice[$key] = 'other';
  449. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  450. }
  451. }
  452. }
  453. }
  454. }
  455. // Protection against a bug on lines before
  456. if (!$errorforline && (price2num($totaldebit, 'MT') != price2num($totalcredit, 'MT')))
  457. {
  458. $error++;
  459. $errorforline++;
  460. $errorforinvoice[$key] = 'amountsnotbalanced';
  461. setEventMessages('Try to insert a non balanced transaction in book for '.$invoicestatic->ref.'. Canceled. Surely a bug.', null, 'errors');
  462. }
  463. if (!$errorforline)
  464. {
  465. $db->commit();
  466. }
  467. else
  468. {
  469. $db->rollback();
  470. if ($error >= 10)
  471. {
  472. setEventMessages($langs->trans("ErrorTooManyErrorsProcessStopped"), null, 'errors');
  473. break; // Break in the foreach
  474. }
  475. }
  476. }
  477. $tabpay = $tabfac;
  478. if (empty($error) && count($tabpay) > 0) {
  479. setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs');
  480. }
  481. elseif (count($tabpay) == $error)
  482. {
  483. setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings');
  484. }
  485. else
  486. {
  487. setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings');
  488. }
  489. $action = '';
  490. // Must reload data, so we make a redirect
  491. if (count($tabpay) != $error)
  492. {
  493. $param = 'id_journal='.$id_journal;
  494. $param .= '&date_startday='.$date_startday;
  495. $param .= '&date_startmonth='.$date_startmonth;
  496. $param .= '&date_startyear='.$date_startyear;
  497. $param .= '&date_endday='.$date_endday;
  498. $param .= '&date_endmonth='.$date_endmonth;
  499. $param .= '&date_endyear='.$date_endyear;
  500. $param .= '&in_bookkeeping='.$in_bookkeeping;
  501. header("Location: ".$_SERVER['PHP_SELF'].($param ? '?'.$param : ''));
  502. exit;
  503. }
  504. }
  505. /*
  506. * View
  507. */
  508. $form = new Form($db);
  509. // Export
  510. if ($action == 'exportcsv') { // ISO and not UTF8 !
  511. $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
  512. $filename = 'journal';
  513. $type_export = 'journal';
  514. include DOL_DOCUMENT_ROOT.'/accountancy/tpl/export_journal.tpl.php';
  515. $companystatic = new Fournisseur($db);
  516. $invoicestatic = new FactureFournisseur($db);
  517. foreach ($tabfac as $key => $val)
  518. {
  519. $companystatic->id = $tabcompany[$key]['id'];
  520. $companystatic->name = $tabcompany[$key]['name'];
  521. $companystatic->code_compta = $tabcompany[$key]['code_compta'];
  522. $companystatic->code_compta_fournisseur = $tabcompany[$key]['code_compta_fournisseur'];
  523. $companystatic->code_client = $tabcompany[$key]['code_client'];
  524. $companystatic->code_fournisseur = $tabcompany[$key]['code_fournisseur'];
  525. $companystatic->fournisseur = 1;
  526. $invoicestatic->id = $key;
  527. $invoicestatic->ref = $val["refsologest"];
  528. $invoicestatic->ref_supplier = $val["refsuppliersologest"];
  529. $invoicestatic->type = $val["type"];
  530. $invoicestatic->description = dol_trunc(html_entity_decode($val["description"]), 32);
  531. $invoicestatic->close_code = $val["close_code"];
  532. $date = dol_print_date($val["date"], 'day');
  533. // Is it a replaced invoice ? 0=not a replaced invoice, 1=replaced invoice not yet dispatched, 2=replaced invoice dispatched
  534. $replacedinvoice = 0;
  535. if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED)
  536. {
  537. $replacedinvoice = 1;
  538. $alreadydispatched = $invoicestatic->getVentilExportCompta(); // Test if replaced invoice already into bookkeeping.
  539. if ($alreadydispatched) $replacedinvoice = 2;
  540. }
  541. // 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)
  542. if ($replacedinvoice == 1)
  543. {
  544. continue;
  545. }
  546. // Third party
  547. foreach ($tabttc[$key] as $k => $mt) {
  548. //if ($mt) {
  549. print '"'.$key.'"'.$sep;
  550. print '"'.$date.'"'.$sep;
  551. print '"'.$val["refsologest"].'"'.$sep;
  552. print '"'.utf8_decode(dol_trunc($companystatic->name, 32)).'"'.$sep;
  553. print '"'.length_accounta(html_entity_decode($k)).'"'.$sep;
  554. print '"'.$conf->global->ACCOUNTING_ACCOUNT_SUPPLIER.'"'.$sep;
  555. print '"'.length_accounta(html_entity_decode($k)).'"'.$sep;
  556. print '"'.$langs->trans("Thirdparty").'"'.$sep;
  557. print '"'.utf8_decode(dol_trunc($companystatic->name, 16)).' - '.$val["refsuppliersologest"].' - '.$langs->trans("Thirdparty").'"'.$sep;
  558. print '"'.($mt < 0 ? price(-$mt) : '').'"'.$sep;
  559. print '"'.($mt >= 0 ? price($mt) : '').'"'.$sep;
  560. print '"'.$journal.'"';
  561. print "\n";
  562. //}
  563. }
  564. // Product / Service
  565. foreach ($tabht[$key] as $k => $mt) {
  566. $accountingaccount = new AccountingAccount($db);
  567. $accountingaccount->fetch(null, $k, true);
  568. //if ($mt) {
  569. print '"'.$key.'"'.$sep;
  570. print '"'.$date.'"'.$sep;
  571. print '"'.$val["refsologest"].'"'.$sep;
  572. print '"'.utf8_decode(dol_trunc($companystatic->name, 32)).'"'.$sep;
  573. print '"'.length_accountg(html_entity_decode($k)).'"'.$sep;
  574. print '"'.length_accountg(html_entity_decode($k)).'"'.$sep;
  575. print '""'.$sep;
  576. print '"'.utf8_decode(dol_trunc($accountingaccount->label, 32)).'"'.$sep;
  577. print '"'.utf8_decode(dol_trunc($companystatic->name, 16)).' - '.$val["refsuppliersologest"].' - '.dol_trunc($accountingaccount->label, 32).'"'.$sep;
  578. print '"'.($mt >= 0 ? price($mt) : '').'"'.$sep;
  579. print '"'.($mt < 0 ? price(-$mt) : '').'"'.$sep;
  580. print '"'.$journal.'"';
  581. print "\n";
  582. //}
  583. }
  584. // VAT
  585. $listoftax = array(0, 1, 2);
  586. foreach ($listoftax as $numtax) {
  587. $arrayofvat = $tabtva;
  588. if ($numtax == 1) $arrayofvat = $tablocaltax1;
  589. if ($numtax == 2) $arrayofvat = $tablocaltax2;
  590. foreach ($arrayofvat[$key] as $k => $mt) {
  591. if ($mt) {
  592. print '"'.$key.'"'.$sep;
  593. print '"'.$date.'"'.$sep;
  594. print '"'.$val["refsologest"].'"'.$sep;
  595. print '"'.utf8_decode(dol_trunc($companystatic->name, 32)).'"'.$sep;
  596. print '"'.length_accountg(html_entity_decode($k)).'"'.$sep;
  597. print '"'.length_accountg(html_entity_decode($k)).'"'.$sep;
  598. print '""'.$sep;
  599. print '"'.$langs->trans("VAT").' - '.$def_tva[$key].'"'.$sep;
  600. print '"'.utf8_decode(dol_trunc($companystatic->name, 16)).' - '.$val["refsuppliersologest"].' - '.$langs->trans("VAT").join(', ', $def_tva[$key][$k]).' %'.($numtax ? ' - Localtax '.$numtax : '').'"'.$sep;
  601. print '"'.($mt >= 0 ? price($mt) : '').'"'.$sep;
  602. print '"'.($mt < 0 ? price(-$mt) : '').'"'.$sep;
  603. print '"'.$journal.'"';
  604. print "\n";
  605. }
  606. }
  607. // VAT counterpart for NPR
  608. if (is_array($tabother[$key]))
  609. {
  610. foreach ($tabother[$key] as $k => $mt) {
  611. if ($mt) {
  612. print '"'.$key.'"'.$sep;
  613. print '"'.$date.'"'.$sep;
  614. print '"'.$val["refsologest"].'"'.$sep;
  615. print '"'.utf8_decode(dol_trunc($companystatic->name, 32)).'"'.$sep;
  616. print '"'.length_accounta(html_entity_decode($k)).'"'.$sep;
  617. print '"'.length_accounta(html_entity_decode($k)).'"'.$sep;
  618. print '"'.length_accounta(html_entity_decode($k)).'"'.$sep;
  619. print '"'.$langs->trans("Thirdparty").'"'.$sep;
  620. print '"'.utf8_decode(dol_trunc($companystatic->name, 16)).' - '.$val["refsuppliersologest"].' - '.$langs->trans("VAT").' NPR"'.$sep;
  621. print '"'.($mt < 0 ? price(-$mt) : '').'"'.$sep;
  622. print '"'.($mt >= 0 ? price($mt) : '').'"'.$sep;
  623. print '"'.$journal.'"';
  624. print "\n";
  625. }
  626. }
  627. }
  628. }
  629. }
  630. }
  631. if (empty($action) || $action == 'view') {
  632. llxHeader('', $langs->trans("PurchasesJournal"));
  633. $nom = $langs->trans("PurchasesJournal").' | '.$accountingjournalstatic->getNomUrl(0, 1, 1, '', 1);
  634. $nomlink = '';
  635. $periodlink = '';
  636. $exportlink = '';
  637. $builddate = dol_now();
  638. $description .= $langs->trans("DescJournalOnlyBindedVisible").'<br>';
  639. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  640. $description .= $langs->trans("DepositsAreNotIncluded");
  641. } else {
  642. $description .= $langs->trans("DepositsAreIncluded");
  643. }
  644. $listofchoices = array('notyet'=>$langs->trans("NotYetInGeneralLedger"), 'already'=>$langs->trans("AlreadyInGeneralLedger"));
  645. $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);
  646. $period .= ' - '.$langs->trans("JournalizationInLedgerStatus").' '.$form->selectarray('in_bookkeeping', $listofchoices, $in_bookkeeping, 1);
  647. $varlink = 'id_journal='.$id_journal;
  648. journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink);
  649. // Button to write into Ledger
  650. if (($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "") || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1') {
  651. print '<br>';
  652. print img_warning().' '.$langs->trans("SomeMandatoryStepsOfSetupWereNotDone");
  653. print ' : '.$langs->trans("AccountancyAreaDescMisc", 4, '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$langs->transnoentitiesnoconv("Setup")."-".$langs->transnoentitiesnoconv("MenuDefaultAccounts").'</strong>');
  654. }
  655. print '<div class="tabsAction tabsActionNoBottom">';
  656. if (!empty($conf->global->ACCOUNTING_ENABLE_EXPORT_DRAFT_JOURNAL)) print '<input type="button" class="butAction" name="exportcsv" value="'.$langs->trans("ExportDraftJournal").'" onclick="launch_export();" />';
  657. if (($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "") || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1') {
  658. print '<input type="button" class="butActionRefused classfortooltip" title="'.dol_escape_htmltag($langs->trans("SomeMandatoryStepsOfSetupWereNotDone")).'" value="'.$langs->trans("WriteBookKeeping").'" />';
  659. }
  660. else {
  661. if ($in_bookkeeping == 'notyet') print '<input type="button" class="butAction" name="writebookkeeping" value="'.$langs->trans("WriteBookKeeping").'" onclick="writebookkeeping();" />';
  662. else print '<a href="#" class="butActionRefused classfortooltip" name="writebookkeeping">'.$langs->trans("WriteBookKeeping").'</a>';
  663. }
  664. print '</div>';
  665. // TODO Avoid using js. We can use a direct link with $param
  666. print '
  667. <script type="text/javascript">
  668. function launch_export() {
  669. $("div.fiche form input[name=\"action\"]").val("exportcsv");
  670. $("div.fiche form input[type=\"submit\"]").click();
  671. $("div.fiche form input[name=\"action\"]").val("");
  672. }
  673. function writebookkeeping() {
  674. console.log("click on writebookkeeping");
  675. $("div.fiche form input[name=\"action\"]").val("writebookkeeping");
  676. $("div.fiche form input[type=\"submit\"]").click();
  677. $("div.fiche form input[name=\"action\"]").val("");
  678. }
  679. </script>';
  680. /*
  681. * Show result array
  682. */
  683. print '<br>';
  684. $i = 0;
  685. print '<div class="div-table-responsive">';
  686. print "<table class=\"noborder\" width=\"100%\">";
  687. print "<tr class=\"liste_titre\">";
  688. print "<td>".$langs->trans("Date")."</td>";
  689. print "<td>".$langs->trans("Piece").' ('.$langs->trans("InvoiceRef").")</td>";
  690. print "<td>".$langs->trans("AccountAccounting")."</td>";
  691. print "<td>".$langs->trans("SubledgerAccount")."</td>";
  692. print "<td>".$langs->trans("LabelOperation")."</td>";
  693. print '<td class="center">'.$langs->trans("Debit")."</td>";
  694. print '<td class="center">'.$langs->trans("Credit")."</td>";
  695. print "</tr>\n";
  696. $r = '';
  697. $invoicestatic = new FactureFournisseur($db);
  698. $companystatic = new Fournisseur($db);
  699. foreach ($tabfac as $key => $val)
  700. {
  701. $companystatic->id = $tabcompany[$key]['id'];
  702. $companystatic->name = $tabcompany[$key]['name'];
  703. $companystatic->code_compta = $tabcompany[$key]['code_compta'];
  704. $companystatic->code_compta_fournisseur = $tabcompany[$key]['code_compta_fournisseur'];
  705. $companystatic->code_client = $tabcompany[$key]['code_client'];
  706. $companystatic->code_fournisseur = $tabcompany[$key]['code_fournisseur'];
  707. $companystatic->fournisseur = 1;
  708. $invoicestatic->id = $key;
  709. $invoicestatic->ref = $val["refsologest"];
  710. $invoicestatic->ref_supplier = $val["refsuppliersologest"];
  711. $invoicestatic->type = $val["type"];
  712. $invoicestatic->description = dol_trunc(html_entity_decode($val["description"]), 32);
  713. $invoicestatic->close_code = $val["close_code"];
  714. $date = dol_print_date($val["date"], 'day');
  715. // Is it a replaced invoice ? 0=not a replaced invoice, 1=replaced invoice not yet dispatched, 2=replaced invoice dispatched
  716. $replacedinvoice = 0;
  717. if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED)
  718. {
  719. $replacedinvoice = 1;
  720. $alreadydispatched = $invoicestatic->getVentilExportCompta(); // Test if replaced invoice already into bookkeeping.
  721. if ($alreadydispatched) $replacedinvoice = 2;
  722. }
  723. // If not already into bookkeeping, we won't add it, if yes, add the counterpart ???.
  724. if ($replacedinvoice == 1)
  725. {
  726. print '<tr class="oddeven">';
  727. print "<!-- Replaced invoice -->";
  728. print "<td>".$date."</td>";
  729. print "<td><strike>".$invoicestatic->getNomUrl(1)."</strike></td>";
  730. // Account
  731. print "<td>";
  732. print $langs->trans("Replaced");
  733. print '</td>';
  734. // Subledger account
  735. print "<td>";
  736. print '</td>';
  737. print "<td>";
  738. print "</td>";
  739. print '<td class="right"></td>';
  740. print '<td class="right"></td>';
  741. print "</tr>";
  742. continue;
  743. }
  744. if ($errorforinvoice[$key] == 'somelinesarenotbound')
  745. {
  746. print '<tr class="oddeven">';
  747. print "<!-- Some lines are not bound -->";
  748. print "<td>".$date."</td>";
  749. print "<td>".$invoicestatic->getNomUrl(1)."</td>";
  750. // Account
  751. print "<td>";
  752. print '<span class="error">'.$langs->trans('ErrorInvoiceContainsLinesNotYetBoundedShort', $val['ref']).'</span>';
  753. print '</td>';
  754. // Subledger account
  755. print "<td>";
  756. print '</td>';
  757. print "<td>";
  758. print "</td>";
  759. print '<td class="right"></td>';
  760. print '<td class="right"></td>';
  761. print "</tr>";
  762. }
  763. // Third party
  764. foreach ($tabttc[$key] as $k => $mt) {
  765. print '<tr class="oddeven">';
  766. print "<!-- Thirdparty -->";
  767. print "<td>".$date."</td>";
  768. print "<td>".$invoicestatic->getNomUrl(1)."</td>";
  769. // Account
  770. print "<td>";
  771. $accountoshow = length_accounta($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER);
  772. if (($accountoshow == "") || $accountoshow == 'NotDefined')
  773. {
  774. print '<span class="error">'.$langs->trans("MainAccountForSuppliersNotDefined").'</span>';
  775. }
  776. else print $accountoshow;
  777. print '</td>';
  778. // Subledger account
  779. print "<td>";
  780. $accountoshow = length_accounta($k);
  781. if (($accountoshow == "") || $accountoshow == 'NotDefined')
  782. {
  783. print '<span class="error">'.$langs->trans("ThirdpartyAccountNotDefined").'</span>';
  784. }
  785. else print $accountoshow;
  786. print '</td>';
  787. print "<td>".$companystatic->getNomUrl(0, 'supplier', 16).' - '.$invoicestatic->ref_supplier.' - '.$langs->trans("SubledgerAccount")."</td>";
  788. print '<td class="right nowraponall">'.($mt < 0 ? price(-$mt) : '')."</td>";
  789. print '<td class="right nowraponall">'.($mt >= 0 ? price($mt) : '')."</td>";
  790. print "</tr>";
  791. }
  792. // Product / Service
  793. foreach ($tabht[$key] as $k => $mt) {
  794. $accountingaccount = new AccountingAccount($db);
  795. $accountingaccount->fetch(null, $k, true);
  796. print '<tr class="oddeven">';
  797. print "<!-- Product -->";
  798. print "<td>".$date."</td>";
  799. print "<td>".$invoicestatic->getNomUrl(1)."</td>";
  800. // Account
  801. print "<td>";
  802. $accountoshow = length_accountg($k);
  803. if (($accountoshow == "") || $accountoshow == 'NotDefined')
  804. {
  805. print '<span class="error">'.$langs->trans("ProductAccountNotDefined").'</span>';
  806. }
  807. else print $accountoshow;
  808. print "</td>";
  809. // Subledger account
  810. print "<td>";
  811. print '</td>';
  812. $companystatic->id = $tabcompany[$key]['id'];
  813. $companystatic->name = $tabcompany[$key]['name'];
  814. print "<td>".$companystatic->getNomUrl(0, 'supplier', 16).' - '.$invoicestatic->ref_supplier.' - '.$accountingaccount->label."</td>";
  815. print '<td class="right nowraponall">'.($mt >= 0 ? price($mt) : '')."</td>";
  816. print '<td class="right nowraponall">'.($mt < 0 ? price(-$mt) : '')."</td>";
  817. print "</tr>";
  818. }
  819. // VAT
  820. $listoftax = array(0, 1, 2);
  821. foreach ($listoftax as $numtax) {
  822. $arrayofvat = $tabtva;
  823. if ($numtax == 1) $arrayofvat = $tablocaltax1;
  824. if ($numtax == 2) $arrayofvat = $tablocaltax2;
  825. foreach ($arrayofvat[$key] as $k => $mt) {
  826. if ($mt) {
  827. print '<tr class="oddeven">';
  828. print "<!-- VAT -->";
  829. print "<td>".$date."</td>";
  830. print "<td>".$invoicestatic->getNomUrl(1)."</td>";
  831. // Account
  832. print "<td>";
  833. $accountoshow = length_accountg($k);
  834. if (($accountoshow == "") || $accountoshow == 'NotDefined')
  835. {
  836. print '<span class="error">'.$langs->trans("VATAccountNotDefined").' ('.$langs->trans("Purchase").')'.'</span>';
  837. }
  838. else print $accountoshow;
  839. print "</td>";
  840. // Subledger account
  841. print "<td>";
  842. print '</td>';
  843. print "<td>";
  844. print $companystatic->getNomUrl(0, 'supplier', 16).' - '.$invoicestatic->ref_supplier.' - '.$langs->trans("VAT").' '.join(', ', $def_tva[$key][$k]).' %'.($numtax ? ' - Localtax '.$numtax : '');
  845. print "</td>";
  846. print '<td class="right nowraponall">'.($mt >= 0 ? price($mt) : '')."</td>";
  847. print '<td class="right nowraponall">'.($mt < 0 ? price(-$mt) : '')."</td>";
  848. print "</tr>";
  849. }
  850. }
  851. }
  852. // VAT counterpart for NPR
  853. if (is_array($tabother[$key]))
  854. {
  855. foreach ($tabother[$key] as $k => $mt) {
  856. if ($mt) {
  857. print '<tr class="oddeven">';
  858. print "<!-- VAT counterpart NPR -->";
  859. print "<td>".$date."</td>";
  860. print "<td>".$invoicestatic->getNomUrl(1)."</td>";
  861. // Account
  862. print "<td>";
  863. $accountoshow = length_accountg($k);
  864. if (($accountoshow == "") || $accountoshow == 'NotDefined')
  865. {
  866. print '<span class="error">'.$langs->trans("VATAccountNotDefined").' ('.$langs->trans("NPR counterpart").'). Set ACCOUNTING_COUNTERPART_VAT_NPR to the subvention account'.'</span>';
  867. }
  868. else print $accountoshow;
  869. print '</td>';
  870. // Subledger account
  871. print "<td>";
  872. print '</td>';
  873. print "<td>".$companystatic->getNomUrl(0, 'supplier', 16).' - '.$invoicestatic->ref_supplier.' - '.$langs->trans("VAT")." NPR (counterpart)</td>";
  874. print '<td class="right nowraponall">'.($mt < 0 ? price(-$mt) : '')."</td>";
  875. print '<td class="right nowraponall">'.($mt >= 0 ? price($mt) : '')."</td>";
  876. print "</tr>";
  877. }
  878. }
  879. }
  880. }
  881. print "</table>";
  882. print '</div>';
  883. // End of page
  884. llxFooter();
  885. }
  886. $db->close();