def send_order_to_WW(orderID): local = MongoTools.LocalDB() order = local.get_delivery_by_walden_id(orderID) ordersArray = [WorkWave.build_order_from_document(order)] response = WorkWave.add_orders(ordersArray) print response print response.text local.disconnect() return response
def set_order_date_to_WW(orderID, set_date): local = MongoTools.LocalDB() local_order = local.get_delivery_by_walden_id(orderID) order = WorkWave.build_order_from_document(local_order, startDate=set_date, lockDate=True) # Send to WW response = WorkWave.replace_order(order, local_order['workwave_ID']) print response.text local.disconnect() return response
def update_local_from_WW(): # as of now this doesn't remove ones that have been flag as no longer delivering # even though they have been deleted from WW print "Getting orders from WW...", # Get order list from WW response = ww.get_orders() print "Done" # Check Error Codes if (response.status_code != 200): return False # Get actual order data orders = response.json()['orders'] # Get Local DB local = mTools.LocalDB() print "Cycling through orders...", for order in orders: ww_order = orders[order] if 'walden_ID' in ww_order['delivery']['customFields']: walden_ID = ww_order['delivery']['customFields']['walden_ID'] local.set_ww_ID(walden_ID, ww_order['id']) print "Done" local.disconnect() return True
def send_all_orders_to_WW(): # this adds all active deliveries to WW # Make sure to clear WW before doing this local = MongoTools.LocalDB() print "Building deliveries array" deliveries = local.get_all_active_deliveries() order_array = [] for order in deliveries: order_array.append(WorkWave.build_order_from_document(order)) # Calulate time to add and send time = len(order_array)/60.0 # len of time to add in minutes print "Sending to WorkWave - this should take " + str(time) + " minutes" response = WorkWave.add_orders(order_array) print response print response.text local.disconnect()
def update_local_mid_month(): # First update from ww flag = update_local_from_WW() if not flag: print "Update from WW not successfull" return False # Get WaldenDB print "Updating from Walden...", update_local_from_walden() print "Done" local = mTools.LocalDB() # Build add/drop arrays drop_IDs = [] add_array = [] all_orders = local.get_all() for order in all_orders: if order['status'] == "active" or order['status'] == "manual": if 'workwave_ID' not in order: add_array.append( ww.build_order_from_document(order) ) else: if 'workwave_ID' in order: drop_IDs.append( order['workwave_ID'] ) local.remove_ww_id( order['walden_ID'] ) local.disconnect() print "num to drop: " + str(len(drop_IDs)) print "num to add: " + str(len(add_array)) # Send to work wave response = ww.add_orders(add_array) print "added: " + str(response.status_code) response = ww.drop_orders(drop_IDs) print "deleted: " + str(response.status_code) + " " + response.text
def update_local_mid_month(): # First update from ww flag = update_local_from_WW() if not flag: print "Update from WW not successfull" return False # Get WaldenDB print "Updating from Walden...", update_local_from_walden() print "Done" local = mTools.LocalDB() # Build add/drop arrays drop_IDs = [] add_array = [] all_orders = local.get_all() for order in all_orders: if order['status'] == "active" or order['status'] == "manual": if 'workwave_ID' not in order: add_array.append(ww.build_order_from_document(order)) else: if 'workwave_ID' in order: drop_IDs.append(order['workwave_ID']) local.remove_ww_id(order['walden_ID']) local.disconnect() print "num to drop: " + str(len(drop_IDs)) print "num to add: " + str(len(add_array)) # Send to work wave response = ww.add_orders(add_array) print "added: " + str(response.status_code) response = ww.drop_orders(drop_IDs) print "deleted: " + str(response.status_code) + " " + response.text
def init_local_from_WW(): print "Getting Orders from WW...", # Get order list from WW response = ww.get_orders() print "Done" # Check Error Codes if (response.status_code != 200): return False # Get actual order data orders = response.json()['orders'] # Get and clear Local DB local = mTools.LocalDB() local.delete_all_local_documents() print "Cycling through orders...", for order in orders: local.add_from_WW_record(orders[order]) print "Done" local.disconnect() return True
def build_route_csv(day): print "Getting routes" # get routes from WW routes_response = WorkWave.get_routes_on_day(day) if routes_response.status_code != 200: print routes_response.text return None routes = routes_response.json()['routes'] print "Getting DBs" # Get Databases local = MongoTools.LocalDB() walden = MongoTools.WaldenDB() deliveries = {} for route in routes: this_route = routes[route] # build section steps = this_route['steps'] lines = [] for step in steps: line = {} if "orderId" in step: delivery = local.get_delivery_by_WW_id(step['orderId']) walden_order = walden.find_from_ID(delivery['walden_ID']) line['walden_ID'] = delivery['walden_ID'] line['delivery_time'] = get_time_from_sec(step['arrivalSec']) line['name'] = delivery['name_first'] + " " + delivery['name_last'] line['address_1'] = delivery['address_1'] if 'address_2' in delivery: if delivery['address_2'] != None: line['address_2'] = delivery['address_2'] else: line['address_2'] = "" else: line['address_2'] = "" line['city'] = delivery['city'] line['zip_code'] = delivery['zip_code'] line['phone'] = walden_order['phone'] note = "" if 'location_type' in delivery: note += delivery['location_type'] note += ' - ' if 'notes' in walden_order: if walden_order['notes'] != None: note += walden_order['notes'] line['note'] = note lines.append(line) deliveries[WorkWave.get_truck_name(this_route['vehicleId'])] = lines local.disconnect() walden.disconnect() #Convert to csv print "Converting to csv..." csv_array = [] for truck in deliveries: csv_array.append("\n\n\n" +truck + "\n") route = deliveries[truck] for stop in route: line = "" + ',' line += "" + ',' line += stop['delivery_time'] + "," line += stop['name'] + "," line += "" + ',' line += "" + ',' line += "" + ',' line += "" + ',' line += "" + ',' line += "" + ',' line += stop['address_1'] if stop['address_2'] != None: line += " - " + stop['address_2'] + "," else: line += " - " + "," line += stop['city'] + "," line += stop['zip_code'] + "," line += stop['phone'] + "," line += "" + ',' line += "" + ',' line += "" + ',' line += "" + ',' line += stop['walden_ID'] + "," csv_array.append(line + "\n") return csv_array