import { NumberTwoDecimal, NumberTenDecimal } from './general_method'
import { AxiosRequestData } from '@/data/axios_request'
// 计算开票产品列表
// 参数： products：产品列表
//       isTaxIncluded：是否含税，false：不含税，true：含税
//       isRedInvoice 是否是红票 true 是 false 不是
export const ProductListCalculation = (products, isTaxIncluded, isRedInvoice) => {
    let productList = []
    products.map((_, i) => {
        let data = ProcessingStrings(products[i].price, products[i].number, products[i].amount, products[i].preferentialFlag, products[i].zeroRateFlag)
        products[i].price = data.price
        products[i].number = data.number
        products[i].amount = data.amount
        products[i].preferentialFlag = data.preferentialFlag
        products[i].zeroRateFlag = data.zeroRateFlag
        let rate = ''

        // 税率特殊计算，适配 2022040101 免征增值税政策；
        const originalTaxRate = products[i].taxRate
        const convertTaxRate = products[i].usePolicy2022040101 && originalTaxRate === '免税' ? 0 : originalTaxRate

        if (products[i].number === '' && products[i].price === '') {
            let productInfo = {}
            if (isTaxIncluded) {
                if (products[i].amount !== '' && convertTaxRate !== '') {
                    rate = Math.abs(parseFloat(products[i].amount)) /
                        (1 + Math.abs(parseFloat(convertTaxRate))) * Math.abs(parseFloat(convertTaxRate))
                }
                if (isRedInvoice) {
                    let amount = ''
                    if (products[i].amount !== '' && products[i].amount !== null) {
                        amount = -Math.abs(NumberTwoDecimal(products[i].amount))
                    }
                    let taxAmount = ''
                    if (rate !== '') {
                        taxAmount = -Math.abs(NumberTwoDecimal(rate))
                    }
                    productInfo = {
                        productCheck: products[i].productCheck,
                        productName: products[i].productName,
                        productCode: products[i].productCode,
                        specification: products[i].specification,
                        units: products[i].units,
                        number: products[i].number,
                        price: products[i].price,
                        amount: amount,
                        taxRate: products[i].taxRate,
                        taxRates: products[i].taxRates,
                        taxAmount: taxAmount,
                        preferentialFlag: products[i].preferentialFlag,
                        preferentialType: products[i].preferentialType,
                        zeroRateFlag: products[i].zeroRateFlag,
                        rowStatus: products[i].rowStatus
                    }
                } else {
                    let amount = ''
                    let taxAmount = ''
                    if (products[i].rowStatus === 2) {
                        if (products[i].amount !== '' && products[i].amount !== null) {
                            amount = NumberTwoDecimal(products[i].amount)
                        }
                        if (rate !== '') {
                            taxAmount = NumberTwoDecimal(rate)
                        }
                    } else {
                        if (products[i].amount !== '' && products[i].amount !== null) {
                            amount = Math.abs(NumberTwoDecimal(products[i].amount))
                        }
                        if (rate !== '') {
                            taxAmount = Math.abs(NumberTwoDecimal(rate))
                        }
                    }
                    productInfo = {
                        productCheck: products[i].productCheck,
                        productName: products[i].productName,
                        productCode: products[i].productCode,
                        specification: products[i].specification,
                        units: products[i].units,
                        number: products[i].number,
                        price: products[i].price,
                        amount: amount,
                        taxRate: products[i].taxRate,
                        taxRates: products[i].taxRates,
                        taxAmount: taxAmount,
                        preferentialFlag: products[i].preferentialFlag,
                        preferentialType: products[i].preferentialType,
                        zeroRateFlag: products[i].zeroRateFlag,
                        rowStatus: products[i].rowStatus,
                        // 适配 2022040101 免征增值税政策；保证数据结构完整；
                        usePolicy2022040101: products[i].usePolicy2022040101,
                        taxRateOld: products[i].taxRateOld
                    }
                }
            } else {
                if (products[i].amount !== '' && convertTaxRate !== '') {
                    rate = Math.abs(parseFloat(products[i].amount)) * Math.abs(parseFloat(convertTaxRate))
                }
                if (isRedInvoice) {
                    let amount = ''
                    if (products[i].amount !== '' && products[i].amount !== null) {
                        amount = -Math.abs(NumberTwoDecimal(products[i].amount))
                    }
                    let taxAmount = ''
                    if (rate !== '') {
                        taxAmount = -Math.abs(NumberTwoDecimal(rate))
                    }
                    productInfo = {
                        productCheck: products[i].productCheck,
                        productName: products[i].productName,
                        productCode: products[i].productCode,
                        specification: products[i].specification,
                        units: products[i].units,
                        number: products[i].number,
                        price: products[i].price,
                        amount: amount,
                        taxRate: products[i].taxRate,
                        taxRates: products[i].taxRates,
                        taxAmount: taxAmount,
                        preferentialFlag: products[i].preferentialFlag,
                        preferentialType: products[i].preferentialType,
                        zeroRateFlag: products[i].zeroRateFlag,
                        rowStatus: products[i].rowStatus
                    }
                } else {
                    let amount = ''
                    let taxAmount = ''
                    if (products[i].rowStatus === 2) {
                        if (products[i].amount !== '' && products[i].amount !== null) {
                            amount = NumberTwoDecimal(products[i].amount)
                        }
                        if (rate !== '') {
                            taxAmount = NumberTwoDecimal(rate)
                        }
                    } else {
                        if (products[i].amount.toString() !== '' && products[i].amount.toString() !== null) {
                            amount = Math.abs(NumberTwoDecimal(products[i].amount))
                        }
                        if (rate !== '') {
                            taxAmount = Math.abs(NumberTwoDecimal(rate))
                        }
                    }
                    productInfo = {
                        productCheck: products[i].productCheck,
                        productName: products[i].productName,
                        productCode: products[i].productCode,
                        specification: products[i].specification,
                        units: products[i].units,
                        number: products[i].number,
                        price: products[i].price,
                        amount: amount,
                        taxRate: products[i].taxRate,
                        taxRates: products[i].taxRates,
                        taxAmount: taxAmount,
                        preferentialFlag: products[i].preferentialFlag,
                        preferentialType: products[i].preferentialType,
                        zeroRateFlag: products[i].zeroRateFlag,
                        rowStatus: products[i].rowStatus,
                        // 适配 2022040101 免征增值税政策；保证数据结构完整；
                        usePolicy2022040101: products[i].usePolicy2022040101,
                        taxRateOld: products[i].taxRateOld
                    }
                }
            }
            productList = productList.concat(productInfo)
        } else {
            let productInfo = {}
            if (isTaxIncluded) {
                let number = ''
                let price = ''
                let amount = ''
                // 计算数量
                if (products[i].number === '') {
                    if (products[i].price !== '' && Math.abs(products[i].price) > 0) {
                        if (products[i].amount !== '') {
                            number = Math.abs(parseFloat(products[i].amount)) / Math.abs(parseFloat(products[i].price))
                        }
                    }
                } else {
                    number = Math.abs(products[i].number)
                }
                // 计算单价
                if (products[i].price === '' && Math.abs(products[i].price) <= 0) {
                    if (number !== '') {
                        if (products[i].amount !== '') {
                            price = Math.abs(parseFloat(products[i].amount)) / Math.abs(parseFloat(number))
                        }
                    }
                } else {
                    price = Math.abs(products[i].price)
                }
                // 计算金额
                if (products[i].amount === '') {
                    if (price !== '' && price > 0) {
                        if (number !== '') {
                            amount = Math.abs(parseFloat(price)) * Math.abs(parseFloat(number))
                        }
                    }
                } else {
                    if (number !== '') {
                        if (price === '' && price <= 0) {
                            price = Math.abs(parseFloat(products[i].amount)) / Math.abs(parseFloat(number))
                        } else {
                            amount = Math.abs(parseFloat(price)) * Math.abs(parseFloat(number))
                        }
                    } else {
                        if (price !== '' && price >= 0) {
                            number = Math.abs(parseFloat(products[i].amount)) / Math.abs(parseFloat(products[i].price))
                        }
                    }
                }
                // 产品税额
                rate = (Math.abs(parseFloat(number)) * Math.abs(parseFloat(price))) /
                    (1 + Math.abs(parseFloat(convertTaxRate))) * Math.abs(parseFloat(convertTaxRate))
                if (isRedInvoice) {
                    let productNumber = ''
                    if (number !== '') {
                        productNumber = -Math.abs(NumberTenDecimal(number))
                    }
                    let productAmount = ''
                    if (amount !== '') {
                        productAmount = -Math.abs(NumberTwoDecimal(amount))
                    }
                    let taxAmount = ''
                    // if (rate >= 0) {
                    //     taxAmount = -Math.abs(NumberTwoDecimal(rate))
                    // }
                    taxAmount = -Math.abs(NumberTwoDecimal(rate))
                    productInfo = {
                        productCheck: products[i].productCheck,
                        productName: products[i].productName,
                        productCode: products[i].productCode,
                        specification: products[i].specification,
                        units: products[i].units,
                        number: productNumber,
                        price: NumberTenDecimal(price),
                        amount: productAmount,
                        taxRate: products[i].taxRate,
                        taxRates: products[i].taxRates,
                        taxAmount: taxAmount,
                        preferentialFlag: products[i].preferentialFlag,
                        preferentialType: products[i].preferentialType,
                        zeroRateFlag: products[i].zeroRateFlag,
                        rowStatus: products[i].rowStatus,
                        // 适配 2022040101 免征增值税政策；保证数据结构完整；
                        usePolicy2022040101: products[i].usePolicy2022040101,
                        taxRateOld: products[i].taxRateOld
                    }
                } else {
                    productInfo = {
                        productCheck: products[i].productCheck,
                        productName: products[i].productName,
                        productCode: products[i].productCode,
                        specification: products[i].specification,
                        units: products[i].units,
                        number: NumberTenDecimal(number),
                        price: NumberTenDecimal(price),
                        amount: NumberTwoDecimal(amount),
                        taxRate: products[i].taxRate,
                        taxRates: products[i].taxRates,
                        taxAmount: NumberTwoDecimal(rate),
                        preferentialFlag: products[i].preferentialFlag,
                        preferentialType: products[i].preferentialType,
                        zeroRateFlag: products[i].zeroRateFlag,
                        rowStatus: products[i].rowStatus,
                        // 适配 2022040101 免征增值税政策；保证数据结构完整；
                        usePolicy2022040101: products[i].usePolicy2022040101,
                        taxRateOld: products[i].taxRateOld
                    }
                }
            } else {
                let number = ''
                let price = ''
                let amount = ''
                // 计算数量
                if (products[i].number === '') {
                    if (products[i].price !== '' && Math.abs(products[i].price) > 0) {
                        if (products[i].amount !== '') {
                            number = Math.abs(parseFloat(products[i].amount)) / Math.abs(parseFloat(products[i].price))
                        }
                    }
                } else {
                    number = Math.abs(products[i].number)
                }
                // 计算单价
                if (products[i].price === '' && Math.abs(products[i].price) <= 0) {
                    if (number !== '') {
                        if (products[i].amount !== '') {
                            price = Math.abs(parseFloat(products[i].amount)) / Math.abs(parseFloat(number))
                        }
                    }
                } else {
                    price = Math.abs(products[i].price)
                }
                // 计算金额
                if (products[i].amount === '') {
                    if (price !== '' && price > 0) {
                        if (number !== '') {
                            amount = Math.abs(parseFloat(price)) * Math.abs(parseFloat(number))
                        }
                    }
                } else {
                    if (number !== '') {
                        if (price === '' && price <= 0) {
                            price = Math.abs(parseFloat(products[i].amount)) / Math.abs(parseFloat(number))
                        } else {
                            amount = Math.abs(parseFloat(price)) * Math.abs(parseFloat(number))
                        }
                    } else {
                        if (price !== '' && price >= 0) {
                            number = Math.abs(parseFloat(products[i].amount)) / Math.abs(parseFloat(products[i].price))
                        }
                    }
                }
                // 产品税额
                rate = (Math.abs(parseFloat(number)) * Math.abs(parseFloat(price))) * Math.abs(parseFloat(convertTaxRate))
                // rate = (parseFloat(number) * parseFloat(price)) /
                //     (1 + parseFloat(products[i].taxRate)) * parseFloat(products[i].taxRate)
                if (isRedInvoice) {
                    let productNumber = ''
                    if (number !== '') {
                        productNumber = -Math.abs(NumberTenDecimal(number))
                    }
                    let productAmount = ''
                    if (amount !== '') {
                        productAmount = -Math.abs(NumberTwoDecimal(amount))
                    }
                    // let taxAmount = ''
                    // if (rate !== '') {
                    //     taxAmount = -Math.abs(NumberTwoDecimal(rate))
                    // }
                    productInfo = {
                        productCheck: products[i].productCheck,
                        productName: products[i].productName,
                        productCode: products[i].productCode,
                        specification: products[i].specification,
                        units: products[i].units,
                        number: productNumber,
                        price: NumberTenDecimal(price),
                        amount: productAmount,
                        taxRate: products[i].taxRate,
                        taxRates: products[i].taxRates,
                        taxAmount: -Math.abs(NumberTwoDecimal(rate)),
                        preferentialFlag: products[i].preferentialFlag,
                        preferentialType: products[i].preferentialType,
                        zeroRateFlag: products[i].zeroRateFlag,
                        rowStatus: products[i].rowStatus,
                        // 适配 2022040101 免征增值税政策；保证数据结构完整；
                        usePolicy2022040101: products[i].usePolicy2022040101,
                        taxRateOld: products[i].taxRateOld
                    }
                } else {
                    productInfo = {
                        productCheck: products[i].productCheck,
                        productName: products[i].productName,
                        productCode: products[i].productCode,
                        specification: products[i].specification,
                        units: products[i].units,
                        number: NumberTenDecimal(number),
                        price: NumberTenDecimal(price),
                        amount: NumberTwoDecimal(amount),
                        taxRate: products[i].taxRate,
                        taxRates: products[i].taxRates,
                        taxAmount: NumberTwoDecimal(rate),
                        preferentialFlag: products[i].preferentialFlag,
                        preferentialType: products[i].preferentialType,
                        zeroRateFlag: products[i].zeroRateFlag,
                        rowStatus: products[i].rowStatus,
                        // 适配 2022040101 免征增值税政策；保证数据结构完整；
                        usePolicy2022040101: products[i].usePolicy2022040101,
                        taxRateOld: products[i].taxRateOld
                    }
                }
            }
            productList = productList.concat(productInfo)
        }
    })
    // console.log('商品计算结果：', productList)
    return productList
}

