diff --git a/addons/point_of_sale/static/src/js/Screens/TicketScreen/TicketScreen.js b/addons/point_of_sale/static/src/js/Screens/TicketScreen/TicketScreen.js index 8ac79ec6e8a..8db5625f5e7 100644 --- a/addons/point_of_sale/static/src/js/Screens/TicketScreen/TicketScreen.js +++ b/addons/point_of_sale/static/src/js/Screens/TicketScreen/TicketScreen.js @@ -193,18 +193,7 @@ odoo.define('point_of_sale.TicketScreen', function (require) { const partner = order.get_partner(); - // Select the lines from toRefundLines (can come from different orders) - // such that: - // - the quantity to refund is not zero - // - if there is partner in the selected paid order, select the items - // with the same orderPartnerId - // - it is not yet linked to an active order (no destinationOrderUid) - const allToRefundDetails = Object.values(this.env.pos.toRefundLines).filter( - ({ qty, orderline, destinationOrderUid }) => - !this.env.pos.isProductQtyZero(qty) && - (partner ? orderline.orderPartnerId == partner.id : true) && - !destinationOrderUid - ); + const allToRefundDetails = this._getRefundableDetails(partner); if (allToRefundDetails.length == 0) { this._state.ui.highlightHeaderNote = !this._state.ui.highlightHeaderNote; return; @@ -220,16 +209,9 @@ odoo.define('point_of_sale.TicketScreen', function (require) { // Add orderline for each toRefundDetail to the destinationOrder. for (const refundDetail of allToRefundDetails) { - const { qty, orderline } = refundDetail; - await destinationOrder.add_product(this.env.pos.db.get_product_by_id(orderline.productId), { - quantity: -qty, - price: orderline.price, - extras: { price_manually_set: true }, - merge: false, - refunded_orderline_id: orderline.id, - tax_ids: orderline.tax_ids, - discount: orderline.discount, - }); + const product = this.env.pos.db.get_product_by_id(refundDetail.orderline.productId); + const options = this._prepareRefundOrderlineOptions(refundDetail); + await destinationOrder.add_product(product, options); refundDetail.destinationOrderUid = destinationOrder.uid; } @@ -418,6 +400,42 @@ odoo.define('point_of_sale.TicketScreen', function (require) { return newToRefundDetail; } } + /** + * Select the lines from toRefundLines, as they can come from different orders. + * Returns only details that: + * - The quantity to refund is not zero + * - Filtered by partner (optional) + * - It's not yet linked to an active order (no destinationOrderUid) + * + * @param {Object} partner (optional) + * @returns {Array} refundableDetails + */ + _getRefundableDetails(partner) { + return Object.values(this.env.pos.toRefundLines).filter( + ({ qty, orderline, destinationOrderUid }) => + !this.env.pos.isProductQtyZero(qty) && + (partner ? orderline.orderPartnerId == partner.id : true) && + !destinationOrderUid + ); + } + /** + * Prepares the options to add a refund orderline. + * + * @param {Object} toRefundDetail + * @returns {Object} + */ + _prepareRefundOrderlineOptions(toRefundDetail) { + const { qty, orderline } = toRefundDetail; + return { + quantity: -qty, + price: orderline.price, + extras: { price_manually_set: true }, + merge: false, + refunded_orderline_id: orderline.id, + tax_ids: orderline.tax_ids, + discount: orderline.discount, + } + } _setOrder(order) { this.env.pos.set_order(order); this.close();