customerMargins.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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/customerMargins.php
  20. * \ingroup margin
  21. * \brief Page des marges par client
  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. $socid = GETPOST('socid','int');
  34. if (! empty($user->societe_id)) $socid=$user->societe_id;
  35. $result = restrictedArea($user, 'societe','','');
  36. $mesg = '';
  37. $page = GETPOST("page",'int');
  38. if ($page == -1) { $page = 0; }
  39. $offset = $conf->liste_limit * $page;
  40. $pageprev = $page - 1;
  41. $pagenext = $page + 1;
  42. $startdate=$enddate='';
  43. if (!empty($_POST['startdatemonth']))
  44. $startdate = dol_mktime(12, 0, 0, $_POST['startdatemonth'], $_POST['startdateday'], $_POST['startdateyear']);
  45. if (!empty($_POST['enddatemonth']))
  46. $enddate = dol_mktime(12, 0, 0, $_POST['enddatemonth'], $_POST['enddateday'], $_POST['enddateyear']);
  47. /*
  48. * View
  49. */
  50. $companystatic = new Societe($db);
  51. $invoicestatic=new Facture($db);
  52. $form = new Form($db);
  53. llxHeader('',$langs->trans("Margins").' - '.$langs->trans("Clients"));
  54. $text=$langs->trans("Margins");
  55. print_fiche_titre($text);
  56. // Show tabs
  57. $head=marges_prepare_head($user);
  58. $titre=$langs->trans("Margins");
  59. $picto='margin';
  60. dol_fiche_head($head, 'customerMargins', $titre, 0, $picto);
  61. print '<form method="post" name="sel" action="'.$_SERVER['PHP_SELF'].'">';
  62. print '<table class="border" width="100%">';
  63. $client = false;
  64. if ($socid > 0) {
  65. $soc = new Societe($db, $socid);
  66. $soc->fetch($socid);
  67. if ($soc->client)
  68. {
  69. print '<tr><td width="20%">'.$langs->trans('ThirdPartyName').'</td>';
  70. print '<td colspan="4">';
  71. $form->form_thirdparty($_SERVER['PHP_SELF'].'?socid='.$socid,$socid,'socid','client=1 OR client=3',1,0,1);
  72. print '</td></tr>';
  73. $client = true;
  74. if (! $sortorder) $sortorder="DESC";
  75. if (! $sortfield) $sortfield="f.datef";
  76. }
  77. }
  78. else {
  79. print '<tr><td width="20%">'.$langs->trans('ThirdPartyName').'</td>';
  80. print '<td colspan="4">';
  81. $form->form_thirdparty($_SERVER['PHP_SELF'],null,'socid','client=1 OR client=3',1,0,1);
  82. print '</td></tr>';
  83. }
  84. $sortfield = GETPOST("sortfield",'alpha');
  85. $sortorder = GETPOST("sortorder",'alpha');
  86. if (! $sortorder) $sortorder="ASC";
  87. if (! $sortfield)
  88. {
  89. if ($client)
  90. {
  91. $sortfield="f.datef";
  92. $sortorder="DESC";
  93. }
  94. else
  95. {
  96. $sortfield="s.nom";
  97. $sortorder="ASC";
  98. }
  99. }
  100. // Start date
  101. print '<td>'.$langs->trans('StartDate').' ('.$langs->trans("DateValidation").')</td>';
  102. print '<td width="20%">';
  103. $form->select_date($startdate,'startdate','','',1,"sel",1,1);
  104. print '</td>';
  105. print '<td width="20%">'.$langs->trans('EndDate').' ('.$langs->trans("DateValidation").')</td>';
  106. print '<td width="20%">';
  107. $form->select_date($enddate,'enddate','','',1,"sel",1,1);
  108. print '</td>';
  109. print '<td style="text-align: center;">';
  110. print '<input type="submit" class="button" value="'.dol_escape_htmltag($langs->trans('Launch')).'" />';
  111. print '</td></tr>';
  112. print "</table>";
  113. print '<br>';
  114. print '<table class="border" width="100%">';
  115. // Total Margin
  116. print '<tr><td width="20%">'.$langs->trans("TotalMargin").'</td><td colspan="4">';
  117. print '<span id="totalMargin"></span>'; // set by jquery (see below)
  118. print '</td></tr>';
  119. // Margin Rate
  120. if (! empty($conf->global->DISPLAY_MARGIN_RATES)) {
  121. print '<tr><td width="20%">'.$langs->trans("MarginRate").'</td><td colspan="4">';
  122. print '<span id="marginRate"></span>'; // set by jquery (see below)
  123. print '</td></tr>';
  124. }
  125. // Mark Rate
  126. if (! empty($conf->global->DISPLAY_MARK_RATES)) {
  127. print '<tr><td width="20%">'.$langs->trans("MarkRate").'</td><td colspan="4">';
  128. print '<span id="markRate"></span>'; // set by jquery (see below)
  129. print '</td></tr>';
  130. }
  131. print "</table>";
  132. print '</form>';
  133. $sql = "SELECT";
  134. $sql.= " s.rowid as socid, s.nom, s.code_client, s.client,";
  135. if ($client) $sql.= " f.rowid as facid, f.facnumber, f.total as total_ht, f.datef, f.paye, f.fk_statut as statut,";
  136. $sql.= " sum(d.total_ht) as selling_price,";
  137. $sql.= " sum(".$db->ifsql('d.total_ht <=0','d.qty * d.buy_price_ht * -1','d.qty * d.buy_price_ht').") as buying_price,";
  138. $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";
  139. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  140. $sql.= ", ".MAIN_DB_PREFIX."facture as f";
  141. $sql.= ", ".MAIN_DB_PREFIX."facturedet as d";
  142. $sql.= " WHERE f.fk_soc = s.rowid";
  143. $sql.= " AND f.fk_statut > 0";
  144. $sql.= " AND s.entity = ".$conf->entity;
  145. $sql.= " AND d.fk_facture = f.rowid";
  146. if ($client)
  147. $sql.= " AND f.fk_soc = ".$socid;
  148. if (!empty($startdate))
  149. $sql.= " AND f.datef >= '".$db->idate($startdate)."'";
  150. if (!empty($enddate))
  151. $sql.= " AND f.datef <= '".$db->idate($enddate)."'";
  152. $sql .= " AND d.buy_price_ht IS NOT NULL";
  153. if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1)
  154. $sql .= " AND d.buy_price_ht <> 0";
  155. if ($client) $sql.= " GROUP BY s.rowid, s.nom, s.code_client, s.client, f.rowid, f.facnumber, f.total, f.datef, f.paye, f.fk_statut";
  156. else $sql.= " GROUP BY s.rowid, s.nom, s.code_client, s.client";
  157. $sql.=$db->order($sortfield,$sortorder);
  158. // TODO: calculate total to display then restore pagination
  159. //$sql.= $db->plimit($conf->liste_limit +1, $offset);
  160. dol_syslog('margin::customerMargins.php sql='.$sql,LOG_DEBUG);
  161. $result = $db->query($sql);
  162. if ($result)
  163. {
  164. $num = $db->num_rows($result);
  165. print '<br>';
  166. print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"",$sortfield,$sortorder,'',0,0,'');
  167. $i = 0;
  168. print "<table class=\"noborder\" width=\"100%\">";
  169. print '<tr class="liste_titre">';
  170. if (! empty($client)) {
  171. print_liste_field_titre($langs->trans("Invoice"),$_SERVER["PHP_SELF"],"f.facnumber","","&amp;socid=".$socid,'',$sortfield,$sortorder);
  172. print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&amp;socid=".$socid,'align="center"',$sortfield,$sortorder);
  173. }
  174. else
  175. print_liste_field_titre($langs->trans("Customer"),$_SERVER["PHP_SELF"],"s.nom","","&amp;socid=".$socid,'',$sortfield,$sortorder);
  176. print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&amp;socid=".$socid,'align="right"',$sortfield,$sortorder);
  177. print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buying_price","","&amp;socid=".$socid,'align="right"',$sortfield,$sortorder);
  178. print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&amp;socid=".$socid,'align="right"',$sortfield,$sortorder);
  179. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  180. print_liste_field_titre($langs->trans("MarginRate"),$_SERVER["PHP_SELF"],"","","&amp;socid=".$socid,'align="right"',$sortfield,$sortorder);
  181. if (! empty($conf->global->DISPLAY_MARK_RATES))
  182. print_liste_field_titre($langs->trans("MarkRate"),$_SERVER["PHP_SELF"],"","","&amp;socid=".$socid,'align="right"',$sortfield,$sortorder);
  183. print "</tr>\n";
  184. $cumul_achat = 0;
  185. $cumul_vente = 0;
  186. $rounding = min($conf->global->MAIN_MAX_DECIMALS_UNIT,$conf->global->MAIN_MAX_DECIMALS_TOT);
  187. if ($num > 0)
  188. {
  189. $var=True;
  190. while ($i < $num /*&& $i < $conf->liste_limit*/)
  191. {
  192. $objp = $db->fetch_object($result);
  193. $pa = $objp->buying_price;
  194. $pv = $objp->selling_price;
  195. $marge = $objp->marge;
  196. if ($marge < 0)
  197. {
  198. $marginRate = ($pa != 0)?-1*(100 * $marge / $pa):'' ;
  199. $markRate = ($pv != 0)?-1*(100 * $marge / $pv):'' ;
  200. }
  201. else
  202. {
  203. $marginRate = ($pa != 0)?(100 * $marge / $pa):'' ;
  204. $markRate = ($pv != 0)?(100 * $marge / $pv):'' ;
  205. }
  206. $var=!$var;
  207. print "<tr ".$bc[$var].">";
  208. if ($client) {
  209. print '<td>';
  210. $invoicestatic->id=$objp->facid;
  211. $invoicestatic->ref=$objp->facnumber;
  212. print $invoicestatic->getNomUrl(1);
  213. print "</td>\n";
  214. print "<td align=\"center\">";
  215. print dol_print_date($db->jdate($objp->datef),'day')."</td>";
  216. }
  217. else {
  218. $companystatic->id=$objp->socid;
  219. $companystatic->nom=$objp->nom;
  220. $companystatic->client=$objp->client;
  221. print "<td>".$companystatic->getNomUrl(1,'customer')."</td>\n";
  222. }
  223. print "<td align=\"right\">".price($pv, null, null, null, null, $rounding)."</td>\n";
  224. print "<td align=\"right\">".price($pa, null, null, null, null, $rounding)."</td>\n";
  225. print "<td align=\"right\">".price($marge, null, null, null, null, $rounding)."</td>\n";
  226. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  227. print "<td align=\"right\">".(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%")."</td>\n";
  228. if (! empty($conf->global->DISPLAY_MARK_RATES))
  229. print "<td align=\"right\">".(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%")."</td>\n";
  230. print "</tr>\n";
  231. $i++;
  232. $cumul_achat += $objp->buying_price;
  233. $cumul_vente += $objp->selling_price;
  234. }
  235. }
  236. // affichage totaux marges
  237. $var=!$var;
  238. $totalMargin = $cumul_vente - $cumul_achat;
  239. if ($totalMargin < 0)
  240. {
  241. $marginRate = ($cumul_achat != 0)?-1*(100 * $totalMargin / $cumul_achat):'';
  242. $markRate = ($cumul_vente != 0)?-1*(100 * $totalMargin / $cumul_vente):'';
  243. }
  244. else
  245. {
  246. $marginRate = ($cumul_achat != 0)?(100 * $totalMargin / $cumul_achat):'';
  247. $markRate = ($cumul_vente != 0)?(100 * $totalMargin / $cumul_vente):'';
  248. }
  249. print '<tr class="liste_total">';
  250. if ($client)
  251. print '<td colspan=2>';
  252. else
  253. print '<td>';
  254. print $langs->trans('TotalMargin')."</td>";
  255. print "<td align=\"right\">".price($cumul_vente, null, null, null, null, $rounding)."</td>\n";
  256. print "<td align=\"right\">".price($cumul_achat, null, null, null, null, $rounding)."</td>\n";
  257. print "<td align=\"right\">".price($totalMargin, null, null, null, null, $rounding)."</td>\n";
  258. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  259. print "<td align=\"right\">".(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%")."</td>\n";
  260. if (! empty($conf->global->DISPLAY_MARK_RATES))
  261. print "<td align=\"right\">".(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%")."</td>\n";
  262. print "</tr>\n";
  263. print "</table>";
  264. }
  265. else
  266. {
  267. dol_print_error($db);
  268. }
  269. $db->free($result);
  270. llxFooter();
  271. $db->close();
  272. ?>
  273. <script type="text/javascript">
  274. $(document).ready(function() {
  275. $("#socid").change(function() {
  276. $("div.fiche form").submit();
  277. });
  278. $("#totalMargin").html("<?php echo price($totalMargin, null, null, null, null, $rounding); ?>");
  279. $("#marginRate").html("<?php echo (($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%"); ?>");
  280. $("#markRate").html("<?php echo (($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%"); ?>");
  281. });
  282. </script>