// 产品列表切换含税或不含税计算
// 参数： products：产品列表
//      isTaxIncluded：是否含税，false：不含税，true：含税
//      isRedInvoice 是否是红票 true 是 false 不是
export const ProductTaxRateSwitch = (products, isTaxIncluded, isRedInvoice) => {
    let productList = []
    products.map((_, i) => {
        let rate = 0.00
        let singleRate = ''
        if (products[i].number === '' && products[i].price === '') {
            let productInfo = {}
            if (isTaxIncluded) {
                let taxRate = ''
                let amount = ''
                let taxAmount = ''
                let totalAmount = ''
                if (products[i].taxRate !== '' || products[i].taxRate != null) {
                    taxRate = products[i].taxRate
                }
                if (products[i].amount !== '' || products[i].amount != null) {
                    amount = Math.abs(products[i].amount)
                }
                // if (products[i].taxAmount !== '' || products[i].taxAmount != null) {
                //     taxAmount = products[i].taxAmount
                // }
                if (amount !== '') {
                    totalAmount = Math.abs(parseFloat(amount)) * (1 + Math.abs(parseFloat(taxRate)))
                    // totalAmount = parseFloat(amount) + parseFloat(taxAmount)
                }
                if (amount !== '' && taxRate !== '') {
                    taxAmount = Math.abs(parseFloat(amount)) * Math.abs(parseFloat(taxRate))
                }
                if (isRedInvoice) {
                    // let number = ''
                    // if (products[i].number !== '' && products[i].number !== null) {
                    //     number = -Math.abs(parseFloat(products[i].number))
                    // }
                    let productAmount = ''
                    if (totalAmount !== '') {
                        productAmount = -Math.abs(NumberTwoDecimal(totalAmount))
                    }
                    let productTaxAmount = ''
                    if (taxAmount !== '') {
                        productTaxAmount = -Math.abs(NumberTwoDecimal(taxAmount))
                    }
                    productInfo = {
                        productCheck: products[i].productCheck,
                        productName: products[i].productName,
                        productCode: products[i].productCode,
                        specification: products[i].specification,
                        units: products[i].units,
                        number: products[i].number,
                        price: products[i].price,
                        amount: productAmount,
                        taxRate: taxRate,
                        taxRates: products[i].taxRates,
                        taxAmount: productTaxAmount,
                        preferentialFlag: products[i].preferentialFlag,
                        preferentialType: products[i].preferentialType,
                        zeroRateFlag: products[i].zeroRateFlag,
                        rowStatus: products[i].rowStatus
                    }
                } else {
                    // let number = ''
                    // if (products[i].number !== '' && products[i].number !== null) {
                    //     number = Math.abs(products[i].number)
                    // }
                    let productAmount = ''
                    let productTaxAmount = ''
                    if (products[i].rowStatus === 2) {
                        if (totalAmount !== '') {
                            productAmount = -Math.abs(NumberTwoDecimal(totalAmount))
                        }
                        if (taxAmount !== '') {
                            productTaxAmount = -Math.abs(NumberTwoDecimal(taxAmount))
                        }
                    } else {
                        if (totalAmount !== '') {
                            productAmount = Math.abs(NumberTwoDecimal(totalAmount))
                        }
                        if (taxAmount !== '') {
                            productTaxAmount = Math.abs(NumberTwoDecimal(taxAmount))
                        }
                    }
                    productInfo = {
                        productCheck: products[i].productCheck,
                        productName: products[i].productName,
                        productCode: products[i].productCode,
                        specification: products[i].specification,
                        units: products[i].units,
                        number: products[i].number,
                        price: products[i].price,
                        amount: productAmount,
                        taxRate: taxRate,
                        taxRates: products[i].taxRates,
                        taxAmount: productTaxAmount,
                        preferentialFlag: products[i].preferentialFlag,
                        preferentialType: products[i].preferentialType,
                        zeroRateFlag: products[i].zeroRateFlag,
                        rowStatus: products[i].rowStatus
                    }
                }
            } else {
                let taxRate = ''
                let amount = ''
                let totalAmount = ''
                if (products[i].taxRate !== '' || products[i].taxRate != null) {
                    taxRate = products[i].taxRate
                }
                if (products[i].amount !== '' || products[i].amount != null) {
                    amount = Math.abs(products[i].amount)
                }
                if (taxRate !== '' && amount !== '') {
                    rate = Math.abs(parseFloat(amount)) / (1 + Math.abs(parseFloat(taxRate))) * Math.abs(parseFloat(taxRate))
                }
                if (amount !== '') {
                    totalAmount = Math.abs(parseFloat(amount)) / (1 + Math.abs(parseFloat(taxRate)))
                }
                if (isRedInvoice) {
                    let number = ''
                    if (products[i].number !== '' && products[i].number !== null) {
                        number = -Math.abs(parseFloat(products[i].number))
                    }
                    let productAmount = ''
                    if (totalAmount !== '') {
                        productAmount = -Math.abs(NumberTwoDecimal(totalAmount))
                    }
                    let taxAmount = ''
                    // if (rate >= 0) {
                    //     taxAmount = -Math.abs(NumberTwoDecimal(rate))
                    // }
                    taxAmount = -Math.abs(NumberTwoDecimal(rate))
                    productInfo = {
                        productCheck: products[i].productCheck,
                        productName: products[i].productName,
                        productCode: products[i].productCode,
                        specification: products[i].specification,
                        units: products[i].units,
                        number: number,
                        price: products[i].price,
                        amount: productAmount,
                        taxRate: taxRate,
                        taxRates: products[i].taxRates,
                        taxAmount: taxAmount,
                        preferentialFlag: products[i].preferentialFlag,
                        preferentialType: products[i].preferentialType,
                        zeroRateFlag: products[i].zeroRateFlag,
                        rowStatus: products[i].rowStatus
                    }
                } else {
                    // let number = ''
                    // if (products[i].number !== '' && products[i].number !== null) {
                    //     number = Math.abs(products[i].number)
                    // }
                    let productAmount = ''
                    let taxAmount = ''
                    if (products[i].rowStatus === 2) {
                        if (totalAmount !== '') {
                            productAmount = -Math.abs(NumberTwoDecimal(totalAmount))
                        }
                        taxAmount = -Math.abs(NumberTwoDecimal(rate))
                    } else {
                        if (totalAmount !== '') {
                            productAmount = Math.abs(NumberTwoDecimal(totalAmount))
                        }
                        taxAmount = Math.abs(NumberTwoDecimal(rate))
                    }
                    // if (rate >= 0) {
                    //     taxAmount = NumberTwoDecimal(rate)
                    // }
                    productInfo = {
                        productCheck: products[i].productCheck,
                        productName: products[i].productName,
                        productCode: products[i].productCode,
                        specification: products[i].specification,
                        units: products[i].units,
                        number: products[i].number,
                        price: products[i].price,
                        amount: productAmount,
                        taxRate: taxRate,
                        taxRates: products[i].taxRates,
                        taxAmount: taxAmount,
                        preferentialFlag: products[i].preferentialFlag,
                        preferentialType: products[i].preferentialType,
                        zeroRateFlag: products[i].zeroRateFlag,
                        rowStatus: products[i].rowStatus
                    }
                }
            }
            productList = productList.concat(productInfo)
        } else {
            let productInfo = {}
            if (isTaxIncluded) {
                let price = ''
                let taxRate = ''
                let number = ''
                let totalAmount = ''
                if (products[i].price !== '' || products[i].price != null) {
                    price = Math.abs(products[i].price)
                }
                if (products[i].taxRate !== '' || products[i].taxRate != null) {
                    taxRate = products[i].taxRate
                }
                if (products[i].number !== '' || products[i].number != null) {
                    number = Math.abs(products[i].number)
                }
                // 单价含税
                if (price !== '' && taxRate !== '') {
                    singleRate = Math.abs(parseFloat(price)) *
                        (1 + Math.abs(parseFloat(taxRate)))
                }
                // 产品税额
                if (number !== '' && taxRate !== '' && price !== '') {
                    rate = (Math.abs(parseFloat(number)) * Math.abs(parseFloat(price))) * Math.abs(parseFloat(taxRate))
                }
                // 产品总价含税
                if (number !== '' && singleRate !== '') {
                    totalAmount = (Math.abs(parseFloat(number)) * Math.abs(singleRate))
                }
                if (isRedInvoice) {
                    let productNumber = ''
                    if (number !== '') {
                        productNumber = -Math.abs(NumberTenDecimal(number))
                    }
                    let amount = ''
                    if (totalAmount !== '') {
                        if (products[i].amount !== '' || products[i].amount != null) {
                            amount = Math.abs(products[i].amount)
                        }
                        if (Math.abs(parseFloat(amount)) < Math.abs(NumberTwoDecimal(totalAmount))) {
                            totalAmount = Math.abs(parseFloat(amount)) * (1 + Math.abs(parseFloat(taxRate)))
                            amount = -Math.abs(NumberTwoDecimal(totalAmount))
                        } else {
                            amount = -Math.abs(NumberTwoDecimal(totalAmount))
                        }
                    }
                    let taxAmount = ''
                    taxAmount = -Math.abs(NumberTwoDecimal(rate))
                    // console.log(rate)
                    // if (rate >= 0) {
                    //     taxAmount = -Math.abs(NumberTwoDecimal(rate))
                    // }
                    productInfo = {
                        productCheck: products[i].productCheck,
                        productName: products[i].productName,
                        productCode: products[i].productCode,
                        specification: products[i].specification,
                        units: products[i].units,
                        number: productNumber,
                        price: singleRate == null || singleRate === '' ? '' : NumberTenDecimal(singleRate),
                        amount: amount,
                        taxRate: taxRate,
                        taxRates: products[i].taxRates,
                        taxAmount: taxAmount,
                        preferentialFlag: products[i].preferentialFlag,
                        preferentialType: products[i].preferentialType,
                        zeroRateFlag: products[i].zeroRateFlag,
                        rowStatus: products[i].rowStatus
                    }
                } else {
                    let productNumber = ''
                    if (number !== '') {
                        productNumber = Math.abs(NumberTenDecimal(number))
                    }
                    let amount = ''
                    if (totalAmount !== '') {
                        amount = Math.abs(NumberTwoDecimal(totalAmount))
                    }
                    let taxAmount = ''
                    // if (rate >= 0) {
                    //     taxAmount = NumberTwoDecimal(rate)
                    // }
                    taxAmount = Math.abs(NumberTwoDecimal(rate))
                    productInfo = {
                        productCheck: products[i].productCheck,
                        productName: products[i].productName,
                        productCode: products[i].productCode,
                        specification: products[i].specification,
                        units: products[i].units,
                        number: productNumber,
                        price: singleRate == null || singleRate === '' ? '' : NumberTenDecimal(singleRate),
                        amount: amount,
                        taxRate: taxRate,
                        taxRates: products[i].taxRates,
                        taxAmount: taxAmount,
                        preferentialFlag: products[i].preferentialFlag,
                        preferentialType: products[i].preferentialType,
                        zeroRateFlag: products[i].zeroRateFlag,
                        rowStatus: products[i].rowStatus
                    }
                }
            } else {
                let price = ''
                let taxRate = ''
                let number = ''
                let totalAmount = ''
                if (products[i].price !== '' || products[i].price != null) {
                    price = Math.abs(products[i].price)
                }
                if (products[i].taxRate !== '' || products[i].taxRate != null) {
                    taxRate = products[i].taxRate
                }
                if (products[i].number !== '' || products[i].number != null) {
                    number = Math.abs(products[i].number)
                }
                // 单价不含税
                if (price !== '' && taxRate !== '') {
                    singleRate = Math.abs(parseFloat(price)) / (1 + Math.abs(parseFloat(taxRate)))
                }
                // 产品税额
                if (number !== '' && taxRate !== '') {
                    rate = (Math.abs(parseFloat(number)) * Math.abs(parseFloat(price))) /
                        (1 + Math.abs(parseFloat(taxRate))) * Math.abs(parseFloat(taxRate))
                }
                // 产品总价不含税
                if (number !== '' && price !== '') {
                    totalAmount = (Math.abs(parseFloat(number)) * Math.abs(singleRate))
                    // totalAmount = (parseFloat(number) * parseFloat(price)) -
                    //     (((parseFloat(number) * parseFloat(price)) /
                    //         (1 + parseFloat(taxRate))) * parseFloat(taxRate))
                }
                if (isRedInvoice) {
                    let productNumber = ''
                    if (number !== '') {
                        productNumber = -Math.abs(NumberTenDecimal(number))
                    }
                    let amount = ''
                    if (totalAmount !== '') {
                        amount = -Math.abs(NumberTwoDecimal(totalAmount))
                    }
                    let taxAmount = ''
                    // if (rate >= 0) {
                    //     taxAmount = -Math.abs(NumberTwoDecimal(rate))
                    // }
                    taxAmount = -Math.abs(NumberTwoDecimal(rate))
                    productInfo = {
                        productCheck: products[i].productCheck,
                        productName: products[i].productName,
                        productCode: products[i].productCode,
                        specification: products[i].specification,
                        units: products[i].units,
                        number: productNumber,
                        price: singleRate == null || singleRate === '' ? '' : NumberTenDecimal(singleRate),
                        amount: amount,
                        taxRate: taxRate,
                        taxRates: products[i].taxRates,
                        taxAmount: taxAmount,
                        preferentialFlag: products[i].preferentialFlag,
                        preferentialType: products[i].preferentialType,
                        zeroRateFlag: products[i].zeroRateFlag,
                        rowStatus: products[i].rowStatus
                    }
                } else {
                    productInfo = {
                        productCheck: products[i].productCheck,
                        productName: products[i].productName,
                        productCode: products[i].productCode,
                        specification: products[i].specification,
                        units: products[i].units,
                        number: number == null || number === '' ? '' : Math.abs(NumberTenDecimal(number)),
                        price: singleRate == null || singleRate === '' ? '' : Math.abs(NumberTenDecimal(singleRate)),
                        amount: totalAmount == null || totalAmount === '' ? '' : Math.abs(NumberTwoDecimal(totalAmount)),
                        taxRate: taxRate,
                        taxRates: products[i].taxRates,
                        taxAmount: rate == null ? 0.00 : NumberTwoDecimal(rate),
                        preferentialFlag: products[i].preferentialFlag,
                        preferentialType: products[i].preferentialType,
                        zeroRateFlag: products[i].zeroRateFlag,
                        rowStatus: products[i].rowStatus
                    }
                }
            }
            productList = productList.concat(productInfo)
        }
    })
    return productList
}

