| 1 |
import { patch } from "@web/core/utils/patch"; |
| 2 |
import { ForecastedDetails } from "@stock/stock_forecasted/forecasted_details"; |
| 3 |
|
| 4 |
patch(ForecastedDetails.prototype, { |
| 5 |
_freeStockCondition(line){ |
| 6 |
return super._freeStockCondition(line) && line?.removal_date != -1; |
| 7 |
}, |
| 8 |
_prepareLines() { |
| 9 |
if (this.props.docs.use_expiration_date) { |
| 10 |
this.props.docs.lines.sort((a, b) => (a.removal_date || 0) - (b.removal_date || 0)); |
| 11 |
} |
| 12 |
super._prepareLines(); |
| 13 |
}, |
| 14 |
_prepareData(){ |
| 15 |
// Whenever there's a Free Stock line without an expiration date, |
| 16 |
// remove the quantities "to remove" from this line to improve readibility |
| 17 |
super._prepareData(); |
| 18 |
for (const productId of this.productIds){ |
| 19 |
if (!(productId in this.FreeStockLinesPerProduct)){ |
| 20 |
continue; |
| 21 |
} |
| 22 |
const lines = this.FreeStockLinesPerProduct[productId] |
| 23 |
let noRemovalDateLine = lines.find(line => !line.removal_date); |
| 24 |
let withRemovalDateLines = lines.filter(line => line.removal_date); |
| 25 |
if (noRemovalDateLine && withRemovalDateLines.length) { |
| 26 |
const sumQty = withRemovalDateLines.reduce((sum, line) => sum + (line.quantity || 0), 0); |
| 27 |
noRemovalDateLine.quantity = noRemovalDateLine.quantity - sumQty; |
| 28 |
} |
| 29 |
if (noRemovalDateLine && noRemovalDateLine?.quantity === 0 && withRemovalDateLines?.length){ |
| 30 |
const removeIndex = this.lines.indexOf(noRemovalDateLine); |
| 31 |
this.lines.splice(removeIndex,1); |
| 32 |
} |
| 33 |
} |
| 34 |
}, |
| 35 |
_sameLineRule(line, nextLine){ |
| 36 |
const res = super._sameLineRule(line, nextLine); |
| 37 |
const FreeStock = this.FreeStockLinesPerProduct[line.product.id] || []; |
| 38 |
return res || (FreeStock.includes(line) && FreeStock.includes(nextLine)); |
| 39 |
} |
| 40 |
}); |