Arafath10 commited on
Commit
aac1bde
1 Parent(s): a5c8045

Create data_collector.py

Browse files
Files changed (1) hide show
  1. data_collector.py +108 -0
data_collector.py ADDED
@@ -0,0 +1,108 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import mysql.connector
2
+ from decimal import Decimal
3
+ import pandas as pd
4
+ # Define the connection parameters
5
+ host = "159.138.104.192"
6
+ user = "storemate_ml"
7
+ password = "bTgZd77VpD^o4Ai6Dw9xs9"
8
+ database = "lite_version"
9
+
10
+
11
+ def get_data(b_id,product_name):
12
+ # Create a connection to the MySQL server
13
+ try:
14
+ # Create a connection to the MySQL server
15
+ connection = mysql.connector.connect(
16
+ host=host,
17
+ user=user,
18
+ password=password,
19
+ database=database
20
+ )
21
+
22
+ if connection.is_connected():
23
+ print("Connected to MySQL database")
24
+
25
+ # Create a cursor object for executing SQL queries
26
+ cursor = connection.cursor()
27
+
28
+ # Define the SQL SELECT query
29
+ sql_query = f"""
30
+ SELECT
31
+ `b`.`id` AS `business_id`,
32
+ `b`.`name` AS `business_name`,
33
+ `p`.`name` AS `product_name`,
34
+ `p`.`type` AS `product_type`,
35
+ `c1`.`name` AS `category_name`,
36
+ `pv`.`name` AS `product_variation`,
37
+ `v`.`name` AS `variation_name`,
38
+ `v`.`sub_sku`,
39
+ `c`.`name` AS `customer`,
40
+ `c`.`contact_id`,
41
+ `t`.`id` AS `transaction_id`,
42
+ `t`.`invoice_no`,
43
+ `t`.`transaction_date` AS `transaction_date`,
44
+ (transaction_sell_lines.quantity - transaction_sell_lines.quantity_returned) AS sell_qty,
45
+ `u`.`short_name` AS `unit`,
46
+ transaction_sell_lines.unit_price_inc_tax,
47
+ transaction_sell_lines.unit_price_before_discount
48
+ FROM `transaction_sell_lines`
49
+ INNER JOIN `transactions` AS `t`
50
+ ON `transaction_sell_lines`.`transaction_id` = `t`.`id`
51
+ INNER JOIN `variations` AS `v`
52
+ ON `transaction_sell_lines`.`variation_id` = `v`.`id`
53
+ LEFT JOIN `transaction_sell_lines_purchase_lines` AS `tspl`
54
+ ON `transaction_sell_lines`.`id` = `tspl`.`sell_line_id`
55
+ LEFT JOIN `purchase_lines` AS `pl`
56
+ ON `tspl`.`purchase_line_id` = `pl`.`id`
57
+ INNER JOIN `product_variations` AS `pv`
58
+ ON `v`.`product_variation_id` = `pv`.`id`
59
+ INNER JOIN `contacts` AS `c`
60
+ ON `t`.`contact_id` = `c`.`id`
61
+ INNER JOIN `products` AS `p`
62
+ ON `pv`.`product_id` = `p`.`id`
63
+ LEFT JOIN `business` AS `b`
64
+ ON `p`.`business_id` = `b`.`id`
65
+ LEFT JOIN `categories` AS `c1`
66
+ ON `p`.`category_id` = `c1`.`id`
67
+ LEFT JOIN `tax_rates`
68
+ ON `transaction_sell_lines`.`tax_id` = `tax_rates`.`id`
69
+ LEFT JOIN `units` AS `u`
70
+ ON `p`.`unit_id` = `u`.`id`
71
+ LEFT JOIN `transaction_payments` AS `tp`
72
+ ON `tp`.`transaction_id` = `t`.`id`
73
+ LEFT JOIN `transaction_sell_lines` AS `tsl`
74
+ ON `transaction_sell_lines`.`parent_sell_line_id` = `tsl`.`id`
75
+ WHERE `t`.`type` = 'sell'
76
+ AND `t`.`status` = 'final'
77
+ AND `t`.`business_id` = {b_id}
78
+ AND `p`.`name` = '{product_name}'
79
+ GROUP BY `b`.`id`,
80
+ `transaction_sell_lines`.`id`;
81
+ """
82
+
83
+ # Execute the SQL query
84
+ cursor.execute(sql_query)
85
+
86
+ # Fetch all the rows as a list of tuples
87
+ results = cursor.fetchall()
88
+ results = [tuple(
89
+ float(val) if isinstance(val, Decimal) else val for val in row
90
+ ) for row in results]
91
+
92
+ #print(results)
93
+
94
+ # Display the results
95
+ #for row in results:
96
+ #print(row) # You can process the results as needed
97
+
98
+ # Close the cursor and connection
99
+ cursor.close()
100
+ connection.close()
101
+
102
+ # Create a DataFrame
103
+ columns = ["business_id","business_name","product_name","product_type","category_name","product_variation","variation_name","sub_sku","customer","contact_id","transaction_id","invoice_no","transaction_date","sell_qty","unit","cost_price","selling_price"]
104
+ df = pd.DataFrame(results, columns=columns)
105
+ return df,"done"
106
+
107
+ except mysql.connector.Error as e:
108
+ return e,"error"