// 计算单价合计（不含税）和税额合计
// 参数： products：产品列表
//      isTaxIncluded：是否含税，false：不含税，true：含税
//      isRedInvoice 是否是红票 true 是 false 不是
export const InvoiceTotalAmount = (products, isTaxIncluded, isRedInvoice) => {
    let data = {}
    let totalAmount = 0.00 // 合计金额；
    let totalTaxRate = 0.00 // 合计税额；
    let amount = 0.00 // 商品金额字段累加字段；
    let taxAmount = 0.00 // 商品税额字段累加字段；
    let discountTotalAmount = 0.00
    let discountTotalRate = 0.00
    products.map((_, i) => {
        if (isTaxIncluded) {
            if (products[i].rowStatus === 2) {
                /** 折扣行计算； */
                // 折扣金额 = 折扣行金额累加；
                discountTotalAmount = discountTotalAmount + parseFloat(products[i].amount)
                // 折扣税额 = 折扣行税额累加；
                discountTotalRate = discountTotalRate + parseFloat(products[i].taxAmount)
            }
            if (products[i].amount !== '') {
                if (isRedInvoice) {
                    if (products[i].rowStatus === 2) {
                        amount = products[i].amount
                    } else {
                        amount = Math.abs(products[i].amount)
                    }
                } else {
                    amount = products[i].amount
                }
            } else {
                amount = 0
            }
            if (products[i].taxAmount !== '') {
                if (isRedInvoice) {
                    if (products[i].rowStatus === 2) {
                        taxAmount = products[i].taxAmount
                    } else {
                        taxAmount = Math.abs(products[i].taxAmount)
                    }
                } else {
                    taxAmount = products[i].taxAmount
                }
            } else {
                taxAmount = 0
            }
            totalAmount = totalAmount + parseFloat(amount.toString()) - parseFloat(taxAmount.toString())
            // if (amount !== '' && taxAmount != '') {
            //     console.log(5)
            //     console.log(amount)
            //     totalAmount = totalAmount + parseFloat(amount) - parseFloat(taxAmount)
            // }
            // if (taxAmount != '') {
            //     console.log(6)
            //     totalTaxRate = totalTaxRate + parseFloat(taxAmount)
            // }
            totalTaxRate = totalTaxRate + parseFloat(taxAmount.toString())
        } else {
            if (products[i].rowStatus === 2) {
                discountTotalAmount = discountTotalAmount + parseFloat(products[i].amount)
                discountTotalRate = discountTotalRate + parseFloat(products[i].taxAmount)
            }
            if (products[i].amount !== '') {
                if (isRedInvoice) {
                    if (products[i].rowStatus === 2) {
                        amount = products[i].amount
                    } else {
                        amount = Math.abs(products[i].amount)
                    }
                } else {
                    amount = products[i].amount
                }
            } else {
                amount = 0
            }
            if (products[i].taxAmount !== '') {
                if (isRedInvoice) {
                    if (products[i].rowStatus === 2) {
                        taxAmount = products[i].taxAmount
                    } else {
                        taxAmount = Math.abs(products[i].taxAmount)
                    }
                } else {
                    taxAmount = products[i].taxAmount
                }
            } else {
                taxAmount = 0
            }
            totalAmount = totalAmount + parseFloat(amount.toString())
            totalTaxRate = totalTaxRate + parseFloat(taxAmount.toString())
            // if (amount != '') {
            //     totalAmount = totalAmount + parseFloat(amount)
            // }
            // if (taxAmount != '') {
            //     totalTaxRate = totalTaxRate + parseFloat(taxAmount)
            // }
        }
    })
    if (isRedInvoice) {
        data = {
            discountTotalAmount: -Math.abs(NumberTwoDecimal(discountTotalAmount)),
            discountTotalRate: -Math.abs(NumberTwoDecimal(discountTotalRate)),
            totalAmount: -Math.abs(NumberTwoDecimal(totalAmount)),
            totalTaxRate: -Math.abs(NumberTwoDecimal(totalTaxRate)),
            totalAmountRate: -Math.abs(NumberTwoDecimal(totalAmount + totalTaxRate))
        }
    } else {
        data = {
            discountTotalAmount: NumberTwoDecimal(discountTotalAmount),
            discountTotalRate: NumberTwoDecimal(discountTotalRate),
            totalAmount: NumberTwoDecimal(totalAmount),
            totalTaxRate: NumberTwoDecimal(totalTaxRate),
            totalAmountRate: NumberTwoDecimal(totalAmount + totalTaxRate)
        }
    }
    return data
}

