index.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544
  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->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  185. $description .= $langs->trans("SupplierDepositsAreNotIncluded");
  186. }
  187. if (!empty($conf->global->MAIN_MODULE_ACCOUNTING)) {
  188. $description .= '<br>'.$langs->trans("ThisIsAnEstimatedValue");
  189. }
  190. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
  191. $builddate = dol_now();
  192. llxHeader('', $name);
  193. //$textprevyear="<a href=\"index.php?year=" . ($year_current-1) . "\">".img_previous($langs->trans("Previous"), 'class="valignbottom"')."</a>";
  194. //$textnextyear=" <a href=\"index.php?year=" . ($year_current+1) . "\">".img_next($langs->trans("Next"), 'class="valignbottom"')."</a>";
  195. //print load_fiche_titre($langs->transcountry("VAT", $mysoc->country_code), $textprevyear." ".$langs->trans("Year")." ".$year_start." ".$textnextyear, 'bill');
  196. report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink, array(), $calcmode);
  197. //report_header($name,'',$textprevyear.$langs->trans("Year")." ".$year_start.$textnextyear,'',$description,$builddate,$exportlink,array(),$calcmode);
  198. print '<br>';
  199. if ($refresh === true) {
  200. print '<div class="fichecenter"><div class="fichethirdleft">';
  201. print load_fiche_titre($langs->trans("VATSummary"), '', '');
  202. print '<table class="noborder centpercent">';
  203. print '<tr class="liste_titre">';
  204. print '<td width="30%">' . $langs->trans("Year") . " " . $y . '</td>';
  205. print '<td class="right">' . $langs->trans("VATToPay") . '</td>';
  206. print '<td class="right">' . $langs->trans("VATToCollect") . '</td>';
  207. print '<td class="right">' . $langs->trans("Balance") . '</td>';
  208. print '<td>&nbsp;</td>' . "\n";
  209. print '</tr>' . "\n";
  210. $tmp = dol_getdate($date_start);
  211. $y = $tmp['year'];
  212. $m = $tmp['mon'];
  213. $tmp = dol_getdate($date_end);
  214. $yend = $tmp['year'];
  215. $mend = $tmp['mon'];
  216. //var_dump($m);
  217. $total = 0;
  218. $subtotalcoll = 0;
  219. $subtotalpaid = 0;
  220. $subtotal = 0;
  221. $i = 0;
  222. $mcursor = 0;
  223. while ((($y < $yend) || ($y == $yend && $m <= $mend)) && $mcursor < 1000) { // $mcursor is to avoid too large loop
  224. //$m = $conf->global->SOCIETE_FISCAL_MONTH_START + ($mcursor % 12);
  225. if ($m == 13) {
  226. $y++;
  227. }
  228. if ($m > 12) {
  229. $m -= 12;
  230. }
  231. $mcursor++;
  232. $x_coll = tax_by_rate('vat', $db, $y, 0, 0, 0, $modetax, 'sell', $m);
  233. $x_paye = tax_by_rate('vat', $db, $y, 0, 0, 0, $modetax, 'buy', $m);
  234. $x_both = array();
  235. //now, from these two arrays, get another array with one rate per line
  236. foreach (array_keys($x_coll) as $my_coll_rate) {
  237. $x_both[$my_coll_rate]['coll']['totalht'] = $x_coll[$my_coll_rate]['totalht'];
  238. $x_both[$my_coll_rate]['coll']['vat'] = $x_coll[$my_coll_rate]['vat'];
  239. $x_both[$my_coll_rate]['paye']['totalht'] = 0;
  240. $x_both[$my_coll_rate]['paye']['vat'] = 0;
  241. $x_both[$my_coll_rate]['coll']['links'] = '';
  242. $x_both[$my_coll_rate]['coll']['detail'] = array();
  243. foreach ($x_coll[$my_coll_rate]['facid'] as $id => $dummy) {
  244. //$invoice_customer->id=$x_coll[$my_coll_rate]['facid'][$id];
  245. //$invoice_customer->ref=$x_coll[$my_coll_rate]['facnum'][$id];
  246. //$invoice_customer->type=$x_coll[$my_coll_rate]['type'][$id];
  247. //$company_static->fetch($x_coll[$my_coll_rate]['company_id'][$id]);
  248. $x_both[$my_coll_rate]['coll']['detail'][] = array(
  249. 'id' => $x_coll[$my_coll_rate]['facid'][$id],
  250. 'descr' => $x_coll[$my_coll_rate]['descr'][$id],
  251. 'pid' => $x_coll[$my_coll_rate]['pid'][$id],
  252. 'pref' => $x_coll[$my_coll_rate]['pref'][$id],
  253. 'ptype' => $x_coll[$my_coll_rate]['ptype'][$id],
  254. 'payment_id' => $x_coll[$my_coll_rate]['payment_id'][$id],
  255. 'payment_amount' => $x_coll[$my_coll_rate]['payment_amount'][$id],
  256. 'ftotal_ttc' => $x_coll[$my_coll_rate]['ftotal_ttc'][$id],
  257. 'dtotal_ttc' => $x_coll[$my_coll_rate]['dtotal_ttc'][$id],
  258. 'dtype' => $x_coll[$my_coll_rate]['dtype'][$id],
  259. 'datef' => $x_coll[$my_coll_rate]['datef'][$id],
  260. 'datep' => $x_coll[$my_coll_rate]['datep'][$id],
  261. //'company_link'=>$company_static->getNomUrl(1,'',20),
  262. 'ddate_start' => $x_coll[$my_coll_rate]['ddate_start'][$id],
  263. 'ddate_end' => $x_coll[$my_coll_rate]['ddate_end'][$id],
  264. 'totalht' => $x_coll[$my_coll_rate]['totalht_list'][$id],
  265. 'vat' => $x_coll[$my_coll_rate]['vat_list'][$id],
  266. //'link' =>$invoice_customer->getNomUrl(1,'',12)
  267. );
  268. }
  269. }
  270. // tva paid
  271. foreach (array_keys($x_paye) as $my_paye_rate) {
  272. $x_both[$my_paye_rate]['paye']['totalht'] = $x_paye[$my_paye_rate]['totalht'];
  273. $x_both[$my_paye_rate]['paye']['vat'] = $x_paye[$my_paye_rate]['vat'];
  274. if (!isset($x_both[$my_paye_rate]['coll']['totalht'])) {
  275. $x_both[$my_paye_rate]['coll']['totalht'] = 0;
  276. $x_both[$my_paye_rate]['coll']['vat'] = 0;
  277. }
  278. $x_both[$my_paye_rate]['paye']['links'] = '';
  279. $x_both[$my_paye_rate]['paye']['detail'] = array();
  280. foreach ($x_paye[$my_paye_rate]['facid'] as $id => $dummy) {
  281. // ExpenseReport
  282. if ($x_paye[$my_paye_rate]['ptype'][$id] == 'ExpenseReportPayment') {
  283. //$expensereport->id=$x_paye[$my_paye_rate]['facid'][$id];
  284. //$expensereport->ref=$x_paye[$my_paye_rate]['facnum'][$id];
  285. //$expensereport->type=$x_paye[$my_paye_rate]['type'][$id];
  286. $x_both[$my_paye_rate]['paye']['detail'][] = array(
  287. 'id' => $x_paye[$my_paye_rate]['facid'][$id],
  288. 'descr' => $x_paye[$my_paye_rate]['descr'][$id],
  289. 'pid' => $x_paye[$my_paye_rate]['pid'][$id],
  290. 'pref' => $x_paye[$my_paye_rate]['pref'][$id],
  291. 'ptype' => $x_paye[$my_paye_rate]['ptype'][$id],
  292. 'payment_id' => $x_paye[$my_paye_rate]['payment_id'][$id],
  293. 'payment_amount' => $x_paye[$my_paye_rate]['payment_amount'][$id],
  294. 'ftotal_ttc' => price2num($x_paye[$my_paye_rate]['ftotal_ttc'][$id]),
  295. 'dtotal_ttc' => price2num($x_paye[$my_paye_rate]['dtotal_ttc'][$id]),
  296. 'dtype' => $x_paye[$my_paye_rate]['dtype'][$id],
  297. 'ddate_start' => $x_paye[$my_paye_rate]['ddate_start'][$id],
  298. 'ddate_end' => $x_paye[$my_paye_rate]['ddate_end'][$id],
  299. 'totalht' => price2num($x_paye[$my_paye_rate]['totalht_list'][$id]),
  300. 'vat' => $x_paye[$my_paye_rate]['vat_list'][$id],
  301. //'link' =>$expensereport->getNomUrl(1)
  302. );
  303. } else {
  304. //$invoice_supplier->id=$x_paye[$my_paye_rate]['facid'][$id];
  305. //$invoice_supplier->ref=$x_paye[$my_paye_rate]['facnum'][$id];
  306. //$invoice_supplier->type=$x_paye[$my_paye_rate]['type'][$id];
  307. //$company_static->fetch($x_paye[$my_paye_rate]['company_id'][$id]);
  308. $x_both[$my_paye_rate]['paye']['detail'][] = array(
  309. 'id' => $x_paye[$my_paye_rate]['facid'][$id],
  310. 'descr' => $x_paye[$my_paye_rate]['descr'][$id],
  311. 'pid' => $x_paye[$my_paye_rate]['pid'][$id],
  312. 'pref' => $x_paye[$my_paye_rate]['pref'][$id],
  313. 'ptype' => $x_paye[$my_paye_rate]['ptype'][$id],
  314. 'payment_id' => $x_paye[$my_paye_rate]['payment_id'][$id],
  315. 'payment_amount' => $x_paye[$my_paye_rate]['payment_amount'][$id],
  316. 'ftotal_ttc' => price2num($x_paye[$my_paye_rate]['ftotal_ttc'][$id]),
  317. 'dtotal_ttc' => price2num($x_paye[$my_paye_rate]['dtotal_ttc'][$id]),
  318. 'dtype' => $x_paye[$my_paye_rate]['dtype'][$id],
  319. 'datef' => $x_paye[$my_paye_rate]['datef'][$id],
  320. 'datep' => $x_paye[$my_paye_rate]['datep'][$id],
  321. //'company_link'=>$company_static->getNomUrl(1,'',20),
  322. 'ddate_start' => $x_paye[$my_paye_rate]['ddate_start'][$id],
  323. 'ddate_end' => $x_paye[$my_paye_rate]['ddate_end'][$id],
  324. 'totalht' => price2num($x_paye[$my_paye_rate]['totalht_list'][$id]),
  325. 'vat' => $x_paye[$my_paye_rate]['vat_list'][$id],
  326. //'link' =>$invoice_supplier->getNomUrl(1,'',12)
  327. );
  328. }
  329. }
  330. }
  331. //now we have an array (x_both) indexed by rates for coll and paye
  332. $action = "tva";
  333. $object = array(&$x_coll, &$x_paye, &$x_both);
  334. $parameters["mode"] = $modetax;
  335. $parameters["year"] = $y;
  336. $parameters["month"] = $m;
  337. $parameters["type"] = 'vat';
  338. // Initialize technical object to manage hooks of expenses. Note that conf->hooks_modules contains array array
  339. $hookmanager->initHooks(array('externalbalance'));
  340. $reshook = $hookmanager->executeHooks('addVatLine', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  341. print '<tr class="oddeven">';
  342. 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>';
  343. $x_coll_sum = 0;
  344. foreach (array_keys($x_coll) as $rate) {
  345. $subtot_coll_total_ht = 0;
  346. $subtot_coll_vat = 0;
  347. foreach ($x_both[$rate]['coll']['detail'] as $index => $fields) {
  348. // Payment
  349. $ratiopaymentinvoice = 1;
  350. if ($modetax != 1) {
  351. // Define type
  352. // We MUST use dtype (type in line). We can use something else, only if dtype is really unknown.
  353. $type = (isset($fields['dtype']) ? $fields['dtype'] : $fields['ptype']);
  354. // Try to enhance type detection using date_start and date_end for free lines where type
  355. // was not saved.
  356. if (!empty($fields['ddate_start'])) {
  357. $type = 1;
  358. }
  359. if (!empty($fields['ddate_end'])) {
  360. $type = 1;
  361. }
  362. if (($type == 0 && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
  363. || ($type == 1 && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice')) {
  364. //print $langs->trans("NA");
  365. } else {
  366. if (isset($fields['payment_amount']) && price2num($fields['ftotal_ttc'])) {
  367. $ratiopaymentinvoice = ($fields['payment_amount'] / $fields['ftotal_ttc']);
  368. }
  369. }
  370. }
  371. //var_dump('type='.$type.' '.$fields['totalht'].' '.$ratiopaymentinvoice);
  372. $temp_ht = $fields['totalht'] * $ratiopaymentinvoice;
  373. $temp_vat = $fields['vat'] * $ratiopaymentinvoice;
  374. $subtot_coll_total_ht += $temp_ht;
  375. $subtot_coll_vat += $temp_vat;
  376. $x_coll_sum += $temp_vat;
  377. }
  378. }
  379. print '<td class="nowrap right"><span class="amount">' . price(price2num($x_coll_sum, 'MT')) . '</span></td>';
  380. $x_paye_sum = 0;
  381. foreach (array_keys($x_paye) as $rate) {
  382. $subtot_paye_total_ht = 0;
  383. $subtot_paye_vat = 0;
  384. foreach ($x_both[$rate]['paye']['detail'] as $index => $fields) {
  385. // Payment
  386. $ratiopaymentinvoice = 1;
  387. if ($modetax != 1) {
  388. // Define type
  389. // We MUST use dtype (type in line). We can use something else, only if dtype is really unknown.
  390. $type = (isset($fields['dtype']) ? $fields['dtype'] : $fields['ptype']);
  391. // Try to enhance type detection using date_start and date_end for free lines where type
  392. // was not saved.
  393. if (!empty($fields['ddate_start'])) {
  394. $type = 1;
  395. }
  396. if (!empty($fields['ddate_end'])) {
  397. $type = 1;
  398. }
  399. if (($type == 0 && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
  400. || ($type == 1 && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice')) {
  401. //print $langs->trans("NA");
  402. } else {
  403. if (isset($fields['payment_amount']) && price2num($fields['ftotal_ttc'])) {
  404. $ratiopaymentinvoice = ($fields['payment_amount'] / $fields['ftotal_ttc']);
  405. }
  406. }
  407. }
  408. //var_dump('type='.$type.' '.$fields['totalht'].' '.$ratiopaymentinvoice);
  409. $temp_ht = $fields['totalht'] * $ratiopaymentinvoice;
  410. $temp_vat = $fields['vat'] * $ratiopaymentinvoice;
  411. $subtot_paye_total_ht += $temp_ht;
  412. $subtot_paye_vat += $temp_vat;
  413. $x_paye_sum += $temp_vat;
  414. }
  415. }
  416. print '<td class="nowrap right"><span class="amount">' . price(price2num($x_paye_sum, 'MT')) . '</span></td>';
  417. $subtotalcoll = $subtotalcoll + $x_coll_sum;
  418. $subtotalpaid = $subtotalpaid + $x_paye_sum;
  419. $diff = $x_coll_sum - $x_paye_sum;
  420. $total = $total + $diff;
  421. $subtotal = price2num($subtotal + $diff, 'MT');
  422. print '<td class="nowrap right"><span class="amount">' . price(price2num($diff, 'MT')) . '</span></td>' . "\n";
  423. print "<td>&nbsp;</td>\n";
  424. print "</tr>\n";
  425. // Total
  426. $i++;
  427. $m++;
  428. if ($i > 2) {
  429. print '<tr class="liste_total">';
  430. print '<td class="right"><a href="quadri_detail.php?leftmenu=tax_vat&q=' . round($m / 3) . '&year=' . $y . '">' . $langs->trans("SubTotal") . '</a>:</td>';
  431. print '<td class="nowrap right">' . price(price2num($subtotalcoll, 'MT')) . '</td>';
  432. print '<td class="nowrap right">' . price(price2num($subtotalpaid, 'MT')) . '</td>';
  433. print '<td class="nowrap right">' . price(price2num($subtotal, 'MT')) . '</td>';
  434. print '<td>&nbsp;</td></tr>';
  435. $i = 0;
  436. $subtotalcoll = 0;
  437. $subtotalpaid = 0;
  438. $subtotal = 0;
  439. }
  440. }
  441. print '<tr class="liste_total"><td class="right" colspan="3">' . $langs->trans("TotalToPay") . ':</td><td class="nowrap right">' . price(price2num($total, 'MT')) . '</td>';
  442. print "<td>&nbsp;</td>\n";
  443. print '</tr>';
  444. print '</table>';
  445. print '</div><div class="fichetwothirdright">';
  446. /*
  447. * Paid
  448. */
  449. print load_fiche_titre($langs->trans("VATPaid"), '', '');
  450. $sql = '';
  451. $sql .= "SELECT SUM(amount) as mm, date_format(tva.datev,'%Y-%m') as dm, 'claimed' as mode";
  452. $sql .= " FROM " . MAIN_DB_PREFIX . "tva as tva";
  453. $sql .= " WHERE tva.entity = " . $conf->entity;
  454. $sql .= " AND (tva.datev >= '" . $db->idate($date_start) . "' AND tva.datev <= '" . $db->idate($date_end) . "')";
  455. $sql .= " GROUP BY dm";
  456. $sql .= " UNION ";
  457. $sql .= "SELECT SUM(ptva.amount) as mm, date_format(tva.datev,'%Y-%m') as dm, 'paid' as mode";
  458. $sql .= " FROM " . MAIN_DB_PREFIX . "tva as tva";
  459. $sql .= " INNER JOIN " . MAIN_DB_PREFIX . "payment_vat as ptva ON (tva.rowid = ptva.fk_tva)";
  460. $sql .= " WHERE tva.entity = " . $conf->entity;
  461. $sql .= " AND (tva.datev >= '" . $db->idate($date_start) . "' AND tva.datev <= '" . $db->idate($date_end) . "')";
  462. $sql .= " GROUP BY dm";
  463. $sql .= " ORDER BY dm ASC, mode ASC";
  464. //print $sql;
  465. pt($db, $sql, $langs->trans("Month"));
  466. print '</div>';
  467. }
  468. llxFooter();
  469. $db->close();