Spaces:
Sleeping
Sleeping
Update data_collector.py
Browse files- data_collector.py +50 -53
data_collector.py
CHANGED
@@ -54,58 +54,49 @@ def get_data(b_id,product_name):
|
|
54 |
|
55 |
# Define the SQL SELECT query
|
56 |
sql_query = f"""
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
67 |
-
|
68 |
-
|
69 |
-
|
70 |
-
|
71 |
-
|
72 |
-
|
73 |
-
|
74 |
-
|
75 |
-
|
76 |
-
|
77 |
-
|
78 |
-
|
79 |
-
|
80 |
-
|
81 |
-
|
82 |
-
|
83 |
-
|
84 |
-
|
85 |
-
|
86 |
-
|
87 |
-
|
88 |
-
|
89 |
-
|
90 |
-
|
91 |
-
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
|
97 |
-
|
98 |
-
|
99 |
-
|
100 |
-
LEFT JOIN `transaction_sell_lines` AS `tsl`
|
101 |
-
ON `transaction_sell_lines`.`parent_sell_line_id` = `tsl`.`id`
|
102 |
-
WHERE `t`.`type` = 'sell'
|
103 |
-
AND `t`.`status` = 'final'
|
104 |
-
AND `t`.`business_id` = {b_id}
|
105 |
-
GROUP BY `b`.`id`,
|
106 |
-
`transaction_sell_lines`.`id`;
|
107 |
-
"""
|
108 |
-
|
109 |
# Execute the SQL query
|
110 |
cursor.execute(sql_query)
|
111 |
|
@@ -126,7 +117,13 @@ def get_data(b_id,product_name):
|
|
126 |
connection.close()
|
127 |
|
128 |
# Create a DataFrame
|
129 |
-
columns = [
|
|
|
|
|
|
|
|
|
|
|
|
|
130 |
df = pd.DataFrame(results, columns=columns)
|
131 |
return df,"done"
|
132 |
|
|
|
54 |
|
55 |
# Define the SQL SELECT query
|
56 |
sql_query = f"""
|
57 |
+
SELECT
|
58 |
+
b.id AS business_id,
|
59 |
+
b.name AS business_name,
|
60 |
+
p.name AS product_name,
|
61 |
+
p.type AS product_type,
|
62 |
+
c1.name AS category_name,
|
63 |
+
br.name AS brand_name,
|
64 |
+
p.image AS product_image,
|
65 |
+
pv.name AS product_variation,
|
66 |
+
v.name AS variation_name,
|
67 |
+
v.sub_sku,
|
68 |
+
c.name AS customer,
|
69 |
+
c.contact_id,
|
70 |
+
t.id AS transaction_id,
|
71 |
+
t.invoice_no,
|
72 |
+
t.transaction_date AS transaction_date,
|
73 |
+
(transaction_sell_lines.quantity - transaction_sell_lines.quantity_returned) AS sell_qty,
|
74 |
+
u.short_name AS unit,
|
75 |
+
transaction_sell_lines.unit_price_inc_tax,
|
76 |
+
transaction_sell_lines.unit_price_before_discount
|
77 |
+
FROM
|
78 |
+
transaction_sell_lines
|
79 |
+
INNER JOIN transactions AS t ON transaction_sell_lines.transaction_id = t.id
|
80 |
+
INNER JOIN variations AS v ON transaction_sell_lines.variation_id = v.id
|
81 |
+
LEFT JOIN transaction_sell_lines_purchase_lines AS tspl ON transaction_sell_lines.id = tspl.sell_line_id
|
82 |
+
LEFT JOIN purchase_lines AS pl ON tspl.purchase_line_id = pl.id
|
83 |
+
INNER JOIN product_variations AS pv ON v.product_variation_id = pv.id
|
84 |
+
INNER JOIN contacts AS c ON t.contact_id = c.id
|
85 |
+
INNER JOIN products AS p ON pv.product_id = p.id
|
86 |
+
LEFT JOIN business AS b ON p.business_id = b.id
|
87 |
+
LEFT JOIN categories AS c1 ON p.category_id = c1.id
|
88 |
+
LEFT JOIN brands AS br ON p.brand_id = br.id
|
89 |
+
LEFT JOIN tax_rates ON transaction_sell_lines.tax_id = tax_rates.id
|
90 |
+
LEFT JOIN units AS u ON p.unit_id = u.id
|
91 |
+
LEFT JOIN transaction_payments AS tp ON tp.transaction_id = t.id
|
92 |
+
LEFT JOIN transaction_sell_lines AS tsl ON transaction_sell_lines.parent_sell_line_id = tsl.id
|
93 |
+
WHERE
|
94 |
+
t.type = 'sell'
|
95 |
+
AND t.status = 'final'
|
96 |
+
AND t.business_id = {b_id}
|
97 |
+
GROUP BY
|
98 |
+
b.id, transaction_sell_lines.id;
|
99 |
+
"""
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
100 |
# Execute the SQL query
|
101 |
cursor.execute(sql_query)
|
102 |
|
|
|
117 |
connection.close()
|
118 |
|
119 |
# Create a DataFrame
|
120 |
+
columns = [
|
121 |
+
"business_id", "business_name", "product_name", "product_type",
|
122 |
+
"category_name", "brand_name", "product_image", "product_variation",
|
123 |
+
"variation_name", "sub_sku", "customer", "contact_id",
|
124 |
+
"transaction_id", "invoice_no", "transaction_date", "sell_qty",
|
125 |
+
"unit", "unit_price_inc_tax", "unit_price_before_discount"
|
126 |
+
]
|
127 |
df = pd.DataFrame(results, columns=columns)
|
128 |
return df,"done"
|
129 |
|