o_form.aspx.cs
上传用户:xrffrp
上传日期:2022-03-25
资源大小:22155k
文件大小:16k
源码类别:

OA系统

开发平台:

ASP/ASPX

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using System.Data.SqlClient;
  12. using System.Net.Mail;
  13. using System.Net;
  14. using System.Net.Sockets;
  15. public partial class web_approvel_ch_o_form : System.Web.UI.Page
  16. {
  17.     private int count = 0;
  18.     ArrayList apply_type = new ArrayList();
  19.     ArrayList operator_id = new ArrayList();
  20.     protected void Page_Load(object sender, EventArgs e)
  21.     {
  22.         if (!IsPostBack)
  23.         {
  24.             LoginDAO.CheckLogin(Session, Response, "../../", 2);
  25.             if (Session["user"] != null)
  26.             {
  27.                 DateMgr mgr = new DateMgr();
  28.                 TxtFormId.Text = mgr.getTime();
  29.                 TxtApplyDate.Text = mgr.getDateTime();
  30.                 User user = (User)Session["user"];
  31.                 TxtEmpName.Text = user.Emp_no + user.Emp_name;
  32.                 EmpId.Value = user.Id.ToString();
  33.                 TxtPosition.Text = user.Position;
  34.                 DepartmentId.Value = user.Department.Id.ToString();
  35.                 TxtDepartment.Text = user.Department.Full_name;
  36.                 PositionSequence.Value = user.PositionSequence.ToString();
  37.                 PositionId.Value = user.PositionId.ToString();
  38.             }
  39.         }
  40.     }
  41.     protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
  42.     {
  43.         if (Calendar1.Visible == true)
  44.         {
  45.             Calendar1.Visible = false;
  46.         }
  47.         else
  48.         {
  49.             Calendar1.Visible = true;
  50.         }
  51.     }
  52.     protected void Calendar1_SelectionChanged(object sender, EventArgs e)
  53.     {
  54.         Calendar1.Visible = false;
  55.         string year = Calendar1.SelectedDate.Year.ToString();
  56.         string month = Calendar1.SelectedDate.Month.ToString();
  57.         if (month.Length == 1)
  58.         {
  59.             month = "0" + month;
  60.         }
  61.         string day = Calendar1.SelectedDate.Day.ToString();
  62.         if (day.Length == 1)
  63.         {
  64.             day = "0" + day;
  65.         }
  66.         TxtBeginTime.Text = year + "-" + month + "-" + day + " 00:00:00";
  67.     }
  68.     protected void Calendar2_SelectionChanged(object sender, EventArgs e)
  69.     {
  70.         Calendar2.Visible = false;
  71.         string year = Calendar2.SelectedDate.Year.ToString();
  72.         string month = Calendar2.SelectedDate.Month.ToString();
  73.         if (month.Length == 1)
  74.         {
  75.             month = "0" + month;
  76.         }
  77.         string day = Calendar2.SelectedDate.Day.ToString();
  78.         if (day.Length == 1)
  79.         {
  80.             day = "0" + day;
  81.         }
  82.         TxtEndTime.Text = year + "-" + month + "-" + day + " 00:00:00";
  83.     }
  84.     
  85.    
  86.     protected void ImageButton2_Click(object sender, ImageClickEventArgs e)
  87.     {
  88.         if (Calendar2.Visible == true)
  89.         {
  90.             Calendar2.Visible = false;
  91.         }
  92.         else
  93.         {
  94.             Calendar2.Visible = true;
  95.         }
  96.     }
  97.     protected void BtnOk_Click(object sender, EventArgs e)
  98.     {
  99.         this.Save(1);
  100.     }
  101.     protected void BtnSave_Click(object sender, EventArgs e)
  102.     {
  103.         this.Save(2);
  104.     }
  105.    
  106.     protected void Save(int state)
  107.     {
  108.         if (this.CheckValidate()&&this.checktime())
  109.         {
  110.             
  111.             SqlConnection conn = dbConnection.getConnection();
  112.             conn.Open();
  113.             SqlTransaction tx = conn.BeginTransaction();
  114.             string sql = "";
  115.             try
  116.             {
  117.                 int id = 1;
  118.                 SqlCommand cmd = new SqlCommand("select max(id) from OA_CH_OFF_FORM", conn);
  119.                 cmd.Transaction = tx;
  120.                 try
  121.                 {
  122.                     id = Convert.ToInt32(cmd.ExecuteScalar().ToString()) + 1;
  123.                 }
  124.                 catch { }
  125.                 string total_result = "跑流程中";
  126.                 if (state == 2)
  127.                 {
  128.                     total_result = "未送件";
  129.                 }
  130.                 //添加表單
  131.                 cmd = new SqlCommand("insert into OA_CH_OFF_FORM(id, form_id, emp_id, department_id, delegate_id, " +
  132.                     "off_type, begin_time, end_time, hours, apply_date, parenthesis, reason, step, total_result, " +
  133.                     "finish_date, state)values(" + id + ",'" + TxtFormId.Text + "','" + EmpId.Value + "','" + DepartmentId.Value +
  134.                     "','" + DelegateId.Value + "','" + DdlOffType.SelectedValue + "','" + TxtBeginTime.Text + "','" +
  135.                     TxtEndTime.Text + "','" + TxtHours.Text + "','" + TxtApplyDate.Text + "','" + TxtParenthesis.Text +
  136.                     "','" + TxtReason.Text.Replace("n", "<br>") + "',0,'" + total_result + "',''," + state + ")", conn);
  137.                 cmd.Transaction = tx;
  138.                 cmd.ExecuteNonQuery();
  139.                 //添加流程
  140.                 int flow_id = 1;
  141.                 cmd = new SqlCommand("select max(id) from OA_CH_APPROVEL_FORM_FLOW", conn);
  142.                 cmd.Transaction = tx;
  143.                 try
  144.                 {
  145.                     flow_id = Convert.ToInt32(cmd.ExecuteScalar().ToString()) + 1;
  146.                 }
  147.                 catch { }
  148.                 //跑代理人
  149.                 cmd = new SqlCommand("select email_work from oa_employee where id=" + DelegateId.Value, conn);
  150.                 cmd.Transaction = tx;
  151.                 string email_work = cmd.ExecuteScalar().ToString();
  152.                 sql = "insert into OA_CH_APPROVEL_FORM_FLOW(id, form_id, operator_id, " +
  153.                     "apply_type, is_check, is_agree, comment, check_date, step, is_business) values (" +
  154.                     flow_id + ", " + id + "," + DelegateId.Value + ",2,'N','N','','',1,'N')";
  155.                 cmd = new SqlCommand(sql, conn);
  156.                 cmd.Transaction = tx;
  157.                 cmd.ExecuteNonQuery();
  158.                 if (state == 1)
  159.                 {
  160.                     mail.mail_d(email_work, TxtDelegate.Text.Substring(7), TxtEmpName.Text);
  161.                 }
  162.                 
  163.                     flow_id++;
  164.                     ArrayList operator_id = new ArrayList();//记录上层节点职级小于登陆者的ID
  165.                     ArrayList name1 = new ArrayList();//记录上层节点职级小于登陆者的name
  166.                     ArrayList mail1 = new ArrayList();//记录上层节点职级小于登陆者的mail
  167.                     ArrayList last_id = new ArrayList();//记录上层节点的ID
  168.                   
  169.                     ArrayList tmp_people = new ArrayList();//记录待签核人的ID
  170.                     ArrayList d_people = new ArrayList();
  171.                     ArrayList b_d_people = new ArrayList();
  172.                     ArrayList mail2 = new ArrayList();//记录代理人的MAIL
  173.                     ArrayList mail2_name = new ArrayList();//记录代理人姓名
  174.                    
  175.                     int ceng = 0;
  176.                     cmd = new SqlCommand("select ceng from oa_department where id=" + DepartmentId.Value, conn);
  177.                     cmd.Transaction = tx;
  178.                     try
  179.                     {
  180.                         ceng = Convert.ToInt32(cmd.ExecuteScalar());
  181.                     }
  182.                     catch { }
  183.                     //找到上层节点部门的ID
  184.                      last_id.Add(DepartmentId.Value.ToString());
  185.                     for (int j = 1; j < ceng; j++)
  186.                     {
  187.                         cmd = new SqlCommand("select distinct last_id from oa_department where id=" + last_id[j - 1], conn);
  188.                         cmd.Transaction = tx;
  189.                         SqlDataReader dr2 = cmd.ExecuteReader();
  190.                         while (dr2.Read())
  191.                         {
  192.                             last_id.Add(dr2["last_id"].ToString());
  193.                         }
  194.                         dr2.Close();
  195.                     }
  196.                     //找到上层节点人的ID
  197.                     for (int i = 1; i < last_id.Count; i++)
  198.                     {
  199.                         cmd = new SqlCommand("select a.id as id,a.email_work,a.emp_name from oa_employee as a left join oa_department as b on a.department_id=b.id" +
  200.                        " where  position<" + PositionId.Value + "and b.id=" + last_id[i] + " and q_manger=1 order by position desc", conn);
  201.                         cmd.Transaction = tx;
  202.                         SqlDataReader dr1 = cmd.ExecuteReader();
  203.                         while (dr1.Read())
  204.                         {
  205.                             operator_id.Add(dr1["id"].ToString());
  206.                             name1.Add(dr1["emp_name"].ToString());
  207.                             mail1.Add(dr1["email_work"].ToString());
  208.                         }
  209.                         dr1.Close();
  210.                     }
  211.                     for (int p = 0; p < operator_id.Count; p++)
  212.                     {
  213.                         if (Convert.ToInt32(operator_id[p]) == 128)
  214.                         { operator_id.RemoveAt(p); }
  215.                     }
  216.                     //  确定是否有代理人签核
  217.                     for (int k = 0; k < operator_id.Count; k++)
  218.                     {
  219.                         cmd = new SqlCommand("select d_people,b_d_people from oa_delegate where  s_date<=convert(varchar, getdate(), 120)" +
  220.                               " and e_date>=convert(varchar, getdate(),120) and b_d_people='" + operator_id[k] + "'", conn);
  221.                         cmd.Transaction = tx;
  222.                         SqlDataReader dr3 = cmd.ExecuteReader();
  223.                         while (dr3.Read())
  224.                         {
  225.                             d_people.Add(dr3["d_people"].ToString());
  226.                             b_d_people.Add(dr3["b_d_people"].ToString());
  227.                             operator_id.RemoveAt(k);
  228.                             operator_id.Add(dr3["d_people"].ToString());
  229.                         }
  230.                         dr3.Close();
  231.                     }        
  232.                
  233.                         for (int i = 0; i < operator_id.Count; i++)
  234.                         {
  235.                             cmd = new SqlCommand("insert into OA_CH_APPROVEL_FORM_FLOW(id, form_id, operator_id, " +
  236.                                 "apply_type, is_check, is_agree, comment, check_date, step, is_business)values(" +
  237.                                 flow_id + ", " + id + "," + operator_id[i] + ",2,'N','N','',''," + (i + 2) + ",'N')", conn);
  238.                             cmd.Transaction = tx;
  239.                             cmd.ExecuteNonQuery();
  240.                             flow_id++;
  241.                         }
  242.                         
  243.                         for (int d = 0; d < operator_id.Count; d++)
  244.                         {
  245.                             cmd = new SqlCommand("insert into OA_DELEGATE(d_people,b_d_people,s_date,e_date)values('"
  246.                             + DelegateId.Value + "','" + Session["user_id"] + "','" + TxtApplyDate.Text + "','" + TxtEndTime.Text + "')", conn);
  247.                             cmd.Transaction = tx;
  248.                             cmd.ExecuteNonQuery();
  249.                             flow_id++;
  250.                         }
  251.                         tx.Commit();                      
  252.                         Response.Redirect("my_list.aspx");
  253.                     }                  
  254.                                        
  255.                                          
  256.                         
  257.             catch (Exception ex)
  258.             {
  259.                 Response.Write(ex.Message);
  260.               //  tx.Rollback();
  261.             }
  262.             conn.Close();
  263.         }
  264.     }
  265.     //遞歸取出辦理人-- position:6-經理; 2-副董
  266.     protected void getEmployee(int department_id, int position)
  267.     {
  268.         int operator_id = 0;
  269.         int flag = 0;
  270.         SqlConnection conn = dbConnection.getConnection();
  271.         conn.Open();
  272.         string sql = "select id from OA_EMPLOYEE where department_id=" + department_id + " and position=" + position;
  273.         if (position == 2)
  274.         {
  275.             sql = "select id from OA_EMPLOYEE where position=" + position;
  276.         }
  277.         SqlCommand cmd = new SqlCommand(sql, conn);
  278.         SqlDataReader dr = cmd.ExecuteReader();
  279.         if (dr.Read())
  280.         {
  281.             operator_id = Convert.ToInt32(dr["id"].ToString());
  282.             OperatorId.Value = operator_id.ToString();
  283.         }
  284.         else
  285.         {
  286.             flag = 1;
  287.         }
  288.         dr.Close();
  289.         count = count + 1;
  290.         if (count == 6)
  291.         {
  292.             flag = 0;
  293.             OperatorId.Value = "0";
  294.         }
  295.         if (flag == 1)
  296.         {
  297.             cmd = new SqlCommand("select last_id from OA_DEPARTMENT where id=" + department_id, conn);
  298.             this.getEmployee(Convert.ToInt32(cmd.ExecuteScalar()), position);
  299.         }
  300.         conn.Close();
  301.     }
  302.     protected bool CheckValidate()
  303.     {
  304.         LblError.Text = "";
  305.         int flag = 0;
  306.         try
  307.         {
  308.             int hours = Convert.ToInt32(TxtHours.Text);
  309.         }
  310.         catch
  311.         {
  312.             flag = 1;
  313.             LblError.Text = "休假時數格式有误";
  314.             Response.Write("<script>alert('休假時數格式有误')</script>");
  315.             TxtHours.Focus();
  316.         }
  317.         if (DelegateId.Value.Equals("") || DelegateId.Value.Equals("0"))
  318.         {
  319.             flag = 1;
  320.             LblError.Text = "請選擇代理人";
  321.             Response.Write("<script>alert('請選擇代理人')</script>");
  322.             TxtDelegate.Focus();
  323.         }
  324.         if (flag == 0)
  325.         {
  326.             return true;
  327.         }
  328.         else
  329.         {
  330.             return false;
  331.         }
  332.     }
  333.     protected bool checktime()
  334.     {
  335.         LblError.Text = "";
  336.         int flag = 0;
  337.         if (TxtBeginTime.Text == "" || TxtEndTime.Text == "")
  338.         { flag = 1; }
  339.         if (flag == 1)
  340.         {
  341.             LblError.Text = "开始时间和结束时间不能为空";
  342.             Response.Write("<script>alert('开始时间和结束时间不能为空')</script>");
  343.             TxtBeginTime.Focus();
  344.             return false;
  345.         }
  346.         else {
  347.             return true;
  348.         }
  349.     }
  350.     //遞歸取自由流程
  351.     protected void GetFlow(int main_id, int number, SqlConnection conn, SqlTransaction tx)
  352.     {
  353.         string sql = "select top 1 apply_type, operator_id, number, next_number, " +
  354.             "larger_than, larger_next from OA_FLOW_DETAIL where main_id=" + main_id + " order by number";
  355.         if (number != 0)
  356.         {
  357.             sql = "select top 1 apply_type, operator_id, number, next_number, " +
  358.              "larger_than, larger_next from OA_FLOW_DETAIL where main_id=" + main_id + " and number=" + number;
  359.         }
  360.         SqlCommand cmd = new SqlCommand(sql, conn);
  361.         cmd.Transaction = tx;
  362.         int next = 0;
  363.         int flag = 0;
  364.         SqlDataReader dr = cmd.ExecuteReader();
  365.         if (dr.Read())
  366.         {
  367.             apply_type.Add(dr["apply_type"].ToString());
  368.             operator_id.Add(dr["operator_id"].ToString());
  369.             if (!dr["larger_than"].ToString().Equals("0"))
  370.             {
  371.                 //if (Convert.ToInt32(dr["larger_than"].ToString()) < Convert.ToInt32(TxtDays.Text))
  372.                 //{
  373.                 //    if (!dr["larger_next"].Equals(""))
  374.                 //    {
  375.                 //        flag = 1;
  376.                 //        next = Convert.ToInt32(dr["larger_next"].ToString());
  377.                 //    }
  378.                 //}
  379.                 //else
  380.                 //{
  381.                 //    if (!dr["next_number"].Equals(""))
  382.                 //    {
  383.                 //        flag = 1;
  384.                 //        next = Convert.ToInt32(dr["next_number"].ToString());
  385.                 //    }
  386.                 //}
  387.             }
  388.             else
  389.             {
  390.                 if (!dr["next_number"].ToString().Equals(""))
  391.                 {
  392.                     flag = 1;
  393.                     next = Convert.ToInt32(dr["next_number"].ToString());
  394.                 }
  395.             }
  396.         }
  397.         dr.Close();
  398.         if (flag == 1)
  399.         {
  400.             this.GetFlow(main_id, next, conn, tx);
  401.         }
  402.     }
  403. }