202 lines
		
	
	
		
			9.1 KiB
		
	
	
	
		
			Python
		
	
	
			
		
		
	
	
			202 lines
		
	
	
		
			9.1 KiB
		
	
	
	
		
			Python
		
	
	
| # -*- coding: utf-8 -*-
 | |
| ##############################################################################
 | |
| #
 | |
| #    datenpol gmbh
 | |
| #    Copyright (C) 2013-TODAY datenpol gmbh (<http://www.datenpol.at/>)
 | |
| #
 | |
| #    This program is free software: you can redistribute it and/or modify
 | |
| #    it under the terms of the GNU Affero General Public License as
 | |
| #    published by the Free Software Foundation, either version 3 of the
 | |
| #    License, or (at your option) any later version.
 | |
| #
 | |
| #    This program is distributed in the hope that it will be useful,
 | |
| #    but WITHOUT ANY WARRANTY; without even the implied warranty of
 | |
| #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 | |
| #    GNU Affero General Public License for more details.
 | |
| #
 | |
| #    You should have received a copy of the GNU Affero General Public License
 | |
| #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 | |
| #
 | |
| ##############################################################################
 | |
| from odoo import api, fields, models, _
 | |
| from odoo.exceptions import ValidationError
 | |
| 
 | |
| 
 | |
| class AccountInvoice(models.Model):
 | |
|     _inherit = 'account.invoice'
 | |
| 
 | |
|     @api.model
 | |
|     def default_inter_company_supplier_isset(self):
 | |
|         return bool(self.env.user.company_id.inter_company_supplier_id.id)
 | |
| 
 | |
|     inter_company_supplier_isset = fields.Boolean(compute='_compute_inter_company_supplier_isset',
 | |
|                                                   default=default_inter_company_supplier_isset)
 | |
| 
 | |
|     reimburse_invoice_id = fields.Many2one(comodel_name='account.invoice', string='Weiterverrechnung')
 | |
|     charge_further = fields.Boolean(string='Weiterverrechnen', compute='_compute_charge_further', store=True,
 | |
|                                     help='Ist gesetzt, wenn das WV-Flag von mindestens einer Zeile gesetzt ist',track_visibility='onchange')
 | |
| 
 | |
|     pg_ic_num = fields.Char('PG_IC_Nummer')
 | |
|     pg_ic_flag = fields.Boolean(string='IC', help='Rechnung nach PG exportiert',track_visibility='onchange')
 | |
|     pg_admin = fields.Boolean(compute='_is_pg_admin', default=False, store=False)
 | |
| 
 | |
|     @api.multi
 | |
|     def _is_pg_admin(self):
 | |
|         for record in self:
 | |
|             record.pg_admin = self.env.user.has_group('base.group_system')
 | |
| 
 | |
|     @api.multi
 | |
|     def _compute_inter_company_supplier_isset(self):
 | |
|         for record in self:
 | |
|             record.inter_company_supplier_isset = bool(self.env.user.company_id.inter_company_supplier_id.id)
 | |
| 
 | |
|     @api.multi
 | |
|     @api.depends('invoice_line_ids.reimbursement')
 | |
|     def _compute_charge_further(self):
 | |
|         for record in self:
 | |
|             record.charge_further = False
 | |
|             for line in record.invoice_line_ids:
 | |
|                 if line.reimbursement:
 | |
|                     record.charge_further = True
 | |
|                     break
 | |
| 
 | |
|     @api.multi
 | |
|     def action_set_all_wv_flag(self):
 | |
|         for record in self:
 | |
|             record.charge_further = True
 | |
|             for line in record.invoice_line_ids:
 | |
|                 line.reimbursement = True
 | |
| 
 | |
|     @api.multi
 | |
|     def action_unset_all_wv_flag(self):
 | |
|         for record in self:
 | |
|             record.charge_further = False
 | |
|             for line in record.invoice_line_ids:
 | |
|                 line.reimbursement = False
 | |
| 
 | |
|     @api.multi
 | |
|     def reimburse_invoice(self):
 | |
|         for record in self:
 | |
|             if not record.charge_further:
 | |
|                 raise ValidationError(_('Sie müssen mindestens eine Position mit WV markieren.'))
 | |
|             if record.charge_further and not record.reimburse_invoice_id and record.state not in ['draft']:
 | |
|                 intercompany_admin_id = record.company_id.admin_user_id
 | |
|                 in_invoice_vals = record._prepare_er_invoice_data()
 | |
|                 invoice_id = self.env['account.invoice'].sudo(intercompany_admin_id).create(
 | |
|                     in_invoice_vals)
 | |
|                 record.reimburse_invoice_id = invoice_id.id
 | |
|                 for line in record.invoice_line_ids:
 | |
|                     if line.reimbursement:
 | |
|                         in_line_vals = record._prepare_er_invoice_line_data(line, invoice_id)
 | |
|                         self.env['account.invoice.line'].sudo(intercompany_admin_id).create(in_line_vals)
 | |
|                 invoice_id.compute_taxes()
 | |
| 
 | |
|     @api.multi
 | |
|     def _prepare_er_invoice_data(self):
 | |
|         self.ensure_one()
 | |
| 
 | |
|         journal = self.env['account.journal'].sudo(self.company_id.admin_user_id).search(
 | |
|             [('type', '=', 'purchase'), ('company_id', '=', self.company_id.id)], limit=1)
 | |
|         vals = {
 | |
|             'company_id': self.company_id.id,
 | |
|             'type': 'in_invoice',
 | |
|             'partner_id': self.company_id.inter_company_supplier_id.partner_id.id,
 | |
|             'origin': "Weiterverrechnung von " + self.number,
 | |
|             'journal_id': journal.id,
 | |
|             'currency_id': self.currency_id and self.currency_id.id,
 | |
|         }
 | |
