Esempio n. 1
0
def update_initialization_data_for_taxpayer(taxpayer,
                                            initialization_execution_log,
                                            logger=None):
    try:
        forest_db = _Utilities.set_connection_to_forest_db()
        db_Taxpayer = forest_db['Taxpayer']
        # Update sl1 success execution date:
        init_date = STATUS_DATES['initialization']
        initialization_date = Datetime.now()
        taxpayer[init_date] = initialization_date
        if initialization_execution_log['initialized'] == False:
            # Update synchronization logs:
            identifier = taxpayer['identifier']
            begin_date = taxpayer[
                'start_date']  #Since taxpayer claim to be synchronized
            begin_date = begin_date.replace(hour=0, minute=0)
            end_date = initialization_date
            cfdis_in_forest_db_count = _Utilities.get_cfdis_count_in_forest_for_this_taxpayer_at_period(
                taxpayer, begin_date, end_date)
            initialization_log = {
                'status': 200,
                'temporal_data': {
                    'completed_date': initialization_date,
                    'period': {
                        'year':
                        int(initialization_execution_log['year_initialized']),
                        'month':
                        int(initialization_execution_log['month_initialized'])
                    },
                },
                'cfdis': {
                    'new': initialization_execution_log['stored'],
                    'updated': 0,
                    'total': cfdis_in_forest_db_count
                }  # End of synchronization_log
            }  # End of initialization_log
            taxpayer_logs = taxpayer['logs'] if 'logs' in taxpayer and taxpayer[
                'logs'] is not None else {}
            taxpayer_initialization_logs = taxpayer_logs[_Constants.INITIALIZATION] if _Constants.INITIALIZATION in taxpayer_logs else []
            taxpayer_initialization_logs.append(initialization_log)
            taxpayer_logs[
                _Constants.INITIALIZATION] = taxpayer_initialization_logs
            taxpayer['logs'] = taxpayer_logs
        db_Taxpayer.save(taxpayer)
    except Exception as e:
        print e
        if logger is not None:
            logger.critical(e.message)
        already_handled_exception = Already_Handled_Exception(e.message)
        raise already_handled_exception
Esempio n. 2
0
def update_synchronization_data_for_taxpayer(taxpayer,
                                             sl1_execution_log,
                                             logger=None):
    try:
        forest_db = _Utilities.set_connection_to_forest_db()
        db_Taxpayer = forest_db['Taxpayer']
        # Update sl1 success execution date:
        sl1_date = STATUS_DATES['synchronization_layer_1']
        synchronization_date = Datetime.now()
        taxpayer[sl1_date] = synchronization_date
        # Update synchronization logs:
        identifier = taxpayer['identifier']
        begin_date = taxpayer[
            'start_date']  #Since taxpayer claim to be synchronized
        begin_date = begin_date.replace(hour=0, minute=0)
        end_date = synchronization_date  # Until now
        cfdis_in_forest_db_count = _Utilities.get_cfdis_count_in_forest_for_this_taxpayer_at_period(
            taxpayer, begin_date, end_date)
        synchronization_log = {
            'status': 200,
            'temporal_data': {
                'completed_date': synchronization_date,
                'period': {
                    'year': synchronization_date.year,
                    'month': synchronization_date.month
                },
            },
            'cfdis': {
                'new': sl1_execution_log['stored'],
                'updated': sl1_execution_log['updated'],
                'total': cfdis_in_forest_db_count
            }  # End of synchronization_log
        }  # End of synchronization_log
        taxpayer_logs = taxpayer['logs'] if 'logs' in taxpayer and taxpayer[
            'logs'] is not None else {}
        taxpayer_synchronization_logs = taxpayer_logs[_Constants.SYNCHRONIZATION] if _Constants.SYNCHRONIZATION in taxpayer_logs else []
        taxpayer_synchronization_logs.append(synchronization_log)
        if len(taxpayer_synchronization_logs
               ) > _Constants.LIMIT_LOGS_PER_TAXPAYER:
            taxpayer_synchronization_logs[:1] = []
        taxpayer_logs[
            _Constants.SYNCHRONIZATION] = taxpayer_synchronization_logs
        taxpayer['logs'] = taxpayer_logs
        db_Taxpayer.save(taxpayer)
    except Exception as e:
        if logger is not None:
            logger.critical(e.message)
        already_handled_exception = Already_Handled_Exception(e.message)
        raise already_handled_exception
