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

import com.yem.wm.utils.YEM;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.entity.DynamicObjectCollection;
import kd.bos.entity.plugin.AbstractOperationServicePlugIn;
import kd.bos.entity.plugin.PreparePropertysEventArgs;
import kd.bos.entity.plugin.args.EndOperationTransactionArgs;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import kd.bos.servicehelper.operation.SaveServiceHelper;

/**
 * @author xwudd
 * @Description 也是提交插件  名称写错了
 * @date 11:05
 **/
public class DeliverNoticeAuditOp extends AbstractOperationServicePlugIn {
    @Override
    public void endOperationTransaction(EndOperationTransactionArgs e) {
        super.endOperationTransaction(e);
        DynamicObject[] dataEntities = e.getDataEntities();
        for(DynamicObject dataEntitie : dataEntities){
            dataEntitie = BusinessDataServiceHelper.loadSingle(dataEntitie.getPkValue(),dataEntitie.getDynamicObjectType().getName());
            createShipCounterDeliver(dataEntitie);
        }
    }

    /**
     * @description 生成出运对应发货信息
     * @author xwudd
     * @param[1] dataEntitie
     * return
     * @time 2023/11/2 14:13
     */

    public void createShipCounterDeliver(DynamicObject dataEntitie){
        DynamicObjectCollection materialInfos = dataEntitie.getDynamicObjectCollection("yem_es_materialinfo");
        for(DynamicObject materialInfo : materialInfos){
            DynamicObject shippingDetails = BusinessDataServiceHelper.loadSingle(materialInfo.getLong("yem_sourcebillid_d"), materialInfo.getString("yem_sourcebilltype_e"));
            if(YEM.isNotEmpty(shippingDetails)){
                long sourceEntryId = materialInfo.getLong("yem_sourceentryid");
                DynamicObjectCollection shipMaterialInfos = shippingDetails.getDynamicObjectCollection("yem_es_materialinfo");
                for(DynamicObject shipMaterialInfo : shipMaterialInfos){
                    long id = shipMaterialInfo.getLong("id");
                    if(id == sourceEntryId){
                        DynamicObjectCollection shipDeliverNotice = shipMaterialInfo.getDynamicObjectCollection("yem_delivernotice");
                        DynamicObject addNew = shipDeliverNotice.addNew();
                        addNew.set("yem_delivernoticeno",dataEntitie.get("billno"));//发货通知单号
                        addNew.set("yem_isgift_d",materialInfo.get("yem_isgift"));//是否赠品
                        addNew.set("yem_businesssort_d",materialInfo.get("yem_businesssort"));//业务分类
                        addNew.set("yem_material_d",materialInfo.get("yem_materiel"));//物料编码
                        addNew.set("yem_unit_d",materialInfo.get("yem_unit"));//计量单位
                        addNew.set("yem_qty_m",materialInfo.get("yem_qty"));//数量
                        addNew.set("yem_priceandtax_d",materialInfo.get("yem_priceandtax"));//销售单价（价格条款）
                        addNew.set("yem_price_d",materialInfo.get("yem_price"));//单价
                        addNew.set("yem_amountandtax_d",materialInfo.get("yem_amountandtax"));//销售金额（价格条款）
                        addNew.set("yem_curamountandtax_d",materialInfo.get("yem_curamountandtax"));//
                        addNew.set("yem_taxrate_d",materialInfo.get("yem_taxrate"));//税率(%)
                        addNew.set("yem_taxamount_d",materialInfo.get("yem_taxamount"));//税额
                        addNew.set("yem_curtaxamount_d",materialInfo.get("yem_curtaxamount"));//税额(本位币)
                        addNew.set("yem_warehouse_d",materialInfo.get("yem_warehouse"));//仓库
                        addNew.set("yem_location_d",materialInfo.get("yem_location"));//库位
                        addNew.set("yem_lotnumber_d",materialInfo.get("yem_lotnumber"));//批号
                        addNew.set("yem_baseunit_d",materialInfo.get("yem_baseunit"));//基本计量单位
                        addNew.set("yem_baseqty_d",materialInfo.get("yem_baseqty"));//基本数量
                        addNew.set("yem_saleorderno_d",shipMaterialInfo.get("yem_contractno"));//外销合同号
                        addNew.set("yem_materialentryid_d",id);//出运明细分录行id
                        addNew.set("yem_deliverentryid_d",materialInfo.get("id"));//来源发货分录Id
                    }
                }
                SaveServiceHelper.save(new DynamicObject[]{shippingDetails});
            }
        }
    }


}
