Table Merge/Move and Kot Merge/Move
This commit is contained in:
2020-09-25 08:48:50 +05:30
parent bf09471e9e
commit cf34c2b855
7 changed files with 191 additions and 140 deletions

View File

@ -26,7 +26,7 @@ from .routers.reports import (
sale_report, sale_report,
tax_report tax_report
) )
from .routers.voucher import show, save, update, receive_payment, void from .routers.voucher import show, save, update, receive_payment, void, merge_move
from .db.base_class import Base from .db.base_class import Base
from .core.config import settings from .core.config import settings
@ -74,6 +74,7 @@ app.include_router(save.router, prefix="/api/voucher", tags=["voucher"])
app.include_router(update.router, prefix="/api/voucher", tags=["voucher"]) app.include_router(update.router, prefix="/api/voucher", tags=["voucher"])
app.include_router(receive_payment.router, prefix="/api/voucher", tags=["voucher"]) app.include_router(receive_payment.router, prefix="/api/voucher", tags=["voucher"])
app.include_router(void.router, prefix="/api/voucher", tags=["voucher"]) app.include_router(void.router, prefix="/api/voucher", tags=["voucher"])
app.include_router(merge_move.router, prefix="/api", tags=["voucher"])
# app.include_router(issue_grid.router, prefix="/api/issue-grid", tags=["vouchers"]) # app.include_router(issue_grid.router, prefix="/api/issue-grid", tags=["vouchers"])
# app.include_router(batch.router, prefix="/api/batch", tags=["vouchers"]) # app.include_router(batch.router, prefix="/api/batch", tags=["vouchers"])

View File

