-
Notifications
You must be signed in to change notification settings - Fork 3
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
[15.0][3917][IMP] purchase_order_supplier_phone: add functionals from sst #94
Merged
+271
−54
Merged
Changes from all commits
Commits
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 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -1,19 +1,185 @@ | ||
# Copyright 2022 Quartile Limited | ||
# License LGPL-3.0 or later (http://www.gnu.org/licenses/lgpl). | ||
# License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). | ||
|
||
from odoo import fields, models | ||
import logging | ||
|
||
from odoo import _, api, fields, models | ||
from odoo.exceptions import UserError | ||
|
||
|
||
class PurchaseOrder(models.Model): | ||
_inherit = "purchase.order" | ||
|
||
phone_update = fields.Char() | ||
mobile_update = fields.Char() | ||
phone_search = fields.Char( | ||
states={ | ||
"purchase": [("readonly", True)], | ||
"done": [("readonly", True)], | ||
"cancel": [("readonly", True)], | ||
}, | ||
) | ||
supplier_phone = fields.Char( | ||
related="partner_id.phone", | ||
string="Supplier Phone", | ||
readonly=True, | ||
store=True, | ||
) | ||
supplier_mobile = fields.Char( | ||
related="partner_id.mobile", | ||
string="Supplier Mobile", | ||
readonly=True, | ||
store=True, | ||
) | ||
tentative_name = fields.Char() | ||
|
||
def write(self, vals): | ||
# Propose supplier logic | ||
if ( | ||
"phone_search" in vals | ||
or "phone_update" in vals | ||
or "mobile_update" in vals | ||
or "tentative_name" in vals | ||
): | ||
val_phone_search = "phone_search" in vals and vals["phone_search"] or False | ||
val_phone = "phone_update" in vals and vals["phone_update"] or False | ||
val_mobile = "mobile_update" in vals and vals["mobile_update"] or False | ||
for order in self: | ||
val_tent_name = ( | ||
"tentative_name" in vals | ||
and vals["tentative_name"] | ||
or order.tentative_name | ||
) | ||
partner_id = ( | ||
"partner_id" in vals and vals["partner_id"] or order.partner_id.id | ||
) | ||
if self.is_default_partner(partner_id) and ( | ||
val_phone or val_mobile or val_phone_search | ||
): | ||
partner_id = self.create_partner( | ||
val_phone or val_phone_search, val_mobile | ||
).id | ||
if not self.is_default_partner(partner_id): | ||
partner = self.env["res.partner"].browse(partner_id) | ||
self.update_partner(partner, val_phone, val_mobile, val_tent_name) | ||
vals["partner_id"] = partner_id | ||
vals["phone_update"] = vals["mobile_update"] = False | ||
return super(PurchaseOrder, self).write(vals) | ||
|
||
@api.model | ||
def create(self, vals): | ||
val_phone_search = "phone_search" in vals and vals["phone_search"] or False | ||
val_phone = "phone_update" in vals and vals["phone_update"] or False | ||
val_mobile = "mobile_update" in vals and vals["mobile_update"] or False | ||
val_tent_name = "tentative_name" in vals and vals["tentative_name"] or False | ||
logging.info(self.is_default_partner(vals["partner_id"])) | ||
if self.is_default_partner(vals["partner_id"]) and ( | ||
val_phone or val_mobile or val_phone_search | ||
): | ||
vals["partner_id"] = self.create_partner( | ||
val_phone or val_phone_search, val_mobile | ||
).id | ||
if not self.is_default_partner(vals["partner_id"]): | ||
partner = self.env["res.partner"].browse(vals["partner_id"]) | ||
self.update_partner(partner, val_phone, val_mobile, val_tent_name) | ||
vals["phone_update"] = vals["mobile_update"] = False | ||
return super(PurchaseOrder, self).create(vals) | ||
|
||
def create_partner(self, phone, mobile): | ||
partner = self.env["res.partner"].create( | ||
{ | ||
"name": "未確認", | ||
"phone": phone, | ||
"mobile": mobile, | ||
"supplier_rank": 1, | ||
"customer_rank": 0, | ||
} | ||
) | ||
return partner | ||
|
||
def update_partner(self, partner, phone, mobile, tent_name): | ||
if phone and partner.phone != phone: | ||
partner.phone = phone | ||
if mobile and partner.mobile != mobile: | ||
partner.mobile = mobile | ||
if tent_name and partner.name != tent_name: | ||
partner.name = tent_name | ||
|
||
@api.onchange("phone_search") | ||
def onchange_phone_search(self): | ||
if self.phone_search: | ||
self.phone_update = False | ||
self.mobile_update = False | ||
partner = self.get_partner_from_phone(self.phone_search) | ||
if partner: | ||
self.partner_id = partner | ||
elif self.partner_id: | ||
if self.is_default_partner(self.partner_id.id): | ||
self.phone_update = self.phone_search | ||
|
||
@api.onchange("phone_update") | ||
def onchange_phone_update(self): | ||
if self.phone_update: | ||
return self.check_onchange_phone(self.phone_update, "phone_update") | ||
|
||
@api.onchange("mobile_update") | ||
def onchange_mobile_update(self): | ||
if self.mobile_update: | ||
return self.check_onchange_phone(self.mobile_update, "mobile_update") | ||
|
||
def check_onchange_phone(self, phone, field): | ||
try: | ||
partner = self.get_partner_from_phone(phone) | ||
if self.partner_id and partner and partner != self.partner_id: | ||
conflicts_user = _( | ||
"\n%(partner_name)s\n- Phone: %(phone)s\n- Mobile: %(mobile)s\n" | ||
) % { | ||
"partner_name": partner.name, | ||
"phone": partner.phone or "", | ||
"mobile": partner.mobile or "", | ||
} | ||
return { | ||
"warning": { | ||
"message": _( | ||
"The entered phone (%(phone)s) conflicts with the following" | ||
" user(s):\n%(conflict_users)s" | ||
) | ||
% {"phone": phone or "N/A", "conflict_users": conflicts_user} | ||
}, | ||
"value": {field: False}, | ||
} | ||
except UserError as e: | ||
return {"warning": {"message": e.name}, "value": {field: False}} | ||
|
||
def get_partner_from_phone(self, phone): | ||
Partner = self.env["res.partner"] | ||
partners = False | ||
# here we use "or" condition instead of "and" | ||
if phone: | ||
partners = Partner.search( | ||
[ | ||
"|", | ||
("phone", "=", phone), | ||
("mobile", "=", phone), | ||
("active", "=", True), | ||
("supplier_rank", ">", 0), | ||
] | ||
) | ||
if partners and len(partners) > 1: | ||
conflicts_users_list = "" | ||
for partner in partners: | ||
conflicts_users_list += _( | ||
"\n%(partner_name)s\n- Phone: %(phone)s\n- Mobile: %(mobile)s\n" | ||
) % { | ||
"partner_name": partner.name, | ||
"phone": partner.phone or "", | ||
"mobile": partner.mobile or "", | ||
} | ||
raise UserError( | ||
_( | ||
"The entered phone (%(phone)s) conflicts with" | ||
" the following user(s):\n%(conflict_users)s" | ||
) | ||
% {"phone": phone or "N/A", "conflict_users": conflicts_users_list} | ||
) | ||
return partners if partners else False |
This file contains 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 |
---|---|---|
@@ -1,3 +1 @@ | ||
This module adds supplier's phone and mobile fields to purchase.order, | ||
add this fields in purchase order tree view and also add this fields in | ||
search view of purchase order. | ||
This module adds the information of supplier in purchase and update supplier's information when update values in purchase order. |
This file contains 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
Oops, something went wrong.
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.
is_default_partner
depends on purchase_ext_sst module.