import { Decimal } from 'decimal.js';

export const countMethods = (
	arr,
	index,
	key,
	exchangeRate,
	unitpriceAccuracy,
	amountAccuracy
) => {
	// 销售订单计算
	if (
		unitpriceAccuracy === '' ||
		unitpriceAccuracy === undefined ||
		unitpriceAccuracy === null
	) {
		unitpriceAccuracy = 6;
	}
	if (
		amountAccuracy === '' ||
		amountAccuracy === undefined ||
		amountAccuracy === null
	) {
		amountAccuracy = 2;
	}
	if (
		exchangeRate === '' ||
		exchangeRate === undefined ||
		exchangeRate === null ||
		exchangeRate === '0' ||
		exchangeRate === 0
	) {
		exchangeRate = '1';
	}
	if (
		arr[index].price === '' ||
		arr[index].price === undefined ||
		arr[index].price === null
	) {
		arr[index].price = '0';
	}
	if (
		arr[index].taxRate === '' ||
		arr[index].taxRate === undefined ||
		arr[index].taxRate === null
	) {
		arr[index].taxRate = '0';
	}
	if (
		arr[index].salesNumber === '' ||
		arr[index].salesNumber === undefined ||
		arr[index].salesNumber === null
	) {
		arr[index].salesNumber = '0';
	}
	if (
		arr[index].disCount === '' ||
		arr[index].disCount === undefined ||
		arr[index].disCount === null
	) {
		arr[index].disCount = '0';
	}
	if (
		arr[index].taxPrice === '' ||
		arr[index].taxPrice === undefined ||
		arr[index].taxPrice === null
	) {
		arr[index].taxPrice = '0';
	}
	if (
		arr[index].discountRate === '' ||
		arr[index].discountRate === undefined ||
		arr[index].discountRate === null
	) {
		arr[index].discountRate = '0';
	}
	if (
		arr[index].taxaMount === '' ||
		arr[index].taxaMount === undefined ||
		arr[index].taxaMount === null
	) {
		arr[index].taxaMount = '0';
	}
	if (
		arr[index].amount === '' ||
		arr[index].amount === undefined ||
		arr[index].amount === null
	) {
		arr[index].amount = '0';
	}
	if (
		arr[index].allAmountLc === '' ||
		arr[index].allAmountLc === undefined ||
		arr[index].allAmountLc === null
	) {
		arr[index].allAmountLc = '0';
	}
	if (
		arr[index].allAmount === '' ||
		arr[index].allAmount === undefined ||
		arr[index].allAmount === null
	) {
		arr[index].allAmount = '0';
	}
	if (arr[index].discountRate > 100) {
		arr[index].discountRate = 100;
	}
	let templateObject = {
		details: [],
	};
	if (index != undefined || index != '' || index != null) {
		if (
			key == 'taxRate' ||
			key == 'salesNumber' ||
			key == 'disCount' ||
			key === 'taxPrice' ||
			key === 'discountRate'
		) {
			// 定义税率 value
			if (arr[index].taxPrice == 0 || arr[index].taxPrice) {
				// 计算单价 税额 判断 含税单价 是否为空
				if (!isNaN(parseFloat(arr[index].taxPrice))) {
					arr[index].price = (
						parseFloat(arr[index].taxPrice).toFixed(
							unitpriceAccuracy
						) /
						(1 +
							parseFloat(arr[index].taxRate).toFixed(
								unitpriceAccuracy
							) /
								100)
					).toFixed(unitpriceAccuracy); // 计算单价
				}
			}
			if (
				arr[index].taxPrice == 0 ||
				arr[index].discountRate == 0 ||
				(arr[index].taxPrice &&
					arr[index].salesNumber &&
					arr[index].discountRate)
			) {
				// 判断 折扣率 含税单价 销售数量 是否为空
				if (
					!isNaN(parseFloat(arr[index].taxPrice)) &&
					!isNaN(parseFloat(arr[index].salesNumber)) &&
					!isNaN(parseFloat(arr[index].discountRate))
				) {
					arr[index].disCount = (
						(parseFloat(arr[index].discountRate) / 100) *
						parseFloat(arr[index].taxPrice) *
						parseFloat(arr[index].salesNumber)
					).toFixed(amountAccuracy);
				}
			}
			if (
				arr[index].taxPrice == 0 ||
				arr[index].disCount == 0 ||
				(arr[index].taxPrice &&
					arr[index].salesNumber &&
					arr[index].disCount)
			) {
				// 计算价税合计 判断 含税单价 数量 折扣额 是否为空
				if (
					!isNaN(parseFloat(arr[index].taxPrice)) &&
					!isNaN(parseFloat(arr[index].salesNumber))
				) {
					let nV =
						parseFloat(arr[index].taxPrice) *
							parseFloat(arr[index].salesNumber) -
						parseFloat(arr[index].disCount);
					arr[index].allAmount = nV.toFixed(amountAccuracy);
				}
			}
			if (arr[index].allAmount && exchangeRate) {
				// 计算价税合计本位币 判断 价税合计 汇率 是否为空
				arr[index].allAmountLc = (
					parseFloat(arr[index].allAmount) * exchangeRate
				).toFixed(amountAccuracy);
			}
			if (arr[index].allAmount) {
				// 计算金额、税额 判断 价税合计 税率 是否为空
				arr[index].taxaMount = (
					(parseFloat(arr[index].allAmount) *
						(arr[index].taxRate / 100)) /
					(1 + arr[index].taxRate / 100)
				).toFixed(amountAccuracy); // 计算税额
				arr[index].amount = (
					parseFloat(arr[index].allAmount) - arr[index].taxaMount
				).toFixed(amountAccuracy); // 计算金额
			}
			let taxAmount = 0;
			let amount = 0;
			let allAmount = 0;
			arr.forEach((el) => {
				if (
					el.taxaMount === '' ||
					el.taxaMount === null ||
					el.taxaMount === undefined
				) {
					el.taxaMount = '0';
				}
				taxAmount += parseFloat(el.taxaMount);
				if (
					el.amount === '' ||
					el.amount === null ||
					el.amount === undefined
				) {
					el.amount = '0';
				}
				amount += parseFloat(el.amount);
				if (
					el.allAmount === '' ||
					el.allAmount === null ||
					el.allAmount === undefined
				) {
					el.allAmount = '0';
				}
				allAmount += parseFloat(el.allAmount);
			});
			templateObject.baseTaxaMount = taxAmount.toFixed(amountAccuracy);
			templateObject.baseAmount = amount.toFixed(amountAccuracy);
			templateObject.baseAllAmount = allAmount.toFixed(amountAccuracy);
			templateObject.details = arr;
		}
	}
	templateObject.baseTaxaMount =
		templateObject.baseTaxaMount == ''
			? 0
			: Number(templateObject.baseTaxaMount) * 1 + '';
	templateObject.baseAmount =
		templateObject.baseAmount == ''
			? 0
			: Number(templateObject.baseAmount) * 1 + '';
	templateObject.baseAllAmount =
		templateObject.baseAllAmount == ''
			? 0
			: Number(templateObject.baseAllAmount) * 1 + '';
	templateObject.details.forEach((el, ind) => {
		templateObject.details[ind].price =
			Number(templateObject.details[ind].price) * 1 + '';
		templateObject.details[ind].allAmount =
			Number(templateObject.details[ind].allAmount) * 1 + '';
		templateObject.details[ind].allAmountLc =
			Number(templateObject.details[ind].allAmountLc) * 1 + '';
		templateObject.details[ind].amount =
			Number(templateObject.details[ind].amount) * 1 + '';
		templateObject.details[ind].disCount =
			Number(templateObject.details[ind].disCount) * 1 + '';
		templateObject.details[ind].taxaMount =
			Number(templateObject.details[ind].taxaMount) * 1 + '';
		// if(el.taxPrice.toString().substring(el.taxPrice.length - 1,el.taxPrice.length) === '.'){
		//     el.taxPrice = el.taxPrice.toString().substring(0,el.taxPrice.length - 1)
		// }
	});
	return templateObject;
};

