quadri_detail.php 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683
  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-2013 Laurent Destailleur <eldy@users.sourceforge.net>
  5. * Copyright (C) 2006-2007 Yannick Warnier <ywarnier@beeznest.org>
  6. * Copyright (C) 2014-2016 Juanjo Menent <jmenent@2byte.es>
  7. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  8. *
  9. * This program is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License as published by
  11. * the Free Software Foundation; either version 3 of the License, or
  12. * (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  21. */
  22. /**
  23. * \file htdocs/compta/tva/quadri_detail.php
  24. * \ingroup tax
  25. * \brief Local tax by rate
  26. */
  27. global $mysoc;
  28. // Load Dolibarr environment
  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. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  36. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  37. require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
  38. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  39. require_once DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
  40. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
  41. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/paymentexpensereport.class.php';
  42. // Load translation files required by the page
  43. $langs->loadLangs(array("other", "compta", "banks", "bills", "companies", "product", "trips", "admin"));
  44. $local = GETPOST('localTaxType', 'int');
  45. // Date range
  46. $year = GETPOST("year", "int");
  47. if (empty($year)) {
  48. $year_current = dol_print_date(dol_now('gmt'), "%Y", 'gmt');
  49. $year_start = $year_current;
  50. } else {
  51. $year_current = $year;
  52. $year_start = $year;
  53. }
  54. $date_start = dol_mktime(0, 0, 0, GETPOST("date_startmonth"), GETPOST("date_startday"), GETPOST("date_startyear"));
  55. $date_end = dol_mktime(23, 59, 59, GETPOST("date_endmonth"), GETPOST("date_endday"), GETPOST("date_endyear"));
  56. // Quarter
  57. if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
  58. $q = GETPOST("q", "int");
  59. if (empty($q)) {
  60. if (GETPOST("month", "int")) {
  61. $date_start = dol_get_first_day($year_start, GETPOST("month", "int"), false);
  62. $date_end = dol_get_last_day($year_start, GETPOST("month", "int"), false);
  63. } else {
  64. $date_start = dol_get_first_day($year_start, empty($conf->global->SOCIETE_FISCAL_MONTH_START) ? 1 : $conf->global->SOCIETE_FISCAL_MONTH_START, false);
  65. if (empty($conf->global->MAIN_INFO_VAT_RETURN) || $conf->global->MAIN_INFO_VAT_RETURN == 2) {
  66. $date_end = dol_time_plus_duree($date_start, 3, 'm') - 1;
  67. } elseif ($conf->global->MAIN_INFO_VAT_RETURN == 3) {
  68. $date_end = dol_time_plus_duree($date_start, 1, 'y') - 1;
  69. } elseif ($conf->global->MAIN_INFO_VAT_RETURN == 1) {
  70. $date_end = dol_time_plus_duree($date_start, 1, 'm') - 1;
  71. }
  72. }
  73. } else {
  74. if ($q == 1) {
  75. $date_start = dol_get_first_day($year_start, 1, false);
  76. $date_end = dol_get_last_day($year_start, 3, false);
  77. }
  78. if ($q == 2) {
  79. $date_start = dol_get_first_day($year_start, 4, false);
  80. $date_end = dol_get_last_day($year_start, 6, false);
  81. }
  82. if ($q == 3) {
  83. $date_start = dol_get_first_day($year_start, 7, false);
  84. $date_end = dol_get_last_day($year_start, 9, false);
  85. }
  86. if ($q == 4) {
  87. $date_start = dol_get_first_day($year_start, 10, false);
  88. $date_end = dol_get_last_day($year_start, 12, false);
  89. }
  90. }
  91. }
  92. $min = price2num(GETPOST("min", "alpha"));
  93. if (empty($min)) {
  94. $min = 0;
  95. }
  96. // Define modetax (0 or 1)
  97. // 0=normal, 1=option vat for services is on debit, 2=option on payments for products
  98. //$modetax = $conf->global->TAX_MODE;
  99. $calc = $conf->global->MAIN_INFO_LOCALTAX_CALC.$local;
  100. $modetax = $conf->global->$calc;
  101. if (GETPOSTISSET("modetax")) {
  102. $modetax = GETPOST("modetax", 'int');
  103. }
  104. if (empty($modetax)) {
  105. $modetax = 0;
  106. }
  107. // Security check
  108. $socid = GETPOST('socid', 'int');
  109. if ($user->socid) {
  110. $socid = $user->socid;
  111. }
  112. $result = restrictedArea($user, 'tax', '', '', 'charges');
  113. if (empty($local)) {
  114. accessforbidden('Parameter localTaxType is missing');
  115. exit;
  116. }
  117. /*
  118. * View
  119. */
  120. $form = new Form($db);
  121. $company_static = new Societe($db);
  122. $invoice_customer = new Facture($db);
  123. $invoice_supplier = new FactureFournisseur($db);
  124. $expensereport = new ExpenseReport($db);
  125. $product_static = new Product($db);
  126. $payment_static = new Paiement($db);
  127. $paymentfourn_static = new PaiementFourn($db);
  128. $paymentexpensereport_static = new PaymentExpenseReport($db);
  129. $morequerystring = '';
  130. $listofparams = array('date_startmonth', 'date_startyear', 'date_startday', 'date_endmonth', 'date_endyear', 'date_endday');
  131. foreach ($listofparams as $param) {
  132. if (GETPOST($param) != '') {
  133. $morequerystring .= ($morequerystring ? '&' : '').$param.'='.GETPOST($param);
  134. }
  135. }
  136. llxHeader('', $langs->trans("LocalTaxReport"), '', '', 0, 0, '', '', $morequerystring);
  137. $fsearch = '<!-- hidden fields for form -->';
  138. $fsearch .= '<input type="hidden" name="token" value="'.newToken().'">';
  139. $fsearch .= '<input type="hidden" name="modetax" value="'.$modetax.'">';
  140. $fsearch .= '<input type="hidden" name="localTaxType" value="'.$local.'">';
  141. $name = $langs->transcountry($local == 1 ? "LT1ReportByQuarters" : "LT2ReportByQuarters", $mysoc->country_code);
  142. $calcmode = '';
  143. if ($modetax == 0) {
  144. $calcmode = $langs->trans('OptionVATDefault');
  145. }
  146. if ($modetax == 1) {
  147. $calcmode = $langs->trans('OptionVATDebitOption');
  148. }
  149. if ($modetax == 2) {
  150. $calcmode = $langs->trans('OptionPaymentForProductAndServices');
  151. }
  152. $calcmode .= ' <span class="opacitymedium">('.$langs->trans("TaxModuleSetupToModifyRules", DOL_URL_ROOT.'/admin/taxes.php').')</span>';
  153. // Set period
  154. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
  155. $prevyear = $year_start;
  156. $prevquarter = $q;
  157. if ($prevquarter > 1) {
  158. $prevquarter--;
  159. } else {
  160. $prevquarter = 4;
  161. $prevyear--;
  162. }
  163. $nextyear = $year_start;
  164. $nextquarter = $q;
  165. if ($nextquarter < 4) {
  166. $nextquarter++;
  167. } else {
  168. $nextquarter = 1;
  169. $nextyear++;
  170. }
  171. $description .= $fsearch;
  172. $builddate = dol_now();
  173. /*if ($conf->global->TAX_MODE_SELL_PRODUCT == 'invoice') $description.=$langs->trans("RulesVATDueProducts");
  174. if ($conf->global->TAX_MODE_SELL_PRODUCT == 'payment') $description.=$langs->trans("RulesVATInProducts");
  175. if ($conf->global->TAX_MODE_SELL_SERVICE == 'invoice') $description.='<br>'.$langs->trans("RulesVATDueServices");
  176. if ($conf->global->TAX_MODE_SELL_SERVICE == 'payment') $description.='<br>'.$langs->trans("RulesVATInServices");
  177. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  178. $description.='<br>'.$langs->trans("DepositsAreNotIncluded");
  179. }
  180. if (! empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  181. $description.='<br>'.$langs->trans("SupplierDepositsAreNotIncluded");
  182. }
  183. */
  184. if (!empty($conf->global->MAIN_MODULE_ACCOUNTING)) {
  185. $description .= $langs->trans("ThisIsAnEstimatedValue");
  186. }
  187. // Customers invoices
  188. $elementcust = $langs->trans("CustomersInvoices");
  189. $productcust = $langs->trans("ProductOrService");
  190. $amountcust = $langs->trans("AmountHT");
  191. $vatcust = $langs->trans("VATReceived");
  192. $namecust = $langs->trans("Name");
  193. if ($mysoc->tva_assuj) {
  194. $vatcust .= ' ('.$langs->trans("StatusToPay").')';
  195. }
  196. // Suppliers invoices
  197. $elementsup = $langs->trans("SuppliersInvoices");
  198. $productsup = $productcust;
  199. $amountsup = $amountcust;
  200. $vatsup = $langs->trans("VATPaid");
  201. $namesup = $namecust;
  202. if ($mysoc->tva_assuj) {
  203. $vatsup .= ' ('.$langs->trans("ToGetBack").')';
  204. }
  205. report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink, array(), $calcmode);
  206. if ($local == 1) {
  207. $vatcust = $langs->transcountry("LT1", $mysoc->country_code);
  208. $vatsup = $langs->transcountry("LT1", $mysoc->country_code);
  209. $vatexpensereport = $langs->transcountry("LT1", $mysoc->country_code);
  210. } else {
  211. $vatcust = $langs->transcountry("LT2", $mysoc->country_code);
  212. $vatsup = $langs->transcountry("LT2", $mysoc->country_code);
  213. $vatexpensereport = $langs->transcountry("LT2", $mysoc->country_code);
  214. }
  215. // VAT Received and paid
  216. print '<div class="div-table-responsive">';
  217. echo '<table class="noborder centpercent">';
  218. $y = $year_current;
  219. $total = 0;
  220. $i = 0;
  221. $columns = 4;
  222. // Load arrays of datas
  223. $x_coll = tax_by_rate('localtax'.$local, $db, 0, 0, $date_start, $date_end, $modetax, 'sell');
  224. $x_paye = tax_by_rate('localtax'.$local, $db, 0, 0, $date_start, $date_end, $modetax, 'buy');
  225. if (!is_array($x_coll) || !is_array($x_paye)) {
  226. $langs->load("errors");
  227. if ($x_coll == -1) {
  228. print '<tr><td colspan="'.$columns.'">'.$langs->trans("ErrorNoAccountancyModuleLoaded").'</td></tr>';
  229. } elseif ($x_coll == -2) {
  230. print '<tr><td colspan="'.$columns.'">'.$langs->trans("FeatureNotYetAvailable").'</td></tr>';
  231. } else {
  232. print '<tr><td colspan="'.$columns.'">'.$langs->trans("Error").'</td></tr>';
  233. }
  234. } else {
  235. $x_both = array();
  236. //now, from these two arrays, get another array with one rate per line
  237. foreach (array_keys($x_coll) as $my_coll_rate) {
  238. $x_both[$my_coll_rate]['coll']['totalht'] = $x_coll[$my_coll_rate]['totalht'];
  239. $x_both[$my_coll_rate]['coll']['localtax'.$local] = $x_coll[$my_coll_rate]['localtax'.$local];
  240. $x_both[$my_coll_rate]['paye']['totalht'] = 0;
  241. $x_both[$my_coll_rate]['paye']['localtax'.$local] = 0;
  242. $x_both[$my_coll_rate]['coll']['links'] = '';
  243. $x_both[$my_coll_rate]['coll']['detail'] = array();
  244. foreach ($x_coll[$my_coll_rate]['facid'] as $id => $dummy) {
  245. $invoice_customer->id = $x_coll[$my_coll_rate]['facid'][$id];
  246. $invoice_customer->ref = $x_coll[$my_coll_rate]['facnum'][$id];
  247. $invoice_customer->type = $x_coll[$my_coll_rate]['type'][$id];
  248. $company_static->fetch($x_coll[$my_coll_rate]['company_id'][$id]);
  249. $x_both[$my_coll_rate]['coll']['detail'][] = array(
  250. 'id' =>$x_coll[$my_coll_rate]['facid'][$id],
  251. 'descr' =>$x_coll[$my_coll_rate]['descr'][$id],
  252. 'pid' =>$x_coll[$my_coll_rate]['pid'][$id],
  253. 'pref' =>$x_coll[$my_coll_rate]['pref'][$id],
  254. 'ptype' =>$x_coll[$my_coll_rate]['ptype'][$id],
  255. 'payment_id'=>$x_coll[$my_coll_rate]['payment_id'][$id],
  256. 'payment_amount'=>$x_coll[$my_coll_rate]['payment_amount'][$id],
  257. 'ftotal_ttc'=>$x_coll[$my_coll_rate]['ftotal_ttc'][$id],
  258. 'dtotal_ttc'=>$x_coll[$my_coll_rate]['dtotal_ttc'][$id],
  259. 'dtype' =>$x_coll[$my_coll_rate]['dtype'][$id],
  260. 'datef' =>$x_coll[$my_coll_rate]['datef'][$id],
  261. 'datep' =>$x_coll[$my_coll_rate]['datep'][$id],
  262. 'company_link'=>$company_static->getNomUrl(1, '', 20),
  263. 'ddate_start'=>$x_coll[$my_coll_rate]['ddate_start'][$id],
  264. 'ddate_end' =>$x_coll[$my_coll_rate]['ddate_end'][$id],
  265. 'totalht' =>$x_coll[$my_coll_rate]['totalht_list'][$id],
  266. 'localtax1'=> $x_coll[$my_coll_rate]['localtax1_list'][$id],
  267. 'localtax2'=> $x_coll[$my_coll_rate]['localtax2_list'][$id],
  268. 'vat' =>$x_coll[$my_coll_rate]['vat_list'][$id],
  269. 'link' =>$invoice_customer->getNomUrl(1, '', 12)
  270. );
  271. }
  272. }
  273. // tva paid
  274. foreach (array_keys($x_paye) as $my_paye_rate) {
  275. $x_both[$my_paye_rate]['paye']['totalht'] = $x_paye[$my_paye_rate]['totalht'];
  276. $x_both[$my_paye_rate]['paye']['vat'] = $x_paye[$my_paye_rate]['vat'];
  277. if (!isset($x_both[$my_paye_rate]['coll']['totalht'])) {
  278. $x_both[$my_paye_rate]['coll']['totalht'] = 0;
  279. $x_both[$my_paye_rate]['coll']['vat'] = 0;
  280. }
  281. $x_both[$my_paye_rate]['paye']['links'] = '';
  282. $x_both[$my_paye_rate]['paye']['detail'] = array();
  283. foreach ($x_paye[$my_paye_rate]['facid'] as $id => $dummy) {
  284. $invoice_supplier->id = $x_paye[$my_paye_rate]['facid'][$id];
  285. $invoice_supplier->ref = $x_paye[$my_paye_rate]['facnum'][$id];
  286. $invoice_supplier->type = $x_paye[$my_paye_rate]['type'][$id];
  287. $x_both[$my_paye_rate]['paye']['detail'][] = array(
  288. 'id' =>$x_paye[$my_paye_rate]['facid'][$id],
  289. 'descr' =>$x_paye[$my_paye_rate]['descr'][$id],
  290. 'pid' =>$x_paye[$my_paye_rate]['pid'][$id],
  291. 'pref' =>$x_paye[$my_paye_rate]['pref'][$id],
  292. 'ptype' =>$x_paye[$my_paye_rate]['ptype'][$id],
  293. 'payment_id'=>$x_paye[$my_paye_rate]['payment_id'][$id],
  294. 'payment_amount'=>$x_paye[$my_paye_rate]['payment_amount'][$id],
  295. 'ftotal_ttc'=>price2num($x_paye[$my_paye_rate]['ftotal_ttc'][$id]),
  296. 'dtotal_ttc'=>price2num($x_paye[$my_paye_rate]['dtotal_ttc'][$id]),
  297. 'dtype' =>$x_paye[$my_paye_rate]['dtype'][$id],
  298. 'datef' =>$x_paye[$my_paye_rate]['datef'][$id],
  299. 'datep' =>$x_paye[$my_paye_rate]['datep'][$id],
  300. 'company_link'=>$company_static->getNomUrl(1, '', 20),
  301. 'ddate_start'=>$x_paye[$my_paye_rate]['ddate_start'][$id],
  302. 'ddate_end' =>$x_paye[$my_paye_rate]['ddate_end'][$id],
  303. 'totalht' =>price2num($x_paye[$my_paye_rate]['totalht_list'][$id]),
  304. 'localtax1'=> $x_paye[$my_paye_rate]['localtax1_list'][$id],
  305. 'localtax2'=> $x_paye[$my_paye_rate]['localtax2_list'][$id],
  306. 'vat' =>$x_paye[$my_paye_rate]['vat_list'][$id],
  307. 'link' =>$invoice_supplier->getNomUrl(1, '', 12)
  308. );
  309. }
  310. }
  311. //now we have an array (x_both) indexed by rates for coll and paye
  312. //print table headers for this quadri - incomes first
  313. $x_coll_sum = 0;
  314. $x_coll_ht = 0;
  315. $x_paye_sum = 0;
  316. $x_paye_ht = 0;
  317. $span = $columns - 1;
  318. if ($modetax != 2) {
  319. $span += 1;
  320. }
  321. if ($modetax != 1) {
  322. $span += 1;
  323. }
  324. // Customers invoices
  325. print '<tr class="liste_titre">';
  326. print '<td class="left">'.$elementcust.'</td>';
  327. print '<td class="left">'.$productcust.'</td>';
  328. if ($modetax != 2) {
  329. print '<td class="right">'.$amountcust.'</td>';
  330. }
  331. if ($modetax != 1) {
  332. print '<td class="right">'.$langs->trans("Payment").' ('.$langs->trans("PercentOfInvoice").')</td>';
  333. }
  334. print '<td class="right">'.$langs->trans("BI").'</td>';
  335. print '<td class="right">'.$vatcust.'</td>';
  336. print '</tr>';
  337. $LT = 0;
  338. $sameLT = false;
  339. foreach (array_keys($x_coll) as $rate) {
  340. $subtot_coll_total_ht = 0;
  341. $subtot_coll_vat = 0;
  342. if (is_array($x_both[$rate]['coll']['detail'])) {
  343. // VAT Rate
  344. if ($rate != 0) {
  345. print "<tr>";
  346. print '<td class="tax_rate">'.$langs->trans("Rate").': '.vatrate($rate).'%</td><td colspan="'.$span.'"></td>';
  347. print '</tr>'."\n";
  348. }
  349. foreach ($x_both[$rate]['coll']['detail'] as $index => $fields) {
  350. if (($local == 1 && $fields['localtax1'] != 0) || ($local == 2 && $fields['localtax2'] != 0)) {
  351. // Define type
  352. $type = ($fields['dtype'] ? $fields['dtype'] : $fields['ptype']);
  353. // Try to enhance type detection using date_start and date_end for free lines where type
  354. // was not saved.
  355. if (!empty($fields['ddate_start'])) {
  356. $type = 1;
  357. }
  358. if (!empty($fields['ddate_end'])) {
  359. $type = 1;
  360. }
  361. print '<tr class="oddeven">';
  362. // Ref
  363. print '<td class="nowrap left">'.$fields['link'].'</td>';
  364. // Description
  365. print '<td class="left">';
  366. if ($fields['pid']) {
  367. $product_static->id = $fields['pid'];
  368. $product_static->ref = $fields['pref'];
  369. $product_static->type = $fields['ptype'];
  370. print $product_static->getNomUrl(1);
  371. if (dol_string_nohtmltag($fields['descr'])) {
  372. print ' - '.dol_trunc(dol_string_nohtmltag($fields['descr']), 16);
  373. }
  374. } else {
  375. if ($type) {
  376. $text = img_object($langs->trans('Service'), 'service');
  377. } else {
  378. $text = img_object($langs->trans('Product'), 'product');
  379. }
  380. if (preg_match('/^\((.*)\)$/', $fields['descr'], $reg)) {
  381. if ($reg[1] == 'DEPOSIT') {
  382. $fields['descr'] = $langs->transnoentitiesnoconv('Deposit');
  383. } elseif ($reg[1] == 'CREDIT_NOTE') {
  384. $fields['descr'] = $langs->transnoentitiesnoconv('CreditNote');
  385. } else {
  386. $fields['descr'] = $langs->transnoentitiesnoconv($reg[1]);
  387. }
  388. }
  389. print $text.' '.dol_trunc(dol_string_nohtmltag($fields['descr']), 16);
  390. // Show range
  391. print_date_range($fields['ddate_start'], $fields['ddate_end']);
  392. }
  393. print '</td>';
  394. // Total HT
  395. if ($modetax != 2) {
  396. print '<td class="nowrap right">';
  397. print price($fields['totalht']);
  398. if (price2num($fields['ftotal_ttc'])) {
  399. $ratiolineinvoice = ($fields['dtotal_ttc'] / $fields['ftotal_ttc']);
  400. }
  401. print '</td>';
  402. }
  403. // Payment
  404. $ratiopaymentinvoice = 1;
  405. if ($modetax != 1) {
  406. if (isset($fields['payment_amount']) && $fields['ftotal_ttc']) {
  407. $ratiopaymentinvoice = ($fields['payment_amount'] / $fields['ftotal_ttc']);
  408. }
  409. print '<td class="nowrap right">';
  410. if ($fields['payment_amount'] && $fields['ftotal_ttc']) {
  411. $payment_static->id = $fields['payment_id'];
  412. print $payment_static->getNomUrl(2);
  413. }
  414. if ($type == 0) {
  415. print $langs->trans("NotUsedForGoods");
  416. } else {
  417. print price($fields['payment_amount']);
  418. if (isset($fields['payment_amount'])) {
  419. print ' ('.round($ratiopaymentinvoice * 100, 2).'%)';
  420. }
  421. }
  422. print '</td>';
  423. }
  424. // Total collected
  425. print '<td class="nowrap right">';
  426. $temp_ht = $fields['totalht'];
  427. if ($type == 1) {
  428. $temp_ht = $fields['totalht'] * $ratiopaymentinvoice;
  429. }
  430. print price(price2num($temp_ht, 'MT'));
  431. print '</td>';
  432. // Localtax
  433. print '<td class="nowrap right">';
  434. $temp_vat = $local == 1 ? $fields['localtax1'] : $fields['localtax2'];
  435. print price(price2num($temp_vat, 'MT'));
  436. //print price($fields['vat']);
  437. print '</td>';
  438. print '</tr>';
  439. $subtot_coll_total_ht += $temp_ht;
  440. $subtot_coll_vat += $temp_vat;
  441. $x_coll_sum += $temp_vat;
  442. }
  443. }
  444. }
  445. // Total customers for this vat rate
  446. print '<tr class="liste_total">';
  447. print '<td></td>';
  448. print '<td class="right">'.$langs->trans("Total").':</td>';
  449. if ($modetax != 1) {
  450. print '<td class="nowrap right">&nbsp;</td>';
  451. print '<td class="right">&nbsp;</td>';
  452. }
  453. print '<td class="right"><span class="amount">'.price(price2num($subtot_coll_total_ht, 'MT')).'</span></td>';
  454. print '<td class="nowrap right"><span class="amount">'.price(price2num($subtot_coll_vat, 'MT')).'</span></td>';
  455. print '</tr>';
  456. }
  457. if (count($x_coll) == 0) { // Show a total ine if nothing shown
  458. print '<tr class="liste_total">';
  459. print '<td>&nbsp;</td>';
  460. print '<td class="right">'.$langs->trans("Total").':</td>';
  461. if ($modetax == 0) {
  462. print '<td class="nowrap right">&nbsp;</td>';
  463. print '<td class="right">&nbsp;</td>';
  464. }
  465. print '<td class="right"><span class="amount">'.price(price2num(0, 'MT')).'</span></td>';
  466. print '<td class="nowrap right"><span class="amount">'.price(price2num(0, 'MT')).'</span></td>';
  467. print '</tr>';
  468. }
  469. // Blank line
  470. print '<tr><td colspan="'.($span + 1).'">&nbsp;</td></tr>';
  471. //print '</table>';
  472. $diff = $x_coll_sum;
  473. //echo '<table class="noborder centpercent">';
  474. //print table headers for this quadri - expenses now
  475. print '<tr class="liste_titre">';
  476. print '<td class="left">'.$elementsup.'</td>';
  477. print '<td class="left">'.$productsup.'</td>';
  478. if ($modetax != 1) {
  479. print '<td class="right">'.$amountsup.'</td>';
  480. print '<td class="right">'.$langs->trans("Payment").' ('.$langs->trans("PercentOfInvoice").')</td>';
  481. }
  482. print '<td class="right">'.$langs->trans("BI").'</td>';
  483. print '<td class="right">'.$vatsup.'</td>';
  484. print '</tr>'."\n";
  485. foreach (array_keys($x_paye) as $rate) {
  486. $subtot_paye_total_ht = 0;
  487. $subtot_paye_vat = 0;
  488. if (is_array($x_both[$rate]['paye']['detail'])) {
  489. if ($rate != 0) {
  490. print "<tr>";
  491. print '<td class="tax_rate">'.$langs->trans("Rate").': '.vatrate($rate).'%</td><td colspan="'.$span.'"></td>';
  492. print '</tr>'."\n";
  493. }
  494. foreach ($x_both[$rate]['paye']['detail'] as $index => $fields) {
  495. if (($local == 1 && $fields['localtax1'] != 0) || ($local == 2 && $fields['localtax2'] != 0)) {
  496. // Define type
  497. $type = ($fields['dtype'] ? $fields['dtype'] : $fields['ptype']);
  498. // Try to enhance type detection using date_start and date_end for free lines where type
  499. // was not saved.
  500. if (!empty($fields['ddate_start'])) {
  501. $type = 1;
  502. }
  503. if (!empty($fields['ddate_end'])) {
  504. $type = 1;
  505. }
  506. print '<tr class="oddeven">';
  507. // Ref
  508. print '<td class="nowrap left">'.$fields['link'].'</td>';
  509. // Description
  510. print '<td class="left">';
  511. if ($fields['pid']) {
  512. $product_static->id = $fields['pid'];
  513. $product_static->ref = $fields['pref'];
  514. $product_static->type = $fields['ptype'];
  515. print $product_static->getNomUrl(1);
  516. if (dol_string_nohtmltag($fields['descr'])) {
  517. print ' - '.dol_trunc(dol_string_nohtmltag($fields['descr']), 16);
  518. }
  519. } else {
  520. if ($type) {
  521. $text = img_object($langs->trans('Service'), 'service');
  522. } else {
  523. $text = img_object($langs->trans('Product'), 'product');
  524. }
  525. print $text.' '.dol_trunc(dol_string_nohtmltag($fields['descr']), 16);
  526. // Show range
  527. print_date_range($fields['ddate_start'], $fields['ddate_end']);
  528. }
  529. print '</td>';
  530. // Total HT
  531. if ($modetax != 2) {
  532. print '<td class="nowrap right">';
  533. print price($fields['totalht']);
  534. if (price2num($fields['ftotal_ttc'])) {
  535. //print $fields['dtotal_ttc']."/".$fields['ftotal_ttc']." - ";
  536. $ratiolineinvoice = ($fields['dtotal_ttc'] / $fields['ftotal_ttc']);
  537. //print ' ('.round($ratiolineinvoice*100,2).'%)';
  538. }
  539. print '</td>';
  540. }
  541. // Payment
  542. $ratiopaymentinvoice = 1;
  543. if ($modetax != 1) {
  544. print '<td class="nowrap right">';
  545. if (isset($fields['payment_amount']) && $fields['ftotal_ttc']) {
  546. $ratiopaymentinvoice = ($fields['payment_amount'] / $fields['ftotal_ttc']);
  547. }
  548. if ($fields['payment_amount'] && $fields['ftotal_ttc']) {
  549. $paymentfourn_static->id = $fields['payment_id'];
  550. print $paymentfourn_static->getNomUrl(2);
  551. }
  552. if ($type == 0) {
  553. print $langs->trans("NA");
  554. } else {
  555. print price(price2num($fields['payment_amount'], 'MT'));
  556. if (isset($fields['payment_amount'])) {
  557. print ' ('.round($ratiopaymentinvoice * 100, 2).'%)';
  558. }
  559. }
  560. print '</td>';
  561. }
  562. // VAT paid
  563. print '<td class="nowrap right">';
  564. $temp_ht = $fields['totalht'] * $ratiopaymentinvoice;
  565. print price(price2num($temp_ht, 'MT'), 1);
  566. print '</td>';
  567. // Localtax
  568. print '<td class="nowrap right">';
  569. $temp_vat = ($local == 1 ? $fields['localtax1'] : $fields['localtax2']) * $ratiopaymentinvoice;
  570. print price(price2num($temp_vat, 'MT'), 1);
  571. //print price($fields['vat']);
  572. print '</td>';
  573. print '</tr>';
  574. $subtot_paye_total_ht += $temp_ht;
  575. $subtot_paye_vat += $temp_vat;
  576. $x_paye_sum += $temp_vat;
  577. }
  578. }
  579. }
  580. // Total suppliers for this vat rate
  581. print '<tr class="liste_total">';
  582. print '<td>&nbsp;</td>';
  583. print '<td class="right">'.$langs->trans("Total").':</td>';
  584. if ($modetax != 1) {
  585. print '<td class="nowrap right">&nbsp;</td>';
  586. print '<td class="right">&nbsp;</td>';
  587. }
  588. print '<td class="right"><span class="amount">'.price(price2num($subtot_paye_total_ht, 'MT')).'</span></td>';
  589. print '<td class="nowrap right"><span class="amount">'.price(price2num($subtot_paye_vat, 'MT')).'</span></td>';
  590. print '</tr>';
  591. }
  592. if (count($x_paye) == 0) { // Show a total line if nothing shown
  593. print '<tr class="liste_total">';
  594. print '<td>&nbsp;</td>';
  595. print '<td class="right">'.$langs->trans("Total").':</td>';
  596. if ($modetax != 1) {
  597. print '<td class="nowrap right">&nbsp;</td>';
  598. print '<td class="right">&nbsp;</td>';
  599. }
  600. print '<td class="right"><span class="amount">'.price(price2num(0, 'MT')).'</span></td>';
  601. print '<td class="nowrap right"><span class="amount">'.price(price2num(0, 'MT')).'</span></td>';
  602. print '</tr>';
  603. }
  604. // Total to pay
  605. $diff = $x_coll_sum - $x_paye_sum;
  606. print '<tr class="liste_total">';
  607. print '<td class="liste_total" colspan="'.$span.'">'.$langs->trans("TotalToPay").($q ? ', '.$langs->trans("Quadri").' '.$q : '').'</td>';
  608. print '<td class="liste_total nowrap right"><b>'.price(price2num($diff, 'MT'))."</b></td>\n";
  609. print "</tr>\n";
  610. $i++;
  611. }
  612. print '</table>';
  613. print '</div>';
  614. // End of page
  615. llxFooter();
  616. $db->close();