Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package org.jleaf.erp.sls.bo.salesorder;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.UUID;
- import org.jleaf.core.*;
- import org.jleaf.core.dao.QueryBuilder;
- import org.jleaf.erp.sls.SalesConstants;
- import org.jleaf.erp.sls.SalesConstantsForSasa;
- import org.jleaf.erp.sls.SalesExceptionConstants;
- import org.jleaf.erp.sls.SalesExceptionConstantsForSasa;
- import org.jleaf.erp.sls.dao.SalesOrderCustomDao;
- import org.jleaf.erp.sls.dao.SalesOrderDao;
- import org.jleaf.erp.sls.dao.SalesOrderItemCustomDao;
- import org.jleaf.erp.sls.dao.SalesOrderItemDao;
- import org.jleaf.erp.inv.dao.ProductBalanceStockReservedDao;
- import org.jleaf.erp.inv.dao.LogProductBalanceStockReservedDao;
- import org.jleaf.erp.sls.entity.SalesOrder;
- import org.jleaf.erp.sls.entity.SalesOrderCustom;
- import org.jleaf.erp.sls.entity.SalesOrderItem;
- import org.jleaf.erp.sls.entity.SalesOrderItemCustom;
- import org.jleaf.erp.inv.entity.ProductBalanceStockReserved;
- import org.jleaf.erp.inv.entity.LogProductBalanceStockReserved;
- import org.jleaf.common.CommonExceptionConstants;
- import org.jleaf.core.annotation.ErrorList;
- import org.jleaf.core.annotation.Info;
- import org.jleaf.core.annotation.InfoIn;
- import org.jleaf.core.annotation.InfoOut;
- import org.jleaf.util.DateUtil;
- import org.jleaf.util.DtoUtil;
- import org.jleaf.util.GsonUtil;
- import org.jleaf.validator.CommonBusinessValidator;
- import org.slf4j.Logger;
- import org.slf4j.LoggerFactory;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.beans.factory.annotation.Qualifier;
- import org.springframework.stereotype.Service;
- import javax.persistence.NoResultException;
- import javax.persistence.Query;
- /**
- *
- * @author Ivan Dec 05, 2019
- *
- */
- //@formatter:off
- @Service("submitSalesOrder")
- @InfoIn(value = {
- @Info(name = "id", description = "sales order id", type = Long.class),
- @Info(name = "version", description = "sales order version", type = Long.class),
- @Info(name = "workflowStatus", description = "Workflow Status", type = String.class),
- @Info(name = "tenantLoginId", description = "tenant login id", type = Long.class),
- @Info(name = "userLoginId", description = "user login id", type = Long.class),
- @Info(name = "roleLoginId", description = "role login id", type = Long.class),
- @Info(name = "datetime", description = "datetime", type = String.class)
- })
- @InfoOut(value = {
- @Info(name = "id", description = "so id", type = Long.class)
- })
- @ErrorList(errorKeys = {
- CommonExceptionConstants.TENANT_LOGIN_CANT_USED,
- CommonExceptionConstants.DATA_CANT_CHANGE_CAUSE_TENANT,
- SalesExceptionConstants.SALES_ORDER_ID_NOT_FOUND,
- SalesExceptionConstants.SALES_ORDER_MUST_HAVE_AT_LEAST_ONE_DETAIL_ITEM
- })
- //@formatter:on
- public class SubmitSalesOrderForSasa extends DefaultBusinessTransaction implements BusinessTransaction {
- private static final Logger log = LoggerFactory.getLogger(SubmitSalesOrderForSasa.class);
- @Autowired
- SalesOrderDao salesOrderDao;
- @Autowired
- SalesOrderCustomDao salesOrderCustomDao;
- @Autowired
- ProductBalanceStockReservedDao productBalanceStockReservedDao;
- @Autowired
- LogProductBalanceStockReservedDao logProductBalanceStockReservedDao;
- @Autowired
- SalesOrderItemDao salesOrderItemDao;
- @Autowired
- SalesOrderItemCustomDao salesOrderItemCustomDao;
- @Autowired
- @Qualifier("findSalesOrderById")
- BusinessFunction findSalesOrderById;
- @Autowired
- @Qualifier("valTenantLoginCanUse")
- BusinessFunction valTenantLoginCanUse;
- @Autowired
- @Qualifier("getSalesOrderItemListBySalesOrder")
- BusinessFunction getSalesOrderItemListBySalesOrder;
- @Autowired
- @Qualifier("getSalesOrderItemPurchasingListBySalesOrderItem")
- BusinessFunction getSalesOrderItemPurchasingListBySalesOrderItem;
- @Autowired
- @Qualifier("findProductMasterPriceByProductForSasa")
- BusinessFunction findProductMasterPriceByProductForSasa;
- @Autowired
- @Qualifier("findSalesOrderItemCustomById")
- BusinessFunction findSalesOrderItemCustomById;
- @Autowired
- @Qualifier("findSalesOrderItemById")
- BusinessFunction findSalesOrderItemById;
- @Autowired
- @Qualifier("findPartnerById")
- BusinessFunction findPartnerById;
- @Autowired
- @Qualifier("valOverPlafonCustomerBySOId")
- BusinessFunction valOverPlafonCustomerBySOId;
- @Override
- public String getDescription() {
- return "submit sales order";
- }
- @SuppressWarnings("unchecked")
- @Override
- public Dto prepare(Dto inputDto, Dto originalDto) throws Exception {
- // Validation tenant login id can be use
- log.info("START EXECUTE BT SUBMIT SALES ORDER>>>");
- Dto loginDto = new Dto();
- loginDto.put("tenantLoginId", inputDto.getLong("tenantLoginId"));
- loginDto.put("userLoginId", inputDto.getLong("userLoginId"));
- loginDto.put("roleLoginId", inputDto.getLong("roleLoginId"));
- valTenantLoginCanUse.execute(loginDto);
- Dto salesOrderDto = findSalesOrderById.execute(inputDto);
- log.debug("salesOrderDto>>>"+salesOrderDto);
- int termOfPayment = salesOrderDto.getInteger("termOfPayment");
- // validate version
- if(! salesOrderDto.getLong("version").equals(inputDto.get("version"))){
- throw new CoreException(SalesExceptionConstants.DOCUMENT_VERSION_NOT_UPTODATE);
- }
- // validation draft document
- if (!SalesConstants.DRAFT_TRANSACTION.equals(salesOrderDto.getString("statusDoc"))) {
- throw new CoreException(SalesExceptionConstants.DOC_ALREADY_SUBMITTED);
- }
- // validasi tanggal dokumen <= tanggal hari ini
- CommonBusinessValidator.valCompareDate(salesOrderDto.getString("docDate"), DateUtil.dateNow(), CommonBusinessValidator.COMPARE_LESS_EQUAL,
- "Doc Date For Submit", "Today");
- if (!salesOrderDto.getLong("tenantId").equals(inputDto.getLong("tenantLoginId"))) {
- throw new CoreException(CommonExceptionConstants.DATA_CANT_CHANGE_CAUSE_TENANT, "Sales Order");
- }
- Dto partnerBillToDto = findPartnerById.execute(new Dto().put("id", salesOrderDto.getLong("partnerBillToId")));
- log.debug("partnerBillToDto>>>"+partnerBillToDto);
- //validasi jika partnerBillTo ="P" maka tidak bisa add Sales Order
- if (SalesConstantsForSasa.POOR.equals(partnerBillToDto.getString("rank"))) {
- throw new CoreException(SalesExceptionConstantsForSasa.CANNOT_SUBMIT_SALES_ORDER_WITH_CUSTOMER_BILL_TO_RANK_P, partnerBillToDto.getString("name"));
- }
- //validasi verifikasi NIK
- log.info("START VALIDATION OF VERIFIED NIK>>>");
- if (termOfPayment != 0){
- String verifiedNik = GeneralConstants.NO;
- QueryBuilder builder = new QueryBuilder();
- builder.add("SELECT flag_verified_nik ")
- .add(" FROM ").add(SalesConstantsForSasa.TABLE_PARTNER_CUSTOM).add(" A ")
- .add(" WHERE A.partner_id = :partnerId ");
- Query queryVerifiedNik = salesOrderDao.createNativeQuery(builder.toString());
- queryVerifiedNik.setParameter("partnerId", salesOrderDto.getLong("partnerId"));
- Object result = null;
- log.info("result of flag verified nik: "+result);
- try {
- result = queryVerifiedNik.getSingleResult();
- if(result != null) {
- verifiedNik = result.toString();
- }
- } catch (NoResultException e) {}
- if (verifiedNik.equals(GeneralConstants.NO)) {
- throw new CoreException(SalesExceptionConstantsForSasa.PARTNER_NIK_NOT_VERIVIED);
- }
- }
- log.info("END VALIDATION OF VERIFIED NIK>>>");
- log.info("START CALLING BF getSalesOrderItemListBySalesOrder >>> ");
- //validasi item SO minimal 1
- Dto itemListDto = getSalesOrderItemListBySalesOrder.execute(new Dto().put("tenantId", inputDto.getLong("tenantLoginId")).put("soId",
- inputDto.getLong("id")));
- List<Dto> salesOrderItemList = itemListDto.getList("salesOrderItemList");
- if (salesOrderItemList == null || salesOrderItemList.isEmpty()) {
- throw new CoreException(SalesExceptionConstants.SALES_ORDER_MUST_HAVE_AT_LEAST_ONE_DETAIL_ITEM);
- }
- log.info("END CALLING BF getSalesOrderItemListBySalesOrder >>>> ");
- List<Dto> soItemList = new ArrayList<Dto>();
- List<Dto> salesOrderItemCustomList = new ArrayList<Dto>();
- log.info("salesOrderItemList: "+salesOrderItemList);
- log.debug("version: "+inputDto.getLong("version"));
- Double totalSOAmount = 0D;
- log.info("START PREPARE DATA FOR SO ITEM>>>");
- for (Dto salesOrderItemDto : salesOrderItemList) {
- Dto outputSoItemDto = findSalesOrderItemById.execute(new Dto().put("id", salesOrderItemDto.getLong("id")));
- totalSOAmount = totalSOAmount + outputSoItemDto.getDouble("nettItemAmount");
- log.info("totalSOAmount: "+totalSOAmount);
- //outputSoItemDto.put("version", inputDto.getLong("version"));
- log.debug("version outputSoItemDto: "+outputSoItemDto.getLong("version"));
- this.prepareUpdateAudit(outputSoItemDto, inputDto.getLong("userLoginId"), inputDto.getString("datetime"));
- log.debug("outputSoItemDto: "+outputSoItemDto);
- log.info("START CALLING BF FindProductMasterPriceByProductForSasa >> Product Id: "+salesOrderItemDto.getLong("productId"));
- Dto inputFindProductMasterPriceByProductForSasa = new Dto();
- inputFindProductMasterPriceByProductForSasa.put("id", salesOrderItemDto.getLong("productId"));
- inputFindProductMasterPriceByProductForSasa.put("tenantId", inputDto.getLong("tenantLoginId"));
- inputFindProductMasterPriceByProductForSasa.put("partnerId", salesOrderDto.getLong("partnerId"));
- inputFindProductMasterPriceByProductForSasa.put("ouId", salesOrderDto.getLong("ouId"));
- inputFindProductMasterPriceByProductForSasa.put("docDate", salesOrderDto.getLong("docDate"));
- log.debug("inputFindProductMasterPriceByProductForSasa: "+inputFindProductMasterPriceByProductForSasa);
- Dto outputFindProductMasterPriceByProductForSasa = findProductMasterPriceByProductForSasa.execute(inputFindProductMasterPriceByProductForSasa);
- log.debug("outputFindProductMasterPriceByProductForSasa: "+outputFindProductMasterPriceByProductForSasa);
- log.info("END CALLING BF FindProductMasterPriceByProductForSasa >>>");
- Dto outputSOItemCustomDto = findSalesOrderItemCustomById.execute(new Dto().put("id", salesOrderItemDto.getLong("id")));
- outputSOItemCustomDto.put("masterSellPrice", outputFindProductMasterPriceByProductForSasa.get("masterSellPrice"));
- outputSOItemCustomDto.put("masterMinSellPrice", outputFindProductMasterPriceByProductForSasa.get("masterMinSellPrice"));
- this.prepareUpdateAudit(outputSOItemCustomDto, inputDto.getLong("userLoginId"), inputDto.getString("datetime"));
- log.debug("outputSOItemCustomDto: "+outputSOItemCustomDto);
- soItemList.add(outputSoItemDto);
- salesOrderItemCustomList.add(outputSOItemCustomDto);
- }
- log.info("END PREPARE DATA FOR SO ITEM>>>");
- log.debug("salesOrderItemCustomList: "+salesOrderItemCustomList);
- log.debug("salesOrderItemList: "+salesOrderItemList);
- log.debug("soItemList: "+soItemList);
- log.debug("totalSOAmount: "+totalSOAmount);
- if(totalSOAmount.equals(Double.valueOf("0"))) {
- throw new CoreException(SalesExceptionConstantsForSasa.TOTAL_SALES_ORDER_AMOUNT_MUST_NOT_ZERO);
- }
- log.info("START Over Plafon Validation>>>");
- // Validasi overplafon atas customer bill to SO ( hanya divalidasi untuk kasus cara bayar SO = CREDIT )
- valOverPlafonCustomerBySOId.execute(new Dto().put("soId", inputDto.getLong("id")));
- log.info("END Over Plafon Validation>>>");
- salesOrderDto.put("statusDoc", SalesConstants.IN_PROGRESS_TRANSACTION);
- salesOrderDto.put("workflowStatus", inputDto.get("workflowStatus"));
- salesOrderDto.put("version", inputDto.getLong("version"));
- this.prepareUpdateAudit(salesOrderDto, inputDto.getLong("userLoginId"), inputDto.getString("datetime"));
- inputDto.put("salesOrderDto", salesOrderDto);
- inputDto.put("salesOrderItemCustomList", salesOrderItemCustomList);
- inputDto.put("salesOrderItemList", soItemList);
- log.info("inputDto before process: "+inputDto);
- log.debug("inputDto process: "+inputDto);
- log.info("END EXCEUTE BT SUBMIT SALES ORDER<<<");
- return null;
- }
- @SuppressWarnings("unchecked")
- @Override
- public Dto process(Dto inputDto, Dto originalDto) throws Exception {
- Long tenantId = inputDto.getLong("tenantLoginId");
- Long userId = inputDto.getLong("userLoginId");
- String currDateTime = inputDto.getString("datetime");
- String sessionId = UUID.randomUUID().toString();
- log.debug("sessionId>>>"+sessionId);
- Long soId = inputDto.getLong("id");
- log.info("START UPDATE DATA SALES ORDER HEADER AND ITEM >>>");
- Dto salesOrderDto = inputDto.getDto("salesOrderDto");
- SalesOrder salesOrder = GsonUtil.fromDto(salesOrderDto, SalesOrder.class);
- salesOrder = salesOrderDao.merge(salesOrderDto.getLong("id"), salesOrder);
- List<Dto> salesOrderItemList = inputDto.getList("salesOrderItemList");
- log.debug("salesOrderItemList: "+salesOrderItemList);
- for (Dto salesOrderItemDto : salesOrderItemList) {
- log.debug("salesOrderItemDto: "+salesOrderItemDto);
- SalesOrderItem salesOrderItem = GsonUtil.fromDto(salesOrderItemDto, SalesOrderItem.class);
- salesOrderItem = salesOrderItemDao.merge(salesOrderItemDto.getLong("id"), salesOrderItem);
- }
- List<Dto> salesOrderItemCustomList = inputDto.getList("salesOrderItemCustomList");
- for (Dto salesOrderItemCustomDto : salesOrderItemCustomList) {
- SalesOrderItemCustom salesOrderItemCustom = GsonUtil.fromDto(salesOrderItemCustomDto, SalesOrderItemCustom.class);
- salesOrderItemCustom = salesOrderItemCustomDao.merge(salesOrderItemCustomDto.getLong("id"), salesOrderItemCustom);
- }
- log.info("END UPDATE DATA SALES ORDER HEADER AND ITEM <<<");
- log.info("START EXECUTE FUNC f_update_and_insert_balance_stock_reserved_for_so >>>");
- QueryBuilder builderFunction = new QueryBuilder();
- builderFunction.add(" SELECT f_update_and_insert_balance_stock_reserved_for_so(:sessionId, :soId, :tenantId, :dateTime, :userId) ");
- Query queryFunction = productBalanceStockReservedDao.createNativeQuery(builderFunction.toString());
- queryFunction.setParameter("sessionId", sessionId);
- queryFunction.setParameter("soId", soId);
- queryFunction.setParameter("tenantId", tenantId);
- queryFunction.setParameter("dateTime", currDateTime);
- queryFunction.setParameter("userId", userId);
- Object returnFunction = queryFunction.getSingleResult();
- log.debug("returnFunction {}", returnFunction);
- log.info("END EXECUTE FUNC f_update_and_insert_balance_stock_reserved_for_so <<<");
- return new Dto().put("id", salesOrder.getId());
- }
- }
Add Comment
Please, Sign In to add comment