export const returnCountMethods = (
	arr,
	index,
	key,
	exchangeRate,
	unitpriceAccuracy,
	amountAccuracy
) => {
	// 销售退货订单计算
	if (
		unitpriceAccuracy === '' ||
		unitpriceAccuracy === undefined ||
		unitpriceAccuracy === null
	) {
		unitpriceAccuracy = 6;
	}
	if (
		amountAccuracy === '' ||
		amountAccuracy === undefined ||
		amountAccuracy === null
	) {
		amountAccuracy = 2;
	}
	if (
		exchangeRate === '' ||
		exchangeRate === undefined ||
		exchangeRate === null ||
		exchangeRate === '0' ||
		exchangeRate === 0
	) {
		exchangeRate = '1';
	}
	if (
		arr[index].taxRate === '' ||
		arr[index].taxRate === undefined ||
		arr[index].taxRate === null
	) {
		arr[index].taxRate = '0';
	}
	if (
		arr[index].realQty === '' ||
		arr[index].realQty === undefined ||
		arr[index].realQty === null
	) {
		arr[index].realQty = '0';
	}
	if (
		arr[index].disCount === '' ||
		arr[index].disCount === undefined ||
		arr[index].disCount === null
	) {
		arr[index].disCount = '0';
	}
	if (
		arr[index].taxPrice === '' ||
		arr[index].taxPrice === undefined ||
		arr[index].taxPrice === null
	) {
		arr[index].taxPrice = '0';
	}
	if (
		arr[index].discountRate === '' ||
		arr[index].discountRate === undefined ||
		arr[index].discountRate === null
	) {
		arr[index].discountRate = '0';
	}
	// if(arr[index].taxaMount === '' || arr[index].taxaMount === undefined || arr[index].taxaMount === null){
	//     arr[index].taxaMount = '0'
	// }
	if (
		arr[index].amount === '' ||
		arr[index].amount === undefined ||
		arr[index].amount === null
	) {
		arr[index].amount = '0';
	}
	if (
		arr[index].allAmountLc === '' ||
		arr[index].allAmountLc === undefined ||
		arr[index].allAmountLc === null
	) {
		arr[index].allAmountLc = '0';
	}
	if (
		arr[index].allAmount === '' ||
		arr[index].allAmount === undefined ||
		arr[index].allAmount === null
	) {
		arr[index].allAmount = '0';
	}
	if (
		arr[index].price === '' ||
		arr[index].price === undefined ||
		arr[index].price === null
	) {
		arr[index].price = '0';
	}
	if (
		arr[index].taxAmount === '' ||
		arr[index].taxAmount === undefined ||
		arr[index].taxAmount === null
	) {
		arr[index].taxAmount = '0';
	}
	let templateObject = {
		details: [],
	};
	if (
		key == 'taxRate' ||
		key == 'realQty' ||
		key == 'disCount' ||
		key == 'discountRate' ||
		key === 'taxPrice'
	) {
		// 定义税率 value
		if (index != undefined) {
			if (arr[index].taxPrice === 0 || arr[index].taxPrice) {
				// 计算单价 税额 判断 含税单价 是否为空
				arr[index].price = (
					parseFloat(arr[index].taxPrice) /
					(1 + parseFloat(arr[index].taxRate) / 100)
				).toFixed(unitpriceAccuracy); // 计算单价
			}
			if (
				arr[index].taxPrice === 0 ||
				(arr[index].taxPrice &&
					arr[index].realQty &&
					arr[index].discountRate)
			) {
				// 计算折扣额 判断 折扣率 含税单价 销售数量 是否为空
				arr[index].disCount = (
					(parseFloat(arr[index].discountRate) *
						parseFloat(arr[index].taxPrice) *
						parseFloat(arr[index].realQty)) /
					100
				).toFixed(amountAccuracy);
			}
			if (
				arr[index].taxPrice === 0 ||
				arr[index].disCount === 0 ||
				(arr[index].taxPrice &&
					arr[index].realQty &&
					arr[index].disCount)
			) {
				// 计算价税合计 判断 含税单价 数量 折扣额 是否为空
				let nV =
					parseFloat(arr[index].taxPrice) *
						parseFloat(arr[index].realQty) -
					parseFloat(arr[index].disCount);
				arr[index].allAmount = nV.toFixed(amountAccuracy);
			}
			if (arr[index].allAmount && exchangeRate) {
				// 计算价税合计本位币 判断 价税合计 汇率 是否为空
				arr[index].allAmountLc = (
					parseFloat(arr[index].allAmount) * exchangeRate
				).toFixed(amountAccuracy);
			}
			if (arr[index].allAmount) {
				// 计算金额、税额 判断 价税合计 税率 是否为空
				arr[index].taxAmount = (
					(parseFloat(arr[index].allAmount) *
						(parseFloat(arr[index].taxRate) / 100)) /
					(1 + parseFloat(arr[index].taxRate) / 100)
				).toFixed(amountAccuracy); // 计算税额
				arr[index].amount = (
					parseFloat(arr[index].allAmount) -
					parseFloat(arr[index].taxAmount)
				).toFixed(amountAccuracy); // 计算金额
			}
		}
	}
	templateObject.details = arr;
	templateObject.details.forEach((el, ind) => {
		templateObject.details[ind].price =
			Number(templateObject.details[ind].price) * 1 + '';
		templateObject.details[ind].allAmount =
			Number(templateObject.details[ind].allAmount) * 1 + '';
		templateObject.details[ind].allAmountLc =
			Number(templateObject.details[ind].allAmountLc) * 1 + '';
		templateObject.details[ind].disCount =
			Number(templateObject.details[ind].disCount) * 1 + '';
		templateObject.details[ind].taxAmount =
			Number(templateObject.details[ind].taxAmount) * 1 + '';
		templateObject.details[ind].amount =
			Number(templateObject.details[ind].amount) * 1 + '';
	});
	return templateObject;
};

