Bỏ qua để đến Nội dung
Menu
Câu hỏi này đã bị gắn cờ
2 Trả lời
2832 Lượt xem

Hello, 

i had to override the create function in sale order line model to create a bonus sale order line depending on a certain condition.

the problem is when i duplicate a sale order it creates two bonus sale order lines not only one.

is there a way to avoid that? or should i find another way to create the bonus line 

here's my code:

class PricelistItem(models.Model):
_inherit = "product.pricelist.item"

compute_price = fields.Selection([
('fixed', 'Fixed Price'),
('percentage', 'Percentage (discount)'),
('formula', 'Formula'),
('bonus', 'Bonus')], index=True, default='fixed', required=True)

ordered_quantity = fields.Float('Ordered Quantity', digits='Product Unit of Measure', default=0)
bonus_quantity = fields.Float('Bonus Quantity', digits='Product Unit of Measure', default=0)


class CustomSaleOrderLine(models.Model):
_inherit = "sale.order.line"

active = fields.Boolean(default=True, readonly=True)
ordered_quantity = fields.Float('Ordered Quantity', digits='Product Unit of Measure', readonly=True, default=0)
bonus_quantity = fields.Float('Bonus Quantity', digits='Product Unit of Measure', readonly=True, default=0)

@api.model
def create(self, vals):
lines = super(CustomSaleOrderLine, self).create(vals)
PricelistItem = self.env['product.pricelist.item']
for line in lines:
product_context = dict(line.env.context, partner_id=line.order_id.partner_id.id, date=line.order_id.date_order,
uom=line.product_uom.id)
final_price, rule_id = line.order_id.pricelist_id.with_context(product_context).get_product_price_rule(line.product_id, line.product_uom_qty or 1.0, line.order_id.partner_id)
if rule_id:
pricelist_item = PricelistItem.browse(rule_id)
if pricelist_item.compute_price == 'bonus':
if line.product_uom_qty >= pricelist_item.ordered_quantity:
line.bonus_quantity = pricelist_item.bonus_quantity
sol = self.env['sale.order.line'].create({
'name': line.product_id.name,
'product_id': line.product_id.id,
'product_uom_qty': pricelist_item.bonus_quantity,
'product_uom': line.product_id.uom_id.id,
'price_unit': 0,
'active': False,
'order_id': line.order_id.id,
})
return lines


thanks   

Ảnh đại diện
Huỷ bỏ

will be nice if you add the codes along with question

Câu trả lời hay nhất

Create method will create the sale order line already and then again you are creating a similar line.

You should better make the change in the "vals" in the create method and just pass it in the super call.


Ảnh đại diện
Huỷ bỏ
Câu trả lời hay nhất

As a workaround, You can add bonus_flag Boolean field to sale order line and mark it as True if the created line is bonus and in override create method check if !line. bonus_flag . By doing this when you duplicate any order it will not create  bonus line twice.

class CustomSaleOrderLine(models.Model):

_inherit = "sale.order.line"



active = fields.Boolean(default=True, readonly=True)

ordered_quantity = fields.Float('Ordered Quantity', digits='Product Unit of Measure', readonly=True, default=0)

bonus_quantity = fields.Float('Bonus Quantity', digits='Product Unit of Measure', readonly=True, default=0)

bonus_flag = fields.Boolean(string='Bonus Flag', default=False)



@api.model

def create(self, vals):

lines = super(CustomSaleOrderLine, self).create(vals)

PricelistItem = self.env['product.pricelist.item']

for line in lines.filtered(lambda l: not l.bonus_flag):

product_context = dict(line.env.context, partner_id=line.order_id.partner_id.id, date=line.order_id.date_order,

uom=line.product_uom.id)

final_price, rule_id = line.order_id.pricelist_id.with_context(product_context).get_product_price_rule(line.product_id, line.product_uom_qty or 1.0, line.order_id.partner_id)

if rule_id:

pricelist_item = PricelistItem.browse(rule_id)

if pricelist_item.compute_price == 'bonus':

if line.product_uom_qty >= pricelist_item.ordered_quantity:

line.bonus_quantity = pricelist_item.bonus_quantity

sol = self.env['sale.order.line'].create({

'name': line.product_id.name,

'product_id': line.product_id.id,

'product_uom_qty': pricelist_item.bonus_quantity,

'product_uom': line.product_id.uom_id.id,

'price_unit': 0,

'active': False,

'order_id': line.order_id.id,

'bonus_flag': True,

})

return lines

Ảnh đại diện
Huỷ bỏ
Bài viết liên quan Trả lời Lượt xem Hoạt động
3
thg 8 22
4371
0
thg 4 21
1285
2
thg 5 24
5114
1
thg 4 24
579
2
thg 2 22
17029