 <?php

//error_reporting(E_ERROR  | E_PARSE);

session_start();
/*
$_SESSION['database'] = $_GET["database"];

$_SESSION['suc_id'] = $_GET["suc_id"];

$_SESSION['est_id'] = $_GET["est_id"];

$_SESSION['id_pack'] =$_GET["id_pack"];

 */
$_SESSION['database'] = $_POST["database"];

$_SESSION['suc_id'] = $_POST["suc_id"];

$_SESSION['est_id'] = $_POST["est_id"];

$_SESSION['id_pack'] = $_POST["id_pack"];

include 'conn.php';

include 'funciones.php';

//logs("xxxxxxxxxxxxxxxxxx");

$hoynow = getdate();

//echo print_r($hoynow);

//echo date_default_timezone_get();

$hora = str_pad($hoynow["hours"], 2, "0", STR_PAD_LEFT);

$minutos = str_pad($hoynow["minutes"], 2, "0", STR_PAD_LEFT);

$mes = str_pad($hoynow["mon"], 2, "0", STR_PAD_LEFT);

$dia = str_pad($hoynow["mday"], 2, "0", STR_PAD_LEFT);

$timeStamp = $hoynow["year"] . "-" . $mes . "-" . $dia . " " . $hora . ":" . $minutos . ":" . $hoynow["seconds"];
if ($_SESSION['est_id'] == "0") {
    $SQLupdate = "update " . $_SESSION['database'] . ".sucursales set suc_ult_con='" . $timeStamp . "',suc_pack_prod=" . $_SESSION['id_pack'] . "  where  suc_id='" . $_SESSION['suc_id'] . "' limit 1 ";
} else {
    $SQLupdate = "update " . $_SESSION['database'] . ".estaciones set e_ult_con='" . $timeStamp . "',e_pack_prod=" . $_SESSION['id_pack'] . "  where suc_id='" . $_SESSION['suc_id'] . "'  and e_id='" . $_SESSION['est_id'] . "' limit 1 ";
}

if ($connection->query($SQLupdate) === true) {
    //  echo "update op";
} else {
    echo "Error: " . $SQLupdate . "<br>" . $conn->error;
}

$montoesql1 = " select  distinct id_pack,
			      	productos.pr_id
					,productos.pr_descripcion
					,productos.pr_id_familia
					,productos.pr_referencia
					,productos.fab_id
					,productos.id_marca
					,productos.pr_nro_parte
					,productos.pr_tipo
					,productos.pr_detalle
					,productos.pr_descripcion2
					,productos.pr_descripcion3
					,productos.pr_detalle2
					,productos.pr_detalle3
					,productos.pr_id_enlace
					,productos.pr_exist_descar
					,productos.pr_exist_cargar
					,productos.pr_en_enlace
					,productos.pr_ubicacion
					,productos.w_tipo
					,productos.w_btcolor
					,productos.w_fncolor
					,productos.w_id_order
					,productos.w_bt_prior
					,productos.med_key
					,productos.pr_no_cuenta
					,productos.pr_dimension
					,productos.pr_peso
					,productos_det.pr_venta1
					,productos_det.pr_venta2
					,productos_det.pr_venta3
					,productos_det.pr_util1
					,productos_det.pr_util2
					,productos_det.pr_util3
					,productos_det.pr_imp1
					,productos_det.pr_imp2
					,productos_det.pr_imp3
					,productos_det.pr_itbms
					,productos_det.pr_costo
					,productos_det.pr_costo_actual
					,productos_det.pr_costo_prom
					,productos_det.pr_activo
					,productos_det.pr_margen1
					,productos_det.pr_margen2
					,productos_det.pr_margen3
					,productos_det.pr_precio1_imp
					,productos_det.pr_precio2_imp
					,productos_det.pr_precio3_imp
					,productos_det.pr_exento
					,productos_det.pr_nivel1
					,productos_det.pr_nivel2
					,productos_det.pr_nivel3
					,productos_det.pr_activa_desemp
					,productos_det.pr_man_seriales
					,productos_det.pr_des_tecla
					,productos_det.pr_minimo
					,productos_det.pr_maximo
					,productos_det.pr_sugerido
					,productos_det.pr_garantia
					,productos_det.pr_condiciones
					,productos_det.pr_creacion
					,productos_det.pr_modificacion
					,productos_det.pr_u_id_crea
					,productos_det.pr_u_id_mod
					,productos_det.ubic_id
					,productos_det.ubic_id2
					,productos_det.ubic_id3
					,productos_det.ubic_id4
					,productos_det.pr_comisionv
					,productos_det.pr_comisionc
					,productos_det.pr_tipo_comision
					,productos_det.pr_comision_prior
					,productos_det.pr_man_exist
					,productos_det.pr_man_lote
					,productos_det.suc_id
					,productos_det.pr_imp_adic
					,productos_det.pr_imp1_adic
					,productos_det.pr_imp2_adic
					,productos_det.pr_imp3_adic
					,productos_det.pr_precio_libre
					,productos_det.pr_gmodif
					,productos_det.pr_show_pres
					,productos_det.pr_venta4
					,productos_det.pr_oferta
					,productos_det.pr_certif
					,productos_det.w_activar
					,productos_det.pr_venta5
					,productos_det.pr_venta6
					,productos_det.pr_venta7
					,productos_det.pr_util5
					,productos_det.pr_util6
					,productos_det.pr_util7
					,productos_det.pr_margen5
					,productos_det.pr_margen6
					,productos_det.pr_margen7
					,productos_det.pr_precio5_imp
					,productos_det.pr_precio6_imp
					,productos_det.pr_precio7_imp
					,productos_det.pr_imp5
					,productos_det.pr_imp6
					,productos_det.pr_imp7
					,productos_det.pr_imp5_adic
					,productos_det.pr_imp6_adic
					,productos_det.pr_imp7_adic
					,productos_det.pr_costo2
					,productos_det.pr_costo_actual2
					,productos_det.pr_costo_prom2
					,productos_det.pr_costo3
					,productos_det.pr_costo_actual3
					,productos_det.pr_costo_prom3
					,productos_det.pr_venta1_moned
					,productos_det.pr_venta2_moned
					,productos_det.pr_venta3_moned
					,productos_det.pr_venta5_moned
					,productos_det.pr_venta6_moned
					,productos_det.pr_venta7_moned
					,productos_det.mon_simb1
					,productos_det.mon_simb2
					,productos_det.mon_simb3
					,productos_det.mon_simb5
					,productos_det.mon_simb6
					,productos_det.mon_simb7
					,productos_det.pr_costo_bk
					,productos_det.pr_costo_actual_bk
					,productos_det.pr_costo_prom_bk
					,productos_det.SyncProcesing
					,productos_det.UserModify
					,productos_det.UserOption
					,productos_det.pr_id_unico

                	,productos.resting_desc
                	,productos.pr_food_stamp
                	,productos.images_id



				  from " . $_SESSION['database'] . "_tmp_pack.productos_pack


				 left outer join  " . $_SESSION['database'] . ".productos on productos_pack.pr_id=productos.pr_id

				  left outer join     " . $_SESSION['database'] . ".productos_det on productos.pr_Id=productos_det.pr_id




				   where  id_pack='" . $_SESSION['id_pack'] . "'   ";

