<?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");

mysqli_set_charset($connection, "utf8");

date_default_timezone_set("America/Panama");

$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;
}

/*
$jsonFalse='{
    "registros": 0,
    "productos": null,
    "productos_com": null,
    "productos_suc": null,
    "productos_asoc": null,
    "prod_modif": null,
    "prod_gmodif": null,
    "modif_prod": null,
    "grup_modif": null,
    "modif_match": null,
    "grup_modif_macth": null,
    "productos_upd": null,
    "productos_sus": null
}';

echo $jsonFalse;


@mysqli_close($connection);

exit;
*/

$montoesql1 = "select   id_pack,
			      	productos.pr_id
					,productos.pr_descripcion
					,productos.pr_id_familia
					,productos.pr_referencia
					,productos.fab_id
					,productos.id_marca
					,LEFT(productos.pr_nro_parte, 30) as 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
					,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
					,marcas.des_marca
					,marcas.DET_MARCAS
					,marcas.mar_activo
					,fabricantes.des_fabricante
					,fabricantes.detalle_fabricante
					,fabricantes.fab_activo
					,fabricantes.Fab_cod

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

                	,familia.w_btcolor as w_btcolorf
                	,familia.w_fncolor as w_fncolorf
                	,familia.w_bt_prior as w_bt_priorf
                	,familia.images_id as images_idf

                		,productos.pr_unids_emp
		                ,productos.pr_unids_acc
		                ,'2' as pr_unids_prec

                        ,productos.e_commerce
                        ,productos.sub_cat_1
                        ,productos.sub_cat_2
                        ,productos.sub_cat_3
                        ,productos.referencia2
                        ,productos.referencia3
                        ,productos.referencia4
                        ,productos.referencia5
                        ,productos.principal_ecommer
                        ,productos.e_commerce_promo
                        ,ifnull(productos.producto_hijo,0) as producto_hijo
                        ,productos.band_grupo,unidad_cpbs, cod_cpbs_cmp, cod_cpbs_abr



				  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

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


                
				   where  id_pack='" . $_SESSION['id_pack'] . "' and ( productos_det.suc_id= '" . $_SESSION['suc_id'] . "') limit 100 ";

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

//echo $montoesql1;

$i = 0;

