Inistate Documentation
  • Welcome
  • Workspace
  • Module
  • User
    • Role
    • Profile
    • Management
  • Configuration
    • Builder
    • Studio
  • Features
    • Basic
    • Information
    • Listing
    • Activity
    • Form
    • State
    • Flow
  • Advanced
    • Formula
    • Authorization
    • Automation
      • Automation Block
      • Scripting
    • Notification
    • Template
      • Report Starter Kit
      • Code
    • Logic
    • Integration
      • Accounting 1
        • Invoice
        • Cash Sale
        • Delivery Order
        • Stock Transfer
        • Sales Order
        • Creditor
        • Credit Note
        • Debit Note
        • A/P Invoice
        • A/P Credit Note
        • A/P Debit Note
        • Customer
      • Accounting 2
        • Debtor
        • Purchase Order
        • Good Receive
        • Payment Voucher
        • Customer Invoice
        • Customer Payment
      • Jobs json
      • Error Handling
      • Call Back
    • Scripting (Beta)
Powered by GitBook
On this page
  • Invoice's Database
  • Invoice's Table Item
  • Invoice's Sample Code
  1. Advanced
  2. Integration
  3. Accounting 1

Invoice

Invoice's Database

UI Name
Database
Type
Remarks

Debtor

String

Debtor must match existing debtor code, and they must be active state. E.g.: 400-A001

Invoice No

String

Avoid using the same Invoice No to prevent overwrite. E.g.: INV00001

Date

String

E.g.: 2024-11-27T00:00:00+08:00

Sales Location

String

Must have existing Sales Location in the accounting system. E.g.: HQ

Credit Term

String

Credit Term must match the same in the accounting system E.g.: C.O.D.

Description

String

E.g.: INVOICE

Table Item

Array

Invoice's Table Item

UI Name
Database
Type
Remarks

Item Code

String

E.g.: A001

Description

String

E.g.: PRODUCT - CL14

D/O Number

String

E.g.: DO-123-456

UOM

String

E.g.: UNIT

Location

String

Must have existing Location in the accounting system.

E.g.: HQ

Qty

Decimal

Value must be number, and value cannot be negative.

E.g.: 10.0

Unit Price

Decimal

Value must be number, and value cannot be negative.

E.g.: 10.0

Invoice's Sample Code

Remember to change to the correct field before proceeding to run ANY script.

To find {fieldName}, you are required to have access to Alpha or use Network in Developer Tools [F12].

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["ourDONo"] = x["{YOUR-DO-NO}"]
    row["uom"] = x["{YOUR-UOM}"]
    row["location"] = x["{YOUR-LOCATION}"]
    row["qty"] = x["{YOUR-QTY}"]
    row["unitPrice"] = x["{YOUR-UNIT-PRICE}"]
    return row


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

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

    # payloadData["payload"]["docNo"] = None if data['{YOUR-DOC-NO}'] == "" or data['{YOUR-DOC-NO}'] is None else data['{YOUR-DOC-NO}']
    payloadData["payload"]["docNo"] = data.DocumentId

    payloadData["payload"]["docDate"] = data["{YOUR-DOC-DATE}"]
    payloadData["payload"]["salesLocation"] = data["{YOUR-SALES-LOCATION}"]
    payloadData["payload"]["displayTerm"] = data["{YOUR-CREDIT-TERM}"]
    payloadData["payload"]["description"] = "INVOICE"

    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)
def image_to_hex(image_url):
    web_client = WebClient()
    image_data = web_client.DownloadData(image_url)

    with MemoryStream(image_data) as image_stream:
        bitmap = Bitmap(image_stream)
        # Resize the image
        original_width = bitmap.Width
        original_height = bitmap.Height
        max_width = 240.0
        max_height = 240.0
        # Calculate new dimensions preserving the aspect ratio
        ratio = min(max_width / original_width, max_height / original_height)
        new_width = int(original_width * ratio)
        new_height = int(original_height * ratio)
        debug("new_width", new_width)

        resized_bitmap = bitmap.GetThumbnailImage(
            new_width, new_height, None, IntPtr.Zero
        )

        # Convert the image to PNG (or other supported formats)
        png_stream = MemoryStream()
        # resized_bitmap.Save(png_stream, ImageFormat.Png)
        png_bytes = png_stream.ToArray()

        # Convert PNG byte array to hexadecimal string
        png_hex = "".join("{:02x}".format(b) for b in png_bytes)

        # Create RTF content with the embedded PNG image
        rtf = []
        rtf.append(
            r"{\rtf1\ansi\ansicpg1252\deff0\deflang1033{\fonttbl{\f0\fnil\fcharset0 Arial;}}"
        )
        rtf.append(r"\viewkind4\uc1")

        # image start
        stream = MemoryStream()
        resized_bitmap.Save(stream, ImageFormat.Bmp)  # Save as BMP
        stream.Position = 0  # Reset stream position

        # Read bytes and convert to hex
        img_bytes = stream.ToArray()
        # debug('img_bytes', img_bytes)
        # debug('here', 'here1')
        # hex_string = binascii.hexlify(img_bytes).decode('ascii')
        hex_string = BitConverter.ToString(img_bytes).Replace("-", "").ToLower()

        # Write the RTF content to a file
        return hex_string
        
        
PreviousAccounting 1NextCash Sale

Last updated 1 month ago

debtorCode
docNo
docDate
salesLocation
displayTerm
description
details
itemCode
description
ourDONo
uom
location
qty
unitPrice
Refer below