Credit Note

Credit Note's Database

UI Name
Database
Type
Remarks

Doc Key

Number

E.g.: 7890

Debtor

String

E.g.: 300-T005

C/N No

String

E.g.: CN2410-005

Date

String

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

Description

String

E.g.: SALES - SE2410009 - - SNKO160240900740

C/N Type

String

E.g.: RETURN

Inclusive

Boolean

E.g.: true

Table Item

Array

Credit Note's Table Item

UI Name
Database
Type
Remarks

Item Code

String

E.g.: 500-0005

Description

String

E.g.: WAREHOUSE STORAGE FEES

Qty

Number

E.g.: 15

UOM

String

E.g.: BOX

Unit Price

Number

E.g.: 300

SubTotal

Number

E.g.: 4500

Tax Adjustment

Number

E.g.: 50

Tax Code

String

E.g.: TX-10

Credit Note's Sample Code

Remember to change to the correct field before proceeding to run the 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' } })
debug('documentList', documentList)

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

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)

Last updated