Dates and times should now be handled properly once the server is set on UTC time and the proper timezone offset environment variable is set.

This commit is contained in:
Amritanshu Agrawal 2021-04-02 06:58:38 +05:30
parent 0da16e9548
commit 97579ea9d3
13 changed files with 104 additions and 78 deletions

View File

@ -94,8 +94,11 @@ def design_bill(
s += "\n\r"
# Products
date = voucher.date + timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)
s += "\n\r" + f"Bill No: {voucher.full_bill_id:>12} {date:%d-%b-%Y %H:%M}"
product_date = (
voucher.date + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES - settings.NEW_DAY_OFFSET_MINUTES)
).date()
voucher_date = voucher.date + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES)
s += "\n\r" + f"Bill No: {voucher.full_bill_id:>12} {voucher_date:%d-%b-%Y %H:%M}"
s += "\n\r" + "Table No.: " + voucher.food_table.name
s += "\n\r" + "-" * 42
s += "\n\r" + "Qty. Particulars Price Amount"
@ -108,13 +111,11 @@ def design_bill(
ProductVersion.product_id == item.product_id,
or_(
ProductVersion.valid_from == None, # noqa: E711
ProductVersion.valid_from
<= (voucher.date - timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)).date(),
ProductVersion.valid_from <= product_date,
),
or_(
ProductVersion.valid_till == None, # noqa: E711
ProductVersion.valid_till
>= (voucher.date - timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)).date(),
ProductVersion.valid_till >= product_date,
),
)
)

View File

@ -2,13 +2,14 @@ import asyncio
import locale
import uuid
from datetime import datetime
from datetime import datetime, timedelta
from arq import ArqRedis, create_pool
from barker.schemas.cashier_report import CashierReport
from sqlalchemy.orm import Session
from ..core.arq import settings as redis_settings
from ..core.config import settings
from ..models.device import Device
from ..models.printer import Printer
from ..models.section_printer import SectionPrinter
@ -36,7 +37,8 @@ def print_cashier_report(report: CashierReport, device_id: uuid.UUID, db: Sessio
def design_cashier_report(report: CashierReport):
s = f"{report.cashier.name} Checkout By {report.user.name} @ {datetime.now():%d-%b-%Y %H:%M}".center(42)
now = datetime.now() + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES)
s = f"{report.cashier.name} Checkout By {report.user.name} @ {now:%d-%b-%Y %H:%M}".center(42)
s += "\n\r" + "-" * 42
s += "\n\r" f"{report.start_date:%d-%b-%Y} To {report.finish_date:%d-%b-%Y}".center(42)
s += "\n\r" + "-" * 42

View File

@ -19,7 +19,10 @@ from ..models.voucher import Voucher
def design_kot(voucher: Voucher, kot: Kot, items: List[Inventory], copy_number: int, db: Session) -> str:
date = voucher.date + timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)
date_ = voucher.date + timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)
product_date = (
voucher.date + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES - settings.NEW_DAY_OFFSET_MINUTES)
).date()
s = (
"KOT / BOT".center(42)
+ "\n\r"
@ -27,7 +30,7 @@ def design_kot(voucher: Voucher, kot: Kot, items: List[Inventory], copy_number:
+ "\n\r"
+ "".ljust(42, "-")
+ "\n\r"
+ f"KOT ID : K-{voucher.kot_id:>5}/S-{kot.code:>5} {date:%d-%b-%Y %H:%M}"
+ f"KOT ID : K-{voucher.kot_id:>5}/S-{kot.code:>5} {date_:%d-%b-%Y %H:%M}"
+ "\n\r"
+ f"Table No.: {voucher.food_table.name}"
+ "\n\r"
@ -45,13 +48,11 @@ def design_kot(voucher: Voucher, kot: Kot, items: List[Inventory], copy_number:
ProductVersion.product_id == item.product_id,
or_(
ProductVersion.valid_from == None, # noqa: E711
ProductVersion.valid_from
<= (voucher.date - timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)).date(),
ProductVersion.valid_from <= product_date,
),
or_(
ProductVersion.valid_till == None, # noqa: E711
ProductVersion.valid_till
>= (voucher.date - timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)).date(),
ProductVersion.valid_till >= product_date,
),
)
)
@ -69,6 +70,9 @@ def print_kot(voucher_id: uuid.UUID, db: Session):
voucher: Voucher = db.query(Voucher).filter(Voucher.id == voucher_id).first()
my_hash = {}
kot: Kot = voucher.kots[-1]
product_date = (
voucher.date + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES - settings.NEW_DAY_OFFSET_MINUTES)
).date()
for item in kot.inventories:
product: ProductVersion = (
db.query(ProductVersion)
@ -77,13 +81,11 @@ def print_kot(voucher_id: uuid.UUID, db: Session):
ProductVersion.product_id == item.product_id,
or_(
ProductVersion.valid_from == None, # noqa: E711
ProductVersion.valid_from
<= (voucher.date - timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)).date(),
ProductVersion.valid_from <= product_date,
),
or_(
ProductVersion.valid_till == None, # noqa: E711
ProductVersion.valid_till
>= (voucher.date - timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)).date(),
ProductVersion.valid_till >= product_date,
),
)
)

