def handle(self, sale_string): seller = self.msg.connection.contact sale_result = self.verify_sale(sale_string, seller) if sale_result[0] == 0: #sale failed self.respond("ERROR: " + ', '.join(sale_result[1]) + ". Sale format: sale serial# firstname lastname mobile# price regioncode description" ) else: sale_data = sale_result[1] product_code = sale_result[2] #verify that seller has the item to be sold in stock current_stock = Stock.get_existing(sale_data['seller'].alias, product_code) if current_stock is None or current_stock.stock_amount <= 0: self.respond("ERROR: No %s in stock." % product_code) return True #verify that the serial # is not a duplicate exists = Sale.by_serial(sale_data['serial']) if exists: self.respond("ERROR: %s is already registered." % sale_data['serial']) return True #now create and save the new sale record s = Sale(**sale_data) s.save() #subtract the sale from the retailer's stock current_stock.stock_amount -= 1 current_stock.save() payment_response = "Cash sale." self.respond("%s registered to %s %s by %s." % (s.serial, s.fname, s.lname, s.seller.alias, ) + " " + payment_response)
def handle(self): target = self.msg.connection.contact trans = list(StockTransaction.objects.filter(recipient=target, status=2)) if trans is None: self.respond("There were no transactions pending." % recip) return True for t in trans: stk_list = t.to_transfer.all() for stk in stk_list: existing = Stock.get_existing(target.alias, stk.product.code) if existing: existing.stock_amount += stk.stock_amount existing.save() stk.delete() else: stk.seller = target stk.save() t.status = 1 t.date_resolved = datetime.now() t.save() self.respond("Transfer from %s done, current stock %s." % (t.initiator.alias, self.get_current_stock(target))) #now confirm with sender t.initiator.message("Transfer confirmed by %s. Current stock %s." % (target.alias, self.get_current_stock(t.initiator)))
def handle(self, restock_string): user = self.msg.connection.contact #Check permissions. Unregistered users will receive an unhelpful error message. if not user.role: self.respond("You do not have permission to use this command.") return True restock_list = self.parse_restock_string(restock_string) errors = [] response = "" for code, amount in restock_list: #ignore incorrect codes if amount <= 0: #no matching product code errors.append(code) else: target_product = Product.by_code(code) current_stock = Stock.get_existing(user.alias, code) if current_stock is None: s = Stock(seller=user, product=target_product, stock_amount=amount) s.save() response += "%s %s, " % (amount, target_product.display_name) else: current_stock.stock_amount += amount current_stock.save() response += "%s %s, " % (amount, target_product.display_name) if response: response = response[:-2] + " added. " if errors: for err in errors: response += "%s " % err response += "not recognized. " self.respond("%sCurrent stock: %s" % (response, self.get_current_stock(user)))
def handle(self): target = self.msg.connection.contact trans = list(StockTransaction.objects.filter(recipient=target, status=2)) if trans is None: self.respond("There were no transactions pending." % recip) return True for t in trans: stk_list = t.to_transfer.all() for stk in stk_list: existing = Stock.get_existing(t.initiator.alias, stk.product.code) if existing: existing.stock_amount += stk.stock_amount existing.save() stk.delete() else: stk.seller = t.initiator stk.save() t.status = 0 t.date_resolved = datetime.now() t.save() self.respond("Transfer from %s rejected, current stock %s." % (t.initiator.alias, self.get_current_stock(target))) #now confirm with sender t.initiator.message("Transfer rejected by %s. Current stock %s." % (target.alias, self.get_current_stock(t.initiator)))
def clean_seller(self): cleaned_data = self.cleaned_data code = cleaned_data.get("serial")[0:2].upper() current_stock = Stock.get_existing(cleaned_data.get("seller").alias, code) prod = Product.objects.get(code=code) if current_stock is None or current_stock.stock_amount <= 0: raise forms.ValidationError("%s has no %s in stock" % (cleaned_data.get("seller").alias, prod.display_name)) return cleaned_data["seller"]
def clean_seller(self): cleaned_data = self.cleaned_data code = cleaned_data.get("serial")[0:2].upper() current_stock = Stock.get_existing( cleaned_data.get("seller").alias, code) prod = Product.objects.get(code=code) if current_stock is None or current_stock.stock_amount <= 0: raise forms.ValidationError( "%s has no %s in stock" % (cleaned_data.get("seller").alias, prod.display_name)) return cleaned_data["seller"]
def handle(self, sale_string): seller = self.msg.connection.contact sale_result = self.verify_sale(sale_string, seller) if sale_result[0] == 0: #sale failed self.respond( "ERROR: " + ', '.join(sale_result[1]) + ". Sale format: sale serial# firstname lastname mobile# price regioncode description" ) else: sale_data = sale_result[1] product_code = sale_result[2] #verify that seller has the item to be sold in stock current_stock = Stock.get_existing(sale_data['seller'].alias, product_code) if current_stock is None or current_stock.stock_amount <= 0: self.respond("ERROR: No %s in stock." % product_code) return True #verify that the serial # is not a duplicate exists = Sale.by_serial(sale_data['serial']) if exists: self.respond("ERROR: %s is already registered." % sale_data['serial']) return True #now create and save the new sale record s = Sale(**sale_data) s.save() #subtract the sale from the retailer's stock current_stock.stock_amount -= 1 current_stock.save() payment_response = "Cash sale." self.respond("%s registered to %s %s by %s." % ( s.serial, s.fname, s.lname, s.seller.alias, ) + " " + payment_response)
def cancel_restocks(self, stocker): pending_restocks = StockTransaction.objects.filter(initiator=stocker, status=2) if not pending_restocks: self.respond("No transactions were pending. current stock %s." % (self.get_current_stock(stocker))) return True for p in pending_restocks: stk_list = p.to_transfer.all() for stk in stk_list: existing = Stock.get_existing(stocker.alias, stk.product.code) if existing: existing.stock_amount += stk.stock_amount existing.save() stk.delete() else: stk.seller = stocker stk.save() p.status = 3 p.date_resolved = datetime.now() p.save() self.respond("All pending transfers canceled, current stock %s." % (self.get_current_stock(stocker))) return True
def handle(self, text): stocker = self.msg.connection.contact args = text.partition(" ") if args[0] == "cancel": self.cancel_restocks(stocker) return True target_alias = args[0].lower() restock_list = self.parse_restock_string(args[2].lower()) if not restock_list: self.help() return True # confirm that alias exists try: target = Contact.objects.get(alias=target_alias) except: self.respond("Sorry, user %s was not found. Please check your spelling and try again" % target_alias) return True errors = [] stockouts = [] pending = [] response = "" notification = "" # admin must create this user, for holding pending stock transactions nobody = Contact.by_alias("nobody") for code, amount in restock_list: if amount == 0: errors.append(code) if amount == -1 or code == "": self.respond( "Missing product code or amount. Restock messages cannot contain spaces.\nExample: restock dnombo 5ew" ) return True else: current_stock = Stock.get_existing(stocker.alias, code) # make sure the initiator of the transaction has enough stock if current_stock is None or current_stock.stock_amount < amount: stockouts.append(code) else: target_product = Product.by_code(code) s = Stock(seller=nobody, product=target_product, stock_amount=amount) s.save() pending.append(s) current_stock.stock_amount -= amount current_stock.save() if pending: # create the transaction object and add stock to be moved trans = StockTransaction(initiator=stocker, recipient=target, status=2, date_initiated=datetime.now()) trans.save() # need to generate a primary key before adding products for p in pending: trans.to_transfer.add(p) response += "%s %s " % (p.stock_amount, p.product.display_name) notification += "%s %s " % (p.stock_amount, p.product.display_name) trans.save() response += "sent to %s. " % target.alias if stockouts: for out in stockouts: response += "%s " % out response += "out of stock. " if errors: for err in errors: response += "%s " % err response += "not recognized." self.respond(response) if notification: notification += "being sent by %s. Reply 'yes' to accept, 'no' to reject." % stocker.alias target.message(notification)
def handle(self, text): stocker = self.msg.connection.contact args = text.partition(' ') if args[0] == 'cancel': self.cancel_restocks(stocker) return True target_alias = args[0].lower() restock_list = self.parse_restock_string(args[2].lower()) if not restock_list: self.help() return True # confirm that alias exists try: target = Contact.objects.get(alias=target_alias) except: self.respond( "Sorry, user %s was not found. Please check your spelling and try again" % target_alias) return True errors = [] stockouts = [] pending = [] response = "" notification = "" #admin must create this user, for holding pending stock transactions nobody = Contact.by_alias("nobody") for code, amount in restock_list: if amount == 0: errors.append(code) if amount == -1 or code == '': self.respond( "Missing product code or amount. Restock messages cannot contain spaces.\nExample: restock dnombo 5ew" ) return True else: current_stock = Stock.get_existing(stocker.alias, code) #make sure the initiator of the transaction has enough stock if current_stock is None or current_stock.stock_amount < amount: stockouts.append(code) else: target_product = Product.by_code(code) s = Stock(seller=nobody, product=target_product, stock_amount=amount) s.save() pending.append(s) current_stock.stock_amount -= amount current_stock.save() if pending: #create the transaction object and add stock to be moved trans = StockTransaction(initiator=stocker, recipient=target, status=2, date_initiated=datetime.now()) trans.save( ) #need to generate a primary key before adding products for p in pending: trans.to_transfer.add(p) response += "%s %s " % (p.stock_amount, p.product.display_name) notification += "%s %s " % (p.stock_amount, p.product.display_name) trans.save() response += "sent to %s. " % target.alias if stockouts: for out in stockouts: response += "%s " % out response += "out of stock. " if errors: for err in errors: response += "%s " % err response += "not recognized." self.respond(response) if notification: notification += "being sent by %s. Reply 'yes' to accept, 'no' to reject." % stocker.alias target.message(notification)