export const countMethodsPur = (
	arr,
	value,
	index,
	key,
	taxRates,
	exchangeRate,
	unitpriceAccuracy,
	amountAccuracy
) => {
	// 采购订单计算
	if (
		unitpriceAccuracy === '' ||
		unitpriceAccuracy === undefined ||
		unitpriceAccuracy === null
	) {
		unitpriceAccuracy = 6;
	}
	if (
		amountAccuracy === '' ||
		amountAccuracy === undefined ||
		amountAccuracy === null
	) {
		amountAccuracy = 2;
	}
	if (value === '' || value === undefined || value === null) {
		value = '0';
	}
	if (
		exchangeRate === '' ||
		exchangeRate === undefined ||
		exchangeRate === null ||
		exchangeRate === '0' ||
		exchangeRate === 0
	) {
		exchangeRate = '1';
	}
	if (
		arr[index].qty === '' ||
		arr[index].qty === undefined ||
		arr[index].qty === null
	) {
		arr[index].qty = '0';
	}
	if (
		arr[index].price === '' ||
		arr[index].price === undefined ||
		arr[index].price === null
	) {
		arr[index].price = '0';
	}
	if (
		arr[index].taxRate === '' ||
		arr[index].taxRate === undefined ||
		arr[index].taxRate === null
	) {
		arr[index].taxRate = '0';
	}
	if (
		arr[index].taxPrice === '' ||
		arr[index].taxPrice === undefined ||
		arr[index].taxPrice === null
	) {
		arr[index].taxPrice = '0';
	}
	if (
		arr[index].allAmount === '' ||
		arr[index].allAmount === undefined ||
		arr[index].allAmount === null
	) {
		arr[index].allAmount = '0';
	}
	if (
		arr[index].localCurrAllAmount === '' ||
		arr[index].localCurrAllAmount === undefined ||
		arr[index].localCurrAllAmount === null
	) {
		arr[index].localCurrAllAmount = '0';
	}
	if (
		arr[index].discountRate === '' ||
		arr[index].discountRate === undefined ||
		arr[index].discountRate === null
	) {
		arr[index].discountRate = '0';
	}
	if (
		arr[index].discountAmount === '' ||
		arr[index].discountAmount === undefined ||
		arr[index].discountAmount === null
	) {
		arr[index].discountAmount = '0';
	}
	if (
		arr[index].taxAmount === '' ||
		arr[index].taxAmount === undefined ||
		arr[index].taxAmount === null
	) {
		arr[index].taxAmount = '0';
	}
	if (
		arr[index].amount === '' ||
		arr[index].amount === undefined ||
		arr[index].amount === null
	) {
		arr[index].amount = '0';
	}
	let templateObject = {
		details: [],
	};
	if (index != undefined || index != '' || index != null) {
		if (
			key == 'taxRate' ||
			key == 'qty' ||
			key == 'discountAmount' ||
			key === 'taxPrice' ||
			key === 'discountRate'
		) {
			// 定义税率 value
			if (arr[index].taxPrice === 0 || arr[index].taxPrice) {
				// 计算单价 税额 判断 含税单价 是否为空
				if (!isNaN(parseFloat(arr[index].taxPrice))) {
					arr[index].price = (
						parseFloat(arr[index].taxPrice) /
						(1 + parseFloat(arr[index].taxRate) / 100)
					).toFixed(unitpriceAccuracy); // 计算单价
				}
			}
			if (
				arr[index].taxPrice === 0 ||
				(arr[index].taxPrice &&
					arr[index].qty &&
					arr[index].discountRate)
			) {
				// 计算折扣额 判断 折扣率 含税单价 销售数量 是否为空
				if (
					!isNaN(parseFloat(arr[index].taxPrice)) &&
					!isNaN(parseFloat(arr[index].qty)) &&
					!isNaN(parseFloat(arr[index].discountRate))
				) {
					// console.log('inner')
					arr[index].discountAmount = (
						(parseFloat(arr[index].discountRate) *
							parseFloat(arr[index].taxPrice) *
							parseFloat(arr[index].qty)) /
						100
					).toFixed(amountAccuracy);
				}
			}
			if (
				arr[index].taxPrice === 0 ||
				arr[index].discountAmount === 0 ||
				(arr[index].taxPrice &&
					arr[index].qty &&
					arr[index].discountAmount)
			) {
				// 计算价税合计 判断 含税单价 数量 折扣额 是否为空
				if (
					!isNaN(parseFloat(arr[index].taxPrice)) &&
					!isNaN(parseFloat(arr[index].qty))
				) {
					let nV =
						parseFloat(arr[index].taxPrice) *
							parseFloat(arr[index].qty) -
						parseFloat(arr[index].discountAmount);
					arr[index].allAmount = nV.toFixed(amountAccuracy);
				}
			}
			if (arr[index].allAmount && exchangeRate) {
				// 计算价税合计本位币 判断 价税合计 汇率 是否为空
				arr[index].localCurrAllAmount = (
					parseFloat(arr[index].allAmount) * exchangeRate
				).toFixed(amountAccuracy);
			}
			if (arr[index].allAmount) {
				// 计算金额、税额 判断 价税合计 税率 是否为空
				// console.log('taxAmount',parseFloat(arr[index].allAmount) * (parseFloat(arr[index].taxRate)/100) / (1 + (parseFloat(arr[index].taxRate)/100)))
				// console.log('allAmount',arr[index].allAmount)
				// console.log('taxAmount',arr[index].taxAmount)
				// console.log('amount',parseFloat(arr[index].allAmount) - parseFloat(arr[index].taxAmount))
				arr[index].taxAmount = (
					(parseFloat(arr[index].allAmount) *
						(parseFloat(arr[index].taxRate) / 100)) /
					(1 + parseFloat(arr[index].taxRate) / 100)
				).toFixed(amountAccuracy); // 计算税额
				arr[index].amount = (
					parseFloat(arr[index].allAmount) -
					parseFloat(arr[index].taxAmount)
				).toFixed(amountAccuracy); // 计算金额
			}

			let taxAmount = 0;
			let amount = 0;
			let allAmount = 0;
			// arr.forEach(el => {
			//     taxAmount =+ parseFloat(el.taxAmount)
			//     amount =+ parseFloat(el.amount)
			//     allAmount =+ parseFloat(el.allAmount)
			// });
			arr.forEach((el) => {
				if (
					el.taxAmount === '' ||
					el.taxAmount === null ||
					el.taxAmount === undefined
				) {
					el.taxAmount = '0';
				}
				taxAmount += parseFloat(el.taxAmount);
				if (
					el.amount === '' ||
					el.amount === null ||
					el.amount === undefined
				) {
					el.amount = '0';
				}
				amount += parseFloat(el.amount);
				if (
					el.allAmount === '' ||
					el.allAmount === null ||
					el.allAmount === undefined
				) {
					el.allAmount = '0';
				}
				allAmount += parseFloat(el.allAmount);
			});
			templateObject.baseTaxaMount = taxAmount.toFixed(amountAccuracy);
			templateObject.baseAmount = amount.toFixed(amountAccuracy);
			templateObject.baseAllAmount = allAmount.toFixed(amountAccuracy);
		}
	}
	templateObject.details = arr;
	templateObject.baseTaxaMount =
		templateObject.baseTaxaMount == ''
			? 0
			: Number(templateObject.baseTaxaMount) * 1 + '';
	templateObject.baseAmount =
		templateObject.baseAmount == ''
			? 0
			: Number(templateObject.baseAmount) * 1 + '';
	templateObject.baseAllAmount =
		templateObject.baseAllAmount == ''
			? 0
			: Number(templateObject.baseAllAmount) * 1 + '';
	templateObject.details.forEach((el, ind) => {
		templateObject.details[ind].price =
			Number(templateObject.details[ind].price) * 1 + '';
		templateObject.details[ind].allAmount =
			Number(templateObject.details[ind].allAmount) * 1 + '';
		templateObject.details[ind].localCurrAllAmount =
			Number(templateObject.details[ind].localCurrAllAmount) * 1 + '';
		templateObject.details[ind].amount =
			Number(templateObject.details[ind].amount) * 1 + '';
		if (isNaN(templateObject.details[ind].discountAmount)) {
			templateObject.details[ind].discountAmount = '0';
		} else {
			templateObject.details[ind].discountAmount =
				Number(templateObject.details[ind].discountAmount) * 1 + '';
		}
		templateObject.details[ind].taxAmount =
			Number(templateObject.details[ind].taxAmount) * 1 + '';
	});
	return templateObject;
};

