html.formmargin.class.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279
  1. <?php
  2. /* Copyright (c) 2015 Laurent Destailleur <eldy@users.sourceforge.net>
  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/core/class/html.formmargin.class.php
  19. * \ingroup core
  20. * \brief Fichier de la classe des fonctions predefinie de composants html autre
  21. */
  22. /**
  23. * Classe permettant la generation de composants html autre
  24. * Only common components are here.
  25. */
  26. class FormMargin
  27. {
  28. var $db;
  29. var $error;
  30. /**
  31. * Constructor
  32. *
  33. * @param DoliDB $db Database handler
  34. */
  35. function __construct($db)
  36. {
  37. $this->db = $db;
  38. return 1;
  39. }
  40. /**
  41. * get array with margin information from lines of object
  42. * TODO Move this in common class.
  43. *
  44. * @param CommonObject $object Object we want to get margin information for
  45. * @param boolean $force_price True of not
  46. * @return array Array with info
  47. */
  48. function getMarginInfosArray($object, $force_price=false)
  49. {
  50. global $conf, $db;
  51. // Default returned array
  52. $marginInfos = array(
  53. 'pa_products' => 0,
  54. 'pv_products' => 0,
  55. 'margin_on_products' => 0,
  56. 'margin_rate_products' => '',
  57. 'mark_rate_products' => '',
  58. 'pa_services' => 0,
  59. 'pv_services' => 0,
  60. 'margin_on_services' => 0,
  61. 'margin_rate_services' => '',
  62. 'mark_rate_services' => '',
  63. 'pa_total' => 0,
  64. 'pv_total' => 0,
  65. 'total_margin' => 0,
  66. 'total_margin_rate' => '',
  67. 'total_mark_rate' => ''
  68. );
  69. foreach($object->lines as $line)
  70. {
  71. if (empty($line->pa_ht) && isset($line->fk_fournprice) && !$force_price)
  72. {
  73. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
  74. $product = new ProductFournisseur($db);
  75. if ($product->fetch_product_fournisseur_price($line->fk_fournprice))
  76. $line->pa_ht = $product->fourn_unitprice * (1 - $product->fourn_remise_percent / 100);
  77. if (isset($conf->global->MARGIN_TYPE) && $conf->global->MARGIN_TYPE == "2" && $product->fourn_unitcharges > 0)
  78. $line->pa_ht += $product->fourn_unitcharges;
  79. }
  80. // si prix d'achat non renseigné et devrait l'être, alors prix achat = prix vente
  81. if ((!isset($line->pa_ht) || $line->pa_ht == 0) && $line->subprice > 0 && (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 1)) {
  82. $line->pa_ht = $line->subprice * (1 - ($line->remise_percent / 100));
  83. }
  84. $pv = $line->qty * $line->subprice * (1 - $line->remise_percent / 100);
  85. $pa_ht = ($pv < 0 ? - $line->pa_ht : $line->pa_ht); // We choosed to have line->pa_ht always positive in database, so we guess the correct sign
  86. $pa = $line->qty * $pa_ht;
  87. // calcul des marges
  88. if (isset($line->fk_remise_except) && isset($conf->global->MARGIN_METHODE_FOR_DISCOUNT)) { // remise
  89. if ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '1') { // remise globale considérée comme produit
  90. $marginInfos['pa_products'] += $pa;
  91. $marginInfos['pv_products'] += $pv;
  92. $marginInfos['pa_total'] += $pa;
  93. $marginInfos['pv_total'] += $pv;
  94. // if credit note, margin = -1 * (abs(selling_price) - buying_price)
  95. //if ($pv < 0)
  96. //{
  97. // $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
  98. //}
  99. //else
  100. $marginInfos['margin_on_products'] += $pv - $pa;
  101. }
  102. elseif ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '2') { // remise globale considérée comme service
  103. $marginInfos['pa_services'] += $pa;
  104. $marginInfos['pv_services'] += $pv;
  105. $marginInfos['pa_total'] += $pa;
  106. $marginInfos['pv_total'] += $pv;
  107. // if credit note, margin = -1 * (abs(selling_price) - buying_price)
  108. //if ($pv < 0)
  109. // $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
  110. //else
  111. $marginInfos['margin_on_services'] += $pv - $pa;
  112. }
  113. elseif ($conf->global->MARGIN_METHODE_FOR_DISCOUNT == '3') { // remise globale prise en compte uniqt sur total
  114. $marginInfos['pa_total'] += $pa;
  115. $marginInfos['pv_total'] += $pv;
  116. }
  117. }
  118. else {
  119. $type=$line->product_type?$line->product_type:$line->fk_product_type;
  120. if ($type == 0) { // product
  121. $marginInfos['pa_products'] += $pa;
  122. $marginInfos['pv_products'] += $pv;
  123. $marginInfos['pa_total'] += $pa;
  124. $marginInfos['pv_total'] += $pv;
  125. // if credit note, margin = -1 * (abs(selling_price) - buying_price)
  126. //if ($pv < 0)
  127. //{
  128. // $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
  129. //}
  130. //else
  131. //{
  132. $marginInfos['margin_on_products'] += $pv - $pa;
  133. //}
  134. }
  135. elseif ($type == 1) { // service
  136. $marginInfos['pa_services'] += $pa;
  137. $marginInfos['pv_services'] += $pv;
  138. $marginInfos['pa_total'] += $pa;
  139. $marginInfos['pv_total'] += $pv;
  140. // if credit note, margin = -1 * (abs(selling_price) - buying_price)
  141. //if ($pv < 0)
  142. // $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
  143. //else
  144. $marginInfos['margin_on_services'] += $pv - $pa;
  145. }
  146. }
  147. }
  148. if ($marginInfos['pa_products'] > 0)
  149. $marginInfos['margin_rate_products'] = 100 * $marginInfos['margin_on_products'] / $marginInfos['pa_products'];
  150. if ($marginInfos['pv_products'] > 0)
  151. $marginInfos['mark_rate_products'] = 100 * $marginInfos['margin_on_products'] / $marginInfos['pv_products'];
  152. if ($marginInfos['pa_services'] > 0)
  153. $marginInfos['margin_rate_services'] = 100 * $marginInfos['margin_on_services'] / $marginInfos['pa_services'];
  154. if ($marginInfos['pv_services'] > 0)
  155. $marginInfos['mark_rate_services'] = 100 * $marginInfos['margin_on_services'] / $marginInfos['pv_services'];
  156. // if credit note, margin = -1 * (abs(selling_price) - buying_price)
  157. //if ($marginInfos['pv_total'] < 0)
  158. // $marginInfos['total_margin'] = -1 * (abs($marginInfos['pv_total']) - $marginInfos['pa_total']);
  159. //else
  160. $marginInfos['total_margin'] = $marginInfos['pv_total'] - $marginInfos['pa_total'];
  161. if ($marginInfos['pa_total'] > 0)
  162. $marginInfos['total_margin_rate'] = 100 * $marginInfos['total_margin'] / $marginInfos['pa_total'];
  163. if ($marginInfos['pv_total'] > 0)
  164. $marginInfos['total_mark_rate'] = 100 * $marginInfos['total_margin'] / $marginInfos['pv_total'];
  165. return $marginInfos;
  166. }
  167. /**
  168. * Show the array with all margin infos
  169. *
  170. * @param CommonObject $object Object we want to get margin information for
  171. * @param boolean $force_price Force price
  172. * @return void
  173. */
  174. function displayMarginInfos($object, $force_price=false)
  175. {
  176. global $langs, $conf, $user;
  177. if (! empty($user->societe_id)) return;
  178. if (! $user->rights->margins->liretous) return;
  179. $rounding = min($conf->global->MAIN_MAX_DECIMALS_UNIT, $conf->global->MAIN_MAX_DECIMALS_TOT);
  180. $marginInfo = $this->getMarginInfosArray($object, $force_price);
  181. if (! empty($conf->global->MARGIN_ADD_SHOWHIDE_BUTTON)) // TODO Warning this feature rely on an external js file that may be removed. Using native js function document.cookie should be better
  182. {
  183. print $langs->trans('ShowMarginInfos').' : ';
  184. $hidemargininfos = $_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW'];
  185. print '<span id="showMarginInfos" class="linkobject '.(!empty($hidemargininfos)?'':'hideobject').'">'.img_picto($langs->trans("Disabled"),'switch_off').'</span>';
  186. print '<span id="hideMarginInfos" class="linkobject '.(!empty($hidemargininfos)?'hideobject':'').'">'.img_picto($langs->trans("Enabled"),'switch_on').'</span>';
  187. print '<script>$(document).ready(function() {
  188. $("span#showMarginInfos").click(function() { $.getScript( "'.dol_buildpath('/includes/jquery/plugins/jquerytreeview/lib/jquery.cookie.js', 1).'", function( data, textStatus, jqxhr ) { $.cookie("DOLUSER_MARGININFO_HIDE_SHOW", 0); $(".margininfos").show(); $("span#showMarginInfos").addClass("hideobject"); $("span#hideMarginInfos").removeClass("hideobject");})});
  189. $("span#hideMarginInfos").click(function() { $.getScript( "'.dol_buildpath('/includes/jquery/plugins/jquerytreeview/lib/jquery.cookie.js', 1).'", function( data, textStatus, jqxhr ) { $.cookie("DOLUSER_MARGININFO_HIDE_SHOW", 1); $(".margininfos").hide(); $("span#hideMarginInfos").addClass("hideobject"); $("span#showMarginInfos").removeClass("hideobject");})});
  190. });</script>';
  191. if (!empty($hidemargininfos)) print '<script>$(document).ready(function() {$(".margininfos").hide();});</script>';
  192. }
  193. print '<!-- Margin table -->'."\n";
  194. print '<table class="noborder margintable centpercent">';
  195. print '<tr class="liste_titre">';
  196. print '<td class="liste_titre">'.$langs->trans('Margins').'</td>';
  197. print '<td class="liste_titre" align="right">'.$langs->trans('SellingPrice').'</td>';
  198. if ($conf->global->MARGIN_TYPE == "1")
  199. print '<td class="liste_titre" align="right">'.$langs->trans('BuyingPrice').'</td>';
  200. else
  201. print '<td class="liste_titre" align="right">'.$langs->trans('CostPrice').'</td>';
  202. print '<td class="liste_titre" align="right">'.$langs->trans('Margin').'</td>';
  203. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  204. print '<td class="liste_titre" align="right">'.$langs->trans('MarginRate').'</td>';
  205. if (! empty($conf->global->DISPLAY_MARK_RATES))
  206. print '<td class="liste_titre" align="right">'.$langs->trans('MarkRate').'</td>';
  207. print '</tr>';
  208. if (! empty($conf->product->enabled))
  209. {
  210. //if ($marginInfo['margin_on_products'] != 0 && $marginInfo['margin_on_services'] != 0) {
  211. print '<tr class="oddeven">';
  212. print '<td>'.$langs->trans('MarginOnProducts').'</td>';
  213. print '<td align="right">'.price($marginInfo['pv_products'], null, null, null, null, $rounding).'</td>';
  214. print '<td align="right">'.price($marginInfo['pa_products'], null, null, null, null, $rounding).'</td>';
  215. print '<td align="right">'.price($marginInfo['margin_on_products'], null, null, null, null, $rounding).'</td>';
  216. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  217. print '<td align="right">'.(($marginInfo['margin_rate_products'] == '')?'':price($marginInfo['margin_rate_products'], null, null, null, null, $rounding).'%').'</td>';
  218. if (! empty($conf->global->DISPLAY_MARK_RATES))
  219. print '<td align="right">'.(($marginInfo['mark_rate_products'] == '')?'':price($marginInfo['mark_rate_products'], null, null, null, null, $rounding).'%').'</td>';
  220. print '</tr>';
  221. }
  222. if (! empty($conf->service->enabled))
  223. {
  224. print '<tr class="oddeven">';
  225. print '<td>'.$langs->trans('MarginOnServices').'</td>';
  226. print '<td align="right">'.price($marginInfo['pv_services'], null, null, null, null, $rounding).'</td>';
  227. print '<td align="right">'.price($marginInfo['pa_services'], null, null, null, null, $rounding).'</td>';
  228. print '<td align="right">'.price($marginInfo['margin_on_services'], null, null, null, null, $rounding).'</td>';
  229. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  230. print '<td align="right">'.(($marginInfo['margin_rate_services'] == '')?'':price($marginInfo['margin_rate_services'], null, null, null, null, $rounding).'%').'</td>';
  231. if (! empty($conf->global->DISPLAY_MARK_RATES))
  232. print '<td align="right">'.(($marginInfo['mark_rate_services'] == '')?'':price($marginInfo['mark_rate_services'], null, null, null, null, $rounding).'%').'</td>';
  233. print '</tr>';
  234. }
  235. if (! empty($conf->product->enabled) && ! empty($conf->service->enabled))
  236. {
  237. print '<tr class="liste_total">';
  238. print '<td>'.$langs->trans('TotalMargin').'</td>';
  239. print '<td align="right">'.price($marginInfo['pv_total'], null, null, null, null, $rounding).'</td>';
  240. print '<td align="right">'.price($marginInfo['pa_total'], null, null, null, null, $rounding).'</td>';
  241. print '<td align="right">'.price($marginInfo['total_margin'], null, null, null, null, $rounding).'</td>';
  242. if (! empty($conf->global->DISPLAY_MARGIN_RATES))
  243. print '<td align="right">'.(($marginInfo['total_margin_rate'] == '')?'':price($marginInfo['total_margin_rate'], null, null, null, null, $rounding).'%').'</td>';
  244. if (! empty($conf->global->DISPLAY_MARK_RATES))
  245. print '<td align="right">'.(($marginInfo['total_mark_rate'] == '')?'':price($marginInfo['total_mark_rate'], null, null, null, null, $rounding).'%').'</td>';
  246. print '</tr>';
  247. }
  248. print '</table>';
  249. }
  250. }