new dealer discount (fix in invoice)
							parent
							
								
									6a84be4a1b
								
							
						
					
					
						commit
						fdbec05acb
					
				|  | @ -2,6 +2,7 @@ | |||
| 
 | ||||
| from odoo import fields, models, api, _ | ||||
| from odoo.exceptions import UserError | ||||
| from lxml import html | ||||
| 
 | ||||
| class Invoice(models.Model): | ||||
|     _inherit = "account.invoice" | ||||
|  | @ -21,42 +22,70 @@ class Invoice(models.Model): | |||
|     def create_dealer_discount(self): | ||||
|         InvoiceLine = self.env['account.invoice.line'] | ||||
| 
 | ||||
|         product_id = self.env.user.company_id.invoice_dealer_discount_product_id | ||||
|         if not product_id: | ||||
|         discount_product_id = self.env.user.company_id.invoice_dealer_discount_product_id | ||||
|         if not discount_product_id: | ||||
|             raise UserError(_('Please set Invoice Dealer Discount product in General Settings first.')) | ||||
| 
 | ||||
|         # Remove Discount line first | ||||
|         self._dealer_discount_unset() | ||||
| 
 | ||||
|         account_id = product_id.property_account_income_id.id | ||||
|         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 | ||||
|             if invoice.dealer_discount_type == 'percentage': | ||||
|                 amount = (invoice.amount_total * invoice.dealer_discount_percentage)/100 | ||||
|             else: | ||||
|                 sep = "; " | ||||
|                 discount_text = "-" + str(invoice.dealer_discount_percentage) + "% " + discount_text + ", " | ||||
|                 discount_pos = "" | ||||
|                 n=0 | ||||
|                 nd=0 | ||||
|                 for line in invoice.invoice_line_ids: | ||||
|                     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 = product_id.taxes_id.filtered(lambda t: t.company_id.id == invoice.company_id.id) | ||||
|             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, product_id, invoice.partner_id).ids | ||||
|                 taxes_ids = self.fiscal_position_id.map_tax(taxes, discount_product_id, invoice.partner_id).ids | ||||
| 
 | ||||
|             # Create the Invoice line | ||||
|             InvoiceLine.create({ | ||||
|                 'name': product_id.name, | ||||
|                 'price_unit': -amount, | ||||
|                 'account_id': account_id, | ||||
|                 'quantity': 1.0, | ||||
|                 'discount': 0.0, | ||||
|                 'uom_id': product_id.uom_id.id, | ||||
|                 'product_id': product_id.id, | ||||
|                 'invoice_id': invoice.id, | ||||
|                 'invoice_line_tax_ids': [(6, 0, taxes_ids)], | ||||
|                 'sequence': 100, | ||||
|             }) | ||||
|             if amount > 0: | ||||
|                 # 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)], | ||||
|                     'sequence': 99999, | ||||
|                 }) | ||||
|         return True | ||||
|  |  | |||
|  | @ -2,6 +2,7 @@ | |||
| 
 | ||||
| from odoo import fields, models, api, _ | ||||
| from odoo.exceptions import UserError | ||||
| from lxml import html | ||||
| 
 | ||||
| class SaleOrder(models.Model): | ||||
|     _inherit = "sale.order" | ||||
|  | @ -30,12 +31,16 @@ class SaleOrder(models.Model): | |||
| 
 | ||||
|         for order in self: | ||||
|             amount = 0 | ||||
|             if order.dealer_discount_type == 'fixed': | ||||
|             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_product_id.name + ", " | ||||
|                 discount_text = "-" + str(order.dealer_discount_percentage) + "% " + discount_text + ", " | ||||
|                 discount_pos = "" | ||||
|                 n=0 | ||||
|                 nd=0 | ||||
|  |  | |||
		Loading…
	
		Reference in New Issue