示例#1
0
    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()
示例#2
0
 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 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
示例#4
0
 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
示例#5
0
    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
示例#6
0
    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
示例#7
0
    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()
示例#8
0
    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()
示例#9
0
    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
示例#10
0
    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
示例#11
0
 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
示例#12
0
    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
示例#13
0
    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
示例#14
0
    def post(self):
        json_data = request.get_json(force=True)

        total = 0
        sold_products = []

        for payment in json_data.get('payments'):

            if json_data.get('credit_charge'):
                charge = json_data.get('credit_charge')
            else:
                charge = 1

            total += payment.get('amount') * payment.get('exchange') * charge

        if not Sale.are_equal(total, json_data.get('total')):
            return {'message': 'Amounts are not correct'}

        if json_data.get('seller_id'):
            user_id = json_data.get('seller_id')
        else:
            user_id = get_jwt_identity()

        sale = SaleModel(total=json_data.get('total'), user_id=user_id, date=json_data.get('date'),
                         sale_id=json_data.get('sale_id'), client_id=json_data.get('client_id'),
                         promoter_id=json_data.get('promoter_id'),
                         promoter_commission=json_data.get('promoter_commission'),
                         user_commission=json_data.get('seller_commission'), discount=json_data.get('discount'))

        sale.save_to_db()

        for product in json_data.get('products'):
            sold_product = SoldProductModel(product_id=product.get('product_id'),
                                            price=product.get('price'), adults=product.get('adults'),
                                            children=product.get('children'), date=product.get('date'),
                                            babies=product.get('babies'),
                                            transfer=product.get('transfer'),
                                            sale_id=sale.sale_id, payment_pending=True)
            sold_product.save_to_db()
            sold_products.append(sold_product)
            prod_sold_msg = prepare_provider_email(product, sale)
            result = send_email(sold_product.product.provider.email, prod_sold_msg)

        category = CategoryModel.find_by_name('venta')

        if not category:
            category = CategoryModel.create_category('venta')

        for payment in json_data.get('payments'):
            transaction = TransactionModel(transaction_id=None, amount=payment.get('amount'), date=str(sale.date)[:19],
                                           description=payment.get('description'), is_expense=False,
                                           category_id=category.category_id,
                                           sale_id=sale.sale_id, exchange=payment.get('exchange'),
                                           method=payment.get('method'), currency_id=payment.get('currency_id'))
            transaction.save_to_db()

        sale_msg = prepare_sale_email(sale, sold_products)
        result_sale = send_email(sale.client.email, sale_msg)

        receipt_msg = prepare_receipt_email(sale, sold_products)
        result_receipt = send_email(sale.client.email, receipt_msg)

        json_sale = sale.json()
        json_sale['sale_email_sent'] = result_sale
        json_sale['receipt_email_sent'] = result_receipt
        return json_sale, 201
示例#15
0
 def __recordTransaction(self, id, amount, timestamp):
     transaction = TransactionModel(amount, timestamp, id)
     try:
         transaction.save_to_db()
     except:
         return {"err": "An error occured while recording the transaction"}