> For the complete documentation index, see [llms.txt](https://kb.logicerp.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://kb.logicerp.com/logic-erp-learning-hub-tutorials-and-best-practices/api-documentation/sale/push/cancel-a-sale-invoice.md).

# Cancel a Sale Invoice

Cancels a **sale invoice** that is already saved in Logic ERP. The invoice is not removed: it stays in Logic ERP **marked as cancelled**, its number stays used, and everything it had put in place is **reversed** — the stock comes back, its accounting entries are removed, and loyalty points, discount coupons and gift vouchers linked to it are released. You can point to the invoice by its **bill series and number**, by your **party order number**, or by Logic ERP's internal number returned when it was saved. A cancel cannot be undone.

### **Endpoint**

| Name          | Detail                                   |
| ------------- | ---------------------------------------- |
| Address       | `https://your-server/api/CancelSaleBill` |
| Method        | `POST`                                   |
| Authorization | Basic Auth                               |

The response and the common messages are explained in [How Every API Call Works](https://kb.logicerp.com/logic-erp-learning-hub-tutorials-and-best-practices/api-documentation/how-every-api-call-works.md).

Send one invoice per request. Field names are matched without regard to upper/lower case; fields not listed below are ignored.

### **Request fields**

| Field           | Type   | Required              | Default | Meaning                                                                                                                                                                                                                                                                                                                                    |
| --------------- | ------ | --------------------- | ------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
| `Branch_Code`   | number | **Yes** (in practice) | `0`     | The branch the invoice belongs to (`Branch_Code` in [Get Branch Info](https://kb.logicerp.com/logic-erp-learning-hub-tutorials-and-best-practices/api-documentation/masters/pull/get-branch-info.md)). The invoice is looked for in this branch only, so with `0` it is never found. A branch short name is not accepted here.             |
| `DocCode`       | number | One of the three ways | `0`     | Logic ERP's internal number of the invoice – the `LastSavedCode` you received when you saved it with [Save a Sale Invoice](https://kb.logicerp.com/logic-erp-learning-hub-tutorials-and-best-practices/api-documentation/sale/push/save-a-sale-invoice.md). When it is above `0`, `BillSeries`, `Bill_No` and `PartyOrderNo` are not used. |
| `BillSeries`    | text   | One of the three ways | empty   | The bill series of the invoice as shown in Logic ERP, for example `"WSL"`. Must be a **sale** series of the branch. Use it together with `Bill_No`.                                                                                                                                                                                        |
| `Bill_No`       | number | With `BillSeries`     | `0`     | The invoice number within the series, for example `59`. Send a **number** only, not text such as `"WSL-59"`.                                                                                                                                                                                                                               |
| `PartyOrderNo`  | text   | One of the three ways | empty   | Your own order number that was sent as the party order number when the invoice was saved. Used when `DocCode` is `0` and the invoice was not found by series and number.                                                                                                                                                                   |
| `Cancel_Reason` | text   | **Yes**               | –       | Why the invoice is cancelled. Stored with the cancellation, together with the Logic ERP user, date and time.                                                                                                                                                                                                                               |
| `DisplayError`  | text   | No                    | `"0"`   | `"1"` = when the cancel fails, `Message` carries the full technical detail instead of the short reason. Useful when the short message does not say why (see the known issue below).                                                                                                                                                        |

### **Rules Logic ERP applies**

**Finding the invoice**

* The invoice must belong to the branch in `Branch_Code` and to the **current financial year** – the year that contains today's date on the server. An invoice of an earlier year is not found; cancel it in Logic ERP itself.
* With `DocCode` above `0`, the invoice with that internal number is used.
* Otherwise `BillSeries` + `Bill_No` are tried first. The series must exist in the branch as a sale series, or the request fails with *Unable to Retrive Bill Series : …*. If no invoice has that number, Logic ERP then looks for the `PartyOrderNo`.
* `PartyOrderNo` must match exactly one invoice of the branch in the current year. If more than one invoice carries it, nothing is cancelled and you get *Duplicate Bill exists againts Party Order No. …*.
* You must send at least `DocCode`, or `BillSeries` with `Bill_No`, or `PartyOrderNo`.

> **Known issue – `DocCode` of a sale return.** `DocCode` is not checked to be a sale invoice. If it belongs to a **sale return** of the same branch and year, that return is cancelled. Send only the `LastSavedCode` of a sale invoice.

**What blocks a cancel**

Nothing is changed when any of these applies:

* The invoice is **already cancelled**.
* `Cancel_Reason` is empty.
* A **GST e-invoice** (IRN) has been generated for it.
* It has been **marked as posted** in Logic ERP's document posting.
* It was **transferred to head office**, **exported to another branch** or **imported from another branch**, when the user rights of your API user stop changes to such documents.
* The invoice date is in a period that is **locked** for entry or modification, is **on or before the lock date** for modifications, or is **on or before the last physical stock entry** date (when that lock is switched on).
* It has been **approved**, when approved documents may not be changed.
* **Payments or receipts have been adjusted** against it, when bills with payments are locked in the user rights. When they are not locked, the cancel goes ahead and those adjustments are removed from the invoice.
* A GST advance receipt has been adjusted against it, it is in an excise register, it is linked to a retail customer discount package (when discount packages are switched on), or it was changed by someone else while the cancel was running.
* Cancelling would make the stock of a lot negative, when negative stock is not allowed.
* The invoice was used to calculate a customer bonus.

> **Invoices made from goods sent on approval.** For an invoice that is linked to a sale-on-approval document and contains approval lots, the cancel does not go through: the answer is `Status: false` with `Document No. … was not cancelled.` Cancel such invoices in Logic ERP.

**What a cancel does**

All of the following happen together, or none of them (a failure part-way undoes everything):

* The invoice and its lines are **marked as cancelled** and kept. Its number is **not** freed for re-use.
* **Stock** of the lines sold directly comes back to the godown it was sold from. Lines that were billed from a sale challan or a packing slip go back to that document instead (see the next point), and lines that do not affect stock are left as they are.
* Quantities taken from **sale orders**, **sale challans**, **delivery orders** and packing slips become **pending** again on those documents.
* Its **accounting entries** are removed, including TDS and TCS entries, and receipts adjusted against it are unlinked.
* **Loyalty points** earned or redeemed on the invoice are removed from the customer's points ledger.
* **Discount coupons** used on the invoice are released for use again, and **gift vouchers** linked to it are unlinked from it.
* Employee-discount ledger entries, scheme and free-item records, customer discount card entries, retail customer ledger entries, warranty, home delivery, alteration and appointment links of the invoice are removed.
* The reason, the Logic ERP user and the date and time are recorded, and an entry is written to the audit trail.

**After the cancel**

* A cancelled invoice **cannot be cancelled again** (*Document has already been cancelled...*), and there is no endpoint that un-cancels it.
* The cancel runs as the Logic ERP user linked to your API site, so that user's rights and locks apply.

> **Known issue – the reason is sometimes missing from `Message`.** Some refusals (for example the customer-bonus check) come back as *The operation was canceled.*, without the reason, even with `"DisplayError": "1"`. Nothing was cancelled. Check the invoice in Logic ERP, or ask your Logic ERP team.

### **Example request**

By series and number:

```json
{
  "Branch_Code": 2,
  "BillSeries": "WSL",
  "Bill_No": 59,
  "Cancel_Reason": "Customer cancelled the order"
}
```

By the internal number returned when the invoice was saved:

```json
{
  "Branch_Code": 2,
  "DocCode": 10452,
  "Cancel_Reason": "Duplicate invoice"
}
```

### **Response**

| Field            | Type         | Meaning                                                                                                                                                                                       |
| ---------------- | ------------ | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
| `Status`         | true / false | `true` when the invoice was cancelled.                                                                                                                                                        |
| `Message`        | text         | `"Success"` when cancelled; otherwise the reason.                                                                                                                                             |
| `LastSavedCode`  | number       | Logic ERP's internal number of the cancelled invoice. `0` when the request failed.                                                                                                            |
| `LastSavedDocNo` | text         | The invoice number: the printed invoice number when the series uses one, otherwise Logic ERP's internal form of series and number (for example `"SLWSL 59"`). `null` when the request failed. |
| `request_ID`     | text         | As described on the common page. `null` when the request failed before the invoice was found.                                                                                                 |

### **Example response**

```json
{
  "LastSavedDocNo": "SLWSL 59",
  "Status": true,
  "Message": "Success",
  "LastSavedCode": 10452,
  "request_ID": "6f1c2a9e-3b7d-4c55-9a0e-2d8f61b0c7aa"
}
```

### **Messages you may see**

| Message                                                                                                                                                                                                                                                 | Meaning                                                                                                                                                                                | What to do                                                  |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------- |
| `Please Define Bill No. Or PartyOrderNo to Continue...`                                                                                                                                                                                                 | None of `DocCode`, `BillSeries` or `PartyOrderNo` was sent.                                                                                                                            | Send one of them.                                           |
| `Bill No. not defined against Bill Series : …`                                                                                                                                                                                                          | `BillSeries` sent with `Bill_No` `0` or missing.                                                                                                                                       | Send the invoice number.                                    |
| `Unable to Retrive Bill Series : …`                                                                                                                                                                                                                     | No sale series with that name in the branch.                                                                                                                                           | Check the series and `Branch_Code`.                         |
| `Duplicate Bill exists againts Party Order No. : … in Branch Code : …`                                                                                                                                                                                  | More than one invoice matched.                                                                                                                                                         | Send `DocCode` or the series and number.                    |
| `Unable to found Document againts Party Order No. … and Branch Code : …`                                                                                                                                                                                | No invoice matched in that branch and the current financial year – also for a `DocCode` or a series and number that is not found (the message names the party order number even then). | Check the branch, the number and the year.                  |
| `Document No. … was not cancelled.`                                                                                                                                                                                                                     | The cancel ran but the invoice is still not cancelled (for example an invoice made from goods sent on approval).                                                                       | Check the invoice in Logic ERP and cancel it there.         |
| `Document has already been cancelled...`                                                                                                                                                                                                                | The invoice is already cancelled.                                                                                                                                                      | Nothing to do.                                              |
| `Please Enter Cancel Remarks to Continue...`                                                                                                                                                                                                            | `Cancel_Reason` is empty.                                                                                                                                                              | Send a reason.                                              |
| `GST eInvoice has been Generated. Cannot allow Cancellation.`                                                                                                                                                                                           | An e-invoice exists for this invoice.                                                                                                                                                  | Cancel the e-invoice and the invoice in Logic ERP.          |
| `Cannot allow cancellation.... This document has been posted.....`                                                                                                                                                                                      | The invoice is marked as posted.                                                                                                                                                       | Ask your Logic ERP team.                                    |
| `This document has been transferred to H.O. Modification not allowed......`, `This document has been Exported to other Branches. Modification not allowed......`, `This document has been imported from other Branches. Modification not allowed......` | The invoice was transferred between branches.                                                                                                                                          | Cancel it where it was created, or ask your Logic ERP team. |
| `Entry/Modification is locked for this period....`                                                                                                                                                                                                      | The invoice date is in a locked period.                                                                                                                                                | Ask your Logic ERP team.                                    |
| `The operation was canceled.`                                                                                                                                                                                                                           | Refused by a check that does not return its reason (for example the customer-bonus check). Nothing was cancelled.                                                                      | Check the invoice in Logic ERP, or ask your Logic ERP team. |


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation by asking a question.

Perform an HTTP GET request on the following URL with the `ask` and `goal` query parameters:

```
GET https://kb.logicerp.com/logic-erp-learning-hub-tutorials-and-best-practices/api-documentation/sale/push/cancel-a-sale-invoice.md?ask=<question>&goal=<user_goal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is what the user is ultimately trying to achieve, the reason they need the answer. Sharing it helps GitBook give you a better, more relevant answer. A goal is most helpful when it describes the outcome the user wants rather than restating the question. For example, with `ask=how do I create an API token`, a goal like `automate deployments from our CI pipeline` lets GitBook tailor the answer to that use case.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
