/// <summary> /// export all GL Transactions in the given year, towards the specified cost centres /// </summary> public static void ExportGLTransactions(string AOutputPath, char ACSVSeparator, string ANewLine, Int32 ALedgerNumber, Int32 AFinancialYear, string ACostCentres, string AIgnoreAccounts, string AIgnoreReferences, ref List <string> ACostCentresInvolved, ref List <string> AAccountsInvolved) { string filename = Path.GetFullPath(Path.Combine(AOutputPath, "transaction.csv")); Console.WriteLine("Writing file: " + filename); TDBTransaction Transaction = new TDBTransaction(); ATransactionTable transactions = new ATransactionTable(); ATransAnalAttribTable TransAnalAttrib = new ATransAnalAttribTable(); ATransactionTable allTransactionsInJournal = new ATransactionTable(); AGiftBatchTable giftbatches = new AGiftBatchTable(); AAccountTable accounts = new AAccountTable(); DBAccess.ReadTransaction(ref Transaction, delegate { TDataBase db = Transaction.DataBaseObj; string sql = String.Format("SELECT T.*, B.{4} AS a_transaction_date_d " + "FROM PUB_{8} AS B, PUB_{7} AS T " + "WHERE B.{9} = {10} AND B.{15} = {16} AND B.{11}='{12}' " + "AND T.{9} = B.{9} AND T.{0} = B.{0} " + "AND T.{13} IN ({14}) " + "AND NOT T.{17} IN ({19}) " + "AND NOT T.{20} IN ({21}) " + "ORDER BY {0}, {1}, {2}", ATransactionTable.GetBatchNumberDBName(), ATransactionTable.GetJournalNumberDBName(), ATransactionTable.GetTransactionNumberDBName(), ATransactionTable.GetTransactionAmountDBName(), ABatchTable.GetDateEffectiveDBName(), ATransactionTable.GetNarrativeDBName(), ATransactionTable.GetReferenceDBName(), ATransactionTable.GetTableDBName(), ABatchTable.GetTableDBName(), ATransactionTable.GetLedgerNumberDBName(), ALedgerNumber, ABatchTable.GetBatchStatusDBName(), MFinanceConstants.BATCH_POSTED, ATransactionTable.GetCostCentreCodeDBName(), "'" + ACostCentres.Replace(",", "','") + "'", ABatchTable.GetBatchYearDBName(), AFinancialYear, ATransactionTable.GetAccountCodeDBName(), ATransactionTable.GetDebitCreditIndicatorDBName(), "'" + AIgnoreAccounts.Replace(",", "','") + "'", ATransactionTable.GetReferenceDBName(), "'" + AIgnoreReferences.Replace(",", "','") + "'"); transactions = (ATransactionTable)db.SelectDT(transactions, sql, Transaction, null, 0, 0); // get the analysis attributes sql = String.Format("SELECT A.* from PUB_{1} AS B, PUB_{13} AS T, PUB_{0} AS A " + "WHERE B.{2} = {3} AND B.{4} = {5} AND B.{6}='{7}' " + "AND T.{2} = B.{2} AND T.{8} = B.{8} " + "AND T.{9} IN ({10}) " + "AND A.{2} = T.{2} AND A.{8} = T.{8} AND A.{11} = T.{11} AND A.{12} = T.{12}", ATransAnalAttribTable.GetTableDBName(), ABatchTable.GetTableDBName(), ABatchTable.GetLedgerNumberDBName(), ALedgerNumber, ABatchTable.GetBatchYearDBName(), AFinancialYear, ABatchTable.GetBatchStatusDBName(), MFinanceConstants.BATCH_POSTED, ATransactionTable.GetBatchNumberDBName(), ATransactionTable.GetCostCentreCodeDBName(), "'" + ACostCentres.Replace(",", "','") + "'", ATransactionTable.GetJournalNumberDBName(), ATransactionTable.GetTransactionNumberDBName(), ATransactionTable.GetTableDBName(), ABatchTable.GetBatchYearDBName()); db.SelectDT(TransAnalAttrib, sql, Transaction, null, 0, 0); TransAnalAttrib.DefaultView.Sort = ATransAnalAttribTable.GetBatchNumberDBName() + "," + ATransAnalAttribTable.GetJournalNumberDBName() + "," + ATransAnalAttribTable.GetTransactionNumberDBName(); // get a list of all batches involved List <Int64> batches = new List <Int64>(); StringBuilder batchnumbers = new StringBuilder(); foreach (ATransactionRow r in transactions.Rows) { if (!batches.Contains(r.BatchNumber)) { batches.Add(r.BatchNumber); batchnumbers.Append(r.BatchNumber.ToString() + ","); } } // get the other transactions in the same journal for finding the opposite cc/acc involved // for performance reasons, get all transactions of the whole batch sql = String.Format("SELECT DISTINCT TJ.* " + "FROM PUB_{0} AS TJ " + "WHERE TJ.{1} = {2} AND TJ.{3} IN ({4})", ATransactionTable.GetTableDBName(), ATransactionTable.GetLedgerNumberDBName(), ALedgerNumber, ATransactionTable.GetBatchNumberDBName(), batchnumbers.ToString() + "-1"); allTransactionsInJournal = (ATransactionTable)db.SelectDT(allTransactionsInJournal, sql, Transaction, null, 0, 0); allTransactionsInJournal.DefaultView.Sort = ATransactionTable.GetBatchNumberDBName() + "," + ATransactionTable.GetJournalNumberDBName(); // get all names of gift batches sql = String.Format("SELECT * FROM PUB_{0} " + "WHERE {1} = {2} " + "AND {3} = {4}", AGiftBatchTable.GetTableDBName(), AGiftBatchTable.GetLedgerNumberDBName(), ALedgerNumber, AGiftBatchTable.GetBatchYearDBName(), AFinancialYear); db.SelectDT(giftbatches, sql, Transaction, null, 0, 0); giftbatches.DefaultView.Sort = AGiftBatchTable.GetBatchNumberDBName(); sql = String.Format("SELECT * FROM PUB_{0} " + "WHERE {1} = {2}", AAccountTable.GetTableDBName(), AAccountTable.GetLedgerNumberDBName(), ALedgerNumber); db.SelectDT(accounts, sql, Transaction, null, 0, 0); accounts.DefaultView.Sort = AAccountTable.GetAccountCodeDBName(); }); StringBuilder sb = new StringBuilder(); int rowCounter = 0; foreach (ATransactionRow row in transactions.Rows) { if (row.DebitCreditIndicator) { row.TransactionAmount *= -1.0m; } StringBuilder attributes = new StringBuilder(); DataRowView[] RelatedTransactions = allTransactionsInJournal.DefaultView.FindRows(new object[] { row.BatchNumber, row.JournalNumber }); ATransactionRow[] OtherTransactions = GetOtherTransactions(row, RelatedTransactions); string OtherCostCentres = string.Empty; string OtherAccountCodes = string.Empty; if (OtherTransactions.Length < 30) { foreach (ATransactionRow r in OtherTransactions) { OtherCostCentres = StringHelper.AddCSV(OtherCostCentres, r.CostCentreCode); OtherAccountCodes = StringHelper.AddCSV(OtherAccountCodes, r.AccountCode); } } if (!ACostCentresInvolved.Contains(row.CostCentreCode)) { ACostCentresInvolved.Add(row.CostCentreCode); } if (!AAccountsInvolved.Contains(row.AccountCode)) { AAccountsInvolved.Add(row.AccountCode); } // we are using gift batch for receiving payments string Narrative = row.Narrative; if (Narrative.StartsWith("GB - Gift Batch ") && row.Reference.StartsWith("GB")) { // find the account and set the account description into the narrative try { DataRowView[] acc = accounts.DefaultView.FindRows(row.AccountCode); Narrative = ((AAccountRow)acc[0].Row).AccountCodeLongDesc; } catch (Exception) { } try { DataRowView[] gb = giftbatches.DefaultView.FindRows(Convert.ToInt32(row.Reference.Substring(2))); Narrative += " " + ((AGiftBatchRow)gb[0].Row).BatchDescription; } catch (Exception) { } } sb.Append(StringHelper.StrMerge( new string[] { "B" + row.BatchNumber.ToString() + "_J" + row.JournalNumber.ToString() + "_T" + row.TransactionNumber.ToString(), row.CostCentreCode, row.AccountCode, row.TransactionDate.ToString("yyyyMMdd"), OtherCostCentres, OtherAccountCodes, Narrative, row.Reference, String.Format("{0:N}", row.TransactionAmount), attributes.ToString() }, ACSVSeparator)); sb.Append(ANewLine); rowCounter++; if (rowCounter % 500 == 0) { TLogging.Log("Processing transactions " + rowCounter.ToString()); } } TLogging.Log("Processing transactions " + rowCounter.ToString()); StreamWriter sw = new StreamWriter(filename, false, Encoding.GetEncoding(1252)); sw.Write(sb.ToString()); sw.Close(); }
public static void GenerateHOSAReports(int ALedgerNumber, int APeriodNumber, int AIchNumber, string ACurrencySelect, out TVerificationResultCollection AVerificationResult ) { AVerificationResult = new TVerificationResultCollection(); //Begin the transaction bool NewTransaction = false; TDBTransaction DBTransaction = DBAccess.GDBAccessObj.GetNewOrExistingTransaction(IsolationLevel.ReadCommitted, out NewTransaction); ALedgerTable ALedger = ALedgerAccess.LoadByPrimaryKey(ALedgerNumber, DBTransaction); AJournalTable AJournal = new AJournalTable(); ATransactionTable ATransaction = new ATransactionTable(); ACostCentreTable ACostCentre = new ACostCentreTable(); try { #if TODO ALedgerRow LedgerRow = PostingDS.ALedger[0]; //Find the Ledger Name = Partner Short Name PPartnerTable PartnerTable = PPartnerAccess.LoadByPrimaryKey(LedgerRow.PartnerKey, DBTransaction); PPartnerRow PartnerRow = (PPartnerRow)PartnerTable.Rows[0]; string LedgerName = PartnerRow.PartnerShortName; #endif // // Load the Journals, and Transactions for this period: String JournalQuery = "SELECT PUB_a_journal.* FROM PUB_a_batch, PUB_a_journal WHERE " + "PUB_a_batch.a_ledger_number_i = " + ALedgerNumber + " AND PUB_a_batch.a_batch_year_i = " + ALedger[0].CurrentFinancialYear + " AND PUB_a_batch.a_batch_period_i = " + APeriodNumber + " AND PUB_a_batch.a_batch_status_c = 'Posted'" + " AND PUB_a_batch.a_ledger_number_i = PUB_a_journal.a_ledger_number_i" + " AND PUB_a_batch.a_batch_number_i = PUB_a_journal.a_batch_number_i"; DBAccess.GDBAccessObj.SelectDT(AJournal, JournalQuery, DBTransaction); String TransactionQuery = "SELECT PUB_a_transaction.* FROM PUB_a_batch, PUB_a_transaction WHERE " + "PUB_a_batch.a_ledger_number_i = " + ALedgerNumber + " AND PUB_a_batch.a_batch_year_i = " + ALedger[0].CurrentFinancialYear + " AND PUB_a_batch.a_batch_period_i = " + APeriodNumber + " AND PUB_a_batch.a_batch_status_c = 'Posted'" + " AND PUB_a_batch.a_ledger_number_i = PUB_a_transaction.a_ledger_number_i" + " AND PUB_a_batch.a_batch_number_i = PUB_a_transaction.a_batch_number_i"; DBAccess.GDBAccessObj.SelectDT(ATransaction, TransactionQuery, DBTransaction); String CostCentreQuery = "SELECT * FROM a_cost_centre WHERE " + ACostCentreTable.GetLedgerNumberDBName() + " = " + ALedgerNumber + " AND " + ACostCentreTable.GetPostingCostCentreFlagDBName() + " = True" + " AND " + ACostCentreTable.GetCostCentreTypeDBName() + " LIKE '" + MFinanceConstants.FOREIGN_CC_TYPE + "'" + " ORDER BY " + ACostCentreTable.GetCostCentreCodeDBName(); DBAccess.GDBAccessObj.SelectDT(ACostCentre, CostCentreQuery, DBTransaction); //Iterate through the cost centres foreach (ACostCentreRow CostCentreRow in ACostCentre.Rows) { bool TransactionExists = false; //Iterate through the journals foreach (AJournalRow JournalRow in AJournal.Rows) { int BatchNumber = JournalRow.BatchNumber; int JournalNumber = JournalRow.JournalNumber; #if TODO String TransFilter = ATransactionTable.GetBatchNumberDBName() + " = " + BatchNumber.ToString() + " AND " + ATransactionTable.GetJournalNumberDBName() + " = " + JournalNumber.ToString() + " AND " + ATransactionTable.GetCostCentreCodeDBName() + " = '" + CostCentreRow.CostCentreCode + "'" + " AND (" + ATransactionTable.GetIchNumberDBName() + " = 0" + " OR " + ATransactionTable.GetIchNumberDBName() + " = " + AIchNumber.ToString() + ")"; DataRow[] FoundTransRows = BatchDS.ATransaction.Select(TransFilter); foreach (DataRow untypedTransRow in FoundTransRows) { ATransactionRow TransactionRow = (ATransactionRow)untypedTransRow; TransactionExists = true; string DefaultData = ALedgerNumber.ToString() + "," + LedgerName + "," + APeriodNumber.ToString() + "," + APeriodNumber.ToString() + "," + CostCentreRow.CostCentreCode + "," + "" + "," + "" + "," + "" + "," + "A" + "," + LedgerRow.CurrentFinancialYear.ToString() + "," + LedgerRow.CurrentPeriod.ToString() + "," + MFinanceConstants.MAX_PERIODS.ToString() + "," + "h" + "," + ACurrency + "," + AIchNumber.ToString(); string ReportTitle = "Home Office Stmt of Acct: " + CostCentreRow.CostCentreName; //call code for gl2120p.p Produces Account Detail, Analysis Attribute and HOSA Reprint reports. /* RUN sm9000.w ("gl2120p.p", * lv_report_title_c, * lv_default_data_c).*/ //TODO: call code to produce reports break; } #endif if (TransactionExists) { //only need to run above code once for 1 transaction per cost centre code break; //goto next cost centre else try next journal } } } if (NewTransaction) { DBAccess.GDBAccessObj.RollbackTransaction(); } } catch (Exception Exp) { if (NewTransaction) { DBAccess.GDBAccessObj.RollbackTransaction(); } TLogging.Log(Exp.Message); TLogging.Log(Exp.StackTrace); throw; } }