export const countMethodsPurHo = (
	arr,
	value,
	index,
	key,
	taxRates,
	exchangeRate,
	unitpriceAccuracy,
	amountAccuracy
) => {
	// 采购入库单计算
	if (
		unitpriceAccuracy === '' ||
		unitpriceAccuracy === undefined ||
		unitpriceAccuracy === null
	) {
		unitpriceAccuracy = 6;
	}
	if (
		amountAccuracy === '' ||
		amountAccuracy === undefined ||
		amountAccuracy === null
	) {
		amountAccuracy = 2;
	}
	if (value === '' || value === undefined || value === null) {
		value = '0';
	}
	if (
		exchangeRate === '' ||
		exchangeRate === undefined ||
		exchangeRate === null ||
		exchangeRate === '0' ||
		exchangeRate === 0
	) {
		exchangeRate = '1';
	}
	if (
		arr[index].realQty === '' ||
		arr[index].realQty === undefined ||
		arr[index].realQty === null
	) {
		arr[index].realQty = '0';
	}
	if (
		arr[index].price === '' ||
		arr[index].price === undefined ||
		arr[index].price === null
	) {
		arr[index].price = '0';
	}
	if (
		arr[index].taxRate === '' ||
		arr[index].taxRate === undefined ||
		arr[index].taxRate === null
	) {
		arr[index].taxRate = '0';
	}
	if (
		arr[index].taxPrice === '' ||
		arr[index].taxPrice === undefined ||
		arr[index].taxPrice === null
	) {
		arr[index].taxPrice = '0';
	}
	if (
		arr[index].allAmount === '' ||
		arr[index].allAmount === undefined ||
		arr[index].allAmount === null
	) {
		arr[index].allAmount = '0';
	}
	if (
		arr[index].localCurrAllAmount === '' ||
		arr[index].localCurrAllAmount === undefined ||
		arr[index].localCurrAllAmount === null
	) {
		arr[index].localCurrAllAmount = '0';
	}
	if (
		arr[index].discountRate === '' ||
		arr[index].discountRate === undefined ||
		arr[index].discountRate === null
	) {
		arr[index].discountRate = '0';
	}
	if (
		arr[index].discountAmount === '' ||
		arr[index].discountAmount === undefined ||
		arr[index].discountAmount === null
	) {
		arr[index].discountAmount = '0';
	}
	if (
		arr[index].billTaxAmount === '' ||
		arr[index].billTaxAmount === undefined ||
		arr[index].billTaxAmount === null
	) {
		arr[index].billTaxAmount = '0';
	}
	if (
		arr[index].amount === '' ||
		arr[index].amount === undefined ||
		arr[index].amount === null
	) {
		arr[index].amount = '0';
	}
	let templateObject = {
		details: [],
	};
	if (index != undefined || index != '' || index != null) {
		// console.log('arr[index]',arr[index])
		if (
			key == 'taxRate' ||
			key == 'realQty' ||
			key == 'discountAmount' ||
			key === 'taxPrice' ||
			key === 'discountRate'
		) {
			// 定义税率 value
			if (arr[index].taxPrice === 0 || arr[index].taxPrice) {
				// 计算单价 税额 判断 含税单价 是否为空
				if (!isNaN(parseFloat(arr[index].taxPrice))) {
					arr[index].price = (
						parseFloat(arr[index].taxPrice) /
						(1 + arr[index].taxRate / 100)
					).toFixed(unitpriceAccuracy);
				}
			}
			if (
				arr[index].taxPrice === 0 ||
				(arr[index].taxPrice &&
					arr[index].realQty &&
					arr[index].discountRate)
			) {
				// 计算折扣额 判断 折扣率 含税单价 销售数量 是否为空

				if (
					!isNaN(parseFloat(arr[index].taxPrice)) &&
					!isNaN(parseFloat(arr[index].realQty)) &&
					!isNaN(parseFloat(arr[index].discountRate))
				) {
					arr[index].discountAmount = (
						(parseFloat(arr[index].discountRate) *
							parseFloat(arr[index].taxPrice) *
							parseFloat(arr[index].realQty)) /
						100
					).toFixed(amountAccuracy);
				}
			}
			if (
				arr[index].taxPrice === 0 ||
				arr[index].discountAmount === 0 ||
				(arr[index].taxPrice &&
					arr[index].realQty &&
					arr[index].discountAmount)
			) {
				// 计算价税合计 判断 含税单价 数量 折扣额 是否为空
				if (
					!isNaN(parseFloat(arr[index].taxPrice)) &&
					!isNaN(parseFloat(arr[index].realQty))
				) {
					let nV =
						parseFloat(arr[index].taxPrice) *
							parseFloat(arr[index].realQty) -
						parseFloat(arr[index].discountAmount);
					arr[index].allAmount = nV.toFixed(amountAccuracy);
					// console.log('arr[index].allAmount',arr[index].allAmount)
				}
			}
			if (arr[index].allAmount && exchangeRate) {
				// 计算价税合计本位币 判断 价税合计 汇率 是否为空
				arr[index].localCurrAllAmount = (
					parseFloat(arr[index].allAmount) * exchangeRate
				).toFixed(amountAccuracy);
			}
			if (arr[index].allAmount) {
				// 计算金额、税额 判断 价税合计 税率 是否为空
				arr[index].billTaxAmount = (
					(parseFloat(arr[index].allAmount) *
						(arr[index].taxRate / 100)) /
					(1 + arr[index].taxRate / 100)
				).toFixed(amountAccuracy);
				arr[index].amount = (
					parseFloat(arr[index].allAmount) -
					parseFloat(arr[index].billTaxAmount)
				).toFixed(amountAccuracy);
			}
			let taxAmount = 0;
			let amount = 0;
			let allAmount = 0;
			arr.forEach((el) => {
				if (
					el.billTaxAmount === '' ||
					el.billTaxAmount === null ||
					el.billTaxAmount === undefined
				) {
					el.billTaxAmount = '0';
				}
				taxAmount += parseFloat(el.billTaxAmount);
				if (
					el.amount === '' ||
					el.amount === null ||
					el.amount === undefined
				) {
					el.amount = '0';
				}
				amount += parseFloat(el.amount);
				if (
					el.allAmount === '' ||
					el.allAmount === null ||
					el.allAmount === undefined
				) {
					el.allAmount = '0';
				}
				allAmount += parseFloat(el.allAmount);
			});
			templateObject.baseTaxaMount = taxAmount.toFixed(amountAccuracy);
			templateObject.baseAmount = amount.toFixed(amountAccuracy);
			templateObject.baseAllAmount = allAmount.toFixed(amountAccuracy);
		}
	}
	templateObject.details = arr;
	templateObject.baseTaxaMount =
		templateObject.baseTaxaMount == ''
			? 0
			: Number(templateObject.baseTaxaMount) * 1 + '';
	templateObject.baseAmount =
		templateObject.baseAmount == ''
			? 0
			: Number(templateObject.baseAmount) * 1 + '';
	templateObject.baseAllAmount =
		templateObject.baseAllAmount == ''
			? 0
			: Number(templateObject.baseAllAmount) * 1 + '';
	templateObject.details.forEach((el, ind) => {
		templateObject.details[ind].price =
			Number(templateObject.details[ind].price) * 1 + '';
		templateObject.details[ind].allAmount =
			Number(templateObject.details[ind].allAmount) * 1 + '';
		if (isNaN(templateObject.details[ind].discountAmount)) {
			templateObject.details[ind].discountAmount = '0';
		} else {
			templateObject.details[ind].discountAmount =
				Number(templateObject.details[ind].discountAmount) * 1 + '';
		}
		templateObject.details[ind].billTaxAmount =
			Number(templateObject.details[ind].billTaxAmount) * 1 + '';
		templateObject.details[ind].amount =
			Number(templateObject.details[ind].amount) * 1 + '';
	});
	return templateObject;
};

