2018-03-16 16:04:33 +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 Reservation Invoice Wizard"""
|
|
|
|
|
|
|
|
from odoo import models, fields, api, _
|
|
|
|
from odoo.exceptions import ValidationError, UserError
|
|
|
|
|
|
|
|
|
|
|
|
class GolemReservationInvoiceWizard(models.TransientModel):
|
|
|
|
""" GOLEM Resource Reservation Invoice Wizard """
|
|
|
|
_name = 'golem.reservation.invoice.wizard'
|
|
|
|
|
|
|
|
reservation_ids = fields.Many2many('golem.resource.reservation',
|
|
|
|
default=lambda self: self._context.get('active_ids', []),
|
|
|
|
string='Reservations to invoice')
|
|
|
|
|
|
|
|
@api.multi
|
|
|
|
def create_invoices(self):
|
2018-03-19 15:07:48 +01:00
|
|
|
""" Create invoices for reservation """
|
2018-03-16 16:04:33 +01:00
|
|
|
self.ensure_one()
|
|
|
|
if self.reservation_ids:
|
|
|
|
|
|
|
|
inv_obj = self.env['account.invoice']
|
|
|
|
partner_id = self.reservation_ids[0].partner_id
|
|
|
|
product = self.reservation_ids[0].resource_id.product_tmpl_id
|
|
|
|
|
|
|
|
if product.id:
|
|
|
|
account_id = product.property_account_income_id.id
|
|
|
|
if not account_id:
|
|
|
|
account_id = product.categ_id.property_account_income_categ_id.id
|
|
|
|
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,))
|
|
|
|
|
|
|
|
lines = []
|
|
|
|
|
|
|
|
for reservation in self.reservation_ids:
|
2018-03-19 15:07:48 +01:00
|
|
|
if partner_id != reservation.partner_id:
|
|
|
|
raise ValidationError(
|
|
|
|
_("You can't group reservations of multiple clients in the same \
|
|
|
|
invoice, please remove inadequate reservations"))
|
|
|
|
|
2018-03-16 16:04:33 +01:00
|
|
|
product = reservation.resource_id.product_tmpl_id
|
|
|
|
amount = product.standard_price
|
2018-03-17 16:23:39 +01:00
|
|
|
quantity = reservation.hour_stop - reservation.hour_start
|
2018-03-16 16:04:33 +01:00
|
|
|
lines.append((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-16 16:04:33 +01:00
|
|
|
'discount': 0.0,
|
|
|
|
'uom_id': product.uom_id.id,
|
|
|
|
'product_id': product.id,
|
|
|
|
}))
|
|
|
|
invoice = inv_obj.create({
|
2018-03-19 15:07:48 +01:00
|
|
|
#'name': reservation.name,
|
|
|
|
#'origin': ,
|
2018-03-16 16:04:33 +01:00
|
|
|
'type': 'out_invoice',
|
|
|
|
'reference': False,
|
|
|
|
'account_id': partner_id.property_account_receivable_id.id,
|
|
|
|
'partner_id': partner_id.id,
|
|
|
|
'invoice_line_ids': lines,
|
|
|
|
})
|
2018-03-16 18:38:18 +01:00
|
|
|
self.reservation_ids.write({'invoice_id': invoice.id})
|
2018-03-18 01:18:22 +01:00
|
|
|
if self._context.get('open_invoices', False):
|
|
|
|
return {'name' : _('Reservation Invoice'),
|
|
|
|
'type' : 'ir.actions.act_window',
|
|
|
|
'res_model' : 'account.invoice',
|
|
|
|
'res_id' : invoice.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'}
|
|
|
|
return {'type': 'ir.actions.act_window_close'}
|