LP#1380803 Update PO summary amounts
[working/Evergreen.git] / Open-ILS / web / js / ui / default / acq / po / item_table.js
1 function PoItemTable() {
2     var self = this;
3
4     this.init = function(po, pcrud) {
5         this.po = po;
6         this.pcrud = pcrud || new openils.PermaCrud();
7
8         this.tHead = dojo.byId("acq-po-item-table-headings");
9         this.tBody = dojo.byId("acq-po-item-table-items");
10         this.template = this.tBody.removeChild(dojo.query("tr", this.tBody)[0]);
11         dojo.byId("acq-po-item-table-new-charge").onclick = function() {
12             self.addItem();
13         };
14         dojo.byId("acq-po-item-table-save-new").onclick = function() {
15             self.saveNew();
16         };
17
18         this.fundAWArgs = {
19             "searchFilter": {"active": "t"},
20             "searchFormat": ["${0} (${1})", "code", "year"],
21             "labelFormat": [
22                 "<span class='fund_${0}'>${1} (${2})</span>",
23                 "id", "code", "year"
24             ],
25             "dijitArgs": {"labelType": "html"},
26             "noCache": true
27         };
28
29         // limit funds fetched to those the user can use
30         new openils.User().getPermOrgList(
31             ['CREATE_PURCHASE_ORDER', 'MANAGE_FUND'],
32             function(orgs) { self.fundAWArgs.searchFilter.org = orgs },
33             true, true // descendants, id_list
34         );
35
36         this.reset();
37     };
38
39     this.empty = function(which) {
40         if (this._empty == which) return; /* nothing to do */
41
42         openils.Util[which ? "show" : "hide"]("acq-po-item-table-i-am-empty");
43         openils.Util[which ? "hide" : "show"](this.tHead, "table-header-group");
44         this._empty = which;
45     };
46
47     this.reset = function() {
48         this.rowId = -1;
49         this.rows = {};
50         this.realItems = {};
51         dojo.empty(this.tBody);
52         this.empty(true);
53
54         this.disableSave();
55     };
56
57     this.hide = function() { openils.Util.hide("acq-po-item-table"); };
58
59     this.show = function() { openils.Util.show("acq-po-item-table"); };
60
61     this.disableSave = function() {
62         dojo.byId("acq-po-item-table-save-new").disabled = true;
63     };
64
65     this.rowIndices = function() {
66         return openils.Util.objectProperties(this.rows);
67     };
68
69     this.newRowIndices = function() {
70         return this.rowIndices().filter(function(o) { return o < 0; });
71     };
72
73     this.saveNew = function() {
74         var virtIds = this.newRowIndices();
75         var po_items = virtIds.map(
76             function(k) {
77                 var widgets = self.rows[k];
78                 var po_item = new acqpoi();
79                 for (var field in widgets)
80                     po_item[field](widgets[field].attr("value"));
81                 po_item.purchase_order(self.po.id());
82                 return po_item;
83             }
84         );
85
86         progressDialog.show(true);
87
88         pcrud.create(
89             po_items, {
90                 "oncomplete": function(r, objs) {
91                     progressDialog.hide();
92                     r = openils.Util.readResponse(r); /* may not use */
93
94                     virtIds.forEach(function(k) { self.deleteRow(k); });
95                     objs.forEach(function(o) { self.addItem(o); });
96                     refreshPOSummaryAmounts();
97                 }
98             }
99         );
100     };
101
102     this._deleteRow = function(id) {
103         dojo.destroy(dojo.query("[rowId='" + id + "']")[0]);
104         delete this.rows[id];
105         delete this.realItems[id];
106
107         if (!this.rowIndices().length) this.reset();
108         else if (!this.newRowIndices().length) this.disableSave();
109     };
110
111     this.deleteRow = function(id) {
112         if (id > 0) {
113             progressDialog.show(true);
114             fieldmapper.standardRequest(
115                 ['open-ils.acq', 'open-ils.acq.po_item.delete'],
116                 {   async : true,
117                     params: [openils.User.authtoken, id],
118                     oncomplete : function(r) {
119                         progressDialog.hide();
120                         r = openils.Util.readResponse(r); /* may not use */
121                         if (r == '1') {
122                             refreshPOSummaryAmounts();
123                             self._deleteRow(id);
124                         } 
125                     }
126                 }
127             );
128         } else {
129             this._deleteRow(id);
130         }
131     };
132
133     this._addItemRow = function(item) {
134         var ourId = item ? item.id() : this.rowId--;
135
136         if (item)
137             this.realItems[ourId] = item;
138
139         this.rows[ourId] = {};
140         var row = dojo.clone(this.template);
141         dojo.attr(row, "rowId", ourId);
142
143         nodeByName("delete", row).onclick = function() {
144             self.deleteRow(ourId);
145         };
146
147         return {"id": ourId, "node": row};
148     };
149
150     /* add a row with widgets for the user to enter new data */
151     this.addItem = function(item) {
152         var row = this._addItemRow(item);
153
154         dojo.query("td[name]", row.node).forEach(
155             function(element) {
156                 var field = dojo.attr(element, "name");
157                 var em = dojo.attr(element, "em");
158                 var awArgs = dojo.mixin(
159                     {
160                         "fmField": field,
161                         "parentNode": dojo.create(
162                             "div", {"style": "width: " +
163                                 String(Number(em) + 1) + "em"},
164                             element, "only"
165                         ),
166                         "orgLimitPerms": ["CREATE_PURCHASE_ORDER"],
167                         "dijitArgs": {"style": "width: " + em + "em"},
168                         "readOnly": Boolean(item)
169                     },
170                     (field == "fund" ? self.fundAWArgs : {}),
171                     (item ? {"fmObject": item} : {"fmClass": "acqpoi"})
172                 );
173                 new openils.widget.AutoFieldWidget(awArgs).build(
174                     function(w) { self.rows[row.id][field] = w; }
175                 );
176             }
177         );
178
179         this.empty(false);
180
181         dojo.place(row.node, this.tBody, "last");
182         if (!item)
183             dojo.byId("acq-po-item-table-save-new").disabled = false;
184     };
185
186     this.init.apply(this, arguments);
187 }