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 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 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()