Files
ventas_php/modules/pedidos-enviar-prov.php

608 lines
14 KiB
PHP
Executable File

<?php
$empresa->AuthUser();
include_once(DOC_ROOT.'/pdf/dompdf_config.inc.php');
if(isset($_GET['cancelarId'])){
$pedidoId = intval($_GET['cancelarId']);
$mode = 'cancelar';
}elseif(isset($_GET['show-pdf'])){
$pedidoId = intval($_GET['show-pdf']);
$mode = 'show';
}elseif(isset($_GET['id'])){
$pedidoId = intval($_GET['id']);
$mode = 'send';
}else{
header('Location: '.WEB_ROOT);
exit;
}
if(WEB_ROOT == 'http://novomoda.ddns.net/html')
$enProduccion = false;
else
$enProduccion = true;
$pedido->setPedidoId($pedidoId);
$info = $pedido->Info();
$rfc->setRfcId(1);
$infE = $util->EncodeRow($rfc->Info());
$direccion = $infE['calle'];
if($infE['noExt'] != '')
$direccion .= ' No. Ext. '.$infE['noExt'];
if($infE['noInt'] != '')
$direccion .= ', No. Int. '.$infE['noInt'];
if($infE['colonia'] != '')
$direccion .= ', Col. '.$infE['colonia'];
if($infE['localidad'] != '')
$direccion .= ', '.$infE['localidad'];
if($infE['ciudad'] != '')
$direccion .= ', '.$infE['ciudad'];
if($infE['municipio'] != '')
$direccion .= ', '.$infE['municipio'];
if($infE['estado'] != '')
$direccion .= ', '.$infE['estado'];
if($infE['codigoPostal'] != '')
$direccion .= 'C.P. '.$infE['codigoPostal'];
$infE['direccion'] = $direccion;
$proveedor->setProveedorId($info['proveedorId']);
$infPv = $util->EncodeRow($proveedor->Info());
$fecha = date('d-m-Y',strtotime($info['fecha']));
$fecha = $util->FormatDateDMMMY($fecha);
$info['fecha'] = $fecha;
$fechaEntrega = date('d-m-Y',strtotime($info['fechaEntrega']));
$info['fechaEntrega'] = $util->FormatDateDMMMY($fechaEntrega);
if($info['fechaEntregaF']){
$fechaEntregaF = date('d-m-Y',strtotime($info['fechaEntregaF']));
$info['fechaEntrega'] .= ' al '.$util->FormatDateDMMMY($fechaEntregaF);
}
if($info['metodoCompra'] == 'conIva')
$info['metodoCompra'] = 'IVA Incluido';
else
$info['metodoCompra'] = 'IVA No Incluido';
//Distribuciones
$resProducts = $pedido->GetProdsDistribucion();
$dist = array();
$sucIds = array();
foreach($resProducts as $res){
$productoId = $res['productoId'];
$sucursalId = $res['sucursalId'];
$pedido->setProductoId($productoId);
$infPP['status'] = $pedido->GetInfoStatusProd();
if($infPP['status'] == 'Sugerencia'){
$pedido->setSucursalId($sucursalId);
$infPS['cantidad'] = $pedido->GetInfoCantProdSug();
$res['cantidad'] = $infPS['cantidad'];
}
if($res['cantidad'] == 0)
continue;
$dist[$productoId][$sucursalId]['cantidad'] = $res['cantidad'];
$dist[$productoId][$sucursalId]['pedDistId'] = $res['pedDistId'];
if(!in_array($sucursalId, $sucIds))
$sucIds[] = $sucursalId;
}
//Sucursales
$sucursals = array();
$tiendas = array();
foreach($sucIds as $sucursalId){
$res['sucursalId'] = $sucursalId;
$sucursal->setSucursalId($sucursalId);
$infS = $sucursal->Info();
$res['noSuc'] = $infS['noSuc'];
$tiendas[] = $res;
}
if(count($tiendas) > 0){
$tiendas = $util->orderMultiDimensionalArray($tiendas, 'noSuc');
$sucursals[] = $tiendas;
}
//Productos
$resProducts = $pedido->GetProductos();
$sugerencias = 0;
$subtotalP = 0;
$products = array();
foreach($resProducts as $card){
$prodCatId = $card['prodCatId'];
$prodSubcatId = $card['prodSubcatId'];
$productoId = $card['productoId'];
$card['productoId'] = $productoId;
$card['prodCatId'] = $prodCatId;
$card['prodSubcatId'] = $prodSubcatId;
$prodCat->setProdCatId($prodCatId);
$card['departamento'] = $prodCat->GetNameById();
$prodSubcat->setProdSubcatId($prodSubcatId);
$card['linea'] = $prodSubcat->GetNameById();
$producto->setProductoId($productoId);
$infP = $producto->Info();
$card['modelo'] = $infP['modelo'].' '.$infP['descripcion'];
$card['imagen'] = $infP['imagen'];
$card['codigoBarra'] = $infP['codigoBarra'];
$card['precioVentaIva'] = $infP['precioVentaIva'];
$card['atributos'] = $producto->GetAtributosAll();
$card['cantidad'] = $card['totalLote'] * $card['cantLotes'];
$totalP = $card['costo'] * $card['cantidad'];
$card['total'] = number_format($totalP,2);
$subtotalP += $totalP;
//PROPORCIONES
//Colores
$producto->setProductoId($productoId);
$resColores = $producto->GetColores();
$colores = array();
foreach($resColores as $res){
$colorId = $res['colorId'];
$pedido->setColorId($colorId);
$pedido->setProductoId($productoId);
$cantidad = $pedido->GetProporcionByColor();
$proporcionC[$colorId] = $cantidad;
$res['cantidad'] = $cantidad;
$atribVal->setAtribValId($res['colorId']);
$res['nombre'] = $atribVal->GetNameById();
$colores[$colorId] = $res;
}//foreach
//Tallas
$producto->setProductoId($productoId);
$resTallas = $producto->GetTallas();
$tallas = array();
foreach($resTallas as $res){
$tallaId = $res['tallaId'];
$pedido->setTallaId($tallaId);
$pedido->setProductoId($productoId);
$cantidad = $pedido->GetProporcionByTalla();
$proporcionT[$tallaId] = $cantidad;
$res['cantidad'] = $cantidad;
$atribVal->setAtribValId($tallaId);
$res['nombre'] = $atribVal->GetNameById();
$tallas[$tallaId] = $res;
}//foreach
//Obtenemos Totales
$totales = array();
$subtotales = array();
foreach($tallas as $idT => $t){
$cantT = $t['cantidad'];
$total = 0;
foreach($colores as $idC => $c){
$cantC = $c['cantidad'];
$subtotal = $cantC * $cantT;
$total += $subtotal;
$subtotales[$idT][$idC] = $subtotal;
}
$totales[$idT] = $total;
}//foreach
$card['tallas'] = $tallas;
$card['colores'] = $colores;
$card['totales'] = $totales;
$card['subtotales'] = $subtotales;
$card['proporcionT'] = $proporcionT;
$card['proporcionC'] = $proporcionC;
//FIN PROPORCIONES
if($card['status'] == 'Sugerencia')
$sugerencias++;
$products[] = $card;
}//foreach
$info['subtotal'] = number_format($info['subtotal'],2);
$info['iva'] = number_format($info['iva'],2);
$info['total'] = number_format($info['total'],2);
//HTML - PDF
$html .= '
<html>
<head>
<style type="text/css">
body{
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:10px;
}
.titulo {
color: #FFFFFF;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-weight: bold;
font-size: 12px;
}
</style>
</head>
<body>
<table width="550" border="0" cellpadding="0" cellspacing="0">
<tr>
<td width="430">
<p><b>'.strtoupper($infE['razonSocial']).'</b><br />
'.$infE['direccion'].'
</td>
<td>
</td>
</tr>
</table>
<br>
<table width="550" border="0" cellpadding="0" cellspacing="0">
<tr>
<td>
<table width="550" border="1" cellspacing="0" cellpadding="0">
<tr>
<td width="" align="center"><b>No. PEDIDO</b></td>
<td width="25%" align="center"><b>METODO COMPRA</b></td>
<td width="25%" align="center"><b>FECHA DE SOLICITUD</b></td>
<td width="26%" align="center"><b>FECHA DE ENTREGA</b></td>
</tr>
<tr>
<td height="15" align="center">'.$info['noPedido'].'</td>
<td align="center">'.$info['metodoCompra'].'</td>
<td align="center">'.$info['fecha'].'</td>
<td align="center">'.$info['fechaEntrega'].'</td>
</tr>
</table>
</td>
</tr>
</table>';
//DATOS DEL PROVEEDOR
$html .= '
<table width="550" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="4" align="center" bgcolor="#809829"><span class="titulo">DATOS DEL PROVEEDOR</span></td>
</tr>
<tr>
<td width="23%"><b>Raz&oacute;n Social</b></td>
<td width="26%">'.$infPv['nombre'].'</td>
<td width="27%"><b>Calle</b></td>
<td width="">'.$infPv['calle'].'</td>
</tr>
<tr>
<td><b>RFC</b></td>
<td>'.$infPv['rfc'].'</td>
<td><b>Colonia</b></td>
<td>'.$infPv['colonia'].'</td>
</tr>
<tr>
<td><b>Tel&eacute;fonos</b></td>
<td>'.$infPv['phone'].'</td>
<td><b>Delegaci&oacute;n o Municipio</b></td>
<td>'.$infPv['municipio'].'</td>
</tr>
<tr>
<td><b>C.P.</b></td>
<td>'.$infS['codigoPostal'].'</td>
<td><b>Estado</b></td>
<td>'.$infS['estado'].'</td>
</tr>
</table>';
//PRODUCTOS
$html .= '
<br>
<table width="550" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="5" align="center" bgcolor="#809829"><span class="titulo">PRODUCTOS</span></td>
</tr>
</table>
<br>';
foreach($products as $res){
//Tallas
$hTallas = '<br>
<table width="" cellpadding="0" cellspacing="0" border="1" align="center">
<tr>
<td colspan="'.count($res['tallas']).'" align="center"><b>Proporci&oacute;n Tallas</b></td>
</tr>
<tr>';
foreach($res['tallas'] as $val)
$hTallas .= '<td align="center" width="30">'.$val['nombre'].'<br>'.$val['cantidad'].'</td>';
$hTallas .= '
</tr>
</table>';
//Colores
$hColores = '<br>
<table width="" cellpadding="0" cellspacing="0" border="1" align="center">
<tr>
<td colspan="'.count($res['colores']).'" align="center"><b>Proporci&oacute;n Colores</b></td>
</tr>
<tr>';
foreach($res['colores'] as $val)
$hColores .= '<td align="center" width="30">'.$val['nombre'].'<br>'.$val['cantidad'].'</td>';
$hColores .= '
</tr>
</table>';
//Producto
$html .= '
<table width="550" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="13%" align="center" bgcolor="#CCCCCC">CODIGO BARRA</td>
<td width="" align="center" bgcolor="#CCCCCC">MODELO</td>
<td width="14%" align="center" bgcolor="#CCCCCC">COSTO UNITARIO</td>
<td width="14%" align="center" bgcolor="#CCCCCC">PRECIO VENTA</td>
<td width="14%" align="center" bgcolor="#CCCCCC">CANTIDAD</td>
<td width="14%" align="center" bgcolor="#CCCCCC">TOTAL</td>
</tr>
<tr>
<td align="center">'.$res['codigoBarra'].'</td>
<td align="center">'.$res['modelo'].'</td>
<td align="center">$'.$res['costo'].'</td>
<td align="center">$'.$res['precioVentaIva'].'</td>
<td align="center">'.$res['cantidad'].'</td>
<td align="right">$'.$res['total'].'</td>
</tr>
</table>
<br>
<table width="550" border="0" cellspacing="0" cellpadding="0">
<tr>
<td>'.$hTallas.'</td>
<td>'.$hColores.'</td>
</tr>
</table>
';
//Distribucion
$html .= '<br>
<div align="center"><b>DISTRIBUCION</b></div>
<br>
<table width="" cellpadding="0" cellspacing="0" border="1" align="center">
';
$html .= '<tr>';
$bloque = 0;
foreach($sucursals as $sucursales){
//$html .= '<tr>';
foreach($sucursales as $val){
$productoId = $res['productoId'];
$sucursalId = $val['sucursalId'];
$sucursal->setSucursalId($sucursalId);
$infS = $sucursal->Info();
$noSuc = $infS['noSuc'];
$distP = $dist[$productoId][$sucursalId];
if($distP['cantidad'] > 0){
$html .= '<td align="center" width="40">Suc. '.$noSuc.'<br>'.$distP['cantidad'].'</td>';
$bloque++;
}
if($bloque == 11){
$html .= '</tr><tr>';
$bloque = 0;
}
}//foreach
}//foreach $sucursals
if($bloque == 0)
$falta = 0;
else
$falta = 11 - $bloque;
for($k=1; $k<=$falta; $k++)
$html .= '<td align="center" width="40"></td>';
$html .= '</tr>';
$html .= '</table>';
}//foreach
//PRODUCTOS
$html .= '
<br>
<table width="550" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="5" align="center" bgcolor="#809829"><span class="titulo">PIEZAS POR CAJA</span></td>
</tr>
</table>
<br>';
$html .= '
<table width="550" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="15%" align="center" bgcolor="#CCCCCC">CODIGO BARRA</td>
<td width="" align="center" bgcolor="#CCCCCC">MODELO</td>
<td width="20%" align="center" bgcolor="#CCCCCC">CANTIDAD</td>
</tr>
';
foreach($products as $res){
$html .= '
<tr>
<td align="center">'.$res['codigoBarra'].'</td>
<td align="left">'.$res['modelo'].'</td>
<td align="center">'.$res['piezasCaja'].'</td>
</tr>';
}
$html .= '</table>';
//TOTALES
$html .= '
<br>
<table width="550" border="0" cellspacing="0" cellpadding="0">
<tr>
<td colspan="5" align="center" bgcolor="#809829"><span class="titulo">TOTALES</span></td>
</tr>
<tr>
<td width="20%"></td>
<td width=""></td>
<td width="16%">&nbsp;</td>
<td width="16%" valign="top">
<b>SUBTOTAL</b> <br>
<b>IVA '.$ivaPorc.'%</b> <br>
<b>TOTAL</b>
</td>
<td width="16%" align="right" valign="top">
$'.$info['subtotal'].'<br>
$'.$info['iva'].'<br>
$'.$info['total'].'
</td>
</tr>
</table>
</body>
</html>
';
$dompdf = new DOMPDF();
$dompdf->set_paper('letter');
$dompdf->load_html($html);
$dompdf->render();
if($mode == 'show'){
$dompdf->stream('orden_compra.pdf');
exit;
}
//Guardamos el archivo temporalmente
$pdfoutput = $dompdf->output();
$filename = DOC_ROOT.'/temp/orden_compra.pdf';
$fp = fopen($filename, "a");
fwrite($fp, $pdfoutput);
fclose($fp);
$_mail = new PHPMailer(true);
//Configurando el servidor SMTP
$_mail->IsSMTP();
$_mail->SMTPAuth = true;
$_mail->Host = SMTP_HOST;
$_mail->Username = SMTP_USER;
$_mail->Password = SMTP_PASS;
$_mail->Port = SMTP_PORT;
//Enviamos el mensaje
$email = $infPv['emailVtas'];
$proveedor = $infPv['nombre'];
if($mode == 'cancelar'){
$subject = 'Cancelacion Orden de Compra No. '.$info['noPedido'];
$message = 'Por medio de la presente le informamos que la Orden de Compra No. '.$info['noPedido'];
$message .= 'ha sido cancelada.';
$message .= '<br><br>Lamentamos los inconvenientes que esto pudiera ocasionar.';
}else{
$subject = 'Envio Orden de Compra No. '.$info['noPedido'];
$message = 'Por medio de la presente le hacemos llegar la Orden de Compra No. '.$info['noPedido'];
}
$message .= '<br><br>Por su atenci&oacute;n';
$message .='<br><br>Gracias';
$html = '<html>
<body>
'.$message.'
</body>
</html>';
try {
$_mail->AddAddress($email, $proveedor);
if($enProduccion){
$_mail->AddBCC('silvano.cruz@novomoda.com.mx');
$_mail->AddBCC('sonia.velazquez@novomoda.com.mx');
}
$_mail->AddBCC('sistemas@novomoda.com.mx');
$_mail->SetFrom('contacto@novomoda.com.mx', 'Novomoda');
$_mail->Subject = $subject;
$_mail->MsgHTML($html);
$_mail->AddAttachment(DOC_ROOT.'/temp/orden_compra.pdf'); // attachment
$_mail->Send();
$emailSent = 1;
} catch (phpmailerException $e) {
$emailSent = 2;
} catch (Exception $e) {
$emailSent = 2;
}
@unlink(DOC_ROOT.'/temp/orden_compra.pdf');
header('Location: '.WEB_ROOT.'/pedidos');
exit;
?>