"""Validate adjustment row.""" from adjustment.data_class import DataClass from adjustment.validate.contract import Contract class Validation: def __init__(self, adjustment): self.__account_id = adjustment[0] self.__contract_id = adjustment[1] self.__upc = adjustment[2] self.__amount = adjustment[3] self.__currency = adjustment[4] self.__activity_year = adjustment[5] self.__activity_month = adjustment[6] self.__apply_to_statement_year = adjustment[7] self.__apply_to_statement_month = adjustment[8] self.__adjustment_type = adjustment[9] self.__comment = adjustment[10] self.__distribution_type = adjustment[11] self._validation_errors = list() def validate_adjustment(self): self.validate_account() self.validate_contract() self.validate_upc() self.validate_amount() self.validate_currency() self.validate_activity_date() self.validate_apply_date() self.validate_adjustment_type() self.validate_comment() self.validate_distribution_type() return self._validation_errors def validate_account(self): if not self.__account_id: self._validation_errors.append('Account Id required') if self.__account_id and str(self.__account_id) not in DataClass.account_ids: self._validation_errors.append(f'{self.__account_id} Account ID does not exist in Abacus') def validate_contract(self): if not self.__contract_id: self._validation_errors.append('Contract Id required') if self.__contract_id and str(self.__contract_id) not in DataClass.contract_ids: self._validation_errors.append(f'{self.__contract_id} Contract ID does not exist in Abacus') def validate_contract_upc(self): """ 1. This function first verify if upc is attached to a contract. 2. If 1st step fails then removes the first/all leading zeros from the upc and then verifies if upc is attached to a contract. 3. If 2nd step fails then checks if upc is "display upc"(gets display upc from art-relation db). if yes then gets the value of upc for display upc and check if that upc is attached to a contract. 4. If 3rd step fails then check if labels of contract owns that upc or not. """ contract = Contract(self.__contract_id, self.__upc) if contract.check_contract_has_upc_attached(): return True if contract.check_contract_has_upc_attached_ignore_leading_zero(): return True if contract.check_contract_has_upc_attached_all_ignore_leading_zero(): return True if contract.check_upc_display_upc_mapping(): return True if contract.check_contract_label_owns_upc(): return True return False def validate_upc(self): if self.__upc: upc = str(self.__upc) if len(upc) < 12 or len(upc) > 13: self._validation_errors.append('UPC must be 12 or 13 characters long') return True if not self.__distribution_type: self._validation_errors.append('UPC must be blank if distribution type is blank') if self.__distribution_type and not self.validate_contract_upc(): self._validation_errors.append('UPC is not on this contract') def validate_amount(self): if not self.__amount: self._validation_errors.append('Amount is required / Amount cannot be zero') def validate_currency(self): supported_currencies = ['AUD','CAD','CHF','DKK','EUR','GBP','JPY','KRW', 'NOK','NZD','SEK','USD'] if not self.__currency: self._validation_errors.append('Currency code is required') if self.__currency and self.__currency.upper() not in supported_currencies: self._validation_errors.append('Currency code is not supported') def validate_distribution_type(self): supported_types = ['digital', 'physical'] if self.__distribution_type and self.__distribution_type.lower() not in supported_types: self._validation_errors.append('Distribution type not supported') if self.__distribution_type and not self.__upc: self._validation_errors.append('Distribution type must be blank if UPC is blank') def validate_comment(self): if self.__comment and len(self.__comment) > 180: self._validation_errors.append('Comment maximum length is 180 characters') def validate_adjustment_type(self): if not self.__adjustment_type: self._validation_errors.append('Adjustment type is required') if self.__adjustment_type and self.__adjustment_type not in DataClass.adjustment_types: self._validation_errors.append('Adjustment type is not supported by Abacus') def validate_activity_month(self): if not self.__activity_month: self._validation_errors.append('Activity month is required') return False if self.__activity_month < 1 or self.__activity_month > 12: self._validation_errors.append('Activity month must be a valid month number') return False return True def validate_apply_month(self): if not self.__apply_to_statement_month: self._validation_errors.append('Apply month is required') return False if self.__apply_to_statement_month < 1 or self.__apply_to_statement_month > 12: self._validation_errors.append('Apply month must be a valid month number') return False return True def validate_activity_year(self): if not self.__activity_year: self._validation_errors.append('Activity year is required') return False if self.__activity_year and len(str(self.__activity_year)) != 4: self._validation_errors.append('Activity year must be four digits') return False return True def validate_apply_year(self): if not self.__apply_to_statement_year: self._validation_errors.append('Apply year is required') return False if self.__apply_to_statement_year and len(str(self.__apply_to_statement_year)) != 4: self._validation_errors.append('Apply year must be four digits') return False return True def validate_activity_date(self): activity_year = self.validate_activity_year() activity_month = self.validate_activity_month() if not (activity_year and activity_month): return True statement_periods = DataClass.statement_periods for statement_period in statement_periods: if ( self.__activity_month == statement_period['statement_month'] and self.__activity_year == statement_period['statement_year'] ): return True self._validation_errors.append('Activity period does not exist in Abacus') def validate_apply_date(self): apply_year = self.validate_apply_year() apply_month = self.validate_apply_month() if not (apply_year and apply_month): return True statement_periods = DataClass.statement_periods for statement_period in statement_periods: if ( self.__apply_to_statement_month == statement_period['statement_month'] and self.__apply_to_statement_year == statement_period['statement_year'] ): return True self._validation_errors.append('Apply period is either missing in Abacus or already closed')