<?php

include "valoda.php";
$sugas = gAtskaite::getSugas();
$braki = gAtskaite::getBrakiCipariem();
$LVM_braki = gAtskaite::getLVMBraki();
$IT_braki = gAtskaite::getITBraki();
$VIKA_braki = gAtskaite::getVIKABraki();
$VIKA_LVM_braki = gAtskaite::getVIKALVMBrakiAkrs();
$LVM_sugas = gAtskaite::getLVMSugas();
class C_REPORT extends C_REPORT_BASE
{
    function C_REPORT(&$My_POST, &$DB, $XML_Variants, $grup)
    {
        global $braki;
        global $VIKA_braki;
        global $LVM_braki;
        global $VIKA_LVM_braki;
        global $sugas;
        global $lang;
        global $translate;
        if ($lang != "RUS") {
            $lang = "LAT";
        }
        $this->DB = $DB;
        $this->XML_Variants = $XML_Variants;
        $this->sugas = $sugas;
        $this->MyPOST = $My_POST;
        $this->grup = $grup;
        if (strrpos($this->MyPOST['pavadzime'], "KRVII") === false) {
            $this->braki = $braki;
 function print_level($query, &$summa_p, $irbrakis_p, $suga, $id)
 {
     global $lang;
     global $translate;
     $select = "";
     $orderby = "ORDER BY ";
     $criteria = array();
     for ($level = 1; $level <= $this->lsk; $level++) {
         // tekošā grupēšanas līmeņa lauks
         $lauks = $this->MyPOST["gtype" . $this->grup . $level];
         if ($lauks == 'garums' || $lauks == 'mind_pirms_red') {
             $this->build_value_ranges($level, $suga);
             if ($lauks == 'garums') {
                 $this->gar_allowed_values = $this->allowed_values;
             } elseif ($lauks == 'mind_pirms_red') {
                 $this->diam_allowed_values = $this->allowed_values;
             }
         }
         if ($lauks == 'fsc') {
             $this->allowed_values = array();
             $this->allowed_values[0] = '0';
             $this->allowed_values[1] = '1';
             $typ = 'string';
         }
         if ($lauks == 'kad_piegad' || $lauks == 'auto' || $lauks == 'brakis' || $lauks == 'cenu_matrica' || $lauks == 'cirsmas_kods' || $lauks == 'kad_piegad' || $lauks == 'kravas_id' || $lauks == 'pavadzime.pavadzime' || $lauks == 'piegad_kods' || $lauks == 'soferis' || $lauks == 'skira' || $lauks == 'suga' || $lauks == 'iecirknis') {
             //no visiem atlas?tajiem ba??iem pavadz?m? atlasa attiec?g? lauka atrodam?s v?rt?bas (pa vienai)
             $this->build_single_values($lauks, $query);
         }
         $row = 0;
         $summa_p->init();
         $myid = 0;
         while (list($key, $val) = each($this->allowed_values)) {
             $myid++;
             if ($lauks == 'suga') {
                 $suga = $val;
             }
             // virsm?ru sadal?jums
             if ($suga && $this->MyPOST['dalit_virsmeri' . $this->grup]) {
                 if ($this->MyPOST['virsmeri' . $this->grup . '_' . $suga] != '') {
                     $virsmeri = explode(',', $this->MyPOST['virsmeri' . $this->grup . '_' . $suga]);
                     for ($i = 0; $i < count($virsmeri); $i++) {
                         $virsmeri[$i] = $virsmeri[$i] / 100;
                     }
                 }
             } else {
                 if ($this->MyPOST['virsmeri' . $this->grup] != '') {
                     $virsmeri = explode(',', $this->MyPOST['virsmeri' . $this->grup]);
                     for ($i = 0; $i < count($virsmeri); $i++) {
                         $virsmeri[$i] = $virsmeri[$i] / 100;
                     }
                 }
             }
             if ($suga && $this->MyPOST['dalit_raukums' . $this->grup]) {
                 $raukums = raukums_2_array($this->MyPOST['raukums' . $this->grup . '_' . $suga]);
             } else {
                 $raukums = raukums_2_array($this->MyPOST['raukums' . $this->grup]);
             }
             if ($suga && $this->MyPOST['dalit_koeficients' . $this->grup]) {
                 $koeficients = $this->MyPOST['koeficients' . $this->grup . '_' . $suga];
             } else {
                 $koeficients = $this->MyPOST['koeficients' . $this->grup];
             }
             if ($typ != 'string') {
                 $mbounds = explode('-', $this->allowed_values[$key]);
             }
             if (count($mbounds) > 1) {
                 $low_range_brakis = 0;
                 $high_range_brakis = 0;
                 if ($mbounds[0] > 5) {
                     $low_range_brakis = $mbounds[0] + 5;
                 } else {
                     $low_range_brakis = $mbounds[0];
                 }
                 if ($mbounds[1] < 994) {
                     $high_range_brakis = $mbounds[1] + 5;
                 } else {
                     $high_range_brakis = $mbounds[1];
                 }
                 //pievienots 17.07.2007
                 if ($myid == 1) {
                     $select .= ", CASE \n";
                 }
                 if ($this->MyPOST["gtype" . $this->grup . $level] == 'mind_pirms_red') {
                     $select .= "WHEN ((balkis_temp.import_type <> 'zbm' AND mind_pirms_red BETWEEN {$mbounds['0']} AND {$mbounds['1']}) OR (balkis_temp.import_type = 'zbm' AND mind_pirms_red BETWEEN {$low_range_brakis} AND {$high_range_brakis})) THEN {$myid} \n";
                     if ($myid == sizeof($this->allowed_values)) {
                         $select .= "END AS diam_group";
                         $orderby .= "diam_group";
                         $criteria[] = "diam_group";
                         //
                     }
                     //$myquery = $query . " and  ((". $this->MyPOST["gtype".$this->grup.$level] . ">=" . $mbounds[0] ." and " . $this->MyPOST["gtype".$this->grup.$level] . "<=" . $mbounds[1] . " AND balkis_temp.import_type <> 'zbm') or (". $this->MyPOST["gtype".$this->grup.$level] . ">=" . $low_range_brakis ." and " . $this->MyPOST["gtype".$this->grup.$level] . "<=" . $high_range_brakis . " AND balkis_temp.import_type = 'zbm'))";
                 } else {
                     $select .= "WHEN (garums BETWEEN {$mbounds['0']} AND {$mbounds['1']}) THEN {$myid} \n";
                     if ($myid == sizeof($this->allowed_values)) {
                         $select .= "END AS gar_group";
                         $orderby .= "gar_group";
                         $criteria[] = "gar_group";
                         //$this->gar_allowed_values = $this->allowed_values;
                     }
                     //$myquery = $query . " and  (". $this->MyPOST["gtype".$this->grup.$level] . ">=" . $mbounds[0] ." and " . $this->MyPOST["gtype".$this->grup.$level] . "<=" . $mbounds[1] . ")";
                 }
             } else {
                 if ($myid == sizeof($this->allowed_values)) {
                     $orderby .= $this->MyPOST["gtype" . $this->grup . $level];
                     $criteria[] = $this->MyPOST["gtype" . $this->grup . $level];
                 }
             }
             if ($myid == sizeof($this->allowed_values) && $level < $this->lsk) {
                 $orderby .= ",";
             }
         }
     }
     $query__ = "select mind_pirms_red as DBruto, garums as LBruto, mind_pec_red as DPecReduc, gar_pec_red as LPecReduc, brakis, suga, pavadzime.cenu_matrica as cm" . $select . $query . $orderby;
     $r = mysql_query($query__);
     $same_group = false;
     $gostu_tabula = $this->MyPOST['gostu_tabula' . $this->grup];
     while ($m = mysql_fetch_array($r)) {
         $last = $m;
         $same_group = true;
         $criteria_changed = 6;
         for ($ci = $this->lsk - 1; $ci >= 0; $ci--) {
             $value = $criteria[$ci];
             if ($old_values[$value] != $m[$value]) {
                 $criteria_changed = $ci;
             }
             if ($old_values[$value] != $m[$value]) {
                 if ($same_group) {
                     $old_bak = $old_values;
                 }
                 $same_group = false;
                 $old_values[$value] = $m[$value];
             }
         }
         if (!$same_group && $old_bak) {
             for ($ci = $this->lsk - 2; $ci >= 0; $ci--) {
                 if (!$summa[$ci + 1]) {
                     $summa[$ci + 1] = new CSumma();
                 }
                 $summa[$ci + 1]->add_skaits($SSkaits);
                 $summa[$ci + 1]->add_skaits_bad($SSkaitsBad);
                 $summa[$ci + 1]->add_virsmers($SVVirsm);
                 $summa[$ci + 1]->add_redukcija_d($SVReducD);
                 $summa[$ci + 1]->add_redukcija_l($SVReducL);
                 $summa[$ci + 1]->add_brakis($SVBrakis);
                 $summa[$ci + 1]->add_neto($SVNeto);
                 $summa[$ci + 1]->add_bruto($SVBruto);
                 if ($criteria_changed <= $ci && !$this->XML_Variants) {
                     //fb($summa,'Summa');
                     $this->AddSumRowAtLevel($ci, $criteria, $summa, $old_bak);
                 }
             }
             $this->arr_index++;
             $this->arr[$this->arr_index] = array();
             foreach ($criteria as $key => $value) {
                 $this->arr[$this->arr_index][$key] = $this->kodi($value, $old_bak[$value]);
             }
             $sm = new CSumma();
             $sm->skaits = $SSkaits;
             $sm->skaits_bad = $SSkaitsBad;
             $sm->virsmers = $SVVirsm;
             $sm->redukcija_d = $SVReducD;
             $sm->redukcija_l = $SVReducL;
             $sm->brakis = $SVBrakis;
             $sm->neto = $SVNeto;
             $sm->bruto = $SVBruto;
             //fb($sm,'Sm');
             $this->arr[$this->arr_index][summa] = $sm;
             //echo $m['brakis'];
             $summa_p->add_skaits($SSkaits);
             $summa_p->add_skaits_bad($SSkaitsBad);
             $summa_p->add_virsmers($SVVirsm);
             $summa_p->add_redukcija_d($SVReducD);
             $summa_p->add_redukcija_l($SVReducL);
             $summa_p->add_brakis($SVBrakis);
             $summa_p->add_neto($SVNeto);
             $summa_p->add_bruto($SVBruto);
             /*NULL vertibu ievietosana*/
             $SVBruto = 0;
             $SVReducD = 0;
             $SVReducL = 0;
             $SVVirsm = 0;
             $SVNeto = 0;
             $SVBrakis = 0;
             $SSkaits = 0;
             $SSkaitsBad = 0;
         }
         $SSkaits++;
         /*Vai ir Vika Wood*/
         //		$min_virsmers = 0;
         if ($suga && $this->MyPOST['dalit_virsmers' . $this->grup]) {
             $min_virsmers = $this->MyPOST['virsmers' . $this->grup . '_' . $suga] / 100;
         } else {
             $min_virsmers = $this->MyPOST['virsmers' . $this->grup] / 100;
         }
         if ($this->MyPOST['metode'] == '1') {
         } else {
             if ($this->MyPOST['metode'] == '2') {
             } else {
                 if ($this->MyPOST['metode'] == '3') {
                 } else {
                     if ($this->MyPOST['metode'] == '4') {
                     } else {
                     }
                 }
             }
         }
         $tilp_Metode = $this->MyPOST['metode'];
         $gar_PirmsRed = 0;
         $gar_PecRed = 0;
         $diam_TievPirmsRed = 0;
         $diam_TievPecRed = 0;
         $diam_VidusPirmsRed = 0;
         $diam_VidusPecRed = 0;
         $diam_ResPirmsRed = 0;
         $diam_ResPecRed = 0;
         //---Tiek veikta pārbaude, pēc kādiem nosacījumiem noapaļot garumu pirms redukcijas, no kura tiek rēķināts Bruto tilpums!-------------------------------------------------------------------------
         $gar_PirmsRed = $m['LBruto'] / 100;
         if ($this->MyPOST['noapalot_garumu'] == '1') {
             $gar_PirmsRed = floor($m['LBruto'] / 10) / 10;
         } elseif ($this->MyPOST['noapalot_garumu'] == '2') {
             $gar_PirmsRed = (floor($m['LBruto'] / 10) + 0.5) / 10;
         }
         //---Tiek pārbaudīts, vai garums tiek ņemts kā nominālais garums, vai notiek nominālā garuma aprēķināšana, no kura tiks rēķināts Neto tilpums!-------------------------------------------------------------------------
         $gar_PecRed = gAtskaite::nom($gar_PirmsRed, $virsmeri, $min_virsmers);
         if ($this->MyPOST['is_vika'] == 'on') {
             $gar_PecRed = $m['LPecReduc'] / 100;
         }
         //---Tiek aprēķināts nominālais garums pirms pirms redukcijas, lai varētu aprēķināt tilpumu bez virsmēra!-------------------------------------------------------------------------
         $gar_NomPirsmRed = gAtskaite::nom($gar_PirmsRed, $virsmeri, $min_virsmers);
         //----------------------------------------------------------------------------
         $diam_TievPirmsRed = $m['DBruto'] / 1000;
         $diam_VidusPirmsRed = $m['DBruto'] / 1000;
         $diam_ResPirmsRed = $m['DBruto'] / 1000;
         $rauk_koef = get_raukums_no_diam($raukums, $diam_TievPirmsRed * 1000);
         if ($this->MyPOST['noapalot_diametru'] == 'on') {
             $diam_TievPirmsRed = (floor($m['DBruto'] / 10) + 0.5) / 100;
             $diam_VidusPirmsRed = (floor($m['DBruto'] / 10) + 0.5) / 100;
             $diam_ResPirmsRed = (floor($m['DBruto'] / 10) + 0.5) / 100;
         }
         //----------------------------------------------------------------------------
         if ($this->MyPOST['braka_virsmers'] == 'on') {
         } else {
         }
         $DPecReduc = $m['DPecReduc'] / 1000;
         $DReduc = $DBruto - $DPecReduc;
         if ($m[brakis] != '255') {
             $irbrakis = true;
             $brkods = 'standart';
         }
         $tilpums_bruto = f2($diam_TievPirmsRed, $gar_PirmsRed, $rauk_koef, $koeficients, $gostu_tabula);
         $tilpums_neto = f2($DPecReduc, $gar_PecRed, $rauk_koef, $koeficients, $gostu_tabula);
         $tilpums_bruto_bez_virsmera = f2($diam_TievPirmsRed, $gar_NomPirsmRed, $rauk_koef, $koeficients, $gostu_tabula);
         $tilpums_virsmers = $tilpums_bruto - $tilpums_bruto_bez_virsmera;
         $tilpums_garuma_redukcija = $tilpums_bruto_bez_virsmera - $tilpums_neto;
         $tilpums_diametrs_redukcija = 0;
         //----------
         $SVBruto += $tilpums_bruto;
         if ($gostu_tabula) {
             if ($VBruto == -1 || $VReducD == -1 || $VReducL == -1 || $VVirsm == -1) {
                 $SSkaitsBad++;
             }
             if ($VBruto == -1) {
                 $VBruto = 0;
             }
             if ($VReducD == -1) {
                 $VReducD = 0;
             }
             if ($VReducL == -1) {
                 $VReducL = 0;
             }
             if ($VVirsm == -1) {
                 $VVirsm = 0;
             }
         }
         $VBrakis = 0;
         if (strlen($this->rejected_values[$key]) == 1) {
             $irbrakis = true;
             $brkods = $this->rejected_values[$key];
         }
         if (!$irbrakis) {
             /*Vai ir brakis?*/
             $SVReducD += $tilpums_diametrs_redukcija;
             $SVReducL += $tilpums_garuma_redukcija;
             if ($gar_PecRed != 0) {
                 $SVVirsm += $tilpums_virsmers;
                 $SVNeto += $tilpums_neto;
             } else {
                 $SVBrakis += $SVBruto;
             }
         } else {
             if ($gar_PecRed != 0) {
                 $SVVirsm += $tilpums_virsmers;
                 $VBrakis = $tilpums_bruto - $tilpums_virsmers;
                 $SVBrakis += $VBrakis;
             } else {
                 $VBrakis = $tilpums_bruto;
                 $SVBrakis += $VBrakis;
             }
         }
     }
     /*Ievietosana*/
     //$this->arr[$this->arr_index][$level-1] = $this->kodi($this->MyPOST['gtype'.$this->grup.$this->lsk],$val);
     /* Vienu reizi pašās beigās papildina ar pēdējo ierakstu */
     $this->arr_index++;
     $this->arr[$this->arr_index] = array();
     foreach ($criteria as $key => $value) {
         //fb($m['value']);
         $this->arr[$this->arr_index][$key] = $this->kodi($value, $last[$value]);
     }
     $sm = new CSumma();
     $sm->skaits = $SSkaits;
     $sm->skaits_bad = $SSkaitsBad;
     $sm->virsmers = $SVVirsm;
     $sm->redukcija_d = $SVReducD;
     $sm->redukcija_l = $SVReducL;
     $sm->brakis = $SVBrakis;
     $sm->neto = $SVNeto;
     $sm->bruto = $SVBruto;
     $this->arr[$this->arr_index][summa] = $sm;
     for ($ci = $this->lsk - 2; $ci >= 0; $ci--) {
         if (!$summa[$ci + 1]) {
             $summa[$ci + 1] = new CSumma();
         }
         $summa[$ci + 1]->add_skaits($SSkaits);
         $summa[$ci + 1]->add_skaits_bad($SSkaitsBad);
         $summa[$ci + 1]->add_virsmers($SVVirsm);
         $summa[$ci + 1]->add_redukcija_d($SVReducD);
         $summa[$ci + 1]->add_redukcija_l($SVReducL);
         $summa[$ci + 1]->add_brakis($SVBrakis);
         $summa[$ci + 1]->add_neto($SVNeto);
         $summa[$ci + 1]->add_bruto($SVBruto);
         if (!$this->XML_Variants) {
             $this->AddSumRowAtLevel($ci, $criteria, $summa, $last);
         }
     }
     $summa_p->add_skaits($SSkaits);
     $summa_p->add_skaits_bad($SSkaitsBad);
     $summa_p->add_virsmers($SVVirsm);
     $summa_p->add_redukcija_d($SVReducD);
     $summa_p->add_redukcija_l($SVReducL);
     $summa_p->add_brakis($SVBrakis);
     $summa_p->add_neto($SVNeto);
     $summa_p->add_bruto($SVBruto);
 }