Heray-Was-Here
Server : Apache
System : Linux mail.lomejor.cr 6.8.0-1059-azure #65~22.04.1-Ubuntu SMP Thu May 28 16:59:19 UTC 2026 x86_64
User : www-data ( 33)
PHP Version : 8.2.31
Disable Function : NONE
Directory :  /var/www/dev/htdocs/custom/sydinitprocess/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /var/www/dev/htdocs/custom/sydinitprocess/commtable_print.php
<?php
/* Copyright (C) 2012-2013	Christophe Battarel	<christophe.battarel@altairis.fr>
 * Copyright (C) 2014		Ferran Marcet		<fmarcet@2byte.es>
 * Copyright (C) 2015       Marcos García       <marcosgdf@gmail.com>
 *
 * 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
 * along with this program. If not, see <https://www.gnu.org/licenses/>.
 */

/**
 *	\file       htdocs/margin/agentMargins.php
 *	\ingroup    margin
 *	\brief      Page des marges par agent commercial
 */
if (false === (@include '../../main.inc.php')) {  // From htdocs directory
	require '../../../main.inc.php'; // From "custom" directory
}

require_once DOL_DOCUMENT_ROOT.'/core/lib/company.lib.php';
require_once DOL_DOCUMENT_ROOT.'/compta/facture/class/facture.class.php';
require_once DOL_DOCUMENT_ROOT.'/product/class/product.class.php';
require_once DOL_DOCUMENT_ROOT.'/margin/lib/margins.lib.php';

// Load translation files required by the page
$langs->loadLangs(array('companies', 'bills', 'products', 'margins'));

$mesg = '';

if(GETPOST('tipo')=='excel'){
	header("Content-type: application/ms-excel");
	header("Content-disposition: attachment; filename=commision".date('dmYHms').".xls");
}

if ($user->rights->margins->read->all) {
	$agentid = GETPOST('agentid', 'int');
} else {
	$agentid = $user->id;
}
if (	!$user->rights->commande->creer  )
	$result = restrictedArea($user, 'margin');


/*
 * Actions
 */

// None



/*
 * View
 */

$userstatic = new User($db);
$companystatic = new Societe($db);
$invoicestatic = new Facture($db);

//print load_fiche_titre($text);

// Get parameters
$startdate = GETPOST('startdate');
$enddate = GETPOST('enddate');
$agentid = GETPOST('agentid');

$invoice_status_except_list = array(Facture::STATUS_DRAFT, Facture::STATUS_ABANDONED);

//Definicion de paramentros para calculo/
$sql = "SELECT description, rango_inicial as ri, rango_final as rf, porcentaje as porc ";
$sql .= "FROM ".MAIN_DB_PREFIX."user_comision ";
$sql .= "ORDER by rango_inicial, rango_final ";

$result = $db->query($sql);

if ($result) {
	$num = $db->num_rows($result);
	$i =0;
	while ($objp = $db->fetch_object($result)) {
		$rangetable .=   '</tr>';
		$rangetable .=   '<tr><td class="titlefield">'.$objp->description.'</td>';
		$rangetable .=   '<td class="titlefield">'.$objp->ri.'</td>';
		$rangetable .=   '<td class="titlefield">'.$objp->rf.'</td>';
		$rangetable .=   '<td class="titlefield">'.$objp->porc.'%</td>';
		$rangetable .=   '</tr>';
		if ($i == 0 ){
			$range = "if (selling_porc between " . $objp->ri  . " and " . $objp->rf  . ",  selling_price * " . ( $objp->porc /100 ) . ", __SUBLEVEL___ ) ";
			$label_range = "if (selling_porc between " . $objp->ri  . " and " . $objp->rf  . ",  " . ( $objp->porc /100 ) . ", __SUBLEVEL___ ) ";
		} else{
			$tmprange = "if (selling_porc between " . $objp->ri  . " and " . $objp->rf  . ",  selling_price * " .  ( $objp->porc /100 )  . ", __SUBLEVEL___ ) ";
			$label_tmprange = "if (selling_porc between " . $objp->ri  . " and " . $objp->rf  . ",  " .  ( $objp->porc /100 )  . ", __SUBLEVEL___ ) ";
			$range =  str_replace(' __SUBLEVEL___', $tmprange, $range);
			$label_range =  str_replace(' __SUBLEVEL___', $label_tmprange, $label_range);
		}
		$i++;
	}
}
$range =  str_replace(', __SUBLEVEL___', ', 0', $range);
$label_range =  str_replace(', __SUBLEVEL___', ', 0', $label_range);


