Advertisement
samuel025

IsInvoiceAlreadyPaidByMappingCashBankInOthersToSo

Aug 18th, 2021
1,273
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.39 KB | None | 0 0
  1. package org.jleaf.erp.sls.bo.invoice;
  2.  
  3. import org.jleaf.core.AbstractBusinessFunction;
  4. import org.jleaf.core.BusinessFunction;
  5. import org.jleaf.core.Dto;
  6. import org.jleaf.core.annotation.ErrorList;
  7. import org.jleaf.core.annotation.Info;
  8. import org.jleaf.core.annotation.InfoIn;
  9. import org.jleaf.core.annotation.InfoOut;
  10. import org.jleaf.core.dao.QueryBuilder;
  11. import org.jleaf.erp.cb.entity.MappingCashBankInSoBalanceItem;
  12. import org.jleaf.erp.sls.SalesConstants;
  13. import org.jleaf.erp.sls.dao.SalesInvoiceDao;
  14. import org.jleaf.erp.sls.dao.SalesInvoiceItemDao;
  15. import org.jleaf.erp.sls.entity.*;
  16. import org.jleaf.util.ValidationUtil;
  17. import org.slf4j.Logger;
  18. import org.slf4j.LoggerFactory;
  19. import org.springframework.beans.factory.annotation.Autowired;
  20. import org.springframework.stereotype.Service;
  21.  
  22. import javax.persistence.Query;
  23. import java.util.List;
  24.  
  25.  
  26. /**
  27.  * Is InvoiceAdvance exists by id
  28.  * @author Fredi, Sep 19, 2014
  29.  * @version 1.0
  30.  */
  31.  
  32. //@ formatter:off
  33. @Service
  34. @InfoIn(value = {
  35.     @Info(name = "invoiceId", description = "invoiceId", type = Long.class, required = true)
  36. })
  37. @InfoOut(value = {
  38.     @Info(name = "exists", description = "exists", type = Boolean.class, required = true),
  39.  
  40. })
  41. @ErrorList(errorKeys = {
  42.  
  43. })
  44. //@ formatter:on
  45. public class IsInvoiceAlreadyPaidByMappingCashBankInOthersToSo extends AbstractBusinessFunction implements BusinessFunction {
  46.     private static Logger log = LoggerFactory.getLogger(IsInvoiceAlreadyPaidByMappingCashBankInOthersToSo.class);
  47.    
  48.     @Autowired
  49.     private SalesInvoiceDao salesInvoiceDao;
  50.  
  51.     @Autowired
  52.     private SalesInvoiceItemDao salesInvoiceItemDao;
  53.  
  54.     @Override
  55.     public Dto execute(Dto inputDto) throws Exception {    
  56.         ValidationUtil.valDtoContainsKey(inputDto, "invoiceId");
  57.         log.debug("inputDto :"+inputDto);
  58.  
  59.         Dto outputDto = new Dto();
  60.  
  61.         List<Object[]> result = null;
  62.  
  63.         // Ambil Total Payment
  64.  
  65.         QueryBuilder builderTotalPayment = new QueryBuilder();
  66.         builderTotalPayment.add(" SELECT CASE WHEN COALESCE(C.amount, 0) = 0 THEN 0 ELSE A.gross_amount END AS payment" )
  67.                             .add(" FROM ") .add(SalesInvoice.TABLE_NAME) .add(" A ")
  68.                             .add(" INNER JOIN ") .add(SalesOrder.TABLE_NAME) .add(" B ON A.ref_id = B.so_id ")
  69.                             .add(" LEFT JOIN ") .add(MappingCashBankInSoBalanceItem.TABLE_NAME) .add(" C ON B.so_id = C.so_id AND B.doc_no = C.doc_no")
  70.                             .add(" WHERE A.invoice_id = :invoiceId ");
  71.  
  72.         Query queryTotalPayment = salesInvoiceDao.createNativeQuery(builderTotalPayment.toString());
  73.         queryTotalPayment.setParameter("invoiceId", inputDto.getLong("invoiceId"));
  74.  
  75.         Double totalPayment = 0D;
  76.  
  77.         Object resultTotalPayment = queryTotalPayment.getSingleResult();
  78.         if (resultTotalPayment != null )  {
  79.             totalPayment = Double.valueOf(resultTotalPayment.toString());
  80.         }
  81.         log.debug("totalPayment : "+totalPayment);
  82.  
  83.         // Ambil Total Tax
  84.         QueryBuilder builderTotalTax = new QueryBuilder();
  85.         builderTotalTax.add(" SELECT SUM(L.tax_amount) AS total_amount " )
  86.                 .add(" FROM ") .add(SalesInvoiceItem.TABLE_NAME) .add(" Z ")
  87.                 .add(" INNER JOIN ") .add(SalesInvoice.TABLE_NAME) .add(" A ON Z.invoice_id = A.invoice_id ")
  88.                 .add(" INNER JOIN ") .add(SalesOrder.TABLE_NAME) .add(" B ON A.ref_id = B.so_id AND A.ref_doc_type_id = B.doc_type_id ")
  89.                 .add(" INNER JOIN ") .add(DeliveryOrder.TABLE_NAME) .add(" C ON B.so_id = C.ref_id ")
  90.                 .add(" INNER JOIN ") .add(SalesOrderBalanceInvoice.TABLE_NAME) .add(" F ON A.tenant_id = F.tenant_id AND F.ou_id = A.ou_id AND F.ref_doc_type_id = :DoDocTypeId ")
  91.                 .add("              AND F.ref_id = C.do_id AND F.so_id = C.ref_id AND F.ref_item_id = Z.ref_item_id ")
  92.                 .add("              AND F.do_receipt_item_id = Z.do_receipt_item_id ")
  93.                 .add(" INNER JOIN ") .add(SalesOrderBalanceInvoiceTax.TABLE_NAME) .add(" L ON L.tenant_id = F.tenant_id ")
  94.                 .add("              AND L.ou_id = F.ou_id AND L.partner_id = F.partner_id AND L.ref_doc_type_id = F.ref_doc_type_id ")
  95.                 .add("              AND L.ref_id = F.ref_id AND L.ref_item_id = F.ref_item_id AND L.do_receipt_item_id = F.do_receipt_item_id ")
  96.                 .add(" WHERE A.invoice_id = :invoiceId ");
  97.  
  98.         Query queryTotalTax = salesInvoiceItemDao.createNativeQuery(builderTotalTax.toString());
  99.         queryTotalTax.setParameter("invoiceId", inputDto.getLong("invoiceId"));
  100.         queryTotalTax.setParameter("DoDocTypeId", SalesConstants.DOCUMENT_DELIVERY_ORDER);
  101.  
  102.         Double totalTax = 0D;
  103.  
  104.         Object resultTotalTax = queryTotalTax.getSingleResult();
  105.         if (resultTotalTax != null )  {
  106.             totalTax = Double.valueOf(resultTotalTax.toString());
  107.         }
  108.         log.debug("totalTax : "+totalTax);
  109.  
  110.  
  111.         // Ambil Total Amount
  112.         QueryBuilder builderTotalAmount = new QueryBuilder();
  113.         builderTotalAmount.add(" SELECT SUM(F.qty_dlv_so * F.price_so) AS total_amount " )
  114.                 .add(" FROM ") .add(SalesInvoiceItem.TABLE_NAME) .add(" Z ")
  115.                 .add(" INNER JOIN ") .add(SalesInvoice.TABLE_NAME) .add(" A ON Z.invoice_id = A.invoice_id ")
  116.                 .add(" INNER JOIN ") .add(SalesOrder.TABLE_NAME) .add(" B ON A.ref_id = B.so_id AND A.ref_doc_type_id = B.doc_type_id ")
  117.                 .add(" INNER JOIN ") .add(DeliveryOrder.TABLE_NAME) .add(" C ON B.so_id = C.ref_id ")
  118.                 .add(" INNER JOIN ") .add(SalesOrderBalanceInvoice.TABLE_NAME) .add(" F ON A.tenant_id = F.tenant_id AND F.ou_id = A.ou_id AND F.ref_doc_type_id = :DoDocTypeId ")
  119.                 .add("              AND F.ref_id = C.do_id AND F.so_id = C.ref_id AND F.ref_item_id = Z.ref_item_id ")
  120.                 .add("              AND F.do_receipt_item_id = Z.do_receipt_item_id ")
  121.                 .add(" WHERE A.invoice_id = :invoiceId ");
  122.  
  123.         Query queryTotalAmount = salesInvoiceItemDao.createNativeQuery(builderTotalAmount.toString());
  124.         queryTotalAmount.setParameter("invoiceId", inputDto.getLong("invoiceId"));
  125.         queryTotalAmount.setParameter("DoDocTypeId", SalesConstants.DOCUMENT_DELIVERY_ORDER);
  126.  
  127.         Double totalAmount = 0D;
  128.  
  129.         Object resultTotalAmount = queryTotalAmount.getSingleResult();
  130.         if (resultTotalAmount != null )  {
  131.             totalAmount = Double.valueOf(resultTotalAmount.toString());
  132.         }
  133.         log.debug("totalAmount : "+totalAmount);
  134.  
  135.         // Hitung Total Due
  136.         Double totalDue = totalAmount + totalTax - totalPayment;
  137.         log.debug("totalDue : "+totalDue);
  138.  
  139.         boolean isExists = false;
  140.         if (totalDue == 0 && totalDue != null) {
  141.             isExists = true;
  142.         }
  143.        
  144.         outputDto.put("exists", isExists);
  145.         log.debug("outputDtoEmailTemplate >> "+outputDto);
  146.        
  147.         return outputDto;
  148.     }
  149.  
  150.     @Override
  151.     public String getDescription() {
  152.         return "Is Invoice Already Paid By Mapping CashBank In Others To So";
  153.     }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement