Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- /*
- Autor M.S.I. Angel Cantu Jauregui
- Mail angel.cantu@sie-group.net
- Web http://www.sie-group.net
- Filename addendas.php
- License GPL v3
- addendas.php is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- addendas.php is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with Foobar. If not, see <http://www.gnu.org/licenses/>.
- */
- # addendas
- function get_addendas( $op )
- {
- $addendas= array(
- "1"=>array("name"=>"Pemex", "tag"=>"Addenda_Pemex", "def"=>"pm:"),
- "2"=>array("name"=>"Ford", "tag"=>"0"),
- "3"=>array("name"=>"Chrisler", "tag"=>"0"),
- "4"=>array("name"=>"Nissan", "tag"=>"0"),
- "5"=>array("name"=>"Axxa", "tag"=>"0"),
- "6"=>array("name"=>"GNP", "tag"=>"0"),
- "7"=>array("name"=>"Soriana", "tag"=>"0"),
- "8"=>array("name"=>"Chedraui", "tag"=>"0"),
- "9"=>array("name"=>"Seguros Monterrey", "tag"=>"0"),
- "10"=>array("name"=>"Smart", "tag"=>"0"),
- "11"=>array("name"=>"Jhon Deer", "tag"=>"0"),
- "12"=>array("name"=>"7-Eleven", "tag"=>"0")
- );
- $addendas_vars= array(
- "1"=>array(
- "headers"=>array( # cabecera para el XML (atributo <addenda>)
- "xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance",
- "xmlns:pm"=>"http://pemex.com/facturaelectronica/addenda/v2",
- "schemaLocation"=>"http://pemex.com/facturaelectronica/addenda/v2 https://pemex.reachcore.com/schemas/addenda-pemex-v2.xsd"
- ),
- # VariableXML => Nombre Imprimible
- "CONTRATO"=>"Contrato",
- "O_SURTIMIENTO"=>"Surtimiento",
- "N_ESTIMACION"=>"Numero de Estimacion",
- "P_ESTIMACION"=>"Periodo de Estimacion",
- "N_ACREEDOR"=>"Numero del Acreedor",
- "C_GESTOR"=>"Gestor",
- "FINIQUITO"=>"Finiquito",
- "POSICIONAP"=>"Afectuacion Presupuestaria",
- "AUTORIZA"=>"Persona que Autoriza",
- "ENTRADA"=>"Num. Entrada Material",
- "EJERCICIO"=>"Ejercicio Fiscal",
- "CEJECUTOR"=>"Centro Ejecutor",
- "RECEPSAP"=>"Num. Recepcion SAP",
- "NREMISION"=>"Num. Remision",
- "PLAZO"=>"Plazo de Pago",
- "RFCPROVEEDOR"=>"RFC Proveedor",
- "REMESA"=>"Folio de Recepcion",
- "VUREGION"=>"Region VU",
- "FICHAE"=>"Ficha COPADE",
- "FICHAF"=>"Ficha Firmante COPADE",
- "MONEDA"=>"Moneda del Comprobante",
- "FONDO"=>"Fondo",
- "POSICIONF"=>"Posicion Financiera",
- "CLAVE_TRANSP"=>"Clave Transportista",
- "A_RELACION"=>"Aualidad de Relacion",
- "ID_ANALITICO"=>"ID Analitico",
- "ANALITICO"=>"Clave Analitico",
- "TIPO_PRODUCTO"=>"Tipo de Producto",
- "CEDULA"=>"Cedula",
- "CONTRATO_SIIC"=>"Contrato SIIC",
- "OCOMERCIAL"=>"Orden Comercial",
- "SERVICIOG"=>"Datos Servicio Generico",
- "SERVICIOA"=>"Datos Servicio Administrativo",
- "CORREOPMI"=>"E-Mail del Solicitante"
- )
- /*
- "2"=>array(
- # cabecera para el XML (atributo <addenda>)
- "headers"=>array(
- "xmlns:xsi"=>"error",
- "schemaLocation"=>"error"
- ),
- # VariableXML => Nombre Imprimible
- "vars?"=>"error"
- )
- */
- );
- if( is_array($op) )
- {
- if( !strcmp($op[0], "get_number") ) # obtiene numero
- {
- foreach( $addendas as $key=>$val )
- {
- if( strstr($val, $op[1]) ) return $key;
- }
- }
- else if( !strcmp($op[0], "get_name") ) # obtiene nombre a partir del numero
- {
- foreach( $addendas as $key=>$val )
- {
- if( !strcmp($key, $op[1]) ) return $val["name"];
- }
- }
- else if( !strcmp($op[0], "get_tag") ) # obtiene el tag de la addenda
- {
- foreach( $addendas as $key=>$val )
- {
- if( !strcmp($key, $op[1]) ) return $val["tag"];
- }
- }
- else if( !strcmp($op[0], "get_vars") ) # obtiene las variables de la addenda
- {
- foreach( $addendas_vars as $key=>$val )
- {
- if( !strcmp($key, $op[1]) ) return $val;
- }
- }
- else if( !strcmp($op[0], "get_headers") ) # obtiene las cabeceras
- {
- foreach( $addendas_vars as $key=>$val )
- {
- if( !strcmp($key, $op[1]) )
- {
- foreach( $val as $key2=>$val2 )
- {
- if( !strcmp($key2, "headers") ) return $val2;
- }
- }
- }
- }
- else if( !strcmp($op[0], "get_definicion") ) # obtiene la definicion de la addenda
- {
- foreach( $addendas as $key=>$val )
- {
- if( !strcmp($key, $op[1]) ) return $val["def"];
- }
- }
- }
- else
- {
- if( !strcmp($op, "print") ) # imprimir
- {
- foreach( $addendas as $key=>$val )
- echo '<option value="'. $key. '">'. $val["name"]. '</option>';
- }
- else if( !strcmp($op, "count") ) # contador de addendas activas
- return count($addendas);
- }
- }
- ########
- # main #
- ########
- # En este punto recibimos las variables de la factura (conceptos, cantidades, etc...) y entre estas una variable llamda "thisaddenda_factura" esta contiene un "numero"
- # que se identifica con la lista de addendas soportadas en el sistema, ver la funcion "get_addendas($op)"
- #
- # de forma resumida, el sistema toma el "numero" de la addenda y consulta las variables, mas que nada para GUARDARLAS en la BDD y genera una "trama" con delimitadores
- # con la sig. nomenclatura: [VARIABLE|valor_escrito_por_usuario][VARIABLE|valor_escrito_por_usuario]....
- # se supone que esta "trama" la guardas en tu BDD asociada con esa factura.
- #
- # Como identifico/asocio que el INPUT (que mira el cliente) con la VARIABLE de la addenda ?... sencillo, uso el mismo nombre y le agrego un soporte "_var".
- # miren el PORT que esta en la linea 170 :D
- #
- # addendas
- if( !$_POST["thisaddenda_factura"] || !strcmp($_POST["thisaddenda_factura"], "error") || $_POST["thisaddenda_factura"]<1 || $_POST["thisaddenda_factura"]>get_addendas("count") )
- $addenda=0;
- else $addenda= $_POST["thisaddenda_factura"]; # ponemos numero de la addenda
- if( !$addenda ) $addenda_vars=0; # no hay addenda, no hay variables
- else # si hay addenda, hay variables
- {
- $addenda_vars=0;
- foreach( get_addendas(array("get_vars", $_POST["thisaddenda_factura"])) as $key=>$val )
- {
- if( strcmp($key, "headers") ) # excluimos las cabeceras XML
- {
- # si existe y es distinto al texto por default
- if( $_POST[$key.'_var'] && strcmp($_POST[$key.'_var'], $val) )
- {
- if( !$addenda_vars ) $addenda_vars= '['. $key. '|'. $_POST[$key.'_var']. ']';
- else $addenda_vars .= '['. $key. '|'. $_POST[$key.'_var']. ']';
- }
- }
- }
- }
- #
- # en este punto debimos ya haber generado el XML, la cadena original y haber TIMBRADO el xml.
- # asi que ponemos aqui entra el sig. codigo para colgarle la addenda al XML Timbrado
- #
- # OJO: las funciones:
- # "consultar_datos_general()" --> sirve para consultar un valor ESPECIFICO a la BDD/
- # "proteger_cadena()" --> protege la variable (obvio: htmlentities())
- # "cargaAtt()" --> le anexa atributos a una etiqueta XML
- # generar addenda y pegarlo al XML
- function generar_addenda( $idfactura, $user, $accion )
- {
- if( !$accion ) return 1; # no pidio addenda
- else
- {
- # adendas
- $addenda_vars= consultar_datos_general( "FACTURACION", "ID='". proteger_cadena($idfactura). "'", "ADDENDA_VARS"); # variables
- $addenda= consultar_datos_general( "FACTURACION", "ID='". proteger_cadena($idfactura). "'", "ADDENDA"); # cliente
- $headers= get_addendas( array( "get_headers", $addenda) ); # las cabeceras de la addenda (atributos)
- $def= get_addendas( array( "get_definicion", $addenda) ); # la definicion de la addenda
- $tag= get_addendas( array( "get_tag", $addenda) ); # el tag de la etiqueta
- $path= 'clientes/'. $user. '/facturas/';
- $file= $idfactura. '.xml';
- $xml = new DOMdocument( "1.0", "UTF-8" );
- $xml->preserveWhiteSpace= FALSE;
- $xml->load($path.$file); # cargamos el XML
- $a= $xml->getElementsByTagName("Comprobante")->item(0); # buscamos elementos comprobante
- $complemento= $xml->createElement("cfdi:Addenda"); # creamos tag
- $a->appendChild($complemento);
- $addendaentidad= $xml->createElement($def.$tag); # creamos tag
- $complemento->appendChild($addendaentidad);
- cargaAtt($addendaentidad, $headers );
- $patron= "/\[(.*?)\|(.*?)\]/is";
- preg_match_all( $patron, $addenda_vars, $data );
- foreach( $data[1] as $key=>$val )
- {
- # echo $val. ' --> '. $data[2][$key]. '<br>';
- $addenda_elemento = $xml->createElement($def.$val, desproteger_cadena($data[2][$key]));
- $addenda_elemento = $addendaentidad->appendChild($addenda_elemento);
- cargaAtt($addenda_elemento, NULL);
- }
- $xml->save($path.$file);
- return 1;
- }
- }
Add Comment
Please, Sign In to add comment