protected void Button1_Click(object sender, EventArgs e)
    {
        Staff _operator = new Staff("Company");
        //物料名稱
        String my_MaterialName = txt_MaterialName.Text;
        //物料單位
        String my_MaterialUnit = txt_MaterialUnit.Text;
        //安全存量
        String my_MaterialSafeQuantity = txt_MaterialSafeQuantity.Text;
        //更新人員
        String USER;
        if (Session["UserID"] != null)
        {
            USER = Session["UserID"].ToString();
        }
        else
        {
            USER = "******";
        }
        if (hidden_Action.Value == "add")
        {
            string sql = "INSERT INTO ICS_Material (MaterialName,MaterialUnit,MaterialSafeQuantity,UpdateTime,UpdateUser,MaterialTypeID) VALUES  ('" + my_MaterialName.ToString() + "','" + my_MaterialUnit.ToString() + "','" + my_MaterialSafeQuantity.ToString() + "',getdate(),'" + USER + "','" + cbo_materialType.SelectedValue + "')  ";
            if (_operator.ExecuteStatement(sql))
            {
                ShowMsg2(UpdatePanel1, "儲存成功");
                UpdateServerData(); //更新Application Data
                Response.AddHeader("Refresh", "3; url=Inventory_AQM.aspx");
            }
            else
            {
                ShowMsg2(UpdatePanel1, "儲存失敗");

            }
        }
        else
        {
            SQLDB db = new SQLDB();
            DataSet ds = new DataSet();
            ds = db.Select("NO = '"+hidden_Materialid.Value+"'","","ICS_Material");
            DataRow dr = ds.Tables[0].Rows[0];
            dr["MaterialName"] = my_MaterialName.ToString();
            dr["MaterialUnit"] = my_MaterialUnit.ToString();
            dr["MaterialSafeQuantity"] = my_MaterialSafeQuantity.ToString();
            dr["UpdateTime"] = DateTime.Now;
            dr["UpdateUser"] = USER;
            dr["MaterialTypeID"] = cbo_materialType.SelectedValue;
            DataSet DSChange = ds.GetChanges(DataRowState.Modified);
            //string sql = "UPDATE from ICS_Material (MaterialName,MaterialUnit,MaterialSafeQuantity,UpdateTime,UpdateUser,MaterialTypeID) VALUES  ('" + my_MaterialName.ToString() + "','" + my_MaterialUnit.ToString() + "','" + my_MaterialSafeQuantity.ToString() + "',getdate(),'" + USER + "','" + cbo_materialType.SelectedValue + "')  ";
            if (db.Update(DSChange))
            {
                ShowMsg2(UpdatePanel1, "儲存成功");
                UpdateServerData(); //更新Application Data
                Response.AddHeader("Refresh", "3; url=Inventory_AQM.aspx");
            }
            else
            {
                ShowMsg2(UpdatePanel1, "儲存失敗");

            }
        }

    }
 protected void cmd_Save_Click1(object sender, EventArgs e)
 {
     SQLDB deviceModel = new SQLDB("DeviceKind");
     if (hidden_Action.Value.Equals("add")) {
         DataSet DS = new DataSet();
         DataTable DT = new DataTable("DeviceKind");
         DT.Columns.Add("DeviceKindId");
         DT.Columns.Add("DeviceKind");
         DT.Columns.Add("DeviceKindName");
         DS.Tables.Add(DT);
         DataRow DR = DS.Tables[0].NewRow();
         DR["DeviceKindId"] = deviceModel.Select().Tables[0].Rows.Count + 1;
         DR["DeviceKind"] = txt_equipmentKind.Text;
         DR["DeviceKindName"] = txt_DeviceKindName.Text;
         DS.Tables[0].Rows.Add(DR);
         DataSet _changed = DS.GetChanges();
         if (deviceModel.Insert(_changed))
         {
             ReDirect("新增成功");
         }
         else {
             ReDirect("新增失敗");
         }
     }
     else if (hidden_Action.Value.Equals("edit")){
         DataSet ds = (DataSet)Session["DS_Mis"];
         if (ds != null){
             ds.Tables[0].Rows[0]["DeviceKind"] = txt_equipmentKind.Text;
             ds.Tables[0].Rows[0]["DeviceKindName"] = txt_DeviceKindName.Text;
             DataSet DSChange = ds.GetChanges(DataRowState.Modified);
             if (deviceModel.Update(DSChange))
             {
                 ReDirect("修改成功");
             }
             else {
                 ReDirect("修改失敗");
             }
         }
     }
 }
    protected void gv_stockOutitem_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        SQLDB db = new SQLDB();
        //抓出庫單序號
        string stockoutitemid = gv_stockOutitem.DataKeys[e.RowIndex].Value.ToString();
        DataSet ds = db.Select("StockOutItemID = '" + stockoutitemid + "'", "", "ICS_StockOut_Item");
        if (ds.Tables[0].Rows.Count > 0)
        {
            TextBox reStockoutQuantity;
            reStockoutQuantity = (TextBox)gv_stockOutitem.Rows[e.RowIndex].Cells[7].Controls[0];
            ds.Tables[0].Rows[0]["ReConsumeQuantity"] = Convert.ToInt32(reStockoutQuantity.Text);
            DataSet DSChane = ds.GetChanges();
            if (db.Update(DSChane))
            {
                ShowMsg2(UpdatePanel1, "退繳成功");
            }

            gv_stockOutitem.EditIndex = -1;
            SearchStockItemProject(hid_stockOutid.Value);
        }

    }
