productMargins.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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->loadLangs(array("companies", "bills", "products", "margins"));
  27. $id = GETPOST('id', 'int');
  28. $ref = GETPOST('ref', 'alpha');
  29. $action=GETPOST('action','alpha');
  30. $confirm=GETPOST('confirm','alpha');
  31. // Security check
  32. $fieldvalue = (! empty($id) ? $id : (! empty($ref) ? $ref : ''));
  33. $fieldtype = (! empty($ref) ? 'ref' : 'rowid');
  34. if (! empty($user->societe_id)) $socid=$user->societe_id;
  35. $result=restrictedArea($user,'produit|service',$fieldvalue,'product&product','','',$fieldtype);
  36. $object = new Product($db);
  37. $mesg = '';
  38. $sortfield = GETPOST("sortfield",'alpha');
  39. $sortorder = GETPOST("sortorder",'alpha');
  40. $page = GETPOST("page",'int');
  41. if (empty($page) || $page == -1) { $page = 0; } // If $page is not defined, or '' or -1
  42. $offset = $conf->liste_limit * $page;
  43. $pageprev = $page - 1;
  44. $pagenext = $page + 1;
  45. if (! $sortorder) $sortorder="DESC";
  46. if (! $sortfield) $sortfield="f.datef";
  47. /*
  48. * View
  49. */
  50. $invoicestatic=new Facture($db);
  51. $form = new Form($db);
  52. if ($id > 0 || ! empty($ref))
  53. {
  54. $result = $object->fetch($id, $ref);
  55. $title = $langs->trans('ProductServiceCard');
  56. $helpurl = '';
  57. $shortlabel = dol_trunc($object->label,16);
  58. if (GETPOST("type") == '0' || ($object->type == Product::TYPE_PRODUCT))
  59. {
  60. $title = $langs->trans('Product')." ". $shortlabel ." - ".$langs->trans('Card');
  61. $helpurl='EN:Module_Products|FR:Module_Produits|ES:M&oacute;dulo_Productos';
  62. }
  63. if (GETPOST("type") == '1' || ($object->type == Product::TYPE_SERVICE))
  64. {
  65. $title = $langs->trans('Service')." ". $shortlabel ." - ".$langs->trans('Card');
  66. $helpurl='EN:Module_Services_En|FR:Module_Services|ES:M&oacute;dulo_Servicios';
  67. }
  68. llxHeader('', $title, $helpurl);
  69. /*
  70. * En mode visu
  71. */
  72. if ($result > 0)
  73. {
  74. $head=product_prepare_head($object);
  75. $titre=$langs->trans("CardProduct".$object->type);
  76. $picto=($object->type== Product::TYPE_SERVICE?'service':'product');
  77. dol_fiche_head($head, 'margin', $titre, -1, $picto);
  78. $linkback = '<a href="'.DOL_URL_ROOT.'/product/list.php?restore_lastsearch_values=1">'.$langs->trans("BackToList").'</a>';
  79. dol_banner_tab($object, 'ref', $linkback, ($user->societe_id?0:1), 'ref');
  80. print '<div class="fichecenter">';
  81. print '<div class="underbanner clearboth"></div>';
  82. print '<table class="border tableforfield" width="100%">';
  83. // Total Margin
  84. print '<tr><td class="titlefield">'.$langs->trans("TotalMargin").'</td><td colspan="3">';
  85. print '<span id="totalMargin"></span>'; // set by jquery (see below)
  86. print '</td></tr>';
  87. // Margin Rate
  88. if (! empty($conf->global->DISPLAY_MARGIN_RATES)) {
  89. print '<tr><td>'.$langs->trans("MarginRate").'</td><td colspan="3">';
  90. print '<span id="marginRate"></span>'; // set by jquery (see below)
  91. print '</td></tr>';
  92. }
  93. // Mark Rate
  94. if (! empty($conf->global->DISPLAY_MARK_RATES)) {
  95. print '<tr><td>'.$langs->trans("MarkRate").'</td><td colspan="3">';
  96. print '<span id="markRate"></span>'; // set by jquery (see below)
  97. print '</td></tr>';
  98. }
  99. print "</table>";
  100. print '</div>';
  101. print '<div style="clear:both"></div>';
  102. dol_fiche_end();
  103. if ($user->rights->facture->lire) {
  104. $sql = "SELECT s.nom as name, s.rowid as socid, s.code_client,";
  105. $sql.= " f.rowid as facid, f.facnumber, f.total as total_ht,";
  106. $sql.= " f.datef, f.paye, f.fk_statut as statut, f.type,";
  107. if (!$user->rights->societe->client->voir && !$socid) $sql.= " sc.fk_soc, sc.fk_user,";
  108. $sql.= " sum(d.total_ht) as selling_price,"; // may be negative or positive
  109. $sql.= " IF(f.type = 2, -1, 1) * sum(d.qty) as qty,"; // not always positive in case of Credit note
  110. $sql.= " IF(f.type = 2, -1, 1) * sum(d.qty * d.buy_price_ht) as buying_price,"; // not always positive in case of Credit note
  111. $sql.= " IF(f.type = 2, -1, 1) * sum(abs(d.total_ht) - (d.buy_price_ht * d.qty)) as marge" ; // not always positive in case of Credit note
  112. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  113. $sql.= ", ".MAIN_DB_PREFIX."facture as f";
  114. $sql.= ", ".MAIN_DB_PREFIX."facturedet as d";
  115. if (!$user->rights->societe->client->voir && !$socid) $sql.= ", ".MAIN_DB_PREFIX."societe_commerciaux as sc";
  116. $sql.= " WHERE f.fk_soc = s.rowid";
  117. $sql.= " AND f.fk_statut > 0";
  118. $sql.= " AND f.entity IN (".getEntity('invoice').")";
  119. $sql.= " AND d.fk_facture = f.rowid";
  120. $sql.= " AND d.fk_product =".$object->id;
  121. if (!$user->rights->societe->client->voir && !$socid) $sql.= " AND s.rowid = sc.fk_soc AND sc.fk_user = " .$user->id;
  122. if (! empty($socid)) $sql.= " AND f.fk_soc = $socid";
  123. $sql .= " AND d.buy_price_ht IS NOT NULL";
  124. if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1) $sql .= " AND d.buy_price_ht <> 0";
  125. $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";
  126. if (!$user->rights->societe->client->voir && !$socid) $sql.= ", sc.fk_soc, sc.fk_user";
  127. $sql.= $db->order($sortfield,$sortorder);
  128. // TODO: calculate total to display then restore pagination
  129. //$sql.= $db->plimit($conf->liste_limit +1, $offset);
  130. dol_syslog('margin:tabs:productMargins.php', LOG_DEBUG);
  131. $result = $db->query($sql);
  132. if ($result) {
  133. $num = $db->num_rows($result);
  134. print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"&amp;id=$object->id",$sortfield,$sortorder,'',0,0,'');
  135. $i = 0;
  136. print '<div class="div-table-responsive">';
  137. print '<table class="noborder" width="100%">';
  138. print '<tr class="liste_titre">';
  139. print_liste_field_titre("Invoice",$_SERVER["PHP_SELF"],"f.facnumber","","&amp;id=".$object->id,'',$sortfield,$sortorder);
  140. print_liste_field_titre("Company",$_SERVER["PHP_SELF"],"s.nom","","&amp;id=".$object->id,'',$sortfield,$sortorder);
  141. print_liste_field_titre("CustomerCode",$_SERVER["PHP_SELF"],"s.code_client","","&amp;id=".$object->id,'',$sortfield,$sortorder);
  142. print_liste_field_titre("DateInvoice",$_SERVER["PHP_SELF"],"f.datef","","&amp;id=".$object->id,'align="center"',$sortfield,$sortorder);
  143. print_liste_field_titre("SellingPrice",$_SERVER["PHP_SELF"],"selling_price","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  144. print_liste_field_titre("BuyingPrice",$_SERVER["PHP_SELF"],"buying_price","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  145. print_liste_field_titre("Qty",$_SERVER["PHP_SELF"],"d.qty","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  146. print_liste_field_titre("Margin",$_SERVER["PHP_SELF"],"marge","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  147. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  148. print_liste_field_titre("MarginRate",$_SERVER["PHP_SELF"],"","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  149. if (! empty($conf->global->DISPLAY_MARK_RATES))
  150. print_liste_field_titre("MarkRate",$_SERVER["PHP_SELF"],"","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  151. print_liste_field_titre("Status",$_SERVER["PHP_SELF"],"f.paye,f.fk_statut","","&amp;id=".$object->id,'align="right"',$sortfield,$sortorder);
  152. print "</tr>\n";
  153. $cumul_achat = 0;
  154. $cumul_vente = 0;
  155. $cumul_qty = 0;
  156. $rounding = min($conf->global->MAIN_MAX_DECIMALS_UNIT,$conf->global->MAIN_MAX_DECIMALS_TOT);
  157. if ($num > 0) {
  158. while ($i < $num /*&& $i < $conf->liste_limit*/) {
  159. $objp = $db->fetch_object($result);
  160. $marginRate = ($objp->buying_price != 0)?(100 * $objp->marge / $objp->buying_price):'' ;
  161. $markRate = ($objp->selling_price != 0)?(100 * $objp->marge / $objp->selling_price):'' ;
  162. print '<tr class="oddeven">';
  163. print '<td>';
  164. $invoicestatic->id=$objp->facid;
  165. $invoicestatic->ref=$objp->facnumber;
  166. print $invoicestatic->getNomUrl(1);
  167. print "</td>\n";
  168. 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>';
  169. print "<td>".$objp->code_client."</td>\n";
  170. print "<td align=\"center\">";
  171. print dol_print_date($db->jdate($objp->datef),'day')."</td>";
  172. print "<td align=\"right\">".price($objp->selling_price, null, null, null, null, $rounding)."</td>\n";
  173. print "<td align=\"right\">".price($objp->buying_price, null, null, null, null, $rounding)."</td>\n";
  174. print "<td align=\"right\">".price($objp->qty, null, null, null, null, $rounding)."</td>\n";
  175. print "<td align=\"right\">".price($objp->marge, null, null, null, null, $rounding)."</td>\n";
  176. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  177. print "<td align=\"right\">".(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%")."</td>\n";
  178. if (! empty($conf->global->DISPLAY_MARK_RATES))
  179. print "<td align=\"right\">".(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%")."</td>\n";
  180. print '<td align="right">'.$invoicestatic->LibStatut($objp->paye,$objp->statut,5).'</td>';
  181. print "</tr>\n";
  182. $i++;
  183. $cumul_achat += $objp->buying_price;
  184. $cumul_vente += $objp->selling_price;
  185. $cumul_qty += $objp->qty;
  186. }
  187. }
  188. // affichage totaux marges
  189. $totalMargin = $cumul_vente - $cumul_achat;
  190. if ($totalMargin < 0)
  191. {
  192. $marginRate = ($cumul_achat != 0)?-1*(100 * $totalMargin / $cumul_achat):'';
  193. $markRate = ($cumul_vente != 0)?-1*(100 * $totalMargin / $cumul_vente):'';
  194. }
  195. else
  196. {
  197. $marginRate = ($cumul_achat != 0)?(100 * $totalMargin / $cumul_achat):'';
  198. $markRate = ($cumul_vente != 0)?(100 * $totalMargin / $cumul_vente):'';
  199. }
  200. print '<tr class="liste_total">';
  201. print '<td colspan=4>'.$langs->trans('TotalMargin')."</td>";
  202. print '<td align="right">'.price($cumul_vente, null, null, null, null, $rounding)."</td>\n";
  203. print '<td align="right">'.price($cumul_achat, null, null, null, null, $rounding)."</td>\n";
  204. print '<td align="right">'.price($cumul_qty, null, null, null, null, $rounding)."</td>\n";
  205. print '<td align="right">'.price($totalMargin, null, null, null, null, $rounding)."</td>\n";
  206. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  207. print '<td align="right">'.(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%")."</td>\n";
  208. if (! empty($conf->global->DISPLAY_MARK_RATES))
  209. print "<td align=\"right\">".(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%")."</td>\n";
  210. print '<td align="right">&nbsp;</td>';
  211. print "</tr>\n";
  212. print "</table>";
  213. print '</div>';
  214. } else {
  215. dol_print_error($db);
  216. }
  217. $db->free($result);
  218. }
  219. }
  220. } else {
  221. dol_print_error();
  222. }
  223. print '
  224. <script type="text/javascript">
  225. $(document).ready(function() {
  226. $("#totalMargin").html("'. price($totalMargin, null, null, null, null, $rounding).'");
  227. $("#marginRate").html("'.(($marginRate === '')?'n/a':price($marginRate, null, null, null, null, $rounding)."%").'");
  228. $("#markRate").html("'.(($markRate === '')?'n/a':price($markRate, null, null, null, null, $rounding)."%").'");
  229. });
  230. </script>
  231. ';
  232. // End of page
  233. llxFooter();
  234. $db->close();