agentMargins.php 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. <?php
  2. /* Copyright (C) 2012-2013 Christophe Battarel <christophe.battarel@altairis.fr>
  3. * Copyright (C) 2014 Ferran Marcet <fmarcet@2byte.es>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 3 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /**
  19. * \file htdocs/margin/agentMargins.php
  20. * \ingroup margin
  21. * \brief Page des marges par agent commercial
  22. */
  23. require '../main.inc.php';
  24. require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
  25. require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
  26. require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
  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("margins");
  32. // Security check
  33. $agentid = GETPOST('agentid','int');
  34. $mesg = '';
  35. $sortfield = GETPOST("sortfield",'alpha');
  36. $sortorder = GETPOST("sortorder",'alpha');
  37. if (! $sortorder) $sortorder="ASC";
  38. if (! $sortfield)
  39. {
  40. if ($agentid > 0)
  41. $sortfield="s.nom";
  42. else
  43. $sortfield="u.lastname";
  44. }
  45. $page = GETPOST("page",'int');
  46. if ($page == -1) { $page = 0; }
  47. $offset = $conf->liste_limit * $page;
  48. $pageprev = $page - 1;
  49. $pagenext = $page + 1;
  50. $startdate=$enddate='';
  51. if (!empty($_POST['startdatemonth']))
  52. $startdate = dol_mktime(12, 0, 0, $_POST['startdatemonth'], $_POST['startdateday'], $_POST['startdateyear']);
  53. if (!empty($_POST['enddatemonth']))
  54. $enddate = dol_mktime(12, 0, 0, $_POST['enddatemonth'], $_POST['enddateday'], $_POST['enddateyear']);
  55. /*
  56. * View
  57. */
  58. $userstatic = new User($db);
  59. $companystatic = new Societe($db);
  60. $invoicestatic=new Facture($db);
  61. $form = new Form($db);
  62. llxHeader('',$langs->trans("Margins").' - '.$langs->trans("Agents"));
  63. $text=$langs->trans("Margins");
  64. print_fiche_titre($text);
  65. // Show tabs
  66. $head=marges_prepare_head($user);
  67. $titre=$langs->trans("Margins");
  68. $picto='margin';
  69. dol_fiche_head($head, 'agentMargins', $titre, 0, $picto);
  70. print '<form method="post" name="sel" action="'.$_SERVER['PHP_SELF'].'">';
  71. print '<table class="border" width="100%">';
  72. print '<tr><td width="20%">'.$langs->trans('CommercialAgent').'</td>';
  73. print '<td colspan="4">';
  74. print $form->select_dolusers($agentid,'agentid',1);
  75. print '</td></tr>';
  76. // Start date
  77. print '<td>'.$langs->trans('StartDate').' ('.$langs->trans("DateValidation").')</td>';
  78. print '<td width="20%">';
  79. $form->select_date($startdate,'startdate','','',1,"sel",1,1);
  80. print '</td>';
  81. print '<td width="20%">'.$langs->trans('EndDate').' ('.$langs->trans("DateValidation").')</td>';
  82. print '<td width="20%">';
  83. $form->select_date($enddate,'enddate','','',1,"sel",1,1);
  84. print '</td>';
  85. print '<td style="text-align: center;">';
  86. print '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans('Launch')).'" />';
  87. print '</td></tr>';
  88. print "</table>";
  89. print '</form>';
  90. $sql = "SELECT";
  91. if ($agentid > 0) $sql.= " s.rowid as socid, s.nom, s.code_client, s.client,";
  92. $sql.= " u.rowid as agent, u.login, u.lastname, u.firstname,";
  93. $sql.= " sum(d.total_ht) as selling_price,";
  94. $sql.= " sum(".$db->ifsql('d.total_ht <=0','d.qty * d.buy_price_ht * -1','d.qty * d.buy_price_ht').") as buying_price,";
  95. $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" ;
  96. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  97. $sql.= ", ".MAIN_DB_PREFIX."facture as f";
  98. $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);
  99. $sql.= ", ".MAIN_DB_PREFIX."facturedet as d";
  100. $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  101. $sql.= ", ".MAIN_DB_PREFIX."user as u";
  102. $sql.= " WHERE f.fk_soc = s.rowid";
  103. $sql.= " AND sc.fk_soc = f.fk_soc";
  104. if (! empty($conf->global->AGENT_CONTACT_TYPE))
  105. $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))";
  106. else
  107. $sql .= " AND sc.fk_user = u.rowid";
  108. $sql.= " AND f.fk_statut > 0";
  109. $sql.= " AND s.entity = ".$conf->entity;
  110. $sql.= " AND d.fk_facture = f.rowid";
  111. if ($agentid > 0) {
  112. if (! empty($conf->global->AGENT_CONTACT_TYPE))
  113. $sql.= " AND ((e.fk_socpeople IS NULL AND sc.fk_user = ".$agentid.") OR (e.fk_socpeople IS NOT NULL AND e.fk_socpeople = ".$agentid."))";
  114. else
  115. $sql .= " AND sc.fk_user = ".$agentid;
  116. }
  117. if (!empty($startdate))
  118. $sql.= " AND f.datef >= '".$db->idate($startdate)."'";
  119. if (!empty($enddate))
  120. $sql.= " AND f.datef <= '".$db->idate($enddate)."'";
  121. $sql .= " AND d.buy_price_ht IS NOT NULL";
  122. if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) $sql .= " AND d.buy_price_ht <> 0";
  123. if ($agentid > 0) $sql.= " GROUP BY s.rowid, s.nom, s.code_client, s.client, u.rowid, u.login, u.lastname, u.firstname";
  124. else $sql.= " GROUP BY u.rowid, u.login, u.lastname, u.firstname";
  125. $sql.=$db->order($sortfield,$sortorder);
  126. // TODO: calculate total to display then restore pagination
  127. //$sql.= $db->plimit($conf->liste_limit +1, $offset);
  128. dol_syslog('margin::agentMargins.php sql='.$sql,LOG_DEBUG);
  129. $result = $db->query($sql);
  130. if ($result)
  131. {
  132. $num = $db->num_rows($result);
  133. print '<br>';
  134. print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"",$sortfield,$sortorder,'',0,0,'');
  135. $i = 0;
  136. print "<table class=\"noborder\" width=\"100%\">";
  137. print '<tr class="liste_titre">';
  138. if ($agentid > 0)
  139. print_liste_field_titre($langs->trans("Customer"),$_SERVER["PHP_SELF"],"s.nom","","&amp;agentid=".$agentid,'',$sortfield,$sortorder);
  140. else
  141. print_liste_field_titre($langs->trans("CommercialAgent"),$_SERVER["PHP_SELF"],"u.lastname","","&amp;agentid=".$agentid,'',$sortfield,$sortorder);
  142. print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
  143. print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buying_price","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
  144. print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
  145. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  146. print_liste_field_titre($langs->trans("MarginRate"),$_SERVER["PHP_SELF"],"","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
  147. if (! empty($conf->global->DISPLAY_MARK_RATES))
  148. print_liste_field_titre($langs->trans("MarkRate"),$_SERVER["PHP_SELF"],"","","&amp;agentid=".$agentid,'align="right"',$sortfield,$sortorder);
  149. print "</tr>\n";
  150. $rounding = min($conf->global->MAIN_MAX_DECIMALS_UNIT,$conf->global->MAIN_MAX_DECIMALS_TOT);
  151. if ($num > 0)
  152. {
  153. $var=true;
  154. while ($i < $num /*&& $i < $conf->liste_limit*/)
  155. {
  156. $objp = $db->fetch_object($result);
  157. $pa = $objp->buying_price;
  158. $pv = $objp->selling_price;
  159. $marge = $objp->marge;
  160. if ($marge < 0)
  161. {
  162. $marginRate = ($pa != 0)?-1*(100 * $marge / $pa):'' ;
  163. $markRate = ($pv != 0)?-1*(100 * $marge / $pv):'' ;
  164. }
  165. else
  166. {
  167. $marginRate = ($pa != 0)?(100 * $marge / $pa):'' ;
  168. $markRate = ($pv != 0)?(100 * $marge / $pv):'' ;
  169. }
  170. $var=!$var;
  171. print "<tr ".$bc[$var].">";
  172. if ($agentid > 0) {
  173. $companystatic->id=$objp->socid;
  174. $companystatic->nom=$objp->nom;
  175. $companystatic->client=$objp->client;
  176. print "<td>".$companystatic->getNomUrl(1,'customer')."</td>\n";
  177. }
  178. else {
  179. $userstatic->fetch($objp->agent);
  180. print "<td>".$userstatic->getFullName($langs,0,0,0)."</td>\n";
  181. }
  182. print "<td align=\"right\">".price($pv, null, null, null, null, $rounding)."</td>\n";
  183. print "<td align=\"right\">".price($pa, null, null, null, null, $rounding)."</td>\n";
  184. print "<td align=\"right\">".price($marge, null, null, null, null, $rounding)."</td>\n";
  185. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  186. print "<td align=\"right\">".(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%")."</td>\n";
  187. if (! empty($conf->global->DISPLAY_MARK_RATES))
  188. print "<td align=\"right\">".(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%")."</td>\n";
  189. print "</tr>\n";
  190. $i++;
  191. }
  192. }
  193. print "</table>";
  194. }
  195. else
  196. {
  197. dol_print_error($db);
  198. }
  199. $db->free($result);
  200. llxFooter();
  201. $db->close();
  202. ?>
  203. <script type="text/javascript">
  204. $(document).ready(function() {
  205. $("#agentid").change(function() {
  206. $("div.fiche form").submit();
  207. });
  208. });
  209. </script>