<?php

class Facturas_franquicias_model extends Databases {
    
    private $db =  DB_NAME_HEAD;
                function __construct() {
        parent::__construct();
    }

    public function verificarComprobantes($comprobantes, $EMP) {
        $compSinSincronizar = array();
        if (is_array($comprobantes) && count($comprobantes) > 0) {
            $PRO = parent::query("SELECT PRO FROM " . $this->db . ".dbo.APAREMP WHERE EMP=$EMP");
            if ($PRO) {
                $PRO = $PRO[0]['PRO'];
            } else {
                return FALSE;
            }

            foreach ($comprobantes as $comp) {
                $ver = parent::query("SELECT NCO FROM " . $this->db . ".dbo.PMAEFACT_AUTO WHERE PRO=$PRO AND TIP='" . $comp['TIP'] . "' AND TCO='" . $comp['TCO'] . "' AND SUC=" . $comp['SUC'] . " AND NCO=" . $comp['NCO']);
                if (!$ver) {
                    $compSinSincronizar[] = $comp;
                    
                    if (count($compSinSincronizar) == 10) { //fix problema en gobar, no devolvía más de 15 elementos el array
                        return $compSinSincronizar;
                    }
                }
            }
        }

        return $compSinSincronizar;
    }

    public function sincronizarComprobantes($comprobante, $EMP) {
        $PRO = parent::query("SELECT PRO FROM " . $this->db . ".dbo.APAREMP WHERE EMP=$EMP");
        if ($PRO) {
            $PRO = $PRO[0]['PRO'];
        } else {
            return FALSE;
        }

        $EMP = parent::query("SELECT EMP FROM " . $this->db . ".dbo.APAREMP WHERE CLI=" . $comprobante['COD']);
        if ($EMP) {
            $EMP = $EMP[0]['EMP'];
        } else {
            return FALSE;
        }
        $arr_pmaefact = array(
            'EMP' => $EMP,
            'TIP' => $comprobante['TIP'],
            'TCO' => $comprobante['TCO'],
            'SUC' => $comprobante['SUC'],
            'NCO' => $comprobante['NCO'],
            'PRO' => $PRO,
            'FEC' => $comprobante['FEC'],
            'NOM' => $comprobante['NOM'],
            'TIV' => $comprobante['TIV'],
            'CUI' => $comprobante['CUI'],
            'FPA' => $comprobante['FPA'],
            'TCA' => 1,
            'DTO' => $comprobante['DTO'],
            'PDT' => 0,
            'NET' => $comprobante['NET'],
            'NEE' => $comprobante['NEE'],
            'IRI' => $comprobante['IRI'],
            'IRS' => 0,
            'IMI' => $comprobante['IMI'],
            'RGA' => 0,
            'RIB' => 0,
            'PIV' => 0,
            'CNG' => $comprobante['IMI2'],
            'TOT' => $comprobante['TOT'],
            'ZON' => $EMP,
            'FEV' => $comprobante['FEV'],
            'OCP' => '',
            'OPE' => 0,
            'REC' => 0,
            'FEP' => $comprobante['FEP'],
            'NPE' => $comprobante['NCO'],
            'CPA' => 0,
            'ENV' => 0,
            'REM' => 0,
            'PRR' => 'N',
            'ANU' => '',
            'TUR' => 0,
            'PLA' => 0,
            'LUG' => 0,
            'ATO' => 0,
            'CCO' => 0,
            'IMA' => 0,
            'CCA1' => 0,
            'CCA2' => 0,
            'CCA3' => 0,
            'CCA4' => 0,
            'CCA5' => 0,
            'CCA6' => 0,
            'CCA7' => 0,
            'CCA8' => 0,
            'CCA0' => 0,
            'OBS' => 'RECEPCION AUTO',
            'RIV' => 0,
            'FECCAI' => '',
            'CAI' => '',
            'CHO' => 1,
            'CTR' => '',
            'PER' => $comprobante['PER'],
            'CCA9' => 0,
            'FECVEN' => $comprobante['FEV'],
            'CNG2' => 0,
            'E_HD' => '',
            'C_HD' => '',
            'PFH' => 'N',
            'CG' => 'C',
            'CCONF' => 0,
            'DETPRO' => 'P',
            'ESTADO' => 1
        );

        $arr_pmovfact = array();
        if (isset($comprobante['DATOS']) && $comprobante['DATOS']) {
            foreach ($comprobante['DATOS'] as $it) {
                $arr_pmovfact[] = array(
                    'EMP' => $EMP,
                    'TIP' => $comprobante['TIP'],
                    'TCO' => $comprobante['TCO'],
                    'SUC' => $comprobante['SUC'],
                    'NCO' => $comprobante['NCO'],
                    'PRO' => $PRO,
                    'ORD' => $it['ORD'],
                    'SEC' => $it['COD'],
                    'ART' => $it['ART'],
                    'RUB' => $it['RUB'],
                    'TIO' => $it['TIO'],
                    'LI0' => '',
                    'CAN' => $it['CAN'],
                    'PUN' => $it['PUN'],
                    'IMI' => $it['IMI'],
                    'IVA' => $it['IVA'],
                    'PUT' => $it['PUT'],
                    'LEG' => 0,
                    'CMF' => 0,
                    'TUR' => 0,
                    'PLA' => 0,
                    'LUG' => 0,
                    'ESC' => 0,
                    'TAN' => 0,
                    'CCO' => 0,
                    'E_HD' => '',
                    'C_HD' => ''
                );
            }
        }
        

        parent::init();
        parent::insert($this->db . '.dbo.PMAEFACT_AUTO', $arr_pmaefact);
        
        foreach ($arr_pmovfact as $it) {
            parent::insert($this->db . '.dbo.PMOVFACT_AUTO', $it);
        }
        parent::commit();

        return TRUE;
    }

}
