Ejemplo n.º 1
0
def excute_initialization_for_taxpayer(forcing_execution=False,forcing_period=False,taxpayer=None,process_logger=None,process_params=None):
	try:
		process_logger.info(2*LOG_INDENT + 'Forcing period: ' + str(forcing_period))
		initialization_data = _Locals.get_initialization_data(taxpayer,logger=process_logger,process_params=process_params)
		initialized = initialization_data['initialized']
		if initialized == False:
			initialization_log = _Locals.new_initialization_log(logger=process_logger)
			# Get CFDIs from DB:
			process_logger.info(2*LOG_INDENT + 'RETRIEVING DATA FROM FOREST DB ... ')
			_year = str(initialization_data['year'])
			_month = str(initialization_data['month'])
			process_logger.info(3*LOG_INDENT + 'Year: ' + str(initialization_data['year']) + ' Month: ' + str(initialization_data['month']))
			process_logger.info(3*LOG_INDENT + 'From ' + str(initialization_data['begin_date']) + ' to ' + str(initialization_data['end_date']))
			cfdis_in_db = _Utilities.get_cfdis_in_forest_for_this_taxpayer_at_period(taxpayer,initialization_data['begin_date'],initialization_data['end_date'],limit=None)
			# Manage or format CFDis data:
			existing_cfdi_uuids = _Utilities.get_existing_uuids_in_forest_db(cfdis_in_db=cfdis_in_db,logger=process_logger)
			# Log Forest data:
			process_logger.info(3*LOG_INDENT + 'Existing:           ' + str(len(existing_cfdi_uuids)))
			# Get CFDIs from firmware:
			get_sat_updates_params = {
				'identifier' : taxpayer['identifier'],
				'password' : taxpayer['password'],
				'year' : initialization_data['year'],
				'months' : [initialization_data['month']],
				'uuids' : existing_cfdi_uuids
			}# End of get_sat_update_params
			# -------------------------------------------------------------------
			# bugSolved 12/Ago/15 
			# Event: timeout value was becoming longer and longer because of connection problems (firmware servers could not be reached due to connection problems instead of logic problems)
			# firmware_timeout = taxpayer['firmware_timeout'] if 'firmware_timeout' in taxpayer and taxpayer['firmware_timeout'] is not None else _Constants.DEFAULT_FIRMWARE_TIMEOUT
			firmware_timeout = _Constants.DEFAULT_FIRMWARE_TIMEOUT
			# -------------------------------------------------------------------
			# process_logger.info(2*LOG_INDENT + 'RETRIEVING DATA FROM FIRMWARE (SAT) constant timeout = ' + str(firmware_timeout) + ' secs')
			process_logger.info(2*LOG_INDENT + 'RETRIEVING DATA FROM FIRMWARE (SAT)')
			sat_updates = _Firmware.isa(instruction='get_sat_updates',params=get_sat_updates_params,log=initialization_log,logger=process_logger,timeout=firmware_timeout,taxpayer=taxpayer)
			new_cfdis = sat_updates['new']
			process_logger.info(3*LOG_INDENT + 'CFDI new:           ' + str(initialization_log['firmware']['new']))
			process_logger.info(3*LOG_INDENT + 'CFDI to-update:     ' + str(initialization_log['firmware']['update']))
			# Update Forest DB -> NEW OR COMPLETED:
			process_logger.info(2*LOG_INDENT + 'UPDATING FOREST DB ... ')
			n = 0
			forest_db = _Utilities.set_connection_to_forest_db()
			for new_cfdi in new_cfdis:
				try:
					uuid = new_cfdi['uuid']
					_Utilities.create_cfdi(new_cfdi,logger=process_logger,log=initialization_log,forest_db=forest_db)
					n = n + 1
					process_logger.info(3*LOG_INDENT + str(n) + '. ' + uuid + ' stored in Forest DB')
				except:
					process_logger.info(3*LOG_INDENT + str(n) + '. ' + uuid + ' could not be stored in Forest DB (ERROR)')
			process_logger.info(2*LOG_INDENT + 'SUMMARY ... ')
			process_logger.info(3*LOG_INDENT + 'New stored:         ' + str(initialization_log['forest_db']['after']['new']))
			process_logger.info(3*LOG_INDENT + 'Pending:            ' + str(initialization_log['forest_db']['after']['pending']))
			initialization_result = {
				'new' : initialization_log['firmware']['new'],
				'stored' : initialization_log['forest_db']['after']['new'],
				'year_initialized' : initialization_data['year'],
				'month_initialized' : initialization_data['month'],
				'avoid_iteration' : initialization_log['avoid_iteration'] if 'avoid_iteration' in initialization_log else False
			}# End of initialization_result
		else:
			initialization_result = {
				'new' : _Constants.ZLATAN,
				'stored' : _Constants.ZLATAN,
				'year_initialized' : _Constants.ZLATAN,
				'month_initialized' : _Constants.ZLATAN
			}# End of initialization_result
		# Update taxpayer:
		new_initialization_data = initialization_data['new_initialization_data']
		initialization_result['initialized'] = initialized
		if forcing_period or ('avoid_iteration' in initialization_result and initialization_result['avoid_iteration'] == True):
			process_logger.info(2*LOG_INDENT + 'NOT updating taxpayer initialization status ... ')
			initialization_result['percentage_initialized'] = "--.-%"
		else:
			taxpayer = _Locals.update_taxpayer_initialization_status(taxpayer,new_initialization_data,logger=process_logger,initialized=initialized)
			initialization_result['percentage_initialized'] = taxpayer['data']['percentage_initialized']
			process_logger.info(3*LOG_INDENT + 'Percentage initialized:            ' + str(initialization_result['percentage_initialized']))
		if forcing_execution:
			process_logger.info(3*LOG_INDENT + 'Sending telegram notification ... ')
			message = 'Ya inicialice a este vato: ' + taxpayer['identifier'] + ' para el periodo ' + _month + '/' + _year
			_Utilities.send_message_to_forest_telegram_contacts(message,logger=process_logger)
		return initialization_result
	except Already_Handled_Exception as already_handled_exception:
		raise already_handled_exception
	except Exception as e:
		process_logger.critical(e.message)
		already_handled_exception = Already_Handled_Exception(e.message)
		raise already_handled_exception
