Created
May 31, 2013 02:11
-
-
Save aliomattux/5682577 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from connection import NetSuiteConnect | |
from SuiteREST import SuiteRequest | |
from forms import send_error | |
class ExportFulfillments(): | |
def get_picking_ids(self): | |
""" Retrieve a list of Picking IDS """ | |
instance = NetSuiteConnect() | |
conn = instance.erp_connection() | |
ids_query = "SELECT id FROM stock_picking" \ | |
" WHERE pending_netsuite = True" \ | |
" AND type='out'" \ | |
" AND state != 'cancel'" | |
id_data = conn.execute(ids_query) | |
picking_ids = [id[0] for id in id_data] | |
return picking_ids | |
def prepare_picking_lines(self, picking_id): | |
""" Prepare a list of dictionaries containing the picking lines """ | |
instance = NetSuiteConnect() | |
conn = instance.erp_connection() | |
order_data = {} | |
picking_lines = [] | |
lines_query = "SELECT A.product_qty, B.netsuite_id, C.origin" \ | |
" FROM stock_move A" \ | |
" JOIN product_product B ON (A.product_id = B.id)" \ | |
" JOIN stock_picking C ON (A.picking_id = C.id)" \ | |
" WHERE B.is_drop_ship IS NOT TRUE" \ | |
" AND A.picking_id = %i" % int(picking_id) | |
lines_data = conn.execute(lines_query) | |
for line in lines_data: | |
#Normally we would raise error here, but we catch this later | |
#so we can send an email | |
ns_id = line[1] or '0' | |
order_data['origin'] = line[2] or 'No Order found' | |
picking_lines.append({'adjustqtyby': int(line[0]) * -1, | |
'item': ns_id, | |
'location': 1, | |
'memo': line[2]}) | |
order_data['picking_lines'] = picking_lines | |
return order_data | |
def prepare_pickings(self): | |
""" Prepare pickings for export Main""" | |
picking_ids = self.get_picking_ids() | |
if picking_ids: | |
for picking_id in picking_ids: | |
order_data = self.prepare_picking_lines(picking_id) | |
if order_data['picking_lines'] == []: | |
continue | |
self.prepare_inventory_adjustment(order_data) | |
return True | |
def prepare_inventory_adjustment(self, order_data): | |
""" Prepare the call to NetSuite """ | |
instance = NetSuiteConnect() | |
conn = instance.erp_connection() | |
print 'Processing Origin', order_data['origin'] | |
vals = { | |
'account': 124, | |
#Harcoded to Roseville | |
'adjlocation': 1, | |
'class': 380, | |
'sublist_fields': { | |
'inventory': order_data['picking_lines']}, | |
'memo': order_data['origin'], | |
'recordtype': 'inventoryadjustment'} | |
recordtype = 'inventoryadjustment' | |
internalid = self.upsert_vals(vals, recordtype) | |
print 'Response', internalid | |
if internalid: | |
conn.execute("UPDATE stock_picking SET pending_netsuite = False WHERE origin = '%s'" % str(order_data['origin'])) | |
return True | |
else: | |
conn.execute("UPDATE stock_picking SET pending_netsuite = False, netsuite_error = True WHERE origin = '%s'" % str(order_data['origin'])) | |
self.prepare_and_send_error(order_data) | |
return True | |
def prepare_and_send_error(self, order_data): | |
lines = [] | |
for line in order_data['picking_lines']: | |
lines.append(('OpenERP Item', line['item'], line['adjustqtyby'])) | |
parser = {} | |
parser['lines'] = lines | |
parser['picking'] = order_data['origin'] | |
parser['error_code'] = 'Generic Error' | |
send_error(parser) | |
return True | |
def upsert_vals(self, vals, recordtype): | |
""" UPSERT Update or Create. Creates the record in NetSuite """ | |
conn = SuiteRequest('1', '2', '3') | |
data = [] | |
data.append(vals) | |
param_data = { | |
'record_type': recordtype, | |
'record_data': data, | |
'do_sourcing': 'T', | |
'ignore_mandatory': 'T' | |
} | |
response = conn.upsert(param_data) | |
if response[0] == True: | |
try: | |
internalid = response[1][0][0] | |
if 'id' in vals.keys(): | |
print 'Successfully Updated Record in NetSuite with ID: %s' % str(vals['id']) | |
else: | |
print 'Successfully Created Record in NetSuite with ID: %s' % str(internalid) | |
return internalid | |
except Exception, e: | |
print e | |
return False | |
else: | |
print response | |
return False | |
if __name__ == '__main__': | |
try: | |
ex = ExportFulfillments() | |
pickings = ex.prepare_pickings() | |
print pickings | |
except Exception, e: | |
print e |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment