PageRenderTime 45ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/htdocs/margin/tabs/thirdpartyMargins.php

https://bitbucket.org/speedealing/speedealing
PHP | 251 lines | 181 code | 39 blank | 31 comment | 30 complexity | 4f1aec97560ed982748074eefca38795 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/thirdpartyMargins.php
  19. * \ingroup product margins
  20. * \brief Page des marges des factures clients pour un tiers
  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. $langs->load("companies");
  27. $langs->load("bills");
  28. $langs->load("products");
  29. $langs->load("margins");
  30. // Security check
  31. $socid = GETPOST('socid','int');
  32. if (! empty($user->societe_id)) $socid=$user->societe_id;
  33. $result = restrictedArea($user, 'societe','','');
  34. $mesg = '';
  35. $sortfield = GETPOST("sortfield",'alpha');
  36. $sortorder = GETPOST("sortorder",'alpha');
  37. $page = GETPOST("page",'int');
  38. if ($page == -1) { $page = 0; }
  39. $offset = $conf->liste_limit * $page;
  40. $pageprev = $page - 1;
  41. $pagenext = $page + 1;
  42. if (! $sortorder) $sortorder="DESC";
  43. if (! $sortfield) $sortfield="f.datef";
  44. /*
  45. * View
  46. */
  47. $invoicestatic=new Facture($db);
  48. $form = new Form($db);
  49. llxHeader('',$langs->trans("ThirdParty").' - '.$langs->trans("Margins"));
  50. if ($socid > 0)
  51. {
  52. $societe = new Societe($db, $socid);
  53. $societe->fetch($socid);
  54. /*
  55. * Affichage onglets
  56. */
  57. $head = societe_prepare_head($societe);
  58. dol_fiche_head($head, 'margin', $langs->trans("ThirdParty"),0,'company');
  59. print '<table class="border" width="100%">';
  60. print '<tr><td width="20%">'.$langs->trans('ThirdPartyName').'</td>';
  61. print '<td colspan="3">';
  62. print $form->showrefnav($societe,'socid','',($user->societe_id?0:1),'rowid','nom');
  63. print '</td></tr>';
  64. if (! empty($conf->global->SOCIETE_USEPREFIX)) // Old not used prefix field
  65. {
  66. print '<tr><td>'.$langs->trans('Prefix').'</td><td colspan="3">'.$societe->prefix_comm.'</td></tr>';
  67. }
  68. if ($societe->client)
  69. {
  70. print '<tr><td>';
  71. print $langs->trans('CustomerCode').'</td><td colspan="3">';
  72. print $societe->code_client;
  73. if ($societe->check_codeclient() <> 0) print ' <font class="error">('.$langs->trans("WrongCustomerCode").')</font>';
  74. print '</td></tr>';
  75. }
  76. if ($societe->fournisseur)
  77. {
  78. print '<tr><td>';
  79. print $langs->trans('SupplierCode').'</td><td colspan="3">';
  80. print $societe->code_fournisseur;
  81. if ($societe->check_codefournisseur() <> 0) print ' <font class="error">('.$langs->trans("WrongSupplierCode").')</font>';
  82. print '</td></tr>';
  83. }
  84. // Total Margin
  85. print '<tr><td>'.$langs->trans("TotalMargin").'</td><td colspan="3">';
  86. print '<span id="totalMargin"></span>'; // set by jquery (see below)
  87. print '</td></tr>';
  88. // Margin Rate
  89. if (! empty($conf->global->DISPLAY_MARGIN_RATES)) {
  90. print '<tr><td>'.$langs->trans("MarginRate").'</td><td colspan="3">';
  91. print '<span id="marginRate"></span>'; // set by jquery (see below)
  92. print '</td></tr>';
  93. }
  94. // Mark Rate
  95. if (! empty($conf->global->DISPLAY_MARK_RATES)) {
  96. print '<tr><td>'.$langs->trans("MarkRate").'</td><td colspan="3">';
  97. print '<span id="markRate"></span>'; // set by jquery (see below)
  98. print '</td></tr>';
  99. }
  100. print "</table>";
  101. print '</div>';
  102. $sql = "SELECT distinct s.nom, s.rowid as socid, s.code_client,";
  103. $sql.= " f.facnumber, f.total as total_ht,";
  104. $sql.= " sum(d.subprice * d.qty * (1 - d.remise_percent / 100)) as selling_price, 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," ;
  105. $sql.= " f.datef, f.paye, f.fk_statut as statut, f.rowid as facid";
  106. $sql.= " FROM ".MAIN_DB_PREFIX."societe as s";
  107. $sql.= ", ".MAIN_DB_PREFIX."facture as f";
  108. $sql.= ", ".MAIN_DB_PREFIX."facturedet as d";
  109. $sql.= " WHERE f.fk_soc = s.rowid";
  110. $sql.= " AND f.fk_statut > 0";
  111. $sql.= " AND s.entity = ".$conf->entity;
  112. $sql.= " AND d.fk_facture = f.rowid";
  113. $sql.= " AND f.fk_soc = $socid";
  114. $sql .= " AND d.buy_price_ht IS NOT NULL";
  115. if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1)
  116. $sql .= " AND d.buy_price_ht <> 0";
  117. $sql.= " GROUP BY f.rowid";
  118. $sql.= " ORDER BY $sortfield $sortorder ";
  119. $sql.= $db->plimit($conf->liste_limit +1, $offset);
  120. $result = $db->query($sql);
  121. if ($result)
  122. {
  123. $num = $db->num_rows($result);
  124. print_barre_liste($langs->trans("MarginDetails"),$page,$_SERVER["PHP_SELF"],"&amp;socid=$societe->id",$sortfield,$sortorder,'',$num,0,'');
  125. $i = 0;
  126. print "<table class=\"noborder\" width=\"100%\">";
  127. print '<tr class="liste_titre">';
  128. print_liste_field_titre($langs->trans("Invoice"),$_SERVER["PHP_SELF"],"f.facnumber","","&amp;socid=".$_REQUEST["socid"],'',$sortfield,$sortorder);
  129. print_liste_field_titre($langs->trans("DateInvoice"),$_SERVER["PHP_SELF"],"f.datef","","&amp;socid=".$_REQUEST["socid"],'align="center"',$sortfield,$sortorder);
  130. print_liste_field_titre($langs->trans("SellingPrice"),$_SERVER["PHP_SELF"],"selling_price","","&amp;socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
  131. print_liste_field_titre($langs->trans("BuyingPrice"),$_SERVER["PHP_SELF"],"buying_price","","&amp;socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
  132. print_liste_field_titre($langs->trans("Margin"),$_SERVER["PHP_SELF"],"marge","","&amp;socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
  133. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  134. print_liste_field_titre($langs->trans("MarginRate"),$_SERVER["PHP_SELF"],"","","&amp;socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
  135. if (! empty($conf->global->DISPLAY_MARK_RATES))
  136. print_liste_field_titre($langs->trans("MarkRate"),$_SERVER["PHP_SELF"],"","","&amp;socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
  137. print_liste_field_titre($langs->trans("Status"),$_SERVER["PHP_SELF"],"f.paye,f.fk_statut","","&amp;socid=".$_REQUEST["socid"],'align="right"',$sortfield,$sortorder);
  138. print "</tr>\n";
  139. $cumul_achat = 0;
  140. $cumul_vente = 0;
  141. $cumul_qty = 0;
  142. $rounding = min($conf->global->MAIN_MAX_DECIMALS_UNIT,$conf->global->MAIN_MAX_DECIMALS_TOT);
  143. if ($num > 0)
  144. {
  145. $var=True;
  146. while ($i < $num && $i < $conf->liste_limit)
  147. {
  148. $objp = $db->fetch_object($result);
  149. $marginRate = ($objp->buying_price != 0)?(100 * round($objp->marge / $objp->buying_price, 5)):'' ;
  150. $markRate = ($objp->selling_price != 0)?(100 * round($objp->marge / $objp->selling_price, 5)):'' ;
  151. $var=!$var;
  152. print "<tr $bc[$var]>";
  153. print '<td>';
  154. $invoicestatic->id=$objp->facid;
  155. $invoicestatic->ref=$objp->facnumber;
  156. print $invoicestatic->getNomUrl(1);
  157. print "</td>\n";
  158. print "<td align=\"center\">";
  159. print dol_print_date($db->jdate($objp->datef),'day')."</td>";
  160. print "<td align=\"right\">".price($objp->selling_price)."</td>\n";
  161. print "<td align=\"right\">".price($objp->buying_price)."</td>\n";
  162. print "<td align=\"right\">".price($objp->marge)."</td>\n";
  163. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  164. print "<td align=\"right\">".(($marginRate === '')?'n/a':price($marginRate)."%")."</td>\n";
  165. if (! empty($conf->global->DISPLAY_MARK_RATES))
  166. print "<td align=\"right\">".(($markRate === '')?'n/a':price($markRate)."%")."</td>\n";
  167. print '<td align="right">'.$invoicestatic->LibStatut($objp->paye,$objp->statut,5).'</td>';
  168. print "</tr>\n";
  169. $i++;
  170. $cumul_achat += round($objp->buying_price, $rounding);
  171. $cumul_vente += round($objp->selling_price, $rounding);
  172. }
  173. }
  174. // affichage totaux marges
  175. $var=!$var;
  176. $totalMargin = $cumul_vente - $cumul_achat;
  177. $marginRate = ($cumul_achat != 0)?(100 * round($totalMargin / $cumul_achat, 5)):'' ;
  178. $markRate = ($cumul_vente != 0)?(100 * round($totalMargin / $cumul_vente, 5)):'' ;
  179. print '<tr '.$bc[$var].' style="border-top: 1px solid #ccc; font-weight: bold">';
  180. print '<td colspan=2>'.$langs->trans('TotalMargin')."</td>";
  181. print "<td align=\"right\">".price($cumul_vente)."</td>\n";
  182. print "<td align=\"right\">".price($cumul_achat)."</td>\n";
  183. print "<td align=\"right\">".price($totalMargin)."</td>\n";
  184. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  185. print "<td align=\"right\">".(($marginRate === '')?'n/a':price($marginRate)."%")."</td>\n";
  186. if (! empty($conf->global->DISPLAY_MARK_RATES))
  187. print "<td align=\"right\">".(($markRate === '')?'n/a':price($markRate)."%")."</td>\n";
  188. print '<td align="right">&nbsp;</td>';
  189. print "</tr>\n";
  190. }
  191. else
  192. {
  193. dol_print_error($db);
  194. }
  195. print "</table>";
  196. print '<br>';
  197. $db->free($result);
  198. }
  199. else
  200. {
  201. dol_print_error();
  202. }
  203. llxFooter();
  204. $db->close();
  205. ?>
  206. <script type="text/javascript">
  207. $(document).ready(function() {
  208. $("#totalMargin").html("<?php echo price($totalMargin); ?>");
  209. $("#marginRate").html("<?php echo (($marginRate === '')?'n/a':price($marginRate)."%"); ?>");
  210. $("#markRate").html("<?php echo (($markRate === '')?'n/a':price($markRate)."%"); ?>");
  211. });
  212. </script>