<?php
//profit
class ProfitCot extends ActiveRecord\Model {
    //static $connection = 'profit';
   static $table_name = 'cotiz_c';
   static $primary_key = 'fact_num';
   //put your code here
   static $campos = array('fact_num','contrib','nombre','rif','nit','status','comentario','descrip','saldo','fec_emis','fec_venc','co_cli','co_ven','co_tran','dir_ent','forma_pag','tot_bruto','tot_neto','glob_desc','tot_reca','porc_gdesc','porc_reca','total_uc','total_cp','tot_flete','monto_dev','totklu','anulada','impresa','iva','iva_dev','feccom','numcom','tasa','moneda','cta_contab','seriales','tasag','tasag10','tasag20','campo1','campo2','campo3','campo4','campo5','campo6','campo7','campo8','co_us_in','fe_us_in','co_us_mo','fe_us_mo','co_us_el','fe_us_el','revisado','trasnfe','co_sucu','rowguid','mon_ilc','otros1','otros2','otros3','aux01','aux02','salestax','origen','origen_d','sta_prod','telefono');
    static $has_many = array(
     array('reng_cac', 'foreign_key' => 'fact_num', 'class_name' => 'ProfitRengCot')
   );
    static $belongs_to = array(
      array('clientes', 'foreign_key' => 'co_cli', 'class_name' => 'ProfitClientes')
	  ,array('vendedor', 'foreign_key' => 'co_ven', 'class_name' => 'ProfitVendedor')
	  //ProfitStatusDoc
	  ,array('status_docs', 'foreign_key' => 'status', 'class_name' => 'ProfitStatusDoc')
	  ,array('condicio', 'foreign_key' => 'forma_pag', 'class_name' => 'ProfitCondicionDoc')
    );
   static function ReleasePistol(){
		//LIBERAMOS LOS ARTICULOS DE LOS PREDESPACHOS ANULADOS
		$sql ="
		UPDATE EFCCA.dbo.VEFC_WHEDETAILS set USED = 0 , order_type = '' , order_id = NULL, update_at = GETDATE(), update_by = '999'
FROM EFCCA.dbo.VEFC_WHEDETAILS wd
WHERE wd.order_type = 'PED'
AND wd.order_id IN (SELECT
		fact_num
	FROM pedidos
	WHERE anulada = 1)
"; 
			$pdo = self::connection()->connection;
			$Exe = $pdo->prepare($sql);
			$Exe->execute();
			//LIBERAMOS LOS ARTICULOS NO ASOCIADOS A UN RENGLON DE PREDESPACHOS
		$sql2 ="
		UPDATE EFCCA.dbo.VEFC_WHEDETAILS
SET USED = 0
   ,order_type = ''
   ,order_id = NULL
   ,update_at = GETDATE()
   ,update_by = '999'
FROM EFCCA.dbo.VEFC_WHEDETAILS wd
INNER JOIN EFCCA.dbo.VEFC_WHENTRANCE we
	ON we.DOCNUMBR = wd.DOCNUMBR
INNER JOIN pedidos p
	ON p.fact_num = wd.order_id
	AND p.status IN (1, 2)
	AND p.anulada = 0
LEFT JOIN reng_ped rp
	ON rp.fact_num = wd.order_id
	AND rp.co_art = we.ITEMNMBR
WHERE wd.order_type = 'PED'
AND wd.USED = 1
AND rp.reng_num IS NULL
"; 
			$pdo = self::connection()->connection;
			$Exe2 = $pdo->prepare($sql2);
			$Exe2->execute();
			
			//LIBERAMOS LOS ARTICULOS NO ASOCIADOS A UN RENGLON DE PREDESPACHOS
		$sql3 ="
		UPDATE EFCCA.dbo.VEFC_WHEDETAILS
SET USED = 0
   ,order_type = ''
   ,order_id = NULL
   ,update_at = GETDATE()
   ,update_by = '999'
FROM EFCCA.dbo.VEFC_WHEDETAILS wd
INNER JOIN EFCCA.dbo.VEFC_WHENTRANCE we
	ON we.DOCNUMBR = wd.DOCNUMBR
INNER JOIN pedidos p
	ON p.fact_num = wd.order_id
	AND p.status IN (0)
	AND p.anulada = 0
INNER JOIN reng_ped rp1
	ON rp1.fact_num = p.fact_num
LEFT JOIN reng_ped rp
	ON rp.fact_num = wd.order_id
	AND rp.co_art = we.ITEMNMBR and rp.nro_lote = we.LOTE
WHERE wd.order_type = 'PED'
AND wd.USED = 1
AND rp.reng_num IS NULL
"; 
			$pdo = self::connection()->connection;
			$Exe3 = $pdo->prepare($sql3);
			$Exe3->execute();
			
			return TRUE;
		   
	}
   
}
