def store(f_xml): parser = SaxReader() xml_dat, _ = parser(f_xml) q = """select * from ncr_save_xml( {}::integer, '{}'::character varying, '{}'::character varying, '{}'::character varying, '{}'::boolean, {}::integer ) AS result( rc integer, msg text ) """.format( # Store procedure parameters req.get('ncr_id', None), # _ncr_id os.path.basename(f_xml), # _file_xml xml_dat['CFDI_SERIE'], # _serie xml_dat['CFDI_FOLIO'], # _folio req.get('saldado', None), # _saldado req.get('usr_id', None) # _usr_id ) logger.debug("Performing query: {}".format(q)) try: res = HelperPg.onfly_query(pt.dbms.pgsql_conn, q, True) if len(res) != 1: raise Exception( 'unexpected result regarding execution of store') rcode, rmsg = res.pop() if rcode == 0: return ErrorCode.SUCCESS raise Exception(rmsg) except: logger.error(dump_exception()) return ErrorCode.DBMS_SQL_ISSUES
def store(f_xmlin, usr_id, prefact_id, no_id): parser = SaxReader() xml_dat, _ = parser(f_xmlin) ref_id = '{}_{}{}'.format(no_id, xml_dat['CFDI_SERIE'], xml_dat['CFDI_FOLIO']) q = """select fac_save_xml from fac_save_xml( '{}'::character varying, {}::integer, {}::integer, '{}'::character varying, '{}'::character varying, '{}'::character varying, '{}'::character varying, '{}'::character varying, '{}'::character varying, '{}'::character varying, '{}'::character varying, '{}'::character varying, '{}'::character varying, '{}'::character varying, '{}'::character varying, '{}'::character varying, '{}'::character varying, '{}'::character varying, '{}'::character varying, {}::double precision, {}::double precision, {}::double precision, {}::boolean, '{}'::character varying )""".format( # Store procedure parameters os.path.basename(f_xmlin), # file_xml prefact_id, # prefact_id usr_id, # usr_id xml_dat['CFDI_DATE'].split('T')[0], # creation_date ref_id, # no_id_emp xml_dat['CFDI_SERIE'], # serie xml_dat['CFDI_FOLIO'], # folio 'THIS FIELD IS DEPRECATED', # items_str 'THIS FIELD IS DEPRECATED', # traslados_str 'THIS FIELD IS DEPRECATED', # retenciones_str xml_dat['INCEPTOR_REG'], # reg_fiscal 'THIS FIELD IS DEPRECATED', # pay_method xml_dat['INCEPTOR_CP'], # exp_place 'FACTURA', # proposito - It is obviously hardcoded 'THIS FIELD IS DEPRECATED', # no_aprob 'THIS FIELD IS DEPRECATED', # ano_aprob xml_dat['RECEPTOR_RFC'], # rfc_custm - RFC customer xml_dat['RECEPTOR_NAME'], # rs_custm - Razon social customer '0000', # account_number - An account fake number invented by me xml_dat['TAXES']['TRAS']['TOTAL'], # total_tras '0', # subtotal_with_desc xml_dat['CFDI_TOTAL'], # total 'false', # refact xml_dat[ 'UUID'] # id de documento - It came from SAT timbrado throughout PAC ) logger.debug("Performing query: {}".format(q)) try: s_out = None for row in HelperPg.onfly_query(q, True): # Just taking first row of query result s_out = row['fac_save_xml'] break # here we should parse s_out line logger.debug( 'store procedure fac_save_xml has returned: {}'.format(s_out)) return ErrorCode.SUCCESS except: logger.error(dump_exception()) return ErrorCode.ETL_ISSUES
def update_consecutive_alpha(f_xmlin): parser = SaxReader() xml_dat, _ = parser(f_xmlin) q = """update fac_cfds_conf_folios set folio_actual = (folio_actual + 1) FROM gral_suc AS SUC LEFT JOIN fac_cfds_conf ON fac_cfds_conf.gral_suc_id = SUC.id LEFT JOIN gral_usr_suc AS USR_SUC ON USR_SUC.gral_suc_id = SUC.id WHERE fac_cfds_conf_folios.proposito = 'PAG' AND fac_cfds_conf_folios.fac_cfds_conf_id=fac_cfds_conf.id AND USR_SUC.gral_usr_id = {}""".format(usr_id) try: HelperPg.onfly_update(q) except: logger.error(dump_exception()) return ErrorCode.DBMS_SQL_ISSUES return ErrorCode.SUCCESS
def undonota(logger, pt, req): ncr_id = req.get('ncr_id', None) usr_id = req.get('usr_id', None) reason = req.get('reason', None) mode = req.get('mode', None) if reason is None: reason = '' if (ncr_id is None) or (usr_id is None) or (mode is None): return ErrorCode.REQUEST_INCOMPLETE.value def get_xml_name(): q = """select ref_id as filename FROM fac_nota_credito WHERE fac_nota_credito.id = {}""".format(ncr_id) for row in HelperPg.onfly_query(pt.dbms.pgsql_conn, q, True): # Just taking first row of query result return row['filename'] + '.xml' source = ProfileReader.get_content(pt.source, ProfileReader.PNODE_UNIQUE) resdir = os.path.abspath(os.path.join(os.path.dirname(source), os.pardir)) rdirs = fetch_rdirs(resdir, pt.res.dirs) _uuid = None _rfc = None try: _rfc = __get_emisor_rfc(logger, usr_id, pt.dbms.pgsql_conn) except: return ErrorCode.DBMS_SQL_ISSUES.value try: cfdi_dir = os.path.join(rdirs['cfdi_output'], _rfc) f_xml = os.path.join(cfdi_dir, get_xml_name()) logger.debug('File to cancel {}'.format(f_xml)) parser = SaxReader() xml_dat, _ = parser(f_xml) _uuid = xml_dat['UUID'] except: return ErrorCode.RESOURCE_NOT_FOUND.value rc = __pac_cancel(logger, _uuid, _rfc, pt.tparty.pac) if rc != ErrorCode.SUCCESS: return rc.value q_do = """select * from ncr_exec_cancel( {}::integer, {}::integer, '{}'::text, {}::integer ) AS result( rc integer, msg text ) """.format( # Store procedure parameters usr_id, # _usr_id ncr_id, # _ncr_id reason, # _reason mode # _mode ) rc = __run_sp_ra(logger, q_do, pt.dbms.pgsql_conn) return rc.value
def undofacturar(logger, pt, req): fact_id = req.get('fact_id', None) usr_id = req.get('usr_id', None) reason = req.get('reason', None) mode = req.get('mode', None) if reason is None: reason = '' if (fact_id is None) or (usr_id is None) or (mode is None): return ErrorCode.REQUEST_INCOMPLETE.value q_val = """select * from fac_val_cancel( {}::integer ) AS result( rc integer, msg text ) """.format( # Store procedure parameters fact_id # _fac_id ) q_do = """select * from fac_exec_cancel( {}::integer, {}::integer, '{}'::text, {}::integer ) AS result( rc integer, msg text ) """.format( # Store procedure parameters usr_id, # _usr_id fact_id, # _fac_id reason, # _reason mode # _mode ) def run_store(q): logger.debug("Performing query: {}".format(q)) res = HelperPg.onfly_query(pt.dbms.pgsql_conn, q, True) # For this case we are just expecting one row if len(res) != 1: raise Exception('unexpected result regarding execution of store') return res def check_result(r): rcode, rmsg = r.pop() if rcode != 0: raise Exception(rmsg) def get_xml_name(): q = """select ref_id as filename FROM fac_docs WHERE fac_docs.id=""" for row in HelperPg.onfly_query(pt.dbms.pgsql_conn, "{0}{1}".format(q, fact_id), True): # Just taking first row of query result return row['filename'] + '.xml' source = ProfileReader.get_content(pt.source, ProfileReader.PNODE_UNIQUE) resdir = os.path.abspath(os.path.join(os.path.dirname(source), os.pardir)) rdirs = fetch_rdirs(resdir, pt.res.dirs) _uuid = None _res = None _rfc = None try: _rfc = __get_emisor_rfc(logger, usr_id, pt.dbms.pgsql_conn) except: return ErrorCode.DBMS_SQL_ISSUES.value try: cfdi_dir = os.path.join(rdirs['cfdi_output'], _rfc) f_xml = os.path.join(cfdi_dir, get_xml_name()) logger.debug('File to cancel {}'.format(f_xml)) parser = SaxReader() xml_dat, _ = parser(f_xml) _uuid = xml_dat['UUID'] except: return ErrorCode.RESOURCE_NOT_FOUND.value rc = __run_sp_ra(logger, q_val, pt.dbms.pgsql_conn, tmode = False) if rc != ErrorCode.SUCCESS: return rc.value rc = __pac_cancel(logger, _uuid, _rfc, pt.tparty.pac) if rc != ErrorCode.SUCCESS: return rc.value rc = __run_sp_ra(logger, q_do, pt.dbms.pgsql_conn) return rc.value