2 require_once 'Verify.php';
3 class Pman_Xtuple_VerifyAccounts extends Pman_Xtuple_Verify {
9 $sd = strtoupper(array_pop(explode('xtuple', HTML_FlexyFramework::get()->database)));
12 $lprefix = preg_match('/(lsg\.php|lhk.php)$/', $this->baseURL) ? 'l' : '';
14 // dump old db -> jsondecode -> get file contents ~/Dropbox/olddb...... [TBD]
15 // get Netsuite_Accounts from SG and HK and merge
17 // review the "View Financial Report" to see how it's pulling
18 // the Balances for the internal accounting accounts
21 // DB_DataObject::debugLevel(1);
23 $db = DB_DataObject::factory('accnt');
25 $db->selectAdd("accnt_number,
30 COALESCE((SELECT SUM(gltrans_amount) FROM gltrans WHERE gltrans_accnt_id=accnt_id),0),
33 // $db->whereAdd('accnt_curr_id NOT IN ( 9,6) '); // THB, TWD - not conversion rates avail..
37 $row = $db->toArray(FALSE, TRUE);
38 $xtd[$row['accnt_number']] = $row;
44 // we only report on HK data..
45 echo json_encode($xtd);
50 $xtd = (array) json_decode(file_get_contents("http://localhost/xtuple/{$lprefix}sg.php/Xtuple/VerifyAccounts"));
52 foreach($xtd as $k=> $v) {
53 $sgdata[(int)$k] = $v;
55 //print_R($sgdata);exit;
57 // echo '<PRE>';var_dump( $sgdata );exit;
59 $old = $this->oldParse('HK');
62 foreach($this->oldParse('SG') as $k =>$v) {
68 foreach($hkdata as $k=>$v) {
70 if (isset($old[$k])) {
75 'description' => $v->accnt_descrip,
76 'id' => $v->accnt_number,
80 foreach($sgdata as $k=>$v) {
82 if (isset($old[$k])) {
87 'description' => $v->accnt_descrip,
88 'id' => $v->accnt_number,
94 //(int) print_r(array_keys($sgdata));
95 // print_r(array_keys($old));
96 foreach($old as $k=>$xx) {
98 //var_dump(array($k, $sgdata[(string)$k]));
100 $old[$k]['hkbalance'] = 0;
101 $old[$k]['sgbalance'] = 0;
102 if (isset($hkdata[(int)$k])) {
103 $v =(object)($hkdata[(int)$k]);
105 $old[(int)$k]['hkbalance'] = $v->balance;
107 if (isset($sgdata[(string)$k])) {
108 //echo "GOT SG? $k<BR>";
109 $v =(object) ($sgdata[(int)$k]);
110 $old[(int)$k]['sgbalance'] = $v->balance;
114 //die("GOT THIS FAR?");
118 header('Content-type: text/csv');
119 header( 'Content-Disposition: attachment;filename=test.csv');
120 $fh = fopen('php://output','w');
122 fputcsv($fh, array("ID", "type", "Description", "NETSUITE", "HK", "SG", "difference"));
123 foreach($old as $k=>$ar) {
125 $ar->netsuite_balance = isset( $ar->netsuite_balance) ? $ar->netsuite_balance : 0;
126 $ar->acctType = empty($ar->acctType) ? '' : $ar->acctType;
127 if ($ar->acctType == '_expense') {
128 $ar->netsuite_balance = -1 * $ar->netsuite_balance ;
131 if (!(1 * $ar->netsuite_balance ) && !(1 *$ar->hkbalance) && !(1 * $ar->sgbalance)) {
134 if ( abs(abs($ar->hkbalance + $ar->sgbalance) - abs($ar->netsuite_balance)) < 100.0) {
143 $ar->netsuite_balance,
146 abs($ar->hkbalance + $ar->sgbalance) - abs($ar->netsuite_balance)));
149 // we now have all the data...
166 function oldParse($cn)
170 $har = explode('/', realpath(__FILE__));
171 $home = '/home/'. $har[2];
172 $fn = "$home/Dropbox/xtuple_working/old_database_snapshot/$cn/Netsuite_Account.sql.json.all";
175 foreach($ar as $i=>$l) {
177 $line = json_decode($l);
179 $ret[$line->id] = array(
181 'description' => $line->description,
182 'netsuite_balance' => $line->balance,
183 'acctType' => $line->acctType,
192 function addEndOfDay($accnt_id , $data, $bbal, $bal, $last_exchange)
194 $ac = DB_DataObject::factory('accnt');
196 $num = $ac->accnt_number;
199 $sd = strtoupper(substr(HTML_FlexyFramework::get()->database,-2));
201 $base = $sd == 'SG' ? 'SGD' : 'HKD';
203 $file = "/home/alan/netsuite_accounts/{$sd}-Account-". $num . '.csv';
205 $last_exchange = false;
207 // fill in dragon data..
210 foreach($data as $i=>$row) {
212 // if the exchange rate changes..
213 // then the balance will get adjusted...
216 // balance = [oldbalance] / 5 * 10
217 if ($last_exchange !== false && $last_exchange != $row['gltrans_curr_rate']) {
218 $bal = ($bal / $last_exchange) * $row['gltrans_curr_rate'];
220 $last_exchange = $row['gltrans_curr_rate'];
225 $bal += $row['gltrans_amount_normal'];
226 $data[$i]['gltrans_balance'] = $bal;
227 $bbal += $row['gltrans_base_normal'];
228 $data[$i]['gltrans_base_balance'] = $bbal;
231 $data[$i]['gltrans_nsbalance'] = '';
232 $data[$i]['gltrans_nsbalance_diff'] = '';
238 if (!file_exists($file)) {
239 //var_dump($file);exit;
246 // find the end of day totals
249 $fh = fopen($file ,'r');
251 die("failed to open $file\n");
256 $daybal_fc = array();
265 $daybal_base = array();
270 while (false !== ($row = fgetcsv($fh, 4000))) {
272 if ($head == false) {
273 //if (trim($row[0]) != 'Date') {
278 foreach($row as $v) {
284 //echo '<PRE>';print_r($head);print_r($row);exit;
285 // print_R($head);exit;
288 foreach($head as $i=>$k) {
290 $line[$k] = date('Y-m-d', strtotime(implode('-', array_reverse(explode('/', $row[$i])))));
293 if ($k == 'Amount') {
299 if (($k == 'Currency') || ($k == 'Amount (Foreign Currency)') || ($k == 'Number') || ($k == 'Posting')) {
302 // do not care about other data!?! at present.
306 if ($line['Posting'] == 'No') {
309 if ($line['Number'] == 'Memorized') {
313 $bal += 1.0*$line['Amount'];
314 $line['Balance'] = $bal ; //$neg . preg_replace('#[^0-9.-]#','', $row[$i]);
320 //echo "FC SET TO :". $line['Currency'];
321 $fc = $line['Currency'];
322 } else if ($fc === '') {
324 } else if ($fc != $line['Currency']) {
328 $am = preg_replace('/[^0-9.]+/', '', $line['Amount (Foreign Currency)']);
329 if ($line['Amount'] < 0) {
334 $basebal += $this->currtobase($line['Currency'] , $base, $am , $line['Date']);
335 $daybal_base[$line['Date']] = $basebal;
340 $daybal_fc[$line['Date']] = $fcbal;
344 if (($last_date != $line['Date']) && !empty($last_date)) {
345 $daybal[$last_date] = $last_balance;
348 $last_balance = $line['Balance'];
349 $last_date = $line['Date'];
352 //echo '<PRE>';print_R($daybal); exit;
353 // now let's add them to the return values..
356 $lastbase_balance = 0.0;
359 foreach($data as $i=>$row) {
361 if (($last_date != $row['gltrans_date']) && !empty($last_date)) {
364 $data[$i-1]['gltrans_nsbalance'] = isset($daybal[$last_date]) ? $daybal[$last_date] : 0;
365 $data[$i-1]['gltrans_nsbalance_diff'] = sprintf("%0.2f", $data[$i-1]['gltrans_nsbalance'] - $data[$i-1]['gltrans_balance'] );
366 if ($last_diff != $data[$i-1]['gltrans_nsbalance_diff']) {
367 $bad_days[] = $last_date;
369 $last_diff = $data[$i-1]['gltrans_nsbalance_diff'];
371 $data[$i-1]['gltrans_nsbalance'] = isset($daybal_fc[$last_date]) ? $daybal_fc[$last_date] : 0;
372 $data[$i-1]['gltrans_nsbalance_diff'] = sprintf("%0.2f", $data[$i-1]['gltrans_nsbalance'] - $data[$i-1]['gltrans_balance'] );
376 $data[$i-1]['gltrans_nsbalance_base'] = isset($daybal_base[$last_date]) ? $daybal_base[$last_date] : $lastbase_balance;
378 $data[$i-1]['gltrans_nsbalance_base_diff'] = sprintf("%0.2f", $data[$i-1]['gltrans_nsbalance_base'] - $data[$i-1]['gltrans_base_balance'] );
379 $lastbase_balance = $data[$i-1]['gltrans_nsbalance_base'];
382 $last_date = $row['gltrans_date'];
385 $data[$i]['gltrans_nsbalance'] = '';
386 $data[$i]['gltrans_nsbalance_diff'] = '';
392 foreach($data as $row) {
393 if (!in_array($row['gltrans_date'], $bad_days)) {
402 //echo '<PRE>';print_R($data); exit;
408 function currtobase($from , $to, $amt , $date)
413 $do = DB_DataObject::factory('curr_symbol');
414 $do->query("select currtobase(getcurrid('{$from}'), {$amt}, '{$date}') as result");