//  $jsonData[] ='';

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

    $jsonTempData = array();
    $jsonTempData['pr_id'] = utf8_encode($r1['pr_id']);
    $jsonTempData['pr_descripcion'] = ($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'] = ($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['pr_venta1'] = utf8_encode($r1['pr_venta1']);
    $jsonTempData['pr_venta2'] = utf8_encode($r1['pr_venta2']);
    $jsonTempData['pr_venta3'] = utf8_encode($r1['pr_venta3']);
    $jsonTempData['pr_util1'] = utf8_encode($r1['pr_util1']);
    $jsonTempData['pr_util2'] = utf8_encode($r1['pr_util2']);
    $jsonTempData['pr_util3'] = utf8_encode($r1['pr_util3']);
    $jsonTempData['pr_imp1'] = utf8_encode($r1['pr_imp1']);
    $jsonTempData['pr_imp2'] = utf8_encode($r1['pr_imp2']);
    $jsonTempData['pr_imp3'] = utf8_encode($r1['pr_imp3']);
    $jsonTempData['pr_itbms'] = utf8_encode($r1['pr_itbms']);
    $jsonTempData['pr_costo'] = utf8_encode($r1['pr_costo']);
    $jsonTempData['pr_costo_actual'] = utf8_encode($r1['pr_costo_actual']);
    $jsonTempData['pr_costo_prom'] = utf8_encode($r1['pr_costo_prom']);
    $jsonTempData['pr_activo'] = utf8_encode($r1['pr_activo']);
    $jsonTempData['pr_margen1'] = utf8_encode($r1['pr_margen1']);
    $jsonTempData['pr_margen2'] = utf8_encode($r1['pr_margen2']);
    $jsonTempData['pr_margen3'] = utf8_encode($r1['pr_margen3']);
    $jsonTempData['pr_precio1_imp'] = utf8_encode($r1['pr_precio1_imp']);
    $jsonTempData['pr_precio2_imp'] = utf8_encode($r1['pr_precio2_imp']);
    $jsonTempData['pr_precio3_imp'] = utf8_encode($r1['pr_precio3_imp']);
    $jsonTempData['pr_exento'] = utf8_encode($r1['pr_exento']);
    $jsonTempData['pr_nivel1'] = utf8_encode($r1['pr_nivel1']);
    $jsonTempData['pr_nivel2'] = utf8_encode($r1['pr_nivel2']);
    $jsonTempData['pr_nivel3'] = utf8_encode($r1['pr_nivel3']);
    $jsonTempData['pr_activa_desemp'] = utf8_encode($r1['pr_activa_desemp']);
    $jsonTempData['pr_man_seriales'] = utf8_encode($r1['pr_man_seriales']);
    $jsonTempData['pr_des_tecla'] = ($r1['pr_des_tecla']);
    $jsonTempData['pr_minimo'] = utf8_encode($r1['pr_minimo']);
    $jsonTempData['pr_maximo'] = utf8_encode($r1['pr_maximo']);
    $jsonTempData['pr_sugerido'] = utf8_encode($r1['pr_sugerido']);
    $jsonTempData['pr_garantia'] = utf8_encode($r1['pr_garantia']);
    $jsonTempData['pr_condiciones'] = utf8_encode($r1['pr_condiciones']);
    $jsonTempData['pr_creacion'] = utf8_encode($r1['pr_creacion']);
    $jsonTempData['pr_modificacion'] = utf8_encode($r1['pr_modificacion']);
    $jsonTempData['pr_u_id_crea'] = utf8_encode($r1['pr_u_id_crea']);
    $jsonTempData['pr_u_id_mod'] = utf8_encode($r1['pr_u_id_mod']);
    $jsonTempData['ubic_id'] = utf8_encode($r1['ubic_id']);
    $jsonTempData['ubic_id2'] = utf8_encode($r1['ubic_id2']);
    $jsonTempData['ubic_id3'] = utf8_encode($r1['ubic_id3']);
    $jsonTempData['ubic_id4'] = utf8_encode($r1['ubic_id4']);
    $jsonTempData['pr_comisionv'] = utf8_encode($r1['pr_comisionv']);
    $jsonTempData['pr_comisionc'] = utf8_encode($r1['pr_comisionc']);
    $jsonTempData['pr_tipo_comision'] = utf8_encode($r1['pr_tipo_comision']);
    $jsonTempData['pr_comision_prior'] = utf8_encode($r1['pr_comision_prior']);
    $jsonTempData['pr_man_exist'] = utf8_encode($r1['pr_man_exist']);
    $jsonTempData['pr_man_lote'] = utf8_encode($r1['pr_man_lote']);
    $jsonTempData['suc_id'] = utf8_encode($r1['suc_id']);
    $jsonTempData['pr_imp_adic'] = utf8_encode($r1['pr_imp_adic']);
    $jsonTempData['pr_imp1_adic'] = utf8_encode($r1['pr_imp1_adic']);
    $jsonTempData['pr_imp2_adic'] = utf8_encode($r1['pr_imp2_adic']);
    $jsonTempData['pr_imp3_adic'] = utf8_encode($r1['pr_imp3_adic']);
    $jsonTempData['pr_precio_libre'] = utf8_encode($r1['pr_precio_libre']);
    $jsonTempData['pr_gmodif'] = utf8_encode($r1['pr_gmodif']);
    $jsonTempData['pr_show_pres'] = utf8_encode($r1['pr_show_pres']);
    $jsonTempData['pr_venta4'] = utf8_encode($r1['pr_venta4']);
    $jsonTempData['pr_oferta'] = utf8_encode($r1['pr_oferta']);
    $jsonTempData['pr_certif'] = utf8_encode($r1['pr_certif']);
    $jsonTempData['w_activar'] = utf8_encode($r1['w_activar']);
    $jsonTempData['pr_venta5'] = utf8_encode($r1['pr_venta5']);
    $jsonTempData['pr_venta6'] = utf8_encode($r1['pr_venta6']);
    $jsonTempData['pr_venta7'] = utf8_encode($r1['pr_venta7']);
    $jsonTempData['pr_util5'] = utf8_encode($r1['pr_util5']);
    $jsonTempData['pr_util6'] = utf8_encode($r1['pr_util6']);
    $jsonTempData['pr_util7'] = utf8_encode($r1['pr_util7']);
    $jsonTempData['pr_margen5'] = utf8_encode($r1['pr_margen5']);
    $jsonTempData['pr_margen6'] = utf8_encode($r1['pr_margen6']);
    $jsonTempData['pr_margen7'] = utf8_encode($r1['pr_margen7']);
    $jsonTempData['pr_precio5_imp'] = utf8_encode($r1['pr_precio5_imp']);
    $jsonTempData['pr_precio6_imp'] = utf8_encode($r1['pr_precio6_imp']);
    $jsonTempData['pr_precio7_imp'] = utf8_encode($r1['pr_precio7_imp']);
    $jsonTempData['pr_imp5'] = utf8_encode($r1['pr_imp5']);
    $jsonTempData['pr_imp6'] = utf8_encode($r1['pr_imp6']);
    $jsonTempData['pr_imp7'] = utf8_encode($r1['pr_imp7']);
    $jsonTempData['pr_imp5_adic'] = utf8_encode($r1['pr_imp5_adic']);
    $jsonTempData['pr_imp6_adic'] = utf8_encode($r1['pr_imp6_adic']);
    $jsonTempData['pr_imp7_adic'] = utf8_encode($r1['pr_imp7_adic']);
    $jsonTempData['pr_costo2'] = utf8_encode($r1['pr_costo2']);
    $jsonTempData['pr_costo_actual2'] = utf8_encode($r1['pr_costo_actual2']);
    $jsonTempData['pr_costo_prom2'] = utf8_encode($r1['pr_costo_prom2']);
    $jsonTempData['pr_costo3'] = utf8_encode($r1['pr_costo3']);
    $jsonTempData['pr_costo_actual3'] = utf8_encode($r1['pr_costo_actual3']);
    $jsonTempData['pr_costo_prom3'] = utf8_encode($r1['pr_costo_prom3']);
    $jsonTempData['pr_venta1_moned'] = utf8_encode($r1['pr_venta1_moned']);
    $jsonTempData['pr_venta2_moned'] = utf8_encode($r1['pr_venta2_moned']);
    $jsonTempData['pr_venta3_moned'] = utf8_encode($r1['pr_venta3_moned']);
    $jsonTempData['pr_venta5_moned'] = utf8_encode($r1['pr_venta5_moned']);
    $jsonTempData['pr_venta6_moned'] = utf8_encode($r1['pr_venta6_moned']);
    $jsonTempData['pr_venta7_moned'] = utf8_encode($r1['pr_venta7_moned']);
    $jsonTempData['mon_simb1'] = utf8_encode($r1['mon_simb1']);
    $jsonTempData['mon_simb2'] = utf8_encode($r1['mon_simb2']);
    $jsonTempData['mon_simb3'] = utf8_encode($r1['mon_simb3']);
    $jsonTempData['mon_simb5'] = utf8_encode($r1['mon_simb5']);
    $jsonTempData['mon_simb6'] = utf8_encode($r1['mon_simb6']);
    $jsonTempData['mon_simb7'] = utf8_encode($r1['mon_simb7']);
    $jsonTempData['pr_costo_bk'] = utf8_encode($r1['pr_costo_bk']);
    $jsonTempData['pr_costo_actual_bk'] = utf8_encode($r1['pr_costo_actual_bk']);
    $jsonTempData['pr_costo_prom_bk'] = utf8_encode($r1['pr_costo_prom_bk']);
    $jsonTempData['SyncProcesing'] = utf8_encode($r1['SyncProcesing']);
    $jsonTempData['UserModify'] = utf8_encode($r1['UserModify']);
    $jsonTempData['UserOption'] = utf8_encode($r1['UserOption']);
    $jsonTempData['pr_id_unico'] = utf8_encode($r1['pr_id_unico']);
    $jsonTempData['f_id'] = utf8_encode($r1['f_id']);
    $jsonTempData['f_descripcion'] = ($r1['f_descripcion']);
    $jsonTempData['f_activo'] = utf8_encode($r1['f_activo']);
    $jsonTempData['F_des_tecla'] = utf8_encode($r1['F_des_tecla']);
    $jsonTempData['f_detalle'] = utf8_encode($r1['f_detalle']);
    $jsonTempData['fam_imp'] = utf8_encode($r1['fam_imp']);
    $jsonTempData['f_ubic_id'] = utf8_encode($r1['f_ubic_id']);
    $jsonTempData['f_ubic_id2'] = utf8_encode($r1['f_ubic_id2']);
    $jsonTempData['f_ubic_id3'] = utf8_encode($r1['f_ubic_id3']);
    $jsonTempData['f_ubic_id4'] = utf8_encode($r1['f_ubic_id4']);
    $jsonTempData['f_no_cuenta'] = utf8_encode($r1['f_no_cuenta']);
    $jsonTempData['f_kds_prior'] = utf8_encode($r1['f_kds_prior']);
    $jsonTempData['des_marca'] = ($r1['des_marca']);
    $jsonTempData['DET_MARCAS'] = utf8_encode($r1['DET_MARCAS']);
    $jsonTempData['mar_activo'] = utf8_encode($r1['mar_activo']);
    $jsonTempData['des_fabricante'] = ($r1['des_fabricante']);
    $jsonTempData['detalle_fabricante'] = utf8_encode($r1['detalle_fabricante']);
    $jsonTempData['fab_activo'] = utf8_encode($r1['fab_activo']);
    $jsonTempData['Fab_cod'] = utf8_encode($r1['Fab_cod']);
    $jsonTempData['id_pack'] = utf8_encode($r1['id_pack']);
    $jsonTempData['resting_desc'] = utf8_encode($r1['resting_desc']);
    $jsonTempData['pr_food_stamp'] = utf8_encode($r1['pr_food_stamp']);

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

    //if ($images==""){$images=0};

    $jsonTempData['images_id'] = $images;

    $jsonTempData['w_btcolorf'] = utf8_encode($r1['w_btcolorf']);
    $jsonTempData['w_fncolorf'] = utf8_encode($r1['w_fncolorf']);
    $jsonTempData['w_bt_priorf'] = utf8_encode($r1['w_bt_priorf']);
    $jsonTempData['images_idf'] = utf8_encode($r1['images_idf']);

    $jsonTempData['pr_unids_emp'] = utf8_encode($r1['pr_unids_emp']);
    $jsonTempData['pr_unids_acc'] = utf8_encode($r1['pr_unids_acc']);

    $jsonTempData['pr_unids_prec'] = utf8_encode($r1['pr_unids_prec']);

    $jsonTempData['e_commerce'] = utf8_encode($r1['e_commerce']);
    $jsonTempData['sub_cat_1'] = utf8_encode($r1['sub_cat_1']);
    $jsonTempData['sub_cat_2'] = utf8_encode($r1['sub_cat_2']);
    $jsonTempData['sub_cat_3'] = utf8_encode($r1['sub_cat_3']);
    $jsonTempData['referencia2'] = utf8_encode($r1['referencia2']);
    $jsonTempData['referencia3'] = utf8_encode($r1['referencia3']);
    $jsonTempData['referencia4'] = utf8_encode($r1['referencia4']);
    $jsonTempData['referencia5'] = utf8_encode($r1['referencia5']);
    $jsonTempData['principal_ecommer'] = utf8_encode($r1['principal_ecommer']);
    $jsonTempData['e_commerce_promo'] = utf8_encode($r1['e_commerce_promo']);
    $jsonTempData['producto_hijo'] = utf8_encode($r1['producto_hijo']);
    $jsonTempData['band_grupo'] = utf8_encode($r1['band_grupo']);
    
    $jsonTempData['unidad_cpbs'] = utf8_encode($r1['unidad_cpbs']);

    $jsonTempData['cod_cpbs_cmp'] = utf8_encode($r1['cod_cpbs_cmp']);

    $jsonTempData['cod_cpbs_abr'] = utf8_encode($r1['cod_cpbs_abr']);

    $jsonData[] = $jsonTempData;

    $i = $i + 1;
}