// 处理开票产品列表转换为开票提交产品列表
// 参数：products：产品列表
//      isTaxIncluded：是否含税，false：不含税，true：含税
//       isRedInvoice 是否是红票 true 是 false 不是
export const InvoiceProducts = (products, isTaxIncluded, isRedInvoice) => {
    let product = {
        state: 0,
        products: []
    }
    products.map((_, i) => {
        let rowStatus = 0
        if (products[i].rowStatus === 1) {
            rowStatus = 2
        }
        if (products[i].rowStatus === 2) {
            rowStatus = 1
            if (products[i - 1].rowStatus === 4) {
                product.products[i - 1].FPHXZ = 2
            }
        }
        if (products[i].productName === '') {
            product.state = -1
            return product
        }
        if (products[i].amount === '') {
            product.state = -1
            return product
        }
        let productList = {}

        // 税率特殊计算，适配 2022040101 免征增值税政策；
        const originalTaxRate = products[i].taxRate
        const convertTaxRate = products[i].usePolicy2022040101 && originalTaxRate === '免税' ? 0 : originalTaxRate

        if (isTaxIncluded) {
            // 折扣行，不用填数量和单价；
            if (products[i].number === '' && products[i].price === '') {
                if (isRedInvoice) {
                    productList = {
                        key: products[i].productName, // 产品Id
                        SPHH: i,
                        FPHXZ: rowStatus, // 发票行性质 0正常行 1折扣行 2被折扣行 必填
                        SPBM: products[i].productCode, // 商品编码
                        ZXBM: products[i].productCode, // 自行编码
                        YHZCBS: products[i].preferentialFlag, // 优惠政策标识 0不使用 1使用
                        LSLBS: products[i].zeroRateFlag, // 零税率标识
                        ZZSTSGL: products[i].preferentialType, // 增值税特殊管理
                        XMMC: products[i].productName, // 项目名称 必填
                        DW: products[i].units, // 计量单位
                        GGXH: products[i].specification, // 规格型号
                        XMSL: products[i].number, // 项目数量
                        XMDJ: products[i].price, // 项目单价
                        XMJE: -Math.abs(NumberTwoDecimal((Math.abs(parseFloat(products[i].amount)) - Math.abs(parseFloat(products[i].taxAmount))))), // 项目金额 不含税
                        SL: products[i].taxRate, // 税率 必填
                        SE: -Math.abs(NumberTwoDecimal(parseFloat(products[i].taxAmount))) // 税额 必填
                    }
                } else {
                    productList = {
                        key: products[i].productName, // 产品Id
                        SPHH: i,
                        FPHXZ: rowStatus, // 发票行性质 0正常行 1折扣行 2被折扣行 必填
                        SPBM: products[i].productCode, // 商品编码
                        ZXBM: products[i].productCode, // 自行编码
                        YHZCBS: products[i].preferentialFlag, // 优惠政策标识 0不使用 1使用
                        LSLBS: products[i].zeroRateFlag, // 零税率标识
                        ZZSTSGL: products[i].preferentialType, // 增值税特殊管理
                        XMMC: products[i].productName, // 项目名称 必填
                        DW: products[i].units, // 计量单位
                        GGXH: products[i].specification, // 规格型号
                        XMSL: products[i].number, // 项目数量
                        XMDJ: products[i].price, // 项目单价
                        XMJE: NumberTwoDecimal((parseFloat(products[i].amount) - parseFloat(products[i].taxAmount))), // 项目金额 不含税
                        SL: convertTaxRate, // 税率 必填
                        SE: NumberTwoDecimal(parseFloat(products[i].taxAmount)) // 税额 必填
                    }

                    // 适配 2022040101 免征增值税政策；
                    if (products[i].usePolicy2022040101 && products[i].taxRate === '免税') {
                        productList.YHZCBS = 1 // 优惠政策标识 0不使用 1使用
                        productList.LSLBS = 1 // 零税率标识 空：非零税率，1：出口零税率和免税，2：不征收，3 普通零税率
                        productList.ZZSTSGL = '免税' // 增值税特殊管理
                    }
                }
            } else {
                if (isRedInvoice) {
                    let number = ''
                    if (products[i].number !== number && products[i].number !== '') {
                        number = -Math.abs(NumberTenDecimal(parseFloat(products[i].number)))
                    }
                    let price = ''
                    if (products[i].price !== number && products[i].price !== '') {
                        price = Math.abs(NumberTenDecimal(parseFloat(products[i].price) / (1 + Math.abs(parseFloat(products[i].taxRate)))))
                    }
                    productList = {
                        key: products[i].productName, // 产品Id
                        SPHH: i,
                        FPHXZ: rowStatus, // 发票行性质 0正常行 1折扣行 2被折扣行 必填
                        SPBM: products[i].productCode, // 商品编码
                        ZXBM: products[i].productCode, // 自行编码
                        YHZCBS: products[i].preferentialFlag, // 优惠政策标识 0不使用 1使用
                        LSLBS: products[i].zeroRateFlag, // 零税率标识
                        ZZSTSGL: products[i].preferentialType, // 增值税特殊管理
                        XMMC: products[i].productName, // 项目名称 必填
                        DW: products[i].units, // 计量单位
                        GGXH: products[i].specification, // 规格型号
                        XMSL: number, // 项目数量
                        XMDJ: price, // 项目单价
                        XMJE: -Math.abs(NumberTwoDecimal(Math.abs(parseFloat(products[i].number)) * parseFloat(products[i].price) -
                            ((Math.abs(parseFloat(products[i].number)) * parseFloat(products[i].price)) /
                                (1 + Math.abs(parseFloat(products[i].taxRate)))) * (Math.abs(parseFloat(products[i].taxRate))))), // 项目金额 不含税
                        SL: products[i].taxRate, // 税率 必填
                        SE: -Math.abs(NumberTwoDecimal(((Math.abs(parseFloat(products[i].number)) * parseFloat(products[i].price)) /
                            (1 + Math.abs(parseFloat(products[i].taxRate)))) * (Math.abs(parseFloat(products[i].taxRate))))) // 税额 必填
                    }
                } else {
                    let number = ''
                    if (products[i].number !== null && products[i].number !== '') {
                        number = Math.abs(NumberTenDecimal(parseFloat(products[i].number)))
                    }
                    let price = ''
                    if (products[i].price !== null && products[i].price !== '') {
                        price = Math.abs(NumberTenDecimal(parseFloat(products[i].price) / (1 + Math.abs(parseFloat(convertTaxRate)))))
                    }
                    productList = {
                        key: products[i].productName, // 产品Id
                        SPHH: i,
                        FPHXZ: rowStatus, // 发票行性质 0正常行 1折扣行 2被折扣行 必填
                        SPBM: products[i].productCode, // 商品编码
                        ZXBM: products[i].productCode, // 自行编码
                        YHZCBS: products[i].preferentialFlag, // 优惠政策标识 0不使用 1使用
                        LSLBS: products[i].zeroRateFlag, // 零税率标识
                        ZZSTSGL: products[i].preferentialType, // 增值税特殊管理
                        XMMC: products[i].productName, // 项目名称 必填
                        DW: products[i].units, // 计量单位
                        GGXH: products[i].specification, // 规格型号
                        XMSL: number, // 项目数量
                        XMDJ: price, // 项目单价
                        XMJE: NumberTwoDecimal(Math.abs(parseFloat(products[i].number)) * parseFloat(products[i].price) -
                            (Math.abs((parseFloat(products[i].number)) * parseFloat(products[i].price)) /
                                (1 + Math.abs(parseFloat(convertTaxRate)))) * (Math.abs(parseFloat(convertTaxRate)))), // 项目金额 不含税
                        SL: convertTaxRate, // 税率 必填
                        SE: NumberTwoDecimal((Math.abs((parseFloat(products[i].number)) * parseFloat(products[i].price)) /
                            (1 + Math.abs(parseFloat(convertTaxRate)))) * (Math.abs(parseFloat(convertTaxRate)))) // 税额 必填
                    }

                    // 适配 2022040101 免征增值税政策；
                    if (products[i].usePolicy2022040101 && products[i].taxRate === '免税') {
                        productList.YHZCBS = 1 // 优惠政策标识 0不使用 1使用
                        productList.LSLBS = 1 // 零税率标识 空：非零税率，1：出口零税率和免税，2：不征收，3 普通零税率
                        productList.ZZSTSGL = '免税' // 增值税特殊管理
                    }
                }
            }
        } else {
            if (isRedInvoice) {
                let number = ''
                if (products[i].number !== number && products[i].number !== '') {
                    number = -Math.abs(products[i].number)
                }
                let price = ''
                if (products[i].price !== number && products[i].price !== '') {
                    price = Math.abs(NumberTenDecimal(parseFloat(products[i].price)))
                }
                productList = {
                    key: products[i].productName, // 产品Id
                    SPHH: i,
                    FPHXZ: rowStatus, // 发票行性质 0正常行 1折扣行 2被折扣行 必填
                    SPBM: products[i].productCode, // 商品编码
                    ZXBM: products[i].productCode, // 自行编码
                    YHZCBS: products[i].preferentialFlag, // 优惠政策标识 0不使用 1使用
                    LSLBS: products[i].zeroRateFlag, // 零税率标识
                    ZZSTSGL: products[i].preferentialType, // 增值税特殊管理
                    XMMC: products[i].productName, // 项目名称 必填
                    DW: products[i].units, // 计量单位
                    GGXH: products[i].specification, // 规格型号
                    XMSL: number, // 项目数量
                    XMDJ: price, // 项目单价
                    XMJE: -Math.abs(NumberTwoDecimal(parseFloat(products[i].amount))), // 项目金额 不含税
                    SL: products[i].taxRate, // 税率 必填
                    SE: -Math.abs(NumberTwoDecimal(parseFloat(products[i].taxAmount))) // 税额 必填
                }
            } else {
                let number = ''
                if (products[i].number !== number && products[i].number !== '') {
                    number = Math.abs(products[i].number)
                }
                let price = ''
                if (products[i].price !== number && products[i].price !== '') {
                    price = NumberTenDecimal(parseFloat(products[i].price))
                }
                productList = {
                    key: products[i].productName, // 产品Id
                    SPHH: i,
                    FPHXZ: rowStatus, // 发票行性质 0正常行 1折扣行 2被折扣行 必填
                    SPBM: products[i].productCode, // 商品编码
                    ZXBM: products[i].productCode, // 自行编码
                    YHZCBS: products[i].preferentialFlag, // 优惠政策标识 0不使用 1使用
                    LSLBS: products[i].zeroRateFlag, // 零税率标识
                    ZZSTSGL: products[i].preferentialType, // 增值税特殊管理
                    XMMC: products[i].productName, // 项目名称 必填
                    DW: products[i].units, // 计量单位
                    GGXH: products[i].specification, // 规格型号
                    XMSL: number, // 项目数量
                    XMDJ: price, // 项目单价
                    XMJE: Math.abs(NumberTwoDecimal(parseFloat(products[i].amount))), // 项目金额 不含税
                    SL: convertTaxRate, // 税率 必填
                    SE: Math.abs(NumberTwoDecimal(parseFloat(products[i].taxAmount))) // 税额 必填
                }

                // 适配 2022040101 免征增值税政策；
                if (products[i].usePolicy2022040101 && products[i].taxRate === '免税') {
                    productList.YHZCBS = 1 // 优惠政策标识 0不使用 1使用
                    productList.LSLBS = 1 // 零税率标识 空：非零税率，1：出口零税率和免税，2：不征收，3 普通零税率
                    productList.ZZSTSGL = '免税' // 增值税特殊管理
                }
            }
        }
        product.products = product.products.concat(productList)
    })
    return product
}

