customerMargins.php 12 KB

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