$giorno $mese per Tutti gli agenti verso Tutti i fornitori" ; } else { $where_for = "O.idfornitore = $idforni" ; $titolo = "Confronto Anno ". sprintf("%s", ($anno - 1)) . " - Anno $anno al $giorno $mese per Tutti gli agenti verso il fornitore $nome_fornitore" ; } } else { $where_ag = "A.idagenti = $idag" ; if ($idforni == 0) { $where_for = "1" ; $titolo = "Confronto Anno ". sprintf("%s", ($anno - 1)) . " - Anno $anno al $giorno $mese per l'agente $nome_agente verso Tutti i fornitori" ; } else { $where_for = "O.idfornitore = $idforni" ; $titolo = "Confronto Anno ". sprintf("%s", ($anno - 1)) . " - Anno $anno al $giorno $mese per l'agente $nome_agente verso il fornitore $nome_fornitore" ; } } $arrAgenti = array() ; $arrFornitori = array() ; $arrVal = array() ; $arrValAllFor = array() ; $annop = ($anno - 1) ; $hdr_agente = "Agente" ; /*********************** * partiamo dai totali ***********************/ for ($an = $anno ; $an >= $anno - 1; $an --) { $dataordine = MySqlDate("$gime/$an") ; $query = " select A.idagenti, A.nome, count(O.idordini) as tot from agenti A left join ordini O on O.idagente = A.idagenti and O.Anno = $an and $where_for where O.dataordine <= '$dataordine' and O.dataordine != '0000/00/00' and $where_ag group by A.idagenti, A.nome order by A.nome " ; debug (0, $query) ; list($n, $d) = $db->RecuperaDati($query) ; for ($i=0; $i<$n; $i++) { $idagente = $d['idagenti'][$i] ; $nomeagente = $d['nome'][$i] ; if (! array_key_exists($idagente, $arrAgenti)) { $arrAgenti[$idagente] = $nomeagente ; $arrVal[$idagente]['NUMORDINI'][$anno] = 0 ; $arrVal[$idagente]['NUMORDINI'][($anno - 1)] = 0 ; $arrVal[$idagente]['VENDUTO'][$anno] = 0 ; $arrVal[$idagente]['VENDUTO'][($anno - 1)] = 0 ; $arrVal[$idagente]['FATTURATO'][$anno] = 0 ; $arrVal[$idagente]['FATTURATO'][($anno - 1)] = 0 ; } $arrVal[$idagente]['NUMORDINI'][$an] = $d['tot'][$i] ; } } /********************** * calcoliamo i totali **********************/ for ($an = $anno ; $an >= $anno - 1; $an --) { $dataordine = MySqlDate("$gime/$an") ; $dataspedfatt = str_replace("/", "-", $dataordine) ; $dbt = new FC_SQL ; $dbord = new FC_SQL ; @reset($arrAgenti) ; foreach($arrAgenti as $id_agente => $nome_agente) { $totforn = 0 ; $totfornFatt = 0 ; $totproforn = 0 ; $qord = " select O.*, F.nome as NomeFornitore, case when (O.ImportoNC > 0) then (O.ImportoNC / 1.2) else 0 end as valNC from ordini O inner join fornitori F on F.IdFornitori = O.IdFornitore where O.Anno = $an and O.idagente = $id_agente and $where_for and O.dataordine <= '$dataordine' " ; list($n, $d) = $dbord->RecuperaDati($qord) ; for ($i=0; $i<$n; $i++) { $idfornitore = $d['IdFornitore'][$i] ; $NomeFornitore = $d['NomeFornitore'][$i] ; debug (0, $idfornitore) ; if (!array_key_exists($idfornitore, $arrValAllFor)) { $arrFornitori[$idfornitore] = $NomeFornitore ; $arrValAllFor[$idfornitore] = array('NUMORDINI' => array($anno => 0, $annop => 0 ) ) ; } $arrValAllFor[$idfornitore]['NUMORDINI'][$an] ++ ; $totordine = 0 ; $sco1 = $d['Sconto1'][$i] ; $sco2 = $d['Sconto2'][$i] ; $sco3 = $d['Sconto3'][$i] ; $idordine = $d['IdOrdini'][$i] ; $valNC = $d['valNC'][$i] ; $idfor = $d['IdFornitore'][$i] ; $impFatt = $d['ImportoFattura'][$i] ; $dataSpedFatt = $d['DataSpedFattura'][$i] ; if ($idfor != $idsarzi) $query = " select * from dettagli D, prodotti P where P.Anno = $an and D.Anno = $an and D.idordine = $idordine and P.idprodotti = D.idprodotto " ; else $query = "select * from dettagli_$an, sarzi_$an where dettagli_$an.idordine = $idordine and dettagli_$an.idprodotto = CONCAT('SA', idprodotti)" ; list($nn, $dd) = $dbt->RecuperaDati($query) ; for ($ii=0; $ii<$nn; $ii++) { $prz = ($dd['PrezzoForzato'][$ii] > 0 ? $dd['PrezzoForzato'][$ii] : $dd['PrezzoUnitario'][$ii]) ; $impparz = $dd['NumCartoni'][$ii] * $dd['ConfezioniDa'][$ii] * $prz ; if ($dd['ScontoPrd'][$ii] != "") $impparz = CalcolaScontoProdotto($impparz, $dd['ScontoPrd'][$ii]) ; $totordine += $impparz ; } $sconto1 = ($totordine * $sco1 / 100) ; $ammpro1 = $totordine - $sconto1 ; $sconto2 = ($ammpro1 * $sco2 / 100) ; $ammpro2 = $ammpro1 - $sconto2 - $valNC; /**************************************************************************************************************** * Calcolo a questo punto l'ammontare delle provvigioni per questo ordine dopo aver sottratto dall'imponibile gli * eventuali 2 sconti forniti dal fornitore e l'eventuale ammontare della nota di credito. Idem per il fatturato *****************************************************************************************************************/ $provvigione = $arrProvv[$an][$idfor][$id_agente] ; $totproordine = ($ammpro2 * $provvigione / 100) ; $sconto = $sconto1 + $sconto2 ; $totordine = $totordine - $sconto - $valNC; $totforn += $totordine ; $totproforn += $totproordine ; if ($impFatt > 0 and $dataSpedFatt <= $dataspedfatt) $totfornFatt += $totordine ; $arrValAllFor[$idfornitore]['VENDUTO'][$an] += $totordine ; if ($impFatt > 0 and $dataSpedFatt <= $dataspedfatt) $arrValAllFor[$idfornitore]['FATTURATO'][$an] += $totordine ; } $arrVal[$id_agente]['FATTURATO'][$an] += $totfornFatt ; $arrVal[$id_agente]['VENDUTO'][$an] += $totforn ; } } if ($idag != 0 and $idforni == 0) { asort($arrFornitori) ; $arrAgenti = $arrFornitori ; $arrVal = $arrValAllFor ; $hdr_agente = "Fornitore" ; } $str = "" ; @reset($arrAgenti) ; foreach($arrAgenti as $id => $nome) { $bOrdiniTrovati = true ; $no_annop = $arrVal[$id]['NUMORDINI'][$annop] ; $no_anno = $arrVal[$id]['NUMORDINI'][$anno] ; $v_annop = $arrVal[$id]['VENDUTO'][$annop] ; $v_anno = $arrVal[$id]['VENDUTO'][$anno] ; $f_annop = $arrVal[$id]['FATTURATO'][$annop] ; $f_anno = $arrVal[$id]['FATTURATO'][$anno] ; if ($no_annop == 0 or $no_anno == 0) $diff_no_perc = @nfp(100) ; else $diff_no_perc = @nfp((($no_anno - $no_annop) / $no_annop * 100)) ; if ($v_annop == 0 or $v_anno == 0) $diff_v_perc = @nfp(100) ; else $diff_v_perc = @nfp((($v_anno - $v_annop) / $v_annop * 100)) ; if ($f_annop == 0 or $f_anno == 0) $diff_f_perc = @nfp(100) ; else $diff_f_perc = @nfp((($f_anno - $f_annop) / $f_annop * 100)) ; if (($no_anno - $no_annop) < 0) $classno = "meno" ; else $classno = "piu" ; if (($v_anno - $v_annop) < 0) $classv = "meno" ; else $classv = "piu" ; if (($f_anno - $f_annop) < 0) $classf = "meno" ; else $classf = "piu" ; $str .= "" ; $str .= "". htmlspecialchars($nome) . "" ; $str .= "$no_annop" ; $str .= "$no_anno" ; $str .= "" . ($arrVal[$id]['NUMORDINI'][$anno] - $arrVal[$id]['NUMORDINI'][$annop]) . " ($diff_no_perc %) " ; $str .= "" . nfp($v_annop) ."" ; $str .= "" . nfp($v_anno) ."" ; $str .= "" . nfp($arrVal[$id]['VENDUTO'][$anno] - $arrVal[$id]['VENDUTO'][$annop]) . " ($diff_v_perc %) " ; $str .= "" . nfp($f_annop) ."" ; $str .= "" . nfp($f_anno) ."" ; $str .= "" . nfp($arrVal[$id]['FATTURATO'][$anno] - $arrVal[$id]['FATTURATO'][$annop]) . " ($diff_f_perc %) " ; $str .= "" ; } $str .= "" ; $header = "$hdr_agente,NUMERO ORDINI,#cspan,#cspan,VENDUTO,#cspan,#cspan,FATTURATO,#cspan,#cspan" ; $attachHeader = "#rspan,$annop,$anno,Differenza,$annop,$anno,Differenza,$annop,$anno,Differenza" ; $strSort = "str,int,int,na,euro,euro,na,euro,euro,na" ; $iniWP = "19,5,5,10,7,7,14,8,8,14" ; $setColType = "ro,ro,ro,ro,ro,ro,ro,ro,ro,ro,ro,ro,ro,ro,ro,ro" ; $setColAlign = "left,right,right,right,right,right,right,right,right,right" ; ?> <? echo TITLE ?>
-- Per la data scelta, non ci sono ordini da confrontare