Example #1
0
    def AddProduct(cls, productPK, quantity):
        """
        This adds to the self.pendingProducts list with Product objects that
        have been built using data from the database.
        @param productPK    The primary key of the product that is to be add.
        @param quantity     The amount of products that is being added.
        
        @return Returns the Product object that was just created or added to.
        """
        cls.cursor.execute("SELECT * FROM Product_T WHERE ProductID='%s'" %
                           (productPK))

        product = cls.GetProduct(productPK)
        messages = []

        if (product == None):
            product = Product(quantity)

            columnNames = []

            for column in cls.cursor.description:
                columnNames.append(column[0])

            #Add those columns to the Product object!
            if (not product.AddColumns(columnNames, list(cls.cursor)[0])):
                print("AddColumns failed within Controller.AddProduct!")

            #Query vendors and materials and add that info to the product.
            cls.cursor.execute("SELECT * FROM Uses_T WHERE ProductID='%s'" %
                               (productPK))

            materialsNeeded = list(cls.cursor)

            #Sometimes there are products that don't even have listed materials. So we'll
            #   just skip over those ones.
            if (materialsNeeded != []):
                for material in materialsNeeded:
                    cls.cursor.execute(
                        "SELECT * FROM RawMaterial_T WHERE MaterialID='%s'" %
                        (material[0]))
                    materialResults = list(cls.cursor)

                    if (materialResults != []):
                        #There should be a single tuple within this list.
                        materialResults = materialResults[0]

                        #Now we query the vendorID and his/her price for this material.
                        cls.cursor.execute(
                            "SELECT * FROM Supplies_T WHERE MaterialID='%s'" %
                            (material[0]))

                        suppliesResults = list(cls.cursor)

                        if (suppliesResults != []):
                            #There should be a single tuple within this list.
                            suppliesResults = suppliesResults[0]

                            #Now we query the name of the vendor
                            cls.cursor.execute(
                                "SELECT * FROM Vendor_T WHERE VendorID=%d" %
                                (int(suppliesResults[0])))

                            vendorResults = list(cls.cursor)

                            if (vendorResults != []):
                                #There should be a single tuple within this list.
                                vendorResults = vendorResults[0]

                                product.AddMaterial(materialResults[0],
                                                    materialResults[1],
                                                    material[2],
                                                    vendorResults[1],
                                                    float(suppliesResults[2]))
                            else:
                                #The standard price for the material in the Product_T is used for the UnitCost if a Vendor isn't found.
                                product.AddMaterial(materialResults[0],
                                                    materialResults[1],
                                                    material[2], "Unknown",
                                                    float(materialResults[6]))
                                messages.append(
                                    "VendorID, %d, doesn't exist within the Vendor_T table!"
                                    % (int(suppliesResults[0])))

                        else:
                            #The standard price for the material in the Product_T is used for the UnitCost if a Vendor isn't found.
                            product.AddMaterial(materialResults[0],
                                                materialResults[1],
                                                material[2], "Unknown",
                                                float(materialResults[6]))
                            messages.append(
                                "MaterialID, %s, doesn't exist within the Supplies_T table!"
                                % (material[0]))

                    else:
                        messages.append(
                            "MaterialID, %s, doesn't exist within the RawMaterial_T table!"
                            % (material[0]))

                if (product.materials != []):
                    cls.pendingProducts.append(product)
                else:
                    messages.append(
                        "There were no materials found for product: %s-%s!" %
                        (product.columnInfo["ProductDescription"],
                         product.columnInfo["ProductFinish"]))
                    product = None
            else:
                messages.append(
                    "There were no materials found for product: %s-%s!" %
                    (product.columnInfo["ProductDescription"],
                     product.columnInfo["ProductFinish"]))
                product = None
        else:
            product.quantity += quantity

        return product, messages