html.formmargin.class.php 13 KB

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