update misc copyright dates
[xtuple] / enyo-client / database / source / xm / javascript / customer.sql
1 select xt.install_js('XM','Customer','xtuple', $$
2 /* Copyright (c) 1999-2014 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
7   if (!XM.Customer) { XM.Customer = {}; }
8
9   XM.Customer.isDispatchable = true;
10
11   /**
12     Determine whether a customer can purchase a given item
13
14     @param {Number} Customer id
15     @param {Number} Item id
16     @param {Date} Schedule date
17     @param {Number} Shipto id
18     @returns {Boolean}
19   */
20   XM.Customer.canPurchase = function (customerId, itemId, scheduleDate, shiptoId) {
21     var sql = 'select customerCanPurchase(item_id, cust_id, $3, $4::date) as canpurchase ' +
22               'from custinfo, item where item_number = $1 and cust_number = $2;',
23       ret;
24     shiptoId = shiptoId && XT.Data.getId(XT.Orm.fetch('XM','CustomerShipto'), shiptoId);
25     ret = plv8.execute(sql, [itemId, customerId, shiptoId, scheduleDate]);
26     return ret.length ? ret[0].canpurchase : true;
27   };
28   
29   /**
30    Returns an object with a price and type for a given customer, item and quantity.
31
32    @param {Number} customer id
33    @param {Number} item id
34    @param {Number} quantity
35    @param {Object} options:  asOf, shiptoId, quantityUnitId, priceUnitId, currencyId, effective
36    @returns Object 
37   */
38   XM.Customer.itemPrice = function(customerId, itemId, quantity, options) {
39     options = options || {};
40     var sql = "select itemipsprice(item_id, cust_id, $3, $4, $5, $6, $7, $8::date, $9::date, null) as result " +
41               "from custinfo, item where item_number = $1 and cust_number = $2",
42       today = new Date(),
43       shiptoId,
44       quantityUnitId,
45       currencyId,
46       priceUnitId,
47       currencyId,
48       effective,
49       asOf,
50       result,
51       err;
52
53     if (!itemId) {
54       err = "Item";
55     } else if (!customerId) {
56       err = "Customer";
57     } else if (!quantity) {
58       err = "Quantity" 
59     };
60     if(err) { plv8.elog(ERROR, err + " is required.") }
61
62     shiptoId = options.shiptoId ?
63        XT.Data.getId(XT.Orm.fetch('XM', 'CustomerShipto'), options.shiptoId) : -1; 
64     quantityUnitId = options.quantityUnitId ?
65        XT.Data.getId(XT.Orm.fetch('XM', 'Unit'), options.quantityUnitId) :
66        plv8.execute("select item_inv_uom_id as result from item where item_id = $1;", [itemId])[0].result; 
67     priceUnitId = options.priceUnitId ?
68       XT.Data.getId(XT.Orm.fetch('XM', 'Unit'), options.priceUnitId) :
69       plv8.execute("select item_price_uom_id as result from item where item_id = $1;", [itemId])[0].result;
70     currencyId = options.currencyId ?
71       XT.Data.getId(XT.Orm.fetch('XM', 'Currency'), options.currencyId) :
72       lplv8.execute("select basecurrid() as result")[0].result;
73     effective = options.effective ? new Date(options.effective) : today;
74     asOf = options.asOf ? new Date(options.asOf) : today;
75     result = plv8.execute(sql, [itemId, customerId, shiptoId, quantity, quantityUnitId, priceUnitId, currencyId, effective, asOf])[0].result;
76
77     result = { price: result.itemprice_price, type: result.itemprice_type };
78     return result; 
79   }
80
81   /**
82    Returns a price for a given customer, item, characteristic and quantity.
83
84    @param {Number} customer id
85    @param {Number} item id
86    @param {Number} characteristic id
87    @param {Number} characteristic value
88    @param {Number} quantity
89    @param {Object} options:  asOf, shiptoId, currencyId, effective
90    @returns Object 
91   */
92   XM.Customer.characteristicPrice = function(customerId, itemId, characteristicId, value, quantity, options) {
93     options = options || {};
94     var sql = "select itemcharprice(item_id, char_id, $3, cust_id, $5, $6, $7, $8::date, $9::date) as result " +
95               "from item, custinfo, char " +
96               "where item_number = $1 and char_name = $2 and cust_number = $4;",
97       today = new Date(),
98       shiptoId = options.shiptoId || -1,
99       currencyId,
100       effective,
101       asOf,
102       result,
103       err;
104
105     if (!itemId) {
106       err = "Item";
107     } else if (!customerId) {
108       err = "Characteristic";
109     } else if (!customerId) {
110       err = "Customer";
111     } else if (!quantity) {
112       err = "Quantity" 
113     };
114     if(err) { plv8.elog(ERROR, err + " is required.") }
115
116     currencyId = options.currencyId ?
117       XT.Data.getId(XT.Orm.fetch('XM', 'Currency'), options.currencyId) :
118       plv8.execute("select basecurrid() as result")[0].result;
119     effective = options.effective ? new Date(options.effective) : today;
120     asOf = options.asOf ? new Date(options.asOf) : today;
121     result = plv8.execute(sql, [itemId, characteristicId, value, customerId, shiptoId, quantity, currencyId, effective, asOf])[0].result;
122
123     return result; 
124   }
125
126   /**
127     Return whether a Customer is referenced by another table.
128     
129     @param {String} Customer Number
130   */
131   XM.Customer.used = function(id) {
132     var exceptions = ["public.crmacct"];
133     return XM.PrivateModel.used("XM.Customer", id, exceptions);
134   };
135
136 }());
137   
138 $$ );