//       echo $montoesql1;

$qur1 = $connection->query($montoesql1);

$i = 0;

//  $jsonData[] ='';

while ($r1 = mysqli_fetch_assoc($qur1)) {

    $jsonTempData = array();
    $jsonTempData2 = array();
    $jsonTempData['pr_id'] = utf8_encode($r1['pr_id']);
    $jsonTempData['pr_descripcion'] = utf8_encode($r1['pr_descripcion']);
    $jsonTempData['pr_id_familia'] = utf8_encode($r1['pr_id_familia']);
    $jsonTempData['pr_referencia'] = utf8_encode($r1['pr_referencia']);
    $jsonTempData['fab_id'] = utf8_encode($r1['fab_id']);
    $jsonTempData['id_marca'] = utf8_encode($r1['id_marca']);
    $jsonTempData['pr_nro_parte'] = utf8_encode($r1['pr_nro_parte']);
    $jsonTempData['pr_tipo'] = utf8_encode($r1['pr_tipo']);
    $jsonTempData['pr_detalle'] = utf8_encode($r1['pr_detalle']);
    $jsonTempData['pr_descripcion2'] = utf8_encode($r1['pr_descripcion2']);
    $jsonTempData['pr_descripcion3'] = utf8_encode($r1['pr_descripcion3']);
    $jsonTempData['pr_detalle2'] = utf8_encode($r1['pr_detalle2']);
    $jsonTempData['pr_detalle3'] = utf8_encode($r1['pr_detalle3']);
    $jsonTempData['pr_id_enlace'] = utf8_encode($r1['pr_id_enlace']);
    $jsonTempData['pr_exist_descar'] = utf8_encode($r1['pr_exist_descar']);
    $jsonTempData['pr_exist_cargar'] = utf8_encode($r1['pr_exist_cargar']);
    $jsonTempData['pr_en_enlace'] = utf8_encode($r1['pr_en_enlace']);
    $jsonTempData['pr_ubicacion'] = utf8_encode($r1['pr_ubicacion']);
    $jsonTempData['w_tipo'] = utf8_encode($r1['w_tipo']);
    $jsonTempData['w_btcolor'] = utf8_encode($r1['w_btcolor']);
    $jsonTempData['w_fncolor'] = utf8_encode($r1['w_fncolor']);
    $jsonTempData['w_id_order'] = utf8_encode($r1['w_id_order']);
    $jsonTempData['w_bt_prior'] = utf8_encode($r1['w_bt_prior']);
    $jsonTempData['med_key'] = utf8_encode($r1['med_key']);
    $jsonTempData['pr_no_cuenta'] = utf8_encode($r1['pr_no_cuenta']);
    $jsonTempData['pr_dimension'] = utf8_encode($r1['pr_dimension']);
    $jsonTempData['pr_peso'] = utf8_encode($r1['pr_peso']);

    $jsonTempData['resting_desc'] = utf8_encode($r1['resting_desc']);
    $jsonTempData['pr_food_stamp'] = utf8_encode($r1['pr_food_stamp']);

    $images = utf8_encode($r1['images_id']);

    $jsonTempData['images_id'] = $images;

    $jsonTempData2['pr_id'] = utf8_encode($r1['pr_id']);
    $jsonTempData2['pr_venta1'] = utf8_encode($r1['pr_venta1']);
    $jsonTempData2['pr_venta2'] = utf8_encode($r1['pr_venta2']);
    $jsonTempData2['pr_venta3'] = utf8_encode($r1['pr_venta3']);
    $jsonTempData2['pr_util1'] = utf8_encode($r1['pr_util1']);
    $jsonTempData2['pr_util2'] = utf8_encode($r1['pr_util2']);
    $jsonTempData2['pr_util3'] = utf8_encode($r1['pr_util3']);
    $jsonTempData2['pr_imp1'] = utf8_encode($r1['pr_imp1']);
    $jsonTempData2['pr_imp2'] = utf8_encode($r1['pr_imp2']);
    $jsonTempData2['pr_imp3'] = utf8_encode($r1['pr_imp3']);
    $jsonTempData2['pr_itbms'] = utf8_encode($r1['pr_itbms']);
    $jsonTempData2['pr_costo'] = utf8_encode($r1['pr_costo']);
    $jsonTempData2['pr_costo_actual'] = utf8_encode($r1['pr_costo_actual']);
    $jsonTempData2['pr_costo_prom'] = utf8_encode($r1['pr_costo_prom']);
    $jsonTempData2['pr_activo'] = utf8_encode($r1['pr_activo']);
    $jsonTempData2['pr_margen1'] = utf8_encode($r1['pr_margen1']);
    $jsonTempData2['pr_margen2'] = utf8_encode($r1['pr_margen2']);
    $jsonTempData2['pr_margen3'] = utf8_encode($r1['pr_margen3']);
    $jsonTempData2['pr_precio1_imp'] = utf8_encode($r1['pr_precio1_imp']);
    $jsonTempData2['pr_precio2_imp'] = utf8_encode($r1['pr_precio2_imp']);
    $jsonTempData2['pr_precio3_imp'] = utf8_encode($r1['pr_precio3_imp']);
    $jsonTempData2['pr_exento'] = utf8_encode($r1['pr_exento']);
    $jsonTempData2['pr_nivel1'] = utf8_encode($r1['pr_nivel1']);
    $jsonTempData2['pr_nivel2'] = utf8_encode($r1['pr_nivel2']);
    $jsonTempData2['pr_nivel3'] = utf8_encode($r1['pr_nivel3']);
    $jsonTempData2['pr_activa_desemp'] = utf8_encode($r1['pr_activa_desemp']);
    $jsonTempData2['pr_man_seriales'] = utf8_encode($r1['pr_man_seriales']);
    $jsonTempData2['pr_des_tecla'] = utf8_encode($r1['pr_des_tecla']);
    $jsonTempData2['pr_minimo'] = utf8_encode($r1['pr_minimo']);
    $jsonTempData2['pr_maximo'] = utf8_encode($r1['pr_maximo']);
    $jsonTempData2['pr_sugerido'] = utf8_encode($r1['pr_sugerido']);
    $jsonTempData2['pr_garantia'] = utf8_encode($r1['pr_garantia']);
    $jsonTempData2['pr_condiciones'] = utf8_encode($r1['pr_condiciones']);
    $jsonTempData2['pr_creacion'] = utf8_encode($r1['pr_creacion']);
    $jsonTempData2['pr_modificacion'] = utf8_encode($r1['pr_modificacion']);
    $jsonTempData2['pr_u_id_crea'] = utf8_encode($r1['pr_u_id_crea']);
    $jsonTempData2['pr_u_id_mod'] = utf8_encode($r1['pr_u_id_mod']);
    $jsonTempData2['ubic_id'] = utf8_encode($r1['ubic_id']);
    $jsonTempData2['ubic_id2'] = utf8_encode($r1['ubic_id2']);
    $jsonTempData2['ubic_id3'] = utf8_encode($r1['ubic_id3']);
    $jsonTempData2['ubic_id4'] = utf8_encode($r1['ubic_id4']);
    $jsonTempData2['pr_comisionv'] = utf8_encode($r1['pr_comisionv']);
    $jsonTempData2['pr_comisionc'] = utf8_encode($r1['pr_comisionc']);
    $jsonTempData2['pr_tipo_comision'] = utf8_encode($r1['pr_tipo_comision']);
    $jsonTempData2['pr_comision_prior'] = utf8_encode($r1['pr_comision_prior']);
    $jsonTempData2['pr_man_exist'] = utf8_encode($r1['pr_man_exist']);
    $jsonTempData2['pr_man_lote'] = utf8_encode($r1['pr_man_lote']);
    $jsonTempData2['suc_id'] = utf8_encode($r1['suc_id']);
    $jsonTempData2['pr_imp_adic'] = utf8_encode($r1['pr_imp_adic']);
    $jsonTempData2['pr_imp1_adic'] = utf8_encode($r1['pr_imp1_adic']);
    $jsonTempData2['pr_imp2_adic'] = utf8_encode($r1['pr_imp2_adic']);
    $jsonTempData2['pr_imp3_adic'] = utf8_encode($r1['pr_imp3_adic']);
    $jsonTempData2['pr_precio_libre'] = utf8_encode($r1['pr_precio_libre']);
    $jsonTempData2['pr_gmodif'] = utf8_encode($r1['pr_gmodif']);
    $jsonTempData2['pr_show_pres'] = utf8_encode($r1['pr_show_pres']);
    $jsonTempData2['pr_venta4'] = utf8_encode($r1['pr_venta4']);
    $jsonTempData2['pr_oferta'] = utf8_encode($r1['pr_oferta']);
    $jsonTempData2['pr_certif'] = utf8_encode($r1['pr_certif']);
    $jsonTempData2['w_activar'] = utf8_encode($r1['w_activar']);
    $jsonTempData2['pr_venta5'] = utf8_encode($r1['pr_venta5']);
    $jsonTempData2['pr_venta6'] = utf8_encode($r1['pr_venta6']);
    $jsonTempData2['pr_venta7'] = utf8_encode($r1['pr_venta7']);
    $jsonTempData2['pr_util5'] = utf8_encode($r1['pr_util5']);
    $jsonTempData2['pr_util6'] = utf8_encode($r1['pr_util6']);
    $jsonTempData2['pr_util7'] = utf8_encode($r1['pr_util7']);
    $jsonTempData2['pr_margen5'] = utf8_encode($r1['pr_margen5']);
    $jsonTempData2['pr_margen6'] = utf8_encode($r1['pr_margen6']);
    $jsonTempData2['pr_margen7'] = utf8_encode($r1['pr_margen7']);
    $jsonTempData2['pr_precio5_imp'] = utf8_encode($r1['pr_precio5_imp']);
    $jsonTempData2['pr_precio6_imp'] = utf8_encode($r1['pr_precio6_imp']);
    $jsonTempData2['pr_precio7_imp'] = utf8_encode($r1['pr_precio7_imp']);
    $jsonTempData2['pr_imp5'] = utf8_encode($r1['pr_imp5']);
    $jsonTempData2['pr_imp6'] = utf8_encode($r1['pr_imp6']);
    $jsonTempData2['pr_imp7'] = utf8_encode($r1['pr_imp7']);
    $jsonTempData2['pr_imp5_adic'] = utf8_encode($r1['pr_imp5_adic']);
    $jsonTempData2['pr_imp6_adic'] = utf8_encode($r1['pr_imp6_adic']);
    $jsonTempData2['pr_imp7_adic'] = utf8_encode($r1['pr_imp7_adic']);
    $jsonTempData2['pr_costo2'] = utf8_encode($r1['pr_costo2']);
    $jsonTempData2['pr_costo_actual2'] = utf8_encode($r1['pr_costo_actual2']);
    $jsonTempData2['pr_costo_prom2'] = utf8_encode($r1['pr_costo_prom2']);
    $jsonTempData2['pr_costo3'] = utf8_encode($r1['pr_costo3']);
    $jsonTempData2['pr_costo_actual3'] = utf8_encode($r1['pr_costo_actual3']);
    $jsonTempData2['pr_costo_prom3'] = utf8_encode($r1['pr_costo_prom3']);
    $jsonTempData2['pr_venta1_moned'] = utf8_encode($r1['pr_venta1_moned']);
    $jsonTempData2['pr_venta2_moned'] = utf8_encode($r1['pr_venta2_moned']);
    $jsonTempData2['pr_venta3_moned'] = utf8_encode($r1['pr_venta3_moned']);
    $jsonTempData2['pr_venta5_moned'] = utf8_encode($r1['pr_venta5_moned']);
    $jsonTempData2['pr_venta6_moned'] = utf8_encode($r1['pr_venta6_moned']);
    $jsonTempData2['pr_venta7_moned'] = utf8_encode($r1['pr_venta7_moned']);
    $jsonTempData2['mon_simb1'] = utf8_encode($r1['mon_simb1']);
    $jsonTempData2['mon_simb2'] = utf8_encode($r1['mon_simb2']);
    $jsonTempData2['mon_simb3'] = utf8_encode($r1['mon_simb3']);
    $jsonTempData2['mon_simb5'] = utf8_encode($r1['mon_simb5']);
    $jsonTempData2['mon_simb6'] = utf8_encode($r1['mon_simb6']);
    $jsonTempData2['mon_simb7'] = utf8_encode($r1['mon_simb7']);
    $jsonTempData2['pr_costo_bk'] = utf8_encode($r1['pr_costo_bk']);
    $jsonTempData2['pr_costo_actual_bk'] = utf8_encode($r1['pr_costo_actual_bk']);
    $jsonTempData2['pr_costo_prom_bk'] = utf8_encode($r1['pr_costo_prom_bk']);
    $jsonTempData2['SyncProcesing'] = utf8_encode($r1['SyncProcesing']);
    $jsonTempData2['UserModify'] = utf8_encode($r1['UserModify']);
    $jsonTempData2['UserOption'] = utf8_encode($r1['UserOption']);
    $jsonTempData2['pr_id_unico'] = utf8_encode($r1['pr_id_unico']);

    $jsonData[] = $jsonTempData;
    $jsonData2[] = $jsonTempData2;

    $i = $i + 1;
}

