implement currency and report
This commit is contained in:
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]
|
||||
|
||||
Reference in New Issue
Block a user