purchasesjournal.php 39 KB

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