$sql = " SELECT rowid, ref, datef, socid, name, code_client, client, agent, login, lastname, firstname, selling_price, buying_price, ";
$sql .= " selling_porc, ". $label_range ." as porc ,". $range ." as comm, rec_comision, ind_comision, cuota_ventas";
$sql .= " FROM (";

$sql .= "SELECT";
$sql .= " d.rowid, f.ref,f.datef, s.rowid as socid, s.nom as name, s.code_client, s.client,";
$sql .= " u.rowid as agent, u.login, u.lastname, u.firstname,";
$sql .= " IF(pe.ind_comision=1,0,d.total_ht) as selling_price,";
// Note: qty and buy_price_ht is always positive (if not your database may be corrupted, you can update this)
$sql .= " IF(pe.ind_comision=1,0, (".$db->ifsql('d.total_ht < 0', 'd.qty * d.buy_price_ht * -1 * (d.situation_percent / 100)', 'd.qty * d.buy_price_ht * (d.situation_percent / 100)').")) as buying_price,";
$sql .= " IF(pe.ind_comision=1,0, (( ((d.total_ht) /  (".$db->ifsql('d.total_ht < 0', 'd.qty * d.buy_price_ht * -1 * (d.situation_percent / 100)', 'd.qty * d.buy_price_ht * (d.situation_percent / 100)').") ) -1 ) *100)) as selling_porc";
$sql .= ", ue.rec_comision, ue.ind_comision, ue.cuota_ventas";
$sql .= " FROM ".MAIN_DB_PREFIX."societe as s";
$sql .= ", ".MAIN_DB_PREFIX."facture as f";
$sql .= " LEFT JOIN ".MAIN_DB_PREFIX."element_contact e ON e.element_id = f.rowid and e.statut = 4 and e.fk_c_type_contact = ".(empty($conf->global->AGENT_CONTACT_TYPE) ? -1 : $conf->global->AGENT_CONTACT_TYPE);
$sql .= " INNER JOIN ".MAIN_DB_PREFIX."facture_extrafields fe ON fe.fk_object = f.rowid ";
$sql .= ", ".MAIN_DB_PREFIX."facturedet as d";
$sql .= ", ".MAIN_DB_PREFIX."user as u";
$sql .= ", ".MAIN_DB_PREFIX."user_extrafields as ue";
$sql .= ", ".MAIN_DB_PREFIX."product_extrafields as pe";
$sql .= " WHERE f.fk_soc = s.rowid";
$sql .= ' AND f.entity IN ('.getEntity('invoice').')';
$sql .= ' AND (d.fk_product = pe.fk_object ) ';
$sql .= " AND (d.product_type = 0 OR d.product_type = 1)";

if (!empty($conf->global->AGENT_CONTACT_TYPE)) {
	$sql .= " AND ((e.fk_socpeople IS NULL) OR (e.fk_socpeople IS NOT NULL AND e.fk_socpeople = u.rowid))";
} else {
	$sql .= " AND u.rowid = NVL((Select fk_user FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = f.fk_soc and fe.idvendedor = sc.fk_user ),
					NVL((Select fk_user FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc WHERE sc.fk_soc = f.fk_soc order by sc.rowid limit 1), fe.idvendedor) )";
}
$sql .= " AND f.fk_statut NOT IN (".$db->sanitize(implode(', ', $invoice_status_except_list)).") ";
$sql .= ' AND s.entity IN ('.getEntity('societe').')';
$sql .= " AND d.fk_facture = f.rowid";
$sql .= " AND ue.fk_object = u.rowid AND ue.rec_comision != 0 ";
if ($agentid > 0) {
	if (!empty($conf->global->AGENT_CONTACT_TYPE)) {
		$sql .= " AND ((e.fk_socpeople IS NULL AND  u.rowid = ".((int) $agentid).") OR (e.fk_socpeople IS NOT NULL AND e.fk_socpeople = ".((int) $agentid)."))";
	} else {
		$sql .= " AND  u.rowid = ".((int) $agentid);
	}
}
if (!empty($startdate)) {
	$sql .= " AND f.datef >= '".$db->idate($startdate)."'";
}
if (!empty($enddate)) {
	$sql .= " AND f.datef <= '".$db->idate($enddate)."'";
}
$sql .= " AND d.buy_price_ht IS NOT NULL";