|         inv = self.env['account.invoice'].with_context(company_id=self.company_id.id).sudo(
 | |
|             self.company_id.admin_user_id).new(vals)
 | |
|         inv._onchange_partner_id()
 | |
|         new_vals = {k: v or False for k, v in dict(inv._cache).items()}
 | |
|         return inv._convert_to_write(new_vals)
 | |
| 
 | |
|     @api.model
 | |
|     def _prepare_er_invoice_line_data(self, line, invoice_id):
 | |
|         vals = {
 | |
|             'name': line.name,
 | |
|             'price_unit': line.price_unit * (line.invoice_id.company_id.percentage_billing / 100),
 | |
|             'quantity': line.quantity,
 | |
|             'discount': line.discount,
 | |
|             'product_id': line.product_id.id or False,
 | |
|             'uom_id': line.uom_id.id or False,
 | |
|             'sequence': line.sequence,
 | |
|             'invoice_id': invoice_id.id
 | |
|         }
 | |
|         new_line = self.env['account.invoice.line'].with_context(company_id=line.invoice_id.company_id.id).sudo(
 | |
|             line.invoice_id.company_id.admin_user_id).new(vals)
 | |
|         new_line._onchange_product_id()
 | |
|         new_line_vals = {k: v or False for k, v in dict(new_line._cache).items()}
 | |
|         return new_line._convert_to_write(new_line_vals)
 | |
| 
 | |
|     @api.multi
 | |
|     def invoice_validate(self):
 | |
|         res = super(AccountInvoice, self).invoice_validate()
 | |
|         for record in self:
 | |
|             if record.type == 'in_invoice' and record.partner_id.id == record.company_id.inter_company_supplier_id.partner_id.id:
 | |
|                 company_id = record.company_id.inter_company_supplier_id
 | |
|                 intercompany_admin_id = company_id.admin_user_id
 | |
|                 out_invoice_vals = record._prepare_ic_invoice_data()
 | |
|                 invoice_id = self.env['account.invoice'].sudo(intercompany_admin_id).create(
 | |
|                     out_invoice_vals)
 | |
|                 for line in record.invoice_line_ids:
 | |
|                     out_line_vals = record._prepare_ic_invoice_line_data(line, invoice_id)
 | |
|                     self.env['account.invoice.line'].sudo(intercompany_admin_id).create(out_line_vals)
 | |
|                 invoice_id.compute_taxes()
 | |
|         return res
 | |
| 
 | |
|     @api.multi
 | |
|     def _prepare_ic_invoice_data(self):
 | |
|         self.ensure_one()
 | |
| 
 | |
|         journal = self.env['account.journal'].sudo(self.company_id.inter_company_supplier_id.admin_user_id).search(
 | |
|             [('type', '=', 'sale'), ('company_id', '=', self.company_id.inter_company_supplier_id.id)], limit=1)
 | |
|         vals = {
 | |
|             'company_id': self.company_id.inter_company_supplier_id.id,
 | |
|             'type': 'out_invoice',
 | |
|             'partner_id': self.company_id.partner_id.id,
 | |
|             'origin': self.origin,
 | |
|             'journal_id': journal.id,
 | |
|             'currency_id': self.currency_id and self.currency_id.id,
 | |
|         }
 | |
|         inv = self.env['account.invoice'].with_context(company_id=self.company_id.inter_company_supplier_id.id).sudo(
 | |
|             self.company_id.inter_company_supplier_id.admin_user_id).new(vals)
 | |
|         inv._onchange_partner_id()
 | |
|         new_vals = {k: v or False for k, v in dict(inv._cache).items()}
 | |
|         return inv._convert_to_write(new_vals)
 | |
| 
 | |
|     @api.model
 | |
|     def _prepare_ic_invoice_line_data(self, line, invoice_id):
 | |
|         vals = {
 | |
|             'name': line.name,
 | |
|             'quantity': line.quantity,
 | |
|             'discount': line.discount,
 | |
|             'product_id': line.product_id.id or False,
 | |
|             'uom_id': line.uom_id.id or False,
 | |
|             'sequence': line.sequence,
 | |
|             'invoice_id': invoice_id.id
 | |
|         }
 | |
|         admin_user = line.invoice_id.company_id.inter_company_supplier_id.admin_user_id
 | |
|         company_id = line.invoice_id.company_id.inter_company_supplier_id.id
 | |
|         new_line = self.env['account.invoice.line'].with_context(company_id=company_id).sudo(admin_user).new(vals)
 | |
|         new_line._onchange_product_id()
 | |
|         new_line.price_unit = line.price_unit
 | |
|         new_line_vals = {k: v or False for k, v in dict(new_line._cache).items()}
 | |
|         return new_line._convert_to_write(new_line_vals)
 | |
| 
 | |
| 
 | |
| class AccountInvoiceLine(models.Model):
 | |
|     _inherit = 'account.invoice.line'
 | |
| 
 | |
|     @api.model
 | |
|     def _default_reimbursement(self):
 | |
|         if self.env.user.company_id.inter_company_supplier_id:
 | |
|             return True
 | |
| 
 | |
|     reimbursement = fields.Boolean(string='WV', default=_default_reimbursement,
 | |
|                                    help='Wenn Weiterverrechnung (WV) gesetzt ist, dann wird diese Zeile intern weiterverrechnet')
 | |
| 
 | |
|     inter_company_supplier_isset = fields.Boolean(related='invoice_id.inter_company_supplier_isset')
 | |
| 
 | |
|     state = fields.Selection(related='invoice_id.state')
 |