private void SaleSpendRatio_Load(object sender, EventArgs e) { m_OrderSet = m_DataSet; SetupBindingSource(); var productAdapter = new VoucherExpense.DamaiDataSetTableAdapters.ProductTableAdapter(); var ingredientAdapter = new VoucherExpense.DamaiDataSetTableAdapters.IngredientTableAdapter(); var voucherAdapter = new VoucherExpense.DamaiDataSetTableAdapters.VoucherTableAdapter(); var voucherDetailAdapter = new VoucherExpense.DamaiDataSetTableAdapters.VoucherDetailTableAdapter(); productAdapter.Connection.ConnectionString = DB.SqlConnectString(MyFunction.HardwareCfg); ingredientAdapter.Connection.ConnectionString = DB.SqlConnectString(MyFunction.HardwareCfg); try { productAdapter.Fill(m_OrderSet.Product); ingredientAdapter.Fill(m_DataSet.Ingredient); voucherAdapter.Fill(m_DataSet.Voucher); voucherDetailAdapter.Fill(m_DataSet.VoucherDetail); } catch (Exception ex) { MessageBox.Show("資料庫讀取錯誤<" + ex.Message + ">, 後續操作無法進行!"); Close(); return; } cSaleItemBindingSource.DataSource = m_SaleList; this.dgViewSale.DataSource = cSaleItemBindingSource; stockItemBindingSource.DataSource = m_StockList; this.dgViewStock.DataSource = stockItemBindingSource; Reload(); }
private void ReportByVender_Load(object sender, EventArgs e) { SetupBindingSource(); var vendorAdapter = new VoucherExpense.DamaiDataSetTableAdapters.VendorTableAdapter(); var voucherAdapter = new VoucherExpense.DamaiDataSetTableAdapters.VoucherTableAdapter(); var voucherDetailAdapter = new VoucherExpense.DamaiDataSetTableAdapters.VoucherDetailTableAdapter(); var IngredientAdapter = new VoucherExpense.DamaiDataSetTableAdapters.IngredientTableAdapter(); vendorAdapter.Connection.ConnectionString = DB.SqlConnectString(MyFunction.HardwareCfg); IngredientAdapter.Connection.ConnectionString = DB.SqlConnectString(MyFunction.HardwareCfg); try { vendorAdapter.Fill(m_DataSet.Vendor); voucherAdapter.Fill(m_DataSet.Voucher); voucherDetailAdapter.Fill(m_DataSet.VoucherDetail); IngredientAdapter.Fill(m_DataSet.Ingredient); } catch (Exception ex) { MessageBox.Show("載入資料庫發生錯誤! 原因:" + ex.Message); } vendors.Add(new CNameIDForComboBox(0, " ")); foreach (var v in m_DataSet.Vendor) { vendors.Add(new CNameIDForComboBox(v.VendorID, v.Name)); } vendors.Add(new CNameIDForComboBox(int.MaxValue, "全部")); vendorIDComboBox.DataSource = vendors; cbBoxMonth.SelectedIndex = DateTime.Now.Month; this.voucherDGView.DataSource = null; // 原本先用了 vEDataSet.Voucher做格式 }
private void Voucher_Load(object sender, EventArgs e) { var accountingTitleAdapter = new VoucherExpense.DamaiDataSetTableAdapters.AccountingTitleTableAdapter(); var vendorAdapter = new VoucherExpense.DamaiDataSetTableAdapters.VendorTableAdapter(); var operatorAdapter = new VoucherExpense.DamaiDataSetTableAdapters.OperatorTableAdapter(); IngredientAdapter.Connection.ConnectionString = DB.SqlConnectString(MyFunction.HardwareCfg); vendorAdapter.Connection.ConnectionString = DB.SqlConnectString(MyFunction.HardwareCfg); operatorAdapter.Connection.ConnectionString = DB.SqlConnectString(MyFunction.HardwareCfg); accountingTitleAdapter.Connection.ConnectionString = DB.SqlConnectString(MyFunction.HardwareCfg); SetupBindingSource(); try { voucherDetailAdapter.Fill(m_DataSet.VoucherDetail); operatorAdapter.Fill(m_DataSet.Operator); IngredientAdapter.Fill(m_DataSet.Ingredient); accountingTitleAdapter.Fill(m_DataSet.AccountingTitle); vendorAdapter.Fill(m_DataSet.Vendor); voucherAdapter.Fill(m_DataSet.Voucher); MyFunction.SetControlLengthFromDB(this, m_DataSet.Voucher); } catch (Exception ex) { MessageBox.Show("載入資料出錯:" + ex.Message); } if (checkMode) { this.Text = "查核進貨"; blockEdit(); dgvVoucher.Columns["columnCheck"].ReadOnly = false; ckBoxAllowEdit.Visible = true; } if (MyFunction.LockAll) { blockEdit(); ckBoxAllowEdit.Visible = false; } if (MyFunction.IntHeaderYear != DateTime.Now.Year) { comboBoxMonth.SelectedIndex = comboBoxMonth.Items.Count - 1; } else { comboBoxMonth.SelectedIndex = DateTime.Now.Month; } // this.btnEditable.BringToFront(); BuildTitleCodeMenu(); dateTimePicker1.MaxDate = new DateTime(MyFunction.IntHeaderYear, 12, 31); dateTimePicker1.MinDate = new DateTime(MyFunction.IntHeaderYear, 1, 1); }
private void btnEvaluate_Click(object sender, EventArgs e) { // 先把螢幕內容存回 inventoryBindingSource.EndEdit(); DetailBindingSource(SaveToDataTable: true); try { DataRowView rowView = inventoryBindingSource.Current as DataRowView; if (rowView == null) { MessageBox.Show("沒有記錄,請先新增一筆!"); return; } var curr = rowView.Row as MyInventoryRow; var details = curr.GetInventoryDetailRows(); var productDetails = curr.GetInventoryProductsRows(); // 清除本單的進貨及金額 if (!isFrist) { foreach (var dRow in details) { dRow.StockMoney = 0; dRow.LostMoney = 0; dRow.CurrentIn = 0; } } MyInventoryRow prev = null; foreach (var r in m_DataSet.Inventory) // 找到前一張單子 { if (r.InventoryID >= curr.InventoryID) { continue; } if (prev == null) { prev = r; } else if (prev.InventoryID < r.InventoryID) { prev = r; } } if (prev == null) // 這是本年第一張 { //var detailTable = sQLVEDataSet.InventoryDetail.GetChanges() as SQLVEDataSet.InventoryDetailDataTable; //if (detailTable != null) inventoryDetailTableAdapter1.Update(sQLVEDataSet.InventoryDetail); //details = curr.GetInventoryDetailRows(); } else if (prev.IsCheckDayNull()) { MessageBox.Show("前期盤點單無日期,無法查找 進貨數量及估值!"); return; } else if (prev.Locked == false) { MessageBox.Show("前期" + prev.CheckDay.ToShortDateString() + "盤點單尚未核可, 本期估值無法進行!"); return; } else if (curr.IsCheckDayNull()) { MessageBox.Show("本盤點單無日期,無法查找 進貨數量及估值!"); return; } else if (curr.CheckDay.Year != MyFunction.IntHeaderYear) { MessageBox.Show("本單盤點日期有誤, 和資料年" + MyFunction.IntHeaderYear.ToString() + "不同,,無法查找 進貨數量及估值!"); return; } else if (curr.CheckDay.Date <= prev.CheckDay.Date) { MessageBox.Show("盤點單日期有誤, 比前期盤點表日期還早!"); return; } // 載入進貨單資料 if (m_VoucherAdapter == null) { m_VoucherAdapter = new MyVoucherAdapter(); m_VoucherAdapter.Fill(m_DataSet.Voucher); } if (m_VoucherDetailAdapter == null) { m_VoucherDetailAdapter = new MyVoucherDetailAdapter(); m_VoucherDetailAdapter.Fill(m_DataSet.VoucherDetail); } //inventoryBindingSource.SuspendBinding(); //inventoryDetailBindingSource.SuspendBinding(); // DataGridView是複雜Binding ,使用無效 // 算先進先出成本,暫存未計算成本的庫存量 Dictionary <int, CalcInventory> dicCalcStock = new Dictionary <int, CalcInventory>(); // 填CalcInventory 內容 if (prev != null) { foreach (var d in details) { CalcInventory inv = new CalcInventory(); int id = d.IngredientID; if (d.IsStockVolumeNull()) { inv.StockVolume = 0; } else { inv.StockVolume = d.StockVolume; } if (!d.IsAreaCodeNull()) { if (d.AreaCode == "???") { d.AreaCode = ""; // 清除??? ,後面再設上去 } } if (dicCalcStock.Keys.Contains(id)) // 表不正常, 重覆了 { d.Delete(); // 刪除重的這筆 } else { dicCalcStock.Add(id, inv); } } } // 找出期間的進貨單 DateTime prevDate = new DateTime(MyFunction.IntHeaderYear - 1, 12, 31); if (prev != null) { prevDate = prev.CheckDay.Date; // 盤點日當天的進貨單,都計入是本期的 } var vouchers = from vo in m_DataSet.Voucher where (!vo.IsStockTimeNull()) && (vo.StockTime.Date > prevDate) && (vo.StockTime.Date <= curr.CheckDay.Date) && (vo.IsRemovedNull() || (!vo.Removed)) orderby vo.StockTime descending select vo; // 計算本期進貨 if (isFrist) { } else { foreach (var vo in vouchers) { var ds = vo.GetVoucherDetailRows(); foreach (var d in ds) { if (d.IsIngredientIDNull()) { continue; } if (d.IsVolumeNull()) { continue; // 沒有數量 } if (d.Volume <= 0) { continue; } int id = d.IngredientID; var des = from de in details where de.IngredientID == id select de; if (des.Count() <= 0) { continue; // 盤點單找不到此食材 } var de1 = des.First(); double vol = (double)d.Volume; de1.CurrentIn += vol; CalcInventory inv = dicCalcStock[de1.IngredientID]; decimal dCost = 0; if (!d.IsCostNull()) { dCost = d.Cost; } inv.CurrInMoney += dCost; // 算出相對應庫存的成本 if (inv.StockVolume >= vol) { de1.StockMoney += dCost; inv.StockVolume -= vol; } else if (inv.StockVolume > 0) { de1.StockMoney += (dCost / (decimal)vol * (decimal)inv.StockVolume); // 小於 inv.StockVolume = 0; } } } } // 將前期盤點及位置 資料填入, 並計算多於進貨remainStock的成本 if (prev == null) { //foreach (SQLVEDataSet.InventoryDetailRow d in details) //{ // d.PrevStockVolume = 0; // CalcInventory inv = dicCalcStock[d.IngredientID]; // if (inv.StockVolume > 0) // RemainStockWarning(d, inv.StockVolume); // d.LostMoney = inv.CurrInMoney - d.StockMoney; // 沒有前期 //} //foreach (var p in productDetails) // p.PrevVolume = 0; try { DataRowView inventoryrow = inventoryBindingSource.Current as DataRowView; if (rowView == null) { return; } var inventory = rowView.Row as MyInventoryRow; inventory.IngredientsCost = 0; var detailrows = inventory.GetInventoryDetailRows(); foreach (var item in detailrows) { if (item.IsStockMoneyNull())//计算初值时,如果输入初值金额,就不估算,没有就估算 { var ingredientrows = from r in m_DataSet.Ingredient where r.IngredientID == item.IngredientID select r; // vEDataSet.Ingredient.Select("IngredientID=" + item.IngredientID); if (item.IsStockVolumeNull()) { continue; } if (ingredientrows.Count() == 0) { continue; } item.StockMoney = (decimal)(item.StockVolume * ingredientrows.First().Price); } else { if (item.StockMoney == 0) { continue; } if (item.IsStockVolumeNull() || item.StockVolume == 0) // 本年第一張 ,數量為0 , 金額不為0 ,不可 { item.StockMoney = 0; } } inventory.IngredientsCost += item.StockMoney; } // 產品不管是不是第一單,在下方都直接以 Volume*EvaluatedCost代入 } catch (Exception ex) { MessageBox.Show("错误:" + ex.Message); }; } else { // 找出食材前期值代入 var prevDetails = prev.GetInventoryDetailRows(); foreach (var d in details) { d.PrevStockVolume = 0; int dIngredientID = d.IngredientID; CalcInventory inv = dicCalcStock[dIngredientID]; double remainStock = inv.StockVolume; var ds = from r in prevDetails where r.IngredientID == dIngredientID select r; if (ds.Count() <= 0) { if (remainStock > 0) { RemainStockWarning(d, remainStock); } continue; } var p = ds.First(); if (!p.IsAreaCodeNull()) { if (d.IsAreaCodeNull() || d.AreaCode.Trim().Count() == 0) // 沒有位置資料才從前期代入 { d.AreaCode = p.AreaCode; } } if (!p.IsStockVolumeNull()) { d.PrevStockVolume = p.StockVolume; if (remainStock > 0) // 庫存量大於進貨量,所以從前一張盤點單算平均成本 { if (p.StockVolume > 0) { if (remainStock > p.StockVolume) { d.StockMoney += p.StockMoney; var ings = from r in m_DataSet.Ingredient where r.IngredientID == dIngredientID select r; if (ings.Count() > 0) { var ing = ings.First(); MessageBox.Show("產品<" + ing.Name + "> ,庫存量大於(本期進貨+前期庫存) " + (remainStock - p.StockVolume).ToString() + ing.Unit + ",多餘部分 估值為 0 !!!"); d.AreaCode = "???"; } } else { d.StockMoney += p.StockMoney / (decimal)p.StockVolume * (decimal)remainStock; } } else { RemainStockWarning(d, remainStock); } } } else { if (remainStock > 0) { RemainStockWarning(d, remainStock); } } // 計算損失金額, 必需在RemainStock計算完之後d.StockMoney才有值 if (!p.IsStockMoneyNull()) { d.LostMoney = inv.CurrInMoney + p.StockMoney - d.StockMoney; } else { d.LostMoney = inv.CurrInMoney - d.StockMoney; } } // 找出前期產品庫存值代入 var prevProducts = prev.GetInventoryProductsRows(); foreach (var pd in productDetails) { var ds = from r in prevProducts where r.ProductID == pd.ProductID select r; if (ds.Count() > 0) { var d = ds.First(); if (!d.IsVolumeNull()) { pd.PrevVolume = d.Volume; } } else { pd.PrevVolume = 0; } } } // 找出產品表.EvaluatedCost 計算產品Cost decimal productCost = 0; foreach (var pd in productDetails) { pd.Cost = 0m; if ((!pd.IsVolumeNull()) && pd.Volume > 0) { var ps = from r in m_OrderSet.Product where r.ProductID == pd.ProductID select r; if (ps.Count() > 0) { var p = ps.First(); if (!p.IsEvaluatedCostNull()) { pd.Cost = p.EvaluatedCost * (decimal)pd.Volume; productCost += pd.Cost; } } } } // 計算總食材庫材及損失 decimal ingredientsCost = 0, ingredientsLost = 0; foreach (var d in details) { if (!d.IsStockMoneyNull()) { ingredientsCost += d.StockMoney; } if (!d.IsLostMoneyNull()) { ingredientsLost += d.LostMoney; } } curr.IngredientsCost = Math.Round(ingredientsCost, 1); curr.IngredientsLost = Math.Round(ingredientsLost, 1); curr.ProductsCost = Math.Round(productCost, 1); curr.EvaluatedDate = DateTime.Now; inventoryBindingSource.ResetBindings(false); DetailBindingSource(SaveToDataTable: false); } catch (Exception ex) { MessageBox.Show("計算估值時發生錯誤,原因:" + ex.Message); } }
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("資料庫讀取錯誤!"); } }
private void DeletetoolStripButton_Click(object sender, EventArgs e) { if (!this.Validate()) { MessageBox.Show("有資料錯誤, 請改好再作刪除操作!"); return; } this.IngredientBindingSource.EndEdit(); string str = this.IngredientIDTextBox.Text.Trim(); int ingredientID = 0; string name; try { ingredientID = Convert.ToInt32(str); name = nameTextBox.Text.Trim(); } catch { MessageBox.Show("要刪除的食材內碼必需是數字!"); return; } string strCodeName = "食材 <" + ingredientID.ToString() + ">" + name; if (MessageBox.Show("能刪除的食材必需是本年度從來沒有進過貨的\r\n按'確定', 開始載入並檢查全年度進貨單!", "刪除" + strCodeName, MessageBoxButtons.OKCancel) != DialogResult.OK) { return; } if (!m_VoucherDetailLoaded) { try { VoucherAdapter.Fill(m_DataSet.Voucher); VoucherDetailAdapter.Fill(m_DataSet.VoucherDetail); } catch (Exception ex) { MessageBox.Show("載入客人進貨細項時,資料庫發生錯誤:" + ex.Message); return; } m_VoucherDetailLoaded = true; } foreach (var row in m_DataSet.VoucherDetail) { if (row.IsIngredientIDNull()) { continue; } if (row.IngredientID == ingredientID) { var voucher = row.VoucherRow; MessageBox.Show("進貨單" + voucher.ID.ToString() + " 己經進過" + strCodeName + " 無法刪除"); return; } } if (MessageBox.Show(strCodeName + " 本年度沒有進過此貨,可以被刪除\r\n按'確定' 刪除", strCodeName, MessageBoxButtons.OKCancel) == DialogResult.OK) { try { IngredientBindingSource.RemoveCurrent(); IngredientAdapter.Update(m_DataSet.Ingredient); } catch (Exception ex) { MessageBox.Show("刪除" + strCodeName + "及存檔過程錯誤:" + ex.Message); return; } MessageBox.Show("己刪除" + strCodeName + " 並存檔成功!"); return; } MessageBox.Show("沒有刪除 " + strCodeName); }