518a196aa1b637d0d490b62e4da45c7b43acabde
[xtuple] / enyo-client / extensions / source / billing / database / source / xm / javascript / billing.sql
1 select xt.install_js('XM','Billing','xtuple', $$
2 /* Copyright (c) 1999-2011 by OpenMFG LLC, d/b/a xTuple.
3    See www.xtuple.com/CPAL for the full text of the software license. */
4
5 (function () {
6   var billingOptions = [
7     "CCValidDays",
8     "InvcNumberGeneration",
9     "NextARMemoNumber",
10     "NextCashRcptNumber",
11     "HideApplyToBalance",
12     "EnableCustomerDeposits",
13     "CreditTaxDiscount",
14     "remitto_name",
15     "remitto_address1",
16     "remitto_address2",
17     "remitto_address3",
18     "remitto_city",
19     "remitto_state",
20     "remitto_zipcode",
21     "remitto_country",
22     "remitto_phone",
23     "AutoCreditWarnLateCustomers",
24     "DefaultAutoCreditWarnGraceDays",
25     "RecurringInvoiceBuffer",
26     "DefaultARIncidentStatus",
27     "AutoCloseARIncident"
28   ],
29     i, option;
30
31   if (XM.Billing) {
32     for(i = 0; i < billingOptions.length; i++) {
33       option = billingOptions[i];
34       if(!XM.Billing.options.contains(option)) {
35         XM.Billing.options.push(option);
36       }
37     }
38
39   } else {
40     XM.Billing = {};
41     XM.Billing.options = billingOptions;
42   }
43
44   XM.Billing.isDispatchable = true;
45
46   /**
47     Return Billing configuration settings.
48
49     @returns {Object}
50   */
51   XM.Billing.settings = function() {
52
53     var keys = XM.Billing.options.slice(0),
54       data = Object.create(XT.Data),
55       ret = {},
56       qry,
57       orm;
58
59     ret.NextARMemoNumber = plv8.execute('select currentARMemoNumber() as value', [])[0].value;
60     ret.NextCashRcptNumber = plv8.execute('select currentCashRcptNumber() as value',[])[0].value;
61
62     ret = XT.extend(data.retrieveMetrics(keys), ret);
63     return ret;
64   }
65
66   /**
67     Update Billing configuration settings. Only valid options as defined in the array
68     XM.Billing.options will be processed.
69
70     @param {Object} settings
71     @returns {Boolean}
72   */
73   XM.Billing.commitSettings = function(patches) {
74     var sql, settings, options = XM.Billing.options.slice(0),
75         data = Object.create(XT.Data), metrics = {};
76
77     /* check privileges */
78     if(!data.checkPrivilege('ConfigureAR')) throw new Error('Access Denied');
79
80     /* Compose our commit settings by applying the patch to what we already have */
81     settings = XM.Billing.settings();
82     if (!XT.jsonpatch.apply(settings, patches)) {
83       plv8.elog(NOTICE, 'Malformed patch document');
84     }
85
86     if (settings['NextARMemoNumber']) {
87       plv8.execute('select setNextARMemoNumber($1)', [settings['NextARMemoNumber'] - 0]);
88     }
89     options.remove('NextARMemoNumber');
90
91     if (settings['NextCashRcptNumber']) {
92       plv8.execute('select setNextCashRcptNumber($1)', [settings['NextCashRcptNumber'] - 0]);
93     }
94     options.remove('NextCashRcptNumber');
95
96     /* update remaining options as metrics
97        first make sure we pass an object that only has valid metric options for this type */
98     for(var i = 0; i < options.length; i++) {
99       var prop = options[i];
100       if(settings[prop] !== undefined) metrics[prop] = settings[prop];
101     }
102
103     return data.commitMetrics(metrics);
104   }
105
106 }());
107
108 $$ );