quadri_detail.php 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875
  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-2015 Yannick Warnier <ywarnier@beeznest.org>
  6. * Copyright (C) 2014 Ferran Marcet <fmarcet@2byte.es>
  7. * Copyright (C) 2018-2021 Frédéric France <frederic.france@netlogic.fr>
  8. * Copyright (C) 2019 Eric Seigne <eric.seigne@cap-rel.fr>
  9. * Copyright (C) 2021-2022 Open-Dsi <support@open-dsi.fr>
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 3 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License
  22. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  23. */
  24. /**
  25. * \file htdocs/compta/tva/quadri_detail.php
  26. * \ingroup tax
  27. * \brief VAT by rate
  28. */
  29. // Load Dolibarr environment
  30. require '../../main.inc.php';
  31. require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
  32. require_once DOL_DOCUMENT_ROOT.'/core/lib/tax.lib.php';
  33. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  34. require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
  35. require_once DOL_DOCUMENT_ROOT.'/compta/localtax/class/localtax.class.php';
  36. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  37. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  38. require_once DOL_DOCUMENT_ROOT.'/compta/paiement/class/paiement.class.php';
  39. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.facture.class.php';
  40. require_once DOL_DOCUMENT_ROOT.'/fourn/class/paiementfourn.class.php';
  41. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/expensereport.class.php';
  42. require_once DOL_DOCUMENT_ROOT.'/expensereport/class/paymentexpensereport.class.php';
  43. // Load translation files required by the page
  44. $langs->loadLangs(array("other", "compta", "banks", "bills", "companies", "product", "trips", "admin"));
  45. $refresh = (GETPOSTISSET('submit') || GETPOSTISSET('vat_rate_show') || GETPOSTISSET('invoice_type')) ? true : false;
  46. $invoice_type = GETPOSTISSET('invoice_type') ? GETPOST('invoice_type', 'alpha') : '';
  47. $vat_rate_show = GETPOSTISSET('vat_rate_show') ? GETPOST('vat_rate_show', 'alphanohtml') : -1;
  48. include DOL_DOCUMENT_ROOT.'/compta/tva/initdatesforvat.inc.php';
  49. $min = price2num(GETPOST("min", "alpha"));
  50. if (empty($min)) {
  51. $min = 0;
  52. }
  53. // Define modetax (0 or 1)
  54. // 0=normal, 1=option vat for services is on debit, 2=option on payments for products
  55. $modetax = (empty($conf->global->TAX_MODE) ? 0 : $conf->global->TAX_MODE);
  56. if (GETPOSTISSET("modetax")) {
  57. $modetax = GETPOSTINT("modetax");
  58. }
  59. if (empty($modetax)) {
  60. $modetax = 0;
  61. }
  62. $object = new Tva($db);
  63. // Security check
  64. $socid = GETPOSTINT('socid');
  65. if ($user->socid) {
  66. $socid = $user->socid;
  67. }
  68. $result = restrictedArea($user, 'tax', '', 'tva', 'charges');
  69. /*
  70. * View
  71. */
  72. $form = new Form($db);
  73. $company_static = new Societe($db);
  74. $invoice_customer = new Facture($db);
  75. $invoice_supplier = new FactureFournisseur($db);
  76. $expensereport = new ExpenseReport($db);
  77. $product_static = new Product($db);
  78. $payment_static = new Paiement($db);
  79. $paymentfourn_static = new PaiementFourn($db);
  80. $paymentexpensereport_static = new PaymentExpenseReport($db);
  81. $morequerystring = '';
  82. $listofparams = array('date_startmonth', 'date_startyear', 'date_startday', 'date_endmonth', 'date_endyear', 'date_endday');
  83. foreach ($listofparams as $param) {
  84. if (GETPOST($param) != '') {
  85. $morequerystring .= ($morequerystring ? '&' : '').$param.'='.GETPOST($param);
  86. }
  87. }
  88. $title = $langs->trans("VATReport")." ".dol_print_date($date_start, '', 'tzserver')." -> ".dol_print_date($date_end, '', 'tzserver');
  89. llxHeader('', $title, '', '', 0, 0, '', '', $morequerystring);
  90. //print load_fiche_titre($langs->trans("VAT"),"");
  91. //$fsearch.='<br>';
  92. $fsearch = '<!-- hidden fields for form -->';
  93. $fsearch .= '<input type="hidden" name="token" value="'.newToken().'">';
  94. $fsearch .= '<input type="hidden" name="modetax" value="'.$modetax.'">';
  95. //$fsearch.=' '.$langs->trans("SalesTurnoverMinimum").': ';
  96. //$fsearch.=' <input type="text" name="min" value="'.$min.'">';
  97. // Show report header
  98. $name = $langs->trans("VATReportByRates");
  99. $calcmode = '';
  100. if ($modetax == 0) {
  101. $calcmode = $langs->trans('OptionVATDefault');
  102. }
  103. if ($modetax == 1) {
  104. $calcmode = $langs->trans('OptionVATDebitOption');
  105. }
  106. if ($modetax == 2) {
  107. $calcmode = $langs->trans('OptionPaymentForProductAndServices');
  108. }
  109. $calcmode .= ' <span class="opacitymedium">('.$langs->trans("TaxModuleSetupToModifyRules", DOL_URL_ROOT.'/admin/taxes.php').')</span>';
  110. // Set period
  111. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0, 0, '', '', '', '', 1, '', '', 'tzserver');
  112. $period .= ' - ';
  113. $period .= $form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0, 0, '', '', '', '', 1, '', '', 'tzserver');
  114. $prevyear = $date_start_year;
  115. $prevquarter = $q;
  116. if ($prevquarter > 1) {
  117. $prevquarter--;
  118. } else {
  119. $prevquarter = 4;
  120. $prevyear--;
  121. }
  122. $nextyear = $date_start_year;
  123. $nextquarter = $q;
  124. if ($nextquarter < 4) {
  125. $nextquarter++;
  126. } else {
  127. $nextquarter = 1;
  128. $nextyear++;
  129. }
  130. $description .= $fsearch;
  131. $builddate = dol_now();
  132. if ($conf->global->TAX_MODE_SELL_PRODUCT == 'invoice') {
  133. $description .= $langs->trans("RulesVATDueProducts");
  134. }
  135. if ($conf->global->TAX_MODE_SELL_PRODUCT == 'payment') {
  136. $description .= $langs->trans("RulesVATInProducts");
  137. }
  138. if ($conf->global->TAX_MODE_SELL_SERVICE == 'invoice') {
  139. $description .= '<br>'.$langs->trans("RulesVATDueServices");
  140. }
  141. if ($conf->global->TAX_MODE_SELL_SERVICE == 'payment') {
  142. $description .= '<br>'.$langs->trans("RulesVATInServices");
  143. }
  144. if (!empty($conf->global->FACTURE_DEPOSITS_ARE_JUST_PAYMENTS)) {
  145. $description .= '<br>'.$langs->trans("DepositsAreNotIncluded");
  146. }
  147. if (!empty($conf->global->FACTURE_SUPPLIER_DEPOSITS_ARE_JUST_PAYMENTS)) {
  148. $description .= $langs->trans("SupplierDepositsAreNotIncluded");
  149. }
  150. if (!empty($conf->global->MAIN_MODULE_ACCOUNTING)) {
  151. $description .= '<br>'.$langs->trans("ThisIsAnEstimatedValue");
  152. }
  153. // Customers invoices
  154. $elementcust = $langs->trans("CustomersInvoices");
  155. $productcust = $langs->trans("ProductOrService");
  156. $amountcust = $langs->trans("AmountHT");
  157. $vatcust = $langs->trans("VATReceived");
  158. $namecust = $langs->trans("Name");
  159. if ($mysoc->tva_assuj) {
  160. $vatcust .= ' ('.$langs->trans("VATToPay").')';
  161. }
  162. // Suppliers invoices
  163. $elementsup = $langs->trans("SuppliersInvoices");
  164. $productsup = $productcust;
  165. $amountsup = $amountcust;
  166. $vatsup = $langs->trans("VATPaid");
  167. $namesup = $namecust;
  168. if ($mysoc->tva_assuj) {
  169. $vatsup .= ' ('.$langs->trans("ToGetBack").')';
  170. }
  171. $optioncss = GETPOST('optioncss', 'alpha');
  172. if ($optioncss != "print") {
  173. report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink, array(), $calcmode);
  174. }
  175. $vatcust = $langs->trans("VATReceived");
  176. $vatsup = $langs->trans("VATPaid");
  177. $vatexpensereport = $langs->trans("VATPaid");
  178. // VAT Received and paid
  179. print '<div class="div-table-responsive">';
  180. print '<table class="noborder centpercent">';
  181. $y = $year_current;
  182. $i = 0;
  183. $columns = 7;
  184. $span = $columns;
  185. if ($modetax != 1) {
  186. $span += 2;
  187. }
  188. // Load arrays of datas
  189. $x_coll = tax_by_rate('vat', $db, 0, 0, $date_start, $date_end, $modetax, 'sell');
  190. $x_paye = tax_by_rate('vat', $db, 0, 0, $date_start, $date_end, $modetax, 'buy');
  191. if (!is_array($x_coll) || !is_array($x_paye)) {
  192. $langs->load("errors");
  193. if ($x_coll == -1) {
  194. print '<tr><td colspan="'.$columns.'">'.$langs->trans("ErrorNoAccountancyModuleLoaded").'</td></tr>';
  195. } elseif ($x_coll == -2) {
  196. print '<tr><td colspan="'.$columns.'">'.$langs->trans("FeatureNotYetAvailable").'</td></tr>';
  197. } else {
  198. print '<tr><td colspan="'.$columns.'">'.$langs->trans("Error").'</td></tr>';
  199. }
  200. } else {
  201. $x_both = array();
  202. //now, from these two arrays, get another array with one rate per line
  203. foreach (array_keys($x_coll) as $my_coll_rate) {
  204. $x_both[$my_coll_rate]['coll']['totalht'] = $x_coll[$my_coll_rate]['totalht'];
  205. $x_both[$my_coll_rate]['coll']['vat'] = $x_coll[$my_coll_rate]['vat'];
  206. $x_both[$my_coll_rate]['paye']['totalht'] = 0;
  207. $x_both[$my_coll_rate]['paye']['vat'] = 0;
  208. $x_both[$my_coll_rate]['coll']['links'] = '';
  209. $x_both[$my_coll_rate]['coll']['detail'] = array();
  210. foreach ($x_coll[$my_coll_rate]['facid'] as $id => $dummy) {
  211. $invoice_customer->id = $x_coll[$my_coll_rate]['facid'][$id];
  212. $invoice_customer->ref = $x_coll[$my_coll_rate]['facnum'][$id];
  213. $invoice_customer->type = $x_coll[$my_coll_rate]['type'][$id];
  214. //$company_static->fetch($x_coll[$my_coll_rate]['company_id'][$id]);
  215. $company_static->id = $x_coll[$my_coll_rate]['company_id'][$id];
  216. $company_static->name = $x_coll[$my_coll_rate]['company_name'][$id];
  217. $company_static->name_alias = $x_coll[$my_coll_rate]['company_alias'][$id];
  218. $company_static->email = $x_coll[$my_coll_rate]['company_email'][$id];
  219. $company_static->tva_intra = $x_coll[$my_coll_rate]['tva_intra'][$id];
  220. $company_static->client = $x_coll[$my_coll_rate]['company_client'][$id];
  221. $company_static->fournisseur = $x_coll[$my_coll_rate]['company_fournisseur'][$id];
  222. $company_static->status = $x_coll[$my_coll_rate]['company_status'][$id];
  223. $company_static->code_client = $x_coll[$my_coll_rate]['company_customer_code'][$id];
  224. $company_static->code_compta_client = $x_coll[$my_coll_rate]['company_customer_accounting_code'][$id];
  225. $company_static->code_fournisseur = $x_coll[$my_coll_rate]['company_supplier_code'][$id];
  226. $company_static->code_compta_fournisseur = $x_coll[$my_coll_rate]['company_supplier_accounting_code'][$id];
  227. $x_both[$my_coll_rate]['coll']['detail'][] = array(
  228. 'id' =>$x_coll[$my_coll_rate]['facid'][$id],
  229. 'descr' =>$x_coll[$my_coll_rate]['descr'][$id],
  230. 'pid' =>$x_coll[$my_coll_rate]['pid'][$id],
  231. 'pref' =>$x_coll[$my_coll_rate]['pref'][$id],
  232. 'ptype' =>$x_coll[$my_coll_rate]['ptype'][$id],
  233. 'payment_id'=>$x_coll[$my_coll_rate]['payment_id'][$id],
  234. 'payment_ref'=>$x_coll[$my_coll_rate]['payment_ref'][$id],
  235. 'payment_amount'=>$x_coll[$my_coll_rate]['payment_amount'][$id],
  236. 'ftotal_ttc'=>$x_coll[$my_coll_rate]['ftotal_ttc'][$id],
  237. 'dtotal_ttc'=>$x_coll[$my_coll_rate]['dtotal_ttc'][$id],
  238. 'dtype' =>$x_coll[$my_coll_rate]['dtype'][$id],
  239. 'datef' =>$x_coll[$my_coll_rate]['datef'][$id],
  240. 'datep' =>$x_coll[$my_coll_rate]['datep'][$id],
  241. 'company_link'=>$company_static->getNomUrl(1, '', 20),
  242. 'ddate_start'=>$x_coll[$my_coll_rate]['ddate_start'][$id],
  243. 'ddate_end' =>$x_coll[$my_coll_rate]['ddate_end'][$id],
  244. 'totalht' =>$x_coll[$my_coll_rate]['totalht_list'][$id],
  245. 'vat' =>$x_coll[$my_coll_rate]['vat_list'][$id],
  246. 'link' =>$invoice_customer->getNomUrl(1, '', 12)
  247. );
  248. }
  249. }
  250. // tva paid
  251. foreach (array_keys($x_paye) as $my_paye_rate) {
  252. $x_both[$my_paye_rate]['paye']['totalht'] = $x_paye[$my_paye_rate]['totalht'];
  253. $x_both[$my_paye_rate]['paye']['vat'] = $x_paye[$my_paye_rate]['vat'];
  254. if (!isset($x_both[$my_paye_rate]['coll']['totalht'])) {
  255. $x_both[$my_paye_rate]['coll']['totalht'] = 0;
  256. $x_both[$my_paye_rate]['coll']['vat'] = 0;
  257. }
  258. $x_both[$my_paye_rate]['paye']['links'] = '';
  259. $x_both[$my_paye_rate]['paye']['detail'] = array();
  260. foreach ($x_paye[$my_paye_rate]['facid'] as $id => $dummy) {
  261. // ExpenseReport
  262. if ($x_paye[$my_paye_rate]['ptype'][$id] == 'ExpenseReportPayment') {
  263. $expensereport->id = $x_paye[$my_paye_rate]['facid'][$id];
  264. $expensereport->ref = $x_paye[$my_paye_rate]['facnum'][$id];
  265. $expensereport->type = $x_paye[$my_paye_rate]['type'][$id];
  266. $x_both[$my_paye_rate]['paye']['detail'][] = array(
  267. 'id' =>$x_paye[$my_paye_rate]['facid'][$id],
  268. 'descr' =>$x_paye[$my_paye_rate]['descr'][$id],
  269. 'pid' =>$x_paye[$my_paye_rate]['pid'][$id],
  270. 'pref' =>$x_paye[$my_paye_rate]['pref'][$id],
  271. 'ptype' =>$x_paye[$my_paye_rate]['ptype'][$id],
  272. 'payment_id' =>$x_paye[$my_paye_rate]['payment_id'][$id],
  273. 'payment_ref' =>$x_paye[$my_paye_rate]['payment_ref'][$id],
  274. 'payment_amount' =>$x_paye[$my_paye_rate]['payment_amount'][$id],
  275. 'ftotal_ttc' =>price2num($x_paye[$my_paye_rate]['ftotal_ttc'][$id]),
  276. 'dtotal_ttc' =>price2num($x_paye[$my_paye_rate]['dtotal_ttc'][$id]),
  277. 'dtype' =>$x_paye[$my_paye_rate]['dtype'][$id],
  278. 'ddate_start' =>$x_paye[$my_paye_rate]['ddate_start'][$id],
  279. 'ddate_end' =>$x_paye[$my_paye_rate]['ddate_end'][$id],
  280. 'totalht' =>price2num($x_paye[$my_paye_rate]['totalht_list'][$id]),
  281. 'vat' =>$x_paye[$my_paye_rate]['vat_list'][$id],
  282. 'link' =>$expensereport->getNomUrl(1)
  283. );
  284. } else {
  285. $invoice_supplier->id = $x_paye[$my_paye_rate]['facid'][$id];
  286. $invoice_supplier->ref = $x_paye[$my_paye_rate]['facnum'][$id];
  287. $invoice_supplier->type = $x_paye[$my_paye_rate]['type'][$id];
  288. $company_static->id = $x_paye[$my_paye_rate]['company_id'][$id];
  289. $company_static->name = $x_paye[$my_paye_rate]['company_name'][$id];
  290. $company_static->name_alias = $x_paye[$my_paye_rate]['company_alias'][$id];
  291. $company_static->email = $x_paye[$my_paye_rate]['company_email'][$id];
  292. $company_static->tva_intra = $x_paye[$my_paye_rate]['tva_intra'][$id];
  293. $company_static->client = $x_paye[$my_paye_rate]['company_client'][$id];
  294. $company_static->fournisseur = $x_paye[$my_paye_rate]['company_fournisseur'][$id];
  295. $company_static->status = $x_paye[$my_paye_rate]['company_status'][$id];
  296. $company_static->code_client = $x_paye[$my_paye_rate]['company_customer_code'][$id];
  297. $company_static->code_compta_client = $x_paye[$my_paye_rate]['company_customer_accounting_code'][$id];
  298. $company_static->code_fournisseur = $x_paye[$my_paye_rate]['company_supplier_code'][$id];
  299. $company_static->code_compta_fournisseur = $x_paye[$my_paye_rate]['company_supplier_accounting_code'][$id];
  300. $x_both[$my_paye_rate]['paye']['detail'][] = array(
  301. 'id' =>$x_paye[$my_paye_rate]['facid'][$id],
  302. 'descr' =>$x_paye[$my_paye_rate]['descr'][$id],
  303. 'pid' =>$x_paye[$my_paye_rate]['pid'][$id],
  304. 'pref' =>$x_paye[$my_paye_rate]['pref'][$id],
  305. 'ptype' =>$x_paye[$my_paye_rate]['ptype'][$id],
  306. 'payment_id'=>$x_paye[$my_paye_rate]['payment_id'][$id],
  307. 'payment_ref'=>$x_paye[$my_paye_rate]['payment_ref'][$id],
  308. 'payment_amount'=>$x_paye[$my_paye_rate]['payment_amount'][$id],
  309. 'ftotal_ttc'=>price2num($x_paye[$my_paye_rate]['ftotal_ttc'][$id]),
  310. 'dtotal_ttc'=>price2num($x_paye[$my_paye_rate]['dtotal_ttc'][$id]),
  311. 'dtype' =>$x_paye[$my_paye_rate]['dtype'][$id],
  312. 'datef' =>$x_paye[$my_paye_rate]['datef'][$id],
  313. 'datep' =>$x_paye[$my_paye_rate]['datep'][$id],
  314. 'company_link'=>$company_static->getNomUrl(1, '', 20),
  315. 'ddate_start'=>$x_paye[$my_paye_rate]['ddate_start'][$id],
  316. 'ddate_end' =>$x_paye[$my_paye_rate]['ddate_end'][$id],
  317. 'totalht' =>price2num($x_paye[$my_paye_rate]['totalht_list'][$id]),
  318. 'vat' =>$x_paye[$my_paye_rate]['vat_list'][$id],
  319. 'link' =>$invoice_supplier->getNomUrl(1, '', 12)
  320. );
  321. }
  322. }
  323. }
  324. //now we have an array (x_both) indexed by rates for coll and paye
  325. //print table headers for this quadri - incomes first
  326. $x_coll_sum = 0;
  327. $x_coll_ht = 0;
  328. $x_paye_sum = 0;
  329. $x_paye_ht = 0;
  330. //print '<tr><td colspan="'.($span+1).'">'..')</td></tr>';
  331. // Customers invoices
  332. print '<tr class="liste_titre">';
  333. print '<td class="left">'.$elementcust.'</td>';
  334. print '<td class="left">'.$langs->trans("DateInvoice").'</td>';
  335. if ($conf->global->TAX_MODE_SELL_PRODUCT == 'payment' || $conf->global->TAX_MODE_SELL_SERVICE == 'payment') {
  336. print '<td class="left">'.$langs->trans("DatePayment").'</td>';
  337. } else {
  338. print '<td></td>';
  339. }
  340. print '<td class="left">'.$namecust.'</td>';
  341. print '<td class="left">'.$productcust.'</td>';
  342. if ($modetax != 1) {
  343. print '<td class="right">'.$amountcust.'</td>';
  344. print '<td class="right">'.$langs->trans("Payment").' ('.$langs->trans("PercentOfInvoice").')</td>';
  345. }
  346. print '<td class="right">'.$langs->trans("AmountHTVATRealReceived").'</td>';
  347. print '<td class="right">'.$vatcust.'</td>';
  348. print '</tr>';
  349. $action = "tvadetail";
  350. $parameters["mode"] = $modetax;
  351. $parameters["start"] = $date_start;
  352. $parameters["end"] = $date_end;
  353. $parameters["type"] = 'vat';
  354. $object = array(&$x_coll, &$x_paye, &$x_both);
  355. // Initialize technical object to manage hooks of expenses. Note that conf->hooks_modules contains array array
  356. $hookmanager->initHooks(array('externalbalance'));
  357. $reshook = $hookmanager->executeHooks('addVatLine', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  358. foreach (array_keys($x_coll) as $rate) {
  359. $subtot_coll_total_ht = 0;
  360. $subtot_coll_vat = 0;
  361. if (is_array($x_both[$rate]['coll']['detail'])) {
  362. // VAT Rate
  363. print "<tr>";
  364. print '<td class="tax_rate" colspan="' . ($span+1) . '">';
  365. print $langs->trans('Rate') . ' : ' . vatrate($rate) . '%';
  366. print ' - <a href="'.DOL_URL_ROOT.'/compta/tva/quadri_detail.php?invoice_type=customer';
  367. if ($invoice_type != 'customer' || !GETPOSTISSET('vat_rate_show') || GETPOST('vat_rate_show') != $rate) {
  368. print '&amp;vat_rate_show='.urlencode($rate);
  369. }
  370. print '&amp;date_startyear='.urlencode($date_start_year).'&amp;date_startmonth='.urlencode($date_start_month).'&amp;date_startday='.urlencode($date_start_day).'&amp;date_endyear='.urlencode($date_end_year).'&amp;date_endmonth='.urlencode($date_end_month).'&amp;date_endday='.urlencode($date_end_day).'">' . img_picto('', 'chevron-down', 'class="paddingrightonly"') . $langs->trans('VATReportShowByRateDetails') . '</a>';
  371. print '</td>';
  372. print '</tr>'."\n";
  373. foreach ($x_both[$rate]['coll']['detail'] as $index => $fields) {
  374. // Define type
  375. // We MUST use dtype (type in line). We can use something else, only if dtype is really unknown.
  376. $type = (isset($fields['dtype']) ? $fields['dtype'] : $fields['ptype']);
  377. // Try to enhance type detection using date_start and date_end for free lines where type
  378. // was not saved.
  379. if (!empty($fields['ddate_start'])) {
  380. $type = 1;
  381. }
  382. if (!empty($fields['ddate_end'])) {
  383. $type = 1;
  384. }
  385. // Payment
  386. $ratiopaymentinvoice=1;
  387. if ($modetax != 1) {
  388. if (($type == 0 && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
  389. || ($type == 1 && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice')) {
  390. } else {
  391. if (isset($fields['payment_amount']) && price2num($fields['ftotal_ttc'])) {
  392. $ratiopaymentinvoice=($fields['payment_amount']/$fields['ftotal_ttc']);
  393. }
  394. }
  395. }
  396. // Total collected
  397. $temp_ht=$fields['totalht']*$ratiopaymentinvoice;
  398. // VAT
  399. $temp_vat=$fields['vat']*$ratiopaymentinvoice;
  400. $subtot_coll_total_ht += $temp_ht;
  401. $subtot_coll_vat += $temp_vat;
  402. $x_coll_sum += $temp_vat;
  403. }
  404. }
  405. if ($invoice_type == 'customer' && $vat_rate_show == $rate) {
  406. if (is_array($x_both[$rate]['coll']['detail'])) {
  407. foreach ($x_both[$rate]['coll']['detail'] as $index => $fields) {
  408. /*$company_static->id = $fields['company_id'];
  409. $company_static->name = $fields['company_name'];
  410. $company_static->name_alias = $fields['company_alias'];
  411. $company_static->email = $fields['company_email'];
  412. $company_static->tva_intra = $fields['tva_intra'];
  413. $company_static->client = $fields['company_client'];
  414. $company_static->fournisseur = $fields['company_fournisseur'];
  415. $company_static->status = $fields['company_status'];
  416. $company_static->code_client = $fields['company_client'];
  417. $company_static->code_compta_client = $fields['company_customer_code'];
  418. $company_static->code_fournisseur = $fields['company_customer_accounting_code'];
  419. $company_static->code_compta_fournisseur = $fields['company_supplier_accounting_code'];*/
  420. // Define type
  421. // We MUST use dtype (type in line). We can use something else, only if dtype is really unknown.
  422. $type = (isset($fields['dtype']) ? $fields['dtype'] : $fields['ptype']);
  423. // Try to enhance type detection using date_start and date_end for free lines where type
  424. // was not saved.
  425. if (!empty($fields['ddate_start'])) {
  426. $type = 1;
  427. }
  428. if (!empty($fields['ddate_end'])) {
  429. $type = 1;
  430. }
  431. print '<tr class="oddeven">';
  432. // Ref
  433. print '<td class="nowrap left">' . $fields['link'] . '</td>';
  434. // Invoice date
  435. print '<td class="left">' . dol_print_date($fields['datef'], 'day') . '</td>';
  436. // Payment date
  437. if ($conf->global->TAX_MODE_SELL_PRODUCT == 'payment' || $conf->global->TAX_MODE_SELL_SERVICE == 'payment') print '<td class="left">' . dol_print_date($fields['datep'], 'day') . '</td>';
  438. else print '<td></td>';
  439. // Company name
  440. print '<td class="tdmaxoverflow150">';
  441. //print $company_static->getNomUrl(1);
  442. print $fields['company_link'];
  443. print '</td>';
  444. // Description
  445. print '<td class="left">';
  446. if ($fields['pid']) {
  447. $product_static->id = $fields['pid'];
  448. $product_static->ref = $fields['pref'];
  449. $product_static->type = $fields['dtype']; // We force with the type of line to have type how line is registered
  450. print $product_static->getNomUrl(1);
  451. if (dol_string_nohtmltag($fields['descr'])) {
  452. print ' - ' . dol_trunc(dol_string_nohtmltag($fields['descr']), 24);
  453. }
  454. } else {
  455. if ($type) {
  456. $text = img_object($langs->trans('Service'), 'service');
  457. } else {
  458. $text = img_object($langs->trans('Product'), 'product');
  459. }
  460. if (preg_match('/^\((.*)\)$/', $fields['descr'], $reg)) {
  461. if ($reg[1] == 'DEPOSIT') {
  462. $fields['descr'] = $langs->transnoentitiesnoconv('Deposit');
  463. } elseif ($reg[1] == 'CREDIT_NOTE') {
  464. $fields['descr'] = $langs->transnoentitiesnoconv('CreditNote');
  465. } else {
  466. $fields['descr'] = $langs->transnoentitiesnoconv($reg[1]);
  467. }
  468. }
  469. print $text . ' ' . dol_trunc(dol_string_nohtmltag($fields['descr']), 24);
  470. // Show range
  471. print_date_range($fields['ddate_start'], $fields['ddate_end']);
  472. }
  473. print '</td>';
  474. // Total HT
  475. if ($modetax != 1) {
  476. print '<td class="nowrap right">';
  477. print price($fields['totalht']);
  478. if (price2num($fields['ftotal_ttc'])) {
  479. //print $fields['dtotal_ttc']."/".$fields['ftotal_ttc']." - ";
  480. $ratiolineinvoice = ($fields['dtotal_ttc'] / $fields['ftotal_ttc']);
  481. //print ' ('.round($ratiolineinvoice*100,2).'%)';
  482. }
  483. print '</td>';
  484. }
  485. // Payment
  486. $ratiopaymentinvoice = 1;
  487. if ($modetax != 1) {
  488. print '<td class="nowrap right">';
  489. //print $fields['totalht']."-".$fields['payment_amount']."-".$fields['ftotal_ttc'];
  490. if ($fields['payment_amount'] && $fields['ftotal_ttc']) {
  491. $payment_static->id = $fields['payment_id'];
  492. $payment_static->ref = $fields['payment_ref'];
  493. print $payment_static->getNomUrl(2, '', '', 0).' ';
  494. }
  495. if (($type == 0 && $conf->global->TAX_MODE_SELL_PRODUCT == 'invoice')
  496. || ($type == 1 && $conf->global->TAX_MODE_SELL_SERVICE == 'invoice')) {
  497. print $langs->trans("NA");
  498. } else {
  499. if (isset($fields['payment_amount']) && price2num($fields['ftotal_ttc'])) {
  500. $ratiopaymentinvoice = ($fields['payment_amount'] / $fields['ftotal_ttc']);
  501. }
  502. print price(price2num($fields['payment_amount'], 'MT'));
  503. if (isset($fields['payment_amount'])) {
  504. print ' (' . round($ratiopaymentinvoice * 100, 2) . '%)';
  505. }
  506. }
  507. print '</td>';
  508. }
  509. // Total collected
  510. print '<td class="nowrap right">';
  511. $temp_ht = $fields['totalht'] * $ratiopaymentinvoice;
  512. print price(price2num($temp_ht, 'MT'), 1);
  513. print '</td>';
  514. // VAT
  515. print '<td class="nowrap right">';
  516. $temp_vat = $fields['vat'] * $ratiopaymentinvoice;
  517. print price(price2num($temp_vat, 'MT'), 1);
  518. //print price($fields['vat']);
  519. print '</td>';
  520. print '</tr>';
  521. //$subtot_coll_total_ht += $temp_ht;
  522. //$subtot_coll_vat += $temp_vat;
  523. //$x_coll_sum += $temp_vat;
  524. }
  525. }
  526. }
  527. // Total customers for this vat rate
  528. print '<tr class="liste_total">';
  529. print '<td colspan="4"></td>';
  530. print '<td class="right">'.$langs->trans("Total").':</td>';
  531. if ($modetax != 1) {
  532. print '<td class="nowrap right">&nbsp;</td>';
  533. print '<td class="right">&nbsp;</td>';
  534. }
  535. print '<td class="right"><span class="amount">'.price(price2num($subtot_coll_total_ht, 'MT')).'</span></td>';
  536. print '<td class="nowrap right"><span class="amount">'.price(price2num($subtot_coll_vat, 'MT')).'</span></td>';
  537. print '</tr>';
  538. }
  539. if (count($x_coll) == 0) { // Show a total line if nothing shown
  540. print '<tr class="liste_total">';
  541. print '<td colspan="4"></td>';
  542. print '<td class="right">'.$langs->trans("Total").':</td>';
  543. if ($modetax != 1) {
  544. print '<td class="nowrap right">&nbsp;</td>';
  545. print '<td class="right">&nbsp;</td>';
  546. }
  547. print '<td class="right">'.price(price2num(0, 'MT')).'</td>';
  548. print '<td class="nowrap right">'.price(price2num(0, 'MT')).'</td>';
  549. print '</tr>';
  550. }
  551. // Blank line
  552. print '<tr><td colspan="'.($span+2).'">&nbsp;</td></tr>';
  553. // Print table headers for this quadri - expenses
  554. print '<tr class="liste_titre liste_titre_topborder">';
  555. print '<td class="left">'.$elementsup.'</td>';
  556. print '<td class="left">'.$langs->trans("DateInvoice").'</td>';
  557. if ($conf->global->TAX_MODE_BUY_PRODUCT == 'payment' || $conf->global->TAX_MODE_BUY_SERVICE == 'payment') print '<td class="left">'.$langs->trans("DatePayment").'</td>';
  558. else print '<td></td>';
  559. print '<td class="left">'.$namesup.'</td>';
  560. print '<td class="left">'.$productsup.'</td>';
  561. if ($modetax != 1) {
  562. print '<td class="right">'.$amountsup.'</td>';
  563. print '<td class="right">'.$langs->trans("Payment").' ('.$langs->trans("PercentOfInvoice").')</td>';
  564. }
  565. print '<td class="right">'.$langs->trans("AmountHTVATRealPaid").'</td>';
  566. print '<td class="right">'.$vatsup.'</td>';
  567. print '</tr>'."\n";
  568. foreach (array_keys($x_paye) as $rate) {
  569. $subtot_paye_total_ht = 0;
  570. $subtot_paye_vat = 0;
  571. if (is_array($x_both[$rate]['paye']['detail'])) {
  572. print "<tr>";
  573. print '<td class="tax_rate" colspan="' . ($span+1) . '">';
  574. print $langs->trans('Rate') . ' : ' . vatrate($rate) . '%';
  575. print ' - <a href="'.DOL_URL_ROOT.'/compta/tva/quadri_detail.php?invoice_type=supplier';
  576. if ($invoice_type != 'supplier' || !GETPOSTISSET('vat_rate_show') || GETPOST('vat_rate_show') != $rate) {
  577. print '&amp;vat_rate_show='.urlencode($rate);
  578. }
  579. print '&amp;date_startyear='.urlencode($date_start_year).'&amp;date_startmonth='.urlencode($date_start_month).'&amp;date_startday='.urlencode($date_start_day).'&amp;date_endyear='.urlencode($date_end_year).'&amp;date_endmonth='.urlencode($date_end_month).'&amp;date_endday='.urlencode($date_end_day).'">' . img_picto('', 'chevron-down', 'class="paddingrightonly"') . $langs->trans('VATReportShowByRateDetails') . '</a>';
  580. print '</td>';
  581. print '</tr>'."\n";
  582. foreach ($x_both[$rate]['paye']['detail'] as $index => $fields) {
  583. // Define type
  584. // We MUST use dtype (type in line). We can use something else, only if dtype is really unknown.
  585. $type = (isset($fields['dtype']) ? $fields['dtype'] : $fields['ptype']);
  586. // Try to enhance type detection using date_start and date_end for free lines where type
  587. // was not saved.
  588. if (!empty($fields['ddate_start'])) {
  589. $type = 1;
  590. }
  591. if (!empty($fields['ddate_end'])) {
  592. $type = 1;
  593. }
  594. // Payment
  595. $ratiopaymentinvoice = 1;
  596. if ($modetax != 1) {
  597. if (($type == 0 && $conf->global->TAX_MODE_BUY_PRODUCT == 'invoice')
  598. || ($type == 1 && $conf->global->TAX_MODE_BUY_SERVICE == 'invoice')) {
  599. } else {
  600. if (isset($fields['payment_amount']) && $fields['ftotal_ttc']) {
  601. $ratiopaymentinvoice = ($fields['payment_amount'] / $fields['ftotal_ttc']);
  602. }
  603. }
  604. }
  605. // VAT paid
  606. $temp_ht = $fields['totalht'] * $ratiopaymentinvoice;
  607. // VAT
  608. $temp_vat = $fields['vat'] * $ratiopaymentinvoice;
  609. $subtot_paye_total_ht += $temp_ht;
  610. $subtot_paye_vat += $temp_vat;
  611. $x_paye_sum += $temp_vat;
  612. }
  613. if ($invoice_type == 'supplier' && $vat_rate_show == $rate) {
  614. foreach ($x_both[$rate]['paye']['detail'] as $index => $fields) {
  615. /*$company_static->id = $fields['company_id'];
  616. $company_static->name = $fields['company_name'];
  617. $company_static->name_alias = $fields['company_alias'];
  618. $company_static->email = $fields['company_email'];
  619. $company_static->tva_intra = $fields['tva_intra'];
  620. $company_static->client = $fields['company_client'];
  621. $company_static->fournisseur = $fields['company_fournisseur'];
  622. $company_static->status = $fields['company_status'];
  623. $company_static->code_client = $fields['company_client'];
  624. $company_static->code_compta_client = $fields['company_customer_code'];
  625. $company_static->code_fournisseur = $fields['company_customer_accounting_code'];
  626. $company_static->code_compta_fournisseur = $fields['company_supplier_accounting_code'];*/
  627. // Define type
  628. // We MUST use dtype (type in line). We can use something else, only if dtype is really unknown.
  629. $type = (isset($fields['dtype']) ? $fields['dtype'] : $fields['ptype']);
  630. // Try to enhance type detection using date_start and date_end for free lines where type
  631. // was not saved.
  632. if (!empty($fields['ddate_start'])) {
  633. $type = 1;
  634. }
  635. if (!empty($fields['ddate_end'])) {
  636. $type = 1;
  637. }
  638. print '<tr class="oddeven">';
  639. // Ref
  640. print '<td class="nowrap left">' . $fields['link'] . '</td>';
  641. // Invoice date
  642. print '<td class="left">' . dol_print_date($fields['datef'], 'day') . '</td>';
  643. // Payment date
  644. if ($conf->global->TAX_MODE_BUY_PRODUCT == 'payment' || $conf->global->TAX_MODE_BUY_SERVICE == 'payment') {
  645. print '<td class="left">' . dol_print_date($fields['datep'], 'day') . '</td>';
  646. } else {
  647. print '<td></td>';
  648. }
  649. // Company name
  650. print '<td class="tdmaxoverflow150">';
  651. //print $company_static->getNomUrl(1);
  652. print $fields['company_link'];
  653. print '</td>';
  654. // Description
  655. print '<td class="left">';
  656. if ($fields['pid']) {
  657. $product_static->id = $fields['pid'];
  658. $product_static->ref = $fields['pref'];
  659. $product_static->type = $fields['dtype']; // We force with the type of line to have type how line is registered
  660. print $product_static->getNomUrl(1);
  661. if (dol_string_nohtmltag($fields['descr'])) {
  662. print ' - ' . dol_trunc(dol_string_nohtmltag($fields['descr']), 24);
  663. }
  664. } else {
  665. if ($type) {
  666. $text = img_object($langs->trans('Service'), 'service');
  667. } else {
  668. $text = img_object($langs->trans('Product'), 'product');
  669. }
  670. if (preg_match('/^\((.*)\)$/', $fields['descr'], $reg)) {
  671. if ($reg[1] == 'DEPOSIT') {
  672. $fields['descr'] = $langs->transnoentitiesnoconv('Deposit');
  673. } elseif ($reg[1] == 'CREDIT_NOTE') {
  674. $fields['descr'] = $langs->transnoentitiesnoconv('CreditNote');
  675. } else {
  676. $fields['descr'] = $langs->transnoentitiesnoconv($reg[1]);
  677. }
  678. }
  679. print $text . ' ' . dol_trunc(dol_string_nohtmltag($fields['descr']), 24);
  680. // Show range
  681. print_date_range($fields['ddate_start'], $fields['ddate_end']);
  682. }
  683. print '</td>';
  684. // Total HT
  685. if ($modetax != 1) {
  686. print '<td class="nowrap right">';
  687. print price($fields['totalht']);
  688. if (price2num($fields['ftotal_ttc'])) {
  689. //print $fields['dtotal_ttc']."/".$fields['ftotal_ttc']." - ";
  690. $ratiolineinvoice = ($fields['dtotal_ttc'] / $fields['ftotal_ttc']);
  691. //print ' ('.round($ratiolineinvoice*100,2).'%)';
  692. }
  693. print '</td>';
  694. }
  695. // Payment
  696. $ratiopaymentinvoice = 1;
  697. if ($modetax != 1) {
  698. print '<td class="nowrap right">';
  699. if ($fields['payment_amount'] && $fields['ftotal_ttc']) {
  700. $paymentfourn_static->id = $fields['payment_id'];
  701. $paymentfourn_static->ref = $fields['payment_ref'];
  702. print $paymentfourn_static->getNomUrl(2, '', '', 0).' ';
  703. }
  704. if (($type == 0 && $conf->global->TAX_MODE_BUY_PRODUCT == 'invoice')
  705. || ($type == 1 && $conf->global->TAX_MODE_BUY_SERVICE == 'invoice')) {
  706. print $langs->trans("NA");
  707. } else {
  708. if (isset($fields['payment_amount']) && $fields['ftotal_ttc']) {
  709. $ratiopaymentinvoice = ($fields['payment_amount'] / $fields['ftotal_ttc']);
  710. }
  711. print price(price2num($fields['payment_amount'], 'MT'));
  712. if (isset($fields['payment_amount'])) {
  713. print ' (' . round($ratiopaymentinvoice * 100, 2) . '%)';
  714. }
  715. }
  716. print '</td>';
  717. }
  718. // VAT paid
  719. print '<td class="nowrap right">';
  720. $temp_ht = $fields['totalht'] * $ratiopaymentinvoice;
  721. print price(price2num($temp_ht, 'MT'), 1);
  722. print '</td>';
  723. // VAT
  724. print '<td class="nowrap right">';
  725. $temp_vat = $fields['vat'] * $ratiopaymentinvoice;
  726. print price(price2num($temp_vat, 'MT'), 1);
  727. //print price($fields['vat']);
  728. print '</td>';
  729. print '</tr>';
  730. //$subtot_paye_total_ht += $temp_ht;
  731. //$subtot_paye_vat += $temp_vat;
  732. //$x_paye_sum += $temp_vat;
  733. }
  734. }
  735. }
  736. // Total suppliers for this vat rate
  737. print '<tr class="liste_total">';
  738. print '<td colspan="4"></td>';
  739. print '<td class="right">'.$langs->trans("Total").':</td>';
  740. if ($modetax != 1) {
  741. print '<td class="nowrap right">&nbsp;</td>';
  742. print '<td class="right">&nbsp;</td>';
  743. }
  744. print '<td class="right"><span class="amount">'.price(price2num($subtot_paye_total_ht, 'MT')).'</span></td>';
  745. print '<td class="nowrap right"><span class="amount">'.price(price2num($subtot_paye_vat, 'MT')).'</span></td>';
  746. print '</tr>';
  747. }
  748. if (count($x_paye) == 0) { // Show a total line if nothing shown
  749. print '<tr class="liste_total">';
  750. print '<td colspan="4"></td>';
  751. print '<td class="right">'.$langs->trans("Total").':</td>';
  752. if ($modetax != 1) {
  753. print '<td class="nowrap right">&nbsp;</td>';
  754. print '<td class="right">&nbsp;</td>';
  755. }
  756. print '<td class="right"><span class="amount">'.price(price2num(0, 'MT')).'</span></td>';
  757. print '<td class="nowrap right"><span class="amount">'.price(price2num(0, 'MT')).'</span></td>';
  758. print '</tr>';
  759. }
  760. print '</table>';
  761. print '</div>';
  762. // Total to pay
  763. print '<br><br>';
  764. print '<table class="noborder centpercent">';
  765. $diff = $x_coll_sum - $x_paye_sum;
  766. print '<tr class="liste_total">';
  767. print '<td class="liste_total" colspan="'.$span.'">'.$langs->trans("TotalToPay").($q ? ', '.$langs->trans("Quadri").' '.$q : '').'</td>';
  768. print '<td class="liste_total nowrap right"><b>'.price(price2num($diff, 'MT'))."</b></td>\n";
  769. print "</tr>\n";
  770. $i++;
  771. }
  772. print '</table>';
  773. llxFooter();
  774. $db->close();