<?php

class deboSync_model extends Databases {

    private $db = DB_NAME_HEAD;
    protected $token = false;

    public function mainSelect($prev_date, $date, $emp) {

        $sql = "select emp,tip,tco,suc,nco from " . $this->db . ".dbo.AMAEFACT where emp=$emp and fec between '$prev_date' and '$date' order by fec desc";
        $data['checkAmae'] = parent::query($sql);

        $sql = "select TCO,count(tco) as CANT from " . $this->db . ".dbo.AMAEFACT where emp=$emp and fec between '$prev_date' and '$date' group by tco";
        $data['checkTcoAmae'] = parent::query($sql);

        $sql = "select amo.TCO,count(amo.tco) as CANT from " . $this->db . ".dbo.AMAEFACT ama"
                . " join AMOVFACT amo on ama.emp=amo.emp and  ama.tip=amo.tip and ama.tco=amo.tco and ama.suc=amo.suc and ama.nco=amo.nco"
                . " where amo.emp=$emp and ama.fec between '$prev_date' and '$date' group by amo.tco";
        $data['checkTcoAmov'] = parent::query($sql);

        $sql = "select amo.tip,amo.tco,amo.suc,amo.nco from " . $this->db . ".dbo.AMAEFACT ama"
                . " join AMOVFACT amo on ama.emp=amo.emp and  ama.tip=amo.tip and ama.tco=amo.tco and ama.suc=amo.suc and ama.nco=amo.nco"
                . " where amo.emp=$emp and ama.fec between '$prev_date' and '$date' order by fec desc";
        $data['checkTcoAmovComp'] = parent::query($sql);

        $sql = "select amo.tip,amo.tco,amo.suc,amo.nco from " . $this->db . ".dbo.AMAEFACT ama"
                . " join acobypag amo on ama.emp=amo.emp and  ama.tip=amo.tip and ama.tco=amo.tco and ama.suc=amo.suc and ama.nco=amo.nco"
                . " where amo.emp=$emp and ama.fec between '$prev_date' and '$date' order by fec desc";
        $data['checkTcoAcobyComp'] = parent::query($sql);
        
        $sql = "select amo.TCO,count(amo.tco) as CANT from " . $this->db . ".dbo.AMAEFACT ama"
                . " join acobypag amo on ama.emp=amo.emp and  ama.tip=amo.tip and ama.tco=amo.tco and ama.suc=amo.suc and ama.nco=amo.nco"
                . " where amo.emp=$emp and ama.fec between '$prev_date' and '$date' group by amo.tco";
        $data['checkTcoAcoby'] = parent::query($sql);

        $sql = "select amo.tip,amo.tco,amo.suc,amo.nco from " . $this->db . ".dbo.AMAEFACT ama"
                . " join amovstoc amo on ama.emp=amo.emp and  ama.tip=amo.tip and ama.tco=amo.tco and ama.suc=amo.suc and ama.nco=amo.nco"
                . " where amo.emp=$emp and ama.fec between '$prev_date' and '$date' order by ama.fec desc";
        $data['checkRegAmovs'] = parent::query($sql);

        $sql = "select amo.TCO,count(amo.tco) as CANT from " . $this->db . ".dbo.AMAEFACT ama"
                . " join amovstoc amo on ama.emp=amo.emp and  ama.tip=amo.tip and ama.tco=amo.tco and ama.suc=amo.suc and ama.nco=amo.nco"
                . " where amo.emp=$emp and ama.fec between '$prev_date' and '$date' group by amo.tco";
        $data['checkRegAmovsComp'] = parent::query($sql);

        return $data;
    }

    public function checkEliminados($comprobantes, $emp) {
        set_time_limit(0);
        ini_set('default_socket_timeout', 0);
        foreach ($comprobantes as $value) {
            $sql = "select * from " . $this->db . ".dbo.COMP_ELIMINADOS where emp=$emp and CLA0='" . $value['CYV'] . "' and CLA1='" . $value['TIP'] . "' and CLA2='" . $value['TCO'] . "' and CLA3=" . $value['SUC'] . " and CLA4=" . $value['NCO'] . " and E_HD='' and C_HD=''";
            $data[] = parent::query($sql);
        }
        foreach ($data as $value) {
            if (!empty($value)) {
                $hay[] = $value[0];
            }
        }
        return $hay;
    }

    public function checkTodosEliminados($emp) {
        set_time_limit(0);
        ini_set('default_socket_timeout', 0);
        $sql = "select * from " . $this->db . ".dbo.COMP_ELIMINADOS where emp=$emp and E_HD='' and C_HD=''";
        $data = parent::query($sql);
        return $data;
    }

}
