You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Javascript库存分配算法优化需求:销售与采购订单匹配改进

库存分配算法优化需求

问题背景

我已实现一个基础的库存分配功能,用于匹配客户销售订单与供应商采购订单,确定销售订单的可满足日期,但当前实现无法覆盖所有场景。

数据示例

销售订单数组

属性说明:

  • created:销售订单创建日期
  • quantity:客户需求数量
const sales= [{
  "id": "S1",
  "created": "2020-01-02",
  "quantity": 6
}, {
  "id": "S2",  
  "created": "2020-11-05",
  "quantity": 2  
}, {
  "id": "S3",  
  "created": "2019-12-04",
  "quantity": 3  
}, {
  "id": "S4",  
  "created": "2020-01-20",
  "quantity": 2  
}, {
  "id": "S5",  
  "created": "2019-12-15",
  "quantity": 9  
}];

采购订单数组

属性说明:

  • receiving:预计到货日期
  • quantity:到货数量
const purchases= [{
  "id": "P1",  
  "receiving": "2020-01-04",
  "quantity": 4
}, {
  "id": "P2",  
  "receiving": "2020-01-05",
  "quantity": 3  
}, {
  "id": "P3",  
  "receiving": "2020-02-01",
  "quantity": 5  
}, {
  "id": "P4",  
  "receiving": "2020-03-05",
  "quantity": 1  
}, {
  "id": "P5",  
  "receiving": "2020-02-20",
  "quantity": 7
}];

当前实现代码

以下是我用JavaScript实现的函数,返回每个销售订单的可满足日期,但存在场景覆盖不全的问题:

function allocate(salesOrders, purchaseOrders) {
    //ordering sales and purchases by date
    const orderedSales = salesOrders.sort((a, b) => a.created.localeCompare(b.created));
    const orderedPurchases = purchaseOrders.sort((a, b) => a.receiving.localeCompare(b.receiving));

    console.log(orderedSales)
    console.log(orderedPurchases)
    let stock = 0;
    const result = [];
    purchaseIndex = 0;
    orderedSales.forEach((sale, index) => {
        const order = orderedPurchases[purchaseIndex];
        if (order) {
            console.log("Processing order", sale.id)
            console.log(`Leftover stock = ${stock}`)
            stock += order.quantity
            console.log(`new stock = ${stock}`)
            stock = stock - sale.quantity;
            console.log(`Sustracting = ${sale.quantity}`)
            console.log(`Remaining = ${stock}`)
            while (stock < 0) {
                purchaseIndex++
                console.log(`Demand NOT satified, moving to next purchase order with index ${purchaseIndex}`)

                stock += order.quantity
                console.log(`Current stock = ${stock}`)
                increaseOrder = false;
            }

            //demand has been satisfied
            console.log(`Demand for ${sale.id} was satified with purchase ${order.id}, time is ${order.receiving}, moving to next purchase order`)
            result.push({
                id: sale.id,
                availabilityDate: order.receiving
            })
            purchaseIndex++

            console.log("Next sale ++++++++")
            console.log(" ++++++++")
        }
    });

    console.log(result);
}

allocate(salesOrders, purchaseOrders)

优化诉求

作为算法开发新手,希望获取该问题的优化方向,比如适用的数据结构或更优的实现方式,以覆盖所有场景。

内容的提问来源于stack exchange,提问作者Marianx

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.18 19:15:43