<? session_start();date_default_timezone_set('America/Mexico_City');?>
<?php

require_once("../../comun/conexion3.php");

require_once("../../comun/actions.php");

require_once("../../comun/funciones.php");

$action = new actions();


$operacion	= $_POST['operacion'];
//$action->alert($_POST['operacion']);

$socialreasonc = trim($_POST['alias']);
$alias = trim($_POST['alias']);
$idcliente	 = $_POST['idcliente'];
//$action->alert($_POST['idcliente']);
$nickname		 = trim($_POST['nickname']);
$idalias		 = $_POST['idalias'];
$tel1	= $_POST['telefono'];
$fax		= $_POST['fax'];
$direction	= $_POST['direccion'];
$no_interior	= $_POST['no_interior'];
$no_exterior	= $_POST['no_exterior'];
$city	= $_POST['municipio'];
$county	= $_POST['colonia'];
$zip		= $_POST['cp'];
$state	= $_POST['estado'];
$country	= $_POST['pais'];
$credito	 = ($_POST['credito']=='1' ? 1:0);
$cuentawwa	 = ($_POST['cuenta_wwa']=='1' ? 1:0);
$montocredito	 = $_POST['monto_credito'];
$diascredito	 = $_POST['dias_credito'];

$invoice_direction	= $_POST['invoice_direction'];
$invoice_no_interior	= $_POST['invoice_no_interior'];
$invoice_no_exterior	= $_POST['invoice_no_exterior'];
$invoice_city	= $_POST['invoice_city'];
$invoice_county	= $_POST['invoice_county'];
$invoice_zip		= $_POST['invoice_zip'];
$invoice_state	= $_POST['invoice_state'];
$invoice_country	= $_POST['invoice_country'];
$facturar_a	= $_POST['facturar_a'];
$rfc	= $_POST['rfc'];
$notificar_a	= $_POST['notificar_a'];
$email_facturas	= $_POST['email_facturas'];

$bl_direction	= $_POST['bl_direction'];
$bl_no_interior	= $_POST['bl_no_interior'];
$bl_no_exterior	= $_POST['bl_no_exterior'];
$bl_city	= $_POST['bl_city'];
$bl_county	= $_POST['bl_county'];
$bl_zip		= $_POST['bl_zip'];
$bl_state	= $_POST['bl_state'];
$bl_country	= $_POST['bl_country'];