// 产品自动拆分列表转换提交
// 参数：product 开票信息列表
//      status：1：表示
export const ProductsListChange = (products, status) => {
    let productList = []
    if (status === 1) {
        products.map((_, i) => {
            let product = JSON.parse(products[i].invoiceData).REQUEST_COMMON_FPKJ
            let productInfo = {
                productInfo: {
                    rowId: i,
                    invoiceNumber: '',
                    invoiceCode: '',
                    invoiceStatus: '',
                    invoiceReqFlowNo: '',
                    orderId: '',
                    invoiceType: ''
                },
                REQUEST_COMMON_FPKJ: {
                    KPLX: product.KPLX, // 开票类型 0-蓝字发票；1-红字发票
                    FPZL: product.FPZL, // 发票种类 1.增值税普通电子发票   2.增值税电子专用发票  3.增值税普通纸质发票  4 增值税专用纸质发票
                    TTLX: product.TTLX, // 收票客户抬头类型 1：个人  2：企业
                    KPF_NSRSBH: product.KPF_NSRSBH, // 开票方纳税人识别号
                    KPF_MC: product.KPF_MC, // 开票方名称
                    KPF_DZ: product.KPF_DZ, // 开票方地址
                    KPF_DH: ' ', // 开票方电话
                    KPF_YHZH: ' ', // 开票方银行账号
                    KPF_KHHMC: product.KPF_KHHMC, // 开票方开户行名称
                    SPF_NSRSBH: product.SPF_NSRSBH, // 收票方纳税人识别号
                    SPF_MC: product.SPF_MC, // 收票方名称
                    SPF_DH: ' ', // 收票方电话
                    SPF_DZ: product.SPF_DZ, // 收票方地址
                    SPF_YHZH: ' ', // 收票方银行账号
                    SPF_KHHMC: product.SPF_KHHMC, // 收票方开户行名称
                    KPR: product.KPR, // 开票人
                    SKR: product.SKR, // 收款人
                    FHR: product.FHR, // 复核人
                    YFP_DM: '', // 原发票代码
                    YFP_HM: '', // 原发票号码
                    JSHJ: product.JSHJ, // 价税合计
                    HJJE: product.HJJE, // 合计金额
                    HJSE: product.HJSE, // 合计税额
                    BZ: product.BZ, // 备注
                    HYLX: product.HYLX, // 发票行性质 0 正常行、1 折扣行、2 被折扣行
                    TSPZ: '00', // 特殊票种标识 “00”不是 “01”农产品销售“02”农产品收购“06”抵扣通行费“07”其它通行费“08”成品油销售说明:如果非特殊票种此节点可以没有。
                    COMMON_FPKJ_XMXXS: {
                        COMMON_FPKJ_XMXX: product.COMMON_FPKJ_XMXXS.COMMON_FPKJ_XMXX // 产品列表
                    }
                }
            }
            productList = productList.concat(productInfo)
        })
    }
    if (status === 2) {
        let product = products.REQUEST_COMMON_FPKJ
        let productInfo = products.productInfo
        productList = {
            productInfo: {
                rowId: productInfo.rowId,
                invoiceNumber: productInfo.invoiceNumber,
                invoiceCode: productInfo.invoiceCode,
                invoiceStatus: productInfo.invoiceStatus,
                invoiceReqFlowNo: productInfo.invoiceReqFlowNo,
                orderId: productInfo.orderId,
                invoiceType: productInfo.invoiceType
            },
            REQUEST_COMMON_FPKJ: {
                KPLX: product.KPLX, // 开票类型 0-蓝字发票；1-红字发票
                FPZL: product.FPZL, // 发票种类 1.增值税普通电子发票   2.增值税电子专用发票  3.增值税普通纸质发票  4 增值税专用纸质发票
                TTLX: product.TTLX, // 收票客户抬头类型 1：个人  2：企业
                KPF_NSRSBH: product.KPF_NSRSBH, // 开票方纳税人识别号
                KPF_MC: product.KPF_MC, // 开票方名称
                KPF_DZ: product.KPF_DZ, // 开票方地址
                KPF_DH: ' ', // 开票方电话
                KPF_YHZH: ' ', // 开票方银行账号
                KPF_KHHMC: product.KPF_KHHMC, // 开票方开户行名称
                SPF_NSRSBH: product.SPF_NSRSBH, // 收票方纳税人识别号
                SPF_MC: product.SPF_MC, // 收票方名称
                SPF_DH: ' ', // 收票方电话
                SPF_DZ: product.SPF_DZ, // 收票方地址
                SPF_YHZH: ' ', // 收票方银行账号
                SPF_KHHMC: product.SPF_KHHMC, // 收票方开户行名称
                KPR: product.KPR, // 开票人
                SKR: product.SKR, // 收款人
                FHR: product.FHR, // 复核人
                YFP_DM: '', // 原发票代码
                YFP_HM: '', // 原发票号码
                JSHJ: product.JSHJ, // 价税合计
                HJJE: product.HJJE, // 合计金额
                HJSE: product.HJSE, // 合计税额
                BZ: product.BZ, // 备注
                HYLX: product.HYLX, // 发票行性质 0 正常行、1 折扣行、2 被折扣行
                TSPZ: '00', // 特殊票种标识 “00”不是 “01”农产品销售“02”农产品收购“06”抵扣通行费“07”其它通行费“08”成品油销售说明:如果非特殊票种此节点可以没有。
                COMMON_FPKJ_XMXXS: {
                    COMMON_FPKJ_XMXX: product.COMMON_FPKJ_XMXXS.COMMON_FPKJ_XMXX // 产品列表
                }
            }
        }
    }
    return productList
}

