Advertisement
Guest User

Untitled

a guest
Jan 22nd, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.85 KB | None | 0 0
  1. public Dto execute(Dto inputDto) throws Exception {
  2.         ValidationUtil.valDtoContainsKey(inputDto, "soId");
  3.         ValidationUtil.valDtoContainsKey(inputDto, "datetime");
  4.         ValidationUtil.valDatetime(inputDto, "datetime");
  5.  
  6.         Long soId = inputDto.getLong("soId");
  7.  
  8.         Dto outputDto = new Dto();
  9.  
  10.         //get type so
  11.         String typeSo = GeneralConstants.EMPTY_VALUE;
  12.  
  13.         //@formatter:off
  14.         QueryBuilder builderSoType = new QueryBuilder();
  15.         builderSoType.add(" SELECT  C.code AS so_type")
  16.             .add(" FROM ").add(SalesOrder.TABLE_NAME).add(" A ")
  17.             .add(" INNER JOIN ").add(SoInfo.TABLE_NAME).add(" B ON A.so_id = B.so_id " )
  18.             .add(" INNER JOIN ").add(ComboValue.TABLE_NAME).add(" C ON B.flg_type_so = C.code AND C.combo_id = :soType ")
  19.             .add(" WHERE A.so_id = :soId ");
  20.         //@formatter:on
  21.  
  22.         Query querySoType = salesOrderDao.createNativeQuery(builderSoType.toString());
  23.         querySoType.setParameter("soId", soId);
  24.         querySoType.setParameter("soType", SalesConstantsForKtmt.COMBO_SO_TYPE);
  25.  
  26.         Object resultQuerySoType = querySoType.getSingleResult();
  27.         if (resultQuerySoType != null) {
  28.             typeSo = resultQuerySoType.toString();
  29.         }
  30.  
  31.         //get group brand code
  32.         String groupBrandCode = GeneralConstants.EMPTY_VALUE;
  33.  
  34.         //@formatter:off
  35.         QueryBuilder builderGroupBrand = new QueryBuilder();
  36.         builderGroupBrand.add(" SELECT  C.group_brand ")
  37.             .add(" FROM ").add(SoBrandItem.TABLE_NAME).add(" A ")
  38.             .add(" INNER JOIN ").add(Brand.TABLE_NAME).add(" B ON A.brand_id = B.brand_id " )
  39.             .add(" INNER JOIN ").add(BrandExt.TABLE_NAME).add(" C ON B.brand_id = C.brand_id ")
  40.             .add(" WHERE A.so_id = :soId ")
  41.             .add(" GROUP BY C.group_brand ");
  42.         //@formatter:on
  43.  
  44.         Query queryGroupBrand = salesOrderDao.createNativeQuery(builderGroupBrand.toString());
  45.         queryGroupBrand.setParameter("soId", soId);
  46.  
  47.         Object resultQueryGroupBrand = queryGroupBrand.getSingleResult();
  48.         if (resultQueryGroupBrand != null) {
  49.             groupBrandCode = resultQueryGroupBrand.toString();
  50.         }
  51.  
  52.         // get curr code so
  53.         String currCode = GeneralConstants.EMPTY_VALUE;
  54.         String docDate = GeneralConstants.EMPTY_VALUE;
  55.  
  56.         //@formatter:off
  57.         QueryBuilder builderCurrCode = new QueryBuilder();
  58.         builderCurrCode.add(" SELECT  A.curr_code, A.doc_date ")
  59.             .add(" FROM ").add(SalesOrder.TABLE_NAME).add(" A ")
  60.             .add(" WHERE A.so_id = :soId ");
  61.         //@formatter:on
  62.  
  63.         Query queryCurrCode = salesOrderDao.createNativeQuery(builderCurrCode.toString());
  64.         queryCurrCode.setParameter("soId", soId);
  65.  
  66.         List<Object[]> resultQueryCurrCode = queryCurrCode.getResultList();
  67.         if (resultQueryCurrCode != null && resultQueryCurrCode.size() > 0) {
  68.             List<Dto> resultCurrCodeList = new ArrayList<Dto>();
  69.             resultCurrCodeList = DtoUtil.createDtoListFromArray(resultQueryCurrCode, "currCode", "docDate");
  70.  
  71.             for (Dto dto : resultCurrCodeList) {
  72.                 currCode = dto.getString("currCode");
  73.                 docDate = dto.getString("docDate");
  74.             }
  75.         }
  76.  
  77.         // get total amount SO
  78.         //@formatter:off
  79.         QueryBuilder builderNettAmount = new QueryBuilder();
  80.         builderNettAmount.add(" WITH temp_amount_so AS ( ")
  81.             .add(" SELECT ((A.nett_sell_price + A.tax_price) * A.qty) AS amount, A.qty, B.partner_id, B.tenant_id, B.ou_id, A.gross_sell_price ")
  82.             .add(" FROM ").add(SoBrandItem.TABLE_NAME).add(" A ")
  83.             .add(" INNER JOIN ").add(SalesOrder.TABLE_NAME).add(" B ON A.so_id = B.so_id ")
  84.             .add(" WHERE A.so_id = :soId ")
  85.             .add(" ) ")
  86.             .add(" SELECT SUM(amount) AS total_nett_amount, SUM(qty) AS total_qty, partner_id, tenant_id, ou_id, gross_sell_price ")
  87.             .add(" FROM temp_amount_so ")
  88.             .add(" GROUP BY partner_id, tenant_id, ou_id, gross_sell_price ");
  89.         //@formatter:on
  90.  
  91.         Query queryNettAmount = salesOrderDao.createNativeQuery(builderNettAmount.toString());
  92.         queryNettAmount.setParameter("soId", soId);
  93.  
  94.         List<Object[]> resultNettAmount = queryNettAmount.getResultList();
  95.         Double totalAmountSo = 0d;
  96.         Double grossSellPrice = 0d;
  97.         Double totalQty = 0d;
  98.         Long partnerId = GeneralConstants.NULL_REF_VALUE_LONG;
  99.         Long tenantId = GeneralConstants.NULL_REF_VALUE_LONG;
  100.         Long ouId = GeneralConstants.NULL_REF_VALUE_LONG;
  101.  
  102.         if (resultNettAmount != null && resultNettAmount.size() > 0) {
  103.             List<Dto> resultNettAmountList = new ArrayList<Dto>();
  104.             resultNettAmountList = DtoUtil.createDtoListFromArray(resultNettAmount, "totalNettAmount", "totalQty", "partnerId",
  105.                     "tenantId", "ouId", "grossSellPrice");
  106.  
  107.             totalAmountSo = resultNettAmountList.get(0).getDouble("totalNettAmount");
  108.             totalQty = resultNettAmountList.get(0).getDouble("totalQty");
  109.             partnerId = resultNettAmountList.get(0).getLong("partnerId");
  110.             tenantId = resultNettAmountList.get(0).getLong("tenantId");
  111.             ouId = resultNettAmountList.get(0).getLong("ouId");
  112.             grossSellPrice = resultNettAmountList.get(0).getDouble("grossSellPrice");
  113.         }
  114.  
  115.         totalAmountSo = new Calc(totalAmountSo).doubleValue();
  116.         totalQty = new Calc(totalQty).doubleValue();
  117.  
  118.         // get Saldo AR yang belum dibayarkan padahal sudah jatuh tempo
  119.         Connection conn = jdbcTemplate.getDataSource().getConnection();
  120.         conn.setAutoCommit(false);
  121.         PreparedStatement stm = null;
  122.  
  123.         try {
  124.  
  125.             String sessionId = inputDto.getString("sessionId");
  126.             Long userId = inputDto.getLong("userId");
  127.             Long roleId = inputDto.getLong("roleId");
  128.             String dueDateType = GeneralConstants.EMPTY_VALUE;
  129.             String dueDateFrom = GeneralConstants.START_DATE;
  130.             String dueDateTo = GeneralConstants.END_DATE;
  131.             String dateNow = DateUtil.dateNow();
  132.  
  133.             stm = conn.prepareStatement("SELECT f_get_summary_aging_ar_list_for_inquiry_progress_ar(?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  134.             stm.setString(1, sessionId);
  135.             stm.setLong(2, tenantId);
  136.             stm.setLong(3, ouId);
  137.             stm.setLong(4, userId);
  138.             stm.setLong(5, roleId);
  139.             stm.setLong(6, partnerId);
  140.             stm.setString(7, dueDateType);
  141.             stm.setString(8, dueDateFrom);
  142.             stm.setString(9, dueDateTo);
  143.             stm.setString(10, dateNow);
  144.  
  145.             ResultSet resultSetFromFunction = stm.executeQuery();
  146.             ResultSet resultSetFromQuery = null;
  147.             if (resultSetFromFunction.next()) {
  148.                 resultSetFromQuery = (ResultSet) resultSetFromFunction.getObject(1);
  149.             }
  150.  
  151.             resultSetFromFunction.close();
  152.  
  153.             Dto detailDto = new Dto();
  154.  
  155.             while (resultSetFromQuery != null && resultSetFromQuery.next()) {
  156.                 detailDto.put("saldoArCurrCode", resultSetFromQuery.getString(1));
  157.                 detailDto.put("saldoAr", resultSetFromQuery.getBigDecimal(5).doubleValue());
  158.             }
  159.            
  160.             log.debug("summary ar : "+detailDto);
  161.  
  162.             stm.close();
  163.             conn.commit();
  164.             conn.close();
  165.  
  166.             if(detailDto.isEmpty()){
  167.                 detailDto.put("saldoArCurrCode", currCode);
  168.                 detailDto.put("saldoAr", new Double(0));
  169.             }
  170.            
  171.             outputDto.putAll(detailDto);
  172.             log.debug("add summary ar : "+outputDto);
  173.         } catch (Exception e) {
  174.             if (stm != null)
  175.                 stm.close();
  176.  
  177.             if (conn != null && !conn.isClosed())
  178.                 conn.rollback();
  179.  
  180.             throw e;
  181.         }
  182.  
  183.         // get flg_due_date
  184.         String flgDueDate = GeneralConstants.NO;
  185.  
  186.         if (Double.valueOf(outputDto.get("saldoAr").toString()) > new Double(0)) {
  187.             flgDueDate = GeneralConstants.YES;
  188.         }
  189.  
  190.         // get promoId dan promo
  191.         String promo = GeneralConstants.EMPTY_VALUE;
  192.         Long promoId = GeneralConstants.NULL_REF_VALUE_LONG;
  193.         Long productLaunchingId = GeneralConstants.NULL_REF_VALUE_LONG;
  194.         Long mouId = GeneralConstants.NULL_REF_VALUE_LONG;
  195.  
  196.         //@formatter:off
  197.         QueryBuilder builderPromo = new QueryBuilder();
  198.         builderPromo.add(" SELECT  A.promo_id, C.promo_code, A.product_launching_id, A.mou_id ")
  199.             .add(" FROM ").add(SoInfo.TABLE_NAME).add(" A ")
  200.             .add(" INNER JOIN ").add(SalesOrder.TABLE_NAME).add(" B ON A.so_id = B.so_id " )
  201.             .add(" INNER JOIN ").add(Promo.TABLE_NAME).add(" C ON A.promo_id = C.promo_id ")
  202.             .add(" WHERE A.so_id = :soId ");
  203.         //@formatter:on
  204.  
  205.         Query queryPromo = salesOrderDao.createNativeQuery(builderPromo.toString());
  206.         queryPromo.setParameter("soId", soId);
  207.  
  208.         List<Object[]> resultPromo = queryPromo.getResultList();
  209.  
  210.         if (resultPromo != null && resultPromo.size() > 0) {
  211.             List<Dto> resultPromoList = new ArrayList<Dto>();
  212.             resultPromoList = DtoUtil.createDtoListFromArray(resultPromo, "promoId", "promo", "productLaunchingId", "mouId");
  213.  
  214.             promoId = resultPromoList.get(0).getLong("promoId");
  215.             promo = resultPromoList.get(0).getString("promo");
  216.             productLaunchingId = resultPromoList.get(0).getLong("productLaunchingId");
  217.             mouId = resultPromoList.get(0).getLong("mouId");
  218.         }
  219.  
  220.         // get flgDiffSellPrice
  221.         Connection connDiffPrice = jdbcTemplate.getDataSource().getConnection();
  222.         connDiffPrice.setAutoCommit(false);
  223.         PreparedStatement stmDiffPrice = null;
  224.  
  225.         Double basicGrossSellPrice = 0d;
  226.  
  227.         try {
  228.  
  229.             String sessionId = inputDto.getString("sessionId");
  230.             Long userId = inputDto.getLong("userId");
  231.             Long roleId = inputDto.getLong("roleId");
  232.  
  233.             stmDiffPrice = connDiffPrice.prepareStatement("SELECT f_get_detail_info_price_for_sob(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
  234.             stmDiffPrice.setString(1, sessionId);
  235.             stmDiffPrice.setLong(2, tenantId);
  236.             stmDiffPrice.setLong(3, ouId);
  237.             stmDiffPrice.setLong(4, userId);
  238.             stmDiffPrice.setLong(5, roleId);
  239.             stmDiffPrice.setLong(6, soId);
  240.             stmDiffPrice.setLong(7, partnerId);
  241.             stmDiffPrice.setLong(8, productLaunchingId);
  242.             stmDiffPrice.setLong(9, promoId);
  243.             stmDiffPrice.setLong(10, mouId);
  244.             stmDiffPrice.setString(11, docDate);
  245.  
  246.             ResultSet resultSetFromFunction = stmDiffPrice.executeQuery();
  247.             ResultSet resultSetFromQuery = null;
  248.             if (resultSetFromFunction.next()) {
  249.                 resultSetFromQuery = (ResultSet) resultSetFromFunction.getObject(1);
  250.             }
  251.  
  252.             resultSetFromFunction.close();
  253.  
  254.             Dto detailDto = new Dto();
  255.  
  256.             while (resultSetFromQuery != null && resultSetFromQuery.next()) {
  257.                 detailDto.put("grossSellPrice", resultSetFromQuery.getBigDecimal(5).doubleValue());
  258.             }
  259.  
  260.             log.debug("detail dto basic sell price "+detailDto);
  261.            
  262.             stmDiffPrice.close();
  263.             connDiffPrice.commit();
  264.             connDiffPrice.close();
  265.  
  266.             basicGrossSellPrice = detailDto.getDouble("grossSellPrice");
  267.  
  268.         } catch (Exception e) {
  269.             if (stmDiffPrice != null)
  270.                 stmDiffPrice.close();
  271.  
  272.             if (connDiffPrice != null && !connDiffPrice.isClosed())
  273.                 connDiffPrice.rollback();
  274.  
  275.             throw e;
  276.         }
  277.  
  278.         if (!grossSellPrice.equals(basicGrossSellPrice)) {
  279.             outputDto.put("flgDiffSellPrice", GeneralConstants.YES);
  280.         } else {
  281.             outputDto.put("flgDiffSellPrice", GeneralConstants.NO);
  282.         }
  283.  
  284.         outputDto.put("soId", soId);
  285.         outputDto.put("typeSo", typeSo);
  286.         outputDto.put("groupBrandCode", groupBrandCode);
  287.         outputDto.put("currCode", currCode);
  288.         outputDto.put("totalAmountSo", totalAmountSo);
  289.         outputDto.put("qtySo", totalQty);
  290.         outputDto.put("flgDueDate", flgDueDate);
  291.         // flg indent dilihat ketika nilai qty saat SOS dan DO < dari nilai qty per brand SOB
  292.         outputDto.put("flgIndent", GeneralConstants.NO);
  293.         outputDto.put("promoId", promoId);
  294.         outputDto.put("promo", promo);
  295.  
  296.         log.debug("output dto : " + outputDto);
  297.         return outputDto;
  298.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement