Skip to content

Commit

Permalink
[IMP] Split of 'product.supplierinfo' for 'product.customerinfo'
Browse files Browse the repository at this point in the history
  • Loading branch information
MiquelRForgeFlow committed Oct 25, 2019
1 parent 48ef65f commit 26121cc
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 76 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ def name_search(self, name='', args=None, operator='ilike', limit=80):
len(res) >= limit):
return res
limit -= len(res)
supplierinfo = self.env['product.supplierinfo'].search(
customerinfo = self.env['product.customerinfo'].search(
[('name', '=', self._context.get('partner_id')), '|',
('product_code', operator, name),
('product_name', operator, name)], limit=limit)
if not supplierinfo:
if not customerinfo:
return res
res_template_ids = self.browse(
[product_id for product_id, _name in res]
).mapped('product_tmpl_id')
product_tmpl_ids = (supplierinfo.mapped('product_tmpl_id') -
product_tmpl_ids = (customerinfo.mapped('product_tmpl_id') -
res_template_ids)
product = self.search(
[('product_tmpl_id', 'in', product_tmpl_ids.ids)], limit=limit)
Expand Down
20 changes: 8 additions & 12 deletions product_supplierinfo_for_customer_sale/models/sale.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ class SaleOrderLine(models.Model):
compute='_compute_product_customer_code',
string='Product Customer Code',
)
partner_is_customer = fields.Boolean(
related='order_id.partner_id.customer',
)

@api.multi
@api.depends('product_id')
def _compute_product_customer_code(self):
for line in self.filtered(lambda sol: sol.product_id.customer_ids):
for line in self.filtered(lambda sol: sol.order_partner_id.customer):
supplierinfo = self.get_customer_supplierinfo(line)
line.product_customer_code = supplierinfo.product_code

Expand Down Expand Up @@ -45,20 +48,13 @@ def product_id_change(self):
line.product_uom_qty = supplierinfo.min_qty
return result

def _supplierinfo_domain(self, line):
"""Common domain for product templates and variants"""
return [
('supplierinfo_type', '=', 'customer'),
('name', '=', line.order_partner_id.id),
]

def get_customer_supplierinfo(self, line):
"""
Search supplierinfo for variant first, if it has not been found then
Search customerinfo for variant first, if it has not been found then
search by product template
"""
supplierinfo = self.env['product.supplierinfo'].search(
self._supplierinfo_domain(line) +
customerinfo = self.env['product.customerinfo'].search(
[('name', '=', line.order_partner_id.id)] +
[
'|',
('product_id', '=', line.product_id.id),
Expand All @@ -68,4 +64,4 @@ def get_customer_supplierinfo(self, line):
],
limit=1,
order='product_id, sequence, min_qty desc, price')
return supplierinfo
return customerinfo
Original file line number Diff line number Diff line change
Expand Up @@ -12,30 +12,28 @@ class TestProductNameSearch(TransactionCase):

def setUp(self):
super(TestProductNameSearch, self).setUp()
self.customer = self.env.ref('base.res_partner_1')
self.customer_1 = self.env.ref('base.res_partner_2')
self.supplier = self.env.ref('base.res_partner_1')
self.customer = self.env.ref('base.res_partner_2')
self.product = self.env['product.product'].create(
{'name': 'Name_product',
'default_code': 'code_product'}
).with_context({'partner_id': self.customer.id,
'supplierinfo_type': 'customer'})
).with_context({'partner_id': self.customer.id})
self.supplierinfo = self.env['product.supplierinfo']

self.supplierinfo_dict = {
self.customerinfo = self.env['product.customerinfo']
self.customerinfo_dict = {
'product_code': 'code_test',
'product_name': 'Name_test',
'name': self.customer.id,
'product_tmpl_id': self.product.product_tmpl_id.id,
'supplierinfo_type': 'customer',
}

def test_10_find_product_customer_code(self):
"""Assign a product_supplierinfo to the product and then search it
"""Assign a product_customerinfo to the product and then search it
using name_search
"""
self.assertFalse(self.product.customer_ids)

self.supplierinfo.create(self.supplierinfo_dict)
self.customerinfo.create(self.customerinfo_dict)
self.assertTrue(self.product.customer_ids)

# Search by product customer code
Expand All @@ -54,7 +52,7 @@ def test_10_find_product_customer_code(self):
# Search by product default code with a different customer used in
# configuration customer
product_names = self.product.with_context(
partner_id=self.customer_1.id).name_search(name='code_product')
partner_id=self.supplier.id).name_search(name='code_product')
self.assertEqual(len(product_names), 1)
self.assertEqual(self.product.id, product_names[0][0])
self.assertEqual('[code_product] Name_product', product_names[0][1])
Expand All @@ -64,8 +62,7 @@ def test_10_find_product_customer_code(self):
self.product_1 = self.env['product.product'].create(
{'name': 'Name_test_1',
'default_code': 'code_test_1'}
).with_context({'partner_id': self.customer.id,
'supplierinfo_type': 'customer'})
).with_context({'partner_id': self.customer.id})

self.assertFalse(self.product_1.customer_ids)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,29 @@ class TestProductSupplierinfoForCustomerSale(TransactionCase):
def setUp(self):
super(TestProductSupplierinfoForCustomerSale, self).setUp()
self.supplierinfo_model = self.env['product.supplierinfo']
self.customerinfo_model = self.env['product.customerinfo']
self.pricelist_item_model = self.env['product.pricelist.item']
self.pricelist_model = self.env['product.pricelist']
self.customer = self._create_customer('customer1')
self.product = self.env.ref('product.product_product_4')
self.product_variant_1 = self.env.ref('product.product_product_4b')
self.product_variant_2 = self.env.ref('product.product_product_4c')
self.supplierinfo = self._create_supplierinfo(
self.customerinfo = self._create_partnerinfo(
'customer', self.customer, self.product)
self.pricelist = self._create_pricelist(
'Test Pricelist', self.product)
self.pricelist_item = self._create_pricelist_item(
'Test Pricelist Item', self.pricelist, self.product)
self.company = self.env.ref('base.main_company')
self._create_supplierinfo(
self._create_partnerinfo(
'customer', self.customer, self.product_variant_1)
self._create_supplierinfo(
self._create_partnerinfo(
'customer', self.customer, self.product_variant_2,
empty_variant=True)
self.product_template = self.env['product.template'].create({
'name': 'product wo variants'
})
self._create_supplierinfo(
self._create_partnerinfo(
'customer', self.customer,
self.product_template.product_variant_ids[:1],
empty_variant=True)
Expand All @@ -47,21 +48,20 @@ def _create_customer(self, name):
'phone': 123456,
})

def _create_supplierinfo(self, supplierinfo_type, partner, product,
empty_variant=False):
def _create_partnerinfo(self, supplierinfo_type, partner, product,
empty_variant=False):
vals = {
'name': partner.id,
'product_id': product.id,
'product_name': 'product4',
'product_code': '00001',
'supplierinfo_type': supplierinfo_type,
'price': 100.0,
'min_qty': 15.0,
}
if empty_variant:
vals.pop('product_id', None)
vals['product_tmpl_id'] = product.product_tmpl_id.id
return self.supplierinfo_model.create(vals)
return self.env['product.' + supplierinfo_type + 'info'].create(vals)

def _create_pricelist(self, name, product):
return self.pricelist_model.create({
Expand Down Expand Up @@ -90,10 +90,10 @@ def test_product_supplierinfo_for_customer_sale(self):
})
line.product_id_change()
self.assertEqual(
line.product_customer_code, self.supplierinfo.product_code,
line.product_customer_code, self.customerinfo.product_code,
"Error: Customer product code was not passed to sale order line")
self.assertEqual(
line.product_uom_qty, self.supplierinfo.min_qty,
line.product_uom_qty, self.customerinfo.min_qty,
"Error: Min qty was not passed to the sale order line")

def test_product_supplierinfo_for_customer_sale_variant(self):
Expand All @@ -107,12 +107,12 @@ def test_product_supplierinfo_for_customer_sale_variant(self):
})
line.product_id_change()
self.assertEqual(
line.product_customer_code, self.supplierinfo.product_code,
line.product_customer_code, self.customerinfo.product_code,
"Error: Customer product code was not passed to sale order line")

def test_product_supplierinfo_for_customer_sale_template(self):
supplierinfo = self._create_supplierinfo('customer', self.customer,
self.product_variant_2)
customerinfo = self._create_partnerinfo(
'customer', self.customer, self.product_variant_2)
so = self.env['sale.order'].create({
'partner_id': self.customer.id,
'pricelist_id': self.pricelist.id,
Expand All @@ -123,7 +123,7 @@ def test_product_supplierinfo_for_customer_sale_template(self):
})
line.product_id_change()
self.assertEqual(
line.product_customer_code, supplierinfo.product_code,
line.product_customer_code, customerinfo.product_code,
"Error: Customer product code was not passed to sale order line")
# Test with product without variants
so2 = self.env['sale.order'].create({
Expand All @@ -136,11 +136,11 @@ def test_product_supplierinfo_for_customer_sale_template(self):
})
line2.product_id_change()
self.assertEqual(
line2.product_customer_code, supplierinfo.product_code,
line2.product_customer_code, customerinfo.product_code,
"Error: Customer product code was not passed to sale order line")

def test_product_supplierinfo_for_customer_sale_variant_wo_template(self):
supplierinfo = self._create_supplierinfo(
customerinfo = self._create_partnerinfo(
'customer', self.customer, self.product_variant_2,
empty_variant=True)
so = self.env['sale.order'].create({
Expand All @@ -153,5 +153,5 @@ def test_product_supplierinfo_for_customer_sale_variant_wo_template(self):
})
line.product_id_change()
self.assertEqual(
line.product_customer_code, supplierinfo.product_code,
line.product_customer_code, customerinfo.product_code,
"Error: Customer product code was not passed to sale order line")
37 changes: 5 additions & 32 deletions product_supplierinfo_for_customer_sale/views/sale_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,15 @@
<field name="name">sale.order.product.code.view.form</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field eval="16" name="priority"/>
<field name="priority" eval="16" />
<field name="arch" type="xml">
<xpath expr="//form//tree//field[@name='product_id']" position="after">
<field name="product_customer_code"/>
<field name="partner_is_customer" invisible="1"/>
<field name="product_customer_code" attrs="{'invisible': [('partner_is_customer', '!=', True)]}"/>
</xpath>
<xpath expr="//form//form//field[@name='product_id']" position="after">
<field name="product_customer_code"/>
<field name="partner_is_customer" invisible="1"/>
<field name="product_customer_code" attrs="{'invisible': [('partner_is_customer', '!=', True)]}"/>
</xpath>
</field>
</record>
Expand All @@ -31,42 +33,13 @@
<xpath expr="//search" position="attributes">
<attribute name="string">Supplierinfo</attribute>
</xpath>
<filter name="active" position="before">
<filter string="Customer" domain="[('supplierinfo_type','=','customer')]"
name="is_customer_filter"/>
<filter string="Supplier" domain="[('supplierinfo_type','=','supplier')]"
name="is_supplier_filter"/>
</filter>
<xpath expr="//search/group" position="attributes">
<attribute name="name">group_by</attribute>
</xpath>
<xpath expr="//search/group" position="inside">
<filter name="partner" string="Partner" icon="terp-partner"
domain="[]" context="{'group_by':'name'}"/>
<filter string="Product Template" domain="[]"
name="gb_product_template"
context="{'group_by':'product_tmpl_id'}"/>
<separator />
<filter string="Type" domain="[]" name="gb_supplierinfo_type" context="{'group_by':'supplierinfo_type'}"/>
</xpath>
</field>
</record>

<record id="sale_order_form_context_supplierinfo" model="ir.ui.view">
<field name="name">sale.order.form.context.supplierinfo</field>
<field name="model">sale.order</field>
<field name="inherit_id" ref="sale.view_order_form"/>
<field name="priority" eval="20"/>
<field name="arch" type="xml">
<xpath expr="//field[@name='order_line']/form//field[@name='product_id']" position="attributes">
<attribute name="context">{'partner_id': parent.partner_id, 'quantity': product_uom_qty,
'pricelist': parent.pricelist_id, 'uom': product_uom, 'company_id': parent.company_id,
'supplierinfo_type': 'customer'}</attribute>
</xpath>
<xpath expr="//field[@name='order_line']/tree//field[@name='product_id']" position="attributes">
<attribute name="context">{'partner_id': parent.partner_id, 'quantity': product_uom_qty,
'pricelist': parent.pricelist_id, 'uom': product_uom, 'company_id': parent.company_id,
'default_lst_price': price_unit, 'supplierinfo_type': 'customer'}</attribute>
</xpath>
</field>
</record>
Expand Down

0 comments on commit 26121cc

Please sign in to comment.