$json->registros = $i;

$json->productos = $jsonData;

$json->productos_det = $jsonData2;

$montoesql1a = "select  * from  " . $_SESSION['database'] . "_tmp_pack.productos_com_pack   where  id_pack='" . $_SESSION['id_pack'] . "'   ";

logs($montoesql1a);
$qur1a = $connection->query($montoesql1a);

$i = 0;
$jsonTempDataa = array();
while ($r1 = mysqli_fetch_assoc($qur1a)) {

    logs("entro" . $r1['comp_id']);

    $jsonTempDataa = array();
    $jsonTempDataa['comp_id'] = utf8_encode($r1['comp_id']);
    $jsonTempDataa['pr_id'] = utf8_encode($r1['pr_id']);
    $jsonTempDataa['cant_equi'] = utf8_encode($r1['cant_equi']);
    $jsonTempDataa['pr_costo'] = utf8_encode($r1['pr_costo']);
    $jsonTempDataa['pr_costo_equi'] = utf8_encode($r1['pr_costo_equi']);
    $jsonTempDataa['suc_id'] = utf8_encode($r1['suc_id']);
    $jsonTempDataa['pr_com_unico'] = utf8_encode($r1['comp_id']) . "-" . utf8_encode($r1['pr_id']);

    $jsonDataa[] = $jsonTempDataa;

    $i = $i + 1;
}

