widana

Untitled

Jan 24th, 2017
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 8.87 KB | None | 0 0
  1. /**
  2.  *
  3.  */
  4. package org.jleaf.erp.inv.bo.returnnote;
  5.  
  6. import java.util.Iterator;
  7. import java.util.List;
  8. import java.util.Map.Entry;
  9.  
  10. import javax.persistence.Query;
  11.  
  12. import org.jleaf.erp.inv.InventoryConstants;
  13. import org.jleaf.erp.inv.InventoryExceptionConstants;
  14. import org.jleaf.erp.inv.dao.BalanceDeliveryOrderItemDao;
  15. import org.jleaf.erp.inv.dao.InventoryDao;
  16. import org.jleaf.erp.inv.dao.InventoryItemDao;
  17. import org.jleaf.erp.inv.dao.ProductBalanceStockDao;
  18. import org.jleaf.erp.inv.entity.BalanceDeliveryOrderItem;
  19. import org.jleaf.erp.inv.entity.Inventory;
  20. import org.jleaf.erp.inv.entity.InventoryItem;
  21. import org.jleaf.erp.inv.entity.ProductBalanceStock;
  22. import org.jleaf.erp.purch.dao.PoBalanceItemConsignmentSoldManualDao;
  23. import org.jleaf.erp.purch.entity.PoBalanceItemConsignmentSoldManual;
  24. import org.jleaf.common.CommonExceptionConstants;
  25. import org.jleaf.core.BusinessFunction;
  26. import org.jleaf.core.BusinessTransaction;
  27. import org.jleaf.core.CoreException;
  28. import org.jleaf.core.DefaultBusinessTransaction;
  29. import org.jleaf.core.Dto;
  30. import org.jleaf.core.annotation.ErrorList;
  31. import org.jleaf.core.annotation.Info;
  32. import org.jleaf.core.annotation.InfoIn;
  33. import org.jleaf.core.annotation.InfoOut;
  34. import org.jleaf.core.dao.QueryBuilder;
  35. import org.jleaf.util.Calc;
  36. import org.jleaf.util.GsonUtil;
  37. import org.slf4j.Logger;
  38. import org.slf4j.LoggerFactory;
  39. import org.springframework.beans.factory.annotation.Autowired;
  40. import org.springframework.beans.factory.annotation.Qualifier;
  41. import org.springframework.stereotype.Service;
  42.  
  43. //@formatter:off
  44. @Service("removeReturnNote")
  45. @InfoIn(value = {
  46.         @Info(name = "id", description = "inventory id", type = Long.class),
  47.         @Info(name = "tenantLoginId", description = "tenant login id", type = Long.class),
  48.         @Info(name = "userLoginId", description = "user login id", type = Long.class),
  49.         @Info(name = "roleLoginId", description = "role login id", type = Long.class),
  50.         @Info(name = "datetime", description = "datetime", type = String.class)
  51. })
  52. @InfoOut(value = {
  53. })
  54. @ErrorList(errorKeys = {
  55.         InventoryExceptionConstants.INVENTORY_ID_NOT_FOUND,
  56.         InventoryExceptionConstants.DOC_ALREADY_SUBMITTED,
  57.         CommonExceptionConstants.DATA_CANT_DELETE_CAUSE_TENANT,
  58.         CommonExceptionConstants.TENANT_LOGIN_CANT_USED
  59. })
  60. //@formatter:on
  61. public class RemoveReturnNoteForCland extends DefaultBusinessTransaction implements BusinessTransaction {
  62.  
  63.     private static Logger log = LoggerFactory.getLogger(RemoveReturnNoteForCland.class);
  64.    
  65.     @Autowired
  66.     private InventoryDao inventoryDao;
  67.  
  68.     @Autowired
  69.     private InventoryItemDao inventoryItemDao;
  70.  
  71.     @Autowired
  72.     private BalanceDeliveryOrderItemDao balanceDeliveryOrderItemDao;
  73.    
  74.     @Autowired
  75.     private ProductBalanceStockDao productBalanceStockDao;
  76.  
  77.     @Autowired
  78.     private PoBalanceItemConsignmentSoldManualDao poBalanceItemConsignmentSoldManualDao;
  79.    
  80.     @Autowired
  81.     @Qualifier("getInventoryItemListByInventory")
  82.     private BusinessFunction getInventoryItemListByInventory;
  83.  
  84.     @Autowired
  85.     @Qualifier("findInventoryById")
  86.     private BusinessFunction findInventoryById;
  87.  
  88.     @Autowired
  89.     @Qualifier("findBalanceDeliveryOrderItemById")
  90.     private BusinessFunction findBalanceDeliveryOrderItemById;
  91.    
  92.     @Autowired
  93.     @Qualifier("findProductBalanceStockByIndex")
  94.     private BusinessFunction findProductBalanceStockByIndex;
  95.    
  96.     @Autowired
  97.     @Qualifier("valTenantLoginCanUse")
  98.     private BusinessFunction valTenantLoginCanUse;
  99.  
  100.     @SuppressWarnings("unchecked")
  101.     @Override
  102.     public Dto prepare(Dto inputDto, Dto originalDto) throws Exception {
  103.         // Validation tenant login id can be use
  104.         Dto loginDto = new Dto();
  105.         loginDto.put("tenantLoginId", inputDto.getLong("tenantLoginId"));
  106.         loginDto.put("userLoginId", inputDto.getLong("userLoginId"));
  107.         loginDto.put("roleLoginId", inputDto.getLong("roleLoginId"));
  108.         valTenantLoginCanUse.execute(loginDto);
  109.  
  110.         // Get data header
  111.         Dto inventoryDto = findInventoryById.execute(new Dto().put("id", inputDto.getLong("id")));
  112.  
  113.         // Check TenantId in return note must be equal with tenantLoginId
  114.         if (!inventoryDto.getLong("tenantId").equals(inputDto.getLong("tenantLoginId"))) {
  115.             throw new CoreException(CommonExceptionConstants.DATA_CANT_DELETE_CAUSE_TENANT, "Return Note");
  116.         }
  117.  
  118.         if (!InventoryConstants.DRAFT_TRANSACTION.equals(inventoryDto.getString("statusDoc"))) {
  119.             throw new CoreException(InventoryExceptionConstants.DOC_ALREADY_SUBMITTED);
  120.         }
  121.  
  122.         inputDto.put("inventoryDto", inventoryDto);
  123.        
  124.         Dto itemListDto = getInventoryItemListByInventory.execute(new Dto().put("tenantId", inputDto.getLong("tenantLoginId")).put("inventoryId", inputDto.getLong("id")));
  125.         List<Dto> inventoryItemList = itemListDto.getList("inventoryItemList");
  126.         Dto balanceDeliveryOrderItemMap = new Dto();
  127.         if (inventoryItemList != null && !inventoryItemList.isEmpty()) {
  128.             for (Dto inventoryItemDto : inventoryItemList) {
  129.                 // Get data balance delivery order item, update status item
  130.                 Dto balanceDeliveryOrderItemDto = findBalanceDeliveryOrderItemById.execute(new Dto().put("deliveryOrderItemId", Long.valueOf(inventoryItemDto.get("refItemId").toString())));
  131.                
  132.                 if (!balanceDeliveryOrderItemMap.containsKey(balanceDeliveryOrderItemDto.get("deliveryOrderItemId").toString())) {
  133.                     Calc qtyIntReturnAfterReturn = new Calc(balanceDeliveryOrderItemDto.getDouble("qtyIntReturn")).subtract(Double.valueOf(inventoryItemDto.getString("qtyRealization")));
  134.                     Calc ratio = new Calc(balanceDeliveryOrderItemDto.getDouble("qtyIntDlv")).divide(balanceDeliveryOrderItemDto.getDouble("qtyDlv"));
  135.                     Calc qtyReturnAfterReturn = qtyIntReturnAfterReturn.multiply(ratio);
  136.                    
  137.                     balanceDeliveryOrderItemDto.put("qtyIntReturn", qtyIntReturnAfterReturn.doubleValue());
  138.                     balanceDeliveryOrderItemDto.put("qtyReturn", qtyReturnAfterReturn.doubleValue());
  139.                     balanceDeliveryOrderItemDto.put("statusItem", InventoryConstants.RELEASED_TRANSACTION);
  140.                     this.prepareUpdateAudit(balanceDeliveryOrderItemDto, inputDto.getLong("userLoginId"), inputDto.getString("datetime"));
  141.                     balanceDeliveryOrderItemMap.put(balanceDeliveryOrderItemDto.get("deliveryOrderItemId").toString(), balanceDeliveryOrderItemDto);
  142.                 }
  143.                
  144.             }
  145.         }
  146.        
  147.         inputDto.put("balanceDeliveryOrderItemMap", balanceDeliveryOrderItemMap);
  148.         inputDto.put("inventoryItemList", inventoryItemList);
  149.        
  150.         return null;
  151.     }
  152.  
  153.     @SuppressWarnings({ "rawtypes", "unchecked" })
  154.     @Override
  155.     public Dto process(Dto inputDto, Dto originalDto) throws Exception {
  156.  
  157.         Dto inventoryDto = inputDto.getDto("inventoryDto");
  158.         List<Dto> inventoryItemList = inputDto.getList("inventoryItemList");
  159.  
  160.         QueryBuilder updatePurchaseOrderConsigmentSoldManual = new QueryBuilder();
  161.         updatePurchaseOrderConsigmentSoldManual.add(" UPDATE  ")
  162.             .add(PoBalanceItemConsignmentSoldManual.TABLE_NAME).add(" A ")
  163.                 .add(" SET status_item = :RELEASED ")
  164.             .add(" FROM ").add(InventoryItem.TABLE_NAME).add(" B ")
  165.             .add(" JOIN ").add(Inventory.TABLE_NAME).add(" C ON B.inventory_id = C.inventory_id ")
  166.             .add(" WHERE C.inventory_id = :inventoryId ")
  167.                 .add(" AND C.doc_type_id = :returnNoteDocumentId ")
  168.                 .add(" AND B.ref_doc_type_id = :deliveryDocumentId ")
  169.                 .add(" AND B.ref_id = A.do_id ")
  170.                 .add(" AND B.ref_item_id = A.do_item_id ");
  171.        
  172.         Query queryUpdatePurchaseOrderConsigmentSoldManual = poBalanceItemConsignmentSoldManualDao.createNativeQuery(updatePurchaseOrderConsigmentSoldManual.toString());
  173.        
  174.         queryUpdatePurchaseOrderConsigmentSoldManual.setParameter("inventoryId",inventoryDto.getLong("id"));
  175.         queryUpdatePurchaseOrderConsigmentSoldManual.setParameter("returnNoteDocumentId", InventoryConstants.DOCUMENT_DELIVERY_ORDER);
  176.         queryUpdatePurchaseOrderConsigmentSoldManual.setParameter("deliveryDocumentId", InventoryConstants.DOCUMENT_RETURN_NOTE);
  177.         queryUpdatePurchaseOrderConsigmentSoldManual.setParameter("RELEASED", InventoryConstants.RELEASED_TRANSACTION);
  178.         queryUpdatePurchaseOrderConsigmentSoldManual.executeUpdate();
  179.         poBalanceItemConsignmentSoldManualDao.flush();
  180.  
  181.         inventoryDao.remove(inventoryDto.getLong("id"));
  182.        
  183.         for (Dto inventoryItemDto : inventoryItemList) {
  184.             inventoryItemDao.remove(Long.valueOf(inventoryItemDto.get("id").toString()));
  185.         }
  186.        
  187.         Dto balanceDeliveryOrderItemMap = inputDto.getDto("balanceDeliveryOrderItemMap");
  188.         Iterator iteratorBalanceDeliveryOrderItem = balanceDeliveryOrderItemMap.entrySet().iterator();
  189.         while (iteratorBalanceDeliveryOrderItem.hasNext()) {
  190.             Entry entry = (Entry)iteratorBalanceDeliveryOrderItem.next();
  191.             Dto balanceDeliveryOrderItemToUpdateDto = (Dto)entry.getValue();
  192.             BalanceDeliveryOrderItem balanceDeliveryOrderItem = GsonUtil.fromDto(balanceDeliveryOrderItemToUpdateDto, BalanceDeliveryOrderItem.class);
  193.             balanceDeliveryOrderItem = balanceDeliveryOrderItemDao.merge(balanceDeliveryOrderItemToUpdateDto.getLong("deliveryOrderItemId"), balanceDeliveryOrderItem);
  194.         }
  195.        
  196.         return null;
  197.     }
  198.  
  199.     public String getDescription() {
  200.         return "remove return note";
  201.     }
  202.  
  203. }
Advertisement
Add Comment
Please, Sign In to add comment