Пример #1
0
 def getAllPurchases(self):
     dao = PurchaseDAO()
     purchases_list = dao.getAllPurchases()
     result_list = []
     for row in purchases_list:
         result = Purchase().build_dict_from_row(row)
         result_list.append(result)
     return jsonify(result_list)
Пример #2
0
 def getPurchaseById(self, purchase_id):
     dao = PurchaseDAO()
     row = dao.getPurchaseById(purchase_id)
     if not row:
         return jsonify(Error="Resource Purchase Not Found"), 404
     else:
         purchase = Purchase().build_dict_from_table(row)
         return jsonify(purchase)
Пример #3
0
    def getUserPurchaseById(self, uid, pi_id):
        userDao = UsersDAO()
        purchasesDao = PurchaseDAO()
        user = userDao.getUserById(uid)
        if not user:
            return jsonify(Error="User Not Found"), 404

        purchase = purchasesDao.getPurchaseById(pi_id)
        if not purchase:
            return jsonify(Error="Purchase Not Found"), 404

        result = Purchase().build_dict_from_table_detailed(purchase)
        return jsonify(result)
Пример #4
0
    def getPurchasesByUserId(self, uid):
        userDao = UsersDAO()
        purchasesDao = PurchaseDAO()
        user = userDao.getUserById(uid)
        if not user:
            return jsonify(Error="User Not Found"), 404

        purchases = purchasesDao.getPurchasesByUid(uid)
        result_list = []
        for row in purchases:
            purchase = Purchase().build_dict_from_row_payment_no_user(row)
            result_list.append(purchase)
        return jsonify(result_list)
Пример #5
0
    def searchPurchases(self, args):
        date = args.get("date")
        total = args.get("total")
        uid = args.get("uid")
        buyer_pi_id = args.get("buyer_pi_id")
        username = args.get("username")  #added by Herbert to implement 18
        dao = PurchaseDAO()
        purchases_list = []
        if (len(args) == 4) and date and total and uid and buyer_pi_id:
            purchases_list = dao.getPurchasesByDateTotalUidAndBuyerPaymentInfoId(
                date, total, uid, buyer_pi_id)
        elif (len(args) == 3) and date and total and uid:
            purchases_list = dao.getPurchasesByDateTotalAndUid(
                date, total, uid)
        elif (len(args) == 3) and date and total and buyer_pi_id:
            purchases_list = dao.getPurchasesByDateTotalAndBuyerPaymentInfoId(
                date, total, buyer_pi_id)
        elif (len(args) == 3) and total and uid and buyer_pi_id:
            purchases_list = dao.getPurchasesByTotalUidAndBuyerPaymentInfoId(
                total, uid, buyer_pi_id)
        elif (len(args) == 3) and date and uid and buyer_pi_id:
            purchases_list = dao.getPurchasesByDateUidAndBuyerPaymentInfoId(
                date, uid, buyer_pi_id)
        elif (len(args) == 2) and date and total:
            purchases_list = dao.getPurchasesByDateAndTotal(date, total)
        elif (len(args) == 2) and date and uid:
            purchases_list = dao.getPurchasesByDateAndUid(date, uid)
        elif (len(args) == 2) and date and buyer_pi_id:
            purchases_list = dao.getPurchasesByDateAndBuyerPaymentInfo(
                date, buyer_pi_id)
        elif (len(args) == 2) and total and uid:
            purchases_list = dao.getPurchasesByTotalAndUid(total, uid)
        elif (len(args) == 2) and total and buyer_pi_id:
            purchases_list = dao.getPurchasesByTotalAndBuyerPaymentInfoId(
                total, buyer_pi_id)
        elif (len(args) == 2) and uid and buyer_pi_id:
            purchases_list = dao.getPurchasesByUidAndBuyerPaymentInfoId(
                uid, buyer_pi_id)
        elif (len(args) == 1) and date:
            purchases_list = dao.getPurchasesByDate(date)
        elif (len(args) == 1) and total:
            purchases_list = dao.getPurchasesByTotal(total)
        elif (len(args) == 1) and uid:
            purchases_list = dao.getPurchasesByUid(uid)
        elif (len(args) == 1) and buyer_pi_id:
            purchases_list = dao.getPurchasesByBuyerPaymentInfoId(buyer_pi_id)
        #this case added by Herbert to implement 18
        elif (len(args) == 1) and username:
            purchases_list = dao.getPurchasesBySupplier(username)
        else:
            return jsonify(Error="Malformed query string"), 400

        if (len(args) == 1) and buyer_pi_id:
            result_list = []
            for row in purchases_list:
                result = Purchase().build_dict_from_row2(row)
                result_list.append(result)
            return jsonify(result_list)
        else:
            result_list = []
            for row in purchases_list:
                result = Purchase().build_dict_from_row(row)
                result_list.append(result)
            return jsonify(result_list)
Пример #6
0
    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})