public void PostTo1Account(string title, decimal money, bool isDebt, bool isCurrent, bool inDuration) { if (title == null || title.Length == 0) { return; } int dir = 1; AccTitle r = FindTitleByCode(title, out dir); if (isDebt) { dir *= -1; // 借方多乘負號 } if (r != null) { if (IsVirtualTitle(r.Code)) // 虛科目當月才加 { if (isCurrent) { r.Add(money * dir); } } else // 實科目在期限內都加 { if (inDuration) { r.Add(money * dir); } } } }
private void comboBoxMonth_SelectedIndexChanged(object sender, EventArgs e) { ComboBox box = (ComboBox)sender; if (box.SelectedIndex < 0 || box.SelectedIndex > 12) { return; // 0 全期 } int mon = box.SelectedIndex; box.Enabled = false; // 設定初值 InitList(); labelRevenue.Text = labelBalance.Text = labelCostSum.Text = labelExpenseSum.Text = ""; Application.DoEvents(); bool inDuration; bool isCurrent; #region ======= 計算費用 ======= Message("計算費用"); foreach (var ro in m_DataSet.Expense) { if (ro.IsApplyTimeNull()) { continue; } if (ro.ApplyTime.Year != MyFunction.IntHeaderYear) { continue; } if (!ro.IsRemovedNull()) { if (ro.Removed) { continue; //作癈的不計 } } if (ro.IsMoneyNull()) { continue; } if (ro.RowState == DataRowState.Deleted) { continue; } int m1 = ro.ApplyTime.Month; inDuration = InDuration(m1, mon); isCurrent = IsCurrent(m1, mon); if (!inDuration && !isCurrent) { continue; } int bankID = 1; if (!ro.IsBankAccountIDNull()) { bankID = ro.BankAccountID; } string debitTitle, creditTitle; if (bankID == 1) // 零用金戶 ,貸方一定是零用金, 資產項要累計 { // +++ 計算貸方科目 +++ creditTitle = null; if (inDuration) // 期前的, 不用算實科目 { BankDefault bank; if (BankDictionary.TryGetValue(bankID, out bank)) // 如果沒有設定好銀行的TitleCode, 零用金會不平 { creditTitle = bank.BankCode; } } // +++ 計算借方科目 +++ if (ro.IsTitleCodeNull()) { debitTitle = NewList.defaultExpense.Code; } else { debitTitle = ro.TitleCode; } NewList.PostToAccount(debitTitle, creditTitle, ro.Money, isCurrent, inDuration); } else // 非零用金 { if (ro.IsTitleCodeNull()) { debitTitle = null; } else { debitTitle = ro.TitleCode; } if (ro.IsTitleCodeCreditNull()) { creditTitle = null; } else { creditTitle = ro.TitleCodeCredit; } NewList.PostToAccount(debitTitle, creditTitle, ro.Money, isCurrent, inDuration); } } #endregion #region ====== 計算轉帳傳票 ====== Message("計算轉帳傳票"); foreach (var ro in m_DataSet.AccVoucher) { if (ro.IsAccVoucherTimeNull()) { continue; } if (ro.AccVoucherTime.Year != MyFunction.IntHeaderYear) { continue; } if (!ro.IsRemovedNull()) { if (ro.Removed) { continue; //作癈的不計 } } if (ro.RowState == DataRowState.Deleted) { continue; } int m1 = ro.AccVoucherTime.Month; inDuration = InDuration(m1, mon); isCurrent = IsCurrent(m1, mon); if (!inDuration && !isCurrent) { continue; } if (!ro.IsTitleCode0Null() && (!ro.IsMoney0Null())) { NewList.PostTo1Account(ro.TitleCode0, ro.Money0, ro.IsDebt0, isCurrent, inDuration); } if (!ro.IsTitleCode1Null() && (!ro.IsMoney1Null())) { NewList.PostTo1Account(ro.TitleCode1, ro.Money1, ro.IsDebt1, isCurrent, inDuration); } if (!ro.IsTitleCode2Null() && (!ro.IsMoney2Null())) { NewList.PostTo1Account(ro.TitleCode2, ro.Money2, ro.IsDebt2, isCurrent, inDuration); } if (!ro.IsTitleCode3Null() && (!ro.IsMoney3Null())) { NewList.PostTo1Account(ro.TitleCode3, ro.Money3, ro.IsDebt3, isCurrent, inDuration); } } #endregion #region ======= 計算成本 ======= // 在付款總表裏是每個月都被4捨5入至小數第一位,所以要分12個月,進位後再加 Message("計算成本"); decimal[] shouldPayMonth = new decimal[13]; // index0不用,以加快運算速度 foreach (var ro in m_DataSet.Voucher) { if (ro.IsStockTimeNull()) { continue; } if (ro.StockTime.Year != MyFunction.IntHeaderYear) { continue; } if (!ro.IsRemovedNull()) { if (ro.Removed) { continue; } } if (ro.RowState == DataRowState.Deleted) { continue; } int m1 = ro.StockTime.Month; // if (m1 < MyFunction.IntHeaderMonth || m1 > mon) continue; if (ro.IsCostNull()) { continue; } if (InDuration(m1, mon)) { shouldPayMonth[m1] += ro.Cost; } if (IsCurrent(m1, mon)) // 加總到每個成本科目去 { foreach (var r1 in ro.GetVoucherDetailRows()) { AccTitle r; if (r1.IsTitleCodeNull()) { r = NewList.defaultCost; } else { r = AccTitleList.Find(r1.TitleCode, NewList.Costs, NewList.defaultCost); } if (r != null && (!r1.IsCostNull())) { r.Add(r1.Cost); } } } } decimal shouldPayTotal = 0; for (int i = 1; i <= 12; i++) { shouldPayTotal += System.Math.Round(shouldPayMonth[i], 1); // 應付貨款只精確到小數第一位,每個月進位 } AccTitle shouldPay = AccTitleList.Find(Setup.VoucherShouldPay, NewList.Liabilitys, NewList.defaultLiability); if (shouldPay != null) { shouldPay.Add(shouldPayTotal); } #endregion #region ====== 計算營業額 ====== Message("計算營業額"); AccTitle cash = AccTitleList.Find(Setup.CashIncome, NewList.Revenues, NewList.defaultIncome); AccTitle credit = AccTitleList.Find(Setup.CardIncome, NewList.Revenues, NewList.defaultIncome); AccTitle cashReceivable = AccTitleList.Find(Setup.CashReceivable, NewList.Assets, NewList.defaultAsset); AccTitle creditReceivable = AccTitleList.Find(Setup.CardReceivable, NewList.Assets, NewList.defaultAsset); MonthlyReportData total; for (int m1 = 1; m1 <= 12; m1++) { inDuration = InDuration(m1, mon); isCurrent = IsCurrent(m1, mon); if (!inDuration && !isCurrent) { continue; } total = CalcRevenue(m1); if (isCurrent) { if (cash != null) { cash.Add(total.Cash); } if (credit != null) { credit.Add(total.CreditCard); } } if (inDuration) { if (cashReceivable != null) { cashReceivable.Add(total.Cash); } if (creditReceivable != null) { creditReceivable.Add(total.CreditCard); } } } #endregion // MessageBox.Show("計算銀行前 1040 刷卡應付 " + creditReceivable.Money.ToString("N1")); #region ====== 計算銀行帳戶 ====== Message("計算銀行帳戶"); foreach (var ro in m_DataSet.BankDetail) { if (ro.IsDayNull()) { continue; } if (ro.Day.Year != MyFunction.IntHeaderYear) { continue; } if (ro.IsMoneyNull()) { continue; } if (ro.RowState == DataRowState.Deleted) { continue; } int m = ro.Day.Month; inDuration = InDuration(m, mon); isCurrent = IsCurrent(m, mon); if (!inDuration && !isCurrent) { continue; } if (ro.IsBankIDNull()) { continue; } if (ro.BankID <= 1) { continue; // 零用金不應存在此資料庫 } string code; BankDefault bank; if (!BankDictionary.TryGetValue(ro.BankID, out bank)) { continue; // 找不到科目的銀行帳號 } bool isPay = false; if (ro.IsIsCreditNull()) { isPay = true; } else { isPay = ro.IsCredit; } if (ro.IsTitleCodeNull() || ro.TitleCode.Length == 0) // 找到此帳戶預設的科目 { code = bank.DefaultCode; } else { code = ro.TitleCode; } if (isPay) { NewList.PostToAccount(code, bank.BankCode, ro.Money, isCurrent, inDuration, bank.DefaultTitle); } else { NewList.PostToAccount(bank.BankCode, code, ro.Money, isCurrent, inDuration, bank.DefaultTitle); } } #endregion // 設定顯示Label decimal sumExpense = SumMarkPercentAndSort(NewList.Expenses); decimal sumCost = SumMarkPercentAndSort(NewList.Costs); decimal sumRevenue = SumMarkPercentAndSort(NewList.Revenues); decimal sumAsset = SumMarkPercentAndSort(NewList.Assets); decimal sumLiability = SumMarkPercentAndSort(NewList.Liabilitys); decimal sumOwnersEquity = SumMarkPercentAndSort(NewList.OwnersEquity); labelExpenseSum.Text = sumExpense.ToString("N1"); labelCostSum.Text = sumCost.ToString("N1"); labelRevenue.Text = sumRevenue.ToString("N1"); labelBalance.Text = (sumRevenue - sumExpense - sumCost).ToString("N1"); labelAsset.Text = sumAsset.ToString("N1"); labelLiability.Text = sumLiability.ToString("N1"); labelEquity.Text = (sumAsset - sumLiability).ToString("N1"); labelOwnersEquity.Text = sumOwnersEquity.ToString("N1"); //AccTitle ownersEquity = AccTitleList.Find(Setup.OwnersEquity, NewList.Liabilitys, null); //if (ownersEquity != null) //{ // labelLiability1.Text = (sumLiability - ownersEquity.Money).ToString("N1"); // labelOwnersEquity.Text = (sumAsset - sumLiability + ownersEquity.Money).ToString("N1"); // } // 設定月對照表 if (mon != 0) { CMonthBalance b = MonthBalances[mon - 1]; b.Assest = sumAsset; b.Liability = sumLiability; b.Cost = sumCost; b.Revenue = sumRevenue; b.Expense = sumExpense; b.CalcBalance(); CMonthBalance sum = MonthBalances[12]; sum.ClearData(); for (int i = 0; i < 12; i++) { sum.Add(MonthBalances[i]); } cMonthBalanceBindingSource.ResetBindings(false); } // ====== 設定DataGridView ====== AccList.CleanListFrom(NewList); accountingTableBindingSource1.DataSource = AccList[comboBox1.SelectedIndex]; accountingTableBindingSource2.DataSource = AccList[comboBox2.SelectedIndex]; Message(""); box.Enabled = true; }
public List <CLedgerRow> CreateTable(int mon, string selectedTitleCode, AccTitleList sourceList) { SelectedTitleCode = selectedTitleCode; if (selectedTitleCode == "") { MessageBox.Show("請選會計科目!"); return(null); } LedgerTable = new List <CLedgerRow>(); NewList = InitList(sourceList); AccTitle selected = NewList.FindTitleByCode(selectedTitleCode, out Direction); if (selected == null) { MessageBox.Show("所選的科目<" + selectedTitleCode + "> 不在類別1至6!"); return(null); } TitleSum = selected.Money; // 年度初值 bool inDuration; bool isCurrent; //#region ====== 股東權益====== //AccTitle ownersEquity = AccTitleList.Find(Setup.DefaultOwnersEquity, NewList.Liabilitys, null); //if (ownersEquity != null) //{ // if (ownersEquity.Code == selectedTitleCode) // { // MessageBox.Show("什麼都和股東權益有關,沒有分類帳! 請看損益表"); // return null; // } //} //#endregion #region ======= 計算費用 ======= Message("計算費用"); foreach (var ro in m_DataSet.Expense) { if (ro.IsApplyTimeNull()) { continue; } if (ro.ApplyTime.Year != MyFunction.IntHeaderYear) { continue; } if (!ro.IsRemovedNull()) { if (ro.Removed) { continue; //作癈的不計 } } if (ro.IsMoneyNull()) { continue; } if (ro.RowState == DataRowState.Deleted) { continue; } int m1 = ro.ApplyTime.Month; inDuration = InDuration(m1, mon); if (!inDuration) { continue; // inDuratio 一定 isCurrent } isCurrent = IsCurrent(m1, mon); int bankID = 1; if (!ro.IsBankAccountIDNull()) { bankID = ro.BankAccountID; } string debitTitle, creditTitle; if (bankID == 1) // 零用金戶 ,貸方一定是零用金, 資產項要累計 { // +++ 計算貸方科目 +++ creditTitle = null; BankDefault bank; if (BankDictionary.TryGetValue(bankID, out bank)) // 如果沒有設定好銀行的TitleCode, 零用金會不平 { creditTitle = bank.BankCode; } else { MessageBox.Show("銀行帳号第一個<零用金>的設定有問題! 計算中止"); return(null); } // +++ 計算借方科目 +++ if (ro.IsTitleCodeNull()) { debitTitle = NewList.defaultExpense.Code; } else { debitTitle = ro.TitleCode; } //NewList.PostToAccount(debitTitle, creditTitle, ro.Money, isCurrent, inDuration); AddIfWant(ro.ApplyTime, debitTitle, ro.Note, ro.Money, true, isCurrent, inDuration, creditTitle); AddIfWant(ro.ApplyTime, creditTitle, ro.Note, ro.Money, false, isCurrent, inDuration, debitTitle); } else // 非零用金 { if (ro.IsTitleCodeNull()) { debitTitle = null; } else { debitTitle = ro.TitleCode; } if (ro.IsTitleCodeCreditNull()) { creditTitle = null; } else { creditTitle = ro.TitleCodeCredit; } //NewList.PostToAccount(debitTitle, creditTitle, ro.Money, isCurrent, inDuration); string note = ""; if (!ro.IsNoteNull()) { note = ro.Note; } AddIfWant(ro.ApplyTime, debitTitle, note, ro.Money, true, isCurrent, inDuration, creditTitle); AddIfWant(ro.ApplyTime, creditTitle, note, ro.Money, false, isCurrent, inDuration, debitTitle); } } #endregion #region ====== 計算轉帳傳票 ====== Message("計算轉帳傳票"); foreach (var ro in m_DataSet.AccVoucher) { if (ro.IsAccVoucherTimeNull()) { continue; } if (ro.AccVoucherTime.Year != MyFunction.IntHeaderYear) { continue; } if (!ro.IsRemovedNull()) { if (ro.Removed) { continue; //作癈的不計 } } if (ro.RowState == DataRowState.Deleted) { continue; } int m1 = ro.AccVoucherTime.Month; inDuration = InDuration(m1, mon); isCurrent = IsCurrent(m1, mon); if (!inDuration) { continue; } string othersideAccTitle = "<傳票" + ro.ID.ToString() + ">"; // 第一個不是數字,就直接顯示 string note = ""; if (!ro.IsNoteNull()) { note = ro.Note; } List <AccTemp> temp = new List <AccTemp>(); if (!ro.IsTitleCode0Null() && (!ro.IsMoney0Null()) && ro.Money0 != 0m) { temp.Add(new AccTemp(ro.TitleCode0, ro.Money0, ro.IsDebt0)); } if (!ro.IsTitleCode1Null() && (!ro.IsMoney1Null()) && ro.Money1 != 0m) { temp.Add(new AccTemp(ro.TitleCode1, ro.Money1, ro.IsDebt1)); } if (!ro.IsTitleCode2Null() && (!ro.IsMoney2Null()) && ro.Money2 != 0m) { temp.Add(new AccTemp(ro.TitleCode2, ro.Money2, ro.IsDebt2)); } if (!ro.IsTitleCode3Null() && (!ro.IsMoney3Null()) && ro.Money3 != 0m) { temp.Add(new AccTemp(ro.TitleCode3, ro.Money3, ro.IsDebt3)); } if (temp.Count == 2) // 只有二個,可以把對沖科目列出 { AddIfWant(ro.AccVoucherTime, temp[0].Code, note, temp[0].Money, temp[0].IsDebt, isCurrent, inDuration, temp[1].Code); AddIfWant(ro.AccVoucherTime, temp[1].Code, note, temp[1].Money, temp[1].IsDebt, isCurrent, inDuration, temp[0].Code); } else { foreach (AccTemp ac in temp) { AddIfWant(ro.AccVoucherTime, ac.Code, note, ac.Money, ac.IsDebt, isCurrent, inDuration, othersideAccTitle); } } } #endregion #region ======= 計算成本 ======= // 在付款總表裏是每個月都被4捨5入至小數第一位,所以要分12個月,進位後再加 Message("計算成本"); decimal[] shouldPayMonth = new decimal[13]; // index0不用,以加快運算速度 AccTitle shouldPay = AccTitleList.Find(Setup.VoucherShouldPay, NewList.Liabilitys, NewList.defaultLiability); if (shouldPay == null) { MessageBox.Show("<應付帳款>科目未設定, 計算可能發生錯誤!"); } foreach (var ro in m_DataSet.Voucher) { if (ro.IsStockTimeNull()) { continue; } if (ro.StockTime.Year != MyFunction.IntHeaderYear) { continue; } if (!ro.IsRemovedNull()) { if (ro.Removed) { continue; } } if (ro.RowState == DataRowState.Deleted) { continue; } int m1 = ro.StockTime.Month; // if (m1 < MyFunction.IntHeaderMonth || m1 > mon) continue; if (ro.IsCostNull()) { continue; } inDuration = InDuration(m1, mon); isCurrent = IsCurrent(m1, mon); string vendorName = ""; string sID = ro.ID.ToString(); if (ro.VendorRow != null) { vendorName = ro.VendorRow.Name; } if (inDuration) { shouldPayMonth[m1] += ro.Cost; if (shouldPay.Code == selectedTitleCode) // 先檢查下不浪費找SubRow時間 { string note = vendorName; // 應付帳款,note放廠商名 ,對立科目放貨單号 AddIfWant(ro.StockTime, shouldPay.Code, note, ro.Cost, false, isCurrent, inDuration, "<貨單" + sID + ">"); } } if (IsCurrent(m1, mon)) // 加總到每個成本科目去 { foreach (var r1 in ro.GetVoucherDetailRows()) { AccTitle r; if (r1.IsTitleCodeNull()) { r = NewList.defaultCost; } else { r = AccTitleList.Find(r1.TitleCode, NewList.Costs, NewList.defaultCost); } if (r != null && (!r1.IsCostNull())) { if (r.Code == selectedTitleCode) // 要找SubRow費時,先檢查一下 { string note = "<" + ro.ID.ToString("d4") + "> "; var ingredient = r1.IngredientRow; if (ingredient != null && (!ingredient.IsNameNull())) { note += r1.IngredientRow.Name; } if (!r1.IsVolumeNull()) { note += " " + r1.Volume.ToString(); } if (ingredient != null && (!ingredient.IsUnitNull())) { note += ingredient.Unit; } AddIfWant(ro.StockTime, r.Code, note, r1.Cost, true, isCurrent, inDuration, shouldPay.Name + "--" + vendorName); } } //r.Add(r1.Cost); } } } decimal shouldPayTotal = 0; for (int i = 1; i <= 12; i++) { shouldPayTotal += System.Math.Round(shouldPayMonth[i], 1); // 應付貨款只精確到小數第一位,每個月進位 } if (shouldPay != null) { shouldPay.Add(shouldPayTotal); } #endregion #region ====== 計算營業額 ====== Message("計算營業額"); AccTitle cashIncome = AccTitleList.Find(Setup.CashIncome, NewList.Revenues, NewList.defaultIncome); // 營收 現金 AccTitle creditIncome = AccTitleList.Find(Setup.CardIncome, NewList.Revenues, NewList.defaultIncome); // 營收 刷卡 AccTitle cashReceivable = AccTitleList.Find(Setup.CashReceivable, NewList.Assets, NewList.defaultAsset); AccTitle creditReceivable = AccTitleList.Find(Setup.CardReceivable, NewList.Assets, NewList.defaultAsset); MonthlyReportData total; // 算營業額很耗時, 先檢查是不是要的.分類帳要有每一天營收,要抓CalcRevenue內資料 if ((selectedTitleCode == cashIncome.Code) || (selectedTitleCode == creditIncome.Code) || (selectedTitleCode == cashReceivable.Code) || (selectedTitleCode == creditReceivable.Code)) { for (int m1 = 1; m1 <= 12; m1++) { inDuration = InDuration(m1, mon); if (!inDuration) { continue; } isCurrent = IsCurrent(m1, mon); List <MonthlyReportData> list; total = CalcRevenue(m1, out list); foreach (MonthlyReportData report in list) { DateTime da = report.Date; string note = da.Month.ToString() + "月" + da.Day.ToString() + "日 "; if (report.Cash != 0m) { AddIfWant(da, cashIncome.Code, cashIncome.Name + note, report.Cash, false, isCurrent, inDuration, cashReceivable.Code); AddIfWant(da, cashReceivable.Code, cashReceivable.Name + note, report.Cash, true, isCurrent, inDuration, cashIncome.Code); } if (report.CreditCard != 0m) { AddIfWant(da, creditIncome.Code, creditIncome.Name + note, report.CreditCard, false, isCurrent, inDuration, creditReceivable.Code); AddIfWant(da, creditReceivable.Code, creditReceivable.Name + note, report.CreditCard, true, isCurrent, inDuration, creditIncome.Code); } } } } #endregion // MessageBox.Show("計算銀行前 1040 刷卡應付 " + creditReceivable.Money.ToString("N1")); #region ====== 計算銀行帳戶 ====== Message("計算銀行帳戶"); foreach (var ro in m_DataSet.BankDetail) { if (ro.IsDayNull()) { continue; } if (ro.Day.Year != MyFunction.IntHeaderYear) { continue; } if (ro.IsMoneyNull()) { continue; } if (ro.RowState == DataRowState.Deleted) { continue; } int m = ro.Day.Month; inDuration = InDuration(m, mon); isCurrent = IsCurrent(m, mon); if (!inDuration && !isCurrent) { continue; } if (ro.IsBankIDNull()) { continue; } if (ro.BankID <= 1) { continue; // 零用金不應存在此資料庫 } string code; BankDefault bank; if (!BankDictionary.TryGetValue(ro.BankID, out bank)) { continue; // 找不到科目的銀行帳號 } if (ro.IsMoneyNull() || ro.Money == 0m) { continue; } bool isPay = false; if (ro.IsIsCreditNull()) { isPay = true; } else { isPay = ro.IsCredit; } if (ro.IsTitleCodeNull() || ro.TitleCode.Length == 0) // 找到此帳戶預設的科目 { code = bank.DefaultCode; } else { code = ro.TitleCode; } //if (isPay) // NewList.PostToAccount(code, bank.BankCode, ro.Money, isCurrent, inDuration, bank.DefaultTitle); //else // NewList.PostToAccount(bank.BankCode, code, ro.Money, isCurrent, inDuration, bank.DefaultTitle); string note = ""; if (!ro.IsNoteNull()) { note = ro.Note; } AddIfWant(ro.Day, code, note, ro.Money, isPay, isCurrent, inDuration, bank.BankCode); AddIfWant(ro.Day, bank.BankCode, note, ro.Money, !isPay, isCurrent, inDuration, code); } #endregion Comparison <CLedgerRow> compare = new Comparison <CLedgerRow>(CompareDate); LedgerTable.Sort(compare); bool isDebtTitle = AccTitleList.IsDebtTitle(selectedTitleCode); if (!AccTitleList.IsVirtualTitle(selectedTitleCode)) // 實科目有期初值,插入一行 { CLedgerRow row = new CLedgerRow(); int month = mon; if (month < 1 || month > 12) { month = 1; } row.Date = new DateTime(MyFunction.IntHeaderYear, month, 1); row.Note = "期初值"; if (TitleSum >= 0) { if (isDebtTitle) { row.Debt = TitleSum; row.Credit = 0; } else { row.Debt = 0; row.Credit = TitleSum; } } else { if (isDebtTitle) { row.Debt = 0; row.Credit = -TitleSum; } else { row.Debt = -TitleSum; row.Credit = 0; } } LedgerTable.Insert(0, row); } decimal sum = 0; foreach (CLedgerRow r in LedgerTable) { if (isDebtTitle) { sum = sum - r.Credit + r.Debt; } else { sum = sum + r.Credit - r.Debt; } r.Sum = sum; } Message(""); return(LedgerTable); }