326 lines
10 KiB
C#
326 lines
10 KiB
C#
using System;
|
||
using System.Data;
|
||
using System.Data.OleDb;
|
||
using System.Web;
|
||
using System.Web.UI;
|
||
using System.Web.UI.WebControls;
|
||
using System.Linq;
|
||
using System.Data.Entity.Infrastructure;
|
||
|
||
public partial class admin_accounting_kind_reg2 : MyWeb.config
|
||
{
|
||
DataTable treeDt = new DataTable();
|
||
const int LevelMax = Model.accounting.KindLevelMax2; //分類層數
|
||
private Model.ezEntities _db = new Model.ezEntities();
|
||
protected void Page_Load(object sender, EventArgs e)
|
||
{
|
||
if (!IsPostBack) {
|
||
|
||
BuildTreeView();
|
||
|
||
if (!isStrNull(Request["num"]))
|
||
{
|
||
int _num = Val(Request["num"]);
|
||
var prod = _db.accounting_kind2.AsEnumerable().Where(q => q.num == _num).FirstOrDefault();
|
||
|
||
if (prod != null)
|
||
{
|
||
title_msg.Text = "修改<span class=\"text-primary\">【" + prod.kind + "】</span>的分類名稱";
|
||
item_name.Text = prod.kind;
|
||
title.Text = prod.title;
|
||
bank_name.Text = prod.bank_name;
|
||
bank_code.Text = prod.bank_code;
|
||
bank_id.Text = prod.bank_id;
|
||
record_payment.Checked = !isStrNull(prod.record_payment) && prod.record_payment.Equals("Y");
|
||
demo.Text = prod.demo;
|
||
HiddenField1.Value = prod.kind;
|
||
}
|
||
else
|
||
{
|
||
Response.Redirect(Request.Url.AbsolutePath );
|
||
|
||
}
|
||
|
||
edit.Visible = true;
|
||
add.Visible = false;
|
||
down_table.Visible = true;
|
||
start.Visible = false;
|
||
}
|
||
else
|
||
{
|
||
table.Visible = false;
|
||
down_table.Visible = false;
|
||
if (Convert.ToString(Request["msg"]) == "A") { start.Text = "【資料修改成功】"; }
|
||
else if (Convert.ToString(Request["msg"]) == "B") { start.Text = "【資料新增成功】"; }
|
||
else if (Convert.ToString(Request["msg"]) == "C") { start.Text = "【資料刪除成功】"; }
|
||
else { start.Text = "【請點選下方欲新增、修改或刪除的分類】"; }
|
||
}
|
||
|
||
}
|
||
}
|
||
|
||
#region 建立分類
|
||
|
||
|
||
protected void TreeTopology()
|
||
{
|
||
MyWeb.sql sql = new MyWeb.sql();
|
||
OleDbConnection sqlConn = sql.conn(db, p_name);
|
||
try
|
||
{
|
||
sqlConn.Open();
|
||
OleDbCommand sqlCmd = new OleDbCommand("", sqlConn);
|
||
sqlCmd.CommandText = "SELECT num,kind,root FROM accounting_kind2 ORDER BY kind,root, range";
|
||
treeDt = sql.dataTable(sqlCmd);
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
Response.Write(ex.Message);
|
||
}
|
||
finally
|
||
{
|
||
sqlConn.Close();
|
||
sqlConn.Dispose();
|
||
}
|
||
}
|
||
|
||
public void BuildTreeView()
|
||
{
|
||
TreeView2.Nodes.Clear();
|
||
TreeTopology();
|
||
BuildChild(0, TreeView2.Nodes);
|
||
treeDt.Dispose();
|
||
}
|
||
|
||
public void BuildChild(int RootUid, TreeNodeCollection Nodes, int Level=1)
|
||
{
|
||
DataTable dt = treeDt;
|
||
foreach (DataRow row in dt.Rows)
|
||
{
|
||
if (row["root"].ToString() == RootUid.ToString())
|
||
{
|
||
TreeNode NewNode = new TreeNode();
|
||
if (Convert.ToString(Request["num"]) == row["num"].ToString() & HiddenField1.Value != "AddMainItem")
|
||
{
|
||
NewNode.Text = "<b><span style=\"color:blue\">" + row["kind"].ToString() + "</span></b>";
|
||
if (Level +1 > LevelMax)
|
||
{
|
||
ImageButton5.Visible = false;
|
||
}
|
||
}
|
||
else
|
||
{
|
||
NewNode.Text = row["kind"].ToString();
|
||
}
|
||
NewNode.NavigateUrl = Request.Url.AbsolutePath + "?num=" + row["num"].ToString() ;
|
||
NewNode.Expand();
|
||
Nodes.Add(NewNode);
|
||
if (Level + 1 <= LevelMax)
|
||
{
|
||
BuildChild((int)row["num"], NewNode.ChildNodes, Level + 1);
|
||
}
|
||
|
||
}
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 新增分類按鈕事件
|
||
|
||
protected void ImageButton5_Click(object sender, EventArgs e)
|
||
{
|
||
title_msg.Text = "於<span class=\"text-primary\">【" + HiddenField1.Value + "】</span>分類下,新增次分類";
|
||
item_name.Text = "";
|
||
title.Text = "";
|
||
demo.Text = "";
|
||
bank_name.Text = "";
|
||
bank_code.Text = "";
|
||
bank_id.Text = "";
|
||
add.Visible = true;
|
||
edit.Visible = false;
|
||
down_table.Visible = false;
|
||
start.Visible = false;
|
||
record_payment.Checked = false;
|
||
}
|
||
protected void ImageButton4_Click(object sender, EventArgs e)
|
||
{
|
||
title_msg.Text = "<span class=\"text-primary\">於根目錄下,新增主分類</span>";
|
||
table.Visible = true;
|
||
item_name.Text = "";
|
||
title.Text = "";
|
||
demo.Text = "";
|
||
bank_name.Text = "";
|
||
bank_code.Text = "";
|
||
bank_id.Text = "";
|
||
record_payment.Checked =false;
|
||
add.Visible = true;
|
||
edit.Visible = false;
|
||
down_table.Visible = false;
|
||
start.Visible = false;
|
||
HiddenField1.Value = "AddMainItem";
|
||
BuildTreeView();
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 資料修改
|
||
|
||
protected void edit_Click(object sender, EventArgs e)
|
||
{
|
||
L_msg.Text = "";
|
||
|
||
int _num = Val(Request["num"]);
|
||
try
|
||
{
|
||
Model.accounting_kind2 accounting_kind2 = _db.accounting_kind2.Where(q => q.num == _num).FirstOrDefault();//修改
|
||
if (accounting_kind2 != null)
|
||
{
|
||
accounting_kind2.kind = item_name.Text;
|
||
accounting_kind2.title = title.Text;
|
||
accounting_kind2.demo = demo.Text;
|
||
accounting_kind2.bank_name = bank_name.Text;
|
||
accounting_kind2.bank_code = bank_code.Text;
|
||
accounting_kind2.bank_id = bank_id.Text;
|
||
accounting_kind2.record_payment = record_payment.Checked?"Y":"";
|
||
|
||
_db.SaveChanges();
|
||
Model.admin_log admin_log = new Model.admin_log();
|
||
admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Accounting, (int)Model.admin_log.Status.Update, "分類(收支帳戶):" + item_name.Text);
|
||
|
||
L_msg.Type = alert_type.success;
|
||
L_msg.Text = "操作成功";
|
||
}
|
||
else
|
||
{
|
||
L_msg.Type = alert_type.danger;
|
||
L_msg.Text = "無此資料";
|
||
}
|
||
|
||
}
|
||
catch (DbUpdateConcurrencyException)
|
||
{
|
||
L_msg.Type = alert_type.danger;
|
||
L_msg.Text = "Error";
|
||
throw;
|
||
}
|
||
|
||
|
||
BuildTreeView();
|
||
|
||
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 資料新增
|
||
|
||
protected void add_Click(object sender, EventArgs e)
|
||
{
|
||
L_msg.Text = "";
|
||
int range = 1;
|
||
int root = 0;
|
||
|
||
if (HiddenField1.Value != "AddMainItem")
|
||
{
|
||
root = Convert.ToInt32(Request["num"]);
|
||
}
|
||
|
||
try
|
||
{
|
||
var prod = _db.accounting_kind2.AsEnumerable().Where(q => q.root == root).OrderByDescending(q => q.range).FirstOrDefault();
|
||
if (prod != null)
|
||
if (prod.range.HasValue)
|
||
range = prod.range.Value + 1;
|
||
|
||
Model.accounting_kind2 accounting_kind2 = new Model.accounting_kind2();//新增
|
||
accounting_kind2.kind = item_name.Text;
|
||
accounting_kind2.title = title.Text;
|
||
accounting_kind2.root = root;
|
||
accounting_kind2.range = range;
|
||
accounting_kind2.demo = demo.Text;
|
||
accounting_kind2.bank_name = bank_name.Text;
|
||
accounting_kind2.bank_code = bank_code.Text;
|
||
accounting_kind2.bank_id = bank_id.Text;
|
||
accounting_kind2.record_payment = record_payment.Checked ? "Y" : "";
|
||
|
||
_db.accounting_kind2.Add(accounting_kind2);
|
||
_db.SaveChanges();
|
||
Model.admin_log admin_log = new Model.admin_log();
|
||
admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Accounting, (int)Model.admin_log.Status.Insert, "分類(收支帳戶):" + item_name.Text);
|
||
|
||
L_msg.Type = alert_type.success;
|
||
L_msg.Text = "操作成功";
|
||
}
|
||
catch (DbUpdateConcurrencyException)
|
||
{
|
||
throw;
|
||
}
|
||
|
||
|
||
BuildTreeView();
|
||
|
||
}
|
||
|
||
#endregion
|
||
|
||
#region 資料刪除
|
||
|
||
protected void ImageButton6_Click(object sender, EventArgs e)
|
||
{
|
||
int num = Val(Request["num"]);
|
||
del_product(num);
|
||
|
||
var prod = _db.accounting_kind2.AsEnumerable().Where(q => q.num == num).FirstOrDefault(); //刪除該筆資料
|
||
if (prod != null)
|
||
{
|
||
_db.accounting_kind2.Remove(prod);
|
||
_db.SaveChanges(); //執行
|
||
}
|
||
|
||
|
||
Del_Ohter_Items(Val(Request["num"]));
|
||
Model.admin_log admin_log = new Model.admin_log();
|
||
admin_log.writeLog(admin.info.u_id, (int)Model.admin_log.Systems.Accounting, (int)Model.admin_log.Status.Delete, "分類(收支帳戶):" + item_name.Text);
|
||
|
||
Response.Redirect(Request.Url.AbsolutePath + "?msg=C");
|
||
}
|
||
|
||
public void Del_Ohter_Items(int d_num)
|
||
{
|
||
var prod = _db.accounting_kind2.AsEnumerable().Where(q => q.root == d_num).ToList();
|
||
if (prod.Count > 0)
|
||
{
|
||
foreach (var row in prod)
|
||
{
|
||
del_product(row.num);
|
||
Del_Ohter_Items(row.num);
|
||
}
|
||
|
||
//查詢結果全部刪除
|
||
_db.accounting_kind2.RemoveRange(prod);
|
||
_db.SaveChanges();
|
||
}
|
||
|
||
}
|
||
|
||
public void del_product(int num)
|
||
{
|
||
var prod = _db.accountings.AsEnumerable().Where(q => q.kind2 == num).ToList();
|
||
if (prod.Count > 0)
|
||
{
|
||
////查詢結果全部刪除
|
||
//_db.accountings.RemoveRange(prod);
|
||
//_db.SaveChanges();
|
||
|
||
//清空分類
|
||
foreach (var item in prod)
|
||
item.kind2 = null;
|
||
_db.SaveChanges();
|
||
}
|
||
}
|
||
|
||
#endregion
|
||
|
||
|
||
} |