clients.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344
  1. <?php
  2. /* Copyright (C) 2011-2014 Juanjo Menent <jmenent@2byte.es>
  3. * Copyright (C) 2014 Ferran Marcet <fmarcet@2byte.es>
  4. * Copyright (C) 2018 Frédéric France <frederic.france@netlogic.fr>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/compta/localtax/clients.php
  21. * \ingroup tax
  22. * \brief Third parties localtax report
  23. */
  24. // Load Dolibarr environment
  25. require '../../main.inc.php';
  26. require_once DOL_DOCUMENT_ROOT.'/core/lib/report.lib.php';
  27. require_once DOL_DOCUMENT_ROOT.'/core/lib/tax.lib.php';
  28. require_once DOL_DOCUMENT_ROOT.'/core/lib/date.lib.php';
  29. require_once DOL_DOCUMENT_ROOT.'/compta/tva/class/tva.class.php';
  30. require_once DOL_DOCUMENT_ROOT.'/compta/localtax/class/localtax.class.php';
  31. // Load translation files required by the page
  32. $langs->loadLangs(array("other", "compta", "banks", "bills", "companies", "product", "trips", "admin"));
  33. $local = GETPOST('localTaxType', 'int');
  34. // Date range
  35. $year = GETPOST("year", "int");
  36. if (empty($year)) {
  37. $year_current = dol_print_date(dol_now('gmt'), "%Y", 'gmt');
  38. $year_start = $year_current;
  39. } else {
  40. $year_current = $year;
  41. $year_start = $year;
  42. }
  43. $date_start = dol_mktime(0, 0, 0, GETPOST("date_startmonth"), GETPOST("date_startday"), GETPOST("date_startyear"));
  44. $date_end = dol_mktime(23, 59, 59, GETPOST("date_endmonth"), GETPOST("date_endday"), GETPOST("date_endyear"));
  45. // Quarter
  46. if (empty($date_start) || empty($date_end)) { // We define date_start and date_end
  47. $q = GETPOST("q");
  48. if (empty($q)) {
  49. if (GETPOST("month")) {
  50. $date_start = dol_get_first_day($year_start, GETPOST("month"), false);
  51. $date_end = dol_get_last_day($year_start, GETPOST("month"), false);
  52. } else {
  53. $date_start = dol_get_first_day($year_start, empty($conf->global->SOCIETE_FISCAL_MONTH_START) ? 1 : $conf->global->SOCIETE_FISCAL_MONTH_START, false);
  54. if (empty($conf->global->MAIN_INFO_VAT_RETURN) || $conf->global->MAIN_INFO_VAT_RETURN == 2) {
  55. $date_end = dol_time_plus_duree($date_start, 3, 'm') - 1;
  56. } elseif ($conf->global->MAIN_INFO_VAT_RETURN == 3) {
  57. $date_end = dol_time_plus_duree($date_start, 1, 'y') - 1;
  58. } elseif ($conf->global->MAIN_INFO_VAT_RETURN == 1) {
  59. $date_end = dol_time_plus_duree($date_start, 1, 'm') - 1;
  60. }
  61. }
  62. } else {
  63. if ($q == 1) {
  64. $date_start = dol_get_first_day($year_start, 1, false);
  65. $date_end = dol_get_last_day($year_start, 3, false);
  66. }
  67. if ($q == 2) {
  68. $date_start = dol_get_first_day($year_start, 4, false);
  69. $date_end = dol_get_last_day($year_start, 6, false);
  70. }
  71. if ($q == 3) {
  72. $date_start = dol_get_first_day($year_start, 7, false);
  73. $date_end = dol_get_last_day($year_start, 9, false);
  74. }
  75. if ($q == 4) {
  76. $date_start = dol_get_first_day($year_start, 10, false);
  77. $date_end = dol_get_last_day($year_start, 12, false);
  78. }
  79. }
  80. }
  81. $min = price2num(GETPOST("min", "alpha"));
  82. if (empty($min)) {
  83. $min = 0;
  84. }
  85. // Define modetax (0 or 1)
  86. // 0=normal, 1=option vat for services is on debit, 2=option on payments for products
  87. $modetax = $conf->global->TAX_MODE;
  88. if (GETPOSTISSET("modetax")) {
  89. $modetax = GETPOST("modetax", 'int');
  90. }
  91. if (empty($modetax)) {
  92. $modetax = 0;
  93. }
  94. // Security check
  95. $socid = GETPOST('socid', 'int');
  96. if ($user->socid) {
  97. $socid = $user->socid;
  98. }
  99. $result = restrictedArea($user, 'tax', '', '', 'charges');
  100. if (empty($local)) {
  101. accessforbidden('Parameter localTaxType is missing');
  102. exit;
  103. }
  104. $calc = 0;
  105. /*
  106. * View
  107. */
  108. $form = new Form($db);
  109. $company_static = new Societe($db);
  110. $morequerystring = '';
  111. $listofparams = array('date_startmonth', 'date_startyear', 'date_startday', 'date_endmonth', 'date_endyear', 'date_endday');
  112. foreach ($listofparams as $param) {
  113. if (GETPOST($param) != '') {
  114. $morequerystring .= ($morequerystring ? '&' : '').$param.'='.GETPOST($param);
  115. }
  116. }
  117. llxHeader('', '', '', '', 0, 0, '', '', $morequerystring);
  118. $name = $langs->transcountry($local == 1 ? "LT1ReportByCustomers" : "LT2ReportByCustomers", $mysoc->country_code);
  119. $fsearch = '<!-- hidden fields for form -->';
  120. $fsearch .= '<input type="hidden" name="token" value="'.newToken().'">';
  121. $fsearch .= '<input type="hidden" name="modetax" value="'.$modetax.'">';
  122. $fsearch .= '<input type="hidden" name="localTaxType" value="'.$local.'">';
  123. $fsearch .= $langs->trans("SalesTurnoverMinimum").': ';
  124. $fsearch .= '<input type="text" name="min" id="min" value="'.$min.'" size="6">';
  125. $calc = $conf->global->MAIN_INFO_LOCALTAX_CALC.$local;
  126. // Affiche en-tete du rapport
  127. if ($calc == 0 || $calc == 1) { // Calculate on invoice for goods and services
  128. $calcmode = $calc == 0 ? $langs->trans("CalcModeLT".$local) : $langs->trans("CalcModeLT".$local."Rec");
  129. $calcmode .= ' <span class="opacitymedium">('.$langs->trans("TaxModuleSetupToModifyRulesLT", DOL_URL_ROOT.'/admin/company.php').')</span>';
  130. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
  131. if (!empty($conf->global->MAIN_MODULE_COMPTABILITE)) {
  132. $description .= '<br>'.$langs->trans("WarningDepositsNotIncluded");
  133. }
  134. $description .= $fsearch;
  135. $description .= ' <span class="opacitymedium">('.$langs->trans("TaxModuleSetupToModifyRulesLT", DOL_URL_ROOT.'/admin/company.php').')</span>';
  136. $builddate = dol_now();
  137. $elementcust = $langs->trans("CustomersInvoices");
  138. $productcust = $langs->trans("Description");
  139. $amountcust = $langs->trans("AmountHT");
  140. $elementsup = $langs->trans("SuppliersInvoices");
  141. $productsup = $langs->trans("Description");
  142. $amountsup = $langs->trans("AmountHT");
  143. }
  144. if ($calc == 2) { // Invoice for goods, payment for services
  145. $calcmode = $langs->trans("CalcModeLT2Debt");
  146. $calcmode .= ' <span class="opacitymedium">('.$langs->trans("TaxModuleSetupToModifyRulesLT", DOL_URL_ROOT.'/admin/company.php').')</span>';
  147. $period = $form->selectDate($date_start, 'date_start', 0, 0, 0, '', 1, 0).' - '.$form->selectDate($date_end, 'date_end', 0, 0, 0, '', 1, 0);
  148. if (!empty($conf->global->MAIN_MODULE_COMPTABILITE)) {
  149. $description .= '<br>'.$langs->trans("WarningDepositsNotIncluded");
  150. }
  151. $description .= $fsearch;
  152. $description .= '<span class="opacitymedium">('.$langs->trans("TaxModuleSetupToModifyRulesLT", DOL_URL_ROOT.'/admin/company.php').')</span>';
  153. $builddate = dol_now();
  154. $elementcust = $langs->trans("CustomersInvoices");
  155. $productcust = $langs->trans("Description");
  156. $amountcust = $langs->trans("AmountHT");
  157. $elementsup = $langs->trans("SuppliersInvoices");
  158. $productsup = $langs->trans("Description");
  159. $amountsup = $langs->trans("AmountHT");
  160. }
  161. report_header($name, '', $period, $periodlink, $description, $builddate, $exportlink, array(), $calcmode);
  162. $vatcust = $langs->transcountry($local == 1 ? "LT1" : "LT2", $mysoc->country_code);
  163. $vatsup = $langs->transcountry($local == 1 ? "LT1" : "LT2", $mysoc->country_code);
  164. print '<div class="div-table-responsive">';
  165. print '<table class="noborder centpercent">';
  166. // IRPF that the customer has retained me
  167. if ($calc == 0 || $calc == 2) {
  168. print '<tr class="liste_titre">';
  169. print '<td class="left">'.$langs->trans("Num").'</td>';
  170. print '<td class="left">'.$langs->trans("Customer").'</td>';
  171. print '<td>'.$langs->transcountry("ProfId1", $mysoc->country_code).'</td>';
  172. print '<td class="right">'.$langs->trans("TotalHT").'</td>';
  173. print '<td class="right">'.$vatcust.'</td>';
  174. print "</tr>\n";
  175. $coll_list = tax_by_thirdparty('localtax'.$local, $db, 0, $date_start, $date_end, $modetax, 'sell');
  176. $action = "tvaclient";
  177. $object = &$coll_list;
  178. $parameters["mode"] = $modetax;
  179. $parameters["start"] = $date_start;
  180. $parameters["end"] = $date_end;
  181. $parameters["direction"] = 'sell';
  182. $parameters["type"] = 'localtax'.$local;
  183. // Initialize technical object to manage hooks of expenses. Note that conf->hooks_modules contains array array
  184. $hookmanager->initHooks(array('externalbalance'));
  185. $reshook = $hookmanager->executeHooks('addVatLine', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  186. if (is_array($coll_list)) {
  187. $total = 0;
  188. $totalamount = 0;
  189. $i = 1;
  190. foreach ($coll_list as $coll) {
  191. if (($min == 0 || ($min > 0 && $coll->amount > $min)) && ($local == 1 ? $coll->localtax1 : $coll->localtax2) != 0) {
  192. $intra = str_replace($find, $replace, $coll->tva_intra);
  193. if (empty($intra)) {
  194. if ($coll->assuj == '1') {
  195. $intra = $langs->trans('Unknown');
  196. } else {
  197. $intra = '';
  198. }
  199. }
  200. print '<tr class="oddeven">';
  201. print '<td class="nowrap">'.$i."</td>";
  202. $company_static->id = $coll->socid;
  203. $company_static->name = $coll->name;
  204. print '<td class="nowrap">'.$company_static->getNomUrl(1).'</td>';
  205. $find = array(' ', '.');
  206. $replace = array('', '');
  207. print '<td class="nowrap">'.$intra.'</td>';
  208. print '<td class="nowrap right">'.price($coll->amount).'</td>';
  209. print '<td class="nowrap right">'.price($local == 1 ? $coll->localtax1 : $coll->localtax2).'</td>';
  210. $totalamount = $totalamount + $coll->amount;
  211. $total = $total + ($local == 1 ? $coll->localtax1 : $coll->localtax2);
  212. print "</tr>\n";
  213. $i++;
  214. }
  215. }
  216. $x_coll_sum = $total;
  217. print '<tr class="liste_total"><td class="right" colspan="3">'.$langs->trans("Total").':</td>';
  218. print '<td class="nowrap right">'.price($totalamount).'</td>';
  219. print '<td class="nowrap right">'.price($total).'</td>';
  220. print '</tr>';
  221. } else {
  222. $langs->load("errors");
  223. if ($coll_list == -1) {
  224. print '<tr><td colspan="5">'.$langs->trans("ErrorNoAccountancyModuleLoaded").'</td></tr>';
  225. } elseif ($coll_list == -2) {
  226. print '<tr><td colspan="5">'.$langs->trans("FeatureNotYetAvailable").'</td></tr>';
  227. } else {
  228. print '<tr><td colspan="5">'.$langs->trans("Error").'</td></tr>';
  229. }
  230. }
  231. }
  232. // IRPF I retained my supplier
  233. if ($calc == 0 || $calc == 1) {
  234. print '<tr class="liste_titre">';
  235. print '<td class="left">'.$langs->trans("Num")."</td>";
  236. print '<td class="left">'.$langs->trans("Supplier")."</td>";
  237. print '<td>'.$langs->transcountry("ProfId1", $mysoc->country_code).'</td>';
  238. print '<td class="right">'.$langs->trans("TotalHT").'</td>';
  239. print '<td class="right">'.$vatsup.'</td>';
  240. print "</tr>\n";
  241. $company_static = new Societe($db);
  242. $coll_list = tax_by_thirdparty('localtax'.$local, $db, 0, $date_start, $date_end, $modetax, 'buy');
  243. $parameters["direction"] = 'buy';
  244. $parameters["type"] = 'localtax'.$local;
  245. $reshook = $hookmanager->executeHooks('addVatLine', $parameters, $object, $action); // Note that $action and $object may have been modified by some hooks
  246. if (is_array($coll_list)) {
  247. $total = 0;
  248. $totalamount = 0;
  249. $i = 1;
  250. foreach ($coll_list as $coll) {
  251. if (($min == 0 || ($min > 0 && $coll->amount > $min)) && ($local == 1 ? $coll->localtax1 : $coll->localtax2) != 0) {
  252. $intra = str_replace($find, $replace, $coll->tva_intra);
  253. if (empty($intra)) {
  254. if ($coll->assuj == '1') {
  255. $intra = $langs->trans('Unknown');
  256. } else {
  257. $intra = '';
  258. }
  259. }
  260. print '<tr class="oddeven">';
  261. print '<td class="nowrap">'.$i."</td>";
  262. $company_static->id = $coll->socid;
  263. $company_static->name = $coll->name;
  264. print '<td class="nowrap">'.$company_static->getNomUrl(1).'</td>';
  265. $find = array(' ', '.');
  266. $replace = array('', '');
  267. print '<td class="nowrap">'.$intra."</td>";
  268. print '<td class="nowrap right">'.price($coll->amount).'</td>';
  269. print '<td class="nowrap right">'.price($local == 1 ? $coll->localtax1 : $coll->localtax2).'</td>';
  270. $totalamount = $totalamount + $coll->amount;
  271. $total = $total + ($local == 1 ? $coll->localtax1 : $coll->localtax2);
  272. print "</tr>\n";
  273. $i++;
  274. }
  275. }
  276. $x_paye_sum = $total;
  277. print '<tr class="liste_total"><td class="right" colspan="3">'.$langs->trans("Total").':</td>';
  278. print '<td class="nowrap right">'.price($totalamount).'</td>';
  279. print '<td class="nowrap right">'.price($total).'</td>';
  280. print '</tr>';
  281. } else {
  282. $langs->load("errors");
  283. if ($coll_list == -1) {
  284. print '<tr><td colspan="5">'.$langs->trans("ErrorNoAccountancyModuleLoaded").'</td></tr>';
  285. } elseif ($coll_list == -2) {
  286. print '<tr><td colspan="5">'.$langs->trans("FeatureNotYetAvailable").'</td></tr>';
  287. } else {
  288. print '<tr><td colspan="5">'.$langs->trans("Error").'</td></tr>';
  289. }
  290. }
  291. }
  292. if ($calc == 0) {
  293. // Total to pay
  294. print '<tr><td colspan="5"></td></tr>';
  295. $diff = $x_coll_sum - $x_paye_sum;
  296. print '<tr class="liste_total">';
  297. print '<td class="liste_total" colspan="4">'.$langs->trans("TotalToPay").($q ? ', '.$langs->trans("Quadri").' '.$q : '').'</td>';
  298. print '<td class="liste_total nowrap right"><b>'.price(price2num($diff, 'MT'))."</b></td>\n";
  299. print "</tr>\n";
  300. }
  301. print '</table>';
  302. print '</div>';
  303. // End of page
  304. llxFooter();
  305. $db->close();