---
tags: mPOS, troubleshoot, maintenance
---
# Incorrect InvoNo generation (20210724 Store221 SC dies)
## Problem
SC dies, next generated sequentials (invoice number, dlv_no, etc.) are incorrect.
data columns : ( THD )
- tdate
- sys_date
- serial_no
- seq_no
- dlv_no
- b1_tax_no (e1_tax_no)
## Solution
**Proposed Solution(s):**
- [x] TKAPI query ERP
- ~~local DB on api rpi to store THD/TDL backups~~
- ~~new table on SC to store THD/TDL backups~~
<table>
<thead><tr><th>Before revision</th></tr></thead>
<tbody>
<tr><td>
1. get invoice setting from INVOICE_D / INVOICE_H@SC
- if no setting found, abort.
2. get last invoice number from THD@SC
3. check if (last invoice number + 1) is within invoice setting range
- if (last invoice number + 1) is in range
- invoice number = (last invoice number + 1)
- else
- invoice number = first in invoice setting
</td></tr>
</tbody>
<tr><td style="border: none !important;text-align:center">⇓</td></tr>
<thead><tr><th>Revised</th></tr></thead>
<tbody>
<tr><td>
1. get invoice setting from INVOICE_D / INVOICE_H@SC
- if no setting found, abort.
2. get last invoice number from THD@SC
3. get last invoice number from ERP (through TKAPI)
4. check `(invoice number + 1)@THD` and `(invoice number + 1)@ERP` is within invoice setting range
- if both not in range
- invoice number = first in invoice setting
- if `(invoice number + 1)@THD` is in range but `(invoice number + 1)@ERP` is not range
- invoice number = `(invoice number + 1)@THD`
- if `(invoice number + 1)@ERP` is in range but `(invoice number + 1)@THD` is not range
- invoice number = `(invoice number + 1)@ERP`
- if both in range
- invoice number = MAX(`(invoice number + 1)@THD`, `(invoice number + 1)@ERP`)
</td></tr>
</tbody>
</table>