PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/commissions/index.php

https://bitbucket.org/speedealing/speedealing
PHP | 329 lines | 239 code | 51 blank | 39 comment | 52 complexity | 527f91b8be0ef34af266eed2d9261af8 MD5 | raw file
Possible License(s): LGPL-3.0, LGPL-2.1, GPL-3.0, MIT
  1. <?php
  2. /* Copyright (C) 2012 Christophe Battarel <christophe.battarel@altairis.fr>
  3. *
  4. * This program is free software; you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation; either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  16. */
  17. /**
  18. * \file htdocs/commissions/index.php
  19. * \ingroup commissions
  20. * \brief Page des commissions par agent commercial
  21. */
  22. require '../main.inc.php';
  23. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  24. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  25. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  26. if (! empty($conf->margin->enabled))
  27. require_once DOL_DOCUMENT_ROOT.'/margin/lib/margins.lib.php';
  28. $langs->load("companies");
  29. $langs->load("bills");
  30. $langs->load("products");
  31. $langs->load("commissions");
  32. if (! empty($conf->margin->enabled))
  33. $langs->load("margins");
  34. // Security check
  35. $agentid = GETPOST('agentid','int');
  36. $mesg = '';
  37. $sortfield = GETPOST("sortfield",'alpha');
  38. $sortorder = GETPOST("sortorder",'alpha');
  39. $page = GETPOST("page",'int');
  40. if ($page == -1) { $page = 0; }
  41. $offset = $conf->liste_limit * $page;
  42. $pageprev = $page - 1;
  43. $pagenext = $page + 1;
  44. $startdate=$enddate='';
  45. if (!empty($_POST['startdatemonth']))
  46. $startdate = date('Y-m-d', dol_mktime(12, 0, 0, $_POST['startdatemonth'], $_POST['startdateday'], $_POST['startdateyear']));
  47. if (!empty($_POST['enddatemonth']))
  48. $enddate = date('Y-m-d', dol_mktime(12, 0, 0, $_POST['enddatemonth'], $_POST['enddateday'], $_POST['enddateyear']));
  49. /*
  50. * View
  51. */
  52. $userstatic = new User($db);
  53. $companystatic = new Societe($db);
  54. $invoicestatic=new Facture($db);
  55. $form = new Form($db);
  56. llxHeader('',$langs->trans("Commissions"));
  57. $text=$langs->trans("Commissions");
  58. print_fiche_titre($text);
  59. print '<form method="post" name="sel">';
  60. print '<table class="border" width="100%">';
  61. if ($agentid > 0) {
  62. print '<tr><td width="20%">'.$langs->trans('CommercialAgent').'</td>';
  63. print '<td colspan="4">';
  64. print $form->select_dolusers($agentid,'agentid',1,'',0,'','');
  65. print '</td></tr>';
  66. if (! $sortorder) $sortorder="ASC";
  67. if (! $sortfield) $sortfield="s.nom";
  68. }
  69. else {
  70. print '<tr><td width="20%">'.$langs->trans('CommercialAgent').'</td>';
  71. print '<td colspan="4">';
  72. print $form->select_dolusers('','agentid',1,'',0,'','');
  73. print '</td></tr>';
  74. if (! $sortorder) $sortorder="ASC";
  75. if (! $sortfield) $sortfield="u.login";
  76. }
  77. // Start date
  78. print '<td>'.$langs->trans('StartDate').'</td>';
  79. print '<td width="20%">';
  80. $form->select_date($startdate,'startdate','','',1,"sel",1,1);
  81. print '</td>';
  82. print '<td width="20%">'.$langs->trans('EndDate').'</td>';
  83. print '<td width="20%">';
  84. $form->select_date($enddate,'enddate','','',1,"sel",1,1);
  85. print '</td>';
  86. print '<td style="text-align: center;">';
  87. print '<input type="submit" value="'.$langs->trans('Launch').'" />';
  88. print '</td></tr>';
  89. // Include unpayed invoices
  90. print '<tr><td>'.$langs->trans("IncludeUnpayedInvoices").'</td><td colspan="4">';
  91. print '<input id="selIncluded" type="checkbox" name="unpayed" ';
  92. if (GETPOST('unpayed') == 'on')
  93. print 'checked ';
  94. print '/>';
  95. print '</td></tr>';
  96. // Total Margin
  97. if ($conf->global->COMMISSION_BASE == "MARGIN") {
  98. print '<tr style="font-weight: bold"><td>'.$langs->trans("TotalMargin").'</td><td colspan="4">';
  99. print '<span id="totalBase"></span>'; // set by jquery (see below)
  100. print '</td></tr>';
  101. }
  102. elseif ($conf->global->COMMISSION_BASE == "TURNOVER") {
  103. print '<tr style="font-weight: bold"><td>'.$langs->trans("TurnoverTotal").'</td><td colspan="4">';
  104. print '<span id="totalBase"></span>'; // set by jquery (see below)
  105. print '</td></tr>';
  106. }
  107. // Total Commission
  108. print '<tr style="font-weight: bold"><td>'.$langs->trans("TotalCommission").'</td><td colspan="4">';
  109. print '<span id="totalCommission"></span>'; // set by jquery (see below)
  110. print '</td></tr>';
  111. print "</table>";
  112. print '</form>';
  113. $sql = "SELECT s.nom, s.rowid as socid, s.code_client, s.client, sc.fk_user as agent,";
  114. $sql.= " u.login,";
  115. if ($conf->global->COMMISSION_BASE == "MARGIN") {
  116. $sql.= " sum(case d.product_type when 1 then 0 else (((d.subprice * (1 - d.remise_percent / 100)) - d.buy_price_ht) * d.qty) end) as productBase," ;
  117. $sql.= " sum(case d.product_type when 1 then (((d.subprice * (1 - d.remise_percent / 100)) - d.buy_price_ht) * d.qty) else 0 end) as serviceBase" ;
  118. }
  119. elseif ($conf->global->COMMISSION_BASE == "TURNOVER") {
  120. $sql.= " sum(case d.product_type when 1 then 0 else (((d.subprice * (1 - d.remise_percent / 100))) * d.qty) end) as productBase," ;
  121. $sql.= " sum(case d.product_type when 1 then (((d.subprice * (1 - d.remise_percent / 100))) * d.qty) else 0 end) as serviceBase" ;
  122. }
  123. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  124. $sql.= ", ".MAIN_DB_PREFIX."facture as f";
  125. $sql .= " LEFT JOIN ".MAIN_DB_PREFIX."element_contact e ON e.element_id = f.rowid and e.statut = 4 and e.fk_c_type_contact = ".(empty($conf->global->AGENT_CONTACT_TYPE)?-1:$conf->global->AGENT_CONTACT_TYPE);
  126. $sql.= ", ".MAIN_DB_PREFIX."facturedet as d";
  127. $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  128. $sql.= ", ".MAIN_DB_PREFIX."user as u";
  129. $sql.= " WHERE f.fk_soc = s.rowid";
  130. $sql.= " AND sc.fk_soc = f.fk_soc";
  131. if (! empty($conf->global->AGENT_CONTACT_TYPE))
  132. $sql.= " AND ((e.fk_socpeople IS NULL AND sc.fk_user = u.rowid) OR (e.fk_socpeople IS NOT NULL AND e.fk_socpeople = u.rowid))";
  133. else
  134. $sql .= " AND sc.fk_user = u.rowid";
  135. if (GETPOST('unpayed') == 'on')
  136. $sql.= " AND f.fk_statut > 0";
  137. else
  138. $sql.= " AND f.fk_statut > 1";
  139. $sql.= " AND s.entity = ".$conf->entity;
  140. $sql.= " AND d.fk_facture = f.rowid";
  141. if ($agentid > 0) {
  142. if (! empty($conf->global->AGENT_CONTACT_TYPE))
  143. $sql.= " AND ((e.fk_socpeople IS NULL AND sc.fk_user = ".$agentid.") OR (e.fk_socpeople IS NOT NULL AND e.fk_socpeople = ".$agentid."))";
  144. else
  145. $sql .= " AND sc.fk_user = ".$agentid;
  146. }
  147. if (!empty($startdate))
  148. $sql.= " AND f.datef >= '".$startdate."'";
  149. if (!empty($enddate))
  150. $sql.= " AND f.datef <= '".$enddate."'";
  151. if ($conf->global->COMMISSION_BASE == "MARGIN")
  152. $sql .= " AND d.buy_price_ht IS NOT NULL";
  153. if (($conf->global->COMMISSION_BASE == "MARGIN") && isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1)
  154. $sql .= " AND d.buy_price_ht <> 0";
  155. if ($agentid > 0)
  156. $sql.= " GROUP BY s.rowid";
  157. else
  158. $sql.= " GROUP BY u.rowid";
  159. $sql.= " ORDER BY $sortfield $sortorder ";
  160. //$sql.= $db->plimit($conf->liste_limit +1, $offset);
  161. $result = $db->query($sql);
  162. if ($result)
  163. {
  164. $num = $db->num_rows($result);
  165. print '<br>';
  166. print_barre_liste($langs->trans("CommissionDetails"),$page,$_SERVER["PHP_SELF"],"",$sortfield,$sortorder,'',$num,0,'');
  167. $i = 0;
  168. print "<table class=\"noborder\" width=\"100%\">";
  169. print '<tr class="liste_titre">';
  170. if ($agentid > 0)
  171. print_liste_field_titre($langs->trans("Customer"),$_SERVER["PHP_SELF"],"s.nom","","&amp;agentid=".$agentid,'align="center"',$sortfield,$sortorder);
  172. else
  173. print_liste_field_titre($langs->trans("CommercialAgent"),$_SERVER["PHP_SELF"],"u.login","","&amp;agentid=".$agentid,'align="center"',$sortfield,$sortorder);
  174. // product commission
  175. if ($conf->global->COMMISSION_BASE == "MARGIN")
  176. print_liste_field_titre($langs->trans("ProductMargin"),$_SERVER["PHP_SELF"],"productBase","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
  177. elseif ($conf->global->COMMISSION_BASE == "TURNOVER")
  178. print_liste_field_titre($langs->trans("ProductTurnover"),$_SERVER["PHP_SELF"],"productBase","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
  179. print_liste_field_titre($langs->trans("CommissionRate"),$_SERVER["PHP_SELF"],"","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
  180. print_liste_field_titre($langs->trans("ProductCommission"),$_SERVER["PHP_SELF"],"","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
  181. // service commission
  182. if ($conf->global->COMMISSION_BASE == "MARGIN")
  183. print_liste_field_titre($langs->trans("ServiceMargin"),$_SERVER["PHP_SELF"],"serviceBase","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
  184. elseif ($conf->global->COMMISSION_BASE == "TURNOVER")
  185. print_liste_field_titre($langs->trans("ServiceTurnover"),$_SERVER["PHP_SELF"],"serviceBase","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
  186. print_liste_field_titre($langs->trans("CommissionRate"),$_SERVER["PHP_SELF"],"","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
  187. print_liste_field_titre($langs->trans("ServiceCommission"),$_SERVER["PHP_SELF"],"","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
  188. // total commission
  189. print_liste_field_titre($langs->trans("TotalCommission"),$_SERVER["PHP_SELF"],"","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
  190. print "</tr>\n";
  191. $cumul_base_produit = 0;
  192. $cumul_base_service = 0;
  193. $cumul_commission_produit = 0;
  194. $cumul_commission_service = 0;
  195. $rounding = min($conf->global->MAIN_MAX_DECIMALS_UNIT,$conf->global->MAIN_MAX_DECIMALS_TOT);
  196. if ($num > 0)
  197. {
  198. $var=True;
  199. while ($i < $num && $i < $conf->liste_limit)
  200. {
  201. $objp = $db->fetch_object($result);
  202. $var=!$var;
  203. print "<tr $bc[$var]>";
  204. if ($agentid > 0) {
  205. $companystatic->id=$objp->socid;
  206. $companystatic->nom=$objp->nom;
  207. $companystatic->client=$objp->client;
  208. print "<td>".$companystatic->getNomUrl(1,'customer')."</td>\n";
  209. }
  210. else {
  211. $userstatic->id=$objp->agent;
  212. $userstatic->login=$objp->login;
  213. print "<td>".$userstatic->getLoginUrl(1)."</td>\n";
  214. }
  215. // product commission
  216. $productCommissionRate=(! empty($conf->global->PRODUCT_COMMISSION_RATE)?$conf->global->PRODUCT_COMMISSION_RATE:0);
  217. $productBase=(! empty($objp->productBase)?$objp->productBase:0);
  218. $productCommission = (! empty($productBase)?($productCommissionRate * $productBase / 100):0);
  219. print "<td align=\"right\">".price($productBase)."</td>\n";
  220. print "<td align=\"right\">".price($productCommissionRate)."</td>\n";
  221. print "<td align=\"right\">".price($productCommission)."</td>\n";
  222. // service commission
  223. $serviceCommissionRate=(! empty($conf->global->SERVICE_COMMISSION_RATE)?$conf->global->SERVICE_COMMISSION_RATE:0);
  224. $serviceBase=(! empty($objp->serviceBase)?$objp->serviceBase:0);
  225. $serviceCommission = (! empty($serviceBase)?($serviceCommissionRate * $serviceBase / 100):0);
  226. print "<td align=\"right\">".price($serviceBase)."</td>\n";
  227. print "<td align=\"right\">".price($serviceCommissionRate)."</td>\n";
  228. print "<td align=\"right\">".price($serviceCommission)."</td>\n";
  229. // total commission
  230. print "<td align=\"right\">".price($productCommission + $serviceCommission)."</td>\n";
  231. print "</tr>\n";
  232. $i++;
  233. $cumul_base_produit += round($productBase, $rounding);
  234. $cumul_base_service += round($serviceBase, $rounding);
  235. $cumul_commission_produit += round($productCommission, $rounding);
  236. $cumul_commission_service += round($serviceCommission, $rounding);
  237. }
  238. }
  239. // affichage totaux commission
  240. $var=!$var;
  241. print '<tr '.$bc[$var].' style="border-top: 1px solid #ccc; font-weight: bold">';
  242. print '<td>';
  243. print $langs->trans('Total');
  244. print "</td>";
  245. // product commission
  246. print "<td align=\"right\">".price($cumul_base_produit)."</td>\n";
  247. print "<td align=\"right\">".price((! empty($conf->global->PRODUCT_COMMISSION_RATE)?$conf->global->PRODUCT_COMMISSION_RATE:0))."</td>\n";
  248. print "<td align=\"right\">".price($cumul_commission_produit)."</td>\n";
  249. // service commission
  250. print "<td align=\"right\">".price($cumul_base_service)."</td>\n";
  251. print "<td align=\"right\">".price((! empty($conf->global->SERVICE_COMMISSION_RATE)?$conf->global->SERVICE_COMMISSION_RATE:0))."</td>\n";
  252. print "<td align=\"right\">".price($cumul_commission_service)."</td>\n";
  253. // total commission
  254. print "<td align=\"right\">".price($cumul_commission_produit + $cumul_commission_service)."</td>\n";
  255. print "</tr>\n";
  256. print "</td>";
  257. print "</table>";
  258. }
  259. else
  260. {
  261. dol_print_error($db);
  262. }
  263. $db->free($result);
  264. llxFooter();
  265. $db->close();
  266. ?>
  267. <script type="text/javascript">
  268. $(document).ready(function() {
  269. $("#agentid").change(function() {
  270. $("div.fiche form").submit();
  271. });
  272. $("#selIncluded").change(function() {
  273. $("div.fiche form").submit();
  274. });
  275. $("#totalBase").html("<?php echo price($cumul_base_produit + $cumul_base_service); ?>");
  276. $("#totalCommission").html("<?php echo price($cumul_commission_produit + $cumul_commission_service); ?>");
  277. });
  278. </script>