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

OA系统

开发平台:

ASP/ASPX

  1. 
  2. using System;
  3. using System.Data;
  4. using System.Configuration;
  5. using System.Collections;
  6. using System.Web;
  7. using System.Web.Security;
  8. using System.Web.UI;
  9. using System.Web.UI.WebControls;
  10. using System.Web.UI.WebControls.WebParts;
  11. using System.Web.UI.HtmlControls;
  12. using System.Data.SqlClient;
  13. using System.Net.Mail;
  14. using System.Net;
  15. using System.Net.Sockets;
  16. public partial class web_approvel_b_form : System.Web.UI.Page
  17. {
  18.     ArrayList apply_type = new ArrayList();
  19.     ArrayList operator_id = new ArrayList();
  20.     private int count = 0;
  21.     protected void Page_Load(object sender, EventArgs e)
  22.     {
  23.         if (!IsPostBack)
  24.         {
  25.             this.SetValue();
  26.             LoginDAO.CheckLogin(Session, Response, "../../", 2);
  27.         }
  28.     }
  29.     protected void SetValue()
  30.     {
  31.         if (Session["user"] != null)
  32.         { 
  33.             DateMgr mgr = new DateMgr();
  34.             User user = (User)Session["user"];
  35.             TxtEmpName.Text = user.Emp_name;
  36.             TxtEmpNo.Text = user.Emp_no;
  37.             TxtDepartment.Text = user.Department.Full_name;
  38.             TxtPosition.Text = user.Position;
  39.             PositionSequence.Value = user.PositionSequence.ToString();
  40.             TxtFormId.Text = mgr.getTime();
  41.             TxtApplyDate.Text = mgr.getDateTime();
  42.             EmployeeId.Value = user.Id.ToString();
  43.             DepartmentId.Value = user.Department.Id.ToString();
  44.             PositionId.Value = user.PositionId.ToString();
  45.         }
  46.     }
  47.    
  48.     protected void ImageButton1_Click(object sender, EventArgs e)
  49.     {
  50.         if (Calendar1.Visible == true)
  51.         {
  52.             Calendar1.Visible = false;
  53.         }
  54.         else
  55.         {
  56.             Calendar1.Visible = true;
  57.         }
  58.     }
  59.     protected void ImageButton2_Click(object sender, EventArgs e)
  60.     {
  61.         if (Calendar2.Visible == true)
  62.         {
  63.             Calendar2.Visible = false;
  64.         }
  65.         else
  66.         {
  67.             Calendar2.Visible = true;
  68.         }
  69.     }
  70.     protected void ImageButton3_Click(object sender, EventArgs e)
  71.     {
  72.         if (Calendar3.Visible == true)
  73.         {
  74.             Calendar3.Visible = false;
  75.         }
  76.         else
  77.         {
  78.             Calendar3.Visible = true;
  79.         }
  80.     }
  81.     protected void ImageButton4_Click(object sender, EventArgs e)
  82.     {
  83.         if (Calendar4.Visible == true)
  84.         {
  85.             Calendar4.Visible = false;
  86.         }
  87.         else
  88.         {
  89.             Calendar4.Visible = true;
  90.         }
  91.     }
  92.     protected void Calendar1_SelectionChanged(object sender, EventArgs e)
  93.     {
  94.         Calendar1.Visible = false;
  95.         string year = Calendar1.SelectedDate.Year.ToString();
  96.         string month = Calendar1.SelectedDate.Month.ToString();
  97.         if (month.Length == 1)
  98.         {
  99.             month = "0" + month;
  100.         }
  101.         string day = Calendar1.SelectedDate.Day.ToString();
  102.         if (day.Length == 1)
  103.         {
  104.             day = "0" + day;
  105.         }
  106.         TxtBeginTime.Text = year + "-" + month + "-" + day + " 00:00:00";
  107.     }
  108.     protected void Calendar2_SelectionChanged(object sender, EventArgs e)
  109.     {
  110.         Calendar2.Visible = false;
  111.         string year = Calendar2.SelectedDate.Year.ToString();
  112.         string month = Calendar2.SelectedDate.Month.ToString();
  113.         if (month.Length == 1)
  114.         {
  115.             month = "0" + month;
  116.         }
  117.         string day = Calendar2.SelectedDate.Day.ToString();
  118.         if (day.Length == 1)
  119.         {
  120.             day = "0" + day;
  121.         }
  122.         TxtEndTime.Text = year + "-" + month + "-" + day + " 00:00:00";
  123.     }
  124.     protected void Calendar3_SelectionChanged(object sender, EventArgs e)
  125.     {
  126.         Calendar3.Visible = false;
  127.         string year = Calendar3.SelectedDate.Year.ToString();
  128.         string month = Calendar3.SelectedDate.Month.ToString();
  129.         if (month.Length == 1)
  130.         {
  131.             month = "0" + month;
  132.         }
  133.         string day = Calendar3.SelectedDate.Day.ToString();
  134.         if (day.Length == 1)
  135.         {
  136.             day = "0" + day;
  137.         }
  138.         TxtDepartTime.Text = year + "-" + month + "-" + day + " 00:00:00";
  139.     }
  140.     protected void Calendar4_SelectionChanged(object sender, EventArgs e)
  141.     {
  142.         Calendar4.Visible = false;
  143.         string year = Calendar4.SelectedDate.Year.ToString();
  144.         string month = Calendar4.SelectedDate.Month.ToString();
  145.         if (month.Length == 1)
  146.         {
  147.             month = "0" + month;
  148.         }
  149.         string day = Calendar4.SelectedDate.Day.ToString();
  150.         if (day.Length == 1)
  151.         {
  152.             day = "0" + day;
  153.         }
  154.         TxtArriveTime.Text = year + "-" + month + "-" + day + " 00:00:00";
  155.     }
  156.     protected void BtnOk_Click(object sender, EventArgs e)
  157.     {
  158.         if (this.checktime())
  159.         {
  160.             this.Save(1);
  161.         }
  162.     }
  163.     protected bool checktime()
  164.     {
  165.         LblError.Text = "";
  166.         int flag = 0;
  167.         if (TxtBeginTime.Text == "" || TxtEndTime.Text == "") 
  168.         {
  169.             flag = 1;
  170.            
  171.         }
  172.         if (TxtDays.Text == "") { flag = 2; }
  173.         if (flag == 1) 
  174.         {
  175.             LblError.Text = "出差日期起止不能为空";
  176.             Response.Write("<script>alert('出差日起止不能为空')</script>");
  177.             TxtBeginTime.Focus();
  178.             return false; 
  179.         }
  180.         else if (flag == 2) {
  181.             LblError.Text = "出差天数不能为空";
  182.             Response.Write("<script>alert('出差天数不能为空')</script>");
  183.             TxtDays.Focus();
  184.             return false;
  185.         }
  186.         else
  187.         { return true; }
  188.     
  189.     }
  190.     protected void Save(int state)
  191.     {
  192.         SqlConnection conn = dbConnection.getConnection();
  193.         conn.Open();
  194.         SqlTransaction tx = conn.BeginTransaction();
  195.         try
  196.         {
  197.             int id = 1;
  198.             SqlCommand cmd = new SqlCommand("select max(id) from OA_CH_BUSINESS_FORM", conn);
  199.             cmd.Transaction = tx;
  200.             try
  201.             {
  202.                 id = Convert.ToInt32(cmd.ExecuteScalar().ToString()) + 1;
  203.             }
  204.             catch { }
  205.             
  206.             string total_result = "跑流程中";
  207.             
  208.             if (state == 2)
  209.             {
  210.                 total_result = "未送件";
  211.               
  212.             }
  213.             //添加表單
  214.             cmd = new SqlCommand("insert into OA_CH_BUSINESS_FORM(id, form_id, emp_id, apply_date, " +
  215.                 "department_id, days, begin_time, end_time, depart_place, arrive_place, depart_time, " +
  216.                 "arrive_time, step, total_result, finish_date, state) values (" + id + ",'" + TxtFormId.Text + "','" +
  217.                 EmployeeId.Value + "','" + TxtApplyDate.Text + "','" + DepartmentId.Value + "','" + TxtDays.Text +
  218.                 "','" + TxtBeginTime.Text + "','" + TxtEndTime.Text + "','" + DdlDepartPlace.SelectedValue + "','" +
  219.                 DdlArrivePlace.SelectedValue + "','" + TxtDepartTime.Text + "','" + TxtArriveTime.Text + "',0,'" +
  220.                 total_result + "',''," + state + ")", conn);
  221.             cmd.Transaction = tx;
  222.             cmd.ExecuteNonQuery();
  223.             //添加表身
  224.             int detail_id = 1;
  225.             cmd = new SqlCommand("select max(id) from OA_CH_BUSINESS_DETAIL", conn);
  226.             cmd.Transaction = tx;
  227.             try
  228.             {
  229.                 detail_id = Convert.ToInt32(cmd.ExecuteScalar().ToString()) + 1;
  230.             }
  231.             catch { }
  232.             #region
  233.             int index = 1;
  234.             if (!TxtCompany1.Text.Trim().Equals("") || !TxtSubject1.Text.Trim().Equals(""))
  235.             {
  236.                 string is_aboard = "N";
  237.                 if (ChkAboard1.Checked)
  238.                 {
  239.                     is_aboard = "Y";
  240.                 }
  241.                 cmd = new SqlCommand("insert into OA_CH_BUSINESS_DETAIL(id, form_id, number, company, " +
  242.                     "subject, linkman, tel, place, visa_begin, visa_end, flight_go_begin, " +
  243.                     "flight_back_end, is_aboard, remark) values (" + detail_id + "," + id + ",'" + index + "','" +
  244.                     TxtCompany1.Text + "','" + TxtSubject1.Text + "','" + TxtLinkman1.Text + "','" + TxtTel1.Text +
  245.                     "','" + TxtPlace1.Text + "','" + TxtVisaBegin1.Text + "','" + TxtVisaEnd1.Text + "','" +
  246.                     TxtFlightGoBegin1.Text + "','" + TxtFlightBackEnd1.Text + "','" + is_aboard + "','" + TxtRemark1.Text + "')", conn);
  247.                 cmd.Transaction = tx;
  248.                 cmd.ExecuteNonQuery();
  249.                 detail_id++;
  250.                 index++;
  251.             }
  252.             if (!TxtCompany2.Text.Trim().Equals("") || !TxtSubject2.Text.Trim().Equals(""))
  253.             {
  254.                 string is_aboard = "N";
  255.                 if (ChkAboard2.Checked)
  256.                 {
  257.                     is_aboard = "Y";
  258.                 }
  259.                 cmd = new SqlCommand("insert into OA_CH_BUSINESS_DETAIL(id, form_id, number, company, " +
  260.                     "subject, linkman, tel, place, visa_begin, visa_end, flight_go_begin, " +
  261.                     "flight_back_end, is_aboard, remark) values (" + detail_id + "," + id + ",'" + index + "','" +
  262.                     TxtCompany2.Text + "','" + TxtSubject2.Text + "','" + TxtLinkman2.Text + "','" + TxtTel2.Text +
  263.                     "','" + TxtPlace2.Text + "','" + TxtVisaBegin2.Text + "','" + TxtVisaEnd2.Text + "','" +
  264.                     TxtFlightGoBegin2.Text + "','" + TxtFlightBackEnd2.Text + "','" + is_aboard + "','" + TxtRemark2.Text + "')", conn);
  265.                 cmd.Transaction = tx;
  266.                 cmd.ExecuteNonQuery();
  267.                 detail_id++;
  268.                 index++;
  269.             }
  270.             if (!TxtCompany3.Text.Trim().Equals("") || !TxtSubject3.Text.Trim().Equals(""))
  271.             {
  272.                 string is_aboard = "N";
  273.                 if (ChkAboard3.Checked)
  274.                 {
  275.                     is_aboard = "Y";
  276.                 }
  277.                 cmd = new SqlCommand("insert into OA_CH_BUSINESS_DETAIL(id, form_id, number, company, " +
  278.                     "subject, linkman, tel, place, visa_begin, visa_end, flight_go_begin, " +
  279.                     "flight_back_end, is_aboard, remark) values (" + detail_id + "," + id + ",'" + index + "','" +
  280.                     TxtCompany3.Text + "','" + TxtSubject3.Text + "','" + TxtLinkman3.Text + "','" + TxtTel3.Text +
  281.                     "','" + TxtPlace3.Text + "','" + TxtVisaBegin3.Text + "','" + TxtVisaEnd3.Text + "','" +
  282.                     TxtFlightGoBegin3.Text + "','" + TxtFlightBackEnd3.Text + "','" + is_aboard + "','" + TxtRemark3.Text + "')", conn);
  283.                 cmd.Transaction = tx;
  284.                 cmd.ExecuteNonQuery();
  285.                 detail_id++;
  286.                 index++;
  287.             }
  288.             if (!TxtCompany4.Text.Trim().Equals("") || !TxtSubject4.Text.Trim().Equals(""))
  289.             {
  290.                 string is_aboard = "N";
  291.                 if (ChkAboard4.Checked)
  292.                 {
  293.                     is_aboard = "Y";
  294.                 }
  295.                 cmd = new SqlCommand("insert into OA_CH_BUSINESS_DETAIL(id, form_id, number, company, " +
  296.                     "subject, linkman, tel, place, visa_begin, visa_end, flight_go_begin, " +
  297.                     "flight_back_end, is_aboard, remark) values (" + detail_id + "," + id + ",'" + index + "','" +
  298.                     TxtCompany4.Text + "','" + TxtSubject4.Text + "','" + TxtLinkman4.Text + "','" + TxtTel4.Text +
  299.                     "','" + TxtPlace4.Text + "','" + TxtVisaBegin4.Text + "','" + TxtVisaEnd4.Text + "','" +
  300.                     TxtFlightGoBegin4.Text + "','" + TxtFlightBackEnd4.Text + "','" + is_aboard + "','" + TxtRemark4.Text + "')", conn);
  301.                 cmd.Transaction = tx;
  302.                 cmd.ExecuteNonQuery();
  303.                 detail_id++;
  304.                 index++;
  305.             }
  306.             if (!TxtCompany5.Text.Trim().Equals("") || !TxtSubject5.Text.Trim().Equals(""))
  307.             {
  308.                 string is_aboard = "N";
  309.                 if (ChkAboard5.Checked)
  310.                 {
  311.                     is_aboard = "Y";
  312.                 }
  313.                 cmd = new SqlCommand("insert into OA_CH_BUSINESS_DETAIL(id, form_id, number, company, " +
  314.                     "subject, linkman, tel, place, visa_begin, visa_end, flight_go_begin, " +
  315.                     "flight_back_end, is_aboard, remark) values (" + detail_id + "," + id + ",'" + index + "','" +
  316.                     TxtCompany5.Text + "','" + TxtSubject5.Text + "','" + TxtLinkman5.Text + "','" + TxtTel5.Text +
  317.                     "','" + TxtPlace5.Text + "','" + TxtVisaBegin5.Text + "','" + TxtVisaEnd5.Text + "','" +
  318.                     TxtFlightGoBegin5.Text + "','" + TxtFlightBackEnd5.Text + "','" + is_aboard + "','" + TxtRemark5.Text + "')", conn);
  319.                 cmd.Transaction = tx;
  320.                 cmd.ExecuteNonQuery();
  321.                 detail_id++;
  322.                 index++;
  323.             }
  324.             #endregion
  325.            //逐级签核
  326.                 
  327.                 ArrayList operator_id = new ArrayList();//记录上层节点职级小于登陆者的ID
  328.                 ArrayList name1 = new ArrayList();//记录上层节点职级小于登陆者的name
  329.                 ArrayList mail1 = new ArrayList();//记录上层节点职级小于登陆者的mail
  330.                 ArrayList last_id = new ArrayList();//记录上层节点的ID                
  331.                 ArrayList tmp_people = new ArrayList();//记录待签核人的ID
  332.                 ArrayList d_people = new ArrayList();
  333.                 ArrayList b_d_people = new ArrayList();
  334.                 ArrayList mail2 = new ArrayList();//记录代理人的MAIL
  335.                 ArrayList mail2_name = new ArrayList();//记录代理人姓名
  336.                 
  337.              
  338.                 int ceng = 0;
  339.                 cmd = new SqlCommand("select ceng from oa_department where id="+DepartmentId.Value,conn);
  340.                 cmd.Transaction = tx;
  341.                 
  342.                 try
  343.                 {
  344.                      ceng = Convert.ToInt32(cmd.ExecuteScalar());
  345.                 }
  346.                 catch { }
  347.                     last_id.Add(DepartmentId.Value.ToString());
  348.                    
  349.                     for (int j = 1; j < ceng; j++)
  350.                     {
  351.                         cmd = new SqlCommand("select distinct last_id from oa_department where id=" + last_id[j - 1], conn);
  352.                         cmd.Transaction = tx;
  353.                         SqlDataReader dr2 = cmd.ExecuteReader();
  354.                         while (dr2.Read())
  355.                         {
  356.                             last_id.Add(dr2["last_id"].ToString());
  357.                         }
  358.                       
  359.                         dr2.Close();
  360.                     }
  361.                     //找到上层节点人的ID
  362.                     for (int i = 1; i < last_id.Count; i++)
  363.                     {
  364.                         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" +
  365.                        " where  position<" + PositionId.Value + "and b.id=" + last_id[i] + " and q_manger=1 order by position desc", conn);
  366.                         cmd.Transaction = tx;
  367.                         SqlDataReader dr1 = cmd.ExecuteReader();
  368.                         while (dr1.Read())
  369.                         {   
  370.                             
  371.                             operator_id.Add(dr1["id"].ToString());
  372.                             name1.Add(dr1["emp_name"].ToString());
  373.                             mail1.Add(dr1["email_work"].ToString());
  374.                         }
  375.                       
  376.                         dr1.Close();
  377.                     }
  378.                     for (int p = 0; p < operator_id.Count; p++) 
  379.                     {
  380.                         if (Convert.ToInt32(operator_id[p]) == 128)
  381.                         { operator_id.RemoveAt(p); }
  382.                     
  383.                     }
  384.                         //  确定是否有代理人签核
  385.                         for (int k = 0; k < operator_id.Count; k++)
  386.                         {
  387.                             cmd = new SqlCommand("select d_people,b_d_people from oa_delegate where  s_date<=convert(varchar, getdate(), 120)" +
  388.                                   " and e_date>=convert(varchar, getdate(),120) and b_d_people='" + operator_id[k] + "'", conn);
  389.                             cmd.Transaction = tx;
  390.                             SqlDataReader dr3 = cmd.ExecuteReader();
  391.                             while (dr3.Read())
  392.                             {
  393.                                 d_people.Add(dr3["d_people"].ToString());
  394.                                 b_d_people.Add(dr3["b_d_people"].ToString());
  395.                                 operator_id.RemoveAt(k);
  396.                                 operator_id.Add(dr3["d_people"].ToString());
  397.                             }
  398.                             dr3.Close();
  399.                         }
  400.                     //   
  401.                     //添加流程
  402.                     int flow_id = 1;
  403.               
  404.                     cmd = new SqlCommand("select max(id) from OA_CH_APPROVEL_FORM_FLOW", conn);
  405.                     cmd.Transaction = tx;
  406.                     try
  407.                     {
  408.                         flow_id = Convert.ToInt32(cmd.ExecuteScalar().ToString()) + 1;
  409.                     }
  410.                     catch { }
  411.                 
  412.                     for (int i = 0; i < operator_id.Count; i++)
  413.                     {
  414.                         cmd = new SqlCommand("insert into OA_CH_APPROVEL_FORM_FLOW(id, form_id, operator_id, " +
  415.                             "apply_type, is_check, is_agree, comment, check_date, step, is_business)values(" +
  416.                             flow_id + ", " + id + "," + operator_id[i] + ",2,'N','N','',''," + (i + 1) + ",'Y')", conn);
  417.                         cmd.Transaction = tx;
  418.                         cmd.ExecuteNonQuery();
  419.                         flow_id++;
  420.                     }
  421.                     if (state == 1)
  422.                     {
  423.                         mail.mail_q(mail1[0], name1[0], TxtEmpName.Text, "中幹出差申請單");
  424.                     }
  425.                    
  426.                     tx.Commit();
  427.                     Response.Redirect("my_list.aspx");
  428.                 }
  429.                
  430.                  
  431.      
  432.         catch (Exception ex)
  433.         {
  434.             Response.Write(ex.Message);
  435.             Response.Write("没找到相應主管簽核,請聯繫管理員");
  436.            // try
  437.           //  {
  438.            //     tx.Rollback();
  439.            // }
  440.          //   catch { }
  441.         }
  442.         conn.Close();  
  443.     }
  444.     protected void BtnSave_Click(object sender, EventArgs e)
  445.     {
  446.         this.Save(2);
  447.     }
  448.     //遞歸取出辦理人-- position:7-副理; 2-副董
  449.     protected void getEmployee(int department_id, int position)
  450.     {
  451.         int operator_id = 0;
  452.         int flag = 0;
  453.         SqlConnection conn = dbConnection.getConnection();
  454.         conn.Open();
  455.         string sql = "select id from OA_EMPLOYEE where department_id=" + department_id + " and position=" + position;
  456.         if (position == 2)
  457.         {
  458.             sql = "select id from OA_EMPLOYEE where position=" + position;
  459.         }
  460.         SqlCommand cmd = new SqlCommand(sql, conn);
  461.         SqlDataReader dr = cmd.ExecuteReader();
  462.         if (dr.Read())
  463.         {
  464.             operator_id = Convert.ToInt32(dr["id"].ToString());
  465.             OperatorId.Value = operator_id.ToString();
  466.         }
  467.         else
  468.         {
  469.             flag = 1;
  470.         }
  471.         dr.Close();
  472.         count = count + 1;
  473.         if (count == 6)
  474.         {
  475.             flag = 0;
  476.             OperatorId.Value = "0";
  477.         }
  478.         if (flag == 1)
  479.         {
  480.             cmd = new SqlCommand("select last_id from OA_DEPARTMENT where id=" + department_id, conn);
  481.             this.getEmployee(Convert.ToInt32(cmd.ExecuteScalar()), position);
  482.         }
  483.         conn.Close();
  484.     }
  485.     //遞歸取自由流程
  486.     protected void GetFlow(int main_id, int number, SqlConnection conn, SqlTransaction tx)
  487.     {
  488.         string sql = "select top 1 apply_type, operator_id, number, next_number, " +
  489.             "larger_than, larger_next from OA_FLOW_DETAIL where main_id=" + main_id + " order by number";
  490.         if (number != 0)
  491.         {
  492.             sql = "select top 1 apply_type, operator_id, number, next_number, " +
  493.              "larger_than, larger_next from OA_FLOW_DETAIL where main_id=" + main_id + " and number=" + number;
  494.         }
  495.         SqlCommand cmd = new SqlCommand(sql, conn);
  496.         cmd.Transaction = tx;
  497.         int next = 0;
  498.         int flag = 0;
  499.         SqlDataReader dr = cmd.ExecuteReader();
  500.         if (dr.Read())
  501.         {
  502.             apply_type.Add(dr["apply_type"].ToString());
  503.             operator_id.Add(dr["operator_id"].ToString());
  504.             if (!dr["larger_than"].ToString().Equals("0"))
  505.             {
  506.                 if (Convert.ToInt32(dr["larger_than"].ToString()) < Convert.ToInt32(TxtDays.Text))
  507.                 {
  508.                     if (!dr["larger_next"].Equals(""))
  509.                     {
  510.                         flag = 1;
  511.                         next = Convert.ToInt32(dr["larger_next"].ToString());
  512.                     }
  513.                 }
  514.                 else
  515.                 {
  516.                     if (!dr["next_number"].Equals(""))
  517.                     {
  518.                         flag = 1;
  519.                         next = Convert.ToInt32(dr["next_number"].ToString());
  520.                     }
  521.                 }
  522.             }
  523.             else
  524.             {
  525.                 if (!dr["next_number"].ToString().Equals(""))
  526.                 {
  527.                     flag = 1;
  528.                     next = Convert.ToInt32(dr["next_number"].ToString());
  529.                 }
  530.             }
  531.         }
  532.         dr.Close();
  533.         if (flag == 1)
  534.         {
  535.             this.GetFlow(main_id, next, conn, tx);
  536.         }
  537.     }
  538. }