if ($i == 0) {
    $jsonTempDataa = array();
    $jsonTempDataa['comp_id'] = 0;
    $jsonTempDataa['pr_id'] = 0;
    $jsonTempDataa['cant_equi'] = 0;
    $jsonTempDataa['pr_costo'] = 0;
    $jsonTempDataa['pr_costo_equi'] = 0;
    $jsonTempDataa['suc_id'] = 0;
    $jsonTempDataa['pr_com_unico'] = 0;

    $jsonDataa[] = $jsonTempDataa;

}
$json->compuestos = $jsonDataa;

$montoesql1b = "select  * from  " . $_SESSION['database'] . "_tmp_pack.productos_suc_pack   where  id_pack='" . $_SESSION['id_pack'] . "'   ";

logs($montoesql1b);
$qur1b = $connection->query($montoesql1b);

$i = 0;
$jsonTempDatab = array();
while ($r1 = mysqli_fetch_assoc($qur1b)) {

    $jsonTempDatab = array();
    $jsonTempDatab['suc_id'] = utf8_encode($r1['suc_id']);
    $jsonTempDatab['pr_id'] = utf8_encode($r1['pr_id']);
    $jsonTempDatab['pr_activo'] = utf8_encode($r1['pr_activo']);
    $jsonTempDatab['pr_suc_unico'] = utf8_encode($r1['suc_id']) . "-" . utf8_encode($r1['pr_id']);

    $jsonDatab[] = $jsonTempDatab;

    $i = $i + 1;
}

if ($i == 0) {
    $jsonTempDatab = array();
    $jsonTempDatab['suc_id'] = 0;
    $jsonTempDatab['pr_id'] = 0;
    $jsonTempDatab['pr_activo'] = 0;
    $jsonTempDatab['pr_suc_unico'] = 0;

    $jsonDatab[] = $jsonTempDatab;

}

