def add_account(self, account_code: str, account_role: str, account_name: str, balance_type: str, is_active: bool = False, is_locked: bool = False) -> AccountModel: validate_roles(account_role) account_model = AccountModel( code=account_code, role=account_role, name=account_name, balance_type=balance_type, coa_id=self.coa.uuid, active=is_active, locked=is_locked ) account_model.clean() account_model.save() return account_model
def get_txs_queryset(self, user_model: UserModel, to_date: str or datetime = None, from_date: str or datetime = None, activity: str = None, role: str = None, entity_slug: str = None, unit_slug: str = None, accounts: str or List[str] or Set[str] = None, posted: bool = True, exclude_zero_bal: bool = True, by_period: bool = False): activity = validate_activity(activity) role = roles.validate_roles(role) from_date, to_date = validate_dates(from_date, to_date) TransactionModel = lazy_importer.get_txs_model() # If IO is on entity model.... if isinstance(self, lazy_importer.get_entity_model()): if unit_slug: txs_qs = TransactionModel.objects.for_unit( user_model=user_model, entity_slug=entity_slug or self.slug, unit_slug=unit_slug) else: txs_qs = TransactionModel.objects.for_entity( user_model=user_model, entity_model=self) # If IO is on ledger model.... elif isinstance(self, lazy_importer.get_ledger_model()): txs_qs = TransactionModel.objects.for_ledger(user_model=user_model, ledger_model=self) elif isinstance(self, lazy_importer.get_unit_model()): if not entity_slug: raise ValidationError( 'Calling digest from Entity Unit requires entity_slug') txs_qs = TransactionModel.objects.for_unit(user_model=user_model, entity_slug=entity_slug, unit_model=self) else: txs_qs = TransactionModel.objects.none() if exclude_zero_bal: txs_qs = txs_qs.filter(amount__gt=0) if posted: txs_qs = txs_qs.posted() if from_date: txs_qs = txs_qs.from_date(from_date=from_date) if to_date: txs_qs = txs_qs.to_date(to_date=to_date) if accounts: if not isinstance(accounts, str): accounts = [accounts] txs_qs = txs_qs.for_accounts(account_list=accounts) if activity: if isinstance(activity, str): activity = [activity] txs_qs = txs_qs.for_activity(activity_list=activity) if role: if isinstance(role, str): role = [role] txs_qs = txs_qs.for_roles(role_list=role) txs_qs = txs_qs.values( 'account__uuid', 'account__balance_type', 'tx_type', 'account__code', 'account__name', 'account__role', ) if by_period: txs_qs = txs_qs.annotate( balance=Sum('amount'), dt_idx=TruncMonth('journal_entry__date'), ).order_by('journal_entry__date', 'account__uuid') else: txs_qs = txs_qs.annotate( balance=Sum('amount'), ).order_by('account__uuid') return txs_qs