@ -1,49 +1,66 @@
import uuid import uuid
import transaction
from datetime import datetime from datetime import datetime
from pyramid.view import view_config from typing import List
from fastapi import APIRouter, HTTPException, status, Depends, Security
from sqlalchemy import func from sqlalchemy import func
from sqlalchemy.exc import SQLAlchemyError
from sqlalchemy.orm import Session
from barker.models import Kot, Voucher, Overview from ...schemas.auth import UserToken
from barker.views.voucher import get_bill_id, do_update_table import barker.schemas.merge_move as schemas
from ...core.security import get_current_active_user as get_user
from ...db.session import SessionLocal
@view_config( from ...models import Voucher, Kot, Overview, Settlement
request_method="POST", from ...routers.voucher import (
route_name="v1_move_kot", do_update_settlements,
renderer="json", get_bill_id,
request_param="merge", do_update_table,
permission="Merge Kots",
trans=True,
) )
def merge_kot(request):
json = request.json_body
kot_id = uuid.UUID(json["kot"]["id"])
new_voucher_id = uuid.UUID(json["newVoucher"]["id"])
request.dbsession.query(Kot).filter(Kot.id == kot_id).update({Kot.voucher_id: new_voucher_id}) router = APIRouter()
transaction.commit()
return True
@view_config( # Dependency
request_method="POST", def get_db():
route_name="v1_move_kot", try:
renderer="json", db = SessionLocal()
request_param="move", yield db
permission="Move Kot to New Table", finally:
trans=True, db.close()
@router.post("/move-kot/merge")
def merge_kot(
data: schemas.MergeKot, db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["merge-kots"]),
):
try:
db.query(Kot).filter(Kot.id == data.kot_id).update({Kot.voucher_id: data.new_voucher_id})
update_vouchers([data.voucher_id, data.new_voucher_id], db)
db.commit()
except SQLAlchemyError as e:
db.rollback()
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e),
) )
def move_kot(request): except Exception:
json = request.json_body db.rollback()
raise
@router.post("/move-kot/move")
def move_kot(
data: schemas.MoveKot,
db: Session = Depends(get_db),
user: UserToken = Security(get_user, scopes=["move-kot-to-new-table"]),
):
try:
now = datetime.now() now = datetime.now()
kot_id = uuid.UUID(json["kot"]["id"]) table_id = data.table_id
table_id = uuid.UUID(json["table"]["id"])
kot = request.dbsession.query(Kot).filter(Kot.id == kot_id).first() kot = db.query(Kot).filter(Kot.id == data.kot_id).first()
bill_id = get_bill_id(kot.voucher.voucher_type, request.dbsession) bill_id = get_bill_id(kot.voucher.voucher_type, db)
kot_id = request.dbsession.query(func.coalesce(func.max(Voucher.kot_id), 0) + 1).scalar() kot_id = db.query(func.coalesce(func.max(Voucher.kot_id), 0) + 1).scalar()
item = Voucher( item = Voucher(
now, now,
@ -51,77 +68,69 @@ def move_kot(request):
bill_id, bill_id,
kot_id, kot_id,
table_id, table_id,
kot.voucher.customer_id, kot.voucher.customer_id if kot.voucher.customer is not None else None,
kot.voucher.voucher_type, kot.voucher.voucher_type,
uuid.UUID(request.authenticated_userid), user.id_,
) )
request.dbsession.add(item) db.add(item)
item.kots.append(kot) item.kots.append(kot)
do_update_table(item, None, request.dbsession) do_update_table(item, None, db)
request.dbsession.add(item.status) update_vouchers([data.voucher_id, item.id], db)
transaction.commit() db.commit()
return True except SQLAlchemyError as e:
db.rollback()
raise HTTPException(
@view_config( status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e),
request_method="POST",
route_name="v1_move_table",
request_param="move",
renderer="json",
permission="Move Table",
trans=True,
) )
def move_table(request): except Exception:
id_ = uuid.UUID(request.json_body["voucher"]["id"]) db.rollback()
table_id = uuid.UUID(request.json_body["table"]["id"]) raise
request.dbsession.query(Overview).filter(Overview.voucher_id == id_).update({Overview.food_table_id: table_id})
request.dbsession.query(Voucher).filter(Voucher.id == id_).update({Voucher.food_table_id: table_id})
transaction.commit()
return True
@view_config( @router.post("/move-table/move")
request_method="POST", def move_table(
route_name="v1_move_table", data: schemas.MoveTable, db: Session = Depends(get_db), user: UserToken = Security(get_user, scopes=["move-table"]),
request_param="merge", ):
renderer="json", try:
permission="Merge Tables", db.query(Overview).filter(Overview.voucher_id == data.voucher_id).update(
trans=True, {Overview.food_table_id: data.table_id}
) )
def merge_table(request): db.query(Voucher).filter(Voucher.id == data.voucher_id).update({Voucher.food_table_id: data.table_id})
json = request.json_body db.commit()
except SQLAlchemyError as e:
db.rollback()
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e),
)
except Exception:
db.rollback()
raise
id_ = uuid.UUID(json["oldVoucher"]["id"])
# table_id = uuid.UUID(json["table"]["id"])
new_voucher_id = uuid.UUID(json["newVoucher"]["id"])
request.dbsession.query(Kot).filter(Kot.voucher_id == id_).update({Kot.voucher_id: new_voucher_id}) @router.post("/move-table/merge")
request.dbsession.query(Voucher).filter(Voucher.id == id_).delete() def merge_table(
transaction.commit() data: schemas.MergeTable,
return True db: Session = Depends(get_db),
user: UserToken = Security(get_user, scopes=["merge-tables"]),
):
try:
db.query(Kot).filter(Kot.voucher_id == data.voucher_id).update({Kot.voucher_id: data.new_voucher_id})
db.query(Overview).filter(Overview.voucher_id == data.voucher_id).delete()
db.query(Settlement).filter(Settlement.voucher_id == data.voucher_id).delete()
db.query(Voucher).filter(Voucher.id == data.voucher_id).delete()
update_vouchers([data.new_voucher_id], db)
db.commit()
except SQLAlchemyError as e:
db.rollback()
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=str(e),
)
except Exception:
db.rollback()
raise
# VoucherBI.UpdateTable(x => x.FoodTableID == _voucher.Table.FoodTableID && x.VoucherID == _voucher.VoucherID, null, null);
# throw new NotImplementedException(); def update_vouchers(vouchers: List[uuid.UUID], db: Session):
# var newTable = _session.QueryOver<FoodTable>().Where(x => x.Name == tableName).SingleOrDefault(); for v in vouchers:
# var newVoucher = _session.Get<Voucher>(newTable.VoucherID); voucher: Voucher = db.query(Voucher).filter(Voucher.id == v).first()
# var oldVoucher = _session.Get<Voucher>(voucherID); do_update_settlements(voucher, [], db)
# for (var i = oldVoucher.Kots.Count - 1; i >= 0; i--)
# {
# var kot = oldVoucher.Kots[i];
# oldVoucher.Kots.Remove(kot);
# kot.Voucher = newVoucher;
# newVoucher.Kots.Add(kot);
# _session.Update(kot);
# }
# _session.Update(newVoucher);
#
#
# foreach (var item in oldVoucher.Settlements)
# {
# _session.Delete(item);
# }
# _session.Delete(oldVoucher);
# return newVoucher.VoucherID;

View File

@ -26,7 +26,7 @@ def get_db():
def from_id( def from_id(
id_: str, db: Session = Depends(get_db), user: UserToken = Security(get_user), id_: str, db: Session = Depends(get_db), user: UserToken = Security(get_user),
): ):
item = db.query(Voucher).filter(Voucher.id == id_).first() item: Voucher = db.query(Voucher).filter(Voucher.id == id_).first()
return voucher_info(item) return voucher_info(item)

View File

