Merge pull request #1213 from expsa/pla-4554-do-related-fixes
fix issues with expense and enhance
This commit is contained in:
commit
10ea21709c
File diff suppressed because it is too large
Load Diff
|
|
@ -3,8 +3,12 @@ from odoo import models,fields
|
|||
|
||||
class AccountMoveLine(models.Model):
|
||||
_inherit = 'account.move.line'
|
||||
benefit_family_id = fields.Many2one(comodel_name='grant.benefit', string='Benefit Family')
|
||||
family_confirm_id = fields.Many2one(comodel_name='confirm.benefit.expense', string='Benefit Family')
|
||||
|
||||
class AccountMove(models.Model):
|
||||
_inherit = 'account.move'
|
||||
family_confirm_id = fields.Many2one(comodel_name='confirm.benefit.expense', string='Benefit Family')
|
||||
family_confirm_id = fields.Many2one(comodel_name='confirm.benefit.expense', string='Benefit Family')
|
||||
|
||||
benefit_family_ids = fields.Many2many(comodel_name='grant.benefit',relation='account_move_grant_family_rel',
|
||||
column1='move_id',column2='family_id', string='Benefit Family')
|
||||
|
|
@ -95,7 +95,7 @@ class GrantBenefitProfile(models.Model):
|
|||
user_id = fields.Many2one('res.users', string="User")
|
||||
password = fields.Char('Password')
|
||||
# Category And Family
|
||||
benefit_category_id = fields.Many2one('benefit.category', string='Benefit Category', compute="get_benefit_category")
|
||||
benefit_category_id = fields.Many2one('benefit.category', string='Benefit Category', compute="get_benefit_category",store=True)
|
||||
family_id = fields.Many2one('benefit.family', string='Benefit Family')
|
||||
# address
|
||||
housing_id = fields.Many2one('benefit.housing', string='Benefit Housing')
|
||||
|
|
@ -358,6 +358,7 @@ class GrantBenefitProfile(models.Model):
|
|||
], string='state', default="draft", tracking=True)
|
||||
branch_id = fields.Many2one('hr.department', string="Branch", domain=[('is_branch', '=', True)])
|
||||
district_id = fields.Many2one('res.districts', string="District", domain="[('branch_id','=',branch_id)]")
|
||||
meal_card = fields.Boolean(string="Meal Card",related="district_id.meal_card", store=True,related_sudo=True)
|
||||
attachment_ids = fields.One2many('ir.attachment', 'benefit_id')
|
||||
family_debits_ids = fields.One2many('family.debits', 'benefit_id')
|
||||
researcher_id = fields.Many2one("committees.line", string="Researcher")
|
||||
|
|
@ -415,6 +416,52 @@ class GrantBenefitProfile(models.Model):
|
|||
family_monthly_meals = fields.Float(string="Family Monthly Meals", compute='_get_family_monthly_values')
|
||||
family_monthly_clotting = fields.Float(string="Family Monthly Clotting", compute='_get_family_monthly_values')
|
||||
total_family_expenses = fields.Float(string="Total Family Expenses", compute='_get_family_monthly_values')
|
||||
total_move_lines = fields.Integer(string="Total Move Lines", compute='_get_total_move_lines')
|
||||
invoices_count = fields.Integer(string="Invoices Count", compute='_get_invoices_count')
|
||||
|
||||
def _get_invoices_count(self):
|
||||
for rec in self:
|
||||
rec.invoices_count = self.env['account.move'].search_count([
|
||||
('benefit_family_ids', 'in', [rec.id]),('move_type','=','in_invoice')
|
||||
])
|
||||
|
||||
|
||||
def _get_total_move_lines(self):
|
||||
for rec in self:
|
||||
rec.total_move_lines = self.env['account.move.line'].search_count([
|
||||
('benefit_family_id', '=', rec.id),
|
||||
])
|
||||
|
||||
def action_open_related_move_line_records(self):
|
||||
""" Opens a tree view with related records filtered by a dynamic domain """
|
||||
move_lines = self.env['account.move.line'].search([
|
||||
('benefit_family_id', '=', self.id),
|
||||
]).ids
|
||||
action = {
|
||||
'type': 'ir.actions.act_window',
|
||||
'name': 'Move Lines',
|
||||
'res_model': 'account.move.line',
|
||||
'view_mode': 'tree,form',
|
||||
'domain': [('id', 'in', move_lines)],
|
||||
'target': 'current',
|
||||
}
|
||||
return action
|
||||
|
||||
|
||||
def action_open_related_invoice_records(self):
|
||||
""" Opens a tree view with related records filtered by a dynamic domain """
|
||||
invoices = self.env['account.move'].search([
|
||||
('benefit_family_ids', 'in', [self.id]),('move_type','=','in_invoice')
|
||||
]).ids
|
||||
action = {
|
||||
'type': 'ir.actions.act_window',
|
||||
'name': 'Invoices',
|
||||
'res_model': 'account.move',
|
||||
'view_mode': 'tree,form',
|
||||
'domain': [('id', 'in', invoices)],
|
||||
'target': 'current',
|
||||
}
|
||||
return action
|
||||
|
||||
def _get_family_monthly_values(self):
|
||||
validation_setting = self.env["family.validation.setting"].search([], limit=1)
|
||||
|
|
|
|||
|
|
@ -46,8 +46,8 @@ class ConfirmBenefitExpense(models.Model):
|
|||
"SEPA Direct Debit: Get paid in the SEPA zone thanks to a mandate your partner will have granted to you. Module account_sepa is necessary.\n")
|
||||
available_payment_method_line_ids = fields.Many2many('account.payment.method.line',
|
||||
compute='_compute_payment_method_line_fields')
|
||||
total_move_lines = fields.Integer(string="Total Move Lines", compute='_get_total_move_lines')
|
||||
total_moves = fields.Integer(string="Total Moves", compute='_get_total_move_lines')
|
||||
total_moves = fields.Integer(string="Total Move Lines", compute='_get_total_move_lines')
|
||||
total_invoices = fields.Integer(string="Total Moves", compute='_get_total_move_lines')
|
||||
|
||||
@api.constrains('expense_type', 'cash_expense', 'meal_expense', 'cloth_expense')
|
||||
def _constraint_check_at_least_one_expense(self):
|
||||
|
|
@ -58,41 +58,39 @@ class ConfirmBenefitExpense(models.Model):
|
|||
|
||||
def _get_total_move_lines(self):
|
||||
for rec in self:
|
||||
rec.total_move_lines = self.env['account.move.line'].search_count([
|
||||
('family_confirm_id', '=', rec.id),
|
||||
])
|
||||
rec.total_moves = self.env['account.move'].search_count([
|
||||
('family_confirm_id', '=', rec.id),
|
||||
('family_confirm_id', '=', rec.id), ('move_type', '!=', 'in_invoice')
|
||||
])
|
||||
rec.total_invoices = self.env['account.move'].search_count([
|
||||
('family_confirm_id', '=', rec.id), ('move_type', '=', 'in_invoice')
|
||||
])
|
||||
|
||||
def action_open_related_move_line_records(self):
|
||||
""" Opens a tree view with related records filtered by a dynamic domain """
|
||||
move_lines = self.env['account.move.line'].search([
|
||||
('family_confirm_id', '=', self.id),
|
||||
]).ids
|
||||
|
||||
return {
|
||||
'name': _('Journal Items'),
|
||||
'type': 'ir.actions.act_window',
|
||||
'res_model': 'account.move.line',
|
||||
'view_mode': 'tree',
|
||||
'view_id': self.env.ref('account.view_move_line_tree_grouped').id,
|
||||
'domain': [('id', 'in', move_lines)],
|
||||
}
|
||||
|
||||
def action_open_related_move_records(self):
|
||||
""" Opens a tree view with related records filtered by a dynamic domain """
|
||||
move_lines = self.env['account.move'].search([
|
||||
('family_confirm_id', '=', self.id),
|
||||
moves = self.env['account.move'].search([
|
||||
('family_confirm_id', '=', self.id), ('move_type', '!=', 'in_invoice')
|
||||
]).ids
|
||||
|
||||
return {
|
||||
'name': _('Moves'),
|
||||
'type': 'ir.actions.act_window',
|
||||
'res_model': 'account.move',
|
||||
'view_mode': 'tree,form',
|
||||
'domain': [('id', 'in', moves)],
|
||||
}
|
||||
|
||||
def action_open_related_invoice_records(self):
|
||||
""" Opens a tree view with related records filtered by a dynamic domain """
|
||||
invoices = self.env['account.move'].search([
|
||||
('family_confirm_id', '=', self.id), ('move_type', '=', 'in_invoice')
|
||||
]).ids
|
||||
|
||||
return {
|
||||
'name': _('Vendor Bills'),
|
||||
'type': 'ir.actions.act_window',
|
||||
'res_model': 'account.move',
|
||||
'view_mode': 'tree',
|
||||
'view_id': self.env.ref('account.view_in_invoice_tree').id,
|
||||
'domain': [('id', 'in', move_lines)],
|
||||
'view_mode': 'tree,form',
|
||||
'domain': [('id', 'in', invoices)],
|
||||
}
|
||||
|
||||
@api.depends('available_payment_method_line_ids')
|
||||
|
|
@ -118,17 +116,30 @@ class ConfirmBenefitExpense(models.Model):
|
|||
|
||||
@api.onchange('expense_type', 'date')
|
||||
def _onchange_expense_type(self):
|
||||
"""Ensure that families can only be added to one expense type per month."""
|
||||
"""Restrict families to a single expense type per month."""
|
||||
journal_domain = []
|
||||
|
||||
# Define journal domains based on expense type
|
||||
if self.expense_type == 'family_expense':
|
||||
journal_domain = [('type', 'in', ['bank', 'cash'])]
|
||||
elif self.expense_type == 'family_invoice':
|
||||
journal_domain = [('type', 'in', ['purchase'])]
|
||||
else:
|
||||
journal_domain = []
|
||||
|
||||
# Define base domain for family selection
|
||||
validation_setting = self.env["family.validation.setting"].search([], limit=1)
|
||||
|
||||
base_domain = [('state', 'in', ('second_approve', 'temporarily_suspend', 'suspend'))]
|
||||
min_income = validation_setting.benefit_category_ids.mapped('mini_income_amount')
|
||||
max_income = validation_setting.benefit_category_ids.mapped('max_income_amount')
|
||||
base_domain.extend([('member_income', '>=', min(min_income)), ('member_income', '<=', max(max_income))])
|
||||
if self.expense_type == 'family_invoice':
|
||||
base_domain.append(('meal_card', '=', True))
|
||||
|
||||
if self.date:
|
||||
# Calculate the start date for the past month range
|
||||
month_ago = self.date - relativedelta(months=1)
|
||||
# Search for records of the same expense_type within the last month
|
||||
|
||||
# Search for conflicting records of the same expense type within the past month
|
||||
conflicting_records = self.search([
|
||||
('date', '>=', month_ago),
|
||||
('date', '<=', self.date),
|
||||
|
|
@ -136,31 +147,17 @@ class ConfirmBenefitExpense(models.Model):
|
|||
])
|
||||
|
||||
if conflicting_records:
|
||||
# Collect the family IDs that are already in records of the same expense type within the past month
|
||||
# Gather the family IDs that are already associated with the same expense type
|
||||
conflicting_family_ids = conflicting_records.mapped('family_ids').ids
|
||||
base_domain.append(('id', 'not in', conflicting_family_ids))
|
||||
|
||||
return {
|
||||
'domain': {
|
||||
'family_ids': [('id', 'not in', conflicting_family_ids),
|
||||
('state', 'in', ('second_approve', 'temporarily_suspend', 'suspend'))],
|
||||
'journal_id': journal_domain,
|
||||
}
|
||||
}
|
||||
else:
|
||||
# No conflicting records found; no restrictions on family selection
|
||||
return {
|
||||
'domain': {
|
||||
'family_ids': [('state', 'in', ('second_approve', 'temporarily_suspend', 'suspend'))],
|
||||
'journal_id': journal_domain,
|
||||
}
|
||||
}
|
||||
else:
|
||||
return {
|
||||
'domain': {
|
||||
'family_ids': [('state', 'in', ('second_approve', 'temporarily_suspend', 'suspend'))],
|
||||
'journal_id': journal_domain,
|
||||
}
|
||||
# Return domain restrictions
|
||||
return {
|
||||
'domain': {
|
||||
'family_ids': base_domain,
|
||||
'journal_id': journal_domain,
|
||||
}
|
||||
}
|
||||
|
||||
def action_confirm_selected(self):
|
||||
for rec in self:
|
||||
|
|
@ -203,8 +200,8 @@ class ConfirmBenefitExpense(models.Model):
|
|||
'name': f'{family.name}/{family.code}', # Family name as the description
|
||||
'account_id': account_id.id, # The same account for all lines
|
||||
'quantity': 1, # Qty is 1
|
||||
'price_unit': family.family_monthly_meals, # Amount is 1
|
||||
'analytic_account_id': family.branch_id.analytic_account_id.id, # The same analytic account
|
||||
'price_unit': family.benefit_member_count * validation_setting.meal_expense,
|
||||
'analytic_account_id': family.branch_id.analytic_account_id.id,
|
||||
}))
|
||||
|
||||
# Create the invoice
|
||||
|
|
@ -213,6 +210,7 @@ class ConfirmBenefitExpense(models.Model):
|
|||
'partner_id': validation_setting.meal_partner_id.id, # The partner for the invoice
|
||||
'invoice_date': rec.date, # The date of the invoice
|
||||
'family_confirm_id': rec.id, # Link to the family expense record
|
||||
'benefit_family_ids': [(6, 0, rec.family_ids.ids)], # Link to the families
|
||||
'journal_id': rec.journal_id.id, # The journal for the invoice
|
||||
'invoice_line_ids': invoice_lines, # The invoice lines
|
||||
'ref': rec.name, # The reference for the invoice
|
||||
|
|
@ -228,14 +226,15 @@ class ConfirmBenefitExpense(models.Model):
|
|||
entry_lines = []
|
||||
|
||||
expense_types = [
|
||||
('meal', 'family_monthly_meals', validation_setting.meal_expense_account_id.id),
|
||||
('cash', 'family_monthly_income', validation_setting.cash_expense_account_id.id),
|
||||
('clothing', 'family_monthly_clotting', validation_setting.clothing_expense_account_id.id),
|
||||
('meal', 'meal_expense', validation_setting.meal_expense_account_id.id),
|
||||
('cash', 'cash_expense', validation_setting.cash_expense_account_id.id),
|
||||
('clothing', 'clothing_expense', validation_setting.clothing_expense_account_id.id),
|
||||
]
|
||||
|
||||
for expense_type, field, debit_account_id in expense_types:
|
||||
amount = getattr(benefit, field, 0.0)
|
||||
if benefit.district_id.meal_card and expense_type == 'meal' and not self.meal_expense:
|
||||
amount = benefit.benefit_member_count * getattr(validation_setting, field, 0.0)
|
||||
if (benefit.district_id.meal_card and expense_type == 'meal') or (
|
||||
not self.meal_expense and expense_type == 'meal'):
|
||||
continue
|
||||
if not self.cash_expense and expense_type == 'cash':
|
||||
continue
|
||||
|
|
@ -253,6 +252,7 @@ class ConfirmBenefitExpense(models.Model):
|
|||
return (0, 0, {
|
||||
'name': name,
|
||||
'family_confirm_id': self.id,
|
||||
'benefit_family_id': benefit.id,
|
||||
'partner_id': benefit.partner_id.id,
|
||||
'analytic_account_id': benefit.branch_id.analytic_account_id.id,
|
||||
'account_id': account_id,
|
||||
|
|
@ -265,6 +265,7 @@ class ConfirmBenefitExpense(models.Model):
|
|||
return (0, 0, {
|
||||
'name': name,
|
||||
'family_confirm_id': self.id,
|
||||
'benefit_family_id': benefit.id,
|
||||
'partner_id': benefit.partner_id.id,
|
||||
'analytic_account_id': benefit.branch_id.analytic_account_id.id,
|
||||
'account_id': account_id,
|
||||
|
|
@ -278,6 +279,8 @@ class ConfirmBenefitExpense(models.Model):
|
|||
'journal_id': journal_id,
|
||||
'date': self.date,
|
||||
'ref': self.name,
|
||||
'family_confirm_id': self.id,
|
||||
'benefit_family_ids': [(6, 0, self.family_ids.ids)],
|
||||
'line_ids': lines,
|
||||
}
|
||||
self.env['account.move'].create(move_vals)
|
||||
|
|
|
|||
|
|
@ -150,6 +150,13 @@
|
|||
<button icon="fa-usd">
|
||||
<field name="total_income" string="Total Income" widget="statinfo"/>
|
||||
</button>
|
||||
<button icon="fa-usd" name="action_open_related_move_line_records" type="object">
|
||||
<field name="total_move_lines" string="Move Lines" widget="statinfo"/>
|
||||
</button>
|
||||
<button icon="fa-usd" name="action_open_related_invoice_records" type="object">
|
||||
<field name="invoices_count" string="Invoices" widget="statinfo"/>
|
||||
</button>
|
||||
|
||||
<!-- <button name="open_expenses"-->
|
||||
<!-- type="object" class="oe_stat_button" icon="fa-money">-->
|
||||
<!-- <field string="Expenses" name="expenses_total" widget="statinfo"/>-->
|
||||
|
|
@ -172,8 +179,10 @@
|
|||
</div>
|
||||
<group>
|
||||
<group>
|
||||
<field name="meal_card" readonly="1"/>
|
||||
<field name="email"
|
||||
attrs="{'readonly':[('state','not in',['draft','complete_info','edit_info'])],'required':True}"/>
|
||||
|
||||
<field name="phone"
|
||||
attrs="{'readonly':[('state','not in',['draft','complete_info','edit_info'])],'required':True}"/>
|
||||
<field name="phone2"
|
||||
|
|
@ -894,6 +903,7 @@
|
|||
<tree string=" Benefit">
|
||||
<field name="name"/>
|
||||
<field name="code"/>
|
||||
<field name="meal_card"/>
|
||||
<field name="request_producer"/>
|
||||
<field name="father_id_number"/>
|
||||
<field name="sms_phone"/>
|
||||
|
|
|
|||
|
|
@ -27,13 +27,13 @@
|
|||
</header>
|
||||
<sheet>
|
||||
<div class="oe_button_box" name="button_box">
|
||||
<button icon="fa-usd" name="action_open_related_move_line_records" type="object"
|
||||
attrs="{'invisible':[('expense_type','!=','family_expense')]}">
|
||||
<field name="total_move_lines" string="Moves" widget="statinfo"/>
|
||||
</button>
|
||||
<button icon="fa-usd" name="action_open_related_move_records" type="object"
|
||||
attrs="{'invisible':[('expense_type','!=','family_expense')]}">
|
||||
<field name="total_moves" string="Moves" widget="statinfo"/>
|
||||
</button>
|
||||
<button icon="fa-usd" name="action_open_related_invoice_records" type="object"
|
||||
attrs="{'invisible':[('expense_type','!=','family_invoice')]}">
|
||||
<field name="total_moves" string="Invoices" widget="statinfo"/>
|
||||
<field name="total_invoices" string="Invoices" widget="statinfo"/>
|
||||
</button>
|
||||
</div>
|
||||
<group>
|
||||
|
|
|
|||
Loading…
Reference in New Issue