Ejemplo n.º 1
0
        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);
                    }
                }
            }
        }
Ejemplo n.º 2
0
        public void Add(AccTitle item)
        {
            if (item == null)
            {
                return;
            }
            if (item.Code == null)
            {
                return;
            }
            if (item.Code.Length == 0)
            {
                return;
            }
            char c = item.Code[0];

            switch (c)
            {
            case '1': Assets.Add(item);         break;

            case '2': Liabilitys.Add(item);     break;

            case '3': OwnersEquity.Add(item);   break;

            case '4': Revenues.Add(item);       break;

            case '5': Costs.Add(item);          break;

            case '6': Expenses.Add(item);       break;
            }
        }
Ejemplo n.º 3
0
        // othersideTitle第一個數字,就當TitleCode找出科目名字,要不然就直接填
        bool AddIfWant(DateTime date, string titleCode, string note, decimal money, bool isDebt, bool isCurrent, bool inDuration, string othersideTitle)
        {
            if (titleCode != SelectedTitleCode)
            {
                return(false);
            }
            if (!AccTitleList.IsVirtualTitle(titleCode))
            {
                if (!isCurrent && inDuration)                    // 要得期初值,所以isCurrent不要
                {
                    if (isDebt)
                    {
                        TitleSum -= (money * Direction);
                    }
                    else
                    {
                        TitleSum += (money * Direction);
                    }
                }
            }
            if (!isCurrent)
            {
                return(true);            // 不是本期的
            }
            CLedgerRow row = new CLedgerRow();

            row.Date = date;
            if (isDebt)
            {
                row.Debt = money; row.Credit = 0;
            }
            else
            {
                row.Debt = 0; row.Credit = money;
            }
            row.Note = note.TrimEnd();            // row.sum最後再一起算
            string titleName = "";

            if (othersideTitle != null)
            {
                if (char.IsDigit(othersideTitle[0]))
                {
                    int      tmp;
                    AccTitle t = NewList.FindTitleByCode(othersideTitle, out tmp);
                    if (t != null)
                    {
                        titleName = t.Name;
                    }
                }
                else
                {
                    titleName = othersideTitle;
                }
            }
            row.OthersideAccTitle = titleName;
            LedgerTable.Add(row);
            return(true);
        }
Ejemplo n.º 4
0
 public void SetDefaultTitle(string asset, string liability, string income, string cost, string expense, string ownersEquity)
 {
     defaultAsset        = Find(asset, Assets, null);
     defaultLiability    = Find(liability, Liabilitys, null);
     defaultIncome       = Find(income, Revenues, null);
     defaultCost         = Find(cost, Costs, null);
     defaultExpense      = Find(expense, Expenses, null);
     defaultOwnersEquity = Find(ownersEquity, OwnersEquity, null);
 }
Ejemplo n.º 5
0
 AccTitle Find(string code, List <AccTitle> table, AccTitle defaultTitle)
 {
     foreach (AccTitle r in table)
     {
         if (code == r.Code)
         {
             return(r);
         }
     }
     return(defaultTitle);
 }
Ejemplo n.º 6
0
 int CompareAccountingTable(AccTitle t1, AccTitle t2)
 {
     if (t1.Money > t2.Money)
     {
         return(-1);
     }
     else if (t2.Money > t1.Money)
     {
         return(1);
     }
     return(0);
 }
Ejemplo n.º 7
0
        List <AccTitle> CopyTable(List <AccTitle> list)
        {
            List <AccTitle> newList = new List <AccTitle>();

            foreach (AccTitle r in list)  // 從空的表Copy過來
            {
                AccTitle item = new AccTitle(r.Code, r.Name);
                item.Money = r.Money;     // 設定初值
                newList.Add(item);
            }
            return(newList);
        }
Ejemplo n.º 8
0
 List <AccTitle> CleanList(List <AccTitle> list)
 {
     for (int i = 0; i < list.Count;)
     {
         AccTitle acc = list[i];
         if (acc.Money == 0)
         {
             list.RemoveAt(i);
         }
         else
         {
             i++;
         }
     }
     return(list);
 }
