agentMargins.php 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. <?php
  2. /* Copyright (C) 2012-2013 Christophe Battarel <christophe.battarel@altairis.fr>
  3. * Copyright (C) 2014 Ferran Marcet <fmarcet@2byte.es>
  4. * Copyright (C) 2015 Marcos García <marcosgdf@gmail.com>
  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 <http://www.gnu.org/licenses/>.
  18. */
  19. /**
  20. * \file htdocs/margin/agentMargins.php
  21. * \ingroup margin
  22. * \brief Page des marges par agent commercial
  23. */
  24. require '../main.inc.php';
  25. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  26. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  27. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  28. require_once DOL_DOCUMENT_ROOT.'/margin/lib/margins.lib.php';
  29. $langs->load("companies");
  30. $langs->load("bills");
  31. $langs->load("products");
  32. $langs->load("margins");
  33. // Security check
  34. if ($user->rights->margins->read->all) {
  35. $agentid = GETPOST('agentid', 'int');
  36. } else {
  37. $agentid = $user->id;
  38. }
  39. $mesg = '';
  40. $sortfield = GETPOST("sortfield",'alpha');
  41. $sortorder = GETPOST("sortorder",'alpha');
  42. if (! $sortorder) $sortorder="ASC";
  43. if (! $sortfield)
  44. {
  45. if ($agentid > 0)
  46. $sortfield="s.nom";
  47. else
  48. $sortfield="u.lastname";
  49. }
  50. $page = GETPOST("page",'int');
  51. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  52. $offset = $conf->liste_limit * $page;
  53. $pageprev = $page - 1;
  54. $pagenext = $page + 1;
  55. $startdate=$enddate='';
  56. if (!empty($_POST['startdatemonth']))
  57. $startdate = dol_mktime(0, 0, 0, $_POST['startdatemonth'], $_POST['startdateday'], $_POST['startdateyear']);
  58. if (!empty($_POST['enddatemonth']))
  59. $enddate = dol_mktime(23, 59, 59, $_POST['enddatemonth'], $_POST['enddateday'], $_POST['enddateyear']);
  60. /*
  61. * View
  62. */
  63. $userstatic = new User($db);
  64. $companystatic = new Societe($db);
  65. $invoicestatic=new Facture($db);
  66. $form = new Form($db);
  67. llxHeader('',$langs->trans("Margins").' - '.$langs->trans("Agents"));
  68. $text=$langs->trans("Margins");
  69. //print load_fiche_titre($text);
  70. // Show tabs
  71. $head=marges_prepare_head($user);
  72. $titre=$langs->trans("Margins");
  73. $picto='margin';
  74. print '<form method="post" name="sel" action="'.$_SERVER['PHP_SELF'].'">';
  75. dol_fiche_head($head, 'agentMargins', $titre, 0, $picto);
  76. print '<table class="border" width="100%">';
  77. print '<tr><td class="titlefield">'.$langs->trans('SalesRepresentative').'</td>';
  78. print '<td class="maxwidthonsmartphone" colspan="4">';
  79. print $form->select_dolusers($agentid, 'agentid', 1, '', $user->rights->margins->read->all ? 0 : 1, '', '', 0, 0, 0, '', 0, '', 'maxwidth300');
  80. print '</td></tr>';
  81. // Start date
  82. print '<td>'.$langs->trans('DateStart').' ('.$langs->trans("DateValidation").')</td>';
  83. print '<td>';
  84. $form->select_date($startdate,'startdate','','',1,"sel",1,1);
  85. print '</td>';
  86. print '<td>'.$langs->trans('DateEnd').' ('.$langs->trans("DateValidation").')</td>';
  87. print '<td>';
  88. $form->select_date($enddate,'enddate','','',1,"sel",1,1);
  89. print '</td>';
  90. print '<td style="text-align: center;">';
  91. print '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans('Refresh')).'" />';
  92. print '</td></tr>';
  93. print "</table>";
  94. dol_fiche_end();
  95. print '</form>';
  96. $sql = "SELECT";
  97. if ($agentid > 0) $sql.= " s.rowid as socid, s.nom as name, s.code_client, s.client,";
  98. $sql.= " u.rowid as agent, u.login, u.lastname, u.firstname,";
  99. $sql.= " sum(d.total_ht) as selling_price,";
  100. // Note: qty and buy_price_ht is always positive (if not your database may be corrupted, you can update this)
  101. $sql.= " sum(".$db->ifsql('d.total_ht < 0','d.qty * d.buy_price_ht * -1','d.qty * d.buy_price_ht').") as buying_price,";
  102. $sql.= " sum(".$db->ifsql('d.total_ht < 0','-1 * (abs(d.total_ht) - (d.buy_price_ht * d.qty))','d.total_ht - (d.buy_price_ht * d.qty)').") as marge" ;
  103. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  104. $sql.= ", ".MAIN_DB_PREFIX."facture as f";
  105. $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);
  106. $sql.= ", ".MAIN_DB_PREFIX."facturedet as d";
  107. $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  108. $sql.= ", ".MAIN_DB_PREFIX."user as u";
  109. $sql.= " WHERE f.fk_soc = s.rowid";
  110. $sql.= ' AND f.entity IN ('.getEntity('facture').')';
  111. $sql.= " AND sc.fk_soc = f.fk_soc";
  112. $sql.= " AND (d.product_type = 0 OR d.product_type = 1)";
  113. if (! empty($conf->global->AGENT_CONTACT_TYPE))
  114. $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))";
  115. else
  116. $sql .= " AND sc.fk_user = u.rowid";
  117. $sql.= " AND f.fk_statut > 0";
  118. $sql.= ' AND s.entity IN ('.getEntity('societe').')';
  119. $sql.= " AND d.fk_facture = f.rowid";
  120. if ($agentid > 0) {
  121. if (! empty($conf->global->AGENT_CONTACT_TYPE))
  122. $sql.= " AND ((e.fk_socpeople IS NULL AND sc.fk_user = ".$agentid.") OR (e.fk_socpeople IS NOT NULL AND e.fk_socpeople = ".$agentid."))";
  123. else
  124. $sql .= " AND sc.fk_user = ".$agentid;
  125. }
  126. if (!empty($startdate))
  127. $sql.= " AND f.datef >= '".$db->idate($startdate)."'";
  128. if (!empty($enddate))
  129. $sql.= " AND f.datef <= '".$db->idate($enddate)."'";
  130. $sql .= " AND d.buy_price_ht IS NOT NULL";
  131. if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) $sql .= " AND d.buy_price_ht <> 0";
  132. if ($agentid > 0) $sql.= " GROUP BY s.rowid, s.nom, s.code_client, s.client, u.rowid, u.login, u.lastname, u.firstname";
  133. else $sql.= " GROUP BY u.rowid, u.login, u.lastname, u.firstname";
  134. $sql.=$db->order($sortfield,$sortorder);
  135. // TODO: calculate total to display then restore pagination
  136. //$sql.= $db->plimit($conf->liste_limit +1, $offset);
  137. print '<br>';
  138. print img_info('').' '.$langs->trans("MarginPerSaleRepresentativeWarning").'<br>';
  139. dol_syslog('margin::agentMargins.php', LOG_DEBUG);
  140. $result = $db->query($sql);
  141. if ($result)
  142. {
  143. $num = $db->num_rows($result);
  144. print '<br>';
  145. print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"",$sortfield,$sortorder,'',$num,$num,'');
  146. if ($conf->global->MARGIN_TYPE == "1")
  147. $labelcostprice=$langs->trans('BuyingPrice');
  148. else // value is 'costprice' or 'pmp'
  149. $labelcostprice=$langs->trans('CostPrice');
  150. $i = 0;
  151. print "<table class=\"noborder\" width=\"100%\">";
  152. print '<tr class="liste_titre">';
  153. if ($agentid > 0)
  154. print_liste_field_titre($langs->trans("Customer"),$_SERVER["PHP_SELF"],"s.nom","","&amp;agentid=".$agentid,'',$sortfield,$sortorder);
  155. else
  156. print_liste_field_titre($langs->trans("SalesRepresentative"),$_SERVER["PHP_SELF"],"u.lastname","","&amp;agentid=".$agentid,'',$sortfield,$sortorder);
  157. print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
  158. print_liste_field_titre($labelcostprice,$_SERVER["PHP_SELF"],"buying_price","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
  159. print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
  160. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  161. print_liste_field_titre($langs->trans("MarginRate"),$_SERVER["PHP_SELF"],"","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
  162. if (! empty($conf->global->DISPLAY_MARK_RATES))
  163. print_liste_field_titre($langs->trans("MarkRate"),$_SERVER["PHP_SELF"],"","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
  164. print "</tr>\n";
  165. $rounding = min($conf->global->MAIN_MAX_DECIMALS_UNIT,$conf->global->MAIN_MAX_DECIMALS_TOT);
  166. if ($num > 0)
  167. {
  168. $var=true;
  169. while ($i < $num /*&& $i < $conf->liste_limit*/)
  170. {
  171. $objp = $db->fetch_object($result);
  172. $pa = $objp->buying_price;
  173. $pv = $objp->selling_price;
  174. $marge = $objp->marge;
  175. if ($marge < 0)
  176. {
  177. $marginRate = ($pa != 0)?-1*(100 * $marge / $pa):'' ;
  178. $markRate = ($pv != 0)?-1*(100 * $marge / $pv):'' ;
  179. }
  180. else
  181. {
  182. $marginRate = ($pa != 0)?(100 * $marge / $pa):'' ;
  183. $markRate = ($pv != 0)?(100 * $marge / $pv):'' ;
  184. }
  185. print '<tr class="oddeven">';
  186. if ($agentid > 0) {
  187. $companystatic->id=$objp->socid;
  188. $companystatic->name=$objp->name;
  189. $companystatic->client=$objp->client;
  190. print "<td>".$companystatic->getNomUrl(1,'customer')."</td>\n";
  191. }
  192. else {
  193. $userstatic->fetch($objp->agent);
  194. print "<td>".$userstatic->getFullName($langs,0,0,0)."</td>\n";
  195. }
  196. print "<td align=\"right\">".price($pv, null, null, null, null, $rounding)."</td>\n";
  197. print "<td align=\"right\">".price($pa, null, null, null, null, $rounding)."</td>\n";
  198. print "<td align=\"right\">".price($marge, null, null, null, null, $rounding)."</td>\n";
  199. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  200. print "<td align=\"right\">".(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%")."</td>\n";
  201. if (! empty($conf->global->DISPLAY_MARK_RATES))
  202. print "<td align=\"right\">".(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%")."</td>\n";
  203. print "</tr>\n";
  204. $i++;
  205. }
  206. }
  207. print "</table>";
  208. }
  209. else
  210. {
  211. dol_print_error($db);
  212. }
  213. $db->free($result);
  214. print "\n".'<script type="text/javascript">
  215. $(document).ready(function() {
  216. $("#agentid").change(function() {
  217. $("div.fiche form").submit();
  218. });
  219. });
  220. </script>'."\n";
  221. llxFooter();
  222. $db->close();