// 处理发票重开产品列表转换为页面显示的产品列表
// 参数: products 产品信息
//      billingTaxId 销方企业税号
//      token token值
//      isRedInvoice 是否是红票 true 是 false 不是
export const InvoiceReopen = async (products, billingTaxId, token, isRedInvoice) => {
    let productList = []
    for (let i = 0; i < products.length; i++) {
        let product = {}
        let rates = ''
        rates = await getRates(products[i].goodsTaxRate, token, billingTaxId, products[i].goodsCode)
        let data = ProcessingStrings(products[i].goodsPrice, products[i].goodsQuantity,
            products[i].goodsTotalPrice, products[i].preferentialMark, products[i].freeTaxMark)
        products[i].goodsPrice = data.price
        products[i].goodsQuantity = data.number
        products[i].goodsTotalPrice = data.amount
        products[i].preferentialMark = data.preferentialFlag
        products[i].freeTaxMark = data.zeroRateFlag
        if (products[i].goodsLineNature === '0') {
            if (isRedInvoice) {
                let number = ''
                if (products[i].goodsQuantity !== '' && products[i].goodsQuantity !== null) {
                    number = -Math.abs(parseFloat(products[i].goodsQuantity))
                }
                let amount = ''
                if (products[i].goodsTotalPrice !== '' && products[i].goodsTotalPrice !== null) {
                    amount = -Math.abs(parseFloat(products[i].goodsTotalPrice))
                }
                let taxAmount = ''
                if (products[i].goodsTotalTax !== '' && products[i].goodsTotalTax !== null) {
                    taxAmount = -Math.abs(parseFloat(products[i].goodsTotalTax))
                }
                product = {
                    productCheck: false,
                    productName: products[i].goodsName,
                    productCode: products[i].goodsCode,
                    specification: products[i].goodsSpecification,
                    units: products[i].goodsUnit,
                    number: number,
                    price: products[i].goodsPrice,
                    amount: amount,
                    taxRate: products[i].goodsTaxRate,
                    taxAmount: taxAmount,
                    taxRates: rates,
                    totalAmountRate: -Math.abs((parseFloat(products[i].goodsTotalPrice) + parseFloat(products[i].goodsTotalTax))),
                    preferentialFlag: products[i].preferentialMark,
                    preferentialType: products[i].vatSpecialManagement,
                    zeroRateFlag: products[i].freeTaxMark,
                    rowStatus: 0
                }
            } else {
                let number = ''
                if (products[i].goodsQuantity !== null && products[i].goodsQuantity !== '') {
                    number = Math.abs(parseFloat(products[i].goodsQuantity))
                }
                let price = ''
                if (products[i].goodsPrice !== null && products[i].goodsPrice !== '') {
                    price = Math.abs(parseFloat(products[i].goodsPrice))
                }
                product = {
                    productCheck: false,
                    productName: products[i].goodsName,
                    productCode: products[i].goodsCode,
                    specification: products[i].goodsSpecification,
                    units: products[i].goodsUnit,
                    number: number,
                    price: price,
                    amount: products[i].goodsTotalPrice,
                    taxRate: products[i].goodsTaxRate,
                    taxAmount: products[i].goodsTotalTax,
                    taxRates: rates,
                    totalAmountRate: parseFloat(products[i].goodsTotalPrice) + parseFloat(products[i].goodsTotalTax),
                    preferentialFlag: products[i].preferentialMark,
                    preferentialType: products[i].vatSpecialManagement,
                    zeroRateFlag: products[i].freeTaxMark,
                    rowStatus: 0
                }
            }
        }
        if (products[i].goodsLineNature === '1') {
            if (isRedInvoice) {
                let amount = productList[productList.length - 1].amount
                let taxAmount = productList[productList.length - 1].taxAmount
                amount = Math.abs(parseFloat(amount)) - Math.abs(parseFloat(products[i].goodsTotalPrice))
                taxAmount = Math.abs((parseFloat(taxAmount))) - Math.abs(parseFloat(products[i].goodsTotalTax))
                productList[productList.length - 1].amount = amount
                productList[productList.length - 1].taxAmount = taxAmount
                productList[productList.length - 1].totalAmountRate = amount + taxAmount
                continue
                // let number = ''
                // if (products[i].goodsQuantity !== '' && products[i].goodsQuantity !== null) {
                //     number = -Math.abs(parseFloat(products[i].goodsQuantity))
                // }
                // let amount = ''
                // if (products[i].goodsTotalPrice !== '' && products[i].goodsTotalPrice !== null) {
                //     amount = -Math.abs(parseFloat(products[i].goodsTotalPrice))
                // }
                // let taxAmount = ''
                // if (products[i].goodsTotalTax !== '' && products[i].goodsTotalTax !== null) {
                //     taxAmount = -Math.abs(parseFloat(products[i].goodsTotalTax))
                // }
                // product = {
                //     productCheck: false,
                //     productName: products[i].goodsName,
                //     productCode: products[i].goodsCode,
                //     specification: products[i].goodsSpecification,
                //     units: products[i].goodsUnit,
                //     number: products[i].goodsQuantity,
                //     price: products[i].goodsPrice,
                //     amount: amount,
                //     taxRate: products[i].goodsTaxRate,
                //     taxAmount: taxAmount,
                //     taxRates: rates,
                //     totalAmountRate: -Math.abs((parseFloat(products[i].goodsTotalPrice) + parseFloat(products[i].goodsTotalTax))),
                //     preferentialFlag: products[i].preferentialMark,
                //     preferentialType: products[i].vatSpecialManagement,
                //     zeroRateFlag: products[i].freeTaxMark,
                //     rowStatus: 2
                // }
            } else {
                let number = ''
                if (products[i].goodsQuantity !== null && products[i].goodsQuantity !== '') {
                    number = Math.abs(parseFloat(products[i].goodsQuantity))
                }
                let price = ''
                if (products[i].goodsPrice !== null && products[i].goodsPrice !== '') {
                    price = Math.abs(parseFloat(products[i].goodsPrice))
                }
                product = {
                    productCheck: false,
                    productName: products[i].goodsName,
                    productCode: products[i].goodsCode,
                    specification: products[i].goodsSpecification,
                    units: products[i].goodsUnit,
                    number: number,
                    price: price,
                    amount: products[i].goodsTotalPrice,
                    taxRate: products[i].goodsTaxRate,
                    taxAmount: products[i].goodsTotalTax,
                    taxRates: rates,
                    totalAmountRate: parseFloat(products[i].goodsTotalPrice) + parseFloat(products[i].goodsTotalTax),
                    preferentialFlag: products[i].preferentialMark,
                    preferentialType: products[i].vatSpecialManagement,
                    zeroRateFlag: products[i].freeTaxMark,
                    rowStatus: 2
                }
            }
        }
        if (products[i].goodsLineNature === '2') {
            if (isRedInvoice) {
                let number = ''
                if (products[i].goodsQuantity !== '' && products[i].goodsQuantity !== null) {
                    number = -Math.abs(parseFloat(products[i].goodsQuantity))
                }
                let amount = ''
                if (products[i].goodsTotalPrice !== '' && products[i].goodsTotalPrice !== null) {
                    amount = -Math.abs(parseFloat(products[i].goodsTotalPrice))
                }
                let taxAmount = ''
                if (products[i].goodsTotalTax !== '' && products[i].goodsTotalTax !== null) {
                    taxAmount = -Math.abs(parseFloat(products[i].goodsTotalTax))
                }
                product = {
                    productCheck: false,
                    productName: products[i].goodsName,
                    productCode: products[i].goodsCode,
                    specification: products[i].goodsSpecification,
                    units: products[i].goodsUnit,
                    number: number,
                    price: products[i].goodsPrice,
                    amount: amount,
                    taxRate: products[i].goodsTaxRate,
                    taxAmount: taxAmount,
                    taxRates: rates,
                    totalAmountRate: -Math.abs((parseFloat(products[i].goodsTotalPrice) + parseFloat(products[i].goodsTotalTax))),
                    preferentialFlag: products[i].preferentialMark,
                    preferentialType: products[i].vatSpecialManagement,
                    zeroRateFlag: products[i].freeTaxMark,
                    rowStatus: 1
                }
            } else {
                let number = ''
                if (products[i].goodsQuantity !== null && products[i].goodsQuantity !== '') {
                    number = Math.abs(parseFloat(products[i].goodsQuantity))
                }
                let price = ''
                if (products[i].goodsPrice !== null && products[i].goodsPrice !== '') {
                    price = Math.abs(parseFloat(products[i].goodsPrice))
                }
                product = {
                    productCheck: false,
                    productName: products[i].goodsName,
                    productCode: products[i].goodsCode,
                    specification: products[i].goodsSpecification,
                    units: products[i].goodsUnit,
                    number: number,
                    price: price,
                    amount: products[i].goodsTotalPrice,
                    taxRate: products[i].goodsTaxRate,
                    taxAmount: products[i].goodsTotalTax,
                    taxRates: rates,
                    totalAmountRate: parseFloat(products[i].goodsTotalPrice) + parseFloat(products[i].goodsTotalTax),
                    preferentialFlag: products[i].preferentialMark,
                    preferentialType: products[i].vatSpecialManagement,
                    zeroRateFlag: products[i].freeTaxMark,
                    rowStatus: 1
                }
            }
        }
        productList = productList.concat(product)
    }
    return productList
}

