diff --git a/app/classes/transactions.py b/app/classes/transactions.py new file mode 100644 index 0000000..ef64b9e --- /dev/null +++ b/app/classes/transactions.py @@ -0,0 +1,21 @@ +from sqlalchemy import Column, Integer, String, Float, DateTime +from app import db +import datetime + + +class Transaction(db.Model): + __tablename__ = "transactions" + + id = Column(Integer, primary_key=True, index=True) + preauthorize_amount = Column(Float, nullable=True) # Amount preauthorized (for auth transactions) + charge_amount = Column(Float, nullable=True) # Final charge amount (for charge/capture transactions) + transaction_type = Column(Integer) # 0 = charge, 1 = auth, 3 = capture + status = Column(Integer) # 0 = approved, 1 = declined + auth_net_transaction_id = Column(String, unique=True, index=True, nullable=True) + customer_id = Column(Integer) + service_id = Column(Integer, nullable=True) # Reference to Service_Service.id + delivery_id = Column(Integer, nullable=True) # Reference to Delivery_Delivery.id + card_id = Column(Integer, nullable=True) # Reference to credit card used for payment + payment_gateway = Column(Integer, default=1) # 1 = Authorize.Net, 0 = Other + rejection_reason = Column(String, nullable=True) # Detailed error message when payment is declined + created_at = Column(DateTime, default=datetime.datetime.utcnow) diff --git a/app/delivery_status/views.py b/app/delivery_status/views.py index fb95f3a..af651b1 100755 --- a/app/delivery_status/views.py +++ b/app/delivery_status/views.py @@ -8,6 +8,7 @@ from app.classes.delivery import (Delivery_Delivery, ) from app.classes.service import Service_Service from app.classes.auto import Auto_Delivery +from app.classes.transactions import Transaction from datetime import date, timedelta, datetime @@ -42,6 +43,8 @@ def get_sidebar_counts(): Service_Service.scheduled_date < start_of_tomorrow ).count() + transaction_count = db.session.query(Transaction).filter(Transaction.transaction_type == 0).count() + return jsonify({ "ok": True, "counts": { @@ -52,6 +55,7 @@ def get_sidebar_counts(): "automatic": automatic_count, "upcoming_service": upcoming_service_count, "today_service": today_service_count, + "transaction": transaction_count, } }), 200 diff --git a/app/payment/views.py b/app/payment/views.py index 4e544b7..963a73e 100755 --- a/app/payment/views.py +++ b/app/payment/views.py @@ -3,6 +3,7 @@ from app.payment import payment from app import db from app.classes.customer import Customer_Customer from app.classes.cards import Card_Card, Card_Card_schema +from app.classes.transactions import Transaction @@ -233,4 +234,46 @@ def update_user_card(card_id): db.session.add(get_card) db.session.commit() - return jsonify({"ok": True}), 200 \ No newline at end of file + return jsonify({"ok": True}), 200 + + +@payment.route("/transactions/authorize/", methods=["GET"]) +def get_authorize_transactions(page): + """ + Gets transactions with transaction_type = 0 (charge), for the authorize page + """ + try: + per_page = 50 + offset = (page - 1) * per_page + + query = ( + db.session + .query(Transaction, Customer_Customer) + .join(Customer_Customer, Transaction.customer_id == Customer_Customer.id) + .order_by(Transaction.created_at.desc()) + .offset(offset) + .limit(per_page) + ) + + results = query.all() + + transactions_data = [] + for transaction, customer in results: + transactions_data.append({ + "id": transaction.id, + "preauthorize_amount": transaction.preauthorize_amount, + "charge_amount": transaction.charge_amount, + "transaction_type": transaction.transaction_type, + "status": transaction.status, + "customer_name": f"{customer.customer_first_name} {customer.customer_last_name}", + "created_at": transaction.created_at.isoformat(), + "auth_net_transaction_id": transaction.auth_net_transaction_id, + "rejection_reason": transaction.rejection_reason, + "delivery_id": transaction.delivery_id, + "service_id": transaction.service_id, + }) + + return jsonify(transactions_data), 200 + + except Exception as e: + return jsonify({"ok": False, "error": str(e)}), 500 diff --git a/app/service/views.py b/app/service/views.py index a2e4e73..c14638f 100644 --- a/app/service/views.py +++ b/app/service/views.py @@ -288,6 +288,12 @@ def delete_service_plan(customer_id): db.session.rollback() return jsonify({"error": str(e)}), 500 +@service.route("/", methods=["GET"]) +def get_service_by_id(id): + service_record = Service_Service.query.get_or_404(id) + service_schema = Service_Service_schema() + return jsonify({"ok": True, "service": service_schema.dump(service_record)}), 200 + @service.route("/delete/", methods=["DELETE"]) def delete_service_call(id): service_record = Service_Service.query.get_or_404(id)