Skip to content

Instantly share code, notes, and snippets.

@jtallieu
Created August 15, 2014 17:43
Show Gist options
  • Save jtallieu/e6a209a436465d0743e4 to your computer and use it in GitHub Desktop.
Save jtallieu/e6a209a436465d0743e4 to your computer and use it in GitHub Desktop.
Python script using the POSSync python VendAPI to generate javascript that can be used in a Firebug console tol void a large number of orders for a specific register https://github.com/xoho/vendhq-api-python
"""
This script will generate the necessary Javascript
to void CLOSED orders from a specific Register. The javascript
is intended to run in a Firebug console.
Nahnay Software, LLC
Makers of www.pos-sync.com
Authors:
------------
Joey Tallieu joey@nahnay.com
Eric Hoxworth eric@nahnay.com
Copyright 2014
This code is free to reuse as you want.
"""
import sys, os
import logging
import json
from pprint import pprint, pformat
from datetime import datetime
from csv import DictWriter
# Depends on Nahnay's python API for Vend
# https://github.com/xoho/vendhq-api-python
from VendHQ.api import ApiClient
logging.basicConfig(level=logging.INFO,
stream=sys.stdout,
format='%(asctime)s %(levelname)-8s[%(name)s] %(message)s',
datefmt='%m/%d %H:%M:%S')
log = logging.getLogger("main")
logging.getLogger("requests").setLevel(logging.DEBUG)
"""
Configuration section
"""
OUTLET = "Main Outlet"
REGISTER = "Register #2"
HOST = "nahnay.vendhq.com"
USERNAME = "----------"
PASSWORD = "-----------"
# Get orders since 2014-8-1 00:00:00
SINCE = datetime(2014,4,1,0,0,0)
# Only list CLOSED orders
STATUS = ""
def get_outlet(api, outlet):
"""
Get the outlet ID for the name given
"""
for _outlet in api.Outlets.enumerate():
if _outlet.name.lower() == outlet.strip().lower():
return _outlet.id
return _outlet.id
def get_register(api, name, outlet_id=None):
"""
Flips through the registers looking for the
register name in the outlet id given
"""
reg = None
for register in api.Registers.enumerate():
if not reg: reg=register
if register.name.lower()==name.strip().lower():
found = True
if outlet_id and not register.outlet_id==outlet_id:
found = False
if found:
reg = register
break
return reg.id if reg else None
if __name__ == "__main__":
ids = []
ids_to_void = []
api = ApiClient(HOST, USERNAME, PASSWORD)
outlet_id = get_outlet(api, OUTLET)
log.info("Outlet id %s" % outlet_id)
register_id = get_register(api, REGISTER, outlet_id)
log.info("Register id %s" % register_id)
q = {}
q['since'] = SINCE
q["outlet_id"] = outlet_id
if STATUS:
q["status"] = STATUS
orders_ = open("orders.csv", "wb")
ords = DictWriter(orders_, ["id", "status", "invoice_no", "register" ,"note"], extrasaction='ignore')
ords.writeheader()
count = 0
for o in api.Register_Sales.enumerate(query=q):
count += 1
# Check for duplicates - see issue with VEND API's sequencing
if o.id in ids:
log.info("---- %d - duplicate %s" % (count, o.id))
continue
else:
ids.append(o.id)
# Sanity check to make sure we have an order for the right register
if o.register_id == register_id:
log.info("<%d> %s (%s)" %(count, o.id, o.status))
ords.writerow({"id": o.id, "status": o.status,"invoice_no": o.invoice_number, "register": o.register_id, "note": o.note})
ids_to_void.append('"%s"' % o.id)
orders_.close()
# Generate the JS
jsfunc = open("_void.js", "r").read()
js = open("void_orders.js", "w")
js.write("var ids=[%s];\n" % ",".join(ids_to_void))
js.write(jsfunc)
js.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment