PageRenderTime 57ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 1ms

/org.adempiere.base/src/org/compiere/model/MDocType.java

https://bitbucket.org/idempiere/idempiere/
Java | 371 lines | 203 code | 31 blank | 137 comment | 29 complexity | 15a8fb80c7a482942ed6d3b0bf1038f8 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.sql.ResultSet;
  19. import java.util.List;
  20. import java.util.Properties;
  21. import java.util.logging.Level;
  22. import org.compiere.util.CCache;
  23. import org.compiere.util.DB;
  24. import org.compiere.util.Env;
  25. /**
  26. * Document Type Model
  27. *
  28. * @author Jorg Janke
  29. * @author Karsten Thiemann FR [ 1782412 ]
  30. * @author Teo Sarca, www.arhipac.ro
  31. * <li>BF [ 2476824 ] MDocType.getOfDocBaseType should return ONLY active records
  32. * <li>BF [ - ] MDocType.getOfClient should return ONLY active records.
  33. * See https://sourceforge.net/forum/message.php?msg_id=6499893
  34. * @version $Id: MDocType.java,v 1.3 2006/07/30 00:54:54 jjanke Exp $
  35. */
  36. public class MDocType extends X_C_DocType
  37. {
  38. /**
  39. *
  40. */
  41. private static final long serialVersionUID = -6556521509479670059L;
  42. /**
  43. * Return the first Doc Type for this BaseType
  44. * @param DocBaseType
  45. * @return
  46. */
  47. static public int getDocType(String DocBaseType)
  48. {
  49. MDocType[] doc = MDocType.getOfDocBaseType(Env.getCtx(), DocBaseType);
  50. return doc.length > 0 ? doc[0].get_ID() : 0;
  51. }
  52. /**
  53. * Get Client Document Type with DocBaseType
  54. * @param ctx context
  55. * @param DocBaseType base document type
  56. * @return array of doc types
  57. */
  58. static public MDocType[] getOfDocBaseType (Properties ctx, String DocBaseType)
  59. {
  60. final String whereClause = "AD_Client_ID=? AND DocBaseType=?";
  61. List<MDocType> list = new Query(ctx, Table_Name, whereClause, null)
  62. .setParameters(Env.getAD_Client_ID(ctx), DocBaseType)
  63. .setOnlyActiveRecords(true)
  64. .setOrderBy("IsDefault DESC, C_DocType_ID")
  65. .list();
  66. return list.toArray(new MDocType[list.size()]);
  67. } // getOfDocBaseType
  68. /**
  69. * Get Client Document Types
  70. * @param ctx context
  71. * @return array of doc types
  72. */
  73. static public MDocType[] getOfClient (Properties ctx)
  74. {
  75. List<MDocType> list = new Query(ctx, Table_Name, null, null)
  76. .setClient_ID()
  77. .setOnlyActiveRecords(true)
  78. .list();
  79. return list.toArray(new MDocType[list.size()]);
  80. } // getOfClient
  81. /**
  82. * Get Document Type (cached)
  83. * @param ctx context
  84. * @param C_DocType_ID id
  85. * @return document type
  86. */
  87. static public MDocType get (Properties ctx, int C_DocType_ID)
  88. {
  89. MDocType retValue = (MDocType)s_cache.get(C_DocType_ID);
  90. if (retValue == null)
  91. {
  92. retValue = new MDocType (ctx, C_DocType_ID, null);
  93. s_cache.put(C_DocType_ID, retValue);
  94. }
  95. return retValue;
  96. } // get
  97. /** Cache */
  98. static private CCache<Integer,MDocType> s_cache = new CCache<Integer,MDocType>(Table_Name, 20);
  99. /**************************************************************************
  100. * Standard Constructor
  101. * @param ctx context
  102. * @param C_DocType_ID id
  103. * @param trxName transaction
  104. */
  105. public MDocType(Properties ctx, int C_DocType_ID, String trxName)
  106. {
  107. super(ctx, C_DocType_ID, trxName);
  108. if (C_DocType_ID == 0)
  109. {
  110. // setName (null);
  111. // setPrintName (null);
  112. // setDocBaseType (null);
  113. // setGL_Category_ID (0);
  114. setDocumentCopies (0);
  115. setHasCharges (false);
  116. setIsDefault (false);
  117. setIsDocNoControlled (false);
  118. setIsSOTrx (false);
  119. setIsPickQAConfirm(false);
  120. setIsShipConfirm(false);
  121. setIsSplitWhenDifference(false);
  122. //
  123. setIsCreateCounter(true);
  124. setIsDefaultCounterDoc(false);
  125. setIsIndexed(true);
  126. }
  127. } // MDocType
  128. /**
  129. * Load Constructor
  130. * @param ctx context
  131. * @param rs result set
  132. * @param trxName transaction
  133. */
  134. public MDocType(Properties ctx, ResultSet rs, String trxName)
  135. {
  136. super(ctx, rs, trxName);
  137. } // MDocType
  138. /**
  139. * New Constructor
  140. * @param ctx context
  141. * @param DocBaseType document base type
  142. * @param Name name
  143. * @param trxName transaction
  144. */
  145. public MDocType (Properties ctx, String DocBaseType, String Name, String trxName)
  146. {
  147. this (ctx, 0, trxName);
  148. setAD_Org_ID(0);
  149. setDocBaseType (DocBaseType);
  150. setName (Name);
  151. setPrintName (Name);
  152. setGL_Category_ID ();
  153. } // MDocType
  154. /**
  155. * Set Default GL Category
  156. */
  157. public void setGL_Category_ID()
  158. {
  159. final String sql = "SELECT GL_Category_ID FROM GL_Category"
  160. +" WHERE AD_Client_ID=?"
  161. +" ORDER BY IsDefault DESC, GL_Category_ID";
  162. int GL_Category_ID = DB.getSQLValue(get_TrxName(), sql, getAD_Client_ID());
  163. setGL_Category_ID(GL_Category_ID);
  164. } // setGL_Category_ID
  165. /**
  166. * Set SOTrx based on document base type
  167. */
  168. public void setIsSOTrx ()
  169. {
  170. boolean isSOTrx = DOCBASETYPE_SalesOrder.equals(getDocBaseType())
  171. || DOCBASETYPE_MaterialDelivery.equals(getDocBaseType())
  172. || getDocBaseType().startsWith("AR");
  173. super.setIsSOTrx (isSOTrx);
  174. } // setIsSOTrx
  175. /**
  176. * String Representation
  177. * @return info
  178. */
  179. public String toString()
  180. {
  181. StringBuilder sb = new StringBuilder("MDocType[");
  182. sb.append(get_ID()).append("-").append(getName())
  183. .append(",DocNoSequence_ID=").append(getDocNoSequence_ID())
  184. .append("]");
  185. return sb.toString();
  186. } // toString
  187. /**
  188. * Is this a Quotation (Binding)
  189. * @return true if Quotation
  190. */
  191. public boolean isQuotation()
  192. {
  193. return DOCSUBTYPESO_Quotation.equals(getDocSubTypeSO())
  194. && DOCBASETYPE_SalesOrder.equals(getDocBaseType());
  195. } // isQuotation
  196. /**
  197. * Is this a Proposal (Not binding)
  198. * @return true if proposal
  199. */
  200. public boolean isProposal()
  201. {
  202. return DOCSUBTYPESO_Proposal.equals(getDocSubTypeSO())
  203. && DOCBASETYPE_SalesOrder.equals(getDocBaseType());
  204. } // isProposal
  205. /**
  206. * Is this a Proposal or Quotation
  207. * @return true if proposal or quotation
  208. */
  209. public boolean isOffer()
  210. {
  211. return (DOCSUBTYPESO_Proposal.equals(getDocSubTypeSO())
  212. || DOCSUBTYPESO_Quotation.equals(getDocSubTypeSO()))
  213. && DOCBASETYPE_SalesOrder.equals(getDocBaseType());
  214. } // isOffer
  215. /**
  216. * Get Print Name
  217. * @param AD_Language language
  218. * @return print Name if available translated
  219. */
  220. public String getPrintName (String AD_Language)
  221. {
  222. if (AD_Language == null || AD_Language.length() == 0)
  223. return super.getPrintName();
  224. return get_Translation (COLUMNNAME_PrintName, AD_Language);
  225. } // getPrintName
  226. /**
  227. * Before Save
  228. * @param newRecord new
  229. * @return true
  230. */
  231. protected boolean beforeSave (boolean newRecord)
  232. {
  233. /*if (getAD_Org_ID() != 0)
  234. setAD_Org_ID(0);*/
  235. return true;
  236. } // beforeSave
  237. /**
  238. * After Save
  239. * @param newRecord new
  240. * @param success success
  241. * @return success
  242. */
  243. protected boolean afterSave (boolean newRecord, boolean success)
  244. {
  245. if (newRecord && success)
  246. {
  247. // Add doctype/docaction access to all roles of client
  248. StringBuilder sqlDocAction = new StringBuilder("INSERT INTO AD_Document_Action_Access ")
  249. .append("(AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,")
  250. .append("C_DocType_ID , AD_Ref_List_ID, AD_Role_ID) ")
  251. .append("(SELECT ")
  252. .append(getAD_Client_ID()).append(",0,'Y', SysDate,")
  253. .append(getUpdatedBy()).append(", SysDate,").append(getUpdatedBy())
  254. .append(", doctype.C_DocType_ID, action.AD_Ref_List_ID, rol.AD_Role_ID ")
  255. .append("FROM AD_Client client ")
  256. .append("INNER JOIN C_DocType doctype ON (doctype.AD_Client_ID=client.AD_Client_ID) ")
  257. .append("INNER JOIN AD_Ref_List action ON (action.AD_Reference_ID=135) ")
  258. .append("INNER JOIN AD_Role rol ON (rol.AD_Client_ID=client.AD_Client_ID) ")
  259. .append("WHERE client.AD_Client_ID=").append(getAD_Client_ID())
  260. .append(" AND doctype.C_DocType_ID=").append(get_ID())
  261. .append(" AND rol.IsManual='N'")
  262. .append(")");
  263. int docact = DB.executeUpdate(sqlDocAction.toString(), get_TrxName());
  264. if (log.isLoggable(Level.FINE)) log.fine("AD_Document_Action_Access=" + docact);
  265. }
  266. return success;
  267. } // afterSave
  268. /**
  269. * Executed before Delete operation.
  270. *
  271. * @return true if delete is a success
  272. */
  273. protected boolean beforeDelete ()
  274. {
  275. // delete access records
  276. StringBuilder msgdb = new StringBuilder("DELETE FROM AD_Document_Action_Access WHERE C_DocType_ID=").append(get_ID());
  277. int docactDel = DB.executeUpdate(msgdb.toString(), get_TrxName());
  278. if (log.isLoggable(Level.FINE)) log.fine("Delete AD_Document_Action_Access=" + docactDel + " for C_DocType_ID: " + get_ID());
  279. return docactDel >= 0;
  280. } // beforeDelete
  281. /**
  282. * Returns Document type for the shipment/receipt based
  283. * on Document type provided for order/rma
  284. * @param docTypeId
  285. * @return shipment/receipt doctype id
  286. */
  287. public static int getShipmentReceiptDocType(int docTypeId)
  288. {
  289. int relatedDocTypeId = 0;
  290. if (docTypeId != 0)
  291. {
  292. MDocType docType = MDocType.get(Env.getCtx(), docTypeId);
  293. // FIXME: Should refactor code and remove the hard coded name
  294. // Should change document type to allow query the value
  295. if ("Return Material".equals(docType.getName()) ||
  296. "Vendor Return".equals(docType.getName())|| !docType.isSOTrx())
  297. {
  298. String relatedDocTypeName = null;
  299. if (("Purchase Order").equals(docType.getName()))
  300. {
  301. relatedDocTypeName = "MM Receipt";
  302. }
  303. else if ("Return Material".equals(docType.getName()))
  304. {
  305. relatedDocTypeName = "MM Returns";
  306. }
  307. else if ("Vendor Return".equals(docType.getName()))
  308. {
  309. relatedDocTypeName = "MM Vendor Returns";
  310. }
  311. if (relatedDocTypeName != null)
  312. {
  313. StringBuilder whereClause = new StringBuilder(30);
  314. whereClause.append("Name='").append(relatedDocTypeName).append("' ");
  315. whereClause.append("and AD_Client_ID=").append(Env.getAD_Client_ID(Env.getCtx()));
  316. whereClause.append(" AND IsActive='Y'");
  317. int relDocTypeIds[] = MDocType.getAllIDs(MDocType.Table_Name, whereClause.toString(), null);
  318. if (relDocTypeIds.length > 0)
  319. {
  320. relatedDocTypeId = relDocTypeIds[0];
  321. }
  322. }
  323. }
  324. else
  325. {
  326. relatedDocTypeId = docType.getC_DocTypeShipment_ID();
  327. }
  328. }
  329. return relatedDocTypeId;
  330. }
  331. /**
  332. * Get translated doctype name
  333. * @return Name if available translated
  334. */
  335. public String getNameTrl()
  336. {
  337. // warning: to cache this translation you need to change the cache to include language (see i.e. MWFNode)
  338. return get_Translation (COLUMNNAME_Name, Env.getAD_Language(getCtx()));
  339. } // getNameTrl
  340. } // MDocType