diff --git a/golem_resource_pack_account/models/golem_resource_pack.py b/golem_resource_pack_account/models/golem_resource_pack.py
index fc80578..c541dad 100644
--- a/golem_resource_pack_account/models/golem_resource_pack.py
+++ b/golem_resource_pack_account/models/golem_resource_pack.py
@@ -32,21 +32,38 @@ class GolemResourcePack(models.Model):
copy=False)
invoice_amount_total = fields.Monetary(related='invoice_id.amount_total')
currency_id = fields.Many2one(related='invoice_id.currency_id')
- is_products_set = fields.Boolean(compute="compute_is_products_set")
+ is_products_set = fields.Boolean(compute="_compute_is_products_set")
@api.multi
- def compute_is_products_set(self):
+ def _compute_is_products_set(self):
+ """ compute is_products_set """
for pack in self:
- product_list = list(map(lambda x: x.resource_product_id, pack.reservation_ids))
- if len(filter(lambda x: x.id == False, product_list)) > 0:
+ if len(filter(lambda x: x.resource_product_id.id is False, pack.reservation_ids)) \
+ > 0:
pack.is_products_set = False
else:
pack.is_products_set = True
+ @api.multi
+ def chek_pack_to_invoice(self):
+ """ chek pack before invoicing """
+ for pack in self:
+ if pack.state != 'validated':
+ raise ValidationError(_('The current pack is not validated, please validate '
+ 'it before creating invoice'))
+ elif not pack.is_products_set:
+ raise ValidationError(_('You can not create an invoice for a pack without '
+ 'linked product on every resource reserved.'))
+ elif pack.invoice_id.id:
+ raise ValidationError(_('You can not create an invoice as there '
+ 'is already one.'))
+
+
@api.multi
def create_invoice(self):
""" Invoice creation """
for pack in self:
+ pack.chek_pack_to_invoice()
pack.reservation_ids.check_before_invoicing()
inv_obj = self.env['account.invoice']
partner_id = pack.partner_id
@@ -56,7 +73,7 @@ class GolemResourcePack(models.Model):
'reference': False,
'account_id': partner_id.property_account_receivable_id.id,
'partner_id': partner_id.id
- })
+ })
pack.invoice_id = invoice_id.id
pack.reservation_ids.create_invoice_line(invoice_id)
diff --git a/golem_resource_pack_account/security/ir.model.access.csv b/golem_resource_pack_account/security/ir.model.access.csv
deleted file mode 100644
index 796d31b..0000000
--- a/golem_resource_pack_account/security/ir.model.access.csv
+++ /dev/null
@@ -1,3 +0,0 @@
-id,name,model_id:id,group_id:id,perm_read,perm_write,perm_create,perm_unlink
-access_golem_resource_pack_user,Access GOLEM Resource Pack User,model_golem_resource_pack,golem_base.group_golem_user,1,0,0,0
-access_golem_resource_pack_manager,Access GOLEM Resource Pack Manager,model_golem_resource_pack,golem_base.group_golem_manager,1,1,1,1
diff --git a/golem_resource_pack_account/models/golem_resource_reservation.py b/golem_resource_pack_account/tests/__init__.py
similarity index 67%
rename from golem_resource_pack_account/models/golem_resource_reservation.py
rename to golem_resource_pack_account/tests/__init__.py
index bf4c7c6..69aaa1d 100644
--- a/golem_resource_pack_account/models/golem_resource_reservation.py
+++ b/golem_resource_pack_account/tests/__init__.py
@@ -15,17 +15,4 @@
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see .
-
-""" GOLEM Resource Reservation """
-
-from math import modf
-from datetime import timedelta
-from odoo import models, fields, api, _
-from odoo.exceptions import ValidationError
-
-
-class GolemResourceReservation(models.Model):
- """ GOLEM Resource Reservation Model """
- _inherit = 'golem.resource.reservation'
-
- pack_id = fields.Many2one('golem_resource_reservation', 'Reservation Pack')
+from . import test_golem_pack_invoice
diff --git a/golem_resource_pack_account/tests/test_golem_pack_invoice.py b/golem_resource_pack_account/tests/test_golem_pack_invoice.py
new file mode 100644
index 0000000..29961a5
--- /dev/null
+++ b/golem_resource_pack_account/tests/test_golem_pack_invoice.py
@@ -0,0 +1,114 @@
+# -*- coding: utf-8 -*-
+
+# Copyright 2018 Youssef El Ouahby
+# Copyright 2018 Fabien Bourgeois
+#
+# 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 .
+
+""" GOLEM Resource Reservation testing """
+
+import logging
+from odoo.tests.common import TransactionCase
+from odoo.exceptions import ValidationError
+_LOGGER = logging.getLogger(__name__)
+
+
+class TestGolemResourcePack(TransactionCase):
+ """ GOLEM Resource Pack testing """
+
+ def setUp(self, *args, **kwargs):
+ """ Bootstrap Resource Reservation """
+ super(TestGolemResourcePack, self).setUp(*args, **kwargs)
+ # set product
+ self.product = self.env['product.template'].create({
+ 'name': 'Product',
+ 'categ_id': self.env.ref('product.product_category_all').id,
+ 'list_price': 7.0,
+ 'type': 'service',
+ 'uom_id': self.env.ref('product.product_uom_hour').id,
+ 'uom_po_id': self.env.ref('product.product_uom_hour').id,
+ 'property_account_income_id': self.env.ref('l10n_fr.pcg_706').id
+ })
+ #set resources
+ self.resource_1 = self.env['golem.resource'].create({
+ 'name': 'Resource1',
+ 'product_tmpl_id': self.product.id,
+ 'avaibility_start': '2018-01-01',
+ 'avaibility_stop': '2020-01-01',
+ 'availibility_24_7': True
+ })
+ self.resource_2 = self.env['golem.resource'].create({
+ 'name': 'Resource2',
+ 'avaibility_start': '2018-01-01',
+ 'avaibility_stop': '2020-01-01',
+ 'availibility_24_7': True
+ })
+
+ #set partners
+ self.partner_1 = self.env['res.partner'].create({'firstname': 'John',
+ 'lastname': 'DOE',
+ 'is_company': False})
+ self.partner_2 = self.env['res.partner'].create({'firstname': 'John2',
+ 'lastname': 'DOE2',
+ 'is_company': False})
+
+ # set reservations
+ self.reservation_1 = self.env['golem.resource.reservation'].create({
+ 'resource_id': self.resource_1.id,
+ 'date_start': '2018-02-05 11:00:00',
+ 'date_stop': '2018-02-05 12:00:00',
+ 'partner_id': self.partner_1.id
+ })
+ self.reservation_2 = self.env['golem.resource.reservation'].create({
+ 'resource_id': self.resource_1.id,
+ 'date_start': '2018-02-06 11:00:00',
+ 'date_stop': '2018-02-06 12:00:00',
+ 'partner_id': self.partner_1.id
+ })
+
+ #set pack env
+ self.pack_obj = self.env['golem.resource.pack']
+ self.pack_data = {
+ 'partner_id': self.partner_1.id,
+ 'reservation_ids': [(4, self.reservation_1.id, 0),
+ (4, self.reservation_2.id, 0)]}
+
+
+ def test_pack_invoice_basic(self):
+ """ Test pack invoice basic """
+ pack = self.pack_obj.create(self.pack_data)
+ pack.state_confirm()
+ pack.create_invoice()
+ self.assertTrue(pack.invoice_id.id)
+ self.assertEqual(pack.invoice_state, 'draft')
+
+ def test_unallowed_pack_invoice(self):
+ """ Test unallowed pack invoice cases """
+ pack = self.pack_obj.create(self.pack_data)
+ with self.assertRaises(ValidationError) as err:
+ pack.create_invoice()
+ self.assertIn(u'current pack is not validated', err.exception.args[0])
+ self.reservation_2.write({'resource_id': self.resource_2.id})#no product linked
+ pack.state_confirm()
+ with self.assertRaises(ValidationError) as err:
+ pack.create_invoice()
+ self.assertIn(u'linked product on every resource', err.exception.args[0])
+ pack.state_draft()
+ self.reservation_2.write({'resource_id': self.resource_1.id})# with product linked
+ pack.state_confirm()
+ pack.create_invoice()
+ self.assertTrue(pack.invoice_id.id)
+ with self.assertRaises(ValidationError) as err:
+ pack.create_invoice()
+ self.assertIn(u'can not create an invoice as there', err.exception.args[0])