// We should not use this here. Option ForceBuyingPriceIfNull should have effect only when inserting data. Once data is recorded, it must be used as it is for report.
// We keep it with value ForceBuyingPriceIfNull = 2 for retroactive effect but results are unpredicable.
if (isset($conf->global->ForceBuyingPriceIfNull) && $conf->global->ForceBuyingPriceIfNull == 2) {
	$sql .= " AND d.buy_price_ht <> 0";
}
//if ($agentid > 0) $sql.= " GROUP BY s.rowid, s.nom, s.code_client, s.client, u.rowid, u.login, u.lastname, u.firstname";
//else $sql.= " GROUP BY u.rowid, u.login, u.lastname, u.firstname";
//$sql .= " GROUP BY  f.ref, s.rowid, s.nom, s.code_client, s.client, u.rowid, u.login, u.lastname, u.firstname ";
$sql .= $db->order($sortfield, $sortorder) .") base ORDER BY datef, agent, socid, name, code_client, client,  login, lastname, firstname  ";
// TODO: calculate total to display then restore pagination
//$sql.= $db->plimit($conf->liste_limit +1, $offset);

$result = $db->query($sql);

if ($result) {
	$num = $db->num_rows($result);

	print '<br>';

	if ($conf->global->MARGIN_TYPE == "1") {
		$labelcostprice = 'BuyingPrice';
	} else { // value is 'costprice' or 'pmp'
		$labelcostprice = 'CostPrice';
	}

	$moreforfilter = '';

	$i = 0;
	print '<div class="div-table-responsive">';
	print '<table class="tagtable liste'.($moreforfilter ? " listwithfilterbefore" : "").'">'."\n";

	print '<tr class="liste_titre">
	<td style="width: 10%">Documento</td>
	<td style="width: 35%">Fecha Documento</td>
	<td style="width: 11%; text-align:right">Cliente</td>
	<td style="width: 11%;text-align:right">Nombre Cliente</td>
	<td style="width: 11%;text-align:right">Vendedor</td>
	<td style="width: 11%; text-align:right">Nombre Vendedor</td>
	<td style="width: 11%; text-align:right">Precio de Venta</td>
	<td style="width: 11%;text-align:right">Precio Compra</td>
	<td style="width: 11%;text-align:right">Margen</td>
	<td style="width: 11%; text-align:right">Porc Correspondiente</td>
	<td style="width: 11%; text-align:right">Comision Calculada</td>
	<td style="width: 11%; text-align:right">Producto</td>
	<td style="width: 11%; text-align:right">Descripcion</td>
	</tr>';

	if ($num > 0) {
		$group_list = array();
    	$ind_comision =0;
		while ($objp = $db->fetch_object($result)) {
			
			$group_id = $objp->ref;
			$group_list[$group_id] = array('name' => $objp->ref, 'htmlname' => $objp->ref, 'ind_comision' => 0, 'selling_price' => 0, 'buying_price' => 0, 'selling_porc' => 0, 'porc' => 0, 'comm' => 0, 'count' => 0);
			

			$seller_nb = 1;
			if ($objp->socid > 0) {
				// sql nb sellers
				$sql_seller  = "SELECT COUNT(sc.rowid) as nb";
				$sql_seller .= " FROM ".MAIN_DB_PREFIX."societe_commerciaux as sc";
				$sql_seller .= " WHERE sc.fk_soc = ".((int) $objp->socid) ;
				$sql_seller .= " LIMIT 1";

				$resql_seller = $db->query($sql_seller);
				if (!$resql_seller) {
					dol_print_error($db);
				} else {
					if ($obj_seller = $db->fetch_object($resql_seller)) {
						if ($obj_seller->nb > 0) {
							$seller_nb = $obj_seller->nb;
						}
					}
				}
			}

			print '<tr class="liste_titre">
			<td style="width: 10%">'.$objp->ref.'</td>
			<td style="width: 35%">'.$objp->datef.'</td>
			<td style="width: 11%">'.$objp->code_client.'</td>
			<td style="width: 11%">'.$objp->name.'</td>
			<td style="width: 11%">'.$objp->agent.'</td>
			<td style="width: 11%">'.$objp->firstname .' '. $objp->lastname.'</td>
			<td style="width: 11%">'.($objp->selling_price / $seller_nb ).'</td>
			<td style="width: 11%">'.($objp->buying_price / $seller_nb ).'</td>
			<td style="width: 11%">'.($objp->selling_porc / $seller_nb ).'</td>
			<td style="width: 11%">'.($objp->porc / $seller_nb ).'</td>
			<td style="width: 11%">'.($objp->comm / $seller_nb ).'</td>
			<td style="width: 11%">'.($objp->fk_product ).'</td>
			<td style="width: 11%">'.str_replace(array("\r", "\n"), '', $objp->description).'</td>
			</tr>';

		}
	}


} else {
	dol_print_error($db);
}
$db->free($result);

$db->close();

Hry