package com.yem.wm.im.delivernotice.utils;

import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;

import java.math.BigDecimal;

/**
 * @author ljw
 * @date 2024/5/15 11:53
 * @description AmountCalculation
 */
public class AmountCalculation {
    public static void Amount(DynamicObject dataEntitys) {
        /**
         * 单价 = 含税单价 / ( 1 + 税率(%) / 100 )
         */
        DynamicObjectCollection dynamicObjectCollection = dataEntitys.getDynamicObjectCollection("yem_es_materialinfo");
        if (dynamicObjectCollection.size() > 0) {
            for (DynamicObject dataEntity : dynamicObjectCollection) {
                //含税单价
                BigDecimal yemPriceandtax = dataEntity.getBigDecimal("yem_priceandtax");
                //税率
                BigDecimal yemTaxrate = dataEntity.getBigDecimal("yem_taxrate");

//                if (yemPriceandtax.compareTo(BigDecimal.ZERO) != 0 && yemTaxrate.compareTo(BigDecimal.ZERO) != 0) {
                //税率(%) / 100
                BigDecimal subtracts = yemTaxrate.divide(new BigDecimal(100), 10, BigDecimal.ROUND_DOWN);
                // 1 + 税率(%)/ 100
                BigDecimal adds = new BigDecimal(1).add(subtracts);
                //单价
                BigDecimal yem_price = yemPriceandtax.divide(adds, 10, BigDecimal.ROUND_DOWN);
                dataEntity.set("yem_price", yem_price);
//                }
                /**
                 * 价税合计 = 含税单价 * 数量
                 */
                //数量
                BigDecimal yemQty = dataEntity.getBigDecimal("yem_qty");
                //价税合计
                BigDecimal yem_amountandtax = yemQty.multiply(yemPriceandtax);
                dataEntity.set("yem_amountandtax", yem_amountandtax);
                /**
                 * 价税合计(本位币) = 价税合计 * 汇率
                 */
                //汇率
                BigDecimal yemExrate = dataEntitys.getBigDecimal("yem_exrate");
                //价税合计(本位币)
                BigDecimal yem_curamountandtax = yem_amountandtax.multiply(yemExrate);
                dataEntity.set("yem_curamountandtax", yem_curamountandtax);
                /**
                 *金额 = 价税合计/(1 + (税率(%) / 100))
                 */
                boolean yemEnteramt = dataEntitys.getBoolean("yem_enteramt");
                //金额
                BigDecimal yem_amt = BigDecimal.ZERO;
                if (!yemEnteramt) {
                    //税率(%) / 100
                    BigDecimal subtract = yemTaxrate.divide(new BigDecimal(100), 10, BigDecimal.ROUND_DOWN);
                    // 1 + 税率(%)/ 100
                    BigDecimal add = new BigDecimal(1).add(subtract);
                    //金额
                    yem_amt = yem_amountandtax.divide(add, 10, BigDecimal.ROUND_DOWN);
                    dataEntity.set("yem_amt", yem_amt);
                }
                /**
                 * 金额本位币 = 金额 * 汇率
                 */
                if (yemExrate.compareTo(BigDecimal.ZERO) != 0) {
                    //金额本位币
                    BigDecimal yem_locamt = yemExrate.multiply(yem_amt);
                    dataEntity.set("yem_locamt", yem_locamt);
                }
                /**
                 * 税额 = 价税合计 - 金额
                 */
                BigDecimal yem_taxamount = BigDecimal.ZERO;
                if (yem_amountandtax.compareTo(BigDecimal.ZERO) != 0 || yem_amt.compareTo(BigDecimal.ZERO) != 0) {
                    //税额
                    yem_taxamount = yem_amountandtax.subtract(yem_amt);
                    dataEntity.set("yem_taxamount", yem_taxamount);
                }
                /**
                 *税额(本位币) = 税额 * 汇率
                 */
                BigDecimal yem_curtaxamount = yem_taxamount.multiply(yemExrate);
                dataEntity.set("yem_curtaxamount", yem_curtaxamount);
            }
        }
    }

}