Ejemplo n.º 2
0
def excute_initialization_for_taxpayer(forcing_execution=False,
                                       forcing_period=False,
                                       taxpayer=None,
                                       process_logger=None,
                                       process_params=None):
    try:
        process_logger.info(2 * LOG_INDENT + 'Forcing period: ' +
                            str(forcing_period))
        initialization_data = _Locals.get_initialization_data(
            taxpayer, logger=process_logger, process_params=process_params)
        initialized = initialization_data['initialized']
        if initialized == False:
            initialization_log = _Locals.new_initialization_log(
                logger=process_logger)
            # Get CFDIs from DB:
            process_logger.info(2 * LOG_INDENT +
                                'RETRIEVING DATA FROM FOREST DB ... ')
            _year = str(initialization_data['year'])
            _month = str(initialization_data['month'])
            process_logger.info(3 * LOG_INDENT + 'Year: ' +
                                str(initialization_data['year']) + ' Month: ' +
                                str(initialization_data['month']))
            process_logger.info(3 * LOG_INDENT + 'From ' +
                                str(initialization_data['begin_date']) +
                                ' to ' + str(initialization_data['end_date']))
            cfdis_in_db = _Utilities.get_cfdis_in_forest_for_this_taxpayer_at_period(
                taxpayer,
                initialization_data['begin_date'],
                initialization_data['end_date'],
                limit=None)
            # Manage or format CFDis data:
            existing_cfdi_uuids = _Utilities.get_existing_uuids_in_forest_db(
                cfdis_in_db=cfdis_in_db, logger=process_logger)
            # Log Forest data:
            process_logger.info(3 * LOG_INDENT + 'Existing:           ' +
                                str(len(existing_cfdi_uuids)))
            # Get CFDIs from firmware:
            get_sat_updates_params = {
                'identifier': taxpayer['identifier'],
                'password': taxpayer['password'],
                'year': initialization_data['year'],
                'months': [initialization_data['month']],
                'uuids': existing_cfdi_uuids
            }  # End of get_sat_update_params
            # -------------------------------------------------------------------
            # bugSolved 12/Ago/15
            # Event: timeout value was becoming longer and longer because of connection problems (firmware servers could not be reached due to connection problems instead of logic problems)
            # firmware_timeout = taxpayer['firmware_timeout'] if 'firmware_timeout' in taxpayer and taxpayer['firmware_timeout'] is not None else _Constants.DEFAULT_FIRMWARE_TIMEOUT
            firmware_timeout = _Constants.DEFAULT_FIRMWARE_TIMEOUT
            # -------------------------------------------------------------------
            # process_logger.info(2*LOG_INDENT + 'RETRIEVING DATA FROM FIRMWARE (SAT) constant timeout = ' + str(firmware_timeout) + ' secs')
            process_logger.info(2 * LOG_INDENT +
                                'RETRIEVING DATA FROM FIRMWARE (SAT)')
            sat_updates = _Firmware.isa(instruction='get_sat_updates',
                                        params=get_sat_updates_params,
                                        log=initialization_log,
                                        logger=process_logger,
                                        timeout=firmware_timeout,
                                        taxpayer=taxpayer)
            new_cfdis = sat_updates['new']
            process_logger.info(3 * LOG_INDENT + 'CFDI new:           ' +
                                str(initialization_log['firmware']['new']))
            process_logger.info(3 * LOG_INDENT + 'CFDI to-update:     ' +
                                str(initialization_log['firmware']['update']))
            # Update Forest DB -> NEW OR COMPLETED:
            process_logger.info(2 * LOG_INDENT + 'UPDATING FOREST DB ... ')
            n = 0
            forest_db = _Utilities.set_connection_to_forest_db()
            for new_cfdi in new_cfdis:
                try:
                    uuid = new_cfdi['uuid']
                    _Utilities.create_cfdi(new_cfdi,
                                           logger=process_logger,
                                           log=initialization_log,
                                           forest_db=forest_db)
                    n = n + 1
                    process_logger.info(3 * LOG_INDENT + str(n) + '. ' + uuid +
                                        ' stored in Forest DB')
                except:
                    process_logger.info(
                        3 * LOG_INDENT + str(n) + '. ' + uuid +
                        ' could not be stored in Forest DB (ERROR)')
            process_logger.info(2 * LOG_INDENT + 'SUMMARY ... ')
            process_logger.info(
                3 * LOG_INDENT + 'New stored:         ' +
                str(initialization_log['forest_db']['after']['new']))
            process_logger.info(
                3 * LOG_INDENT + 'Pending:            ' +
                str(initialization_log['forest_db']['after']['pending']))
            initialization_result = {
                'new':
                initialization_log['firmware']['new'],
                'stored':
                initialization_log['forest_db']['after']['new'],
                'year_initialized':
                initialization_data['year'],
                'month_initialized':
                initialization_data['month'],
                'avoid_iteration':
                initialization_log['avoid_iteration']
                if 'avoid_iteration' in initialization_log else False
            }  # End of initialization_result
        else:
            initialization_result = {
                'new': _Constants.ZLATAN,
                'stored': _Constants.ZLATAN,
                'year_initialized': _Constants.ZLATAN,
                'month_initialized': _Constants.ZLATAN
            }  # End of initialization_result
        # Update taxpayer:
        new_initialization_data = initialization_data[
            'new_initialization_data']
        initialization_result['initialized'] = initialized
        if forcing_period or ('avoid_iteration' in initialization_result
                              and initialization_result['avoid_iteration']
                              == True):
            process_logger.info(
                2 * LOG_INDENT +
                'NOT updating taxpayer initialization status ... ')
            initialization_result['percentage_initialized'] = "--.-%"
        else:
            taxpayer = _Locals.update_taxpayer_initialization_status(
                taxpayer,
                new_initialization_data,
                logger=process_logger,
                initialized=initialized)
            initialization_result['percentage_initialized'] = taxpayer['data'][
                'percentage_initialized']
            process_logger.info(
                3 * LOG_INDENT + 'Percentage initialized:            ' +
                str(initialization_result['percentage_initialized']))
        if forcing_execution:
            process_logger.info(3 * LOG_INDENT +
                                'Sending telegram notification ... ')
            message = 'Ya inicialice a este vato: ' + taxpayer[
                'identifier'] + ' para el periodo ' + _month + '/' + _year
            _Utilities.send_message_to_forest_telegram_contacts(
                message, logger=process_logger)
        return initialization_result
    except Already_Handled_Exception as already_handled_exception:
        raise already_handled_exception
    except Exception as e:
        process_logger.critical(e.message)
        already_handled_exception = Already_Handled_Exception(e.message)
        raise already_handled_exception
