The script checks whether fulfilment center Fulfilment Center Name has sufficient stock for all order items, if yes then the order is automatically allocated to the fulfilment center
Complete code (intialize and static method, copy bits inside Initialize method and IsOrderFolderExists static function)
namespace linnworks.finaware.CommonData.Objects // leave untouched
{ // leave untouched
public class ScriptMacroClass : linnworks.scripting.core.IOrderScript // leave untouched
{ // leave untouched
public void Initialize(linnworks.finaware.CommonData.Objects.Order order,linnworks.scripting.core.Debugger debug) // leave untouched
{ // leave untouched
/*
* This script will get all stock items
*/
System.Collections.Generic.List<System.Guid> list = new System.Collections.Generic.List<System.Guid>(); // declare a list of unique ids
bool SufficientStock = true;// bool to flag if we have sufficient stock
string FulfilmentCenterName = "Fulfilment Center Name"; // the name of the fulfilment center
// lets collect all order items in to a list, so that we can bulk check the stock level for a specific location
foreach(OrderItem i in order.OrderItems){ // itterate through all order items
if (i.pkStockItemId != System.Guid.Empty){ // is order item linked?
list.Add(i.pkStockItemId); // if yes add to the list
}
else if(!i.IsService && i.pkStockItemId == System.Guid.Empty) { // if not linked
debug.AddEntry(i.ItemNumber + " is unlinked"); // add line to debug
SufficientStock = false; // no sufficient stock - we have an unlinked item in the order
}
}
// we can get stock levels for a list of items in bulk using ListStockAvailablityInLocation method
System.Collections.Generic.List<linnworks.finaware.CommonData.Inventory.StockItemAvailability> availability = linnworks.finaware.CommonData.Inventory.ListStockAvailablityInLocation(list, FulfilmentCenterName, order.GetConnectionString);
// iterate through the result set of ListStockAvailablityInLocation
foreach(linnworks.finaware.CommonData.Inventory.StockItemAvailability aItem in availability){
debug.AddEntry(aItem.SKU + "=" + aItem.Available.ToString());
if (aItem.Available <= 0){ // if we have insufficent stock for one of the items
SufficientStock = false; // set no Sufficient stock
}
}
if (SufficientStock){ //if we have sufficient stock
order.SetFulfilmentCenter(FulfilmentCenterName); // allocate order to a fulfilment center
order.SetUnassignFromFolder("Waiting Fulfilment Center Stock"); // remove order from folder "Waiting Fulfilment Center Stock" just in case it was assigned to a folder before
debug.AddEntry("Order allocated to " + FulfilmentCenterName); // output something in debug
order.Save(0); // SAVE ORDER
}
else{ // if no sufficient stock
if (!IsOrderFolderExists(order.OrderFolder, "Waiting Fulfilment Center Stock")){//check if the order is allocated to a folder "Waiting Fulfilment Center Stock"
OrderFolder folder = new OrderFolder("Waiting Fulfilment Center Stock"); // if not, create new folder object
order.OrderFolder.Add(folder); // assign to an order
debug.AddEntry("Allocated to folder"); // debug line
order.Save(0); // SAVE ORDER
}
debug.DoNotLogScriptRun = true; // here we need to tell Linnworks not to treat this order as SCRIPT_RUN and re-execute this script next time we sync for this order
}
} // leave untouched
/// <summary>
/// static method to check if an order is assigned to a folder,
/// here we simply itterate through all folders and check if folderName = FolderName
/// </summary>
static bool IsOrderFolderExists(System.Collections.Generic.List<OrderFolder> list,string folderName){
foreach(OrderFolder f in list)
if(f.FolderName == folderName){return true;}
return false;
}
} // leave untouched
} // leave untouched