PHP code example of devtally / tally-xml

1. Go to this page and download the library: Download devtally/tally-xml library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

devtally / tally-xml example snippets


use Devtally\TallyXml\Converters\JsonToVoucherConverter;

$json = [
    'company_name' => 'My Company Ltd',
    'voucher' => [
        'voucher_type' => 'Sale',
        'voucher_number' => 'INV-001',
        'voucher_date' => '2023-12-01',
        'ledger_name' => 'Customer Name',
        'narration' => 'Sale of goods',
        'total' => 11800,
        'voucher_items' => [
            [
                'ledger_name' => 'Sales Account',
                'stock_item_name' => 'Product A',
                'unit' => 'Nos',
                'price' => 1000,
                'qty' => 10,
                'discount' => 0
            ]
        ],
        'tax_details' => [
            ['name' => 'CGST @ 9%', 'amount' => 900],
            ['name' => 'SGST @ 9%', 'amount' => 900]
        ],
        'additional_charges' => [],
        'round_off' => ['name' => 'Round Off', 'amount' => 0]
    ]
];

$converter = new JsonToVoucherConverter();
$xml = $converter->convert($json);

// Save or send to Tally
file_put_contents('voucher.xml', $xml);

use Devtally\TallyXml\Builders\LedgerBuilder;
use Devtally\TallyXml\TallyXmlConverter;

// Create a customer ledger
$ledger = LedgerBuilder::customer('ABC Corporation')
    -> address('123 Business Park')
    ->state('Tamil Nadu')
    ->country('India')
    ->pincode('600001')
    ->gstRegistrationType('Regular')
    ->gstin('33ABCDE1234F1Z5')
    ->build();

// Convert to XML
$converter = new TallyXmlConverter();
$xml = $converter->convert($ledger);

// Supplier
$supplier = LedgerBuilder::supplier('XYZ Supplies')->build();

// Sales Account
$sales = LedgerBuilder::salesAccount('Local Sales')->build();

// Purchase Account
$purchase = LedgerBuilder::purchaseAccount('Local Purchases')->build();

// Custom ledger with parent
$ledger = LedgerBuilder::create('Transport Charges', 'Indirect Expenses')->build();

use Devtally\TallyXml\Builders\StockItemBuilder;

$stockItem = StockItemBuilder::create('Laptop HP Pavilion', 'Nos')
    ->gstApplicable(true)
    ->gstSupplyType('Goods')
    ->gstDetails('84713000', 18, 45000) // HSN, GST%, Price
    ->build();

$xml = $converter->convert($stockItem);

use Devtally\TallyXml\Builders\UnitBuilder;

$unit = UnitBuilder::create('Boxes', 'BOX')
    ->decimalPlaces(2)
    ->build();

$xml = $converter->convert($unit);

use Devtally\TallyXml\Builders\VoucherBuilder;

$voucher = VoucherBuilder::sales('INV-2024-001', '2024-01-15')
    ->partyName('ABC Corporation')
    ->partyAddress('123 Business Street')
    ->partyAddress('Bangalore, Karnataka')
    ->partyState('Karnataka')
    ->partyCountry('India')
    ->partyPincode('560001')
    ->partyGstRegistrationType('Regular')
    ->partyGstin('29ABCDE1234F1Z5')
    ->placeOfSupply('Karnataka')
    ->narration('Sale of laptops and accessories')
    ->reference('PO-ABC-456')
    
    // Add items
    ->addVoucherItem('Sales Account', 'Laptop HP Pavilion', 'Nos', 45000, 2, 0)
    ->addVoucherItem('Sales Account', 'Wireless Mouse', 'Nos', 500, 10, 0)
    
    // Add taxes
    ->addTax('CGST @ 9%', 8550)
    ->addTax('SGST @ 9%', 8550)
    
    // Round off
    ->roundOff('Round Off', 0.50)
    
    // Party total amount
    ->partyAmount('ABC Corporation', 112600.50)
    
    ->build();

$xml = $converter->convert($voucher);

$voucher = VoucherBuilder::purchase('PUR-001', '2024-01-15')
    ->partyName('Supplier XYZ')
    ->narration('Purchase of raw materials')
    ->addVoucherItem('Purchase Account', 'Raw Material A', 'Kgs', 100, 50, 0)
    ->addTax('CGST @ 6%', 300)
    ->addTax('SGST @ 6%', 300)
    ->partyAmount('Supplier XYZ', 5600)
    ->build();

$voucher = VoucherBuilder::payment('PAY-001', '2024-01-15')
    ->partyLedger('Supplier XYZ')
    ->narration('Payment for invoice PUR-001')
    ->reference('PUR-001')
    ->addLedgerEntry('Supplier XYZ', -5600, false) // Credit
    ->addLedgerEntry('Cash', 5600, true) // Debit
    ->build();

$voucher = VoucherBuilder::saleReturn('SR-001', '2024-01-15')
    ->partyName('ABC Corporation')
    ->narration('Return of defective items')
    ->addVoucherItem('Sales Account', 'Laptop HP Pavilion', 'Nos', 45000, 1, 0)
    ->addTax('CGST @ 9%', -4050)
    ->addTax('SGST @ 9%', -4050)
    ->partyAmount('ABC Corporation', -53100)
    ->build();