From 217e7dc3d18ae8dd0f17699ba91c03d238d052b0 Mon Sep 17 00:00:00 2001 From: ronozoro Date: Sun, 6 Oct 2024 23:19:23 -0700 Subject: [PATCH] fix selecting first line of budget --- .../models/account_invoice.py | 7 +++--- .../exp_budget_check/models/hr_expense.py | 15 ++++++++----- .../models/purchase_request.py | 22 ++++++++++--------- 3 files changed, 26 insertions(+), 18 deletions(-) diff --git a/odex25_accounting/exp_budget_check/models/account_invoice.py b/odex25_accounting/exp_budget_check/models/account_invoice.py index 1739c8afe..1792e0d45 100644 --- a/odex25_accounting/exp_budget_check/models/account_invoice.py +++ b/odex25_accounting/exp_budget_check/models/account_invoice.py @@ -189,16 +189,17 @@ class AccountMove(models.Model): if line.analytic_account_id: if not line.analytic_account_id: raise ValidationError(_('Please Choose Analytic account for This Bill')) + budget_post = self.env['account.budget.post'].search([]).filtered(lambda x: line.account_id in x.account_ids) budget_lines = self.env['crossovered.budget.lines'].search( [('analytic_account_id', '=', line.analytic_account_id.id), - ('general_budget_id.account_ids', 'in', [line.account_id.id]), + ('general_budget_id', 'in', budget_post), ('crossovered_budget_id.state', '=', 'done'), ('crossovered_budget_id.date_from', '<=', self.invoice_date), ('crossovered_budget_id.date_to', '>=', self.invoice_date)]) if not budget_lines: confirmation_lines=[] else: - remain = abs(budget_lines[0].remain) + remain = abs(budget_lines.remain) tax_id = line.tax_ids[0].analytic if line.tax_ids else False amount = amount + (line.price_subtotal + (line.price_tax if tax_id else 0)) new_remain = remain - amount @@ -206,7 +207,7 @@ class AccountMove(models.Model): 'amount': line.price_subtotal + (line.price_tax if tax_id else 0), 'analytic_account_id': line.analytic_account_id.id, 'description': line.product_id.name, - 'budget_line_id': budget_lines[0].id, + 'budget_line_id': budget_lines.id, 'remain': new_remain + (line.price_subtotal + line.price_tax), 'new_balance': new_remain, 'account_id': line.account_id.id diff --git a/odex25_accounting/exp_budget_check/models/hr_expense.py b/odex25_accounting/exp_budget_check/models/hr_expense.py index a24d1ac9c..e87b530b9 100644 --- a/odex25_accounting/exp_budget_check/models/hr_expense.py +++ b/odex25_accounting/exp_budget_check/models/hr_expense.py @@ -140,14 +140,19 @@ class AccountMove(models.Model): return res def action_budget(self): - budget_lines = self.analytic_account_id.crossovered_budget_line.filtered( - lambda x: x.crossovered_budget_id.state == 'done' and - fields.Date.from_string(x.date_from) <= fields.Date.from_string(self.date) <= fields.Date.from_string(x.date_to) - ) + + budget_post = self.env['account.budget.post'].search([]).filtered(lambda x: self.account_id in x.account_ids) + budget_lines = self.env['crossovered.budget.lines'].search( + [('analytic_account_id', '=', self.analytic_account_id.id), + ('general_budget_id', 'in', budget_post), + ('crossovered_budget_id.state', '=', 'done'), + ('crossovered_budget_id.date_from', '<=', self.date), + ('crossovered_budget_id.date_to', '>=', self.date)]) + if not budget_lines: raise UserError(_('Analytic account %s is not linked with budget') % self.analytic_account_id.name) else: - budget_line = budget_lines[0] + budget_line = budget_lines remain = abs(budget_line.remain) tax_id = self.tax_ids[0].analytic if self.tax_ids else False price_before_tax= self.unit_amount* self.quantity diff --git a/odex25_accounting/initial_engagement_budget/models/purchase_request.py b/odex25_accounting/initial_engagement_budget/models/purchase_request.py index 4988e42d6..3de55f7d2 100755 --- a/odex25_accounting/initial_engagement_budget/models/purchase_request.py +++ b/odex25_accounting/initial_engagement_budget/models/purchase_request.py @@ -59,29 +59,31 @@ class PurchaseRequest(models.Model): for order in self: for rec in order.line_ids: - if not ( - rec.product_id.property_account_expense_id.id and rec.product_id.property_account_expense_id.id or rec.product_id.categ_id.property_account_expense_categ_id.id): + account_id = rec.product_id.property_account_expense_id and rec.product_id.property_account_expense_id or rec.product_id.categ_id.property_account_expense_categ_id + if not account_id: raise ValidationError( _("This product has no expense account") + ': {}'.format(rec.product_id.name)) - budget_lines = analytic_account.crossovered_budget_line.filtered( - lambda x: - x.crossovered_budget_id.state == 'done' and - rec.product_id.property_account_expense_id.id in x.general_budget_id.account_ids.ids and - fields.Date.from_string(x.date_from) <= fields.Date.from_string(self.date) and - fields.Date.from_string(x.date_to) >= fields.Date.from_string(self.date)) + budget_post = self.env['account.budget.post'].search([]).filtered(lambda x: account_id in x.account_ids) + budget_lines = self.env['crossovered.budget.lines'].search( + [('analytic_account_id', '=', analytic_account.id), + ('general_budget_id', 'in', budget_post.ids), + ('crossovered_budget_id.state', '=', 'done'), + ('crossovered_budget_id.date_from', '<=', self.date), + ('crossovered_budget_id.date_to', '>=', self.date)]) + budget_line = budget_lines.mapped('crossovered_budget_id') if len(budget_line) > 1: self.budget_id = budget_line[0].id if budget_lines: - remain = abs(budget_lines[0].remain) + remain = abs(budget_lines.remain) amount = amount + rec.line_total new_balance = remain - amount confirmation_lines.append((0, 0, { 'amount': rec.line_total, 'analytic_account_id': analytic_account.id, 'description': rec.product_id.name, - 'budget_line_id': budget_lines[0].id, + 'budget_line_id': budget_lines.id, 'remain': remain, 'new_balance': new_balance, 'account_id': rec.product_id.property_account_expense_id.id and rec.product_id.property_account_expense_id.id or rec.product_id.categ_id.property_account_expense_categ_id.id