index.php 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. <?php
  2. /* Copyright (C) 2001-2003 Rodolphe Quiedeville <rodolphe@quiedeville.org>
  3. * Copyright (C) 2004 Eric Seigne <eric.seigne@ryxeo.com>
  4. * Copyright (C) 2004-2018 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2005-2009 Regis Houssin <regis.houssin@inodbox.com>
  6. * Copyright (C) 2014 Ferran Marcet <fmarcet@2byte.es>
  7. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  8. * Copyright (C) 2021 Gauthier VERDOL <gauthier.verdol@atm-consulting.fr>
  9. * Copyright (C) 2021 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/compta/tva/index.php
  26. * \ingroup tax
  27. * \brief Index page of VAT reports
  28. */
  29. require '../../main.inc.php';
  30. require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/tax.lib.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  33. require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
  34. require_once DOL_DOCUMENT_ROOT.'/compta/localtax/class/localtax.class.php';
  35. // Load translation files required by the page
  36. $langs->loadLangs(array("other", "compta", "banks", "bills", "companies", "product", "trips", "admin"));
  37. $refresh = GETPOSTISSET('submit') ? true : false;
  38. $year_current = GETPOSTISSET('year') ? GETPOST('year', 'int') : dol_print_date($now, '%Y', 'tzserver');
  39. $year_start = $year_current;
  40. $month_current = GETPOSTISSET('month') ? GETPOST('month', 'int') : dol_print_date($now, '%m', 'tzserver');
  41. $month_start = $month_current;
  42. $refresh = true;
  43. include DOL_DOCUMENT_ROOT.'/compta/tva/initdatesforvat.inc.php';
  44. // Define modetax (0 or 1)
  45. // 0=normal, 1=option vat for services is on debit, 2=option on payments for products
  46. $modetax = $conf->global->TAX_MODE;
  47. if (GETPOSTISSET("modetax")) {
  48. $modetax = GETPOST("modetax", 'int');
  49. }
  50. if (empty($modetax)) {
  51. $modetax = 0;
  52. }
  53. // Security check
  54. $socid = GETPOST('socid', 'int');
  55. if ($user->socid) {
  56. $socid = $user->socid;
  57. }
  58. $result = restrictedArea($user, 'tax', '', 'tva', 'charges');
  59. /**
  60. * print function
  61. *
  62. * @param DoliDB $db Database handler
  63. * @param string $sql SQL Request
  64. * @param string $date Date
  65. * @return void
  66. */
  67. function pt($db, $sql, $date)
  68. {
  69. global $conf, $bc, $langs, $form;
  70. $result = $db->query($sql);
  71. if ($result) {
  72. $num = $db->num_rows($result);
  73. $i = 0;
  74. $total = 0;
  75. print '<table class="noborder centpercent">';
  76. print '<tr class="liste_titre">';
  77. print '<td class="nowrap">'.$date.'</td>';
  78. print '<td class="right">'.$langs->trans("ClaimedForThisPeriod").'</td>';
  79. print '<td class="right">'.$langs->trans("PaidDuringThisPeriod").$form->textwithpicto('', $langs->trans('PaidDuringThisPeriodDesc'), 1).'</td>';
  80. print "</tr>\n";
  81. $totalclaimed = 0;
  82. $totalpaid = 0;
  83. $amountclaimed = 0;
  84. $amountpaid = 0;
  85. $previousmonth = '';
  86. $previousmode = '';
  87. $mode = '';
  88. while ($i < $num) {
  89. $obj = $db->fetch_object($result);
  90. $mode = $obj->mode;
  91. //print $obj->dm.' '.$obj->mode.' '.$previousmonth.' '.$previousmode;
  92. if ($obj->mode == 'claimed' && !empty($previousmode)) {
  93. print '<tr class="oddeven">';
  94. print '<td class="nowrap">'.$previousmonth."</td>\n";
  95. print '<td class="nowrap right"><span class="amount">'.price($amountclaimed)."</span></td>\n";
  96. print '<td class="nowrap right"><span class="amount">'.price($amountpaid)."</span></td>\n";
  97. print "</tr>\n";
  98. $amountclaimed = 0;
  99. $amountpaid = 0;
  100. }
  101. if ($obj->mode == 'claimed') {
  102. $amountclaimed = $obj->mm;
  103. $totalclaimed = $totalclaimed + $amountclaimed;
  104. }
  105. if ($obj->mode == 'paid') {
  106. $amountpaid = $obj->mm;
  107. $totalpaid = $totalpaid + $amountpaid;
  108. }
  109. if ($obj->mode == 'paid') {
  110. print '<tr class="oddeven">';
  111. print '<td class="nowrap">'.$obj->dm."</td>\n";
  112. print '<td class="nowrap right"><span class="amount">'.price($amountclaimed)."</span></td>\n";
  113. print '<td class="nowrap right"><span class="amount">'.price($amountpaid)."</span></td>\n";
  114. print "</tr>\n";
  115. $amountclaimed = 0;
  116. $amountpaid = 0;
  117. $previousmode = '';
  118. $previousmonth = '';
  119. } else {
  120. $previousmode = $obj->mode;
  121. $previousmonth = $obj->dm;
  122. }
  123. $i++;
  124. }
  125. if ($mode == 'claimed' && !empty($previousmode)) {
  126. print '<tr class="oddeven">';
  127. print '<td class="nowrap">'.$previousmonth."</td>\n";
  128. print '<td class="nowrap right">'.price($amountclaimed)."</td>\n";
  129. print '<td class="nowrap right">'.price($amountpaid)."</td>\n";
  130. print "</tr>\n";
  131. $amountclaimed = 0;
  132. $amountpaid = 0;
  133. }
  134. print '<tr class="liste_total">';
  135. print '<td class="right">'.$langs->trans("Total").'</td>';
  136. print '<td class="nowrap right">'.price($totalclaimed).'</td>';
  137. print '<td class="nowrap right">'.price($totalpaid).'</td>';
  138. print "</tr>";
  139. print "</table>";
  140. $db->free($result);
  141. } else {
  142. dol_print_error($db);
  143. }
  144. }
  145. /*
  146. * View
  147. */
  148. $form = new Form($db);
  149. $company_static = new Societe($db);
  150. $tva = new Tva($db);
  151. $fsearch = '<!-- hidden fields for form -->';
  152. $fsearch .= '<input type="hidden" name="token" value="'.newToken().'">';
  153. $fsearch .= '<input type="hidden" name="modetax" value="'.$modetax.'">';
  154. $description = $fsearch;
  155. // Show report header
  156. $name = $langs->trans("VATReportByMonth");
  157. $calcmode = '';
  158. if ($modetax == 0) {
  159. $calcmode = $langs->trans('OptionVATDefault');
  160. }
  161. if ($modetax == 1) {
  162. $calcmode = $langs->trans('OptionVATDebitOption');
  163. }
  164. if ($modetax == 2) {
  165. $calcmode = $langs->trans('OptionPaymentForProductAndServices');
  166. }
  167. $calcmode .= ' <span class="opacitymedium">('.$langs->trans("TaxModuleSetupToModifyRules", DOL_URL_ROOT.'/admin/taxes.php').')</span>';
  168. $description .= $langs->trans("VATSummary").'<br>';
  169. if ($conf->global->TAX_MODE_SELL_PRODUCT == 'invoice') {
  170. $description .= $langs->trans("RulesVATDueProducts");
  171. }
  172. if ($conf->global->TAX_MODE_SELL_PRODUCT == 'payment') {
  173. $description .= $langs->trans("RulesVATInProducts");
  174. }
  175. if ($conf->global->TAX_MODE_SELL_SERVICE == 'invoice') {
  176. $description .= '<br>'.$langs->trans("RulesVATDueServices");
  177. }
  178. if ($conf->global->TAX_MODE_SELL_SERVICE == 'payment') {
  179. $description .= '<br>'.$langs->trans("RulesVATInServices");
  180. }
  181. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  182. $description .= '<br>'.$langs->trans("DepositsAreNotIncluded");
  183. }
  184. if (!empty($conf->global->MAIN_MODULE_ACCOUNTING)) {
  185. $description .= '<br>'.$langs->trans("ThisIsAnEstimatedValue");
  186. }
  187. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
  188. $builddate = dol_now();
  189. llxHeader('', $name);
  190. //$textprevyear="<a href=\"index.php?year=" . ($year_current-1) . "\">".img_previous($langs->trans("Previous"), 'class="valignbottom"')."</a>";
  191. //$textnextyear=" <a href=\"index.php?year=" . ($year_current+1) . "\">".img_next($langs->trans("Next"), 'class="valignbottom"')."</a>";
  192. //print load_fiche_titre($langs->transcountry("VAT", $mysoc->country_code), $textprevyear." ".$langs->trans("Year")." ".$year_start." ".$textnextyear, 'bill');
  193. report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink, array(), $calcmode);
  194. //report_header($name,'',$textprevyear.$langs->trans("Year")." ".$year_start.$textnextyear,'',$description,$builddate,$exportlink,array(),$calcmode);
  195. print '<br>';
  196. if ($refresh === true) {
  197. print '<div class="fichecenter"><div class="fichethirdleft">';
  198. print load_fiche_titre($langs->trans("VATSummary"), '', '');
  199. print '<table class="noborder centpercent">';
  200. print '<tr class="liste_titre">';
  201. print '<td width="30%">' . $langs->trans("Year") . " " . $y . '</td>';
  202. print '<td class="right">' . $langs->trans("VATToPay") . '</td>';
  203. print '<td class="right">' . $langs->trans("VATToCollect") . '</td>';
  204. print '<td class="right">' . $langs->trans("Balance") . '</td>';
  205. print '<td>&nbsp;</td>' . "\n";
  206. print '</tr>' . "\n";
  207. $tmp = dol_getdate($date_start);
  208. $y = $tmp['year'];
  209. $m = $tmp['mon'];
  210. $tmp = dol_getdate($date_end);
  211. $yend = $tmp['year'];
  212. $mend = $tmp['mon'];
  213. //var_dump($m);
  214. $total = 0;
  215. $subtotalcoll = 0;
  216. $subtotalpaid = 0;
  217. $subtotal = 0;
  218. $i = 0;
  219. $mcursor = 0;
  220. while ((($y < $yend) || ($y == $yend && $m <= $mend)) && $mcursor < 1000) { // $mcursor is to avoid too large loop
  221. //$m = $conf->global->SOCIETE_FISCAL_MONTH_START + ($mcursor % 12);
  222. if ($m == 13) {
  223. $y++;
  224. }
  225. if ($m > 12) {
  226. $m -= 12;
  227. }
  228. $mcursor++;
  229. $x_coll = tax_by_rate('vat', $db, $y, 0, 0, 0, $modetax, 'sell', $m);
  230. $x_paye = tax_by_rate('vat', $db, $y, 0, 0, 0, $modetax, 'buy', $m);
  231. $x_both = array();
  232. //now, from these two arrays, get another array with one rate per line
  233. foreach (array_keys($x_coll) as $my_coll_rate) {
  234. $x_both[$my_coll_rate]['coll']['totalht'] = $x_coll[$my_coll_rate]['totalht'];
  235. $x_both[$my_coll_rate]['coll']['vat'] = $x_coll[$my_coll_rate]['vat'];
  236. $x_both[$my_coll_rate]['paye']['totalht'] = 0;
  237. $x_both[$my_coll_rate]['paye']['vat'] = 0;
  238. $x_both[$my_coll_rate]['coll']['links'] = '';
  239. $x_both[$my_coll_rate]['coll']['detail'] = array();
  240. foreach ($x_coll[$my_coll_rate]['facid'] as $id => $dummy) {
  241. //$invoice_customer->id=$x_coll[$my_coll_rate]['facid'][$id];
  242. //$invoice_customer->ref=$x_coll[$my_coll_rate]['facnum'][$id];
  243. //$invoice_customer->type=$x_coll[$my_coll_rate]['type'][$id];
  244. //$company_static->fetch($x_coll[$my_coll_rate]['company_id'][$id]);
  245. $x_both[$my_coll_rate]['coll']['detail'][] = array(
  246. 'id' => $x_coll[$my_coll_rate]['facid'][$id],
  247. 'descr' => $x_coll[$my_coll_rate]['descr'][$id],
  248. 'pid' => $x_coll[$my_coll_rate]['pid'][$id],
  249. 'pref' => $x_coll[$my_coll_rate]['pref'][$id],
  250. 'ptype' => $x_coll[$my_coll_rate]['ptype'][$id],
  251. 'payment_id' => $x_coll[$my_coll_rate]['payment_id'][$id],
  252. 'payment_amount' => $x_coll[$my_coll_rate]['payment_amount'][$id],
  253. 'ftotal_ttc' => $x_coll[$my_coll_rate]['ftotal_ttc'][$id],
  254. 'dtotal_ttc' => $x_coll[$my_coll_rate]['dtotal_ttc'][$id],
  255. 'dtype' => $x_coll[$my_coll_rate]['dtype'][$id],
  256. 'datef' => $x_coll[$my_coll_rate]['datef'][$id],
  257. 'datep' => $x_coll[$my_coll_rate]['datep'][$id],
  258. //'company_link'=>$company_static->getNomUrl(1,'',20),
  259. 'ddate_start' => $x_coll[$my_coll_rate]['ddate_start'][$id],
  260. 'ddate_end' => $x_coll[$my_coll_rate]['ddate_end'][$id],
  261. 'totalht' => $x_coll[$my_coll_rate]['totalht_list'][$id],
  262. 'vat' => $x_coll[$my_coll_rate]['vat_list'][$id],
  263. //'link' =>$invoice_customer->getNomUrl(1,'',12)
  264. );
  265. }
  266. }
  267. // tva paid
  268. foreach (array_keys($x_paye) as $my_paye_rate) {
  269. $x_both[$my_paye_rate]['paye']['totalht'] = $x_paye[$my_paye_rate]['totalht'];
  270. $x_both[$my_paye_rate]['paye']['vat'] = $x_paye[$my_paye_rate]['vat'];
  271. if (!isset($x_both[$my_paye_rate]['coll']['totalht'])) {
  272. $x_both[$my_paye_rate]['coll']['totalht'] = 0;
  273. $x_both[$my_paye_rate]['coll']['vat'] = 0;
  274. }
  275. $x_both[$my_paye_rate]['paye']['links'] = '';
  276. $x_both[$my_paye_rate]['paye']['detail'] = array();
  277. foreach ($x_paye[$my_paye_rate]['facid'] as $id => $dummy) {
  278. // ExpenseReport
  279. if ($x_paye[$my_paye_rate]['ptype'][$id] == 'ExpenseReportPayment') {
  280. //$expensereport->id=$x_paye[$my_paye_rate]['facid'][$id];
  281. //$expensereport->ref=$x_paye[$my_paye_rate]['facnum'][$id];
  282. //$expensereport->type=$x_paye[$my_paye_rate]['type'][$id];
  283. $x_both[$my_paye_rate]['paye']['detail'][] = array(
  284. 'id' => $x_paye[$my_paye_rate]['facid'][$id],
  285. 'descr' => $x_paye[$my_paye_rate]['descr'][$id],
  286. 'pid' => $x_paye[$my_paye_rate]['pid'][$id],
  287. 'pref' => $x_paye[$my_paye_rate]['pref'][$id],
  288. 'ptype' => $x_paye[$my_paye_rate]['ptype'][$id],
  289. 'payment_id' => $x_paye[$my_paye_rate]['payment_id'][$id],
  290. 'payment_amount' => $x_paye[$my_paye_rate]['payment_amount'][$id],
  291. 'ftotal_ttc' => price2num($x_paye[$my_paye_rate]['ftotal_ttc'][$id]),
  292. 'dtotal_ttc' => price2num($x_paye[$my_paye_rate]['dtotal_ttc'][$id]),
  293. 'dtype' => $x_paye[$my_paye_rate]['dtype'][$id],
  294. 'ddate_start' => $x_paye[$my_paye_rate]['ddate_start'][$id],
  295. 'ddate_end' => $x_paye[$my_paye_rate]['ddate_end'][$id],
  296. 'totalht' => price2num($x_paye[$my_paye_rate]['totalht_list'][$id]),
  297. 'vat' => $x_paye[$my_paye_rate]['vat_list'][$id],
  298. //'link' =>$expensereport->getNomUrl(1)
  299. );
  300. } else {
  301. //$invoice_supplier->id=$x_paye[$my_paye_rate]['facid'][$id];
  302. //$invoice_supplier->ref=$x_paye[$my_paye_rate]['facnum'][$id];
  303. //$invoice_supplier->type=$x_paye[$my_paye_rate]['type'][$id];
  304. //$company_static->fetch($x_paye[$my_paye_rate]['company_id'][$id]);
  305. $x_both[$my_paye_rate]['paye']['detail'][] = array(
  306. 'id' => $x_paye[$my_paye_rate]['facid'][$id],
  307. 'descr' => $x_paye[$my_paye_rate]['descr'][$id],
  308. 'pid' => $x_paye[$my_paye_rate]['pid'][$id],
  309. 'pref' => $x_paye[$my_paye_rate]['pref'][$id],
  310. 'ptype' => $x_paye[$my_paye_rate]['ptype'][$id],
  311. 'payment_id' => $x_paye[$my_paye_rate]['payment_id'][$id],
  312. 'payment_amount' => $x_paye[$my_paye_rate]['payment_amount'][$id],
  313. 'ftotal_ttc' => price2num($x_paye[$my_paye_rate]['ftotal_ttc'][$id]),
  314. 'dtotal_ttc' => price2num($x_paye[$my_paye_rate]['dtotal_ttc'][$id]),
  315. 'dtype' => $x_paye[$my_paye_rate]['dtype'][$id],
  316. 'datef' => $x_paye[$my_paye_rate]['datef'][$id],
  317. 'datep' => $x_paye[$my_paye_rate]['datep'][$id],
  318. //'company_link'=>$company_static->getNomUrl(1,'',20),
  319. 'ddate_start' => $x_paye[$my_paye_rate]['ddate_start'][$id],
  320. 'ddate_end' => $x_paye[$my_paye_rate]['ddate_end'][$id],
  321. 'totalht' => price2num($x_paye[$my_paye_rate]['totalht_list'][$id]),
  322. 'vat' => $x_paye[$my_paye_rate]['vat_list'][$id],
  323. //'link' =>$invoice_supplier->getNomUrl(1,'',12)
  324. );
  325. }
  326. }
  327. }
  328. //now we have an array (x_both) indexed by rates for coll and paye
  329. $action = "tva";
  330. $object = array(&$x_coll, &$x_paye, &$x_both);
  331. $parameters["mode"] = $modetax;
  332. $parameters["year"] = $y;
  333. $parameters["month"] = $m;
  334. $parameters["type"] = 'vat';
  335. // Initialize technical object to manage hooks of expenses. Note that conf->hooks_modules contains array array
  336. $hookmanager->initHooks(array('externalbalance'));
  337. $reshook = $hookmanager->executeHooks('addVatLine', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  338. if (!is_array($x_coll) && $coll_listbuy == -1) {
  339. $langs->load("errors");
  340. print '<tr><td colspan="5">' . $langs->trans("ErrorNoAccountancyModuleLoaded") . '</td></tr>';
  341. break;
  342. }
  343. if (!is_array($x_paye) && $coll_listbuy == -2) {
  344. print '<tr><td colspan="5">' . $langs->trans("FeatureNotYetAvailable") . '</td></tr>';
  345. break;
  346. }
  347. print '<tr class="oddeven">';
  348. print '<td class="nowrap"><a href="' . DOL_URL_ROOT . '/compta/tva/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>';
  349. $x_coll_sum = 0;
  350. foreach (array_keys($x_coll) as $rate) {
  351. $subtot_coll_total_ht = 0;
  352. $subtot_coll_vat = 0;
  353. foreach ($x_both[$rate]['coll']['detail'] as $index => $fields) {
  354. // Payment
  355. $ratiopaymentinvoice = 1;
  356. if ($modetax != 1) {
  357. // Define type
  358. // We MUST use dtype (type in line). We can use something else, only if dtype is really unknown.
  359. $type = (isset($fields['dtype']) ? $fields['dtype'] : $fields['ptype']);
  360. // Try to enhance type detection using date_start and date_end for free lines where type
  361. // was not saved.
  362. if (!empty($fields['ddate_start'])) {
  363. $type = 1;
  364. }
  365. if (!empty($fields['ddate_end'])) {
  366. $type = 1;
  367. }
  368. if (($type == 0 && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
  369. || ($type == 1 && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice')) {
  370. //print $langs->trans("NA");
  371. } else {
  372. if (isset($fields['payment_amount']) && price2num($fields['ftotal_ttc'])) {
  373. $ratiopaymentinvoice = ($fields['payment_amount'] / $fields['ftotal_ttc']);
  374. }
  375. }
  376. }
  377. //var_dump('type='.$type.' '.$fields['totalht'].' '.$ratiopaymentinvoice);
  378. $temp_ht = $fields['totalht'] * $ratiopaymentinvoice;
  379. $temp_vat = $fields['vat'] * $ratiopaymentinvoice;
  380. $subtot_coll_total_ht += $temp_ht;
  381. $subtot_coll_vat += $temp_vat;
  382. $x_coll_sum += $temp_vat;
  383. }
  384. }
  385. print '<td class="nowrap right"><span class="amount">' . price(price2num($x_coll_sum, 'MT')) . '</span></td>';
  386. $x_paye_sum = 0;
  387. foreach (array_keys($x_paye) as $rate) {
  388. $subtot_paye_total_ht = 0;
  389. $subtot_paye_vat = 0;
  390. foreach ($x_both[$rate]['paye']['detail'] as $index => $fields) {
  391. // Payment
  392. $ratiopaymentinvoice = 1;
  393. if ($modetax != 1) {
  394. // Define type
  395. // We MUST use dtype (type in line). We can use something else, only if dtype is really unknown.
  396. $type = (isset($fields['dtype']) ? $fields['dtype'] : $fields['ptype']);
  397. // Try to enhance type detection using date_start and date_end for free lines where type
  398. // was not saved.
  399. if (!empty($fields['ddate_start'])) {
  400. $type = 1;
  401. }
  402. if (!empty($fields['ddate_end'])) {
  403. $type = 1;
  404. }
  405. if (($type == 0 && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
  406. || ($type == 1 && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice')) {
  407. //print $langs->trans("NA");
  408. } else {
  409. if (isset($fields['payment_amount']) && price2num($fields['ftotal_ttc'])) {
  410. $ratiopaymentinvoice = ($fields['payment_amount'] / $fields['ftotal_ttc']);
  411. }
  412. }
  413. }
  414. //var_dump('type='.$type.' '.$fields['totalht'].' '.$ratiopaymentinvoice);
  415. $temp_ht = $fields['totalht'] * $ratiopaymentinvoice;
  416. $temp_vat = $fields['vat'] * $ratiopaymentinvoice;
  417. $subtot_paye_total_ht += $temp_ht;
  418. $subtot_paye_vat += $temp_vat;
  419. $x_paye_sum += $temp_vat;
  420. }
  421. }
  422. print '<td class="nowrap right"><span class="amount">' . price(price2num($x_paye_sum, 'MT')) . '</span></td>';
  423. $subtotalcoll = $subtotalcoll + $x_coll_sum;
  424. $subtotalpaid = $subtotalpaid + $x_paye_sum;
  425. $diff = $x_coll_sum - $x_paye_sum;
  426. $total = $total + $diff;
  427. $subtotal = price2num($subtotal + $diff, 'MT');
  428. print '<td class="nowrap right"><span class="amount">' . price(price2num($diff, 'MT')) . '</span></td>' . "\n";
  429. print "<td>&nbsp;</td>\n";
  430. print "</tr>\n";
  431. // Total
  432. $i++;
  433. $m++;
  434. if ($i > 2) {
  435. print '<tr class="liste_total">';
  436. print '<td class="right"><a href="quadri_detail.php?leftmenu=tax_vat&q=' . round($m / 3) . '&year=' . $y . '">' . $langs->trans("SubTotal") . '</a>:</td>';
  437. print '<td class="nowrap right">' . price(price2num($subtotalcoll, 'MT')) . '</td>';
  438. print '<td class="nowrap right">' . price(price2num($subtotalpaid, 'MT')) . '</td>';
  439. print '<td class="nowrap right">' . price(price2num($subtotal, 'MT')) . '</td>';
  440. print '<td>&nbsp;</td></tr>';
  441. $i = 0;
  442. $subtotalcoll = 0;
  443. $subtotalpaid = 0;
  444. $subtotal = 0;
  445. }
  446. }
  447. print '<tr class="liste_total"><td class="right" colspan="3">' . $langs->trans("TotalToPay") . ':</td><td class="nowrap right">' . price(price2num($total, 'MT')) . '</td>';
  448. print "<td>&nbsp;</td>\n";
  449. print '</tr>';
  450. print '</table>';
  451. print '</div><div class="fichetwothirdright">';
  452. /*
  453. * Paid
  454. */
  455. print load_fiche_titre($langs->trans("VATPaid"), '', '');
  456. $sql = '';
  457. $sql .= "SELECT SUM(amount) as mm, date_format(tva.datev,'%Y-%m') as dm, 'claimed' as mode";
  458. $sql .= " FROM " . MAIN_DB_PREFIX . "tva as tva";
  459. $sql .= " WHERE tva.entity = " . $conf->entity;
  460. $sql .= " AND (tva.datev >= '" . $db->idate($date_start) . "' AND tva.datev <= '" . $db->idate($date_end) . "')";
  461. $sql .= " GROUP BY dm";
  462. $sql .= " UNION ";
  463. $sql .= "SELECT SUM(ptva.amount) as mm, date_format(tva.datev,'%Y-%m') as dm, 'paid' as mode";
  464. $sql .= " FROM " . MAIN_DB_PREFIX . "tva as tva";
  465. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "payment_vat as ptva ON (tva.rowid = ptva.fk_tva)";
  466. $sql .= " WHERE tva.entity = " . $conf->entity;
  467. $sql .= " AND (tva.datev >= '" . $db->idate($date_start) . "' AND tva.datev <= '" . $db->idate($date_end) . "')";
  468. $sql .= " GROUP BY dm";
  469. $sql .= " ORDER BY dm ASC, mode ASC";
  470. //print $sql;
  471. pt($db, $sql, $langs->trans("Month"));
  472. print '</div>';
  473. }
  474. llxFooter();
  475. $db->close();