Ejemplo n.º 1
0
    public static ReturnObject DoImport(string file_name)
    {
        leave_assign page_object   = new leave_assign();
        DBConnection db_connection = new DBConnection();
        ReturnObject return_object = new ReturnObject();
        DataTable    leave_data    = new DataTable();

        string leave_code            = string.Empty;
        string effectd_rows          = string.Empty;
        string excelConnectionString = string.Empty;
        string upload_path           = string.Empty;
        string ExcelFullPath         = string.Empty;
        string employee_id           = string.Empty;

        string[] rows = new string[2];
        string[] leave_codes_Master    = null;
        string[] employee_codes_master = null;
        string   Return_message        = string.Empty;

        double max_leave = 0.0, leave_applied = 0.0;
        int    row_number = 0;
        int    total_rows = 0;

        bool
            leave_code_flag = true,
            max_leaves_flag = true;

        string row_inserted = string.Empty, row_rejected = string.Empty;

        try
        {
            upload_path   = ConfigurationManager.AppSettings["TEMP_FILE_UPLOAD"].ToString();
            ExcelFullPath = HttpContext.Current.Server.MapPath("~/" + upload_path + "/" + file_name);

            leave_data = page_object.ReturnExcelasDataTable(ExcelFullPath, "");

            if (leave_data.Rows.Count > 0)
            {
                total_rows            = leave_data.Rows.Count;
                employee_codes_master = GetEmployeeCodes();

                foreach (DataRow dReader in leave_data.Rows)
                {
                    row_number++;
                    if (row_number == 1)
                    {
                        continue;
                    }
                    employee_id = Convert.ToString(dReader["EMP_CODE"]).Trim();

                    if (!string.IsNullOrEmpty(employee_id))
                    {
                        if (Array.IndexOf(employee_codes_master, employee_id) >= 0)
                        {
                            leave_codes_Master = GetLeaveCodes(employee_id);

                            if (!string.IsNullOrEmpty(dReader["LEAVE_CODE"].ToString()))
                            {
                                leave_code = Convert.ToString(dReader["LEAVE_CODE"]);
                            }
                            else
                            {
                                Return_message += " Leave code cannot be empty on row: " + row_number + Environment.NewLine;
                                leave_code_flag = false;
                            }


                            if (!string.IsNullOrEmpty(dReader["MAX_LEAVES"].ToString()))
                            {
                                max_leave = Convert.ToDouble(dReader["MAX_LEAVES"]);
                            }
                            else
                            {
                                Return_message += " Max Leaves cannot be empty on row: " + row_number + Environment.NewLine;
                                max_leaves_flag = false;
                            }

                            if (!string.IsNullOrEmpty(dReader["LEAVES_APPLIED"].ToString()))
                            {
                                leave_applied = Convert.ToDouble(dReader["LEAVES_APPLIED"]);
                            }
                            else
                            {
                                leave_applied = 0.0;
                            }

                            if (leave_code_flag && max_leaves_flag && leave_applied <= max_leave)
                            {
                                if (Array.IndexOf(leave_codes_Master, leave_code) >= 0)
                                {
                                    Hashtable hshParam = new Hashtable();
                                    hshParam.Add("piempcode", employee_id);
                                    hshParam.Add("pileavecode", leave_code);
                                    hshParam.Add("piMaxleave", max_leave);
                                    hshParam.Add("pileaveapplied", leave_applied);

                                    db_connection.ExecuteStoredProcedureWithHashtable_WithoutReturn("spimportleave", hshParam);

                                    Return_message += leave_code + " Leave assigned successfully for employee " + employee_id + Environment.NewLine;
                                }
                                else
                                {
                                    Return_message += leave_code + " Leave does not belong for employee " + employee_id + Environment.NewLine;
                                }
                            }
                        }
                        else
                        {
                            Return_message += " You don't have permission to assign leave for Employee Code " + employee_id + " on row: " + row_number + Environment.NewLine;
                        }
                    }
                    else
                    {
                        Return_message += " Employee Code cannot be Empty on row: " + row_number + Environment.NewLine;
                    }

                    max_leaves_flag = true;
                    leave_code_flag = true;
                }

                rows[0] = total_rows.ToString();
                rows[1] = effectd_rows;
            }

            return_object.status = "success";
            //return_object.return_data = JsonConvert.SerializeObject(rows, Formatting.Indented);
            return_object.return_data = Return_message;
        }
        catch (Exception ex)
        {
            Logger.LogException(ex, page, "IMPORT_FILE_DATA");

            return_object.status      = "error";
            return_object.return_data = "An error occurred while loading Importing Leaves data. Please try again. If the error persists, please contact Support.";
        }

        return(return_object);
    }