samuel025

BT Remove DO Receipt

Nov 28th, 2021
553
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package org.jleaf.erp.inv.bo.doreceipt;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. import javax.persistence.Query;
  7.  
  8. import org.jleaf.common.CommonExceptionConstants;
  9. import org.jleaf.core.BusinessFunction;
  10. import org.jleaf.core.BusinessTransaction;
  11. import org.jleaf.core.CoreException;
  12. import org.jleaf.core.DefaultBusinessTransaction;
  13. import org.jleaf.core.Dto;
  14. import org.jleaf.core.annotation.ErrorList;
  15. import org.jleaf.core.annotation.Info;
  16. import org.jleaf.core.annotation.InfoIn;
  17. import org.jleaf.core.annotation.InfoOut;
  18. import org.jleaf.core.dao.QueryBuilder;
  19. import org.jleaf.erp.inv.InventoryConstants;
  20. import org.jleaf.erp.inv.InventoryExceptionConstants;
  21. import org.jleaf.erp.inv.dao.BalanceDeliveryOrderItemDao;
  22. import org.jleaf.erp.inv.dao.DeliveryOrderReceiptDao;
  23. import org.jleaf.erp.inv.dao.DeliveryOrderReceiptItemDao;
  24. import org.jleaf.erp.inv.dao.DeliveryOrderReceiptProductDao;
  25. import org.jleaf.erp.inv.entity.BalanceDeliveryOrderItem;
  26. import org.jleaf.erp.inv.entity.DeliveryOrderReceiptItem;
  27. import org.jleaf.erp.inv.entity.DeliveryOrderReceiptProduct;
  28. import org.jleaf.util.Calc;
  29. import org.jleaf.util.GsonUtil;
  30. import org.jleaf.util.ValidationUtil;
  31. import org.springframework.beans.factory.annotation.Autowired;
  32. import org.springframework.beans.factory.annotation.Qualifier;
  33. import org.springframework.stereotype.Service;
  34.  
  35. /**
  36.  *
  37.  * @see http://jleaf.org:8181/browse/INVSVC-22
  38.  * @author Fredi, 26 Agustus 2014
  39.  *
  40.  * Process
  41.  * - validate input
  42.  * - val tenant login not admin
  43.  * - find do receipt
  44.  * - validate doc draft
  45.  * - get balance do item from do reff
  46.  * - per balance do item update status item to R
  47.  * - perbalance do item update : restore qtyIntReturn and qtyReturn from do receipt item
  48.  * - prepare remove do receipt
  49.  * - remove do receipt
  50.  * - remove do receipt item
  51.  * - remove do receipt product
  52.  *
  53.  */
  54. //@formatter:off
  55. @Service
  56. @InfoIn(value = {
  57.         @Info(name = "tenantLoginId", description = "Tenant login id", type = Long.class),
  58.         @Info(name = "userLoginId", description = "User login id", type = Long.class),
  59.         @Info(name = "roleLoginId", description = "User login id", type = Long.class),
  60.         @Info(name = "datetime", description = "Datetime", type = String.class),
  61.        
  62.         @Info(name = "id", description = "do Id", type = Long.class)
  63. })
  64. @InfoOut(value = {
  65.        
  66. })
  67. @ErrorList(errorKeys = {
  68.         CommonExceptionConstants.TENANT_LOGIN_CANT_USED,
  69.         CommonExceptionConstants.DATA_CANT_USE_DIFF_TENANT
  70. })
  71. //@formatter:on
  72. public class RemoveDeliveryOrderReceipt extends DefaultBusinessTransaction implements BusinessTransaction {
  73.  
  74.     @Autowired
  75.     @Qualifier("valTenantLoginCanUse")
  76.     BusinessFunction valTenantLoginCanUse;
  77.  
  78.     @Autowired
  79.     @Qualifier("findDeliveryOrderReceiptById")
  80.     BusinessFunction findDeliveryOrderReceiptById;
  81.  
  82.     @Autowired
  83.     @Qualifier("getBalanceDeliveryOrderItemListByDeliveryOrder")
  84.     BusinessFunction getBalanceDeliveryOrderItemListByDeliveryOrder;
  85.    
  86.     @Autowired
  87.     @Qualifier("findDeliveryOrderReceiptItemByDeliveryOrderItem")
  88.     BusinessFunction findDeliveryOrderReceiptItemByDeliveryOrderItem;
  89.    
  90.     @Autowired
  91.     DeliveryOrderReceiptDao deliveryOrderReceiptDao;
  92.    
  93.     @Autowired
  94.     DeliveryOrderReceiptItemDao deliveryOrderReceiptItemDao;
  95.    
  96.     @Autowired
  97.     DeliveryOrderReceiptProductDao deliveryOrderReceiptProductDao;
  98.  
  99.     @Autowired
  100.     BalanceDeliveryOrderItemDao balanceDeliveryOrderItemDao;
  101.    
  102.     @Override
  103.     public String getDescription() {
  104.         return "Remove delivery order receipt";
  105.     }
  106.  
  107.     @SuppressWarnings("unchecked")
  108.     @Override
  109.     public Dto prepare(Dto inputDto, Dto originalDto) throws Exception {
  110.         ValidationUtil.valDtoContainsKey(inputDto, "tenantLoginId");
  111.         ValidationUtil.valDtoContainsKey(inputDto, "userLoginId");
  112.         ValidationUtil.valDtoContainsKey(inputDto, "roleLoginId");
  113.         ValidationUtil.valDtoContainsKey(inputDto, "datetime");
  114.        
  115.         Long tenantLoginId = inputDto.getLong("tenantLoginId");
  116.         Long userLoginId = inputDto.getLong("userLoginId");
  117.         Long roleLoginId = inputDto.getLong("roleLoginId");
  118.         String datetime = inputDto.getString("datetime");
  119.        
  120.         // - validation: tenant login can use
  121.         Dto inputDtoForValTenantLoginCanUse = new Dto();
  122.         inputDtoForValTenantLoginCanUse.put("tenantLoginId", tenantLoginId);
  123.         inputDtoForValTenantLoginCanUse.put("userLoginId", userLoginId);
  124.         inputDtoForValTenantLoginCanUse.put("roleLoginId", roleLoginId);
  125.         valTenantLoginCanUse.execute(inputDto);
  126.  
  127.         ValidationUtil.valDtoContainsKey(inputDto, "id");
  128.        
  129.         Long id = inputDto.getLong("id");
  130.        
  131.         Dto deliveryOrderReceiptDto = findDeliveryOrderReceiptById.execute(new Dto().put("id", id));
  132.        
  133.         if (!deliveryOrderReceiptDto.getLong("tenantId").equals(inputDto.getLong("tenantLoginId"))) {
  134.             throw new CoreException(CommonExceptionConstants.DATA_CANT_CHANGE_CAUSE_TENANT, "Delivery Order Receipt");
  135.         }
  136.  
  137.         if (!InventoryConstants.DRAFT_TRANSACTION.equals(deliveryOrderReceiptDto.getString("statusDoc"))) {
  138.             throw new CoreException(InventoryExceptionConstants.DOC_ALREADY_SUBMITTED);
  139.         }
  140.        
  141.         // prepare update balance
  142.         List<Dto> updateBalanceDeliveryOrderItemList = new ArrayList<Dto>();
  143.        
  144.         // find balance do item
  145.         Dto outputGetBalanceDeliveryOrderItemListByDeliveryOrder = getBalanceDeliveryOrderItemListByDeliveryOrder.execute(new Dto()
  146.             .put("doId", deliveryOrderReceiptDto.get("refId")));
  147.         List<Dto> balanceDeliveryOrderItemList = outputGetBalanceDeliveryOrderItemListByDeliveryOrder.getList("balanceDeliveryOrderItemList");
  148.         for (Dto balanceDeliveryOrderItem : balanceDeliveryOrderItemList) {
  149.             balanceDeliveryOrderItem.put("statusItem", InventoryConstants.RELEASED_TRANSACTION);
  150.             this.prepareUpdateAudit(balanceDeliveryOrderItem, userLoginId, datetime);
  151.            
  152.             updateBalanceDeliveryOrderItemList.add(balanceDeliveryOrderItem);
  153.         }
  154.        
  155.         // prepare remove
  156.         inputDto.put("deliveryOrderReceiptDto", deliveryOrderReceiptDto);
  157.         inputDto.put("updateBalanceDeliveryOrderItemList", updateBalanceDeliveryOrderItemList);
  158.         inputDto.put("doId", deliveryOrderReceiptDto.getLong("refId"));
  159.         return null;
  160.     }
  161.  
  162.     @SuppressWarnings("unchecked")
  163.     @Override
  164.     public Dto process(Dto inputDto, Dto originalDto) throws Exception {
  165.         Dto deliveryOrderReceiptDto = inputDto.getDto("deliveryOrderReceiptDto");
  166.        
  167.         // update balance do item
  168.         List<Dto> updateBalanceDeliveryOrderItemList = inputDto.getList("updateBalanceDeliveryOrderItemList");
  169.         for (Dto balanceDeliveryOrderItemDto : updateBalanceDeliveryOrderItemList) {
  170.             BalanceDeliveryOrderItem balanceDeliveryOrderItem = GsonUtil.fromDto(balanceDeliveryOrderItemDto, BalanceDeliveryOrderItem.class);
  171.             balanceDeliveryOrderItem = balanceDeliveryOrderItemDao.merge(balanceDeliveryOrderItem.getDeliveryOrderItemId(), balanceDeliveryOrderItem);
  172.         }
  173.        
  174.         // update status sl_so, sl_so_balance_item, sl_so_balance_invoice
  175.         QueryBuilder builder = new QueryBuilder();
  176.         builder.add(" SELECT COALESCE(COUNT(1), 0) FROM sl_update_status_so_for_remove_do_receipt(:doId, :userId, :datetime)");
  177.        
  178.         Query q = deliveryOrderReceiptProductDao.createNativeQuery(builder.toString());
  179.         q.setParameter("doId", inputDto.get("doId"));
  180.         q.setParameter("userId", inputDto.get("userLoginId"));
  181.         q.setParameter("datetime", inputDto.get("datetime"));
  182.        
  183.         Object obj = q.getSingleResult();
  184.        
  185.         // remove do receipt product
  186.         Query queryDeleteDoReceiptProduct = deliveryOrderReceiptProductDao.createNativeQuery(new QueryBuilder()
  187.             .add(" DELETE FROM "+DeliveryOrderReceiptProduct.TABLE_NAME+" ")
  188.             .add(" WHERE EXISTS ( ")
  189.             .add("      SELECT 1 ")
  190.             .add("      FROM "+DeliveryOrderReceiptItem.TABLE_NAME+"  ")
  191.             .add("      WHERE "+DeliveryOrderReceiptItem.TABLE_NAME+".do_receipt_item_id = "+DeliveryOrderReceiptProduct.TABLE_NAME+".do_receipt_item_id ")
  192.             .add("          AND "+DeliveryOrderReceiptItem.TABLE_NAME+".do_receipt_id = :doReceiptId ")
  193.             .add(" ) ")
  194.             .toString());
  195.         queryDeleteDoReceiptProduct.setParameter("doReceiptId", deliveryOrderReceiptDto.getLong("id"));
  196.         queryDeleteDoReceiptProduct.executeUpdate();
  197.        
  198.         // remove do receipt item
  199.         Query queryDeleteDoReceiptItem = deliveryOrderReceiptProductDao.createNativeQuery(new QueryBuilder()
  200.             .add(" DELETE FROM "+DeliveryOrderReceiptItem.TABLE_NAME+" ")
  201.             .add(" WHERE do_receipt_id = :doReceiptId ")
  202.             .toString());
  203.         queryDeleteDoReceiptItem.setParameter("doReceiptId", deliveryOrderReceiptDto.getLong("id"));
  204.         queryDeleteDoReceiptItem.executeUpdate();
  205.        
  206.         // remove do receipt
  207.         deliveryOrderReceiptDao.remove(deliveryOrderReceiptDto.getLong("id"));
  208.        
  209.         return new Dto();
  210.     }
  211.  
  212. }
  213.  
RAW Paste Data