export const countMethodsPurHoRe = (
	arr,
	value,
	index,
	key,
	taxRates,
	exchangeRate,
	unitpriceAccuracy,
	amountAccuracy
) => {
	// 采购退料单计算
	if (
		unitpriceAccuracy === '' ||
		unitpriceAccuracy === undefined ||
		unitpriceAccuracy === null
	) {
		unitpriceAccuracy = 6;
	}
	if (
		amountAccuracy === '' ||
		amountAccuracy === undefined ||
		amountAccuracy === null
	) {
		amountAccuracy = 2;
	}
	if (value === '' || value === undefined || value === null) {
		value = '0';
	}
	if (
		exchangeRate === '' ||
		exchangeRate === undefined ||
		exchangeRate === null ||
		exchangeRate === '0' ||
		exchangeRate === 0
	) {
		exchangeRate = '1';
	}
	if (
		arr[index].returnRealQty === '' ||
		arr[index].returnRealQty === undefined ||
		arr[index].returnRealQty === null
	) {
		arr[index].returnRealQty = '0';
	}
	if (
		arr[index].taxPrice === '' ||
		arr[index].taxPrice === undefined ||
		arr[index].taxPrice === null
	) {
		arr[index].taxPrice = '0';
	}
	if (
		arr[index].taxRate === '' ||
		arr[index].taxRate === undefined ||
		arr[index].taxRate === null
	) {
		arr[index].taxRate = '0';
	}
	if (
		arr[index].price === '' ||
		arr[index].price === undefined ||
		arr[index].price === null
	) {
		arr[index].price = '0';
	}
	if (
		arr[index].allAmount === '' ||
		arr[index].allAmount === undefined ||
		arr[index].allAmount === null
	) {
		arr[index].allAmount = '0';
	}
	if (
		arr[index].taxAmount === '' ||
		arr[index].taxAmount === undefined ||
		arr[index].taxAmount === null
	) {
		arr[index].taxAmount = '0';
	}
	if (
		arr[index].amount === '' ||
		arr[index].amount === undefined ||
		arr[index].amount === null
	) {
		arr[index].amount = '0';
	}
	if (
		arr[index].replenishQty === '' ||
		arr[index].replenishQty === undefined ||
		arr[index].replenishQty === null
	) {
		arr[index].replenishQty = '0';
	}
	let templateObject = {
		details: [],
	};
	if (index != undefined || index != '' || index != null) {
		if (key == 'taxRate' || key == 'returnRealQty' || key === 'taxPrice') {
			// 定义税率 value
			if (arr[index].taxPrice === 0 || arr[index].taxPrice) {
				// 计算单价 税额 判断 含税单价 是否为空
				if (!isNaN(parseFloat(arr[index].taxPrice))) {
					arr[index].price = (
						parseFloat(arr[index].taxPrice) /
						(1 + arr[index].taxRate / 100)
					).toFixed(unitpriceAccuracy); // 计算单价
				}
			}
			// && arr[index].returnRealQty
			if (arr[index].taxPrice === 0 || arr[index].taxPrice) {
				// 计算价税合计 判断 含税单价 数量 折扣额 是否为空
				if (
					!isNaN(parseFloat(arr[index].taxPrice)) &&
					!isNaN(parseFloat(arr[index].returnRealQty))
				) {
					let nV =
						parseFloat(arr[index].taxPrice) *
						parseFloat(arr[index].returnRealQty);
					arr[index].allAmount = nV.toFixed(amountAccuracy);
				}
			}
			if (arr[index].allAmount) {
				// 计算金额、税额 判断 价税合计 税率 是否为空
				arr[index].taxAmount = (
					(parseFloat(arr[index].allAmount) *
						(arr[index].taxRate / 100)) /
					(1 + arr[index].taxRate / 100)
				).toFixed(amountAccuracy); // 计算税额
				arr[index].amount = (
					parseFloat(arr[index].allAmount) /
					(1 + arr[index].taxRate / 100)
				).toFixed(amountAccuracy); // 计算金额
			}
			// && arr[index].returnRealQty
			if (arr[index].taxPrice === 0 || arr[index].taxPrice) {
				// 计算价税合计 判断 含税单价 数量 折扣额 是否为空
				if (
					!isNaN(parseFloat(arr[index].taxPrice)) &&
					!isNaN(parseFloat(arr[index].returnRealQty))
				) {
					let nV =
						parseFloat(arr[index].taxPrice) *
						parseFloat(arr[index].returnRealQty);
					arr[index].allAmount = nV.toFixed(amountAccuracy);
				}
			}
			let taxAmount = 0;
			let amount = 0;
			let allAmount = 0;
			arr.forEach((el) => {
				if (
					el.taxAmount === '' ||
					el.taxAmount === null ||
					el.taxAmount === undefined
				) {
					el.taxAmount = '0';
				}
				taxAmount += parseFloat(el.taxAmount);
				if (
					el.amount === '' ||
					el.amount === null ||
					el.amount === undefined
				) {
					el.amount = '0';
				}
				amount += parseFloat(el.amount);
				if (
					el.allAmount === '' ||
					el.allAmount === null ||
					el.allAmount === undefined
				) {
					el.allAmount = '0';
				}
				allAmount += parseFloat(el.allAmount);
			});
			templateObject.baseTaxaMount = taxAmount.toFixed(amountAccuracy);
			templateObject.baseAmount = amount.toFixed(amountAccuracy);
			templateObject.baseAllAmount = allAmount.toFixed(amountAccuracy);
		}
	}
	templateObject.details = arr;
	templateObject.baseTaxaMount =
		templateObject.baseTaxaMount == ''
			? 0
			: Number(templateObject.baseTaxaMount) * 1 + '';
	templateObject.baseAmount =
		templateObject.baseAmount == ''
			? 0
			: Number(templateObject.baseAmount) * 1 + '';
	templateObject.baseAllAmount =
		templateObject.baseAllAmount == ''
			? 0
			: Number(templateObject.baseAllAmount) * 1 + '';
	templateObject.details.forEach((el, ind) => {
		templateObject.details[ind].price =
			Number(templateObject.details[ind].price) * 1 + '';
		templateObject.details[ind].allAmount =
			Number(templateObject.details[ind].allAmount) * 1 + '';
		templateObject.details[ind].taxAmount =
			Number(templateObject.details[ind].taxAmount) * 1 + '';
		templateObject.details[ind].amount =
			Number(templateObject.details[ind].amount) * 1 + '';
	});
	return templateObject;
};

