Beer consumption report done
This commit is contained in:
@ -364,6 +364,7 @@ def includeme(config):
|
||||
|
||||
config.add_route("v1_product_sale_report", "/v1/product-sale-report")
|
||||
config.add_route("v1_bill_settlement_report", "/v1/bill-settlement-report")
|
||||
config.add_route("v1_beer_consumption_report", "/v1/beer-consumption-report")
|
||||
# Done till here
|
||||
|
||||
config.add_route("customer", "/Customer.json")
|
||||
@ -402,6 +403,5 @@ def includeme(config):
|
||||
|
||||
# config.add_route('customer_id', '/Customer/{id}.json')
|
||||
|
||||
config.add_route("beer_consumption", "/BeerConsumption.json")
|
||||
|
||||
config.add_static_view("", "barker:static")
|
||||
|
||||
@ -1,42 +1,64 @@
|
||||
import datetime
|
||||
from datetime import datetime, timedelta
|
||||
|
||||
from pyramid.httpexceptions import HTTPForbidden
|
||||
from pyramid.view import view_config
|
||||
from sqlalchemy import func
|
||||
|
||||
from barker.models import Inventory, Kot, Product, Settlement, SettleOption, Voucher
|
||||
from barker.models import Inventory, Kot, Product, Voucher, VoucherType
|
||||
from barker.models.validation_exception import ValidationError
|
||||
from barker.views.reports import get_start_date, get_finish_date
|
||||
|
||||
|
||||
@view_config(request_method='GET', route_name='beer_consumption', renderer='json', permission='Beer Consumption',
|
||||
request_param=('s', 'f'))
|
||||
@view_config(
|
||||
request_method="GET",
|
||||
route_name="v1_beer_consumption_report",
|
||||
renderer="json",
|
||||
permission="Beer Consumption",
|
||||
)
|
||||
def beer_consumption(request):
|
||||
start_date = datetime.datetime.strptime(request.GET['s'], '%d-%b-%Y %H:%M')
|
||||
finish_date = datetime.datetime.strptime(request.GET['f'], '%d-%b-%Y %H:%M')
|
||||
start_date = get_start_date(request.GET.get("s", None)) - timedelta(hours=7)
|
||||
finish_date = get_finish_date(request.GET.get("f", None)) - timedelta(hours=7)
|
||||
|
||||
if (datetime.date.today() - start_date.date()).days > 5 and 'Accounts Audit' not in request.effective_principals:
|
||||
raise HTTPForbidden("Accounts Audit")
|
||||
if (
|
||||
datetime.today() - start_date.replace(hour=0)
|
||||
).days > 5 and "Accounts Audit" not in request.effective_principals:
|
||||
raise ValidationError("Accounts Audit")
|
||||
|
||||
list = request.dbsession.query(
|
||||
Voucher.date, Product.id, Product.name, func.sum(Inventory.quantity * Product.quantity)
|
||||
).join(Voucher.kots).join(Kot.inventories).join(Inventory.product).filter(
|
||||
Voucher.date >= start_date,
|
||||
Voucher.date <= finish_date,
|
||||
Voucher.is_void == False,
|
||||
Voucher.settlements.any(Settlement.settled.in_([
|
||||
SettleOption.CASH(),
|
||||
SettleOption.CREDIT_CARD(),
|
||||
SettleOption.BILL_TO_COMPANY(),
|
||||
SettleOption.STAFF(),
|
||||
SettleOption.NO_CHARGE()
|
||||
]))
|
||||
).group_by(
|
||||
Voucher.date, Product.id, Product.name
|
||||
).order_by(
|
||||
Voucher.date, Product.id, Product.name
|
||||
).all()
|
||||
return [{
|
||||
'Date': i[0].strftime('%d-%b-%Y %H:%M:%S'),
|
||||
'ProductID': str(i[1]),
|
||||
'Name': i[2],
|
||||
'Quantity': i[3]
|
||||
} for i in list if i[3] != 0]
|
||||
day = func.date_trunc("day", Voucher.date - timedelta(hours=7)).label("day")
|
||||
sum_ = func.sum(Inventory.quantity * Product.quantity).label("sum")
|
||||
list_ = (
|
||||
request.dbsession.query(day, Product.name, sum_)
|
||||
.join(Voucher.kots)
|
||||
.join(Kot.inventories)
|
||||
.join(Inventory.product)
|
||||
.filter(
|
||||
day >= start_date,
|
||||
day <= finish_date,
|
||||
Voucher.voucher_type.in_(
|
||||
[
|
||||
VoucherType.REGULAR_BILL.value,
|
||||
VoucherType.NO_CHARGE.value,
|
||||
VoucherType.STAFF.value,
|
||||
]
|
||||
),
|
||||
)
|
||||
.group_by(day, Product.name)
|
||||
.having(sum_ != 0)
|
||||
.order_by(day, Product.name)
|
||||
.all()
|
||||
)
|
||||
headers = []
|
||||
data = []
|
||||
for date, name, quantity in list_:
|
||||
if name not in headers:
|
||||
headers.append(name)
|
||||
old = [d for d in data if d["date"] == date.strftime("%d-%b-%Y")]
|
||||
if len(old):
|
||||
old[0][name] = quantity
|
||||
else:
|
||||
data.append({"date": date.strftime("%d-%b-%Y"), name: quantity})
|
||||
return {
|
||||
"startDate": start_date.date().strftime("%d-%b-%Y"),
|
||||
"finishDate": (finish_date - timedelta(days=1)).date().strftime("%d-%b-%Y"),
|
||||
"headers": headers,
|
||||
"data": data,
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user