def print_orders(self): #Query orders from the data base printable_orderno = dbman.get_printable_orderno(self.databasePath) for item in printable_orderno: #These items are printable order, but still there is a need to check if we are within time frame for print now = datetime.datetime.now() #Get customer information customer_info = dbman.get_customer(self.databasePath, item[0]) #Get items that was ordered by customer order_items = dbman.get_orderItems(self.databasePath, item[0]) if item[8] == 'now': if item[3] == 'delivery': #Send print to packer #Instantiate the printer first printer = Printer(self.printerParam['printerNear']) printer.printOrder_packer(item, customer_info, order_items, self.print_translation) printer = Printer(self.printerParam['printerNear']) printer.printDriver(customer_info, item) printer = Printer(self.printerParam['printerFar']) printer.printOrder_kitchen(item, customer_info, order_items, self.print_translation) #set print status to yes in the data base dbman.setPrintedStatus(self.databasePath, item[0], 'yes') if item[3] == 'pickup': printer = Printer(self.printerParam['printerNear']) printer.printOrder_packer(item, customer_info, order_items, self.print_translation) printer = Printer(self.printerParam['printerFar']) printer.printOrder_kitchen(item, customer_info, order_items, self.print_translation) #set print status to yes in the data base dbman.setPrintedStatus(self.databasePath, item[0], 'yes') if item[3] == 'delivery': execution_time = self.convert_delivery_datetime( item[4], item[5], 'start_time') execution_time = execution_time - datetime.timedelta( minutes=60) if now >= execution_time: #Send print to packer #Instantiate the printer first printer = Printer(self.printerParam['printerNear']) printer.printOrder_packer(item, customer_info, order_items, self.print_translation) printer = Printer(self.printerParam['printerNear']) printer.printDriver(customer_info, item) printer = Printer(self.printerParam['printerFar']) printer.printOrder_kitchen(item, customer_info, order_items, self.print_translation) #set print status to yes in the data base dbman.setPrintedStatus(self.databasePath, item[0], 'yes') if item[3] == 'pickup': execution_time = self.convert_pickup_datetime(item[4], item[5]) execution_time = execution_time - datetime.timedelta( minutes=60) if now >= execution_time: printer = Printer(self.printerParam['printerNear']) printer.printOrder_packer(item, customer_info, order_items, self.print_translation) printer = Printer(self.printerParam['printerFar']) printer.printOrder_kitchen(item, customer_info, order_items, self.print_translation) #set print status to yes in the data base dbman.setPrintedStatus(self.databasePath, item[0], 'yes')
def sms_delayWarn(self, orders): #Get the new incoming orders for item in orders: #Start by extracting orderno orderno = item['name'].split('#')[1] #Query the data base to obtain customer name, created_at, and delivery wished #Connect to data base and extract customer info customer = dbman.get_customer(self.databasePath, orderno) #Get customer order execution data order_execution = dbman.get_order_execution( self.databasePath, orderno) mobile_phone = customer[3] if order_execution[3] == 'delivery' and order_execution[ 7] == 'no': #no delay warning sent yet #Delay date: #Convert to datetime created_at = self.created_at_to_datetime(order_execution[6]) start_delivery_time = self.convert_delivery_datetime( order_execution[4], order_execution[5], 'start_time') end_delivery_time = self.convert_delivery_datetime( order_execution[4], order_execution[5], 'end_time') #set the expected time with delay taken into account expected_delivery = end_delivery_time + datetime.timedelta( minutes=30) expected_delivery = expected_delivery.strftime('%H:%M') #Evaluate how long in advance the order was created created_in_advance = ( start_delivery_time - created_at).total_seconds( ) / 60 #minutes customer created the order in advance if created_in_advance <= 0: #Greedy delivery time, where created at time is in the delivery time frame #no hesistation. Send delay sms expected_delivery = end_delivery_time + datetime.timedelta( minutes=30) expected_delivery.strftime('%Y-%m-%d %H:%M%S') smstext = f'''Dear {customer[1]}, Thanks for your order. We are currently very busy, and expect to deliver your order to you at around {expected_delivery}. Thanks for your understanding. This sms cannot be answered. For direct contact you can reach us at 33 12 88 28 Hidden Dimsum with Thanks!''' #send first dummy sms to myself to assure the sanity of the sms self.send_sms( smstext, '+4560732838' ) #change to mobile_phone!!!!!!!!!!!!!!!!!!!!!!!!!!!!! self.send_sms(smstext, mobile_phone) dbman.delay_warn(self.databasePath, orderno, 'yes') if created_in_advance > 0 and created_in_advance < 30: #Check how many other orders we have in the requested delivery time tmp_timedelta = (end_delivery_time - start_delivery_time) / 2 midpoint = start_delivery_time + tmp_timedelta delay_date = order_execution[4] deliver_data, pickup_data = dbman.count_orders( self.databasePath, delay_date) #From the received order, count order amount in the same delivery time frame counter = 0 for item in deliver_data: #For each delivery data, check if our midpoint is within the time-span existing_order_delivery_start = self.convert_delivery_datetime( item[4], item[5], 'start_time') existing_order_delivery_end = self.convert_delivery_datetime( item[4], item[5], 'end_time') if midpoint >= existing_order_delivery_start and midpoint <= existing_order_delivery_end: counter += 1 for item in pickup_data: #For each pickup data, check if our midpoint is within the time-span existing_order_pickup_time = self.convert_pickup_datetime( item[4], item[5]) #If midpoint is larger than 30 min earlier than existing pickup time and smaller than existing pickup time #counter is increased by 1 if midpoint >= existing_order_pickup_time - datetime.timedelta( minutes=30 ) and midpoint <= existing_order_pickup_time: counter += 1 if counter > 4: expected_delivery = end_delivery_time + datetime.timedelta( minutes=30) expected_delivery.strftime('%Y-%m-%d %H:%M%S') smstext = f'''Dear {customer[1]}, Thanks for your order. We are currently very busy, and expect to deliver your order to you at around {expected_delivery}. Thanks for your understanding. This sms cannot be answered. For direct contact you can reach us at 33 12 88 28 Hidden Dimsum with Thanks!''' self.send_sms(smstext, mobile_phone) self.send_sms( smstext, '+4560732838' ) #Change to mobile_phone!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! dbman.delay_warn(self.databasePath, orderno, 'yes') if order_execution[3] == 'pickup' and order_execution[ 7] == 'no': #no delay warning sent yet #Delay date: #Convert to datetime created_at = self.created_at_to_datetime(order_execution[6]) pickup_time = self.convert_pickup_datetime( order_execution[4], order_execution[5]) #set the expected time with delay taken into account expected_pickup_time = pickup_time + datetime.timedelta( minutes=30) expected_pickup_time = expected_pickup_time.strftime('%H:%M') #Evaluate how long in advance the order was created created_in_advance = (pickup_time - created_at).total_seconds( ) / 60 #minutes customer created the order in advance if created_in_advance <= 0: #Greedy pickup time, where created pickup time is after pickup. This logic should not be possible due to restrictions in the webpage #no hesistation. Send delay sms smstext = f'''Dear {customer[1]}, Thanks for your order. We are currently very busy, and as a consequence there can be a delay in your pickup. Your pickup will be ready at {expected_pickup_time}. We will notify you, should it be ready for pickup earlier. Thanks for your understanding. This sms cannot be answered. For direct contact you can reach us at 33 12 88 28 Hidden Dimsum with Thanks!''' #send first dummy sms to myself to assure the sanity of the sms self.send_sms( smstext, '+4560732838' ) #change to mobile_phone!!!!!!!!!!!!!!!!!!!!!!!!!!!!! self.send_sms(smstext, mobile_phone) dbman.delay_warn(self.databasePath, orderno, 'yes') if created_in_advance > 0 and created_in_advance < 30: #Check how many other orders we have in the requested time range delay_date = order_execution[4] deliver_data, pickup_data = dbman.count_orders( self.databasePath, delay_date) #From the received order, count order amount in the same delivery time frame counter = 0 for item in deliver_data: #For each delivery data, check if our midpoint is within the time-span existing_order_delivery_start = self.convert_delivery_datetime( item[4], item[5], 'start_time') existing_order_delivery_end = self.convert_delivery_datetime( item[4], item[5], 'end_time') if pickup_time >= existing_order_delivery_start and pickup_time <= existing_order_delivery_end: counter += 1 for item in pickup_data: #For each pickup data, check if our midpoint is within the time-span existing_order_pickup_time = self.convert_pickup_datetime( item[4], item[5]) #If midpoint is larger than 30 min earlier than existing pickup time and smaller than existing pickup time #counter is increased by 1 if pickup_time >= existing_order_pickup_time - datetime.timedelta( minutes=30 ) and pickup_time <= existing_order_pickup_time: counter += 1 if counter > 4: smstext = f'''Dear {customer[1]}, Thanks for your order. We are currently very busy, and as a consequence there can be a delay in your pickup. Your pickup will be ready at {expected_pickup_time}. We will notify you, should it be ready for pickup earlier. Thanks for your understanding. This sms cannot be answered. For direct contact you can reach us at 33 12 88 28 Hidden Dimsum with Thanks!''' self.send_sms(smstext, mobile_phone) self.send_sms( smstext, '+4560732838' ) #Change to mobile_phone!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! dbman.delay_warn(self.databasePath, orderno, 'yes')
def __init__(self, dbpath, printerParam): #Get translation dict with open('Shopify_item2print.txt') as f: translation = json.load(f) #Connect to the data base conn = sqlite3.Connection(dbpath) c = conn.cursor() #check from order_execution table if any orders are within print date and time mystr = '''SELECT ORDERNO, ORDER_TYPE, DATE, TIME, PRINT_STATUS FROM order_execution WHERE PRINT_STATUS = 'no' ''' c.execute(mystr) data = c.fetchall() #Also get data from which sqldata base was manually set to print now mystr = '''SELECT ORDERNO, ORDER_TYPE, DATE, TIME, PRINT_STATUS FROM order_execution WHERE PRINT_STATUS = 'print now' ''' c.execute(mystr) data2 = c.fetchall() conn.close() #if there is something we need to print now if data2: for item in data2: printdict = self.add_executionInfo_to_Printdict(item) #Get items in the order conn = sqlite3.Connection(dbpath) c = conn.cursor() mystr = f'''SELECT ITEM, AMOUNT, UNIT_PRICE, ORDERNO FROM items WHERE ORDERNO = {printdict['orderno']} ''' c.execute(mystr) data2 = c.fetchall() printdict['items'] = data2 #Get customer information customer = dbman.get_customer(dbpath, printdict['orderno']) printdict = self.add_customerInfo_to_printdict(customer, printdict) #connect to printer and print twice one to kitchen and one to host p = printer(printerParam['printerNear']) p.printDelivery(printdict, translation) p = printer(printerParam['printerFar']) p.printDelivery(printdict, translation) #After printing, the printed status in data base will be switched to 'yes' dbman.setPrintedStatus(dbpath, printdict['orderno'], 'yes') if not data: print('no printable orders') return for item in data: #check first if date is today dateformat = '%d/%m/%Y' orderDate = datetime.datetime.strptime(item[2], dateformat) todayDate = datetime.datetime.today().date() if orderDate.date() == todayDate: #within date scope for printing. Check if within time scope as well #execution time tmp = item[3].split('-') startstr = str(todayDate) + ' ' + tmp[0].strip() timeformat = '%Y-%m-%d %I:%M %p' start = datetime.datetime.strptime(startstr, timeformat) executionStart = start - datetime.timedelta(minutes=60) now = datetime.datetime.now() if now >= executionStart: printdict = self.add_executionInfo_to_Printdict(item) #Get items in the order conn = sqlite3.Connection(dbpath) c = conn.cursor() mystr = f'''SELECT ITEM, AMOUNT, UNIT_PRICE, ORDERNO FROM items WHERE ORDERNO = {printdict['orderno']} ''' c.execute(mystr) data2 = c.fetchall() printdict['items'] = data2 #Get customer information customer = dbman.get_customer(dbpath, printdict['orderno']) printdict = self.add_customerInfo_to_printdict(customer, printdict) #connect to printer and print twice one to kitchen and one to host p = printer(printerParam['printerNear']) p.printDelivery(printdict, translation) p = printer(printerParam['printerFar']) p.printDelivery(printdict, translation) #After printing, the printed status in data base will be switched to 'yes' dbman.setPrintedStatus(dbpath, printdict['orderno'], 'yes')