$json->productos_suc = $jsonDatab;

$montoesql1c = " select pr_id_asoc,prod_asoc.pr_id,pr_cant from   " . $_SESSION['database'] . ".prod_asoc

left outer join  " . $_SESSION['database'] . "_tmp_pack.productos_pack on prod_asoc.pr_id= " . $_SESSION['database'] . "_tmp_pack.productos_pack.pr_id
where id_pack='" . $_SESSION['id_pack'] . "'   ";

logs($montoesql1c);
$qur1c = $connection->query($montoesql1c);

$i = 0;
$jsonTempDatac = array();
while ($r1 = mysqli_fetch_assoc($qur1c)) {

    $jsonTempDatac = array();
    $jsonTempDatac['pr_id_asoc'] = utf8_encode($r1['pr_id_asoc']);
    $jsonTempDatac['pr_id'] = utf8_encode($r1['pr_id']);
    $jsonTempDatac['pr_cant'] = utf8_encode($r1['pr_cant']);
    $jsonTempDatac['pr_asoc_unico'] = utf8_encode($r1['pr_id_asoc']) . "-" . utf8_encode($r1['pr_id']);

    $jsonDatac[] = $jsonTempDatac;

    $i = $i + 1;
}

if ($i == 0) {
    $jsonTempDatac = array();
    $jsonTempDatac['pr_id_asoc'] = 0;
    $jsonTempDatac['pr_id'] = 0;
    $jsonTempDatac['pr_cant'] = 0;
    $jsonTempDatac['pr_asoc_unico'] = 0;
    $jsonDatac[] = $jsonTempDatac;

}

$json->prod_asoc = $jsonDatac;

$montoesql1f = " select familia.f_id
					,familia.f_descripcion
					,familia.f_activo
					,familia.F_des_tecla
					,familia.f_detalle
					,familia.fam_imp
					,familia.f_ubic_id
					,familia.f_ubic_id2
					,familia.f_ubic_id3
					,familia.f_ubic_id4
					,familia.f_no_cuenta
					,familia.f_kds_prior
				    ,familia.w_btcolor
                	,familia.w_fncolor
                	,familia.w_bt_prior
                	,familia.images_id from    " . $_SESSION['database'] . ".productos

left outer join   " . $_SESSION['database'] . "_tmp_pack.productos_pack on productos.pr_id=  " . $_SESSION['database'] . "_tmp_pack.productos_pack.pr_id

left outer join   " . $_SESSION['database'] . ".familia on  " . $_SESSION['database'] . ".productos.pr_id_familia=  " . $_SESSION['database'] . ".familia.f_id


where id_pack='" . $_SESSION['id_pack'] . "' group by familia.f_id  ";

logs($montoesql1f);
$qur1f = $connection->query($montoesql1f);

$i = 0;
$jsonTempDatac = array();
while ($r1 = mysqli_fetch_assoc($qur1f)) {

    $jsonTempDataf = array();
    $jsonTempDataf['f_id'] = utf8_encode($r1['f_id']);
    $jsonTempDataf['f_descripcion'] = utf8_encode($r1['f_descripcion']);
    $jsonTempDataf['f_activo'] = utf8_encode($r1['f_activo']);
    $jsonTempDataf['F_des_tecla'] = utf8_encode($r1['F_des_tecla']);
    $jsonTempDataf['f_detalle'] = utf8_encode($r1['f_detalle']);
    $jsonTempDataf['fam_imp'] = utf8_encode($r1['fam_imp']);
    $jsonTempDataf['f_ubic_id'] = utf8_encode($r1['f_ubic_id']);
    $jsonTempDataf['f_ubic_id2'] = utf8_encode($r1['f_ubic_id2']);
    $jsonTempDataf['f_ubic_id3'] = utf8_encode($r1['f_ubic_id3']);
    $jsonTempDataf['f_ubic_id4'] = utf8_encode($r1['f_ubic_id4']);
    $jsonTempDataf['f_no_cuenta'] = utf8_encode($r1['f_no_cuenta']);
    $jsonTempDataf['f_kds_prior'] = utf8_encode($r1['f_kds_prior']);
    $jsonTempDataf['w_btcolor'] = utf8_encode($r1['w_btcolor']);
    $jsonTempDataf['w_fncolor'] = utf8_encode($r1['w_fncolor']);
    $jsonTempDataf['w_bt_prior'] = utf8_encode($r1['w_bt_prior']);
    $jsonTempDataf['images_id'] = utf8_encode($r1['images_id']);

    $jsonDataf[] = $jsonTempDataf;

    $i = $i + 1;
}

if ($i == 0) {
    $jsonTempDataf = array();
    $jsonTempDataf['f_id'] = 0;
    $jsonTempDataf['f_descripcion'] = 0;
    $jsonTempDataf['f_activo'] = 0;
    $jsonTempDataf['F_des_tecla'] = 0;
    $jsonTempDataf['f_detalle'] = 0;
    $jsonTempDataf['fam_imp'] = 0;
    $jsonTempDataf['f_ubic_id'] = 0;
    $jsonTempDataf['f_ubic_id2'] = 0;
    $jsonTempDataf['f_ubic_id3'] = 0;
    $jsonTempDataf['f_ubic_id4'] = 0;
    $jsonTempDataf['f_no_cuenta'] = 0;
    $jsonTempDataf['f_kds_prior'] = 0;
    $jsonTempDataf['w_btcolor'] = 0;
    $jsonTempDataf['w_fncolor'] = 0;
    $jsonTempDataf['w_bt_prior'] = 0;
    $jsonTempDataf['images_id'] = 0;
    $jsonDataf[] = $jsonTempDataf;

}

$json->familia = $jsonDataf;

$montoesql1m = "select marcas.id_marca
					,marcas.des_marca
					,marcas.det_marcas
					,marcas.mar_activo
					,marcas.images_id
					,marcas.mar_no_cuenta
					 from   " . $_SESSION['database'] . ".productos

