By Faubix

Reading Tally data for FBR: the traps that raise no error

A practical guide for the person who connects Tally to FBR digital invoicing. Almost every failure here shows up as an invoice that looks right, or a quiet day with no invoices. Very few of them raise an error.

An accountant working with a calculator and a notebook

Answer first. Tally doesn’t fail loudly. Ask its gateway for the wrong company and it answers with an empty list and a success code (HTTP 200). Read a voucher’s lines the obvious way and every single-line invoice disappears. Take amounts at face value and every sale comes out negative. None of these raise an error. The first sign is a day with no invoices, or FBR figures that don’t match Tally’s own sales register. The fix: know the list below, then check each item against a week of real vouchers before you send anything to FBR.

How Tally hands over data

TallyPrime and Tally.ERP 9 are desktop programs. They have no way to send a change to another system. Instead, they offer a gateway. Switch on Tally’s setting to act as a server (its place in the menus differs between releases). Tally then answers XML requests on the port you set, such as 9000, over your local network. Tally must be running, with the company loaded. There is no REST interface and no published data format. You send an envelope that describes what you want, and Tally sends back XML.

The most stable way to ask is an inline collection. The request names exactly the fields it wants, instead of relying on a built-in report whose layout changes from one installation to the next. Here is a sample request for a month of vouchers. Match each field name to your own Tally company:

<ENVELOPE>
  <HEADER>
    <VERSION>1</VERSION>
    <TALLYREQUEST>Export</TALLYREQUEST>
    <TYPE>Collection</TYPE>
    <ID>FbrSalesVouchers</ID>
  </HEADER>
  <BODY>
    <DESC>
      <STATICVARIABLES>
        <SVEXPORTFORMAT>$$SysName:XML</SVEXPORTFORMAT>
        <SVCURRENTCOMPANY>EXACT COMPANY NAME</SVCURRENTCOMPANY>
        <SVFROMDATE>20260901</SVFROMDATE>
        <SVTODATE>20260930</SVTODATE>
      </STATICVARIABLES>
      <TDL><TDLMESSAGE>
        <COLLECTION NAME="FbrSalesVouchers">
          <TYPE>Voucher</TYPE>
          <FETCH>Date, VoucherNumber, VoucherTypeName, PartyLedgerName,
                 Reference, IsCancelled, IsOptional, AllInventoryEntries</FETCH>
        </COLLECTION>
      </TDLMESSAGE></TDL>
    </DESC>
  </BODY>
</ENVELOPE>

Three facts about this set-up shape everything else. Tally only serves companies that are open in it right now. It handles one request at a time. And the gateway has no password at all. Anything that can reach the port can read the whole company file, so it belongs on your local network and nowhere else.

Traps in the response itself

  • A wrong company name returns nothing, with a success code. Tally matches the company name letter for letter. A near miss (a missing full stop, “Pvt” for “(Private)”) comes back as an empty list with HTTP 200, which looks exactly like a day with no sales. Other failures, such as a company that isn’t loaded, arrive as a LINEERROR element inside a 200 reply. Never take the success code at its word. Check that the company you asked for is the company that answered, and raise an alert on any day with zero vouchers.
  • Single-line invoices can vanish. XML has no way to say “a list of one”. An invoice with one stock line arrives with one inventory element, where a three-line invoice has three. A parser that turns repeated elements into arrays but leaves single ones as objects will skip every one-line invoice. Always force every list element into an array. The bug is easy to miss, because most test invoices have several lines.
  • Fields you ask for can quietly go missing. Misspell a name in the fetch list and Tally leaves the field out without a word. A required field that is simply missing is the warning sign. Test that each field is there, not just its value.
  • The text encoding is not what it seems. Tally’s responses can arrive without a declared character set and with Windows-1252 characters such as curly quotes. Decode them as UTF-8 and every name with special (non-ASCII) characters is garbled. Control characters, including raw 0x04 bytes and &#4; references, turn up in narrations and addresses, and they stop a strict XML parser dead. Strip them before parsing, and before escaping ampersands, or you will damage the data you meant to clean.
  • Tally can simply stop answering. An open dialog box on the Tally PC can freeze the gateway. A very wide date range can time out. Two requests at once can come back empty. Ask for small date ranges, one request at a time, with a time limit and a second try.

Traps in the numbers

  • Sales are negative. Tally follows double-entry signs. A sale credits sales and tax, so amounts on stock and tax entries arrive negative. Flip the sign on purpose, in one place. Test with a credit note, so you know the flip works in both directions.
  • Quantities carry their unit. A quantity arrives as text such as 100 Kgs. Reading it as a number either fails or quietly drops the unit. And the unit is exactly what you need to translate into FBR’s wording.
  • Billed and actual quantity differ. Tally records both. When a delivery differs from what was billed, the invoice uses the billed quantity. Take the other one, and the value and tax no longer agree.
  • Discount is already applied. Tally’s discount is a percentage, and the line amount already has it taken off. Subtract it again and you undercharge tax on every discounted line.
  • Tax is posted once, but FBR wants it per line. Tally books sales tax as one entry on the voucher, against a ledger under Duties & Taxes. FBR needs the tax on each line. Split it across the lines in whole paisa, and give the rounding difference to one line, so the lines add up exactly to the voucher total. Floating-point maths gets this wrong sometimes, and once is enough to fail a check against your books.

