PageRenderTime 56ms CodeModel.GetById 23ms RepoModel.GetById 1ms app.codeStats 0ms

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

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