2015-06-06 15:19:25 +02:00
< ? php
2019-12-31 17:30:27 +01:00
/* Copyright ( c ) 2015 - 2019 Laurent Destailleur < eldy @ users . sourceforge . net >
2015-06-06 15:19:25 +02:00
*
* This program is free software ; you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation ; either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
2019-09-23 21:55:30 +02:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2015-06-06 15:19:25 +02:00
*/
/**
* \file htdocs / core / class / html . formmargin . class . php
* \ingroup core
* \brief Fichier de la classe des fonctions predefinie de composants html autre
*/
/**
* Classe permettant la generation de composants html autre
* Only common components are here .
*/
class FormMargin
{
2020-10-31 14:32:18 +01:00
/**
* @ var DoliDB Database handler .
*/
public $db ;
2018-09-16 10:22:23 +02:00
2020-10-31 14:32:18 +01:00
/**
2018-08-22 10:37:16 +02:00
* @ var string Error code ( or message )
*/
2020-04-10 10:59:32 +02:00
public $error = '' ;
2015-06-06 15:19:25 +02:00
2020-10-31 14:32:18 +01:00
/**
* Constructor
*
* @ param DoliDB $db Database handler
*/
public function __construct ( $db )
{
$this -> db = $db ;
}
2015-06-06 15:19:25 +02:00
/**
* get array with margin information from lines of object
2017-01-28 15:01:17 +01:00
* TODO Move this in common class .
2015-06-06 15:19:25 +02:00
*
* @ param CommonObject $object Object we want to get margin information for
* @ param boolean $force_price True of not
* @ return array Array with info
*/
2019-02-27 20:45:07 +01:00
public function getMarginInfosArray ( $object , $force_price = false )
2015-06-06 15:19:25 +02:00
{
global $conf , $db ;
// Default returned array
$marginInfos = array (
'pa_products' => 0 ,
'pv_products' => 0 ,
'margin_on_products' => 0 ,
'margin_rate_products' => '' ,
'mark_rate_products' => '' ,
'pa_services' => 0 ,
'pv_services' => 0 ,
'margin_on_services' => 0 ,
'margin_rate_services' => '' ,
'mark_rate_services' => '' ,
'pa_total' => 0 ,
'pv_total' => 0 ,
'total_margin' => 0 ,
'total_margin_rate' => '' ,
'total_mark_rate' => ''
);
2021-02-23 22:03:23 +01:00
foreach ( $object -> lines as $line ) {
if ( empty ( $line -> pa_ht ) && isset ( $line -> fk_fournprice ) && ! $force_price ) {
2015-06-06 15:19:25 +02:00
require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.product.class.php' ;
2020-09-19 23:30:29 +02:00
$product = new ProductFournisseur ( $this -> db );
2021-02-23 22:03:23 +01:00
if ( $product -> fetch_product_fournisseur_price ( $line -> fk_fournprice )) {
2015-06-06 15:19:25 +02:00
$line -> pa_ht = $product -> fourn_unitprice * ( 1 - $product -> fourn_remise_percent / 100 );
2021-02-23 22:03:23 +01:00
}
2015-06-06 15:19:25 +02:00
}
2021-03-24 21:49:45 +01:00
// 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.
//if ((!isset($line->pa_ht) || $line->pa_ht == 0) && $line->subprice > 0 && (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull > 0)) {
2023-11-27 12:24:18 +01:00
if (( ! isset ( $line -> pa_ht )) && $line -> subprice > 0 && ( isset ( $conf -> global -> ForceBuyingPriceIfNull ) && getDolGlobalInt ( 'ForceBuyingPriceIfNull' ) > 0 )) {
2015-06-06 15:19:25 +02:00
$line -> pa_ht = $line -> subprice * ( 1 - ( $line -> remise_percent / 100 ));
}
2019-05-13 12:02:46 +02:00
$pv = $line -> total_ht ;
2023-05-10 23:55:50 +02:00
// We choosed to have line->pa_ht always positive in database, so we guess the correct sign
$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 );
2023-03-15 13:49:28 +01:00
if ( getDolGlobalInt ( 'INVOICE_USE_SITUATION' ) == 1 ) { // Special case for old situation mode
if (( $object -> element == 'facture' && $object -> type == $object :: TYPE_SITUATION )
|| ( $object -> element == 'facture' && $object -> type == $object :: TYPE_CREDIT_NOTE && getDolGlobalInt ( 'INVOICE_USE_SITUATION_CREDIT_NOTE' ) && $object -> situation_counter > 0 )) {
// We need a compensation relative to $line->situation_percent
$pa = $line -> qty * $pa_ht * ( $line -> situation_percent / 100 );
} else {
$pa = $line -> qty * $pa_ht ;
}
2019-12-19 10:35:38 +01:00
} else {
$pa = $line -> qty * $pa_ht ;
}
2018-02-25 14:13:09 +01:00
2015-06-06 15:19:25 +02:00
// calcul des marges
if ( isset ( $line -> fk_remise_except ) && isset ( $conf -> global -> MARGIN_METHODE_FOR_DISCOUNT )) { // remise
2022-10-10 23:18:13 +02:00
if ( getDolGlobalString ( 'MARGIN_METHODE_FOR_DISCOUNT' ) == '1' ) { // remise globale considérée comme produit
2015-06-06 15:19:25 +02:00
$marginInfos [ 'pa_products' ] += $pa ;
$marginInfos [ 'pv_products' ] += $pv ;
2020-04-10 10:59:32 +02:00
$marginInfos [ 'pa_total' ] += $pa ;
$marginInfos [ 'pv_total' ] += $pv ;
2015-06-06 15:19:25 +02:00
// if credit note, margin = -1 * (abs(selling_price) - buying_price)
2017-01-28 15:01:17 +01:00
//if ($pv < 0)
//{
// $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
//}
//else
2023-12-04 12:04:36 +01:00
$marginInfos [ 'margin_on_products' ] += $pv - $pa ;
2022-10-10 23:18:13 +02:00
} elseif ( getDolGlobalString ( 'MARGIN_METHODE_FOR_DISCOUNT' ) == '2' ) { // remise globale considérée comme service
2015-06-06 15:19:25 +02:00
$marginInfos [ 'pa_services' ] += $pa ;
$marginInfos [ 'pv_services' ] += $pv ;
2020-04-10 10:59:32 +02:00
$marginInfos [ 'pa_total' ] += $pa ;
$marginInfos [ 'pv_total' ] += $pv ;
2015-06-06 15:19:25 +02:00
// if credit note, margin = -1 * (abs(selling_price) - buying_price)
2017-01-28 15:01:17 +01:00
//if ($pv < 0)
// $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
//else
2023-12-04 12:04:36 +01:00
$marginInfos [ 'margin_on_services' ] += $pv - $pa ;
2022-10-10 23:18:13 +02:00
} elseif ( getDolGlobalString ( 'MARGIN_METHODE_FOR_DISCOUNT' ) == '3' ) { // remise globale prise en compte uniqt sur total
2015-06-06 15:19:25 +02:00
$marginInfos [ 'pa_total' ] += $pa ;
$marginInfos [ 'pv_total' ] += $pv ;
}
2020-05-21 15:05:19 +02:00
} else {
2020-04-10 10:59:32 +02:00
$type = $line -> product_type ? $line -> product_type : $line -> fk_product_type ;
2015-06-06 15:19:25 +02:00
if ( $type == 0 ) { // product
$marginInfos [ 'pa_products' ] += $pa ;
$marginInfos [ 'pv_products' ] += $pv ;
2020-04-10 10:59:32 +02:00
$marginInfos [ 'pa_total' ] += $pa ;
$marginInfos [ 'pv_total' ] += $pv ;
2015-06-06 15:19:25 +02:00
// if credit note, margin = -1 * (abs(selling_price) - buying_price)
2017-01-28 15:01:17 +01:00
//if ($pv < 0)
//{
// $marginInfos['margin_on_products'] += -1 * (abs($pv) - $pa);
//}
//else
//{
2023-12-04 12:04:36 +01:00
$marginInfos [ 'margin_on_products' ] += $pv - $pa ;
2017-01-28 15:01:17 +01:00
//}
2020-05-21 15:05:19 +02:00
} elseif ( $type == 1 ) { // service
2015-06-06 15:19:25 +02:00
$marginInfos [ 'pa_services' ] += $pa ;
$marginInfos [ 'pv_services' ] += $pv ;
2020-04-10 10:59:32 +02:00
$marginInfos [ 'pa_total' ] += $pa ;
$marginInfos [ 'pv_total' ] += $pv ;
2015-06-06 15:19:25 +02:00
// if credit note, margin = -1 * (abs(selling_price) - buying_price)
2017-01-28 15:01:17 +01:00
//if ($pv < 0)
// $marginInfos['margin_on_services'] += -1 * (abs($pv) - $pa);
//else
2023-12-04 12:04:36 +01:00
$marginInfos [ 'margin_on_services' ] += $pv - $pa ;
2015-06-06 15:19:25 +02:00
}
}
}
2021-02-23 22:03:23 +01:00
if ( $marginInfos [ 'pa_products' ] > 0 ) {
2015-06-06 15:19:25 +02:00
$marginInfos [ 'margin_rate_products' ] = 100 * $marginInfos [ 'margin_on_products' ] / $marginInfos [ 'pa_products' ];
2021-02-23 22:03:23 +01:00
}
if ( $marginInfos [ 'pv_products' ] > 0 ) {
2015-06-06 15:19:25 +02:00
$marginInfos [ 'mark_rate_products' ] = 100 * $marginInfos [ 'margin_on_products' ] / $marginInfos [ 'pv_products' ];
2021-02-23 22:03:23 +01:00
}
2015-06-06 15:19:25 +02:00
2021-02-23 22:03:23 +01:00
if ( $marginInfos [ 'pa_services' ] > 0 ) {
2015-06-06 15:19:25 +02:00
$marginInfos [ 'margin_rate_services' ] = 100 * $marginInfos [ 'margin_on_services' ] / $marginInfos [ 'pa_services' ];
2021-02-23 22:03:23 +01:00
}
if ( $marginInfos [ 'pv_services' ] > 0 ) {
2015-06-06 15:19:25 +02:00
$marginInfos [ 'mark_rate_services' ] = 100 * $marginInfos [ 'margin_on_services' ] / $marginInfos [ 'pv_services' ];
2021-02-23 22:03:23 +01:00
}
2015-06-06 15:19:25 +02:00
// if credit note, margin = -1 * (abs(selling_price) - buying_price)
2017-01-28 15:01:17 +01:00
//if ($marginInfos['pv_total'] < 0)
// $marginInfos['total_margin'] = -1 * (abs($marginInfos['pv_total']) - $marginInfos['pa_total']);
//else
2023-12-04 12:04:36 +01:00
$marginInfos [ 'total_margin' ] = $marginInfos [ 'pv_total' ] - $marginInfos [ 'pa_total' ];
2021-02-23 22:03:23 +01:00
if ( $marginInfos [ 'pa_total' ] > 0 ) {
2015-06-06 15:19:25 +02:00
$marginInfos [ 'total_margin_rate' ] = 100 * $marginInfos [ 'total_margin' ] / $marginInfos [ 'pa_total' ];
2021-02-23 22:03:23 +01:00
}
if ( $marginInfos [ 'pv_total' ] > 0 ) {
2015-06-06 15:19:25 +02:00
$marginInfos [ 'total_mark_rate' ] = 100 * $marginInfos [ 'total_margin' ] / $marginInfos [ 'pv_total' ];
2021-02-23 22:03:23 +01:00
}
2015-06-06 15:19:25 +02:00
return $marginInfos ;
}
/**
* Show the array with all margin infos
*
* @ param CommonObject $object Object we want to get margin information for
* @ param boolean $force_price Force price
* @ return void
*/
2019-02-27 20:45:07 +01:00
public function displayMarginInfos ( $object , $force_price = false )
2015-06-06 15:19:25 +02:00
{
2021-11-18 10:50:03 +01:00
global $langs , $conf , $user , $hookmanager ;
2023-08-26 12:02:19 +02:00
global $action ;
2015-06-06 15:19:25 +02:00
2021-02-23 22:03:23 +01:00
if ( ! empty ( $user -> socid )) {
return ;
}
2015-06-06 15:19:25 +02:00
2023-10-15 15:32:35 +02:00
if ( ! $user -> hasRight ( 'margins' , 'liretous' )) {
2021-02-23 22:03:23 +01:00
return ;
}
2015-06-06 15:19:25 +02:00
$marginInfo = $this -> getMarginInfosArray ( $object , $force_price );
2023-03-15 13:49:28 +01:00
$parameters = array ( 'marginInfo' => & $marginInfo );
2021-11-18 10:50:03 +01:00
$reshook = $hookmanager -> executeHooks ( 'displayMarginInfos' , $parameters , $object , $action );
if ( $reshook < 0 ) {
setEventMessages ( $hookmanager -> error , $hookmanager -> errors , 'errors' );
} elseif ( empty ( $reshook )) {
2023-11-27 11:24:19 +01:00
if ( getDolGlobalString ( 'MARGIN_ADD_SHOWHIDE_BUTTON' )) {
2022-12-06 14:04:17 +01:00
print $langs -> trans ( 'ShowMarginInfos' ) . ' ' ;
2021-11-18 10:50:03 +01:00
$hidemargininfos = preg_replace ( '/[^a-zA-Z0-9_\-]/' , '' , $_COOKIE [ 'DOLUSER_MARGININFO_HIDE_SHOW' ]); // Clean cookie
2022-12-06 14:04:17 +01:00
print '<span id="showMarginInfos" class="linkobject valignmiddle ' . ( ! empty ( $hidemargininfos ) ? '' : 'hideobject' ) . '">' . img_picto ( $langs -> trans ( " Disabled " ), 'switch_off' ) . '</span>' ;
print '<span id="hideMarginInfos" class="linkobject valignmiddle ' . ( ! empty ( $hidemargininfos ) ? 'hideobject' : '' ) . '">' . img_picto ( $langs -> trans ( " Enabled " ), 'switch_on' ) . '</span>' ;
2015-06-06 15:19:25 +02:00
2023-02-18 15:10:05 +01:00
print '<script nonce="' . getNonce () . '">$(document).ready(function() {' ;
print '$("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"); });' ;
print '$("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"); });' ;
2021-11-18 10:50:03 +01:00
if ( ! empty ( $hidemargininfos )) {
2023-02-18 15:10:05 +01:00
print 'console.log("hide the margin info"); $("#margintable").hide();' ;
2021-11-18 10:50:03 +01:00
}
2023-02-18 15:10:05 +01:00
print '});</script>' ;
2021-02-23 22:03:23 +01:00
}
2018-03-16 11:19:58 +01:00
2023-03-15 13:49:28 +01:00
print '<!-- displayMarginInfos() - Show margin table -->' . " \n " ;
2021-11-18 10:50:03 +01:00
print '<div class="div-table-responsive-no-min">' ;
2015-06-06 15:19:25 +02:00
2022-12-06 14:04:17 +01:00
print '<table class="noborder margintable centpercent" id="margintable">' ;
2021-11-18 10:50:03 +01:00
print '<tr class="liste_titre">' ;
print '<td class="liste_titre">' . $langs -> trans ( 'Margins' ) . '</td>' ;
print '<td class="liste_titre right">' . $langs -> trans ( 'SellingPrice' ) . '</td>' ;
2023-10-08 23:19:49 +02:00
if ( getDolGlobalString ( 'MARGIN_TYPE' ) == " 1 " ) {
2021-11-18 10:50:03 +01:00
print '<td class="liste_titre right">' . $langs -> trans ( 'BuyingPrice' ) . '</td>' ;
} else {
print '<td class="liste_titre right">' . $langs -> trans ( 'CostPrice' ) . '</td>' ;
}
print '<td class="liste_titre right">' . $langs -> trans ( 'Margin' ) . '</td>' ;
2023-11-27 11:24:19 +01:00
if ( getDolGlobalString ( 'DISPLAY_MARGIN_RATES' )) {
2021-11-18 10:50:03 +01:00
print '<td class="liste_titre right">' . $langs -> trans ( 'MarginRate' ) . '</td>' ;
2021-02-23 22:03:23 +01:00
}
2023-11-27 11:24:19 +01:00
if ( getDolGlobalString ( 'DISPLAY_MARK_RATES' )) {
2021-11-18 10:50:03 +01:00
print '<td class="liste_titre right">' . $langs -> trans ( 'MarkRate' ) . '</td>' ;
2021-02-23 22:03:23 +01:00
}
2015-06-06 15:19:25 +02:00
print '</tr>' ;
2022-08-23 20:01:34 +02:00
if ( isModEnabled ( " product " )) {
2021-11-18 10:50:03 +01:00
//if ($marginInfo['margin_on_products'] != 0 && $marginInfo['margin_on_services'] != 0) {
print '<tr class="oddeven">' ;
print '<td>' . $langs -> trans ( 'MarginOnProducts' ) . '</td>' ;
print '<td class="right">' . price ( $marginInfo [ 'pv_products' ]) . '</td>' ;
print '<td class="right">' . price ( $marginInfo [ 'pa_products' ]) . '</td>' ;
print '<td class="right">' . price ( $marginInfo [ 'margin_on_products' ]) . '</td>' ;
2023-11-27 11:24:19 +01:00
if ( getDolGlobalString ( 'DISPLAY_MARGIN_RATES' )) {
2021-11-18 10:50:03 +01:00
print '<td class="right">' . (( $marginInfo [ 'margin_rate_products' ] == '' ) ? '' : price ( $marginInfo [ 'margin_rate_products' ], null , null , null , null , 2 ) . '%' ) . '</td>' ;
}
2023-11-27 11:24:19 +01:00
if ( getDolGlobalString ( 'DISPLAY_MARK_RATES' )) {
2021-11-18 10:50:03 +01:00
print '<td class="right">' . (( $marginInfo [ 'mark_rate_products' ] == '' ) ? '' : price ( $marginInfo [ 'mark_rate_products' ], null , null , null , null , 2 ) . '%' ) . '</td>' ;
}
print '</tr>' ;
2021-02-23 22:03:23 +01:00
}
2015-06-06 15:19:25 +02:00
2022-08-23 20:02:37 +02:00
if ( isModEnabled ( " service " )) {
2021-11-18 10:50:03 +01:00
print '<tr class="oddeven">' ;
print '<td>' . $langs -> trans ( 'MarginOnServices' ) . '</td>' ;
print '<td class="right">' . price ( $marginInfo [ 'pv_services' ]) . '</td>' ;
print '<td class="right">' . price ( $marginInfo [ 'pa_services' ]) . '</td>' ;
print '<td class="right">' . price ( $marginInfo [ 'margin_on_services' ]) . '</td>' ;
2023-11-27 11:24:19 +01:00
if ( getDolGlobalString ( 'DISPLAY_MARGIN_RATES' )) {
2021-11-18 10:50:03 +01:00
print '<td class="right">' . (( $marginInfo [ 'margin_rate_services' ] == '' ) ? '' : price ( $marginInfo [ 'margin_rate_services' ], null , null , null , null , 2 ) . '%' ) . '</td>' ;
}
2023-11-27 11:24:19 +01:00
if ( getDolGlobalString ( 'DISPLAY_MARK_RATES' )) {
2021-11-18 10:50:03 +01:00
print '<td class="right">' . (( $marginInfo [ 'mark_rate_services' ] == '' ) ? '' : price ( $marginInfo [ 'mark_rate_services' ], null , null , null , null , 2 ) . '%' ) . '</td>' ;
}
print '</tr>' ;
2021-02-23 22:03:23 +01:00
}
2021-11-18 10:50:03 +01:00
2022-08-23 20:02:37 +02:00
if ( isModEnabled ( " product " ) && isModEnabled ( " service " )) {
2021-11-18 10:50:03 +01:00
print '<tr class="liste_total">' ;
print '<td>' . $langs -> trans ( 'TotalMargin' ) . '</td>' ;
print '<td class="right">' . price ( $marginInfo [ 'pv_total' ]) . '</td>' ;
print '<td class="right">' . price ( $marginInfo [ 'pa_total' ]) . '</td>' ;
print '<td class="right">' . price ( $marginInfo [ 'total_margin' ]) . '</td>' ;
2023-11-27 11:24:19 +01:00
if ( getDolGlobalString ( 'DISPLAY_MARGIN_RATES' )) {
2021-11-18 10:50:03 +01:00
print '<td class="right">' . (( $marginInfo [ 'total_margin_rate' ] == '' ) ? '' : price ( $marginInfo [ 'total_margin_rate' ], null , null , null , null , 2 ) . '%' ) . '</td>' ;
}
2023-11-27 11:24:19 +01:00
if ( getDolGlobalString ( 'DISPLAY_MARK_RATES' )) {
2021-11-18 10:50:03 +01:00
print '<td class="right">' . (( $marginInfo [ 'total_mark_rate' ] == '' ) ? '' : price ( $marginInfo [ 'total_mark_rate' ], null , null , null , null , 2 ) . '%' ) . '</td>' ;
}
print '</tr>' ;
2021-02-23 22:03:23 +01:00
}
2021-11-18 10:50:03 +01:00
print $hookmanager -> resPrint ;
print '</table>' ;
print '</div>' ;
} elseif ( $reshook > 0 ) {
print $hookmanager -> resPrint ;
2015-06-06 15:19:25 +02:00
}
}
}