export const countMethodsUnitNumber = (
	tempList,
	value,
	salesNumber,
	isCount,
	salePrecisionNumber,
	copy
) => {
	let returnValue = '';
	let returnCount = 2;
	if (tempList.length == 0) {
		return {
			value: value * 1,
			isDefault: 1,
		};
	}
	console.log(tempList, '=tempList=');
	console.log(value, '=value=');
	console.log(salesNumber, '=salesNumber=');
	console.log(isCount, '=isCount=');
	console.log(salePrecisionNumber, '=salePrecisionNumber=');

	if (value) {
		if (value.toString().charAt(value.length - 1) == '.') {
			return {
				value: value,
				isDefault: 1,
			};
		}
	}
	if (tempList[0].roundoffType) {
		if (tempList[0].roundoffType == 1) {
			if (salePrecisionNumber != 0) {
				let sumNum = '';
				if (isCount == 1 || copy) {
					if (salesNumber) {
						sumNum =
							Decimal(salesNumber).toFixed(
								salePrecisionNumber,
								Decimal.ROUND_UP
							) * 1;
						returnValue = sumNum;
					}
				} else {
					if (value.toString().indexOf('.') != -1) {
						if (value && salesNumber) {
							let indexTax = value.toString().lastIndexOf('.');
							let newValue = value
								.toString()
								.substring(indexTax + 1, value.length);
							sumNum =
								newValue.length <= salePrecisionNumber
									? salesNumber
									: salesNumber
											.toString()
											.slice(
												0,
												value.toString().length -
													(newValue.length -
														salePrecisionNumber)
											);
							returnValue = sumNum;
						}
					} else {
						if (value && salesNumber) {
							sumNum =
								Decimal(salesNumber).toFixed(
									salePrecisionNumber,
									Decimal.ROUND_UP
								) * 1;
							returnValue = sumNum;
						}
					}
				}
			} else {
				if (salesNumber) {
					let sumNum = Decimal.ceil(salesNumber).toNumber();
					returnValue = sumNum;
				}
			}
		}
		if (tempList[0].roundoffType == 2) {
			if (salePrecisionNumber != 0 && value) {
				if (value.toString().indexOf('.') == -1) {
					returnValue = value;
				} else {
					let indexTax = value.toString().lastIndexOf('.');
					let newValue = value
						.toString()
						.substring(indexTax + 1, value.length);
					returnValue =
						newValue.length <= salePrecisionNumber
							? value
							: value
									.toString()
									.slice(
										0,
										value.toString().length -
											(newValue.length -
												salePrecisionNumber)
									);
				}
			} else {
				if (value) {
					let sumNum =
						Decimal(salesNumber).toFixed(
							salePrecisionNumber,
							Decimal.ROUND_DOWN
						) * 1;
					returnValue = sumNum;
				}
			}
		}
		if (tempList[0].roundoffType == 3) {
			if (salePrecisionNumber != 0 && value && salesNumber) {
				if (value.toString().indexOf('.') != -1) {
					let indexTax = value.toString().lastIndexOf('.');
					let newValue = value
						.toString()
						.substring(indexTax + 1, value.length);
					returnValue =
						newValue.length <= salePrecisionNumber
							? value
							: value
									.toString()
									.slice(
										0,
										value.toString().length -
											(newValue.length -
												salePrecisionNumber)
									);
					if (isCount == 1 || copy) {
						let indexTax = value.toString().lastIndexOf('.');
						let newValue = value
							.toString()
							.substring(indexTax + 1, value.length);
						returnValue =
							newValue.length < salePrecisionNumber
								? value
								: Decimal(value).toFixed(salePrecisionNumber);
					}
				} else {
					let indexTax = value.toString().lastIndexOf('.');
					let newValue = value
						.toString()
						.substring(indexTax + 1, value.length);
					returnValue =
						newValue.length < salePrecisionNumber
							? value
							: Decimal(value).toFixed(salePrecisionNumber);
				}
			}
			if (salePrecisionNumber == 0) {
				if (value) {
					let sumNum = Decimal(value).toFixed(salePrecisionNumber);
					returnValue = sumNum;
				}
			}
		}
	}
	return {
		value: returnValue * 1,
		isDefault: returnCount,
	};
};
