WarehouseReceiveAction.java

package com.mycim.webapp.actions.warehouse;

import com.fa.sesa.exception.Assert;
import com.fa.sesa.exception.Errors;
import com.fa.sesa.i18n.I18nUtils;
import com.fa.sesa.threadlocal.LocalContext;
import com.mycim.framework.utils.lang.StringUtils;
import com.mycim.valueobject.MessageIdList;
import com.mycim.valueobject.consts.AsmConst;
import com.mycim.valueobject.consts.ReferenceDetailNames;
import com.mycim.valueobject.consts.ReferenceFileConst;
import com.mycim.valueobject.inv.MaterialDO;
import com.mycim.valueobject.inv.value.WarehouseNames;
import com.mycim.valueobject.prp.Operation;
import com.mycim.valueobject.security.User;
import com.mycim.webapp.WebUtils;
import com.mycim.webapp.actions.AsmSetupAction;
import com.mycim.webapp.forms.WarehouseInfoForm;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 库房接收
 *
 * @author shijie.deng
 * @date 2019/8/28
 **/
public class WarehouseReceiveAction extends AsmSetupAction {

    @Override
    public ActionForward init(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                              HttpServletResponse response) throws Exception {
        WarehouseInfoForm theform = (WarehouseInfoForm) form;
        String warehouseId = WebUtils.getParameterUpperCase("warehouseId", request);
        warehouseId = StringUtils.isEmpty(warehouseId) ? theform.getInstanceId() : warehouseId;
        Assert.isFalse(StringUtils.isEmpty(warehouseId),
                       Errors.create().key(MessageIdList.ASM_MISSING_WAREHOUSE_ID).content("库房号不能为空!").build());

        Long facilityRrn = LocalContext.getFacilityRrn();
        Operation warehouse = warehouseService.getWarehouse(facilityRrn, warehouseId);
        Assert.isFalse(warehouse == null || warehouse.getInstanceRrn() <= 0,
                       Errors.create().key(MessageIdList.ASM_MISSING_WAREHOUSE).content("Warehouse is not exist!")
                             .build());

        theform.setInstanceId(warehouse.getInstanceId());
        theform.setNamedSpace(warehouse.getNamedSpace());
        theform.setInstanceDesc(warehouse.getInstanceDesc());
        return mapping.findForward(WarehouseNames.RECEIVE_TRANS);
    }

    public Map<String, String> addReceive(WarehouseInfoForm theform) throws Exception {
        Long facilityRrn = LocalContext.getFacilityRrn();
        String warehouseId = theform.getWarehouseId();
        Operation warehouse = warehouseService.getWarehouse(facilityRrn, warehouseId);

        String materialId = theform.getMaterialId();
        MaterialDO material = asmService.getAndCheckMaterial(facilityRrn, materialId);

        String lotNumber = theform.getLotNumber();
        Assert.isFalse(StringUtils.isEmpty(lotNumber),
                       Errors.create().key(MessageIdList.ASM_MISSING_LOT_NUMBER_ID).content("物料号不能为空!").build());

        Double receiptQty = theform.getReceiptQty();
        String userInfo = theform.getCheckUser();

        validateReceiptQty(receiptQty.toString());
        validateDateInfoForInventory(theform);

        User user = securityService.getUser(userInfo, facilityRrn);
        userInfo = StringUtils.isEmpty(user.getUserName()) ? userInfo : userInfo + " " + user.getUserName();

        warehouseService.checkWarehouseReceiveType(warehouse.getInstanceId(), material.getObjectSubtype());

        Map<String, String> result = buildReceiveInfo(theform, lotNumber, receiptQty.toString(), userInfo, material);
        return result;

    }

    private void validateReceiptQty(String receiptQty) {
        Assert.isFalse(StringUtils.isEmpty(receiptQty),
                       Errors.create().key(MessageIdList.ASM_MISSING_QUANTITY).content("The quantity cannot be empty!")
                             .build());

        BigDecimal n = new BigDecimal(receiptQty);
        Assert.isFalse(n.doubleValue() <= 0,
                       Errors.create().key(MessageIdList.ASM_QUANTITY_ILLEGAL).content("Quantity illegal!").build());
    }

    private Map<String, String> buildReceiveInfo(WarehouseInfoForm theform, String lotNumber, String receiptQty,
                                                 String userInfo, MaterialDO material) {
        Map<String, String> result = new HashMap<>();

        result.put(AsmConst.MATERIAL_RRN, Long.toString(material.getInstanceRrn()));
        result.put(AsmConst.MATERIAL_ID, material.getInstanceId());
        result.put(AsmConst.MATERIAL_DESC, material.getInstanceDesc() == null ? "" : material.getInstanceDesc());
        result.put(AsmConst.LOT_NUMBER, lotNumber);
        result.put(AsmConst.QTY, receiptQty);
        result.put(AsmConst.STORE_UOM, StringUtils.trimToEmpty(sysService.getRefFileValue(
                ReferenceDetailNames.UNIT_OF_MEASURE_FOR_BANK, material.getStoreUom(),
                ReferenceFileConst.DATA_1_VALUE)));
        result.put(AsmConst.SUPPLIER_ID, theform.getSupplierId());
        result.put(AsmConst.MANUFACTURER_ID, theform.getManufacturerId());
        result.put(AsmConst.EXPIRATION_DATE, theform.getExpirationDate());
        result.put(AsmConst.PRODUCTION_DATE, theform.getProductionDate());
        result.put(AsmConst.INCOMING_DATE, theform.getIncomingDate());
        result.put(AsmConst.CHECK_DATE, theform.getCheckDate());
        result.put(AsmConst.CHECK_USER, userInfo);
        result.put(AsmConst.CHECK_RESULT, theform.getCheckResult());
        result.put(AsmConst.COMMENTS, theform.getComments());

        return result;
    }

    public String receive(WarehouseInfoForm theform) throws Exception {
        String warehouseId = theform.getWarehouseId();
        List<Map<String, Object>> receiptList = theform.getReceiveList();
        Assert.isFalse(StringUtils.isEmpty(warehouseId) || receiptList == null || receiptList.isEmpty(),
                       Errors.create().content("Parameter missing, please login again!").build());

        Long facilityRrn = LocalContext.getFacilityRrn();
        String userId = LocalContext.getUserId();
        warehouseService.receiveMaterialToWarehouse(facilityRrn, userId, warehouseId, receiptList);
        return I18nUtils.getMessage(MessageIdList.ASM_RECEIVE_SUCCESS, "Material Acceptance Successful!");
    }

}