From: Galen Charlton <gmc@equinoxinitiative.org>
Date: Tue, 6 Mar 2018 23:00:23 +0000 (-0500)
Subject: LP#1721807: fix webstaff report templates that have might_have and has_many joins
X-Git-Url: https://old-git.evergreen-ils.org/?a=commitdiff_plain;h=88bdd77b97a85bd6c41b174faa2e828c34d3a948;p=evergreen%2Fequinox.git

LP#1721807: fix webstaff report templates that have might_have and has_many joins

This patch fixes a bug where using a virtual field in the web staff
report template editor to join another table would result in a syntax
error in the generated SQL.

To test
-------
[1] Apply the patch and verify that the regression test in the previous
    commit passes.
[2] Create new report template in the web staff client that include
    virtual fields and joins and verify that they work. One example
    would be a report that uses ILS User as the base source and joins
    in the patron notes virtual field.
[3] If available, clone a report template originally created in the web
    staff client that had previously failed, then save it. The new
    report should work.

Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Signed-off-by: Chris Sharp <csharp@georgialibraries.org>
Signed-off-by: Kathy Lussier <klussier@masslnc.org>
---

diff --git a/Open-ILS/web/js/ui/default/staff/reporter/template/app.js b/Open-ILS/web/js/ui/default/staff/reporter/template/app.js
index 9ea625ed75..b50dabc021 100644
--- a/Open-ILS/web/js/ui/default/staff/reporter/template/app.js
+++ b/Open-ILS/web/js/ui/default/staff/reporter/template/app.js
@@ -119,7 +119,14 @@ function($scope , $q , $routeParams , $location , $timeout , $window,  egCore ,
                 if (i) { // not at the top of the tree
                     if (i == 1) join_path = join_path.split('-')[0];
 
-                    var uplink = p.uplink.name;
+                    // SQLBuilder relies on the first dash-separated component
+                    // of the join key to specify the column of left-hand relation
+                    // to join on; for has_many and might_have link types, we have to grab the
+                    // primary key of the left-hand table; otherwise, we can
+                    // just use the field/column name found in p.uplink.name.
+                    var uplink = (p.uplink.reltype == 'has_many' || p.uplink.reltype == 'might_have') ?
+                        egCore.idl.classes[p.from.split('.').slice(-1)[0]].pkey + '-' + p.uplink.name :
+                        p.uplink.name;
                     join_path += '-' + uplink;
                     alias = hex_md5(join_path);