// 处理获取到的红字信息表的产品列表转换为页面显示的产品列表
// 参数: products： 产品列表
//      billingTaxId：企业税号
//      token： token值
export const RedLetterInfoProduct = async (products, billingTaxId, token) => {
    let productList = []
    for (let i = 0; i < products.length; i++) {
        let product = {}
        let rates = ''
        rates = await getRates(products[i].SL, token, billingTaxId, products[i].SPBM)
        let data = ProcessingStrings(products[i].XMDJ, products[i].XMSL,
            products[i].XMJE, products[i].YHZCBS, products[i].LSLBS)
        products[i].XMDJ = data.price
        products[i].XMSL = data.number
        products[i].XMJE = data.amount
        products[i].YHZCBS = data.preferentialFlag
        products[i].LSLBS = data.zeroRateFlag
        let number = ''
        if (products[i].XMSL !== null && products[i].XMSL !== '') {
            number = -Math.abs(products[i].XMSL)
        }
        let amount = ''
        if (products[i].XMJE !== null && products[i].XMJE !== '') {
            amount = -Math.abs(products[i].XMJE)
        }
        if (products[i].FPHXZ === '0') {
            product = {
                productCheck: false,
                productName: products[i].XMMC,
                productCode: products[i].SPBM,
                specification: products[i].GGXH,
                units: products[i].DW,
                number: number,
                price: products[i].XMDJ,
                amount: amount,
                taxRate: products[i].SL,
                taxAmount: -Math.abs(products[i].SE),
                taxRates: rates,
                totalAmountRate: -Math.abs((parseFloat(products[i].XMJE) + parseFloat(products[i].SE))),
                preferentialFlag: products[i].YHZCBS,
                preferentialType: products[i].ZZSTSGL,
                zeroRateFlag: products[i].LSLBS,
                rowStatus: 0
            }
        }
        if (products[i].FPHXZ === '1') {
            product = {
                productCheck: false,
                productName: products[i].XMMC,
                productCode: products[i].SPBM,
                specification: products[i].GGXH,
                units: products[i].DW,
                number: number,
                price: products[i].XMDJ,
                amount: amount,
                taxRate: products[i].SL,
                taxAmount: -Math.abs(products[i].SE),
                taxRates: rates,
                totalAmountRate: -Math.abs((parseFloat(products[i].XMJE) + parseFloat(products[i].SE))),
                preferentialFlag: products[i].YHZCBS,
                preferentialType: products[i].ZZSTSGL,
                zeroRateFlag: products[i].LSLBS,
                rowStatus: 2
            }
        }
        if (products[i].FPHXZ === '2') {
            product = {
                productCheck: false,
                productName: products[i].XMMC,
                productCode: products[i].SPBM,
                specification: products[i].GGXH,
                units: products[i].DW,
                number: number,
                price: products[i].XMDJ,
                amount: amount,
                taxRate: products[i].SL,
                taxAmount: -Math.abs(products[i].SE),
                taxRates: rates,
                totalAmountRate: -Math.abs((parseFloat(products[i].XMJE) + parseFloat(products[i].SE))),
                preferentialFlag: products[i].YHZCBS,
                preferentialType: products[i].ZZSTSGL,
                zeroRateFlag: products[i].LSLBS,
                rowStatus: 1
            }
        }
        productList = productList.concat(product)
    }
    return productList
}

