/*************************************************************
* tot_venduto_singolo_agente.php
* Calcola il venduto per un singolo agente.
* Non si fanno visualizzare i nome dei clienti/fornitori altrimenti
* la pagina scorrerebbe troppo in basso.
**************************************************************/
include ("../common/public.php") ;
// print_r($_GET + $_POST) ; exit ;
$where_age = "
O.idagente = $id and
AFP.idagente = $id and
A.idagenti = $id
" ;
$where_cli = "1" ;
$where_for = "1" ;
if ($from == "totali_cli")
{
$sei_in = "AGENTI >> Totali >> Da Agente/i al Cliente/i" ;
list($id, $nome_agente) = split("\^", $agente) ;
$strIdCli = substr($hidcli, 0, -1) ;
$arrIdCli = explode(",", $strIdCli) ;
$strNomeCli = substr($hnomecli, 0, -1) ;
$strNomeCli = str_replace(",", " / ", $strNomeCli) ;
if (in_array(0, $arrIdCli))
$where_cli = "1" ;
else
$where_cli = "C.idclienti IN ($strIdCli)" ;
if ($id == 0)
{
$single_agente = false ;
// $titolo = "Totali di Tutti gli agenti verso il/i cliente/i $strNomeCli" ;
$titolo = "Totali di Tutti gli agenti verso il/i cliente/i " ;
$where_age = "A.idagenti = O.idagente and
AFP.idagente = O.idagente" ;
}
else
{
$single_agente = true ;
// $titolo = "Totali dell'agente $nome_agente verso il/i cliente/i $strNomeCli" ;
$titolo = "Totali dell'agente $nome_agente verso il/i cliente/i" ;
$sei_in = "AGENTI >> Totali vs Cliente/i" ;
$where_age = "
O.idagente = $id and
AFP.idagente = $id and
A.idagenti = $id
" ;
}
}
elseif ($from == "totali_for")
{
$sei_in = "AGENTI >> Totali >> Da Agente/i al Fornitore/i" ;
list($id, $nome_agente) = split("\^", $agente) ;
$strIdFor = substr($hidfor, 0, -1) ;
$arrIdFor = explode(",", $strIdFor) ;
$strNomeFor = substr($hnomefor, 0, -1) ;
$strNomeFor = str_replace(",", " / ", $strNomeFor) ;
if (in_array(0, $arrIdFor))
$where_for = "1" ;
else
$where_for = "F.idfornitori IN ($strIdFor)" ;
if ($id == 0)
{
$single_agente = false ;
// $titolo = "Totali di Tutti gli agenti verso il/i fornitore/i $strNomeFor" ;
$titolo = "Totali di Tutti gli agenti verso il/i fornitore/i" ;
$where_age = "A.idagenti = O.idagente and
AFP.idagente = O.idagente" ;
}
else
{
$single_agente = true ;
// $titolo = "Totali dell'agente $nome_agente verso il/i fornitore/i $strNomeFor" ;
$titolo = "Totali dell'agente $nome_agente verso il/i fornitore/i" ;
$where_age = "
O.idagente = $id and
AFP.idagente = $id and
A.idagenti = $id
" ;
}
}
elseif ($from == "ordini_per_cliente_per_mese")
{
$sei_in = "AGENTI >> Totali >> Singolo mese per Agente" ;
$single_agente = false ;
$meseToVis = $arrMesi[$mese] ;
$titolo = "Totali dell'agente $nome_agente per il mese di $meseToVis" ;
$where_cli = "month(O.dataordine) = $mese" ;
$where_for = "1" ;
}
// exit ;
debug (0, "id = $id") ;
$dbt = new FC_SQL ;
$arrVal = array() ;
$TotVen = 0 ;
$TotVenProvv = 0 ;
$TotFatt = 0 ;
$TotFattProvv = 0 ;
$nOrdFatt = 0 ;
$str = "" ;
/*************************************************************************************
* sembra che speficicando "limit" nella query si velocizza di parecchio il tutto
************************************************************************************/
$query = "
select
*,
F.nome as NomeFornitore,
C.nome as NomeCliente,
A.nome as NomeAgente,
case
when (AFP.percentuale <> 0) then AFP.percentuale
else A.percentuale
end as Percentuale
from
$tordini O
inner join $tfornitori F on F.idfornitori = O.idfornitore
inner join $tageforper AFP on AFP.idfornitore = F.idfornitori
inner join $tclienti C on C.idclienti = O.idcliente,
$tagenti A
where
$where_age and
$where_cli and
$where_for
order by
O.idordini
limit 0,2000
" ;
debug (2, $query) ;
list($n, $d) = $db->RecuperaDati($query) ;
for ($i=0; $i<$n; $i++)
{
$idordine = $d['IdOrdini'][$i] ;
$idfor = $d['IdFornitore'][$i] ;
$importoNC = $d['ImportoNC'][$i] ;
if ($importoNC > 0)
$valNC = ($importoNC / 1.2) ;
else
$valNC = 0 ;
if ($idfor != $idsarzi)
$qdett = "
select
*
from
$tdettagli,
$tprodotti
where
$tdettagli.idordine = $idordine and
$tprodotti.idprodotti = $tdettagli.idprodotto
" ;
else
$qdett = "
select
*
from
$tdettagli,
$tsarzi
where
$tdettagli.idordine = $idordine and
$tdettagli.idprodotto = CONCAT('SA',idprodotti)
" ;
$dbt->query($qdett) ;
$importo = 0 ;
$ammprov = 0 ;
$subsconti = 0 ;
$subtot = 0 ;
$subtotiva = 0 ;
while ($dbt->next_record())
{
$impparz = $dbt->f("NumCartoni") * $dbt->f("ConfezioniDa") * $dbt->f("PrezzoUnitario") ;
if ($dbt->f("ScontoPrd") != "" )
$impparz = CalcolaScontoProdotto($impparz, $dbt->f("ScontoPrd")) ;
$importo += $impparz ;
}
/********************************************************************
* Per calcolare la percentuale di provvigione, devo
* considerare, per questo ordine, se ci sono sconti
* esattamente lo sconto1 e sconto2
* Invece per l'ammontare dell'ordine, mi comporto
* come nel file dettagliordini.php
*************************************************/
/* Calcolo provvigione
*************************************************/
$sconto1 = ($importo * $d['Sconto1'][$i] / 100) ;
$subtotp1 = $importo - $sconto1 ;
$sconto2 = ($subtotp1 * $d['Sconto2'][$i] / 100) ;
$sconti = $sconto1 + $sconto2 ;
$baseprov = $importo - $sconti - $valNC ;
$percentuale = $d['Percentuale'][$i] ;
$ammprov += ($baseprov * $percentuale / 100 ) ;
/***********************************************************
* Il valore da visualizzare č l'imponibile provvigionale cioč
* l'ammontare da cui calcolare la provvigione
***********************************************************/
$subtot += $baseprov ;
$TotVen += $subtot ;
$TotVenProvv += $ammprov ;
if ($d['ImportoFattura'][$i] <> 0)
{
$nOrdFatt ++ ;
$TotFatt += $subtot ;
$TotFattProvv += $ammprov ;
}
$str .= "" ;
$str .= "| " . $d['IdOrdini'][$i] . " | " ;
$str .= "" . $d['NumFattura'][$i] . " | " ;
$str .= "" . htmlspecialchars($d['NomeAgente'][$i]) . " | " ;
$str .= "" . htmlspecialchars($d['NomeCliente'][$i]) . " | " ;
$str .= "" . htmlspecialchars($d['NomeFornitore'][$i]) . " | " ;
$str .= "" . nfe($subtot) . " | " ;
$str .= "" . ItaDate($d['DataOrdine'][$i]) . " | " ;
$str .= "" . ItaDate($d['DataSpedFattura'][$i]) . " | " ;
$str .= "" . nfp($percentuale) . " | " ;
$str .= "" . nfp($ammprov) . " | " ;
$str .= "
" ;
}
$str .= "" ;
$header = "N.ORD,N.FATT,AGENTE,CLIENTE,FORNITORE,IMPON PROVV,DATA ORDINE,DATA FATTURA,% PROVV,PROVV" ;
if ($single_agente)
{
$footer = "TOTALE VENDUTO:-#cspan-#cspan-#cspan-" . nfe($TotVen) . "-----" . nfe($TotVenProvv) ;
$footer2 = "TOTALE FATTURATO:-#cspan-#cspan-#cspan-" . nfe($TotFatt) . "-----" . nfe($TotFattProvv) ;
}
else
{
$footer = "TOTALE VENDUTO:-#cspan-#cspan-#cspan-#cspan-" . nfe($TotVen) . "----" . nfe($TotVenProvv) ;
$footer2 = "TOTALE FATTURATO:-#cspan-#cspan-#cspan-#cspan-" . nfe($TotFatt) . "----" . nfe($TotFattProvv) ;
}
$strSort = "int,int,str,str,str,euro,date,date,int,euro" ;
$iniWP = "5,6,10,13,22,11,11,12,8,10" ;
$setColType = "ro,ro,ro,ro,ro,ro,ro,ro,ro,ro" ;
$setColAlign = "center,center,left,left,left,right,center,center,center,right" ;
?>
echo TITLE ?>
if ($from == "totali_cli" or $from == "totali_for" or $from == "ordini_per_cliente_per_mese") : ?>
echo $titolo ?>
endif ?>
Numero Ordini: echo $n ?>;
Numero Ordini con Fattura: echo $nOrdFatt ?>
(per selezionare un ordine clicca sulla riga corrispondente)