From a209f2a6b993f46400abda1938c5cb06fb963aeb Mon Sep 17 00:00:00 2001 From: Angga Date: Mon, 23 Sep 2024 12:35:05 +0700 Subject: [PATCH] [IMP] query by journal account --- wizards/wz_ledger_bank_and_cash.py | 34 +++++++++++++++++++++-------- wizards/wz_ledger_bank_and_cash.xml | 3 +++ 2 files changed, 28 insertions(+), 9 deletions(-) diff --git a/wizards/wz_ledger_bank_and_cash.py b/wizards/wz_ledger_bank_and_cash.py index 63bf86a..14e584f 100644 --- a/wizards/wz_ledger_bank_and_cash.py +++ b/wizards/wz_ledger_bank_and_cash.py @@ -14,7 +14,15 @@ class WzLedgerBankAndCash(models.TransientModel): date_to = fields.Date("To Date") account_ids = fields.Many2many('account.account', string='Account') user_type_id = fields.Many2one('account.account.type', string='User Type', default=lambda self:self.default_user_type()) + company_id = fields.Many2one('res.company', default=lambda self: self.env.company) + journal_ids = fields.Many2many('account.journal') + @api.onchange('journal_ids') + def onchange_journal_ids(self): + if not self.journal_ids: + return + acc_ids = [x.default_account_id.id for x in self.journal_ids] + self.account_ids = acc_ids @api.constrains("date_from", "date_to") def _check_filtered(self): for rec in self: @@ -23,28 +31,35 @@ class WzLedgerBankAndCash(models.TransientModel): def generate(self): tools.drop_view_if_exists(self.env.cr, 'snk_bank_and_cash_report') - company = self.env['res.company'].browse(1) - currency_id = company.currency_id.id + company_id = self.company_id + currency_id = company_id.currency_id.id + journal_banks = self.env['account.journal'].search([('type','=','bank')]) + account_banks = [str(x.default_account_id.id) for x in journal_banks] + print(account_banks) + n_account = len(account_banks) + str_account = ",".join(account_banks) + print(str_account) + self.env.cr.execute(""" CREATE OR REPLACE VIEW snk_bank_and_cash_report AS ( - select ROW_NUMBER () OVER () as id, 'Begining Balance' as description, null as partner_id, - null as account_id, + acm.account_id as account_id, {currency_id} as currency_id, max(acm.date) as date, sum(acm.debit) as debit, sum(acm.credit) as credit, sum(acm.debit - acm.credit) as balance from account_move_line acm left join account_journal aj on acm.journal_id = aj.id - where date < '{start_date}' and aj.type in ('bank', 'cash') - + left join account_account account on acm.account_id = account.id + where date < '{start_date}' and aj.type in ('bank', 'cash') and account.id in ({str_account}) + group by acm.account_id union all select - ROW_NUMBER () OVER () + 1 as id, + ROW_NUMBER () OVER () + {n} as id, acm.name as description, null as partner_id, acm.account_id, @@ -53,8 +68,9 @@ class WzLedgerBankAndCash(models.TransientModel): acm.debit, acm.credit,acm.balance from account_move_line acm left join account_journal aj on acm.journal_id = aj.id - where date between '{start_date}' and '{end_date}' and aj.type in ('bank', 'cash') - )""".format(start_date=self.date_from,end_date=self.date_to, currency_id=currency_id)) + left join account_account account on acm.account_id = account.id + where date between '{start_date}' and '{end_date}' and aj.type in ('bank', 'cash') and account.id in ({str_account}) + )""".format(start_date=self.date_from,end_date=self.date_to, currency_id=currency_id, n=n_account,str_account=str_account)) return { 'name': _('Sillo Report Bank and Cash'), diff --git a/wizards/wz_ledger_bank_and_cash.xml b/wizards/wz_ledger_bank_and_cash.xml index cd5e93b..638779d 100644 --- a/wizards/wz_ledger_bank_and_cash.xml +++ b/wizards/wz_ledger_bank_and_cash.xml @@ -11,6 +11,9 @@ + + +