[14.0][ADD] create expense from purchase request#2564
[14.0][ADD] create expense from purchase request#2564
Conversation
c8e372c to
03630e6
Compare
| def _compute_expense_ids(self): | ||
| for request in self: | ||
| expenses = request.line_ids.mapped("expense_ids") | ||
| request.expense_ids = expenses |
There was a problem hiding this comment.
You can do :
request.update({ "expense_ids": expenses, "expense_count": len(expenses) })
And you can remove commented line
| self.ensure_one() | ||
| expenses = self._get_related_expenses() | ||
| action = { | ||
| "name": "Expenses", |
There was a problem hiding this comment.
| "name": "Expenses", | |
| "name": _("Expenses"), |
| } | ||
|
|
||
| # Only add advance-related fields if the module is installed | ||
| if self._has_expense_advance_clearing(): |
There was a problem hiding this comment.
I would say this should be done in a glue module with advance clearing one
| raise UserError(_("The purchase has already been completed.")) | ||
|
|
||
| line_company_id = line.company_id and line.company_id.id or False | ||
| if company_id is not False and line_company_id != company_id: |
There was a problem hiding this comment.
Doesn't base model has constraints for that ?
|
|
||
| @api.model | ||
| def check_group(self, request_lines): | ||
| if len(list(set(request_lines.mapped("request_id.group_id")))) > 1: |
There was a problem hiding this comment.
You can do len(set(...)) directly
| # Create expense | ||
| expense_vals = self._prepare_expense(item) | ||
| expense = expense_obj.create(expense_vals) | ||
| expenses += expense |
There was a problem hiding this comment.
Instead of that, prefer building list of expense_vals and then call create() once.
| ) | ||
|
|
||
| @api.onchange("line_id") | ||
| def _onchange_line_id(self): |
There was a problem hiding this comment.
Prefer changing product_id to a computed field stored and readonly=False to avoid onchanges.
| def _onchange_line_id(self): | ||
| if self.line_id: | ||
| # Get advance product for filtering | ||
| advance_product = self.env.ref( |
There was a problem hiding this comment.
As said above, this should be put in a glue module that override the compute to take the product_emp_advance instead of first found product that can be expensed
| if product: | ||
| self.product_id = product.id | ||
|
|
||
| name = fields.Char(string="Description", required=True) |
There was a problem hiding this comment.
Please put all fields on top
| estimated_cost = fields.Float(string="Estimated Cost") | ||
|
|
||
| @api.onchange("product_id") | ||
| def onchange_product_id(self): |
There was a problem hiding this comment.
You can avoid onchanges by using computed/stored/readonly=False fields instead
…line_make_expense.py file.
…se_2 [IMP] purchase_request_expense: fixed and changed the code.
No description provided.