tax.lib.php 58 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261
  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. * Copyright (C) 2021-2022 Open-Dsi <support@open-dsi.fr>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  23. */
  24. /**
  25. * \file htdocs/core/lib/tax.lib.php
  26. * \ingroup tax
  27. * \brief Library for tax module
  28. */
  29. /**
  30. * Prepare array with list of tabs
  31. *
  32. * @param ChargeSociales $object Object related to tabs
  33. * @return array Array of tabs to show
  34. */
  35. function tax_prepare_head(ChargeSociales $object)
  36. {
  37. global $db, $langs, $conf, $user;
  38. $h = 0;
  39. $head = array();
  40. $head[$h][0] = DOL_URL_ROOT.'/compta/sociales/card.php?id='.$object->id;
  41. $head[$h][1] = $langs->trans('SocialContribution');
  42. $head[$h][2] = 'card';
  43. $h++;
  44. // Show more tabs from modules
  45. // Entries must be declared in modules descriptor with line
  46. // $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
  47. // $this->tabs = array('entity:-tabname); to remove a tab
  48. complete_head_from_modules($conf, $langs, $object, $head, $h, 'tax');
  49. require_once DOL_DOCUMENT_ROOT.'/core/lib/files.lib.php';
  50. require_once DOL_DOCUMENT_ROOT.'/core/class/link.class.php';
  51. $upload_dir = $conf->tax->dir_output."/".dol_sanitizeFileName($object->ref);
  52. $nbFiles = count(dol_dir_list($upload_dir, 'files', 0, '', '(\.meta|_preview.*\.png)$'));
  53. $nbLinks = Link::count($db, $object->element, $object->id);
  54. $head[$h][0] = DOL_URL_ROOT.'/compta/sociales/document.php?id='.$object->id;
  55. $head[$h][1] = $langs->trans("Documents");
  56. if (($nbFiles + $nbLinks) > 0) {
  57. $head[$h][1] .= '<span class="badge marginleftonlyshort">'.($nbFiles + $nbLinks).'</span>';
  58. }
  59. $head[$h][2] = 'documents';
  60. $h++;
  61. $nbNote = 0;
  62. if (!empty($object->note_private)) {
  63. $nbNote++;
  64. }
  65. if (!empty($object->note_public)) {
  66. $nbNote++;
  67. }
  68. $head[$h][0] = DOL_URL_ROOT.'/compta/sociales/note.php?id='.$object->id;
  69. $head[$h][1] = $langs->trans('Notes');
  70. if ($nbNote > 0) {
  71. $head[$h][1] .= (empty($conf->global->MAIN_OPTIMIZEFORTEXTBROWSER) ? '<span class="badge marginleftonlyshort">'.$nbNote.'</span>' : '');
  72. }
  73. $head[$h][2] = 'note';
  74. $h++;
  75. $head[$h][0] = DOL_URL_ROOT.'/compta/sociales/info.php?id='.$object->id;
  76. $head[$h][1] = $langs->trans("Info");
  77. $head[$h][2] = 'info';
  78. $h++;
  79. complete_head_from_modules($conf, $langs, $object, $head, $h, 'tax', 'remove');
  80. return $head;
  81. }
  82. /**
  83. * Look for collectable VAT clients in the chosen year (and month)
  84. *
  85. * @param string $type Tax type, either 'vat', 'localtax1' or 'localtax2'
  86. * @param DoliDB $db Database handle
  87. * @param int $y Year
  88. * @param string $date_start Start date
  89. * @param string $date_end End date
  90. * @param int $modetax Not used
  91. * @param string $direction 'sell' or 'buy'
  92. * @param int $m Month
  93. * @param int $q Quarter
  94. * @return array|int Array with details of VATs (per third parties), -1 if no accountancy module, -2 if not yet developped, -3 if error
  95. */
  96. function tax_by_thirdparty($type, $db, $y, $date_start, $date_end, $modetax, $direction, $m = 0, $q = 0)
  97. {
  98. global $conf;
  99. // If we use date_start and date_end, we must not use $y, $m, $q
  100. if (($date_start || $date_end) && (!empty($y) || !empty($m) || !empty($q))) {
  101. dol_print_error('', 'Bad value of input parameter for tax_by_rate');
  102. }
  103. $list = array();
  104. if ($direction == 'sell') {
  105. $invoicetable = 'facture';
  106. $invoicedettable = 'facturedet';
  107. $fk_facture = 'fk_facture';
  108. $fk_facture2 = 'fk_facture';
  109. $fk_payment = 'fk_paiement';
  110. $total_tva = 'total_tva';
  111. $paymenttable = 'paiement';
  112. $paymentfacturetable = 'paiement_facture';
  113. $invoicefieldref = 'ref';
  114. } elseif ($direction == 'buy') {
  115. $invoicetable = 'facture_fourn';
  116. $invoicedettable = 'facture_fourn_det';
  117. $fk_facture = 'fk_facture_fourn';
  118. $fk_facture2 = 'fk_facturefourn';
  119. $fk_payment = 'fk_paiementfourn';
  120. $total_tva = 'tva';
  121. $paymenttable = 'paiementfourn';
  122. $paymentfacturetable = 'paiementfourn_facturefourn';
  123. $invoicefieldref = 'ref';
  124. }
  125. if (strpos($type, 'localtax') === 0) {
  126. $f_rate = $type.'_tx';
  127. } else {
  128. $f_rate = 'tva_tx';
  129. }
  130. $total_localtax1 = 'total_localtax1';
  131. $total_localtax2 = 'total_localtax2';
  132. // CAS DES BIENS/PRODUITS
  133. // Define sql request
  134. $sql = '';
  135. if (($direction == 'sell' && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
  136. || ($direction == 'buy' && $conf->global->TAX_MODE_BUY_PRODUCT == 'invoice')) {
  137. // Count on delivery date (use invoice date as delivery is unknown)
  138. $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,";
  139. $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  140. $sql .= " d.date_start as date_start, d.date_end as date_end,";
  141. $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
  142. $sql .= " s.nom as company_name, s.name_alias as company_alias, s.rowid as company_id, s.client as company_client, s.fournisseur as company_fournisseur, s.email as company_email,";
  143. $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
  144. $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
  145. $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
  146. $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype, p.tosell as pstatus, p.tobuy as pstatusbuy,";
  147. $sql .= " 0 as payment_id, '' as payment_ref, 0 as payment_amount";
  148. $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  149. $sql .= " ".MAIN_DB_PREFIX."societe as s,";
  150. $sql .= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
  151. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  152. $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
  153. $sql .= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
  154. if ($direction == 'buy') {
  155. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  156. $sql .= " AND f.type IN (0,1,2,5)";
  157. } else {
  158. $sql .= " AND f.type IN (0,1,2,3,5)";
  159. }
  160. } else {
  161. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  162. $sql .= " AND f.type IN (0,1,2,5)";
  163. } else {
  164. $sql .= " AND f.type IN (0,1,2,3,5)";
  165. }
  166. }
  167. $sql .= " AND f.rowid = d.".$fk_facture;
  168. $sql .= " AND s.rowid = f.fk_soc";
  169. if ($y && $m) {
  170. $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
  171. $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
  172. } elseif ($y) {
  173. $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
  174. $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
  175. }
  176. if ($q) {
  177. $sql .= " AND f.datef > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
  178. $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
  179. }
  180. if ($date_start && $date_end) {
  181. $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  182. }
  183. $sql .= " AND (d.product_type = 0"; // Limit to products
  184. $sql .= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of products
  185. if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) {
  186. $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
  187. }
  188. $sql .= " ORDER BY d.rowid, d.".$fk_facture;
  189. } else {
  190. // Count on payments date
  191. $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,";
  192. $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  193. $sql .= " d.date_start as date_start, d.date_end as date_end,";
  194. $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
  195. $sql .= " s.nom as company_name, s.name_alias as company_alias, s.rowid as company_id, s.client as company_client, s.fournisseur as company_fournisseur, s.email as company_email,";
  196. $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
  197. $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
  198. $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
  199. $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype, p.tosell as pstatus, p.tobuy as pstatusbuy,";
  200. $sql .= " pf.".$fk_payment." as payment_id, pf.amount as payment_amount,";
  201. $sql .= " pa.datep as datep, pa.ref as payment_ref";
  202. $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  203. $sql .= " ".MAIN_DB_PREFIX.$paymentfacturetable." as pf,";
  204. $sql .= " ".MAIN_DB_PREFIX.$paymenttable." as pa,";
  205. $sql .= " ".MAIN_DB_PREFIX."societe as s,";
  206. $sql .= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
  207. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  208. $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
  209. $sql .= " AND f.fk_statut in (1,2)"; // Paid (partially or completely)
  210. if ($direction == 'buy') {
  211. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  212. $sql .= " AND f.type IN (0,1,2,5)";
  213. } else {
  214. $sql .= " AND f.type IN (0,1,2,3,5)";
  215. }
  216. } else {
  217. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  218. $sql .= " AND f.type IN (0,1,2,5)";
  219. } else {
  220. $sql .= " AND f.type IN (0,1,2,3,5)";
  221. }
  222. }
  223. $sql .= " AND f.rowid = d.".$fk_facture;
  224. $sql .= " AND s.rowid = f.fk_soc";
  225. $sql .= " AND pf.".$fk_facture2." = f.rowid";
  226. $sql .= " AND pa.rowid = pf.".$fk_payment;
  227. if ($y && $m) {
  228. $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
  229. $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
  230. } elseif ($y) {
  231. $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
  232. $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
  233. }
  234. if ($q) {
  235. $sql .= " AND pa.datep > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
  236. $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
  237. }
  238. if ($date_start && $date_end) {
  239. $sql .= " AND pa.datep >= '".$db->idate($date_start)."' AND pa.datep <= '".$db->idate($date_end)."'";
  240. }
  241. $sql .= " AND (d.product_type = 0"; // Limit to products
  242. $sql .= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of products
  243. if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) {
  244. $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
  245. }
  246. $sql .= " ORDER BY d.rowid, d.".$fk_facture.", pf.rowid";
  247. }
  248. if (!$sql) {
  249. return -1;
  250. }
  251. if ($sql == 'TODO') {
  252. return -2;
  253. }
  254. if ($sql != 'TODO') {
  255. dol_syslog("Tax.lib.php::tax_by_thirdparty", LOG_DEBUG);
  256. $resql = $db->query($sql);
  257. if ($resql) {
  258. $company_id = -1;
  259. $oldrowid = '';
  260. while ($assoc = $db->fetch_array($resql)) {
  261. if (!isset($list[$assoc['company_id']]['totalht'])) {
  262. $list[$assoc['company_id']]['totalht'] = 0;
  263. }
  264. if (!isset($list[$assoc['company_id']]['vat'])) {
  265. $list[$assoc['company_id']]['vat'] = 0;
  266. }
  267. if (!isset($list[$assoc['company_id']]['localtax1'])) {
  268. $list[$assoc['company_id']]['localtax1'] = 0;
  269. }
  270. if (!isset($list[$assoc['company_id']]['localtax2'])) {
  271. $list[$assoc['company_id']]['localtax2'] = 0;
  272. }
  273. if ($assoc['rowid'] != $oldrowid) { // Si rupture sur d.rowid
  274. $oldrowid = $assoc['rowid'];
  275. $list[$assoc['company_id']]['totalht'] += $assoc['total_ht'];
  276. $list[$assoc['company_id']]['vat'] += $assoc['total_vat'];
  277. $list[$assoc['company_id']]['localtax1'] += $assoc['total_localtax1'];
  278. $list[$assoc['company_id']]['localtax2'] += $assoc['total_localtax2'];
  279. }
  280. $list[$assoc['company_id']]['dtotal_ttc'][] = $assoc['total_ttc'];
  281. $list[$assoc['company_id']]['dtype'][] = $assoc['dtype'];
  282. $list[$assoc['company_id']]['datef'][] = $db->jdate($assoc['datef']);
  283. $list[$assoc['company_id']]['datep'][] = $db->jdate($assoc['datep']);
  284. $list[$assoc['company_id']]['company_name'][] = $assoc['company_name'];
  285. $list[$assoc['company_id']]['company_id'][] = $assoc['company_id'];
  286. $list[$assoc['company_id']]['company_alias'][] = $assoc['company_alias'];
  287. $list[$assoc['company_id']]['company_email'][] = $assoc['company_email'];
  288. $list[$assoc['company_id']]['company_tva_intra'][] = $assoc['company_tva_intra'];
  289. $list[$assoc['company_id']]['company_client'][] = $assoc['company_client'];
  290. $list[$assoc['company_id']]['company_fournisseur'][] = $assoc['company_fournisseur'];
  291. $list[$assoc['company_id']]['company_customer_code'][] = $assoc['company_customer_code'];
  292. $list[$assoc['company_id']]['company_supplier_code'][] = $assoc['company_supplier_code'];
  293. $list[$assoc['company_id']]['company_customer_accounting_code'][] = $assoc['company_customer_accounting_code'];
  294. $list[$assoc['company_id']]['company_supplier_accounting_code'][] = $assoc['company_supplier_accounting_code'];
  295. $list[$assoc['company_id']]['company_status'][] = $assoc['company_status'];
  296. $list[$assoc['company_id']]['drate'][] = $assoc['rate'];
  297. $list[$assoc['company_id']]['ddate_start'][] = $db->jdate($assoc['date_start']);
  298. $list[$assoc['company_id']]['ddate_end'][] = $db->jdate($assoc['date_end']);
  299. $list[$assoc['company_id']]['facid'][] = $assoc['facid'];
  300. $list[$assoc['company_id']]['facnum'][] = $assoc['facnum'];
  301. $list[$assoc['company_id']]['type'][] = $assoc['type'];
  302. $list[$assoc['company_id']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
  303. $list[$assoc['company_id']]['descr'][] = $assoc['descr'];
  304. $list[$assoc['company_id']]['totalht_list'][] = $assoc['total_ht'];
  305. $list[$assoc['company_id']]['vat_list'][] = $assoc['total_vat'];
  306. $list[$assoc['company_id']]['localtax1_list'][] = $assoc['total_localtax1'];
  307. $list[$assoc['company_id']]['localtax2_list'][] = $assoc['total_localtax2'];
  308. $list[$assoc['company_id']]['pid'][] = $assoc['pid'];
  309. $list[$assoc['company_id']]['pref'][] = $assoc['pref'];
  310. $list[$assoc['company_id']]['ptype'][] = $assoc['ptype'];
  311. $list[$assoc['company_id']]['payment_id'][] = $assoc['payment_id'];
  312. $list[$assoc['company_id']]['payment_amount'][] = $assoc['payment_amount'];
  313. $company_id = $assoc['company_id'];
  314. }
  315. } else {
  316. dol_print_error($db);
  317. return -3;
  318. }
  319. }
  320. // CAS DES SERVICES
  321. // Define sql request
  322. $sql = '';
  323. if (($direction == 'sell' && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice')
  324. || ($direction == 'buy' && $conf->global->TAX_MODE_BUY_SERVICE == 'invoice')) {
  325. // Count on invoice date
  326. $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,";
  327. $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  328. $sql .= " d.date_start as date_start, d.date_end as date_end,";
  329. $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
  330. $sql .= " s.nom as company_name, s.name_alias as company_alias, s.rowid as company_id, s.client as company_client, s.fournisseur as company_fournisseur, s.email as company_email,";
  331. $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
  332. $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
  333. $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
  334. $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype, p.tosell as pstatus, p.tobuy as pstatusbuy,";
  335. $sql .= " 0 as payment_id, '' as payment_ref, 0 as payment_amount";
  336. $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  337. $sql .= " ".MAIN_DB_PREFIX."societe as s,";
  338. $sql .= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
  339. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  340. $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
  341. $sql .= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
  342. if ($direction == 'buy') {
  343. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  344. $sql .= " AND f.type IN (0,1,2,5)";
  345. } else {
  346. $sql .= " AND f.type IN (0,1,2,3,5)";
  347. }
  348. } else {
  349. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  350. $sql .= " AND f.type IN (0,1,2,5)";
  351. } else {
  352. $sql .= " AND f.type IN (0,1,2,3,5)";
  353. }
  354. }
  355. $sql .= " AND f.rowid = d.".$fk_facture;
  356. $sql .= " AND s.rowid = f.fk_soc";
  357. if ($y && $m) {
  358. $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
  359. $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
  360. } elseif ($y) {
  361. $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
  362. $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
  363. }
  364. if ($q) {
  365. $sql .= " AND f.datef > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
  366. $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
  367. }
  368. if ($date_start && $date_end) {
  369. $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  370. }
  371. $sql .= " AND (d.product_type = 1"; // Limit to services
  372. $sql .= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
  373. if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) {
  374. $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
  375. }
  376. $sql .= " ORDER BY d.rowid, d.".$fk_facture;
  377. } else {
  378. // Count on payments date
  379. $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,";
  380. $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  381. $sql .= " d.date_start as date_start, d.date_end as date_end,";
  382. $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
  383. $sql .= " s.nom as company_name, s.name_alias as company_alias, s.rowid as company_id, s.client as company_client, s.fournisseur as company_fournisseur, s.email as company_email,";
  384. $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
  385. $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
  386. $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
  387. $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype, p.tosell as pstatus, p.tobuy as pstatusbuy,";
  388. $sql .= " pf.".$fk_payment." as payment_id, pf.amount as payment_amount,";
  389. $sql .= " pa.datep as datep, pa.ref as payment_ref";
  390. $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f,";
  391. $sql .= " ".MAIN_DB_PREFIX.$paymentfacturetable." as pf,";
  392. $sql .= " ".MAIN_DB_PREFIX.$paymenttable." as pa,";
  393. $sql .= " ".MAIN_DB_PREFIX."societe as s,";
  394. $sql .= " ".MAIN_DB_PREFIX.$invoicedettable." as d";
  395. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  396. $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
  397. $sql .= " AND f.fk_statut in (1,2)"; // Paid (partially or completely)
  398. if ($direction == 'buy') {
  399. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  400. $sql .= " AND f.type IN (0,1,2,5)";
  401. } else {
  402. $sql .= " AND f.type IN (0,1,2,3,5)";
  403. }
  404. } else {
  405. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  406. $sql .= " AND f.type IN (0,1,2,5)";
  407. } else {
  408. $sql .= " AND f.type IN (0,1,2,3,5)";
  409. }
  410. }
  411. $sql .= " AND f.rowid = d.".$fk_facture;
  412. $sql .= " AND s.rowid = f.fk_soc";
  413. $sql .= " AND pf.".$fk_facture2." = f.rowid";
  414. $sql .= " AND pa.rowid = pf.".$fk_payment;
  415. if ($y && $m) {
  416. $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
  417. $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
  418. } elseif ($y) {
  419. $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
  420. $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
  421. }
  422. if ($q) {
  423. $sql .= " AND pa.datep > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
  424. $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
  425. }
  426. if ($date_start && $date_end) {
  427. $sql .= " AND pa.datep >= '".$db->idate($date_start)."' AND pa.datep <= '".$db->idate($date_end)."'";
  428. }
  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. if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) {
  432. $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
  433. }
  434. $sql .= " ORDER BY d.rowid, d.".$fk_facture.", pf.rowid";
  435. }
  436. if (!$sql) {
  437. dol_syslog("Tax.lib.php::tax_by_rate no accountancy module enabled".$sql, LOG_ERR);
  438. return -1; // -1 = Not accountancy module enabled
  439. }
  440. if ($sql == 'TODO') {
  441. return -2; // -2 = Feature not yet available
  442. }
  443. if ($sql != 'TODO') {
  444. dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
  445. $resql = $db->query($sql);
  446. if ($resql) {
  447. $company_id = -1;
  448. $oldrowid = '';
  449. while ($assoc = $db->fetch_array($resql)) {
  450. if (!isset($list[$assoc['company_id']]['totalht'])) {
  451. $list[$assoc['company_id']]['totalht'] = 0;
  452. }
  453. if (!isset($list[$assoc['company_id']]['vat'])) {
  454. $list[$assoc['company_id']]['vat'] = 0;
  455. }
  456. if (!isset($list[$assoc['company_id']]['localtax1'])) {
  457. $list[$assoc['company_id']]['localtax1'] = 0;
  458. }
  459. if (!isset($list[$assoc['company_id']]['localtax2'])) {
  460. $list[$assoc['company_id']]['localtax2'] = 0;
  461. }
  462. if ($assoc['rowid'] != $oldrowid) { // Si rupture sur d.rowid
  463. $oldrowid = $assoc['rowid'];
  464. $list[$assoc['company_id']]['totalht'] += $assoc['total_ht'];
  465. $list[$assoc['company_id']]['vat'] += $assoc['total_vat'];
  466. $list[$assoc['company_id']]['localtax1'] += $assoc['total_localtax1'];
  467. $list[$assoc['company_id']]['localtax2'] += $assoc['total_localtax2'];
  468. }
  469. $list[$assoc['company_id']]['dtotal_ttc'][] = $assoc['total_ttc'];
  470. $list[$assoc['company_id']]['dtype'][] = $assoc['dtype'];
  471. $list[$assoc['company_id']]['datef'][] = $db->jdate($assoc['datef']);
  472. $list[$assoc['company_id']]['datep'][] = $db->jdate($assoc['datep']);
  473. $list[$assoc['company_id']]['company_name'][] = $assoc['company_name'];
  474. $list[$assoc['company_id']]['company_id'][] = $assoc['company_id'];
  475. $list[$assoc['company_id']]['company_alias'][] = $assoc['company_alias'];
  476. $list[$assoc['company_id']]['company_email'][] = $assoc['company_email'];
  477. $list[$assoc['company_id']]['company_tva_intra'][] = $assoc['company_tva_intra'];
  478. $list[$assoc['company_id']]['company_client'][] = $assoc['company_client'];
  479. $list[$assoc['company_id']]['company_fournisseur'][] = $assoc['company_fournisseur'];
  480. $list[$assoc['company_id']]['company_customer_code'][] = $assoc['company_customer_code'];
  481. $list[$assoc['company_id']]['company_supplier_code'][] = $assoc['company_supplier_code'];
  482. $list[$assoc['company_id']]['company_customer_accounting_code'][] = $assoc['company_customer_accounting_code'];
  483. $list[$assoc['company_id']]['company_supplier_accounting_code'][] = $assoc['company_supplier_accounting_code'];
  484. $list[$assoc['company_id']]['company_status'][] = $assoc['company_status'];
  485. $list[$assoc['company_id']]['drate'][] = $assoc['rate'];
  486. $list[$assoc['company_id']]['ddate_start'][] = $db->jdate($assoc['date_start']);
  487. $list[$assoc['company_id']]['ddate_end'][] = $db->jdate($assoc['date_end']);
  488. $list[$assoc['company_id']]['facid'][] = $assoc['facid'];
  489. $list[$assoc['company_id']]['facnum'][] = $assoc['facnum'];
  490. $list[$assoc['company_id']]['type'][] = $assoc['type'];
  491. $list[$assoc['company_id']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
  492. $list[$assoc['company_id']]['descr'][] = $assoc['descr'];
  493. $list[$assoc['company_id']]['totalht_list'][] = $assoc['total_ht'];
  494. $list[$assoc['company_id']]['vat_list'][] = $assoc['total_vat'];
  495. $list[$assoc['company_id']]['localtax1_list'][] = $assoc['total_localtax1'];
  496. $list[$assoc['company_id']]['localtax2_list'][] = $assoc['total_localtax2'];
  497. $list[$assoc['company_id']]['pid'][] = $assoc['pid'];
  498. $list[$assoc['company_id']]['pref'][] = $assoc['pref'];
  499. $list[$assoc['company_id']]['ptype'][] = $assoc['ptype'];
  500. $list[$assoc['company_id']]['payment_id'][] = $assoc['payment_id'];
  501. $list[$assoc['company_id']]['payment_ref'][] = $assoc['payment_ref'];
  502. $list[$assoc['company_id']]['payment_amount'][] = $assoc['payment_amount'];
  503. $company_id = $assoc['company_id'];
  504. }
  505. } else {
  506. dol_print_error($db);
  507. return -3;
  508. }
  509. }
  510. // CASE OF EXPENSE REPORT
  511. if ($direction == 'buy') { // buy only for expense reports
  512. // Define sql request
  513. $sql = '';
  514. // Count on payments date
  515. $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,";
  516. $sql .= " d.total_localtax1 as total_localtax1, d.total_localtax2 as total_localtax2, ";
  517. $sql .= " e.date_debut as date_start, e.date_fin as date_end, e.fk_user_author,";
  518. $sql .= " e.ref as facnum, e.total_ttc as ftotal_ttc, e.date_create, d.fk_c_type_fees as type,";
  519. $sql .= " p.fk_bank as payment_id, p.amount as payment_amount, p.rowid as pid, e.ref as pref";
  520. $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as e";
  521. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."expensereport_det as d ON d.fk_expensereport = e.rowid ";
  522. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."payment_expensereport as p ON p.fk_expensereport = e.rowid ";
  523. $sql .= " WHERE e.entity = ".$conf->entity;
  524. $sql .= " AND e.fk_statut in (6)";
  525. if ($y && $m) {
  526. $sql .= " AND p.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
  527. $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
  528. } elseif ($y) {
  529. $sql .= " AND p.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
  530. $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
  531. }
  532. if ($q) {
  533. $sql .= " AND p.datep > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
  534. $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
  535. }
  536. if ($date_start && $date_end) {
  537. $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  538. }
  539. $sql .= " AND (d.product_type = -1";
  540. $sql .= " OR e.date_debut is NOT null OR e.date_fin IS NOT NULL)"; // enhance detection of service
  541. if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) {
  542. $sql .= " AND (d.".$f_rate." <> 0 OR d.total_tva <> 0)";
  543. }
  544. $sql .= " ORDER BY e.rowid";
  545. if (!$sql) {
  546. dol_syslog("Tax.lib.php::tax_by_rate no accountancy module enabled".$sql, LOG_ERR);
  547. return -1; // -1 = Not accountancy module enabled
  548. }
  549. if ($sql == 'TODO') {
  550. return -2; // -2 = Feature not yet available
  551. }
  552. if ($sql != 'TODO') {
  553. dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
  554. $resql = $db->query($sql);
  555. if ($resql) {
  556. $company_id = -1;
  557. $oldrowid = '';
  558. while ($assoc = $db->fetch_array($resql)) {
  559. if (!isset($list[$assoc['company_id']]['totalht'])) {
  560. $list[$assoc['company_id']]['totalht'] = 0;
  561. }
  562. if (!isset($list[$assoc['company_id']]['vat'])) {
  563. $list[$assoc['company_id']]['vat'] = 0;
  564. }
  565. if (!isset($list[$assoc['company_id']]['localtax1'])) {
  566. $list[$assoc['company_id']]['localtax1'] = 0;
  567. }
  568. if (!isset($list[$assoc['company_id']]['localtax2'])) {
  569. $list[$assoc['company_id']]['localtax2'] = 0;
  570. }
  571. if ($assoc['rowid'] != $oldrowid) { // Si rupture sur d.rowid
  572. $oldrowid = $assoc['rowid'];
  573. $list[$assoc['company_id']]['totalht'] += $assoc['total_ht'];
  574. $list[$assoc['company_id']]['vat'] += $assoc['total_vat'];
  575. $list[$assoc['company_id']]['localtax1'] += $assoc['total_localtax1'];
  576. $list[$assoc['company_id']]['localtax2'] += $assoc['total_localtax2'];
  577. }
  578. $list[$assoc['company_id']]['dtotal_ttc'][] = $assoc['total_ttc'];
  579. $list[$assoc['company_id']]['dtype'][] = 'ExpenseReportPayment';
  580. $list[$assoc['company_id']]['datef'][] = $assoc['datef'];
  581. $list[$assoc['company_id']]['company_name'][] = '';
  582. $list[$assoc['company_id']]['company_id'][] = '';
  583. $list[$assoc['company_id']]['company_alias'][] = '';
  584. $list[$assoc['company_id']]['company_email'][] = '';
  585. $list[$assoc['company_id']]['company_tva_intra'][] = '';
  586. $list[$assoc['company_id']]['company_client'][] = '';
  587. $list[$assoc['company_id']]['company_fournisseur'][] = '';
  588. $list[$assoc['company_id']]['company_customer_code'][] = '';
  589. $list[$assoc['company_id']]['company_supplier_code'][] = '';
  590. $list[$assoc['company_id']]['company_customer_accounting_code'][] = '';
  591. $list[$assoc['company_id']]['company_supplier_accounting_code'][] = '';
  592. $list[$assoc['company_id']]['company_status'][] = '';
  593. $list[$assoc['company_id']]['user_id'][] = $assoc['fk_user_author'];
  594. $list[$assoc['company_id']]['drate'][] = $assoc['rate'];
  595. $list[$assoc['company_id']]['ddate_start'][] = $db->jdate($assoc['date_start']);
  596. $list[$assoc['company_id']]['ddate_end'][] = $db->jdate($assoc['date_end']);
  597. $list[$assoc['company_id']]['facid'][] = $assoc['facid'];
  598. $list[$assoc['company_id']]['facnum'][] = $assoc['facnum'];
  599. $list[$assoc['company_id']]['type'][] = $assoc['type'];
  600. $list[$assoc['company_id']]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
  601. $list[$assoc['company_id']]['descr'][] = $assoc['descr'];
  602. $list[$assoc['company_id']]['totalht_list'][] = $assoc['total_ht'];
  603. $list[$assoc['company_id']]['vat_list'][] = $assoc['total_vat'];
  604. $list[$assoc['company_id']]['localtax1_list'][] = $assoc['total_localtax1'];
  605. $list[$assoc['company_id']]['localtax2_list'][] = $assoc['total_localtax2'];
  606. $list[$assoc['company_id']]['pid'][] = $assoc['pid'];
  607. $list[$assoc['company_id']]['pref'][] = $assoc['pref'];
  608. $list[$assoc['company_id']]['ptype'][] = 'ExpenseReportPayment';
  609. $list[$assoc['company_id']]['payment_id'][] = $assoc['payment_id'];
  610. $list[$assoc['company_id']]['payment_ref'][] = $assoc['payment_ref'];
  611. $list[$assoc['company_id']]['payment_amount'][] = $assoc['payment_amount'];
  612. $company_id = $assoc['company_id'];
  613. }
  614. } else {
  615. dol_print_error($db);
  616. return -3;
  617. }
  618. }
  619. }
  620. return $list;
  621. }
  622. /**
  623. * Gets Tax to collect for the given year (and given quarter or month)
  624. * The function gets the Tax in split results, as the Tax declaration asks
  625. * to report the amounts for different Tax rates as different lines.
  626. *
  627. * @param string $type Tax type, either 'vat', 'localtax1' or 'localtax2'
  628. * @param DoliDB $db Database handler object
  629. * @param int $y Year
  630. * @param int $q Quarter
  631. * @param string $date_start Start date
  632. * @param string $date_end End date
  633. * @param int $modetax Not used
  634. * @param int $direction 'sell' (customer invoice) or 'buy' (supplier invoices)
  635. * @param int $m Month
  636. * @return array|int Array with details of VATs (per rate), -1 if no accountancy module, -2 if not yet developped, -3 if error
  637. */
  638. function tax_by_rate($type, $db, $y, $q, $date_start, $date_end, $modetax, $direction, $m = 0)
  639. {
  640. global $conf;
  641. // If we use date_start and date_end, we must not use $y, $m, $q
  642. if (($date_start || $date_end) && (!empty($y) || !empty($m) || !empty($q))) {
  643. dol_print_error('', 'Bad value of input parameter for tax_by_rate');
  644. }
  645. $list = array();
  646. if ($direction == 'sell') {
  647. $invoicetable = 'facture';
  648. $invoicedettable = 'facturedet';
  649. $fk_facture = 'fk_facture';
  650. $fk_facture2 = 'fk_facture';
  651. $fk_payment = 'fk_paiement';
  652. $total_tva = 'total_tva';
  653. $paymenttable = 'paiement';
  654. $paymentfacturetable = 'paiement_facture';
  655. $invoicefieldref = 'ref';
  656. } else {
  657. $invoicetable = 'facture_fourn';
  658. $invoicedettable = 'facture_fourn_det';
  659. $fk_facture = 'fk_facture_fourn';
  660. $fk_facture2 = 'fk_facturefourn';
  661. $fk_payment = 'fk_paiementfourn';
  662. $total_tva = 'tva';
  663. $paymenttable = 'paiementfourn';
  664. $paymentfacturetable = 'paiementfourn_facturefourn';
  665. $invoicefieldref = 'ref';
  666. }
  667. if (strpos($type, 'localtax') === 0) {
  668. $f_rate = $type.'_tx';
  669. } else {
  670. $f_rate = 'tva_tx';
  671. }
  672. $total_localtax1 = 'total_localtax1';
  673. $total_localtax2 = 'total_localtax2';
  674. // CASE OF PRODUCTS/GOODS
  675. // Define sql request
  676. $sql = '';
  677. if (($direction == 'sell' && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
  678. || ($direction == 'buy' && $conf->global->TAX_MODE_BUY_PRODUCT == 'invoice')) {
  679. // Count on delivery date (use invoice date as delivery is unknown)
  680. $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.vat_src_code as vat_src_code, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
  681. $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  682. $sql .= " d.date_start as date_start, d.date_end as date_end,";
  683. $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
  684. $sql .= " s.nom as company_name, s.name_alias as company_alias, s.rowid as company_id, s.client as company_client, s.fournisseur as company_fournisseur, s.email as company_email,";
  685. $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
  686. $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
  687. $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
  688. $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
  689. $sql .= " 0 as payment_id, '' as payment_ref, 0 as payment_amount";
  690. $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f";
  691. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
  692. $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$invoicedettable." as d ON d.".$fk_facture."=f.rowid";
  693. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  694. $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
  695. $sql .= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
  696. if ($direction == 'buy') {
  697. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  698. $sql .= " AND f.type IN (0,1,2,5)";
  699. } else {
  700. $sql .= " AND f.type IN (0,1,2,3,5)";
  701. }
  702. } else {
  703. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  704. $sql .= " AND f.type IN (0,1,2,5)";
  705. } else {
  706. $sql .= " AND f.type IN (0,1,2,3,5)";
  707. }
  708. }
  709. if ($y && $m) {
  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. } elseif ($y) {
  713. $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
  714. $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
  715. }
  716. if ($q) {
  717. $sql .= " AND f.datef > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
  718. $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
  719. }
  720. if ($date_start && $date_end) {
  721. $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  722. }
  723. $sql .= " AND (d.product_type = 0"; // Limit to products
  724. $sql .= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of products
  725. if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) {
  726. $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
  727. }
  728. $sql .= " ORDER BY d.rowid, d.".$fk_facture;
  729. } else {
  730. // Count on payments date
  731. $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.vat_src_code as vat_src_code, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
  732. $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  733. $sql .= " d.date_start as date_start, d.date_end as date_end,";
  734. $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
  735. $sql .= " s.nom as company_name, s.name_alias as company_alias, s.rowid as company_id, s.client as company_client, s.fournisseur as company_fournisseur, s.email as company_email,";
  736. $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
  737. $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
  738. $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
  739. $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
  740. $sql .= " pf.".$fk_payment." as payment_id, pf.amount as payment_amount,";
  741. $sql .= " pa.datep as datep, pa.ref as payment_ref";
  742. $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f";
  743. $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$paymentfacturetable." as pf ON pf.".$fk_facture2." = f.rowid";
  744. $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$paymenttable." as pa ON pa.rowid = pf.".$fk_payment;
  745. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
  746. $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$invoicedettable." as d ON d.".$fk_facture." = f.rowid";
  747. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  748. $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
  749. $sql .= " AND f.fk_statut in (1,2)"; // Paid (partially or completely)
  750. if ($direction == 'buy') {
  751. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  752. $sql .= " AND f.type IN (0,1,2,5)";
  753. } else {
  754. $sql .= " AND f.type IN (0,1,2,3,5)";
  755. }
  756. } else {
  757. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  758. $sql .= " AND f.type IN (0,1,2,5)";
  759. } else {
  760. $sql .= " AND f.type IN (0,1,2,3,5)";
  761. }
  762. }
  763. if ($y && $m) {
  764. $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
  765. $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
  766. } elseif ($y) {
  767. $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
  768. $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
  769. }
  770. if ($q) {
  771. $sql .= " AND pa.datep > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
  772. $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
  773. }
  774. if ($date_start && $date_end) {
  775. $sql .= " AND pa.datep >= '".$db->idate($date_start)."' AND pa.datep <= '".$db->idate($date_end)."'";
  776. }
  777. $sql .= " AND (d.product_type = 0"; // Limit to products
  778. $sql .= " AND d.date_start is null AND d.date_end IS NULL)"; // enhance detection of products
  779. if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) {
  780. $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
  781. }
  782. $sql .= " ORDER BY d.rowid, d.".$fk_facture.", pf.rowid";
  783. }
  784. if (!$sql) {
  785. return -1;
  786. }
  787. if ($sql == 'TODO') {
  788. return -2;
  789. }
  790. if ($sql != 'TODO') {
  791. dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
  792. $resql = $db->query($sql);
  793. if ($resql) {
  794. $rate = -1;
  795. $oldrowid = '';
  796. while ($assoc = $db->fetch_array($resql)) {
  797. $rate_key = $assoc['rate'];
  798. if ($f_rate == 'tva_tx' && !empty($assoc['vat_src_code']) && !preg_match('/\(/', $rate_key)) {
  799. $rate_key .= ' (' . $assoc['vat_src_code'] . ')';
  800. }
  801. // Code to avoid warnings when array entry not defined
  802. if (!isset($list[$rate_key]['totalht'])) {
  803. $list[$rate_key]['totalht'] = 0;
  804. }
  805. if (!isset($list[$rate_key]['vat'])) {
  806. $list[$rate_key]['vat'] = 0;
  807. }
  808. if (!isset($list[$rate_key]['localtax1'])) {
  809. $list[$rate_key]['localtax1'] = 0;
  810. }
  811. if (!isset($list[$rate_key]['localtax2'])) {
  812. $list[$rate_key]['localtax2'] = 0;
  813. }
  814. if ($assoc['rowid'] != $oldrowid) { // Si rupture sur d.rowid
  815. $oldrowid = $assoc['rowid'];
  816. $list[$rate_key]['totalht'] += $assoc['total_ht'];
  817. $list[$rate_key]['vat'] += $assoc['total_vat'];
  818. $list[$rate_key]['localtax1'] += $assoc['total_localtax1'];
  819. $list[$rate_key]['localtax2'] += $assoc['total_localtax2'];
  820. }
  821. $list[$rate_key]['dtotal_ttc'][] = $assoc['total_ttc'];
  822. $list[$rate_key]['dtype'][] = $assoc['dtype'];
  823. $list[$rate_key]['datef'][] = $db->jdate($assoc['datef']);
  824. $list[$rate_key]['datep'][] = $db->jdate($assoc['datep']);
  825. $list[$rate_key]['company_name'][] = $assoc['company_name'];
  826. $list[$rate_key]['company_id'][] = $assoc['company_id'];
  827. $list[$rate_key]['company_alias'][] = $assoc['company_alias'];
  828. $list[$rate_key]['company_email'][] = $assoc['company_email'];
  829. $list[$rate_key]['company_tva_intra'][] = $assoc['company_tva_intra'];
  830. $list[$rate_key]['company_client'][] = $assoc['company_client'];
  831. $list[$rate_key]['company_fournisseur'][] = $assoc['company_fournisseur'];
  832. $list[$rate_key]['company_customer_code'][] = $assoc['company_customer_code'];
  833. $list[$rate_key]['company_supplier_code'][] = $assoc['company_supplier_code'];
  834. $list[$rate_key]['company_customer_accounting_code'][] = $assoc['company_customer_accounting_code'];
  835. $list[$rate_key]['company_supplier_accounting_code'][] = $assoc['company_supplier_accounting_code'];
  836. $list[$rate_key]['company_status'][] = $assoc['company_status'];
  837. $list[$rate_key]['ddate_start'][] = $db->jdate($assoc['date_start']);
  838. $list[$rate_key]['ddate_end'][] = $db->jdate($assoc['date_end']);
  839. $list[$rate_key]['facid'][] = $assoc['facid'];
  840. $list[$rate_key]['facnum'][] = $assoc['facnum'];
  841. $list[$rate_key]['type'][] = $assoc['type'];
  842. $list[$rate_key]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
  843. $list[$rate_key]['descr'][] = $assoc['descr'];
  844. $list[$rate_key]['totalht_list'][] = $assoc['total_ht'];
  845. $list[$rate_key]['vat_list'][] = $assoc['total_vat'];
  846. $list[$rate_key]['localtax1_list'][] = $assoc['total_localtax1'];
  847. $list[$rate_key]['localtax2_list'][] = $assoc['total_localtax2'];
  848. $list[$rate_key]['pid'][] = $assoc['pid'];
  849. $list[$rate_key]['pref'][] = $assoc['pref'];
  850. $list[$rate_key]['ptype'][] = $assoc['ptype'];
  851. $list[$rate_key]['payment_id'][] = $assoc['payment_id'];
  852. $list[$rate_key]['payment_ref'][] = $assoc['payment_ref'];
  853. $list[$rate_key]['payment_amount'][] = $assoc['payment_amount'];
  854. $rate = $assoc['rate'];
  855. }
  856. } else {
  857. dol_print_error($db);
  858. return -3;
  859. }
  860. }
  861. // CASE OF SERVICES
  862. // Define sql request
  863. $sql = '';
  864. if (($direction == 'sell' && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice')
  865. || ($direction == 'buy' && $conf->global->TAX_MODE_BUY_SERVICE == 'invoice')) {
  866. // Count on invoice date
  867. $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.vat_src_code as vat_src_code, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
  868. $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  869. $sql .= " d.date_start as date_start, d.date_end as date_end,";
  870. $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
  871. $sql .= " s.nom as company_name, s.name_alias as company_alias, s.rowid as company_id, s.client as company_client, s.fournisseur as company_fournisseur, s.email as company_email,";
  872. $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
  873. $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
  874. $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
  875. $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
  876. $sql .= " 0 as payment_id, '' as payment_ref, 0 as payment_amount";
  877. $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f";
  878. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
  879. $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$invoicedettable." as d ON d.".$fk_facture." = f.rowid";
  880. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  881. $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
  882. $sql .= " AND f.fk_statut in (1,2)"; // Validated or paid (partially or completely)
  883. if ($direction == 'buy') {
  884. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  885. $sql .= " AND f.type IN (0,1,2,5)";
  886. } else {
  887. $sql .= " AND f.type IN (0,1,2,3,5)";
  888. }
  889. } else {
  890. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  891. $sql .= " AND f.type IN (0,1,2,5)";
  892. } else {
  893. $sql .= " AND f.type IN (0,1,2,3,5)";
  894. }
  895. }
  896. if ($y && $m) {
  897. $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
  898. $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
  899. } elseif ($y) {
  900. $sql .= " AND f.datef >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
  901. $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
  902. }
  903. if ($q) {
  904. $sql .= " AND f.datef > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
  905. $sql .= " AND f.datef <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
  906. }
  907. if ($date_start && $date_end) {
  908. $sql .= " AND f.datef >= '".$db->idate($date_start)."' AND f.datef <= '".$db->idate($date_end)."'";
  909. }
  910. $sql .= " AND (d.product_type = 1"; // Limit to services
  911. $sql .= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
  912. if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) {
  913. $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
  914. }
  915. $sql .= " ORDER BY d.rowid, d.".$fk_facture;
  916. } else {
  917. // Count on payments date
  918. $sql = "SELECT d.rowid, d.product_type as dtype, d.".$fk_facture." as facid, d.$f_rate as rate, d.vat_src_code as vat_src_code, d.total_ht as total_ht, d.total_ttc as total_ttc, d.".$total_tva." as total_vat, d.description as descr,";
  919. $sql .= " d.".$total_localtax1." as total_localtax1, d.".$total_localtax2." as total_localtax2, ";
  920. $sql .= " d.date_start as date_start, d.date_end as date_end,";
  921. $sql .= " f.".$invoicefieldref." as facnum, f.type, f.total_ttc as ftotal_ttc, f.datef,";
  922. $sql .= " s.nom as company_name, s.name_alias as company_alias, s.rowid as company_id, s.client as company_client, s.fournisseur as company_fournisseur, s.email as company_email,";
  923. $sql .= " s.code_client as company_customer_code, s.code_fournisseur as company_supplier_code,";
  924. $sql .= " s.code_compta as company_customer_accounting_code, s.code_compta_fournisseur as company_supplier_accounting_code,";
  925. $sql .= " s.status as company_status, s.tva_intra as company_tva_intra,";
  926. $sql .= " p.rowid as pid, p.ref as pref, p.fk_product_type as ptype,";
  927. $sql .= " pf.".$fk_payment." as payment_id, pf.amount as payment_amount,";
  928. $sql .= " pa.datep as datep, pa.ref as payment_ref";
  929. $sql .= " FROM ".MAIN_DB_PREFIX.$invoicetable." as f";
  930. $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$paymentfacturetable." as pf ON pf.".$fk_facture2." = f.rowid";
  931. $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$paymenttable." as pa ON pa.rowid = pf.".$fk_payment;
  932. $sql .= " INNER JOIN ".MAIN_DB_PREFIX."societe as s ON s.rowid = f.fk_soc";
  933. $sql .= " INNER JOIN ".MAIN_DB_PREFIX.$invoicedettable." as d ON d.".$fk_facture." = f.rowid";
  934. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."product as p on d.fk_product = p.rowid";
  935. $sql .= " WHERE f.entity IN (".getEntity($invoicetable).")";
  936. $sql .= " AND f.fk_statut in (1,2)"; // Paid (partially or completely)
  937. if ($direction == 'buy') {
  938. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  939. $sql .= " AND f.type IN (0,1,2,5)";
  940. } else {
  941. $sql .= " AND f.type IN (0,1,2,3,5)";
  942. }
  943. } else {
  944. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  945. $sql .= " AND f.type IN (0,1,2,5)";
  946. } else {
  947. $sql .= " AND f.type IN (0,1,2,3,5)";
  948. }
  949. }
  950. if ($y && $m) {
  951. $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
  952. $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
  953. } elseif ($y) {
  954. $sql .= " AND pa.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
  955. $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
  956. }
  957. if ($q) {
  958. $sql .= " AND pa.datep > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
  959. $sql .= " AND pa.datep <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
  960. }
  961. if ($date_start && $date_end) {
  962. $sql .= " AND pa.datep >= '".$db->idate($date_start)."' AND pa.datep <= '".$db->idate($date_end)."'";
  963. }
  964. $sql .= " AND (d.product_type = 1"; // Limit to services
  965. $sql .= " OR d.date_start is NOT null OR d.date_end IS NOT NULL)"; // enhance detection of service
  966. if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) {
  967. $sql .= " AND (d.".$f_rate." <> 0 OR d.".$total_tva." <> 0)";
  968. }
  969. $sql .= " ORDER BY d.rowid, d.".$fk_facture.", pf.rowid";
  970. }
  971. if (!$sql) {
  972. dol_syslog("Tax.lib.php::tax_by_rate no accountancy module enabled".$sql, LOG_ERR);
  973. return -1; // -1 = Not accountancy module enabled
  974. }
  975. if ($sql == 'TODO') {
  976. return -2; // -2 = Feature not yet available
  977. }
  978. if ($sql != 'TODO') {
  979. dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
  980. $resql = $db->query($sql);
  981. if ($resql) {
  982. $rate = -1;
  983. $oldrowid = '';
  984. while ($assoc = $db->fetch_array($resql)) {
  985. $rate_key = $assoc['rate'];
  986. if ($f_rate == 'tva_tx' && !empty($assoc['vat_src_code']) && !preg_match('/\(/', $rate_key)) {
  987. $rate_key .= ' (' . $assoc['vat_src_code'] . ')';
  988. }
  989. // Code to avoid warnings when array entry not defined
  990. if (!isset($list[$rate_key]['totalht'])) {
  991. $list[$rate_key]['totalht'] = 0;
  992. }
  993. if (!isset($list[$rate_key]['vat'])) {
  994. $list[$rate_key]['vat'] = 0;
  995. }
  996. if (!isset($list[$rate_key]['localtax1'])) {
  997. $list[$rate_key]['localtax1'] = 0;
  998. }
  999. if (!isset($list[$rate_key]['localtax2'])) {
  1000. $list[$rate_key]['localtax2'] = 0;
  1001. }
  1002. if ($assoc['rowid'] != $oldrowid) { // Si rupture sur d.rowid
  1003. $oldrowid = $assoc['rowid'];
  1004. $list[$rate_key]['totalht'] += $assoc['total_ht'];
  1005. $list[$rate_key]['vat'] += $assoc['total_vat'];
  1006. $list[$rate_key]['localtax1'] += $assoc['total_localtax1'];
  1007. $list[$rate_key]['localtax2'] += $assoc['total_localtax2'];
  1008. }
  1009. $list[$rate_key]['dtotal_ttc'][] = $assoc['total_ttc'];
  1010. $list[$rate_key]['dtype'][] = $assoc['dtype'];
  1011. $list[$rate_key]['datef'][] = $db->jdate($assoc['datef']);
  1012. $list[$rate_key]['datep'][] = $db->jdate($assoc['datep']);
  1013. $list[$rate_key]['ddate_start'][] = $db->jdate($assoc['date_start']);
  1014. $list[$rate_key]['ddate_end'][] = $db->jdate($assoc['date_end']);
  1015. $list[$rate_key]['company_name'][] = $assoc['company_name'];
  1016. $list[$rate_key]['company_id'][] = $assoc['company_id'];
  1017. $list[$rate_key]['company_alias'][] = $assoc['company_alias'];
  1018. $list[$rate_key]['company_email'][] = $assoc['company_email'];
  1019. $list[$rate_key]['company_tva_intra'][] = $assoc['company_tva_intra'];
  1020. $list[$rate_key]['company_client'][] = $assoc['company_client'];
  1021. $list[$rate_key]['company_fournisseur'][] = $assoc['company_fournisseur'];
  1022. $list[$rate_key]['company_customer_code'][] = $assoc['company_customer_code'];
  1023. $list[$rate_key]['company_supplier_code'][] = $assoc['company_supplier_code'];
  1024. $list[$rate_key]['company_customer_accounting_code'][] = $assoc['company_customer_accounting_code'];
  1025. $list[$rate_key]['company_supplier_accounting_code'][] = $assoc['company_supplier_accounting_code'];
  1026. $list[$rate_key]['company_status'][] = $assoc['company_status'];
  1027. $list[$rate_key]['facid'][] = $assoc['facid'];
  1028. $list[$rate_key]['facnum'][] = $assoc['facnum'];
  1029. $list[$rate_key]['type'][] = $assoc['type'];
  1030. $list[$rate_key]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
  1031. $list[$rate_key]['descr'][] = $assoc['descr'];
  1032. $list[$rate_key]['totalht_list'][] = $assoc['total_ht'];
  1033. $list[$rate_key]['vat_list'][] = $assoc['total_vat'];
  1034. $list[$rate_key]['localtax1_list'][] = $assoc['total_localtax1'];
  1035. $list[$rate_key]['localtax2_list'][] = $assoc['total_localtax2'];
  1036. $list[$rate_key]['pid'][] = $assoc['pid'];
  1037. $list[$rate_key]['pref'][] = $assoc['pref'];
  1038. $list[$rate_key]['ptype'][] = $assoc['ptype'];
  1039. $list[$rate_key]['payment_id'][] = $assoc['payment_id'];
  1040. $list[$rate_key]['payment_ref'][] = $assoc['payment_ref'];
  1041. $list[$rate_key]['payment_amount'][] = $assoc['payment_amount'];
  1042. $rate = $assoc['rate'];
  1043. }
  1044. } else {
  1045. dol_print_error($db);
  1046. return -3;
  1047. }
  1048. }
  1049. // CASE OF EXPENSE REPORT
  1050. if ($direction == 'buy') { // buy only for expense reports
  1051. // Define sql request
  1052. $sql = '';
  1053. // Count on payments date
  1054. $sql = "SELECT d.rowid, d.product_type as dtype, e.rowid as facid, d.$f_rate as rate, d.vat_src_code as vat_src_code, d.total_ht as total_ht, d.total_ttc as total_ttc, d.total_tva as total_vat, e.note_private as descr,";
  1055. $sql .= " d.total_localtax1 as total_localtax1, d.total_localtax2 as total_localtax2, ";
  1056. $sql .= " e.date_debut as date_start, e.date_fin as date_end, e.fk_user_author,";
  1057. $sql .= " e.ref as facnum, e.total_ttc as ftotal_ttc, e.date_create, d.fk_c_type_fees as type,";
  1058. $sql .= " p.fk_bank as payment_id, p.amount as payment_amount, p.rowid as pid, e.ref as pref";
  1059. $sql .= " FROM ".MAIN_DB_PREFIX."expensereport as e";
  1060. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."expensereport_det as d ON d.fk_expensereport = e.rowid";
  1061. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."payment_expensereport as p ON p.fk_expensereport = e.rowid";
  1062. $sql .= " WHERE e.entity = ".$conf->entity;
  1063. $sql .= " AND e.fk_statut in (6)";
  1064. if ($y && $m) {
  1065. $sql .= " AND p.datep >= '".$db->idate(dol_get_first_day($y, $m, false))."'";
  1066. $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, $m, false))."'";
  1067. } elseif ($y) {
  1068. $sql .= " AND p.datep >= '".$db->idate(dol_get_first_day($y, 1, false))."'";
  1069. $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, 12, false))."'";
  1070. }
  1071. if ($q) {
  1072. $sql .= " AND p.datep > '".$db->idate(dol_get_first_day($y, (($q - 1) * 3) + 1, false))."'";
  1073. $sql .= " AND p.datep <= '".$db->idate(dol_get_last_day($y, ($q * 3), false))."'";
  1074. }
  1075. if ($date_start && $date_end) {
  1076. $sql .= " AND p.datep >= '".$db->idate($date_start)."' AND p.datep <= '".$db->idate($date_end)."'";
  1077. }
  1078. $sql .= " AND (d.product_type = -1";
  1079. $sql .= " OR e.date_debut is NOT null OR e.date_fin IS NOT NULL)"; // enhance detection of service
  1080. if (!empty($conf->global->MAIN_NOT_INCLUDE_ZERO_VAT_IN_REPORTS)) {
  1081. $sql .= " AND (d.".$f_rate." <> 0 OR d.total_tva <> 0)";
  1082. }
  1083. $sql .= " ORDER BY e.rowid";
  1084. if (!$sql) {
  1085. dol_syslog("Tax.lib.php::tax_by_rate no accountancy module enabled".$sql, LOG_ERR);
  1086. return -1; // -1 = Not accountancy module enabled
  1087. }
  1088. if ($sql == 'TODO') {
  1089. return -2; // -2 = Feature not yet available
  1090. }
  1091. if ($sql != 'TODO') {
  1092. dol_syslog("Tax.lib.php::tax_by_rate", LOG_DEBUG);
  1093. $resql = $db->query($sql);
  1094. if ($resql) {
  1095. $rate = -1;
  1096. $oldrowid = '';
  1097. while ($assoc = $db->fetch_array($resql)) {
  1098. $rate_key = $assoc['rate'];
  1099. if ($f_rate == 'tva_tx' && !empty($assoc['vat_src_code']) && !preg_match('/\(/', $rate_key)) {
  1100. $rate_key .= ' (' . $assoc['vat_src_code'] . ')';
  1101. }
  1102. // Code to avoid warnings when array entry not defined
  1103. if (!isset($list[$rate_key]['totalht'])) {
  1104. $list[$rate_key]['totalht'] = 0;
  1105. }
  1106. if (!isset($list[$rate_key]['vat'])) {
  1107. $list[$rate_key]['vat'] = 0;
  1108. }
  1109. if (!isset($list[$rate_key]['localtax1'])) {
  1110. $list[$rate_key]['localtax1'] = 0;
  1111. }
  1112. if (!isset($list[$rate_key]['localtax2'])) {
  1113. $list[$rate_key]['localtax2'] = 0;
  1114. }
  1115. if ($assoc['rowid'] != $oldrowid) { // Si rupture sur d.rowid
  1116. $oldrowid = $assoc['rowid'];
  1117. $list[$rate_key]['totalht'] += $assoc['total_ht'];
  1118. $list[$rate_key]['vat'] += $assoc['total_vat'];
  1119. $list[$rate_key]['localtax1'] += $assoc['total_localtax1'];
  1120. $list[$rate_key]['localtax2'] += $assoc['total_localtax2'];
  1121. }
  1122. $list[$rate_key]['dtotal_ttc'][] = $assoc['total_ttc'];
  1123. $list[$rate_key]['dtype'][] = 'ExpenseReportPayment';
  1124. $list[$rate_key]['datef'][] = $assoc['datef'];
  1125. $list[$rate_key]['company_name'][] = '';
  1126. $list[$rate_key]['company_id'][] = '';
  1127. $list[$rate_key]['user_id'][] = $assoc['fk_user_author'];
  1128. $list[$rate_key]['ddate_start'][] = $db->jdate($assoc['date_start']);
  1129. $list[$rate_key]['ddate_end'][] = $db->jdate($assoc['date_end']);
  1130. $list[$rate_key]['facid'][] = $assoc['facid'];
  1131. $list[$rate_key]['facnum'][] = $assoc['facnum'];
  1132. $list[$rate_key]['type'][] = $assoc['type'];
  1133. $list[$rate_key]['ftotal_ttc'][] = $assoc['ftotal_ttc'];
  1134. $list[$rate_key]['descr'][] = $assoc['descr'];
  1135. $list[$rate_key]['totalht_list'][] = $assoc['total_ht'];
  1136. $list[$rate_key]['vat_list'][] = $assoc['total_vat'];
  1137. $list[$rate_key]['localtax1_list'][] = $assoc['total_localtax1'];
  1138. $list[$rate_key]['localtax2_list'][] = $assoc['total_localtax2'];
  1139. $list[$rate_key]['pid'][] = $assoc['pid'];
  1140. $list[$rate_key]['pref'][] = $assoc['pref'];
  1141. $list[$rate_key]['ptype'][] = 'ExpenseReportPayment';
  1142. $list[$rate_key]['payment_id'][] = $assoc['payment_id'];
  1143. $list[$rate_key]['payment_ref'][] = $assoc['payment_ref'];
  1144. $list[$rate_key]['payment_amount'][] = $assoc['payment_amount'];
  1145. $rate = $assoc['rate'];
  1146. }
  1147. } else {
  1148. dol_print_error($db);
  1149. return -3;
  1150. }
  1151. }
  1152. }
  1153. return $list;
  1154. }