有一个在 Zoho Books/Inventory 中运行的 deluge 脚本,它根据客户/项目模块和创建者表中的自定义字段计算一些费用。
脚本按预期工作(虽然比希望的慢一点)但是我正在努力克服两个最后的障碍。
问题 1) 脚本遍历销售订单上的每个行项目以检查它是否在项目模块的相关自定义字段中有一个条目然后它有效地刷新销售订单与行项目和一个新的行项目“回收费”与计算的费率。这很好,但是有一个非常具体的利基项目需要在每个销售订单上更新描述。在这种情况下,脚本将运行它并将用户更新的描述替换为该项目的默认描述。
问题 2) 脚本按预期执行工作的计算,但是,如果销售订单上存在行项目“回收费用”,则没有任何反应。如果用户删除该行并保存销售订单,它将按预期更新。
修复 1) 我尝试从销售订单中提取描述并将其添加到“pdlist”,虽然这确实保留了用户编辑的描述,但它从销售订单中删除了 item_id,因此销售订单上的项目只是描述.
修复 2) 我尝试将变量分配给 false,然后检查行项目以查看是否存在“回收费”,如果存在则更新费率。这解决了添加多个“回收费”行项目但仍然不会更新费率(如果存在)的问题。
我在下面截断了脚本,应该是相关部分。
// Fetch SO and customer records
resp = zoho.inventory.getRecordsByID("salesorders",organizationID,salesorderID,"inventory1");
//info resp;
salesorder = resp.get("salesorder");
if(true)
{
info salesorder;
// return;
}
...
//GETTING CUSTOMER/SO SHIPPING INFO HERE//
...
for each custom_field in customer_custom_fields
{
if(custom_field.get("label") == "Remitter ID")
{
cf_remitter_id = custom_field.get("value");
}
}
// Set Recycling Fee to 0
Fee = 0;
// Check if Remitter ID is null
if(cf_remitter_id == "")
{
line_items = salesorder.get("line_items");
pdlist = List();
for each ele1 in line_items
{
item_id = ele1.get("item_id");
itemresp = zoho.inventory.getRecordsByID("items",organizationID,item_id,"inventory1");
//info itemresp;
item = itemresp.get("item");
custom_fields1 = item.get("custom_fields");
//info custom_fields1;
cf_recycling_category = "";
flag = false;
pro2 = Map();
pro2.put("item_id",ele1.get("item_id"));
//pro2.put("description",ele1.get("description"));
pro2.put("quantity",ele1.get("quantity"));
pro2.put("rate",ele1.get("rate"));
//pro.put("total",item_total.toDecimal());
//pro.put("net_total",item_total.toDecimal());
pdlist.add(pro2);
info pro2;
for each custom_field1 in custom_fields1
{
...
//LOOP TO CHECK FEES GOES HERE//
...
}
found = false;
for each ele1 in line_items
{
if(ele1.get("item_id") == "2015797000015488030")
{
ele1.put("rate",Fee.toDecimal());
ele1.put("quantity",1);
found = true;
break;
}
}
// Add new line item if it does not exist
if(found = false)
{
pro = Map();
pro.put("item_id",2015797000015488030);
pro.put("rate",Fee.toDecimal());
pro.put("quantity",1);
// pro.put("description");
pdlist.add(pro);
info pro;
}
mp = Map();
mp.put("line_items",pdlist);
up = zoho.inventory.updateRecord("salesorders",organizationID,salesorderID,mp,"inventory1");
info up;
}
2023-01-13 编辑:
我已经修改了行项目第一次循环的功能。 基本上,我们保留行项目的所有属性/键,只删除
line_item_id
.
...
if(cf_remitter_id == "")
{
line_items = salesorder.get("line_items");
pdlist = List();
for each ele1 in line_items
{
item_id = ele1.get("item_id");
itemresp = zoho.inventory.getRecordsByID("items",organizationID,item_id,"inventory1");
//info itemresp;
item = itemresp.get("item");
custom_fields1 = item.get("custom_fields");
//info custom_fields1;
cf_recycling_category = "";
flag = false;
pro2 = Map();
/*
// Instead of manually setting up each key of a line item
// We will just copy from ele1 to pro2 variable
// Then remove the line_item_id, as currently we are not able to override line items with that
pro2.put("item_id",ele1.get("item_id"));
//pro2.put("description",ele1.get("description"));
pro2.put("quantity",ele1.get("quantity"));
pro2.put("rate",ele1.get("rate"));
//pro.put("total",item_total.toDecimal());
//pro.put("net_total",item_total.toDecimal());
*/
pro2 = ele1;
pro2.remove("line_item_id"); // remove the key
pdlist.add(pro2);
info pro2;
for each custom_field1 in custom_fields1
{
...
//LOOP TO CHECK FEES GOES HERE//
...
}
found = false;
for each ele1 in line_items
{
...
我不太清楚问题#1。为什么您仍然需要每个订单项的
item_id
?是否有任何信息需要保留但在使用该功能时丢失了?
对于问题 #2,您需要先删除费率,然后再添加它。
....
}
found = false;
for each ele1 in line_items
{
if(ele1.get("item_id") == "2015797000015488030")
{
// remove rate
ele1.remove("rate");
// then add again
ele1.put("rate",Fee.toDecimal());
ele1.put("quantity",1);
found = true;
break;
}
}
// Add new line item if it does not exist
if(found = false)
{
pro = Map()
....
原来的问题是,当传递“描述”值时,它破坏了项目链接。
为了在不覆盖描述/仓库的情况下工作,必须传递“warehouse_id”和“description”字段并包含“line_item_id”和“item_id”。
最后看起来像这样:
products = Map();
products.put("item_id",salesorderitem.get("item_id"));
products.put("line_item_id",salesorderitem.get("line_item_id"));
products.put("quantity",salesorderitem.get("quantity"));
products.put("rate",salesorderitem.get("rate"));
products.put("description",salesorderitem.get("description"));
products.put("warehouse_id",salesorderitem.get("warehouse_id"));