> For the complete documentation index, see [llms.txt](https://inistate.gitbook.io/home/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://inistate.gitbook.io/home/advanced/integration/accounting-1/credit-note.md).

# Credit Note

## Credit Note's Database

<table><thead><tr><th>UI Name</th><th width="164">Database</th><th width="107">Type</th><th width="222">Remarks</th><th>Important</th></tr></thead><tbody><tr><td>Doc Key</td><td><pre><code>docKey
</code></pre></td><td><strong><code>Number</code></strong></td><td>E.g.: <code>7890</code></td><td></td></tr><tr><td>Debtor</td><td><pre><code>debtorCode
</code></pre></td><td><strong><code>String</code></strong></td><td>E.g.: <code>300-T005</code></td><td></td></tr><tr><td>C/N No</td><td><pre><code>docNo
</code></pre></td><td><strong><code>String</code></strong></td><td>E.g.: <code>CN2410-005</code></td><td></td></tr><tr><td>Date</td><td><pre><code>docDate
</code></pre></td><td><strong><code>String</code></strong></td><td>E.g.: <code>2024-11-20T00:00:00+08:00</code></td><td></td></tr><tr><td>Description</td><td><pre><code>description
</code></pre></td><td><strong><code>String</code></strong></td><td>E.g.: <code>SALES - SE2410009 - - SNKO160240900740</code></td><td>Limit 80 Characters Max</td></tr><tr><td>C/N Type</td><td><pre><code>journalType
</code></pre></td><td><strong><code>String</code></strong></td><td>E.g.: <code>RETURN</code></td><td></td></tr><tr><td>Inclusive</td><td><pre><code>inclusiveTax
</code></pre></td><td><strong><code>Boolean</code></strong></td><td>E.g.: <code>true</code></td><td></td></tr><tr><td>Table Item</td><td><pre><code>details
</code></pre></td><td><strong><code>Array</code></strong></td><td><a href="#credit-notes-table-item">Refer Below</a></td><td></td></tr></tbody></table>

## Credit Note's Table Item

<table><thead><tr><th>UI Name</th><th>Database</th><th>Type</th><th>Remarks</th></tr></thead><tbody><tr><td>Item Code</td><td><pre><code>accNo
</code></pre></td><td><strong><code>String</code></strong></td><td>E.g.: <code>500-0005</code></td></tr><tr><td>Description</td><td><pre><code>description
</code></pre></td><td><strong><code>String</code></strong></td><td>E.g.: <code>WAREHOUSE STORAGE FEES</code></td></tr><tr><td>Qty</td><td><pre><code>qty
</code></pre></td><td><strong><code>Number</code></strong></td><td>E.g.: <code>15</code></td></tr><tr><td>UOM</td><td><pre><code>uom
</code></pre></td><td><strong><code>String</code></strong></td><td>E.g.: <code>BOX</code></td></tr><tr><td>Unit Price</td><td><pre><code>unitPrice
</code></pre></td><td><strong><code>Number</code></strong></td><td>E.g.: <code>300</code></td></tr><tr><td>SubTotal</td><td><pre><code>subtotal
</code></pre></td><td><strong><code>Number</code></strong></td><td>E.g.: <code>4500</code></td></tr><tr><td>Tax Adjustment</td><td><pre><code>taxAdjustment
</code></pre></td><td><strong><code>Number</code></strong></td><td>E.g.: <code>50</code></td></tr><tr><td>Tax Code</td><td><pre><code>taxCode
</code></pre></td><td><strong><code>String</code></strong></td><td>E.g.: <code>TX-10</code></td></tr></tbody></table>

## Credit Note's Sample Code

{% hint style="warning" %}
Remember to change to the **correct field** before proceeding to run **ANY** script.
{% endhint %}

{% tabs %}
{% tab title="Logic Script" %}
{% hint style="info" %}
To find `{fieldName}`, you are required to have access to **Alpha** or use **Network** in Developer Tools `[F12]`.
{% endhint %}

{% code fullWidth="true" %}

```python
import clr
clr.AddReference("Newtonsoft.Json")
clr.AddReference("System.Net")
clr.AddReference("System.IO")

from System.Text import *
from Newtonsoft.Json import *
from Newtonsoft.Json.Linq import *
from System import *

clr.AddReference("System.Drawing")
from System.Drawing import Bitmap, Image
from System.Drawing.Imaging import ImageFormat
from System.IO import MemoryStream, StreamWriter
from System.Net import WebClient
import binascii
from System import BitConverter

#search
documentList = Module('{YOUR-MODULE-NAME}').findAll({ 
    'Audit.UpdatedDate': { '$gt': Inputs['lastSync'] }, 
    'Audit.UpdatedBy': { "$ne": 'External' }, 
    'Dynamic.Extension.{fieldName}': {'$in': [None, '']}, #Error Message
    "Dynamic.Extension.{fieldName}": {"$eq" : None }, #E-Invoice Validated Date
})
debug('documentList', documentList)

resultList = []

def prepareRow(x):
    row = JObject()
    row["accNo"] = x['Item Codes']['{YOUR-ITEM-CODE}']
    row["description"] = x['{YOUR-DESCRIPTION}']
    row["qty"] = x['{YOUR-QUANTITY}']
    row["uom"] = "N/A"
    row["unitPrice"] = x['{YOUR-UNIT-PRICE}']
    row["subtotal"] = x['{YOUR-TOTAL}']
    row["taxAdjustment"] = None
    if x["{YOUR-TAX-CODE}"].Id != 0:
        row["taxCode"] = x['{YOUR-TAX-CODE}']['{YOUR-TAX-CODE}']
        # row["taxRate"] = x['{YOUR-TAX-CODE}']['{YOUR-PERCENTAGE}'].ToString()
    return row
    
def preparePayload(data):
    payloadData = JObject()
    if data['{YOUR-IS-ARCN}'] == True:
        payloadData["type"] = "ARCN"
    else:
        payloadData["type"] = "CN"
        
    payloadData["cancelled"] = data['{YOUR-CANCELLED}']
    payloadData["payload"] = JObject()
    
    if data['{YOUR-CODE}'] != None and data['{YOUR-CODE}'] != 0:
        payloadData["payload"]["docKey"] = data['{YOUR-CODE}'] 
    
    payloadData['payload']['debtorCode'] = data['{YOUR-COMPANY-CODE}']
    payloadData['payload']['docNo'] = None if data['{YOUR-RUNNING-NUMBER}'] == "" or data['{YOUR-RUNNING-NUMBER}'] is None else data['{YOUR-RUNNING-NUMBER}']
    payloadData['payload']['docDate'] = data['{YOUR-CREDIT-NOTE-DATE}']
    payloadData['payload']['description'] = data['{YOUR-TRANSACTION-DESCRIPTION}']
    payloadData['payload']['journalType'] = "SALES"
    payloadData['payload']['inclusiveTax'] = True
    payloadData['payload']['details'] = JArray.FromObject(data['{YOUR-DETAILS}'].Select(lambda x: prepareRow(x)))
    
    payloadData["LastModified"] = data.UpdatedDate
    return payloadData

#end of added
for entry in documentList:
    payloadData = preparePayload(entry)
    if payloadData["payload"]["debtorCode"] != None and payloadData["payload"]["debtorCode"] != "":
        resultList.append(payloadData)
    #debug('payloadData', payloadData)
    
#debug('done', resultList)

outputs['results'] = JArray.FromObject(resultList)
```

{% endcode %}
{% endtab %}
{% endtabs %}
