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"""
|
|
|
|
|
2018-03-16 18:38:18 +01:00
|
|
|
|
2018-03-15 17:28:21 +01:00
|
|
|
from odoo import models, fields, api, _
|
2018-03-19 15:07:48 +01:00
|
|
|
from odoo.exceptions import UserError
|
2018-03-15 17:28:21 +01:00
|
|
|
|
|
|
|
|
|
|
|
class GolemResourceReservation(models.Model):
|
|
|
|
""" GOLEM Resource Reservation Adaptation """
|
|
|
|
_inherit = 'golem.resource.reservation'
|
|
|
|
|
2018-03-16 16:13:18 +01:00
|
|
|
invoice_id = fields.Many2one('account.invoice')
|
2018-03-17 16:23:39 +01:00
|
|
|
invoicing_state = fields.Char(compute="_compute_invoicing_state",
|
|
|
|
search='_search_invoicing_state',
|
|
|
|
string="Invoicing Status",
|
|
|
|
default="None")
|
|
|
|
|
|
|
|
def _search_invoicing_state(self, operator, value):
|
|
|
|
if value == "None":
|
2018-03-19 15:07:48 +01:00
|
|
|
reservation = self.env['golem.resource.reservation'].search(
|
|
|
|
[('invoice_id', '=', False)])
|
2018-03-17 16:23:39 +01:00
|
|
|
return [('id', 'in', reservation.mapped('id'))]
|
|
|
|
else:
|
|
|
|
return [('invoice_id.state', operator, value)]
|
|
|
|
|
|
|
|
@api.multi
|
|
|
|
@api.depends('invoice_id')
|
|
|
|
def _compute_invoicing_state(self):
|
|
|
|
""" Compute invoicing_state """
|
|
|
|
for reservation in self:
|
|
|
|
if reservation.invoice_id:
|
|
|
|
reservation.invoicing_state = reservation.invoice_id.state
|
|
|
|
else:
|
|
|
|
reservation.invoicing_state = "None"
|
2018-03-16 16:13:18 +01:00
|
|
|
|
2018-03-18 01:18:22 +01:00
|
|
|
@api.multi
|
2018-03-19 15:07:48 +01:00
|
|
|
def open_invoice(self):
|
|
|
|
""" open invoice """
|
2018-03-18 01:18:22 +01:00
|
|
|
for reservation in self:
|
|
|
|
if reservation.invoice_id:
|
|
|
|
return {'name' : _('Reservation Invoice'),
|
|
|
|
'type' : 'ir.actions.act_window',
|
|
|
|
'res_model' : 'account.invoice',
|
|
|
|
'res_id' : reservation.invoice_id.id,
|
|
|
|
'view_mode': 'form',
|
2018-03-19 15:07:48 +01:00
|
|
|
'view_id': self.env.ref('account.invoice_form').id,
|
2018-03-18 01:18:22 +01:00
|
|
|
'target': 'current'}
|
|
|
|
|
2018-03-16 16:04:33 +01:00
|
|
|
|
2018-03-15 17:28:21 +01:00
|
|
|
@api.multi
|
|
|
|
def create_invoice(self):
|
2018-03-19 15:07:48 +01:00
|
|
|
""" Create invoice """
|
2018-03-15 17:28:21 +01:00
|
|
|
for reservation in self:
|
2018-03-19 18:05:58 +01:00
|
|
|
if reservation.state != "validated":
|
|
|
|
raise UserError(
|
|
|
|
_("The reservation '%s' is not validated, please validate it before \
|
|
|
|
creating invoice") % reservation.name)
|
|
|
|
|
2018-03-15 17:28:21 +01:00
|
|
|
inv_obj = self.env['account.invoice']
|
|
|
|
partner_id = reservation.partner_id
|
|
|
|
product = reservation.resource_id.product_tmpl_id
|
|
|
|
amount = product.standard_price
|
2018-03-19 18:05:58 +01:00
|
|
|
delta = fields.Datetime.from_string(reservation.date_stop) - \
|
|
|
|
fields.Datetime.from_string(reservation.date_start)
|
|
|
|
|
|
|
|
quantity = (delta.days * 24) + (delta.seconds/3600.0)
|
|
|
|
account_id = False
|
|
|
|
if not product:
|
|
|
|
raise UserError(
|
|
|
|
_("There is no product linked to resource : '%s', you can't invoice \
|
|
|
|
reservation with no product linked") % (reservation.resource_id.name,))
|
2018-03-15 17:28:21 +01:00
|
|
|
if product.id:
|
|
|
|
account_id = product.property_account_income_id.id
|
2018-03-16 16:04:33 +01:00
|
|
|
|
2018-03-15 17:28:21 +01:00
|
|
|
if not account_id:
|
|
|
|
account_id = 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:
|
|
|
|
raise UserError(
|
|
|
|
_('There is no income account defined for this product: "%s". \
|
|
|
|
You may have to install a chart of account from Accounting \
|
|
|
|
app, settings menu.') % (product.name,))
|
2018-03-19 15:07:48 +01:00
|
|
|
invoice = inv_obj.create({
|
2018-03-15 17:28:21 +01:00
|
|
|
'name': reservation.name,
|
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,
|
|
|
|
'partner_id': partner_id.id,
|
|
|
|
'invoice_line_ids': [(0, 0, {
|
|
|
|
'name': reservation.resource_id.name,
|
|
|
|
#'origin': ,
|
|
|
|
'account_id': account_id,
|
|
|
|
'price_unit': amount,
|
2018-03-17 16:23:39 +01:00
|
|
|
'quantity': quantity,
|
2018-03-15 17:28:21 +01:00
|
|
|
'discount': 0.0,
|
|
|
|
'uom_id': product.uom_id.id,
|
|
|
|
'product_id': product.id,
|
|
|
|
})],
|
|
|
|
})
|
2018-03-19 15:07:48 +01:00
|
|
|
reservation.invoice_id = invoice.id
|