fair_add.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_fair_index : System.Web.UI.Page
  16. {
  17.     private ArrayList attachments;
  18.     private ArrayList attachment_save_names;
  19.     private ArrayList content_lengths;
  20.     protected void Page_Load(object sender, EventArgs e)
  21.     {
  22.         if (!IsPostBack)
  23.         {
  24.             if (Session["user"] != null)
  25.             {
  26.                 User user = (User)Session["user"];
  27.                 EmpId.Value = user.Id.ToString();
  28.                 DepartmentId.Value = user.Department.Id.ToString();
  29.                 PositionId.Value = user.PositionSequence.ToString();
  30.                 TxtEmpName.Text = user.Emp_name;
  31.                 TxtEmpNo.Text = user.Emp_no;
  32.                 DateMgr mgr = new DateMgr();
  33.                 TxtFormNo.Text = mgr.getTime();
  34.                 TxtApplyDate.Text = mgr.getDateTime();
  35.             }
  36.         }
  37.     }
  38.    
  39.     
  40.     protected void BtnOk_Click(object sender, EventArgs e)
  41.     {
  42.         if (this.CheckValidate())
  43.         {
  44.             if (this.UploadFiles(TxtFormNo.Text))
  45.             {
  46.                 SqlConnection conn = dbConnection.getConnection();
  47.                 conn.Open();
  48.                 SqlTransaction tx = conn.BeginTransaction();
  49.                 try
  50.                 {
  51.                     int fair_id = 1;
  52.                     SqlCommand cmd = new SqlCommand("select max(id) from OA_FAIR", conn);
  53.                     cmd.Transaction = tx;
  54.                     try
  55.                     {
  56.                         fair_id = Convert.ToInt32(cmd.ExecuteScalar().ToString()) + 1;
  57.                     }
  58.                     catch { }
  59.                     int form_id = 0;
  60.                     string form_table = "";
  61.                     if (!FormValue.Value.Equals(""))
  62.                     {
  63.                         try
  64.                         {
  65.                             string[] values = FormValue.Value.Split(new char[] { '-'});
  66.                             form_id = Convert.ToInt32(values[0]);
  67.                             form_table = values[2];
  68.                         }
  69.                         catch { }
  70.                     }
  71.                     //添加表單
  72.                     cmd = new SqlCommand("insert into OA_FAIR(id, emp_id, apply_date, subject, amount, " +
  73.                         "remark, form_no, step, total_result, finish_date, form_id, form_table)values(" +
  74.                         fair_id + "," + EmpId.Value + ",'" + TxtApplyDate.Text + "','" + TxtSubject.Text + "','" +
  75.                         TxtAmount.Text + "','" + TxtRemark.Text + "','" + TxtFormNo.Text +
  76.                         "',0,'跑流程中','','" + form_id + "','" + form_table + "')", conn);
  77.                     cmd.Transaction = tx;
  78.                     cmd.ExecuteNonQuery();
  79.                     //添加圖檔
  80.                     int attachment_id = 1;
  81.                     if (attachments.Count > 0)
  82.                     {
  83.                         cmd = new SqlCommand("select max(id) from OA_FAIR_ATTACHMENT", conn);
  84.                         cmd.Transaction = tx;
  85.                         try
  86.                         {
  87.                             attachment_id = Convert.ToInt32(cmd.ExecuteScalar().ToString()) + 1;
  88.                         }
  89.                         catch { }
  90.                     }
  91.                     for (int i = 0; i < attachments.Count; i++)
  92.                     {
  93.                         cmd = new SqlCommand("insert into OA_FAIR_ATTACHMENT(id, fair_id, attachment, " +
  94.                             "attachment_save_name, content_length)values(" + attachment_id + "," + 
  95.                             fair_id + ",'" + attachments[i] + "','" + attachment_save_names[i] + "','" + 
  96.                             content_lengths[i] + "')", conn);
  97.                         cmd.Transaction = tx;
  98.                         cmd.ExecuteNonQuery();
  99.                         attachment_id++;
  100.                     }
  101.                     //添加流程
  102.                     int flow_id = 1;
  103.                     cmd = new SqlCommand("select max(id) from OA_FAIR_FORM_FLOW", conn);
  104.                     cmd.Transaction = tx;
  105.                     try
  106.                     {
  107.                         flow_id = Convert.ToInt32(cmd.ExecuteScalar().ToString()) + 1;
  108.                     }
  109.                     catch { }
  110.                 
  111.                     //添加逐级签核流程
  112.                 //   if (main_id != 0)
  113.                 //   {
  114.                        ArrayList id1 = new ArrayList();//记录上层节点职级小于登陆者的ID
  115.                        ArrayList name1 = new ArrayList();//记录上层节点职级小于登陆者的name
  116.                        ArrayList mail1 = new ArrayList();//记录上层节点职级小于登陆者的mail
  117.                        ArrayList last_id = new ArrayList();//记录上层节点的ID
  118.                        ArrayList tmp_people = new ArrayList();//记录待签核人的ID                       
  119.                        ArrayList mail2 = new ArrayList();//记录代理人的MAIL
  120.                        ArrayList mail2_name = new ArrayList();//记录代理人姓名
  121.                        ArrayList operator_ids = new ArrayList();
  122.                        ArrayList d_people = new ArrayList();
  123.                       ArrayList b_d_people=new ArrayList();
  124.                    
  125.                        if (Convert.ToInt32(PositionId.Value) >8)
  126.                        {
  127.                            last_id.Add(DepartmentId.Value.ToString());
  128.                            int times = 3;                           
  129.                            if (Convert.ToInt32(PositionId.Value) == 9) { times = 2; }
  130.                            else if (Convert.ToInt32(PositionId.Value) == 10) { times = 2; }
  131.                            else if (Convert.ToInt32(PositionId.Value) == 11) { times = 3; }
  132.                            else { times = 3; }
  133.                            for (int j = 1; j < times; j++)
  134.                            {
  135.                                cmd = new SqlCommand("select distinct last_id from oa_department where id=" + last_id[j - 1], conn);
  136.                                cmd.Transaction = tx;
  137.                                SqlDataReader dr2 = cmd.ExecuteReader();
  138.                                while (dr2.Read())
  139.                                {
  140.                                    last_id.Add(dr2["last_id"].ToString());
  141.                                }
  142.                                dr2.Close();
  143.                            }
  144.                            //找到上层节点人的ID
  145.                            if (last_id.Count != 0)
  146.                            {
  147.                                for (int i = 0; i < last_id.Count; i++)
  148.                                {
  149.                                    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" +
  150.                                        " where  position<" + PositionId.Value + "and b.id=" + last_id[i] + " and q_manger=1 order by position desc", conn);
  151.                                    cmd.Transaction = tx;
  152.                                    SqlDataReader dr1 = cmd.ExecuteReader();
  153.                                    while (dr1.Read())
  154.                                    {
  155.                                        operator_ids.Add(dr1["id"].ToString());
  156.                                        name1.Add(dr1["emp_name"].ToString());
  157.                                        mail1.Add(dr1["email_work"].ToString());                                       
  158.                                      
  159.                                    } dr1.Close();
  160.                                }
  161.                            }
  162.                            cmd = new SqlCommand("select a.id as id from oa_employee as a left join oa_department as b on a.department_id=b.id" +
  163.                                       " where b.id=90 and q_manger=1 order by position desc", conn);
  164.                            cmd.Transaction = tx;
  165.                            SqlDataReader dr3 = cmd.ExecuteReader();
  166.                            while (dr3.Read())
  167.                            {
  168.                                operator_ids.Add(dr3["id"].ToString());                            
  169.                            } dr3.Close();
  170.                          
  171.                            operator_ids.Add("128");
  172.                            //  确定是否有代理人签核
  173.                            for (int k = 0; k < operator_ids.Count; k++)
  174.                            {
  175.                                cmd = new SqlCommand("select d_people,b_d_people from oa_delegate where  s_date<=convert(varchar, getdate(), 120)" +
  176.                                      " and e_date>=convert(varchar, getdate(),120) and b_d_people='" + operator_ids[k] + "'", conn);
  177.                                cmd.Transaction = tx;
  178.                                SqlDataReader dr4 = cmd.ExecuteReader();
  179.                                while (dr4.Read())
  180.                                {
  181.                                    d_people.Add(dr4["d_people"].ToString());
  182.                                    b_d_people.Add(dr4["b_d_people"].ToString());
  183.                                    operator_ids.RemoveAt(k);
  184.                                    operator_ids.Add(dr4["d_people"].ToString());
  185.                                }
  186.                                dr4.Close();
  187.                            }
  188.                            //是本人,跳過
  189.                            for (int i = 0; i < operator_ids.Count; i++)
  190.                            {
  191.                                if (operator_ids[i].ToString().Equals(Session["user_id"].ToString())) { operator_ids.RemoveAt(i); }
  192.                            }
  193.                            //
  194.                        }
  195.                        else {
  196.                            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" +
  197.                                       " where b.id=90 and q_manger=1 order by position desc", conn);
  198.                            cmd.Transaction = tx;
  199.                            SqlDataReader dr1 = cmd.ExecuteReader();
  200.                            while (dr1.Read())
  201.                            {
  202.                                operator_ids.Add(dr1["id"].ToString());
  203.                                name1.Add(dr1["emp_name"].ToString());
  204.                                mail1.Add(dr1["email_work"].ToString());
  205.                                tmp_people.Add(dr1["id"].ToString());
  206.                               
  207.                            } dr1.Close();
  208.                                                     
  209.                            operator_ids.Add("128");
  210.                            //  确定是否有代理人签核
  211.                            for (int k = 0; k < operator_ids.Count; k++)
  212.                            {
  213.                                cmd = new SqlCommand("select d_people,b_d_people from oa_delegate where  s_date<=convert(varchar, getdate(), 120)" +
  214.                                      " and e_date>=convert(varchar, getdate(),120) and b_d_people='" + operator_ids[k] + "'", conn);
  215.                                cmd.Transaction = tx;
  216.                                SqlDataReader dr3 = cmd.ExecuteReader();
  217.                                while (dr3.Read())
  218.                                {
  219.                                    d_people.Add(dr3["d_people"].ToString());
  220.                                    b_d_people.Add(dr3["b_d_people"].ToString());
  221.                                    operator_ids.RemoveAt(k);
  222.                                    operator_ids.Add(dr3["d_people"].ToString());
  223.                                }
  224.                                dr3.Close();
  225.                            }
  226.                        }
  227.                        //是本人,跳過
  228.                        for (int i = 0; i < operator_ids.Count; i++)
  229.                        {
  230.                            if (operator_ids[i].ToString().Equals(Session["user_id"].ToString())) { operator_ids.RemoveAt(i); }
  231.                        }
  232.                        //
  233.                      
  234.                        mail.mail_q(mail1[0], name1[0], TxtEmpName.Text,"費用申請單");
  235.                         for (int i = 0; i < operator_ids.Count; i++)
  236.                         {
  237.                             cmd = new SqlCommand("insert into OA_FAIR_FORM_FLOW(id, form_id, operator_id, " +
  238.                                 "apply_type, is_check, is_agree, comment, check_date, step)values(" +
  239.                                 flow_id + ", " + fair_id + "," + operator_ids[i] + ",2,'N','N','',''," + (i + 1) + ")", conn);
  240.                             cmd.Transaction = tx;
  241.                             cmd.ExecuteNonQuery();
  242.                             flow_id++;
  243.                         }
  244.                         tx.Commit();
  245.                         Response.Redirect("my_list.aspx");
  246.              
  247.                 }
  248.                 catch (Exception ex)
  249.                 {
  250.                     Response.Write(ex.Message);
  251.                     try
  252.                     {
  253.                         tx.Rollback();
  254.                     }
  255.                     catch { }
  256.                 }
  257.                 conn.Close();
  258.             }
  259.         }
  260.     }
  261.     protected bool CheckValidate()
  262.     {
  263.         try
  264.         {
  265.             double amount = Convert.ToDouble(TxtAmount.Text.Trim());
  266.         }
  267.         catch 
  268.         {
  269.             LblError.Text = "申請金額格式有誤";
  270.             Response.Write("<script>alert('申請金額格式有誤')</script>");
  271.             return false;
  272.         }
  273.         return true;
  274.     }
  275.     protected bool UploadFiles(string time)
  276.     {
  277.         attachments = new ArrayList();
  278.         attachment_save_names = new ArrayList();
  279.         content_lengths = new ArrayList();
  280.         bool flag = Upload(FileUpload1, time) && Upload(FileUpload2, time) && Upload(FileUpload3, time)
  281.             && Upload(FileUpload4, time) && Upload(FileUpload5, time);
  282.         return flag;
  283.     }
  284.     protected bool Upload(FileUpload fileUpload, string time)
  285.     {
  286.         bool flag = true;
  287.         try
  288.         {
  289.             string directory = Server.MapPath("~/web/fair_picture/");
  290.             string file_extension = System.IO.Path.GetExtension(fileUpload.FileName).ToLower();
  291.             string[] allowed_extensions = { ".jpg", ".gif" };
  292.             bool extenstion = false ;
  293.             if (fileUpload.HasFile)
  294.             {                
  295.                 for (int i = 0; i < allowed_extensions.Length; i++)
  296.                 {
  297.                     if (file_extension == allowed_extensions[i])
  298.                     {
  299.                         extenstion = true;
  300.                         break;
  301.                     }
  302.                 }
  303.                 if (!extenstion)
  304.                 {
  305.                     LblError.Text = "只允許上傳格式為gif, jpg的圖檔!";
  306.                     Response.Write("<script>alert('只允許上傳格式為gif, jpg的圖檔')</script>");
  307.                     return false;
  308.                 }
  309.                 else
  310.                 {
  311.                     string name = fileUpload.FileName.Substring(0, fileUpload.FileName.Length - 4) + 
  312.                         time + System.IO.Path.GetExtension(fileUpload.FileName).ToLower();
  313.                     fileUpload.PostedFile.SaveAs(directory + name);
  314.                     attachments.Add(fileUpload.FileName);
  315.                     attachment_save_names.Add(name);
  316.                     content_lengths.Add(fileUpload.PostedFile.ContentLength.ToString());
  317.                     flag = true;
  318.                 }
  319.             }
  320.         }
  321.         catch
  322.         {
  323.             LblError.Text = "上傳檔案失敗!";
  324.             Response.Write("<script>alert('上傳檔案失敗')</script>");
  325.             return false;
  326.         }
  327.         return flag;
  328.     }
  329. }