View File

@ -2,12 +2,13 @@ import asyncio
import locale
import uuid
from datetime import datetime
from datetime import datetime, timedelta
from arq import ArqRedis, create_pool
from sqlalchemy.orm import Session
from ..core.arq import settings as redis_settings
from ..core.config import settings
from ..models.device import Device
from ..models.printer import Printer
from ..models.section_printer import SectionPrinter
@ -36,7 +37,8 @@ def print_sale_report(report: SaleReport, device_id: uuid.UUID, db: Session):
def design_sale_report(report: SaleReport):
s = f"{report.user.name} @ {datetime.now():%d-%b-%Y %H:%M}".center(42)
now = datetime.now() + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES)
s = f"{report.user.name} @ {now:%d-%b-%Y %H:%M}".center(42)
s += "\n\r" + "-" * 42
s += "\n\r" f"{report.start_date:%d-%b-%Y} To {report.finish_date:%d-%b-%Y}".center(42)
s += "\n\r" + "-" * 42

View File

@ -1,6 +1,6 @@
import uuid
from datetime import date, timedelta
from datetime import date, datetime, time, timedelta
from typing import Dict, List
from fastapi import APIRouter, Cookie, Depends, HTTPException, Security, status
@ -45,8 +45,8 @@ def active_cashiers(
db: Session = Depends(get_db),
user: UserToken = Security(get_user, scopes=["cashier-report"]),
) -> List[UserLink]:
if (date.today() - start_date).days > 5 and "audit" not in user.permissions:
today = (datetime.now() + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES)).date()
if (today - start_date).days > 5 and "audit" not in user.permissions:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Accounts Audit",
@ -55,13 +55,19 @@ def active_cashiers(
def get_active_cashiers(start_date: date, finish_date: date, db: Session) -> List[UserLink]:
start_datetime = datetime.combine(start_date, time()) + timedelta(
minutes=settings.TIMEZONE_OFFSET_MINUTES - settings.NEW_DAY_OFFSET_MINUTES
)
finish_datetime = datetime.combine(finish_date, time()) + timedelta(
days=1, minutes=settings.TIMEZONE_OFFSET_MINUTES - settings.NEW_DAY_OFFSET_MINUTES
)
users = (
db.query(User)
.filter(
User.id.in_(
db.query(distinct(Voucher.user_id)).filter(
Voucher.date >= start_date + timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES),
Voucher.date <= finish_date + timedelta(days=1, minutes=settings.NEW_DAY_OFFSET_MINUTES),
Voucher.date >= start_datetime,
Voucher.date <= finish_datetime,
)
)
)
@ -79,7 +85,8 @@ def show_id(
db: Session = Depends(get_db),
user: UserToken = Security(get_user, scopes=["cashier-report"]),
) -> CashierReport:
if (date.today() - start_date).days > 5 and "audit" not in user.permissions:
today = (datetime.now() + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES)).date()
if (today - start_date).days > 5 and "audit" not in user.permissions:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Accounts Audit",
@ -90,12 +97,18 @@ def show_id(
def get_id(id_: uuid.UUID, start_date: date, finish_date: date, user: UserLink, db: Session) -> CashierReport:
cashier: str = db.query(User.name).filter(User.id == id_).scalar()
start_datetime = datetime.combine(start_date, time()) + timedelta(
minutes=settings.TIMEZONE_OFFSET_MINUTES - settings.NEW_DAY_OFFSET_MINUTES
)
finish_datetime = datetime.combine(finish_date, time()) + timedelta(
days=1, minutes=settings.TIMEZONE_OFFSET_MINUTES - settings.NEW_DAY_OFFSET_MINUTES
)
vouchers = (
db.query(Voucher)
.options(joinedload(Voucher.settlements, innerjoin=True).joinedload(Settlement.settle_option, innerjoin=True))
.filter(
Voucher.date >= start_date + timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES),
Voucher.date <= finish_date + timedelta(days=1, minutes=settings.NEW_DAY_OFFSET_MINUTES),
Voucher.date >= start_datetime,
Voucher.date <= finish_datetime,
Voucher.user_id == id_,
)
.order_by(Voucher.voucher_type)
@ -142,7 +155,8 @@ def print_report(
db: Session = Depends(get_db),
user: UserToken = Security(get_user, scopes=["cashier-report"]),
) -> bool:
if (date.today() - start_date).days > 5 and "audit" not in user.permissions:
today = (datetime.now() + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES)).date()
if (today - start_date).days > 5 and "audit" not in user.permissions:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Accounts Audit",
@ -159,7 +173,8 @@ def show_blank(
db: Session = Depends(get_db),
user: UserToken = Security(get_user, scopes=["cashier-report"]),
) -> CashierReport:
if (date.today() - start_date).days > 5 and "audit" not in user.permissions:
today = (datetime.now() + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES)).date()
if (today - start_date).days > 5 and "audit" not in user.permissions:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Accounts Audit",

