From 9a958fffb8b2d9b6e012c06eadc89498fdcfb575 Mon Sep 17 00:00:00 2001 From: Feilong Wang Date: Fri, 1 Sep 2017 13:41:25 +1200 Subject: [PATCH] Fix grant product Change-Id: I942e21d79bb809fc54bb5224edb21903206493de --- distil/erp/drivers/odoo.py | 7 ++++-- distil/tests/unit/erp/drivers/test_odoo.py | 28 +++++++++++++++++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/distil/erp/drivers/odoo.py b/distil/erp/drivers/odoo.py index 330fc1b..54cbb77 100644 --- a/distil/erp/drivers/odoo.py +++ b/distil/erp/drivers/odoo.py @@ -16,6 +16,7 @@ import collections from decimal import Decimal import json +import re import odoorpc from oslo_log import log @@ -229,8 +230,10 @@ class OdooDriver(driver.BaseDriver): 'cost': round(line['price_subtotal'], constants.PRICE_DIGITS) } - # Original product is a string like "[hour] NZ-POR-1.c1.c2r8" - product = line['product_id'][1].split(']')[1].strip() + product = line['product_id'][1] + if re.match(r"\[.+\].+", product): + product = product.split(']')[1].strip() + category = self.product_category_mapping[line['product_id'][0]] if category not in detail_dict: diff --git a/distil/tests/unit/erp/drivers/test_odoo.py b/distil/tests/unit/erp/drivers/test_odoo.py index 66aed13..b20448e 100644 --- a/distil/tests/unit/erp/drivers/test_odoo.py +++ b/distil/tests/unit/erp/drivers/test_odoo.py @@ -151,17 +151,26 @@ class TestOdooDriver(base.DistilTestCase): 'uos_id': [1, 'Gigabyte-hour(s)'], 'price_subtotal': 0.492, 'product_id': [1, '[hour] NZ-POR-1.c1.c2r8'] + }, + { + "name": "Development Grant", + "quantity": 1, + "price_unit": -0.1, + 'uos_id': [1, 'Unit(s)'], + "price_subtotal": -0.1, + 'product_id': [4, 'cloud-dev-grant'] } ] ] odoodriver.odoo.execute.return_value = [ {'id': 1, 'date_invoice': '2017-03-31', 'amount_total': 0.371, 'state': 'paid'}, - {'id': 2, 'date_invoice': '2017-04-30', 'amount_total': 0.859, + {'id': 2, 'date_invoice': '2017-04-30', 'amount_total': 0.759, 'state': 'open'} ] odoodriver.product_category_mapping = { - 1: 'Compute' + 1: 'Compute', + 4: 'Discounts' } invoices = odoodriver.get_invoices( @@ -200,9 +209,22 @@ class TestOdooDriver(base.DistilTestCase): } }, '2017-04-30': { - 'total_cost': 0.86, + 'total_cost': 0.76, 'status': 'open', 'details': { + "Discounts":{ + "total_cost": -0.1, + "breakdown":{ + 'cloud-dev-grant': [ + { + 'quantity': 1.0, + 'unit': 'Unit(s)', + 'cost': -0.1, + 'resource_name': 'Development Grant', + 'rate': -0.1} + ] + } + }, 'Compute': { 'total_cost': 0.86, 'breakdown': {