1
1
# -*- coding: utf-8 -*-
2
2
# Copyright 2016-2017 LasLabs Inc.
3
- # License LGPL -3.0 or later (http://www.gnu.org/licenses/lgpl ).
3
+ # License AGPL -3.0 or later (http://www.gnu.org/licenses/agpl ).
4
4
5
5
import logging
6
6
7
- from odoo import _ , api , fields , models
7
+ from odoo import api , fields , models
8
8
9
9
10
10
_logger = logging .getLogger (__name__ )
@@ -14,6 +14,7 @@ class ClouderContract(models.Model):
14
14
""" It provides formulas specific to billing Clouder contracts. """
15
15
16
16
_name = 'clouder.contract'
17
+ _description = 'Clouder Contracts'
17
18
_inherits = {'account.analytic.account' : 'ref_contract_id' }
18
19
19
20
ref_contract_id = fields .Many2one (
@@ -35,7 +36,7 @@ def invoice_policy_map(self):
35
36
""" It returns a mapping of invoice policies to processing methods.
36
37
37
38
Returns:
38
- ( dict) Mapping keyed by invoice policy type, pointing to the
39
+ dict: Mapping keyed by invoice policy type, pointing to the
39
40
method that should determine the quantity to use for
40
41
invoicing.
41
42
The method will receive the recurring contract line and the
@@ -59,31 +60,29 @@ def get_invoice_line_quantity(self, account, account_line, invoice):
59
60
60
61
.. code-block:: python
61
62
62
- result = env['clouder.contract'].get_invoice_line_quantity(
63
- contract, line, invoice,
64
- )
63
+ result = env['clouder.contract'].get_invoice_line_quantity(
64
+ contract, line, invoice,
65
+ )
65
66
66
67
Args:
67
- account (account.analytic.account ): Contract that recurring
68
+ account (AccountAnalyticAccount ): Contract that recurring
68
69
invoice line belongs to. This is called
69
- account_line (account.analytic.invoice.line ): Recurring invoice
70
+ account_line (AccountAnalyticInvoiceLine ): Recurring invoice
70
71
line being referenced.
71
- invoice (account.invoice ): Invoice that is being created.
72
+ invoice (AccountInvoice ): Invoice that is being created.
72
73
Returns:
73
- ( int) Quantity to use on invoice line in the UOM defined on the
74
+ int: Quantity to use on invoice line in the UOM defined on the
74
75
``contract_line``.
75
76
"""
76
77
invoice_policy = account_line .product_id .invoice_policy
77
78
invoice_policy_map = self .invoice_policy_map
78
79
try :
79
80
method = invoice_policy_map [invoice_policy ]
80
81
except KeyError :
81
- _logger .info (_ (
82
+ _logger .info (
82
83
'No calculation method found for invoice policy "%s". '
83
- 'Defaulting to Flat Rate instead.'
84
- ) % (
85
- invoice_policy ,
86
- ))
84
+ 'Defaulting to Flat Rate instead.' , invoice_policy
85
+ )
87
86
method = invoice_policy_map ['order' ]
88
87
return method (account_line , invoice )
89
88
@@ -92,14 +91,15 @@ def _create_default_vals(self, account):
92
91
""" It returns default values to create and link new ClouderContracts.
93
92
94
93
Args:
95
- account (account.analytic.account ): Account that ClouderContract
94
+ account (AccountAnalyticAccount ): Account that ClouderContract
96
95
will reference.
97
96
Returns:
98
- ( dict) Values fed to ``create`` in ``_get_contract_by_account``.
97
+ dict: Values fed to ``create`` in ``_get_contract_by_account``.
99
98
"""
100
99
number = self .env ['ir.sequence' ].next_by_code ('clouder.contract' )
101
100
# Default to the current users company if not set
102
- company_id = account .company_id .id or self .env .user .company_id .id
101
+ company_id = (account .company_id and account .company_id .id or
102
+ self .env .user .company_id .id )
103
103
return {
104
104
'name' : number ,
105
105
'company_id' : company_id ,
@@ -111,11 +111,11 @@ def _get_contract_by_account(self, account, create=False):
111
111
""" It returns the ClouderContract or possibly creates a new one.
112
112
113
113
Args:
114
- account: (account.analytic.account ) Contract to search by.
114
+ account: (AccountAnalyticAccount ) Contract to search by.
115
115
create: (bool) True will create a new ClouderContract if one does
116
116
not already exist.
117
117
Returns:
118
- ( clouder.contract) Clouder contract associated with ``account``.
118
+ clouder.contract: Clouder contract associated with ``account``.
119
119
"""
120
120
contract = self .search ([('ref_contract_id' , '=' , account .id )])
121
121
if create and not contract :
@@ -126,11 +126,11 @@ def _get_contract_by_account(self, account, create=False):
126
126
def _get_quantity_flat (self , account_line , invoice ):
127
127
""" It returns the base quantity with no calculations
128
128
Args:
129
- account_line (account.analytic.invoice.line ): Recurring invoice
129
+ account_line (AccountAnalyticInvoiceLine ): Recurring invoice
130
130
line being referenced.
131
- invoice (account.invoice ): Invoice that is being created.
131
+ invoice (AccountInvoice ): Invoice that is being created.
132
132
Returns:
133
- ( float) Quantity with no calculations performed
133
+ float: Quantity with no calculations performed
134
134
"""
135
135
return account_line .quantity
136
136
@@ -139,11 +139,11 @@ def _get_quantity_threshold(self, account_line, invoice):
139
139
""" It functions like flat rate for the most part
140
140
141
141
Args:
142
- account_line (account.analytic.invoice.line ): Recurring invoice
142
+ account_line (AccountAnalyticInvoiceLine ): Recurring invoice
143
143
line being referenced.
144
- invoice (account.invoice ): Invoice that is being created.
144
+ invoice (AccountInvoice ): Invoice that is being created.
145
145
Returns:
146
- ( float) Quantity to use on invoice line in the UOM defined on the
146
+ float: Quantity to use on invoice line in the UOM defined on the
147
147
``contract_line``.
148
148
"""
149
149
return account_line .quantity
@@ -152,14 +152,13 @@ def _get_quantity_threshold(self, account_line, invoice):
152
152
def _get_quantity_usage (self , account_line , invoice ):
153
153
""" It provides a quantity based on unbilled and used metrics
154
154
Args:
155
- account_line (account.analytic.invoice.line ): Recurring invoice
155
+ account_line (AccountAnalyticInvoiceLine ): Recurring invoice
156
156
line being referenced.
157
- invoice (account.invoice ): Invoice that is being created.
157
+ invoice (AccountInvoice ): Invoice that is being created.
158
158
Returns:
159
- ( float) Quantity to use on invoice line in the UOM defined on the
159
+ float: Quantity to use on invoice line in the UOM defined on the
160
160
``contract_line``.
161
161
"""
162
- usage = 0
163
162
vals = account_line .metric_interface_id .metric_value_ids
164
163
inv_date = fields .Datetime .from_string (invoice .date_invoice )
165
164
inv_delta = self .ref_contract_id .get_relative_delta (
@@ -174,7 +173,5 @@ def filter(rec):
174
173
start = fields .Datetime .from_string (rec .date_start )
175
174
end = fields .Datetime .from_string (rec .date_end )
176
175
return start >= start_date and end <= inv_date
177
- usage_values = vals .filtered (filter )
178
- for val in usage_values :
179
- usage += val .value
180
- return usage
176
+
177
+ return sum (vals .filtered (filter ).mapped ('value' )) or 0.0
0 commit comments