74 lines
		
	
	
		
			3.2 KiB
		
	
	
	
		
			Python
		
	
	
			
		
		
	
	
			74 lines
		
	
	
		
			3.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 Dealer 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_amount = fields.Float("Discount Amount", readonly=True, states={'draft': [('readonly', False)],'sent': [('readonly', False)]},)
 | 
						|
    dealer_discount_percentage = fields.Float("Discount Percentage", readonly=True, states={'draft': [('readonly', False)],'sent': [('readonly', False)]},)
 | 
						|
 | 
						|
    @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 create_dealer_discount(self):
 | 
						|
        Line = self.env['sale.order.line']
 | 
						|
 | 
						|
        discount_product_id = self.env.user.company_id.sale_dealer_discount_product_id
 | 
						|
        if not discount_product_id:
 | 
						|
            raise UserError(_('Please set Sale Dealer Discount product in General Settings 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:
 | 
						|
                    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:
 | 
						|
                # 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': 99999,
 | 
						|
                })
 | 
						|
 | 
						|
        return True
 |