productMargins.php 13 KB

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