LP1859701 Cash Reports Tidying
[evergreen-equinox.git] / Open-ILS / src / eg2 / src / app / staff / admin / local / cash-reports / cash-reports.component.ts
1 import {Component, OnInit, Input, ViewChild} from '@angular/core';
2 import {GridComponent} from '@eg/share/grid/grid.component';
3 import {GridDataSource, GridColumn, GridRowFlairEntry} from '@eg/share/grid/grid';
4 import {IdlService} from '@eg/core/idl.service';
5 import {NetService} from '@eg/core/net.service';
6 import {AuthService} from '@eg/core/auth.service';
7 import {OrgService} from '@eg/core/org.service';
8 import {UserDialogComponent} from './user-dialog.component';
9
10 class DeskTotals {
11     cash_payment = 0;
12     check_payment = 0;
13     credit_card_payment = 0;
14 }
15
16 class UserTotals {
17     forgive_payment = 0;
18     work_payment = 0;
19     credit_payment = 0;
20     goods_payment = 0;
21 }
22
23 @Component({
24     templateUrl: './cash-reports.component.html'
25 })
26 export class CashReportsComponent implements OnInit {
27     initDone = false;
28     deskPaymentDataSource: GridDataSource = new GridDataSource();
29     userPaymentDataSource: GridDataSource = new GridDataSource();
30     userDataSource: GridDataSource = new GridDataSource();
31     deskIdlClass = 'mwps';
32     userIdlClass = 'mups';
33     selectedOrg = this.org.get(this.auth.user().ws_ou());
34     today = new Date();
35     startDate = `${this.today.getFullYear()}-${String(this.today.getMonth() + 1).padStart(2, '0')}-${String(this.today.getDate()).padStart(2, '0')}`;
36     endDate = `${this.today.getFullYear()}-${String(this.today.getMonth() + 1).padStart(2, '0')}-${String(this.today.getDate()).padStart(2, '0')}`;
37     deskTotals = new DeskTotals();
38     userTotals = new UserTotals();
39     disabledOrgs = [];
40     activeTab = 'deskPayments';
41
42     // Default sort field, used when no grid sorting is applied.
43     @Input() sortField: string;
44     @ViewChild('userDialog') userDialog: UserDialogComponent;
45     @ViewChild('deskPaymentGrid') deskPaymentGrid: GridComponent;
46     @ViewChild('userPaymentGrid') userPaymentGrid: GridComponent;
47     @ViewChild('userGrid') userGrid: GridComponent;
48
49     constructor(
50         private idl: IdlService,
51         private net: NetService,
52         private org: OrgService,
53         private auth: AuthService) {}
54
55     ngOnInit() {
56         this.disabledOrgs = this.getFilteredOrgList();
57         this.searchForData(this.startDate, this.endDate);
58     }
59
60     onRowActivate(userObject) {
61         if (userObject.user && this.userDataSource.data.length === 0) {
62             this.userDataSource.data = [userObject.user];
63             this.showUserInformation();
64         } else {
65             this.eraseUserGrid();
66         }
67     }
68
69     showUserInformation() {
70         return this.userDialog.open({size: 'lg'}).toPromise();
71     }
72
73     searchForData(start, end) {
74         this.userDataSource.data = [];
75         this.fillGridData(this.deskIdlClass, 'deskPaymentDataSource',
76             this.net.request(
77                 'open-ils.circ',
78                 'open-ils.circ.money.org_unit.desk_payments',
79                 this.auth.token(), this.selectedOrg.id(), start, end));
80
81         this.fillGridData(this.userIdlClass, 'userPaymentDataSource',
82             this.net.request(
83                 'open-ils.circ',
84                 'open-ils.circ.money.org_unit.user_payments',
85                 this.auth.token(), this.selectedOrg.id(), start, end));
86     }
87
88     fillGridData(idlClass, dataSource, data) {
89         data.subscribe((result) => {
90             if (idlClass === this.userIdlClass) {
91                 result.forEach((userObject, index) => {
92                     result[index].user = userObject.usr();
93                     result[index].usr(userObject.usr().usrname());
94                 });
95             }
96             this[dataSource].data = result;
97             this.eraseUserGrid();
98         });
99     }
100
101     eraseUserGrid() {
102         this.userDataSource.data = [];
103     }
104
105     getDeskTotal(idlObjects) {
106         this.deskTotals = new DeskTotals();
107
108         if (idlObjects.length > 0) {
109             const idlObjectFormat = this.idl.create('mwps');
110             idlObjects.forEach((idlObject) => {
111                 this.deskTotals['cash_payment'] += parseFloat(idlObject.cash_payment());
112                 this.deskTotals['check_payment'] += parseFloat(idlObject.check_payment());
113                 this.deskTotals['credit_card_payment'] += parseFloat(idlObject.credit_card_payment());
114             });
115             idlObjectFormat.cash_payment(this.deskTotals['cash_payment']);
116             idlObjectFormat.check_payment(this.deskTotals['check_payment']);
117             idlObjectFormat.credit_card_payment(this.deskTotals['credit_card_payment']);
118             return idlObjectFormat;
119         }
120     }
121
122     getUserTotal(idlObjects) {
123         this.userTotals = new UserTotals();
124         if (idlObjects.length > 0) {
125             const idlObjectFormat = this.idl.create('mups');
126             idlObjects.forEach((idlObject, index) => {
127                 this.userTotals['forgive_payment'] += parseFloat(idlObject.forgive_payment());
128                 this.userTotals['work_payment'] += parseFloat(idlObject.work_payment());
129                 this.userTotals['credit_payment'] += parseFloat(idlObject.credit_payment());
130                 this.userTotals['goods_payment'] += parseFloat(idlObject.goods_payment());
131                 this.userDataSource.data = idlObjects[index].usr();
132             });
133             idlObjectFormat.forgive_payment(this.userTotals['forgive_payment']);
134             idlObjectFormat.work_payment(this.userTotals['work_payment']);
135             idlObjectFormat.credit_payment(this.userTotals['credit_payment']);
136             idlObjectFormat.goods_payment(this.userTotals['goods_payment']);
137             return idlObjectFormat;
138         }
139     }
140
141     getFilteredOrgList() {
142         const orgFilter = {canHaveUsers: false};
143         return this.org.filterList(orgFilter, true);
144     }
145
146     onStartDateChange(date) {
147         this.startDate = date;
148     }
149
150     onEndDateChange(date) {
151         this.endDate = date;
152     }
153
154     onOrgChange(org) {
155         this.selectedOrg = org;
156         this.searchForData(this.startDate, this.endDate);
157     }
158 }