$json->registros = $i;

$_SESSION['productos_cant'] = $i;

$json->productos = $jsonData;

$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['id_pack'] = utf8_encode($r1['id_pack']);

    $jsonTempDataa['pr_com_unico'] = utf8_encode($r1['pr_com_unico']);
    $jsonTempDataa['flag_borra'] = utf8_encode($r1['flag_borra']);

    $jsonDataa[] = $jsonTempDataa;

    $i = $i + 1;
}

if ($_SESSION['productos_cant'] > 0) {
    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['id_pack'] = 0;
        $jsonTempDataa['pr_com_unico'] = 0;
        $jsonTempDataa['flag_borra'] = 0;

        $jsonDataa[] = $jsonTempDataa;

    }
}

$json->productos_com = $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['id_pack'] = utf8_encode($r1['id_pack']);
    

    $jsonDatab[] = $jsonTempDatab;

    $i = $i + 1;
}

if ($_SESSION['productos_cant'] > 0) {
    if ($i == 0) {
        $jsonTempDatab = array();
        $jsonTempDatab['suc_id'] = 0;
        $jsonTempDatab['pr_id'] = 0;
        $jsonTempDatab['pr_activo'] = 0;
        $jsonTempDatab['id_pack'] = 0;
        $jsonDatab[] = $jsonTempDatab;

    }}