Ejemplo n.º 9
0
        AccTitle FindTitleByCodeWithDefault(string code, AccTitle defaultTitle, out int credit)
        {
            AccTitle r;

            if (code.Length == 0)
            {
                credit = 0; return(null);
            }
            switch (code[0])
            {
            case '1': r = Find(code, Assets, defaultTitle); break;

            case '2': r = Find(code, Liabilitys, defaultTitle); break;

            case '3': r = Find(code, OwnersEquity, defaultOwnersEquity); break;

            case '4': r = Find(code, Revenues, defaultTitle); break;

            case '5': r = Find(code, Costs, defaultTitle); break;

            case '6': r = Find(code, Expenses, defaultTitle); break;

            default: credit = 0; return(null);
            }
            switch (r.Code[0])
            {
            case '1': credit = -1; break;

            case '2': credit = 1; break;

            case '3': credit = 1; break;

            case '4': credit = 1; break;

            case '5': credit = -1; break;

            case '6': credit = -1; break;

            default: credit = 0;   break;
            }
            return(r);
        }
Ejemplo n.º 10
0
        private void FormBankDetail_Load(object sender, EventArgs e)
        {
            SetupBindingSource();
            var bankAccountAdapter     = new VoucherExpense.DamaiDataSetTableAdapters.BankAccountTableAdapter();
            var accountingTitleAdapter = new VoucherExpense.DamaiDataSetTableAdapters.AccountingTitleTableAdapter();

            accountingTitleAdapter.Connection.ConnectionString = DB.SqlConnectString(MyFunction.HardwareCfg);

            bankAccountAdapter.Fill(m_DataSet.BankAccount);
            accountingTitleAdapter.Fill(m_DataSet.AccountingTitle);
            bankDetailAdapter.Fill(m_DataSet.BankDetail);
            //accountingTitleBindingSource.Filter =
            //    "(TitleCode like '1*' or TitleCode like '2*')";
            //int btm = bankDetailBindingNavigator.Bottom + 5;
            //dgvBankDetail.Top = btm - Top;
            //dgvBankDetail.Height = Height - dgvBankDetail.Top - 5;

            calendar.MaxDate = new DateTime(MyFunction.IntHeaderYear, 12, 31);
            calendar.MinDate = new DateTime(MyFunction.IntHeaderYear, 1, 1);

            MakeBankAccountComboBox();

            List <AccTitle> AssetList = new List <AccTitle>();

            foreach (var r in m_DataSet.AccountingTitle)
            {
                if (r.TitleCode.Length == 0)
                {
                    continue;
                }
                AccTitle item = new AccTitle(r.TitleCode, r.Name);
                if (r.IsInitialValueNull())
                {
                    item.Money = 0;
                }
                else
                {
                    item.Money = r.InitialValue;
                }
                char c = r.TitleCode[0];
                if (c == '1')
                {
                    AssetList.Add(item);
                }
            }

            foreach (var r in m_DataSet.BankAccount)
            {
                BankDictionary.Add(r.ID, new BankDefault(r.AccountTitleCode, r.DefaultTitleCode));
            }

            AccTitle defaultAsset = Find(Setup.DefaultAsset, AssetList, null);

            foreach (KeyValuePair <int, BankDefault> pair in BankDictionary)
            {
                BankDefault bank = pair.Value;
                bank.DefaultTitle = Find(bank.DefaultCode, AssetList, defaultAsset);
                AccTitle title = Find(bank.BankCode, AssetList, defaultAsset);
                if (title != null)
                {
                    bank.InitialValue = title.Money;
                }
            }

            if (cbSelectBank.Items.Count > 1)
            {
                cbSelectBank.SelectedIndex = cbSelectBank.Items.Count - 1;
            }
            if (MyFunction.LockAll)
            {
                dgvBankDetail.ReadOnly = true;
            }
        }
