Skip to Content
Menu
Musisz się zarejestrować, aby móc wchodzić w interakcje z tą społecznością.
To pytanie dostało ostrzeżenie

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   

Awatar
Odrzuć

will be nice if you add the codes along with question

Najlepsza odpowiedź

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.


Awatar
Odrzuć
Najlepsza odpowiedź

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

Awatar
Odrzuć
Powiązane posty Odpowiedzi Widoki Czynność
3
sie 22
4364
0
kwi 21
1285
2
maj 24
5108
1
kwi 24
579
2
lut 22
17025