Initial dev repository
[kcls-web.git] / js / ui / default / acq / lineitem / related.js
diff --git a/js/ui/default/acq/lineitem/related.js b/js/ui/default/acq/lineitem/related.js
new file mode 100644 (file)
index 0000000..c898314
--- /dev/null
@@ -0,0 +1,144 @@
+dojo.require("openils.acq.Lineitem");
+dojo.require("openils.Util");
+dojo.require("openils.XUL");
+dojo.require("openils.CGI");
+dojo.require("openils.PermaCrud");
+dojo.require('openils.BibTemplate');
+dojo.require('fieldmapper.OrgUtils');
+
+var liTable;
+var identTarget;
+var bibRecord;
+var paramPL;
+var paramPO;
+
+function fetchLi() {
+    fieldmapper.standardRequest(
+        ["open-ils.acq", "open-ils.acq.lineitem.retrieve.authoritative"], {
+            "async": true,
+            "params": [openils.User.authtoken, targetId, {
+                "flesh_attrs": true,
+                "flesh_li_details": true,
+                "flesh_fund_debit": true,
+                "flesh_cancel_reason": true
+            }],
+            "oncomplete": function(r) {
+                var li = openils.Util.readResponse(r);
+                fetchBib(li.eg_bib_id());
+            }
+        }
+    );
+}
+
+
+function fetchRelated() {
+    var method = 'open-ils.acq.lineitems_for_bib.by_lineitem_id';
+    if(identTarget == 'bib')
+        var method = 'open-ils.acq.lineitems_for_bib.by_bib_id';
+
+    var total = 0;
+    fieldmapper.standardRequest(
+        ["open-ils.acq", method], {
+            "async": true,
+            "params": [openils.User.authtoken, targetId, {
+                "flesh_attrs": true,
+                "flesh_notes": true,
+                "flesh_cancel_reason": true
+            }],
+            "onresponse": function(r) {
+                var resp = openils.Util.readResponse(r);
+                if (resp) {
+                    total++;
+                    liTable.show("list");
+                    liTable.addLineitem(resp);
+                }
+            }
+        }
+    );
+}
+
+function fetchBib(bibId) {
+    bibId = bibId || targetId;
+    new openils.BibTemplate({ 
+        record : bibId, 
+        org_unit : fieldmapper.aou.findOrgUnit(openils.User.user.ws_ou()).shortname()
+    }).render();
+
+    new openils.PermaCrud().retrieve('bre', bibId, {
+        oncomplete : function(r) {
+            bibRecord = openils.Util.readResponse(r);
+            // render bib details
+            // perhaps we just pull these from the beating heart of bibtemplate
+        }
+    }) 
+}
+
+function createLi(oncomplete) {
+    return function() {
+        progressDialog.show();
+        liTable.reset();
+        fieldmapper.standardRequest(
+            ["open-ils.acq", "open-ils.acq.biblio.create_by_id"], {
+                "params": [
+                    openils.User.authtoken, [bibRecord.id()], {
+                        "flesh_attrs": true,
+                        "flesh_cancel_reason": true,
+                        "flesh_notes": true
+                    }
+                ],
+                "async": false,
+                "onresponse": function(r) {
+                    var li = openils.Util.readResponse(r);
+                    if (typeof(li) == "object") {
+                        liTable.show("list");
+                        liTable.addLineitem(li);
+                        dojo.query(
+                            "input[name='selectbox']", liTable._findLiRow(li)
+                        )[0].checked = true;
+                    }
+                },
+                "oncomplete": function() {
+                    progressDialog.hide();
+                    oncomplete();
+                }
+            }
+        );
+    };
+}
+
+function prepareButtons() {
+    addToPlButton.onClick = createLi(
+        function() { /* oncomplete */
+            acqLitSavePlDialog.show();
+        }
+    );
+    createPoButton.onClick = createLi(
+        function() { /* oncomplete */
+            liTable._loadPOSelect();
+            acqLitPoCreateDialog.show();
+        }
+    );
+}
+
+function load() {
+    var cgi = new openils.CGI();
+
+    identTarget = cgi.param('target');
+    paramPL = cgi.param('pl');
+//    paramPO = cgi.param('po');
+
+    if (identTarget == 'bib') {
+        fetchBib();
+    } else {
+        fetchLi(); 
+    }
+
+    liTable = new AcqLiTable();
+    liTable.reset();
+    liTable._isRelatedViewer = true;
+
+    prepareButtons();
+    fetchRelated();
+}
+
+openils.Util.addOnLoad(load);