From 63dfe0504470913906343f777cd29c1c0c17ada5 Mon Sep 17 00:00:00 2001 From: tanshu Date: Sat, 4 Sep 2021 12:57:56 +0530 Subject: [PATCH] Chore: Changed the Section Printer from Menu Categories to Sale Categories. This is more logical, in the older software, there as no concept of sale categories so menu categories was used. This should make the whole thing much easier to update and read. --- .../versions/81d94c5223a7_section_printer.py | 73 +++++++++++++++++++ barker/barker/models/section_printer.py | 10 +-- barker/barker/printing/bill.py | 2 +- barker/barker/printing/cashier_report.py | 2 +- barker/barker/printing/discount_report.py | 2 +- barker/barker/printing/kot.py | 6 +- barker/barker/printing/product_sale_report.py | 10 +-- barker/barker/printing/sale_report.py | 2 +- barker/barker/routers/section_printer.py | 40 +++++----- barker/barker/schemas/section_printer.py | 4 +- bookie/src/app/core/section-printer.ts | 6 +- .../section-printer.component.html | 12 +-- .../section-printer.component.ts | 10 +-- .../section-printer.service.ts | 8 -- 14 files changed, 124 insertions(+), 63 deletions(-) create mode 100644 barker/alembic/versions/81d94c5223a7_section_printer.py diff --git a/barker/alembic/versions/81d94c5223a7_section_printer.py b/barker/alembic/versions/81d94c5223a7_section_printer.py new file mode 100644 index 0000000..7f20194 --- /dev/null +++ b/barker/alembic/versions/81d94c5223a7_section_printer.py @@ -0,0 +1,73 @@ +"""section printer + +Revision ID: 81d94c5223a7 +Revises: c123dbf9c659 +Create Date: 2021-08-17 12:10:01.082632 + +""" +import sqlalchemy as sa + +from alembic import op +from sqlalchemy import column, table +from sqlalchemy.dialects import postgresql + + +# revision identifiers, used by Alembic. +revision = "81d94c5223a7" +down_revision = "c123dbf9c659" +branch_labels = None +depends_on = None + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + sp = table( + "section_printers", + column("id", postgresql.UUID(as_uuid=True)), + ) + op.execute(sp.delete()) + + op.add_column("section_printers", sa.Column("sale_category_id", postgresql.UUID(as_uuid=True), nullable=True)) + op.drop_constraint("uq_section_printers_menu_category_id", "section_printers", type_="unique") + op.create_unique_constraint( + op.f("uq_section_printers_sale_category_id"), "section_printers", ["sale_category_id", "section_id"] + ) + op.drop_constraint("fk_section_printers_menu_category_id_menu_categories", "section_printers", type_="foreignkey") + op.create_foreign_key( + op.f("fk_section_printers_sale_category_id_sale_categories"), + "section_printers", + "sale_categories", + ["sale_category_id"], + ["id"], + ) + op.drop_column("section_printers", "menu_category_id") + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + sp = table( + "section_printers", + column("id", postgresql.UUID(as_uuid=True)), + ) + op.execute(sp.delete()) + + op.add_column( + "section_printers", sa.Column("menu_category_id", postgresql.UUID(), autoincrement=False, nullable=True) + ) + op.drop_constraint( + op.f("fk_section_printers_sale_category_id_sale_categories"), "section_printers", type_="foreignkey" + ) + op.create_foreign_key( + "fk_section_printers_menu_category_id_menu_categories", + "section_printers", + "menu_categories", + ["menu_category_id"], + ["id"], + ) + op.drop_constraint(op.f("uq_section_printers_sale_category_id"), "section_printers", type_="unique") + op.create_unique_constraint( + "uq_section_printers_menu_category_id", "section_printers", ["menu_category_id", "section_id"] + ) + op.drop_column("section_printers", "sale_category_id") + # ### end Alembic commands ### diff --git a/barker/barker/models/section_printer.py b/barker/barker/models/section_printer.py index 67c9e8c..dfd6a42 100644 --- a/barker/barker/models/section_printer.py +++ b/barker/barker/models/section_printer.py @@ -8,30 +8,30 @@ from sqlalchemy.orm import relationship class SectionPrinter(Base): __tablename__ = "section_printers" - __table_args__ = (UniqueConstraint("menu_category_id", "section_id"),) + __table_args__ = (UniqueConstraint("sale_category_id", "section_id"),) id = Column( "id", UUID(as_uuid=True), primary_key=True, server_default=text("gen_random_uuid()"), default=uuid.uuid4 ) - menu_category_id = Column("menu_category_id", UUID(as_uuid=True), ForeignKey("menu_categories.id")) + sale_category_id = Column("sale_category_id", UUID(as_uuid=True), ForeignKey("sale_categories.id")) section_id = Column("section_id", UUID(as_uuid=True), ForeignKey("sections.id"), nullable=False) printer_id = Column("printer_id", UUID(as_uuid=True), ForeignKey("printers.id"), nullable=False) copies = Column("copies", Integer, nullable=False) - menu_category = relationship("MenuCategory", backref="section_printers") + sale_category = relationship("SaleCategory", backref="section_printers") section = relationship("Section", backref="section_printers") printer = relationship("Printer", backref="section_printers") def __init__( self, - menu_category_id=None, + sale_category_id=None, section_id=None, printer_id=None, copies=None, id_=None, ): self.id = id_ - self.menu_category_id = menu_category_id + self.sale_category_id = sale_category_id self.section_id = section_id self.printer_id = printer_id self.copies = copies diff --git a/barker/barker/printing/bill.py b/barker/barker/printing/bill.py index e276556..6402245 100644 --- a/barker/barker/printing/bill.py +++ b/barker/barker/printing/bill.py @@ -31,7 +31,7 @@ def print_bill(voucher_id: uuid.UUID, db: Session): select(Printer) .join(SectionPrinter.printer) .where(SectionPrinter.section_id == voucher.food_table.section_id) - .where(SectionPrinter.menu_category_id == None) # noqa: E711 + .where(SectionPrinter.sale_category_id == None) # noqa: E711 ).scalar_one() items_dict = {} diff --git a/barker/barker/printing/cashier_report.py b/barker/barker/printing/cashier_report.py index 905417f..9dc1540 100644 --- a/barker/barker/printing/cashier_report.py +++ b/barker/barker/printing/cashier_report.py @@ -25,7 +25,7 @@ def print_cashier_report(report: CashierReport, device_id: uuid.UUID, db: Sessio select(Printer) .join(SectionPrinter.printer) .where(SectionPrinter.section_id == section_id) - .where(SectionPrinter.menu_category_id == None) # noqa: E711 + .where(SectionPrinter.sale_category_id == None) # noqa: E711 ).scalar_one() redis: ArqRedis = asyncio.run(create_pool(redis_settings)) diff --git a/barker/barker/printing/discount_report.py b/barker/barker/printing/discount_report.py index 210992a..07f5dab 100644 --- a/barker/barker/printing/discount_report.py +++ b/barker/barker/printing/discount_report.py @@ -22,7 +22,7 @@ def print_discount_report(report: DiscountReport, device_id: uuid.UUID, db: Sess select(Printer) .join(SectionPrinter.printer) .where(SectionPrinter.section_id == section_id) - .where(SectionPrinter.menu_category_id == None) # noqa: E711 + .where(SectionPrinter.sale_category_id == None) # noqa: E711 ).scalar_one() redis: ArqRedis = asyncio.run(create_pool(redis_settings)) diff --git a/barker/barker/printing/kot.py b/barker/barker/printing/kot.py index a4b4fcf..f4e0a32 100644 --- a/barker/barker/printing/kot.py +++ b/barker/barker/printing/kot.py @@ -94,11 +94,11 @@ def print_kot(voucher_id: uuid.UUID, db: Session): .where(SectionPrinter.section_id == voucher.food_table.section_id) .where( or_( - SectionPrinter.menu_category_id == product.menu_category_id, - SectionPrinter.menu_category_id == None, # noqa: E711 + SectionPrinter.sale_category_id == product.sale_category_id, + SectionPrinter.sale_category_id == None, # noqa: E711 ) ) - .order_by(SectionPrinter.menu_category_id) + .order_by(SectionPrinter.sale_category_id) ).first() key = (printer.id, copies) if key not in my_hash: diff --git a/barker/barker/printing/product_sale_report.py b/barker/barker/printing/product_sale_report.py index a893d3c..17f9b70 100644 --- a/barker/barker/printing/product_sale_report.py +++ b/barker/barker/printing/product_sale_report.py @@ -23,7 +23,7 @@ def print_product_sale_report(report, device_id: uuid.UUID, db: Session): select(Printer) .join(SectionPrinter.printer) .where(SectionPrinter.section_id == section_id) - .where(SectionPrinter.menu_category_id == None) # noqa: E711 + .where(SectionPrinter.sale_category_id == None) # noqa: E711 ).scalar_one() redis: ArqRedis = asyncio.run(create_pool(redis_settings)) @@ -42,10 +42,10 @@ def design_product_sale_report(report): s += "\n\r" + "-" * 42 s += "\n\rUnbilled Sale NC Staff Void" for item in report["amounts"]: - s += f"\n\r{item['name']: ^42.42}" f"\n\r" + (f"{item['kot']: >7.2f} " if "kot" in item else " ") + ( - f"{item['regularBill']: >7.2f} " if "regularBill" in item else " " - ) + (f"{item['noCharge']: >7.2f} " if "noCharge" in item else " ") + ( - f"{item['staff']: >7.2f} " if "staff" in item else " " + s += f"\n\r{item['name']: ^42.42}" f"\n\r" + (f"{item['kot']: >7.2f} " if "kot" in item else " ") + ( + f"{item['regularBill']: >7.2f} " if "regularBill" in item else " " + ) + (f"{item['noCharge']: >7.2f} " if "noCharge" in item else " ") + ( + f"{item['staff']: >7.2f} " if "staff" in item else " " ) + ( f"{item['void']: >7.2f}" if "void" in item else " " ) diff --git a/barker/barker/printing/sale_report.py b/barker/barker/printing/sale_report.py index ba4c5af..badbfde 100644 --- a/barker/barker/printing/sale_report.py +++ b/barker/barker/printing/sale_report.py @@ -25,7 +25,7 @@ def print_sale_report(report: SaleReport, device_id: uuid.UUID, db: Session): select(Printer) .join(SectionPrinter.printer) .where(SectionPrinter.section_id == section_id) - .where(SectionPrinter.menu_category_id == None) # noqa: E711 + .where(SectionPrinter.sale_category_id == None) # noqa: E711 ).scalar_one() redis: ArqRedis = asyncio.run(create_pool(redis_settings)) diff --git a/barker/barker/routers/section_printer.py b/barker/barker/routers/section_printer.py index 295934d..8deec85 100644 --- a/barker/barker/routers/section_printer.py +++ b/barker/barker/routers/section_printer.py @@ -12,7 +12,7 @@ from sqlalchemy.orm import Session from ..core.security import get_current_active_user as get_user from ..db.session import SessionFuture -from ..models.menu_category import MenuCategory +from ..models.sale_category import SaleCategory from ..models.section_printer import SectionPrinter from ..schemas.user_token import UserToken @@ -30,39 +30,39 @@ def save( with SessionFuture() as db: current = [] default_id = uuid.uuid4() - for mcs in data: - if mcs.menu_category is None and mcs.printer is None: + for scs in data: + if scs.sale_category is None and scs.printer is None: raise ValueError("Please choose a default printer") - if mcs.printer is None: + if scs.printer is None: continue stmt = ( pg_insert(SectionPrinter) .values( - id=default_id if mcs.menu_category is None else uuid.uuid4(), - menu_category_id=mcs.menu_category.id_ if mcs.menu_category is not None else None, + id=default_id if scs.sale_category is None else uuid.uuid4(), + sale_category_id=scs.sale_category.id_ if scs.sale_category is not None else None, section_id=id_, - printer_id=mcs.printer.id_, - copies=mcs.copies, + printer_id=scs.printer.id_, + copies=scs.copies, ) .on_conflict_do_update( - index_elements=["menu_category_id", "section_id"], - set_=dict(printer_id=mcs.printer.id_, copies=mcs.copies), + index_elements=["sale_category_id", "section_id"], + set_=dict(printer_id=scs.printer.id_, copies=scs.copies), ) ) db.execute(stmt) - current.append(None if mcs.menu_category is None else mcs.menu_category.id_) + current.append(None if scs.sale_category is None else scs.sale_category.id_) db.execute( delete(SectionPrinter).where( or_( and_( SectionPrinter.section_id == id_, - SectionPrinter.menu_category_id == None, # noqa: E711 + SectionPrinter.sale_category_id == None, # noqa: E711 SectionPrinter.id != default_id, ), and_( SectionPrinter.section_id == id_, - SectionPrinter.menu_category_id != None, # noqa: E711 - ~SectionPrinter.menu_category_id.in_([x for x in current if x is not None]), + SectionPrinter.sale_category_id != None, # noqa: E711 + ~SectionPrinter.sale_category_id.in_([x for x in current if x is not None]), ), ) ) @@ -112,18 +112,14 @@ def show_id( def report(section_id: Optional[uuid.UUID], db: Session) -> List[schemas.SectionPrinter]: - menu_categories = db.execute( - select(MenuCategory.id, MenuCategory.name) - .where(MenuCategory.is_active == True) # noqa: E712 - .order_by(MenuCategory.sort_order) - ).all() + sale_categories = db.execute(select(SaleCategory.id, SaleCategory.name).order_by(SaleCategory.name)).all() list_ = [] - for mc_id, mc_name in [(None, None)] + menu_categories: + for sc_id, sc_name in [(None, None)] + sale_categories: section_printer = ( db.execute( select(SectionPrinter).where( SectionPrinter.section_id == section_id, - SectionPrinter.menu_category_id == mc_id, + SectionPrinter.sale_category_id == sc_id, ) ) .scalars() @@ -131,7 +127,7 @@ def report(section_id: Optional[uuid.UUID], db: Session) -> List[schemas.Section ) list_.append( schemas.SectionPrinter( - menuCategory=None if mc_id is None else schemas.MenuCategoryLink(id=mc_id, name=mc_name, products=[]), + saleCategory=None if sc_id is None else schemas.SaleCategoryLink(id=sc_id, name=sc_name), printer=None if section_printer is None else schemas.PrinterLink(id=section_printer.printer_id), copies=0 if section_printer is None else section_printer.copies, ) diff --git a/barker/barker/schemas/section_printer.py b/barker/barker/schemas/section_printer.py index 1e27181..6f4da26 100644 --- a/barker/barker/schemas/section_printer.py +++ b/barker/barker/schemas/section_printer.py @@ -3,12 +3,12 @@ from typing import Optional from pydantic import BaseModel, Field from . import to_camel -from .menu_category import MenuCategoryLink from .printer import PrinterLink +from .sale_category import SaleCategoryLink class SectionPrinter(BaseModel): - menu_category: Optional[MenuCategoryLink] + sale_category: Optional[SaleCategoryLink] printer: Optional[PrinterLink] copies: int = Field(ge=0) diff --git a/bookie/src/app/core/section-printer.ts b/bookie/src/app/core/section-printer.ts index 7808be4..d61b945 100644 --- a/bookie/src/app/core/section-printer.ts +++ b/bookie/src/app/core/section-printer.ts @@ -1,13 +1,13 @@ -import { MenuCategory } from './menu-category'; import { Printer } from './printer'; +import { SaleCategory } from './sale-category'; export class SectionPrinter { - menuCategory: MenuCategory | null; + saleCategory: SaleCategory | null; printer: Printer | null; copies: number; public constructor(init?: Partial) { - this.menuCategory = null; + this.saleCategory = null; this.printer = null; this.copies = 0; Object.assign(this, init); diff --git a/bookie/src/app/section-printers/section-printer.component.html b/bookie/src/app/section-printers/section-printer.component.html index c21d262..68f0655 100644 --- a/bookie/src/app/section-printers/section-printer.component.html +++ b/bookie/src/app/section-printers/section-printer.component.html @@ -26,11 +26,11 @@ - - - - Menu Category - {{ row.menuCategory?.name || 'Default' }} + + + + Sale Category + {{ row.saleCategory?.name || 'Default' }} @@ -53,7 +53,7 @@ Copies - + Copies diff --git a/bookie/src/app/section-printers/section-printer.component.ts b/bookie/src/app/section-printers/section-printer.component.ts index e4646f4..4432dc6 100644 --- a/bookie/src/app/section-printers/section-printer.component.ts +++ b/bookie/src/app/section-printers/section-printer.component.ts @@ -29,7 +29,7 @@ export class SectionPrinterComponent implements OnInit { sections: Section[] = []; printers: Printer[] = []; sectionId = ''; - displayedColumns = ['menuCategory', 'printer', 'copies']; + displayedColumns = ['saleCategory', 'printer', 'copies']; constructor( private route: ActivatedRoute, @@ -42,7 +42,7 @@ export class SectionPrinterComponent implements OnInit { // Create form this.form = this.fb.group({ section: '', - menuCategories: this.fb.array([]), + saleCategories: this.fb.array([]), }); route.params.pipe(map((p) => p.id)).subscribe((x) => { this.sectionId = x; @@ -64,11 +64,11 @@ export class SectionPrinterComponent implements OnInit { this.list = list; (this.form.get('section') as AbstractControl).setValue(this.sectionId); this.form.setControl( - 'menuCategories', + 'saleCategories', this.fb.array( this.list.map((x) => this.fb.group({ - menuCategory: x.menuCategory?.name || 'Default', + saleCategory: x.saleCategory?.name || 'Default', printer: x.printer?.id, copies: `${x.copies}`, }), @@ -119,7 +119,7 @@ export class SectionPrinterComponent implements OnInit { getItem(): SectionPrinter[] { const formModel = this.form.value; this.sectionId = formModel.section; - const array = this.form.get('menuCategories') as FormArray; + const array = this.form.get('saleCategories') as FormArray; this.list.forEach((item, index) => { const cont = array.controls[index].value; if (cont.printer === null || cont.printer === undefined) { diff --git a/bookie/src/app/section-printers/section-printer.service.ts b/bookie/src/app/section-printers/section-printer.service.ts index 476fb50..a454d6a 100644 --- a/bookie/src/app/section-printers/section-printer.service.ts +++ b/bookie/src/app/section-printers/section-printer.service.ts @@ -27,14 +27,6 @@ export class SectionPrinterService { >; } - // item(id: string, menuCategoryId: string): Observable { - // const options = {params: new HttpParams().set('m', menuCategoryId)}; - // return this.http.get(`${url}/${id}`, options) - // .pipe( - // catchError(this.log.handleError(serviceName, 'list')) - // ) as Observable; - // } - save(sectionId: string, list: SectionPrinter[]): Observable { return this.http .post(`${url}/${sectionId}`, list, httpOptions)