left outer join  " . $_SESSION['database'] . "_tmp_pack.productos_pack on productos.pr_id= " . $_SESSION['database'] . "_tmp_pack.productos_pack.pr_id

left outer join  " . $_SESSION['database'] . ".marcas on " . $_SESSION['database'] . ".productos.id_marca= " . $_SESSION['database'] . ".marcas.id_marca


where id_pack='" . $_SESSION['id_pack'] . "' group by id_marca";

logs($montoesql1m);
$qur1m = $connection->query($montoesql1m);

$i = 0;
$jsonTempDatam = array();
while ($r1 = mysqli_fetch_assoc($qur1m)) {

    $jsonTempDatam = array();
    $jsonTempDatam['id_marca'] = utf8_encode($r1['id_marca']);
    $jsonTempDatam['des_marca'] = utf8_encode($r1['des_marca']);
    $jsonTempDatam['det_marcas'] = utf8_encode($r1['det_marcas']);
    $jsonTempDatam['mar_activo'] = utf8_encode($r1['mar_activo']);
    $jsonTempDatam['images_id'] = utf8_encode($r1['images_id']);
    $jsonTempDatam['mar_no_cuenta'] = utf8_encode($r1['mar_no_cuenta']);

    $jsonDatam[] = $jsonTempDatam;

    $i = $i + 1;
}

if ($i == 0) {
    $jsonTempDatam = array();
    $jsonTempDatam['id_marca'] = 0;
    $jsonTempDatam['des_marca'] = 0;
    $jsonTempDatam['det_marcas'] = 0;
    $jsonTempDatam['mar_activo'] = 0;
    $jsonTempDatam['images_id'] = 0;
    $jsonTempDatam['mar_no_cuenta'] = 0;
    $jsonDatam[] = $jsonTempDatam;

}

$json->marcas = $jsonDatam;

$montoesql1fb = " select              fabricantes.fab_id
					,fabricantes.des_fabricante
					,fabricantes.pa_id
					,fabricantes.detalle_fabricante
					,fabricantes.fab_activo


					 from    " . $_SESSION['database'] . ".productos

left outer join   " . $_SESSION['database'] . "_tmp_pack.productos_pack on productos.pr_id=  " . $_SESSION['database'] . "_tmp_pack.productos_pack.pr_id

left outer join   " . $_SESSION['database'] . ".fabricantes on  " . $_SESSION['database'] . ".productos.fab_id=  " . $_SESSION['database'] . ".fabricantes.fab_id


where id_pack='" . $_SESSION['id_pack'] . "'  group by fab_id;";

logs($montoesql1fb);
$qur1fb = $connection->query($montoesql1fb);

$i = 0;
$jsonTempDatafb = array();
while ($r1 = mysqli_fetch_assoc($qur1fb)) {

    $jsonTempDatafb = array();
    $jsonTempDatafb['fab_id'] = utf8_encode($r1['fab_id']);
    $jsonTempDatafb['des_fabricante'] = utf8_encode($r1['des_fabricante']);
    $jsonTempDatafb['pa_id'] = utf8_encode($r1['pa_id']);
    $jsonTempDatafb['detalle_fabricante'] = utf8_encode($r1['detalle_fabricante']);
    $jsonTempDatafb['fab_activo'] = utf8_encode($r1['fab_activo']);

    $jsonDatafb[] = $jsonTempDatafb;

    $i = $i + 1;
}

if ($i == 0) {
    $jsonTempDatam = array();
    $jsonTempDatafb['fab_id'] = 0;
    $jsonTempDatafb['des_fabricante'] = 0;
    $jsonTempDatafb['pa_id'] = 0;
    $jsonTempDatafb['detalle_fabricante'] = 0;
    $jsonTempDatafb['fab_activo'] = 0;

    $jsonDatafb[] = $jsonTempDatafb;

}

$json->fabricantes = $jsonDatafb;

$montoesql1pm = " select  distinct prod_modif.*
				  from " . $_SESSION['database'] . "_tmp_pack.productos_pack


				  left outer join     " . $_SESSION['database'] . ".prod_modif on productos_pack.pr_Id=prod_modif.pr_id



				   where  id_pack='" . $_SESSION['id_pack'] . "' ";

logs($montoesql1pm);
$qur1pm = $connection->query($montoesql1pm);

$i = 0;
$jsonTempDatapm = array();
while ($r1 = mysqli_fetch_assoc($qur1pm)) {

    $jsonTempDatapm = array();
    $jsonTempDatapm['pr_id'] = utf8_encode($r1['pr_id']);
    $jsonTempDatapm['mod_id_auto'] = utf8_encode($r1['mod_id_auto']);
    $jsonTempDatapm['mod_min'] = utf8_encode($r1['mod_min']);
    $jsonTempDatapm['mod_max'] = utf8_encode($r1['mod_max']);
    $jsonTempDatapm['modt_precio'] = utf8_encode($r1['modt_precio']);

    $jsonTempDatapm['mod_precio'] = utf8_encode($r1['mod_precio']);
    $jsonTempDatapm['mod_orden'] = utf8_encode($r1['mod_orden']);
    $jsonTempDatapm['mod_unic1'] = utf8_encode($r1['mod_unic1']);

    $jsonTempDatapm['ocultar'] = utf8_encode($r1['ocultar']);
    $jsonTempDatapm['orden'] = utf8_encode($r1['orden']);
    $jsonTempDatapm['tipo_prod'] = utf8_encode($r1['tipo_prod']);
    $jsonTempDatapm['mod_ref_descr'] = utf8_encode($r1['mod_ref_descr']);
    $jsonTempDatapm['mod_cant'] = utf8_encode($r1['mod_cant']);

    $jsonDatapm[] = $jsonTempDatapm;

    $i = $i + 1;
}

