194 lines
9.7 KiB
Python
194 lines
9.7 KiB
Python
|
import mysql.connector as mariadb
|
||
|
|
||
|
from config import database
|
||
|
from scripts import loggorilla
|
||
|
|
||
|
class inventory:
|
||
|
|
||
|
def __init__(self):
|
||
|
self.db_main = mariadb.connect(**database.db_main)
|
||
|
self.cursor = self.db_main.cursor(dictionary=True)
|
||
|
|
||
|
def list(self, params):
|
||
|
APIADDR = "/api/invlab/inventory/list"
|
||
|
response = {}
|
||
|
self.cursor.execute("BEGIN;")
|
||
|
try:
|
||
|
ls = []
|
||
|
self.cursor.execute("SELECT item.id AS item_id, item.name AS item_name, SUM(CASE WHEN inv.flow = 'sell' THEN price.value * inv.qty WHEN inv.flow = 'buy' THEN -price.value * inv.qty ELSE 0 END) AS profit, SUM(CASE WHEN inv.flow = 'buy' THEN inv.qty WHEN inv.flow = 'sell' THEN -inv.qty ELSE 0 END) AS current_qty FROM inventory inv LEFT JOIN item ON inv.item = item.id LEFT JOIN item_price price ON price.id = (SELECT sub.id FROM item_price sub WHERE sub.item = item.id AND sub.type = inv.flow AND sub.periods <= inv.when ORDER BY sub.periods DESC LIMIT 1) GROUP BY item.id, item.name ORDER BY item.name ASC;")
|
||
|
l1 = self.cursor.fetchall()
|
||
|
c1 = 0
|
||
|
for d1 in l1:
|
||
|
ls.append({
|
||
|
"item":{
|
||
|
"id" : d1["item_id" ],
|
||
|
"name" : d1["item_name" ]
|
||
|
},
|
||
|
"qty" : int(d1["current_qty" ]),
|
||
|
"profit" : int(d1["profit" ]),
|
||
|
"history" : []
|
||
|
})
|
||
|
self.cursor.execute("SELECT inv.id, inv.flow, DATE_FORMAT(inv.when, '%Y-%m-%d %H:%i:%S') AS `when`, DATE_FORMAT(price.periods, '%Y-%m-%d %H:%i:%S') AS price_periods, price.value AS price_value, inv.qty, (price.value * inv.qty) AS `total` FROM inventory inv LEFT JOIN item ON inv.item = item.id LEFT JOIN item_price price ON price.id = (SELECT sub.id FROM item_price sub WHERE sub.item = item.id AND sub.type = inv.flow AND sub.periods <= inv.when ORDER BY sub.periods DESC LIMIT 1 ) WHERE item.id = %s ORDER BY inv.when DESC; ", ( d1["item_id"], ) )
|
||
|
l2 = self.cursor.fetchall()
|
||
|
c2 = 0
|
||
|
for d2 in l2:
|
||
|
ls[c1]["history"].append({
|
||
|
"id" : d2["id" ],
|
||
|
"flow" : d2["flow" ],
|
||
|
"when" : d2["when" ],
|
||
|
"price" : {
|
||
|
"periods" : d2["price_periods" ],
|
||
|
"value" : d2["price_value" ]
|
||
|
},
|
||
|
"qty" : d2["qty" ],
|
||
|
"total" : d2["total" ]
|
||
|
})
|
||
|
c2+=2
|
||
|
c1+=1
|
||
|
loggorilla.prcss(APIADDR, "Giving response")
|
||
|
response["status" ] = "success"
|
||
|
response["desc" ] = "data collected"
|
||
|
response["data" ] = ls
|
||
|
except Exception as e:
|
||
|
self.cursor.execute("ROLLBACK;")
|
||
|
loggorilla.error(APIADDR, str(e) )
|
||
|
loggorilla.prcss(APIADDR, "Giving response")
|
||
|
response["status" ] = "failed"
|
||
|
response["desc" ] = "Internal Server Error. Please contact us if you still have an error."
|
||
|
finally:
|
||
|
self.cursor.execute("COMMIT;")
|
||
|
self.cursor.close()
|
||
|
self.db_main.close()
|
||
|
return response
|
||
|
|
||
|
def detail(self, params):
|
||
|
APIADDR = "/api/invlab/inventory/detail"
|
||
|
response = {}
|
||
|
loggorilla.prcss(APIADDR, "Define parameters")
|
||
|
id = params["id"]
|
||
|
self.cursor.execute("BEGIN;")
|
||
|
try:
|
||
|
self.cursor.execute("SELECT item.id AS item_id, item.name AS item_name, SUM(CASE WHEN inv.flow = 'sell' THEN price.value * inv.qty WHEN inv.flow = 'buy' THEN -price.value * inv.qty ELSE 0 END) AS profit, SUM(CASE WHEN inv.flow = 'buy' THEN inv.qty WHEN inv.flow = 'sell' THEN -inv.qty ELSE 0 END) AS current_qty FROM inventory inv LEFT JOIN item ON inv.item = item.id LEFT JOIN item_price price ON price.id = (SELECT sub.id FROM item_price sub WHERE sub.item = item.id AND sub.type = inv.flow AND sub.periods <= inv.when ORDER BY sub.periods DESC LIMIT 1) WHERE item.id = %s GROUP BY item.id, item.name ORDER BY item.name ASC;", ( id, ) )
|
||
|
d1 = self.cursor.fetchone()
|
||
|
data = {
|
||
|
"item":{
|
||
|
"id" : d1["item_id" ],
|
||
|
"name" : d1["item_name" ]
|
||
|
},
|
||
|
"qty" : int(d1["current_qty" ]),
|
||
|
"profit" : int(d1["profit" ]),
|
||
|
"history" : []
|
||
|
}
|
||
|
self.cursor.execute("SELECT inv.id, inv.flow, DATE_FORMAT(inv.when, '%Y-%m-%d %H:%i:%S') AS `when`, DATE_FORMAT(price.periods, '%Y-%m-%d %H:%i:%S') AS price_periods, price.value AS price_value, inv.qty, (price.value * inv.qty) AS `total` FROM inventory inv LEFT JOIN item ON inv.item = item.id LEFT JOIN item_price price ON price.id = (SELECT sub.id FROM item_price sub WHERE sub.item = item.id AND sub.type = inv.flow AND sub.periods <= inv.when ORDER BY sub.periods DESC LIMIT 1 ) WHERE item.id = %s ORDER BY inv.when DESC; ", ( d1["item_id"], ) )
|
||
|
l2 = self.cursor.fetchall()
|
||
|
c2 = 0
|
||
|
for d2 in l2:
|
||
|
data["history"].append({
|
||
|
"id" : d2["id" ],
|
||
|
"flow" : d2["flow" ],
|
||
|
"when" : d2["when" ],
|
||
|
"price" : {
|
||
|
"periods" : d2["price_periods" ],
|
||
|
"value" : d2["price_value" ]
|
||
|
},
|
||
|
"qty" : d2["qty" ],
|
||
|
"total" : d2["total" ]
|
||
|
})
|
||
|
c2+=2
|
||
|
loggorilla.prcss(APIADDR, "Giving response")
|
||
|
response["status" ] = "success"
|
||
|
response["desc" ] = "data collected"
|
||
|
response["data" ] = data
|
||
|
except Exception as e:
|
||
|
self.cursor.execute("ROLLBACK;")
|
||
|
loggorilla.error(APIADDR, str(e) )
|
||
|
loggorilla.prcss(APIADDR, "Giving response")
|
||
|
response["status" ] = "failed"
|
||
|
response["desc" ] = "Internal Server Error. Please contact us if you still have an error."
|
||
|
finally:
|
||
|
self.cursor.execute("COMMIT;")
|
||
|
self.cursor.close()
|
||
|
self.db_main.close()
|
||
|
return response
|
||
|
|
||
|
def add(self, params):
|
||
|
APIADDR = "/api/invlab/inventory/add"
|
||
|
response = {}
|
||
|
loggorilla.prcss(APIADDR, "Define parameters")
|
||
|
item = params["item" ]
|
||
|
flow = params["flow" ]
|
||
|
when = params["when" ]
|
||
|
qty = params["qty" ]
|
||
|
note = params["note" ]
|
||
|
self.cursor.execute("BEGIN;")
|
||
|
try:
|
||
|
loggorilla.prcss(APIADDR, "Inserting")
|
||
|
self.cursor.execute("INSERT INTO `inventory` VALUES (DEFAULT, %s, %s, %s, %s, %s) ;", (item, flow, when, qty, note) )
|
||
|
loggorilla.prcss(APIADDR, "Giving response")
|
||
|
response["status" ] = "success"
|
||
|
response["desc" ] = "data added"
|
||
|
except Exception as e:
|
||
|
self.cursor.execute("ROLLBACK;")
|
||
|
loggorilla.error(APIADDR, str(e) )
|
||
|
loggorilla.prcss(APIADDR, "Giving response")
|
||
|
response["status" ] = "failed"
|
||
|
response["desc" ] = "Internal Server Error. Please contact us if you still have an error."
|
||
|
finally:
|
||
|
self.cursor.execute("COMMIT;")
|
||
|
self.cursor.close()
|
||
|
self.db_main.close()
|
||
|
return response
|
||
|
|
||
|
def edit(self, params):
|
||
|
APIADDR = "/api/invlab/inventory/edit"
|
||
|
response = {}
|
||
|
loggorilla.prcss(APIADDR, "Define parameters")
|
||
|
key = params["id" ]
|
||
|
item = params["item" ]
|
||
|
flow = params["flow" ]
|
||
|
when = params["when" ]
|
||
|
qty = params["qty" ]
|
||
|
note = params["note" ]
|
||
|
self.cursor.execute("BEGIN;")
|
||
|
try:
|
||
|
loggorilla.prcss(APIADDR, "Updating")
|
||
|
self.cursor.execute("UPDATE `inventory` SET `item` = %s, `flow` = %s, `when` = %s, `qty` = %s, `note` = %s WHERE `id` = %s ;", (item, flow, when, qty, note, key) )
|
||
|
loggorilla.prcss(APIADDR, "Giving response")
|
||
|
response["status" ] = "success"
|
||
|
response["desc" ] = "data change"
|
||
|
except Exception as e:
|
||
|
self.cursor.execute("ROLLBACK;")
|
||
|
loggorilla.error(APIADDR, str(e) )
|
||
|
loggorilla.prcss(APIADDR, "Giving response")
|
||
|
response["status" ] = "failed"
|
||
|
response["desc" ] = "Internal Server Error. Please contact us if you still have an error."
|
||
|
finally:
|
||
|
self.cursor.execute("COMMIT;")
|
||
|
self.cursor.close()
|
||
|
self.db_main.close()
|
||
|
return response
|
||
|
|
||
|
def remove(self, params):
|
||
|
APIADDR = "/api/invlab/inventory/remove"
|
||
|
response = {}
|
||
|
loggorilla.prcss(APIADDR, "Define parameters")
|
||
|
key = params["id" ]
|
||
|
self.cursor.execute("BEGIN;")
|
||
|
try:
|
||
|
loggorilla.prcss(APIADDR, "Deleting")
|
||
|
self.cursor.execute("DELETE FROM `inventory` WHERE `id` = %s ;", (key,) )
|
||
|
loggorilla.prcss(APIADDR, "Giving response")
|
||
|
response["status" ] = "success"
|
||
|
response["desc" ] = "data removed"
|
||
|
except Exception as e:
|
||
|
self.cursor.execute("ROLLBACK;")
|
||
|
loggorilla.error(APIADDR, str(e) )
|
||
|
loggorilla.prcss(APIADDR, "Giving response")
|
||
|
response["status" ] = "failed"
|
||
|
response["desc" ] = "Internal Server Error. Please contact us if you still have an error."
|
||
|
finally:
|
||
|
self.cursor.execute("COMMIT;")
|
||
|
self.cursor.close()
|
||
|
self.db_main.close()
|
||
|
return response
|