$json->productos_suc = $jsonDatab;

$montoesql1c = " select pr_id_asoc,prod_asoc.pr_id,id_pack,pr_cant,pr_asoc_unico 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'] . "'  group by  " . $_SESSION['database'] . ".prod_asoc.pr_id," . $_SESSION['database'] . ".prod_asoc.pr_id,pr_asoc_unico  ";

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['id_pack'] = utf8_encode($r1['id_pack']);
    $jsonTempDatac['pr_cant'] = utf8_encode($r1['pr_cant']);
    $jsonTempDatac['pr_asoc_unico'] = utf8_encode($r1['pr_asoc_unico']);

    $jsonDatac[] = $jsonTempDatac;

    $i = $i + 1;
}

if ($_SESSION['productos_cant'] > 0) {
    if ($i == 0) {
        $jsonTempDatac = array();
        $jsonTempDatac['pr_id_asoc'] = 0;
        $jsonTempDatac['pr_id'] = 0;
        $jsonTempDatac['id_pack'] = 0;
        $jsonTempDatac['pr_cant'] = 0;
        $jsonTempDatac['pr_asoc_unico'] = 0;

        $jsonDatac[] = $jsonTempDatac;

    }}

$json->productos_asoc = $jsonDatac;

$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'] . "' and    " . $_SESSION['database'] . ".prod_modif.pr_id is not null ";

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']);
    $jsonTempDatapm['id_pack'] = $_SESSION['id_pack'];
    $jsonTempDatapm['mod_del'] = utf8_encode($r1['mod_del']);
    $jsonTempDatapm['gmod_cod'] = utf8_encode($r1['gmod_cod']);

    $jsonDatapm[] = $jsonTempDatapm;

    $i = $i + 1;
}

