tplus-pos
Version:
pos module
161 lines (143 loc) • 7.19 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ReturnCouponProcessor = undefined;
var _classCallCheck2 = require('babel-runtime/helpers/classCallCheck');
var _classCallCheck3 = _interopRequireDefault(_classCallCheck2);
var _createClass2 = require('babel-runtime/helpers/createClass');
var _createClass3 = _interopRequireDefault(_createClass2);
var _Enum = require('../../../commom/Enum');
var _Format = require('../../../commom/Format');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
*退代金券算法
* @export
* @class ReturnCouponProcessor
* @auther tuzha
*/
var ReturnCouponProcessor = exports.ReturnCouponProcessor = function () {
function ReturnCouponProcessor() {
(0, _classCallCheck3.default)(this, ReturnCouponProcessor);
}
(0, _createClass3.default)(ReturnCouponProcessor, null, [{
key: 'execute',
value: function execute(dto) {
//没有代金券可以退回或者退款小于任何的优惠券值
if (!dto.RetailCouponDetails || dto.RetailCouponDetails.length == 0 || dto.RetailCouponDetails.findIndex(function (it) {
return it.ReductionAmount <= -dto.TotalTaxAmount;
}) < 0) {
var findIndex = dto.RetailPaymentDetails.findIndex(function (it) {
return it.SettleStyle.Code == _Enum.SettleCode.Coupon;
});
dto.RetailPaymentDetails.splice(findIndex, 1);
return;
}
//存在代金券,且可以分配
var newCouponDetails = this.getCouponDetails(dto.RetailCouponDetails, -dto.TotalTaxAmount);
if (!newCouponDetails || newCouponDetails.length == 0) {
var _findIndex = dto.RetailPaymentDetails.findIndex(function (it) {
return it.SettleStyle.Code == _Enum.SettleCode.Coupon;
});
dto.RetailPaymentDetails.splice(_findIndex, 1);
return;
}
//开始赋值
dto.RetailCouponDetails = [];
newCouponDetails.forEach(function (item) {
item.ReductionAmount = -item.ReductionAmount;
dto.RetailCouponDetails.push(item);
});
//排序处理
dto.RetailCouponDetails = dto.RetailCouponDetails.sort(function (a, b) {
if (b.ReductionAmount == a.ReductionAmount) {
if (b.CouponTitle > a.CouponTitle) {
return 1;
} else if (b.CouponTitle < a.CouponTitle) {
return -1;
} else {
return 0;
}
} else {
return Math.abs(b.ReductionAmount) - Math.abs(a.ReductionAmount);
}
});
var totalCouponAmount = dto.RetailCouponDetails.Sum("ReductionAmount");
//对代金券赋值
var paymentDetailDto = dto.RetailPaymentDetails.find(function (it) {
return it.SettleStyle.Code == _Enum.SettleCode.Coupon;
});
var _format = new _Format.Format();
paymentDetailDto.Amount = totalCouponAmount;
paymentDetailDto.OrigAmount = _format.FormatAmount(Math.Multiply(paymentDetailDto.Amount, paymentDetailDto.ExchangeRate));
JSON.setCellValue(dto, "TotalCanReturnAmount", Math.Minus(-dto.TotalTaxAmount, -totalCouponAmount));
}
//回溯查找代金券明细
}, {
key: 'findCouponDetails',
value: function findCouponDetails(retailCouponDetails, begin, targetAmount, newCouponDetails, newCouponDetailsList) {
if (targetAmount == 0 || begin >= retailCouponDetails.length) {
var info = {};
info.TargetAmount = targetAmount;
info.CouponDetails = JSON.Clone(newCouponDetails);
newCouponDetailsList.push(info);
return true;
}
if (targetAmount < 0) {
return true;
}
for (var index = begin; index < retailCouponDetails.length; index++) {
// 为了避免将负数传递到下一个分支,这里剪枝
if (targetAmount - retailCouponDetails[index].ReductionAmount < 0 && newCouponDetails.length > 0) {
var _info = {};
_info.TargetAmount = targetAmount;
_info.CouponDetails = JSON.Clone(newCouponDetails);
newCouponDetailsList.push(_info);
continue;
}
if (targetAmount - retailCouponDetails[index].ReductionAmount < 0 && newCouponDetails.length == 0) {
continue;
}
// 这一步之所以能够生效,其前提是数组一定是排好序的,这样才能保证:
// 在递归调用的统一深度(层)中,一个元素只使用一次。
// 这一步剪枝操作基于 retailCouponDetails 数组是排序数组的前提下,相同部分剪枝
if (index > begin && retailCouponDetails[index].ReductionAmount == retailCouponDetails[index - 1].ReductionAmount) {
continue;
}
newCouponDetails.push(retailCouponDetails[index]);
// 【关键】因为元素不可以重复使用,这里递归传递下去的是 index + 1 而不是 index
var isFind = this.findCouponDetails(retailCouponDetails, index + 1, targetAmount - retailCouponDetails[index].ReductionAmount, newCouponDetails, newCouponDetailsList);
if (isFind) {
break;
}
newCouponDetails.pop();
}
}
//获取代金券明细
}, {
key: 'getCouponDetails',
value: function getCouponDetails(retailCouponDetails, targetAmount) {
var totalCouponAmount = retailCouponDetails.Sum("ReductionAmount");
//如果代金券的总金额小于退款总金额,则全部分配
if (targetAmount > totalCouponAmount) {
return retailCouponDetails;
}
var newCouponDetails = [];
var newCouponDetailsList = [];
//按照代金券金额倒序
retailCouponDetails = retailCouponDetails.sort(function (a, b) {
return b.ReductionAmount - a.ReductionAmount;
});
this.findCouponDetails(retailCouponDetails, 0, targetAmount, newCouponDetails, newCouponDetailsList);
if (newCouponDetailsList.length > 0) {
newCouponDetailsList = newCouponDetailsList.sort(function (a, b) {
return Math.abs(a.TargetAmount) - Math.abs(b.TargetAmount);
});
newCouponDetails = newCouponDetailsList[0].CouponDetails;
}
return newCouponDetails;
}
}]);
return ReturnCouponProcessor;
}();
//# sourceMappingURL=ReturnCouponProcessor.js.map