<?php
require_once 'ApiGlobalFunctions.php';

$usuario=$_POST['uid'];
$password=$_POST['key'];
$url=$_POST['url'];
$printer=$_POST['printer'];
$tipo=$_POST['tipo']; // 0 - Slot     1 - Entrada   2 - Asset
$data=$_POST['data'];

$include=includec($url);
include $include;
$validate=loginkey($usuario,$password,$url);
error_log("SLOTINOUT Usuario: ".$usuario."printer ".$printer." Tkpo: ".$tipo."\n", 3, "php.log");
if($validate==1){
    
    if($printer=="ZPL"){ 
        if($tipo==0){ //SLOT
            $temp_name="SLOTS";
            $temp_string="^XA
^JC
^MNY
^XZ
^XA
^PW812
^LL797
^LH0,0

^CF0,36
^FO280,300^FDUBICACION SLOT^FS

^BY3,3,160
^FO160,350
^BCN,160,Y,N,N,N,N
^FD".$data."^FS

^XZ";
        }else if($tipo==1){ //ENTRADA //ASSET
        $query=mysqli_query($link,"SELECT subject, cf_2017, cf_2023 , cf_2074
        FROM vtiger_assets
        INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_assets.assetsid
        INNER JOIN vtiger_assetscf ON vtiger_assets.assetsid=vtiger_assetscf.assetsid
        LEFT JOIN vtiger_purchaseorder ON vtiger_purchaseorder.purchaseorderid=vtiger_assetscf.cf_2152
        WHERE deleted='0' AND serialnumber='$data'");
        $dataE=mysqli_fetch_array($query);
        $oc=$dataE['subject'];
        $fingreso=$dataE['cf_2017'];
        $cantidad=$dataE['cf_2023'];
        $codigo=$dataE['cf_2074'];
        $temp_name="Etiqueta de Stock";
        $temp_string="^XA
^JC
^MNY
^XZ
^XA
^PW812
^LL797
^LH0,0
^CF0,36

^FO180,140^BY3
^BCN,100,Y,N,N,N,N
^FD".$codigo."^FS

^CF0,30
^FO120,320^FDORDEN DE COMPRA: ".$oc."^FS
^FO120,360^FDFECHA DE INGRESO: ".$fingreso."^FS
^FO120,400^FDCANTIDAD EN PALLET: ".$cantidad."^FS

^CF0,30
^FO270,480^FDNUMERO DE SERIE^FS

^FO240,534^BY3
^BCN,100,Y,N,N,N,N
^FD".$data."^FS
^PQ1
^XZ";
        }else if($tipo==2){ //SALIDA
            $query=mysqli_query($link,"SELECT crmid, accountname, concat (cf_1937,' ',cf_1939,' ',cf_1941) as 'direccion', cf_856, subject, salesorder_no, paquetes_tks_fechadellegadaosa, concat(first_name,' ',last_name) as 'usuario', cf_2685
            FROM vtiger_paquetes
            INNER JOIN vtiger_paquetescf ON vtiger_paquetes.paquetesid=vtiger_paquetescf.paquetesid
            INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_paquetes.paquetesid
            INNER JOIN vtiger_account ON vtiger_account.accountid=vtiger_paquetes.paquetes_tks_cuenta
            INNER JOIN vtiger_salesorder ON vtiger_salesorder.salesorderid=vtiger_paquetes.paquetes_tks_ordendesalida
            INNER JOIN vtiger_salesordercf ON vtiger_salesordercf.salesorderid=vtiger_salesordercf.salesorderid
            INNER JOIN vtiger_users ON vtiger_users.id=vtiger_crmentity.smownerid
            WHERE vtiger_crmentity.deleted='0' AND  vtiger_paquetes.paquetesno='$data'
            GROUP BY 1;");
            $dataS=mysqli_fetch_array($query);
            $cuenta=$dataS['accountname'];
            $direccion=$dataS['direccion'];
            $tenvio=$dataS['cf_856'];
            $pedido=$dataS['subject'];
            $ot=$dataS['salesorder_no'];
            $fecha=$dataS['paquetes_tks_fechadellegadaosa'];
            $pick=$dataS['usuario'];
            $camion=$dataS['cf_2685'];
            
            $temp_name="SALIDA";
            $temp_string="^XA
^JC
^MNY
^XZ
^XA
^PW812
^LL797
^LH0,0

^CF0,30
^FO220,120^FDINDUSTRIAL Y COMERCIAL^FS
^FO260,160^FDSOLUCORP LIMITADA^FS

^CF0,28
^FO80,240^FDNombre de la empresa: ".$cuenta."^FS
^FO80,270^FDDireccion: ".$direccion."^FS
^FO80,300^FDTipo de envio: ".$tenvio."^FS
^FO80,330^FDNumero Pedido: ".$pedido."^FS
^FO80,370^FDNumero OT: ".$ot."^FS
^FO80,400^FDNumero de Caja: ".$data."^FS
^FO80,430^FDFecha de Preparacion: ".$fecha."^FS
^FO80,460^FDPikinero: ".$pick."^FS
^FO80,490^FDCamion: ".$camion."^FS
^BY3

^FO260,580
^BCN,100,Y,N,N,N,N
^FD".$data."^FS
^PQ1
^XZ";
        }  
        
        $TemArray['0']=array("Template_ID"=>"0","Template_name"=>$temp_name,"Template_String"=>$temp_string,"printer"=>$printer);
        
    }else if($printer=="Generic"){
        /*if($tipo==0){
            $temp_string="";
        }else if($tipo==1){
            
        }else if($tipo==2){
        
        } */
        
        $query=mysqli_query($link,"SELECT * 
        FROM vtiger_templatemov
        INNER JOIN vtiger_crmentity ON vtiger_crmentity.crmid=vtiger_templatemov.templatemovid
        INNER JOIN vtiger_templatemovcf ON vtiger_templatemovcf.templatemovid=vtiger_templatemov.templatemovid
        WHERE deleted='0' AND templatemov_tks_estado='Activo'");
        $num=mysqli_num_rows($query);
        if($num>0){
            $z=0;
             while ($row = mysqli_fetch_array($query)) {
                $temp_id=$row['crmid'];
                $temp_name=$row['templatemov_tks_nombre'];
                $temp_string='SIZE '.$row["templatemov_tks_ancho"].' mm,'.$row["templatemov_tks_alto"].' mm GAP '.$row["templatemov_tks_espacioentreet"].' mm,0 DENSITY '.$row["templatemov_tks_densidadtinta"].' SPEED '.$row["templatemov_tks_velocidad"].' DIRECTION '.$row["templatemov_tks_direccion"].' REFERENCE 0,0 CLS TEXT '.$row["templatemov_tks_coordenadastex"].',"'.$row["cf_2797"].'",'.$row["cf_2799"].','.$row["templatemov_tks_escaladotexto"].',"'.$row["templatemov_tks_texto"].'" BARCODE '.$row["templatemov_tks_coordenadasbc"].',"'.$row["templatemov_tks_tipo"].'",'.$row["templatemov_tks_altura"].','.$row["templatemov_tks_muestratexto"].','.$row["cf_2801"].','.$row["templatemov_tks_lineadelgada"].','.$row["templatemov_tks_lineagruesa"].',"'.$serial.'" PRINT 1';
                $TemArray[$z]=array("Template_ID"=>$temp_id,"Template_name"=>$temp_name,"Template_String"=>$temp_string);
                $z++;
            }
	    }else{
	        $TemArray[0]=array("Template_ID"=>"","Template_name"=>"","Template_String"=>"");
	    }
    }
    
	echo json_encode(setDataJSON($TemArray,1));
	
}