if ($_SESSION['productos_cant'] > 0) {
    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;
        $jsonTempDatapm['id_pack'] = 0;
        $jsonTempDatapm['mod_del'] = 0;
        $jsonTempDatapm['gmod_cod'] =0;
        $jsonDatapm[] = $jsonTempDatapm;

    }}

$json->prod_modif = $jsonDatapm;

//////

$montoesql1pmg = " select  distinct prod_gmodif.pr_id,
                                  prod_gmodif.gmod_id_auto,
                                  prod_gmodif.mod_min,
                                  prod_gmodif.mod_max,
                                  prod_gmodif.modt_precio,
                                  prod_gmodif.mod_precio,
                                  ifnull(prod_gmodif.gmod_orden,0) as gmod_orden,
                                  prod_gmodif.gmod_unic3,
                                  prod_gmodif.tipo_prod,
                                  prod_gmodif.mod_del,
                                  id_pack
				  from " . $_SESSION['database'] . "_tmp_pack.productos_pack


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



				   where  id_pack='" . $_SESSION['id_pack'] . "' and    " . $_SESSION['database'] . ".prod_gmodif.pr_id is not null ";

logs($montoesql1pmg);
$qur1pmg = $connection->query($montoesql1pmg);

$i = 0;
$jsonTempDatapmg = array();
while ($r1 = mysqli_fetch_assoc($qur1pmg)) {

    $jsonTempDatapmg = array();
    $jsonTempDatapmg['pr_id'] = utf8_encode($r1['pr_id']);
    $jsonTempDatapmg['gmod_id_auto'] = utf8_encode($r1['gmod_id_auto']);
    $jsonTempDatapmg['mod_min'] = utf8_encode($r1['mod_min']);
    $jsonTempDatapmg['mod_max'] = utf8_encode($r1['mod_max']);
    $jsonTempDatapmg['modt_precio'] = utf8_encode($r1['modt_precio']);

    $jsonTempDatapmg['mod_precio'] = utf8_encode($r1['mod_precio']);
    $jsonTempDatapmg['gmod_orden'] = utf8_encode($r1['gmod_orden']);
    $jsonTempDatapmg['gmod_unic3'] = utf8_encode($r1['gmod_unic3']);
    $jsonTempDatapmg['tipo_prod'] = utf8_encode($r1['tipo_prod']);
    $jsonTempDatapmg['mod_del'] = utf8_encode($r1['mod_del']);
    $jsonTempDatapmg['id_pack'] = $_SESSION['id_pack'];

    $jsonDatapmg[] = $jsonTempDatapmg;

    $i = $i + 1;
}

if ($_SESSION['productos_cant'] > 0) {
    if ($i == 0) {
        $jsonTempDatapmg = array();
        $jsonTempDatapmg['pr_id'] = 0;
        $jsonTempDatapmg['gmod_id_auto'] = 0;
        $jsonTempDatapmg['mod_min'] = 0;
        $jsonTempDatapmg['mod_max'] = 0;
        $jsonTempDatapmg['modt_precio'] = 0;

        $jsonTempDatapmg['mod_precio'] = 0;
        $jsonTempDatapmg['gmod_orden'] = 0;
        $jsonTempDatapmg['mod_unic1'] = 0;
        $jsonTempDatapmg['mod_del'] = 0;

        $jsonTempDatapmg['id_pack'] = 0;

        $jsonDatapmg[] = $jsonTempDatapmg;

    }}

