2018-03-15 17:28:21 +01:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright 2018 Youssef El Ouahby <youssef@yaltik.com>
|
|
|
|
# Copyright 2018 Fabien Bourgeois <fabien@yaltik.com>
|
|
|
|
#
|
|
|
|
# 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/>.
|
|
|
|
|
|
|
|
""" GOLEM Resource Reservation Adaptation"""
|
|
|
|
|
|
|
|
from odoo import models, fields, api, _
|
2018-03-26 11:11:16 +02:00
|
|
|
from odoo.exceptions import ValidationError
|
2018-03-15 17:28:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
class GolemResourceReservation(models.Model):
|
|
|
|
""" GOLEM Resource Reservation Adaptation """
|
|
|
|
_inherit = 'golem.resource.reservation'
|
|
|
|
|
2018-03-26 11:37:31 +02:00
|
|
|
resource_product_id = fields.Many2one(related='resource_id.product_tmpl_id')
|
2018-03-26 17:20:53 +02:00
|
|
|
invoice_line_id = fields.Many2one('account.invoice.line', copy=False)
|
2018-03-26 12:02:41 +02:00
|
|
|
invoice_line_price_subtotal = fields.Monetary(related='invoice_line_id.price_subtotal')
|
2018-03-26 12:35:12 +02:00
|
|
|
invoice_id = fields.Many2one(related='invoice_line_id.invoice_id',
|
|
|
|
string='Invoice')
|
2018-03-26 17:20:53 +02:00
|
|
|
invoice_state = fields.Selection(related='invoice_id.state', store=True,
|
|
|
|
copy=False)
|
2018-03-26 12:35:12 +02:00
|
|
|
invoice_amount_total = fields.Monetary(related='invoice_id.amount_total')
|
2018-03-26 12:02:41 +02:00
|
|
|
currency_id = fields.Many2one(related='invoice_id.currency_id')
|
2018-03-17 16:23:39 +01:00
|
|
|
|
|
|
|
@api.multi
|
2018-03-26 17:08:03 +02:00
|
|
|
def check_before_invoicing(self):
|
|
|
|
""" Checks data coherence before invoicing """
|
2018-03-17 16:23:39 +01:00
|
|
|
for reservation in self:
|
2018-03-26 17:59:49 +02:00
|
|
|
if reservation.state != "validated":
|
|
|
|
raise ValidationError(
|
|
|
|
_('The reservation "{}" is not validated, please validate '
|
|
|
|
'it before creating invoice'.format(reservation.name)))
|
2018-03-26 12:36:17 +02:00
|
|
|
if reservation.invoice_line_id:
|
|
|
|
raise ValidationError(_('You can not create an invoice as there '
|
|
|
|
'is already one.'))
|
2018-03-15 17:28:21 +01:00
|
|
|
product = reservation.resource_id.product_tmpl_id
|
|
|
|
|
2018-03-26 11:11:16 +02:00
|
|
|
if not product:
|
|
|
|
raise ValidationError(_('You can not create an invoice without '
|
|
|
|
'linked product on the resource reserved.'))
|
2018-03-16 16:04:33 +01:00
|
|
|
|
2018-03-26 11:11:16 +02:00
|
|
|
account_id = product.property_account_income_id.id or \
|
|
|
|
product.categ_id.property_account_income_categ_id.id
|
2018-03-16 16:04:33 +01:00
|
|
|
|
2018-03-15 17:28:21 +01:00
|
|
|
if not account_id:
|
2018-03-26 11:11:16 +02:00
|
|
|
raise ValidationError(
|
2018-03-26 11:22:36 +02:00
|
|
|
_('There is no income account defined for this product: "{}"'
|
|
|
|
'. You have to configure it on the product form.'.format(product.name)))
|
2018-03-16 16:13:18 +01:00
|
|
|
|
2018-03-18 01:18:22 +01:00
|
|
|
@api.multi
|
2018-03-26 17:08:03 +02:00
|
|
|
def create_invoice_line(self, invoice_id):
|
|
|
|
""" Create invoice line corresponding to reservation """
|
2018-03-18 01:18:22 +01:00
|
|
|
for reservation in self:
|
2018-03-26 17:08:03 +02:00
|
|
|
product = reservation.resource_id.product_tmpl_id
|
|
|
|
amount = product.list_price
|
|
|
|
account_id = product.property_account_income_id.id or \
|
|
|
|
product.categ_id.property_account_income_categ_id.id
|
2018-03-26 17:59:49 +02:00
|
|
|
delta = fields.Datetime.from_string(reservation.date_stop) - \
|
|
|
|
fields.Datetime.from_string(reservation.date_start)
|
|
|
|
quantity = (delta.days * 24) + (delta.seconds/3600.0)
|
2018-03-18 01:18:22 +01:00
|
|
|
|
2018-03-26 17:08:03 +02:00
|
|
|
line_id = self.env['account.invoice.line'].create({
|
|
|
|
'invoice_id': invoice_id.id,
|
|
|
|
'name': product.name,
|
|
|
|
'origin': reservation.name,
|
|
|
|
'price_unit': amount,
|
2018-03-26 17:59:49 +02:00
|
|
|
'quantity': quantity,
|
2018-03-26 17:08:03 +02:00
|
|
|
'uom_id': product.uom_id.id,
|
|
|
|
'account_id': account_id,
|
|
|
|
'product_id': product.id,
|
|
|
|
})
|
|
|
|
reservation.invoice_line_id = line_id
|
2018-03-16 16:04:33 +01:00
|
|
|
|
2018-03-15 17:28:21 +01:00
|
|
|
@api.multi
|
|
|
|
def create_invoice(self):
|
2018-03-26 17:08:03 +02:00
|
|
|
""" Invoice creation """
|
2018-03-15 17:28:21 +01:00
|
|
|
for reservation in self:
|
2018-03-26 17:08:03 +02:00
|
|
|
reservation.check_before_invoicing()
|
2018-03-15 17:28:21 +01:00
|
|
|
inv_obj = self.env['account.invoice']
|
|
|
|
partner_id = reservation.partner_id
|
2018-03-16 16:04:33 +01:00
|
|
|
|
2018-03-26 17:08:03 +02:00
|
|
|
invoice_id = inv_obj.create({
|
2018-03-19 15:07:48 +01:00
|
|
|
'origin': reservation.name,
|
2018-03-15 17:28:21 +01:00
|
|
|
'type': 'out_invoice',
|
|
|
|
'reference': False,
|
|
|
|
'account_id': partner_id.property_account_receivable_id.id,
|
2018-03-26 17:08:03 +02:00
|
|
|
'partner_id': partner_id.id
|
2018-03-26 12:36:17 +02:00
|
|
|
})
|
2018-03-26 17:08:03 +02:00
|
|
|
reservation.create_invoice_line(invoice_id)
|
2018-03-26 12:36:42 +02:00
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def show_invoice(self):
|
|
|
|
""" Redirects to linked invoice """
|
|
|
|
self.ensure_one()
|
|
|
|
reservation = self[0]
|
|
|
|
if reservation.invoice_id:
|
|
|
|
return {'type': 'ir.actions.act_window',
|
|
|
|
'res_model': 'account.invoice',
|
|
|
|
'res_id': reservation.invoice_id.id,
|
|
|
|
'view_mode': 'form',
|
|
|
|
'view_id': self.env.ref('account.invoice_form').id}
|