Ejemplo n.º 11
0
        private void LoadLedgerData()
        {
            if (DataPrepared)
            {
                return;
            }

            try
            {
                var headerSQLAdapter = new DamaiDataSetTableAdapters.HeaderTableAdapter();
                headerSQLAdapter.Fill(damaiDataSet.Header);
            }
            catch { MessageBox.Show("標頭資料讀取錯誤,你的資料庫版本可能不對"); }
            int count = damaiDataSet.Header.Count;

            if (count == 0)
            {
                MessageBox.Show("無資料!");
                Close();
                return;
            }
            var row = damaiDataSet.Header[count - 1];

            m_Revenue = new RevenueCalcBakery(row.DataDate, 0);
            AccList.NewAll();
            var expenseSQLAdapter       = new DamaiDataSetTableAdapters.ExpenseTableAdapter();
            var voucherSQLAdapter       = new DamaiDataSetTableAdapters.VoucherTableAdapter();
            var voucherDetailSQLAdapter = new DamaiDataSetTableAdapters.VoucherDetailTableAdapter();
            var bankDetailSQLAdapter    = new DamaiDataSetTableAdapters.BankDetailTableAdapter();
            var accVoucherSQLAdapter    = new DamaiDataSetTableAdapters.AccVoucherTableAdapter();
            var ingredientSQLAdapter    = new DamaiDataSetTableAdapters.IngredientTableAdapter();
            var bankAccountSQLAdapter   = new DamaiDataSetTableAdapters.BankAccountTableAdapter();
            var vendorSQLAdapter        = new DamaiDataSetTableAdapters.VendorTableAdapter();

            ingredientSQLAdapter.Connection.ConnectionString = DB.SqlConnectString(MyFunction.HardwareCfg);
            vendorSQLAdapter.Connection.ConnectionString     = DB.SqlConnectString(MyFunction.HardwareCfg);

            DamaiDataSet.AccountingTitleDataTable accTitleTable = damaiDataSet.AccountingTitle;
            try
            {
                bankAccountSQLAdapter.Fill(damaiDataSet.BankAccount);
                vendorSQLAdapter.Fill(damaiDataSet.Vendor);
                expenseSQLAdapter.Fill(damaiDataSet.Expense);          // expense檔案小,先全部讀進記憶體
                voucherSQLAdapter.Fill(damaiDataSet.Voucher);
                voucherDetailSQLAdapter.Fill(damaiDataSet.VoucherDetail);
                bankDetailSQLAdapter.Fill(damaiDataSet.BankDetail);
                accVoucherSQLAdapter.Fill(damaiDataSet.AccVoucher);
                ingredientSQLAdapter.Fill(damaiDataSet.Ingredient);
                foreach (var r in accTitleTable)
                {
                    AccTitle item = new AccTitle(r.TitleCode, r.Name);
                    if (r.IsInitialValueNull())
                    {
                        item.Money = 0;
                    }
                    else
                    {
                        item.Money = r.InitialValue;
                    }
                    if (r.TitleCode.Length == 0)
                    {
                        continue;
                    }
                    AccList.Add(item);
                }
                AccList1.CopyTableFrom(AccList);
                Setup.Load();
                m_Generator = new LedgerTableGenerator(labelMessage, Setup, damaiDataSet, new CalcRevenueDelegate(CalcRevenue));
            }

            catch (Exception ex)
            {
                MessageBox.Show("資料庫讀取錯誤! 原因:" + ex.Message);
                return;
            }
            DataPrepared = true;
        }
