Oh...I know what the issue may be. The 'merchantOrders' list is a zero-based indexed list. I started the indexes at '1', so your subOrderNo will start at '1'.
The following code should resolve this. I adjusted the code to still start the subOrderNo at '1'.
MATCH (cart:Cart{id: 100})
CALL {
MERGE (id:UniqueId {name:"orderNo"})
ON CREATE SET id.id = 1
ON MATCH SET id.id = id.id+1
RETURN id.id as orderNo
}
MATCH (cart)-[contains:CONTAINS]->(product:Product)-[sold:IS_SOLD_BY]->(merchant:Merchant)
WITH orderNo, merchant, collect({c: contains, p: product}) as merchantProducts, count(product) as noOfItems, sum(product.price*contains.quantity) as orderTotalPrice
WITH orderNo, collect({m: merchant, p: merchantProducts, n: noOfItems, t: orderTotalPrice}) as merchantOrders
WITH orderNo, merchantOrders, range(0, size(merchantOrders)-1) as indexes
UNWIND indexes as index
WITH orderNo, index + 1 as subOrderNo, merchantOrders[index] as merchantOrder
WITH orderNo, subOrderNo, merchantOrder.m as merchant, merchantOrder.p as merchantProducts, merchantOrder.n as noOfItems, merchantOrder.t as orderTotalPrice
CREATE (order:Order)-[b:BELONGS_TO]->(merchant)
SET order.number = orderNo,
order.subOrder = subOrderNo,
order.itemsAmount = orderTotalPrice,
order.itemsNumber = noOfItems,
order.transportAmount = noOfItems + 9,
order.totalAmount = orderTotalPrice + noOfItems + 9,
order.uuid = apoc.create.uuid(),
order.created_at = dateTime(),
order.status = "created"
WITH order, merchantProducts
UNWIND merchantProducts as merchantProduct
WITH order, merchantProduct.c as contains, merchantProduct.p as product
MERGE (order)-[c:CONTAINS]->(product)
set c=contains,
c.price = product.price
RETURN distinct(order) as order