91 lines
4.2 KiB
Python
91 lines
4.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
|
|
from odoo import fields, models, api, _
|
|
from odoo.exceptions import UserError
|
|
from lxml import html
|
|
|
|
class SaleOrder(models.Model):
|
|
_inherit = "sale.order"
|
|
|
|
dealer_discount = fields.Boolean("Add Discount", readonly=True, states={'draft': [('readonly', False),],'sent': [('readonly', False)]},)
|
|
dealer_discount_type = fields.Selection([('fixed','Fixed'),('percentage','Percentage')],
|
|
"Discount Type", readonly=True, states={'draft': [('readonly', False)],'sent': [('readonly', False)]}, default='percentage')
|
|
dealer_discount_product = fields.Many2one('product.product', string='Rabatt-Artikel', domain=[('categ_id.name', '=', 'Discount')])
|
|
dealer_discount_amount = fields.Float("Discount Amount", readonly=True, states={'draft': [('readonly', False)],'sent': [('readonly', False)]},
|
|
size = 10,)
|
|
dealer_discount_percentage = fields.Float("Discount Percentage", readonly=True,
|
|
states={'draft': [('readonly', False)],'sent': [('readonly', False)]},
|
|
size = 5,)
|
|
|
|
@api.multi
|
|
def _dealer_discount_unset(self):
|
|
if self.env.user.company_id.sale_dealer_discount_product_id:
|
|
self.env['sale.order.line'].search([('order_id', 'in', self.ids), ('product_id', '=', self.env.user.company_id.sale_dealer_discount_product_id.id)]).unlink()
|
|
|
|
@api.multi
|
|
def unset_dealer_discount(self):
|
|
discount_products_ids = self.env['product.product'].search([('categ_id', '=', 'Discount')]).ids
|
|
self.env['sale.order.line'].search([('order_id', 'in', self.ids), ('product_id', 'in', discount_products_ids)]).unlink()
|
|
|
|
@api.multi
|
|
def create_dealer_discount(self):
|
|
Line = self.env['sale.order.line']
|
|
|
|
if self.dealer_discount_product:
|
|
discount_product_id = self.dealer_discount_product
|
|
else:
|
|
# discount_product_id = self.env.user.company_id.sale_dealer_discount_product_id
|
|
# if not discount_product_id:
|
|
raise UserError(_('Please select Discount product first.'))
|
|
|
|
# Remove Discount line first
|
|
# self._dealer_discount_unset()
|
|
|
|
for order in self:
|
|
amount = 0
|
|
if discount_product_id.product_description:
|
|
discount_text = html.fromstring(discount_product_id.product_description).text_content()
|
|
else:
|
|
discount_text = discount_product_id.name
|
|
|
|
if order.dealer_discount_type == 'fixed':
|
|
amount = order.dealer_discount_amount
|
|
else:
|
|
sep = "; "
|
|
discount_text = "-" + str(order.dealer_discount_percentage) + "% " + discount_text + ", "
|
|
discount_pos = ""
|
|
n=0
|
|
nd=0
|
|
for line in order.order_line.sorted(key=lambda l: l.sequence):
|
|
n += 1
|
|
if not line.product_id.product_tmpl_id.material_type_id.no_dealer_discount:
|
|
nd += 1
|
|
amount += (line.price_subtotal * order.dealer_discount_percentage)/100
|
|
discount_pos += str(n)+sep
|
|
if nd>1:
|
|
discount_text = discount_text + "Positionen: "
|
|
else:
|
|
discount_text = discount_text + "Position: "
|
|
|
|
discount_text = discount_text + discount_pos.rstrip(sep)
|
|
|
|
if amount > 0:
|
|
last_so_line = self.env['sale.order.line'].search([('order_id', '=', order.id)], order='sequence desc',
|
|
limit=1)
|
|
last_sequence = last_so_line.sequence + 1 if last_so_line else 99999
|
|
# Create the Sale line
|
|
Line.create({
|
|
'name': discount_text,
|
|
'price_unit': -amount,
|
|
'product_uom_qty': 1.0,
|
|
'discount': 0.0,
|
|
'product_uom': discount_product_id.uom_id.id,
|
|
'product_id': discount_product_id.id,
|
|
'order_id': order.id,
|
|
'sequence': last_sequence,
|
|
})
|
|
|
|
self.dealer_discount_product = ''
|
|
|
|
return True
|