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('Company Profile').findAll({
'Audit.UpdatedDate': { '$gt': Inputs['lastSync'] },
'Audit.UpdatedBy': { "$ne": 'External' },
#Put Business logic here
})
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 preparePayload(data):
payloadData = JObject()
payloadData["type"] = "AR_CUSTOMER"
payloadData["payload"] = JObject()
if data['Customer Code'] != None and data['Customer Code'] != "":
payloadData["payload"]["CODE"] = data['Customer Code']
payloadData["payload"]["CONTROLACCOUNT"] = '300-000'
payloadData["payload"]["COMPANYNAME"] = data['Company Name']
payloadData["payload"]["BRN2"] = data['Reg No.'] if data['Reg No.'] is not None else ""
payloadData["payload"]["CURRENCYCODE"] = "----"
payloadData["payload"]["CREDITTERM"] = str(data['Payment Term'])
payloadData["payload"]["AGENT"] = str(data['Sales Staff']) if data['Sales Staff'] is not None else ""
payloadData["payload"]["cdsBranch"] = JArray.FromObject([{
"CODE": data['Customer Code'],
"BRANCHTYPE": "B",
"BRANCHNAME": str(data['Branch']) if data['Branch'] is not None else "",
"PHONE1": data['Tel 1'] if data['Tel 1'] is not None else "",
"PHONE2": data['Tel 2'] if data['Tel 2'] is not None else "",
"FAX1": data['Fax 1'] if data['Fax 1'] is not None else "",
"FAX2": data['Fax 2'] if data['Fax 2'] is not None else "",
"EMAIL": data['Email 1'] if data['Email 1'] is not None else " ",
"ADDRESS1": str(data['Address 1'])[:40] if data['Address 1'] is not None else "",
"ADDRESS2": str(data['Address 2'])[:40] if data['Address 2'] is not None else "",
"ADDRESS3": str(data['Address 3'])[:40] if data['Address 3'] is not None else "",
"ADDRESS4": str(data['Address 4'])[:40] if data['Address 4'] is not None else "",
"ATTENTION": str(data['Person In Charge']) if data['Person In Charge'] is not None else ""
}])
payloadData["LastModified"] = data.UpdatedDate
return payloadData
#end of added
for entry in documentList:
payloadData = preparePayload(entry)
resultList.append(payloadData)
outputs['results'] = JArray.FromObject(resultList)