@ -0,0 +1,45 @@
import uuid
from pydantic import BaseModel
from barker.schemas import to_camel
class MergeKot(BaseModel):
voucher_id: uuid.UUID
kot_id: uuid.UUID
table_id: uuid.UUID
new_voucher_id: uuid.UUID
class Config:
alias_generator = to_camel
fields = {"id_": "id"}
class MoveKot(BaseModel):
voucher_id: uuid.UUID
kot_id: uuid.UUID
table_id: uuid.UUID
class Config:
alias_generator = to_camel
fields = {"id_": "id"}
class MergeTable(BaseModel):
voucher_id: uuid.UUID
table_id: uuid.UUID
new_voucher_id: uuid.UUID
class Config:
alias_generator = to_camel
fields = {"id_": "id"}
class MoveTable(BaseModel):
voucher_id: uuid.UUID
table_id: uuid.UUID
class Config:
alias_generator = to_camel
fields = {"id_": "id"}

View File

@ -133,7 +133,7 @@ export class BillsComponent implements OnInit {
} }
moveKot(kot: Kot) { moveKot(kot: Kot) {
const canMergeTables = (this.auth.user.perms.indexOf('merge-tables') === -1); const canMergeTables = (this.auth.user.perms.indexOf('merge-tables') !== -1);
this.dialog.open(TablesDialogComponent, { this.dialog.open(TablesDialogComponent, {
// width: '750px', // width: '750px',
data: { data: {

View File

@ -100,45 +100,41 @@ export class VoucherService {
} }
moveTable(id: string, table: Table): Observable<boolean> { moveTable(id: string, table: Table): Observable<boolean> {
const options = {params: new HttpParams().set('move', '')}; return <Observable<boolean>>this.http.post<boolean>(`${urlMoveTable}/move`, {
return <Observable<boolean>>this.http.post<boolean>(urlMoveTable, { voucherId: id,
voucher: {id: id}, tableId: table.id
table: {id: table.id} }).pipe(
}, options).pipe(
catchError(this.log.handleError(serviceName, 'moveTable')) catchError(this.log.handleError(serviceName, 'moveTable'))
); );
} }
mergeTable(id: string, table: Table): Observable<boolean> { mergeTable(id: string, table: Table): Observable<boolean> {
const options = {params: new HttpParams().set('merge', '')}; return <Observable<boolean>>this.http.post<boolean>(`${urlMoveTable}/merge`, {
return <Observable<boolean>>this.http.post<boolean>(urlMoveTable, { voucherId: id,
voucher: {id: id}, tableId: table.id,
table: {id: table.id}, newVoucherId: table.voucherId
newVoucher: {id: table.voucherId} }).pipe(
}, options).pipe(
catchError(this.log.handleError(serviceName, 'mergeTable')) catchError(this.log.handleError(serviceName, 'mergeTable'))
); );
} }
moveKotToNewTable(id: string, kotId: string, table: Table): Observable<boolean> { moveKotToNewTable(id: string, kotId: string, table: Table): Observable<boolean> {
const options = {params: new HttpParams().set('move', '')}; return <Observable<boolean>>this.http.post<boolean>(`${urlMoveKot}/move`, {
return <Observable<boolean>>this.http.post<boolean>(urlMoveKot, { voucherId: id,
voucher: {id: id}, kotId: kotId,
kot: {id: kotId}, tableId: table.id
table: {id: table.id} }).pipe(
}, options).pipe(
catchError(this.log.handleError(serviceName, 'moveKotToNewTable')) catchError(this.log.handleError(serviceName, 'moveKotToNewTable'))
); );
} }
mergeKotWithOldBill(id: string, kotId: string, table: Table): Observable<boolean> { mergeKotWithOldBill(id: string, kotId: string, table: Table): Observable<boolean> {
const options = {params: new HttpParams().set('merge', '')}; return <Observable<boolean>>this.http.post<boolean>(`${urlMoveKot}/merge`, {
return <Observable<boolean>>this.http.post<boolean>(urlMoveKot, { voucherId: id,
voucher: {id: id}, kotId: kotId,
kot: {id: kotId}, tableId: table.id,
table: {id: table.id}, newVoucherId: table.voucherId
newVoucher: {id: table.voucherId} }).pipe(
}, options).pipe(
catchError(this.log.handleError(serviceName, 'mergeKotWithOldBill')) catchError(this.log.handleError(serviceName, 'mergeKotWithOldBill'))
); );
} }

View File

@ -265,7 +265,7 @@ export class SalesHomeComponent implements OnInit {
if (!this.moveTableAllowed()) { if (!this.moveTableAllowed()) {
return; return;
} }
const canMergeTables = (this.auth.user.perms.indexOf('merge-tables') === -1); const canMergeTables = (this.auth.user.perms.indexOf('merge-tables') !== -1);
this.dialog.open(TablesDialogComponent, { this.dialog.open(TablesDialogComponent, {
// width: '750px', // width: '750px',
data: { data: {