purchasesjournal.php 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004
  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 <http://www.gnu.org/licenses/>.
  24. */
  25. /**
  26. * \file htdocs/accountancy/journal/purchasesjournal.php
  27. * \ingroup Advanced accountancy
  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. $idpays = $mysoc->country_id;
  82. $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,";
  83. $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,";
  84. $sql .= " s.rowid as socid, s.nom as name, s.fournisseur, s.code_client, s.code_fournisseur, s.code_compta, s.code_compta_fournisseur,";
  85. $sql .= " p.accountancy_code_buy , aa.rowid as fk_compte, aa.account_number as compte, aa.label as label_compte";
  86. $sql .= " FROM " . MAIN_DB_PREFIX . "facture_fourn_det as fd";
  87. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product as p ON p.rowid = fd.fk_product";
  88. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = fd.fk_code_ventilation";
  89. $sql .= " JOIN " . MAIN_DB_PREFIX . "facture_fourn as f ON f.rowid = fd.fk_facture_fourn";
  90. $sql .= " JOIN " . MAIN_DB_PREFIX . "societe as s ON s.rowid = f.fk_soc";
  91. $sql .= " WHERE f.fk_statut > 0";
  92. $sql .= " AND fd.fk_code_ventilation > 0";
  93. $sql .= " AND f.entity IN (" . getEntity('facture_fourn', 0) . ")"; // We don't share object for accountancy
  94. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  95. $sql .= " AND f.type IN (" . FactureFournisseur::TYPE_STANDARD . "," . FactureFournisseur::TYPE_REPLACEMENT . "," . FactureFournisseur::TYPE_CREDIT_NOTE . "," . FactureFournisseur::TYPE_SITUATION . ")";
  96. } else {
  97. $sql .= " AND f.type IN (" . FactureFournisseur::TYPE_STANDARD . "," . FactureFournisseur::TYPE_REPLACEMENT . "," . FactureFournisseur::TYPE_CREDIT_NOTE . "," . FactureFournisseur::TYPE_DEPOSIT . "," . FactureFournisseur::TYPE_SITUATION . ")";
  98. }
  99. if ($date_start && $date_end)
  100. $sql .= " AND f.datef >= '" . $db->idate($date_start) . "' AND f.datef <= '" . $db->idate($date_end) . "'";
  101. // Already in bookkeeping or not
  102. if ($in_bookkeeping == 'already')
  103. {
  104. $sql .= " AND f.rowid IN (SELECT fk_doc FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab WHERE ab.doc_type='supplier_invoice')";
  105. }
  106. if ($in_bookkeeping == 'notyet')
  107. {
  108. $sql .= " AND f.rowid NOT IN (SELECT fk_doc FROM " . MAIN_DB_PREFIX . "accounting_bookkeeping as ab WHERE ab.doc_type='supplier_invoice')";
  109. }
  110. $sql .= " ORDER BY f.datef";
  111. dol_syslog('accountancy/journal/purchasesjournal.php', LOG_DEBUG);
  112. $result = $db->query($sql);
  113. if ($result) {
  114. $tabfac = array ();
  115. $tabht = array ();
  116. $tabtva = array ();
  117. $def_tva = array ();
  118. $tabttc = array ();
  119. $tablocaltax1 = array ();
  120. $tablocaltax2 = array ();
  121. $tabcompany = array ();
  122. $tabother = array();
  123. $num = $db->num_rows($result);
  124. // Variables
  125. $cptfour = ($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER != "") ? $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER : 'NotDefined';
  126. $cpttva = (! empty($conf->global->ACCOUNTING_VAT_BUY_ACCOUNT)) ? $conf->global->ACCOUNTING_VAT_BUY_ACCOUNT : 'NotDefined';
  127. $i = 0;
  128. while ( $i < $num ) {
  129. $obj = $db->fetch_object($result);
  130. // Controls
  131. $compta_soc = ($obj->code_compta_fournisseur != "") ? $obj->code_compta_fournisseur : $cptfour;
  132. $compta_prod = $obj->compte;
  133. if (empty($compta_prod)) {
  134. if ($obj->product_type == 0)
  135. $compta_prod = (! empty($conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT)) ? $conf->global->ACCOUNTING_PRODUCT_BUY_ACCOUNT : 'NotDefined';
  136. else
  137. $compta_prod = (! empty($conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT)) ? $conf->global->ACCOUNTING_SERVICE_BUY_ACCOUNT : 'NotDefined';
  138. }
  139. $vatdata = getTaxesFromId($obj->tva_tx.($obj->vat_src_code?' ('.$obj->vat_src_code.')':''), $mysoc, $mysoc, 0);
  140. $compta_tva = (! empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $cpttva);
  141. $compta_localtax1 = (! empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $cpttva);
  142. $compta_localtax2 = (! empty($vatdata['accountancy_code_buy']) ? $vatdata['accountancy_code_buy'] : $cpttva);
  143. $compta_counterpart_tva_npr = (! empty($conf->global->ACCOUNTING_COUNTERPART_VAT_NPR)) ? $conf->global->ACCOUNTING_COUNTERPART_VAT_NPR : 'NotDefined';
  144. // Define array to display all VAT rates that use this accounting account $compta_tva
  145. if (price2num($obj->tva_tx) || ! empty($obj->vat_src_code))
  146. {
  147. $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.')':''));
  148. }
  149. $line = new SupplierInvoiceLine($db);
  150. $line->fetch($obj->fdid);
  151. $tabfac[$obj->rowid]["date"] = $db->jdate($obj->df);
  152. $tabfac[$obj->rowid]["datereg"] = $db->jdate($obj->dlr);
  153. $tabfac[$obj->rowid]["ref"] = $obj->ref_supplier . ' (' . $obj->ref . ')';
  154. $tabfac[$obj->rowid]["refsologest"] = $obj->ref;
  155. $tabfac[$obj->rowid]["refsuppliersologest"] = $obj->ref_supplier;
  156. $tabfac[$obj->rowid]["type"] = $obj->type;
  157. $tabfac[$obj->rowid]["description"] = $obj->description;
  158. $tabfac[$obj->rowid]["close_code"] = $obj->close_code; // close_code = 'replaced' for replacement invoices (not used in most european countries)
  159. //$tabfac[$obj->rowid]["fk_facturefourndet"] = $obj->fdid;
  160. // Avoid warnings
  161. if (! isset($tabttc[$obj->rowid][$compta_soc])) $tabttc[$obj->rowid][$compta_soc] = 0;
  162. if (! isset($tabht[$obj->rowid][$compta_prod])) $tabht[$obj->rowid][$compta_prod] = 0;
  163. if (! isset($tabtva[$obj->rowid][$compta_tva])) $tabtva[$obj->rowid][$compta_tva] = 0;
  164. if (! isset($tablocaltax1[$obj->rowid][$compta_localtax1])) $tablocaltax1[$obj->rowid][$compta_localtax1] = 0;
  165. if (! isset($tablocaltax2[$obj->rowid][$compta_localtax2])) $tablocaltax2[$obj->rowid][$compta_localtax2] = 0;
  166. $tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc;
  167. $tabht[$obj->rowid][$compta_prod] += $obj->total_ht;
  168. $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva;
  169. if (! empty($line->tva_npr)) // Add an entry for counterpart
  170. {
  171. $tabother[$obj->rowid][$compta_counterpart_tva_npr] += $obj->total_tva;
  172. }
  173. $tablocaltax1[$obj->rowid][$compta_localtax1] += $obj->total_localtax1;
  174. $tablocaltax2[$obj->rowid][$compta_localtax2] += $obj->total_localtax2;
  175. $tabcompany[$obj->rowid] = array (
  176. 'id' => $obj->socid,
  177. 'name' => $obj->name,
  178. 'code_fournisseur' => $obj->code_fournisseur,
  179. 'code_compta_fournisseur' => $compta_soc
  180. );
  181. $i++;
  182. }
  183. } else {
  184. dol_print_error($db);
  185. }
  186. $errorforinvoice = array();
  187. // Loop in invoices to detect lines with not binding lines
  188. foreach ($tabfac as $key => $val) { // Loop on each invoice
  189. $sql = "SELECT COUNT(fd.rowid) as nb";
  190. $sql.= " FROM " . MAIN_DB_PREFIX . "facture_fourn_det as fd";
  191. $sql.= " WHERE fd.product_type <= 2 AND fd.fk_code_ventilation <= 0";
  192. $sql.= " AND fd.total_ttc <> 0 AND fk_facture_fourn = ".$key;
  193. $resql=$db->query($sql);
  194. if ($resql)
  195. {
  196. $obj = $db->fetch_object($resql);
  197. if ($obj->nb > 0)
  198. {
  199. $errorforinvoice[$key]='somelinesarenotbound';
  200. }
  201. }
  202. else dol_print_error($db);
  203. }
  204. //var_dump($errorforinvoice);exit;
  205. // Bookkeeping Write
  206. if ($action == 'writebookkeeping') {
  207. $now = dol_now();
  208. $error = 0;
  209. $companystatic = new Societe($db);
  210. $invoicestatic = new FactureFournisseur($db);
  211. foreach ($tabfac as $key => $val) { // Loop on each invoice
  212. $errorforline = 0;
  213. $totalcredit = 0;
  214. $totaldebit = 0;
  215. $db->begin();
  216. $companystatic->id = $tabcompany[$key]['id'];
  217. $companystatic->name = $tabcompany[$key]['name'];
  218. $companystatic->code_compta = $tabcompany[$key]['code_compta'];
  219. $companystatic->code_compta_fournisseur = $tabcompany[$key]['code_compta_fournisseur'];
  220. $companystatic->code_client = $tabcompany[$key]['code_client'];
  221. $companystatic->code_fournisseur = $tabcompany[$key]['code_fournisseur'];
  222. $companystatic->fournisseur = 1;
  223. $invoicestatic->id = $key;
  224. $invoicestatic->ref = (string) $val["refsologest"];
  225. $invoicestatic->ref_supplier = $val["refsuppliersologest"];
  226. $invoicestatic->type = $val["type"];
  227. $invoicestatic->description = html_entity_decode(dol_trunc($val["description"], 32));
  228. $invoicestatic->close_code = $val["close_code"];
  229. $date = dol_print_date($val["date"], 'day');
  230. // Is it a replaced invoice ? 0=not a replaced invoice, 1=replaced invoice not yet dispatched, 2=replaced invoice dispatched
  231. $replacedinvoice = 0;
  232. if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED)
  233. {
  234. $replacedinvoice = 1;
  235. $alreadydispatched = $invoicestatic->getVentilExportCompta(); // Test if replaced invoice already into bookkeeping.
  236. if ($alreadydispatched) $replacedinvoice = 2;
  237. }
  238. // 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)
  239. if ($replacedinvoice == 1)
  240. {
  241. $db->rollback();
  242. continue;
  243. }
  244. // Error if some lines are not binded/ready to be journalized
  245. if ($errorforinvoice[$key] == 'somelinesarenotbound')
  246. {
  247. $error++;
  248. $errorforline++;
  249. setEventMessages($langs->trans('ErrorInvoiceContainsLinesNotYetBounded', $val['ref']), null, 'errors');
  250. }
  251. // Thirdparty
  252. if (! $errorforline)
  253. {
  254. foreach ( $tabttc[$key] as $k => $mt ) {
  255. //if ($mt) {
  256. $bookkeeping = new BookKeeping($db);
  257. $bookkeeping->doc_date = $val["date"];
  258. $bookkeeping->date_lim_reglement = $val["datereg"];
  259. $bookkeeping->doc_ref = $val["refsologest"];
  260. $bookkeeping->date_create = $now;
  261. $bookkeeping->doc_type = 'supplier_invoice';
  262. $bookkeeping->fk_doc = $key;
  263. $bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add
  264. $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
  265. $bookkeeping->subledger_account = $tabcompany[$key]['code_compta_fournisseur'];
  266. $bookkeeping->subledger_label = $tabcompany[$key]['name'];
  267. $bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER;
  268. $accountingaccount->fetch(null, $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER, true);
  269. $bookkeeping->label_compte = $accountingaccount->label;
  270. $bookkeeping->label_operation = dol_trunc($companystatic->name, 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("SubledgerAccount");
  271. $bookkeeping->montant = $mt;
  272. $bookkeeping->sens = ($mt >= 0) ? 'C' : 'D';
  273. $bookkeeping->debit = ($mt <= 0) ? -$mt : 0;
  274. $bookkeeping->credit = ($mt > 0) ? $mt : 0;
  275. $bookkeeping->code_journal = $journal;
  276. $bookkeeping->journal_label = $journal_label;
  277. $bookkeeping->fk_user_author = $user->id;
  278. $bookkeeping->entity = $conf->entity;
  279. $totaldebit += $bookkeeping->debit;
  280. $totalcredit += $bookkeeping->credit;
  281. $result = $bookkeeping->create($user);
  282. if ($result < 0) {
  283. if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
  284. {
  285. $error++;
  286. $errorforline++;
  287. $errorforinvoice[$key]='alreadyjournalized';
  288. //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
  289. }
  290. else
  291. {
  292. $error++;
  293. $errorforline++;
  294. $errorforinvoice[$key]='other';
  295. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  296. }
  297. }
  298. //}
  299. }
  300. }
  301. // Product / Service
  302. if (! $errorforline)
  303. {
  304. foreach ( $tabht[$key] as $k => $mt ) {
  305. //if ($mt) {
  306. // get compte id and label
  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_compte = $accountingaccount->label;
  321. $bookkeeping->label_operation = dol_trunc($companystatic->name, 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $accountingaccount->label;
  322. $bookkeeping->montant = $mt;
  323. $bookkeeping->sens = ($mt < 0) ? 'C' : 'D';
  324. $bookkeeping->debit = ($mt > 0) ? $mt : 0;
  325. $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
  326. $bookkeeping->code_journal = $journal;
  327. $bookkeeping->journal_label = $journal_label;
  328. $bookkeeping->fk_user_author = $user->id;
  329. $bookkeeping->entity = $conf->entity;
  330. $totaldebit += $bookkeeping->debit;
  331. $totalcredit += $bookkeeping->credit;
  332. $result = $bookkeeping->create($user);
  333. if ($result < 0) {
  334. if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
  335. {
  336. $error++;
  337. $errorforline++;
  338. $errorforinvoice[$key]='alreadyjournalized';
  339. //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
  340. }
  341. else
  342. {
  343. $error++;
  344. $errorforline++;
  345. $errorforinvoice[$key]='other';
  346. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  347. }
  348. }
  349. }
  350. //}
  351. }
  352. }
  353. // VAT
  354. // var_dump($tabtva);
  355. if (! $errorforline)
  356. {
  357. $listoftax=array(0, 1, 2);
  358. foreach($listoftax as $numtax)
  359. {
  360. $arrayofvat = $tabtva;
  361. if ($numtax == 1) $arrayofvat = $tablocaltax1;
  362. if ($numtax == 2) $arrayofvat = $tablocaltax2;
  363. foreach ( $arrayofvat[$key] as $k => $mt ) {
  364. if ($mt) {
  365. $bookkeeping = new BookKeeping($db);
  366. $bookkeeping->doc_date = $val["date"];
  367. $bookkeeping->date_lim_reglement = $val["datereg"];
  368. $bookkeeping->doc_ref = $val["refsologest"];
  369. $bookkeeping->date_create = $now;
  370. $bookkeeping->doc_type = 'supplier_invoice';
  371. $bookkeeping->fk_doc = $key;
  372. $bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add
  373. $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
  374. $bookkeeping->subledger_account = '';
  375. $bookkeeping->subledger_label = '';
  376. $bookkeeping->numero_compte = $k;
  377. $accountingaccount->fetch($k, null, true);
  378. $bookkeeping->label_compte = $accountingaccount->label;
  379. $bookkeeping->label_operation = dol_trunc($companystatic->name, 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("VAT").' '.join(', ',$def_tva[$key][$k]) .' %' . ($numtax?' - Localtax '.$numtax:'');
  380. $bookkeeping->montant = $mt;
  381. $bookkeeping->sens = ($mt < 0) ? 'C' : 'D';
  382. $bookkeeping->debit = ($mt > 0) ? $mt : 0;
  383. $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
  384. $bookkeeping->code_journal = $journal;
  385. $bookkeeping->journal_label = $journal_label;
  386. $bookkeeping->fk_user_author = $user->id;
  387. $bookkeeping->entity = $conf->entity;
  388. $totaldebit += $bookkeeping->debit;
  389. $totalcredit += $bookkeeping->credit;
  390. $result = $bookkeeping->create($user);
  391. if ($result < 0) {
  392. if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
  393. {
  394. $error++;
  395. $errorforline++;
  396. $errorforinvoice[$key]='alreadyjournalized';
  397. //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
  398. }
  399. else
  400. {
  401. $error++;
  402. $errorforline++;
  403. $errorforinvoice[$key]='other';
  404. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  405. }
  406. }
  407. }
  408. }
  409. }
  410. }
  411. // Counterpart of VAT for VAT NPR
  412. // var_dump($tabother);
  413. if (! $errorforline && is_array($tabother[$key]))
  414. {
  415. foreach ( $tabother[$key] as $k => $mt ) {
  416. if ($mt) {
  417. $bookkeeping = new BookKeeping($db);
  418. $bookkeeping->doc_date = $val["date"];
  419. $bookkeeping->date_lim_reglement = $val["datereg"];
  420. $bookkeeping->doc_ref = $val["refsologest"];
  421. $bookkeeping->date_create = $now;
  422. $bookkeeping->doc_type = 'supplier_invoice';
  423. $bookkeeping->fk_doc = $key;
  424. $bookkeeping->fk_docdet = 0; // Useless, can be several lines that are source of this record to add
  425. $bookkeeping->thirdparty_code = $companystatic->code_fournisseur;
  426. $bookkeeping->subledger_account = '';
  427. $bookkeeping->subledger_label = '';
  428. $bookkeeping->numero_compte = $k;
  429. $bookkeeping->label_operation = dol_trunc($companystatic->name, 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("VAT").' NPR';
  430. $bookkeeping->montant = $mt;
  431. $bookkeeping->sens = ($mt < 0) ? 'C' : 'D';
  432. $bookkeeping->debit = ($mt > 0) ? $mt : 0;
  433. $bookkeeping->credit = ($mt <= 0) ? -$mt : 0;
  434. $bookkeeping->code_journal = $journal;
  435. $bookkeeping->journal_label = $journal_label;
  436. $bookkeeping->fk_user_author = $user->id;
  437. $bookkeeping->entity = $conf->entity;
  438. $totaldebit += $bookkeeping->debit;
  439. $totalcredit += $bookkeeping->credit;
  440. $result = $bookkeeping->create($user);
  441. if ($result < 0) {
  442. if ($bookkeeping->error == 'BookkeepingRecordAlreadyExists') // Already exists
  443. {
  444. $error++;
  445. $errorforline++;
  446. $errorforinvoice[$key]='alreadyjournalized';
  447. //setEventMessages('Transaction for ('.$bookkeeping->doc_type.', '.$bookkeeping->fk_doc.', '.$bookkeeping->fk_docdet.') were already recorded', null, 'warnings');
  448. }
  449. else
  450. {
  451. $error++;
  452. $errorforline++;
  453. $errorforinvoice[$key]='other';
  454. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  455. }
  456. }
  457. }
  458. }
  459. }
  460. // Protection against a bug on lines before
  461. if (! $errorforline && (price2num($totaldebit) != price2num($totalcredit)))
  462. {
  463. $error++;
  464. $errorforline++;
  465. $errorforinvoice[$key]='amountsnotbalanced';
  466. setEventMessages('Try to insert a non balanced transaction in book for '.$invoicestatic->ref.'. Canceled. Surely a bug.', null, 'errors');
  467. }
  468. if (! $errorforline)
  469. {
  470. $db->commit();
  471. }
  472. else
  473. {
  474. $db->rollback();
  475. if ($error >= 10)
  476. {
  477. setEventMessages($langs->trans("ErrorTooManyErrorsProcessStopped"), null, 'errors');
  478. break; // Break in the foreach
  479. }
  480. }
  481. }
  482. $tabpay = $tabfac;
  483. if (empty($error) && count($tabpay) > 0) {
  484. setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs');
  485. }
  486. elseif (count($tabpay) == $error)
  487. {
  488. setEventMessages($langs->trans("NoNewRecordSaved"), null, 'warnings');
  489. }
  490. else
  491. {
  492. setEventMessages($langs->trans("GeneralLedgerSomeRecordWasNotRecorded"), null, 'warnings');
  493. }
  494. $action='';
  495. // Must reload data, so we make a redirect
  496. if (count($tabpay) != $error)
  497. {
  498. $param='id_journal='.$id_journal;
  499. $param.='&date_startday='.$date_startday;
  500. $param.='&date_startmonth='.$date_startmonth;
  501. $param.='&date_startyear='.$date_startyear;
  502. $param.='&date_endday='.$date_endday;
  503. $param.='&date_endmonth='.$date_endmonth;
  504. $param.='&date_endyear='.$date_endyear;
  505. $param.='&in_bookkeeping='.$in_bookkeeping;
  506. header("Location: ".$_SERVER['PHP_SELF'].($param?'?'.$param:''));
  507. exit;
  508. }
  509. }
  510. /*
  511. * View
  512. */
  513. $form = new Form($db);
  514. // Export
  515. if ($action == 'exportcsv') { // ISO and not UTF8 !
  516. $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
  517. $filename = 'journal';
  518. $type_export = 'journal';
  519. include DOL_DOCUMENT_ROOT . '/accountancy/tpl/export_journal.tpl.php';
  520. $companystatic = new Fournisseur($db);
  521. $invoicestatic = new FactureFournisseur($db);
  522. foreach ( $tabfac as $key => $val )
  523. {
  524. $companystatic->id = $tabcompany[$key]['id'];
  525. $companystatic->name = $tabcompany[$key]['name'];
  526. $companystatic->code_compta = $tabcompany[$key]['code_compta'];
  527. $companystatic->code_compta_fournisseur = $tabcompany[$key]['code_compta_fournisseur'];
  528. $companystatic->code_client = $tabcompany[$key]['code_client'];
  529. $companystatic->code_fournisseur = $tabcompany[$key]['code_fournisseur'];
  530. $companystatic->fournisseur = 1;
  531. $invoicestatic->id = $key;
  532. $invoicestatic->ref = $val["refsologest"];
  533. $invoicestatic->ref_supplier = $val["refsuppliersologest"];
  534. $invoicestatic->type = $val["type"];
  535. $invoicestatic->description = dol_trunc(html_entity_decode($val["description"]), 32);
  536. $invoicestatic->close_code = $val["close_code"];
  537. $date = dol_print_date($val["date"], 'day');
  538. // Is it a replaced invoice ? 0=not a replaced invoice, 1=replaced invoice not yet dispatched, 2=replaced invoice dispatched
  539. $replacedinvoice = 0;
  540. if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED)
  541. {
  542. $replacedinvoice = 1;
  543. $alreadydispatched = $invoicestatic->getVentilExportCompta(); // Test if replaced invoice already into bookkeeping.
  544. if ($alreadydispatched) $replacedinvoice = 2;
  545. }
  546. // 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)
  547. if ($replacedinvoice == 1)
  548. {
  549. continue;
  550. }
  551. // Third party
  552. foreach ( $tabttc[$key] as $k => $mt ) {
  553. //if ($mt) {
  554. print '"' . $key . '"' . $sep;
  555. print '"' . $date . '"' . $sep;
  556. print '"' . $val["refsologest"] . '"' . $sep;
  557. print '"' . utf8_decode ( dol_trunc($companystatic->name, 32) ). '"' . $sep;
  558. print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
  559. print '"' . $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER . '"' . $sep;
  560. print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
  561. print '"' . $langs->trans("Thirdparty") . '"' . $sep;
  562. print '"' . utf8_decode ( dol_trunc($companystatic->name, 16) ) . ' - ' . $val["refsuppliersologest"] . ' - ' . $langs->trans("Thirdparty") . '"' . $sep;
  563. print '"' . ($mt < 0 ? price(- $mt) : '') . '"' . $sep;
  564. print '"' . ($mt >= 0 ? price($mt) : '') . '"'. $sep;
  565. print '"' . $journal . '"' ;
  566. print "\n";
  567. //}
  568. }
  569. // Product / Service
  570. foreach ( $tabht[$key] as $k => $mt ) {
  571. $accountingaccount = new AccountingAccount($db);
  572. $accountingaccount->fetch(null, $k, true);
  573. //if ($mt) {
  574. print '"' . $key . '"' . $sep;
  575. print '"' . $date . '"' . $sep;
  576. print '"' . $val["refsologest"] . '"' . $sep;
  577. print '"' . utf8_decode ( dol_trunc($companystatic->name, 32) ) . '"' . $sep;
  578. print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep;
  579. print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep;
  580. print '""' . $sep;
  581. print '"' . utf8_decode ( dol_trunc($accountingaccount->label, 32) ) . '"' . $sep;
  582. print '"' . utf8_decode ( dol_trunc($companystatic->name, 16) ) . ' - ' . $val["refsuppliersologest"] . ' - ' . dol_trunc($accountingaccount->label, 32) . '"' . $sep;
  583. print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep;
  584. print '"' . ($mt < 0 ? price(- $mt) : '') . '"'. $sep;
  585. print '"' . $journal . '"' ;
  586. print "\n";
  587. //}
  588. }
  589. // VAT
  590. $listoftax = array(0, 1, 2);
  591. foreach ($listoftax as $numtax) {
  592. $arrayofvat = $tabtva;
  593. if ($numtax == 1) $arrayofvat = $tablocaltax1;
  594. if ($numtax == 2) $arrayofvat = $tablocaltax2;
  595. foreach ($arrayofvat[$key] as $k => $mt) {
  596. if ($mt) {
  597. print '"' . $key . '"' . $sep;
  598. print '"' . $date . '"' . $sep;
  599. print '"' . $val["refsologest"] . '"' . $sep;
  600. print '"' . utf8_decode ( dol_trunc($companystatic->name, 32) ) . '"' . $sep;
  601. print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep;
  602. print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep;
  603. print '""' . $sep;
  604. print '"' . $langs->trans("VAT") . ' - ' . $def_tva[$key] . '"' . $sep;
  605. print '"' . utf8_decode(dol_trunc($companystatic->name, 16) ) . ' - ' . $val["refsuppliersologest"] . ' - ' . $langs->trans("VAT") . join(', ',$def_tva[$key][$k]) .' %' . ($numtax?' - Localtax '.$numtax:'') . '"' . $sep;
  606. print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep;
  607. print '"' . ($mt < 0 ? price(- $mt) : '') . '"'. $sep;
  608. print '"' . $journal . '"' ;
  609. print "\n";
  610. }
  611. }
  612. // VAT counterpart for NPR
  613. if (is_array($tabother[$key]))
  614. {
  615. foreach ( $tabother[$key] as $k => $mt ) {
  616. if ($mt) {
  617. print '"' . $key . '"' . $sep;
  618. print '"' . $date . '"' . $sep;
  619. print '"' . $val["refsologest"] . '"' . $sep;
  620. print '"' . utf8_decode ( dol_trunc($companystatic->name, 32) ). '"' . $sep;
  621. print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
  622. print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
  623. print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
  624. print '"' . $langs->trans("Thirdparty") . '"' . $sep;
  625. print '"' . utf8_decode ( dol_trunc($companystatic->name, 16) ) . ' - ' . $val["refsuppliersologest"] . ' - ' . $langs->trans("VAT") . ' NPR"' . $sep;
  626. print '"' . ($mt < 0 ? price(- $mt) : '') . '"' . $sep;
  627. print '"' . ($mt >= 0 ? price($mt) : '') . '"'. $sep;
  628. print '"' . $journal . '"' ;
  629. print "\n";
  630. }
  631. }
  632. }
  633. }
  634. }
  635. }
  636. if (empty($action) || $action == 'view') {
  637. llxHeader('', $langs->trans("PurchasesJournal"));
  638. $nom = $langs->trans("PurchasesJournal") . ' | ' . $accountingjournalstatic->getNomUrl(0,1,1,'',1);
  639. $nomlink = '';
  640. $periodlink = '';
  641. $exportlink = '';
  642. $builddate=dol_now();
  643. $description.= $langs->trans("DescJournalOnlyBindedVisible").'<br>';
  644. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  645. $description .= $langs->trans("DepositsAreNotIncluded");
  646. } else {
  647. $description .= $langs->trans("DepositsAreIncluded");
  648. }
  649. $listofchoices=array('notyet'=>$langs->trans("NotYetInGeneralLedger"), 'already'=>$langs->trans("AlreadyInGeneralLedger"));
  650. $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);
  651. $period .= ' - ' .$langs->trans("JournalizationInLedgerStatus").' '. $form->selectarray('in_bookkeeping', $listofchoices, $in_bookkeeping, 1);
  652. $varlink = 'id_journal=' . $id_journal;
  653. journalHead($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array('action' => ''), '', $varlink);
  654. // Button to write into Ledger
  655. if (($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == "") || $conf->global->ACCOUNTING_ACCOUNT_SUPPLIER == '-1') {
  656. print img_warning().' '.$langs->trans("SomeMandatoryStepsOfSetupWereNotDone");
  657. print ' : '.$langs->trans("AccountancyAreaDescMisc", 4, '<strong>'.$langs->transnoentitiesnoconv("MenuAccountancy").'-'.$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 (empty($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></td>";
  693. print "<td>" . $langs->trans("Date") . "</td>";
  694. print "<td>" . $langs->trans("Piece") . ' (' . $langs->trans("InvoiceRef") . ")</td>";
  695. print "<td>" . $langs->trans("AccountAccounting") . "</td>";
  696. print "<td>" . $langs->trans("SubledgerAccount") . "</td>";
  697. print "<td>" . $langs->trans("LabelOperation") . "</td>";
  698. print "<td class='right'>" . $langs->trans("Debit") . "</td>";
  699. print "<td class='right'>" . $langs->trans("Credit") . "</td>";
  700. print "</tr>\n";
  701. $r = '';
  702. $invoicestatic = new FactureFournisseur($db);
  703. $companystatic = new Fournisseur($db);
  704. foreach ( $tabfac as $key => $val )
  705. {
  706. $companystatic->id = $tabcompany[$key]['id'];
  707. $companystatic->name = $tabcompany[$key]['name'];
  708. $companystatic->code_compta = $tabcompany[$key]['code_compta'];
  709. $companystatic->code_compta_fournisseur = $tabcompany[$key]['code_compta_fournisseur'];
  710. $companystatic->code_client = $tabcompany[$key]['code_client'];
  711. $companystatic->code_fournisseur = $tabcompany[$key]['code_fournisseur'];
  712. $companystatic->fournisseur = 1;
  713. $invoicestatic->id = $key;
  714. $invoicestatic->ref = $val["refsologest"];
  715. $invoicestatic->ref_supplier = $val["refsuppliersologest"];
  716. $invoicestatic->type = $val["type"];
  717. $invoicestatic->description = dol_trunc(html_entity_decode($val["description"]), 32);
  718. $invoicestatic->close_code = $val["close_code"];
  719. $date = dol_print_date($val["date"], 'day');
  720. // Is it a replaced invoice ? 0=not a replaced invoice, 1=replaced invoice not yet dispatched, 2=replaced invoice dispatched
  721. $replacedinvoice = 0;
  722. if ($invoicestatic->close_code == FactureFournisseur::CLOSECODE_REPLACED)
  723. {
  724. $replacedinvoice = 1;
  725. $alreadydispatched = $invoicestatic->getVentilExportCompta(); // Test if replaced invoice already into bookkeeping.
  726. if ($alreadydispatched) $replacedinvoice = 2;
  727. }
  728. // If not already into bookkeeping, we won't add it, if yes, add the counterpart ???.
  729. if ($replacedinvoice == 1)
  730. {
  731. print '<tr class="oddeven">';
  732. print "<td><!-- Replaced invoice --></td>";
  733. print "<td>" . $date . "</td>";
  734. print "<td><strike>" . $invoicestatic->getNomUrl(1) . "</strike></td>";
  735. // Account
  736. print "<td>";
  737. print $langs->trans("Replaced");
  738. print '</td>';
  739. // Subledger account
  740. print "<td>";
  741. print '</td>';
  742. print "<td>";
  743. print "</td>";
  744. print '<td class="right"></td>';
  745. print '<td class="right"></td>';
  746. print "</tr>";
  747. continue;
  748. }
  749. if ($errorforinvoice[$key] == 'somelinesarenotbound')
  750. {
  751. print '<tr class="oddeven">';
  752. print "<td><!-- Some lines are not bound --></td>";
  753. print "<td>" . $date . "</td>";
  754. print "<td>" . $invoicestatic->getNomUrl(1) . "</td>";
  755. // Account
  756. print "<td>";
  757. print '<span class="error">'.$langs->trans('ErrorInvoiceContainsLinesNotYetBoundedShort', $val['ref']).'</span>';
  758. print '</td>';
  759. // Subledger account
  760. print "<td>";
  761. print '</td>';
  762. print "<td>";
  763. print "</td>";
  764. print '<td class="right"></td>';
  765. print '<td class="right"></td>';
  766. print "</tr>";
  767. }
  768. // Third party
  769. foreach ( $tabttc[$key] as $k => $mt ) {
  770. //if ($mt) {
  771. print '<tr class="oddeven">';
  772. print "<td><!-- Thirdparty --></td>";
  773. print "<td>" . $date . "</td>";
  774. print "<td>" . $invoicestatic->getNomUrl(1) . "</td>";
  775. // Account
  776. print "<td>";
  777. $accountoshow = length_accounta($conf->global->ACCOUNTING_ACCOUNT_SUPPLIER);
  778. if (($accountoshow == "") || $accountoshow == 'NotDefined')
  779. {
  780. print '<span class="error">'.$langs->trans("MainAccountForSuppliersNotDefined").'</span>';
  781. }
  782. else print $accountoshow;
  783. print '</td>';
  784. // Subledger account
  785. print "<td>";
  786. $accountoshow = length_accounta($k);
  787. if (($accountoshow == "") || $accountoshow == 'NotDefined')
  788. {
  789. print '<span class="error">'.$langs->trans("ThirdpartyAccountNotDefined").'</span>';
  790. }
  791. else print $accountoshow;
  792. print '</td>';
  793. print "<td>" . $companystatic->getNomUrl(0, 'supplier', 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("SubledgerAccount") . "</td>";
  794. print '<td class="right">'. ($mt < 0 ? price(- $mt) : '') . "</td>";
  795. print '<td class="right">' . ($mt >= 0 ? price($mt) : '') . "</td>";
  796. print "</tr>";
  797. //}
  798. }
  799. // Product / Service
  800. foreach ( $tabht[$key] as $k => $mt ) {
  801. $accountingaccount = new AccountingAccount($db);
  802. $accountingaccount->fetch(null, $k, true);
  803. //if ($mt) {
  804. print '<tr class="oddeven">';
  805. print "<td><!-- Product --></td>";
  806. print "<td>" . $date . "</td>";
  807. print "<td>" . $invoicestatic->getNomUrl(1) . "</td>";
  808. // Account
  809. print "<td>";
  810. $accountoshow = length_accountg($k);
  811. if (($accountoshow == "") || $accountoshow == 'NotDefined')
  812. {
  813. print '<span class="error">'.$langs->trans("ProductAccountNotDefined").'</span>';
  814. }
  815. else print $accountoshow;
  816. print "</td>";
  817. // Subledger account
  818. print "<td>";
  819. print '</td>';
  820. $companystatic->id = $tabcompany[$key]['id'];
  821. $companystatic->name = $tabcompany[$key]['name'];
  822. print "<td>" . $companystatic->getNomUrl(0, 'supplier', 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $accountingaccount->label . "</td>";
  823. print '<td class="right">' . ($mt >= 0 ? price($mt) : '') . "</td>";
  824. print '<td class="right">' . ($mt < 0 ? price(- $mt) : '') . "</td>";
  825. print "</tr>";
  826. //}
  827. }
  828. // VAT
  829. $listoftax = array(0, 1, 2);
  830. foreach ($listoftax as $numtax) {
  831. $arrayofvat = $tabtva;
  832. if ($numtax == 1) $arrayofvat = $tablocaltax1;
  833. if ($numtax == 2) $arrayofvat = $tablocaltax2;
  834. foreach ( $arrayofvat[$key] as $k => $mt ) {
  835. if ($mt) {
  836. print '<tr class="oddeven">';
  837. print "<td><!-- VAT --></td>";
  838. print "<td>" . $date . "</td>";
  839. print "<td>" . $invoicestatic->getNomUrl(1) . "</td>";
  840. // Account
  841. print "<td>";
  842. $accountoshow = length_accountg($k);
  843. if (($accountoshow == "") || $accountoshow == 'NotDefined')
  844. {
  845. print '<span class="error">'.$langs->trans("VATAccountNotDefined").' ('.$langs->trans("Purchase").')'.'</span>';
  846. }
  847. else print $accountoshow;
  848. print "</td>";
  849. // Subledger account
  850. print "<td>";
  851. print '</td>';
  852. print "<td>";
  853. print $companystatic->getNomUrl(0, 'supplier', 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("VAT"). ' '.join(', ',$def_tva[$key][$k]).' %'.($numtax?' - Localtax '.$numtax:'');
  854. print "</td>";
  855. print '<td class="right">' . ($mt >= 0 ? price($mt) : '') . "</td>";
  856. print '<td class="right">' . ($mt < 0 ? price(- $mt) : '') . "</td>";
  857. print "</tr>";
  858. }
  859. }
  860. }
  861. // VAT counterpart for NPR
  862. if (is_array($tabother[$key]))
  863. {
  864. foreach ( $tabother[$key] as $k => $mt ) {
  865. if ($mt) {
  866. print '<tr class="oddeven">';
  867. print "<td><!-- VAT counterpart NPR --></td>";
  868. print "<td>" . $date . "</td>";
  869. print "<td>" . $invoicestatic->getNomUrl(1) . "</td>";
  870. // Account
  871. print "<td>";
  872. $accountoshow = length_accountg($k);
  873. if (($accountoshow == "") || $accountoshow == 'NotDefined')
  874. {
  875. print '<span class="error">'.$langs->trans("VATAccountNotDefined").' ('.$langs->trans("NPR counterpart").'). Set ACCOUNTING_COUNTERPART_VAT_NPR to the subvention account'.'</span>';
  876. }
  877. else print $accountoshow;
  878. print '</td>';
  879. // Subledger account
  880. print "<td>";
  881. print '</td>';
  882. print "<td>" . $companystatic->getNomUrl(0, 'supplier', 16) . ' - ' . $invoicestatic->ref_supplier . ' - ' . $langs->trans("VAT") . " NPR (counterpart)</td>";
  883. print '<td class="right">' . ($mt < 0 ? price(- $mt) : '') . "</td>";
  884. print '<td class="right">' . ($mt >= 0 ? price($mt) : '') . "</td>";
  885. print "</tr>";
  886. }
  887. }
  888. }
  889. }
  890. print "</table>";
  891. print '</div>';
  892. // End of page
  893. llxFooter();
  894. }
  895. $db->close();