diff --git a/barker/routes.py b/barker/routes.py index f69658d..11d9926 100644 --- a/barker/routes.py +++ b/barker/routes.py @@ -365,13 +365,13 @@ 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") + config.add_route("v1_discount_report", "/v1/discount-report") # Done till here config.add_route("customer", "/Customer.json") config.add_route("customer_list", "/Customers.json") config.add_route("customer_id", "/Customer/{id}.json") - config.add_route("discount_report", "/DiscountReport.json") config.add_route("location_list", "/Locations.json") diff --git a/barker/views/reports/discount_report.py b/barker/views/reports/discount_report.py index a48fc4b..bdc3dd8 100644 --- a/barker/views/reports/discount_report.py +++ b/barker/views/reports/discount_report.py @@ -1,36 +1,56 @@ -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, MenuCategory, Settlement, SettleOption, Voucher +from barker.models import Inventory, Kot, Product, Voucher, SaleCategory, 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='discount_report', renderer='json', permission='Discount Report', - request_param=('s', 'f')) -def discount_report(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') +@view_config( + request_method="GET", + route_name="v1_discount_report", + renderer="json", + permission="Discount Report", +) +def discount_report_view(request): + start_date = get_start_date(request.GET.get("s", None)) + finish_date = get_finish_date(request.GET.get("f", None)) - 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") - amount = func.sum(Inventory.quantity * Inventory.effective_price * Inventory.discount).label('Amount') - list = request.dbsession.query( - MenuCategory.group_type, amount - ).join(Voucher.kots).join(Kot.inventories).join(Inventory.product).join(Product.menu_category).filter( - Voucher.date >= start_date, - Voucher.date <= finish_date, - Voucher.is_void == False, - Inventory.discount != 0, - Voucher.settlements.any(~Settlement.settled.in_( - [SettleOption.CASH(), SettleOption.CREDIT_CARD(), SettleOption.BILL_TO_COMPANY(), - SettleOption.UNSETTLED()])) - ).group_by( - MenuCategory.group_type - ).order_by( - MenuCategory.group_type - ).all() + return { + "startDate": start_date.date().strftime("%d-%b-%Y"), + "finishDate": (finish_date - timedelta(days=1)).date().strftime("%d-%b-%Y"), + "amounts": discount_report(start_date, finish_date, request.dbsession), + } - return [{'GroupType': pg, 'Amount': amt} for pg, amt in list] + +def discount_report(start_date, finish_date, dbsession): + amount = func.sum( + Inventory.quantity * Inventory.effective_price * Inventory.discount + ).label("Amount") + list_ = ( + dbsession.query(SaleCategory.name, amount) + .join(Voucher.kots) + .join(Kot.inventories) + .join(Inventory.product) + .join(Product.sale_category) + .filter( + Inventory.discount != 0, + Voucher.date >= start_date, + Voucher.date <= finish_date, + Voucher.voucher_type.in_( + [VoucherType.REGULAR_BILL.value, VoucherType.KOT.value] + ), + ) + .group_by(SaleCategory.name) + .order_by(SaleCategory.name) + .all() + ) + + return [{"name": pg, "amount": amt} for pg, amt in list_] diff --git a/bookie/src/app/app-routing.module.ts b/bookie/src/app/app-routing.module.ts index 216e24d..f670090 100644 --- a/bookie/src/app/app-routing.module.ts +++ b/bookie/src/app/app-routing.module.ts @@ -21,6 +21,10 @@ const routes: Routes = [ path: 'devices', loadChildren: () => import('./devices/devices.module').then(mod => mod.DevicesModule) }, + { + path: 'discount-report', + loadChildren: () => import('./discount-report/discount-report.module').then(mod => mod.DiscountReportModule) + }, { path: 'guest-book', loadChildren: () => import('./guest-book/guest-book.module').then(mod => mod.GuestBookModule) diff --git a/bookie/src/app/discount-report/discount-report-datasource.ts b/bookie/src/app/discount-report/discount-report-datasource.ts new file mode 100644 index 0000000..7cfc51a --- /dev/null +++ b/bookie/src/app/discount-report/discount-report-datasource.ts @@ -0,0 +1,18 @@ +import { DataSource } from '@angular/cdk/collections'; +import { Observable, of as observableOf } from 'rxjs'; +import { DiscountReportItem } from './discount-report'; + + +export class DiscountReportDataSource extends DataSource { + + constructor(public data: DiscountReportItem[]) { + super(); + } + + connect(): Observable { + return observableOf(this.data); + } + + disconnect() { + } +} diff --git a/bookie/src/app/discount-report/discount-report-resolver.service.spec.ts b/bookie/src/app/discount-report/discount-report-resolver.service.spec.ts new file mode 100644 index 0000000..ed04e61 --- /dev/null +++ b/bookie/src/app/discount-report/discount-report-resolver.service.spec.ts @@ -0,0 +1,15 @@ +import {inject, TestBed} from '@angular/core/testing'; + +import {DiscountReportResolver} from './discount-report-resolver.service'; + +describe('DiscountReportResolver', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [DiscountReportResolver] + }); + }); + + it('should be created', inject([DiscountReportResolver], (service: DiscountReportResolver) => { + expect(service).toBeTruthy(); + })); +}); diff --git a/bookie/src/app/discount-report/discount-report-resolver.service.ts b/bookie/src/app/discount-report/discount-report-resolver.service.ts new file mode 100644 index 0000000..3fd3f17 --- /dev/null +++ b/bookie/src/app/discount-report/discount-report-resolver.service.ts @@ -0,0 +1,20 @@ +import {Injectable} from '@angular/core'; +import {ActivatedRouteSnapshot, Resolve, RouterStateSnapshot} from '@angular/router'; +import {Observable} from 'rxjs/internal/Observable'; +import {DiscountReport} from './discount-report'; +import {DiscountReportService} from './discount-report.service'; + +@Injectable({ + providedIn: 'root' +}) +export class DiscountReportResolver implements Resolve { + + constructor(private ser: DiscountReportService) { + } + + resolve(route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable { + const startDate = route.queryParamMap.get('startDate') || null; + const finishDate = route.queryParamMap.get('finishDate') || null; + return this.ser.get(startDate, finishDate); + } +} diff --git a/bookie/src/app/discount-report/discount-report-routing.module.spec.ts b/bookie/src/app/discount-report/discount-report-routing.module.spec.ts new file mode 100644 index 0000000..dd1808f --- /dev/null +++ b/bookie/src/app/discount-report/discount-report-routing.module.spec.ts @@ -0,0 +1,13 @@ +import {DiscountReportRoutingModule} from './discount-report-routing.module'; + +describe('DiscountReportRoutingModule', () => { + let discountReportRoutingModule: DiscountReportRoutingModule; + + beforeEach(() => { + discountReportRoutingModule = new DiscountReportRoutingModule(); + }); + + it('should create an instance', () => { + expect(discountReportRoutingModule).toBeTruthy(); + }); +}); diff --git a/bookie/src/app/discount-report/discount-report-routing.module.ts b/bookie/src/app/discount-report/discount-report-routing.module.ts new file mode 100644 index 0000000..cefe4d6 --- /dev/null +++ b/bookie/src/app/discount-report/discount-report-routing.module.ts @@ -0,0 +1,37 @@ +import { NgModule } from '@angular/core'; +import { CommonModule } from '@angular/common'; +import { RouterModule, Routes } from '@angular/router'; +import { DiscountReportResolver } from './discount-report-resolver.service'; +import { AuthGuard } from '../auth/auth-guard.service'; +import { DiscountReportComponent } from './discount-report.component'; + +const DiscountReportRoutes: Routes = [ + { + path: '', + component: DiscountReportComponent, + canActivate: [AuthGuard], + data: { + permission: 'Discount Report' + }, + resolve: { + info: DiscountReportResolver + }, + runGuardsAndResolvers: 'always' + } +]; + +@NgModule({ + imports: [ + CommonModule, + RouterModule.forChild(DiscountReportRoutes) + + ], + exports: [ + RouterModule + ], + providers: [ + DiscountReportResolver + ] +}) +export class DiscountReportRoutingModule { +} diff --git a/bookie/src/app/discount-report/discount-report.component.css b/bookie/src/app/discount-report/discount-report.component.css new file mode 100644 index 0000000..a9626b3 --- /dev/null +++ b/bookie/src/app/discount-report/discount-report.component.css @@ -0,0 +1,4 @@ +.right { + display: flex; + justify-content: flex-end; +} diff --git a/bookie/src/app/discount-report/discount-report.component.html b/bookie/src/app/discount-report/discount-report.component.html new file mode 100644 index 0000000..4fb56a3 --- /dev/null +++ b/bookie/src/app/discount-report/discount-report.component.html @@ -0,0 +1,44 @@ + + + Discount Report + + + +
+
+ + + + + + + + + + + +
+
+ + + + + Name + {{row.name}} + + + + + Amount + {{row.amount | currency:'INR'}} + + + + +
+
diff --git a/bookie/src/app/discount-report/discount-report.component.spec.ts b/bookie/src/app/discount-report/discount-report.component.spec.ts new file mode 100644 index 0000000..74573d7 --- /dev/null +++ b/bookie/src/app/discount-report/discount-report.component.spec.ts @@ -0,0 +1,25 @@ +import {async, ComponentFixture, TestBed} from '@angular/core/testing'; + +import {DiscountReportComponent} from './discount-report.component'; + +describe('DiscountReportComponent', () => { + let component: DiscountReportComponent; + let fixture: ComponentFixture; + + beforeEach(async(() => { + TestBed.configureTestingModule({ + declarations: [DiscountReportComponent] + }) + .compileComponents(); + })); + + beforeEach(() => { + fixture = TestBed.createComponent(DiscountReportComponent); + component = fixture.componentInstance; + fixture.detectChanges(); + }); + + it('should create', () => { + expect(component).toBeTruthy(); + }); +}); diff --git a/bookie/src/app/discount-report/discount-report.component.ts b/bookie/src/app/discount-report/discount-report.component.ts new file mode 100644 index 0000000..6bbe4b0 --- /dev/null +++ b/bookie/src/app/discount-report/discount-report.component.ts @@ -0,0 +1,90 @@ +import { Component, OnInit } from '@angular/core'; +import { FormBuilder, FormGroup } from '@angular/forms'; +import { ActivatedRoute, Router } from '@angular/router'; +import * as moment from 'moment'; +import { DiscountReportDataSource } from './discount-report-datasource'; +import { DiscountReport } from './discount-report'; +import { ToCsvService } from '../shared/to-csv.service'; + +@Component({ + selector: 'app-discount-report', + templateUrl: './discount-report.component.html', + styleUrls: ['./discount-report.component.css'] +}) +export class DiscountReportComponent implements OnInit { + dataSource: DiscountReportDataSource; + form: FormGroup; + info: DiscountReport; + + /** Columns displayed in the table. Columns IDs can be added, removed, or reordered. */ + displayedColumns = ['name', 'amount']; + + + constructor( + private route: ActivatedRoute, + private router: Router, + private fb: FormBuilder, + private toCsv: ToCsvService + ) { + this.createForm(); + + } + + ngOnInit() { + this.route.data + .subscribe((data: { info: DiscountReport }) => { + this.info = data.info; + this.form.setValue({ + startDate: moment(this.info.startDate, 'DD-MMM-YYYY').toDate(), + finishDate: moment(this.info.finishDate, 'DD-MMM-YYYY').toDate() + }); + this.dataSource = new DiscountReportDataSource(this.info.amounts); + }); + } + + show() { + const info = this.getInfo(); + this.router.navigate(['discount-report'], { + queryParams: { + startDate: info.startDate, + finishDate: info.finishDate + } + }); + } + + createForm() { + this.form = this.fb.group({ + startDate: '', + finishDate: '' + }); + } + + getInfo(): DiscountReport { + const formModel = this.form.value; + + return { + startDate: moment(formModel.startDate).format('DD-MMM-YYYY'), + finishDate: moment(formModel.finishDate).format('DD-MMM-YYYY') + }; + } + + exportCsv() { + const headers = { + Date: 'date', + Name: 'name', + Type: 'type', + Narration: 'narration', + Debit: 'debit', + Credit: 'credit', + Running: 'running', + Posted: 'posted' + }; + const csvData = new Blob([this.toCsv.toCsv(headers, this.dataSource.data)], {type: 'text/csv;charset=utf-8;'}); + const link = document.createElement('a'); + link.href = window.URL.createObjectURL(csvData); + link.setAttribute('download', 'discount-report.csv'); + document.body.appendChild(link); + link.click(); + document.body.removeChild(link); + } +} diff --git a/bookie/src/app/discount-report/discount-report.module.spec.ts b/bookie/src/app/discount-report/discount-report.module.spec.ts new file mode 100644 index 0000000..f0a828e --- /dev/null +++ b/bookie/src/app/discount-report/discount-report.module.spec.ts @@ -0,0 +1,13 @@ +import {DiscountReportModule} from './discount-report.module'; + +describe('DiscountReportModule', () => { + let discountReportModule: DiscountReportModule; + + beforeEach(() => { + discountReportModule = new DiscountReportModule(); + }); + + it('should create an instance', () => { + expect(discountReportModule).toBeTruthy(); + }); +}); diff --git a/bookie/src/app/discount-report/discount-report.module.ts b/bookie/src/app/discount-report/discount-report.module.ts new file mode 100644 index 0000000..baeca1b --- /dev/null +++ b/bookie/src/app/discount-report/discount-report.module.ts @@ -0,0 +1,61 @@ +import { NgModule } from '@angular/core'; +import { CommonModule } from '@angular/common'; +import { MatAutocompleteModule } from '@angular/material/autocomplete'; +import { MatButtonModule } from '@angular/material/button'; +import { MatCardModule } from '@angular/material/card'; +import { DateAdapter, MAT_DATE_FORMATS, MAT_DATE_LOCALE, MatNativeDateModule } from '@angular/material/core'; +import { MatDatepickerModule } from '@angular/material/datepicker'; +import { MatFormFieldModule } from '@angular/material/form-field'; +import { MatIconModule } from '@angular/material/icon'; +import { MatInputModule } from '@angular/material/input'; +import { MatTableModule } from '@angular/material/table'; +import { SharedModule} from '../shared/shared.module'; +import { ReactiveFormsModule } from '@angular/forms'; +import { CdkTableModule } from '@angular/cdk/table'; +import { DiscountReportRoutingModule } from './discount-report-routing.module'; +import { DiscountReportComponent } from './discount-report.component'; +import { MomentDateAdapter } from '@angular/material-moment-adapter'; +import { A11yModule } from '@angular/cdk/a11y'; +import { FlexLayoutModule } from '@angular/flex-layout'; + +export const MY_FORMATS = { + parse: { + dateInput: 'DD-MMM-YYYY', + }, + display: { + dateInput: 'DD-MMM-YYYY', + monthYearLabel: 'MMM YYYY', + dateA11yLabel: 'DD-MMM-YYYY', + monthYearA11yLabel: 'MMM YYYY', + }, +}; + +@NgModule({ + imports: [ + A11yModule, + CommonModule, + CdkTableModule, + FlexLayoutModule, + MatAutocompleteModule, + MatButtonModule, + MatCardModule, + MatDatepickerModule, + MatFormFieldModule, + MatIconModule, + MatInputModule, + MatNativeDateModule, + MatTableModule, + ReactiveFormsModule, + SharedModule, + DiscountReportRoutingModule + ], + declarations: [ + DiscountReportComponent + ], + providers: [ + {provide: DateAdapter, useClass: MomentDateAdapter, deps: [MAT_DATE_LOCALE]}, + {provide: MAT_DATE_FORMATS, useValue: MY_FORMATS}, + ] +}) +export class DiscountReportModule { +} diff --git a/bookie/src/app/discount-report/discount-report.service.spec.ts b/bookie/src/app/discount-report/discount-report.service.spec.ts new file mode 100644 index 0000000..667e395 --- /dev/null +++ b/bookie/src/app/discount-report/discount-report.service.spec.ts @@ -0,0 +1,15 @@ +import {inject, TestBed} from '@angular/core/testing'; + +import {DiscountReportService} from './discount-report.service'; + +describe('DiscountReportService', () => { + beforeEach(() => { + TestBed.configureTestingModule({ + providers: [DiscountReportService] + }); + }); + + it('should be created', inject([DiscountReportService], (service: DiscountReportService) => { + expect(service).toBeTruthy(); + })); +}); diff --git a/bookie/src/app/discount-report/discount-report.service.ts b/bookie/src/app/discount-report/discount-report.service.ts new file mode 100644 index 0000000..74da45f --- /dev/null +++ b/bookie/src/app/discount-report/discount-report.service.ts @@ -0,0 +1,36 @@ +import { Injectable } from '@angular/core'; +import { catchError } from 'rxjs/operators'; +import { Observable } from 'rxjs/internal/Observable'; +import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http'; +import { DiscountReport } from './discount-report'; +import { ErrorLoggerService } from '../core/error-logger.service'; + +const httpOptions = { + headers: new HttpHeaders({'Content-Type': 'application/json'}) +}; + +const url = '/v1/discount-report'; +const serviceName = 'DiscountReportService'; + +@Injectable({ + providedIn: 'root' +}) +export class DiscountReportService { + + constructor(private http: HttpClient, private log: ErrorLoggerService) { + } + + get(startDate: string, finishDate): Observable { + const options = {params: new HttpParams()}; + if (startDate !== null) { + options.params = options.params.set('s', startDate); + } + if (finishDate !== null) { + options.params = options.params.set('f', finishDate); + } + return >this.http.get(url, options) + .pipe( + catchError(this.log.handleError(serviceName, 'get')) + ); + } +} diff --git a/bookie/src/app/discount-report/discount-report.ts b/bookie/src/app/discount-report/discount-report.ts new file mode 100644 index 0000000..dec3a53 --- /dev/null +++ b/bookie/src/app/discount-report/discount-report.ts @@ -0,0 +1,10 @@ +export class DiscountReportItem { + name: string; + amount: number; +} + +export class DiscountReport { + startDate: string; + finishDate: string; + amounts?: DiscountReportItem[]; +} diff --git a/bookie/src/app/home/home.component.html b/bookie/src/app/home/home.component.html index c1411de..efeff58 100644 --- a/bookie/src/app/home/home.component.html +++ b/bookie/src/app/home/home.component.html @@ -29,6 +29,9 @@

Beer Consumption Report

+ +

Discount Report

+

Tables