$lbCampo_Valido = true;
$realizado		= false;
$alertar		= false;

		if($operacion== "ALTA"){
			$existente	= false;
			$query		= "SELECT * FROM alias  WHERE nickname='$nickname'
			and socialreason = '$alias'
			";
			$result		= mysqli_query( $query);

			if ( mysqli_num_rows($result)>0 ) {
					$existente	= true;
			}

			if ( $existente ) {
				$action->focus("alias");
				$realizado	= false;
			} else {
				mysqli_query("insert into alias 
				(`idcliente`, `nickname`, `socialreason`, `direction`,`no_interior`,`no_exterior`,
				 `city`, `zip`, `county`, `state`, `country`, `tel1`, `fax`, `credito`,
				 `montocredito`, `diascredito`, `cuentawwa`,	
				 `creoArea`,invoice_direction,`invoice_no_interior`,`invoice_no_exterior`,
				 invoice_city,invoice_county,
				 invoice_zip,invoice_state,invoice_country,rfc,facturar_a,
				 notificar_a,email_facturas,bl_direction,`bl_no_interior`,`bl_no_exterior`,
				 bl_city,bl_county,bl_zip,bl_state,bl_country,usuarioCreo,fechaRegistro) values 
				('$idcliente','$nickname','".$_POST[alias]."','$direction','$no_interior','$no_exterior',
				'$city','$zip','$county','$state','$country','$tel1','$fax','$credito',
   				'$montocredito','$diascredito','$cuentawwa','".$_SESSION[area]."',
				'$invoice_direction','$invoice_no_interior','$invoice_no_exterior',
				'$invoice_city','$invoice_county',
				'$invoice_zip','$invoice_state','$invoice_country','$rfc','$facturar_a',
				'$notificar_a','$email_facturas',
				'$bl_direction','$bl_no_interior','$bl_no_exterior',
				'$bl_city','$bl_county',
				'$bl_zip','$bl_state','$bl_country','".$_POST[idusuario]."','".date("Y-m-d H:i:s")."'
				) ");
				$realizado	= true;
			}


					if ( !$realizado ) {
						$msg="Ya existe esta sucursal, por favor rectifique.";
						$alertar	= true;
					}

			}


		if($operacion== "BAJA"){
			$existente	= false;
			$query		= "SELECT * FROM contactos  WHERE idalias='$idalias'";
			$result		= mysqli_query( $query);

			if ( mysqli_num_rows($result) ) {
					$existente	= true;
			}
			
			if ( $existente ) {
				$realizado	= false;
			} else {
				
			mysqli_query("insert into quien_borro (idusuario,idEntidad,fecha,descripcion)
			values ('$idusuario','$idalias','".date("Y-m-d H:i:s")."','sucursal')");
				mysqli_query("delete from alias where id= '$idalias'");
				$realizado	= true;
			
			}

					if ( !$realizado ) {
						$msg="No es posible borrar esta sucursal ya que tiene contactos registrados, por favor rectifique.";
						$alertar	= true;
					}

		}

		if($operacion== "CAMBIO"){
			$existente	= false;
			$query		= "SELECT * FROM alias  WHERE nickname='%".$nickname."%'  and id<>'$idalias' and socialreason = '$alias'";
			$result		= mysqli_query( $query);

			if (mysqli_num_rows($result) ) {
					$existente	= true;
			}

			if ( $existente ) {
				$action->focus("alias");
				$realizado	= false;
			} else {
				mysqli_query("update alias set  `nickname`='$nickname', `socialreason`='$alias',
				`direction` = '$direction', 
				 no_interior ='$no_interior',no_exterior ='$no_exterior',
				`city`='$city', `zip`='$zip',
				`county` = '$county', `state`='$state', `country`='$country', 
				`tel1`='$tel1', `fax`='$fax', `credito`='$credito',
				`montocredito`='$montocredito', `diascredito`='$diascredito',
				`cuentawwa`='$cuentawwa', invoice_direction ='$invoice_city',
				invoice_no_interior ='$invoice_no_interior',invoice_no_exterior ='$invoice_no_exterior',
				invoice_city ='$invoice_city',
				invoice_county = '$invoice_county',
				invoice_zip = 	'$invoice_zip',
				invoice_state = '$invoice_state',
				invoice_country = '$invoice_country',
				rfc = '$rfc',
				facturar_a = '$facturar_a',
				notificar_a = '$notificar_a',
				email_facturas = '$email_facturas',
				bl_direction ='$bl_city',
				bl_no_interior ='$bl_no_interior',bl_no_exterior ='$bl_no_exterior',
				bl_city ='$bl_city',
				bl_county = '$bl_county',
				bl_zip = 	'$bl_zip',
				bl_state = '$bl_state',
				bl_country = '$bl_country',
				usuarioEdito='".$_POST['idusuario']."',fechaUpdate='".date("Y-m-d H:i:s")."'
				WHERE id='$idalias'");
				$realizado	= true;
				//$action->alert(mysqli_error());
			}

					if ( !$realizado ) {
						$msg="Esta sucursal ya esta registrada para este cliente, por favor rectifique.";
						$alertar	= true;
					}

			}


	if ( $lbCampo_Valido && $realizado ){

		$action->alert( "Operación de $operacion OK  "); // .mysqli_error()
		$action->add_js_code("genericAjax('divBuscar','../alias/buscar_alias_ei2.php?idcliente='+$('#idcliente').val());
			Cancelar(document.forma)");
	}else if ( $alertar ){

		$action->alert( $msg );
	}

$action->send_response();

return;

?>