Ejemplo n.º 3
0
def excute_synchronization_layer_1_for_taxpayer(forcing_execution=False,
                                                taxpayer=None,
                                                sl1_data=None,
                                                process_logger=None):
    try:
        sl1_execution_log = _Locals.new_synchronization_layer_1_log()
        # Get CFDIs from DB:
        process_logger.info(2 * LOG_INDENT +
                            'RETRIEVING DATA FROM FOREST DB ... ')
        cfdis_in_db = _Utilities.get_cfdis_in_forest_for_this_taxpayer_at_period(
            taxpayer, sl1_data['begin_date'], sl1_data['end_date'])
        pending_cfdis_in_db = _Utilities.get_pending_cfdis_in_forest_for_this_taxpayer_at_period(
            taxpayer, sl1_data['begin_date'], sl1_data['end_date'])
        # Manage or format CFDis data:
        existing_cfdi_uuids = _Locals.get_existing_uuids_in_forest_db(
            cfdis_in_db=cfdis_in_db,
            logger=process_logger,
            sl1_execution_log=sl1_execution_log)
        pending_cfdi_uuids = _Locals.get_pending_uuids_in_forest_db(
            pending_cfdis_in_db=pending_cfdis_in_db,
            logger=process_logger,
            sl1_execution_log=sl1_execution_log)
        # Log Forest data:
        process_logger.info(
            3 * LOG_INDENT + 'Existing:           ' +
            str(sl1_execution_log['forest_db']['before']['good']))
        process_logger.info(
            3 * LOG_INDENT + 'Pending:            ' +
            str(sl1_execution_log['forest_db']['before']['pending']))
        # Get CFDIs from firmware:
        get_sat_updates_params = {
            'identifier': taxpayer['identifier'],
            'password': taxpayer['password'],
            'year': sl1_data['year'],
            'months': sl1_data['months'],
            'uuids': existing_cfdi_uuids
        }  # End of get_sat_update_params
        # -------------------------------------------------------------------
        # bugSolved 12/Ago/15
        # Event: timeout value was becoming longer and longer because of connection problems (firmware servers could not be reached due to connection problems instead of logic problems)
        # firmware_timeout = taxpayer['firmware_timeout'] if 'firmware_timeout' in taxpayer and taxpayer['firmware_timeout'] is not None else _Constants.DEFAULT_FIRMWARE_TIMEOUT
        firmware_timeout = _Constants.DEFAULT_FIRMWARE_TIMEOUT
        # -------------------------------------------------------------------
        # process_logger.info(2*LOG_INDENT + 'RETRIEVING DATA FROM FIRMWARE (SAT) constant timeout = ' + str(firmware_timeout) + ' secs')
        process_logger.info(2 * LOG_INDENT +
                            'RETRIEVING DATA FROM FIRMWARE (SAT)')
        sat_updates = _Firmware.isa(instruction='get_sat_updates',
                                    params=get_sat_updates_params,
                                    log=sl1_execution_log,
                                    logger=process_logger,
                                    timeout=firmware_timeout,
                                    taxpayer=taxpayer)
        new_cfdis = sat_updates['new']
        updated_cfdis = sat_updates['updated']
        process_logger.info(3 * LOG_INDENT + 'CFDI new:           ' +
                            str(sl1_execution_log['firmware']['new']))
        process_logger.info(3 * LOG_INDENT + 'CFDI to-update:     ' +
                            str(sl1_execution_log['firmware']['update']))
        # Update Forest DB -> NEW OR COMPLETED:
        process_logger.info(2 * LOG_INDENT +
                            'UPDATING FOREST DB (creting new CFDIs) ... ')
        n = 0
        for new_cfdi in new_cfdis:
            n = n + 1
            uuid = new_cfdi['uuid']
            if uuid in pending_cfdi_uuids:  # Complete pending cfdis:
                if 'xml' in new_cfdi and new_cfdi['xml'] != '':
                    cfdi_with_missing_xml = new_cfdi
                    _Locals.set_xml_to_pending_cfdi(
                        cfdi_with_missing_xml,
                        logger=process_logger,
                        sl1_execution_log=sl1_execution_log)
                    process_logger.info(3 * LOG_INDENT + str(n) + '. ' + uuid +
                                        ' completed in Forest DB')
            else:  # Completely new ones:
                try:
                    _Utilities.create_cfdi(new_cfdi,
                                           logger=process_logger,
                                           log=sl1_execution_log)
                    process_logger.info(3 * LOG_INDENT + str(n) + '. ' + uuid +
                                        ' stored in Forest DB')
                except Exception as e:
                    process_logger.info(3 * LOG_INDENT + str(n) +
                                        '. Exception catched with this one')
                    process_logger.info(str(e))
        # Update Forest DB -> JUST UPDATING:
        process_logger.info(
            2 * LOG_INDENT +
            'UPDATING FOREST DB (updating existing CFDIs) ... ')
        cfdis_in_db = _Utilities.get_cfdis_in_forest_for_this_taxpayer_at_period(
            taxpayer, sl1_data['begin_date'], sl1_data['end_date']
        )  # Get cfdis for updating data (They must be retrieved again due to cursor invalidation problems):
        updated_cfdi_uuids = []
        updated_cfdis_status_by_uuid = {}
        cfdis_with_status_updated = 0
        default_canceled_xml_inserted_in_updating = 0
        n = 0
        for updated_cfdi in updated_cfdis:
            for cfdi_in_db in cfdis_in_db:
                cfdi_in_db_uuid = cfdi_in_db['uuid']
                updating_cfdi_uuid = updated_cfdi['uuid']
                if cfdi_in_db_uuid == updating_cfdi_uuid:
                    updated_cfdi_status = updated_cfdi['status']
                    # Compare the one in forest db and the one in SAT to equalize CFDI state:
                    if updated_cfdi_status == _Constants.CANCELED_STATUS:
                        _Locals.set_cancelled_status_to_cfdi(
                            cfdi_in_db,
                            updated_cfdi,
                            logger=process_logger,
                            sl1_execution_log=sl1_execution_log)
                        n = n + 1
                        process_logger.info(3 * LOG_INDENT + str(n) + '. ' +
                                            cfdi_in_db_uuid +
                                            ' updated in Forest DB')
        process_logger.info(2 * LOG_INDENT + 'SUMMARY ... ')
        process_logger.info(
            3 * LOG_INDENT + 'New stored:         ' +
            str(sl1_execution_log['forest_db']['after']['new']))
        process_logger.info(
            3 * LOG_INDENT + 'Pending completed:  ' +
            str(sl1_execution_log['forest_db']['after']['pending_completed']))
        process_logger.info(
            3 * LOG_INDENT + 'Pending:            ' +
            str(sl1_execution_log['forest_db']['after']['pending']))
        process_logger.info(
            3 * LOG_INDENT + 'Updated:            ' +
            str(sl1_execution_log['forest_db']['after']['updated']))
        if forcing_execution:
            process_logger.info(3 * LOG_INDENT +
                                'Sending telegram notification ... ')
            message = 'Ya sincronice a este vato: ' + taxpayer['identifier']
            _Utilities.send_message_to_forest_telegram_contacts(
                message, logger=process_logger)
        return sl1_execution_log
    except Already_Handled_Exception as already_handled_exception:
        raise already_handled_exception
    except Exception as e:
        process_logger.critical(e.message)
        already_handled_exception = Already_Handled_Exception(e.message)
        raise already_handled_exception