diabliyo

moneyBox - Codigo PHP para Addendas

Mar 28th, 2014
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 8.85 KB | None | 0 0
  1. <?php
  2. /*
  3.     Autor       M.S.I. Angel Cantu Jauregui
  4.     Mail        angel.cantu@sie-group.net
  5.     Web     http://www.sie-group.net
  6.     Filename    addendas.php
  7.    
  8.     License GPL v3
  9.  
  10.     addendas.php is free software: you can redistribute it and/or modify
  11.     it under the terms of the GNU General Public License as published by
  12.     the Free Software Foundation, either version 3 of the License, or
  13.     (at your option) any later version.
  14.  
  15.     addendas.php is distributed in the hope that it will be useful,
  16.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  17.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  18.     GNU General Public License for more details.
  19.  
  20.     You should have received a copy of the GNU General Public License
  21.     along with Foobar.  If not, see <http://www.gnu.org/licenses/>.
  22. */
  23.  
  24. # addendas
  25. function get_addendas( $op )
  26.     {
  27.     $addendas= array(
  28.         "1"=>array("name"=>"Pemex", "tag"=>"Addenda_Pemex", "def"=>"pm:"),
  29.         "2"=>array("name"=>"Ford", "tag"=>"0"),
  30.         "3"=>array("name"=>"Chrisler", "tag"=>"0"),
  31.         "4"=>array("name"=>"Nissan", "tag"=>"0"),
  32.         "5"=>array("name"=>"Axxa", "tag"=>"0"),
  33.         "6"=>array("name"=>"GNP", "tag"=>"0"),
  34.         "7"=>array("name"=>"Soriana", "tag"=>"0"),
  35.         "8"=>array("name"=>"Chedraui", "tag"=>"0"),
  36.         "9"=>array("name"=>"Seguros Monterrey", "tag"=>"0"),
  37.         "10"=>array("name"=>"Smart", "tag"=>"0"),
  38.         "11"=>array("name"=>"Jhon Deer", "tag"=>"0"),
  39.         "12"=>array("name"=>"7-Eleven", "tag"=>"0")
  40.         );
  41.    
  42.     $addendas_vars= array(
  43.         "1"=>array(
  44.                     "headers"=>array(  # cabecera para el XML (atributo <addenda>)
  45.                                         "xmlns:xsi"=>"http://www.w3.org/2001/XMLSchema-instance",
  46.                                         "xmlns:pm"=>"http://pemex.com/facturaelectronica/addenda/v2",
  47.                                         "schemaLocation"=>"http://pemex.com/facturaelectronica/addenda/v2 https://pemex.reachcore.com/schemas/addenda-pemex-v2.xsd"
  48.                                         ),
  49.                     # VariableXML  =>  Nombre Imprimible
  50.                     "CONTRATO"=>"Contrato",
  51.                     "O_SURTIMIENTO"=>"Surtimiento",
  52.                     "N_ESTIMACION"=>"Numero de Estimacion",
  53.                     "P_ESTIMACION"=>"Periodo de Estimacion",
  54.                     "N_ACREEDOR"=>"Numero del Acreedor",
  55.                     "C_GESTOR"=>"Gestor",
  56.                     "FINIQUITO"=>"Finiquito",
  57.                     "POSICIONAP"=>"Afectuacion Presupuestaria",
  58.                     "AUTORIZA"=>"Persona que Autoriza",
  59.                     "ENTRADA"=>"Num. Entrada Material",
  60.                     "EJERCICIO"=>"Ejercicio Fiscal",
  61.                     "CEJECUTOR"=>"Centro Ejecutor",
  62.                     "RECEPSAP"=>"Num. Recepcion SAP",
  63.                     "NREMISION"=>"Num. Remision",
  64.                     "PLAZO"=>"Plazo de Pago",
  65.                     "RFCPROVEEDOR"=>"RFC Proveedor",
  66.                     "REMESA"=>"Folio de Recepcion",
  67.                     "VUREGION"=>"Region VU",
  68.                     "FICHAE"=>"Ficha COPADE",
  69.                     "FICHAF"=>"Ficha Firmante COPADE",
  70.                     "MONEDA"=>"Moneda del Comprobante",
  71.                     "FONDO"=>"Fondo",
  72.                     "POSICIONF"=>"Posicion Financiera",
  73.                     "CLAVE_TRANSP"=>"Clave Transportista",
  74.                     "A_RELACION"=>"Aualidad de Relacion",
  75.                     "ID_ANALITICO"=>"ID Analitico",
  76.                     "ANALITICO"=>"Clave Analitico",
  77.                     "TIPO_PRODUCTO"=>"Tipo de Producto",
  78.                     "CEDULA"=>"Cedula",
  79.                     "CONTRATO_SIIC"=>"Contrato SIIC",
  80.                     "OCOMERCIAL"=>"Orden Comercial",
  81.                     "SERVICIOG"=>"Datos Servicio Generico",
  82.                     "SERVICIOA"=>"Datos Servicio Administrativo",
  83.                     "CORREOPMI"=>"E-Mail del Solicitante"
  84.                 )
  85.         /*
  86.         "2"=>array(
  87.                     # cabecera para el XML (atributo <addenda>)
  88.                     "headers"=>array(
  89.                                         "xmlns:xsi"=>"error",
  90.                                         "schemaLocation"=>"error"
  91.                                         ),
  92.                     # VariableXML  =>  Nombre Imprimible
  93.                     "vars?"=>"error"
  94.                 )
  95.         */
  96.         );
  97.    
  98.     if( is_array($op) )
  99.         {
  100.         if( !strcmp($op[0], "get_number") )     # obtiene numero
  101.             {
  102.             foreach( $addendas as $key=>$val )
  103.                 {
  104.                 if( strstr($val, $op[1]) )      return $key;
  105.                 }
  106.             }
  107.         else if( !strcmp($op[0], "get_name") )      # obtiene nombre a partir del numero
  108.             {
  109.             foreach( $addendas as $key=>$val )
  110.                 {
  111.                 if( !strcmp($key, $op[1]) )     return $val["name"];
  112.                 }
  113.             }
  114.         else if( !strcmp($op[0], "get_tag") )       # obtiene el tag de la addenda
  115.             {
  116.             foreach( $addendas as $key=>$val )
  117.                 {
  118.                 if( !strcmp($key, $op[1]) )     return $val["tag"];
  119.                 }
  120.             }
  121.         else if( !strcmp($op[0], "get_vars") )      # obtiene las variables de la addenda
  122.             {
  123.             foreach( $addendas_vars as $key=>$val )
  124.                 {
  125.                 if( !strcmp($key, $op[1]) )     return $val;
  126.                 }
  127.             }
  128.         else if( !strcmp($op[0], "get_headers") )       # obtiene las cabeceras
  129.             {
  130.             foreach( $addendas_vars as $key=>$val )
  131.                 {
  132.                 if( !strcmp($key, $op[1]) )
  133.                     {
  134.                     foreach( $val as $key2=>$val2 )
  135.                         {
  136.                         if( !strcmp($key2, "headers") )     return $val2;
  137.                         }
  138.                     }
  139.                 }
  140.             }
  141.         else if( !strcmp($op[0], "get_definicion") )        # obtiene la definicion de la addenda
  142.             {
  143.             foreach( $addendas as $key=>$val )
  144.                 {
  145.                 if( !strcmp($key, $op[1]) )     return $val["def"];
  146.                 }
  147.             }
  148.         }
  149.     else
  150.         {
  151.         if( !strcmp($op, "print") )     # imprimir
  152.             {
  153.             foreach( $addendas as $key=>$val )
  154.                 echo '<option value="'. $key. '">'. $val["name"]. '</option>';
  155.             }
  156.         else if( !strcmp($op, "count") )        # contador de addendas activas
  157.             return count($addendas);
  158.         }
  159.     }
  160.  
  161. ########
  162. # main #
  163. ########
  164.  
  165. # En este punto recibimos las variables de la factura (conceptos, cantidades, etc...) y entre estas una variable llamda "thisaddenda_factura" esta contiene un "numero"
  166. # que se identifica con la lista de addendas soportadas en el sistema, ver la funcion "get_addendas($op)"
  167. #
  168. # 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
  169. # con la sig. nomenclatura: [VARIABLE|valor_escrito_por_usuario][VARIABLE|valor_escrito_por_usuario]....
  170. # se supone que esta "trama" la guardas en tu BDD asociada con esa factura.
  171. #
  172. # 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".
  173. # miren el PORT que esta en la linea 170 :D
  174. #
  175.  
  176.         # addendas
  177.         if( !$_POST["thisaddenda_factura"] || !strcmp($_POST["thisaddenda_factura"], "error") || $_POST["thisaddenda_factura"]<1 || $_POST["thisaddenda_factura"]>get_addendas("count") )
  178.             $addenda=0;
  179.         else        $addenda= $_POST["thisaddenda_factura"]; # ponemos numero de la addenda
  180.        
  181.         if( !$addenda )     $addenda_vars=0; # no hay addenda, no hay variables
  182.         else # si hay addenda, hay variables
  183.             {
  184.             $addenda_vars=0;
  185.             foreach( get_addendas(array("get_vars", $_POST["thisaddenda_factura"])) as $key=>$val )
  186.                 {
  187.                 if( strcmp($key, "headers") ) # excluimos las cabeceras XML
  188.                     {
  189.                     # si existe y es distinto al texto por default
  190.                     if( $_POST[$key.'_var'] && strcmp($_POST[$key.'_var'], $val) )
  191.                         {
  192.                         if( !$addenda_vars )        $addenda_vars= '['. $key. '|'. $_POST[$key.'_var']. ']';
  193.                         else        $addenda_vars .= '['. $key. '|'. $_POST[$key.'_var']. ']';
  194.                         }
  195.                     }
  196.                 }
  197.             }
  198.  
  199. #
  200. # en este punto debimos ya haber generado el XML, la cadena original y haber TIMBRADO el xml.
  201. # asi que ponemos aqui entra el sig. codigo para colgarle la addenda al XML Timbrado
  202. #
  203. # OJO: las funciones:
  204. # "consultar_datos_general()" --> sirve para consultar un valor ESPECIFICO a la BDD/
  205. # "proteger_cadena()" --> protege la variable (obvio: htmlentities())
  206. # "cargaAtt()" --> le anexa atributos a una etiqueta XML
  207.  
  208. # generar addenda y pegarlo al XML
  209. function generar_addenda( $idfactura, $user, $accion )
  210.     {
  211.     if( !$accion )      return 1; # no pidio addenda
  212.     else
  213.         {
  214.         # adendas
  215.         $addenda_vars= consultar_datos_general( "FACTURACION", "ID='". proteger_cadena($idfactura). "'", "ADDENDA_VARS"); # variables
  216.         $addenda= consultar_datos_general( "FACTURACION", "ID='". proteger_cadena($idfactura). "'", "ADDENDA"); # cliente
  217.         $headers= get_addendas( array( "get_headers", $addenda) ); # las cabeceras de la addenda (atributos)
  218.         $def= get_addendas( array( "get_definicion", $addenda) ); # la definicion de la addenda
  219.         $tag= get_addendas( array( "get_tag", $addenda) ); # el tag de la etiqueta
  220.  
  221.         $path= 'clientes/'. $user. '/facturas/';
  222.         $file= $idfactura. '.xml';
  223.         $xml = new DOMdocument( "1.0", "UTF-8" );
  224.         $xml->preserveWhiteSpace= FALSE;
  225.         $xml->load($path.$file); # cargamos el XML             
  226.         $a= $xml->getElementsByTagName("Comprobante")->item(0); # buscamos elementos comprobante
  227.         $complemento= $xml->createElement("cfdi:Addenda"); # creamos tag
  228.         $a->appendChild($complemento);
  229.         $addendaentidad= $xml->createElement($def.$tag); # creamos tag
  230.         $complemento->appendChild($addendaentidad);
  231.         cargaAtt($addendaentidad, $headers );
  232.  
  233.         $patron= "/\[(.*?)\|(.*?)\]/is";
  234.         preg_match_all( $patron, $addenda_vars, $data );
  235.        
  236.         foreach( $data[1] as $key=>$val )
  237.             {
  238.             # echo $val. ' --> '. $data[2][$key]. '<br>';
  239.             $addenda_elemento = $xml->createElement($def.$val, desproteger_cadena($data[2][$key]));
  240.             $addenda_elemento = $addendaentidad->appendChild($addenda_elemento);
  241.             cargaAtt($addenda_elemento, NULL);
  242.             }
  243.  
  244.         $xml->save($path.$file);
  245.  
  246.         return 1;
  247.         }
  248.     }
Add Comment
Please, Sign In to add comment