Ejemplo n.º 12
0
        private void ReportByTitle_Load(object sender, EventArgs e)
        {
            var headerAdapter = new MyHeaderAdapter();

            m_OrderSet = m_DataSet;
            var accTitleAdapter      = new VoucherExpense.DamaiDataSetTableAdapters.AccountingTitleTableAdapter();
            var bankAccountAdapter   = new VoucherExpense.DamaiDataSetTableAdapters.BankAccountTableAdapter();
            var expenseAdapter       = new VoucherExpense.DamaiDataSetTableAdapters.ExpenseTableAdapter();
            var voucherAdapter       = new VoucherExpense.DamaiDataSetTableAdapters.VoucherTableAdapter();
            var voucherDetailAdapter = new VoucherExpense.DamaiDataSetTableAdapters.VoucherDetailTableAdapter();
            var bankDetailAdapter    = new VoucherExpense.DamaiDataSetTableAdapters.BankDetailTableAdapter();
            var accVoucherAdapter    = new VoucherExpense.DamaiDataSetTableAdapters.AccVoucherTableAdapter();

            accTitleAdapter.Connection.ConnectionString = DB.SqlConnectString(MyFunction.HardwareCfg);

            try
            {
                headerAdapter.Fill(m_OrderSet.Header);
            }
            catch { MessageBox.Show("標頭資料讀取錯誤,你的資料庫版本可能不對"); }
            int count = m_OrderSet.Header.Count;

            if (count == 0)
            {
                MessageBox.Show("無資料!");
                Close();
                return;
            }
            var row = m_OrderSet.Header[count - 1];

            Revenue = new RevenueCalcBakery(row.DataDate, 0);
            AccList.NewAll();
            BankDictionary = new Dictionary <int, BankDefault>();
            RevenueCache   = new MonthlyReportData[12];
            MonthBalances  = new CMonthBalance[13];
            for (int i = 0; i < 13; i++)
            {
                MonthBalances[i]       = new CMonthBalance();
                MonthBalances[i].Month = i + 1;
            }
            MonthBalances[12].Month = 0;   // 第13月統計用
            cMonthBalanceBindingSource.DataSource = MonthBalances;
            string[] Name = new string[6] {
                "資產", "負債", "收入", "成本", "費用", "股東權益"
            };
            comboBox1.Items.Clear();
            comboBox2.Items.Clear();
            foreach (string str in Name)
            {
                comboBox1.Items.Add(str);
                comboBox2.Items.Add(str);
            }
            try
            {
                accTitleAdapter.Fill(m_DataSet.AccountingTitle);
                bankAccountAdapter.Fill(m_DataSet.BankAccount);
                expenseAdapter.Fill(m_DataSet.Expense);         // expense檔案小,先全部讀進記憶體
                voucherAdapter.Fill(m_DataSet.Voucher);
                voucherDetailAdapter.Fill(m_DataSet.VoucherDetail);
                bankDetailAdapter.Fill(m_DataSet.BankDetail);
                accVoucherAdapter.Fill(m_DataSet.AccVoucher);
                foreach (var r in m_DataSet.AccountingTitle)
                {
                    AccTitle item = new AccTitle(r.TitleCode, r.Name);
                    if (r.IsInitialValueNull())
                    {
                        item.Money = 0;
                    }
                    else
                    {
                        item.Money = r.InitialValue;
                    }
                    if (r.TitleCode.Length == 0)
                    {
                        continue;
                    }
                    AccList.Add(item);
                }
                AccList1.CopyTableFrom(AccList);

                foreach (var r in m_DataSet.BankAccount)
                {
                    BankDictionary.Add(r.ID, new BankDefault(r.AccountTitleCode, r.DefaultTitleCode));
                }
                comboBox1.SelectedIndex     = 0;
                comboBox2.SelectedIndex     = 1;
                comboBoxStart.SelectedIndex = MyFunction.IntHeaderMonth;
                Setup.Load();
                dataGridView1.Focus();
            }
            catch
            {
                MessageBox.Show("資料庫讀取錯誤!");
            }
        }
Ejemplo n.º 13
0
        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;
        }
