2012-07-18 13:52:15 +00:00
< ? php
2015-01-31 13:29:15 +00:00
/* Copyright ( C ) 2012 Christophe Battarel < christophe . battarel @ altairis . fr >
* Copyright ( C ) 2014 - 2015 Marcos García < marcosgdf @ gmail . com >
2016-02-09 19:01:43 +00:00
* Copyright ( C ) 2016 Florian Henry < florian . henry @ open - concept . pro >
2024-03-19 20:50:46 +00:00
* Copyright ( C ) 2024 MDW < mdeweerd @ users . noreply . github . com >
2012-07-18 13:52:15 +00: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
2013-01-16 14:36:08 +00:00
* the Free Software Foundation ; either version 3 of the License , or
2012-07-18 13:52:15 +00:00
* ( 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 19:55:30 +00:00
* along with this program . If not , see < https :// www . gnu . org / licenses />.
2012-07-18 13:52:15 +00:00
*/
/**
2012-07-20 07:57:50 +00:00
* \file / htdocs / margin / lib / margins . lib . php
* \ingroup margin
* \brief Library for common margin functions
2012-07-18 13:52:15 +00:00
*/
/**
* Define head array for tabs of marges tools setup pages
2012-07-25 10:20:45 +00:00
*
2024-10-13 23:59:44 +00:00
* @ return array < array { 0 : string , 1 : string , 2 : string } > Array of tabs to show
2012-07-18 13:52:15 +00:00
*/
function marges_admin_prepare_head ()
{
global $langs , $conf ;
2012-07-20 07:57:50 +00:00
2012-07-18 13:52:15 +00:00
$h = 0 ;
$head = array ();
2012-07-23 14:10:54 +00:00
$head [ $h ][ 0 ] = DOL_URL_ROOT . " /margin/admin/margin.php " ;
2012-07-18 13:52:15 +00:00
$head [ $h ][ 1 ] = $langs -> trans ( " Parameters " );
$head [ $h ][ 2 ] = 'parameters' ;
$h ++ ;
2012-07-20 07:57:50 +00:00
2012-11-03 15:32:49 +00:00
// Show more tabs from modules
// Entries must be declared in modules descriptor with line
2020-09-07 08:18:17 +00:00
// $this->tabs = array('entity:+tabname:Title:@mymodule:/mymodule/mypage.php?id=__ID__'); to add new tab
// $this->tabs = array('entity:-tabname); to remove a tab
2020-08-20 20:17:34 +00:00
complete_head_from_modules ( $conf , $langs , null , $head , $h , 'margesadmin' );
2012-07-18 13:52:15 +00:00
2020-08-20 20:17:34 +00:00
complete_head_from_modules ( $conf , $langs , null , $head , $h , 'margesadmin' , 'remove' );
2012-12-01 14:45:05 +00:00
2012-11-03 15:32:49 +00:00
return $head ;
2012-07-18 13:52:15 +00:00
}
2012-11-03 15:32:49 +00:00
/**
* Return array of tabs to used on pages for third parties cards .
*
2024-10-13 23:59:44 +00:00
* @ return array < array { 0 : string , 1 : string , 2 : string } > Array of tabs to show
2012-11-03 15:32:49 +00:00
*/
function marges_prepare_head ()
2012-07-18 13:52:15 +00:00
{
2015-01-13 00:48:58 +00:00
global $langs , $conf , $user ;
2016-03-30 21:33:19 +00:00
$langs -> load ( " margins " );
2012-07-18 13:52:15 +00:00
$h = 0 ;
$head = array ();
2023-10-15 16:39:13 +00:00
if ( $user -> hasRight ( 'produit' , 'lire' )) {
2015-01-13 00:48:58 +00:00
$head [ $h ][ 0 ] = DOL_URL_ROOT . " /margin/productMargins.php " ;
$head [ $h ][ 1 ] = $langs -> trans ( " ProductMargins " );
$head [ $h ][ 2 ] = 'productMargins' ;
$h ++ ;
}
2012-07-18 13:52:15 +00:00
2022-09-10 09:07:00 +00:00
if ( $user -> hasRight ( 'societe' , 'lire' )) {
2015-01-13 00:48:58 +00:00
$head [ $h ][ 0 ] = DOL_URL_ROOT . " /margin/customerMargins.php " ;
$head [ $h ][ 1 ] = $langs -> trans ( " CustomerMargins " );
$head [ $h ][ 2 ] = 'customerMargins' ;
$h ++ ;
}
2012-07-18 13:52:15 +00:00
2023-10-15 16:39:13 +00:00
if ( $user -> hasRight ( 'margins' , 'read' , 'all' )) {
2015-01-18 15:42:11 +00:00
$title = 'UserMargins' ;
} else {
$title = 'SalesRepresentativeMargins' ;
}
2012-07-20 07:57:50 +00:00
$head [ $h ][ 0 ] = DOL_URL_ROOT . " /margin/agentMargins.php " ;
2015-01-18 15:42:11 +00:00
$head [ $h ][ 1 ] = $langs -> trans ( $title );
2012-07-18 13:52:15 +00:00
$head [ $h ][ 2 ] = 'agentMargins' ;
2016-02-09 19:01:43 +00:00
2019-04-02 18:36:00 +00:00
2023-10-15 16:39:13 +00:00
if ( $user -> hasRight ( 'margins' , 'creer' )) {
2016-02-09 19:01:43 +00:00
$h ++ ;
$head [ $h ][ 0 ] = DOL_URL_ROOT . " /margin/checkMargins.php " ;
$head [ $h ][ 1 ] = $langs -> trans ( 'CheckMargins' );
$head [ $h ][ 2 ] = 'checkMargins' ;
}
2012-07-18 13:52:15 +00:00
2019-04-02 18:36:00 +00:00
complete_head_from_modules ( $conf , $langs , null , $head , $h , 'margins' );
2019-03-27 08:33:51 +00:00
2022-09-22 12:06:50 +00:00
complete_head_from_modules ( $conf , $langs , null , $head , $h , 'margins' , 'remove' );
2012-07-18 13:52:15 +00:00
return $head ;
}
2012-07-25 10:20:45 +00:00
/**
2013-12-16 12:24:00 +00:00
* Return an array with margins information of a line
2012-07-25 10:20:45 +00:00
*
2024-01-14 11:26:37 +00:00
* @ param float $pv_ht Selling price without tax
2013-12-16 13:15:03 +00:00
* @ param float $remise_percent Discount percent on line
* @ param float $tva_tx Vat rate ( not used )
* @ param float $localtax1_tx Vat rate special 1 ( not used )
* @ param float $localtax2_tx Vat rate special 2 ( not used )
2024-01-14 11:26:37 +00:00
* @ param int $fk_pa Id of buying price ( prefer set this to 0 and provide $pa_ht instead . With id , buying price may have change )
* @ param float $pa_ht Buying price without tax
2024-11-04 11:32:13 +00:00
* @ return array { 0 : float , 1 : float , 2 : float } Array of margin info ( buying price , marge rate , marque rate )
2012-07-25 10:20:45 +00:00
*/
2024-01-14 11:26:37 +00:00
function getMarginInfos ( $pv_ht , $remise_percent , $tva_tx , $localtax1_tx , $localtax2_tx , $fk_pa , $pa_ht )
2012-07-25 10:20:45 +00:00
{
2013-12-16 12:24:00 +00:00
global $db , $conf ;
2012-11-03 15:32:49 +00:00
2020-04-10 08:59:32 +00:00
$marge_tx_ret = '' ;
$marque_tx_ret = '' ;
2012-11-03 15:32:49 +00:00
2024-01-14 11:26:37 +00:00
if ( $fk_pa > 0 && empty ( $pa_ht )) {
2012-11-03 15:32:49 +00:00
require_once DOL_DOCUMENT_ROOT . '/fourn/class/fournisseur.product.class.php' ;
$product = new ProductFournisseur ( $db );
2021-02-26 17:26:44 +00:00
if ( $product -> fetch_product_fournisseur_price ( $fk_pa )) {
2024-01-14 11:26:37 +00:00
$pa_ht_ret = $product -> fourn_unitprice * ( 1 - $product -> fourn_remise_percent / 100 );
2020-05-21 13:05:19 +00:00
} else {
2024-01-14 11:26:37 +00:00
$pa_ht_ret = $pa_ht ;
2013-12-16 12:24:00 +00:00
}
2020-05-21 13:05:19 +00:00
} else {
2024-01-14 11:26:37 +00:00
$pa_ht_ret = $pa_ht ;
2013-12-16 12:24:00 +00:00
}
2014-03-19 23:21:52 +00:00
2013-12-16 12:24:00 +00:00
// Calculate selling unit price including line discount
// We don't use calculate_price, because this function is dedicated to calculation of total with accuracy of total. We need an accuracy of a unit price.
// Also we must not apply rounding on non decimal rule defined by option MAIN_ROUNDING_RULE_TOT
2024-01-14 11:26:37 +00:00
$pu_ht_remise = $pv_ht * ( 1 - ( $remise_percent / 100 ));
2013-12-16 12:24:00 +00:00
$pu_ht_remise = price2num ( $pu_ht_remise , 'MU' );
2012-11-03 15:32:49 +00:00
2013-10-05 08:21:38 +00:00
// calcul marge
2021-02-26 17:26:44 +00:00
if ( $pu_ht_remise < 0 ) {
2024-03-19 20:50:46 +00:00
$marge = - 1 * ( abs (( float ) $pu_ht_remise ) - $pa_ht_ret );
2021-02-26 17:26:44 +00:00
} else {
2024-03-23 00:13:02 +00:00
$marge = ( float ) $pu_ht_remise - $pa_ht_ret ;
2021-02-26 17:26:44 +00:00
}
2013-10-05 08:21:38 +00:00
2012-11-03 15:32:49 +00:00
// calcul taux marge
2024-01-14 11:26:37 +00:00
if ( $pa_ht_ret != 0 ) {
$marge_tx_ret = ( 100 * $marge ) / $pa_ht_ret ;
2021-02-26 17:26:44 +00:00
}
2012-11-03 15:32:49 +00:00
// calcul taux marque
2021-02-26 17:26:44 +00:00
if ( $pu_ht_remise != 0 ) {
2024-03-23 00:13:02 +00:00
$marque_tx_ret = ( 100 * $marge ) / ( float ) $pu_ht_remise ;
2021-02-26 17:26:44 +00:00
}
2012-11-03 15:32:49 +00:00
2024-01-14 11:26:37 +00:00
return array ( $pa_ht_ret , $marge_tx_ret , $marque_tx_ret );
2012-07-18 13:52:15 +00:00
}