Skip to content

Instantly share code, notes, and snippets.

@marawan-nwh
Created March 12, 2023 11:03
Show Gist options
  • Save marawan-nwh/b74bc630e887af015f5ac241134b4a02 to your computer and use it in GitHub Desktop.
Save marawan-nwh/b74bc630e887af015f5ac241134b4a02 to your computer and use it in GitHub Desktop.
/**
* @NApiVersion 2.1
* @NScriptType Restlet
*/
define([
"N/record",
"N/runtime",
"N/search",
"/SuiteApps/com.netsuite.base/api",
"/SuiteApps/com.netsuite.base/utils",
], function (record, runtime, search, api, utils) {
let scriptID = "RL-JobCodes";
let correlationID;
let logger;
let Items = [];
let DelayItemID;
let TriangulationItemID;
let GensetItemID;
let XRayDelayItemID;
let NolonItemID;
let AccessoriesItemID;
let CustomFormID;
function init() {
// production
if (runtime.accountId == "7237495") {
NolonItemID = 1012;
Items = [
{ id: 1012, name: "TRUCKING SERVICE" },
{ id: 1114, name: "Delay" },
{ id: 1112, name: "Generator Genset" },
{ id: 1116, name: "Labels" },
{ id: 1115, name: "Other Expenses" },
{ id: 1113, name: "Triangulation" },
{ id: 1313, name: "Allocation Fees" },
{ id: 1319, name: "Badr-Port" },
{ id: 1318, name: "Cil-EG" },
{ id: 1317, name: "Dam Port" },
{ id: 1314, name: "Load/Off Loading Fees" },
{ id: 1315, name: "Suez Canal" },
{ id: 1316, name: "Wieght / Scales" },
{ id: 921, name: "DPW - ops" },
{ id: 893, name: "أطلنتس - ops" },
{ id: 875, name: "أيصالات طريق-ops" },
{ id: 878, name: "اكرامية - ops" },
{ id: 879, name: "ايصال ارضيات - ops" },
{ id: 880, name: "ايصال تحميل/تعتيق - ops" },
{ id: 881, name: "ايصال تصريح جمركى - ops" },
{ id: 882, name: "ايصال جيش - ops" },
{ id: 883, name: "ايصال ساحة صينية - ops" },
{ id: 884, name: "ايصال سيناء - ops" },
{ id: 885, name: "ايصال طرق وكبارى - ops" },
{ id: 886, name: "ايصال غرامه ارضيه - ops" },
{ id: 887, name: "ايصال معدية - ops" },
{ id: 888, name: "ايصال ميزان بيسكول - ops" },
{ id: 889, name: "ايصال نفق - ops" },
{ id: 890, name: "ايصال نوباتجية - ops" },
{ id: 891, name: "ايصال هيئة الميناء - ops" },
{ id: 892, name: "ايصال VGM - ops" },
{ id: 874, name: "ايصالات AICT - OPS" },
{ id: 894, name: "ايصلات تخصيص - ops" },
{ id: 877, name: "ايصلات هيئة - OPS" },
{ id: 895, name: "بوصله - ops" },
{ id: 896, name: "بوليصة مشترك - ops" },
{ id: 897, name: "تأشيرة / تجديد تأشيره - ops" },
{ id: 898, name: "تحميل - ops" },
{ id: 899, name: "تخصيص - ops" },
{ id: 900, name: "تعتيق - ops" },
{ id: 917, name: "ساجة هامبورج - ops" },
{ id: 904, name: "ساحة الجودة - ops" },
{ id: 901, name: "ساحة النورس - ops" },
{ id: 906, name: "ساحة الوطنية - ops" },
{ id: 902, name: "شفت حاويه - ops" },
{ id: 903, name: "شهاده - ops" },
{ id: 905, name: "ضمان الخط - ops" },
{ id: 873, name: "ضمان توكيل - OPS" },
{ id: 907, name: "ضمان جمرك - ops" },
{ id: 908, name: "ضمان خط - ops" },
{ id: 909, name: "عطلة X-ray - ops" },
{ id: 910, name: "فاتورة xray - ops" },
{ id: 876, name: "فك بوصلة - OPS" },
{ id: 911, name: "فك ستوب - ops" },
{ id: 912, name: "كارتات - ops" },
{ id: 913, name: "كارتة محاجر - ops" },
{ id: 871, name: "كشف استخلاص - OPS" },
{ id: 914, name: "م انتقالات - ops" },
{ id: 915, name: "م تحميل - ops" },
{ id: 916, name: "م تخليص - ops" },
{ id: 918, name: "م تصريح - ops" },
{ id: 919, name: "م تعتيق - ops" },
{ id: 920, name: "م زيادة وزن - ops" },
{ id: 922, name: "م غرامات - ops" },
{ id: 923, name: "م محضر - ops" },
{ id: 924, name: "م PTI - ops" },
{ id: 925, name: "مصروف ميناء - ops" },
{ id: 872, name: "مصروف ميناء - OPS" },
{ id: 926, name: "مصروفات اخري - ops" },
{ id: 927, name: "موازين هيئة - ops" },
{ id: 834, name: "B/L بوصيله" },
{ id: 932, name: "DPW" },
{ id: 851, name: "ايصال تخصيص / تجديد تخصيص" },
{ id: 858, name: "ايصال شروق" },
{ id: 928, name: "أطلنتس" },
{ id: 813, name: "أيصالات طريق" },
{ id: 865, name: "اكرامية" },
{ id: 862, name: "ايصال ارضيات" },
{ id: 852, name: "ايصال تحميل/تعتيق" },
{ id: 864, name: "ايصال تصريح جمركى" },
{ id: 849, name: "ايصال جيش" },
{ id: 859, name: "ايصال ساحة صينية" },
{ id: 857, name: "ايصال سيناء" },
{ id: 863, name: "ايصال طرق وكبارى" },
{ id: 854, name: "ايصال غرامه ارضيه" },
{ id: 855, name: "ايصال معدية" },
{ id: 853, name: "ايصال ميزان بيسكول" },
{ id: 856, name: "ايصال نفق" },
{ id: 868, name: "ايصال نوباتجية" },
{ id: 850, name: "ايصال هيئة الميناء" },
{ id: 860, name: "ايصال VGM" },
{ id: 861, name: "ايصال XRAY" },
{ id: 814, name: "ايصالات AICT" },
{ id: 815, name: "ايصلات تخصيص" },
{ id: 816, name: "ايصلات هيئة" },
{ id: 831, name: "بوصله" },
{ id: 844, name: "بوليصة مشترك" },
{ id: 846, name: "تأشيرة / تجديد تأشيره" },
{ id: 822, name: "تحميل" },
{ id: 829, name: "تخصيص" },
{ id: 823, name: "تعتيق" },
{ id: 931, name: "ساجة هامبورج" },
{ id: 929, name: "ساحة الجودة" },
{ id: 869, name: "ساحة النورس" },
{ id: 930, name: "ساحة الوطنية" },
{ id: 848, name: "شفت حاويه" },
{ id: 832, name: "شهاده" },
{ id: 812, name: "ضمان توكيل" },
{ id: 824, name: "ضمان الخط" },
{ id: 817, name: "ضمان توكيل" },
{ id: 825, name: "ضمان جمرك" },
{ id: 830, name: "ضمان خط" },
{ id: 866, name: "عطلة X-ray" },
{ id: 826, name: "فاتورة xray" },
{ id: 818, name: "فك بوصلة" },
{ id: 847, name: "فك ستوب" },
{ id: 819, name: "كارتات" },
{ id: 870, name: "كارتة محاجر" },
{ id: 820, name: "كشف استخلاص" },
{ id: 843, name: "م انتقالات" },
{ id: 835, name: "م تحميل" },
{ id: 841, name: "م تخليص" },
{ id: 839, name: "م تصريح" },
{ id: 836, name: "م تعتيق" },
{ id: 867, name: "م زيادة وزن" },
{ id: 838, name: "م ضمان جمرك" },
{ id: 837, name: "م ضمان خط" },
{ id: 840, name: "م غرامات" },
{ id: 833, name: "م محضر" },
{ id: 842, name: "م PTI" },
{ id: 821, name: "مصروف ميناء" },
{ id: 845, name: "مصروفات اخري" },
{ id: 827, name: "موازين هيئة" },
{ id: 828, name: "ميزان هيئة" },
];
DelayItemID = 1114;
TriangulationItemID = 1113;
GensetItemID = 1112;
XRayDelayItemID = 866;
AccessoriesItemID = 1413;
CustomFormID = 168;
}
// sandbox
if (runtime.accountId == "7237495_SB1") {
NolonItemID = 46;
Items = [
{ id: 46, name: "النولون" },
{ id: 10, name: "Official Receipts service" },
{ id: 11, name: "Road Tolls" },
{ id: 12, name: "Allocation Fees" },
{ id: 13, name: "Load/Off Loading Fees" },
{ id: 32, name: "Accessories" },
{ id: 33, name: "Generator Genset" },
{ id: 34, name: "Labels" },
{ id: 35, name: "Triangulation" },
{ id: 36, name: "ضمان توكيل" },
{ id: 37, name: "أيصالات طريق" },
{ id: 38, name: "ايصالات AICT" },
{ id: 39, name: "ايصلات تخصيص" },
{ id: 40, name: "ايصلات هيئة" },
{ id: 41, name: "ضمان توكيل" },
{ id: 42, name: "فك بوصلة" },
{ id: 43, name: "كارتات" },
{ id: 44, name: "كشف استخلاص" },
{ id: 45, name: "مصروف ميناء" },
{ id: 47, name: "تحميل" },
{ id: 48, name: "تعتيق" },
{ id: 49, name: "ضمان الخط" },
{ id: 50, name: "ضمان جمرك" },
{ id: 51, name: "فاتورة xray" },
{ id: 52, name: "موازين هيئة" },
{ id: 55, name: "ميزان هيئة" },
{ id: 56, name: "تخصيص" },
{ id: 57, name: "ضمان خط" },
{ id: 258, name: "بوصله" },
{ id: 259, name: "شهاده" },
{ id: 260, name: "م محضر" },
{ id: 261, name: "B/L بوصيله" },
{ id: 262, name: "م تحميل" },
{ id: 263, name: "م تعتيق" },
{ id: 264, name: "م ضمان خط" },
{ id: 265, name: "م ضمان جمرك" },
{ id: 266, name: "م تصريح" },
{ id: 267, name: "م غرامات" },
{ id: 268, name: "م تخليص" },
{ id: 269, name: "م PTI" },
{ id: 270, name: "م انتقالات" },
{ id: 271, name: "بوليصة مشترك" },
{ id: 272, name: "مصروفات اخري" },
{ id: 273, name: "تأشيرة / تجديد تأشيره" },
{ id: 274, name: "فك ستوب" },
{ id: 275, name: "شفت حاويه" },
{ id: 358, name: "ايصال جيش" },
{ id: 359, name: "ايصال هيئة الميناء" },
{ id: 360, name: "ايصال تخصيص / تجديد تخصيص" },
{ id: 361, name: "ايصال تحميل/تعتيق" },
{ id: 362, name: "ايصال ميزان بيسكول" },
{ id: 363, name: "ايصال غرامه ارضيه" },
{ id: 364, name: "ايصال معدية" },
{ id: 365, name: "ايصال نفق" },
{ id: 366, name: "ايصال سيناء" },
{ id: 367, name: "ايصال شروق" },
{ id: 368, name: "ايصال ساحة صينية" },
{ id: 369, name: "ايصال VGM" },
{ id: 370, name: "ايصال XRAY" },
{ id: 371, name: "ايصال ارضيات" },
{ id: 372, name: "ايصال طرق وكبارى" },
{ id: 373, name: "ايصال تصريح جمركى" },
{ id: 374, name: "اكرامية" },
{ id: 375, name: "عطلة X-ray" },
{ id: 376, name: "م زيادة وزن" },
{ id: 377, name: "ايصال نوباتجية" },
{ id: 378, name: "ساحة النورس" },
{ id: 379, name: "كارتة محاجر" },
{ id: 458, name: "كشف استخلاص - OPS" },
{ id: 459, name: "مصروف ميناء - OPS" },
{ id: 460, name: "ضمان توكيل - OPS" },
{ id: 463, name: "ايصالات AICT - OPS" },
{ id: 464, name: "أيصالات طريق-ops" },
{ id: 465, name: "فك بوصلة - OPS" },
{ id: 466, name: "ايصلات هيئة - OPS" },
{ id: 470, name: "اكرامية - ops" },
{ id: 472, name: "ايصال ارضيات - ops" },
{ id: 473, name: "ايصال تحميل/تعتيق - ops" },
{ id: 474, name: "ايصال تصريح جمركى - ops" },
{ id: 475, name: "ايصال جيش - ops" },
{ id: 476, name: "ايصال ساحة صينية - ops" },
{ id: 477, name: "ايصال سيناء - ops" },
{ id: 478, name: "ايصال طرق وكبارى - ops" },
{ id: 479, name: "ايصال غرامه ارضيه - ops" },
{ id: 480, name: "ايصال معدية - ops" },
{ id: 481, name: "ايصال ميزان بيسكول - ops" },
{ id: 482, name: "ايصال نفق - ops" },
{ id: 483, name: "ايصال نوباتجية - ops" },
{ id: 484, name: "ايصال هيئة الميناء - ops" },
{ id: 485, name: "ايصال VGM - ops" },
{ id: 486, name: "أطلنتس - ops" },
{ id: 487, name: "ايصلات تخصيص - ops" },
{ id: 488, name: "بوصله - ops" },
{ id: 489, name: "بوليصة مشترك - ops" },
{ id: 490, name: "تأشيرة / تجديد تأشيره - ops" },
{ id: 491, name: "تحميل - ops" },
{ id: 492, name: "تخصيص - ops" },
{ id: 493, name: "تعتيق - ops" },
{ id: 494, name: "ساحة النورس - ops" },
{ id: 495, name: "شفت حاويه - ops" },
{ id: 496, name: "شهاده - ops" },
{ id: 497, name: "ساحة الجودة - ops" },
{ id: 498, name: "ضمان الخط - ops" },
{ id: 499, name: "ساحة الوطنية - ops" },
{ id: 500, name: "ضمان جمرك - ops" },
{ id: 501, name: "ضمان خط - ops" },
{ id: 502, name: "عطلة X-ray - ops" },
{ id: 503, name: "فاتورة xray - ops" },
{ id: 504, name: "فك ستوب - ops" },
{ id: 505, name: "كارتات - ops" },
{ id: 506, name: "كارتة محاجر - ops" },
{ id: 507, name: "م انتقالات - ops" },
{ id: 508, name: "م تحميل - ops" },
{ id: 509, name: "م تخليص - ops" },
{ id: 510, name: "ساجة هامبورج - ops" },
{ id: 511, name: "م تصريح - ops" },
{ id: 512, name: "م تعتيق - ops" },
{ id: 513, name: "م زيادة وزن - ops" },
{ id: 514, name: "DPW - ops" },
{ id: 515, name: "م غرامات - ops" },
{ id: 516, name: "م محضر - ops" },
{ id: 517, name: "م PTI - ops" },
{ id: 518, name: "مصروف ميناء - ops" },
{ id: 519, name: "مصروفات اخري - ops" },
{ id: 520, name: "موازين هيئة - ops" },
{ id: 570, name: "أطلنتس" },
{ id: 571, name: "ساحة الجودة" },
{ id: 572, name: "ساحة الوطنية" },
{ id: 573, name: "ساجة هامبورج" },
{ id: 574, name: "DPW" },
{ id: 670, name: "Operating Expense" },
{ id: 673, name: "Delay" },
{ id: 675, name: "Suez Canal" },
{ id: 676, name: "Other Expenses" },
{ id: 677, name: "Wieght / Scales" },
{ id: 678, name: "Dam Port" },
{ id: 679, name: "Cil-EG" },
{ id: 680, name: "Badr-Port" },
{ id: 681, name: "Other Expenses" },
];
DelayItemID = 673;
TriangulationItemID = 35;
GensetItemID = 33;
XRayDelayItemID = 375;
AccessoriesItemID = 684;
CustomFormID = 168;
}
// initialize logger
correlationID = Math.floor(Date.now()).toString();
logger = {
log: function (title, details) {
log.debug(
correlationID + " | " + title.toUpperCase(),
correlationID + " | " + scriptID + " | " + details
);
},
};
}
let schema = [
{
key: "id",
destination: "externalid",
type: "string",
required: true,
},
{
key: "currency",
destination: "currencysymbol",
type: "string",
set: "text",
required: true,
},
{
key: "driver_id",
destination: "entity",
type: "reference",
reference: "vendor",
required: true,
set: "value",
},
{
key: "client_id",
destination: "custbody5",
type: "reference",
reference: "customer",
required: true,
set: "value",
},
{
key: "date",
destination: "trandate",
type: "date",
required: true,
set: "text",
},
{
key: "execution_date",
destination: "custbody_execute_date",
type: "date",
set: "text",
},
{
key: "client_ref",
destination: "custbody_client_ref_po",
type: "string",
set: "text",
},
{
key: "cutoff_date",
destination: "custbodyclient_cut_of_date",
type: "date",
set: "text",
},
{
key: "jobcode_status",
destination: "custbody_last_job_status",
type: "string",
required: true,
set: "text",
},
{
key: "location",
destination: "location",
type: "string",
required: true,
set: "text",
},
{
key: "vertical",
destination: "department",
type: "string",
required: true,
set: "text",
},
{
key: "class",
destination: "class",
type: "string",
set: "text",
},
{
key: "shipper_id",
destination: "custbody26",
type: "reference",
reference: "customer",
set: "value",
},
{
key: "gate_in",
destination: "custbody_gate_in",
},
{
key: "gate_out",
destination: "custbody_gate_out",
required: true,
},
{
key: "destination",
destination: "custbody_destination",
type: "string",
required: true,
set: "text",
},
{
key: "start_date",
destination: "custbody27",
type: "date",
required: true,
set: "text",
},
{
key: "job_group_on_bill",
destination: "custbody_job_gr_bill",
type: "string",
set: "text",
},
{
key: "booking_number",
destination: "custbody_job_code_booking_num",
},
{
key: "has_genset",
destination: "custbody_has_genset",
type: "boolean",
required: true,
set: "text",
},
{
key: "genset_cost_price",
destination: "custbody_genset_cost_price",
type: "number",
// type: "decimal",
set: "text",
},
{
key: "genset_sales_price",
destination: "custbody_genset_sales_price",
type: "number",
// type: "decimal",
set: "text",
},
{
key: "has_triangulation",
destination: "custbody_has_traingulation",
type: "boolean",
required: true,
set: "text",
},
{
key: "triangulation_cost_price",
destination: "custbody_traing_cost_price",
type: "number",
// type: "decimal",
set: "text",
},
{
key: "triangulation_sales_price",
destination: "custbody_traing_sales_price",
type: "number",
// type: "decimal",
set: "text",
},
{
key: "has_delay",
destination: "custbody_has_delay",
type: "boolean",
required: true,
set: "text",
},
{
key: "delay_cost_price",
destination: "custbody_delay_cost_price",
type: "number",
// type: "decimal",
set: "text",
},
{
key: "delay_sales_price",
destination: "custbody_delay_sale_price",
type: "number",
// type: "decimal",
set: "text",
},
{
key: "has_xray_delay",
destination: "custbody_has_x_r_delay",
type: "boolean",
required: true,
set: "text",
},
{
key: "xray_delay_cost_price",
destination: "custbody_x_r_delay_cost",
type: "number",
// type: "decimal",
set: "text",
},
{
key: "xray_delay_sales_price",
destination: "custbody_x_r_delay_price",
type: "number",
// type: "decimal",
set: "text",
},
{
key: "contractor_id",
destination: "custbody_main_contractor",
type: "reference",
reference: "vendor",
set: "value",
},
{
key: "bill_of_lading",
destination: "custbody_bill_of_lading",
type: "string",
required: true,
set: "text",
},
{
key: "truck_number",
destination: "custbody_truck_no",
type: "string",
required: true,
set: "text",
},
{
key: "truck_license_number",
destination: "custbody_truck_license_number",
type: "string",
required: true,
set: "text",
},
{
key: "tail_code",
destination: "custbody_tail_code",
type: "string",
set: "text",
},
{
key: "tail_license_number",
destination: "custbody_tail_license_number",
type: "string",
set: "text",
},
{
key: "cargo",
destination: "custbodybill_cargo",
required: true,
},
{
key: "first_container",
destination: "custbody_first_container",
type: "string",
set: "text",
},
{
key: "second_container",
destination: "custbody_sec_container",
type: "string",
set: "text",
},
{
key: "load_weight",
destination: "custbody_load_weight",
type: "number",
set: "text",
},
];
function post(context) {
init();
try {
logger.log("Request", JSON.stringify(context));
let result = api.validate({
schema: schema,
context: context,
});
// other validations
let rules = [
// class is required only only if department is Marin
{
condition:
context["department"] == "Marin" && !utils.isDefined("class"),
error: "class is required if department is Marin",
},
// booking_number is required only if department is NOT Domestic
{
condition:
context["department"] != "Domestic" &&
!utils.isDefined("booking_number"),
error: "booking_number is required if department is not Domestic",
},
// gate_in is required only if department is Marin
{
condition:
context["department"] == "Marin" &&
!utils.isDefined(context["gate_in"]),
error: "gate_in is required if department is Marin",
},
// genset_cost_price is required only if has_genset is true
{
condition:
context["has_genset"] && !utils.isDefined("genset_cost_price"),
error: "genset_cost_price is required if has_genset is true",
},
// genset_sales_price is required only if has_genset is true
{
condition:
context["has_genset"] && !utils.isDefined("genset_sales_price"),
error: "genset_sales_price is required if has_genset is true",
},
// triangulation_cost_price is required only if has_triangulation is true
{
condition:
context["has_triangulation"] &&
!utils.isDefined("triangulation_cost_price"),
error:
"triangulation_cost_price is required if has_triangulation is true",
},
// triangulation_sales_price is required only if has_triangulation is true
{
condition:
context["has_triangulation"] &&
!utils.isDefined("triangulation_sales_price"),
error:
"triangulation_sales_price is required if has_triangulation is true",
},
// delay_cost_price is required only if has_delay is true
{
condition:
context["has_delay"] && !utils.isDefined("delay_cost_price"),
error: "delay_cost_price is required if has_delay is true",
},
// delay_sales_price is required only if has_delay is true
{
condition:
context["has_delay"] && !utils.isDefined("delay_sales_price"),
error: "delay_sales_price is required if has_delay is true",
},
// xray_delay_cost_price is required only if has_xray_delay is true
{
condition:
context["has_xray_delay"] &&
!utils.isDefined("xray_delay_cost_price"),
error: "xray_delay_cost_price is required if has_xray_delay is true",
},
// xray_delay_sales_price is required only if has_xray_delay is true
{
condition:
context["has_xray_delay"] &&
!utils.isDefined("xray_delay_sales_price"),
error: "xray_delay_sales_price is required if has_xray_delay is true",
},
// first_container is required only if department is Marin
{
condition:
context["department"] == "Marin" &&
!utils.isDefined(context["first_container"]),
error: "first_container is required if department is Marin",
},
// second_container is required only if department is Marin
{
condition:
context["department"] == "Marin" &&
!utils.isDefined(context["second_container"]),
error: "second_container is required if department is Marin",
},
];
if (result.ok) {
for (let i = 0; i < rules.length; i++) {
if (rules[i].condition) {
result.ok = false;
result.error = rules[i].error;
break;
}
}
}
if (result.ok) {
// map (has_x from ture and false to Yes and No)
context["has_genset"] = context["has_genset"] ? "Yes" : "No";
context["has_triangulation"] = context["has_triangulation"]
? "Yes"
: "No";
context["has_delay"] = context["has_delay"] ? "Yes" : "No";
context["has_xray_delay"] = context["has_xray_delay"] ? "Yes" : "No";
let isNew;
let vendorbill;
let vendorbillID;
let srch = search.create({
type: "vendorbill",
filters: [["externalid", "is", context["id"]]],
});
let searchResultCount = srch.runPaged().count;
if (searchResultCount > 0) {
vendorbillID = srch.run().getRange(0, 1)[0].id;
vendorbill = record.load({
type: "vendorbill",
id: vendorbillID,
isDynamic: true,
});
let invoiceID = vendorbill.getValue("custbody_inv_jobgroup_no");
if (utils.isDefined(invoiceID)) {
let json = {
correlation_id: correlationID,
status: {
code: 400,
message: "Jobcode is invoiced. Edits are not allowed.",
},
error: {
name: "Jobcode is invoiced. Edits are not allowed.",
message: "Jobcode is invoiced. Edits are not allowed.",
stack: "",
},
};
logger.log("Response", JSON.stringify(json));
return json;
}
} else {
vendorbill = record.create({
type: "vendorbill",
isDynamic: true,
});
vendorbill.setValue("tranid", context["id"]);
vendorbill.setValue("customform", CustomFormID);
vendorbill.setValue("externalid", context["id"]);
vendorbill.setValue("custbody_externalid", context["id"]);
isNew = true;
}
if (utils.isDefined(context["booking_number"])) {
let bnumID;
srch = search.create({
type: "customrecord_booking_number",
filters: [
["externalid", "is", context["booking_number"].toString()],
"AND",
["isinactive", "is", "F"],
],
});
searchResultCount = srch.runPaged().count;
if (searchResultCount > 0) {
bnumID = srch.run().getRange(0, 1)[0].id;
} else {
bnum = record.create({
type: "customrecord_booking_number",
isDynamic: true,
});
bnum.setValue("name", context["booking_number"].toString());
bnum.setValue("externalid", context["booking_number"].toString());
bnumID = bnum.save();
}
vendorbill.setValue("custbody_job_code_booking_num", bnumID);
}
api.set({
schema: schema,
context: context,
record: vendorbill,
});
// handle gate in
srch = search.create({
type: "customrecord_gate_in_list_ed",
filters: [
["name", "is", context["gate_in"]],
"AND",
["isinactive", "is", "F"],
],
});
if (srch.runPaged().count == 0) {
let rec = record.create({
type: "customrecord_gate_in_list_ed",
});
rec.setValue("name", context["gate_in"]);
rec.save();
}
vendorbill.setValue(
"custbody_gate_in",
srch.run().getRange(0, 1)[0].id
);
// handle gate out
srch = search.create({
type: "customrecord_gate_out_list_ed",
filters: [
["name", "is", context["gate_out"]],
"AND",
["isinactive", "is", "F"],
],
});
if (srch.runPaged().count == 0) {
let rec = record.create({
type: "customrecord_gate_out_list_ed",
});
rec.setValue("name", context["gate_out"]);
rec.save();
}
vendorbill.setValue(
"custbody_gate_out",
srch.run().getRange(0, 1)[0].id
);
// handle cargo
srch = search.create({
type: "customrecord_kind_of_cargo",
filters: [
["name", "is", context["cargo"]],
"AND",
["isinactive", "is", "F"],
],
});
if (srch.runPaged().count == 0) {
let rec = record.create({
type: "customrecord_kind_of_cargo",
});
rec.setValue("name", context["cargo"]);
rec.save();
}
vendorbill.setValue(
"custbodybill_cargo",
srch.run().getRange(0, 1)[0].id
);
let currID;
if (utils.isDefined(context["currency"])) {
srch = search.create({
type: "currency",
filters: [
["symbol", "is", context["currency"]],
"AND",
["isinactive", "is", "F"],
],
});
searchResultCount = srch.runPaged().count;
if (searchResultCount > 0) {
currID = srch.run().getRange(0, 1)[0].id;
vendorbill.setValue("currency", currID);
}
}
let clientID;
if (utils.isDefined(context["client_id"])) {
srch = search.create({
type: "customer",
filters: [
["externalid", "is", context["client_id"]],
"AND",
["isinactive", "is", "F"],
],
});
searchResultCount = srch.runPaged().count;
if (searchResultCount > 0) {
clientID = srch.run().getRange(0, 1)[0].id;
}
}
for (let i = 0; i < Items.length; i++) {
if (isNew) {
vendorbill.selectNewLine({
sublistId: "item",
});
} else {
vendorbill.selectLine({
sublistId: "item",
line: i,
});
}
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "item",
value: Items[i].id,
});
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "quantity",
value: 1,
});
if (i == 0) {
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "rate",
value: Number(context["driver_cost"]),
});
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "custcol_customer_rate",
value: Number(context["customer_price"]),
});
} else if (isNew) {
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "rate",
value: 0,
});
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "custcol_customer_rate",
value: 0,
});
}
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "customer",
value: clientID,
});
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "isbillable",
value:
Items[i].name.toLowerCase().indexOf("ops") > -1 ||
Items[i].id == NolonItemID
? false
: true,
});
vendorbill.setCurrentSublistText({
sublistId: "item",
fieldId: "location",
text: context["location"],
});
vendorbill.setCurrentSublistText({
sublistId: "item",
fieldId: "department",
text: context["department"],
});
vendorbill.commitLine({ sublistId: "item" });
}
let additionalCharges = [
{
itemID: DelayItemID,
condition: context["has_delay"],
rate: context["delay_cost_price"],
custrate: context["delay_sales_price"],
},
{
itemID: XRayDelayItemID,
condition: context["has_xray_delay"],
rate: context["xray_delay_cost_price"],
custrate: context["xray_delay_sales_price"],
},
{
itemID: TriangulationItemID,
condition: context["has_triangulation"],
rate: context["triangulation_cost_price"],
custrate: context["triangulation_sales_price"],
},
{
itemID: GensetItemID,
condition: context["has_genset"],
rate: context["genset_cost_price"],
custrate: context["genset_sales_price"],
},
];
let count = vendorbill.getLineCount({
sublistId: "item",
});
for (let i = 0; i < additionalCharges.length; i++) {
if (additionalCharges[i].condition == "Yes") {
// select the correct line
for (let j = 0; j < count; j++) {
vendorbill.selectLine({
sublistId: "item",
line: j,
});
let itemID = vendorbill.getCurrentSublistValue({
sublistId: "item",
fieldId: "item",
});
if (itemID == additionalCharges[i].itemID) {
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "rate",
value: Number(additionalCharges[i].rate),
});
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "custcol_customer_rate",
value: Number(additionalCharges[i].custrate),
});
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "isbillable",
value: false,
});
vendorbill.commitLine({ sublistId: "item" });
}
}
} else {
for (let j = 0; j < count; j++) {
vendorbill.selectLine({
sublistId: "item",
line: j,
});
let itemID = vendorbill.getCurrentSublistValue({
sublistId: "item",
fieldId: "item",
});
if (itemID == additionalCharges[i].itemID) {
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "rate",
value: 0,
});
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "custcol_customer_rate",
value: 0,
});
vendorbill.commitLine({ sublistId: "item" });
}
}
}
}
// handle accessories
if (
utils.isDefined(context["accessories"]) &&
context["accessories"].length > 0
) {
for (let i = 0; i < context["accessories"].length; i++) {
let exists = false;
// edit it if it exists
for (let j = 0; j < count; j++) {
vendorbill.selectLine({
sublistId: "item",
line: j,
});
let itemID = vendorbill.getCurrentSublistValue({
sublistId: "item",
fieldId: "item",
});
let description = vendorbill.getCurrentSublistValue({
sublistId: "item",
fieldId: "description",
});
if (
itemID == AccessoriesItemID &&
context["accessories"][i].name == description
) {
exists = true;
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "rate",
value: Number(context["accessories"][i].cost),
});
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "quantity",
value: Number(context["accessories"][i].qty),
});
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "custcol_customer_rate",
value: Number(context["accessories"][i].price),
});
vendorbill.commitLine({ sublistId: "item" });
}
}
// add it if it doesn't exist
if (!exists) {
vendorbill.selectNewLine({
sublistId: "item",
});
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "item",
value: AccessoriesItemID,
});
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "description",
value: context["accessories"][i].name,
});
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "rate",
value: Number(context["accessories"][i].cost),
});
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "quantity",
value: Number(context["accessories"][i].qty),
});
vendorbill.setCurrentSublistValue({
sublistId: "item",
fieldId: "custcol_customer_rate",
value: Number(context["accessories"][i].price),
});
vendorbill.commitLine({ sublistId: "item" });
}
}
}
// remove accessories that are not in the request
for (let i = 0; i < count; i++) {
vendorbill.selectLine({
sublistId: "item",
line: i,
});
let itemID = vendorbill.getCurrentSublistValue({
sublistId: "item",
fieldId: "item",
});
let description = vendorbill.getCurrentSublistValue({
sublistId: "item",
fieldId: "description",
});
if (itemID == AccessoriesItemID) {
let exists = false;
for (let j = 0; j < context["accessories"].length; j++) {
if (context["accessories"][j].name == description) {
exists = true;
}
}
if (!exists) {
vendorbill.removeLine({
sublistId: "item",
line: i,
});
}
}
}
vendorbillID = vendorbill.save({
enableSourcing: true,
ignoreMandatoryFields: true,
});
vendorbill = record.load({
type: "vendorbill",
id: vendorbillID,
isDynamic: true,
});
let json = {
correlation_id: correlationID,
vendorbill_number: context["id"],
vendorbill_internal_id: vendorbillID.toString(),
status: {
code: 200,
message: "Succeeded!",
},
};
logger.log("Response", JSON.stringify(json));
return json;
} else {
let json = {
correlation_id: correlationID,
status: {
code: 400,
message: result.error,
},
error: {
name: result.error,
message: result.error,
stack: "",
},
};
logger.log("Response", JSON.stringify(json));
return json;
}
} catch (err) {
let json = {
correlation_id: correlationID,
status: {
code: 500,
message: err.message,
},
error: {
name: err.name,
message: err.message,
stack: err.stack,
},
};
logger.log("Response", JSON.stringify(json));
return json;
}
}
return {
post: post,
};
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment