]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/eg2/src/app/staff/acq/po/charges.component.ts
LP1929741 ACQ Selection List & PO Angluar Port
[Evergreen.git] / Open-ILS / src / eg2 / src / app / staff / acq / po / charges.component.ts
1 import {Component, OnInit, Input} from '@angular/core';
2 import {Router, ActivatedRoute, ParamMap} from '@angular/router';
3 import {IdlService, IdlObject} from '@eg/core/idl.service';
4 import {PcrudService} from '@eg/core/pcrud.service';
5 import {ComboboxEntry} from '@eg/share/combobox/combobox.component';
6 import {PoService} from './po.service';
7
8 @Component({
9   templateUrl: 'charges.component.html',
10   selector: 'eg-acq-po-charges'
11 })
12 export class PoChargesComponent implements OnInit {
13
14     showBody = false;
15     autoId = -1;
16
17     constructor(
18         private idl: IdlService,
19         private pcrud: PcrudService,
20         public  poService: PoService
21     ) {}
22
23     ngOnInit() {
24         this.poService.poRetrieved.subscribe(() => {
25             if (this.po().po_items().length > 0) {
26                 this.showBody = true;
27             }
28         });
29     }
30
31     po(): IdlObject {
32         return this.poService.currentPo;
33     }
34
35     newCharge() {
36         this.showBody = true;
37         const chg = this.idl.create('acqpoi');
38         chg.isnew(true);
39         chg.purchase_order(this.po().id());
40         chg.id(this.autoId--);
41         this.po().po_items().push(chg);
42     }
43
44     saveCharge(charge: IdlObject) {
45         if (!charge.inv_item_type()) { return; }
46
47         charge.id(undefined);
48         this.pcrud.create(charge).toPromise()
49         .then(item => {
50             charge.id(item.id());
51             charge.isnew(false);
52         })
53         .then(_ => this.poService.refreshOrderSummary());
54     }
55
56     removeCharge(charge: IdlObject) {
57         this.po().po_items( // remove local copy
58             this.po().po_items().filter(item => item.id() !== charge.id())
59         );
60
61         if (!charge.isnew()) {
62             this.pcrud.remove(charge).toPromise()
63             .then(_ => this.poService.refreshOrderSummary());
64         }
65     }
66 }
67