tax.lib.php 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624
  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. * Copyright (C) 2012-2016 Juanjo Menent <jmenent@2byte.es>
  6. * Copyright (C) 2012 Cédric Salvador <csalvador@gpcsolutions.fr>
  7. * Copyright (C) 2012-2014 Raphaël Doursenaud <rdoursenaud@gpcsolutions.fr>
  8. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 3 of the License, or
  13. * (at your option) any later version.
  14. *
  15. * This program is distributed in the hope that it will be useful,
  16. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  17. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  18. * GNU General Public License for more details.
  19. *
  20. * You should have received a copy of the GNU General Public License
  21. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  22. */
  23. /**
  24. * \file htdocs/core/lib/tax.lib.php
  25. * \ingroup tax
  26. * \brief Library for tax module
  27. */
  28. /**
  29. * Prepare array with list of tabs
  30. *
  31. * @param ChargeSociales $object Object related to tabs
  32. * @return array Array of tabs to show
  33. */
  34. function tax_prepare_head(ChargeSociales $object)
  35. {
  36. global $db, $langs, $conf, $user;
  37. $h = 0;
  38. $head = array();
  39. $head[$h][0] = DOL_URL_ROOT.'/compta/sociales/card.php?id='.$object->id;
  40. $head[$h][1] = $langs->trans('Card');
  41. $head[$h][2] = 'card';
  42. $h++;
  43. // Show more tabs from modules
  44. // Entries must be declared in modules descriptor with line
  45. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  46. // $this->tabs = array('entity:-tabname); to remove a tab
  47. complete_head_from_modules($conf,$langs,$object,$head,$h,'tax');
  48. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  49. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  50. $upload_dir = $conf->tax->dir_output . "/" . dol_sanitizeFileName($object->ref);
  51. $nbFiles = count(dol_dir_list($upload_dir,'files',0,'','(\.meta|_preview.*\.png)$'));
  52. $nbLinks=Link::count($db, $object->element, $object->id);
  53. $head[$h][0] = DOL_URL_ROOT.'/compta/sociales/document.php?id='.$object->id;
  54. $head[$h][1] = $langs->trans("Documents");
  55. if (($nbFiles+$nbLinks) > 0) $head[$h][1].= ' <span class="badge">'.($nbFiles+$nbLinks).'</span>';
  56. $head[$h][2] = 'documents';
  57. $h++;
  58. $head[$h][0] = DOL_URL_ROOT.'/compta/sociales/info.php?id='.$object->id;
  59. $head[$h][1] = $langs->trans("Info");
  60. $head[$h][2] = 'info';
  61. $h++;
  62. complete_head_from_modules($conf,$langs,$object,$head,$h,'tax','remove');
  63. return $head;
  64. }
  65. /**
  66. * Look for collectable VAT clients in the chosen year (and month)
  67. *
  68. * @param DoliDB $db Database handle
  69. * @param int $y Year
  70. * @param string $date_start Start date
  71. * @param string $date_end End date
  72. * @param int $modetax 0 or 1 (option vat on debit, 1 => $modecompta = 'CREANCES-DETTES')
  73. * @param string $direction 'sell' or 'buy'
  74. * @param int $m Month
  75. * @return array List of customers third parties with vat, -1 if no accountancy module, -2 if not yet developped, -3 if error
  76. */
  77. function vat_by_thirdparty($db, $y, $date_start, $date_end, $modetax, $direction, $m=0)
  78. {
  79. global $conf;
  80. $list=array();
  81. if ($direction == 'sell')
  82. {
  83. $invoicetable='facture';
  84. $total_ht='total';
  85. $total_tva='tva';
  86. }
  87. if ($direction == 'buy')
  88. {
  89. $invoicetable='facture_fourn';
  90. $total_ht='total_ht';
  91. $total_tva='total_tva';
  92. }
  93. // Define sql request
  94. $sql='';
  95. if ($modetax == 1)
  96. {
  97. // If vat paid on due invoices (non draft)
  98. $sql = "SELECT s.rowid as socid, s.nom as name, s.tva_intra as tva_intra, s.tva_assuj as assuj,";
  99. $sql.= " sum(f.$total_ht) as amount, sum(f.".$total_tva.") as tva,";
  100. $sql.= " sum(f.localtax1) as localtax1,";
  101. $sql.= " sum(f.localtax2) as localtax2";
  102. $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  103. $sql.= " ".MAIN_DB_PREFIX."societe as s";
  104. $sql.= " WHERE f.entity = " . $conf->entity;
  105. $sql.= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
  106. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql.= " AND f.type IN (0,1,2,5)";
  107. else $sql.= " AND f.type IN (0,1,2,3,5)";
  108. if ($y && $m)
  109. {
  110. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  111. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  112. }
  113. else if ($y)
  114. {
  115. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  116. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  117. }
  118. if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  119. $sql.= " AND s.rowid = f.fk_soc";
  120. $sql.= " GROUP BY s.rowid, s.nom, s.tva_intra, s.tva_assuj";
  121. }
  122. else
  123. {
  124. // Tva sur factures payes (should be on payment)
  125. /* $sql = "SELECT s.rowid as socid, s.nom as nom, s.tva_intra as tva_intra, s.tva_assuj as assuj,";
  126. $sql.= " sum(fd.total_ht) as amount, sum(".$total_tva.") as tva";
  127. $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f, ".MAIN_DB_PREFIX.$invoicetable." as fd, ".MAIN_DB_PREFIX."societe as s";
  128. $sql.= " WHERE ";
  129. $sql.= " f.fk_statut in (2)"; // Paid (partially or completely)
  130. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql.= " AND f.type IN (0,1,2,5)";
  131. else $sql.= " AND f.type IN (0,1,2,3,5)";
  132. if ($y && $m)
  133. {
  134. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  135. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  136. }
  137. else if ($y)
  138. {
  139. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  140. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  141. }
  142. if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  143. $sql.= " AND s.rowid = f.fk_soc AND f.rowid = fd.".$fk_facture;
  144. $sql.= " GROUP BY s.rowid as socid, s.nom as nom, s.tva_intra as tva_intra, s.tva_assuj as assuj";
  145. */
  146. }
  147. if (! $sql) return -1;
  148. dol_syslog("Tax.lib:thirdparty", LOG_DEBUG);
  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. * Gets VAT to collect for the given year (and given quarter or month)
  167. * The function gets the VAT in split results, as the VAT declaration asks
  168. * to report the amounts for different VAT rates as different lines.
  169. * This function also accounts recurrent invoices.
  170. *
  171. * @param DoliDB $db Database handler object
  172. * @param int $y Year
  173. * @param int $q Quarter
  174. * @param string $date_start Start date
  175. * @param string $date_end End date
  176. * @param int $modetax 0 or 1 (option vat on debit)
  177. * @param int $direction 'sell' (customer invoice) or 'buy' (supplier invoices)
  178. * @param int $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. $invoicefieldref='facnumber';
  198. }
  199. if ($direction == 'buy')
  200. {
  201. $invoicetable='facture_fourn';
  202. $invoicedettable='facture_fourn_det';
  203. $fk_facture='fk_facture_fourn';
  204. $fk_facture2='fk_facturefourn';
  205. $fk_payment='fk_paiementfourn';
  206. $total_tva='tva';
  207. $total_localtax1='total_localtax1';
  208. $total_localtax2='total_localtax2';
  209. $paymenttable='paiementfourn';
  210. $paymentfacturetable='paiementfourn_facturefourn';
  211. $invoicefieldref='ref';
  212. }
  213. // CAS DES BIENS
  214. // Define sql request
  215. $sql='';
  216. if ($modetax == 1) // Option vat on delivery for goods (payment) and debit invoice for services
  217. {
  218. // Count on delivery date (use invoice date as delivery is unknown)
  219. $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,";
  220. $sql .=" d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  221. $sql.= " d.date_start as date_start, d.date_end as date_end,";
  222. $sql.= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef, s.nom as company_name, s.rowid as company_id,";
  223. $sql.= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
  224. $sql.= " 0 as payment_id, 0 as payment_amount";
  225. $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  226. $sql.= " ".MAIN_DB_PREFIX."societe as s,";
  227. $sql.= " ".MAIN_DB_PREFIX.$invoicedettable." as d" ;
  228. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  229. $sql.= " WHERE f.entity = " . $conf->entity;
  230. $sql.= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
  231. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql.= " AND f.type IN (0,1,2,5)";
  232. else $sql.= " AND f.type IN (0,1,2,3,5)";
  233. $sql.= " AND f.rowid = d.".$fk_facture;
  234. $sql.= " AND s.rowid = f.fk_soc";
  235. if ($y && $m)
  236. {
  237. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  238. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  239. }
  240. else if ($y)
  241. {
  242. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  243. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  244. }
  245. if ($q) $sql.= " AND (date_format(f.datef,'%m') > ".(($q-1)*3)." AND date_format(f.datef,'%m') <= ".($q*3).")";
  246. if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  247. $sql.= " AND (d.product_type = 0"; // Limit to products
  248. $sql.= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of service
  249. $sql.= " ORDER BY d.rowid, d.".$fk_facture;
  250. }
  251. else // Option vat on delivery for goods (payments) and payments for services
  252. {
  253. // Count on delivery date (use invoice date as delivery is unknown)
  254. $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,";
  255. $sql .=" d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  256. $sql.= " d.date_start as date_start, d.date_end as date_end,";
  257. $sql.= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef as date_f, s.nom as company_name, s.rowid as company_id,";
  258. $sql.= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
  259. $sql.= " 0 as payment_id, 0 as payment_amount";
  260. $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  261. $sql.= " ".MAIN_DB_PREFIX."societe as s,";
  262. $sql.= " ".MAIN_DB_PREFIX.$invoicedettable." as d" ;
  263. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  264. $sql.= " WHERE f.entity = " . $conf->entity;
  265. $sql.= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
  266. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql.= " AND f.type IN (0,1,2,5)";
  267. else $sql.= " AND f.type IN (0,1,2,3,5)";
  268. $sql.= " AND f.rowid = d.".$fk_facture;
  269. $sql.= " AND s.rowid = f.fk_soc";
  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. //print $sql;
  286. }
  287. //print $sql.'<br>';
  288. if (! $sql) return -1;
  289. if ($sql == 'TODO') return -2;
  290. if ($sql != 'TODO')
  291. {
  292. dol_syslog("Tax.lib.php::vat_by_date", LOG_DEBUG);
  293. $resql = $db->query($sql);
  294. if ($resql)
  295. {
  296. $rate = -1;
  297. $oldrowid='';
  298. while($assoc = $db->fetch_array($resql))
  299. {
  300. if (! isset($list[$assoc['rate']]['totalht'])) $list[$assoc['rate']]['totalht']=0;
  301. if (! isset($list[$assoc['rate']]['vat'])) $list[$assoc['rate']]['vat']=0;
  302. if (! isset($list[$assoc['rate']]['localtax1'])) $list[$assoc['rate']]['localtax1']=0;
  303. if (! isset($list[$assoc['rate']]['localtax2'])) $list[$assoc['rate']]['localtax2']=0;
  304. if ($assoc['rowid'] != $oldrowid) // Si rupture sur d.rowid
  305. {
  306. $oldrowid=$assoc['rowid'];
  307. $list[$assoc['rate']]['totalht'] += $assoc['total_ht'];
  308. $list[$assoc['rate']]['vat'] += $assoc['total_vat'];
  309. $list[$assoc['rate']]['localtax1'] += $assoc['total_localtax1'];
  310. $list[$assoc['rate']]['localtax2'] += $assoc['total_localtax2'];
  311. }
  312. $list[$assoc['rate']]['dtotal_ttc'][] = $assoc['total_ttc'];
  313. $list[$assoc['rate']]['dtype'][] = $assoc['dtype'];
  314. $list[$assoc['rate']]['datef'][] = $assoc['datef'];
  315. $list[$assoc['rate']]['company_name'][] = $assoc['company_name'];
  316. $list[$assoc['rate']]['company_id'][] = $assoc['company_id'];
  317. $list[$assoc['rate']]['ddate_start'][] = $db->jdate($assoc['date_start']);
  318. $list[$assoc['rate']]['ddate_end'][] = $db->jdate($assoc['date_end']);
  319. $list[$assoc['rate']]['facid'][] = $assoc['facid'];
  320. $list[$assoc['rate']]['facnum'][] = $assoc['facnum'];
  321. $list[$assoc['rate']]['type'][] = $assoc['type'];
  322. $list[$assoc['rate']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
  323. $list[$assoc['rate']]['descr'][] = $assoc['descr'];
  324. $list[$assoc['rate']]['totalht_list'][] = $assoc['total_ht'];
  325. $list[$assoc['rate']]['vat_list'][] = $assoc['total_vat'];
  326. $list[$assoc['rate']]['localtax1_list'][] = $assoc['total_localtax1'];
  327. $list[$assoc['rate']]['localtax2_list'][] = $assoc['total_localtax2'];
  328. $list[$assoc['rate']]['pid'][] = $assoc['pid'];
  329. $list[$assoc['rate']]['pref'][] = $assoc['pref'];
  330. $list[$assoc['rate']]['ptype'][] = $assoc['ptype'];
  331. $list[$assoc['rate']]['payment_id'][] = $assoc['payment_id'];
  332. $list[$assoc['rate']]['payment_amount'][] = $assoc['payment_amount'];
  333. $rate = $assoc['rate'];
  334. }
  335. }
  336. else
  337. {
  338. dol_print_error($db);
  339. return -3;
  340. }
  341. }
  342. // CAS DES SERVICES
  343. // Define sql request
  344. $sql='';
  345. if ($modetax == 1) // Option vat on delivery for goods (payment) and debit invoice for services
  346. {
  347. // Count on invoice date
  348. $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,";
  349. $sql .=" d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  350. $sql.= " d.date_start as date_start, d.date_end as date_end,";
  351. $sql.= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef, s.nom as company_name, s.rowid as company_id,";
  352. $sql.= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
  353. $sql.= " 0 as payment_id, 0 as payment_amount";
  354. $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  355. $sql.= " ".MAIN_DB_PREFIX."societe as s,";
  356. $sql.= " ".MAIN_DB_PREFIX.$invoicedettable." as d" ;
  357. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  358. $sql.= " WHERE f.entity = " . $conf->entity;
  359. $sql.= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
  360. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql.= " AND f.type IN (0,1,2,5)";
  361. else $sql.= " AND f.type IN (0,1,2,3,5)";
  362. $sql.= " AND f.rowid = d.".$fk_facture;
  363. $sql.= " AND s.rowid = f.fk_soc";
  364. if ($y && $m)
  365. {
  366. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  367. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  368. }
  369. else if ($y)
  370. {
  371. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  372. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  373. }
  374. if ($q) $sql.= " AND (date_format(f.datef,'%m') > ".(($q-1)*3)." AND date_format(f.datef,'%m') <= ".($q*3).")";
  375. if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  376. $sql.= " AND (d.product_type = 1"; // Limit to services
  377. $sql.= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
  378. $sql.= " ORDER BY d.rowid, d.".$fk_facture;
  379. }
  380. else // Option vat on delivery for goods (payments) and payments for services
  381. {
  382. // Count on payments date
  383. $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,";
  384. $sql .=" d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  385. $sql.= " d.date_start as date_start, d.date_end as date_end,";
  386. $sql.= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef, s.nom as company_name, s.rowid as company_id,";
  387. $sql.= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
  388. $sql.= " pf.".$fk_payment." as payment_id, pf.amount as payment_amount";
  389. $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  390. $sql.= " ".MAIN_DB_PREFIX.$paymentfacturetable." as pf,";
  391. $sql.= " ".MAIN_DB_PREFIX.$paymenttable." as pa,";
  392. $sql.= " ".MAIN_DB_PREFIX."societe as s,";
  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)"; // Paid (partially or completely)
  397. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql.= " AND f.type IN (0,1,2,5)";
  398. else $sql.= " AND f.type IN (0,1,2,3,5)";
  399. $sql.= " AND f.rowid = d.".$fk_facture;
  400. $sql.= " AND s.rowid = f.fk_soc";
  401. $sql.= " AND pf.".$fk_facture2." = f.rowid";
  402. $sql.= " AND pa.rowid = pf.".$fk_payment;
  403. if ($y && $m)
  404. {
  405. $sql.= " AND pa.datep >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  406. $sql.= " AND pa.datep <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  407. }
  408. else if ($y)
  409. {
  410. $sql.= " AND pa.datep >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  411. $sql.= " AND pa.datep <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  412. }
  413. if ($q) $sql.= " AND (date_format(pa.datep,'%m') > ".(($q-1)*3)." AND date_format(pa.datep,'%m') <= ".($q*3).")";
  414. if ($date_start && $date_end) $sql.= " AND pa.datep >= '".$db->idate($date_start)."' AND pa.datep <= '".$db->idate($date_end)."'";
  415. $sql.= " AND (d.product_type = 1"; // Limit to services
  416. $sql.= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
  417. $sql.= " ORDER BY d.rowid, d.".$fk_facture.", pf.rowid";
  418. }
  419. if (! $sql)
  420. {
  421. dol_syslog("Tax.lib.php::vat_by_date no accountancy module enabled".$sql,LOG_ERR);
  422. return -1; // -1 = Not accountancy module enabled
  423. }
  424. if ($sql == 'TODO') return -2; // -2 = Feature not yet available
  425. if ($sql != 'TODO')
  426. {
  427. dol_syslog("Tax.lib.php::vat_by_date", LOG_DEBUG);
  428. $resql = $db->query($sql);
  429. if ($resql)
  430. {
  431. $rate = -1;
  432. $oldrowid='';
  433. while($assoc = $db->fetch_array($resql))
  434. {
  435. if (! isset($list[$assoc['rate']]['totalht'])) $list[$assoc['rate']]['totalht']=0;
  436. if (! isset($list[$assoc['rate']]['vat'])) $list[$assoc['rate']]['vat']=0;
  437. if (! isset($list[$assoc['rate']]['localtax1'])) $list[$assoc['rate']]['localtax1']=0;
  438. if (! isset($list[$assoc['rate']]['localtax2'])) $list[$assoc['rate']]['localtax2']=0;
  439. if ($assoc['rowid'] != $oldrowid) // Si rupture sur d.rowid
  440. {
  441. $oldrowid=$assoc['rowid'];
  442. $list[$assoc['rate']]['totalht'] += $assoc['total_ht'];
  443. $list[$assoc['rate']]['vat'] += $assoc['total_vat'];
  444. $list[$assoc['rate']]['localtax1'] += $assoc['total_localtax1'];
  445. $list[$assoc['rate']]['localtax2'] += $assoc['total_localtax2'];
  446. }
  447. $list[$assoc['rate']]['dtotal_ttc'][] = $assoc['total_ttc'];
  448. $list[$assoc['rate']]['dtype'][] = $assoc['dtype'];
  449. $list[$assoc['rate']]['datef'][] = $assoc['datef'];
  450. $list[$assoc['rate']]['company_name'][] = $assoc['company_name'];
  451. $list[$assoc['rate']]['company_id'][] = $assoc['company_id'];
  452. $list[$assoc['rate']]['ddate_start'][] = $db->jdate($assoc['date_start']);
  453. $list[$assoc['rate']]['ddate_end'][] = $db->jdate($assoc['date_end']);
  454. $list[$assoc['rate']]['facid'][] = $assoc['facid'];
  455. $list[$assoc['rate']]['facnum'][] = $assoc['facnum'];
  456. $list[$assoc['rate']]['type'][] = $assoc['type'];
  457. $list[$assoc['rate']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
  458. $list[$assoc['rate']]['descr'][] = $assoc['descr'];
  459. $list[$assoc['rate']]['totalht_list'][] = $assoc['total_ht'];
  460. $list[$assoc['rate']]['vat_list'][] = $assoc['total_vat'];
  461. $list[$assoc['rate']]['localtax1_list'][] = $assoc['total_localtax1'];
  462. $list[$assoc['rate']]['localtax2_list'][] = $assoc['total_localtax2'];
  463. $list[$assoc['rate']]['pid'][] = $assoc['pid'];
  464. $list[$assoc['rate']]['pref'][] = $assoc['pref'];
  465. $list[$assoc['rate']]['ptype'][] = $assoc['ptype'];
  466. $list[$assoc['rate']]['payment_id'][] = $assoc['payment_id'];
  467. $list[$assoc['rate']]['payment_amount'][] = $assoc['payment_amount'];
  468. $rate = $assoc['rate'];
  469. }
  470. }
  471. else
  472. {
  473. dol_print_error($db);
  474. return -3;
  475. }
  476. }
  477. // Expense Report
  478. if ($direction == 'buy')
  479. {
  480. // Define sql request
  481. $sql='';
  482. // Count on payments date
  483. $sql = "SELECT e.rowid, d.product_type as dtype, e.rowid 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, e.note_private as descr,";
  484. $sql .=" d.total_localtax1 as total_localtax1, d.total_localtax2 as total_localtax2, ";
  485. $sql.= " e.date_debut as date_start, e.date_fin as date_end,";
  486. $sql.= " e.ref as facnum, e.total_ttc as ftotal_ttc, e.date_create, s.nom as company_name, s.rowid as company_id, d.fk_c_type_fees as type,";
  487. $sql.= " p.fk_bank as payment_id, p.amount as payment_amount, p.rowid as pid, e.ref as pref";
  488. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s,";
  489. $sql.= " ".MAIN_DB_PREFIX."expensereport_det as d,";
  490. $sql.= " ".MAIN_DB_PREFIX."expensereport as e,";
  491. $sql.= " ".MAIN_DB_PREFIX."payment_expensereport as p";
  492. $sql.= " WHERE e.entity = " . $conf->entity;
  493. $sql.= " AND e.fk_statut in (6)";
  494. $sql.= " AND e.rowid = p.fk_expensereport";
  495. $sql.= " AND s.rowid = e.entity";
  496. $sql.= " AND d.fk_expensereport = e.rowid";
  497. if ($y && $m)
  498. {
  499. $sql.= " AND p.datep >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  500. $sql.= " AND p.datep <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  501. }
  502. else if ($y)
  503. {
  504. $sql.= " AND p.datep >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  505. $sql.= " AND p.datep <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  506. }
  507. if ($q) $sql.= " AND (date_format(p.datep,'%m') > ".(($q-1)*3)." AND date_format(p.datep,'%m') <= ".($q*3).")";
  508. if ($date_start && $date_end) $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  509. $sql.= " AND (d.product_type = -1";
  510. $sql.= " OR e.date_debut is NOT null OR e.date_fin IS NOT NULL)"; // enhance detection of service
  511. $sql.= " ORDER BY e.rowid";
  512. if (! $sql)
  513. {
  514. dol_syslog("Tax.lib.php::vat_by_date no accountancy module enabled".$sql,LOG_ERR);
  515. return -1; // -1 = Not accountancy module enabled
  516. }
  517. if ($sql == 'TODO') return -2; // -2 = Feature not yet available
  518. if ($sql != 'TODO')
  519. {
  520. dol_syslog("Tax.lib.php::vat_by_date", LOG_DEBUG);
  521. $resql = $db->query($sql);
  522. if ($resql)
  523. {
  524. $rate = -1;
  525. $oldrowid='';
  526. while($assoc = $db->fetch_array($resql))
  527. {
  528. if (! isset($list[$assoc['rate']]['totalht'])) $list[$assoc['rate']]['totalht']=0;
  529. if (! isset($list[$assoc['rate']]['vat'])) $list[$assoc['rate']]['vat']=0;
  530. if (! isset($list[$assoc['rate']]['localtax1'])) $list[$assoc['rate']]['localtax1']=0;
  531. if (! isset($list[$assoc['rate']]['localtax2'])) $list[$assoc['rate']]['localtax2']=0;
  532. if ($assoc['rowid'] != $oldrowid) // Si rupture sur d.rowid
  533. {
  534. $oldrowid=$assoc['rowid'];
  535. $list[$assoc['rate']]['totalht'] += $assoc['total_ht'];
  536. $list[$assoc['rate']]['localtax1'] += $assoc['total_localtax1'];
  537. $list[$assoc['rate']]['localtax2'] += $assoc['total_localtax2'];
  538. }
  539. $list[$assoc['rate']]['vat'] = $assoc['total_vat'];
  540. $list[$assoc['rate']]['dtotal_ttc'][] = $assoc['total_ttc'];
  541. $list[$assoc['rate']]['dtype'][] = 'ExpenseReportPayment';
  542. $list[$assoc['rate']]['datef'][] = $assoc['datef'];
  543. $list[$assoc['rate']]['company_name'][] = $assoc['company_name'];
  544. $list[$assoc['rate']]['company_id'][] = $assoc['company_id'];
  545. $list[$assoc['rate']]['ddate_start'][] = $db->jdate($assoc['date_start']);
  546. $list[$assoc['rate']]['ddate_end'][] = $db->jdate($assoc['date_end']);
  547. $list[$assoc['rate']]['facid'][] = $assoc['facid'];
  548. $list[$assoc['rate']]['facnum'][] = $assoc['facnum'];
  549. $list[$assoc['rate']]['type'][] = $assoc['type'];
  550. $list[$assoc['rate']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
  551. $list[$assoc['rate']]['descr'][] = $assoc['descr'];
  552. $list[$assoc['rate']]['totalht_list'][] = $assoc['total_ht'];
  553. $list[$assoc['rate']]['vat_list'][] = $assoc['total_vat'];
  554. $list[$assoc['rate']]['localtax1_list'][] = $assoc['total_localtax1'];
  555. $list[$assoc['rate']]['localtax2_list'][] = $assoc['total_localtax2'];
  556. $list[$assoc['rate']]['pid'][] = $assoc['pid'];
  557. $list[$assoc['rate']]['pref'][] = $assoc['pref'];
  558. $list[$assoc['rate']]['ptype'][] = 'ExpenseReportPayment';
  559. $list[$assoc['rate']]['payment_id'][] = $assoc['payment_id'];
  560. $list[$assoc['rate']]['payment_amount'][] = $assoc['payment_amount'];
  561. $rate = $assoc['rate'];
  562. }
  563. }
  564. else
  565. {
  566. dol_print_error($db);
  567. return -3;
  568. }
  569. }
  570. }
  571. return $list;
  572. }