Exemple #4
0
    protected void btn_saveAll_Click(object sender, EventArgs e)
    {
        DataSet ds_StockInItem = (DataSet)Session["StockInItem"];
        DataSet ds_StockProject = (DataSet)Session["ICS_Stock_Project"];
        #region 驗證是否有輸入資料
        if (ds_StockInItem == null || ds_StockProject == null)
        {
            ShowMsg2(UpdatePanel1, "請輸入資料");
            return;
        }
        #endregion
        #region 驗證物料成本跟計畫分攤成本有沒有相同
        int stockcost = 0;
        int projectcost = 0;
        //驗證物料成本跟計畫分攤成本有沒有相同
        if (ds_StockInItem != null)
        {
            for (int i = 0; i < ds_StockInItem.Tables[0].Rows.Count; i++)
            {
                stockcost += Convert.ToInt32(ds_StockInItem.Tables[0].Rows[i]["StockIn_Cost"].ToString());

            }
        }
        if (ds_StockProject!= null)
        {
            for (int j = 0; j < ds_StockProject.Tables[0].Rows.Count; j++)
            {
                projectcost += Convert.ToInt32(ds_StockProject.Tables[0].Rows[j]["ContractCost"].ToString());
            }
        }
        if (stockcost != projectcost)
        {
            ShowMsg2(UpdatePanel1, "入庫單總成本與計畫總成本不一致,請重新調整");
            return;
        }
        #endregion
        #region 新增入庫資料與計畫成本

        string userid = (string)Session["UserID"];
        string stockinid = "";
        SQLDB db = new SQLDB();
        DateTime now = DateTime.Now;
        
        if (hidden_Action.Value == "add")
        {
            //將入庫單時間紀錄起來,之後利用入庫單時間與登入者帳號抓取入庫單編號
            string Stockin_date = now.ToString("yyyy/MM/dd HH:mm:ss");
            string insertstockrecord = "INSERT INTO ICS_StockIn_Record ([UpdateTime],[UpdateUser]) VALUES ('" + Stockin_date + "','" + userid + "')";

            if (db.ExecuteStatement(insertstockrecord))
            {
                DataSet ds = db.Select("UpdateTime ='" + Stockin_date + "'and UpdateUser ='******'", "", "ICS_StockIn_Record");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    //抓到入庫單編號
                    stockinid = ds.Tables[0].Rows[0]["StockInID"].ToString();

                    //將入庫單編號塞到StockInItem裡
                    for (int i = 0; i < ds_StockInItem.Tables[0].Rows.Count; i++)
                    {
                        DataRow dr = ds_StockInItem.Tables[0].Rows[i];
                        dr["StockInID"] = stockinid;
                        dr["UpdateTime"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                        dr["UpdateUser"] = userid;
                    }
                    //將入庫單編號塞到StockProject裡
                    for (int j = 0; j < ds_StockProject.Tables[0].Rows.Count; j++)
                    {
                        DataRow dr = ds_StockProject.Tables[0].Rows[j];
                        dr["StockInID"] = stockinid;
                        dr["UpdateTime"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                        dr["UpdateUser"] = userid;
                    }
                    //刪除rowindex欄位
                    ds_StockInItem.Tables[0].Columns.Remove("rowindex");
                    ds_StockProject.Tables[0].Columns.Remove("rowindex");
                    DataSet DSChangeItem = ds_StockInItem.GetChanges();
                    DataSet DSChangeProject = ds_StockProject.GetChanges();
                    if (db.Insert(DSChangeItem))
                        ShowMsg2(UpdatePanel1, "入庫單內容新增成功");
                    else
                        ShowMsg2(UpdatePanel1, "入庫單內容新增失敗");

                    if (db.Insert(DSChangeProject))
                        ShowMsg2(UpdatePanel1, "入庫分攤成本新增成功");
                    else
                        ShowMsg2(UpdatePanel1, "入庫分攤成本新增失敗");
                }
            }

        }
        else//入庫單內容修改(目前暫時隱藏因為要考量的東西太多)
        {
            DataSet DSChangeItem = ds_StockInItem.GetChanges(DataRowState.Deleted);
            DataSet DSChangeProject = ds_StockProject.GetChanges(DataRowState.Deleted);
            if (DSChangeItem != null)
            {
                if (db.Update(DSChangeItem))
                    ShowMsg2(UpdatePanel1, "入庫單內容修改成功");
                else
                    ShowMsg2(UpdatePanel1, "入庫單內容修改失敗");
            }
            if (DSChangeProject != null)
            {
                if (db.Update(DSChangeProject))
                    ShowMsg2(UpdatePanel1, "入庫分攤成本修改成功");
                else
                    ShowMsg2(UpdatePanel1, "入庫分攤成本修改失敗");
            }
        }
        #endregion

        #region 更新物料庫存量與料價

        #region 舊寫法用庫存資料即時算出物料庫存量與料價再存到ICS_Inventory
        /* 
        //更新庫存量
        //庫存數量 = 目前庫存數量+(入庫數 X 轉換係數)

        //查詢庫存資料
        DataSet ds_inventory = db.Select("", "", "ICS_Inventory");
        //查詢物料轉換係數
        DataSet ds_material = db.Select("", "", "ICS_Material");
        for (int i = 0; i < ds_StockInItem.Tables[0].Rows.Count; i++)
        {
            string materialid = ds_StockInItem.Tables[0].Rows[i]["MaterialID"].ToString();
            string materialName = ds_StockInItem.Tables[0].Rows[i]["MaterialName"].ToString();
            int stockInQuantity = Convert.ToInt32(ds_StockInItem.Tables[0].Rows[i]["StockIn_Quantity"].ToString());
            int materialValue = Convert.ToInt32(ds_StockInItem.Tables[0].Rows[i]["Material_Single_Value"].ToString());
            int stockinCost = Convert.ToInt32(ds_StockInItem.Tables[0].Rows[i]["StockIn_Cost"].ToString());

            DataRow[] dr_invertory = ds_inventory.Tables[0].Select("MaterialID = '" + materialid + "'");
            DataRow[] dr_material = ds_material.Tables[0].Select("NO = '" + materialid + "'");
            double ConversionFactor = Convert.ToDouble(dr_material[0]["ConversionFactor"].ToString());//轉換係數
            //若庫存有資料
            if (dr_invertory.Length > 0)
            {
                double invetory_stockinquantity = Convert.ToDouble(dr_invertory[0]["StockInQuantity"].ToString());//庫存購入數量
                double invetory_q = Convert.ToDouble(dr_invertory[0]["Quantity"].ToString());//庫存量
                double inventoryCost = Convert.ToDouble(dr_invertory[0]["InventoryCost"].ToString());//庫存成本
                //更新庫存購入數量:庫存購入數量+入庫量
                dr_invertory[0]["StockInQuantity"] = invetory_stockinquantity + stockInQuantity;
                //更新庫存數量:庫存數量 = 目前庫存數量+(入庫數 X 轉換係數)
                dr_invertory[0]["Quantity"] = invetory_q + (stockInQuantity * ConversionFactor);
                //更新料價: (庫存成本+入庫成本)/(庫存數量+入庫數量)
                dr_invertory[0]["MaterialCost"] = (inventoryCost + stockinCost) / (stockInQuantity + invetory_stockinquantity);
                //更新庫存成本:料價 X 庫存購入數量
                dr_invertory[0]["InventoryCost"] = (inventoryCost + stockinCost) / (stockInQuantity + invetory_stockinquantity) * (invetory_stockinquantity + stockInQuantity);
                dr_invertory[0]["UpdateTime"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            }
            else//若庫存無此物料資料則新增
            {
                DataRow dr = ds_inventory.Tables[0].NewRow();
                dr["MaterialID"] = materialid;
                dr["MaterialName"] = materialName;
                dr["StockInQuantity"] = stockInQuantity;
                dr["Quantity"] = stockInQuantity * ConversionFactor;
                dr["MaterialCost"] = materialValue;
                dr["InventoryCost"] = stockinCost;
                dr["UpdateTime"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
                dr["UpdateUser"] = userid;
                ds_inventory.Tables[0].Rows.Add(dr);
            }

        }
        DataSet ds_invetoryEdit = ds_inventory.GetChanges();
        if (db.Update(ds_invetoryEdit))
            ShowMsg2(UpdatePanel1, "庫存量更新成功、料價更新成功");
        else
            ShowMsg2(UpdatePanel1, "庫存量更新成功、料價更新失敗");
         */
        #endregion

        #region 新寫法:從View_ICS_Inventory查出物料庫存量與料價再存到ICS_Inventory_History
        //更新庫存量
        //庫存數量 = 目前庫存數量+(入庫數 X 轉換係數)

        
        //組出要查詢的入庫物料
        StringBuilder stockinItem = new StringBuilder("MaterialID in (");
        for (int i = 0; i < ds_StockInItem.Tables[0].Rows.Count; i++)
        {
            string materialid  = ds_StockInItem.Tables[0].Rows[i]["MaterialID"].ToString();
            stockinItem.Append(" ' ");
            stockinItem.Append(materialid);
            stockinItem.Append(" ',");
        }
        stockinItem.Remove(stockinItem.Length - 1, 1);  //將最後的,號拿掉
        stockinItem.Append(")");
        
        //查詢View_ICS_Inventory即時物料庫存量與料價
        DataSet ds_viewInventory = db.Select(stockinItem.ToString(), "", "View_ICS_Inventory");
        //查詢庫存歷史資料
        DataSet ds_inventoryHis = db.Select(" 1= 0", "", "ICS_Inventory_History");

        for (int i = 0; i < ds_StockInItem.Tables[0].Rows.Count; i++)
        {
            string materialid = ds_StockInItem.Tables[0].Rows[i]["MaterialID"].ToString();
            string materialName = ds_StockInItem.Tables[0].Rows[i]["MaterialName"].ToString();
            string stockLocation = ds_StockInItem.Tables[0].Rows[i]["Stock_Location"].ToString();

            DataRow[] dr_viewinventory = ds_viewInventory.Tables[0].Select("MaterialID = '" + materialid + "'");
            DataRow dr = ds_inventoryHis.Tables[0].NewRow();
            dr["StockInid"] = stockinid;
            dr["MaterialID"] = materialid;
            dr["MaterialName"] = materialName;
            dr["StockInQuantity"] = Convert.ToDouble(ds_StockInItem.Tables[0].Rows[i]["StockIn_Quantity"].ToString());
            dr["Quantity"] = dr_viewinventory[0]["Quantity"];
            dr["MaterialCost"] = dr_viewinventory[0]["MaterialCost"];
            dr["InventoryCost"] = dr_viewinventory[0]["InventoryCost"];
            dr["StockLocation"] = stockLocation;
            dr["UpdateTime"] = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss");
            dr["UpdateUser"] = userid;
            ds_inventoryHis.Tables[0].Rows.Add(dr);
        }
        DataSet ds_invetoryEdit = ds_inventoryHis.GetChanges();
        if (db.Update(ds_invetoryEdit))
            ShowMsg2(UpdatePanel1, "庫存量更新成功、料價更新成功");
        else
            ShowMsg2(UpdatePanel1, "庫存量更新成功、料價更新失敗");
        #endregion
       
        #endregion
    }
    protected void Button_add_click(object sender, EventArgs e)
    {
        //通報者名稱
        string reportid = Session["UserID"].ToString();

        if (TextBox_FaultDescribe_add.Text.Length == 0)
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "window.alert('請輸入故障描述!');", true);
            return;
        }
        if (hidden_Action.Value == "edit")
        {
            SQLDB _operator = new SQLDB();
            DataSet ds_edit = _operator.Select("CaseID = '" + hidden_Caseid.Value + "'", "", "WarrantyNotify");
            DataRow dr = ds_edit.Tables[0].Rows[0];
            dr["FaultModel"] = this.DropDownList_FaultModel_add.Text;
            dr["FaultDescribe"] = this.TextBox_FaultDescribe_add.Text;
            dr["RepeatNotify"] = this.TextBox_RepeatNotify_add.Text;
            dr["WarrantyCompany"] = this.DropDownList_WarrantyCompany_add.SelectedItem.ToString();
            dr["WarrantyContract"] = int.Parse(this.DropDownList_WarrantyContract_add.SelectedValue);
            dr["NotifyDate"] = Convert.ToDateTime(this.TextBox_NotifyDate_add.Text);
             if (this.DropDownList_ContractCombineNum.SelectedIndex > 0)
            {
                dr["ContractCombineNum"] = int.Parse(this.DropDownList_ContractCombineNum.SelectedItem.ToString());
            }

             switch (this.DropDownList_RepairDateOption_add.SelectedIndex)
             {
                 case 0:
                     //修復日期選項
                     ScriptManager.RegisterStartupScript(this, this.GetType(), "", "window.alert('請點選修復日期選項!');", true);
                     return;
                 //break;
                 case 1:
                     dr["RepairDateOption"] = 1;
                     break;
                 case 2:
                     dr["RepairDateOption"] = 2;
                     break;
                 case 3:
                     dr["RepairDateOption"] = 3;
                     dr["RepairDeadline"] = Convert.ToDateTime(this.TextBox_RepairDeadline_add.Text);
                     break;
             }
             DataSet DSChange = ds_edit.GetChanges(DataRowState.Modified);
             if (_operator.Update(DSChange))
             {
                 ScriptManager.RegisterStartupScript(this, this.GetType(), "", "window.alert('修改成功');", true);
                 UpdateServerData();
             }
        }
        else
        {
            WarrantyNotify wn = new WarrantyNotify();
            wn.DeviceID = this.TextBox_DeviceID_add.Text;
            wn.FaultModel = this.DropDownList_FaultModel_add.Text;
            wn.FaultDescribe = this.TextBox_FaultDescribe_add.Text;
            wn.RepeatNotify = this.TextBox_RepeatNotify_add.Text;
            wn.WarrantyCompany = this.DropDownList_WarrantyCompany_add.SelectedItem.ToString();
            if (this.DropDownList_WarrantyContract_add.SelectedIndex > 0)
            {
                wn.WarrantyContract = int.Parse(this.DropDownList_WarrantyContract_add.SelectedValue);
            }
            wn.NotifyDate = Convert.ToDateTime(this.TextBox_NotifyDate_add.Text);
            wn.NotifyConfirm = false;
            if (this.DropDownList_ContractCombineNum.SelectedIndex > 0)
            {
                wn.ContractCombineNum = int.Parse(this.DropDownList_ContractCombineNum.SelectedItem.ToString());
            }

            wn.Status = "維修中";


            switch (this.DropDownList_RepairDateOption_add.SelectedIndex)
            {
                case 0:
                    //修復日期選項
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "window.alert('請點選修復日期選項!');", true);
                    return;
                //break;
                case 1:
                    wn.RepairDateOption = 1;
                    break;
                case 2:
                    wn.RepairDateOption = 2;
                    break;
                case 3:
                    wn.RepairDateOption = 3;
                    wn.RepairDeadline = Convert.ToDateTime(this.TextBox_RepairDeadline_add.Text);
                    break;
            }

            wn.Add();
            ScriptManager.RegisterStartupScript(this, this.GetType(), "", "window.alert('新增成功');", true);
            UpdateServerData();
        }

        if (chk_isSendMail.Checked)
        {
            try
            {
                //傳送通知email給廠商
                string[] mailaddress = new string[1];
                SQLDB _operateor = new SQLDB();
                DataSet ds = new DataSet();
                ds = _operateor.Select("CompanyID ='" + DropDownList_WarrantyCompany_add.SelectedValue + "'", "", "Company");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    mailaddress[0] = ds.Tables[0].Rows[0]["Email"].ToString();
                }
                Email notifyemail = new Email();

                //通報內容
                //string body = "";
                string first_td = " <td width='150' align='center' style='background-color: #c4ffde;' > ";
                string second_td = " <td align='center' style='background-color: #ffff9d;' > ";

                StringBuilder body = new StringBuilder();

                body.Append("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">");
                body.Append("<HTML><HEAD><META http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\">");
                body.Append("</HEAD><BODY>");

                body.Append(" <table width='80%' border='1' align='center' cellpadding='0' cellspacing='0'>");
                body.Append(" <tr> ");
                body.Append(" <td  colspan='2' align='center' style='background-color: #ffcc33;'><h3>  " + "新北市停車管理系統報修服務單" + "</h3> </td> ");
                body.Append(" </tr> ");
                body.Append(" <tr> ");
                body.Append(first_td + "設備編號" + " </td> ");
                body.Append(second_td + TextBox_DeviceID_add.Text + " </td> ");
                body.Append(" </td> ");
                body.Append(" </tr> ");
                body.Append(" <tr> ");
                body.Append(first_td + "通報者" + " </td> ");
                body.Append(second_td + reportid + " </td> ");
                body.Append(" </td> ");
                body.Append(" </tr> ");
                body.Append(" <tr> ");
                body.Append(first_td + "通報時間" + " </td> ");
                body.Append(second_td + TextBox_NotifyDate_add.Text + " </td> ");
                body.Append(" </td> ");
                body.Append(" </tr> ");
                body.Append(" <tr> ");
                body.Append(first_td + "指定修復日期" + " </td> ");
                body.Append(second_td + TextBox_RepairDeadline_add.Text + " </td> ");
                body.Append(" </td> ");
                body.Append(" </tr> ");
                body.Append(" <tr> ");
                body.Append(first_td + "損壞原因描述" + " </td> ");
                body.Append(second_td + TextBox_FaultDescribe_add.Text + " </td> ");
                body.Append(" </td> ");
                body.Append(" </tr> ");
                body.Append(" <tr> ");
                body.Append(" <td  colspan='2' align='center' style='background-color: #cccc99;'> " + "本郵件由發信系統主動發出,請勿直接回覆,如有任何問題或意見,請撥電話至停管中心" + " </td> ");
                body.Append(" </tr> ");
                body.Append(" </table> ");

                body.Append("</BODY></HTML>");

                if (notifyemail.toSend("新北市停車管理系統報修服務單", mailaddress, body.ToString()))
                {
                    ScriptManager.RegisterStartupScript(this, this.GetType(), "", "window.alert('郵件寄送成功');", true);
                }
            }
            catch (Exception ee)
            {
                ScriptManager.RegisterStartupScript(this, this.GetType(), "", "window.alert('新增保固單失敗!');", true);
            }
        }
    }