if ($i == 0) {
    $jsonTempDatapm = array();
    $jsonTempDatapm['pr_id'] = 0;
    $jsonTempDatapm['mod_id_auto'] = 0;
    $jsonTempDatapm['mod_min'] = 0;
    $jsonTempDatapm['mod_max'] = 0;
    $jsonTempDatapm['modt_precio'] = 0;

    $jsonTempDatapm['mod_precio'] = 0;
    $jsonTempDatapm['mod_orden'] = 0;
    $jsonTempDatapm['mod_unic1'] = 0;

    $jsonTempDatapm['ocultar'] = 0;
    $jsonTempDatapm['orden'] = 0;
    $jsonTempDatapm['tipo_prod'] = 0;
    $jsonTempDatapm['mod_ref_descr'] = 0;
    $jsonTempDatapm['mod_cant'] = 0;

    $jsonDatapm[] = $jsonTempDatapm;

}

$json->prod_modif = $jsonDatapm;

$montoesql1mp = " select  distinct modif_prod.*
				  from  " . $_SESSION['database'] . "_tmp_pack.productos_pack

				  left outer join      " . $_SESSION['database'] . ".prod_modif on productos_pack.pr_Id=prod_modif.pr_id

                  left outer join      " . $_SESSION['database'] . ".modif_prod on   " . $_SESSION['database'] . ".prod_modif.mod_id_auto=modif_prod.mod_cod


				   where  id_pack='" . $_SESSION['id_pack'] . "' ";

logs($montoesql1mp);
$qur1mp = $connection->query($montoesql1mp);

$i = 0;
$jsonTempDatamp = array();
while ($r1 = mysqli_fetch_assoc($qur1mp)) {

    $jsonTempDatamp = array();
    $jsonTempDatamp['mod_cod'] = utf8_encode($r1['mod_cod']);
    $jsonTempDatamp['mod_descripcion'] = utf8_encode($r1['mod_descripcion']);
    $jsonTempDatamp['mod_detalle'] = utf8_encode($r1['mod_detalle']);
    $jsonTempDatamp['mod_activo'] = utf8_encode($r1['mod_activo']);
    $jsonTempDatamp['w_btcolor'] = utf8_encode($r1['w_btcolor']);

    $jsonTempDatamp['w_bt_prior'] = utf8_encode($r1['w_bt_prior']);
    $jsonTempDatamp['mod_ins_auto'] = utf8_encode($r1['mod_ins_auto']);

    $jsonDatamp[] = $jsonTempDatamp;

    $i = $i + 1;
}

if ($i == 0) {
    $jsonTempDatamp = array();
    $jsonTempDatamp['mod_cod'] = 0;
    $jsonTempDatamp['mod_descripcion'] = 0;
    $jsonTempDatamp['mod_detalle'] = 0;
    $jsonTempDatamp['mod_activo'] = 0;
    $jsonTempDatamp['w_btcolor'] = 0;

    $jsonTempDatamp['w_bt_prior'] = 0;
    $jsonTempDatamp['mod_ins_auto'] = 0;

    $jsonDatamp[] = $jsonTempDatamp;

}

$json->modif_prod = $jsonDatamp;

$montoesql1mm = " select  distinct modif_match.*
				  from  " . $_SESSION['database'] . "_tmp_pack.productos_pack

				  left outer join      " . $_SESSION['database'] . ".prod_modif on productos_pack.pr_Id=prod_modif.pr_id

                  left outer join      " . $_SESSION['database'] . ".modif_match on   " . $_SESSION['database'] . ".modif_match.mod_id_auto=prod_modif.mod_id_auto



				   where  id_pack='" . $_SESSION['id_pack'] . "' ";

logs($montoesql1mm);
$qur1mm = $connection->query($montoesql1mm);

$i = 0;
$jsonTempDatamm = array();
while ($r1 = mysqli_fetch_assoc($qur1mm)) {

    $jsonTempDatamm = array();
    $jsonTempDatamm['mod_id_auto'] = utf8_encode($r1['mod_id_auto']);
    $jsonTempDatamm['pr_id'] = utf8_encode($r1['pr_id']);
    $jsonTempDatamm['modt_precio'] = utf8_encode($r1['modt_precio']);
    $jsonTempDatamm['mod_precio'] = utf8_encode($r1['mod_precio']);
    $jsonTempDatamm['mod_itbms'] = utf8_encode($r1['mod_itbms']);

    $jsonTempDatamm['mod_orden'] = utf8_encode($r1['mod_orden']);
    $jsonTempDatamm['mod_id_unico'] = utf8_encode($r1['mod_id_unico']);

    $jsonDatamm[] = $jsonTempDatamm;

    $i = $i + 1;
}

if ($i == 0) {

    $jsonTempDatamm = array();
    $jsonTempDatamm['mod_id_auto'] = 0;
    $jsonTempDatamm['pr_id'] = 0;
    $jsonTempDatamm['modt_precio'] = 0;
    $jsonTempDatamm['mod_precio'] = 0;
    $jsonTempDatamm['mod_itbms'] = 0;

    $jsonTempDatamm['mod_orden'] = 0;
    $jsonTempDatamm['mod_id_unico'] = 0;

    $jsonDatamm[] = $jsonTempDatamm;

}

$json->modif_match = $jsonDatamm;

//$_SESSION['database'] = $arg_1;

$hoynow = getdate();

$timeStamp = $hoynow["year"] . "-" . $hoynow["mon"] . "-" . $hoynow["mday"] . " " . $hoynow["hours"] . ":" . $hoynow["minutes"] . ":" . $hoynow["seconds"];

$timeStamp2 = $hoynow["year"] . "-" . $hoynow["mon"] . "-01 00:00:01";
//echo $timeStamp;

$SQLflags = "select idflag from " . $_SESSION['database'] . ".flag  limit 1";

//    logs($SQLflags);
$qur = $connection->query($SQLflags);

$_SESSION['flags'] = "9";
while ($r = mysqli_fetch_assoc($qur)) {

    $_SESSION['flags'] = $r['idflag'];

//logs("Esta es la banderaaaaaaaaaaaaaaaaaaaaaaa ".$_SESSION['flags']);

}

