# Cash Sale

## Cash Sale's Database

<table><thead><tr><th>UI Name</th><th>Database</th><th>Type</th><th>Remarks</th></tr></thead><tbody><tr><td>Debtor</td><td><pre><code>debtorCode
</code></pre></td><td><strong><code>String</code></strong></td><td><strong><code>Debtor</code></strong> must match existing <strong>debtor code</strong>, and they must be <strong>active</strong> state.<br><br>E.g.: <code>400-A001</code></td></tr><tr><td>C/S No</td><td><pre><code>docNo
</code></pre></td><td><strong><code>String</code></strong></td><td>Avoid using the same <strong><code>C/S No</code></strong> to prevent overwrite.<br><br>E.g.: <code>CSH00001</code></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-27T00:00:00+08:00</code></td></tr><tr><td>Sales Location</td><td><pre><code>salesLocation
</code></pre></td><td><strong><code>String</code></strong></td><td>Must have existing <strong><code>Sales Location</code></strong> in the accounting system.<br><br>E.g.: <code>HQ</code></td></tr><tr><td>Credit Term</td><td><pre><code>displayTerm
</code></pre></td><td><strong><code>String</code></strong></td><td><strong><code>Credit Term</code></strong> must match the same in the accounting system<br><br>E.g.: <code>C.O.D.</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>CASH SALE</code></td></tr><tr><td>Remark 1</td><td><pre><code>Remark1
</code></pre></td><td><strong><code>String</code></strong></td><td>E.g.: <code>Sample Text</code></td></tr><tr><td>Table Item</td><td><pre><code>details
</code></pre></td><td><strong><code>Array</code></strong></td><td><a href="#cash-sales-table-item">Refer below</a></td></tr></tbody></table>

## Cash Sale'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>itemCode
</code></pre></td><td><strong><code>String</code></strong></td><td>E.g.: <code>A001</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>PRODUCT - CL14</code></td></tr><tr><td>Location</td><td><pre><code>location
</code></pre></td><td><strong><code>String</code></strong></td><td><p>Must have existing <strong><code>Location</code></strong> in the accounting system.</p><p></p><p>E.g.: <code>HQ</code></p></td></tr><tr><td>UOM</td><td><pre><code>uom
</code></pre></td><td><strong><code>String</code></strong></td><td>E.g.: <code>UNIT</code></td></tr><tr><td>Qty</td><td><pre><code>qty
</code></pre></td><td><strong><code>Decimal</code></strong></td><td><p>Value must be <strong>number</strong>, and value <strong>cannot</strong> be <strong>negative.</strong></p><p></p><p>E.g.: <code>10.0</code></p></td></tr><tr><td>Unit Price</td><td><pre><code>unitPrice
</code></pre></td><td><strong><code>Decimal</code></strong></td><td><p>Value must be <strong>number</strong>, and value <strong>cannot</strong> be <strong>negative.</strong></p><p></p><p>E.g.: <code>10.0</code></p></td></tr></tbody></table>

## Cash Sale'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}": {"$ne": None},
    }
)
debug("documentList", documentList)

resultList = []

def prepareRow(x):
    row = JObject()
    row["itemCode"] = x["{YOUR-ITEM-CODE}"]
    row["description"] = x["{YOUR-DESCRIPTION}"].ToString()
    row["location"] = x["{YOUR-LOCATION}"]
    row["uom"] = x["{YOUR-UOM}"]
    row["qty"] = x["{YOUR-QTY}"]
    row["unitPrice"] = x["{YOUR-UNIT-PRICE}"]
    return row


def preparePayload(data):
    payloadData = JObject()
    payloadData["type"] = "CashSale"
    payloadData["payload"] = JObject()

    payloadData["payload"]["debtorCode"] = data["{YOUR-DEBTOR-CODE}"]

    # payloadData["payload"]["docNo"] = None if data['Running Number'] == "" or data['Running Number'] is None else data['Running Number']
    payloadData["payload"]["docNo"] = data.DocumentId

    payloadData["payload"]["docDate"] = data["{YOUR-DOC-DATE}"]
    payloadData["payload"]["salesLocation"] = data["{YOUR-SALES-LOCATION}"]
    payloadData["payload"]["displayTerm"] = "C.O.D."
    payloadData["payload"]["description"] = "CASH SALE"
    payloadData["payload"]["Remark1"] = data["{YOUR-REMARK1}"]

    payloadData["payload"]["details"] = JArray.FromObject(
        data["{YOUR-TABLE}"].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)
debug("result", resultList)
```

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