$json->prod_gmodif = $jsonDatapmg;

/////

$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'] . "' and   " . $_SESSION['database'] . ".modif_prod.mod_cod is not null  ";

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']);
    $jsonTempDatamp['id_pack'] = $_SESSION['id_pack'];

    $jsonDatamp[] = $jsonTempDatamp;

    $i = $i + 1;
}
if ($_SESSION['productos_cant'] > 0) {

    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;
        $jsonTempDatamp['id_pack'] = 0;

        $jsonDatamp[] = $jsonTempDatamp;

    }}

$json->modif_prod = $jsonDatamp;

////

$montoesql1mpg = " select  distinct grup_modif.*
				  from  " . $_SESSION['database'] . "_tmp_pack.productos_pack

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

                  left outer join      " . $_SESSION['database'] . ".grup_modif on   " . $_SESSION['database'] . ".grup_modif.gmod_cod=prod_gmodif.gmod_id_auto



				   where  id_pack='" . $_SESSION['id_pack'] . "' and " . $_SESSION['database'] . ".grup_modif.gmod_cod is not null



                   ";

logs($montoesql1mpg);
$qur1mpg = $connection->query($montoesql1mpg);

$i = 0;
$jsonTempDatampg = array();
while ($r1 = mysqli_fetch_assoc($qur1mpg)) {

    $jsonTempDatampg = array();
    $jsonTempDatampg['gmod_cod'] = utf8_encode($r1['gmod_cod']);
    $jsonTempDatampg['gmod_descripcion'] = utf8_encode($r1['gmod_descripcion']);
    $jsonTempDatampg['gmod_detalle'] = utf8_encode($r1['gmod_detalle']);

    $jsonTempDatampg['gmod_increm'] = utf8_encode($r1['gmod_increm']);
    $jsonTempDatampg['gmod_del'] = utf8_encode($r1['gmod_del']);

    $jsonTempDatampg['gmod_activo'] = utf8_encode($r1['gmod_activo']);
    $jsonTempDatampg['w_btcolor'] = utf8_encode($r1['w_btcolor']);
    $jsonTempDatampg['w_bt_prior'] = utf8_encode($r1['w_bt_prior']);
    $jsonTempDatampg['id_pack'] = $_SESSION['id_pack'];

    $jsonDatampg[] = $jsonTempDatampg;

    $i = $i + 1;
}
if ($_SESSION['productos_cant'] > 0) {

    if ($i == 0) {
        $jsonTempDatampg = array();
        $jsonTempDatampg['gmod_cod'] = 0;
        $jsonTempDatampg['gmod_descripcion'] = 0;
        $jsonTempDatampg['gmod_detalle'] = 0;
        $jsonTempDatampg['gmod_increm'] = 0;

        $jsonTempDatampg['gmod_activo'] = 0;
        $jsonTempDatampg['w_btcolor'] = 0;

        $jsonTempDatampg['w_bt_prior'] = 0;
        $jsonTempDatampg['id_pack'] = 0;

        $jsonDatampg[] = $jsonTempDatampg;

    }}

$json->grup_modif = $jsonDatampg;
////

$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'] . "' and   " . $_SESSION['database'] . ".modif_match.mod_id_auto is not null";

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']);

    $jsonTempDatamm['id_pack'] = $_SESSION['id_pack'];
    $jsonTempDatamm['mod_del'] = utf8_encode($r1['mod_del']);

    $jsonDatamm[] = $jsonTempDatamm;

    $i = $i + 1;
}

if ($_SESSION['productos_cant'] > 0) {
    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;
        $jsonTempDatamm['id_pack'] = 0;
        $jsonTempDatamm['mod_del'] = 0;

        $jsonDatamm[] = $jsonTempDatamm;

    }
}

$json->modif_match = $jsonDatamm;

//////////

$montoesql1mmg = " select  distinct grup_modif_macth.*
				  from   " . $_SESSION['database'] . "_tmp_pack.productos_pack

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

                  left outer join       " . $_SESSION['database'] . ".grup_modif_macth on    " . $_SESSION['database'] . ".grup_modif_macth.gmod_id_auto=prod_gmodif.gmod_id_auto



				   where    id_pack='" . $_SESSION['id_pack'] . "' and   " . $_SESSION['database'] . ".grup_modif_macth.mod_id_auto is not null";

logs($montoesql1mmg);
$qur1mmg = $connection->query($montoesql1mmg);

