PageRenderTime 53ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/margin/tabs/productMargins.php

https://bitbucket.org/speedealing/speedealing
PHP | 263 lines | 191 code | 37 blank | 35 comment | 34 complexity | 6c48fbb8c1177f5151a33afd174c6c25 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/tabs/productMargins.php
  19. * \ingroup product margins
  20. * \brief Page des marges des factures clients pour un produit
  21. */
  22. require '../../main.inc.php';
  23. require_once DOL_DOCUMENT_ROOT.'/core/lib/product.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. $langs->load("companies");
  27. $langs->load("bills");
  28. $langs->load("products");
  29. $langs->load("margins");
  30. $id = GETPOST('id', 'int');
  31. $ref = GETPOST('ref', 'alpha');
  32. $action=GETPOST('action','alpha');
  33. $confirm=GETPOST('confirm','alpha');
  34. // Security check
  35. $fieldvalue = (! empty($id) ? $id : (! empty($ref) ? $ref : ''));
  36. $fieldtype = (! empty($ref) ? 'ref' : 'rowid');
  37. if (! empty($user->societe_id)) $socid=$user->societe_id;
  38. $result=restrictedArea($user,'produit|service',$fieldvalue,'product&product','','',$fieldtype);
  39. $object = new Product($db);
  40. $mesg = '';
  41. $sortfield = GETPOST("sortfield",'alpha');
  42. $sortorder = GETPOST("sortorder",'alpha');
  43. $page = GETPOST("page",'int');
  44. if ($page == -1) { $page = 0; }
  45. $offset = $conf->liste_limit * $page;
  46. $pageprev = $page - 1;
  47. $pagenext = $page + 1;
  48. if (! $sortorder) $sortorder="DESC";
  49. if (! $sortfield) $sortfield="f.datef";
  50. /*
  51. * View
  52. */
  53. $invoicestatic=new Facture($db);
  54. $form = new Form($db);
  55. if ($id > 0 || ! empty($ref))
  56. {
  57. $result = $object->fetch($id, $ref);
  58. llxHeader("","",$langs->trans("CardProduct".$object->type));
  59. /*
  60. * En mode visu
  61. */
  62. if ($result > 0)
  63. {
  64. $head=product_prepare_head($object, $user);
  65. $titre=$langs->trans("CardProduct".$object->type);
  66. $picto=($object->type==1?'service':'product');
  67. dol_fiche_head($head, 'margin', $titre, 0, $picto);
  68. print '<table class="border" width="100%">';
  69. // Reference
  70. print '<tr>';
  71. print '<td width="30%">'.$langs->trans("Ref").'</td><td colspan="3">';
  72. print $form->showrefnav($object,'ref','',1,'ref');
  73. print '</td>';
  74. print '</tr>';
  75. // Libelle
  76. print '<tr><td>'.$langs->trans("Label").'</td><td colspan="3">'.$object->libelle.'</td>';
  77. print '</tr>';
  78. // Status (to sell)
  79. print '<tr><td>'.$langs->trans("Status").' ('.$langs->trans("Sell").')</td><td colspan="3">';
  80. print $object->getLibStatut(2,0);
  81. print '</td></tr>';
  82. // Status (to buy)
  83. print '<tr><td>'.$langs->trans("Status").' ('.$langs->trans("Buy").')</td><td colspan="3">';
  84. print $object->getLibStatut(2,1);
  85. print '</td></tr>';
  86. // Total Margin
  87. print '<tr><td>'.$langs->trans("TotalMargin").'</td><td colspan="3">';
  88. print '<span id="totalMargin"></span>'; // set by jquery (see below)
  89. print '</td></tr>';
  90. // Margin Rate
  91. if (! empty($conf->global->DISPLAY_MARGIN_RATES)) {
  92. print '<tr><td>'.$langs->trans("MarginRate").'</td><td colspan="3">';
  93. print '<span id="marginRate"></span>'; // set by jquery (see below)
  94. print '</td></tr>';
  95. }
  96. // Mark Rate
  97. if (! empty($conf->global->DISPLAY_MARK_RATES)) {
  98. print '<tr><td>'.$langs->trans("MarkRate").'</td><td colspan="3">';
  99. print '<span id="markRate"></span>'; // set by jquery (see below)
  100. print '</td></tr>';
  101. }
  102. print "</table>";
  103. print '</div>';
  104. $sql = "SELECT DISTINCT s.nom, s.rowid as socid, s.code_client,";
  105. $sql.= " f.facnumber, f.total as total_ht,";
  106. $sql.= " (d.subprice * d.qty * (1 - d.remise_percent / 100)) as selling_price, (d.buy_price_ht * d.qty) as buying_price, d.qty, ((d.subprice - d.buy_price_ht) * d.qty) as marge," ;
  107. $sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid";
  108. if (!$user->rights->societe->client->voir && !$socid) $sql.= ", sc.fk_soc, sc.fk_user ";
  109. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  110. $sql.= ", ".MAIN_DB_PREFIX."facture as f";
  111. $sql.= ", ".MAIN_DB_PREFIX."facturedet as d";
  112. if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  113. $sql.= " WHERE f.fk_soc = s.rowid";
  114. $sql.= " AND f.fk_statut > 0";
  115. $sql.= " AND s.entity = ".$conf->entity;
  116. $sql.= " AND d.fk_facture = f.rowid";
  117. $sql.= " AND d.fk_product =".$object->id;
  118. if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
  119. if (! empty($socid)) $sql.= " AND f.fk_soc = $socid";
  120. $sql .= " AND d.buy_price_ht IS NOT NULL";
  121. if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1)
  122. $sql .= " AND d.buy_price_ht <> 0";
  123. $sql.= " ORDER BY $sortfield $sortorder ";
  124. $sql.= $db->plimit($conf->liste_limit +1, $offset);
  125. $result = $db->query($sql);
  126. if ($result)
  127. {
  128. $num = $db->num_rows($result);
  129. print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"&amp;id=$object->id",$sortfield,$sortorder,'',$num,0,'');
  130. $i = 0;
  131. print "<table class=\"noborder\" width=\"100%\">";
  132. print '<tr class="liste_titre">';
  133. print_liste_field_titre($langs->trans("Invoice"),$_SERVER["PHP_SELF"],"f.facnumber","","&amp;id=".$object->id,'',$sortfield,$sortorder);
  134. print_liste_field_titre($langs->trans("Company"),$_SERVER["PHP_SELF"],"s.nom","","&amp;id=".$object->id,'',$sortfield,$sortorder);
  135. print_liste_field_titre($langs->trans("CustomerCode"),$_SERVER["PHP_SELF"],"s.code_client","","&amp;id=".$object->id,'',$sortfield,$sortorder);
  136. print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&amp;id=".$object->id,'align="center"',$sortfield,$sortorder);
  137. print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  138. print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buying_price","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  139. print_liste_field_titre($langs->trans("Qty"),$_SERVER["PHP_SELF"],"d.qty","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  140. print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  141. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  142. print_liste_field_titre($langs->trans("MarginRate"),$_SERVER["PHP_SELF"],"","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  143. if (! empty($conf->global->DISPLAY_MARK_RATES))
  144. print_liste_field_titre($langs->trans("MarkRate"),$_SERVER["PHP_SELF"],"","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  145. print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"f.paye,f.fk_statut","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  146. print "</tr>\n";
  147. $cumul_achat = 0;
  148. $cumul_vente = 0;
  149. $cumul_qty = 0;
  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. $var=!$var;
  158. $marginRate = ($objp->buying_price != 0)?(100 * round($objp->marge / $objp->buying_price, 5)):'' ;
  159. $markRate = ($objp->selling_price != 0)?(100 * round($objp->marge / $objp->selling_price, 5)):'' ;
  160. print "<tr $bc[$var]>";
  161. print '<td>';
  162. $invoicestatic->id=$objp->facid;
  163. $invoicestatic->ref=$objp->facnumber;
  164. print $invoicestatic->getNomUrl(1);
  165. print "</td>\n";
  166. print '<td><a href="'.DOL_URL_ROOT.'/comm/fiche.php?socid='.$objp->socid.'">'.img_object($langs->trans("ShowCompany"),"company").' '.dol_trunc($objp->nom,44).'</a></td>';
  167. print "<td>".$objp->code_client."</td>\n";
  168. print "<td align=\"center\">";
  169. print dol_print_date($db->jdate($objp->datef),'day')."</td>";
  170. print "<td align=\"right\">".price($objp->selling_price)."</td>\n";
  171. print "<td align=\"right\">".price($objp->buying_price)."</td>\n";
  172. print "<td align=\"right\">".price($objp->qty)."</td>\n";
  173. print "<td align=\"right\">".price($objp->marge)."</td>\n";
  174. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  175. print "<td align=\"right\">".(($marginRate === '')?'n/a':price($marginRate)."%")."</td>\n";
  176. if (! empty($conf->global->DISPLAY_MARK_RATES))
  177. print "<td align=\"right\">".(($markRate === '')?'n/a':price($markRate)."%")."</td>\n";
  178. print '<td align="right">'.$invoicestatic->LibStatut($objp->paye,$objp->statut,5).'</td>';
  179. print "</tr>\n";
  180. $i++;
  181. $cumul_achat += round($objp->buying_price, $rounding);
  182. $cumul_vente += round($objp->selling_price, $rounding);
  183. $cumul_qty += $objp->qty;
  184. }
  185. }
  186. // affichage totaux marges
  187. $var=!$var;
  188. $totalMargin = $cumul_vente - $cumul_achat;
  189. $marginRate = ($cumul_achat != 0)?(100 * round($totalMargin / $cumul_achat, 5)):'';
  190. $markRate = ($cumul_vente != 0)?(100 * round($totalMargin / $cumul_vente, 5)):'';
  191. print '<tr '.$bc[$var].' style="border-top: 1px solid #ccc; font-weight: bold">';
  192. print '<td colspan=4>'.$langs->trans('TotalMargin')."</td>";
  193. print "<td align=\"right\">".price($cumul_vente)."</td>\n";
  194. print "<td align=\"right\">".price($cumul_achat)."</td>\n";
  195. print "<td align=\"right\">".price($cumul_qty)."</td>\n";
  196. print "<td align=\"right\">".price($totalMargin)."</td>\n";
  197. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  198. print "<td align=\"right\">".(($marginRate === '')?'n/a':price($marginRate)."%")."</td>\n";
  199. if (! empty($conf->global->DISPLAY_MARK_RATES))
  200. print "<td align=\"right\">".(($markRate === '')?'n/a':price($markRate)."%")."</td>\n";
  201. print '<td align="right">&nbsp;</td>';
  202. print "</tr>\n";
  203. }
  204. else
  205. {
  206. dol_print_error($db);
  207. }
  208. print "</table>";
  209. print '<br>';
  210. $db->free($result);
  211. }
  212. }
  213. else
  214. {
  215. dol_print_error();
  216. }
  217. llxFooter();
  218. $db->close();
  219. ?>
  220. <script type="text/javascript">
  221. $(document).ready(function() {
  222. $("#totalMargin").html("<?php echo price($totalMargin); ?>");
  223. $("#marginRate").html("<?php echo (($marginRate === '')?'n/a':price($marginRate)."%"); ?>");
  224. $("#markRate").html("<?php echo (($markRate === '')?'n/a':price($markRate)."%"); ?>");
  225. });
  226. </script>