html.formmargin.class.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. <?php
  2. /* Copyright (c) 2015-2019 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 <https://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. public 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. public 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. if (empty($line->pa_ht) && isset($line->fk_fournprice) && !$force_price) {
  76. require_once DOL_DOCUMENT_ROOT.'/fourn/class/fournisseur.product.class.php';
  77. $product = new ProductFournisseur($this->db);
  78. if ($product->fetch_product_fournisseur_price($line->fk_fournprice)) {
  79. $line->pa_ht = $product->fourn_unitprice * (1 - $product->fourn_remise_percent / 100);
  80. }
  81. }
  82. // If buy price is not defined (null), we will use the sell price. If defined to 0 (it means it was forced to 0 during insert, for example for a free to get product), we must still use 0.
  83. //if ((!isset($line->pa_ht) || $line->pa_ht == 0) && $line->subprice > 0 && (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull > 0)) {
  84. if ((!isset($line->pa_ht)) && $line->subprice > 0 && (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull > 0)) {
  85. $line->pa_ht = $line->subprice * (1 - ($line->remise_percent / 100));
  86. }
  87. $pv = $line->total_ht;
  88. $pa_ht = (($pv < 0 || ($pv == 0 && in_array($object->element, array('facture', 'facture_fourn')) && $object->type == $object::TYPE_CREDIT_NOTE)) ? -$line->pa_ht : $line->pa_ht); // We choosed to have line->pa_ht always positive in database, so we guess the correct sign
  89. if (getDolGlobalInt('INVOICE_USE_SITUATION') == 1) { // Special case for old situation mode
  90. if (($object->element == 'facture' && $object->type == $object::TYPE_SITUATION)
  91. || ($object->element == 'facture' && $object->type == $object::TYPE_CREDIT_NOTE && getDolGlobalInt('INVOICE_USE_SITUATION_CREDIT_NOTE') && $object->situation_counter > 0)) {
  92. // We need a compensation relative to $line->situation_percent
  93. $pa = $line->qty * $pa_ht * ($line->situation_percent / 100);
  94. } else {
  95. $pa = $line->qty * $pa_ht;
  96. }
  97. } else {
  98. $pa = $line->qty * $pa_ht;
  99. }
  100. // calcul des marges
  101. if (isset($line->fk_remise_except) && isset($conf->global->MARGIN_METHODE_FOR_DISCOUNT)) { // remise
  102. if (getDolGlobalString('MARGIN_METHODE_FOR_DISCOUNT') == '1') { // remise globale considérée comme produit
  103. $marginInfos['pa_products'] += $pa;
  104. $marginInfos['pv_products'] += $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. //{
  110. // $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
  111. //}
  112. //else
  113. $marginInfos['margin_on_products'] += $pv - $pa;
  114. } elseif (getDolGlobalString('MARGIN_METHODE_FOR_DISCOUNT') == '2') { // remise globale considérée comme service
  115. $marginInfos['pa_services'] += $pa;
  116. $marginInfos['pv_services'] += $pv;
  117. $marginInfos['pa_total'] += $pa;
  118. $marginInfos['pv_total'] += $pv;
  119. // if credit note, margin = -1 * (abs(selling_price) - buying_price)
  120. //if ($pv < 0)
  121. // $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
  122. //else
  123. $marginInfos['margin_on_services'] += $pv - $pa;
  124. } elseif (getDolGlobalString('MARGIN_METHODE_FOR_DISCOUNT') == '3') { // remise globale prise en compte uniqt sur total
  125. $marginInfos['pa_total'] += $pa;
  126. $marginInfos['pv_total'] += $pv;
  127. }
  128. } else {
  129. $type = $line->product_type ? $line->product_type : $line->fk_product_type;
  130. if ($type == 0) { // product
  131. $marginInfos['pa_products'] += $pa;
  132. $marginInfos['pv_products'] += $pv;
  133. $marginInfos['pa_total'] += $pa;
  134. $marginInfos['pv_total'] += $pv;
  135. // if credit note, margin = -1 * (abs(selling_price) - buying_price)
  136. //if ($pv < 0)
  137. //{
  138. // $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
  139. //}
  140. //else
  141. //{
  142. $marginInfos['margin_on_products'] += $pv - $pa;
  143. //}
  144. } elseif ($type == 1) { // service
  145. $marginInfos['pa_services'] += $pa;
  146. $marginInfos['pv_services'] += $pv;
  147. $marginInfos['pa_total'] += $pa;
  148. $marginInfos['pv_total'] += $pv;
  149. // if credit note, margin = -1 * (abs(selling_price) - buying_price)
  150. //if ($pv < 0)
  151. // $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
  152. //else
  153. $marginInfos['margin_on_services'] += $pv - $pa;
  154. }
  155. }
  156. }
  157. if ($marginInfos['pa_products'] > 0) {
  158. $marginInfos['margin_rate_products'] = 100 * $marginInfos['margin_on_products'] / $marginInfos['pa_products'];
  159. }
  160. if ($marginInfos['pv_products'] > 0) {
  161. $marginInfos['mark_rate_products'] = 100 * $marginInfos['margin_on_products'] / $marginInfos['pv_products'];
  162. }
  163. if ($marginInfos['pa_services'] > 0) {
  164. $marginInfos['margin_rate_services'] = 100 * $marginInfos['margin_on_services'] / $marginInfos['pa_services'];
  165. }
  166. if ($marginInfos['pv_services'] > 0) {
  167. $marginInfos['mark_rate_services'] = 100 * $marginInfos['margin_on_services'] / $marginInfos['pv_services'];
  168. }
  169. // if credit note, margin = -1 * (abs(selling_price) - buying_price)
  170. //if ($marginInfos['pv_total'] < 0)
  171. // $marginInfos['total_margin'] = -1 * (abs($marginInfos['pv_total']) - $marginInfos['pa_total']);
  172. //else
  173. $marginInfos['total_margin'] = $marginInfos['pv_total'] - $marginInfos['pa_total'];
  174. if ($marginInfos['pa_total'] > 0) {
  175. $marginInfos['total_margin_rate'] = 100 * $marginInfos['total_margin'] / $marginInfos['pa_total'];
  176. }
  177. if ($marginInfos['pv_total'] > 0) {
  178. $marginInfos['total_mark_rate'] = 100 * $marginInfos['total_margin'] / $marginInfos['pv_total'];
  179. }
  180. return $marginInfos;
  181. }
  182. /**
  183. * Show the array with all margin infos
  184. *
  185. * @param CommonObject $object Object we want to get margin information for
  186. * @param boolean $force_price Force price
  187. * @return void
  188. */
  189. public function displayMarginInfos($object, $force_price = false)
  190. {
  191. global $langs, $conf, $user, $hookmanager;
  192. if (!empty($user->socid)) {
  193. return;
  194. }
  195. if (empty($user->rights->margins->liretous)) {
  196. return;
  197. }
  198. $marginInfo = $this->getMarginInfosArray($object, $force_price);
  199. $parameters=array('marginInfo' => &$marginInfo);
  200. $reshook = $hookmanager->executeHooks('displayMarginInfos', $parameters, $object, $action);
  201. if ($reshook < 0) {
  202. setEventMessages($hookmanager->error, $hookmanager->errors, 'errors');
  203. } elseif (empty($reshook)) {
  204. if (!empty($conf->global->MARGIN_ADD_SHOWHIDE_BUTTON)) {
  205. print $langs->trans('ShowMarginInfos') . ' ';
  206. $hidemargininfos = preg_replace('/[^a-zA-Z0-9_\-]/', '', $_COOKIE['DOLUSER_MARGININFO_HIDE_SHOW']); // Clean cookie
  207. print '<span id="showMarginInfos" class="linkobject valignmiddle ' . (!empty($hidemargininfos) ? '' : 'hideobject') . '">' . img_picto($langs->trans("Disabled"), 'switch_off') . '</span>';
  208. print '<span id="hideMarginInfos" class="linkobject valignmiddle ' . (!empty($hidemargininfos) ? 'hideobject' : '') . '">' . img_picto($langs->trans("Enabled"), 'switch_on') . '</span>';
  209. print '<script>$(document).ready(function() {
  210. $("span#showMarginInfos").click(function() { console.log("click on showMargininfos"); date = new Date(); date.setTime(date.getTime()+(30*86400000)); document.cookie = "DOLUSER_MARGININFO_HIDE_SHOW=0; expires=" + date.toGMTString() + "; path=/ "; $(".margintable").show(); $("span#showMarginInfos").addClass("hideobject"); $("span#hideMarginInfos").removeClass("hideobject"); });
  211. $("span#hideMarginInfos").click(function() { console.log("click on hideMarginInfos"); date = new Date(); date.setTime(date.getTime()+(30*86400000)); document.cookie = "DOLUSER_MARGININFO_HIDE_SHOW=1; expires=" + date.toGMTString() + "; path=/ "; $(".margintable").hide(); $("span#hideMarginInfos").addClass("hideobject"); $("span#showMarginInfos").removeClass("hideobject"); });
  212. });</script>';
  213. if (!empty($hidemargininfos)) {
  214. print '<script>$(document).ready(function() { console.log("hide the margin info"); $("#margintable").hide(); });</script>';
  215. }
  216. }
  217. print '<!-- displayMarginInfos() - Show margin table -->' . "\n";
  218. print '<div class="div-table-responsive-no-min">';
  219. print '<table class="noborder margintable centpercent" id="margintable">';
  220. print '<tr class="liste_titre">';
  221. print '<td class="liste_titre">' . $langs->trans('Margins') . '</td>';
  222. print '<td class="liste_titre right">' . $langs->trans('SellingPrice') . '</td>';
  223. if ($conf->global->MARGIN_TYPE == "1") {
  224. print '<td class="liste_titre right">' . $langs->trans('BuyingPrice') . '</td>';
  225. } else {
  226. print '<td class="liste_titre right">' . $langs->trans('CostPrice') . '</td>';
  227. }
  228. print '<td class="liste_titre right">' . $langs->trans('Margin') . '</td>';
  229. if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
  230. print '<td class="liste_titre right">' . $langs->trans('MarginRate') . '</td>';
  231. }
  232. if (!empty($conf->global->DISPLAY_MARK_RATES)) {
  233. print '<td class="liste_titre right">' . $langs->trans('MarkRate') . '</td>';
  234. }
  235. print '</tr>';
  236. if (isModEnabled("product")) {
  237. //if ($marginInfo['margin_on_products'] != 0 && $marginInfo['margin_on_services'] != 0) {
  238. print '<tr class="oddeven">';
  239. print '<td>' . $langs->trans('MarginOnProducts') . '</td>';
  240. print '<td class="right">' . price($marginInfo['pv_products']) . '</td>';
  241. print '<td class="right">' . price($marginInfo['pa_products']) . '</td>';
  242. print '<td class="right">' . price($marginInfo['margin_on_products']) . '</td>';
  243. if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
  244. print '<td class="right">' . (($marginInfo['margin_rate_products'] == '') ? '' : price($marginInfo['margin_rate_products'], null, null, null, null, 2) . '%') . '</td>';
  245. }
  246. if (!empty($conf->global->DISPLAY_MARK_RATES)) {
  247. print '<td class="right">' . (($marginInfo['mark_rate_products'] == '') ? '' : price($marginInfo['mark_rate_products'], null, null, null, null, 2) . '%') . '</td>';
  248. }
  249. print '</tr>';
  250. }
  251. if (isModEnabled("service")) {
  252. print '<tr class="oddeven">';
  253. print '<td>' . $langs->trans('MarginOnServices') . '</td>';
  254. print '<td class="right">' . price($marginInfo['pv_services']) . '</td>';
  255. print '<td class="right">' . price($marginInfo['pa_services']) . '</td>';
  256. print '<td class="right">' . price($marginInfo['margin_on_services']) . '</td>';
  257. if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
  258. print '<td class="right">' . (($marginInfo['margin_rate_services'] == '') ? '' : price($marginInfo['margin_rate_services'], null, null, null, null, 2) . '%') . '</td>';
  259. }
  260. if (!empty($conf->global->DISPLAY_MARK_RATES)) {
  261. print '<td class="right">' . (($marginInfo['mark_rate_services'] == '') ? '' : price($marginInfo['mark_rate_services'], null, null, null, null, 2) . '%') . '</td>';
  262. }
  263. print '</tr>';
  264. }
  265. if (isModEnabled("product") && isModEnabled("service")) {
  266. print '<tr class="liste_total">';
  267. print '<td>' . $langs->trans('TotalMargin') . '</td>';
  268. print '<td class="right">' . price($marginInfo['pv_total']) . '</td>';
  269. print '<td class="right">' . price($marginInfo['pa_total']) . '</td>';
  270. print '<td class="right">' . price($marginInfo['total_margin']) . '</td>';
  271. if (!empty($conf->global->DISPLAY_MARGIN_RATES)) {
  272. print '<td class="right">' . (($marginInfo['total_margin_rate'] == '') ? '' : price($marginInfo['total_margin_rate'], null, null, null, null, 2) . '%') . '</td>';
  273. }
  274. if (!empty($conf->global->DISPLAY_MARK_RATES)) {
  275. print '<td class="right">' . (($marginInfo['total_mark_rate'] == '') ? '' : price($marginInfo['total_mark_rate'], null, null, null, null, 2) . '%') . '</td>';
  276. }
  277. print '</tr>';
  278. }
  279. print $hookmanager->resPrint;
  280. print '</table>';
  281. print '</div>';
  282. } elseif ($reshook > 0) {
  283. print $hookmanager->resPrint;
  284. }
  285. }
  286. }