View File

@ -1,6 +1,6 @@
import uuid
from datetime import date, timedelta
from datetime import date, datetime, time, timedelta
from typing import List
from fastapi import APIRouter, Cookie, Depends, HTTPException, Security, status
@ -41,7 +41,8 @@ def discount_report(
db: Session = Depends(get_db),
user: UserToken = Security(get_user, scopes=["discount-report"]),
) -> DiscountReport:
if (date.today() - start_date).days > 5 and "audit" not in user.permissions:
today = (datetime.now() + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES)).date()
if (today - start_date).days > 5 and "audit" not in user.permissions:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Accounts Audit",
@ -54,8 +55,8 @@ def discount_report(
def get_discount_report(s: date, f: date, db: Session) -> List[DiscountReportItem]:
start_date = s + timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)
finish_date = f + timedelta(days=1, minutes=settings.NEW_DAY_OFFSET_MINUTES)
start_date = datetime.combine(s, time()) + timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)
finish_date = datetime.combine(f, time()) + timedelta(days=1, minutes=settings.NEW_DAY_OFFSET_MINUTES)
day = func.date_trunc("day", Voucher.date - timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)).label("day")
amount = func.sum(Inventory.quantity * Inventory.effective_price * Inventory.discount).label("Amount")
@ -95,7 +96,8 @@ def print_report(
db: Session = Depends(get_db),
user: UserToken = Security(get_user, scopes=["discount-report"]),
) -> bool:
if (date.today() - start_date).days > 5 and "audit" not in user.permissions:
today = (datetime.now() + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES)).date()
if (today - start_date).days > 5 and "audit" not in user.permissions:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Accounts Audit",

View File