// 红字信息表列表界面点击提交按钮转化产品列表进行提交
// 参数: invoiceInfo： 红字信息
export const RedInvoiceSubmit = (invoiceInfo) => {
    for (let i = 0; i < invoiceInfo.COMMON_FPKJ_XMXXS.COMMON_FPKJ_XMXX.length; i++) {
        invoiceInfo.COMMON_FPKJ_XMXXS.COMMON_FPKJ_XMXX[i].SE = Math.abs(invoiceInfo.COMMON_FPKJ_XMXXS.COMMON_FPKJ_XMXX[i].SE)
        invoiceInfo.COMMON_FPKJ_XMXXS.COMMON_FPKJ_XMXX[i].XMJE = Math.abs(invoiceInfo.COMMON_FPKJ_XMXXS.COMMON_FPKJ_XMXX[i].XMJE)
        let number = null
        if (invoiceInfo.COMMON_FPKJ_XMXXS.COMMON_FPKJ_XMXX[i].XMSL !== null && invoiceInfo.COMMON_FPKJ_XMXXS.COMMON_FPKJ_XMXX[i].XMSL.XMSL !== '') {
            invoiceInfo.COMMON_FPKJ_XMXXS.COMMON_FPKJ_XMXX[i].XMSL = Math.abs(invoiceInfo.COMMON_FPKJ_XMXXS.COMMON_FPKJ_XMXX[i].XMSL)
        } else {
            invoiceInfo.COMMON_FPKJ_XMXXS.COMMON_FPKJ_XMXX[i].XMSL = number
        }
    }
    return invoiceInfo
}

// 获取产品的可用税率
// 参数： productRate：当前产品税率
//       tokenL：token值
//       entTaxId：企业税号
//       productCode：产品编码
export const getRates = async (productRate, token, entTaxId, productCode) => {
    let rates = ''
    const url = '/sys/entInfo/rates'
    let res = await AxiosRequestData(
        url,
        token,
        {
            entTaxId: entTaxId,
            taxCateCode: productCode
        },
        false,
        2
    )
    if (res) {
        let rate = productRate.toString()
        if (res.code === 0) {
            if (res.data.length > 0) {
                for (let i = 0; i < res.data.length; i++) {
                    if (rate.indexOf(res.data[i].toString()) === -1) {
                        rate = rate + ',' + res.data[i]
                    }
                }
            }
            rates = rate
        } else {
            rates = rate
        }
        if (rates.length > 0) {
            return rates.split(',')
        } else {
            return rates
        }
    }
}

// 处理产品列表信息中单价、数量、总金额转换为数值
// 参数 number 数量
//     price 单价
//     amount 总金额
export const ProcessingStrings = (price, number, amount, preferentialFlag, zeroRateFlag) => {
    let data = {
        price: '',
        number: '',
        amount: '',
        preferentialFlag: 0,
        zeroRateFlag: null
    }
    let priceInfo = parseFloat(price)
    if (!isNaN(priceInfo)) {
        data.price = price.toString()
    }
    let numberInfo = parseFloat(number)
    if (!isNaN(numberInfo)) {
        data.number = number.toString()
    }
    let amountInfo = parseFloat(amount)
    if (!isNaN(amountInfo)) {
        data.amount = amount.toString()
    }
    data.preferentialFlag = parseInt(preferentialFlag)
    if (zeroRateFlag !== '' && zeroRateFlag !== null && !isNaN(zeroRateFlag)) {
        data.zeroRateFlag = parseInt(zeroRateFlag)
    }
    return data
}
