implement currency and report
This commit is contained in:
@@ -8,11 +8,12 @@ from routes.transporters import transporters_bp
|
||||
from routes.destinations import destinations_bp
|
||||
from routes.orders_out import orders_bp
|
||||
from routes.ouders_in import orders_in_bp
|
||||
from routes.report import report_bp
|
||||
from routes.report_order_out import report_order_out_bp
|
||||
from admin.subscription import admin_subscription_bp
|
||||
from routes.subscription import subscription_bp
|
||||
from admin.tenants import admin_user_bp
|
||||
from routes.company_user import company_user_bp
|
||||
from routes.currency import currency_bp
|
||||
|
||||
from apscheduler.schedulers.background import BackgroundScheduler
|
||||
from models.subscription import Subscription
|
||||
@@ -66,11 +67,12 @@ app.register_blueprint(transporters_bp, url_prefix="/transporters")
|
||||
app.register_blueprint(destinations_bp, url_prefix="/destinations")
|
||||
app.register_blueprint(orders_bp, url_prefix="/orders")
|
||||
app.register_blueprint(orders_in_bp, url_prefix="/orders_in")
|
||||
app.register_blueprint(report_bp, url_prefix="/report")
|
||||
app.register_blueprint(report_order_out_bp)
|
||||
app.register_blueprint(admin_subscription_bp)
|
||||
app.register_blueprint(subscription_bp)
|
||||
app.register_blueprint(admin_user_bp)
|
||||
app.register_blueprint(company_user_bp)
|
||||
app.register_blueprint(currency_bp)
|
||||
|
||||
def update_subscription_statuses_job():
|
||||
print("[Scheduler] Running daily subscription status check...")
|
||||
|
||||
Binary file not shown.
53
transportmanager/server/models/currency.py
Normal file
53
transportmanager/server/models/currency.py
Normal file
@@ -0,0 +1,53 @@
|
||||
from datetime import datetime
|
||||
from database import get_connection, is_postgres
|
||||
|
||||
class Currency:
|
||||
def __init__(self):
|
||||
self.ph = "%s" if is_postgres() else "?"
|
||||
|
||||
def currency_to_dict(self, row):
|
||||
currency = {
|
||||
'id': row[0],
|
||||
'user_id': row[1],
|
||||
'name': row[2],
|
||||
'value': row[3],
|
||||
'created_at': row[4]
|
||||
}
|
||||
return currency
|
||||
|
||||
def insert_currency(self, access_data):
|
||||
created_at = datetime.now().isoformat()
|
||||
with get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
returning = "RETURNING id" if is_postgres() else ""
|
||||
query = f"""
|
||||
INSERT INTO currency (
|
||||
user_id, name, value, created_at
|
||||
) VALUES ({self.ph}, {self.ph}, {self.ph}, {self.ph}) {returning}
|
||||
"""
|
||||
cursor.execute(
|
||||
query,
|
||||
(
|
||||
access_data['user_id'],
|
||||
access_data['name'],
|
||||
access_data['value'],
|
||||
created_at
|
||||
)
|
||||
)
|
||||
inserted_id = None
|
||||
if is_postgres():
|
||||
inserted_id = cursor.fetchone()[0]
|
||||
else:
|
||||
inserted_id = cursor.lastrowid
|
||||
if hasattr(conn, "commit"):
|
||||
conn.commit()
|
||||
return inserted_id
|
||||
|
||||
def get_currency_user_id(self, user_id):
|
||||
with get_connection() as conn:
|
||||
cursor = conn.cursor()
|
||||
cursor.execute(f"SELECT * FROM currency WHERE user_id = {self.ph}", (user_id,))
|
||||
row = cursor.fetchone()
|
||||
return self.currency_to_dict(row) if row else None
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@ class OrdersIn:
|
||||
"created_at": row[10],
|
||||
"file":row[11],
|
||||
"expenses": row[12],
|
||||
"currency": row[13]
|
||||
}
|
||||
|
||||
def order_point_to_dict(self, row):
|
||||
@@ -43,8 +44,8 @@ class OrdersIn:
|
||||
f"""
|
||||
INSERT INTO orders_in
|
||||
(user_id, client_id, products_description, received_price, order_number,
|
||||
ldb_quantity, kg_quantity, track_reg_number, trailer_reg_number, created_at, file_name, expenses)
|
||||
VALUES ({self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}){returning}
|
||||
ldb_quantity, kg_quantity, track_reg_number, trailer_reg_number, created_at, file_name, expenses, currency)
|
||||
VALUES ({self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}){returning}
|
||||
""",
|
||||
(
|
||||
data["user_id"],
|
||||
@@ -58,7 +59,8 @@ class OrdersIn:
|
||||
data["trailer_reg_number"],
|
||||
created_at,
|
||||
data["file"],
|
||||
data["expenses"]
|
||||
data["expenses"],
|
||||
data['currency']
|
||||
),
|
||||
)
|
||||
new_id = cur.fetchone()[0] if is_postgres() else getattr(cur, "lastrowid", None)
|
||||
@@ -75,7 +77,7 @@ class OrdersIn:
|
||||
user_id = {self.ph}, client_id = {self.ph}, products_description = {self.ph},
|
||||
received_price = {self.ph}, order_number = {self.ph},
|
||||
ldb_quantity = {self.ph}, kg_quantity = {self.ph}, track_reg_number = {self.ph},
|
||||
trailer_reg_number = {self.ph}, file_name = {self.ph}, expenses = {self.ph}
|
||||
trailer_reg_number = {self.ph}, file_name = {self.ph}, expenses = {self.ph}, currency = {self.ph}
|
||||
WHERE id = {self.ph}
|
||||
""",
|
||||
(
|
||||
@@ -90,6 +92,7 @@ class OrdersIn:
|
||||
data["trailer_reg_number"],
|
||||
data['file'],
|
||||
data['expenses'],
|
||||
data['currency'],
|
||||
data["id"],
|
||||
),
|
||||
)
|
||||
|
||||
@@ -21,7 +21,9 @@ class OrdersOut:
|
||||
"received_price": row[10],
|
||||
"paid_price": row[11],
|
||||
"created_at": row[12],
|
||||
"status": row[13]
|
||||
"status": row[13],
|
||||
"order_in_number": row[14],
|
||||
"currency":row[15]
|
||||
}
|
||||
|
||||
def order_point_to_dict(self, row):
|
||||
@@ -44,8 +46,8 @@ class OrdersOut:
|
||||
f"""
|
||||
INSERT INTO orders_out
|
||||
(user_id, client_id, transporter_id, products_description, received_price, paid_price, order_number,
|
||||
ldb_quantity, kg_quantity, track_reg_number, trailer_reg_number, created_at)
|
||||
VALUES ({self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}){returning}
|
||||
ldb_quantity, kg_quantity, track_reg_number, trailer_reg_number, created_at, order_in_number, currency)
|
||||
VALUES ({self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}){returning}
|
||||
""",
|
||||
(
|
||||
data["user_id"],
|
||||
@@ -60,6 +62,8 @@ class OrdersOut:
|
||||
data["track_reg_number"],
|
||||
data["trailer_reg_number"],
|
||||
created_at,
|
||||
data['order_in_number'],
|
||||
data['currency']
|
||||
),
|
||||
)
|
||||
new_id = cursor.fetchone()[0] if is_postgres() else getattr(cursor, "lastrowid", None)
|
||||
@@ -75,7 +79,7 @@ class OrdersOut:
|
||||
UPDATE orders_out SET client_id = {self.ph}, transporter_id = {self.ph}, products_description = {self.ph},
|
||||
received_price = {self.ph}, paid_price = {self.ph}, order_number = {self.ph},
|
||||
ldb_quantity = {self.ph}, kg_quantity = {self.ph}, track_reg_number = {self.ph},
|
||||
trailer_reg_number = {self.ph}
|
||||
trailer_reg_number = {self.ph}, order_in_number = {self.ph}, currency = {self.ph}
|
||||
WHERE id = {self.ph}
|
||||
""",
|
||||
(
|
||||
@@ -89,6 +93,8 @@ class OrdersOut:
|
||||
data["kg_quantity"],
|
||||
data["track_reg_number"],
|
||||
data["trailer_reg_number"],
|
||||
data['order_in_number'],
|
||||
data['currency'],
|
||||
data["id"],
|
||||
),
|
||||
)
|
||||
|
||||
@@ -83,10 +83,10 @@ class Users:
|
||||
returning = "RETURNING id" if is_postgres() else ""
|
||||
query = f"""
|
||||
INSERT INTO users (
|
||||
name, email, password_hash, created_at, user_role, company_id
|
||||
) VALUES ({self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}) {returning}
|
||||
name, email, password_hash, created_at, user_role, company_id, temporary_password
|
||||
) VALUES ({self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}, {self.ph}) {returning}
|
||||
"""
|
||||
cursor.execute(query, (name, email, password_hash, created_at, user_role, company_id))
|
||||
cursor.execute(query, (name, email, password_hash, created_at, user_role, company_id, 1))
|
||||
inserted_id = None
|
||||
if is_postgres():
|
||||
inserted_id = cursor.fetchone()[0]
|
||||
|
||||
36
transportmanager/server/routes/currency.py
Normal file
36
transportmanager/server/routes/currency.py
Normal file
@@ -0,0 +1,36 @@
|
||||
from flask import Blueprint, request, jsonify
|
||||
from models.currency import Currency
|
||||
from models.user import Users
|
||||
|
||||
from flask_jwt_extended import jwt_required, get_jwt_identity
|
||||
|
||||
currency_bp = Blueprint("currency", __name__, url_prefix="/currency")
|
||||
|
||||
@currency_bp.route("/", methods=["GET"])
|
||||
@jwt_required()
|
||||
def list_currency():
|
||||
#currency = Currency()
|
||||
currency = [
|
||||
{
|
||||
'id':1,
|
||||
'name':'USD',
|
||||
'value':'',
|
||||
},
|
||||
{
|
||||
'id':2,
|
||||
'name':'EURO',
|
||||
'value':'',
|
||||
},
|
||||
{
|
||||
'id':3,
|
||||
'name':'CHF',
|
||||
'value':'',
|
||||
},
|
||||
{
|
||||
'id':4,
|
||||
'name':'GBP',
|
||||
'value':'',
|
||||
}
|
||||
]
|
||||
|
||||
return jsonify(currency), 200
|
||||
@@ -50,6 +50,8 @@ def create_order_route():
|
||||
'track_reg_number': incoming_data["track_reg_number"],
|
||||
'trailer_reg_number': incoming_data["trailer_reg_number"],
|
||||
'products_description': incoming_data["products_description"],
|
||||
'order_in_number': incoming_data["order_in_number"],
|
||||
'currency': incoming_data["currency"]
|
||||
}
|
||||
order_id = orders.create_order(order_data)
|
||||
|
||||
@@ -110,6 +112,8 @@ def update_order_route(order_id):
|
||||
"track_reg_number": data.get("track_reg_number", order["track_reg_number"]),
|
||||
"trailer_reg_number": data.get("trailer_reg_number", order["trailer_reg_number"]),
|
||||
"products_description": data.get("products_description", order["products_description"]),
|
||||
"order_in_number": data.get("order_in_number", order["order_in_number"]),
|
||||
"currency":data.get("currency", order["currency"]),
|
||||
})
|
||||
|
||||
orders.delete_points_by_order_id(order_id)
|
||||
|
||||
@@ -33,7 +33,8 @@ def create_order_in_route():
|
||||
'trailer_reg_number': incoming_data["trailer_reg_number"],
|
||||
'products_description': incoming_data["products_description"],
|
||||
'file': incoming_data['file'],
|
||||
'expenses': incoming_data['expenses']
|
||||
'expenses': incoming_data['expenses'],
|
||||
'currency': incoming_data['currency']
|
||||
}
|
||||
#print(order_data)
|
||||
order_id = orders.create_order(order_data)
|
||||
@@ -91,9 +92,10 @@ def update_order_route(order_id):
|
||||
"track_reg_number": data.get("track_reg_number", order["track_reg_number"]),
|
||||
"trailer_reg_number": data.get("trailer_reg_number", order["trailer_reg_number"]),
|
||||
"products_description": data.get("products_description", order["products_description"]),
|
||||
'file': data.get("file", order["file"]),
|
||||
'expenses': data.get("expenses", order["expenses"]),
|
||||
"user_id":user_id
|
||||
"file": data.get("file", order["file"]),
|
||||
"expenses": data.get("expenses", order["expenses"]),
|
||||
"currency": data.get("currency", order["currency"]),
|
||||
"user_id":user_id,
|
||||
})
|
||||
|
||||
orders.delete_points_by_order_id(order_id)
|
||||
|
||||
@@ -30,7 +30,8 @@ def get_profile():
|
||||
"terms": user["terms"],
|
||||
"first_order_number": user["first_order_number"],
|
||||
"user_role": user["user_role"],
|
||||
"vat":user["vat"]
|
||||
"vat":user["vat"],
|
||||
"company_id":user['company_id']
|
||||
})
|
||||
|
||||
|
||||
|
||||
@@ -4,9 +4,9 @@ from models.order_out import OrdersOut # Your plain SQL model
|
||||
from datetime import datetime
|
||||
from models.user import Users
|
||||
|
||||
report_bp = Blueprint("report", __name__, url_prefix="/report")
|
||||
report_order_out_bp = Blueprint("report_order_out", __name__, url_prefix="/report_order_out")
|
||||
|
||||
@report_bp.route("/profit", methods=["GET"])
|
||||
@report_order_out_bp.route("/profit", methods=["GET"])
|
||||
@jwt_required()
|
||||
def get_profit_report():
|
||||
try:
|
||||
@@ -1,4 +1,26 @@
|
||||
-- Reset schema: drop tables first (children → parents), then recreate.
|
||||
BEGIN TRANSACTION;
|
||||
PRAGMA foreign_keys=OFF;
|
||||
|
||||
-- Drop child tables first to avoid FK constraints
|
||||
DROP TABLE IF EXISTS order_in_points;
|
||||
DROP TABLE IF EXISTS order_out_points;
|
||||
DROP TABLE IF EXISTS orders_in;
|
||||
DROP TABLE IF EXISTS orders_out;
|
||||
DROP TABLE IF EXISTS email;
|
||||
DROP TABLE IF EXISTS subscriptions;
|
||||
DROP TABLE IF EXISTS company_user_access;
|
||||
DROP TABLE IF EXISTS destinations;
|
||||
DROP TABLE IF EXISTS transporters;
|
||||
DROP TABLE IF EXISTS clients;
|
||||
DROP TABLE IF EXISTS currency;
|
||||
DROP TABLE IF EXISTS users;
|
||||
|
||||
PRAGMA foreign_keys=ON;
|
||||
COMMIT;
|
||||
|
||||
-- Users table
|
||||
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
name TEXT NOT NULL,
|
||||
@@ -18,7 +40,7 @@ CREATE TABLE IF NOT EXISTS users (
|
||||
user_role TEXT NOT NULL DEFAULT 'user' CHECK (user_role IN ('user', 'admin', 'company_user')),
|
||||
company_id INTEGER DEFAULT 0,
|
||||
active INTEGER DEFAULT 1,
|
||||
temporary_passwrd INTEGER DEFAULT 0
|
||||
temporary_password INTEGER DEFAULT 0
|
||||
);
|
||||
|
||||
-- Clients table
|
||||
@@ -79,6 +101,8 @@ CREATE TABLE IF NOT EXISTS orders_out (
|
||||
paid_price DOUBLE PRECISION,
|
||||
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
|
||||
order_status TEXT NOT NULL DEFAULT 'active' CHECK (order_status IN ('active', 'inactive', 'cancelled')),
|
||||
order_in_number TEXT,
|
||||
currency TEXT,
|
||||
FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(client_id) REFERENCES clients(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(transporter_id) REFERENCES transporters(id) ON DELETE CASCADE
|
||||
@@ -99,6 +123,7 @@ CREATE TABLE IF NOT EXISTS orders_in (
|
||||
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
|
||||
file_name TEXT,
|
||||
expenses DOUBLE PRECISION,
|
||||
currency TEXT,
|
||||
FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE,
|
||||
FOREIGN KEY(client_id) REFERENCES clients(id) ON DELETE CASCADE
|
||||
);
|
||||
@@ -162,3 +187,12 @@ CREATE TABLE IF NOT EXISTS company_user_access (
|
||||
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY(company_user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS currency (
|
||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER NOT NULL,
|
||||
name TEXT,
|
||||
value DOUBLE PRECISION,
|
||||
created_at TIMESTAMPTZ DEFAULT CURRENT_TIMESTAMP,
|
||||
FOREIGN KEY(user_id) REFERENCES users(id) ON DELETE CASCADE
|
||||
);
|
||||
@@ -191,9 +191,13 @@ def generate_order_pdf(order, user_data, transporter_data, logo_path, save_to_di
|
||||
|
||||
doc.build(elements)
|
||||
buffer.seek(0)
|
||||
user_id = order['user_id']
|
||||
if user_data['user_role'] == 'company_user':
|
||||
user_id = user_data['company_id']
|
||||
if save_to_disk:
|
||||
save_path=f"generated_pdfs/order_{order['user_id']}_{order['order_number']}.pdf"
|
||||
save_path=f"generated_pdfs/order_{user_id}_{order['order_number']}.pdf"
|
||||
with open(save_path, "wb") as f:
|
||||
f.write(buffer.getvalue())
|
||||
|
||||
return buffer
|
||||
return buffer
|
||||
|
||||
|
||||
Reference in New Issue
Block a user