119 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			Python
		
	
	
			
		
		
	
	
			119 lines
		
	
	
		
			5.5 KiB
		
	
	
	
		
			Python
		
	
	
| # -*- coding: utf-8 -*-
 | |
| 
 | |
| from odoo import fields, models, api, _
 | |
| from odoo.exceptions import UserError
 | |
| from lxml import html
 | |
| 
 | |
| class Invoice(models.Model):
 | |
|     _inherit = "account.invoice"
 | |
| 
 | |
|     dealer_discount = fields.Boolean("Add Individual Discount", readonly=True, states={'draft': [('readonly', False)]},)
 | |
|     dealer_discount_type = fields.Selection([('fixed','Fixed'),('percentage','Percentage')],
 | |
|         "Discount Type", readonly=True, states={'draft': [('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)]},)
 | |
|     dealer_discount_percentage = fields.Float("Discount Percentage", readonly=True, states={'draft': [('readonly', False)]},)
 | |
| 
 | |
|     @api.multi
 | |
|     def _dealer_discount_unset(self):
 | |
|         if self.env.user.company_id.invoice_dealer_discount_product_id:
 | |
|             self.env['account.invoice.line'].search([('invoice_id', 'in', self.ids), ('product_id', '=', self.env.user.company_id.invoice_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['account.invoice.line'].search([('invoice_id', 'in', self.ids), ('product_id', 'in', discount_products_ids)]).unlink()
 | |
| 
 | |
|     @api.multi
 | |
|     def create_dealer_discount(self):
 | |
|         InvoiceLine = self.env['account.invoice.line']
 | |
| 
 | |
|         if self.dealer_discount_product:
 | |
|             discount_product_id = self.dealer_discount_product
 | |
|         else:
 | |
|         #     discount_product_id = self.env.user.company_id.invoice_dealer_discount_product_id
 | |
|         # if not discount_product_id:
 | |
|             raise UserError(_('Please select Discount product first.'))
 | |
| 
 | |
|         # Remove Discount line first
 | |
| #        self._dealer_discount_unset()
 | |
| 
 | |
|         account_id = discount_product_id.property_account_income_id.id
 | |
|         if not account_id:
 | |
|             prop = self.env['ir.property'].get('property_account_income_categ_id', 'product.category')
 | |
|             account_id = prop and prop.id or False
 | |
| 
 | |
|         for invoice 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 invoice.dealer_discount_type == 'fixed':
 | |
|             #     amount = invoice.dealer_discount_amount
 | |
|             # if invoice.dealer_discount_type == 'percentage':
 | |
|             #     amount = (invoice.amount_total * invoice.dealer_discount_percentage)/100
 | |
| 
 | |
|             if invoice.dealer_discount_type == 'fixed':
 | |
|                 amount = invoice.dealer_discount_amount
 | |
|             else:
 | |
|                 sep = "; "
 | |
|                 # discount_text = "-" + str(invoice.dealer_discount_percentage) + "% " + discount_text + ", "
 | |
| 
 | |
|                 discount_text = str(abs(invoice.dealer_discount_percentage)) + "% " + discount_text + ", "
 | |
|                 if invoice.dealer_discount_percentage < 0:
 | |
|                     discount_text = "+" + discount_text
 | |
|                 else:
 | |
|                     discount_text = "-" + discount_text
 | |
| 
 | |
|                 discount_pos = ""
 | |
|                 n=0
 | |
|                 nd=0
 | |
|                 for line in invoice.invoice_line_ids.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 * invoice.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)
 | |
| 
 | |
|             # Apply fiscal position
 | |
|             taxes = discount_product_id.taxes_id.filtered(lambda t: t.company_id.id == invoice.company_id.id)
 | |
|             taxes_ids = taxes.ids
 | |
|             if invoice.partner_id and self.fiscal_position_id:
 | |
|                 taxes_ids = self.fiscal_position_id.map_tax(taxes, discount_product_id, invoice.partner_id).ids
 | |
| 
 | |
|             layout_category_id = self.env['sale.layout_category'].search([('name', '=', discount_product_id.manufacturing_number)])
 | |
| 
 | |
|             if amount != 0:
 | |
|                 last_inv_line = self.env['account.invoice.line'].search([('invoice_id', '=', invoice.id)], order='sequence desc',
 | |
|                                                                   limit=1)
 | |
|                 last_sequence = last_inv_line.sequence + 1 if last_inv_line else 99999
 | |
|                 # Create the Invoice line
 | |
|                 InvoiceLine.create({
 | |
|                     'name': discount_text,
 | |
|                     'price_unit': -amount,
 | |
|                     'account_id': account_id,
 | |
|                     'quantity': 1.0,
 | |
|                     'discount': 0.0,
 | |
|                     'uom_id': discount_product_id.uom_id.id,
 | |
|                     'product_id': discount_product_id.id,
 | |
|                     'invoice_id': invoice.id,
 | |
|                     'invoice_line_tax_ids': [(6, 0, taxes_ids)],
 | |
|                     'layout_category_id':layout_category_id.id,
 | |
|                     'sequence': last_sequence,
 | |
|                 })
 | |
| 
 | |
|         super()._onchange_invoice_line_ids()
 | |
| 
 | |
|         self.dealer_discount_product = ''
 | |
| 
 | |
|         return True
 |