Skip to Content
Меню
Вам необхідно зареєструватися, щоб взаємодіяти зі спільнотою.
Це запитання позначене
2 Відповіді
2815 Переглядів

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

Аватар
Відмінити
Related Posts Відповіді Переглядів Дія
3
серп. 22
4364
0
квіт. 21
1285
2
трав. 24
5108
1
квіт. 24
579
2
лют. 22
17021