productMargins.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /* Copyright (C) 2012-2013 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 (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  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. $title = $langs->trans('ProductServiceCard');
  59. $helpurl = '';
  60. $shortlabel = dol_trunc($object->label,16);
  61. if (GETPOST("type") == '0' || ($object->type == Product::TYPE_PRODUCT))
  62. {
  63. $title = $langs->trans('Product')." ". $shortlabel ." - ".$langs->trans('Card');
  64. $helpurl='EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
  65. }
  66. if (GETPOST("type") == '1' || ($object->type == Product::TYPE_SERVICE))
  67. {
  68. $title = $langs->trans('Service')." ". $shortlabel ." - ".$langs->trans('Card');
  69. $helpurl='EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios';
  70. }
  71. llxHeader('', $title, $helpurl);
  72. /*
  73. * En mode visu
  74. */
  75. if ($result > 0)
  76. {
  77. $head=product_prepare_head($object);
  78. $titre=$langs->trans("CardProduct".$object->type);
  79. $picto=($object->type== Product::TYPE_SERVICE?'service':'product');
  80. dol_fiche_head($head, 'margin', $titre, -1, $picto);
  81. $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php">'.$langs->trans("BackToList").'</a>';
  82. dol_banner_tab($object, 'ref', $linkback, ($user->societe_id?0:1), 'ref');
  83. print '<div class="fichecenter">';
  84. print '<div class="underbanner clearboth"></div>';
  85. print '<table class="border tableforfield" width="100%">';
  86. // Total Margin
  87. print '<tr><td class="titlefield">'.$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. print '<div style="clear:both"></div>';
  105. dol_fiche_end();
  106. if ($user->rights->facture->lire) {
  107. $sql = "SELECT s.nom as name, s.rowid as socid, s.code_client,";
  108. $sql.= " f.rowid as facid, f.facnumber, f.total as total_ht,";
  109. $sql.= " f.datef, f.paye, f.fk_statut as statut, f.type,";
  110. if (!$user->rights->societe->client->voir && !$socid) $sql.= " sc.fk_soc, sc.fk_user,";
  111. $sql.= " sum(d.total_ht) as selling_price,"; // may be negative or positive
  112. $sql.= " sum(d.qty) as qty,";
  113. $sql.= " sum(d.qty * d.buy_price_ht) as buying_price,"; // always positive
  114. $sql.= " sum(abs(d.total_ht) - (d.buy_price_ht * d.qty)) as marge" ; // always positive
  115. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  116. $sql.= ", ".MAIN_DB_PREFIX."facture as f";
  117. $sql.= ", ".MAIN_DB_PREFIX."facturedet as d";
  118. if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  119. $sql.= " WHERE f.fk_soc = s.rowid";
  120. $sql.= " AND f.fk_statut > 0";
  121. $sql.= " AND s.entity = ".$conf->entity;
  122. $sql.= " AND d.fk_facture = f.rowid";
  123. $sql.= " AND d.fk_product =".$object->id;
  124. if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
  125. if (! empty($socid)) $sql.= " AND f.fk_soc = $socid";
  126. $sql .= " AND d.buy_price_ht IS NOT NULL";
  127. if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) $sql .= " AND d.buy_price_ht <> 0";
  128. $sql.= " GROUP BY s.nom, s.rowid, s.code_client, f.rowid, f.facnumber, f.total, f.datef, f.paye, f.fk_statut, f.type";
  129. if (!$user->rights->societe->client->voir && !$socid) $sql.= ", sc.fk_soc, sc.fk_user";
  130. $sql.= $db->order($sortfield,$sortorder);
  131. // TODO: calculate total to display then restore pagination
  132. //$sql.= $db->plimit($conf->liste_limit +1, $offset);
  133. dol_syslog('margin:tabs:productMargins.php', LOG_DEBUG);
  134. $result = $db->query($sql);
  135. if ($result) {
  136. $num = $db->num_rows($result);
  137. print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"&amp;id=$object->id",$sortfield,$sortorder,'',0,0,'');
  138. $i = 0;
  139. print '<div class="div-table-responsive">';
  140. print '<table class="noborder" width="100%">';
  141. print '<tr class="liste_titre">';
  142. print_liste_field_titre($langs->trans("Invoice"),$_SERVER["PHP_SELF"],"f.facnumber","","&amp;id=".$object->id,'',$sortfield,$sortorder);
  143. print_liste_field_titre($langs->trans("Company"),$_SERVER["PHP_SELF"],"s.nom","","&amp;id=".$object->id,'',$sortfield,$sortorder);
  144. print_liste_field_titre($langs->trans("CustomerCode"),$_SERVER["PHP_SELF"],"s.code_client","","&amp;id=".$object->id,'',$sortfield,$sortorder);
  145. print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&amp;id=".$object->id,'align="center"',$sortfield,$sortorder);
  146. print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  147. print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buying_price","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  148. print_liste_field_titre($langs->trans("Qty"),$_SERVER["PHP_SELF"],"d.qty","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  149. print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  150. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  151. print_liste_field_titre($langs->trans("MarginRate"),$_SERVER["PHP_SELF"],"","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  152. if (! empty($conf->global->DISPLAY_MARK_RATES))
  153. print_liste_field_titre($langs->trans("MarkRate"),$_SERVER["PHP_SELF"],"","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  154. print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"f.paye,f.fk_statut","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  155. print "</tr>\n";
  156. $cumul_achat = 0;
  157. $cumul_vente = 0;
  158. $cumul_qty = 0;
  159. $rounding = min($conf->global->MAIN_MAX_DECIMALS_UNIT,$conf->global->MAIN_MAX_DECIMALS_TOT);
  160. if ($num > 0) {
  161. $var=True;
  162. while ($i < $num /*&& $i < $conf->liste_limit*/) {
  163. $objp = $db->fetch_object($result);
  164. $marginRate = ($objp->buying_price != 0)?(100 * $objp->marge / $objp->buying_price):'' ;
  165. $markRate = ($objp->selling_price != 0)?(100 * $objp->marge / $objp->selling_price):'' ;
  166. print '<tr class="oddeven">';
  167. print '<td>';
  168. $invoicestatic->id=$objp->facid;
  169. $invoicestatic->ref=$objp->facnumber;
  170. print $invoicestatic->getNomUrl(1);
  171. print "</td>\n";
  172. print '<td><a href="'.DOL_URL_ROOT.'/comm/card.php?socid='.$objp->socid.'">'.img_object($langs->trans("ShowCompany"),"company").' '.dol_trunc($objp->name,44).'</a></td>';
  173. print "<td>".$objp->code_client."</td>\n";
  174. print "<td align=\"center\">";
  175. print dol_print_date($db->jdate($objp->datef),'day')."</td>";
  176. print "<td align=\"right\">".price($objp->selling_price, null, null, null, null, $rounding)."</td>\n";
  177. print "<td align=\"right\">".price($objp->buying_price, null, null, null, null, $rounding)."</td>\n";
  178. print "<td align=\"right\">".price($objp->qty, null, null, null, null, $rounding)."</td>\n";
  179. print "<td align=\"right\">".price($objp->marge, null, null, null, null, $rounding)."</td>\n";
  180. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  181. print "<td align=\"right\">".(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%")."</td>\n";
  182. if (! empty($conf->global->DISPLAY_MARK_RATES))
  183. print "<td align=\"right\">".(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%")."</td>\n";
  184. print '<td align="right">'.$invoicestatic->LibStatut($objp->paye,$objp->statut,5).'</td>';
  185. print "</tr>\n";
  186. $i++;
  187. $cumul_achat += $objp->buying_price;
  188. $cumul_vente += $objp->selling_price;
  189. $cumul_qty += $objp->qty;
  190. }
  191. }
  192. // affichage totaux marges
  193. $totalMargin = $cumul_vente - $cumul_achat;
  194. if ($totalMargin < 0)
  195. {
  196. $marginRate = ($cumul_achat != 0)?-1*(100 * $totalMargin / $cumul_achat):'';
  197. $markRate = ($cumul_vente != 0)?-1*(100 * $totalMargin / $cumul_vente):'';
  198. }
  199. else
  200. {
  201. $marginRate = ($cumul_achat != 0)?(100 * $totalMargin / $cumul_achat):'';
  202. $markRate = ($cumul_vente != 0)?(100 * $totalMargin / $cumul_vente):'';
  203. }
  204. print '<tr class="liste_total">';
  205. print '<td colspan=4>'.$langs->trans('TotalMargin')."</td>";
  206. print '<td align="right">'.price($cumul_vente, null, null, null, null, $rounding)."</td>\n";
  207. print '<td align="right">'.price($cumul_achat, null, null, null, null, $rounding)."</td>\n";
  208. print '<td align="right">'.price($cumul_qty, null, null, null, null, $rounding)."</td>\n";
  209. print '<td align="right">'.price($totalMargin, null, null, null, null, $rounding)."</td>\n";
  210. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  211. print '<td align="right">'.(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%")."</td>\n";
  212. if (! empty($conf->global->DISPLAY_MARK_RATES))
  213. print "<td align=\"right\">".(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%")."</td>\n";
  214. print '<td align="right">&nbsp;</td>';
  215. print "</tr>\n";
  216. print "</table>";
  217. print '</div>';
  218. } else {
  219. dol_print_error($db);
  220. }
  221. $db->free($result);
  222. }
  223. }
  224. } else {
  225. dol_print_error();
  226. }
  227. print '
  228. <script type="text/javascript">
  229. $(document).ready(function() {
  230. $("#totalMargin").html("'. price($totalMargin, null, null, null, null, $rounding).'");
  231. $("#marginRate").html("'.(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%").'");
  232. $("#markRate").html("'.(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%").'");
  233. });
  234. </script>
  235. ';
  236. llxFooter();
  237. $db->close();