コード例 #1
0
    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)