minor: fixed amount, amount increase and decrease
Added the possibility to scan a fixed amount (1, 2, 5 or 10) so you don't have to scan the same item over and over again. Also, you can increase the amount by +x and -x, depending on what code you scan.
This commit is contained in:
parent
f37cfbdaaa
commit
f137400399
@ -34,9 +34,14 @@ CODE128 = data['barcode']['codeid']['Code128'] if data and 'barcode' in data and
|
|||||||
EAN8 = data['barcode']['codeid']['EAN8'] if data and 'barcode' in data and 'codeid' in data['barcode'] and 'EAN8' in data['barcode']['codeid'] else "C"
|
EAN8 = data['barcode']['codeid']['EAN8'] if data and 'barcode' in data and 'codeid' in data['barcode'] and 'EAN8' in data['barcode']['codeid'] else "C"
|
||||||
EAN13 = data['barcode']['codeid']['EAN13'] if data and 'barcode' in data and 'codeid' in data['barcode'] and 'EAN13' in data['barcode']['codeid'] else "D"
|
EAN13 = data['barcode']['codeid']['EAN13'] if data and 'barcode' in data and 'codeid' in data['barcode'] and 'EAN13' in data['barcode']['codeid'] else "D"
|
||||||
|
|
||||||
|
OFFLINE_LOGIN = data['users'] if "users" in data else ""
|
||||||
|
|
||||||
|
del(data)
|
||||||
|
|
||||||
TEMPFILE = "scans.json"
|
TEMPFILE = "scans.json"
|
||||||
|
|
||||||
TIMEOUT = 60 # Number of seconds for a timeout after being logged in
|
TIMEOUT = 60 # Number of seconds for a timeout after being logged in
|
||||||
|
SET_AMOUNTS = ["1", "2", "5", "10"]
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
@ -49,49 +54,75 @@ def main() -> None:
|
|||||||
LOGGER.debug("Login successful")
|
LOGGER.debug("Login successful")
|
||||||
scanning(user)
|
scanning(user)
|
||||||
|
|
||||||
def delete(scanned: list):
|
def delete(scanned: list[dict[int: int]]):
|
||||||
i, _, _ = timedInput([stdin], [], [], TIMEOUT)
|
amount = 1
|
||||||
if not i:
|
while True:
|
||||||
return #TODO send a short timeout message before return
|
i, _, _ = timedInput([stdin], [], [], TIMEOUT)
|
||||||
scan = stdin.readline().strip()
|
if not i:
|
||||||
codeid, scan = split_codeid(scan, "")
|
return #TODO send a short timeout message before return
|
||||||
match scan:
|
scan = stdin.readline().strip()
|
||||||
case "delete":
|
codeid, scan = split_codeid(scan, "")
|
||||||
try:
|
match codeid:
|
||||||
scanned.pop()
|
case str(EAN8):
|
||||||
except IndexError as e:
|
try:
|
||||||
LOGGER.exception()
|
scanned.remove({scan: amount})
|
||||||
case _:
|
except ValueError as e:
|
||||||
try:
|
scanned.insert(0, {scan: -amount})
|
||||||
scanned.remove(scan)
|
LOGGER.debug(f"Tried to delete {scan} with amount {amount}.")
|
||||||
except ValueError as e:
|
finally:
|
||||||
LOGGER.exception()
|
break
|
||||||
|
case str(EAN13):
|
||||||
|
try:
|
||||||
|
scanned.remove({scan: amount})
|
||||||
|
except ValueError as e:
|
||||||
|
scanned.insert(0, {scan: -amount})
|
||||||
|
LOGGER.debug(f"Tried to delete {scan} with amount {amount}.")
|
||||||
|
finally:
|
||||||
|
break
|
||||||
|
case str(CODE128):
|
||||||
|
match scan:
|
||||||
|
case "delete":
|
||||||
|
try:
|
||||||
|
scanned.pop()
|
||||||
|
except IndexError as e:
|
||||||
|
LOGGER.exception()
|
||||||
|
finally:
|
||||||
|
break
|
||||||
|
case _:
|
||||||
|
try:
|
||||||
|
amount += int(scan)
|
||||||
|
except ValueError as e:
|
||||||
|
LOGGER.exception()
|
||||||
|
|
||||||
def group_scanning(scanned: list[int]) -> dict[int: int]:
|
def group_previous_scans(previous_scans: list):
|
||||||
scan_dict = {}
|
newscans = []
|
||||||
for scan in scanned:
|
for scan in previous_scans:
|
||||||
if scan not in scan_dict:
|
|
||||||
scan_dict[scan] = 1
|
|
||||||
else:
|
|
||||||
scan_dict[scan] += 1
|
|
||||||
return scan_dict
|
|
||||||
|
|
||||||
def group_temp(TEMP: list):
|
|
||||||
NEWTEMP = []
|
|
||||||
for temp in TEMP:
|
|
||||||
found = False
|
found = False
|
||||||
for newtemp in NEWTEMP:
|
for newscan in newscans:
|
||||||
if newtemp['date'] == temp['date'] and newtemp['user'] == temp['user']:
|
if newscan['date'] == scan['date'] and newscan['user'] == scan['user']:
|
||||||
for key, value in temp['items'].items():
|
for key, value in scan['items'].items():
|
||||||
if key in newtemp['items']:
|
if key in newscan['items']:
|
||||||
newtemp['items'][key] += value
|
newscan['items'][key] += value
|
||||||
else:
|
else:
|
||||||
newtemp['items'][key] = value
|
newscan['items'][key] = value
|
||||||
found = True
|
found = True
|
||||||
break
|
break
|
||||||
if not found:
|
if not found:
|
||||||
NEWTEMP.append(deepcopy(temp))
|
newscans.append(deepcopy(scan))
|
||||||
return NEWTEMP
|
return newscans
|
||||||
|
|
||||||
|
def group_scanning(scanned: list[dict[int: int]]) -> dict[int: int]:
|
||||||
|
scan_dict = {}
|
||||||
|
for scan in scanned:
|
||||||
|
for key, value in scan.items():
|
||||||
|
if key not in scan_dict:
|
||||||
|
scan_dict[scan] = value
|
||||||
|
else:
|
||||||
|
scan_dict[scan] += value
|
||||||
|
for key, value in scan_dict.items():
|
||||||
|
if value <= 0:
|
||||||
|
del(scan_dict[key])
|
||||||
|
return scan_dict
|
||||||
|
|
||||||
def login(user: str = None):
|
def login(user: str = None):
|
||||||
if not user:
|
if not user:
|
||||||
@ -103,13 +134,14 @@ def login(user: str = None):
|
|||||||
return None
|
return None
|
||||||
if not connection.check_login(user):
|
if not connection.check_login(user):
|
||||||
LOGGER.debug("Login failed")
|
LOGGER.debug("Login failed")
|
||||||
if not ("users" in data and user in data['users']):
|
if not user in OFFLINE_LOGIN:
|
||||||
return None
|
return None
|
||||||
LOGGER.debug("Using local login")
|
LOGGER.debug("Using local login")
|
||||||
return user
|
return user
|
||||||
|
|
||||||
def scanning(user: str) -> dict[int: int]:
|
def scanning(user: str) -> dict[int: int]:
|
||||||
scan, scanned = "", []
|
scan, scanned = "", []
|
||||||
|
amount = 1
|
||||||
while True:
|
while True:
|
||||||
i, _, _ = timedInput([stdin], [], [], TIMEOUT)
|
i, _, _ = timedInput([stdin], [], [], TIMEOUT)
|
||||||
if not i:
|
if not i:
|
||||||
@ -118,9 +150,11 @@ def scanning(user: str) -> dict[int: int]:
|
|||||||
codeid, scan = split_codeid(scan, "A")
|
codeid, scan = split_codeid(scan, "A")
|
||||||
match codeid:
|
match codeid:
|
||||||
case str(EAN8):
|
case str(EAN8):
|
||||||
scanned.append(scan)
|
scanned.append({scan: amount})
|
||||||
|
amount = 1
|
||||||
case str(EAN13):
|
case str(EAN13):
|
||||||
scanned.append(scan)
|
scanned.append({scan: amount})
|
||||||
|
amount = 1
|
||||||
case str(CODE128):
|
case str(CODE128):
|
||||||
match scan:
|
match scan:
|
||||||
case "logout":
|
case "logout":
|
||||||
@ -128,38 +162,44 @@ def scanning(user: str) -> dict[int: int]:
|
|||||||
case "delete":
|
case "delete":
|
||||||
delete(scanned)
|
delete(scanned)
|
||||||
case _:
|
case _:
|
||||||
altuser = login(scan)
|
try:
|
||||||
if not altuser:
|
if scan in SET_AMOUNTS:
|
||||||
continue
|
amount = int(scan)
|
||||||
LOGGER.debug("Login successful")
|
else:
|
||||||
scanning(altuser)
|
amount += int(scan)
|
||||||
break
|
except:
|
||||||
|
altuser = login(scan)
|
||||||
|
if not altuser:
|
||||||
|
continue
|
||||||
|
LOGGER.debug("Login successful")
|
||||||
|
scanning(altuser)
|
||||||
|
break
|
||||||
case _:
|
case _:
|
||||||
LOGGER.debug(f"Unknown barcode scanned: {codeid}_{scan}")
|
LOGGER.debug(f"Unknown barcode scanned: {codeid}_{scan}")
|
||||||
scanned = group_scanning(scanned)
|
scanned = group_scanning(scanned)
|
||||||
send_scan(user, scanned)
|
send_scan(user, scanned)
|
||||||
|
|
||||||
def send_scan(user: str, scanned: list, temp: list[dict] = []):
|
def send_scan(user: str, scanned: dict[int: int], previous_scans: list[dict] = []):
|
||||||
if exists(TEMPFILE):
|
if exists(TEMPFILE):
|
||||||
with open(TEMPFILE, "r") as file:
|
with open(TEMPFILE, "r") as file:
|
||||||
temp.extend(jload(file))
|
previous_scans.extend(jload(file))
|
||||||
result = connection.send_scan(user, scanned)
|
result = connection.send_scan(user, scanned)
|
||||||
if result != True:
|
if result != True:
|
||||||
result['date'] = str(date.today())
|
result['date'] = str(date.today())
|
||||||
temp.append(result)
|
previous_scans.append(result)
|
||||||
if temp:
|
if previous_scans:
|
||||||
temp = group_temp(temp)
|
previous_scans = group_previous_scans(previous_scans)
|
||||||
for bought in list(temp):
|
for bought in list(previous_scans):
|
||||||
result = connection.send_scan(bought['user'], bought['items'], bought['date'])
|
result = connection.send_scan(bought['user'], bought['items'], bought['date'])
|
||||||
temp.remove(bought)
|
previous_scans.remove(bought)
|
||||||
if result != True:
|
if result != True:
|
||||||
temp.append(result)
|
previous_scans.append(result)
|
||||||
if temp: # if temp still present, save it
|
if previous_scans: # if previous scans still present, save it
|
||||||
with open(TEMPFILE, "w") as file:
|
with open(TEMPFILE, "w") as file:
|
||||||
jdump(temp, file)
|
jdump(previous_scans, file)
|
||||||
elif exists(TEMPFILE):
|
elif exists(TEMPFILE): # if no scans remain, delete the json
|
||||||
remove(TEMPFILE)
|
remove(TEMPFILE)
|
||||||
LOGGER.info(temp)
|
LOGGER.info(previous_scans)
|
||||||
|
|
||||||
def split_codeid(scan: str, default_codeid: str = ""):
|
def split_codeid(scan: str, default_codeid: str = ""):
|
||||||
match CODEID_POS:
|
match CODEID_POS:
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user