PageRenderTime 49ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/margin/customerMargins.php

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