$i = 0;
$jsonTempDatammg = array();
while ($r1 = mysqli_fetch_assoc($qur1mmg)) {

    $jsonTempDatammg = array();
    $jsonTempDatammg['gmod_id_auto'] = utf8_encode($r1['gmod_id_auto']);
    $jsonTempDatammg['mod_id_auto'] = utf8_encode($r1['mod_id_auto']);
    $jsonTempDatammg['gmod_orden'] = utf8_encode($r1['gmod_orden']);
    $jsonTempDatammg['gmod_precio'] = utf8_encode($r1['gmod_precio']);
    $jsonTempDatammg['gmod_unic2'] = utf8_encode($r1['gmod_unic2']);

    $jsonTempDatammg['gmod_del'] = utf8_encode($r1['gmod_del']);
    $jsonTempDatammg['id_pack'] = $_SESSION['id_pack'];

    $jsonDatammg[] = $jsonTempDatammg;

    $i = $i + 1;
}

if ($_SESSION['productos_cant'] > 0) {
    if ($i == 0) {

        $jsonTempDatammg = array();
        $jsonTempDatammg['gmod_id_auto'] = 0;
        $jsonTempDatammg['mod_id_auto'] = 0;
        $jsonTempDatammg['gmod_orden'] = 0;
        $jsonTempDatammg['gmod_precio'] = 0;
        $jsonTempDatammg['gmod_unic2'] = 0;

        $jsonTempDatammg['gmod_del'] = 0;

        $jsonDatammg[] = $jsonTempDatammg;

    }
}

$json->grup_modif_macth = $jsonDatammg;

////////

$montoesql1u = " select pr_id_asoc,prod_asoc.pr_id,id_pack,pr_cant,pr_asoc_unico 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'] . "'  group by  " . $_SESSION['database'] . ".prod_asoc.pr_id," . $_SESSION['database'] . ".prod_asoc.pr_id,pr_asoc_unico  ";

$montoesql1u = "select   productos_upd.*
				  from " . $_SESSION['database'] . "_tmp_pack.productos_pack  left outer join     " . $_SESSION['database'] . ".productos_upd on productos_pack.pr_Id=productos_upd.pr_id
				 where  id_pack='" . $_SESSION['id_pack'] . "' and    " . $_SESSION['database'] . ".productos_upd.pr_id is not null ";

logs($montoesql1u);
$qur1u = $connection->query($montoesql1u);

$i = 0;
$jsonTempDatau = array();
while ($r1 = mysqli_fetch_assoc($qur1u)) {

    $jsonTempDatau = array();
    $jsonTempDatau['pr_id'] = utf8_encode($r1['pr_id']);
    $jsonTempDatau['pr_venta1'] = utf8_encode($r1['pr_venta1']);
    $jsonTempDatau['pr_venta2'] = utf8_encode($r1['pr_venta2']);
    $jsonTempDatau['pr_venta3'] = utf8_encode($r1['pr_venta3']);
    $jsonTempDatau['pr_venta5'] = utf8_encode($r1['pr_venta5']);
    $jsonTempDatau['pr_venta6'] = utf8_encode($r1['pr_venta6']);
    $jsonTempDatau['pr_venta7'] = utf8_encode($r1['pr_venta7']);

    $jsonTempDatau['pr_venta1_old'] = utf8_encode($r1['pr_venta1_old']);
    $jsonTempDatau['pr_venta2_old'] = utf8_encode($r1['pr_venta2_old']);
    $jsonTempDatau['pr_venta3_old'] = utf8_encode($r1['pr_venta3_old']);
    $jsonTempDatau['pr_venta5_old'] = utf8_encode($r1['pr_venta5_old']);
    $jsonTempDatau['pr_venta6_old'] = utf8_encode($r1['pr_venta6_old']);
    $jsonTempDatau['pr_venta7_old'] = utf8_encode($r1['pr_venta7_old']);
    $jsonTempDatau['fecha_registro'] = utf8_encode($r1['fecha_registro']);
    $jsonTempDatau['u_id'] = utf8_encode($r1['u_id']);

    $jsonDatau[] = $jsonTempDatau;

    $i = $i + 1;
}
if ($_SESSION['productos_cant'] > 0) {

    if ($i == 0) {

        $jsonTempDatau = array();
        $jsonTempDatau['pr_id'] = 0;
        $jsonTempDatau['pr_venta1'] = 0;
        $jsonTempDatau['pr_venta2'] = 0;
        $jsonTempDatau['pr_venta3'] = 0;
        $jsonTempDatau['pr_venta5'] = 0;
        $jsonTempDatau['pr_venta6'] = 0;
        $jsonTempDatau['pr_venta7'] = 0;

        $jsonTempDatau['pr_venta1_old'] = 0;
        $jsonTempDatau['pr_venta2_old'] = 0;
        $jsonTempDatau['pr_venta3_old'] = 0;
        $jsonTempDatau['pr_venta5_old'] = 0;
        $jsonTempDatau['pr_venta6_old'] = 0;
        $jsonTempDatau['pr_venta7_old'] = 0;
        $jsonTempDatau['fecha_registro'] = 0;
        $jsonTempDatau['u_id'] = 0;

        $jsonDatau[] = $jsonTempDatau;

    }
}

