/// <summary> /// Get Account /// </summary> /// <param name="AcctType"></param> /// <param name="as1"></param> /// <returns>Account</returns> public MAccount GetAccount(int AcctType, MAcctSchema as1) { if (AcctType < 0 || AcctType > 4) { return(null); } // String sql = "SELECT T_Due_Acct, T_Liability_Acct, T_Credit_Acct, T_Receivables_Acct, T_Expense_Acct " + "FROM C_Tax_Acct WHERE C_Tax_ID=" + _C_Tax_ID + " AND C_AcctSchema_ID=" + as1.GetC_AcctSchema_ID(); int validCombination_ID = 0; IDataReader idr = null; try { idr = DataBase.DB.ExecuteReader(sql, null, null); if (idr.Read()) { validCombination_ID = Utility.Util.GetValueOfInt(idr[AcctType]);// .getInt(AcctType + 1); // 1..5 } idr.Close(); } catch (Exception e) { if (idr != null) { idr.Close(); idr = null; } log.Log(Level.SEVERE, sql, e); } if (validCombination_ID == 0) { return(null); } return(MAccount.Get(as1.GetCtx(), validCombination_ID)); }
/// <summary> /// Create Landed Cost accounting & Cost lines /// </summary> /// <param name="as1">accounting schema</param> /// <param name="fact">fact</param> /// <param name="line">document line</param> /// <param name="dr">DR entry (normal api)</param> /// <returns>true if landed costs were created</returns> private bool LandedCost(MAcctSchema as1, Fact fact, DocLine line, bool dr) { int C_InvoiceLine_ID = line.Get_ID(); MLandedCostAllocation[] lcas = MLandedCostAllocation.GetOfInvoiceLine( GetCtx(), C_InvoiceLine_ID, GetTrx()); if (lcas.Length == 0) { return(false); } // Delete Old String sql = "DELETE FROM M_CostDetail WHERE C_InvoiceLine_ID=" + C_InvoiceLine_ID; int no = DataBase.DB.ExecuteQuery(sql, null, GetTrx()); if (no != 0) { log.Config("CostDetail Deleted #" + no); } // Calculate Total Base double totalBase = 0; for (int i = 0; i < lcas.Length; i++) { totalBase += Convert.ToDouble(lcas[i].GetBase());//.doubleValue(); } // Create New MInvoiceLine il = new MInvoiceLine(GetCtx(), C_InvoiceLine_ID, GetTrx()); for (int i = 0; i < lcas.Length; i++) { MLandedCostAllocation lca = lcas[i]; if (Env.Signum(lca.GetBase()) == 0) { continue; } double percent = totalBase / Convert.ToDouble(lca.GetBase()); String desc = il.GetDescription(); if (desc == null) { desc = percent + "%"; } else { desc += " - " + percent + "%"; } if (line.GetDescription() != null) { desc += " - " + line.GetDescription(); } // Accounting ProductCost pc = new ProductCost(GetCtx(), lca.GetM_Product_ID(), lca.GetM_AttributeSetInstance_ID(), GetTrx()); Decimal?drAmt = null; Decimal?crAmt = null; if (dr) { drAmt = lca.GetAmt(); } else { crAmt = lca.GetAmt(); } FactLine fl = fact.CreateLine(line, pc.GetAccount(ProductCost.ACCTTYPE_P_CostAdjustment, as1), GetC_Currency_ID(), drAmt, crAmt); fl.SetDescription(desc); // Cost Detail - Convert to AcctCurrency Decimal allocationAmt = lca.GetAmt(); if (GetC_Currency_ID() != as1.GetC_Currency_ID()) { allocationAmt = MConversionRate.Convert(GetCtx(), allocationAmt, GetC_Currency_ID(), as1.GetC_Currency_ID(), GetDateAcct(), GetC_ConversionType_ID(), GetAD_Client_ID(), GetAD_Org_ID()); } if (Env.Scale(allocationAmt) > as1.GetCostingPrecision()) { allocationAmt = Decimal.Round(allocationAmt, as1.GetCostingPrecision(), MidpointRounding.AwayFromZero); } if (!dr) { allocationAmt = Decimal.Negate(allocationAmt); } if (!IsPosted()) { MCostDetail cd = new MCostDetail(as1, lca.GetAD_Org_ID(), lca.GetM_Product_ID(), lca.GetM_AttributeSetInstance_ID(), lca.GetM_CostElement_ID(), allocationAmt, lca.GetQty(), // Qty desc, GetTrx()); cd.SetC_InvoiceLine_ID(C_InvoiceLine_ID); bool ok = cd.Save(); if (ok && !cd.IsProcessed()) { MClient client = MClient.Get(as1.GetCtx(), as1.GetAD_Client_ID()); if (client.IsCostImmediate()) { cd.Process(); } } } } log.Config("Created #" + lcas.Length); return(true); }
/** * Get Charge Account * @param C_Charge_ID charge * @param as account schema * @param amount amount for expense(+)/revenue(-) * @return Charge Account or null */ public static MAccount GetAccount(int C_Charge_ID, MAcctSchema aSchema, Decimal amount) { if (C_Charge_ID == 0 || aSchema == null) { return(null); } int acct_index = 1; // Expense (positive amt) if (amount < 0) { acct_index = 2; // Revenue (negative amt) } String sql = "SELECT CH_Expense_Acct, CH_Revenue_Acct FROM C_Charge_Acct WHERE C_Charge_ID=" + C_Charge_ID + " AND C_AcctSchema_ID=" + aSchema.GetC_AcctSchema_ID(); int Account_ID = 0; IDataReader dr = null; try { // PreparedStatement pstmt = DataBase.prepareStatement(sql, null); // pstmt.setInt (1, C_Charge_ID); // pstmt.setInt (2, aSchema.getC_AcctSchema_ID()); // ResultSet dr = pstmt.executeQuery(); dr = DB.ExecuteReader(sql, null, null); if (dr.Read()) { Account_ID = Util.GetValueOfInt(dr[acct_index - 1].ToString()); } dr.Close(); //pstmt.close(); } catch (SqlException e) { if (dr != null) { dr.Close(); } _log.Log(Level.SEVERE, sql, e); return(null); } finally { if (dr != null) { dr.Close(); } } // No account if (Account_ID == 0) { _log.Severe("NO account for C_Charge_ID=" + C_Charge_ID); return(null); } // Return Account MAccount acct = MAccount.Get(aSchema.GetCtx(), Account_ID); return(acct); } // getAccount