PageRenderTime 26ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/base/src/org/compiere/model/MTransaction.java

https://gitlab.com/giggog66/adempiere
Java | 283 lines | 171 code | 25 blank | 87 comment | 30 complexity | 32c5a687efe7dc95b0c37225ee4fdd3e MD5 | raw file
  1. /******************************************************************************
  2. * Product: Adempiere ERP & CRM Smart Business Solution *
  3. * Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. *
  4. * This program is free software; you can redistribute it and/or modify it *
  5. * under the terms version 2 of the GNU General Public License as published *
  6. * by the Free Software Foundation. This program is distributed in the hope *
  7. * that it will be useful, but WITHOUT ANY WARRANTY; without even the implied *
  8. * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. *
  9. * See the GNU General Public License for more details. *
  10. * You should have received a copy of the GNU General Public License along *
  11. * with this program; if not, write to the Free Software Foundation, Inc., *
  12. * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
  13. * For the text or an alternative of this public license, you may reach us *
  14. * ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA *
  15. * or via info@compiere.org or http://www.compiere.org/license.html *
  16. *****************************************************************************/
  17. package org.compiere.model;
  18. import java.math.BigDecimal;
  19. import java.sql.ResultSet;
  20. import java.sql.Timestamp;
  21. import java.util.ArrayList;
  22. import java.util.List;
  23. import java.util.Properties;
  24. import org.adempiere.engine.CostEngineFactory;
  25. import org.adempiere.engine.IDocumentLine;
  26. import org.compiere.util.Env;
  27. /**
  28. * Material Transaction Model
  29. *
  30. * @author Jorg Janke
  31. * @version $Id: MTransaction.java,v 1.3 2006/07/30 00:51:03 jjanke Exp $
  32. */
  33. public class MTransaction extends X_M_Transaction
  34. {
  35. /**
  36. *
  37. */
  38. private static final long serialVersionUID = 3411351000865493212L;
  39. /**
  40. * get the transaction based on Document Line and movement type
  41. * @param model IDocumentLine
  42. * @param type Movement Type
  43. * @return first MTransaction
  44. */
  45. public static MTransaction getByDocumentLine(IDocumentLine model, String type)
  46. {
  47. final String column_id = model.get_TableName() + "_ID";
  48. final String whereClause = column_id + "=? AND "
  49. + MTransaction.COLUMNNAME_MovementType + "=? ";
  50. return new Query (model.getCtx(), I_M_Transaction.Table_Name, whereClause, model.get_TrxName())
  51. .setClient_ID()
  52. .setParameters(model.get_ID(), type)
  53. .first();
  54. }
  55. /**
  56. * get the Material Transaction after Date Account
  57. * @param ctx Context
  58. * @param M_Product_ID Product ID
  59. * @param dateAcct Date Account
  60. * @param trxName Transaction name
  61. * @return List with the MTransaction after date account
  62. */
  63. static public List<MTransaction> getAfterDateAcct(Properties ctx , int M_Product_ID,Timestamp dateAcct, String trxName)
  64. {
  65. ArrayList<MTransaction> list = new ArrayList();
  66. final String whereClause = I_M_Transaction.COLUMNNAME_M_Product_ID + "=?";
  67. List<MTransaction> trxs = new Query(ctx, Table_Name, whereClause, trxName)
  68. .setClient_ID()
  69. .setParameters(M_Product_ID)
  70. .list();
  71. for(MTransaction trx : trxs)
  72. {
  73. IDocumentLine model = trx.getDocumentLine();
  74. if(model.getDateAcct().compareTo(dateAcct) > 0)
  75. {
  76. list.add(trx);
  77. }
  78. }
  79. return list;
  80. }
  81. /**
  82. * get all material transaction for MInOutLine
  83. * @param line MInOutLine
  84. * @return List the MTransaction
  85. */
  86. static public List<MTransaction> getByInOutLine(MInOutLine line)
  87. {
  88. ArrayList<MTransaction> transactions = new ArrayList();
  89. List<MInOutLineMA> lines = MInOutLineMA.get(line.getCtx(), line.getM_InOutLine_ID(), line.get_TrxName());
  90. if(lines != null && lines.size() == 0)
  91. {
  92. MTransaction transaction = get(line, line.getM_AttributeSetInstance_ID());
  93. if (transaction != null && transaction.get_ID() > 0)
  94. transactions.add(transaction);
  95. return transactions;
  96. }
  97. for(MInOutLineMA ma : lines)
  98. {
  99. MTransaction trx = get(line, ma.getM_AttributeSetInstance_ID());
  100. transactions.add(trx);
  101. }
  102. return transactions;
  103. }
  104. static public MTransaction get(MInOutLine line , int M_ASI_ID)
  105. {
  106. final String whereClause = I_M_InOutLine.COLUMNNAME_M_Product_ID + "=? AND "
  107. + I_M_InOutLine.COLUMNNAME_M_InOutLine_ID + "=? AND "
  108. + I_M_InOutLine.COLUMNNAME_M_AttributeSetInstance_ID + "=?";
  109. return new Query(line.getCtx(), Table_Name, whereClause, line.get_TrxName())
  110. .setClient_ID()
  111. .setParameters(line.getM_Product_ID(),line.getM_InOutLine_ID(), M_ASI_ID)
  112. .firstOnly();
  113. }
  114. /**
  115. *
  116. * get Material transaction for Reversal Document
  117. * @param trx MTransaction
  118. * @return
  119. */
  120. static public MTransaction getByDocumentLine (MTransaction trx)
  121. {
  122. IDocumentLine reversal = trx.getDocumentLine().getReversalDocumentLine();
  123. List<Object> parameters = new ArrayList();
  124. String columnName = reversal.get_TableName()+"_ID";
  125. StringBuffer whereClause = new StringBuffer(I_M_Transaction.COLUMNNAME_M_Product_ID);
  126. whereClause.append( "=? AND ");
  127. parameters.add(reversal.getM_Product_ID());
  128. whereClause.append( columnName ).append("=? AND ");
  129. parameters.add(reversal.get_ID());
  130. if (trx.getM_AttributeSetInstance_ID() > 0)
  131. {
  132. whereClause.append(I_M_Transaction.COLUMNNAME_M_AttributeSetInstance_ID).append("=? AND ");
  133. parameters.add(trx.getM_AttributeSetInstance_ID());
  134. }
  135. whereClause.append(I_M_Transaction.COLUMNNAME_MovementType).append("=? AND ");
  136. if(MTransaction.MOVEMENTTYPE_InventoryIn.equals(trx.getMovementType()))
  137. parameters.add(MTransaction.MOVEMENTTYPE_InventoryOut);
  138. else if(MTransaction.MOVEMENTTYPE_InventoryOut.equals(trx.getMovementType()))
  139. parameters.add(MTransaction.MOVEMENTTYPE_InventoryIn);
  140. else
  141. parameters.add(trx.getMovementType());
  142. whereClause.append(I_M_Transaction.COLUMNNAME_M_Transaction_ID).append("<>?");
  143. parameters.add(trx.getM_Transaction_ID());
  144. return new Query(trx.getCtx(), Table_Name, whereClause.toString(), trx.get_TrxName())
  145. .setClient_ID()
  146. .setParameters(parameters)
  147. .first();
  148. }
  149. /**
  150. * Standard Constructor
  151. * @param ctx context
  152. * @param M_Transaction_ID id
  153. * @param trxName transaction
  154. */
  155. public MTransaction (Properties ctx, int M_Transaction_ID, String trxName)
  156. {
  157. super (ctx, M_Transaction_ID, trxName);
  158. if (M_Transaction_ID == 0)
  159. {
  160. // setM_Transaction_ID (0); // PK
  161. // setM_Locator_ID (0);
  162. // setM_Product_ID (0);
  163. setMovementDate (new Timestamp(System.currentTimeMillis()));
  164. setMovementQty (Env.ZERO);
  165. // setMovementType (MOVEMENTTYPE_CustomerShipment);
  166. }
  167. } // MTransaction
  168. /**
  169. * Load Constructor
  170. * @param ctx context
  171. * @param rs result set
  172. * @param trxName transaction
  173. */
  174. public MTransaction (Properties ctx, ResultSet rs, String trxName)
  175. {
  176. super(ctx, rs, trxName);
  177. } // MTransaction
  178. /**
  179. * Detail Constructor
  180. * @param ctx context
  181. * @param AD_Org_ID org
  182. * @param MovementType movement type
  183. * @param M_Locator_ID locator
  184. * @param M_Product_ID product
  185. * @param M_AttributeSetInstance_ID attribute
  186. * @param MovementQty qty
  187. * @param MovementDate optional date
  188. * @param trxName transaction
  189. */
  190. public MTransaction (Properties ctx, int AD_Org_ID,
  191. String MovementType,
  192. int M_Locator_ID, int M_Product_ID, int M_AttributeSetInstance_ID,
  193. BigDecimal MovementQty, Timestamp MovementDate, String trxName)
  194. {
  195. super(ctx, 0, trxName);
  196. setAD_Org_ID(AD_Org_ID);
  197. setMovementType (MovementType);
  198. if (M_Locator_ID == 0)
  199. throw new IllegalArgumentException("No Locator");
  200. setM_Locator_ID (M_Locator_ID);
  201. if (M_Product_ID == 0)
  202. throw new IllegalArgumentException("No Product");
  203. setM_Product_ID (M_Product_ID);
  204. setM_AttributeSetInstance_ID (M_AttributeSetInstance_ID);
  205. //
  206. if (MovementQty != null) // Can be 0
  207. setMovementQty (MovementQty);
  208. if (MovementDate == null)
  209. setMovementDate (new Timestamp(System.currentTimeMillis()));
  210. else
  211. setMovementDate(MovementDate);
  212. } // MTransaction
  213. protected boolean afterSave (boolean newRecord, boolean success)
  214. {
  215. if (newRecord)
  216. {
  217. CostEngineFactory.getCostEngine(getAD_Client_ID()).createCostDetail(this , getDocumentLine());
  218. }
  219. return true;
  220. } // afterSave
  221. public IDocumentLine getDocumentLine()
  222. {
  223. if(getM_InOutLine_ID() > 0)
  224. return (IDocumentLine) getM_InOutLine();
  225. if(getM_InventoryLine_ID() > 0)
  226. return (IDocumentLine) getM_InventoryLine();
  227. if(getM_MovementLine_ID() > 0)
  228. return (IDocumentLine) getM_MovementLine();
  229. if(getM_ProductionLine_ID() > 0)
  230. return (IDocumentLine) getM_ProductionLine();
  231. if(getPP_Cost_Collector_ID() > 0)
  232. return (IDocumentLine) getPP_Cost_Collector();
  233. return null;
  234. }
  235. /**
  236. * get Warehouse ID
  237. * @return Warehouse ID
  238. */
  239. public int getM_Warehouse_ID()
  240. {
  241. return getM_Locator().getM_Warehouse_ID();
  242. }
  243. /**
  244. * String Representation
  245. * @return info
  246. */
  247. public String toString ()
  248. {
  249. StringBuffer sb = new StringBuffer ("MTransaction[");
  250. sb.append(get_ID()).append(",").append(getMovementType())
  251. .append(",MovementDate=").append(getMovementDate())
  252. .append(",Qty=").append(getMovementQty())
  253. .append(",M_Product_ID=").append(getM_Product_ID())
  254. .append(",ASI=").append(getM_AttributeSetInstance_ID())
  255. .append ("]");
  256. return sb.toString ();
  257. } // toString
  258. } // MTransaction