PageRenderTime 55ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/core/lib/tax.lib.php

https://github.com/asterix14/dolibarr
PHP | 561 lines | 397 code | 33 blank | 131 comment | 92 complexity | 266c9049c97cee761fd927e33273f8cf MD5 | raw file
Possible License(s): LGPL-2.0
  1. <?php
  2. /* Copyright (C) 2004-2009 Laurent Destailleur <eldy@users.sourceforge.net>
  3. * Copyright (C) 2006-2007 Yannick Warnier <ywarnier@beeznest.org>
  4. * Copyright (C) 2011 Regis Houssin <regis@dolibarr.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/core/lib/tax.lib.php
  21. * \ingroup tax
  22. * \brief Library for tax module
  23. */
  24. /**
  25. * Look for collectable VAT clients in the chosen year (and month)
  26. * @param db Database handle
  27. * @param y Year
  28. * @param date_start Start date
  29. * @param date_end End date
  30. * @param modetax 0 or 1 (option vat on debit)
  31. * @param direction 'sell' or 'buy'
  32. * @param m Month
  33. * @return array List of customers third parties with vat, -1 if no accountancy module, -2 if not yet developped, -3 if error
  34. */
  35. function vat_by_thirdparty($db, $y, $date_start, $date_end, $modetax, $direction, $m=0)
  36. {
  37. global $conf;
  38. $list=array();
  39. //print "xx".$conf->global->MAIN_MODULE_ACCOUNTING;
  40. //print "xx".$conf->global->MAIN_MODULE_COMPTABILITE;
  41. if ($direction == 'sell')
  42. {
  43. $invoicetable='facture';
  44. $invoicedettable='facturedet';
  45. $fk_facture='fk_facture';
  46. $total_tva='total_tva';
  47. $total_localtax1='total_localtax1';
  48. $total_localtax2='total_localtax2';
  49. }
  50. if ($direction == 'buy')
  51. {
  52. $invoicetable='facture_fourn';
  53. $invoicedettable='facture_fourn_det';
  54. $fk_facture='fk_facture_fourn';
  55. $total_tva='tva';
  56. $total_localtax1='total_localtax1';
  57. $total_localtax2='total_localtax2';
  58. }
  59. // Define sql request
  60. $sql='';
  61. if ($modetax == 1)
  62. {
  63. // If vat paid on due invoices (non draft)
  64. if ($conf->global->MAIN_MODULE_ACCOUNTING)
  65. {
  66. // TODO a ce jour on se sait pas la compter car le montant tva d'un payment
  67. // n'est pas stocke dans la table des payments.
  68. // Seul le module compta expert peut resoudre ce probleme.
  69. // (Il faut quand un payment a lieu, stocker en plus du montant du paiement le
  70. // detail part tva et part ht).
  71. $sql = 'TODO';
  72. }
  73. if ($conf->global->MAIN_MODULE_COMPTABILITE)
  74. {
  75. $sql = "SELECT s.rowid as socid, s.nom as nom, s.siren as tva_intra, s.tva_assuj as assuj,";
  76. $sql.= " sum(fd.total_ht) as amount, sum(fd.".$total_tva.") as tva,";
  77. $sql.= " sum(fd.".$total_localtax1.") as localtax1,";
  78. $sql.= " sum(fd.".$total_localtax2.") as localtax2";
  79. $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  80. $sql.= " ".MAIN_DB_PREFIX.$invoicedettable." as fd,";
  81. $sql.= " ".MAIN_DB_PREFIX."societe as s";
  82. $sql.= " WHERE f.entity = " . $conf->entity;
  83. $sql.= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
  84. $sql.= " AND (f.type = 0"; // Standard
  85. $sql.= " OR f.type = 1"; // Replacement
  86. $sql.= " OR f.type = 2)"; // Credit note
  87. //$sql.= " OR f.type = 3"; // We do not include deposit
  88. if ($y && $m)
  89. {
  90. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  91. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  92. }
  93. else if ($y)
  94. {
  95. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  96. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  97. }
  98. if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  99. $sql.= " AND s.rowid = f.fk_soc AND f.rowid = fd.".$fk_facture;
  100. $sql.= " GROUP BY s.rowid, s.nom, s.tva_intra, s.tva_assuj";
  101. }
  102. }
  103. else
  104. {
  105. if ($conf->global->MAIN_MODULE_ACCOUNTING)
  106. {
  107. // If vat paid on payments
  108. // TODO a ce jour on se sait pas la compter car le montant tva d'un payment
  109. // n'est pas stocke dans la table des payments.
  110. // Seul le module compta expert peut resoudre ce probleme.
  111. // (Il faut quand un payment a lieu, stocker en plus du montant du paiement le
  112. // detail part tva et part ht).
  113. $sql = 'TODO';
  114. }
  115. if ($conf->global->MAIN_MODULE_COMPTABILITE)
  116. {
  117. // Tva sur factures payes (should be on payment)
  118. /* $sql = "SELECT s.rowid as socid, s.nom as nom, s.tva_intra as tva_intra, s.tva_assuj as assuj,";
  119. $sql.= " sum(fd.total_ht) as amount, sum(".$total_tva.") as tva";
  120. $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f, ".MAIN_DB_PREFIX.$invoicetable." as fd, ".MAIN_DB_PREFIX."societe as s";
  121. $sql.= " WHERE ";
  122. $sql.= " f.fk_statut in (2)"; // Paid (partially or completely)
  123. $sql.= " AND (f.type = 0"; // Standard
  124. $sql.= " OR f.type = 1"; // Replacement
  125. $sql.= " OR f.type = 2)"; // Credit note
  126. //$sql.= " OR f.type = 3"; // We do not include deposit
  127. if ($y && $m)
  128. {
  129. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  130. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  131. }
  132. else if ($y)
  133. {
  134. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  135. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  136. }
  137. if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  138. $sql.= " AND s.rowid = f.fk_soc AND f.rowid = fd.".$fk_facture;
  139. $sql.= " GROUP BY s.rowid as socid, s.nom as nom, s.tva_intra as tva_intra, s.tva_assuj as assuj";
  140. */
  141. $sql = 'TODO';
  142. }
  143. }
  144. if (! $sql) return -1;
  145. if ($sql == 'TODO') return -2;
  146. if ($sql != 'TODO')
  147. {
  148. dol_syslog("Tax.lib:thirdparty sql=".$sql);
  149. $resql = $db->query($sql);
  150. if ($resql)
  151. {
  152. while($assoc = $db->fetch_object($resql))
  153. {
  154. $list[] = $assoc;
  155. }
  156. $db->free($resql);
  157. return $list;
  158. }
  159. else
  160. {
  161. dol_print_error($db);
  162. return -3;
  163. }
  164. }
  165. }
  166. /**
  167. * \brief Gets VAT to collect for the given year (and given quarter or month)
  168. * The function gets the VAT in split results, as the VAT declaration asks
  169. * to report the amounts for different VAT rates as different lines.
  170. * This function also accounts recurrent invoices
  171. * \param db Database handler object
  172. * \param y Year
  173. * \param q Quarter
  174. * \param date_start Start date
  175. * \param date_end End date
  176. * \param modetax 0 or 1 (option vat on debit)
  177. * \param direction 'sell' (customer invoice) or 'buy' (supplier invoices)
  178. * \param m Month
  179. * \return array List of quarters with vat
  180. */
  181. function vat_by_date($db, $y, $q, $date_start, $date_end, $modetax, $direction, $m=0)
  182. {
  183. global $conf;
  184. $list=array();
  185. if ($direction == 'sell')
  186. {
  187. $invoicetable='facture';
  188. $invoicedettable='facturedet';
  189. $fk_facture='fk_facture';
  190. $fk_facture2='fk_facture';
  191. $fk_payment='fk_paiement';
  192. $total_tva='total_tva';
  193. $total_localtax1='total_localtax1';
  194. $total_localtax2='total_localtax2';
  195. $paymenttable='paiement';
  196. $paymentfacturetable='paiement_facture';
  197. }
  198. if ($direction == 'buy')
  199. {
  200. $invoicetable='facture_fourn';
  201. $invoicedettable='facture_fourn_det';
  202. $fk_facture='fk_facture_fourn';
  203. $fk_facture2='fk_facturefourn';
  204. $fk_payment='fk_paiementfourn';
  205. $total_tva='tva';
  206. $total_localtax1='total_localtax1';
  207. $total_localtax2='total_localtax2';
  208. $paymenttable='paiementfourn';
  209. $paymentfacturetable='paiementfourn_facturefourn';
  210. }
  211. // CAS DES BIENS
  212. // Define sql request
  213. $sql='';
  214. if ($modetax == 1) // Option vat on delivery for goods (payment) and debit invoice for services
  215. {
  216. if ($conf->global->MAIN_MODULE_ACCOUNTING)
  217. {
  218. // TODO a ce jour on se sait pas la compter car le montant tva d'un payment
  219. // n'est pas stocke dans la table des payments.
  220. // Seul le module compta expert peut resoudre ce probleme.
  221. // (Il faut quand un payment a lieu, stocker en plus du montant du paiement le
  222. // detail part tva et part ht).
  223. $sql='TODO';
  224. }
  225. if ($conf->global->MAIN_MODULE_COMPTABILITE)
  226. {
  227. // Count on delivery date (use invoice date as delivery is unknown)
  228. $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.tva_tx as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
  229. $sql .=" d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  230. $sql.= " d.date_start as date_start, d.date_end as date_end,";
  231. $sql.= " f.facnumber as facnum, f.type, f.total_ttc as ftotal_ttc,";
  232. $sql.= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
  233. $sql.= " 0 as payment_id, 0 as payment_amount";
  234. $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  235. $sql.= " ".MAIN_DB_PREFIX.$invoicedettable." as d" ;
  236. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  237. $sql.= " WHERE f.entity = " . $conf->entity;
  238. $sql.= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
  239. $sql.= " AND (f.type = 0"; // Standard
  240. $sql.= " OR f.type = 1"; // Replacement
  241. $sql.= " OR f.type = 2)"; // Credit note
  242. //$sql.= " OR f.type = 3"; // We do not include deposit
  243. $sql.= " AND f.rowid = d.".$fk_facture;
  244. if ($y && $m)
  245. {
  246. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  247. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  248. }
  249. else if ($y)
  250. {
  251. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  252. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  253. }
  254. if ($q) $sql.= " AND (date_format(f.datef,'%m') > ".(($q-1)*3)." AND date_format(f.datef,'%m') <= ".($q*3).")";
  255. if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  256. $sql.= " AND (d.product_type = 0"; // Limit to products
  257. $sql.= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of service
  258. $sql.= " ORDER BY d.rowid, d.".$fk_facture;
  259. }
  260. }
  261. else // Option vat on delivery for goods (payments) and payments for services
  262. {
  263. if ($conf->global->MAIN_MODULE_ACCOUNTING)
  264. {
  265. // TODO a ce jour on se sait pas la compter car le montant tva d'un payment
  266. // n'est pas stocke dans la table des payments.
  267. // Seul le module compta expert peut resoudre ce probleme.
  268. // (Il faut quand un payment a lieu, stocker en plus du montant du paiement le
  269. // detail part tva et part ht).
  270. $sql='TODO';
  271. }
  272. if ($conf->global->MAIN_MODULE_COMPTABILITE)
  273. {
  274. // Count on delivery date (use invoice date as delivery is unknown)
  275. $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.tva_tx as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
  276. $sql .=" d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  277. $sql.= " d.date_start as date_start, d.date_end as date_end,";
  278. $sql.= " f.facnumber as facnum, f.type, f.total_ttc as ftotal_ttc,";
  279. $sql.= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
  280. $sql.= " 0 as payment_id, 0 as payment_amount";
  281. // $sql.= " pf.".$fk_payment." as payment_id, pf.amount as payment_amount";
  282. $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  283. // $sql.= " ".MAIN_DB_PREFIX.$paymentfacturetable." as pf,";
  284. // $sql.= " ".MAIN_DB_PREFIX.$paymenttable." as pa,";
  285. $sql.= " ".MAIN_DB_PREFIX.$invoicedettable." as d" ;
  286. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  287. $sql.= " WHERE f.entity = " . $conf->entity;
  288. $sql.= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
  289. $sql.= " AND (f.type = 0"; // Standard
  290. $sql.= " OR f.type = 1"; // Replacement
  291. $sql.= " OR f.type = 2)"; // Credit note
  292. //$sql.= " OR f.type = 3"; // We do not include deposit
  293. $sql.= " AND f.rowid = d.".$fk_facture;
  294. // $sql.= " AND pf.".$fk_facture2." = f.rowid";
  295. // $sql.= " AND pa.rowid = pf.".$fk_payment;
  296. // $sql.= " AND pa.datep >= '".$y."0101000000' AND pa.datep <= '".$y."1231235959'";
  297. // $sql.= " AND (date_format(pa.datep,'%m') > ".(($q-1)*3)." AND date_format(pa.datep,'%m') <= ".($q*3).")";
  298. if ($y && $m)
  299. {
  300. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  301. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  302. }
  303. else if ($y)
  304. {
  305. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  306. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  307. }
  308. if ($q) $sql.= " AND (date_format(f.datef,'%m') > ".(($q-1)*3)." AND date_format(f.datef,'%m') <= ".($q*3).")";
  309. if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  310. $sql.= " AND (d.product_type = 0"; // Limit to products
  311. $sql.= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of service
  312. $sql.= " ORDER BY d.rowid, d.".$fk_facture;
  313. //print $sql;
  314. }
  315. }
  316. //print $sql.'<br>';
  317. if (! $sql) return -1;
  318. if ($sql == 'TODO') return -2;
  319. if ($sql != 'TODO')
  320. {
  321. dol_syslog("Tax.lib.php::vat_by_date sql=".$sql);
  322. $resql = $db->query($sql);
  323. if ($resql)
  324. {
  325. $rate = -1;
  326. $oldrowid='';
  327. while($assoc = $db->fetch_array($resql))
  328. {
  329. if (! isset($list[$assoc['rate']]['totalht'])) $list[$assoc['rate']]['totalht']=0;
  330. if (! isset($list[$assoc['rate']]['vat'])) $list[$assoc['rate']]['vat']=0;
  331. if (! isset($list[$assoc['rate']]['locatax1'])) $list[$assoc['rate']]['localtax1']=0;
  332. if (! isset($list[$assoc['rate']]['locatax2'])) $list[$assoc['rate']]['localtax2']=0;
  333. if ($assoc['rowid'] != $oldrowid) // Si rupture sur d.rowid
  334. {
  335. $oldrowid=$assoc['rowid'];
  336. $list[$assoc['rate']]['totalht'] += $assoc['total_ht'];
  337. $list[$assoc['rate']]['vat'] += $assoc['total_vat'];
  338. $list[$assoc['rate']]['localtax1'] += $assoc['total_localtax1'];
  339. $list[$assoc['rate']]['localtax2'] += $assoc['total_localtax2'];
  340. }
  341. $list[$assoc['rate']]['dtotal_ttc'][] = $assoc['total_ttc'];
  342. $list[$assoc['rate']]['dtype'][] = $assoc['dtype'];
  343. $list[$assoc['rate']]['ddate_start'][] = $db->jdate($assoc['date_start']);
  344. $list[$assoc['rate']]['ddate_end'][] = $db->jdate($assoc['date_end']);
  345. $list[$assoc['rate']]['facid'][] = $assoc['facid'];
  346. $list[$assoc['rate']]['facnum'][] = $assoc['facnum'];
  347. $list[$assoc['rate']]['type'][] = $assoc['type'];
  348. $list[$assoc['rate']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
  349. $list[$assoc['rate']]['descr'][] = $assoc['descr'];
  350. $list[$assoc['rate']]['totalht_list'][] = $assoc['total_ht'];
  351. $list[$assoc['rate']]['vat_list'][] = $assoc['total_vat'];
  352. $list[$assoc['rate']]['localtax1_list'][] = $assoc['total_localtax1'];
  353. $list[$assoc['rate']]['localtax2_list'][] = $assoc['total_localtax2'];
  354. $list[$assoc['rate']]['pid'][] = $assoc['pid'];
  355. $list[$assoc['rate']]['pref'][] = $assoc['pref'];
  356. $list[$assoc['rate']]['ptype'][] = $assoc['ptype'];
  357. $list[$assoc['rate']]['payment_id'][] = $assoc['payment_id'];
  358. $list[$assoc['rate']]['payment_amount'][] = $assoc['payment_amount'];
  359. $rate = $assoc['rate'];
  360. }
  361. }
  362. else
  363. {
  364. dol_print_error($db);
  365. return -3;
  366. }
  367. }
  368. // CAS DES SERVICES
  369. // Define sql request
  370. $sql='';
  371. if ($modetax == 1) // Option vat on delivery for goods (payment) and debit invoice for services
  372. {
  373. if ($conf->global->MAIN_MODULE_ACCOUNTING)
  374. {
  375. // Count on invoice date
  376. // TODO a ce jour on se sait pas la compter car le montant tva d'un payment
  377. // n'est pas stocke dans la table des payments.
  378. // Seul le module compta expert peut resoudre ce probleme.
  379. // (Il faut quand un payment a lieu, stocker en plus du montant du paiement le
  380. // detail part tva et part ht).
  381. $sql='TODO';
  382. }
  383. if ($conf->global->MAIN_MODULE_COMPTABILITE)
  384. {
  385. // Count on invoice date
  386. $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.tva_tx as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
  387. $sql .=" d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  388. $sql.= " d.date_start as date_start, d.date_end as date_end,";
  389. $sql.= " f.facnumber as facnum, f.type, f.total_ttc as ftotal_ttc,";
  390. $sql.= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
  391. $sql.= " 0 as payment_id, 0 as payment_amount";
  392. $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  393. $sql.= " ".MAIN_DB_PREFIX.$invoicedettable." as d" ;
  394. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  395. $sql.= " WHERE f.entity = " . $conf->entity;
  396. $sql.= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
  397. $sql.= " AND (f.type = 0"; // Standard
  398. $sql.= " OR f.type = 1"; // Replacement
  399. $sql.= " OR f.type = 2)"; // Credit note
  400. //$sql.= " OR f.type = 3"; // We do not include deposit
  401. $sql.= " AND f.rowid = d.".$fk_facture;
  402. if ($y && $m)
  403. {
  404. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  405. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  406. }
  407. else if ($y)
  408. {
  409. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  410. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  411. }
  412. if ($q) $sql.= " AND (date_format(f.datef,'%m') > ".(($q-1)*3)." AND date_format(f.datef,'%m') <= ".($q*3).")";
  413. if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  414. $sql.= " AND (d.product_type = 1"; // Limit to services
  415. $sql.= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
  416. $sql.= " ORDER BY d.rowid, d.".$fk_facture;
  417. }
  418. }
  419. else // Option vat on delivery for goods (payments) and payments for services
  420. {
  421. if ($conf->global->MAIN_MODULE_ACCOUNTING)
  422. {
  423. // Count on payments date
  424. // TODO a ce jour on se sait pas la compter car le montant tva d'un payment
  425. // n'est pas stocke dans la table des payments.
  426. // Seul le module compta expert peut resoudre ce probleme.
  427. // (Il faut quand un paiement a lieu, stocker en plus du montant du paiement le
  428. // detail part tva et part ht).
  429. $sql='TODO';
  430. }
  431. if ($conf->global->MAIN_MODULE_COMPTABILITE)
  432. {
  433. // Count on payments date
  434. $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.tva_tx as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
  435. $sql .=" d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  436. $sql.= " d.date_start as date_start, d.date_end as date_end,";
  437. $sql.= " f.facnumber as facnum, f.type, f.total_ttc as ftotal_ttc,";
  438. $sql.= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
  439. $sql.= " pf.".$fk_payment." as payment_id, pf.amount as payment_amount";
  440. $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  441. $sql.= " ".MAIN_DB_PREFIX.$paymentfacturetable." as pf,";
  442. $sql.= " ".MAIN_DB_PREFIX.$paymenttable." as pa,";
  443. $sql.= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
  444. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  445. $sql.= " WHERE f.entity = " . $conf->entity;
  446. $sql.= " AND f.fk_statut in (1,2)"; // Paid (partially or completely)
  447. $sql.= " AND (f.type = 0"; // Standard
  448. $sql.= " OR f.type = 1"; // Replacement
  449. $sql.= " OR f.type = 2)"; // Credit note
  450. //$sql.= " OR f.type = 3"; // We do not include deposit
  451. $sql.= " AND f.rowid = d.".$fk_facture;;
  452. $sql.= " AND pf.".$fk_facture2." = f.rowid";
  453. $sql.= " AND pa.rowid = pf.".$fk_payment;
  454. if ($y && $m)
  455. {
  456. $sql.= " AND pa.datep >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  457. $sql.= " AND pa.datep <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  458. }
  459. else if ($y)
  460. {
  461. $sql.= " AND pa.datep >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  462. $sql.= " AND pa.datep <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  463. }
  464. if ($q) $sql.= " AND (date_format(pa.datep,'%m') > ".(($q-1)*3)." AND date_format(pa.datep,'%m') <= ".($q*3).")";
  465. if ($date_start && $date_end) $sql.= " AND pa.datep >= ".$db->idate($date_start)." AND pa.datep <= ".$db->idate($date_end);
  466. $sql.= " AND (d.product_type = 1"; // Limit to services
  467. $sql.= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
  468. $sql.= " ORDER BY d.rowid, d.".$fk_facture.", pf.rowid";
  469. }
  470. }
  471. if (! $sql)
  472. {
  473. dol_syslog("Tax.lib.php::vat_by_date no accountancy module enabled".$sql,LOG_ERR);
  474. return -1; // -1 = Not accountancy module enabled
  475. }
  476. if ($sql == 'TODO') return -2; // -2 = Feature not yet available
  477. if ($sql != 'TODO')
  478. {
  479. dol_syslog("Tax.lib.php::vat_by_date sql=".$sql);
  480. $resql = $db->query($sql);
  481. if ($resql)
  482. {
  483. $rate = -1;
  484. $oldrowid='';
  485. while($assoc = $db->fetch_array($resql))
  486. {
  487. if (! isset($list[$assoc['rate']]['totalht'])) $list[$assoc['rate']]['totalht']=0;
  488. if (! isset($list[$assoc['rate']]['vat'])) $list[$assoc['rate']]['vat']=0;
  489. if (! isset($list[$assoc['rate']]['locatax1'])) $list[$assoc['rate']]['localtax1']=0;
  490. if (! isset($list[$assoc['rate']]['locatax2'])) $list[$assoc['rate']]['localtax2']=0;
  491. if ($assoc['rowid'] != $oldrowid) // Si rupture sur d.rowid
  492. {
  493. $oldrowid=$assoc['rowid'];
  494. $list[$assoc['rate']]['totalht'] += $assoc['total_ht'];
  495. $list[$assoc['rate']]['vat'] += $assoc['total_vat'];
  496. $list[$assoc['rate']]['localtax1'] += $assoc['total_localtax1'];
  497. $list[$assoc['rate']]['localtax2'] += $assoc['total_localtax2'];
  498. }
  499. $list[$assoc['rate']]['dtotal_ttc'][] = $assoc['total_ttc'];
  500. $list[$assoc['rate']]['dtype'][] = $assoc['dtype'];
  501. $list[$assoc['rate']]['ddate_start'][] = $db->jdate($assoc['date_start']);
  502. $list[$assoc['rate']]['ddate_end'][] = $db->jdate($assoc['date_end']);
  503. $list[$assoc['rate']]['facid'][] = $assoc['facid'];
  504. $list[$assoc['rate']]['facnum'][] = $assoc['facnum'];
  505. $list[$assoc['rate']]['type'][] = $assoc['type'];
  506. $list[$assoc['rate']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
  507. $list[$assoc['rate']]['descr'][] = $assoc['descr'];
  508. $list[$assoc['rate']]['totalht_list'][] = $assoc['total_ht'];
  509. $list[$assoc['rate']]['vat_list'][] = $assoc['total_vat'];
  510. $list[$assoc['rate']]['localtax1_list'][] = $assoc['total_localtax1'];
  511. $list[$assoc['rate']]['localtax2_list'][] = $assoc['total_localtax2'];
  512. $list[$assoc['rate']]['pid'][] = $assoc['pid'];
  513. $list[$assoc['rate']]['pref'][] = $assoc['pref'];
  514. $list[$assoc['rate']]['ptype'][] = $assoc['ptype'];
  515. $list[$assoc['rate']]['payment_id'][] = $assoc['payment_id'];
  516. $list[$assoc['rate']]['payment_amount'][] = $assoc['payment_amount'];
  517. $rate = $assoc['rate'];
  518. }
  519. }
  520. else
  521. {
  522. dol_print_error($db);
  523. return -3;
  524. }
  525. }
  526. return $list;
  527. }
  528. ?>