Skip to content

[14.0][ADD] create expense from purchase request#2564

Open
hitrosol wants to merge 8 commits intoOCA:14.0from
solusiaglis:14.0-ADD-purchase_request_expense_1
Open

[14.0][ADD] create expense from purchase request#2564
hitrosol wants to merge 8 commits intoOCA:14.0from
solusiaglis:14.0-ADD-purchase_request_expense_1

Conversation

@hitrosol
Copy link
Copy Markdown

No description provided.

@hitrosol hitrosol force-pushed the 14.0-ADD-purchase_request_expense_1 branch from c8e372c to 03630e6 Compare February 18, 2025 01:31
Copy link
Copy Markdown
Contributor

@rousseldenis rousseldenis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def _compute_expense_ids(self):
for request in self:
expenses = request.line_ids.mapped("expense_ids")
request.expense_ids = expenses
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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",
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"name": "Expenses",
"name": _("Expenses"),

}

# Only add advance-related fields if the module is installed
if self._has_expense_advance_clearing():
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can do len(set(...)) directly

# Create expense
expense_vals = self._prepare_expense(item)
expense = expense_obj.create(expense_vals)
expenses += expense
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of that, prefer building list of expense_vals and then call create() once.

)

@api.onchange("line_id")
def _onchange_line_id(self):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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(
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please put all fields on top

estimated_cost = fields.Float(string="Estimated Cost")

@api.onchange("product_id")
def onchange_product_id(self):
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can avoid onchanges by using computed/stored/readonly=False fields instead

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants