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

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