3 * Table Definition for accnt
5 require_once 'DB/DataObject.php';
7 class Pman_Xtuple_DataObjects_Accnt extends DB_DataObject
10 /* the code below is auto generated do not remove the above tag */
12 public $__table = 'accnt'; // table name
13 public $accnt_id; // int4(4) not_null default_nextval%28%28accnt_accnt_id_seq%29%3A%3Aregclass%29 primary_key
14 public $accnt_number; // text(-1)
15 public $accnt_descrip; // text(-1)
16 public $accnt_comments; // text(-1)
17 public $accnt_profit; // text(-1)
18 public $accnt_sub; // text(-1)
19 public $accnt_type; // bpchar(-1) not_null
20 public $accnt_extref; // text(-1)
21 public $accnt_company; // text(-1)
22 public $accnt_closedpost; // bool(1)
23 public $accnt_forwardupdate; // bool(1)
24 public $accnt_subaccnttype_code; // text(-1)
25 public $accnt_curr_id; // int4(4) default_basecurrid%28%29
26 public $accnt_active; // bool(1) not_null default_true
27 public $accnt_name; // text(-1)
28 public $accnt_code_alt; // text default ''
29 public $accnt_descrip_alt; // text default ''
33 * Getter / Setter for $accnt_company
35 * @param mixed (optional) value to assign
38 public function company() {
39 return func_num_args() ? $this->link('accnt_company', func_get_arg(0)) : $this->link('accnt_company');
43 * Getter / Setter for $accnt_curr_id
45 * @param mixed (optional) value to assign
48 public function curr() {
49 return func_num_args() ? $this->link('accnt_curr_id', func_get_arg(0)) : $this->link('accnt_curr_id');
53 /* the code above is auto generated do not remove the tag below */
55 function applyFilters($q, $authUser, $roo)
57 $dt = date('Y-m-01', isset($q['_as_of']) ? strtotime($q['_as_of']) : time());
60 // check all the currencies..
61 $ac = $this->factory($this->tableName());
63 $ac->accnt_active = 1;
64 $ac->selectAdd('distinct(accnt_curr_id) as accnt_curr_id');
65 $curs = $ac->fetchAll('accnt_curr_id');
68 DB_DataObject::Factory('curr_rate')->hasRates(
70 isset($q['_as_of']) ? $q['_as_of'] : false,
76 if(isset($q['_with_xt_balances'])){
80 SUM(trialbal_ending - trialbal_beginning)
89 period_id = trialbal_period_id
91 trialbal_accnt_id=accnt_id
97 $this->orderBy('accnt_subaccnttype_code ASC, accnt_descrip ASC');
100 if(isset($q['_general_ledger'])){
101 $this->orderBy("accnt_type asc,accnt_subaccnttype_code asc, accnt_descrip asc");
105 if (isset($q['_with_balances']))
107 // all gltrans are in HKD.
109 // for hk office - the hkd value is line/by/line correct.
111 // if baseCurrId() == getcurrid('HKD')
112 // for the sg office hkd value ==
113 // check we have rates..
115 //DB_DataObject::debugLevel(1);
116 $p = DB_DataObject::factory('period');
117 if (!$p->get('period_start', $dt)) {
118 // get the last period
119 $p = DB_DataObject::factory('period');
120 $p->orderBy('period_start DESC');
124 //$roo->jerr("period does not exist");
128 // for trial balances - if it's not an asset or Liability..
129 // then ending is only the end of that period...
140 SUM( CASE WHEN accnt_type IN ( 'A','E') THEN
141 (trialbal_ending - trialbal_beginning) * -1
143 trialbal_ending - trialbal_beginning
153 period_id = trialbal_period_id
155 trialbal_accnt_id=accnt_id
157 period_start <= '$dt'
165 CASE WHEN accnt_type IN ( 'A','E') THEN
166 SUM(trialbal_ending - trialbal_beginning) * -1
169 SUM(trialbal_ending - trialbal_beginning)
170 END as trialbal_ending
177 period_id = trialbal_period_id
179 trialbal_accnt_id=accnt_id
181 period_start <= '$dt'
186 CASE WHEN accnt_type IN ( 'A', 'E') THEN
190 END as trialbal_ending
197 period_id = trialbal_period_id
199 trialbal_accnt_id=accnt_id
203 ) as balance_base_soy,
209 SUM( CASE WHEN accnt_type IN ( 'A','E') THEN
210 (trialbal_ending - trialbal_beginning) * -1
212 trialbal_ending - trialbal_beginning
223 period_id = trialbal_period_id
225 trialbal_accnt_id=accnt_id
227 period_start <= '$dt'
229 (SELECT curr_name from curr_symbol where curr_id = baseCurrId()) as base_curr,
231 {$p->pid()} as period_id
242 netsuite_balance.accnt_id = accnt.accnt_id
244 period_id = {$p->pid()}
246 curr_id = accnt_curr_id
251 COALESCE(MAX(curr_id),0) as curr_id
255 netsuite_balance.accnt_id = accnt.accnt_id
257 period_id = {$p->pid()}
267 netsuite_balance.accnt_id = accnt.accnt_id
269 period_id = {$p->pid()}
278 netsuite_balance.accnt_id = accnt.accnt_id
280 period_id = {$p->pid()}
289 netsuite_balance.accnt_id = accnt.accnt_id
291 period_id = {$p->pid()}
301 // $this->whereAdd('accnt_curr_id NOT IN ( 9,6) ');
302 $this->whereAdd("accnt_id IN (
303 select distinct(trialbal_accnt_id)
309 period_id = trialbal_period_id
312 period_start <= '$dt'::date
315 trialbal_credits != 0.0
317 trialbal_debits != 0.0
319 trialbal_ending != 0.0
321 trialbal_beginning != 0.0
326 $this->orderBy('accnt_subaccnttype_code ASC, accnt_descrip ASC');
329 if(!empty($q['search']['name'])){
331 accnt_name ILIKE '{$this->escape($q['search']['name'])}%'
333 accnt_descrip ILIKE '{$this->escape($q['search']['name'])}%'
335 accnt_descrip_alt ILIKE '{$this->escape($q['search']['name'])}%'
341 function toRooArray($q)
343 $ret = $this->toArray();
346 if (empty($q['_with_balances'])) {
349 static $netsuite = false;
351 $netsuite= $this->oldParse();
354 //$ret['balance_ns'] = isset($netsuite[$this->accnt_number]) ?
355 // $netsuite[$this->accnt_number]->netsuite_balance : 0.0;
364 $ff = HTML_FlexyFramework::get();
366 $which = array_pop(explode('/', $ff->page->baseURL));
367 $cn = $which == 'hk.php' ? 'HK' : 'SG';
369 $har = explode('/', realpath(__FILE__));
370 $home = '/home/alan';
371 $fn = "$home/Dropbox/xtuple_working/old_database_snapshot/$cn/Netsuite_Account.sql.json.all";
373 // echo '<PRE>';print_R($ar);exit;
375 foreach($ar as $i=>$l) {
377 $line = json_decode($l);
379 $ret[$line->id] = (object)array(
381 'description' => $line->description,
382 'netsuite_balance' => $line->balance,
383 'acctType' => $line->acctType,
392 // used by gltrans reports?
393 function multiplier() {
401 return $map[$this->accnt_type];
407 'accnt_descrip' => '',
408 'accnt_comments' => '',
409 'accnt_extref' => '',
410 'accnt_closedpost'=> false,
411 'accnt_forwardupdate' => false,
412 'accnt_active' => true,
417 * public $accnt_id; // int4(4) not_null default_nextval%28%28accnt_accnt_id_seq%29%3A%3Aregclass%29 primary_key
418 public $accnt_number; // text(-1)
419 public $accnt_descrip; // text(-1)
420 public $accnt_comments; // text(-1)
421 public $accnt_profit; // text(-1)
422 public $accnt_sub; // text(-1)
423 public $accnt_type; // bpchar(-1) not_null
424 public $accnt_extref; // text(-1)
425 public $accnt_company; // text(-1)
426 public $accnt_closedpost; // bool(1)
427 public $accnt_forwardupdate; // bool(1)
428 public $accnt_subaccnttype_code; // text(-1)
429 public $accnt_curr_id; // int4(4) default_basecurrid%28%29
430 public $accnt_active; // bool(1) not_null default_true
435 function joinAddBankaccnt()
441 bankaccnt_accnt_id = accnt_id
443 $bankaccnt = DB_DataObject::Factory('bankaccnt');
444 $this->selectAs($bankaccnt, '%s');
448 function importFromArray($roo, $accounts)
450 foreach ($accounts as $account){
451 $accnt = DB_DataObject::factory('accnt');
452 if($accnt->get('accnt_name', $account['accnt_name'])){
455 $accnt->setFrom($accnt->defaults());
457 $curr = DB_DataObject::factory('curr_symbol');
458 if(!$curr->get('curr_abbr', $account['accnt_curr_abbr'])){
459 $roo->jerr("Missing currency : {$account['accnt_curr_abbr']}");
461 $accnt->accnt_number = $account['accnt_number'];
462 $accnt->accnt_descrip = $account['accnt_descrip'];
463 $accnt->accnt_profit = $account['accnt_profit'];
464 $accnt->accnt_sub = $account['accnt_sub'];
465 $accnt->accnt_type = $account['accnt_type'];
466 $accnt->accnt_company = $account['accnt_company'];
467 $accnt->accnt_subaccnttype_code = $account['accnt_subaccnttype_code'];
468 $accnt->accnt_name = $account['accnt_name'];
471 //$roo->jerr($accnt->accnt_name);exit;
472 if(empty($account['bankaccnt_id'])){
476 $bankaccnt = DB_DataObject::factory('bankaccnt');
477 $bankaccnt->setFrom($account);
479 $curr = DB_DataObject::factory('curr_symbol');
480 if(!$curr->get('curr_abbr', $account['bankaccnt_curr_abbr'])){
481 $roo->jerr("Missing currency : {$account['bankaccnt_curr_abbr']}");
483 $bankaccnt->bankaccnt_curr_id = $curr->pid();
484 $bankaccnt->bankaccnt_accnt_id = $accnt->pid();
485 $bankaccnt->insert();
489 function checkAlternativeCode($roo, $download = false)
492 * check the alternative code
493 * if an account has transactions and there is not a alt code for it - then display an error
496 $accnt = DB_DataObject::factory('accnt');
501 CASE WHEN (accnt_descrip_alt IS NOT NULL AND accnt_descrip_alt != '') THEN
507 $accnt->orderBy('accnt_code_alt DESC');
509 while ($accnt->fetch()){
510 if(!empty($accnt->accnt_code_alt)){
511 $a = DB_DataObject::factory('accnt');
512 $a->accnt_code_alt = $accnt->accnt_code_alt;
514 $errors[] = "Duplicate alternative code : " . $accnt->accnt_code_alt . " - " . $accnt->accnt_descrip;
519 $gltrans = DB_DataObject::factory('gltrans');
520 $gltrans->selectAdd();
521 $gltrans->selectAdd('gltrans_id');
523 gltrans_accnt_id = {$accnt->accnt_id}
527 if(!$gltrans->count()){
531 $errors[] = "Missing alternative code : " . $accnt->accnt_descrip;
534 $errors = array_filter($errors);
538 $roo->jerr(implode("\n", $errors), array(), 'text/plain');
541 $roo->jerr(implode("\n", $errors));