Esempio n. 3
0
def update_synchronization_data_for_taxpayer(taxpayer,sl1_execution_log,logger=None):
	try:
		forest_db = _Utilities.set_connection_to_forest_db()
		db_Taxpayer = forest_db['Taxpayer']
		# Update sl1 success execution date:
		sl1_date = STATUS_DATES['synchronization_layer_1']
		synchronization_date = Datetime.now()
		taxpayer[sl1_date] = synchronization_date
		# Update synchronization logs:
		identifier = taxpayer['identifier']
		begin_date = taxpayer['start_date']#Since taxpayer claim to be synchronized
		begin_date = begin_date.replace(hour=0, minute=0)
		end_date = synchronization_date# Until now
		cfdis_in_forest_db_count = _Utilities.get_cfdis_count_in_forest_for_this_taxpayer_at_period(taxpayer,begin_date,end_date)
		synchronization_log = {
			'status' : 200,
			'temporal_data' : {
				'completed_date' : synchronization_date,
				'period' : {
					'year' : synchronization_date.year,
					'month' : synchronization_date.month
				},
			},
			'cfdis' : {
				'new' : sl1_execution_log['stored'],
				'updated' : sl1_execution_log['updated'],
				'total' : cfdis_in_forest_db_count
			}# End of synchronization_log
		}# End of synchronization_log
		taxpayer_logs = taxpayer['logs'] if 'logs' in taxpayer and taxpayer['logs'] is not None else {}
		taxpayer_synchronization_logs = taxpayer_logs[_Constants.SYNCHRONIZATION] if _Constants.SYNCHRONIZATION in taxpayer_logs else []
		taxpayer_synchronization_logs.append(synchronization_log)
		if len(taxpayer_synchronization_logs) > _Constants.LIMIT_LOGS_PER_TAXPAYER: 
			taxpayer_synchronization_logs[:1] = []
		taxpayer_logs[_Constants.SYNCHRONIZATION] = taxpayer_synchronization_logs
		taxpayer['logs'] = taxpayer_logs
		db_Taxpayer.save(taxpayer)		
	except Exception as e:
		if logger is not None:
			logger.critical(e.message)
		already_handled_exception = Already_Handled_Exception(e.message)
		raise already_handled_exception
Esempio n. 4
0
def update_initialization_data_for_taxpayer(taxpayer,initialization_execution_log,logger=None):
	try:
		forest_db = _Utilities.set_connection_to_forest_db()
		db_Taxpayer = forest_db['Taxpayer']
		# Update sl1 success execution date:
		init_date = STATUS_DATES['initialization']
		initialization_date = Datetime.now()
		taxpayer[init_date] = initialization_date
		if initialization_execution_log['initialized'] == False:
			# Update synchronization logs:
			identifier = taxpayer['identifier']
			begin_date = taxpayer['start_date']#Since taxpayer claim to be synchronized
			begin_date = begin_date.replace(hour=0, minute=0)
			end_date = initialization_date
			cfdis_in_forest_db_count = _Utilities.get_cfdis_count_in_forest_for_this_taxpayer_at_period(taxpayer,begin_date,end_date)
			initialization_log = {
				'status' : 200,
				'temporal_data' : {
					'completed_date' : initialization_date,
					'period' : {
						'year' : int(initialization_execution_log['year_initialized']),
						'month' : int(initialization_execution_log['month_initialized'])
					},
				},
				'cfdis' : {
					'new' : initialization_execution_log['stored'],
					'updated' : 0,
					'total' : cfdis_in_forest_db_count
				}# End of synchronization_log
			}# End of initialization_log
			taxpayer_logs = taxpayer['logs'] if 'logs' in taxpayer and taxpayer['logs'] is not None else {}
			taxpayer_initialization_logs = taxpayer_logs[_Constants.INITIALIZATION] if _Constants.INITIALIZATION in taxpayer_logs else []
			taxpayer_initialization_logs.append(initialization_log)
			taxpayer_logs[_Constants.INITIALIZATION] = taxpayer_initialization_logs
			taxpayer['logs'] = taxpayer_logs
		db_Taxpayer.save(taxpayer)		
	except Exception as e:
		print e
		if logger is not None:
			logger.critical(e.message)
		already_handled_exception = Already_Handled_Exception(e.message)
		raise already_handled_exception
