# Debit Note

## Debit Note's Database

<table><thead><tr><th>UI Name</th><th width="173">Database</th><th width="100">Type</th><th width="225">Remarks</th><th>Important</th></tr></thead><tbody><tr><td>Debtor</td><td><pre><code>debtorCode
</code></pre></td><td><strong><code>String</code></strong></td><td>E.g.: <code>301-Z999</code></td><td></td></tr><tr><td>D/N No</td><td><pre><code>docNo
</code></pre></td><td><strong><code>String</code></strong></td><td>E.g.: <code>DN2412-999</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-12-31T00: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 - SI2412999 - ABCD1234 -</code></td><td>Limit 80 Characters Max</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>Display Term</td><td><pre><code>displayTerm
</code></pre></td><td><strong><code>String</code></strong></td><td>E.g.: <code>NET 15</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="#debit-notes-table-item">Refer Below</a></td><td></td></tr></tbody></table>

## Debit 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-9999</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>SERVICE CHARGES</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>5</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>PCS</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>1234.56</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>6172.80</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>SV-0</code></td></tr></tbody></table>

## Debit 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['{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-ARDN}'] == True:
        payloadData["type"] = "ARDN"
    else:
        payloadData["type"] = "DN"
        
    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-DEBIT-NOTE-DATE}']
    payloadData["payload"]["description"] = data['{YOUR-TRANSACTION-DESCRIPTION}']
    payloadData["payload"]["journalType"] = "SALES"
    payloadData["payload"]["inclusiveTax"] = True
    payloadData["payload"]["displayTerm"] = data['{YOUR-TERM}']
    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 %}
