tax.lib.php 50 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011
  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@inodbox.com>
  5. * Copyright (C) 2012-2017 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 string $type Tax type, either 'vat', 'localtax1' or 'localtax2'
  69. * @param DoliDB $db Database handle
  70. * @param int $y Year
  71. * @param string $date_start Start date
  72. * @param string $date_end End date
  73. * @param int $modetax Not used
  74. * @param string $direction 'sell' or 'buy'
  75. * @param int $m Month
  76. * @param int $q Quarter
  77. * @return array Array with details of VATs (per third parties), -1 if no accountancy module, -2 if not yet developped, -3 if error
  78. */
  79. function tax_by_thirdparty($type, $db, $y, $date_start, $date_end, $modetax, $direction, $m=0, $q=0)
  80. {
  81. global $conf;
  82. // If we use date_start and date_end, we must not use $y, $m, $q
  83. if (($date_start || $date_end) && (! empty($y) || ! empty($m) || ! empty($q)))
  84. {
  85. dol_print_error('', 'Bad value of input parameter for tax_by_rate');
  86. }
  87. $list=array();
  88. if ($direction == 'sell')
  89. {
  90. $invoicetable='facture';
  91. $invoicedettable='facturedet';
  92. $fk_facture='fk_facture';
  93. $fk_facture2='fk_facture';
  94. $fk_payment='fk_paiement';
  95. $total_tva='total_tva';
  96. $paymenttable='paiement';
  97. $paymentfacturetable='paiement_facture';
  98. $invoicefieldref='facnumber';
  99. }
  100. if ($direction == 'buy')
  101. {
  102. $invoicetable='facture_fourn';
  103. $invoicedettable='facture_fourn_det';
  104. $fk_facture='fk_facture_fourn';
  105. $fk_facture2='fk_facturefourn';
  106. $fk_payment='fk_paiementfourn';
  107. $total_tva='tva';
  108. $paymenttable='paiementfourn';
  109. $paymentfacturetable='paiementfourn_facturefourn';
  110. $invoicefieldref='ref';
  111. }
  112. if ( strpos( $type, 'localtax' ) === 0 ) {
  113. $f_rate = $type . '_tx';
  114. } else {
  115. $f_rate = 'tva_tx';
  116. }
  117. $total_localtax1='total_localtax1';
  118. $total_localtax2='total_localtax2';
  119. // CAS DES BIENS/PRODUITS
  120. // Define sql request
  121. $sql='';
  122. if (($direction == 'sell' && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
  123. || ($direction == 'buy' && $conf->global->TAX_MODE_BUY_PRODUCT == 'invoice'))
  124. {
  125. // Count on delivery date (use invoice date as delivery is unknown)
  126. $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
  127. $sql .=" d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  128. $sql.= " d.date_start as date_start, d.date_end as date_end,";
  129. $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,";
  130. $sql.= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
  131. $sql.= " 0 as payment_id, 0 as payment_amount";
  132. $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  133. $sql.= " ".MAIN_DB_PREFIX."societe as s,";
  134. $sql.= " ".MAIN_DB_PREFIX.$invoicedettable." as d" ;
  135. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  136. $sql.= " WHERE f.entity = " . $conf->entity;
  137. $sql.= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
  138. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql.= " AND f.type IN (0,1,2,5)";
  139. else $sql.= " AND f.type IN (0,1,2,3,5)";
  140. $sql.= " AND f.rowid = d.".$fk_facture;
  141. $sql.= " AND s.rowid = f.fk_soc";
  142. if ($y && $m)
  143. {
  144. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  145. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  146. }
  147. else if ($y)
  148. {
  149. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  150. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  151. }
  152. if ($q) $sql.= " AND (date_format(f.datef,'%m') > ".(($q-1)*3)." AND date_format(f.datef,'%m') <= ".($q*3).")";
  153. if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  154. $sql.= " AND (d.product_type = 0"; // Limit to products
  155. $sql.= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of products
  156. if (empty($conf->global->MAIN_INCLUDE_ZERO_VAT_IN_REPORTS)) $sql.= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
  157. $sql.= " ORDER BY d.rowid, d.".$fk_facture;
  158. }
  159. else
  160. {
  161. // Count on payments date
  162. $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
  163. $sql .=" d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  164. $sql.= " d.date_start as date_start, d.date_end as date_end,";
  165. $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,";
  166. $sql.= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
  167. $sql.= " pf.".$fk_payment." as payment_id, pf.amount as payment_amount,";
  168. $sql.= " pa.datep as datep";
  169. $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  170. $sql.= " ".MAIN_DB_PREFIX.$paymentfacturetable." as pf,";
  171. $sql.= " ".MAIN_DB_PREFIX.$paymenttable." as pa,";
  172. $sql.= " ".MAIN_DB_PREFIX."societe as s,";
  173. $sql.= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
  174. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  175. $sql.= " WHERE f.entity = " . $conf->entity;
  176. $sql.= " AND f.fk_statut in (1,2)"; // Paid (partially or completely)
  177. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql.= " AND f.type IN (0,1,2,5)";
  178. else $sql.= " AND f.type IN (0,1,2,3,5)";
  179. $sql.= " AND f.rowid = d.".$fk_facture;
  180. $sql.= " AND s.rowid = f.fk_soc";
  181. $sql.= " AND pf.".$fk_facture2." = f.rowid";
  182. $sql.= " AND pa.rowid = pf.".$fk_payment;
  183. if ($y && $m)
  184. {
  185. $sql.= " AND pa.datep >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  186. $sql.= " AND pa.datep <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  187. }
  188. else if ($y)
  189. {
  190. $sql.= " AND pa.datep >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  191. $sql.= " AND pa.datep <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  192. }
  193. if ($q) $sql.= " AND (date_format(pa.datep,'%m') > ".(($q-1)*3)." AND date_format(pa.datep,'%m') <= ".($q*3).")";
  194. if ($date_start && $date_end) $sql.= " AND pa.datep >= '".$db->idate($date_start)."' AND pa.datep <= '".$db->idate($date_end)."'";
  195. $sql.= " AND (d.product_type = 0"; // Limit to products
  196. $sql.= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of products
  197. if (empty($conf->global->MAIN_INCLUDE_ZERO_VAT_IN_REPORTS)) $sql.= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
  198. $sql.= " ORDER BY d.rowid, d.".$fk_facture.", pf.rowid";
  199. }
  200. if (! $sql) return -1;
  201. if ($sql == 'TODO') return -2;
  202. if ($sql != 'TODO')
  203. {
  204. dol_syslog("Tax.lib.php::tax_by_thirdparty", LOG_DEBUG);
  205. $resql = $db->query($sql);
  206. if ($resql)
  207. {
  208. $company_id = -1;
  209. $oldrowid='';
  210. while($assoc = $db->fetch_array($resql))
  211. {
  212. if (! isset($list[$assoc['company_id']]['totalht'])) $list[$assoc['company_id']]['totalht']=0;
  213. if (! isset($list[$assoc['company_id']]['vat'])) $list[$assoc['company_id']]['vat']=0;
  214. if (! isset($list[$assoc['company_id']]['localtax1'])) $list[$assoc['company_id']]['localtax1']=0;
  215. if (! isset($list[$assoc['company_id']]['localtax2'])) $list[$assoc['company_id']]['localtax2']=0;
  216. if ($assoc['rowid'] != $oldrowid) // Si rupture sur d.rowid
  217. {
  218. $oldrowid=$assoc['rowid'];
  219. $list[$assoc['company_id']]['totalht'] += $assoc['total_ht'];
  220. $list[$assoc['company_id']]['vat'] += $assoc['total_vat'];
  221. $list[$assoc['company_id']]['localtax1'] += $assoc['total_localtax1'];
  222. $list[$assoc['company_id']]['localtax2'] += $assoc['total_localtax2'];
  223. }
  224. $list[$assoc['company_id']]['dtotal_ttc'][] = $assoc['total_ttc'];
  225. $list[$assoc['company_id']]['dtype'][] = $assoc['dtype'];
  226. $list[$assoc['company_id']]['datef'][] = $db->jdate($assoc['datef']);
  227. $list[$assoc['company_id']]['datep'][] = $db->jdate($assoc['datep']);
  228. $list[$assoc['company_id']]['company_name'][] = $assoc['company_name'];
  229. $list[$assoc['company_id']]['company_id'][] = $assoc['company_id'];
  230. $list[$assoc['company_id']]['drate'][] = $assoc['rate'];
  231. $list[$assoc['company_id']]['ddate_start'][] = $db->jdate($assoc['date_start']);
  232. $list[$assoc['company_id']]['ddate_end'][] = $db->jdate($assoc['date_end']);
  233. $list[$assoc['company_id']]['facid'][] = $assoc['facid'];
  234. $list[$assoc['company_id']]['facnum'][] = $assoc['facnum'];
  235. $list[$assoc['company_id']]['type'][] = $assoc['type'];
  236. $list[$assoc['company_id']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
  237. $list[$assoc['company_id']]['descr'][] = $assoc['descr'];
  238. $list[$assoc['company_id']]['totalht_list'][] = $assoc['total_ht'];
  239. $list[$assoc['company_id']]['vat_list'][] = $assoc['total_vat'];
  240. $list[$assoc['company_id']]['localtax1_list'][] = $assoc['total_localtax1'];
  241. $list[$assoc['company_id']]['localtax2_list'][] = $assoc['total_localtax2'];
  242. $list[$assoc['company_id']]['pid'][] = $assoc['pid'];
  243. $list[$assoc['company_id']]['pref'][] = $assoc['pref'];
  244. $list[$assoc['company_id']]['ptype'][] = $assoc['ptype'];
  245. $list[$assoc['company_id']]['payment_id'][] = $assoc['payment_id'];
  246. $list[$assoc['company_id']]['payment_amount'][] = $assoc['payment_amount'];
  247. $company_id = $assoc['company_id'];
  248. }
  249. }
  250. else
  251. {
  252. dol_print_error($db);
  253. return -3;
  254. }
  255. }
  256. // CAS DES SERVICES
  257. // Define sql request
  258. $sql='';
  259. if (($direction == 'sell' && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice')
  260. || ($direction == 'buy' && $conf->global->TAX_MODE_BUY_SERVICE == 'invoice'))
  261. {
  262. // Count on invoice date
  263. $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
  264. $sql .=" d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  265. $sql.= " d.date_start as date_start, d.date_end as date_end,";
  266. $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,";
  267. $sql.= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
  268. $sql.= " 0 as payment_id, 0 as payment_amount";
  269. $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  270. $sql.= " ".MAIN_DB_PREFIX."societe as s,";
  271. $sql.= " ".MAIN_DB_PREFIX.$invoicedettable." as d" ;
  272. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  273. $sql.= " WHERE f.entity = " . $conf->entity;
  274. $sql.= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
  275. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql.= " AND f.type IN (0,1,2,5)";
  276. else $sql.= " AND f.type IN (0,1,2,3,5)";
  277. $sql.= " AND f.rowid = d.".$fk_facture;
  278. $sql.= " AND s.rowid = f.fk_soc";
  279. if ($y && $m)
  280. {
  281. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  282. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  283. }
  284. else if ($y)
  285. {
  286. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  287. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  288. }
  289. if ($q) $sql.= " AND (date_format(f.datef,'%m') > ".(($q-1)*3)." AND date_format(f.datef,'%m') <= ".($q*3).")";
  290. if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  291. $sql.= " AND (d.product_type = 1"; // Limit to services
  292. $sql.= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
  293. if (empty($conf->global->MAIN_INCLUDE_ZERO_VAT_IN_REPORTS)) $sql.= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
  294. $sql.= " ORDER BY d.rowid, d.".$fk_facture;
  295. }
  296. else
  297. {
  298. // Count on payments date
  299. $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
  300. $sql .=" d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  301. $sql.= " d.date_start as date_start, d.date_end as date_end,";
  302. $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,";
  303. $sql.= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
  304. $sql.= " pf.".$fk_payment." as payment_id, pf.amount as payment_amount,";
  305. $sql.= " pa.datep as datep";
  306. $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  307. $sql.= " ".MAIN_DB_PREFIX.$paymentfacturetable." as pf,";
  308. $sql.= " ".MAIN_DB_PREFIX.$paymenttable." as pa,";
  309. $sql.= " ".MAIN_DB_PREFIX."societe as s,";
  310. $sql.= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
  311. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  312. $sql.= " WHERE f.entity = " . $conf->entity;
  313. $sql.= " AND f.fk_statut in (1,2)"; // Paid (partially or completely)
  314. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql.= " AND f.type IN (0,1,2,5)";
  315. else $sql.= " AND f.type IN (0,1,2,3,5)";
  316. $sql.= " AND f.rowid = d.".$fk_facture;
  317. $sql.= " AND s.rowid = f.fk_soc";
  318. $sql.= " AND pf.".$fk_facture2." = f.rowid";
  319. $sql.= " AND pa.rowid = pf.".$fk_payment;
  320. if ($y && $m)
  321. {
  322. $sql.= " AND pa.datep >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  323. $sql.= " AND pa.datep <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  324. }
  325. else if ($y)
  326. {
  327. $sql.= " AND pa.datep >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  328. $sql.= " AND pa.datep <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  329. }
  330. if ($q) $sql.= " AND (date_format(pa.datep,'%m') > ".(($q-1)*3)." AND date_format(pa.datep,'%m') <= ".($q*3).")";
  331. if ($date_start && $date_end) $sql.= " AND pa.datep >= '".$db->idate($date_start)."' AND pa.datep <= '".$db->idate($date_end)."'";
  332. $sql.= " AND (d.product_type = 1"; // Limit to services
  333. $sql.= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
  334. if (empty($conf->global->MAIN_INCLUDE_ZERO_VAT_IN_REPORTS)) $sql.= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
  335. $sql.= " ORDER BY d.rowid, d.".$fk_facture.", pf.rowid";
  336. }
  337. if (! $sql)
  338. {
  339. dol_syslog("Tax.lib.php::tax_by_rate no accountancy module enabled".$sql,LOG_ERR);
  340. return -1; // -1 = Not accountancy module enabled
  341. }
  342. if ($sql == 'TODO') return -2; // -2 = Feature not yet available
  343. if ($sql != 'TODO')
  344. {
  345. dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
  346. $resql = $db->query($sql);
  347. if ($resql)
  348. {
  349. $company_id = -1;
  350. $oldrowid='';
  351. while($assoc = $db->fetch_array($resql))
  352. {
  353. if (! isset($list[$assoc['company_id']]['totalht'])) $list[$assoc['company_id']]['totalht']=0;
  354. if (! isset($list[$assoc['company_id']]['vat'])) $list[$assoc['company_id']]['vat']=0;
  355. if (! isset($list[$assoc['company_id']]['localtax1'])) $list[$assoc['company_id']]['localtax1']=0;
  356. if (! isset($list[$assoc['company_id']]['localtax2'])) $list[$assoc['company_id']]['localtax2']=0;
  357. if ($assoc['rowid'] != $oldrowid) // Si rupture sur d.rowid
  358. {
  359. $oldrowid=$assoc['rowid'];
  360. $list[$assoc['company_id']]['totalht'] += $assoc['total_ht'];
  361. $list[$assoc['company_id']]['vat'] += $assoc['total_vat'];
  362. $list[$assoc['company_id']]['localtax1'] += $assoc['total_localtax1'];
  363. $list[$assoc['company_id']]['localtax2'] += $assoc['total_localtax2'];
  364. }
  365. $list[$assoc['company_id']]['dtotal_ttc'][] = $assoc['total_ttc'];
  366. $list[$assoc['company_id']]['dtype'][] = $assoc['dtype'];
  367. $list[$assoc['company_id']]['datef'][] = $db->jdate($assoc['datef']);
  368. $list[$assoc['company_id']]['datep'][] = $db->jdate($assoc['datep']);
  369. $list[$assoc['company_id']]['company_name'][] = $assoc['company_name'];
  370. $list[$assoc['company_id']]['company_id'][] = $assoc['company_id'];
  371. $list[$assoc['company_id']]['drate'][] = $assoc['rate'];
  372. $list[$assoc['company_id']]['ddate_start'][] = $db->jdate($assoc['date_start']);
  373. $list[$assoc['company_id']]['ddate_end'][] = $db->jdate($assoc['date_end']);
  374. $list[$assoc['company_id']]['facid'][] = $assoc['facid'];
  375. $list[$assoc['company_id']]['facnum'][] = $assoc['facnum'];
  376. $list[$assoc['company_id']]['type'][] = $assoc['type'];
  377. $list[$assoc['company_id']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
  378. $list[$assoc['company_id']]['descr'][] = $assoc['descr'];
  379. $list[$assoc['company_id']]['totalht_list'][] = $assoc['total_ht'];
  380. $list[$assoc['company_id']]['vat_list'][] = $assoc['total_vat'];
  381. $list[$assoc['company_id']]['localtax1_list'][] = $assoc['total_localtax1'];
  382. $list[$assoc['company_id']]['localtax2_list'][] = $assoc['total_localtax2'];
  383. $list[$assoc['company_id']]['pid'][] = $assoc['pid'];
  384. $list[$assoc['company_id']]['pref'][] = $assoc['pref'];
  385. $list[$assoc['company_id']]['ptype'][] = $assoc['ptype'];
  386. $list[$assoc['company_id']]['payment_id'][] = $assoc['payment_id'];
  387. $list[$assoc['company_id']]['payment_amount'][] = $assoc['payment_amount'];
  388. $company_id = $assoc['company_id'];
  389. }
  390. }
  391. else
  392. {
  393. dol_print_error($db);
  394. return -3;
  395. }
  396. }
  397. // CASE OF EXPENSE REPORT
  398. if ($direction == 'buy') // buy only for expense reports
  399. {
  400. // Define sql request
  401. $sql='';
  402. // Count on payments date
  403. $sql = "SELECT d.rowid, d.product_type as dtype, e.rowid as facid, d.$f_rate 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,";
  404. $sql .=" d.total_localtax1 as total_localtax1, d.total_localtax2 as total_localtax2, ";
  405. $sql.= " e.date_debut as date_start, e.date_fin as date_end, e.fk_user_author,";
  406. $sql.= " e.ref as facnum, e.total_ttc as ftotal_ttc, e.date_create, d.fk_c_type_fees as type,";
  407. $sql.= " p.fk_bank as payment_id, p.amount as payment_amount, p.rowid as pid, e.ref as pref";
  408. $sql.= " FROM ".MAIN_DB_PREFIX."expensereport as e";
  409. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."expensereport_det as d ON d.fk_expensereport = e.rowid ";
  410. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."payment_expensereport as p ON p.fk_expensereport = e.rowid ";
  411. $sql.= " WHERE e.entity = " . $conf->entity;
  412. $sql.= " AND e.fk_statut in (6)";
  413. if ($y && $m)
  414. {
  415. $sql.= " AND p.datep >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  416. $sql.= " AND p.datep <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  417. }
  418. else if ($y)
  419. {
  420. $sql.= " AND p.datep >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  421. $sql.= " AND p.datep <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  422. }
  423. if ($q) $sql.= " AND (date_format(p.datep,'%m') > ".(($q-1)*3)." AND date_format(p.datep,'%m') <= ".($q*3).")";
  424. if ($date_start && $date_end) $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  425. $sql.= " AND (d.product_type = -1";
  426. $sql.= " OR e.date_debut is NOT null OR e.date_fin IS NOT NULL)"; // enhance detection of service
  427. if (empty($conf->global->MAIN_INCLUDE_ZERO_VAT_IN_REPORTS)) $sql.= " AND (d.".$f_rate." <> 0 OR d.total_tva <> 0)";
  428. $sql.= " ORDER BY e.rowid";
  429. if (! $sql)
  430. {
  431. dol_syslog("Tax.lib.php::tax_by_rate no accountancy module enabled".$sql,LOG_ERR);
  432. return -1; // -1 = Not accountancy module enabled
  433. }
  434. if ($sql == 'TODO') return -2; // -2 = Feature not yet available
  435. if ($sql != 'TODO')
  436. {
  437. dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
  438. $resql = $db->query($sql);
  439. if ($resql)
  440. {
  441. $company_id = -1;
  442. $oldrowid='';
  443. while($assoc = $db->fetch_array($resql))
  444. {
  445. if (! isset($list[$assoc['company_id']]['totalht'])) $list[$assoc['company_id']]['totalht']=0;
  446. if (! isset($list[$assoc['company_id']]['vat'])) $list[$assoc['company_id']]['vat']=0;
  447. if (! isset($list[$assoc['company_id']]['localtax1'])) $list[$assoc['company_id']]['localtax1']=0;
  448. if (! isset($list[$assoc['company_id']]['localtax2'])) $list[$assoc['company_id']]['localtax2']=0;
  449. if ($assoc['rowid'] != $oldrowid) // Si rupture sur d.rowid
  450. {
  451. $oldrowid=$assoc['rowid'];
  452. $list[$assoc['company_id']]['totalht'] += $assoc['total_ht'];
  453. $list[$assoc['company_id']]['vat'] += $assoc['total_vat'];
  454. $list[$assoc['company_id']]['localtax1'] += $assoc['total_localtax1'];
  455. $list[$assoc['company_id']]['localtax2'] += $assoc['total_localtax2'];
  456. }
  457. $list[$assoc['company_id']]['dtotal_ttc'][] = $assoc['total_ttc'];
  458. $list[$assoc['company_id']]['dtype'][] = 'ExpenseReportPayment';
  459. $list[$assoc['company_id']]['datef'][] = $assoc['datef'];
  460. $list[$assoc['company_id']]['company_name'][] = '';
  461. $list[$assoc['company_id']]['company_id'][] = '';
  462. $list[$assoc['company_id']]['user_id'][] = $assoc['fk_user_author'];
  463. $list[$assoc['company_id']]['drate'][] = $assoc['rate'];
  464. $list[$assoc['company_id']]['ddate_start'][] = $db->jdate($assoc['date_start']);
  465. $list[$assoc['company_id']]['ddate_end'][] = $db->jdate($assoc['date_end']);
  466. $list[$assoc['company_id']]['facid'][] = $assoc['facid'];
  467. $list[$assoc['company_id']]['facnum'][] = $assoc['facnum'];
  468. $list[$assoc['company_id']]['type'][] = $assoc['type'];
  469. $list[$assoc['company_id']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
  470. $list[$assoc['company_id']]['descr'][] = $assoc['descr'];
  471. $list[$assoc['company_id']]['totalht_list'][] = $assoc['total_ht'];
  472. $list[$assoc['company_id']]['vat_list'][] = $assoc['total_vat'];
  473. $list[$assoc['company_id']]['localtax1_list'][] = $assoc['total_localtax1'];
  474. $list[$assoc['company_id']]['localtax2_list'][] = $assoc['total_localtax2'];
  475. $list[$assoc['company_id']]['pid'][] = $assoc['pid'];
  476. $list[$assoc['company_id']]['pref'][] = $assoc['pref'];
  477. $list[$assoc['company_id']]['ptype'][] = 'ExpenseReportPayment';
  478. $list[$assoc['company_id']]['payment_id'][] = $assoc['payment_id'];
  479. $list[$assoc['company_id']]['payment_amount'][] = $assoc['payment_amount'];
  480. $company_id = $assoc['company_id'];
  481. }
  482. }
  483. else
  484. {
  485. dol_print_error($db);
  486. return -3;
  487. }
  488. }
  489. }
  490. return $list;
  491. }
  492. /**
  493. * Gets Tax to collect for the given year (and given quarter or month)
  494. * The function gets the Tax in split results, as the Tax declaration asks
  495. * to report the amounts for different Tax rates as different lines.
  496. *
  497. * @param string $type Tax type, either 'vat', 'localtax1' or 'localtax2'
  498. * @param DoliDB $db Database handler object
  499. * @param int $y Year
  500. * @param int $q Quarter
  501. * @param string $date_start Start date
  502. * @param string $date_end End date
  503. * @param int $modetax Not used
  504. * @param int $direction 'sell' (customer invoice) or 'buy' (supplier invoices)
  505. * @param int $m Month
  506. * @return array Array with details of VATs (per rate), -1 if no accountancy module, -2 if not yet developped, -3 if error
  507. */
  508. function tax_by_rate($type, $db, $y, $q, $date_start, $date_end, $modetax, $direction, $m=0)
  509. {
  510. global $conf;
  511. // If we use date_start and date_end, we must not use $y, $m, $q
  512. if (($date_start || $date_end) && (! empty($y) || ! empty($m) || ! empty($q)))
  513. {
  514. dol_print_error('', 'Bad value of input parameter for tax_by_rate');
  515. }
  516. $list=array();
  517. if ($direction == 'sell')
  518. {
  519. $invoicetable='facture';
  520. $invoicedettable='facturedet';
  521. $fk_facture='fk_facture';
  522. $fk_facture2='fk_facture';
  523. $fk_payment='fk_paiement';
  524. $total_tva='total_tva';
  525. $paymenttable='paiement';
  526. $paymentfacturetable='paiement_facture';
  527. $invoicefieldref='facnumber';
  528. }
  529. else
  530. {
  531. $invoicetable='facture_fourn';
  532. $invoicedettable='facture_fourn_det';
  533. $fk_facture='fk_facture_fourn';
  534. $fk_facture2='fk_facturefourn';
  535. $fk_payment='fk_paiementfourn';
  536. $total_tva='tva';
  537. $paymenttable='paiementfourn';
  538. $paymentfacturetable='paiementfourn_facturefourn';
  539. $invoicefieldref='ref';
  540. }
  541. if ( strpos( $type, 'localtax' ) === 0 ) {
  542. $f_rate = $type . '_tx';
  543. } else {
  544. $f_rate = 'tva_tx';
  545. }
  546. $total_localtax1='total_localtax1';
  547. $total_localtax2='total_localtax2';
  548. // CAS DES BIENS/PRODUITS
  549. // Define sql request
  550. $sql='';
  551. if (($direction == 'sell' && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
  552. || ($direction == 'buy' && $conf->global->TAX_MODE_BUY_PRODUCT == 'invoice'))
  553. {
  554. // Count on delivery date (use invoice date as delivery is unknown)
  555. $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
  556. $sql .=" d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  557. $sql.= " d.date_start as date_start, d.date_end as date_end,";
  558. $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,";
  559. $sql.= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
  560. $sql.= " 0 as payment_id, 0 as payment_amount";
  561. $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  562. $sql.= " ".MAIN_DB_PREFIX."societe as s,";
  563. $sql.= " ".MAIN_DB_PREFIX.$invoicedettable." as d" ;
  564. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  565. $sql.= " WHERE f.entity = " . $conf->entity;
  566. $sql.= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
  567. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql.= " AND f.type IN (0,1,2,5)";
  568. else $sql.= " AND f.type IN (0,1,2,3,5)";
  569. $sql.= " AND f.rowid = d.".$fk_facture;
  570. $sql.= " AND s.rowid = f.fk_soc";
  571. if ($y && $m)
  572. {
  573. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  574. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  575. }
  576. else if ($y)
  577. {
  578. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  579. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  580. }
  581. if ($q) $sql.= " AND (date_format(f.datef,'%m') > ".(($q-1)*3)." AND date_format(f.datef,'%m') <= ".($q*3).")";
  582. if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  583. $sql.= " AND (d.product_type = 0"; // Limit to products
  584. $sql.= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of products
  585. if (empty($conf->global->MAIN_INCLUDE_ZERO_VAT_IN_REPORTS)) $sql.= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
  586. $sql.= " ORDER BY d.rowid, d.".$fk_facture;
  587. }
  588. else
  589. {
  590. // Count on payments date
  591. $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
  592. $sql .=" d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  593. $sql.= " d.date_start as date_start, d.date_end as date_end,";
  594. $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,";
  595. $sql.= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
  596. $sql.= " pf.".$fk_payment." as payment_id, pf.amount as payment_amount,";
  597. $sql.= " pa.datep as datep";
  598. $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  599. $sql.= " ".MAIN_DB_PREFIX.$paymentfacturetable." as pf,";
  600. $sql.= " ".MAIN_DB_PREFIX.$paymenttable." as pa,";
  601. $sql.= " ".MAIN_DB_PREFIX."societe as s,";
  602. $sql.= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
  603. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  604. $sql.= " WHERE f.entity = " . $conf->entity;
  605. $sql.= " AND f.fk_statut in (1,2)"; // Paid (partially or completely)
  606. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql.= " AND f.type IN (0,1,2,5)";
  607. else $sql.= " AND f.type IN (0,1,2,3,5)";
  608. $sql.= " AND f.rowid = d.".$fk_facture;
  609. $sql.= " AND s.rowid = f.fk_soc";
  610. $sql.= " AND pf.".$fk_facture2." = f.rowid";
  611. $sql.= " AND pa.rowid = pf.".$fk_payment;
  612. if ($y && $m)
  613. {
  614. $sql.= " AND pa.datep >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  615. $sql.= " AND pa.datep <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  616. }
  617. else if ($y)
  618. {
  619. $sql.= " AND pa.datep >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  620. $sql.= " AND pa.datep <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  621. }
  622. if ($q) $sql.= " AND (date_format(pa.datep,'%m') > ".(($q-1)*3)." AND date_format(pa.datep,'%m') <= ".($q*3).")";
  623. if ($date_start && $date_end) $sql.= " AND pa.datep >= '".$db->idate($date_start)."' AND pa.datep <= '".$db->idate($date_end)."'";
  624. $sql.= " AND (d.product_type = 0"; // Limit to products
  625. $sql.= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of products
  626. if (empty($conf->global->MAIN_INCLUDE_ZERO_VAT_IN_REPORTS)) $sql.= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
  627. $sql.= " ORDER BY d.rowid, d.".$fk_facture.", pf.rowid";
  628. }
  629. if (! $sql) return -1;
  630. if ($sql == 'TODO') return -2;
  631. if ($sql != 'TODO')
  632. {
  633. dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
  634. $resql = $db->query($sql);
  635. if ($resql)
  636. {
  637. $rate = -1;
  638. $oldrowid='';
  639. while($assoc = $db->fetch_array($resql))
  640. {
  641. // Code to avoid warnings when array entry not defined
  642. if (! isset($list[$assoc['rate']]['totalht'])) $list[$assoc['rate']]['totalht']=0;
  643. if (! isset($list[$assoc['rate']]['vat'])) $list[$assoc['rate']]['vat']=0;
  644. if (! isset($list[$assoc['rate']]['localtax1'])) $list[$assoc['rate']]['localtax1']=0;
  645. if (! isset($list[$assoc['rate']]['localtax2'])) $list[$assoc['rate']]['localtax2']=0;
  646. if ($assoc['rowid'] != $oldrowid) // Si rupture sur d.rowid
  647. {
  648. $oldrowid=$assoc['rowid'];
  649. $list[$assoc['rate']]['totalht'] += $assoc['total_ht'];
  650. $list[$assoc['rate']]['vat'] += $assoc['total_vat'];
  651. $list[$assoc['rate']]['localtax1'] += $assoc['total_localtax1'];
  652. $list[$assoc['rate']]['localtax2'] += $assoc['total_localtax2'];
  653. }
  654. $list[$assoc['rate']]['dtotal_ttc'][] = $assoc['total_ttc'];
  655. $list[$assoc['rate']]['dtype'][] = $assoc['dtype'];
  656. $list[$assoc['rate']]['datef'][] = $db->jdate($assoc['datef']);
  657. $list[$assoc['rate']]['datep'][] = $db->jdate($assoc['datep']);
  658. $list[$assoc['rate']]['company_name'][] = $assoc['company_name'];
  659. $list[$assoc['rate']]['company_id'][] = $assoc['company_id'];
  660. $list[$assoc['rate']]['ddate_start'][] = $db->jdate($assoc['date_start']);
  661. $list[$assoc['rate']]['ddate_end'][] = $db->jdate($assoc['date_end']);
  662. $list[$assoc['rate']]['facid'][] = $assoc['facid'];
  663. $list[$assoc['rate']]['facnum'][] = $assoc['facnum'];
  664. $list[$assoc['rate']]['type'][] = $assoc['type'];
  665. $list[$assoc['rate']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
  666. $list[$assoc['rate']]['descr'][] = $assoc['descr'];
  667. $list[$assoc['rate']]['totalht_list'][] = $assoc['total_ht'];
  668. $list[$assoc['rate']]['vat_list'][] = $assoc['total_vat'];
  669. $list[$assoc['rate']]['localtax1_list'][] = $assoc['total_localtax1'];
  670. $list[$assoc['rate']]['localtax2_list'][] = $assoc['total_localtax2'];
  671. $list[$assoc['rate']]['pid'][] = $assoc['pid'];
  672. $list[$assoc['rate']]['pref'][] = $assoc['pref'];
  673. $list[$assoc['rate']]['ptype'][] = $assoc['ptype'];
  674. $list[$assoc['rate']]['payment_id'][] = $assoc['payment_id'];
  675. $list[$assoc['rate']]['payment_amount'][] = $assoc['payment_amount'];
  676. $rate = $assoc['rate'];
  677. }
  678. }
  679. else
  680. {
  681. dol_print_error($db);
  682. return -3;
  683. }
  684. }
  685. // CAS DES SERVICES
  686. // Define sql request
  687. $sql='';
  688. if (($direction == 'sell' && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice')
  689. || ($direction == 'buy' && $conf->global->TAX_MODE_BUY_SERVICE == 'invoice'))
  690. {
  691. // Count on invoice date
  692. $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
  693. $sql .=" d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  694. $sql.= " d.date_start as date_start, d.date_end as date_end,";
  695. $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,";
  696. $sql.= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
  697. $sql.= " 0 as payment_id, 0 as payment_amount";
  698. $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  699. $sql.= " ".MAIN_DB_PREFIX."societe as s,";
  700. $sql.= " ".MAIN_DB_PREFIX.$invoicedettable." as d" ;
  701. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  702. $sql.= " WHERE f.entity = " . $conf->entity;
  703. $sql.= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
  704. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql.= " AND f.type IN (0,1,2,5)";
  705. else $sql.= " AND f.type IN (0,1,2,3,5)";
  706. $sql.= " AND f.rowid = d.".$fk_facture;
  707. $sql.= " AND s.rowid = f.fk_soc";
  708. if ($y && $m)
  709. {
  710. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  711. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  712. }
  713. else if ($y)
  714. {
  715. $sql.= " AND f.datef >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  716. $sql.= " AND f.datef <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  717. }
  718. if ($q) $sql.= " AND (date_format(f.datef,'%m') > ".(($q-1)*3)." AND date_format(f.datef,'%m') <= ".($q*3).")";
  719. if ($date_start && $date_end) $sql.= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  720. $sql.= " AND (d.product_type = 1"; // Limit to services
  721. $sql.= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
  722. if (empty($conf->global->MAIN_INCLUDE_ZERO_VAT_IN_REPORTS)) $sql.= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
  723. $sql.= " ORDER BY d.rowid, d.".$fk_facture;
  724. }
  725. else
  726. {
  727. // Count on payments date
  728. $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
  729. $sql .=" d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  730. $sql.= " d.date_start as date_start, d.date_end as date_end,";
  731. $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,";
  732. $sql.= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
  733. $sql.= " pf.".$fk_payment." as payment_id, pf.amount as payment_amount,";
  734. $sql.= " pa.datep as datep";
  735. $sql.= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  736. $sql.= " ".MAIN_DB_PREFIX.$paymentfacturetable." as pf,";
  737. $sql.= " ".MAIN_DB_PREFIX.$paymenttable." as pa,";
  738. $sql.= " ".MAIN_DB_PREFIX."societe as s,";
  739. $sql.= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
  740. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  741. $sql.= " WHERE f.entity = " . $conf->entity;
  742. $sql.= " AND f.fk_statut in (1,2)"; // Paid (partially or completely)
  743. if (! empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) $sql.= " AND f.type IN (0,1,2,5)";
  744. else $sql.= " AND f.type IN (0,1,2,3,5)";
  745. $sql.= " AND f.rowid = d.".$fk_facture;
  746. $sql.= " AND s.rowid = f.fk_soc";
  747. $sql.= " AND pf.".$fk_facture2." = f.rowid";
  748. $sql.= " AND pa.rowid = pf.".$fk_payment;
  749. if ($y && $m)
  750. {
  751. $sql.= " AND pa.datep >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  752. $sql.= " AND pa.datep <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  753. }
  754. else if ($y)
  755. {
  756. $sql.= " AND pa.datep >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  757. $sql.= " AND pa.datep <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  758. }
  759. if ($q) $sql.= " AND (date_format(pa.datep,'%m') > ".(($q-1)*3)." AND date_format(pa.datep,'%m') <= ".($q*3).")";
  760. if ($date_start && $date_end) $sql.= " AND pa.datep >= '".$db->idate($date_start)."' AND pa.datep <= '".$db->idate($date_end)."'";
  761. $sql.= " AND (d.product_type = 1"; // Limit to services
  762. $sql.= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
  763. if (empty($conf->global->MAIN_INCLUDE_ZERO_VAT_IN_REPORTS)) $sql.= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
  764. $sql.= " ORDER BY d.rowid, d.".$fk_facture.", pf.rowid";
  765. }
  766. if (! $sql)
  767. {
  768. dol_syslog("Tax.lib.php::tax_by_rate no accountancy module enabled".$sql,LOG_ERR);
  769. return -1; // -1 = Not accountancy module enabled
  770. }
  771. if ($sql == 'TODO') return -2; // -2 = Feature not yet available
  772. if ($sql != 'TODO')
  773. {
  774. dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
  775. $resql = $db->query($sql);
  776. if ($resql)
  777. {
  778. $rate = -1;
  779. $oldrowid='';
  780. while($assoc = $db->fetch_array($resql))
  781. {
  782. // Code to avoid warnings when array entry not defined
  783. if (! isset($list[$assoc['rate']]['totalht'])) $list[$assoc['rate']]['totalht']=0;
  784. if (! isset($list[$assoc['rate']]['vat'])) $list[$assoc['rate']]['vat']=0;
  785. if (! isset($list[$assoc['rate']]['localtax1'])) $list[$assoc['rate']]['localtax1']=0;
  786. if (! isset($list[$assoc['rate']]['localtax2'])) $list[$assoc['rate']]['localtax2']=0;
  787. if ($assoc['rowid'] != $oldrowid) // Si rupture sur d.rowid
  788. {
  789. $oldrowid=$assoc['rowid'];
  790. $list[$assoc['rate']]['totalht'] += $assoc['total_ht'];
  791. $list[$assoc['rate']]['vat'] += $assoc['total_vat'];
  792. $list[$assoc['rate']]['localtax1'] += $assoc['total_localtax1'];
  793. $list[$assoc['rate']]['localtax2'] += $assoc['total_localtax2'];
  794. }
  795. $list[$assoc['rate']]['dtotal_ttc'][] = $assoc['total_ttc'];
  796. $list[$assoc['rate']]['dtype'][] = $assoc['dtype'];
  797. $list[$assoc['rate']]['datef'][] = $db->jdate($assoc['datef']);
  798. $list[$assoc['rate']]['datep'][] = $db->jdate($assoc['datep']);
  799. $list[$assoc['rate']]['company_name'][] = $assoc['company_name'];
  800. $list[$assoc['rate']]['company_id'][] = $assoc['company_id'];
  801. $list[$assoc['rate']]['ddate_start'][] = $db->jdate($assoc['date_start']);
  802. $list[$assoc['rate']]['ddate_end'][] = $db->jdate($assoc['date_end']);
  803. $list[$assoc['rate']]['facid'][] = $assoc['facid'];
  804. $list[$assoc['rate']]['facnum'][] = $assoc['facnum'];
  805. $list[$assoc['rate']]['type'][] = $assoc['type'];
  806. $list[$assoc['rate']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
  807. $list[$assoc['rate']]['descr'][] = $assoc['descr'];
  808. $list[$assoc['rate']]['totalht_list'][] = $assoc['total_ht'];
  809. $list[$assoc['rate']]['vat_list'][] = $assoc['total_vat'];
  810. $list[$assoc['rate']]['localtax1_list'][] = $assoc['total_localtax1'];
  811. $list[$assoc['rate']]['localtax2_list'][] = $assoc['total_localtax2'];
  812. $list[$assoc['rate']]['pid'][] = $assoc['pid'];
  813. $list[$assoc['rate']]['pref'][] = $assoc['pref'];
  814. $list[$assoc['rate']]['ptype'][] = $assoc['ptype'];
  815. $list[$assoc['rate']]['payment_id'][] = $assoc['payment_id'];
  816. $list[$assoc['rate']]['payment_amount'][] = $assoc['payment_amount'];
  817. $rate = $assoc['rate'];
  818. }
  819. }
  820. else
  821. {
  822. dol_print_error($db);
  823. return -3;
  824. }
  825. }
  826. // CASE OF EXPENSE REPORT
  827. if ($direction == 'buy') // buy only for expense reports
  828. {
  829. // Define sql request
  830. $sql='';
  831. // Count on payments date
  832. $sql = "SELECT d.rowid, d.product_type as dtype, e.rowid as facid, d.$f_rate 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,";
  833. $sql .=" d.total_localtax1 as total_localtax1, d.total_localtax2 as total_localtax2, ";
  834. $sql.= " e.date_debut as date_start, e.date_fin as date_end, e.fk_user_author,";
  835. $sql.= " e.ref as facnum, e.total_ttc as ftotal_ttc, e.date_create, d.fk_c_type_fees as type,";
  836. $sql.= " p.fk_bank as payment_id, p.amount as payment_amount, p.rowid as pid, e.ref as pref";
  837. $sql.= " FROM ".MAIN_DB_PREFIX."expensereport as e ";
  838. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."expensereport_det as d ON d.fk_expensereport = e.rowid ";
  839. $sql.= " LEFT JOIN ".MAIN_DB_PREFIX."payment_expensereport as p ON p.fk_expensereport = e.rowid ";
  840. $sql.= " WHERE e.entity = " . $conf->entity;
  841. $sql.= " AND e.fk_statut in (6)";
  842. if ($y && $m)
  843. {
  844. $sql.= " AND p.datep >= '".$db->idate(dol_get_first_day($y,$m,false))."'";
  845. $sql.= " AND p.datep <= '".$db->idate(dol_get_last_day($y,$m,false))."'";
  846. }
  847. else if ($y)
  848. {
  849. $sql.= " AND p.datep >= '".$db->idate(dol_get_first_day($y,1,false))."'";
  850. $sql.= " AND p.datep <= '".$db->idate(dol_get_last_day($y,12,false))."'";
  851. }
  852. if ($q) $sql.= " AND (date_format(p.datep,'%m') > ".(($q-1)*3)." AND date_format(p.datep,'%m') <= ".($q*3).")";
  853. if ($date_start && $date_end) $sql.= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  854. $sql.= " AND (d.product_type = -1";
  855. $sql.= " OR e.date_debut is NOT null OR e.date_fin IS NOT NULL)"; // enhance detection of service
  856. if (empty($conf->global->MAIN_INCLUDE_ZERO_VAT_IN_REPORTS)) $sql.= " AND (d.".$f_rate." <> 0 OR d.total_tva <> 0)";
  857. $sql.= " ORDER BY e.rowid";
  858. if (! $sql)
  859. {
  860. dol_syslog("Tax.lib.php::tax_by_rate no accountancy module enabled".$sql,LOG_ERR);
  861. return -1; // -1 = Not accountancy module enabled
  862. }
  863. if ($sql == 'TODO') return -2; // -2 = Feature not yet available
  864. if ($sql != 'TODO')
  865. {
  866. dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
  867. $resql = $db->query($sql);
  868. if ($resql)
  869. {
  870. $rate = -1;
  871. $oldrowid='';
  872. while($assoc = $db->fetch_array($resql))
  873. {
  874. // Code to avoid warnings when array entry not defined
  875. if (! isset($list[$assoc['rate']]['totalht'])) $list[$assoc['rate']]['totalht']=0;
  876. if (! isset($list[$assoc['rate']]['vat'])) $list[$assoc['rate']]['vat']=0;
  877. if (! isset($list[$assoc['rate']]['localtax1'])) $list[$assoc['rate']]['localtax1']=0;
  878. if (! isset($list[$assoc['rate']]['localtax2'])) $list[$assoc['rate']]['localtax2']=0;
  879. if ($assoc['rowid'] != $oldrowid) // Si rupture sur d.rowid
  880. {
  881. $oldrowid=$assoc['rowid'];
  882. $list[$assoc['rate']]['totalht'] += $assoc['total_ht'];
  883. $list[$assoc['rate']]['vat'] += $assoc['total_vat'];
  884. $list[$assoc['rate']]['localtax1'] += $assoc['total_localtax1'];
  885. $list[$assoc['rate']]['localtax2'] += $assoc['total_localtax2'];
  886. }
  887. $list[$assoc['rate']]['dtotal_ttc'][] = $assoc['total_ttc'];
  888. $list[$assoc['rate']]['dtype'][] = 'ExpenseReportPayment';
  889. $list[$assoc['rate']]['datef'][] = $assoc['datef'];
  890. $list[$assoc['rate']]['company_name'][] = '';
  891. $list[$assoc['rate']]['company_id'][] = '';
  892. $list[$assoc['rate']]['user_id'][] = $assoc['fk_user_author'];
  893. $list[$assoc['rate']]['ddate_start'][] = $db->jdate($assoc['date_start']);
  894. $list[$assoc['rate']]['ddate_end'][] = $db->jdate($assoc['date_end']);
  895. $list[$assoc['rate']]['facid'][] = $assoc['facid'];
  896. $list[$assoc['rate']]['facnum'][] = $assoc['facnum'];
  897. $list[$assoc['rate']]['type'][] = $assoc['type'];
  898. $list[$assoc['rate']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
  899. $list[$assoc['rate']]['descr'][] = $assoc['descr'];
  900. $list[$assoc['rate']]['totalht_list'][] = $assoc['total_ht'];
  901. $list[$assoc['rate']]['vat_list'][] = $assoc['total_vat'];
  902. $list[$assoc['rate']]['localtax1_list'][] = $assoc['total_localtax1'];
  903. $list[$assoc['rate']]['localtax2_list'][] = $assoc['total_localtax2'];
  904. $list[$assoc['rate']]['pid'][] = $assoc['pid'];
  905. $list[$assoc['rate']]['pref'][] = $assoc['pref'];
  906. $list[$assoc['rate']]['ptype'][] = 'ExpenseReportPayment';
  907. $list[$assoc['rate']]['payment_id'][] = $assoc['payment_id'];
  908. $list[$assoc['rate']]['payment_amount'][] = $assoc['payment_amount'];
  909. $rate = $assoc['rate'];
  910. }
  911. }
  912. else
  913. {
  914. dol_print_error($db);
  915. return -3;
  916. }
  917. }
  918. }
  919. return $list;
  920. }