Esempio n. 5
0
def equalize_dbs_for_a_taxpayer(forcing_period=False,
                                forcing_execution=None,
                                taxpayer=None,
                                process_logger=None,
                                process_params=None):
    try:
        indent = '    '
        log = {'before': {}, 'after': {'stored': 0, 'errors': 0}}  # End of log
        # Get params:
        identifier = taxpayer['identifier']
        created_at = taxpayer['created_at']
        created_at_lower_limit = Datetime.now() - relativedelta(months=1)
        force_start_date = True
        # process_logger.info(2*LOG_INDENT + 'FORCING START DATE')
        if created_at > created_at_lower_limit:
            begin_date = taxpayer[
                'start_date']  #Since taxpayer claim to be synchronized
            process_logger.info(2 * LOG_INDENT +
                                'Chosing start date as begin date')
        else:
            current_date = Datetime.now()
            current_year = current_date.year
            begin_date = Datetime(current_year, 1,
                                  1)  # Jan 1st of the current year
            process_logger.info(2 * LOG_INDENT + 'Chosing ' + str(begin_date) +
                                ' as begin date')
        if forcing_period:
            year = int(process_params['year'])
            month = int(process_params['month'])
            begin_date = Datetime(year, month, 1)
            begin_date = begin_date.replace(hour=0, minute=0)
            if 'all' in process_params:
                end_date = Datetime.now()  # Until now
            else:
                end_date = begin_date + relativedelta(months=1)
            process_logger.info(2 * LOG_INDENT + 'Forcing ' + str(begin_date) +
                                ' as begin date')
            process_logger.info(2 * LOG_INDENT + 'Forcing ' + str(end_date) +
                                ' as end date')
        # year =  str(Datetime.now().year)
        # months = _Utilities.get_current_fiscal_declaration_period(_Constants.TWO_MONTHS_PERIOD)
        # begin_date = Datetime(int(year),int(months[0]),1)# Since previous month (optimization introduced on Sep 8, 2015)
        begin_date = begin_date.replace(hour=0, minute=0)
        if not forcing_period:
            end_date = Datetime.now()  # Until now
        process_logger.info(2 * LOG_INDENT + 'Equalizing dbs from ' +
                            str(begin_date) + ' to ' + str(end_date))
        # Get CFDIs from Forest DB:
        cfdis_in_forest_db_count = _Utilities.get_cfdis_count_in_forest_for_this_taxpayer_at_period(
            taxpayer, begin_date, end_date)
        process_logger.info(2 * LOG_INDENT + 'Retrieving ' +
                            str(cfdis_in_forest_db_count) +
                            ' from Forest DB ... ')
        cfdis_in_forest_db = _Utilities.get_cfdis_in_forest_for_this_taxpayer_at_period(
            taxpayer,
            begin_date,
            end_date,
            limit=None,
            logger=process_logger,
            dict_result=True)
        # Get CFDIs from Corebook DB:
        try:
            cfdis_in_corebook_db_count = _Locals.get_cfdis_count_in_corebook_for_this_taxpayer_at_period(
                identifier, begin_date, end_date, logger=process_logger)
            process_logger.info(2 * LOG_INDENT + 'Retrieving ' +
                                str(cfdis_in_corebook_db_count) +
                                ' from Corebook DB ... ')
            cfdis_in_corebook_db = _Locals.get_cfdis_in_corebook_for_this_taxpayer_at_period(
                identifier,
                begin_date,
                end_date,
                limit=None,
                logger=process_logger)
        except Already_Handled_Exception as already_handled_exception:
            process_logger.info(2 * LOG_INDENT +
                                already_handled_exception.value)
            log['before'] = {
                'forest_db': 0,
                'corebook_db': 0,
                'f_but_not_in_cb': 0,
                'cb_but_not_in_f': 0
            }  # End of log
            log['after']['errors'] = log['after']['errors'] + 1
            return log
        # Log data in db:
        log['before']['forest_db'] = len(cfdis_in_forest_db)
        log['before']['corebook_db'] = len(cfdis_in_corebook_db)
        # Get missing CFDIs in Corebook:
        process_logger.info(2 * LOG_INDENT + 'Getting differences in dbs ... ')
        # _Utilities.log_cfdis_uuids(title='Forest CFDIs: ',indent=2*LOG_INDENT,cfdis=cfdis_in_forest_db,logger=process_logger,dict=True)
        # _Utilities.log_cfdis_uuids(title='Corebook CFDIs: ',indent=2*LOG_INDENT,cfdis=cfdis_in_corebook_db,logger=process_logger,dict=True)
        missing_cfdis = _Locals.get_missing_cfdis_in_each_db(
            cfdis_in_forest_db, cfdis_in_corebook_db, logger=process_logger)
        missing_cfdis_in_corebook_db = missing_cfdis['in_corebook_db']
        missing_cfdis_in_forest_db = missing_cfdis['in_forest_db']
        cfdis_with_different_status = missing_cfdis[
            'cfdis_with_different_status']
        # General status:
        log['before']['f_but_not_in_cb'] = len(missing_cfdis_in_corebook_db)
        log['before']['cb_but_not_in_f'] = len(missing_cfdis_in_forest_db)
        log['before'][
            'cfdis_with_different_status'] = cfdis_with_different_status
        process_logger.info(2 * LOG_INDENT + 'DBs Status: ')
        process_logger.info(3 * LOG_INDENT + 'Forest DB   -> ' +
                            str(log['before']['forest_db']))
        process_logger.info(3 * LOG_INDENT + 'Corebook DB -> ' +
                            str(log['before']['corebook_db']))
        process_logger.info(3 * LOG_INDENT + 'F not in CB -> ' +
                            str(log['before']['f_but_not_in_cb'] -
                                log['before']['cfdis_with_different_status']))
        process_logger.info(3 * LOG_INDENT + 'CB not in F -> ' +
                            str(log['before']['cb_but_not_in_f']))
        process_logger.info(3 * LOG_INDENT + 'Diff Status -> ' +
                            str(log['before']['cfdis_with_different_status']))
        if len(missing_cfdis_in_corebook_db) > 0:
            # _Utilities.log_cfdis_uuids(title='Missing CFDIs: ',indent=2*LOG_INDENT,cfdis=missing_cfdis_in_corebook_db,logger=process_logger)
            # log['after']['stored'] = len(missing_cfdis_in_corebook_db)
            # log['after']['errors'] = 0
            cb_summary = _Locals.store_missing_cfdis_in_corebook(
                missing_cfdis_in_corebook_db,
                identifier,
                logger=process_logger,
                limit=None)
            log['after']['stored'] = cb_summary['stored']
            log['after']['errors'] = cb_summary['errors']
            process_logger.info(2 * LOG_INDENT + 'Equalization Summary: ')
            process_logger.info(3 * LOG_INDENT + 'CFDIs stored in CB  -> ' +
                                str(log['after']['stored']))
            process_logger.info(3 * LOG_INDENT + 'Errors occurred     -> ' +
                                str(log['after']['errors']))
        if forcing_execution:
            process_logger.info(3 * LOG_INDENT +
                                'Sending telegram notification ... ')
            message = 'Ya iguale a este vato: ' + taxpayer['identifier']
            _Utilities.send_message_to_forest_telegram_contacts(
                message, logger=process_logger)
        return log
    except Already_Handled_Exception as already_handled_exception:
        raise already_handled_exception
    except Exception as e:
        equalization_logger.critical(e.message)
        already_handled_exception = Already_Handled_Exception(e.message)
        raise already_handled_exception
