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

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 = 1; 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.                        else {
  190.                            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" +
  191.                                       " where b.id=90 and q_manger=1 order by position desc", conn);
  192.                            cmd.Transaction = tx;
  193.                            SqlDataReader dr1 = cmd.ExecuteReader();
  194.                            while (dr1.Read())
  195.                            {
  196.                                operator_ids.Add(dr1["id"].ToString());
  197.                                name1.Add(dr1["emp_name"].ToString());
  198.                                mail1.Add(dr1["email_work"].ToString());
  199.                                tmp_people.Add(dr1["id"].ToString());
  200.                               
  201.                            } dr1.Close();
  202.                                                     
  203.                            operator_ids.Add("128");
  204.                            //  确定是否有代理人签核
  205.                            for (int k = 0; k < operator_ids.Count; k++)
  206.                            {
  207.                                cmd = new SqlCommand("select d_people,b_d_people from oa_delegate where  s_date<=convert(varchar, getdate(), 120)" +
  208.                                      " and e_date>=convert(varchar, getdate(),120) and b_d_people='" + operator_ids[k] + "'", conn);
  209.                                cmd.Transaction = tx;
  210.                                SqlDataReader dr3 = cmd.ExecuteReader();
  211.                                while (dr3.Read())
  212.                                {
  213.                                    d_people.Add(dr3["d_people"].ToString());
  214.                                    b_d_people.Add(dr3["b_d_people"].ToString());
  215.                                    operator_ids.RemoveAt(k);
  216.                                    operator_ids.Add(dr3["d_people"].ToString());
  217.                                }
  218.                                dr3.Close();
  219.                            }
  220.                        }
  221.                      
  222.                        mail.mail_q(mail1[0], name1[0], TxtEmpName.Text,"費用申請單");
  223.                         for (int i = 0; i < operator_ids.Count; i++)
  224.                         {
  225.                             cmd = new SqlCommand("insert into OA_FAIR_FORM_FLOW(id, form_id, operator_id, " +
  226.                                 "apply_type, is_check, is_agree, comment, check_date, step)values(" +
  227.                                 flow_id + ", " + fair_id + "," + operator_ids[i] + ",2,'N','N','',''," + (i + 1) + ")", conn);
  228.                             cmd.Transaction = tx;
  229.                             cmd.ExecuteNonQuery();
  230.                             flow_id++;
  231.                         }
  232.                         tx.Commit();
  233.                         Response.Redirect("my_list.aspx");
  234.              
  235.                 }
  236.                 catch (Exception ex)
  237.                 {
  238.                     Response.Write(ex.Message);
  239.                     try
  240.                     {
  241.                         tx.Rollback();
  242.                     }
  243.                     catch { }
  244.                 }
  245.                 conn.Close();
  246.             }
  247.         }
  248.     }
  249.     protected bool CheckValidate()
  250.     {
  251.         try
  252.         {
  253.             double amount = Convert.ToDouble(TxtAmount.Text.Trim());
  254.         }
  255.         catch 
  256.         {
  257.             LblError.Text = "申請金額格式有誤";
  258.             Response.Write("<script>alert('申請金額格式有誤')</script>");
  259.             return false;
  260.         }
  261.         return true;
  262.     }
  263.     protected bool UploadFiles(string time)
  264.     {
  265.         attachments = new ArrayList();
  266.         attachment_save_names = new ArrayList();
  267.         content_lengths = new ArrayList();
  268.         bool flag = Upload(FileUpload1, time) && Upload(FileUpload2, time) && Upload(FileUpload3, time)
  269.             && Upload(FileUpload4, time) && Upload(FileUpload5, time);
  270.         return flag;
  271.     }
  272.     protected bool Upload(FileUpload fileUpload, string time)
  273.     {
  274.         bool flag = true;
  275.         try
  276.         {
  277.             string directory = Server.MapPath("~/web/fair_picture/");
  278.             string file_extension = System.IO.Path.GetExtension(fileUpload.FileName).ToLower();
  279.             string[] allowed_extensions = { ".jpg", ".gif" };
  280.             bool extenstion = false ;
  281.             if (fileUpload.HasFile)
  282.             {                
  283.                 for (int i = 0; i < allowed_extensions.Length; i++)
  284.                 {
  285.                     if (file_extension == allowed_extensions[i])
  286.                     {
  287.                         extenstion = true;
  288.                         break;
  289.                     }
  290.                 }
  291.                 if (!extenstion)
  292.                 {
  293.                     LblError.Text = "只允許上傳格式為gif, jpg的圖檔!";
  294.                     Response.Write("<script>alert('只允許上傳格式為gif, jpg的圖檔')</script>");
  295.                     return false;
  296.                 }
  297.                 else
  298.                 {
  299.                     string name = fileUpload.FileName.Substring(0, fileUpload.FileName.Length - 4) + 
  300.                         time + System.IO.Path.GetExtension(fileUpload.FileName).ToLower();
  301.                     fileUpload.PostedFile.SaveAs(directory + name);
  302.                     attachments.Add(fileUpload.FileName);
  303.                     attachment_save_names.Add(name);
  304.                     content_lengths.Add(fileUpload.PostedFile.ContentLength.ToString());
  305.                     flag = true;
  306.                 }
  307.             }
  308.         }
  309.         catch
  310.         {
  311.             LblError.Text = "上傳檔案失敗!";
  312.             Response.Write("<script>alert('上傳檔案失敗')</script>");
  313.             return false;
  314.         }
  315.         return flag;
  316.     }
  317. }