2024-09-19 22:48:17 +07:00
|
|
|
from odoo import models, fields, api, _, tools
|
2024-09-16 02:26:07 +07:00
|
|
|
from datetime import date, datetime, timedelta
|
|
|
|
from dateutil.relativedelta import relativedelta
|
|
|
|
from odoo.exceptions import ValidationError
|
|
|
|
|
|
|
|
class WzLedgerBankAndCash(models.TransientModel):
|
|
|
|
_name = 'wz.ledger.bank.and.cash'
|
|
|
|
_description = 'Wizard Bank and Cash Report'
|
|
|
|
|
|
|
|
def default_user_type(self):
|
|
|
|
return self.env['account.account.type'].sudo().search([('name', '=', 'Bank and Cash')], limit=1)
|
|
|
|
|
|
|
|
date_from = fields.Date("From Date")
|
|
|
|
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())
|
2024-09-23 12:35:05 +07:00
|
|
|
company_id = fields.Many2one('res.company', default=lambda self: self.env.company)
|
|
|
|
journal_ids = fields.Many2many('account.journal')
|
2024-09-16 02:26:07 +07:00
|
|
|
|
2024-09-23 12:35:05 +07:00
|
|
|
@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
|
2024-09-16 02:26:07 +07:00
|
|
|
@api.constrains("date_from", "date_to")
|
|
|
|
def _check_filtered(self):
|
|
|
|
for rec in self:
|
|
|
|
if rec.date_from > rec.date_to:
|
|
|
|
raise ValidationError(_("Invalid Date ! End date should be greater than the start date"))
|
|
|
|
|
|
|
|
def generate(self):
|
2024-09-19 22:48:17 +07:00
|
|
|
tools.drop_view_if_exists(self.env.cr, 'snk_bank_and_cash_report')
|
2024-09-23 12:35:05 +07:00
|
|
|
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)
|
|
|
|
|
2024-09-19 22:48:17 +07:00
|
|
|
|
2024-09-16 02:26:07 +07:00
|
|
|
self.env.cr.execute("""
|
2024-09-19 22:48:17 +07:00
|
|
|
CREATE OR REPLACE VIEW snk_bank_and_cash_report AS (
|
|
|
|
select
|
|
|
|
ROW_NUMBER () OVER () as id,
|
|
|
|
'Begining Balance' as description,
|
|
|
|
null as partner_id,
|
2024-09-23 12:35:05 +07:00
|
|
|
acm.account_id as account_id,
|
2024-09-19 22:48:17 +07:00
|
|
|
{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
|
2024-09-23 12:35:05 +07:00
|
|
|
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
|
2024-09-19 22:48:17 +07:00
|
|
|
union all
|
|
|
|
select
|
2024-09-23 12:35:05 +07:00
|
|
|
ROW_NUMBER () OVER () + {n} as id,
|
2024-09-19 22:48:17 +07:00
|
|
|
acm.name as description,
|
|
|
|
null as partner_id,
|
|
|
|
acm.account_id,
|
|
|
|
{currency_id} as currency_id,
|
|
|
|
acm.date,
|
|
|
|
acm.debit,
|
|
|
|
acm.credit,acm.balance
|
|
|
|
from account_move_line acm left join account_journal aj on acm.journal_id = aj.id
|
2024-09-23 12:35:05 +07:00
|
|
|
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))
|
2024-09-19 22:48:17 +07:00
|
|
|
|
2024-09-16 02:26:07 +07:00
|
|
|
return {
|
2024-09-19 22:48:17 +07:00
|
|
|
'name': _('Sillo Report Bank and Cash'),
|
2024-09-16 02:26:07 +07:00
|
|
|
'type': 'ir.actions.act_window',
|
2024-09-19 22:48:17 +07:00
|
|
|
'view_mode': 'tree',
|
|
|
|
'res_model': 'snk.bank.and.cash.report',
|
|
|
|
'views': [(False, 'tree')],
|
|
|
|
'target': False
|
2024-09-16 02:26:07 +07:00
|
|
|
}
|
2024-09-19 22:48:17 +07:00
|
|
|
|
|
|
|
|
|
|
|
# beginning_balance = 0
|
|
|
|
# balance = 0
|
|
|
|
# total_balance = 0
|
|
|
|
# start = self.date_from
|
|
|
|
# end = self.date_to
|
|
|
|
# move_line = self.env['account.move.line'].search([('date', '>=', start), ('date', '<=', end),('account_id', 'in', self.account_ids.ids), ('move_id.state', '=', 'posted')])
|
|
|
|
# bank_cash = self.env['ledger.bank.and.cash']
|
|
|
|
# move_line2 = self.env['account.move.line'].search([('date', '<=', start), ('account_id', 'in', self.account_ids.ids), ('move_id.state', '=', 'posted')])
|
|
|
|
# self.env.cr.execute("""
|
|
|
|
# delete from ledger_bank_and_cash
|
|
|
|
# """)
|
|
|
|
# if move_line:
|
|
|
|
# no = 1
|
|
|
|
# mv_line_sorted = sorted(move_line, key=lambda x: x["date"])
|
|
|
|
# for account in self.account_ids:
|
|
|
|
# for rec in mv_line_sorted:
|
|
|
|
# if rec.account_id.id == account.id:
|
|
|
|
# beginning_balance = sum(move_line2.filtered(lambda x : x.account_id.id == rec.account_id.id).mapped('balance'))
|
|
|
|
# if no == 1:
|
|
|
|
# balance = beginning_balance + (rec.debit - rec.credit)
|
|
|
|
# total_balance = balance
|
|
|
|
# else:
|
|
|
|
# total_balance += rec.balance
|
|
|
|
# bank_cash.create({
|
|
|
|
# 'date': rec.date,
|
|
|
|
# 'account_id': rec.account_id.id,
|
|
|
|
# 'move_id': rec.move_id.id,
|
|
|
|
# 'partner_id': rec.partner_id.id,
|
|
|
|
# 'currency_id': rec.currency_id.id,
|
|
|
|
# 'beginning_balance': beginning_balance,
|
|
|
|
# 'balance': total_balance,
|
|
|
|
# 'debit': rec.debit,
|
|
|
|
# 'credit': rec.credit,
|
|
|
|
# })
|
|
|
|
# no += 1
|
|
|
|
# no = 1
|
|
|
|
# if bank_cash:
|
|
|
|
# bank_cash.sorted(key=lambda r: (r.date, r.account_id))
|
|
|
|
# return {
|
|
|
|
# 'name': _("Cash and Bank Book Period %s s/d %s" %(self.date_from.strftime("%d-%m-%Y"), self.date_to.strftime("%d-%m-%Y"))),
|
|
|
|
# 'view_mode': 'list',
|
|
|
|
# 'view_id': self.env.ref('snk_accounting_report.accounting_report_ledger_bank_and_cash_view_tree').id,
|
|
|
|
# 'res_model': 'ledger.bank.and.cash',
|
|
|
|
# 'type': 'ir.actions.act_window',
|
|
|
|
# 'context': {
|
|
|
|
# 'group_by': ['account_id'],
|
|
|
|
# }
|
|
|
|
# }
|