mirror of
https://gitlab.com/hibou-io/hibou-odoo/suite.git
synced 2025-01-20 12:37:31 +02:00
[MOV] product_cores: from Hibou Suite Enterprise for 13.0
This commit is contained in:
5
product_cores/models/__init__.py
Normal file
5
product_cores/models/__init__.py
Normal file
@@ -0,0 +1,5 @@
|
||||
# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details.
|
||||
|
||||
from . import product
|
||||
from . import purchase
|
||||
from . import sale
|
||||
61
product_cores/models/product.py
Normal file
61
product_cores/models/product.py
Normal file
@@ -0,0 +1,61 @@
|
||||
# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details.
|
||||
|
||||
from odoo import api, fields, models
|
||||
|
||||
|
||||
class ProductTemplate(models.Model):
|
||||
_inherit = 'product.template'
|
||||
|
||||
core_ok = fields.Boolean(string='Core')
|
||||
product_core_service_id = fields.Many2one('product.product', string='Product Core Deposit',
|
||||
compute='_compute_product_core_service',
|
||||
inverse='_set_product_core_service')
|
||||
product_core_id = fields.Many2one('product.product', string='Product Core',
|
||||
compute='_compute_product_core',
|
||||
inverse='_set_product_core')
|
||||
product_core_validity = fields.Integer(string='Product Core Return Validity',
|
||||
help='How long after a sale the core is eligible for return. (in days)')
|
||||
|
||||
@api.depends('product_variant_ids', 'product_variant_ids.product_core_service_id')
|
||||
def _compute_product_core_service(self):
|
||||
unique_variants = self.filtered(lambda template: len(template.product_variant_ids) == 1)
|
||||
for template in unique_variants:
|
||||
template.product_core_service_id = template.product_variant_ids.product_core_service_id
|
||||
for template in (self - unique_variants):
|
||||
template.product_core_service_id = False
|
||||
|
||||
@api.depends('product_variant_ids', 'product_variant_ids.product_core_id')
|
||||
def _compute_product_core(self):
|
||||
unique_variants = self.filtered(lambda template: len(template.product_variant_ids) == 1)
|
||||
for template in unique_variants:
|
||||
template.product_core_id = template.product_variant_ids.product_core_id
|
||||
for template in (self - unique_variants):
|
||||
template.product_core_id = False
|
||||
|
||||
def _set_product_core_service(self):
|
||||
if len(self.product_variant_ids) == 1:
|
||||
self.product_variant_ids.product_core_service_id = self.product_core_service_id
|
||||
|
||||
def _set_product_core(self):
|
||||
if len(self.product_variant_ids) == 1:
|
||||
self.product_variant_ids.product_core_id = self.product_core_id
|
||||
|
||||
|
||||
class ProductProduct(models.Model):
|
||||
_inherit = 'product.product'
|
||||
|
||||
product_core_service_id = fields.Many2one('product.product', string='Product Core Deposit')
|
||||
product_core_id = fields.Many2one('product.product', string='Product Core')
|
||||
|
||||
def get_purchase_core_service(self, vendor):
|
||||
seller_line = self.seller_ids.filtered(lambda l: l.name == vendor and l.product_core_service_id)
|
||||
# only want to return the first one
|
||||
for l in seller_line:
|
||||
return l.product_core_service_id
|
||||
return seller_line
|
||||
|
||||
|
||||
class ProductSupplierinfo(models.Model):
|
||||
_inherit = 'product.supplierinfo'
|
||||
|
||||
product_core_service_id = fields.Many2one('product.product', string='Product Core Deposit')
|
||||
64
product_cores/models/purchase.py
Normal file
64
product_cores/models/purchase.py
Normal file
@@ -0,0 +1,64 @@
|
||||
# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details.
|
||||
|
||||
from odoo import api, fields, models, _
|
||||
from odoo.exceptions import UserError
|
||||
|
||||
|
||||
class PurchaseOrder(models.Model):
|
||||
_inherit = 'purchase.order'
|
||||
|
||||
def copy(self, default=None):
|
||||
new_po = super(PurchaseOrder, self).copy(default=default)
|
||||
for line in new_po.order_line.filtered(lambda l: l.product_id.core_ok and not l.core_line_id):
|
||||
line.unlink()
|
||||
return new_po
|
||||
|
||||
|
||||
class PurchaseOrderLine(models.Model):
|
||||
_inherit = 'purchase.order.line'
|
||||
|
||||
core_line_id = fields.Many2one('purchase.order.line', string='Core Purchase Line', copy=False)
|
||||
|
||||
@api.model
|
||||
def create(self, values):
|
||||
res = super(PurchaseOrderLine, self).create(values)
|
||||
other_product = res.product_id.get_purchase_core_service(res.order_id.partner_id)
|
||||
if other_product:
|
||||
values['product_id'] = other_product.id
|
||||
values['name'] = other_product.name
|
||||
values['price_unit'] = other_product.list_price
|
||||
values['core_line_id'] = res.id
|
||||
other_line = self.create(values)
|
||||
other_line._compute_tax_id()
|
||||
return res
|
||||
|
||||
def write(self, values):
|
||||
res = super(PurchaseOrderLine, self).write(values)
|
||||
if 'product_id' in values or 'product_qty' in values or 'product_uom' in values:
|
||||
self.filtered(lambda l: not l.core_line_id)\
|
||||
.mapped('order_id.order_line')\
|
||||
.filtered('core_line_id')\
|
||||
._update_core_line()
|
||||
return res
|
||||
|
||||
def unlink(self):
|
||||
for line in self:
|
||||
if line.core_line_id and line.core_line_id and not self.env.user.has_group('purchase.group_purchase_user'):
|
||||
raise UserError(_('You cannot delete a core line while the original still exists.'))
|
||||
# Unlink any linked core lines.
|
||||
other_line = line.order_id.order_line.filtered(lambda l: l.core_line_id == line)
|
||||
if other_line and other_line not in self:
|
||||
other_line.write({'core_line_id': False})
|
||||
other_line.unlink()
|
||||
return super(PurchaseOrderLine, self).unlink()
|
||||
|
||||
def _update_core_line(self):
|
||||
for line in self:
|
||||
if line.core_line_id and line.core_line_id.product_id.get_purchase_core_service(line.order_id.partner_id):
|
||||
line.update({
|
||||
'product_id': line.core_line_id.product_id.get_purchase_core_service(line.order_id.partner_id).id,
|
||||
'product_qty': line.core_line_id.product_qty,
|
||||
'product_uom': line.core_line_id.product_uom.id,
|
||||
})
|
||||
elif line.core_line_id:
|
||||
line.unlink()
|
||||
68
product_cores/models/sale.py
Normal file
68
product_cores/models/sale.py
Normal file
@@ -0,0 +1,68 @@
|
||||
# Part of Hibou Suite Professional. See LICENSE_PROFESSIONAL file for full copyright and licensing details.
|
||||
|
||||
from odoo import api, fields, models, _
|
||||
from odoo.exceptions import UserError
|
||||
|
||||
|
||||
class SaleOrder(models.Model):
|
||||
_inherit = 'sale.order'
|
||||
|
||||
def copy(self, default=None):
|
||||
new_so = super(SaleOrder, self).copy(default=default)
|
||||
for line in new_so.order_line.filtered(lambda l: l.product_id.core_ok and not l.core_line_id):
|
||||
line.unlink()
|
||||
return new_so
|
||||
|
||||
|
||||
class SaleOrderLine(models.Model):
|
||||
_inherit = 'sale.order.line'
|
||||
|
||||
core_line_id = fields.Many2one('sale.order.line', string='Core Sale Line', copy=False)
|
||||
|
||||
@api.model
|
||||
def create(self, values):
|
||||
res = super(SaleOrderLine, self).create(values)
|
||||
if res.product_id.product_core_service_id:
|
||||
other_product = res.product_id.product_core_service_id
|
||||
values['product_id'] = other_product.id
|
||||
values['name'] = other_product.name
|
||||
values['price_unit'] = other_product.list_price
|
||||
values['core_line_id'] = res.id
|
||||
other_line = self.create(values)
|
||||
other_line._compute_tax_id()
|
||||
return res
|
||||
|
||||
def write(self, values):
|
||||
res = super(SaleOrderLine, self).write(values)
|
||||
if 'product_id' in values or 'product_uom_qty' in values or 'product_uom' in values:
|
||||
for line in self.filtered(lambda l: not l.core_line_id):
|
||||
line.mapped('order_id.order_line').filtered(lambda l: l.core_line_id == line)._update_core_line()
|
||||
return res
|
||||
|
||||
def unlink(self):
|
||||
for line in self:
|
||||
if line.core_line_id and line.core_line_id and not self.env.user.has_group('sales_team.group_sale_manager'):
|
||||
raise UserError(_('You cannot delete a core line while the original still exists.'))
|
||||
# Unlink any linked core lines.
|
||||
other_line = line.order_id.order_line.filtered(lambda l: l.core_line_id == line)
|
||||
if other_line and other_line not in self:
|
||||
other_line.write({'core_line_id': False})
|
||||
other_line.unlink()
|
||||
return super(SaleOrderLine, self).unlink()
|
||||
|
||||
def _update_core_line(self):
|
||||
for line in self:
|
||||
if line.core_line_id and line.core_line_id.product_id.product_core_service_id:
|
||||
line.write({
|
||||
'product_id': line.core_line_id.product_id.product_core_service_id.id,
|
||||
'product_uom_qty': line.core_line_id.product_uom_qty,
|
||||
'product_uom': line.core_line_id.product_uom.id,
|
||||
})
|
||||
elif line.core_line_id:
|
||||
line.unlink()
|
||||
|
||||
@api.depends('core_line_id.qty_delivered')
|
||||
def _compute_qty_delivered(self):
|
||||
super(SaleOrderLine, self)._compute_qty_delivered()
|
||||
for line in self.filtered(lambda l: l.core_line_id):
|
||||
line.qty_delivered = line.core_line_id.qty_delivered
|
||||
Reference in New Issue
Block a user