if ($_SESSION['flags'] == "0") {

    $SQLupdate44 = "UPDATE " . $_SESSION['database'] . ".flag SET idflag=1 ";

    if ($connection->query($SQLupdate44) === true) {

        logs("Cambio la Bandera a 1 " . $_SESSION['est_id']);

    } else {
        echo "Error: " . $SQLupdate4 . "<br>" . $conn->error;
    }

    $montoesql3 = "select  " . $_SESSION['database'] . ".productos_det.pr_id as pr_id from " . $_SESSION['database'] . ".productos_det where usermodify=1  limit 1";

//echo  $montoesql;
    //logs($montoesql);

    $qur = $connection->query($montoesql3);

    $_SESSION['pr_id'] = "NO";

    while ($r = mysqli_fetch_assoc($qur)) {

        $_SESSION['pr_id'] = $r['pr_id'];
    }

    if ($_SESSION['pr_id'] != "NO") {

        $SQLupdate = "UPDATE   " . $_SESSION['database'] . ".productos_det SET syncprocesing=1,pr_id_unico = CONCAT(suc_id, '-',pr_id) WHERE  usermodify=1  limit 200";

//echo $SQLupdate;

        logs("uuuuuuuuuuuuuuuuuuuuuuuuuuu" . $SQLupdate);

        if ($connection->query($SQLupdate) === true) {
            //  echo "update op";
        } else {
            echo "Error: " . $SQLupdate . "<br>" . $conn->error;
        }

        if ($_SESSION['est_id'] == "0") {

            $getData = "select suc_corre_r+1 as suc_corre_r,suc_corre_e+1 as corre_e,suc_id FROM " . $_SESSION['database'] . ".sucursales  where   suc_id=1";
        } else {

            $getData = "select e_corre_r+1 as e_corre_r,e_corre_e+1 as corre_e,e_id FROM " . $_SESSION['database'] . ".estaciones  where   e_id=1";
        }

        logs("ccccccccccccccccccccccccccccccc" . $getData);

//$getData = "select * from user";
        $qur = $connection->query($getData);

        $_SESSION['corre'] = 0;

        while ($r = mysqli_fetch_assoc($qur)) {

            $_SESSION['corre'] = $r['corre_e'];
        }

        if ($_SESSION['corre'] > 0) {

            $SQLupdate2 = "  insert into  " . $_SESSION['database'] . "_tmp_pack.productos_pack  (id_pack,pr_id) select   " . $_SESSION['corre'] . "  as id,
			      	productos_det.pr_id


				  from
				 " . $_SESSION['database'] . ".productos_det


				where productos_det.syncprocesing=1 ";

            logs($SQLupdate2);

            if ($connection->query($SQLupdate2) === true) {

                $SQLupdate2a = "insert into  " . $_SESSION['database'] . "_tmp_pack.productos_com_pack select   " . $_SESSION['corre'] . "  as id
	                       ,comp_id,compuestos.pr_id,cant_equi,compuestos.pr_costo,pr_costo_equi,compuestos.suc_id FROM " . $_SESSION['database'] . ".productos_det

                           left outer join " . $_SESSION['database'] . ".compuestos on productos_det.pr_id=compuestos.comp_id where productos_det.syncprocesing=1 and comp_id is not null  ";

                logs($SQLupdate2a);

                if ($connection->query($SQLupdate2a) === true) {}
                ;

                //  echo "update op";

                $SQLupdate3 = "select id_pack from " . $_SESSION['database'] . "_tmp_pack.productos_pack where  id_pack=" . $_SESSION['corre'] . " limit 1";

                logs('xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' . $SQLupdate3);
                $qur = $connection->query($SQLupdate3);

                $empaco = "NO";
                while ($r = mysqli_fetch_assoc($qur)) {

                    $empaco = $r['id_pack'];

                    logs("si consiguio un registro  " . $empaco);

                }

                if ($_SESSION['corre'] == $empaco) {

                    logs("   empaco" . $empaco . " corre  " . $_SESSION['corre']);

                    $SQLupdate4 = "UPDATE " . $_SESSION['database'] . ".productos_det SET syncprocesing=0,usermodify=0 WHERE   syncprocesing=1 ";

                    if ($connection->query($SQLupdate4) === true) {

                        //$empaco=$empaco+1;

                        if ($_SESSION['est_id'] == "0") {
                            $connection->query("update " . $_SESSION['database'] . ".sucursales set suc_corre_e=suc_corre_e+1  ");
                        } else {
                            //      $connection->query("update ".$_SESSION['database'].".estaciones set e_corre_e=e_corre_e+1  ");

                            $connection->query("update " . $_SESSION['database'] . ".estaciones set e_corre_e=" . $empaco . "");

                            logs("xxxxxxxxxxxxxxxxxxxxxxxxxxxxx update " . $_SESSION['database'] . ".estaciones set e_corre_e=" . $empaco . "");

                        }
                        //     echo "Empacado correctamente: ".$_SESSION['corre'];

                    } else {
                        echo "Error: " . $SQLupdate4 . "<br>" . $conn->error;
                    }

                } else {
                    echo "Errorde empaque: " . $_SESSION['corre'] . " " . $empaco . "<br>";
                }

            }

        } else {

            //echo "error en busca de corre";

        }

    }

    $SQLupdate44 = "UPDATE " . $_SESSION['database'] . ".flag SET idflag=0 ";

    if ($connection->query($SQLupdate44) === true) {

        logs("Cambio la Bandera a 0 " . $_SESSION['est_id']);

    } else {
        echo "Error: " . $SQLupdate4 . "<br>" . $conn->error;
    }

} else {

    logs("Esta ocupado por la estacionnnnnnnn " . $_SESSION['est_id'] . " Bandera " . $_SESSION['flags']);

}

header('content-type: application/json');
print_r(json_encode($json));
// echo json_encode($json);
//header('content-type: application/text_plain');
//header('content-type: application/json');

//empacando($_SESSION['database']);

$qur = null;
@mysqli_close($connection);
?>