diff --git a/source_ecommerce_make_order.py b/source_ecommerce_make_order.py index 4db3bfb..aa336bd 100644 --- a/source_ecommerce_make_order.py +++ b/source_ecommerce_make_order.py @@ -18,6 +18,7 @@ import re import shutil import typing import paramiko +import decimal import records # type: ignore @@ -43,9 +44,9 @@ def main(): continue else: process_files(file) - shutil.move(file, SHIPMENTS_DIRECTORY / file.name) + #shutil.move(file, SHIPMENTS_DIRECTORY / file.name) # archives are in the shipping folder - combine_zshpords() + #combine_zshpords() def sftp_server() -> paramiko.SFTPClient: with paramiko.SSHClient() as ssh_client: @@ -119,7 +120,16 @@ def process_files(file): #I am assuming I am getting a sorted csv file by order sales_order.output(import_file) sales_order = SalesOrder() previous_order = current_order - shopify_order_info = get_details_from_shopify(current_order) + shopify_order_info = get_details_from_shopify(current_order) + shopify_line_dict = create_shopify_dict(shopify_order_info) + for entry in shopify_line_dict: + sales_order.append( + SalesOrderDetail( + itmref=shopify_line_dict[entry]['sku'], + qty=int(shopify_line_dict[entry]['quantity']), + gropri=shopify_line_dict[entry]['price'] + ) + ) ship_site = row[0] order_id = row[6] order_date = row[9] @@ -131,7 +141,6 @@ def process_files(file): #I am assuming I am getting a sorted csv file by order # shipzip = row[13] tracking = row[16] weight = row[18] - #pprint.pprint(order_id) taxes = shopify_order_info[0]['current_total_tax']#row[22] ship_charge = shopify_order_info[0]['shipping_lines__price']#row[21] discount = shopify_order_info[0]['current_total_discounts']#row[24] @@ -145,18 +154,13 @@ def process_files(file): #I am assuming I am getting a sorted csv file by order sales_order.header.invdtaamt_8 = taxes #gather line data - line_product = row[1] - line_qty = row[3] - line_lot = row[4] - line_price = row[20] - shopify_item_data = get_item_from_shopify_order(shopify_order_info, line_product, line_price) - sales_order.append( - SalesOrderDetail( - itmref=shopify_item_data['sku'],#line_product, - qty=int(shopify_item_data['quantity']),#int(line_qty), - gropri=shopify_item_data['price']#line_price - ) - ) + # line_product = row[1] + # line_qty = int(row[3]) + # line_lot = row[4] + # line_price = row[20] + # shopify_item_data = get_item_from_shopify_order(shopify_line_dict, line_product, line_qty) + + # shopify_line_dict = remove_item_from_shopify_order(shopify_line_dict, shopify_item_data['sku'], shopify_item_data['quantity'],shopify_item_data['price']) time_stamp = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") with yamamotoyama.x3_imports.open_import_file( SOH_IMPORT_DIRECTORY / f"ZSHPORD_{current_order}_{time_stamp}_{sales_order.header.cusordref}.dat" @@ -164,10 +168,44 @@ def process_files(file): #I am assuming I am getting a sorted csv file by order sales_order.output(import_file) -def get_item_from_shopify_order(line_item_list, product, gropri): - for record in line_item_list: - if record['sku'] == product and str(record['price']).rstrip('0') == gropri: - return record +def create_shopify_dict(shopify_record): + order_info = {} + for record in shopify_record: + sku = record['sku'] + qty = record['quantity'] + price = record['price'] + key = sku + '_' + str(price) + if key in order_info: + order_info[key]['quantity'] += qty + else: + order_info[key] = { + 'sku':record['sku'], + 'quantity':record['quantity'], + 'price':record['price'], + } + return order_info + + +def remove_item_from_shopify_order(line_item_dict, product, qty, price): + #after using some or all of a line, decrement or remove it + for line in line_item_dict: + line_info = line_item_dict[line] + qty_to_remove = line_info['quantity'] + if line_info['sku'] == product and line_info['quantity'] > 0 and line_info['price'] == price: + if qty_to_remove > line_info['quantity']: #more qty is required than on the line + pprint.pprint('too much to remove from 1 line') + remainder = qty_to_remove - line_info['quantity'] + line_item_dict = remove_item_from_shopify_order(line_item_dict, product, remainder, price) + else: + line_info['quantity'] -= qty_to_remove + return line_item_dict + + +def get_item_from_shopify_order(line_item_dict, product, qty): + for line in line_item_dict: + line_info = line_item_dict[line] + if line_info == product and line_info['quantity'] > 0: + return line_info return None diff --git a/source_ecommerce_make_shipment.py b/source_ecommerce_make_shipment.py index 5065e52..f362565 100644 --- a/source_ecommerce_make_shipment.py +++ b/source_ecommerce_make_shipment.py @@ -240,11 +240,9 @@ class WarehouseShipmentDetail: where [SOP].[SOHNUM_0] = :sohnum and [SOP].[ITMREF_0] = :itmref - and [SOP].[GROPRI_0] = :gropri """, sohnum=self.sohnum, itmref=self.itmref, - gropri=self.gropri, ) .first() .how_many @@ -260,13 +258,11 @@ class WarehouseShipmentDetail: where [SOP].[SOHNUM_0] = :sohnum and [SOP].[ITMREF_0] = :itmref - and [SOP].[GROPRI_0] = :gropri order by [SOP].[SOPLIN_0] """, sohnum=self.sohnum, itmref=self.itmref, - gropri=self.gropri, ).first() else: pprint.pprint(self.sohnum)