"""Transaction Model. Models the `dig_sales_detail` rows supplied from StatementDB. Enforces basic validation and structure. """ from accounting.models.base_model import BaseModel class Transaction(BaseModel): """Transaction data model. Fields: statement_id (int): The ID of the statement that this txn came from. date (str): Date of the txn upc (int): Unique ID of the top level product. cd (int): The cd number, starting at 0, that the track belongs to. track_id (int): The ID of the track that was spun. qty (int): The number of spins/items/whathaveyou unit_price (float): total (float): trans_type (str): retail_price (float): original_price (float): discount (float): statement_detail_id (int): Unique ID of the txn assigned by our system. period_id (int): The numeric accounting period ID. customer_id (int): Numeric ID of the customer/store vendor_id (int): The ID of the label that has a claim on the track. owner_id (int): Numeric ID of the owner. """ fields = { 'statement_id': 0, 'date': '', 'upc': 0, 'cd': 0, 'track_id': 0, 'qty': 0, 'unit_price': 0, 'total': 0, 'trans_type': '', 'retail_price': 0, 'original_price': 0, 'discount': 0, 'statement_detail_id': 0, 'period_id': 0, 'customer_id': 0, 'vendor_id': 0, 'owner_id': 0, 'original_currency_id': 0, 'activity_rate': 0 } int_fields = [ 'statement_id', 'upc', 'cd', 'track_id', 'qty', 'statement_detail_id', 'period_id', 'customer_id', 'vendor_id', 'owner_id', 'original_currency_id'] float_fields = [ 'unit_price', 'total', 'retail_price', 'original_price', 'discount', 'activity_rate'] required_fields = [ 'statement_detail_id', 'period_id', 'customer_id', 'statement_id', 'vendor_id', 'owner_id', 'date', 'upc', 'qty', 'total', 'trans_type', 'original_currency_id', 'activity_rate'] def validate(self, errors=[]): """Validate transaction data. Validates that unit_price and original_price are set if trans_type is not one of a few values. @see BaseModel.validate() """ if not errors: errors = [] if self._instance_fields['trans_type'] not in ['AP', 'AS', 'AV']: if not self._instance_fields['unit_price']: errors.append('"unit_price" is not set') if not self._instance_fields['original_price']: errors.append('"original_price" is not set') return super().validate(errors)