def post(self): data = PayProducts.parser.parse_args() try: for payed_product in data.get('products'): sold_product = SoldProductModel.find_by_id( payed_product.id['id']) sold_product.payment_pending = False sold_product.update_to_db() except: return { "message": "an error occurred while updating the sales" }, 500 category = CategoryModel.find_by_name('pago_provedor') if not category: category = CategoryModel.create_category('pago_provedor') transaction = TransactionModel(transaction_id=None, amount=data.get('total'), date=str(data.get('date'))[:19], description=data.get('description'), is_expense=True, currency_id=data.get('currency_id'), category_id=category.category_id, method=data.get('method'), exchange=data.get('exchange')) transaction.save_to_db() return transaction.json(), 201
def put(self): data = Transaction.parser.parse_args() transaction = TransactionModel.find_by_id(data['id']) if transaction is None: transaction = TransactionModel( datetime.strptime(data['date'], "%Y-%m-%d"), data['vendor'].lower(), data['category'].lower(), data['price']) else: transaction.date = datetime.strptime(data['date'], "%Y-%m-%d") transaction.vendor = data['vendor'].lower() transaction.category = data['category'].lower() transaction.price = data['price'] try: transaction.save_to_db() except: return { "message": "An error occurred inserting/updating the transaction." }, 500 return transaction.json()
def delete(self, transaction_id): try: TransactionModel.delete_from_db(transaction_id) except: return {"message": "An error occurred deleting the transaction"}, 500 return {"message": "Transaction deleted"}, 200
def get(self): custom_filter = TransactionList.parser.parse_args() if custom_filter.get('deleted'): return [transaction.json() for transaction in TransactionModel.filter_by_deleted(str_to_bool(custom_filter.get('deleted')))] else: return [transaction.json() for transaction in TransactionModel.find_all()]
def put(self): data = Transaction.parser.parse_args() transaction = TransactionModel(**data) try: transaction.update_to_db() except: return {"message": "An error occurred updating the transaction"}, 500 return transaction.json(), 200
def post(self, id): data = Transaction.parser.parse_args() transaction = TransactionModel.find_by_name(id) if transaction: return {"message": "Transaction already axists"} transaction = TransactionModel(**data) print(transaction) transaction.save_to_db() return {"message": "data has been saved"}, 201
def post(self): data = Transaction.parser.parse_args() transaction = TransactionModel( datetime.strptime(data['date'], "%Y-%m-%d"), data['vendor'].lower(), data['category'].lower(), data['price']) try: transaction.save_to_db() except Exception as e: return { "message": f"An error occurred inserting the transaction. {e}" }, 500 return transaction.json()
def post(cls, request_id): data = request.get_json() req = ReturnRequestModel.find_by_id(request_id) if not req: return {"message": "no request found"}, 404 if data['response']: req.book.is_borrowed = False transaction = TransactionModel.find_issued_book(req.book_id) if not transaction: return {"message": "transaction not found"}, 404 transaction.return_date = datetime.datetime.now() if transaction.book.till_date < datetime.datetime.now(): interval = transaction.book.till_date - datetime.datetime.now() user = UserModel.find_by_id(transaction.lent_to) print(user.merit_point) user.merit_point = user.merit_point + (interval.days + 1) * 2 user.save_to_db() transaction.save_to_db() switcher = {1: -6, 2: -4, 3: -2, 4: 0, 5: 2} user = UserModel.find_by_id(req.sent_by) user.merit_point = user.merit_point + switcher[data['rating']] user.save_to_db() req.delete_from_db() return {"message": "Accepted request"}, 200 else: req.delete_from_db() return {"message": "Rejected request"}, 200
def post(self): # you could deposit using the user_name/pwd data = request.get_json() name = data.get("name") pwd = data.get("pwd") amount = data.get("amount") user = UserModel.query.filter(UserModel.name == name).filter(UserModel.password == pwd).first() if user: account = AccountModel.query.filter(AccountModel.user_id == user.id).first() if account: if account.status == ACTIVE: account.balance += amount transaction = TransactionModel(from_accnum=DEPOSIT_FROM_ACCNUM, to_accnum=account.acc_num, message="Money deposited: External Source", amount=amount, to_acc_balance=account.balance, transaction_date=datetime.now() ) db.session.add_all([account, transaction]) db.session.commit() return {"message": "Deposit successful, new balance: %s" % account.balance} else: return {"message": "Account Inactive, call customer care to activate the account"}, 403 else: return {"message": "Technical error retrieving account, please call customer care"}, 500 return {"message": "Username/Pwd incorrect"}, 401
def delete(self): data = Transaction.parser.parse_args() transaction = TransactionModel.find_by_id(data['id']) if transaction: transaction.delete_from_db() return {"message": "Transaction deleted."}
def get(self): data = Transaction.parser.parse_args() transaction = TransactionModel.find_by_id(data['id']) if transaction: return transaction.json() else: return {"message": "Transaction not found."}, 404
def get(self, wallet_id): transaction = TransactionModel.find_by_walletid(wallet_id) if transaction: return json.dumps(transaction) return {'err': 'There are no transactions associated with this wallet'}, 404
def get(self, user_id): transactions = [ transaction_schema.dump(transaction) for transaction in TransactionModel.find_all(user_id) ] if transactions: return transactions, 200 return {'message': 'no transactions available'}, 200
def delete(self, sale_id): try: products_from_sale = SoldProductModel.find_by_sale_id(sale_id) payments_from_sale = TransactionModel.find_by_sale_id(sale_id) for sold_product in products_from_sale: SoldProductModel.delete_from_db(sold_product.sold_product_id) for payment in payments_from_sale: TransactionModel.delete_from_db(payment.transaction_id) SaleModel.delete_from_db(sale_id) except: return {"message": "An error occurred deleting the sale"}, 500 return {"message": "Sale deleted"}, 200
def get(self, user_id, _id): if user_id == get_jwt_identity(): transaction = TransactionModel.find_by_id(user_id, _id) if transaction and transaction.user_id == get_jwt_identity(): return transaction_schema.dump(transaction), 200 return {'message': 'Transaction not found'} return {'message': 'You can only fetch your own transactions'}
def put(self, id): data = Transaction.parser.parse_args() transaction = TransactionModel.find_by_name(id) if transaction: transaction.start_date = data['start_date'] transaction.end_date = data['end_date'] transaction.total_price = data['total_price'] transaction.modified_by = data['modified_by'] print(transaction.modified_by) transaction.save_to_db() return {"message": "data has been update"} transaction = TransactionModel(**data) print(transaction) transaction.save_to_db() return {"message": "data has been saved"}, 201
def get(self, user_id, date): date_for_filter = datetime.datetime.strptime(date, "%Y-%m-%d") transactions = [ transaction_schema.dump(transaction) for transaction in TransactionModel.find_by_date( user_id, date_for_filter) ] if transactions: return transactions, 200 return {'message': 'no transactions available'}, 200
def post(self): data = request.get_json() name = data.get("name") pwd = data.get("pwd") amount = data.get("amount") to_acc_num = data.get("to_acc_num") message = data.get("message") to_account = AccountModel.query.filter( AccountModel.acc_num == to_acc_num).first() if not to_account: return { "message": "Entered INVALID account number, check receivers account number" }, 400 user = UserModel.query.filter(UserModel.name == name).filter( UserModel.password == pwd).first() if to_account.user_id == user.id: return {"message": "Cannot transfer to your own account"}, 400 elif to_account.status != ACTIVE: return { "message": "Cannot process the transaction, receivers account not ACTIVE" }, 400 if user: from_account = AccountModel.query.filter( AccountModel.user_id == user.id).first() if from_account.status != ACTIVE: return { "message": "Senders Account not active, call customer care to activate the account" }, 400 if from_account.balance < amount: return { "message": "Not enough funds to make this transfer" }, 400 else: to_account.balance += amount from_account.balance -= amount transaction = TransactionModel( from_accnum=from_account.acc_num, to_accnum=to_account.acc_num, message=message, amount=amount, from_acc_balance=from_account.balance, to_acc_balance=to_account.balance, transaction_date=datetime.now()) db.session.add_all([to_account, from_account, transaction]) db.session.commit() return { "message": "Transfer successful, new balance: %s" % from_account.balance } else: return {"message": "Username/Pwd incorrect"}, 401
def post(cls, request_id): data = request.get_json() req = BorrowRequestModel.find_by_id(request_id) if not req: return {"message": "no request found"}, 404 if data['response']: if req.book.is_borrowed: return {"message": "already borrowed"}, 400 req.book.is_borrowed = True req.save_to_db() transaction = TransactionModel(borrow_date=datetime.datetime.now(), borrowed_from=req.received_by, lent_to=req.sent_by, book_id=req.book_id) transaction.save_to_db() req.delete_from_db() return {"message": "Request Accepted"}, 200 else: req.delete_from_db() return {"message": "Request rejected"}, 200
def delete(self, user_id, _id): if user_id == get_jwt_identity(): transaction = TransactionModel.find_by_id(user_id, _id) if transaction and transaction.user_id == get_jwt_identity(): transaction.delete_from_db() return {'message': f'Transaction with id:{_id} deleted'}, 200 return {'message': 'Transaction not found'}, 404 return {'message': 'You can only delete your own transactions'}, 401
def put(self): data = Category.parser.parse_args() data = { "name": data['name'].lower(), "type": data['type'].lower(), "budget": data['budget'] } category = CategoryModel.find_by_name(data['name'].lower()) if category is None: category = CategoryModel(**data) else: if data['budget'] is not None: category.budget = data['budget'] if not category.type == data['type']: category.type = data['type'] TransactionModel.update_prices(**data) category.save_to_db() return category.json()
def put(self, user_id, _id): if user_id == get_jwt_identity(): transaction_json = request.get_json() transaction = TransactionModel.find_by_id(user_id, _id) if transaction: for key in transaction_json: setattr(transaction, key, transaction_json[key]) transaction.save_to_db() return transaction_schema.dump(transaction), 200 return {'message': 'Transaction not found'}, 404 return {'message': 'You can only edit your own transactions'}, 401
def post(cls, book_id): book = BookModel.find_by_id(book_id) if not book: return {"message": "Book not found"}, 404 if not book.is_borrowed: return {"message": "Book not borrowed"}, 400 transaction = TransactionModel.find_issued_book(book_id) if not transaction: return {"message": "Transaction not found"}, 404 req = ReturnRequestModel.find_by_book_id(book_id) if req: return {"message": "Already sent"}, 400 req = ReturnRequestModel(date=datetime.datetime.now(), sent_by=transaction.lent_to, received_by=transaction.borrowed_from, book_id=book_id) req.save_to_db() return {"message": "Return request sent"}, 200
def get(self, sale_id): sale = SaleModel.find_by_id(sale_id) if not sale: return {"message": "no sale with that id"}, 404 products_from_sale = SoldProductModel.find_by_sale_id(sale_id) payments_from_sale = TransactionModel.find_by_sale_id(sale_id) return {"sale_id": sale.sale_id, "date": str(sale.date)[:19], "client": sale.client.json(), "total": sale.total, "discount": sale.discount, "user_commission": sale.user_commission, "promoter_commission": sale.promoter_commission, "payments": [payment.json() for payment in payments_from_sale], "products": [product.json() for product in products_from_sale], "deleted": sale.deleted}, 200
def put(self, transaction_id): data = Transaction.parser.parse_args() transaction = TransactionModel.find_by_transaction_id(transaction_id) if transaction: transaction.price = data['price'] transaction.payer_id = data['payer_id'] else: transaction = TransactionModel(transaction_id, **data) transaction.save_to_db() return transaction.json()
def post(self): data = Transaction.parser.parse_args() type_ = data['transaction_type'] sender = data['sender'] receiver = data['receiver'] details = data['transaction_details'] amount = data['amount'] transaction = TransactionModel(type_, sender, receiver, amount, details) sender_account = Account.find_by_user_id(sender) receiver_account = Account.find_by_user_id(receiver) if sender_account: balance = int(str(sender_account.balance)) int_amount = int(str(amount)) if balance < int_amount: return {'message': 'Insufficient Balance'}, 400 new_balance = balance - int_amount Account.update_account(sender, new_balance) if receiver_account: receiver_balance = int(str(receiver_account.balance)) receiver_new_balance = receiver_balance + int_amount Account.update_account(receiver, receiver_new_balance) transaction.save_transaction() return {'message': 'Transaction saved successfully'}, 201 return { 'message': "No Account has been Associated with this User ID" }, 400
def post(self, transaction_id): if TransactionModel.find_by_transaction_id(transaction_id): return { 'message': "An transaction with transaction_id '{}' already exists.". format(transaction_id) }, 400 data = Transaction.parser.parse_args() transaction = TransactionModel(transaction_id, **data) try: transaction.save_to_db() except: return { "message": "An error occurred inserting the transaction." }, 500 return transaction.json(), 201
def post(self, product_id): transaction = TransactionModel.find_unfinished(product_id=product_id) if transaction: try: transaction.mark_done() except: return { 'message': 'An error occurred marking the transaction complete.' }, 500 else: transaction = TransactionModel(product_id, trip_id=trip_id) try: transaction.save_to_db() except: return { 'message': 'An error occurred writing to the transaction table.' }, 500 return transaction.json(), 201
def get(cls, user_id): transactions = TransactionModel.find_due_book(user_id) for transaction in transactions: if transaction.book.till_date < datetime.datetime.now(): return {"due": True}, 200 return {"due": False}, 200
def get(cls, user_id): return { "transactions": transaction_list_schema.dump( TransactionModel.find_user_summary(user_id)) }, 200