1 select xt.install_js('XM','Customer','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. */
7 if (!XM.Customer) { XM.Customer = {}; }
9 XM.Customer.isDispatchable = true;
12 Determine whether a customer can purchase a given item
14 @param {Number} Customer id
15 @param {Number} Item id
16 @param {Date} Schedule date
17 @param {Number} Shipto id
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;',
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;
30 Returns an object with a price and type for a given customer, item and quantity.
32 @param {Number} customer id
33 @param {Number} item id
34 @param {Number} quantity
35 @param {Object} options: asOf, shiptoId, quantityUnitId, priceUnitId, currencyId, effective
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",
55 } else if (!customerId) {
57 } else if (!quantity) {
60 if(err) { plv8.elog(ERROR, err + " is required.") }
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;
77 result = { price: result.itemprice_price, type: result.itemprice_type };
82 Returns a price for a given customer, item, characteristic and quantity.
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
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;",
98 shiptoId = options.shiptoId || -1,
107 } else if (!customerId) {
108 err = "Characteristic";
109 } else if (!customerId) {
111 } else if (!quantity) {
114 if(err) { plv8.elog(ERROR, err + " is required.") }
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;
127 Return whether a Customer is referenced by another table.
129 @param {String} Customer Number
131 XM.Customer.used = function(id) {
132 var exceptions = ["public.crmacct"];
133 return XM.PrivateModel.used("XM.Customer", id, exceptions);