def getAllPurchases(self):
     dao = TransactionDAO()
     transaction_list = dao.getAllPurchases()
     result_list = []
     for row in transaction_list:
         result = self.build_transaction_dict(row)
         result_list.append(result)
     return jsonify(Purchases=result_list)
 def getPurchaseById(self, tid):
     dao = TransactionDAO()
     transaction_list = dao.getPurchaseById(tid)
     result_list = []
     for row in transaction_list:
         result = self.build_transaction_dict(row)
         result_list.append(result)
     return jsonify(Purchase=result_list)
 def getTransactionByAmount(self, amount):
     dao = TransactionDAO()
     transaction_list = dao.getTransactionByAmmount(amount)
     result_list = []
     for row in transaction_list:
         result = self.build_transaction_dict(row)
         result_list.append(result)
     return jsonify(Transactions=result_list)
 def getTransactionByResource(self, rid):
     dao = TransactionDAO()
     transaction_list = dao.getTransactionByResource(rid)
     result_list = []
     for row in transaction_list:
         result = self.build_transaction_dict(row)
         result_list.append(result)
     return jsonify(Transactions=result_list)
 def getTransactionBySupplier(self, uids):
     dao = TransactionDAO()
     transaction_list = dao.getTransactionBySupplier(uids)
     result_list = []
     for row in transaction_list:
         result = self.build_transaction_dict(row)
         result_list.append(result)
     return jsonify(Transactions=result_list)
 def getTransactionByPayer(self, spid):
     dao = TransactionDAO()
     transaction_list = dao.getTransactionBySupplierPaymentInfo(spid)
     result_list = []
     for row in transaction_list:
         result = self.build_transaction_dict(row)
         result_list.append(result)
     return jsonify(Transactions=result_list)
 def getTransactionByQuantity(self, qty):
     dao = TransactionDAO()
     transaction_list = dao.getTransactionByQuantity(qty)
     result_list = []
     for row in transaction_list:
         result = self.build_transaction_dict(row)
         result_list.append(result)
     return jsonify(Transactions=result_list)
 def getResourceByTransactionId(self, tid):
     dao = TransactionDAO()
     if not dao.getTransactionById(tid):
         return jsonify(Error="Transaction Not Found"), 404
     resource_list = dao.getResourceByTransactionId(tid)
     result_list = []
     for row in resource_list:
         result = self.build_resource_dict(row)
         result_list.append(result)
     return jsonify(Resources=result_list)
 def getSupplierPaymentInfoByTransactionId(self, tid):
     dao = TransactionDAO()
     if not dao.getTransactionById(tid):
         return jsonify(Error="Transaction Not Found"), 404
     payments_list = dao.getSupplierPaymentInfoByTransactionId(tid)
     result_list = []
     for row in payments_list:
         result = self.build_user_dict(row)
         result_list.append(result)
     return jsonify(Resources=result_list)
    def insertTransaction(self, form, rid):
        tdate = form['tdate']
        tquantity = form['tquantity']
        tpayerpid = form['tpayerpid']
        tsupplierpid = form['tsupplierpid']
        #rid = form['rid'] MADE A CHANGE SO THAT WE TAKE RID FROM THE PURCHASE RESOURCE ROUTE
        tamount = form['tamount']

        """
        resourceDao = ResourcesDAO()
        if 'rid' in form:
            try:
                if resourceDao.getResourceById(int(rid)) is None:
                    return jsonify(error='rid does not exist')
            except ValueError:
                return jsonify(error='rid must be an int')
        """

        dao = TransactionDAO()
        #THIS IS MY ATTEMPT TO MAKE RESERVATIONS NOT NEED ANY PAYMENT INFO
        #BUT WE WOULD NEED TO CHANGE THE SCHEMA
        """
        if tamount == 0:
            result = dao.insertReservation(tdate, tquantity, rid, tamount)
        """

        paymentDao = PaymentDAO()

        if 'tpayerpid' in form:
            try:
                if paymentDao.getCardById(tpayerpid) is None:
                    return jsonify(error='tpayerpid ' + tpayerpid + ' does not exist')
            except:
                return jsonify(error='tpayerid must be an int')
        if 'tsupplierpid' in form:
            try:

                if not paymentDao.checkPaymentIsFromSupplier(int(tsupplierpid)):
                    return jsonify(error='tsupplierpid ' + tsupplierpid + ' does not exist or is not a supplier')
            except ValueError:
                return jsonify(error='supplierid must be an int')

        result = dao.insertPurchase(tdate, tquantity, tpayerpid, tsupplierpid, rid, tamount)
        return jsonify(Reqid=result)
 def updateTransaction(self, tid, form):
     dao = TransactionDAO()
     return f"Updated transaction with id: {tid}"
 def deleteTransaction(self, tid):
     dao = TransactionDAO()
     return f"Deleted transaction with id: {tid}"
 def countTransactions(self):
     dao = TransactionDAO()
     tCount = dao.countTransactions()
     return jsonify(TransactionCount=tCount)