Esempio n. 6
0
def equalize_dbs_for_a_taxpayer(forcing_period=False,forcing_execution=None,taxpayer=None,process_logger=None,process_params=None):
	try:
		indent = '    '
		log = {
			'before' : {},
			'after' : {
				'stored' : 0,
				'errors' : 0
			}
		}# End of log
		# Get params:
		identifier = taxpayer['identifier']
		created_at = taxpayer['created_at']
		created_at_lower_limit = Datetime.now() - relativedelta(months=1)
		force_start_date = True
		# process_logger.info(2*LOG_INDENT + 'FORCING START DATE')
		if created_at > created_at_lower_limit:
			begin_date = taxpayer['start_date']#Since taxpayer claim to be synchronized
			process_logger.info(2*LOG_INDENT + 'Chosing start date as begin date')
		else:
			current_date = Datetime.now()
			current_year = current_date.year
			begin_date = Datetime(current_year,1,1)# Jan 1st of the current year
			process_logger.info(2*LOG_INDENT + 'Chosing ' + str(begin_date) + ' as begin date')
		if forcing_period:
			year = int(process_params['year'])
			month = int(process_params['month'])
			begin_date = Datetime(year,month,1)
			begin_date = begin_date.replace(hour=0, minute=0)
			if 'all' in process_params:
				end_date = Datetime.now()# Until now
			else:
				end_date = begin_date + relativedelta(months=1)
			process_logger.info(2*LOG_INDENT + 'Forcing ' + str(begin_date) + ' as begin date')
			process_logger.info(2*LOG_INDENT + 'Forcing ' + str(end_date) + ' as end date')
		# year =  str(Datetime.now().year)
		# months = _Utilities.get_current_fiscal_declaration_period(_Constants.TWO_MONTHS_PERIOD)
		# begin_date = Datetime(int(year),int(months[0]),1)# Since previous month (optimization introduced on Sep 8, 2015)
		begin_date = begin_date.replace(hour=0, minute=0)
		if not forcing_period:
			end_date = Datetime.now()# Until now
		process_logger.info(2*LOG_INDENT + 'Equalizing dbs from ' + str(begin_date) + ' to ' + str(end_date))
		# Get CFDIs from Forest DB:
		cfdis_in_forest_db_count = _Utilities.get_cfdis_count_in_forest_for_this_taxpayer_at_period(taxpayer,begin_date,end_date)
		process_logger.info(2*LOG_INDENT + 'Retrieving ' + str(cfdis_in_forest_db_count) + ' from Forest DB ... ')
		cfdis_in_forest_db = _Utilities.get_cfdis_in_forest_for_this_taxpayer_at_period(taxpayer,begin_date,end_date,limit=None,logger=process_logger,dict_result=True)	
		# Get CFDIs from Corebook DB:
		try:
			cfdis_in_corebook_db_count = _Locals.get_cfdis_count_in_corebook_for_this_taxpayer_at_period(identifier,begin_date,end_date,logger=process_logger)
			process_logger.info(2*LOG_INDENT + 'Retrieving ' + str(cfdis_in_corebook_db_count) + ' from Corebook DB ... ')
			cfdis_in_corebook_db = _Locals.get_cfdis_in_corebook_for_this_taxpayer_at_period(identifier,begin_date,end_date,limit=None,logger=process_logger)	
		except Already_Handled_Exception as already_handled_exception:
			process_logger.info(2*LOG_INDENT + already_handled_exception.value)
			log['before'] = {
				'forest_db' : 0,
				'corebook_db' : 0,
				'f_but_not_in_cb' : 0,
				'cb_but_not_in_f' : 0
			}# End of log
			log['after']['errors'] = log['after']['errors'] + 1
			return log
		# Log data in db:
		log['before']['forest_db'] = len(cfdis_in_forest_db)
		log['before']['corebook_db'] = len(cfdis_in_corebook_db)
		# Get missing CFDIs in Corebook:
		process_logger.info(2*LOG_INDENT + 'Getting differences in dbs ... ')
		# _Utilities.log_cfdis_uuids(title='Forest CFDIs: ',indent=2*LOG_INDENT,cfdis=cfdis_in_forest_db,logger=process_logger,dict=True)
		# _Utilities.log_cfdis_uuids(title='Corebook CFDIs: ',indent=2*LOG_INDENT,cfdis=cfdis_in_corebook_db,logger=process_logger,dict=True)
		missing_cfdis = _Locals.get_missing_cfdis_in_each_db(cfdis_in_forest_db,cfdis_in_corebook_db,logger=process_logger)
		missing_cfdis_in_corebook_db = missing_cfdis['in_corebook_db']
		missing_cfdis_in_forest_db = missing_cfdis['in_forest_db']
		cfdis_with_different_status = missing_cfdis['cfdis_with_different_status']
		# General status:
		log['before']['f_but_not_in_cb'] = len(missing_cfdis_in_corebook_db)
		log['before']['cb_but_not_in_f'] = len(missing_cfdis_in_forest_db)
		log['before']['cfdis_with_different_status'] = cfdis_with_different_status
		process_logger.info(2*LOG_INDENT + 'DBs Status: ')
		process_logger.info(3*LOG_INDENT + 'Forest DB   -> ' + str(log['before']['forest_db']))
		process_logger.info(3*LOG_INDENT + 'Corebook DB -> ' + str(log['before']['corebook_db']))
		process_logger.info(3*LOG_INDENT + 'F not in CB -> ' + str(log['before']['f_but_not_in_cb'] - log['before']['cfdis_with_different_status']))
		process_logger.info(3*LOG_INDENT + 'CB not in F -> ' + str(log['before']['cb_but_not_in_f']))
		process_logger.info(3*LOG_INDENT + 'Diff Status -> ' + str(log['before']['cfdis_with_different_status']))
		if len(missing_cfdis_in_corebook_db) > 0:
			# _Utilities.log_cfdis_uuids(title='Missing CFDIs: ',indent=2*LOG_INDENT,cfdis=missing_cfdis_in_corebook_db,logger=process_logger)
			# log['after']['stored'] = len(missing_cfdis_in_corebook_db)
			# log['after']['errors'] = 0
			cb_summary = _Locals.store_missing_cfdis_in_corebook(missing_cfdis_in_corebook_db,identifier,logger=process_logger,limit=None)
			log['after']['stored'] = cb_summary['stored']
			log['after']['errors'] = cb_summary['errors']
			process_logger.info(2*LOG_INDENT + 'Equalization Summary: ')
			process_logger.info(3*LOG_INDENT + 'CFDIs stored in CB  -> ' + str(log['after']['stored']))
			process_logger.info(3*LOG_INDENT + 'Errors occurred     -> ' + str(log['after']['errors']))
		if forcing_execution:
			process_logger.info(3*LOG_INDENT + 'Sending telegram notification ... ')
			message = 'Ya iguale a este vato: ' + taxpayer['identifier']
			_Utilities.send_message_to_forest_telegram_contacts(message,logger=process_logger)
		return log
	except Already_Handled_Exception as already_handled_exception:
		raise already_handled_exception
	except Exception as e:
		equalization_logger.critical(e.message)
		already_handled_exception = Already_Handled_Exception(e.message)
		raise already_handled_exception