@ -1,6 +1,6 @@
import uuid
from datetime import date, timedelta
from datetime import date, datetime, time, timedelta
from typing import List
from fastapi import APIRouter, Cookie, Depends, HTTPException, Security, status
@ -45,7 +45,8 @@ def get_sale_analysis(
db: Session = Depends(get_db),
user: UserToken = Security(get_user, scopes=["sale-report"]),
) -> SaleReport:
if (date.today() - start_date).days > 5 and "audit" not in user.permissions:
today = (datetime.now() + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES)).date()
if (today - start_date).days > 5 and "audit" not in user.permissions:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Accounts Audit",
@ -66,8 +67,8 @@ def get_sale_analysis(
def get_sale(s: date, f: date, db: Session) -> List[SaleReportItem]:
start_date = s + timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)
finish_date = f + timedelta(days=1, minutes=settings.NEW_DAY_OFFSET_MINUTES)
start_date = datetime.combine(s, time()) + timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)
finish_date = datetime.combine(f, time()) + timedelta(days=1, minutes=settings.NEW_DAY_OFFSET_MINUTES)
day = func.date_trunc("day", Voucher.date - timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)).label("day")
list_ = (
@ -102,8 +103,8 @@ def get_sale(s: date, f: date, db: Session) -> List[SaleReportItem]:
def get_settlements(s: date, f: date, db: Session) -> List[SaleReportItem]:
start_date = s + timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)
finish_date = f + timedelta(days=1, minutes=settings.NEW_DAY_OFFSET_MINUTES)
start_date = datetime.combine(s, time()) + timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)
finish_date = datetime.combine(f, time()) + timedelta(days=1, minutes=settings.NEW_DAY_OFFSET_MINUTES)
list_ = (
db.query(SettleOption.name, func.sum(Settlement.amount))
@ -130,7 +131,8 @@ def print_report(
db: Session = Depends(get_db),
user: UserToken = Security(get_user, scopes=["discount-report"]),
) -> bool:
if (date.today() - start_date).days > 5 and "audit" not in user.permissions:
today = (datetime.now() + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES)).date()
if (today - start_date).days > 5 and "audit" not in user.permissions:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Accounts Audit",

View File

@ -86,7 +86,7 @@ def do_save(
user: UserToken,
):
now = datetime.now()
product_date = (now - timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)).date()
product_date = (now + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES - settings.NEW_DAY_OFFSET_MINUTES)).date()
check_permissions(None, voucher_type, user.permissions)
bill_id = get_bill_id(voucher_type, db)

View File

