Update hr_expense.py

This commit is contained in:
zainab2097 2024-09-10 16:46:59 +03:00 committed by GitHub
parent ecdd4719a0
commit d731e35605
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 47 additions and 42 deletions

View File

@ -50,48 +50,53 @@ class AccountMove(models.Model):
is_approve = fields.Boolean(defaul=False, copy=False)
def action_submit_expenses(self):
if self.analytic_account_id.is_analytic_budget:
if self.state == 'draft':
self.write({
'state': 'confirm'
})
elif self.state == 'confirm':
raise UserError(_('Please Check Budget First'))
elif self.state == 'wait_budget':
raise UserError(_("The Budget Confirmation Doesn't Approve yet"))
elif self.state == 'budget_approve':
if self.is_approve:
confirm_budget = self.env['budget.confirmation'].search([('expense_id', '=', self.id)])
confirm_budget.write({'ref': self.name})
analytic_account_id = self.analytic_account_id
budget_post = self.env['account.budget.post'].search([]).filtered(
lambda x: self.account_id in x.account_ids)
budget_lines = analytic_account_id.crossovered_budget_line.filtered(
lambda x: x.general_budget_id in budget_post and
x.crossovered_budget_id.state == 'done' and
x.date_from <= self.date <= x.date_to)
amount = budget_lines.confirm
amount += self.total_amount
budget_lines.write({'confirm': amount})
budget_lines.write({'reserve': abs(self.total_amount - budget_lines.reserve)})
todo = self.filtered(lambda x: x.payment_mode == 'own_account') or self.filtered(
lambda x: x.payment_mode == 'company_account')
sheet = self.env['hr.expense.sheet'].create({
'company_id': self.company_id.id,
'employee_id': self[0].employee_id.id,
'name': todo[0].name if len(todo) == 1 else '',
'expense_line_ids': [(6, 0, todo.ids)]
})
return {
'name': _('New Expense Report'),
'type': 'ir.actions.act_window',
'view_mode': 'form',
'res_model': 'hr.expense.sheet',
'target': 'current',
'res_id': sheet.id,
}
else:
return super(AccountMove, self).action_submit_expenses()
for record in self:
if record.analytic_account_id.is_analytic_budget:
if record.state == 'draft':
record.write({'state': 'confirm'})
elif record.state == 'confirm':
raise UserError(_('Please Check Budget First'))
elif record.state == 'wait_budget':
raise UserError(_("The Budget Confirmation Doesn't Approve yet"))
elif record.state == 'budget_approve':
if record.is_approve:
confirm_budget = self.env['budget.confirmation'].search([('expense_id', '=', record.id)])
confirm_budget.write({'ref': record.name})
analytic_account_id = record.analytic_account_id
budget_post = self.env['account.budget.post'].search([]).filtered(
lambda x: record.account_id in x.account_ids)
budget_lines = analytic_account_id.crossovered_budget_line.filtered(
lambda x: x.general_budget_id in budget_post and
x.crossovered_budget_id.state == 'done' and
x.date_from <= record.date <= x.date_to)
amount = budget_lines.confirm
amount += record.total_amount
budget_lines.write({'confirm': amount})
budget_lines.write({'reserve': abs(record.total_amount - budget_lines.reserve)})
todo = record.filtered(lambda x: x.payment_mode == 'own_account') or record.filtered(
lambda x: x.payment_mode == 'company_account')
sheet = self.env['hr.expense.sheet'].create({
'company_id': record.company_id.id,
'employee_id': record.employee_id.id,
'name': todo[0].name if len(todo) == 1 else '',
'expense_line_ids': [(6, 0, todo.ids)]
})
return {
'name': _('New Expense Report'),
'type': 'ir.actions.act_window',
'view_mode': 'form',
'res_model': 'hr.expense.sheet',
'target': 'current',
'res_id': sheet.id,
}
else:
return super(AccountMove, record).action_submit_expenses()
def button_cancel(self):
res = super(AccountMove, self).button_cancel()