Ejemplo n.º 14
0
        private void FormLedger_Load(object sender, EventArgs e)
        {
            accountingTitleBindingSource.DataSource = m_DataSet;
            m_OrderSet = m_DataSet;
            try
            {
                headerAdapter.Fill(m_DataSet.Header);
            }
            catch { MessageBox.Show("標頭資料讀取錯誤,你的資料庫版本可能不對"); }
            int count = m_DataSet.Header.Count;

            if (count == 0)
            {
                MessageBox.Show("無資料!");
                Close();
                return;
            }
            var row = m_DataSet.Header[count - 1];

            m_Revenue = new RevenueCalcBakery(row.DataDate, 0);

            AccList.NewAll();
            var accTitleAdapter      = new DamaiDataSetTableAdapters.AccountingTitleTableAdapter();
            var bankAccountAdapter   = new DamaiDataSetTableAdapters.BankAccountTableAdapter();
            var expenseAdapter       = new DamaiDataSetTableAdapters.ExpenseTableAdapter();
            var voucherAdapter       = new DamaiDataSetTableAdapters.VoucherTableAdapter();
            var voucherDetailAdapter = new DamaiDataSetTableAdapters.VoucherDetailTableAdapter();
            var bankDetailAdapter    = new DamaiDataSetTableAdapters.BankDetailTableAdapter();
            var accVoucherAdapter    = new DamaiDataSetTableAdapters.AccVoucherTableAdapter();
            var vendorAdapter        = new DamaiDataSetTableAdapters.VendorTableAdapter();
            var ingredientAdapter    = new DamaiDataSetTableAdapters.IngredientTableAdapter();

            try
            {
                ingredientAdapter.Connection.ConnectionString = DB.SqlConnectString(MyFunction.HardwareCfg);
                accTitleAdapter.Connection.ConnectionString   = DB.SqlConnectString(MyFunction.HardwareCfg);
                vendorAdapter.Connection.ConnectionString     = DB.SqlConnectString(MyFunction.HardwareCfg);

                accTitleAdapter.Fill(m_DataSet.AccountingTitle);
                bankAccountAdapter.Fill(m_DataSet.BankAccount);
                expenseAdapter.Fill(m_DataSet.Expense);         // expense檔案小,先全部讀進記憶體
                voucherAdapter.Fill(m_DataSet.Voucher);
                voucherDetailAdapter.Fill(m_DataSet.VoucherDetail);
                bankDetailAdapter.Fill(m_DataSet.BankDetail);
                accVoucherAdapter.Fill(m_DataSet.AccVoucher);
                vendorAdapter.Fill(m_DataSet.Vendor);
                ingredientAdapter.Fill(m_DataSet.Ingredient);

                foreach (var r in m_DataSet.AccountingTitle)
                {
                    AccTitle item = new AccTitle(r.TitleCode, r.Name);
                    if (r.IsInitialValueNull())
                    {
                        item.Money = 0;
                    }
                    else
                    {
                        item.Money = r.InitialValue;
                    }
                    if (r.TitleCode.Length == 0)
                    {
                        continue;
                    }
                    AccList.Add(item);
                }
                AccList1.CopyTableFrom(AccList);

                Setup.Load();
                m_Generator = new LedgerTableGenerator(labelMessage, Setup, m_DataSet, new CalcRevenueDelegate(CalcRevenue));
                cLedgerTableDataGridView.Focus();
            }
            catch (Exception ex)
            {
                MessageBox.Show("資料庫讀取錯誤! 原因:" + ex.Message);
            }

            // 設置comboBoxAccTitle的初值為 <零用金>
            if (m_DataSet.BankAccount.Rows.Count > 1)
            {
                var bank = m_DataSet.BankAccount[0];
                if (bank.IsAccountTitleCodeNull())
                {
                    comboBoxAccTitle.SelectedIndex = 0;
                }
                else
                {
                    comboBoxAccTitle.SelectedValue = bank.AccountTitleCode;
                }
            }
            DontRefresh = false;     // Form剛Load不Refresh DataGridView
        }
Ejemplo n.º 15
0
        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);
        }
Ejemplo n.º 16
0
        public void PostToAccount(string debit, string credit, decimal money, bool isCurrent, bool inDuration, AccTitle defaultTitle)
        {
            AccTitle r;
            // +++ 計算借方科目 +++
            int dir = 1;

            r = FindTitleByCodeWithDefault(debit, defaultTitle, out dir);
            if (r != null)
            {
                if (IsVirtualTitle(r.Code))         // 虛科目當月才加
                {
                    if (isCurrent)
                    {
                        r.Add(-money * dir);
                    }
                }
                else                                // 實科目在期限內都加
                {
                    if (inDuration)
                    {
                        r.Add(-money * dir);
                    }
                }
            }
            // +++ 計算貸方科目 +++
            r = FindTitleByCodeWithDefault(credit, defaultTitle, out dir);
            if (r != null)
            {
                if (IsVirtualTitle(r.Code))         // 虛科目當月才加
                {
                    if (isCurrent)
                    {
                        r.Add(money * dir);
                    }
                }
                else                                // 實科目在期限內都加
                {
                    if (inDuration)
                    {
                        r.Add(money * dir);
                    }
                }
            }
        }