clients.php 32 KB

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