63 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
			
		
		
	
	
			63 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			Python
		
	
	
		
			Executable File
		
	
| # -*- coding: utf-8 -*-
 | |
| 
 | |
| from odoo import fields, models, api, _
 | |
| from odoo.exceptions import UserError
 | |
| 
 | |
| class Invoice(models.Model):
 | |
|     _inherit = "account.invoice"
 | |
| 
 | |
|     global_discount = fields.Boolean("Add Global Disocunt", readonly=True, states={'draft': [('readonly', False)]},)
 | |
|     discount_type = fields.Selection([('fixed','Fixed'),('percentage','Percentage')], 
 | |
|         "Disocunt Type", readonly=True, states={'draft': [('readonly', False)]}, default='fixed')
 | |
|     discount_amount = fields.Float("Disocunt Amount", readonly=True, states={'draft': [('readonly', False)]},)
 | |
|     discount_percentage = fields.Float("Disocunt Percentage", readonly=True, states={'draft': [('readonly', False)]},)
 | |
| 
 | |
|     @api.multi
 | |
|     def _discount_unset(self):
 | |
|         if self.env.user.company_id.invoice_discount_product_id:
 | |
|             self.env['account.invoice.line'].search([('invoice_id', 'in', self.ids), ('product_id', '=', self.env.user.company_id.invoice_discount_product_id.id)]).unlink()
 | |
| 
 | |
|     @api.multi
 | |
|     def create_discount(self):
 | |
|         InvoiceLine = self.env['account.invoice.line']
 | |
| 
 | |
|         product_id = self.env.user.company_id.invoice_discount_product_id
 | |
|         if not product_id:
 | |
|             raise UserError(_('Please set Invoice Discount product in General Settings first.'))
 | |
| 
 | |
|         # Remove Discount line first
 | |
|         self._discount_unset()
 | |
| 
 | |
|         account_id = 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 invoice.discount_type == 'fixed':
 | |
|                 amount = invoice.discount_amount
 | |
|             if invoice.discount_type == 'percentage':
 | |
|                 amount = (invoice.amount_total * invoice.discount_percentage)/100
 | |
| 
 | |
|             # Apply fiscal position
 | |
|             taxes = 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
 | |
| 
 | |
|             # 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,
 | |
|             })
 | |
|         return True
 |