What counts as an invoice

  • Voucher types are set by the user. “Sales” is only Tally’s default. Companies create “Sales – Lahore”, “Export Sales”, “Cash Sales”. Filtering on the name “Sales” misses them all. Keep a clear list of which voucher types are FBR sales invoices and which are notes.
  • Cancelled and optional vouchers look like invoices. Both carry a flag, ISCANCELLED and ISOPTIONAL, and both must be left out, or you file invoices that were never issued. That is not a harmless mistake. FBR’s API has no way to cancel, so undoing it means using FBR’s portal, within 72 hours.
  • Voucher numbers are unique per type, not per company. “1001” can exist as a cash sale and as a credit sale. Your invoice number for FBR needs something that makes it unique, and it can only contain letters, digits and hyphens.
  • The reference field means different things. Many companies use it for the customer’s purchase order on a sales voucher, and for the original invoice on a note. Don’t copy it blindly into FBR’s reference field.
  • Some vouchers must never reach FBR. Stock journals, branch transfers and own-consumption postings are not sales to a buyer. FBR also refuses an invoice where the buyer is the seller. Filter them out before anything is sent.

Identity fields that lie

Tally’s company and party ledgers have income-tax and sales-tax number fields. By habit, many Pakistani businesses type the NTN into the first and the STRN into the second. Both are free text with no checks, so treat them as a starting point, not the final answer.

  • Leading zeros disappear the moment an ID is stored as a number. Keep every ID as text from start to finish.
  • Punctuation hides a trap. Strip the dashes from an STRN and you can end up with exactly thirteen digits, the length of a CNIC. It will pass a length check and file against the wrong identity. Check each ID by its field, not by its length.
  • Province is free text, and there is no city field. The ledger’s state field is blank in many company files, and addresses are unlabelled lines. FBR wants one of its provinces (a city name such as “Karachi” is rejected). So set the province once for each customer, in a customer table you keep alongside Tally.
  • Registration type doesn’t exist in Tally. Whether a buyer is registered or unregistered changes what FBR expects on the invoice, and Tally has nowhere to hold it. Look it up from FBR using the buyer’s number, and store it in your customer table.

Noticing new invoices

Tally can’t announce a new voucher, so something has to keep asking. The simplest way checks a short date range every few minutes and remembers which vouchers it has already handled. It works, but it reads the same vouchers again and again. It can also miss a voucher that is back-dated into a range it has already passed. A sturdier way tracks Tally’s alteration identifiers and asks only for what has changed since the highest one it last saw.

Whichever you choose, keep the gap between checks short. FBR expects each invoice to be sent as soon as it is created. A nightly run breaks that rule every single day.

Proving it before going live

You can test everything above before you send a single invoice. FBR offers a dry run: a validation check that judges an invoice by the same rules as a real one, but leaves nothing behind. No record, no number. Run a full week of real Tally vouchers through it. Then compare the total value and tax with Tally’s own sales register for the same week. If the two don’t match to the rupee, one of the traps on this page is the reason. It is far cheaper to find it now than after filing.

After that comes FBR’s test system (the sandbox). Each business gets its own set of test scenarios, never all 28, and must pass them before FBR gives live access. Our guide to FBR’s test system and going live covers that stage.

FAQs

Can Tally send invoices to FBR by itself? No. Tally has no FBR connection and cannot send data to another system. Invoices have to be read from Tally’s gateway and given the FBR fields that Tally doesn’t hold. Then each one is sent on its own, through a service that sends invoices to FBR.

Should we use ODBC instead of the XML gateway? No, not for this. Tally’s ODBC interface is 32-bit and handles one task at a time. It also flattens the nested stock lines an invoice depends on. It is fine for reports in Excel. For reading whole invoices, the XML gateway is the more reliable route.

Is it safe to open Tally’s port so a cloud service can reach it? No. The gateway has no password, so anyone who can reach it can read the entire company file. Run the reader on the same local network as Tally, and have it make only outgoing connections.

Why does our reader return no invoices on some days? Check the company name first. Tally matches it letter for letter and answers a near miss with an empty result, not an error. Then check that the company is open in Tally and that no dialog box is waiting on the Tally PC.

Where should the HS code and sale type come from if Tally doesn’t hold them? From a separate table kept alongside Tally. Set them once for each stock item or stock group, and check them against FBR’s own lists. Our guide to matching tax codes to FBR sale types walks through it.

Related reading: connecting Tally to FBR, matching tax codes to FBR sale types and FBR invoice error codes.

Ready to simplify your FBR digital invoicing?

Join 2000+ businesses that use eInvoicePro to make invoices and send them to FBR.

Chat with us