@ -1,13 +1,14 @@
import re
import uuid
from datetime import date
from datetime import datetime, timedelta
from typing import Optional
from fastapi import APIRouter, Depends, HTTPException, Security, status
from sqlalchemy import and_, or_
from sqlalchemy.orm import Session
from ...core.config import settings
from ...core.security import get_current_active_user as get_user
from ...db.session import SessionLocal
from ...models.food_table import FoodTable
@ -113,7 +114,10 @@ def from_table(
return voucher_blank(table, guest)
def voucher_product(product_id: uuid.UUID, date_: date, is_happy_hour: bool, db: Session):
def voucher_product(product_id: uuid.UUID, date_: datetime, is_happy_hour: bool, db: Session):
product_date = (
date_ + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES - settings.NEW_DAY_OFFSET_MINUTES)
).date()
product: ProductVersion = (
db.query(ProductVersion)
.filter(
@ -121,11 +125,11 @@ def voucher_product(product_id: uuid.UUID, date_: date, is_happy_hour: bool, db:
ProductVersion.product_id == product_id,
or_(
ProductVersion.valid_from == None, # noqa: E711
ProductVersion.valid_from <= date_,
ProductVersion.valid_from <= product_date,
),
or_(
ProductVersion.valid_till == None, # noqa: E711
ProductVersion.valid_till >= date_,
ProductVersion.valid_till >= product_date,
),
)
)
@ -158,14 +162,14 @@ def voucher_info(item: Voucher, db: Session):
"creationDateTip": item.creation_date.strftime("%d-%b-%Y %H:%M:%S"),
"lastEditDate": item.last_edit_date.strftime("%H:%M"),
"lastEditDateTip": item.last_edit_date.strftime("%d-%b-%Y %H:%M:%S"),
"kotId": item.kot_id,
"billId": item.full_bill_id,
"table": {"id": item.food_table_id, "name": item.food_table.name},
"customer": {"id": item.customer_id, "name": item.customer.name} if item.customer is not None else None,
"settlements": [],
"narration": item.narration,
"guest": None, # TODO: Wire this functionality
"narration": item.narration, # TODO: Wire this functionality
"reason": item.reason,
"voucherType": item.voucher_type,
"kotId": item.kot_id,
"kots": [
{
"id": k.id,
@ -176,7 +180,7 @@ def voucher_info(item: Voucher, db: Session):
{
"id": i.id,
"sortOrder": i.sort_order,
"product": voucher_product(i.product_id, item.date.date(), i.is_happy_hour, db),
"product": voucher_product(i.product_id, item.date, i.is_happy_hour, db),
"quantity": i.quantity,
"price": i.price,
"isHappyHour": i.is_happy_hour,
@ -197,7 +201,6 @@ def voucher_info(item: Voucher, db: Session):
}
for k in item.kots
],
"reprints": [],
}

View File

@ -59,7 +59,9 @@ def update(
):
try:
now = datetime.now()
product_date = (now - timedelta(minutes=settings.NEW_DAY_OFFSET_MINUTES)).date()
product_date = (
now + timedelta(minutes=settings.TIMEZONE_OFFSET_MINUTES - settings.NEW_DAY_OFFSET_MINUTES)
).date()
update_table = u
voucher_type = VoucherType(p)
guest_book = get_guest_book(g, db)

View File

@ -8,49 +8,39 @@ import { VoucherType } from './voucher-type';
export class Bill {
id: string | undefined;
date: string;
dateTip: string;
pax: number;
customer?: { id: string; name: string };
user: User;
creationDate: string;
creationDateTip: string;
lastEditDate: string;
billId: string;
lastEditDateTip: string;
kotId: string;
billId: string;
table: Table;
customer?: { id: string; name: string };
guest: GuestBook;
// settlements: any[];
voidReason: string;
reason: string;
voucherType: VoucherType;
serial: number;
kots: Kot[];
// reprints: any[];
get dateTip(): string {
return this.date;
}
get creationDateTip(): string {
return this.date;
}
get lastEditDateTip(): string {
return this.date;
}
public constructor(init?: Partial<Bill>) {
this.id = undefined;
this.date = '';
this.dateTip = '';
this.pax = 0;
this.user = new User();
this.creationDate = '';
this.creationDateTip = '';
this.lastEditDate = '';
this.lastEditDateTip = '';
this.billId = '';
this.kotId = '';
this.table = new Table();
this.guest = new GuestBook();
// this.settlements = [];
this.voidReason = '';
this.reason = '';
this.voucherType = VoucherType.Kot;
this.serial = 0;
this.kots = [];
// this.reprints = [];
Object.assign(this, init);

View File

@ -27,12 +27,12 @@
</ng-container>
<ng-container matColumnDef="time-details">
<mat-header-cell *matHeaderCellDef class="deep-purple-100 bold right-align"
><span [matTooltip]="bs.bill.dateTip">{{ bs.bill.date }}</span
>&nbsp;/&nbsp;<span [matTooltip]="bs.bill.creationDateTip">{{
bs.bill.creationDate
><span [matTooltip]="bs.bill.dateTip | localTime">{{ bs.bill.date | localTime }}</span
>&nbsp;/&nbsp;<span [matTooltip]="bs.bill.creationDateTip | localTime">{{
bs.bill.creationDate | localTime
}}</span
>&nbsp;/&nbsp;<span [matTooltip]="bs.bill.lastEditDateTip">{{
bs.bill.lastEditDate
>&nbsp;/&nbsp;<span [matTooltip]="bs.bill.lastEditDateTip | localTime">{{
bs.bill.lastEditDate | localTime
}}</span></mat-header-cell
>
</ng-container>

View File

@ -9,6 +9,11 @@ export class LocalTimePipe implements PipeTransform {
if (value === undefined) {
return '';
}
if (value.length === 5) {
return moment(value, 'HH:mm')
.subtract(new Date().getTimezoneOffset(), 'minutes')
.format('HH:mm');
}
return moment(value, 'DD-MMM-YYYY HH:mm')
.subtract(new Date().getTimezoneOffset(), 'minutes')
.format('DD-MMM-YYYY HH:mm');