Monday 27 March 2017

Tally ERP 9's Web Service Architecture (Introduction to PHP to Tally Integration)




This post introduces basic PHP to integrate with Tally.ERP 9.
Before you go further with the introduction, my assumption is that you already know basics of Tally along with Tally's XML schema and if not then watch the following video for the basic idea of Tally's XML:

Alternate link

The following is a working PHP code which sends a payment voucher to Tally database through SOAP request into the Web Service Architecture of Tally and fetches a response from Tally indicating the action taken by Tally for the request in XML format.
Check it out.


1: Go to "Gateway of Tally >> F12 Configure >> Advanced Configuration" settings (Some initial settings needed to be done inside Tally)




2: Request by the PHP application code

$xml_str = ""
. "<ENVELOPE>"
. "<HEADER><TALLYREQUEST>Import Data</TALLYREQUEST></HEADER>"
. "<BODY>"
. "<IMPORTDATA>"
. "<REQUESTDESC><REPORTNAME>Vouchers</REPORTNAME><STATICVARIABLES><SVCURRENTCOMPANY>APB</SVCURRENTCOMPANY></STATICVARIABLES></REQUESTDESC>"
. "<REQUESTDATA>"
. "<TALLYMESSAGE xmlns:UDF=\"TallyUDF\">"
. "<VOUCHER REMOTEID=\"00000001\" VCHTYPE=\"Receipt\" ACTION=\"Create\" OBJVIEW=\"Accounting Voucher View\">"    
. "<DATE>20160401</DATE>"
. "<VOUCHERTYPENAME>Receipt</VOUCHERTYPENAME>"
. "<VOUCHERNUMBER>1</VOUCHERNUMBER>"
. "<PARTYLEDGERNAME>Cash</PARTYLEDGERNAME>"
. "<PERSISTEDVIEW>Accounting Voucher View</PERSISTEDVIEW>"
. "<ALLLEDGERENTRIES.LIST>"
. "<LEDGERNAME>Capital Account</LEDGERNAME>"
. "<ISDEEMEDPOSITIVE>No</ISDEEMEDPOSITIVE>"
. "<AMOUNT>50000.00</AMOUNT>"
. "</ALLLEDGERENTRIES.LIST>"
. "<ALLLEDGERENTRIES.LIST>"    
. "<LEDGERNAME>Cash</LEDGERNAME>"
. "<ISDEEMEDPOSITIVE>Yes</ISDEEMEDPOSITIVE>"
. "<AMOUNT>-50000.00</AMOUNT>"        
. "</ALLLEDGERENTRIES.LIST>"  
. "</VOUCHER>"
. "</TALLYMESSAGE>"
. "</REQUESTDATA>"
. "</IMPORTDATA>"
. "</BODY>"
                . "</ENVELOPE>";
$url = "http://localhost:9000";
$headers = array("Content-type: text/xml", "Content-length:" . strlen($xml_str), "Connection: close");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 100);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_str);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$data = curl_exec($ch);

if(curl_errno($ch)) {
print curl_error($ch);
} else {
print "<pre>" . htmlentities($data) . "</pre>";
curl_close($ch);
}


3: Response by the Tally Web Service Architecture




4: Voucher inserted inside Tally