index.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. <?php
  2. /* Copyright (C) 2011-2014 Juanjo Menent <jmenent@2byte.es>
  3. * Copyright (C) 2014 Ferran Marcet <fmarcet@2byte.es>
  4. * Copyright (C) 2018 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  19. */
  20. /**
  21. * \file htdocs/compta/localtax/index.php
  22. * \ingroup tax
  23. * \brief Index page of IRPF reports
  24. */
  25. require '../../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/tax.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/compta/localtax/class/localtax.class.php';
  31. // Load translation files required by the page
  32. $langs->loadLangs(array("other", "compta", "banks", "bills", "companies", "product", "trips", "admin"));
  33. $localTaxType = GETPOST('localTaxType', 'int');
  34. // Date range
  35. $year = GETPOST("year", "int");
  36. if (empty($year)) {
  37. $year_current = strftime("%Y", dol_now());
  38. $year_start = $year_current;
  39. } else {
  40. $year_current = $year;
  41. $year_start = $year;
  42. }
  43. $date_start = dol_mktime(0, 0, 0, GETPOST("date_startmonth"), GETPOST("date_startday"), GETPOST("date_startyear"));
  44. $date_end = dol_mktime(23, 59, 59, GETPOST("date_endmonth"), GETPOST("date_endday"), GETPOST("date_endyear"));
  45. if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
  46. $q = GETPOST("q", "int");
  47. if (empty($q)) {
  48. if (GETPOST("month", "int")) {
  49. $date_start = dol_get_first_day($year_start, GETPOST("month", "int"), false);
  50. $date_end = dol_get_last_day($year_start, GETPOST("month", "int"), false);
  51. } else {
  52. $date_start = dol_get_first_day($year_start, $conf->global->SOCIETE_FISCAL_MONTH_START, false);
  53. $date_end = dol_time_plus_duree($date_start, 1, 'y') - 1;
  54. }
  55. } else {
  56. if ($q == 1) {
  57. $date_start = dol_get_first_day($year_start, 1, false);
  58. $date_end = dol_get_last_day($year_start, 3, false);
  59. }
  60. if ($q == 2) {
  61. $date_start = dol_get_first_day($year_start, 4, false);
  62. $date_end = dol_get_last_day($year_start, 6, false);
  63. }
  64. if ($q == 3) {
  65. $date_start = dol_get_first_day($year_start, 7, false);
  66. $date_end = dol_get_last_day($year_start, 9, false);
  67. }
  68. if ($q == 4) {
  69. $date_start = dol_get_first_day($year_start, 10, false);
  70. $date_end = dol_get_last_day($year_start, 12, false);
  71. }
  72. }
  73. }
  74. // Define modetax (0 or 1)
  75. // 0=normal, 1=option vat for services is on debit, 2=option on payments for products
  76. $modetax = $conf->global->TAX_MODE;
  77. if (GETPOSTISSET("modetax")) {
  78. $modetax = GETPOST("modetax", 'int');
  79. }
  80. if (empty($modetax)) {
  81. $modetax = 0;
  82. }
  83. // Security check
  84. $socid = GETPOST('socid', 'int');
  85. if ($user->socid) {
  86. $socid = $user->socid;
  87. }
  88. $result = restrictedArea($user, 'tax', '', '', 'charges');
  89. /**
  90. * print function
  91. *
  92. * @param DoliDB $db Database handler
  93. * @param string $sql SQL Request
  94. * @param string $date Date
  95. * @return void
  96. */
  97. function pt($db, $sql, $date)
  98. {
  99. global $conf, $bc, $langs;
  100. $result = $db->query($sql);
  101. if ($result) {
  102. $num = $db->num_rows($result);
  103. $i = 0;
  104. $total = 0;
  105. print '<table class="noborder centpercent">';
  106. print '<tr class="liste_titre">';
  107. print '<td class="nowrap">'.$date.'</td>';
  108. print '<td class="right">'.$langs->trans("ClaimedForThisPeriod").'</td>';
  109. print '<td class="right">'.$langs->trans("PaidDuringThisPeriod").'</td>';
  110. print "</tr>\n";
  111. $totalclaimed = 0;
  112. $totalpaid = 0;
  113. $amountclaimed = 0;
  114. $amountpaid = 0;
  115. $previousmonth = '';
  116. $previousmode = '';
  117. $mode = '';
  118. while ($i < $num) {
  119. $obj = $db->fetch_object($result);
  120. $mode = $obj->mode;
  121. //print $obj->dm.' '.$obj->mode.' '.$previousmonth.' '.$previousmode;
  122. if ($obj->mode == 'claimed' && !empty($previousmode)) {
  123. print '<tr class="oddeven">';
  124. print '<td class="nowrap">'.$previousmonth."</td>\n";
  125. print '<td class="nowrap right">'.price($amountclaimed)."</td>\n";
  126. print '<td class="nowrap right">'.price($amountpaid)."</td>\n";
  127. print "</tr>\n";
  128. $amountclaimed = 0;
  129. $amountpaid = 0;
  130. }
  131. if ($obj->mode == 'claimed') {
  132. $amountclaimed = $obj->mm;
  133. $totalclaimed = $totalclaimed + $amountclaimed;
  134. }
  135. if ($obj->mode == 'paid') {
  136. $amountpaid = $obj->mm;
  137. $totalpaid = $totalpaid + $amountpaid;
  138. }
  139. if ($obj->mode == 'paid') {
  140. print '<tr class="oddeven">';
  141. print '<td class="nowrap">'.$obj->dm."</td>\n";
  142. print '<td class="nowrap right">'.price($amountclaimed)."</td>\n";
  143. print '<td class="nowrap right">'.price($amountpaid)."</td>\n";
  144. print "</tr>\n";
  145. $amountclaimed = 0;
  146. $amountpaid = 0;
  147. $previousmode = '';
  148. $previousmonth = '';
  149. } else {
  150. $previousmode = $obj->mode;
  151. $previousmonth = $obj->dm;
  152. }
  153. $i++;
  154. }
  155. if ($mode == 'claimed' && !empty($previousmode)) {
  156. print '<tr class="oddeven">';
  157. print '<td class="nowrap">'.$previousmonth."</td>\n";
  158. print '<td class="nowrap right">'.price($amountclaimed)."</td>\n";
  159. print '<td class="nowrap right">'.price($amountpaid)."</td>\n";
  160. print "</tr>\n";
  161. $amountclaimed = 0;
  162. $amountpaid = 0;
  163. }
  164. print '<tr class="liste_total">';
  165. print '<td class="right">'.$langs->trans("Total").'</td>';
  166. print '<td class="nowrap right">'.price($totalclaimed).'</td>';
  167. print '<td class="nowrap right">'.price($totalpaid).'</td>';
  168. print "</tr>";
  169. print "</table>";
  170. $db->free($result);
  171. } else {
  172. dol_print_error($db);
  173. }
  174. }
  175. if (empty($localTaxType)) {
  176. accessforbidden('Parameter localTaxType is missing');
  177. exit;
  178. }
  179. /*
  180. * Actions
  181. */
  182. // None
  183. /*
  184. * View
  185. */
  186. $form = new Form($db);
  187. $company_static = new Societe($db);
  188. $tva = new Tva($db);
  189. if ($localTaxType == 1) {
  190. $LT = 'LT1';
  191. $LTSummary = 'LT1Summary';
  192. $LTPaid = 'LT1Paid';
  193. $LTCustomer = 'LT1Customer';
  194. $LTSupplier = 'LT1Supplier';
  195. $CalcLT = $conf->global->MAIN_INFO_LOCALTAX_CALC1;
  196. } else {
  197. $LT = 'LT2';
  198. $LTSummary = 'LT2Summary';
  199. $LTPaid = 'LT2Paid';
  200. $LTCustomer = 'LT2Customer';
  201. $LTSupplier = 'LT2Supplier';
  202. $CalcLT = $conf->global->MAIN_INFO_LOCALTAX_CALC2;
  203. }
  204. $fsearch = '<!-- hidden fields for form -->';
  205. $fsearch .= '<input type="hidden" name="token" value="'.newToken().'">';
  206. $fsearch .= '<input type="hidden" name="localTaxType" value="'.$localTaxType.'">';
  207. $fsearch .= '<input type="hidden" name="modetax" value="'.$modetax.'">';
  208. $description = $fsearch;
  209. // Show report header
  210. $name = $langs->transcountry($localTaxType == 1 ? "LT1ReportByMonth" : "LT2ReportByMonth", $mysoc->country_code);
  211. $description .= $langs->trans($LT);
  212. $calcmode = $langs->trans("LTReportBuildWithOptionDefinedInModule").' ';
  213. $calcmode .= ' <span class="opacitymedium">('.$langs->trans("TaxModuleSetupToModifyRulesLT", DOL_URL_ROOT.'/admin/company.php').')</span>';
  214. //if (! empty($conf->global->MAIN_MODULE_ACCOUNTING)) $description.='<br>'.$langs->trans("ThisIsAnEstimatedValue");
  215. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
  216. $builddate = dol_now();
  217. llxHeader('', $name);
  218. //$textprevyear="<a href=\"index.php?localTaxType=".$localTaxType."&year=" . ($year_current-1) . "\">".img_previous()."</a>";
  219. //$textnextyear=" <a href=\"index.php?localTaxType=".$localTaxType."&year=" . ($year_current+1) . "\">".img_next()."</a>";
  220. //print load_fiche_titre($langs->transcountry($LT,$mysoc->country_code),"$textprevyear ".$langs->trans("Year")." $year_start $textnextyear", 'bill');
  221. report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink, array(), $calcmode);
  222. //report_header($name,'',$textprevyear.$langs->trans("Year")." ".$year_start.$textnextyear,'',$description,$builddate,$exportlink,array(),$calcmode);
  223. print '<br>';
  224. print '<div class="fichecenter"><div class="fichethirdleft">';
  225. print load_fiche_titre($langs->transcountry($LTSummary, $mysoc->country_code), '', '');
  226. print '<table class="noborder centpercent">';
  227. print '<tr class="liste_titre">';
  228. print '<td width="30%">'.$langs->trans("Year")." ".$y."</td>";
  229. if ($CalcLT == 0) {
  230. print '<td class="right">'.$langs->transcountry($LTCustomer, $mysoc->country_code).'</td>';
  231. print '<td class="right">'.$langs->transcountry($LTSupplier, $mysoc->country_code).'</td>';
  232. }
  233. if ($CalcLT == 1) {
  234. print '<td class="right">'.$langs->transcountry($LTSupplier, $mysoc->country_code).'</td><td></td>';
  235. }
  236. if ($CalcLT == 2) {
  237. print '<td class="right">'.$langs->transcountry($LTCustomer, $mysoc->country_code).'</td><td></td>';
  238. }
  239. print '<td class="right">'.$langs->trans("TotalToPay").'</td>';
  240. print "<td>&nbsp;</td>\n";
  241. print "</tr>\n";
  242. $tmp = dol_getdate($date_start);
  243. $y = $tmp['year'];
  244. $m = $tmp['mon'];
  245. $tmp = dol_getdate($date_end);
  246. $yend = $tmp['year'];
  247. $mend = $tmp['mon'];
  248. $total = 0;
  249. $subtotalcoll = 0;
  250. $subtotalpaid = 0;
  251. $subtotal = 0;
  252. $i = 0;
  253. $mcursor = 0;
  254. while ((($y < $yend) || ($y == $yend && $m <= $mend)) && $mcursor < 1000) { // $mcursor is to avoid too large loop
  255. //$m = $conf->global->SOCIETE_FISCAL_MONTH_START + ($mcursor % 12);
  256. if ($m == 13) {
  257. $y++;
  258. }
  259. if ($m > 12) {
  260. $m -= 12;
  261. }
  262. $mcursor++;
  263. // Get array with details of each line
  264. $x_coll = tax_by_rate(($localTaxType == 1 ? 'localtax1' : 'localtax2'), $db, $y, 0, 0, 0, $modetax, 'sell', $m);
  265. $x_paye = tax_by_rate(($localTaxType == 1 ? 'localtax1' : 'localtax2'), $db, $y, 0, 0, 0, $modetax, 'buy', $m);
  266. $x_both = array();
  267. //now, from these two arrays, get another array with one rate per line
  268. foreach (array_keys($x_coll) as $my_coll_rate) {
  269. $x_both[$my_coll_rate]['coll']['totalht'] = $x_coll[$my_coll_rate]['totalht'];
  270. $x_both[$my_coll_rate]['coll']['vat'] = $x_coll[$my_coll_rate]['vat'];
  271. $x_both[$my_coll_rate]['coll']['localtax1'] = $x_coll[$my_coll_rate]['localtax1'];
  272. $x_both[$my_coll_rate]['coll']['localtax2'] = $x_coll[$my_coll_rate]['localtax2'];
  273. $x_both[$my_coll_rate]['paye']['totalht'] = 0;
  274. $x_both[$my_coll_rate]['paye']['vat'] = 0;
  275. $x_both[$my_coll_rate]['paye']['localtax1'] = 0;
  276. $x_both[$my_coll_rate]['paye']['localtax2'] = 0;
  277. $x_both[$my_coll_rate]['coll']['links'] = '';
  278. $x_both[$my_coll_rate]['coll']['detail'] = array();
  279. foreach ($x_coll[$my_coll_rate]['facid'] as $id => $dummy) {
  280. //$invoice_customer->id=$x_coll[$my_coll_rate]['facid'][$id];
  281. //$invoice_customer->ref=$x_coll[$my_coll_rate]['facnum'][$id];
  282. //$invoice_customer->type=$x_coll[$my_coll_rate]['type'][$id];
  283. //$company_static->fetch($x_coll[$my_coll_rate]['company_id'][$id]);
  284. $x_both[$my_coll_rate]['coll']['detail'][] = array(
  285. 'id' =>$x_coll[$my_coll_rate]['facid'][$id],
  286. 'descr' =>$x_coll[$my_coll_rate]['descr'][$id],
  287. 'pid' =>$x_coll[$my_coll_rate]['pid'][$id],
  288. 'pref' =>$x_coll[$my_coll_rate]['pref'][$id],
  289. 'ptype' =>$x_coll[$my_coll_rate]['ptype'][$id],
  290. 'payment_id'=>$x_coll[$my_coll_rate]['payment_id'][$id],
  291. 'payment_amount'=>$x_coll[$my_coll_rate]['payment_amount'][$id],
  292. 'ftotal_ttc'=>$x_coll[$my_coll_rate]['ftotal_ttc'][$id],
  293. 'dtotal_ttc'=>$x_coll[$my_coll_rate]['dtotal_ttc'][$id],
  294. 'dtype' =>$x_coll[$my_coll_rate]['dtype'][$id],
  295. 'datef' =>$x_coll[$my_coll_rate]['datef'][$id],
  296. 'datep' =>$x_coll[$my_coll_rate]['datep'][$id],
  297. //'company_link'=>$company_static->getNomUrl(1,'',20),
  298. 'ddate_start'=>$x_coll[$my_coll_rate]['ddate_start'][$id],
  299. 'ddate_end' =>$x_coll[$my_coll_rate]['ddate_end'][$id],
  300. 'totalht' =>$x_coll[$my_coll_rate]['totalht_list'][$id],
  301. 'vat' =>$x_coll[$my_coll_rate]['vat_list'][$id],
  302. 'localtax1' =>$x_coll[$my_coll_rate]['localtax1_list'][$id],
  303. 'localtax2' =>$x_coll[$my_coll_rate]['localtax2_list'][$id],
  304. //'link' =>$invoice_customer->getNomUrl(1,'',12)
  305. );
  306. }
  307. }
  308. // tva paid
  309. foreach (array_keys($x_paye) as $my_paye_rate) {
  310. $x_both[$my_paye_rate]['paye']['totalht'] = $x_paye[$my_paye_rate]['totalht'];
  311. $x_both[$my_paye_rate]['paye']['vat'] = $x_paye[$my_paye_rate]['vat'];
  312. $x_both[$my_paye_rate]['paye']['localtax1'] = $x_paye[$my_paye_rate]['localtax1'];
  313. $x_both[$my_paye_rate]['paye']['localtax2'] = $x_paye[$my_paye_rate]['localtax2'];
  314. if (!isset($x_both[$my_paye_rate]['coll']['totalht'])) {
  315. $x_both[$my_paye_rate]['coll']['totalht'] = 0;
  316. $x_both[$my_paye_rate]['coll']['vat'] = 0;
  317. $x_both[$my_paye_rate]['coll']['localtax1'] = 0;
  318. $x_both[$my_paye_rate]['coll']['localtax2'] = 0;
  319. }
  320. $x_both[$my_paye_rate]['paye']['links'] = '';
  321. $x_both[$my_paye_rate]['paye']['detail'] = array();
  322. foreach ($x_paye[$my_paye_rate]['facid'] as $id => $dummy) {
  323. // ExpenseReport
  324. if ($x_paye[$my_paye_rate]['ptype'][$id] == 'ExpenseReportPayment') {
  325. //$expensereport->id=$x_paye[$my_paye_rate]['facid'][$id];
  326. //$expensereport->ref=$x_paye[$my_paye_rate]['facnum'][$id];
  327. //$expensereport->type=$x_paye[$my_paye_rate]['type'][$id];
  328. $x_both[$my_paye_rate]['paye']['detail'][] = array(
  329. 'id' =>$x_paye[$my_paye_rate]['facid'][$id],
  330. 'descr' =>$x_paye[$my_paye_rate]['descr'][$id],
  331. 'pid' =>$x_paye[$my_paye_rate]['pid'][$id],
  332. 'pref' =>$x_paye[$my_paye_rate]['pref'][$id],
  333. 'ptype' =>$x_paye[$my_paye_rate]['ptype'][$id],
  334. 'payment_id' =>$x_paye[$my_paye_rate]['payment_id'][$id],
  335. 'payment_amount' =>$x_paye[$my_paye_rate]['payment_amount'][$id],
  336. 'ftotal_ttc' =>price2num($x_paye[$my_paye_rate]['ftotal_ttc'][$id]),
  337. 'dtotal_ttc' =>price2num($x_paye[$my_paye_rate]['dtotal_ttc'][$id]),
  338. 'dtype' =>$x_paye[$my_paye_rate]['dtype'][$id],
  339. 'ddate_start' =>$x_paye[$my_paye_rate]['ddate_start'][$id],
  340. 'ddate_end' =>$x_paye[$my_paye_rate]['ddate_end'][$id],
  341. 'totalht' =>price2num($x_paye[$my_paye_rate]['totalht_list'][$id]),
  342. 'vat' =>$x_paye[$my_paye_rate]['vat_list'][$id],
  343. 'localtax1' =>$x_paye[$my_paye_rate]['localtax1_list'][$id],
  344. 'localtax2' =>$x_paye[$my_paye_rate]['localtax2_list'][$id],
  345. //'link' =>$expensereport->getNomUrl(1)
  346. );
  347. } else {
  348. //$invoice_supplier->id=$x_paye[$my_paye_rate]['facid'][$id];
  349. //$invoice_supplier->ref=$x_paye[$my_paye_rate]['facnum'][$id];
  350. //$invoice_supplier->type=$x_paye[$my_paye_rate]['type'][$id];
  351. //$company_static->fetch($x_paye[$my_paye_rate]['company_id'][$id]);
  352. $x_both[$my_paye_rate]['paye']['detail'][] = array(
  353. 'id' =>$x_paye[$my_paye_rate]['facid'][$id],
  354. 'descr' =>$x_paye[$my_paye_rate]['descr'][$id],
  355. 'pid' =>$x_paye[$my_paye_rate]['pid'][$id],
  356. 'pref' =>$x_paye[$my_paye_rate]['pref'][$id],
  357. 'ptype' =>$x_paye[$my_paye_rate]['ptype'][$id],
  358. 'payment_id'=>$x_paye[$my_paye_rate]['payment_id'][$id],
  359. 'payment_amount'=>$x_paye[$my_paye_rate]['payment_amount'][$id],
  360. 'ftotal_ttc'=>price2num($x_paye[$my_paye_rate]['ftotal_ttc'][$id]),
  361. 'dtotal_ttc'=>price2num($x_paye[$my_paye_rate]['dtotal_ttc'][$id]),
  362. 'dtype' =>$x_paye[$my_paye_rate]['dtype'][$id],
  363. 'datef' =>$x_paye[$my_paye_rate]['datef'][$id],
  364. 'datep' =>$x_paye[$my_paye_rate]['datep'][$id],
  365. //'company_link'=>$company_static->getNomUrl(1,'',20),
  366. 'ddate_start'=>$x_paye[$my_paye_rate]['ddate_start'][$id],
  367. 'ddate_end' =>$x_paye[$my_paye_rate]['ddate_end'][$id],
  368. 'totalht' =>price2num($x_paye[$my_paye_rate]['totalht_list'][$id]),
  369. 'vat' =>$x_paye[$my_paye_rate]['vat_list'][$id],
  370. 'localtax1' =>$x_paye[$my_paye_rate]['localtax1_list'][$id],
  371. 'localtax2' =>$x_paye[$my_paye_rate]['localtax2_list'][$id],
  372. //'link' =>$invoice_supplier->getNomUrl(1,'',12)
  373. );
  374. }
  375. }
  376. }
  377. //now we have an array (x_both) indexed by rates for coll and paye
  378. $action = "tva";
  379. $object = array(&$x_coll, &$x_paye, &$x_both);
  380. $parameters["mode"] = $modetax;
  381. $parameters["year"] = $y;
  382. $parameters["month"] = $m;
  383. $parameters["type"] = 'localtax'.$localTaxType;
  384. // Initialize technical object to manage hooks of expenses. Note that conf->hooks_modules contains array array
  385. $hookmanager->initHooks(array('externalbalance'));
  386. $reshook = $hookmanager->executeHooks('addVatLine', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  387. if (!is_array($x_coll) && $coll_listbuy == -1) {
  388. $langs->load("errors");
  389. print '<tr><td colspan="5">'.$langs->trans("ErrorNoAccountancyModuleLoaded").'</td></tr>';
  390. break;
  391. }
  392. if (!is_array($x_paye) && $coll_listbuy == -2) {
  393. print '<tr><td colspan="5">'.$langs->trans("FeatureNotYetAvailable").'</td></tr>';
  394. break;
  395. }
  396. print '<tr class="oddeven">';
  397. print '<td class="nowrap"><a href="'.DOL_URL_ROOT.'/compta/localtax/quadri_detail.php?leftmenu=tax_vat&month='.$m.'&year='.$y.'">'.dol_print_date(dol_mktime(0, 0, 0, $m, 1, $y), "%b %Y").'</a></td>';
  398. $x_coll_sum = 0;
  399. foreach (array_keys($x_coll) as $rate) {
  400. $subtot_coll_total_ht = 0;
  401. $subtot_coll_vat = 0;
  402. foreach ($x_both[$rate]['coll']['detail'] as $index => $fields) {
  403. // Payment
  404. $ratiopaymentinvoice = 1;
  405. if ($modetax != 1) {
  406. // Define type
  407. // We MUST use dtype (type in line). We can use something else, only if dtype is really unknown.
  408. $type = (isset($fields['dtype']) ? $fields['dtype'] : $fields['ptype']);
  409. // Try to enhance type detection using date_start and date_end for free lines where type
  410. // was not saved.
  411. if (!empty($fields['ddate_start'])) {
  412. $type = 1;
  413. }
  414. if (!empty($fields['ddate_end'])) {
  415. $type = 1;
  416. }
  417. if (($type == 0 && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
  418. || ($type == 1 && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice')) {
  419. //print $langs->trans("NA");
  420. } else {
  421. if (isset($fields['payment_amount']) && price2num($fields['ftotal_ttc'])) {
  422. $ratiopaymentinvoice = ($fields['payment_amount'] / $fields['ftotal_ttc']);
  423. }
  424. }
  425. }
  426. //var_dump('type='.$type.' '.$fields['totalht'].' '.$ratiopaymentinvoice);
  427. $temp_ht = $fields['totalht'] * $ratiopaymentinvoice;
  428. $temp_vat = $fields['localtax'.$localTaxType] * $ratiopaymentinvoice;
  429. $subtot_coll_total_ht += $temp_ht;
  430. $subtot_coll_vat += $temp_vat;
  431. $x_coll_sum += $temp_vat;
  432. }
  433. }
  434. print '<td class="nowrap right">'.price(price2num($x_coll_sum, 'MT')).'</td>';
  435. $x_paye_sum = 0;
  436. foreach (array_keys($x_paye) as $rate) {
  437. $subtot_paye_total_ht = 0;
  438. $subtot_paye_vat = 0;
  439. foreach ($x_both[$rate]['paye']['detail'] as $index => $fields) {
  440. // Payment
  441. $ratiopaymentinvoice = 1;
  442. if ($modetax != 1) {
  443. // Define type
  444. // We MUST use dtype (type in line). We can use something else, only if dtype is really unknown.
  445. $type = (isset($fields['dtype']) ? $fields['dtype'] : $fields['ptype']);
  446. // Try to enhance type detection using date_start and date_end for free lines where type
  447. // was not saved.
  448. if (!empty($fields['ddate_start'])) {
  449. $type = 1;
  450. }
  451. if (!empty($fields['ddate_end'])) {
  452. $type = 1;
  453. }
  454. if (($type == 0 && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
  455. || ($type == 1 && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice')) {
  456. //print $langs->trans("NA");
  457. } else {
  458. if (isset($fields['payment_amount']) && price2num($fields['ftotal_ttc'])) {
  459. $ratiopaymentinvoice = ($fields['payment_amount'] / $fields['ftotal_ttc']);
  460. }
  461. }
  462. }
  463. //var_dump('type='.$type.' '.$fields['totalht'].' '.$ratiopaymentinvoice);
  464. $temp_ht = $fields['totalht'] * $ratiopaymentinvoice;
  465. $temp_vat = $fields['localtax'.$localTaxType] * $ratiopaymentinvoice;
  466. $subtot_paye_total_ht += $temp_ht;
  467. $subtot_paye_vat += $temp_vat;
  468. $x_paye_sum += $temp_vat;
  469. }
  470. }
  471. print '<td class="nowrap right">'.price(price2num($x_paye_sum, 'MT')).'</td>';
  472. $subtotalcoll = $subtotalcoll + $x_coll_sum;
  473. $subtotalpaid = $subtotalpaid + $x_paye_sum;
  474. $diff = $x_coll_sum - $x_paye_sum;
  475. $total = $total + $diff;
  476. $subtotal = price2num($subtotal + $diff, 'MT');
  477. print '<td class="nowrap right">'.price(price2num($diff, 'MT')).'</td>'."\n";
  478. print "<td>&nbsp;</td>\n";
  479. print "</tr>\n";
  480. $i++;
  481. $m++;
  482. if ($i > 2) {
  483. print '<tr class="liste_total">';
  484. print '<td class="right"><a href="quadri_detail.php?leftmenu=tax_vat&q='.round($m / 3).'&year='.$y.'">'.$langs->trans("SubTotal").'</a>:</td>';
  485. print '<td class="nowrap right">'.price(price2num($subtotalcoll, 'MT')).'</td>';
  486. print '<td class="nowrap right">'.price(price2num($subtotalpaid, 'MT')).'</td>';
  487. print '<td class="nowrap right">'.price(price2num($subtotal, 'MT')).'</td>';
  488. print '<td>&nbsp;</td></tr>';
  489. $i = 0;
  490. $subtotalcoll = 0;
  491. $subtotalpaid = 0;
  492. $subtotal = 0;
  493. }
  494. }
  495. print '<tr class="liste_total"><td class="right" colspan="3">'.$langs->trans("TotalToPay").':</td><td class="nowrap right">'.price(price2num($total, 'MT')).'</td>';
  496. print "<td>&nbsp;</td>\n";
  497. print '</tr>';
  498. print '</table>';
  499. print '</div><div class="fichetwothirdright">';
  500. /*
  501. * Paid
  502. */
  503. print load_fiche_titre($langs->transcountry($LTPaid, $mysoc->country_code), '', '');
  504. $sql = '';
  505. $sql .= "SELECT SUM(amount) as mm, date_format(f.datev,'%Y-%m') as dm, 'claimed' as mode";
  506. $sql .= " FROM ".MAIN_DB_PREFIX."localtax as f";
  507. $sql .= " WHERE f.entity = ".$conf->entity;
  508. $sql .= " AND (f.datev >= '".$db->idate($date_start)."' AND f.datev <= '".$db->idate($date_end)."')";
  509. $sql .= " AND localtaxtype=".((int) $localTaxType);
  510. $sql .= " GROUP BY dm";
  511. $sql .= " UNION ";
  512. $sql .= "SELECT SUM(amount) as mm, date_format(f.datep,'%Y-%m') as dm, 'paid' as mode";
  513. $sql .= " FROM ".MAIN_DB_PREFIX."localtax as f";
  514. $sql .= " WHERE f.entity = ".$conf->entity;
  515. $sql .= " AND (f.datep >= '".$db->idate($date_start)."' AND f.datep <= '".$db->idate($date_end)."')";
  516. $sql .= " AND localtaxtype=".((int) $localTaxType);
  517. $sql .= " GROUP BY dm";
  518. $sql .= " ORDER BY dm ASC, mode ASC";
  519. //print $sql;
  520. pt($db, $sql, $langs->trans("Month"));
  521. print '</div></div>';
  522. // End of page
  523. llxFooter();
  524. $db->close();