fdf61aa9e4843ec969e8988c19083f6cd9a73aa6
[xtuple] / enyo-client / database / source / xm / javascript / account.sql
1 select xt.install_js('XM','Account','xtuple', $$
2   /* Copyright (c) 1999-2011 by OpenMFG LLC, d/b/a xTuple. 
3      See www.xm.ple.com/CPAL for the full text of the software license. */
4   
5   XM.Account = {};
6
7   XM.Account.isDispatchable = true,
8   
9   XM.Account.findExisting = function(key, value, id) {
10     /* look in crmacct for any records that have num */
11     /* if you don't find one, return 0 meaning you didn't find anything
12       if you do find a result, look to see if prospect or cust columns are populated.
13       return an object where 1 property is the crmacct_id and the other is A for account, P for prospect or C for customer*/
14       /* use result.crmacct_id to get the crmacct_id column.  the result of the query will be an array so get the 1st result */
15       
16     var res,
17       retVal,
18       row,
19       sql = "select crmacct_number, cust_number, prospect_number " +
20             "from crmacct " +
21             "left join custinfo on cust_id = crmacct_cust_id " +
22             "left join prospect on prospect_id = crmacct_prospect_id " +
23             " where crmacct_number = $1";
24         
25     res = XM.Model.findExisting("XM.Account", key, value, id);
26     
27     /*if the result is falsey, no CRM account exists by this name, and return 0. */
28     if (!res) {
29       retVal = 0;
30     } else {
31       row = plv8.execute(sql, [res])[0];
32       retVal = {};
33       if (row.cust_number) {
34         retVal.id = row.cust_number;
35         retVal.type = "C";
36       } else if (row.prospect_number) {
37         retVal.id = row.prospect_number;
38         retVal.type = "P";
39       } else {
40         retVal.id = row.crmacct_number;
41         retVal.type = "A";
42       }
43     }
44     
45     return retVal;
46   }
47
48   XM.Account.used = function(id) {
49     var sql = "select (crmacct_cust_id is null" +
50         " and crmacct_prospect_id is null" +
51         " and crmacct_emp_id is null" +
52         " and crmacct_salesrep_id is null" +
53         " and crmacct_taxauth_id is null" +
54         " and crmacct_vend_id is null" +
55         " and crmacct_usr_username is null) as result " +
56         "from crmacct where crmacct_number = $1",
57       res = plv8.execute(sql, [id])[0].result;
58      return res ? XM.Model.used('XM.Account', id) : true;
59   }
60
61 $$ );
62