콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다

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   

아바타
취소

will be nice if you add the codes along with question

베스트 답변

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.


아바타
취소
베스트 답변

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

아바타
취소
관련 게시물 답글 화면 활동
3
8월 22
4429
0
4월 21
1301
2
5월 24
5170
1
4월 24
579
2
2월 22
17156