sellsjournal.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561
  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@dolibarr.fr>
  6. * Copyright (C) 2013 Christophe Battarel <christophe.battarel@altairis.fr>
  7. * Copyright (C) 2013-2016 Alexandre Spangaro <aspangaro.dolibarr@gmail.com>
  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. *
  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/sellsjournal.php
  27. * \ingroup Advanced accountancy
  28. * \brief Page with sells journal
  29. */
  30. require '../../main.inc.php';
  31. // Class
  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 . '/compta/facture/class/facture.class.php';
  36. require_once DOL_DOCUMENT_ROOT . '/societe/class/client.class.php';
  37. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/bookkeeping.class.php';
  38. require_once DOL_DOCUMENT_ROOT . '/accountancy/class/accountingaccount.class.php';
  39. // Langs
  40. $langs->load("commercial");
  41. $langs->load("compta");
  42. $langs->load("bills");
  43. $langs->load("other");
  44. $langs->load("main");
  45. $langs->load("accountancy");
  46. $date_startmonth = GETPOST('date_startmonth');
  47. $date_startday = GETPOST('date_startday');
  48. $date_startyear = GETPOST('date_startyear');
  49. $date_endmonth = GETPOST('date_endmonth');
  50. $date_endday = GETPOST('date_endday');
  51. $date_endyear = GETPOST('date_endyear');
  52. $now = dol_now();
  53. // Security check
  54. if ($user->societe_id > 0)
  55. accessforbidden();
  56. $action = GETPOST('action');
  57. /*
  58. * View
  59. */
  60. $year_current = strftime("%Y", dol_now());
  61. $pastmonth = strftime("%m", dol_now()) - 1;
  62. $pastmonthyear = $year_current;
  63. if ($pastmonth == 0) {
  64. $pastmonth = 12;
  65. $pastmonthyear --;
  66. }
  67. $date_start = dol_mktime(0, 0, 0, $date_startmonth, $date_startday, $date_startyear);
  68. $date_end = dol_mktime(23, 59, 59, $date_endmonth, $date_endday, $date_endyear);
  69. if (empty($date_start) || empty($date_end)) // We define date_start and date_end
  70. {
  71. $date_start = dol_get_first_day($pastmonthyear, $pastmonth, false);
  72. $date_end = dol_get_last_day($pastmonthyear, $pastmonth, false);
  73. }
  74. $p = explode(":", $conf->global->MAIN_INFO_SOCIETE_COUNTRY);
  75. $idpays = $p[0];
  76. $sql = "SELECT f.rowid, f.facnumber, f.type, f.datef as df, f.ref_client,";
  77. $sql .= " fd.rowid as fdid, fd.description, fd.product_type, fd.total_ht, fd.total_tva, fd.tva_tx, fd.total_ttc,";
  78. $sql .= " s.rowid as socid, s.nom as name, s.code_compta, s.code_client,";
  79. $sql .= " p.rowid as pid, p.ref as pref, p.accountancy_code_sell, aa.rowid as fk_compte, aa.account_number as compte, aa.label as label_compte,";
  80. $sql .= " fd.situation_percent,ct.accountancy_code_sell as account_tva";
  81. $sql .= " FROM " . MAIN_DB_PREFIX . "facturedet as fd";
  82. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "product as p ON p.rowid = fd.fk_product";
  83. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "accounting_account as aa ON aa.rowid = fd.fk_code_ventilation";
  84. $sql .= " JOIN " . MAIN_DB_PREFIX . "facture as f ON f.rowid = fd.fk_facture";
  85. $sql .= " JOIN " . MAIN_DB_PREFIX . "societe as s ON s.rowid = f.fk_soc";
  86. $sql .= " LEFT JOIN " . MAIN_DB_PREFIX . "c_tva as ct ON fd.tva_tx = ct.taux AND ct.fk_pays = '" . $idpays . "'";
  87. $sql .= " WHERE fd.fk_code_ventilation > 0 ";
  88. if (! empty($conf->multicompany->enabled)) {
  89. $sql .= " AND f.entity IN (" . getEntity("facture", 1) . ")";
  90. }
  91. $sql .= " AND f.fk_statut > 0";
  92. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  93. $sql .= " AND f.type IN (" . Facture::TYPE_STANDARD . "," . Facture::TYPE_REPLACEMENT . "," . Facture::TYPE_CREDIT_NOTE . "," . Facture::TYPE_SITUATION . ")";
  94. } else {
  95. $sql .= " AND f.type IN (" . Facture::TYPE_STANDARD . "," . Facture::TYPE_STANDARD . "," . Facture::TYPE_CREDIT_NOTE . "," . Facture::TYPE_DEPOSIT . "," . Facture::TYPE_SITUATION . ")";
  96. }
  97. $sql .= " AND fd.product_type IN (0,1)";
  98. if ($date_start && $date_end)
  99. $sql .= " AND f.datef >= '" . $db->idate($date_start) . "' AND f.datef <= '" . $db->idate($date_end) . "'";
  100. $sql .= " ORDER BY f.datef";
  101. dol_syslog('accountancy/journal/sellsjournal.php', LOG_DEBUG);
  102. $result = $db->query($sql);
  103. if ($result) {
  104. $tabfac = array ();
  105. $tabht = array ();
  106. $tabtva = array ();
  107. $def_tva = array ();
  108. $tabttc = array ();
  109. $tabcompany = array ();
  110. $num = $db->num_rows($result);
  111. $i = 0;
  112. while ( $i < $num ) {
  113. $obj = $db->fetch_object($result);
  114. // les variables
  115. $cptcli = (! empty($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER)) ? $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER : $langs->trans("CodeNotDef");
  116. $compta_soc = (! empty($obj->code_compta)) ? $obj->code_compta : $cptcli;
  117. $compta_prod = $obj->compte;
  118. if (empty($compta_prod)) {
  119. if ($obj->product_type == 0)
  120. $compta_prod = (! empty($conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT)) ? $conf->global->ACCOUNTING_PRODUCT_SOLD_ACCOUNT : $langs->trans("CodeNotDef");
  121. else
  122. $compta_prod = (! empty($conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT)) ? $conf->global->ACCOUNTING_SERVICE_SOLD_ACCOUNT : $langs->trans("CodeNotDef");
  123. }
  124. $cpttva = (! empty($conf->global->ACCOUNTING_VAT_SOLD_ACCOUNT)) ? $conf->global->ACCOUNTING_VAT_SOLD_ACCOUNT : $langs->trans("CodeNotDef");
  125. $compta_tva = (! empty($obj->account_tva) ? $obj->account_tva : $cpttva);
  126. //Define array for display vat tx
  127. $def_tva[$obj->rowid]=price($obj->tva_tx);
  128. // Situation invoices handling
  129. $line = new FactureLigne($db);
  130. $line->fetch($obj->fdid);
  131. $prev_progress = $line->get_prev_progress($obj->fdid);
  132. if ($obj->type == Facture::TYPE_SITUATION) {
  133. // Avoid divide by 0
  134. if ($obj->situation_percent == 0) {
  135. $situation_ratio = 0;
  136. } else {
  137. $situation_ratio = ($obj->situation_percent - $prev_progress) / $obj->situation_percent;
  138. }
  139. } else {
  140. $situation_ratio = 1;
  141. }
  142. // Invoice lines
  143. $tabfac[$obj->rowid]["date"] = $obj->df;
  144. $tabfac[$obj->rowid]["ref"] = $obj->facnumber;
  145. $tabfac[$obj->rowid]["type"] = $obj->type;
  146. $tabfac[$obj->rowid]["description"] = $obj->label_compte;
  147. $tabfac[$obj->rowid]["fk_facturedet"] = $obj->fdid;
  148. if (! isset($tabttc[$obj->rowid][$compta_soc]))
  149. $tabttc[$obj->rowid][$compta_soc] = 0;
  150. if (! isset($tabht[$obj->rowid][$compta_prod]))
  151. $tabht[$obj->rowid][$compta_prod] = 0;
  152. if (! isset($tabtva[$obj->rowid][$compta_tva]))
  153. $tabtva[$obj->rowid][$compta_tva] = 0;
  154. $tabttc[$obj->rowid][$compta_soc] += $obj->total_ttc * $situation_ratio;
  155. $tabht[$obj->rowid][$compta_prod] += $obj->total_ht * $situation_ratio;
  156. $tabtva[$obj->rowid][$compta_tva] += $obj->total_tva * $situation_ratio;
  157. $tabcompany[$obj->rowid] = array (
  158. 'id' => $obj->socid,
  159. 'name' => $obj->name,
  160. 'code_client' => $obj->code_compta
  161. );
  162. $i ++;
  163. }
  164. } else {
  165. dol_print_error($db);
  166. }
  167. /*
  168. * Action
  169. * FIXME Action must be set before any view part to respect MVC
  170. */
  171. // Bookkeeping Write
  172. if ($action == 'writebookkeeping') {
  173. $now = dol_now();
  174. $error = 0;
  175. foreach ( $tabfac as $key => $val ) {
  176. $companystatic = new Societe($db);
  177. $invoicestatic = new Facture($db);
  178. $companystatic->id = $tabcompany[$key]['id'];
  179. $companystatic->name = $tabcompany[$key]['name'];
  180. $companystatic->client = $tabcompany[$key]['code_client'];
  181. $invoicestatic->id = $key;
  182. $invoicestatic->ref = (string) $val["ref"];
  183. foreach ( $tabttc[$key] as $k => $mt ) {
  184. $bookkeeping = new BookKeeping($db);
  185. $bookkeeping->doc_date = $val["date"];
  186. $bookkeeping->doc_ref = $val["ref"];
  187. $bookkeeping->date_create = $now;
  188. $bookkeeping->doc_type = 'customer_invoice';
  189. $bookkeeping->fk_doc = $key;
  190. $bookkeeping->fk_docdet = $val["fk_facturedet"];
  191. $bookkeeping->code_tiers = $tabcompany[$key]['code_client'];
  192. $bookkeeping->numero_compte = $conf->global->ACCOUNTING_ACCOUNT_CUSTOMER;
  193. // $bookkeeping->label_compte = $tabcompany[$key]['name'];
  194. $bookkeeping->label_compte = utf8_decode(dol_trunc($companystatic->name, 16)) . ' - ' . $invoicestatic->ref . ' - ' . $langs->trans("Code_tiers");
  195. $bookkeeping->montant = $mt;
  196. $bookkeeping->sens = ($mt >= 0) ? 'D' : 'C';
  197. $bookkeeping->debit = ($mt >= 0) ? $mt : 0;
  198. $bookkeeping->credit = ($mt < 0) ? $mt : 0;
  199. $bookkeeping->code_journal = $conf->global->ACCOUNTING_SELL_JOURNAL;
  200. $bookkeeping->fk_user_author = $user->id;
  201. $result = $bookkeeping->create($user);
  202. if ($result < 0) {
  203. $error ++;
  204. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  205. }
  206. }
  207. // Product / Service
  208. foreach ( $tabht[$key] as $k => $mt ) {
  209. if ($mt) {
  210. // get compte id and label
  211. $accountingaccount = new AccountingAccount($db);
  212. if ($accountingaccount->fetch(null, $k)) {
  213. $bookkeeping = new BookKeeping($db);
  214. $bookkeeping->doc_date = $val["date"];
  215. $bookkeeping->doc_ref = $val["ref"];
  216. $bookkeeping->date_create = $now;
  217. $bookkeeping->doc_type = 'customer_invoice';
  218. $bookkeeping->fk_doc = $key;
  219. $bookkeeping->fk_docdet = $val["fk_facturedet"];
  220. $bookkeeping->code_tiers = '';
  221. $bookkeeping->numero_compte = $k;
  222. $bookkeeping->label_compte = utf8_decode(dol_trunc($companystatic->name, 16)) . ' - ' . $invoicestatic->ref . ' - ' . utf8_decode($accountingaccount->label);
  223. $bookkeeping->montant = $mt;
  224. $bookkeeping->sens = ($mt < 0) ? 'D' : 'C';
  225. $bookkeeping->debit = ($mt < 0) ? $mt : 0;
  226. $bookkeeping->credit = ($mt >= 0) ? $mt : 0;
  227. $bookkeeping->code_journal = $conf->global->ACCOUNTING_SELL_JOURNAL;
  228. $bookkeeping->fk_user_author = $user->id;
  229. $result = $bookkeeping->create($user);
  230. if ($result < 0) {
  231. $error ++;
  232. setEventMessages($bookkeeping->error, $bookkeeping->errors, 'errors');
  233. }
  234. }
  235. }
  236. }
  237. // VAT
  238. // var_dump($tabtva);
  239. foreach ( $tabtva[$key] as $k => $mt ) {
  240. if ($mt) {
  241. $bookkeeping = new BookKeeping($db);
  242. $bookkeeping->doc_date = $val["date"];
  243. $bookkeeping->doc_ref = $val["ref"];
  244. $bookkeeping->date_create = $now;
  245. $bookkeeping->doc_type = 'customer_invoice';
  246. $bookkeeping->fk_doc = $key;
  247. $bookkeeping->fk_docdet = $val["fk_facturedet"];
  248. $bookkeeping->code_tiers = '';
  249. $bookkeeping->numero_compte = $k;
  250. $bookkeeping->label_compte = utf8_decode(dol_trunc($companystatic->name, 16)) . ' - ' . $invoicestatic->ref . ' - ' . $langs->trans("VAT").' '.$def_tva[$key];
  251. $bookkeeping->montant = $mt;
  252. $bookkeeping->sens = ($mt < 0) ? 'D' : 'C';
  253. $bookkeeping->debit = ($mt < 0) ? $mt : 0;
  254. $bookkeeping->credit = ($mt >= 0) ? $mt : 0;
  255. $bookkeeping->code_journal = $conf->global->ACCOUNTING_SELL_JOURNAL;
  256. $bookkeeping->fk_user_author = $user->id;
  257. $result = $bookkeeping->create($user);
  258. if ($result < 0) {
  259. $error ++;
  260. setEventMessages($object->error, $object->errors, 'errors');
  261. }
  262. }
  263. }
  264. }
  265. if (empty($error)) {
  266. setEventMessages($langs->trans("GeneralLedgerIsWritten"), null, 'mesgs');
  267. }
  268. }
  269. // Export
  270. if ($action == 'export_csv') {
  271. $sep = $conf->global->ACCOUNTING_EXPORT_SEPARATORCSV;
  272. $sell_journal = $conf->global->ACCOUNTING_SELL_JOURNAL;
  273. include DOL_DOCUMENT_ROOT . '/accountancy/tpl/export_journal.tpl.php';
  274. $companystatic = new Client($db);
  275. // Model Cegid Expert Export
  276. if ($conf->global->ACCOUNTING_EXPORT_MODELCSV == 2) {
  277. $sep = ";";
  278. foreach ( $tabfac as $key => $val ) {
  279. $companystatic->id = $tabcompany[$key]['id'];
  280. $companystatic->name = $tabcompany[$key]['name'];
  281. $companystatic->client = $tabcompany[$key]['code_client'];
  282. $invoicestatic->id = $key;
  283. $invoicestatic->ref = $val["ref"];
  284. $date = dol_print_date($db->jdate($val["date"]), '%d%m%Y');
  285. foreach ( $tabttc[$key] as $k => $mt ) {
  286. print $date . $sep;
  287. print $sell_journal . $sep;
  288. print length_accountg($conf->global->ACCOUNTING_ACCOUNT_CUSTOMER) . $sep;
  289. print length_accounta(html_entity_decode($k)) . $sep;
  290. print ($mt < 0 ? 'C' : 'D') . $sep;
  291. print ($mt <= 0 ? price(- $mt) : $mt) . $sep;
  292. print utf8_decode(dol_trunc($companystatic->name, 16)) . ' - ' . $invoicestatic->ref . ' - ' . $langs->trans("Code_tiers") . $sep;
  293. // print utf8_decode($companystatic->name) . $sep;
  294. print $val["ref"];
  295. print "\n";
  296. }
  297. // Product / Service
  298. foreach ( $tabht[$key] as $k => $mt ) {
  299. $accountingaccount_static = new AccountingAccount($db);
  300. if ($accountingaccount_static->fetch(null, $k)) {
  301. print $date . $sep;
  302. print $sell_journal . $sep;
  303. print length_accountg(html_entity_decode($k)) . $sep;
  304. print $sep;
  305. print ($mt < 0 ? 'D' : 'C') . $sep;
  306. print ($mt <= 0 ? price(- $mt) : $mt) . $sep;
  307. print utf8_decode(dol_trunc($companystatic->name, 16)) . ' - ' . $invoicestatic->ref . ' - ' . utf8_decode(utf8_decode($accountingaccount_static->label)) . $sep;
  308. // print dol_trunc($accountingaccount_static->label, 32) . $sep;
  309. print $val["ref"];
  310. print "\n";
  311. }
  312. }
  313. // TVA
  314. foreach ( $tabtva[$key] as $k => $mt ) {
  315. if ($mt) {
  316. print $date . $sep;
  317. print $sell_journal . $sep;
  318. print length_accountg(html_entity_decode($k)) . $sep;
  319. print $sep;
  320. print ($mt < 0 ? 'D' : 'C') . $sep;
  321. print ($mt <= 0 ? price(- $mt) : $mt) . $sep;
  322. print utf8_decode(dol_trunc($companystatic->name, 16)) . ' - ' . $invoicestatic->ref . ' - ' . $langs->trans("VAT") . $sep;
  323. // print $langs->trans("VAT") . $sep;
  324. print $val["ref"];
  325. print "\n";
  326. }
  327. }
  328. }
  329. } elseif ($conf->global->ACCOUNTING_EXPORT_MODELCSV == 1) {
  330. // Model Classic Export
  331. foreach ( $tabfac as $key => $val ) {
  332. $companystatic->id = $tabcompany[$key]['id'];
  333. $companystatic->name = $tabcompany[$key]['name'];
  334. $companystatic->client = $tabcompany[$key]['code_client'];
  335. $invoicestatic->id = $key;
  336. $invoicestatic->ref = $val["ref"];
  337. $date = dol_print_date($db->jdate($val["date"]), 'day');
  338. foreach ( $tabttc[$key] as $k => $mt ) {
  339. print '"' . $date . '"' . $sep;
  340. print '"' . $val["ref"] . '"' . $sep;
  341. print '"' . length_accounta(html_entity_decode($k)) . '"' . $sep;
  342. print '"' . utf8_decode(dol_trunc($companystatic->name, 16)) . ' - ' . $invoicestatic->ref . ' - ' . $langs->trans("Code_tiers") . '"' . $sep;
  343. // print '"' . utf8_decode($companystatic->name) . '"' . $sep;
  344. print '"' . ($mt >= 0 ? price($mt) : '') . '"' . $sep;
  345. print '"' . ($mt < 0 ? price(- $mt) : '') . '"';
  346. print "\n";
  347. }
  348. // Product / Service
  349. foreach ( $tabht[$key] as $k => $mt ) {
  350. $accountingaccount = new AccountingAccount($db);
  351. $accountingaccount->fetch(null, $k);
  352. if ($mt) {
  353. print '"' . $date . '"' . $sep;
  354. print '"' . $val["ref"] . '"' . $sep;
  355. print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep;
  356. print '"' . utf8_decode(dol_trunc($companystatic->name, 16)) . ' - ' . utf8_decode(dol_trunc($accountingaccount->label, 32)) . '"' . $sep;
  357. // print '"' . dol_trunc($accountingaccount->label, 32) . '"' . $sep;
  358. print '"' . ($mt < 0 ? price(- $mt) : '') . '"' . $sep;
  359. print '"' . ($mt >= 0 ? price($mt) : '') . '"';
  360. print "\n";
  361. }
  362. }
  363. // VAT
  364. foreach ( $tabtva[$key] as $k => $mt ) {
  365. if ($mt) {
  366. print '"' . $date . '"' . $sep;
  367. print '"' . $val["ref"] . '"' . $sep;
  368. print '"' . length_accountg(html_entity_decode($k)) . '"' . $sep;
  369. print '"' . utf8_decode(dol_trunc($companystatic->name, 16)) . ' - ' . $invoicestatic->ref . ' - ' . $langs->trans("VAT") . '"' . $sep;
  370. // print '"' . $langs->trans("VAT") . '"' . $sep;
  371. print '"' . ($mt < 0 ? price(- $mt) : '') . '"' . $sep;
  372. print '"' . ($mt >= 0 ? price($mt) : '') . '"';
  373. print "\n";
  374. }
  375. }
  376. }
  377. }
  378. } else {
  379. $form = new Form($db);
  380. llxHeader('', $langs->trans("SellsJournal"));
  381. $nom = $langs->trans("SellsJournal");
  382. $nomlink = '';
  383. $periodlink = '';
  384. $exportlink = '';
  385. $builddate = time();
  386. $description = $langs->trans("DescSellsJournal") . '<br>';
  387. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS))
  388. $description .= $langs->trans("DepositsAreNotIncluded");
  389. else
  390. $description .= $langs->trans("DepositsAreIncluded");
  391. $period = $form->select_date($date_start, 'date_start', 0, 0, 0, '', 1, 0, 1) . ' - ' . $form->select_date($date_end, 'date_end', 0, 0, 0, '', 1, 0, 1);
  392. report_header($nom, $nomlink, $period, $periodlink, $description, $builddate, $exportlink, array (
  393. 'action' => ''
  394. ));
  395. if ($conf->global->ACCOUNTING_EXPORT_MODELCSV != 1 || $conf->global->ACCOUNTING_EXPORT_MODELCSV != 2) {
  396. print '<input type="button" class="butActionRefused" style="float: right;" value="' . $langs->trans("Export") . '" disabeld="disabled" title="' . $langs->trans('ExportNotSupported') . '"/>';
  397. } else {
  398. print '<input type="button" class="butAction" style="float: right;" value="' . $langs->trans("Export") . '" onclick="launch_export();" />';
  399. }
  400. print '<input type="button" class="button" value="' . $langs->trans("WriteBookKeeping") . '" onclick="writebookkeeping();" />';
  401. print '
  402. <script type="text/javascript">
  403. function launch_export() {
  404. $("div.fiche div.tabBar form input[name=\"action\"]").val("export_csv");
  405. $("div.fiche div.tabBar form input[type=\"submit\"]").click();
  406. $("div.fiche div.tabBar form input[name=\"action\"]").val("");
  407. }
  408. function writebookkeeping() {
  409. $("div.fiche div.tabBar form input[name=\"action\"]").val("writebookkeeping");
  410. $("div.fiche div.tabBar form input[type=\"submit\"]").click();
  411. $("div.fiche div.tabBar form input[name=\"action\"]").val("");
  412. }
  413. </script>';
  414. /*
  415. * Show result array
  416. */
  417. print '<br><br>';
  418. $i = 0;
  419. print "<table class=\"noborder\" width=\"100%\">";
  420. print "<tr class=\"liste_titre\">";
  421. print "<td>" . $langs->trans("Date") . "</td>";
  422. print "<td>" . $langs->trans("Piece") . ' (' . $langs->trans("InvoiceRef") . ")</td>";
  423. print "<td>" . $langs->trans("Account") . "</td>";
  424. print "<td>" . $langs->trans("Type") . "</td>";
  425. print "<td align='right'>" . $langs->trans("Debit") . "</td>";
  426. print "<td align='right'>" . $langs->trans("Credit") . "</td>";
  427. print "</tr>\n";
  428. $var = true;
  429. $r = '';
  430. $invoicestatic = new Facture($db);
  431. $companystatic = new Client($db);
  432. foreach ( $tabfac as $key => $val ) {
  433. $invoicestatic->id = $key;
  434. $invoicestatic->ref = $val["ref"];
  435. $invoicestatic->type = $val["type"];
  436. $date = dol_print_date($db->jdate($val["date"]), 'day');
  437. // Third party
  438. foreach ( $tabttc[$key] as $k => $mt ) {
  439. print "<tr " . $bc[$var] . ">";
  440. print "<td>" . $date . "</td>";
  441. print "<td>" . $invoicestatic->getNomUrl(1) . "</td>";
  442. $companystatic->id = $tabcompany[$key]['id'];
  443. $companystatic->name = $tabcompany[$key]['name'];
  444. $companystatic->client = $tabcompany[$key]['code_client'];
  445. print "<td>" . length_accounta($k);
  446. // print "</td><td>" . $langs->trans("ThirdParty");
  447. // print ' (' . $companystatic->getNomUrl(0, 'customer', 16) . ')';
  448. print "<td>" . $companystatic->getNomUrl(0, 'customer', 16) . ' - ' . $invoicestatic->ref . ' - ' . $langs->trans("Code_tiers") . "</td>";
  449. print "</td><td align='right'>" . ($mt >= 0 ? price($mt) : '') . "</td>";
  450. print "<td align='right'>" . ($mt < 0 ? price(- $mt) : '') . "</td>";
  451. }
  452. print "</tr>";
  453. // Product / Service
  454. foreach ( $tabht[$key] as $k => $mt ) {
  455. $accountingaccount = new AccountingAccount($db);
  456. $accountingaccount->fetch(null, $k);
  457. if ($mt) {
  458. print "<tr " . $bc[$var] . ">";
  459. print "<td>" . $date . "</td>";
  460. print "<td>" . $invoicestatic->getNomUrl(1) . "</td>";
  461. print "<td>" . length_accountg($k) . "</td>";
  462. // print "<td>" . $accountingaccount->label . "</td>";
  463. print "<td>" . $companystatic->getNomUrl(0, 'customer', 16) . ' - ' . $invoicestatic->ref . ' - ' . utf8_decode(utf8_decode($accountingaccount->label)) . "</td>";
  464. print "<td align='right'>" . ($mt < 0 ? price(- $mt) : '') . "</td>";
  465. print "<td align='right'>" . ($mt >= 0 ? price($mt) : '') . "</td>";
  466. print "</tr>";
  467. }
  468. }
  469. // VAT
  470. foreach ( $tabtva[$key] as $k => $mt ) {
  471. if ($mt) {
  472. print "<tr " . $bc[$var] . ">";
  473. print "<td>" . $date . "</td>";
  474. print "<td>" . $invoicestatic->getNomUrl(1) . "</td>";
  475. print "<td>" . length_accountg($k) . "</td>";
  476. print "<td>" . $companystatic->getNomUrl(0, 'customer', 16) . ' - ' . $invoicestatic->ref . ' - ' . $langs->trans("VAT") . ' '.$def_tva[$key]. "</td>";
  477. // print "<td>" . $langs->trans("VAT") . "</td>";
  478. print "<td align='right'>" . ($mt < 0 ? price(- $mt) : '') . "</td>";
  479. print "<td align='right'>" . ($mt >= 0 ? price($mt) : '') . "</td>";
  480. print "</tr>";
  481. }
  482. }
  483. $var = ! $var;
  484. }
  485. print "</table>";
  486. // End of page
  487. llxFooter();
  488. }
  489. $db->close();