def get_current_bill(req, response): j = yield get_tenant(req, response) tenant = j >> unpack tenant # just to thawrt PEP warning j = yield get_account(req, response) adef = j >> unpack end_date = datetime.datetime.utcnow() end_date = datetime.date(end_date.year, end_date.month, adef.bdom) start_date = subtract_one_month(end_date) br = gdata.bills(from_date=start_date, to_date=end_date, account_id=adef.id).fetch(1000) bill = None resp = None if len(br) > 0: bill = br[0] if bill is not None: bi = { "bill" : gdata.to_dict(bill), "billitems" : [] } iq = gdata.BillItem.all() iq.filter("bill_id = " , bill.id) ir = iq.fetch(1000) for item in ir: bi['billitems'].append(gdata.to_dict(item)) resp = bi response.set_status('200 OK') else: response.set_status('404 A bill Not Found') yield Just(resp)
def get_bill(req, response): j = yield get_tenant(req, response) tenant = j >> unpack tenant # just to thawrt PEP warning j = yield get_account(req, response) adef = j >> unpack r = gdata.bills(account_id=adef.id,id=req['bill_id']).fetch(1000) resp = None if len(r) == 1: bill = r[0] bi = { "bill" : gdata.to_dict(bill), "billitems" : [] } iq = gdata.BillItem.all() iq.filter("bill_id = " , bill.id) ir = iq.fetch(1000) for item in ir: bi['billitems'].append(gdata.to_dict(item)) resp = bi response.set_status('200 OK') else: response.set_status('404 A bill Not Found') yield Just(resp)
def delete_current_bill(req, response): j = yield get_tenant(req, response) tenant = j >> unpack tenant # just to thawrt PEP warning j = yield get_account(req, response) adef = j >> unpack end_date = datetime.datetime.utcnow() end_date = datetime.date(end_date.year, end_date.month, adef.bdom) start_date = subtract_one_month(end_date) br = gdata.bills(from_date=start_date,to_date=end_date,account_id=adef.id).fetch(1000) bill = None if len(br) > 0: bill = br[0] if bill is not None: delete_bill(bill.id) response.set_status('204 No Content') else: response.set_status('404 Current Bill Not Found') yield Nothing()
def delete_bill(bill_id): br = gdata.bills(id=bill_id).fetch(1000) if len(br) > 0: b = br[0] ir = gdata.billItems(bill_id=b.id).fetch(1000) for item in ir: sr = gdata.billItemSubBalances(bill_item_id=item.id).fetch(1000) for s in sr: logging.info("DELETING subbalance with id " + s.id) db.delete(s) logging.info("DELETING item with id " + item.id) db.delete(item) logging.info("DELETING bill with id " + b.id) db.delete(b)
def create_bill(req, response): j = yield get_tenant(req, response) tenant = j >> unpack tenant # just to thawrt PEP warning j = yield get_account(req, response) adef = j >> unpack end_date = datetime.datetime.utcnow() end_date = datetime.date(end_date.year, end_date.month, adef.bdom) start_date = subtract_one_month(end_date) br = gdata.bills(from_date=start_date, to_date=end_date, account_id=adef.id).fetch(1000) bill = None btot = 0 if len(br) > 0: bill = br[0] else: acct_attrs = {} if adef.attributes is not None: acct_attrs = json.loads(adef.attributes, parse_float=Decimal) bill = gdata.Bill(id=genid(), from_date=start_date, to_date=end_date, account_id=adef.id, bill_total_charge=str(btot)) balances = {} # TODO (btot, items, subbalances) = get_bill_items(bill.id, adef.account_no, adef.currency, acct_attrs, start_date, end_date, balances) for item in items: logging.info(gdata.to_dict(item)) item.put() for sb in subbalances: sb.put() bill.bill_total_charge = str(btot) bill.put() loc= "/tenants/"+req['tenant_id'] +"/accounts/"+req['account_no'] +"/bills/"+bill.id response.headers['Location'] = str(loc) response.set_status(201) yield Nothing()
def list_bills(req, response): j = yield get_tenant(req, response) tenant = j >> unpack tenant # just to thawrt PEP warning j = yield get_account(req, response) adef = j >> unpack r = gdata.bills(account_id=adef.id).fetch(1000) resp = { 'account_no' : adef.account_no, 'bills' : [] } for bill in r: bi = { "bill" : gdata.to_dict(bill), "billitems" : [] } ir = gdata.billItems(bill_id=bill.id).fetch(1000) for item in ir: bi['billitems'].append(gdata.to_dict(item)) resp['bills'].append(bi) response.set_status('200 OK') logging.info(resp) yield Just(resp)