-
Notifications
You must be signed in to change notification settings - Fork 76
/
vendor_bills.py
136 lines (112 loc) · 6.09 KB
/
vendor_bills.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
import logging
from netsuitesdk.internal.utils import PaginatedSearch
from .base import ApiBase
from typing import List
from collections import OrderedDict
logger = logging.getLogger(__name__)
class VendorBills(ApiBase):
"""
VendorBills are not directly searchable - only via as transactions
"""
def __init__(self, ns_client):
ApiBase.__init__(self, ns_client=ns_client, type_name='vendorBill')
def get_all_generator(self):
record_type_search_field = self.ns_client.SearchStringField(searchValue='VendorBill', operator='contains')
basic_search = self.ns_client.basic_search_factory('Transaction', recordType=record_type_search_field)
paginated_search = PaginatedSearch(client=self.ns_client,
type_name='Transaction',
basic_search=basic_search,
pageSize=20)
return self._paginated_search_to_generator(paginated_search=paginated_search)
def post(self, data) -> OrderedDict:
assert data['externalId'], 'missing external id'
vb = self.ns_client.VendorBill(externalId=data['externalId'])
# If expesess are present, add them
if 'expenseList' in data and data['expenseList']:
expense_list = []
for eod in data['expenseList']:
if 'customFieldList' in eod and eod['customFieldList']:
custom_fields = []
for field in eod['customFieldList']:
if field['type'] == 'String':
custom_fields.append(
self.ns_client.StringCustomFieldRef(
scriptId=field['scriptId'] if 'scriptId' in field else None,
internalId=field['internalId'] if 'internalId' in field else None,
value=field['value']
)
)
elif field['type'] == 'Select':
custom_fields.append(
self.ns_client.SelectCustomFieldRef(
scriptId=field['scriptId'] if 'scriptId' in field else None,
internalId=field['internalId'] if 'internalId' in field else None,
value=self.ns_client.ListOrRecordRef(
internalId=field['value']
)
)
)
eod['customFieldList'] = self.ns_client.CustomFieldList(custom_fields)
vbe = self.ns_client.VendorBillExpense(**eod)
expense_list.append(vbe)
vb['expenseList'] = self.ns_client.VendorBillExpenseList(expense=expense_list)
# If items are present, add them
if 'itemList' in data and data['itemList']:
item_list = []
for eod in data['itemList']:
if 'customFieldList' in eod and eod['customFieldList']:
custom_fields = []
for field in eod['customFieldList']:
if field['type'] == 'String':
custom_fields.append(
self.ns_client.StringCustomFieldRef(
scriptId=field['scriptId'] if 'scriptId' in field else None,
internalId=field['internalId'] if 'internalId' in field else None,
value=field['value']
)
)
elif field['type'] == 'Select':
custom_fields.append(
self.ns_client.SelectCustomFieldRef(
scriptId=field['scriptId'] if 'scriptId' in field else None,
internalId=field['internalId'] if 'internalId' in field else None,
value=self.ns_client.ListOrRecordRef(
internalId=field['value']
)
)
)
eod['customFieldList'] = self.ns_client.CustomFieldList(custom_fields)
vbe = self.ns_client.VendorBillItem(**eod)
item_list.append(vbe)
vb['itemList']= self.ns_client.VendorBillItemList(item=item_list)
if 'currency' in data:
vb['currency'] = self.ns_client.RecordRef(**(data['currency']))
if 'memo' in data:
vb['memo'] = data['memo']
if 'tranDate' in data:
vb['tranDate'] = data['tranDate']
if 'tranId' in data:
vb['tranId'] = data['tranId']
if 'class' in data:
vb['class'] = self.ns_client.RecordRef(**(data['class']))
if 'location' in data:
vb['location'] = self.ns_client.RecordRef(**(data['location']))
if 'department' in data:
vb['department'] = self.ns_client.RecordRef(**(data['department']))
if 'account' in data:
vb['account'] = self.ns_client.RecordRef(**(data['account']))
if 'customFieldList' in data:
vb['customFieldList'] = data['customFieldList']
if 'entity' in data:
vb['entity'] = self.ns_client.RecordRef(**(data['entity']))
if 'taxDetailsOverride' in data:
vb['taxDetailsOverride'] = data['taxDetailsOverride']
if 'taxDetailsList' in data and data['taxDetailsList']:
tax_details_list = []
if 'taxDetails' in data['taxDetailsList']:
for tdl in data['taxDetailsList']['taxDetails']:
tax_details_list.append(self.ns_client.TaxDetails(**tdl))
vb['taxDetailsList'] = self.ns_client.TaxDetailsList(taxDetails=tax_details_list)
logger.debug('able to create vb = %s', vb)
res = self.ns_client.upsert(vb, 'bills')
return self._serialize(res)