-
Notifications
You must be signed in to change notification settings - Fork 2.7k
[IMP] last_ordered_products: improved sale and purchase by adding this module #654
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
ppch-odoo
wants to merge
7
commits into
odoo:18.0
Choose a base branch
from
odoo-dev:18.0-imp-last-ordered-products-ppch
base: 18.0
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 6 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
fe709da
[IMP] last_ordered_products: improved sale and purchase by adding thi…
ppch-odoo 484372a
[IMP] last_ordered_products: improved sale and purchase by adding thi…
ppch-odoo 1932940
[IMP] last_ordered_products: improved sale and purchase by adding thi…
ppch-odoo 7922c90
[IMP] last_ordered_products: testcases added
ppch-odoo 8f87feb
[IMP] last_ordered_products: web_tour added
ppch-odoo 9e01f9e
[IMP] last_ordered_products: code improvement
ppch-odoo 558af97
[IMP] last_ordered_products: Bridge modules created
ppch-odoo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| from . import models |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| { | ||
| 'name': "Last Ordered Products", | ||
| 'version': '1.0', | ||
| 'depends': ['sale_management', 'purchase', 'stock'], | ||
| 'author': "Parthav Chodvadiya (PPCH)", | ||
| 'category': '', | ||
| 'description': """ | ||
| Show last ordered products for customers in sale order and for vendors in purchase order | ||
| """, | ||
| 'data': [ | ||
| 'data/last_ordered_products_tour.xml', | ||
| 'views/account_move_form.xml', | ||
| 'views/sale_order_form.xml', | ||
| 'views/purchase_order_form.xml', | ||
| 'views/product_views.xml', | ||
| ], | ||
| 'assets': { | ||
| 'web.assets_backend': [ | ||
| 'last_ordered_products/static/src/**/*.js', | ||
| 'last_ordered_products/static/src/**/*.xml', | ||
| ], | ||
| }, | ||
| 'license': 'LGPL-3', | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| <?xml version="1.0" encoding="utf-8"?> | ||
| <odoo> | ||
| <record id="last_ordered_products_tour" model="web_tour.tour"> | ||
| <field name="name">last_ordered_products_tour</field> | ||
| <field name="sequence">1</field> | ||
| <field name="rainbow_man_message">Congrats, best of luck catching such big fish! :)</field> | ||
| </record> | ||
| </odoo> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| from . import product_product | ||
| from . import product_template | ||
| from . import sale_order | ||
| from . import sale_order_line |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| from datetime import datetime | ||
| from odoo import api, fields, models | ||
| from odoo.osv import expression | ||
|
|
||
|
|
||
| class ProductProduct(models.Model): | ||
| _inherit = 'product.product' | ||
|
|
||
| last_order_time = fields.Datetime(compute='_compute_last_order_time') | ||
| last_date_str = fields.Char(compute='_compute_last_order_time') | ||
|
|
||
| @api.depends_context('order_id') | ||
| def _compute_last_order_time(self): | ||
| """Compute the last order time for each product based on the latest sale or purchase.""" | ||
|
|
||
| order_type = False | ||
| if self.env.context.get('active_model') == 'sale.order.line': | ||
| partner_id = self.env['sale.order'].browse(self.env.context.get('order_id')).partner_id.id | ||
| order_type = 'sale' | ||
| elif self.env.context.get('active_model') == 'purchase.order.line': | ||
| partner_id = self.env['purchase.order'].browse(self.env.context.get('order_id')).partner_id.id | ||
| order_type = 'purchase' | ||
| elif self.env.context.get('active_model') == 'account.journal': | ||
| active_id = self.env.context.get('active_id') | ||
| if active_id: | ||
| order_type = self.env['account.journal'].browse(active_id).type | ||
| partner_id = self.env.context.get('partner_id') or self.env.context.get('default_partner_id') | ||
| else: | ||
| partner_id = self.env.context.get('partner_id') | ||
| order_type = self.env.context.get('order_type') | ||
|
||
|
|
||
| if not partner_id: | ||
| for record in self: | ||
| record.last_order_time = False | ||
| record.last_date_str = False | ||
| return | ||
|
|
||
| last_ordered_products = {} | ||
|
|
||
| if order_type == 'sale': | ||
| last_ordered_products = self._get_last_sold_products(partner_id) | ||
| elif order_type == 'purchase': | ||
| last_ordered_products = self._get_last_purchased_products(partner_id) | ||
|
|
||
| for record in self: | ||
| last_date = last_ordered_products.get(record.id) | ||
|
|
||
| record.last_order_time = last_date if last_date else False | ||
| record.last_date_str = self.env['product.template']._get_time_ago_string(last_date) if last_date else False | ||
|
|
||
| def _get_last_sold_products(self, partner_id): | ||
| '''Fetch products last sold to the given customer''' | ||
|
|
||
| sale_order_lines = self.env['sale.order.line'].search([ | ||
| ('order_id.partner_id', '=', partner_id) | ||
| ]) | ||
|
|
||
| if not sale_order_lines: | ||
| return {} | ||
|
|
||
| invoices = self.env['account.move'].search([ | ||
| ('partner_id', '=', partner_id), | ||
| ('invoice_origin', 'in', sale_order_lines.order_id.mapped('name')) | ||
| ]) | ||
|
|
||
| last_sale_ordered_products = {} | ||
| invoice_dates = {inv.invoice_origin: inv.create_date for inv in invoices} | ||
| for sol in sale_order_lines: | ||
| last_date = invoice_dates.get(sol.order_id.name) | ||
| if last_date: | ||
| product_id = sol.product_id.id | ||
| if product_id not in last_sale_ordered_products or last_date > last_sale_ordered_products[product_id]: | ||
| last_sale_ordered_products[product_id] = last_date | ||
|
|
||
| return last_sale_ordered_products | ||
|
|
||
| def _get_last_purchased_products(self, partner_id): | ||
| '''Fetch products last purchased to the given vendor''' | ||
|
|
||
| purchase_order_line = self.env['purchase.order.line'].search([ | ||
| ('order_id.partner_id', '=', partner_id) | ||
| ]) | ||
|
|
||
| if not purchase_order_line: | ||
| return {} | ||
|
|
||
| invoices = self.env['account.move'].search([ | ||
| ('partner_id', '=', partner_id), | ||
| ('invoice_origin', 'in', purchase_order_line.order_id.mapped('name')) | ||
| ]) | ||
|
|
||
| last_purchased_order_products = {} | ||
| invoice_dates = {inv.invoice_origin: inv.create_date for inv in invoices} | ||
| for sol in purchase_order_line: | ||
| last_date = invoice_dates.get(sol.order_id.name) | ||
| if last_date: | ||
| product_id = sol.product_id.id | ||
| if product_id not in last_purchased_order_products or last_date > last_purchased_order_products[product_id]: | ||
| last_purchased_order_products[product_id] = last_date | ||
|
|
||
| return last_purchased_order_products | ||
|
|
||
| @api.model | ||
| def name_search(self, name='', args=None, operator='ilike', limit=100): | ||
| '''Modify product dropdown in sale order line to show last sold date''' | ||
|
|
||
| domain = args or [] | ||
| partner_id = self.env.context.get('partner_id') | ||
| order_type = self.env.context.get('order_type') | ||
| active_id = self.env.context.get('active_id') | ||
| if not order_type and active_id: | ||
| order_type = self.env['account.journal'].browse(active_id).type | ||
|
|
||
| if partner_id: | ||
| last_ordered_products = {} | ||
| if order_type == 'sale': | ||
| last_ordered_products = self._get_last_sold_products(partner_id) | ||
| elif order_type == 'purchase': | ||
| last_ordered_products = self._get_last_purchased_products(partner_id) | ||
|
|
||
| product_ids = list(last_ordered_products.keys()) | ||
|
|
||
| products = self.search_fetch(expression.AND([domain, [('id', 'in', product_ids)], [("name", operator, name)]]), ['display_name'], limit=limit) | ||
| limit_rest = limit and limit - len(products) | ||
| if limit_rest is None or limit_rest > 0: | ||
| products |= self.search_fetch(expression.AND([domain, [('id', 'not in', product_ids)], [("name", operator, name)]]), ['display_name'], limit=limit_rest) | ||
|
|
||
| products = sorted(products, key=lambda p: last_ordered_products.get(p.id, datetime.min), reverse=True) | ||
|
|
||
| return [(product.id, product.display_name, self.env['product.template']._get_time_ago_string(last_ordered_products.get(product.id, False))) for product in products] | ||
|
|
||
| return super().name_search(name, args, operator, limit) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| from datetime import datetime | ||
| from odoo import api, fields, models | ||
| from odoo.osv import expression | ||
|
|
||
|
|
||
| class ProductTemplate(models.Model): | ||
| _inherit = 'product.template' | ||
|
|
||
| last_order_time = fields.Datetime(compute='_compute_last_order_time') | ||
| last_date_str = fields.Char(compute='_compute_last_order_time') | ||
|
|
||
| @api.depends_context('order_id') | ||
| def _compute_last_order_time(self): | ||
| """Compute the last order time for each product based on the latest sale or purchase.""" | ||
|
|
||
| partner_id = self.env.context.get('partner_id') | ||
| order_type = self.env.context.get('order_type') | ||
|
|
||
| if not partner_id: | ||
| for record in self: | ||
| record.last_order_time = False | ||
| record.last_date_str = False | ||
| return | ||
|
|
||
| last_ordered_products = {} | ||
|
|
||
| if order_type == 'sale': | ||
| last_ordered_products = self._get_last_sold_products(partner_id) | ||
|
|
||
| for record in self: | ||
| last_date = last_ordered_products.get(record.id) | ||
|
|
||
| record.last_order_time = last_date if last_date else False | ||
| record.last_date_str = self._get_time_ago_string(last_date) if last_date else False | ||
|
|
||
| def _get_last_sold_products(self, partner_id): | ||
| '''Fetch products last sold to the given customer''' | ||
|
|
||
| sale_order_lines = self.env['sale.order.line'].search([ | ||
| ('order_id.partner_id', '=', partner_id) | ||
| ]) | ||
|
|
||
| if not sale_order_lines: | ||
| return {} | ||
|
|
||
| invoices = self.env['account.move'].search([ | ||
| ('partner_id', '=', partner_id), | ||
| ('invoice_origin', 'in', sale_order_lines.order_id.mapped('name')) | ||
| ]) | ||
|
|
||
| last_sale_ordered_products = {} | ||
| invoice_dates = {inv.invoice_origin: inv.create_date for inv in invoices} | ||
| for sol in sale_order_lines: | ||
| last_date = invoice_dates.get(sol.order_id.name) | ||
| if last_date: | ||
| product_id = sol.product_id.product_tmpl_id.id | ||
| if product_id not in last_sale_ordered_products or last_date > last_sale_ordered_products[product_id]: | ||
| last_sale_ordered_products[product_id] = last_date | ||
|
|
||
| return last_sale_ordered_products | ||
|
|
||
| def _get_time_ago_string(self, last_date): | ||
| '''Convert datetime to human-readable time difference (e.g., "1d", "4h", "4mo")''' | ||
|
|
||
| if not last_date: | ||
| return "" | ||
|
|
||
| now = fields.Datetime.now() | ||
| diff = now - last_date | ||
|
|
||
| if diff.days > 365: | ||
| return f"{diff.days // 365}y" | ||
| elif diff.days > 30: | ||
| return f"{diff.days // 30}mo" | ||
| elif diff.days > 0: | ||
| return f"{diff.days}d" | ||
| elif diff.seconds >= 3600: | ||
| return f"{diff.seconds // 3600}h" | ||
| elif diff.seconds >= 60: | ||
| return f"{diff.seconds // 60}m" | ||
| else: | ||
| return f"{diff.seconds}s" | ||
|
|
||
| @api.model | ||
| def name_search(self, name='', args=None, operator='ilike', limit=100): | ||
| '''Modify product dropdown in sale order line to show last sold date''' | ||
|
|
||
| domain = args or [] | ||
| partner_id = self.env.context.get('partner_id') | ||
| order_type = self.env.context.get('order_type') | ||
| active_id = self.env.context.get('active_id') | ||
| if not order_type and active_id: | ||
| order_type = self.env['account.journal'].browse(active_id).type | ||
|
|
||
| if partner_id: | ||
| last_ordered_products = {} | ||
| if order_type == 'sale': | ||
| last_ordered_products = self._get_last_sold_products(partner_id) | ||
|
|
||
| product_ids = list(last_ordered_products.keys()) | ||
|
|
||
| products = self.search_fetch(expression.AND([domain, [('id', 'in', product_ids)], [("name", operator, name)]]), ['display_name'], limit=limit) | ||
| limit_rest = limit and limit - len(products) | ||
| if limit_rest is None or limit_rest > 0: | ||
| products |= self.search_fetch(expression.AND([domain, [('id', 'not in', product_ids)], [("name", operator, name)]]), ['display_name'], limit=limit_rest) | ||
|
|
||
| products = sorted(products, key=lambda p: p.last_order_time if p.last_order_time else datetime.min, reverse=True) | ||
|
|
||
| return [(product.id, product.display_name, product.last_date_str if product.last_date_str else False) for product in products] | ||
|
|
||
| return super().name_search(name, args, operator, limit) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| from odoo import models | ||
|
|
||
|
|
||
| class SaleOrder(models.Model): | ||
| _inherit = 'sale.order' | ||
|
|
||
| def _get_action_add_from_catalog_extra_context(self): | ||
| return { | ||
| **super()._get_action_add_from_catalog_extra_context(), | ||
| 'display_uom': self.env.user.has_group('uom.group_uom'), | ||
| } | ||
|
|
||
| def _get_product_catalog_order_data(self, products, **kwargs): | ||
| res = super()._get_product_catalog_order_data(products, **kwargs) | ||
| for product in products: | ||
| res[product.id]['uom'] = { | ||
| 'display_name': product.uom_id.display_name, | ||
| 'id': product.uom_id.id, | ||
| } | ||
| return res |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| from odoo import models | ||
|
|
||
|
|
||
| class SaleOrderLine(models.Model): | ||
| _inherit = 'sale.order.line' | ||
|
|
||
| def _get_product_catalog_lines_data(self, **kwargs): | ||
| res = super()._get_product_catalog_lines_data(**kwargs) | ||
| if len(self) == 1: | ||
| res['uom'] = { | ||
| 'display_name': self.product_id.uom_id.display_name, | ||
| 'id': self.product_id.uom_id.id, | ||
| } | ||
| if self.product_id.uom_id != self.product_uom: | ||
| res['sale_uom'] = { | ||
| 'display_name': self.product_uom.display_name, | ||
| 'id': self.product_uom.id, | ||
| } | ||
| return res |
16 changes: 16 additions & 0 deletions
16
last_ordered_products/static/src/core/autocomplete/autocomplete.xml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <templates xml:space="preserve"> | ||
| <t t-inherit="web.AutoComplete" t-inherit-mode="extension"> | ||
| <xpath expr="//t[@t-esc='option.label']" position="replace"> | ||
| <t t-if="option.time_str"> | ||
| <div class="d-flex justify-content-between"> | ||
| <span><t t-esc="option.label" /></span> | ||
| <span><t t-esc="option.time_str" /></span> | ||
| </div> | ||
| </t> | ||
| <t t-else=""> | ||
| <span t-esc="option.label"/> | ||
| </t> | ||
| </xpath> | ||
| </t> | ||
| </templates> |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would not make a module depending on three major app because even if I want to use this feature only in Sale app it will install inventory and purchase module
So, I was thinking something generic only depending on product app which will contain helper methods and fields to compute those date and if needed create other two modules to call those methods or override them
What do you think?