$json->productos_upd = $jsonDatau;

// sustitutos

$montoesql1sus = "SELECT grp_id,pr_id,notas,flag_borrar FROM " . $_SESSION['database'] . ".productos_interc where grp_id in (select

bb.grp_id from " . $_SESSION['database'] . "_tmp_pack.productos_pack pp

left outer join " . $_SESSION['database'] . ".productos_interc bb on pp.pr_id=bb.pr_id  where  id_pack='" . $_SESSION['id_pack'] . "')";

$qur1sus = $connection->query($montoesql1sus);

$i = 0;
$jsonTempDatasus = array();

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

    $jsonTempDatasus = array();
    $jsonTempDatasus['grp_id'] = utf8_encode($r1['grp_id']);
    $jsonTempDatasus['pr_id'] = utf8_encode($r1['pr_id']);
    $jsonTempDatasus['notas'] = utf8_encode($r1['notas']);
    $jsonTempDatasus['flag_borrar'] = utf8_encode($r1['flag_borrar']);

    $jsonDatasus[] = $jsonTempDatasus;

    $i = $i + 1;
}

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

    if ($i == 0) {

        $jsonTempDatasus = array();
        $jsonTempDatasus['grp_id'] = '0';
        $jsonTempDatasus['pr_id'] = '0';
        $jsonTempDatasus['notas'] = '0';
        $jsonTempDatasus['flag_borrar'] = '0';

        $jsonDatasus[] = $jsonTempDatasus;

    }}

$json->productos_sus = $jsonDatasus;

//$_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,timestampdiff(minute ,idflagdate,now()) as dif  from " . $_SESSION['database'] . ".flag  limit 1";

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

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

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

    logs("Esta es la diferencias " . $_SESSION['dif']);

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

}

if ($_SESSION['suc_id']=="9" and $_SESSION['est_id']="1"){

if ($_SESSION['flags'] == "0" || $_SESSION['dif'] > 4) {

    $SQLupdate44 = "UPDATE " . $_SESSION['database'] . ".flag SET idflag=1,idflagdate=now() ";

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

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

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

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

//echo  $montoesql;
    logs('aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa' . $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 SET syncprocesing=1 WHERE  usermodify=1  and " . $_SESSION['database'] . ".productos.syncprocesing=0  limit 10";
        //$SQLupdate = "UPDATE   " . $_SESSION['database'] . ".productos_det SET syncprocesing=1 WHERE  usermodify=1  and " . $_SESSION['database'] . ".productos_det.syncprocesing=0 and productos_det.suc_id=1 limit 10";

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


        logs("pppppppppppppppppppppppp" . $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 and  suc_id=1";
        }

        logs("pppppppppppppppppppppppp" . $getData);

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

        $_SESSION['corre'] = 0;

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

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

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

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

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


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


				where " . $_SESSION['database'] . ".productos.syncprocesing=1  limit 10";

            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,compuestos.pr_com_unico,compuestos.flag_borra FROM " . $_SESSION['database'] . ".productos

                           left outer join " . $_SESSION['database'] . ".compuestos on productos.pr_id=compuestos.comp_id where productos.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('pppppppppppppppppppppppppppppppppppppppppppppppp' . $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 SET syncprocesing=0,usermodify=0 WHERE   syncprocesing=1  limit 10";

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

                        $sqlmax = "select max(id_pack) as id_pack from " . $_SESSION['database'] . "_tmp_pack.productos_pack limit 1";

                        $qur12 = $connection->query($sqlmax);

                        $_SESSION['empaco'] = 0;

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

                            $_SESSION['empaco'] = $r['id_pack'];
                        }

                        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=" . $_SESSION['empaco'] . "");

                            logs("pppppppppppppppppppppppp update " . $_SESSION['database'] . ".estaciones set e_corre_e=" . $_SESSION['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']);

}}else{

    logs("Esta ocupado por no ser estacionnnnnnnn " . $_SESSION['est_id'] . " Bandera " . $_SESSION['flags']);
}
    */

header('content-type: application/json;charset=utf-8');
echo json_encode($json, JSON_UNESCAPED_UNICODE);
// echo json_encode($json);
//header('content-type: application/text_plain');
//header('content-type: application/json');

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

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