sellsjournal.php 36 KB

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