database))); $lprefix = preg_match('/(lsg\.php|lhk.php)$/', $this->baseURL) ? 'l' : ''; // dump old db -> jsondecode -> get file contents ~/Dropbox/olddb...... [TBD] // get Netsuite_Accounts from SG and HK and merge // // review the "View Financial Report" to see how it's pulling // the Balances for the internal accounting accounts // // // DB_DataObject::debugLevel(1); //DB_DataObject::DebugLevel(1); $db = DB_DataObject::factory('custinfo'); $db->selectAdd(" cust_number, (SELECT COALESCE( SUM( CASE WHEN (aropen_doctype IN ('C', 'R')) THEN (aropen_amount - aropen_paid) * -1.0 ELSE (aropen_amount - aropen_paid) END ), 0.0) FROM aropen WHERE aropen_cust_id = cust_id AND aropen_open ) as balance "); $db->orderBy('cust_number ASC'); $xdb =$db->fetchAll('cust_number', 'balance'); ; //ksort($xtd); //if ($sd != 'HK') { // we only report on HK data.. // echo json_encode($xdb); // exit; //} $hkdata = $xdb; //$sgdata = (array) json_decode(file_get_contents("http://localhost/xtuple/{$lprefix}sg.php/Xtuple/VerifyAR")); // echo '
';var_dump($sgdata);exit;
        
       //echo '
';print_R($sgdatas['97']);exit;
        
       //  echo '
';var_dump( $sgdata );exit;
        // at this point we
        //$old = $this->oldParse('HK');
        
        
        foreach($this->oldParse('SG') as $k =>$v) {
            $old[$k] = $v;
        }
        
        
          
        //echo '
';
        //var_dump($old);
       //(int) print_r(array_keys($sgdata));
       // print_r(array_keys($old));
        //exit;
        
        $out = array();
        
      
        $out[] = array("Customer",  "spacer","spacer", "NETSUITE", "HK", "SG", "difference");
        foreach($old as $k=>$v) {
            $k = trim($k);
            $hk = isset($hkdata[''.$k]) ? $hkdata[''.$k] : 0;
            $sg = isset($sgdata[''.$k]) ? $sgdata[''.$k] : 0;
             
            if ( ( ($hk + $sg) - $v) == 0.0) {
                continue;
            }
            
            if (abs(( ($hk + $sg) - $v)) < 1.5) {
                continue;
            }
            
            
            $historyfor[$k] = $v;
            
            
            
            $out[] =  array(
                $k,
                '',
                '',
                $v,
                $hk,
                $sg,
                ($hk + $sg) - $v
            );
        }
        
        
          header('Content-type: text/csv');
        header( 'Content-Disposition: attachment;filename=ARreport.csv');
        $fh = fopen('php://output','w');
        foreach($out as $o) {
            fputcsv($fh, $o);
        }
        fclose($fh);
        exit;
        //exit;
        // we now have all the data...
        
        // for ar - look in aropen..
        // eg. sgtru = SG based (224
        
        // aropen  - columns... docdate, doctype, docnumber,amount, paid,
        $max = 99999;
        $i = 0;
        foreach($historyfor as $cust=>$expect) {
            $i++;
            if ($i > $max) {
                break;
            }
            $args = $this->toParams(array(
                'query[cust_number]'=> trim($cust),
                 
                'sort'=>'aropen_docdate,aropen_id',
                'dir'=>'ASC',
                
                
                'csvCols[0]' => 'aropen_docdate',
                'csvCols[1]' => 'aropen_doctype',
                'csvCols[2]' => 'aropen_docnumber_r',
                'csvCols[3]' => 'aropen_amount',
                'csvCols[4]' => 'aropen_paid',
                'csvCols[5]' => 'aropen_remaining',
                'csvCols[6]' => 'aropen_running',
                 
                'csvTitles[0]' => 'Date',
                'csvTitles[1]' => 'Type',
                'csvTitles[2]' => 'Doc no.',
                'csvTitles[3]' => 'Amount',
                'csvTitles[4]' => 'Paid',
                'csvTitles[5]' => 'Outstanding',
                'csvTitles[6]' => 'Running',
                'start' => 0,
                'limit' => 800
                
            ));
            
            //fwrite($fh, "\n\n\"http://www.bloomandgrowdirect.com/xtuple/{$lprefix}hk.php/Roo/aropen?$args\"\n");
            //fwrite($fh, "\n\n\"http://www.bloomandgrowdirect.com/xtuple/{$lprefix}sg.php/Roo/aropen?$args\"\n");
            
            fwrite($fh, "\n\n-- ". $cust ." ,,Expecting:, $expect\n");
                       
                       
            fwrite($fh, file_get_contents("http://localhost/xtuple/{$lprefix}hk.php/Roo/aropen?$args"));
            fwrite($fh, file_get_contents("http://localhost/xtuple/{$lprefix}sg.php/Roo/aropen?$args"));
        }
        //echo '
';
        //print_R($old);
        //exit;
        
        
        
        exit;
        
        
        
        
        
        
        
    }
    function oldParse($cn)
    {
        $har = explode('/', realpath(__FILE__));
        $home = '/home/'. $har[2];
        $fn = "$home/Dropbox/xtuple_working/old_database_snapshot/$cn/Netsuite_Customer.sql.json.all";
        $ar = file($fn);
        $ret = array();
        foreach($ar as $i=>$l) {
            if (!$i) continue;
            $line = json_decode($l);
         
            $ret[strtoupper($line->entityId)] =    $line->balance;
        }
        return $ret;
         

        
    }
    
}