Skip to content

Commit

Permalink
[IMP] purchase_order_supplier_phone: add functionals from sst
Browse files Browse the repository at this point in the history
  • Loading branch information
AungKoKoLin1997 committed Dec 4, 2023
1 parent 3d07619 commit d0fbe56
Show file tree
Hide file tree
Showing 2 changed files with 207 additions and 34 deletions.
168 changes: 167 additions & 1 deletion purchase_order_supplier_phone/models/purchase_order.py
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).

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
73 changes: 40 additions & 33 deletions purchase_order_supplier_phone/views/purchase_order_views.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,54 @@
<?xml version="1.0" encoding="utf-8" ?>
<odoo>

<record id="purchase_order_form" model="ir.ui.view">
<field name="name">purchase.order.form</field>
<record id="purchase_order_form_inherit" model="ir.ui.view">
<field name="name">purchase.order.form.inherit</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase.purchase_order_form" />
<field name="priority" eval="18" />
<field name="arch" type="xml">
<xpath expr="//field[@name='partner_id']" position="before">
<field name="phone_search" />
</xpath>
<xpath expr="//field[@name='partner_id']" position="after">
<field name="supplier_phone" />
<field name="supplier_mobile" />
</xpath>
<xpath expr="//field[@name='date_order']" position="after">
<field
name="tentative_name"
string="Supplier Name Update (Tentative Name)"
/>
<field name="phone_update" />
<field name="mobile_update" />
</xpath>
</field>
</record>
<record id="purchase_order_view_tree" model="ir.ui.view">
<field name="name">purchase.order.tree</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase.purchase_order_view_tree" />
<field name="arch" type="xml">
<xpath expr="//field[@name='partner_id']" position="after">
<field name="phone_search" optional="show" />
<field name="supplier_phone" optional="show" />
<field name="supplier_mobile" optional="show" />
</xpath>
</field>
</record>
<record id="purchase_order_kpis_tree" model="ir.ui.view">
<field name="name">purchase.order.tree</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase.purchase_order_kpis_tree" />
<field name="arch" type="xml">
<xpath expr="//field[@name='partner_id']" position="after">
<field name="phone_search" optional="show" />
<field name="supplier_phone" optional="show" />
<field name="supplier_mobile" optional="show" />
</xpath>
</field>
</record>

<record id="view_purchase_order_filter" model="ir.ui.view">
<field name="name">purchase.order.filter</field>
<field name="name">request.quotation.select</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase.view_purchase_order_filter" />
<field name="arch" type="xml">
Expand All @@ -28,9 +61,8 @@
</xpath>
</field>
</record>

<record id="view_purchase_order_view_search" model="ir.ui.view">
<field name="name">purchase.order.list.search</field>
<record id="purchase_order_view_search" model="ir.ui.view">
<field name="name">request.quotation.select</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase.purchase_order_view_search" />
<field name="arch" type="xml">
Expand All @@ -43,29 +75,4 @@
</xpath>
</field>
</record>

<record id="view_purchase_order_tree" model="ir.ui.view">
<field name="name">purchase.order.kpis.tree</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase.purchase_order_kpis_tree" />
<field name="arch" type="xml">
<xpath expr="//field[@name='partner_id']" position="after">
<field name="supplier_phone" optional="show" />
<field name="supplier_mobile" optional="show" />
</xpath>
</field>
</record>

<record id="view_purchase_order_view_tree" model="ir.ui.view">
<field name="name">purchase.order.view.tree</field>
<field name="model">purchase.order</field>
<field name="inherit_id" ref="purchase.purchase_order_view_tree" />
<field name="arch" type="xml">
<xpath expr="//field[@name='partner_id']" position="after">
<field name="supplier_phone" optional="show" />
<field name="supplier_mobile" optional="show" />
</xpath>
</field>
</record>

</odoo>

0 comments on commit d0fbe56

Please sign in to comment.