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.                 mail.mail_d(email_work, TxtDelegate.Text.Substring(7), TxtEmpName.Text);
  159.                 if (state == 1)
  160.                 {
  161.                     flow_id++;
  162.                     ArrayList operator_id = new ArrayList();//记录上层节点职级小于登陆者的ID
  163.                     ArrayList name1 = new ArrayList();//记录上层节点职级小于登陆者的name
  164.                     ArrayList mail1 = new ArrayList();//记录上层节点职级小于登陆者的mail
  165.                     ArrayList last_id = new ArrayList();//记录上层节点的ID
  166.                     ArrayList ceng = new ArrayList();//记录层的ID
  167.                     ArrayList tmp_people = new ArrayList();//记录待签核人的ID
  168.                     ArrayList d_people = new ArrayList();
  169.                     ArrayList b_d_people = new ArrayList();
  170.                     ArrayList mail2 = new ArrayList();//记录代理人的MAIL
  171.                     ArrayList mail2_name = new ArrayList();//记录代理人姓名
  172.                     int times=3;
  173.                     if (Convert.ToInt32(PositionSequence.Value) <= 8) { times = 3; }
  174.                     else if (Convert.ToInt32(PositionSequence.Value) == 9) { times = 2; }
  175.                     else if (Convert.ToInt32(PositionSequence.Value) == 10) { times = 2; }
  176.                     else if (Convert.ToInt32(PositionSequence.Value) == 11) { times = 3; }
  177.                     else { times = 3; }
  178.                     //找到上层节点部门的ID
  179.                     last_id.Add(DepartmentId.Value.ToString());
  180.                     for (int j = 1; j < times; j++)
  181.                     {
  182.                         cmd = new SqlCommand("select distinct last_id from oa_department where id=" + last_id[j - 1], conn);
  183.                         cmd.Transaction = tx;
  184.                         SqlDataReader dr2 = cmd.ExecuteReader();
  185.                         while (dr2.Read())
  186.                         {
  187.                             last_id.Add(dr2["last_id"].ToString());
  188.                         }
  189.                         dr2.Close();
  190.                     }
  191.                     //找到上层节点人的ID
  192.                     for (int i = 1; i < last_id.Count; i++)
  193.                     {
  194.                         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" +
  195.                        " where  position<" + PositionId.Value + "and b.id=" + last_id[i] + " and q_manger=1 order by position desc", conn);
  196.                         cmd.Transaction = tx;
  197.                         SqlDataReader dr1 = cmd.ExecuteReader();
  198.                         while (dr1.Read())
  199.                         {
  200.                             operator_id.Add(dr1["id"].ToString());
  201.                             name1.Add(dr1["emp_name"].ToString());
  202.                             mail1.Add(dr1["email_work"].ToString());
  203.                         }
  204.                         dr1.Close();
  205.                     }
  206.                     //  确定是否有代理人签核
  207.                     for (int k = 0; k < operator_id.Count; k++)
  208.                     {
  209.                         cmd = new SqlCommand("select d_people,b_d_people from oa_delegate where  s_date<=convert(varchar, getdate(), 120)" +
  210.                               " and e_date>=convert(varchar, getdate(),120) and b_d_people='" + operator_id[k] + "'", conn);
  211.                         cmd.Transaction = tx;
  212.                         SqlDataReader dr3 = cmd.ExecuteReader();
  213.                         while (dr3.Read())
  214.                         {
  215.                             d_people.Add(dr3["d_people"].ToString());
  216.                             b_d_people.Add(dr3["b_d_people"].ToString());
  217.                             operator_id.RemoveAt(k);
  218.                             operator_id.Add(dr3["d_people"].ToString());
  219.                         }
  220.                         dr3.Close();
  221.                     }
  222.              
  223.                
  224.                         for (int i = 0; i < operator_id.Count; i++)
  225.                         {
  226.                             cmd = new SqlCommand("insert into OA_CH_APPROVEL_FORM_FLOW(id, form_id, operator_id, " +
  227.                                 "apply_type, is_check, is_agree, comment, check_date, step, is_business)values(" +
  228.                                 flow_id + ", " + id + "," + operator_id[i] + ",2,'N','N','',''," + (i + 2) + ",'N')", conn);
  229.                             cmd.Transaction = tx;
  230.                             cmd.ExecuteNonQuery();
  231.                             flow_id++;
  232.                         }
  233.                         
  234.                         for (int d = 0; d < operator_id.Count; d++)
  235.                         {
  236.                             cmd = new SqlCommand("insert into OA_DELEGATE(d_people,b_d_people,s_date,e_date)values('"
  237.                             + DelegateId.Value + "','" + Session["user_id"] + "','" + TxtApplyDate.Text + "','" + TxtEndTime.Text + "')", conn);
  238.                             cmd.Transaction = tx;
  239.                             cmd.ExecuteNonQuery();
  240.                             flow_id++;
  241.                         }
  242.                         tx.Commit();                      
  243.                         Response.Redirect("my_list.aspx");
  244.                     }                  
  245.                                        
  246.                 }                          
  247.                         
  248.             catch (Exception ex)
  249.             {
  250.                 Response.Write(ex.Message);
  251.               //  tx.Rollback();
  252.             }
  253.             conn.Close();
  254.         }
  255.     }
  256.     //遞歸取出辦理人-- position:6-經理; 2-副董
  257.     protected void getEmployee(int department_id, int position)
  258.     {
  259.         int operator_id = 0;
  260.         int flag = 0;
  261.         SqlConnection conn = dbConnection.getConnection();
  262.         conn.Open();
  263.         string sql = "select id from OA_EMPLOYEE where department_id=" + department_id + " and position=" + position;
  264.         if (position == 2)
  265.         {
  266.             sql = "select id from OA_EMPLOYEE where position=" + position;
  267.         }
  268.         SqlCommand cmd = new SqlCommand(sql, conn);
  269.         SqlDataReader dr = cmd.ExecuteReader();
  270.         if (dr.Read())
  271.         {
  272.             operator_id = Convert.ToInt32(dr["id"].ToString());
  273.             OperatorId.Value = operator_id.ToString();
  274.         }
  275.         else
  276.         {
  277.             flag = 1;
  278.         }
  279.         dr.Close();
  280.         count = count + 1;
  281.         if (count == 6)
  282.         {
  283.             flag = 0;
  284.             OperatorId.Value = "0";
  285.         }
  286.         if (flag == 1)
  287.         {
  288.             cmd = new SqlCommand("select last_id from OA_DEPARTMENT where id=" + department_id, conn);
  289.             this.getEmployee(Convert.ToInt32(cmd.ExecuteScalar()), position);
  290.         }
  291.         conn.Close();
  292.     }
  293.     protected bool CheckValidate()
  294.     {
  295.         LblError.Text = "";
  296.         int flag = 0;
  297.         try
  298.         {
  299.             int hours = Convert.ToInt32(TxtHours.Text);
  300.         }
  301.         catch
  302.         {
  303.             flag = 1;
  304.             LblError.Text = "休假時數格式有误";
  305.             Response.Write("<script>alert('休假時數格式有误')</script>");
  306.             TxtHours.Focus();
  307.         }
  308.         if (DelegateId.Value.Equals("") || DelegateId.Value.Equals("0"))
  309.         {
  310.             flag = 1;
  311.             LblError.Text = "請選擇代理人";
  312.             Response.Write("<script>alert('請選擇代理人')</script>");
  313.             TxtDelegate.Focus();
  314.         }
  315.         if (flag == 0)
  316.         {
  317.             return true;
  318.         }
  319.         else
  320.         {
  321.             return false;
  322.         }
  323.     }
  324.     protected bool checktime()
  325.     {
  326.         LblError.Text = "";
  327.         int flag = 0;
  328.         if (TxtBeginTime.Text == "" || TxtEndTime.Text == "")
  329.         { flag = 1; }
  330.         if (flag == 1)
  331.         {
  332.             LblError.Text = "开始时间和结束时间不能为空";
  333.             Response.Write("<script>alert('开始时间和结束时间不能为空')</script>");
  334.             TxtBeginTime.Focus();
  335.             return false;
  336.         }
  337.         else {
  338.             return true;
  339.         }
  340.     }
  341.     //遞歸取自由流程
  342.     protected void GetFlow(int main_id, int number, SqlConnection conn, SqlTransaction tx)
  343.     {
  344.         string sql = "select top 1 apply_type, operator_id, number, next_number, " +
  345.             "larger_than, larger_next from OA_FLOW_DETAIL where main_id=" + main_id + " order by number";
  346.         if (number != 0)
  347.         {
  348.             sql = "select top 1 apply_type, operator_id, number, next_number, " +
  349.              "larger_than, larger_next from OA_FLOW_DETAIL where main_id=" + main_id + " and number=" + number;
  350.         }
  351.         SqlCommand cmd = new SqlCommand(sql, conn);
  352.         cmd.Transaction = tx;
  353.         int next = 0;
  354.         int flag = 0;
  355.         SqlDataReader dr = cmd.ExecuteReader();
  356.         if (dr.Read())
  357.         {
  358.             apply_type.Add(dr["apply_type"].ToString());
  359.             operator_id.Add(dr["operator_id"].ToString());
  360.             if (!dr["larger_than"].ToString().Equals("0"))
  361.             {
  362.                 //if (Convert.ToInt32(dr["larger_than"].ToString()) < Convert.ToInt32(TxtDays.Text))
  363.                 //{
  364.                 //    if (!dr["larger_next"].Equals(""))
  365.                 //    {
  366.                 //        flag = 1;
  367.                 //        next = Convert.ToInt32(dr["larger_next"].ToString());
  368.                 //    }
  369.                 //}
  370.                 //else
  371.                 //{
  372.                 //    if (!dr["next_number"].Equals(""))
  373.                 //    {
  374.                 //        flag = 1;
  375.                 //        next = Convert.ToInt32(dr["next_number"].ToString());
  376.                 //    }
  377.                 //}
  378.             }
  379.             else
  380.             {
  381.                 if (!dr["next_number"].ToString().Equals(""))
  382.                 {
  383.                     flag = 1;
  384.                     next = Convert.ToInt32(dr["next_number"].ToString());
  385.                 }
  386.             }
  387.         }
  388.         dr.Close();
  389.         if (flag == 1)
  390.         {
  391.             this.GetFlow(main_id, next, conn, tx);
  392.         }
  393.     }
  394. }