Zend - The PHP Company




Commerce

Add Code


functions for ofx,qfx,qif,csv export..  

Type: code fragment
Added by: joeldg
Entered: 17/10/2002
Last modified: 31/10/2001
Rating: - (fewer than 3 votes)
Views: 13658
export data to all the xml type formats recognized by the money programs out there. MSMoney, Quicken, CSV etc. The code is really designed for financial data obviously. It is in use in the IPN system I developed for working with Paypal. See the example URL and go into the test account and look in the history to see a live example. Any comments would be appreciated.


<?
/*
 functions for ofx,qfx,qif,csv export...
 2002(c) - Joel De Gan..
 http://www.tenshimedia.com
 http://www.joihost.com

 these are all the formats recognized by the money programs out there...
 bankid etc set as paypal and merchant as us..
 
*/
/*
export_ofx_data - 
    style : ofx,qfx,qif,csv
    $actid : email or whatever
    datestart : 20020611120000 <- format
    dateend : 20020611120000 <- format
    tarray : transaction array of -> array(TRNTYPE, DTPOSTED, TRNAMT, NAME);
        i.e $in[0][TRNTYPE] = "deposit";
            $in[0][DTPOSTED] = 20020611120000;
            $in[0][TRNAMT] = 15.99;
            $in[0][NAME] = "shirt";
            $in[1] = array(...) etc...
    actbal = accout balance, float..
*/
function export_ofx_data($style$actid$datestart$dateend$tarray$actbal$act="PAYPAL"$merchid="COMPANYNAME"){

  if (
$style == "ofx" || $style == "qfx"){
    
$currdate date("Ymd");
    
$tophour "120000"//tacked on the end of currdate..
    
$sendback "OFXHEADER:100rnDATA:OFXSGMLrnVERSION:102rnSECURITY:NONErnENCODING:USASCIIrnCHARSET:1252rn";
    
$sendback .= "COMPRESSION:NONErnOLDFILEUID:NONErnNEWFILEUID:NONErn<OFX>rn<SIGNONMSGSRSV1>rn<SONRS>rn<STATUS>rn";
    
$sendback .= "<CODE>0rn<SEVERITY>INFOrn</STATUS>rn<DTSERVER>".$currdate.$tophour."[0:GMT]rn<LANGUAGE>ENGrn</SONRS>rn";
    
$sendback .= "</SIGNONMSGSRSV1>rn<BANKMSGSRSV1>rn<STMTTRNRS>rn<TRNUID>1rn<STATUS>rn<CODE>0rn<SEVERITY>INFOrn</STATUS>rn";
    
$sendback .= "<STMTRS>rn<CURDEF>USDrn<BANKACCTFROM>rn<BANKID>".$merchid."rn<ACCTID>".$actid."rn<ACCTTYPE>$actrn";
    
$sendback .= "</BANKACCTFROM>rn<BANKTRANLIST>rn<DTSTART>".$datestart."[0:GMT]rn<DTEND>".$dateend."[0:GMT]rn";
    
    while (list (
$key$val) = each ($tarray)) {
        
$sendback .= "<STMTTRN>rn<TRNTYPE>";
        
$sendback .= strtoupper($val["TRNTYPE"])."rn";
        
$sendback .= "<DTPOSTED>".$val["DTPOSTED"]."[0:GMT]rn";
        
$sendback .= "<TRNAMT>".$val["TRNAMT"]."rn";
        
$sendback .= "<FITID>".date("Ymd"$val["DTPOSTED"]).$key."rn";
        
$sendback .= "<NAME>".strtoupper($val["NAME"])."rn";
        
$sendback .= "<STMTTRN>rn";
    }
//wend
    
$sendback .= "</BANKTRANLIST>rn<LEDGERBAL>rn<BALAMT>$actbalrn<DTASOF>".$currdate.$tophour."[0:GMT]rn</LEDGERBAL>rn";
    
$sendback .= "</STMTRS>rn</STMTTRNRS>rn</BANKMSGSRSV1>rn</OFX>rn";
  }
//fi    
  
  
if ($style == "qif"){
    
$sendback "";
      while (list (
$key$val) = each ($tarray)) {
            
$val["DTPOSTED"] = mts2ut($val["DTPOSTED"]);
        
$qifdate date("d"$val["DTPOSTED"])."/".date("m"$val["DTPOSTED"])."/".date("Y"$val["DTPOSTED"]);
            
$sendback .= "C*rn";
        
$sendback .= "D"$qifdate ."rn";
        
$sendback .= "Nrn";
        
$sendback .= "P".strtoupper($val["NAME"])."rn";
        
$sendback .= "T".$val["TRNAMT"]."rn";
        
$sendback .= "^rn";
      }
//wend
  
}// fi

  
if ($style == "csv"){
    
$sendback ""TRNTYPE","DTPOSTED","TRNAMT","NAME"rn";
      while (list (
$key$val) = each ($tarray)) {
            
$sendback .= """. strtoupper($val["TRNTYPE"]) ."","$val["DTPOSTED"].","$val["TRNAMT"] .","". strtoupper($val["NAME"]) .""rn";
      }
//wend
  
}// fi
  
return $sendback;
}
//end function export_ofx_data

//*******************************************************************************//
// function to parse multi arrays into csv data
// array in... array of array(datasets); first dataset = field names.
// usage: 
/*
     $toparse[0][0] = "field1";
     $toparse[0][1] = "field2";
     $toparse[1][0] = "value1";
     $toparse[1][1] = "123123123"; // to see
    echo export_to_csv($toparse);
*/
//*******************************************************************************//
function export_to_csv($inarray){
      while (list (
$key1$val1) = each ($inarray)) {
        while (list (
$key$val) = each ($val1)) {
          if (
is_numeric($val)){
        
$sendback .= $val.",";
           }else{
            
$sendback .= """. $val ."",";
          }
//fi
        
}//wend
        
$sendback substr($sendback0, -1); //chop last ,
        
$sendback .= "rn";
      }
//wend
    
return ($sendback);
}
// end function

//*******************************************************************************//
// send the file to the client.. pretty simple.
// usage : send_file_to_client("data.csv", export_to_csv($data));
//*******************************************************************************//
function send_file_to_client($filename$data){
    
header("Content-type: application/ofx");
    
header("Content-Disposition: attachment; filename=$filename");
    echo 
$data;    
};



// testing code below...
/*

$in[0][TRNTYPE] = "deposit";
$in[0][DTPOSTED] = 20020611120000;
$in[0][TRNAMT] = 15.99;
$in[0][NAME] = "shirt";

$in[1][TRNTYPE] = "deposit";
$in[1][DTPOSTED] = 20020611120001;
$in[1][TRNAMT] = 115.99;
$in[1][NAME] = "other";
//print_r($in);
echo export_to_csv($in);

echo export_ofx_data("ofx", "joeldg", 20020611120000, 20020611120000, $in, 3000, $act="PAYPAL", $merchid="JOIHOSTIPN");

*/
?>


Usage Example


See the example


Rate This Script





Search



This Category All Categories