sellsjournal.php 38 KB

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