Advertisement
Guest User

Untitled

a guest
May 25th, 2018
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.77 KB | None | 0 0
  1. /*
  2. * COPYRIGHT FH 2018 - ALL RIGHTS RESERVED.
  3. *
  4. * This software is only to be used for the purpose for which it has been
  5. * provided. No part of it is to be reproduced, disassembled, transmitted,
  6. * stored in a retrieval system nor translated in any human or computer
  7. * language in any way or for any other purposes whatsoever without the prior
  8. * written consent of FH.
  9. */
  10. package br.com.fh.accounting.odata.dailycontrolline.business.bo;
  11.  
  12. import java.io.Serializable;
  13. import java.math.BigDecimal;
  14. import java.sql.Connection;
  15. import java.sql.Date;
  16. import java.sql.PreparedStatement;
  17. import java.sql.ResultSet;
  18. import java.sql.SQLException;
  19. import java.sql.Timestamp;
  20. import java.util.ArrayList;
  21. import java.util.List;
  22. import java.util.Map;
  23. import java.util.Properties;
  24.  
  25. import org.compiere.util.Env;
  26.  
  27. import com.fh.camaleo.core.utils.StringUtils;
  28. import com.fh.camaleo.integrator.db.connection.DatabaseConnection;
  29.  
  30. import br.com.fh.accounting.model.I_FHX_ReimbursableExpense;
  31. import br.com.fh.accounting.model.extension.MFHXDailyControl;
  32. import br.com.fh.accounting.model.extension.MFHXReimbursableExpense;
  33. import br.com.fh.administration.model.extension.MFHFromTo;
  34. import br.com.fh.extension.model.extension.MFHXUser;
  35.  
  36. /**
  37. * @author FH - edusilva
  38. *
  39. */
  40. public class DailyControlLineBO implements Serializable {
  41.  
  42. /**
  43. * long - serialVersionUID.
  44. */
  45. private static final long serialVersionUID = -1674802600277379392L;
  46.  
  47. private static final String OTHERSYSTEM_B1 = "B1";
  48.  
  49. private BigDecimal amt;
  50.  
  51. private BigDecimal amtApproved;
  52.  
  53. private BigDecimal totalRefunded;
  54.  
  55. private List<DailyControlLineDTO> dailyControlLines;
  56.  
  57.  
  58.  
  59. public BigDecimal getAmt() {
  60. return amt;
  61. }
  62.  
  63. public void setAmt(BigDecimal amt) {
  64. this.amt = amt;
  65. }
  66.  
  67. public BigDecimal getAmtApproved() {
  68. return amtApproved;
  69. }
  70.  
  71. public void setAmtApproved(BigDecimal amtApproved) {
  72. this.amtApproved = amtApproved;
  73. }
  74.  
  75. public BigDecimal getTotalRefunded() {
  76. return totalRefunded;
  77. }
  78.  
  79. public void setTotalRefunded(BigDecimal totalRefunded) {
  80. this.totalRefunded = totalRefunded;
  81. }
  82.  
  83. public List<DailyControlLineDTO> getDailyControlLines() {
  84. return dailyControlLines;
  85. }
  86.  
  87. public void setDailyControlLines(List<DailyControlLineDTO> dailyControlLines) {
  88. this.dailyControlLines = dailyControlLines;
  89. }
  90.  
  91. public static DailyControlLineBO getAllDailyControlLinesFromB1(Properties ctx, Date dateFrom, Date dateTo, Integer userId, String trxName)
  92. throws Exception {
  93.  
  94. Map<Integer, Integer> mapEmployeeCodeByUserId = MFHXUser.getMapEmployeeCodeByUserId();
  95.  
  96. MFHXDailyControl dailyControl = MFHXDailyControl
  97. .getDailyControlFromPeriod(ctx, new Timestamp(dateFrom.getTime()), new Timestamp(dateTo.getTime()), trxName);
  98.  
  99. Map<String, BigDecimal> mapOfBalances = MFHXReimbursableExpense
  100. .getMapOfBalances(ctx, new Timestamp(dateFrom.getTime()), new Timestamp(dateTo.getTime()), userId, trxName);
  101.  
  102. List<String> listCompanies = DailyControlLineBO.getUserCompanies(mapEmployeeCodeByUserId, dateFrom, dateTo, userId);
  103.  
  104.  
  105. DailyControlLineBO dailyControlLineBO = new DailyControlLineBO();
  106.  
  107. Connection conn = null;
  108. PreparedStatement pstmt = null;
  109. ResultSet rs = null;
  110.  
  111. StringBuilder query = null;
  112. for (String company : listCompanies) {
  113. query = new StringBuilder();
  114. query.append(" SELECT a.empresa, a.empId, count(DISTINCT a.data) AS qty ");
  115. query.append(" FROM FH_INTRANET.dbgBuilder qo.Apontamentos a ");
  116. query.append(" INNER JOIN " + company + ".dbo.ORDR p ON (a.docNum = p.DocNum AND a.empresa = ? ");
  117. query.append(" and p.U_tipoProjFaturavel = ?) ");
  118. query.append(" WHERE a.data BETWEEN ? AND ? ");
  119. if (userId != null) {
  120. query.append(" AND a.empId = ? ");
  121. }
  122. query.append(" GROUP by a.empresa, a.empId ");
  123.  
  124. try {
  125. conn = DatabaseConnection.getConnection("SQLSERVER");
  126. pstmt = conn.prepareStatement(query.toString());
  127. pstmt.setString(1, company);
  128. pstmt.setString(2, "1");
  129. pstmt.setDate(3, dateFrom);
  130. pstmt.setDate(4, dateTo);
  131. if (userId != null) {
  132. pstmt.setInt(5, mapEmployeeCodeByUserId.get(userId));
  133. }
  134. rs = pstmt.executeQuery();
  135. DailyControlLineBO.fillListDaiyControl(dailyControlLineBO, rs, dailyControl, mapOfBalances, userId, mapEmployeeCodeByUserId);
  136. } catch (Exception e) {
  137. throw e;
  138. } finally {
  139. DatabaseConnection.close(conn, pstmt);
  140. }
  141. }
  142. return dailyControlLineBO;
  143. }
  144.  
  145. public static List<String> getUserCompanies(Map<Integer, Integer> mapEmployeeCodeByUserId, Date dateFrom,
  146. Date dateTo, Integer userId) throws Exception {
  147. List<String> listCompany = new ArrayList<String>();
  148.  
  149. StringBuilder query = new StringBuilder();
  150. query.append(" SELECT DISTINCT a.empresa ");
  151. query.append(" FROM FH_INTRANET.dbo.Apontamentos a ");
  152. query.append(" WHERE a.data BETWEEN ? AND ? AND a.empresa IS NOT NULL ");
  153. if (userId != null) {
  154. query.append(" AND a.empID = ? ");
  155. }
  156.  
  157. Connection conn = null;
  158. PreparedStatement pstmt = null;
  159. ResultSet rs = null;
  160. try {
  161. conn = DatabaseConnection.getConnection("SQLSERVER");
  162. pstmt = conn.prepareStatement(query.toString());
  163. pstmt.setDate(1, dateFrom);
  164. pstmt.setDate(2, dateTo);
  165. if (userId != null) {
  166. pstmt.setInt(3, mapEmployeeCodeByUserId.get(userId));
  167. }
  168. rs = pstmt.executeQuery();
  169. while (rs.next()) {
  170. listCompany.add(rs.getString("empresa"));
  171. }
  172. } catch (Exception e) {
  173. throw e;
  174. } finally {
  175. DatabaseConnection.close(conn, pstmt);
  176. }
  177.  
  178. return listCompany;
  179. }
  180.  
  181. public static void fillListDaiyControl(DailyControlLineBO dailyControlLineBO, ResultSet rs,
  182. MFHXDailyControl mDailyControl, Map<String, BigDecimal> mapOfBalances, Integer userId, Map<Integer, Integer> mapEmployeeCodeByUserId) throws SQLException {
  183.  
  184. Map<String, String> mapOrgFromTo = MFHFromTo.getMapInternalByExternal(Env.getAD_Client_ID(Env.getCtx()), "AD_Org_ID", OTHERSYSTEM_B1, Env.getCtx());
  185.  
  186. List<DailyControlLineDTO> listDailyControlLines = dailyControlLineBO.getDailyControlLines();
  187. if (listDailyControlLines == null) {
  188. listDailyControlLines = new ArrayList<DailyControlLineDTO>();
  189. }
  190.  
  191. while (rs.next()) {
  192. DailyControlLineDTO dailyControlLineDTO = new DailyControlLineDTO();
  193. String org = rs.getString("empresa");
  194. if (StringUtils.isNotNullOrEmpty(org) && mapOrgFromTo.get(org) != null) {
  195. dailyControlLineDTO.setOrgId(Integer.parseInt(mapOrgFromTo.get(org)));
  196. }
  197. dailyControlLineDTO.setQty(rs.getBigDecimal("qty"));
  198.  
  199. if (userId == null)
  200. dailyControlLineDTO.setUserId(getUserIdByEmployeeCode(mapEmployeeCodeByUserId, rs.getInt("empId")));
  201. else
  202. dailyControlLineDTO.setUserId(userId);
  203.  
  204. if (mDailyControl != null)
  205. dailyControlLineDTO.setDailyValue(mDailyControl.getFHX_Daily());
  206. else
  207. dailyControlLineDTO.setDailyValue(BigDecimal.ZERO);
  208.  
  209. listDailyControlLines.add(dailyControlLineDTO);
  210. }
  211. dailyControlLineBO.setDailyControlLines(listDailyControlLines);
  212.  
  213. dailyControlLineBO.setAmt(mapOfBalances.get(I_FHX_ReimbursableExpense.COLUMNNAME_Amt));
  214.  
  215. dailyControlLineBO.setAmtApproved(mapOfBalances.get(I_FHX_ReimbursableExpense.COLUMNNAME_FHX_AmtApproved));
  216.  
  217. dailyControlLineBO.setTotalRefunded(mapOfBalances.get(I_FHX_ReimbursableExpense.COLUMNNAME_FHX_TotalRefunded));
  218. }
  219.  
  220. private static Integer getUserIdByEmployeeCode(Map<Integer, Integer> mapEmployeeCodeByUserId, Integer empId) {
  221. Integer userId = null;
  222. for (Map.Entry<Integer, Integer> entry : mapEmployeeCodeByUserId.entrySet()) {
  223. if (entry.getValue().equals(empId)) {
  224. userId = entry.getKey();
  225. }
  226. }
  227. return userId;
  228. }
  229.  
  230.  
  231.  
  232. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement