def deleteSupplier(self, supplierid): dao = SuppliersDAO() if not dao.getSupplierById(supplierid): return jsonify(Error="Supplier not found."), 404 else: dao.delete(supplierid) return jsonify(DeleteStatus="OK"), 200
def updateSupplierJson(self, supplierid, json): dao = SuppliersDAO() if not dao.getSupplierById(supplierid): return jsonify(Error="Admin not found."), 404 else: username = json['UserName'] password = json['Password'] email = json['Email'] slocation = json['SLocation'] affiliation = json['affiliation'] firstname = json['FirstName'] lastname = json['LastName'] dateofbirth = json['DateofBirth'] gender = json['Gender'] categoryid = json['CategoryID'] categoryname = json['CategoryName'] if username and password and email and slocation and affiliation and firstname and lastname and dateofbirth and gender and categoryid and categoryname: dao.update(supplierid, username, password, email, slocation, affiliation, firstname, lastname, dateofbirth, gender, categoryid, categoryname) result = self.build_suppliers_attributes( supplierid, username, password, email, slocation, affiliation, firstname, lastname, dateofbirth, gender, categoryid, categoryname) return jsonify(Administrators=result), 200
def insertSupplier(self, form): print("form: ", form) if len(form) != 9: return jsonify(Error="Malformed post request"), 400 else: username = form['UserName'] password = form['Password'] email = form['Email'] slocation = form['SLocation'] firstname = form['FirstName'] lastname = form['LastName'] dateofbirth = form['DateofBirth'] gender = form['Gender'] categoryname = form['CategoryName'] if username and password and email and slocation and firstname and lastname and dateofbirth and gender and categoryname: dao = SuppliersDAO() supplierid = dao.insert(username, password, email, slocation, firstname, lastname, dateofbirth, gender, categoryname) result = self.build_suppliers_attributes( supplierid, username, password, email, slocation, firstname, lastname, dateofbirth, gender, categoryname) return jsonify(Suppliers=result), 201 else: return jsonify( Error="Unexpected attributes in post request"), 400
def updateSupplier(self, supplierid, form): dao = SuppliersDAO() if not dao.getSupplierById(supplierid): return jsonify(Error="Supplier not found."), 404 else: if len(form) != 11: return jsonify(Error="Malformed update request"), 400 else: username = form['UserName'] password = form['Password'] email = form['Email'] slocation = form['SLocation'] affiliation = form['affiliation'] firstname = form['FirstName'] lastname = form['LastName'] dateofbirth = form['DateofBirth'] gender = form['Gender'] categoryid = form['CategoryID'] categoryname = form['CategoryName'] if username and password and email and slocation and affiliation and firstname and lastname and dateofbirth and gender and categoryid and categoryname: dao.update(username, password, email, slocation, affiliation, firstname, lastname, dateofbirth, gender, categoryid, categoryname) result = self.build_suppliers_attributes( username, password, email, slocation, affiliation, firstname, lastname, dateofbirth, gender, categoryid, categoryname) return jsonify(Suppliers=result), 200 else: return jsonify( Error="Unexpected attributes in update request"), 400
def getAllSuppliers(self): dao = SuppliersDAO() suppliers_list = dao.getAllSuppliers() result_list = [] for row in suppliers_list: supplier = Supplier().build_dict_from_row(row) result_list.append(supplier) return jsonify(result_list)
def getSupplierById(self, suppID): dao = SuppliersDAO() row = dao.getSupplierById(suppID) if not row: return jsonify(Error="User not found"), 404 else: supplier = self.build_supplier_dict(row) return jsonify(Supplier=supplier)
def getAllSuppliers(self): dao = SuppliersDAO() suppliers_list = dao.getAllSuppliers() result_list = [] for row in suppliers_list: result = self.build_supplier_dict(row) result_list.append(result) return jsonify(Suppliers=result_list)
def getSupplierById(self, sid): dao = SuppliersDAO() row = dao.getSupplierById(sid) if not row: return jsonify(Error="Supplier Not Found"), 404 else: supplier = Supplier().build_dict_from_row(row) return jsonify(supplier)
def getSupplierById(self, SupplierID): dao = SuppliersDAO() row = dao.getSupplierById(SupplierID) if not row: return jsonify(Error="Supplier Not Found"), 404 else: suppliers = self.build_suppliers_dict(row) return jsonify(Suppliers=suppliers)
def getSuppliersCountPerRegion(self): dao = SuppliersDAO() counts_list = dao.getSuppliersCountPerRegion() result_list = [] for row in counts_list: count = Address().build_dict_from_row_count(row) result_list.append(count) return jsonify(result_list)
def getAddressBySid(self, sid): supplierDAO = SuppliersDAO() row = supplierDAO.getSupplierById(sid) if not row: return jsonify(Error="Supplier Not Found"), 404 # If supplier found, get all the stocks from that supplier else: dao = AddressesDAO() address = dao.getAddressBySid(sid) result_list = Address().build_dict_from_row(address) return jsonify(result_list)
def getSupplierTransactionById(self, sid, tid): supplierDAO = SuppliersDAO() supplier = supplierDAO.getSupplierById(sid) if not supplier: return jsonify(Error="Supplier Not Found"), 404 #If supplier found, get all the stocks from that supplier transactionsDao = ResourceTransactionsDAO() transaction = transactionsDao.getTransactionById(tid) if not transaction: return jsonify(Error="Transaction Not Found"), 404 result = ResourceTransaction().build_dict_from_table_no_sup_no_pur( transaction) return jsonify(result)
def getTransactionsBySupplierId(self, sid): supplierDAO = SuppliersDAO() row = supplierDAO.getSupplierById(sid) if not row: return jsonify(Error="Supplier Not Found"), 404 #If supplier found, get all the stocks from that supplier else: dao = ResourceTransactionsDAO() transactions_list = dao.getTransactionsBySid(sid) result_list = [] for row in transactions_list: transaction = ResourceTransaction().build_dict_from_row(row) result_list.append(transaction) return jsonify(result_list)
def getAvailabilityAnnouncementsBySID(self, sid): #TOMORROW FIX: WHATS UP WITH THE DIC. SOMETHING ABOUT ADMIN. supplierDAO = SuppliersDAO() supplier = supplierDAO.getSupplierById(sid) if not supplier: return jsonify(Error="Supplier Not Found"), 404 dao = AvailabilityAnnouncementsDAO() table = dao.getAnnouncementBySIDWithDetails(sid) if not table: return jsonify(Error="Availability Announcement Not Found"), 404 else: result = AvailabilityAnnouncement().build_dict_from_table_no_sup( table) return jsonify(result)
def getStocksBySupplierId(self, sid): # Check if supplier exists supplierDAO = SuppliersDAO() row = supplierDAO.getSupplierById(sid) if not row: return jsonify(Error="Supplier Not Found"), 404 #If supplier found, get all the stocks from that supplier else: dao = StocksDAO() stocks_list = dao.getStocksBySid(sid) result_list = [] for row in stocks_list: stock = Stock().build_dict_from_row_resource(row) result_list.append(stock) return jsonify(result_list)
def getAvailabilityAnnouncementByIds(self, sid, ann_id): supplierDAO = SuppliersDAO() supplier = supplierDAO.getSupplierById(sid) if not supplier: return jsonify(Error="Supplier Not Found"), 404 dao = AvailabilityAnnouncementsDAO() supplier = dao.getAnnouncementBySIDWithDetails(sid) if not supplier[0]: return jsonify(Error="Availability Announcement not found"), 404 result = dao.getAnnouncementByIdWithDetailsNoSup(ann_id) if not result: return jsonify(Error="Availability Announcement Not Found"), 404 else: newresult = AvailabilityAnnouncement( ).build_dict_from_table_no_sup(result) return jsonify(newresult)
def searchSuppliers(self, args): username = args.get("Username") password = args.get("Password") email = args.get("Email") slocation = args.get("SLocation") firstname = args.get("FirstName") lastname = args.get("LastName") dateofbirth = args.get("DateofBirth") gender = args.get("Gender") categoryname = args.get("CategoryName") dao = SuppliersDAO() suppliers_list = [] if (len(args) == 2) and username and password: suppliers_list = dao.getSupplierbyUserNameandPassword( username, password) elif (len(args) == 1) and username: suppliers_list = dao.getSupplierbyUsername(username) elif (len(args) == 1) and email: suppliers_list = dao.getSupplierrByEmail(email) elif (len(args) == 1) and slocation: suppliers_list = dao.getSupplierbyLocation(slocation) elif (len(args) == 1) and categoryname: suppliers_list = dao.getSupplierAndResourcesByCategoryName( categoryname) else: return jsonify(Error="Malformed query string"), 400 result_list = [] for row in suppliers_list: result = self.build_suppliers_dict(row) result_list.append(result) return jsonify(Suppliers=result_list)
def searchStocks(self, sid, args): dao = SuppliersDAO() row = dao.getSupplierById(sid) if not row: return jsonify(Error="Supplier Not Found"), 404 allowed_keys = {"rid", "rname", "catid", "catname"} allowed_range_keys = {"qtysum", "currentpriceperitem"} # Allow every query parameter stated in allowed_keys to have a min or max value max_and_min_keys = set() for key in allowed_range_keys: max_and_min_keys.add("max-" + key) max_and_min_keys.add("min-" + key) allowed_keys = allowed_keys.union(max_and_min_keys) allowed_keys = allowed_keys.union(allowed_range_keys) # Divide the args given by user into min, max and equal parameters for use in DAO max_args = {} min_args = {} equal_args = {} for key in args.keys(): if key in allowed_keys and key[0:4] == "max-": max_args[key[4:]] = args[key] elif key in allowed_keys and key[0:4] == "min-": min_args[key[4:]] = args[key] elif key not in allowed_keys: return jsonify(Error="Malfromed query string"), 400 else: equal_args[key] = args[key] # Added sid for searching specific uid equal_args['sid'] = sid # Get all the results for the search dao = StocksDAO() stocks_list = dao.getStocksByParamsNoSupplier(equal_args, max_args, min_args) result_list = [] for row in stocks_list: stock = Stock().build_dict_from_row_no_supplier(row) result_list.append(stock) return jsonify(result_list)
def searchSuppliers(self, args): # Query parameters allowed when searching # These parameters are from Resource, Category and Stock allowed_keys = {"rid", "rname", "catid", "catname", "region", "city"} allowed_range_keys = {"qtysum", "currentpriceperitem", "zipcode"} # Allow every query parameter stated in allowed_keys to have a min or max value max_and_min_keys = set() for key in allowed_range_keys: max_and_min_keys.add("max-" + key) max_and_min_keys.add("min-" + key) allowed_keys = allowed_keys.union(max_and_min_keys) allowed_keys = allowed_keys.union(allowed_range_keys) # Divide the args given by user into min, max and equal parameters for use in DAO max_args = {} min_args = {} equal_args = {} for key in args.keys(): if key in allowed_keys and key[0:4] == "max-": max_args[key[4:]] = args[key] elif key in allowed_keys and key[0:4] == "min-": min_args[key[4:]] = args[key] elif key not in allowed_keys: return jsonify(Error="Malfromed query string"), 400 else: equal_args[key] = args[key] # Get all the results for the search dao = SuppliersDAO() suppliers_list = dao.getSuppliersByResourceParams( equal_args, max_args, min_args) result_list = [] for row in suppliers_list: supplier = Supplier().build_dict_from_row_stock(row) result_list.append(supplier) return jsonify(result_list)
def insert(self, form): if len(form) != 1: return jsonify(Error="Malformed post request"), 400 else: uid = form['uid'] if uid: dao = UsersDAO() if not dao.getUserById(uid): return jsonify(Error="User not found"), 404 dao = SuppliersDAO() sid = dao.insert(uid) result = Supplier().build_dict_from_row( dao.getSupplierById(sid)) return jsonify(result) else: return jsonify( Error="Unexpected attributes in post request"), 400
def insert(self, form): if len(form) != 2: return jsonify(Error="Malformed post request"), 400 else: # VALIDATION # TODO: Check if supplier piid is the same # TODO: Check if valid objects in details array # Check if payment is valid buyer_pi_id = PaymentInfoDAO().getPaymentInfoById(form['piId'])[0] if not buyer_pi_id: return jsonify(Error="Purchase Info not found"), 400 supplier_resource_array = [] try: transaction_details = sorted(form['transactions'], key=lambda k: k['sid']) except: return jsonify(Error="Malformed post request"), 400 for detail in transaction_details: sid = detail['sid'] rid = detail['rid'] qty = detail['qty'] # Create function # Check if supplier and resource key is repeated if not self.checkIfExists(supplier_resource_array, sid, rid): # Check if supplier exists if not SuppliersDAO().getSupplierById(sid): return jsonify(Error="Supplier not found"), 400 # Check if resource exists if not ResourcesDAO().getResourceById(rid): return jsonify(Error="Resource not found"), 400 # Check if qty is valid # Check if stock exists available_stock = StocksDAO().getStockQtyById(rid, sid) if not available_stock: return jsonify(Error="Resource not in stock"), 400 # Check if qty is sufficient if available_stock[0] < qty: return jsonify( Error="Resource not enough quantity"), 400 supplier_resource_array.append({ 'sid': detail['sid'], 'rid': detail['rid'] }) else: return jsonify(Error="Repeated"), 400 user_id = UsersDAO().getUserIdByPIID(buyer_pi_id) # Create Purchase purchase_id = PurchaseDAO().insert(user_id, buyer_pi_id, 0, time.strftime("%Y/%m/%d")) current_sid = None current_transaction = None purchase_total = 0 transaction_total = 0 for detail in transaction_details: sid = detail['sid'] rid = detail['rid'] qty = detail['qty'] if not current_sid or current_sid != sid: # TODO: SUPPLIER PI ID pi_id = PaymentInfoDAO().getPaymentInfoBySid(sid) print(current_transaction) if current_transaction: # Update transaction print('hello') ResourceTransactionsDAO().updateTransactionAmmount( current_transaction[0], transaction_total) # Create Transaction current_transaction = ResourceTransactionsDAO().insert( sid, 0, purchase_id, pi_id) print(current_transaction) transaction_total = 0 # GET the current stock current_stock = StocksDAO().getStockQtyById(rid, sid)[0] # GET the current price current_price = StocksDAO().getStockPriceById(rid, sid)[0] # Create Transaction Detail ResourceTransactionDetailsDAO().insert(current_transaction, rid, current_price, qty) # Subtract Stock new_qty = current_stock - qty StocksDAO().updateQty(sid, rid, new_qty) purchase_total = purchase_total + current_price * qty transaction_total = transaction_total + current_price * qty # Modify Purchase Total PurchaseDAO().updatePurchasePrice(purchase_id, purchase_total) ResourceTransactionsDAO().updateTransactionAmmount( current_transaction[0], transaction_total) return jsonify({"purchase_id": purchase_id})
def count(self): dao = SuppliersDAO() result = dao.count() return jsonify(result[0])