using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace HRM.BO
{
#region EnumBulkUploadColumnKind
///
/// What a bulk-upload column holds. Drives the number format and the dropdown in the
/// generated workbook, and (later) how the uploaded value is parsed and validated.
///
public enum EnumBulkUploadColumnKind
{
Text = 1,
Date = 2,
Integer = 3,
Decimal = 4,
Percentage = 5,
Email = 6,
Gender = 7,
MaritalStatus = 8,
SponsoredType = 10,
TaxCircle = 11,
/// A dropdown of the blood groups the Employee Profile lists (A+ … AB-).
BloodGroup = 12,
/// A Bangladesh mobile number, as the Employee Profile accepts it.
Mobile = 13
}
#endregion
#region EnumBulkUploadMasterData
///
/// The DB table a bulk-upload column's value must exist in — each member is named after its
/// table. The validator loads that table and matches the cell against it — by Employee No for
/// Employee, by name / description for the rest.
///
public enum EnumBulkUploadMasterData
{
None = 0,
Category = 1,
Religion = 2,
Grades = 3,
Occupation = 4,
District = 5,
Thana = 6,
PersonRelation = 7,
EducationType = 8,
EducationLevel = 9,
Institution = 10,
Discipline = 11,
ResultType = 12,
NominationPurpose = 13,
TrainingType = 14,
NatureOfTraining = 15,
Country = 16,
Employee = 17,
/// No column uses it: the Division a new District is added under.
Division = 18,
Nationality = 19
}
#endregion
#region EmployeeBulkUploadColumn
/// One column of the Employee Bulk Upload sheet (one cell of header row 3).
public class EmployeeBulkUploadColumn
{
public EmployeeBulkUploadColumn(string label, bool isMandatory = false,
EnumBulkUploadColumnKind kind = EnumBulkUploadColumnKind.Text, double width = 0,
EnumBulkUploadMasterData masterData = EnumBulkUploadMasterData.None, string dependsOn = null)
{
Label = label;
IsMandatory = isMandatory;
Kind = kind;
MasterData = masterData;
DependsOn = dependsOn;
// An explicit width, else sized to the label — but never narrower than the heading,
// so the heading row stays one line high and nothing wraps.
double baseWidth = width > 0 ? width : Math.Max(12, Math.Min(26, label.Length + 4));
Width = Math.Max(baseWidth, Math.Ceiling(Heading.Length * 1.15) + 2);
}
public string Label { get; set; }
public bool IsMandatory { get; set; }
public EnumBulkUploadColumnKind Kind { get; set; }
public double Width { get; set; }
/// The table this column's value must exist in; None for free values.
public EnumBulkUploadMasterData MasterData { get; set; }
///
/// Label of the column in the same sub-group this value belongs under — Thana under
/// District, Education Level under Education Platform. Null when it stands alone.
///
public string DependsOn { get; set; }
public bool IsMasterData
{
get { return MasterData != EnumBulkUploadMasterData.None; }
}
/// True when the cell holds the record's code (Employee No), not its name.
public bool MatchesByCode
{
get { return MasterData == EnumBulkUploadMasterData.Employee; }
}
///
/// The column of the table the cell is matched against — its name
/// column, or its code column when . Null when not master data.
///
public string MasterDataColumn
{
get
{
switch (MasterData)
{
case EnumBulkUploadMasterData.Category: return "DESCRIPTION";
case EnumBulkUploadMasterData.Religion: return "NAME";
case EnumBulkUploadMasterData.Nationality: return "DESCRIPTION";
case EnumBulkUploadMasterData.Grades: return "DESCRIPTION";
case EnumBulkUploadMasterData.Occupation: return "DESCRIPTION";
case EnumBulkUploadMasterData.District: return "NAME";
case EnumBulkUploadMasterData.Thana: return "NAME";
case EnumBulkUploadMasterData.PersonRelation: return "DESCRIPTION";
case EnumBulkUploadMasterData.EducationType: return "DESCRIPTION";
case EnumBulkUploadMasterData.EducationLevel: return "DESCRIPTION";
case EnumBulkUploadMasterData.Institution: return "NAME";
case EnumBulkUploadMasterData.Discipline: return "DESCRIPTION";
case EnumBulkUploadMasterData.ResultType: return "DESCRIPTION";
case EnumBulkUploadMasterData.NominationPurpose: return "DESCRIPTION";
case EnumBulkUploadMasterData.TrainingType: return "NAME";
case EnumBulkUploadMasterData.NatureOfTraining: return "NAME";
case EnumBulkUploadMasterData.Country: return "NAME";
case EnumBulkUploadMasterData.Employee: return "EMPLOYEENO";
default: return null;
}
}
}
///
/// Unique field name of this column across the whole layout, e.g. "children01_birthdate".
/// Set by EmployeeBulkUploadTemplate.GetGroups(); an uploaded row's values are keyed by it.
///
public string Key { get; set; }
/// The heading as it is written into the sheet; mandatory columns carry a " *".
public string Heading
{
get { return IsMandatory ? Label + " *" : Label; }
}
/// The blood groups in EnumBloodGroup order (A+ = 1 … AB- = 8).
public const string BloodGroups = "A+,A-,B+,B-,O+,O-,AB+,AB-";
/// The inline dropdown list for this column, or null when it is free text.
public string DropdownList
{
get
{
switch (Kind)
{
case EnumBulkUploadColumnKind.Gender: return "Male,Female,Other";
case EnumBulkUploadColumnKind.MaritalStatus: return "Married,UnMarried,Divorced,Widow";
// The EnumTaxCircle names.
case EnumBulkUploadColumnKind.TaxCircle: return "CityCorporation,Municipal,Union";
// As the Employee Profile lists them; saved as EnumBloodGroup 1-8 in this order.
case EnumBulkUploadColumnKind.BloodGroup: return BloodGroups;
// The EnumSponsoredType names, as the ESS profile-update page lists them.
case EnumBulkUploadColumnKind.SponsoredType:
return "TrainingBeforeJoining,TrainingConductedByCompany,SelfInitiatedTraining,TrainingBeforeJoiningButAtCompany";
default: return null;
}
}
}
/// The Excel number format for this column, or null to leave it General.
public string NumberFormat
{
get
{
switch (Kind)
{
// "@" = Text: digit strings (NID, TIN, mobile, account no) keep their leading
// zeros and never turn into 1.23E+16.
case EnumBulkUploadColumnKind.Text:
case EnumBulkUploadColumnKind.Mobile:
case EnumBulkUploadColumnKind.Email: return "@";
case EnumBulkUploadColumnKind.Date: return "DD-MMM-YYYY";
case EnumBulkUploadColumnKind.Integer: return "0";
case EnumBulkUploadColumnKind.Decimal: return "#,##0.00";
case EnumBulkUploadColumnKind.Percentage: return "0.00";
default: return null;
}
}
}
}
#endregion
#region EmployeeBulkUploadSubGroup
///
/// One band of header row 2, such as "Children 01". Name is null for a group that has no
/// second band — such a group merges rows 1 and 2.
///
public class EmployeeBulkUploadSubGroup
{
public EmployeeBulkUploadSubGroup(string name, List columns,
bool isRepeatBlock = false)
{
Name = name;
Columns = columns ?? new List();
IsRepeatBlock = isRepeatBlock;
}
public string Name { get; set; }
public List Columns { get; set; }
///
/// One of several numbered copies (Children 01, Children 02, ...). An employee may have
/// fewer records than copies, so a block left wholly empty in a row means "no record" and
/// its mandatory columns apply only once the block holds any value.
///
public bool IsRepeatBlock { get; set; }
}
#endregion
#region EmployeeBulkUploadGroup
/// One band of header row 1, with its colour and its sub-bands.
public class EmployeeBulkUploadGroup
{
public EmployeeBulkUploadGroup(string name, string bandColor, string subBandColor,
string headerColor, string textColor, List subGroups)
{
Name = name;
BandColor = bandColor;
SubBandColor = subBandColor;
HeaderColor = headerColor;
TextColor = textColor;
SubGroups = subGroups ?? new List();
}
public string Name { get; set; }
public string BandColor { get; set; } // header row 1
public string SubBandColor { get; set; } // header row 2
public string HeaderColor { get; set; } // header row 3
public string TextColor { get; set; }
public List SubGroups { get; set; }
public int ColumnCount
{
get { return SubGroups.Sum(x => x.Columns.Count); }
}
/// True when row 2 carries its own bands, so row 1 is not merged downwards.
public bool HasSubBands
{
get { return SubGroups.Any(x => !string.IsNullOrEmpty(x.Name)); }
}
}
#endregion
#region EmployeeBulkUploadTemplate
///
/// The layout of the Employee Bulk Upload workbook — one row per employee, with the employee's
/// children, spouse, education, nominee, experience and training in that same row.
///
/// This is the single source of truth: EmployeeBulkUploadService renders the downloadable
/// template from it, and the upload validator is meant to read the uploaded file against it,
/// so the template and the validation can never drift apart.
///
/// Every sub-group carries exactly the fields of the matching screen — no extra columns:
/// Basic Information follows the core-hr Employee Profile (General, Extended Personal
/// Information and the Contact tab), the other groups the ESS profile-update forms.
///
/// The user picks which sub-groups go into the downloaded file (Employee is always in), so a
/// file holds a subset of this layout: the uploader must locate each sub-group by its row-2
/// band name and each column by its row-3 heading, never by absolute column position.
/// Within a sub-group, column order and headings are fixed.
/// Adding a column means appending it inside its group, never inserting in the middle.
///
public static class EmployeeBulkUploadTemplate
{
#region Sheet constants
public const string FileName = "Employee-Bulk-Upload-Template.xlsx";
public const string SheetName = "Employee Data";
public const string ContentType =
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
public const int GroupBandRow = 1;
public const int SubBandRow = 2;
public const int HeadingRow = 3;
public const int FirstDataRow = 4;
/// Data rows given an explicit number format in the generated file.
public const int FormattedDataRows = 200;
/// Data rows covered by the dropdown validations.
public const int ValidatedDataRows = 997;
/// Largest upload accepted, in MB.
public const int MaxUploadMegabytes = 10;
/// Most employee rows read from one file.
public const int MaxUploadRows = 2000;
///
/// The sub-group that is always in the file, whatever the user picks: Employee No and
/// Employee Name, which tie every row to an employee. Every other sub-group is optional.
///
public const string AlwaysIncludedSubGroup = "Employee";
///
/// A master-data value this alike (or more) to exactly one record is taken as that record;
/// below it the value counts as missing.
///
public const int CloseMatchPercent = 90;
public const string MandatoryTextColor = "#B23B3B";
public const string OptionalTextColor = "#1F2933";
public const string BorderColor = "#7B8794";
#endregion
#region Column definitions
/// The key of every row — always in the file, first, and frozen while scrolling.
private static List EmployeeColumns()
{
return new List
{
new EmployeeBulkUploadColumn("Employee No", true),
new EmployeeBulkUploadColumn("Employee Name", true, EnumBulkUploadColumnKind.Text, 24),
};
}
///
/// The employee's core record — the "General" band of Basic Information. Follows the
/// core-hr Employee Profile "Personal Information" form, with its mandatory fields; the
/// saved Name is First + Middle + Last Name, as the form saves it.
///
private static List GeneralColumns()
{
return new List
{
new EmployeeBulkUploadColumn("First Name", true),
new EmployeeBulkUploadColumn("Middle Name"),
new EmployeeBulkUploadColumn("Last Name", true),
new EmployeeBulkUploadColumn("Name (Ban)", false, EnumBulkUploadColumnKind.Text, 24),
new EmployeeBulkUploadColumn("Date of Birth", true, EnumBulkUploadColumnKind.Date),
new EmployeeBulkUploadColumn("Gender", true, EnumBulkUploadColumnKind.Gender),
new EmployeeBulkUploadColumn("Mobile No", true, EnumBulkUploadColumnKind.Mobile, 16),
new EmployeeBulkUploadColumn("Email", true, EnumBulkUploadColumnKind.Email, 26),
new EmployeeBulkUploadColumn("Date of Joining", true, EnumBulkUploadColumnKind.Date),
new EmployeeBulkUploadColumn("Blood Group", true, EnumBulkUploadColumnKind.BloodGroup),
new EmployeeBulkUploadColumn("Marital Status", true, EnumBulkUploadColumnKind.MaritalStatus),
new EmployeeBulkUploadColumn("Religion Name", true, masterData: EnumBulkUploadMasterData.Religion),
new EmployeeBulkUploadColumn("Nationality", masterData: EnumBulkUploadMasterData.Nationality),
new EmployeeBulkUploadColumn("Category Name", true, masterData: EnumBulkUploadMasterData.Category),
new EmployeeBulkUploadColumn("Tax Circle", false, EnumBulkUploadColumnKind.TaxCircle, 20),
new EmployeeBulkUploadColumn("Grade Code", true, masterData: EnumBulkUploadMasterData.Grades),
new EmployeeBulkUploadColumn("Confirmation Date", false, EnumBulkUploadColumnKind.Date),
new EmployeeBulkUploadColumn("Line Manager ID", masterData: EnumBulkUploadMasterData.Employee),
};
}
///
/// The core-hr Employee Profile "Extended Personal Information" fields, except Spouse Name
/// (the Spouse group carries it) and the Signature upload.
///
private static List ExtendedPersonalColumns()
{
return new List
{
new EmployeeBulkUploadColumn("Global Emp ID"),
new EmployeeBulkUploadColumn("National ID No", false, EnumBulkUploadColumnKind.Text, 20),
new EmployeeBulkUploadColumn("TIN No"),
new EmployeeBulkUploadColumn("Passport No"),
new EmployeeBulkUploadColumn("Passport Issued Date", false, EnumBulkUploadColumnKind.Date),
new EmployeeBulkUploadColumn("Passport Expiry Date", false, EnumBulkUploadColumnKind.Date),
new EmployeeBulkUploadColumn("Driving License No", false, EnumBulkUploadColumnKind.Text, 20),
new EmployeeBulkUploadColumn("Father's Name", false, EnumBulkUploadColumnKind.Text, 24),
new EmployeeBulkUploadColumn("Father's Name (Ban)", false, EnumBulkUploadColumnKind.Text, 24),
new EmployeeBulkUploadColumn("Father's Occupation", false, EnumBulkUploadColumnKind.Text, 20, masterData: EnumBulkUploadMasterData.Occupation),
new EmployeeBulkUploadColumn("Mother's Name", false, EnumBulkUploadColumnKind.Text, 24),
new EmployeeBulkUploadColumn("Mother's Name (Ban)", false, EnumBulkUploadColumnKind.Text, 24),
new EmployeeBulkUploadColumn("Mother's Occupation", false, EnumBulkUploadColumnKind.Text, 20, masterData: EnumBulkUploadMasterData.Occupation),
new EmployeeBulkUploadColumn("Birth Place"),
new EmployeeBulkUploadColumn("Height (Cm)"),
};
}
///
/// One address block of the core-hr Employee Profile "Contact" tab — Present and Permanent
/// Address share the same fields.
///
private static List AddressColumns()
{
return new List
{
new EmployeeBulkUploadColumn("Address", false, EnumBulkUploadColumnKind.Text, 34),
new EmployeeBulkUploadColumn("Address (Bn)", false, EnumBulkUploadColumnKind.Text, 34),
new EmployeeBulkUploadColumn("District", masterData: EnumBulkUploadMasterData.District),
new EmployeeBulkUploadColumn("Thana", masterData: EnumBulkUploadMasterData.Thana, dependsOn: "District"),
new EmployeeBulkUploadColumn("Land Phone No"),
new EmployeeBulkUploadColumn("Post Office Name (Bn)", false, EnumBulkUploadColumnKind.Text, 22),
new EmployeeBulkUploadColumn("Mobile No"),
new EmployeeBulkUploadColumn("Postal Code"),
};
}
/// The "Contact Details" block of the Employee Profile "Contact" tab.
private static List ContactDetailsColumns()
{
return new List
{
new EmployeeBulkUploadColumn("Land Phone No"),
new EmployeeBulkUploadColumn("Mobile No"),
new EmployeeBulkUploadColumn("Personal Email", false, EnumBulkUploadColumnKind.Email, 24),
new EmployeeBulkUploadColumn("Fax"),
};
}
/// The "Emergency Contact Details" block of the Employee Profile "Contact" tab.
private static List EmergencyContactColumns()
{
return new List
{
new EmployeeBulkUploadColumn("Contact Person", false, EnumBulkUploadColumnKind.Text, 22),
new EmployeeBulkUploadColumn("Address", false, EnumBulkUploadColumnKind.Text, 34),
new EmployeeBulkUploadColumn("Relation", masterData: EnumBulkUploadMasterData.PersonRelation),
new EmployeeBulkUploadColumn("Mobile No"),
};
}
///
/// The fields of the ESS profile-update "Children Information" form. Its passport fields
/// are commented out on that form, so they are not collected here either.
///
private static List ChildrenColumns()
{
return new List
{
new EmployeeBulkUploadColumn("Name", true, EnumBulkUploadColumnKind.Text, 22),
new EmployeeBulkUploadColumn("Gender", true, EnumBulkUploadColumnKind.Gender),
new EmployeeBulkUploadColumn("Birth Date", true, EnumBulkUploadColumnKind.Date),
new EmployeeBulkUploadColumn("Occupation", masterData: EnumBulkUploadMasterData.Occupation),
new EmployeeBulkUploadColumn("Marital Status", true, EnumBulkUploadColumnKind.MaritalStatus),
};
}
/// The fields of the ESS profile-update "Spouse Information" form.
private static List SpouseColumns()
{
return new List
{
new EmployeeBulkUploadColumn("Name", true, EnumBulkUploadColumnKind.Text, 22),
new EmployeeBulkUploadColumn("Level of Education", false, EnumBulkUploadColumnKind.Text, 20, masterData: EnumBulkUploadMasterData.EducationLevel),
new EmployeeBulkUploadColumn("Occupation", masterData: EnumBulkUploadMasterData.Occupation),
new EmployeeBulkUploadColumn("Birth Date", false, EnumBulkUploadColumnKind.Date),
new EmployeeBulkUploadColumn("Marriage Date", false, EnumBulkUploadColumnKind.Date),
};
}
/// The fields of the ESS profile-update "Educational Information" form; its file attachment is left out.
private static List EducationColumns()
{
return new List
{
new EmployeeBulkUploadColumn("Education Platform", true, EnumBulkUploadColumnKind.Text, 20, masterData: EnumBulkUploadMasterData.EducationType),
new EmployeeBulkUploadColumn("Education Level", true, EnumBulkUploadColumnKind.Text, 20, masterData: EnumBulkUploadMasterData.EducationLevel, dependsOn: "Education Platform"),
new EmployeeBulkUploadColumn("University / Board", false, EnumBulkUploadColumnKind.Text, 24, masterData: EnumBulkUploadMasterData.Institution),
new EmployeeBulkUploadColumn("Discipline", false, EnumBulkUploadColumnKind.Text, 20, masterData: EnumBulkUploadMasterData.Discipline),
new EmployeeBulkUploadColumn("Institute Name", false, EnumBulkUploadColumnKind.Text, 26),
new EmployeeBulkUploadColumn("Exam Year", false, EnumBulkUploadColumnKind.Integer),
new EmployeeBulkUploadColumn("Passing Year", true, EnumBulkUploadColumnKind.Integer),
new EmployeeBulkUploadColumn("Result Standard", false, EnumBulkUploadColumnKind.Text, 18, masterData: EnumBulkUploadMasterData.ResultType),
new EmployeeBulkUploadColumn("GPA / Marks", false, EnumBulkUploadColumnKind.Decimal),
new EmployeeBulkUploadColumn("Out of", false, EnumBulkUploadColumnKind.Decimal),
};
}
/// The fields of the ESS profile-update "Nominee Information" form; its file attachment is left out.
private static List NomineeColumns()
{
return new List
{
new EmployeeBulkUploadColumn("Nomination Purpose", true, EnumBulkUploadColumnKind.Text, 22, masterData: EnumBulkUploadMasterData.NominationPurpose),
new EmployeeBulkUploadColumn("Nominee Name", true, EnumBulkUploadColumnKind.Text, 22),
new EmployeeBulkUploadColumn("Relation", true, masterData: EnumBulkUploadMasterData.PersonRelation),
new EmployeeBulkUploadColumn("Percentage", true, EnumBulkUploadColumnKind.Percentage),
new EmployeeBulkUploadColumn("Birth Date", false, EnumBulkUploadColumnKind.Date),
new EmployeeBulkUploadColumn("Occupation", masterData: EnumBulkUploadMasterData.Occupation),
new EmployeeBulkUploadColumn("Address", false, EnumBulkUploadColumnKind.Text, 34),
new EmployeeBulkUploadColumn("Telephone"),
new EmployeeBulkUploadColumn("Email", false, EnumBulkUploadColumnKind.Email, 24),
};
}
/// The fields of the ESS profile-update "Experience Information" form; its file attachment is left out.
private static List ExperienceColumns()
{
return new List
{
new EmployeeBulkUploadColumn("Employer", true, EnumBulkUploadColumnKind.Text, 24),
new EmployeeBulkUploadColumn("Business Type", false, EnumBulkUploadColumnKind.Text, 20),
new EmployeeBulkUploadColumn("Employer Contact Person", false, EnumBulkUploadColumnKind.Text, 24),
new EmployeeBulkUploadColumn("Designation", true, EnumBulkUploadColumnKind.Text, 20),
new EmployeeBulkUploadColumn("Address", false, EnumBulkUploadColumnKind.Text, 34),
new EmployeeBulkUploadColumn("Role Definition", false, EnumBulkUploadColumnKind.Text, 30),
new EmployeeBulkUploadColumn("Telephone No"),
new EmployeeBulkUploadColumn("From Date", true, EnumBulkUploadColumnKind.Date),
new EmployeeBulkUploadColumn("To Date", false, EnumBulkUploadColumnKind.Date),
};
}
///
/// The fields of the ESS profile-update "Training Information" form, in the form's order.
/// The certificate attachment has no place in a sheet and is left out.
///
private static List TrainingColumns()
{
return new List
{
new EmployeeBulkUploadColumn("Training Type", true, EnumBulkUploadColumnKind.Text, 20, masterData: EnumBulkUploadMasterData.TrainingType),
new EmployeeBulkUploadColumn("Nature of Training", true, EnumBulkUploadColumnKind.Text, 20, masterData: EnumBulkUploadMasterData.NatureOfTraining),
new EmployeeBulkUploadColumn("Training Title", true, EnumBulkUploadColumnKind.Text, 26),
new EmployeeBulkUploadColumn("Description", false, EnumBulkUploadColumnKind.Text, 30),
new EmployeeBulkUploadColumn("Institution", true, EnumBulkUploadColumnKind.Text, 24, masterData: EnumBulkUploadMasterData.Institution),
new EmployeeBulkUploadColumn("Institute Place", false, EnumBulkUploadColumnKind.Text, 20),
new EmployeeBulkUploadColumn("Country", masterData: EnumBulkUploadMasterData.Country),
new EmployeeBulkUploadColumn("Achievement", false, EnumBulkUploadColumnKind.Text, 20),
new EmployeeBulkUploadColumn("From Date", false, EnumBulkUploadColumnKind.Date),
new EmployeeBulkUploadColumn("To Date", false, EnumBulkUploadColumnKind.Date),
new EmployeeBulkUploadColumn("Sponsor Type", false, EnumBulkUploadColumnKind.SponsoredType, 30),
new EmployeeBulkUploadColumn("Months", false, EnumBulkUploadColumnKind.Integer),
new EmployeeBulkUploadColumn("Days", false, EnumBulkUploadColumnKind.Integer),
new EmployeeBulkUploadColumn("Hours", false, EnumBulkUploadColumnKind.Integer),
new EmployeeBulkUploadColumn("Training Fees", false, EnumBulkUploadColumnKind.Decimal),
new EmployeeBulkUploadColumn("Other Costs", false, EnumBulkUploadColumnKind.Decimal),
};
}
#endregion
#region Layout
///
/// The seven header-row-1 groups, left to right. Built fresh on each call so a caller
/// cannot mutate the layout for everybody else.
///
public static List GetGroups()
{
List groups = new List
{
new EmployeeBulkUploadGroup("Basic Information",
"#D8E4F0", "#E9F0F8", "#F4F8FC", "#2C4A6B",
new List
{
new EmployeeBulkUploadSubGroup(AlwaysIncludedSubGroup, EmployeeColumns()),
new EmployeeBulkUploadSubGroup("General", GeneralColumns()),
new EmployeeBulkUploadSubGroup("Extended Personal Information", ExtendedPersonalColumns()),
new EmployeeBulkUploadSubGroup("Present Address", AddressColumns()),
new EmployeeBulkUploadSubGroup("Permanent Address", AddressColumns()),
new EmployeeBulkUploadSubGroup("Contact Details", ContactDetailsColumns()),
new EmployeeBulkUploadSubGroup("Emergency Contact Details", EmergencyContactColumns())
}),
new EmployeeBulkUploadGroup("Children",
"#DCEBD8", "#EDF5EB", "#F6FAF5", "#3D5A36",
new List
{
new EmployeeBulkUploadSubGroup("Children 01", ChildrenColumns(), true),
new EmployeeBulkUploadSubGroup("Children 02", ChildrenColumns(), true)
}),
new EmployeeBulkUploadGroup("Spouse",
"#E6DCEC", "#F1EBF4", "#F8F5FA", "#55415E",
new List
{
new EmployeeBulkUploadSubGroup("Spouse 01", SpouseColumns(), true),
new EmployeeBulkUploadSubGroup("Spouse 02", SpouseColumns(), true)
}),
new EmployeeBulkUploadGroup("Academic / Education",
"#F2E2D3", "#F8EFE7", "#FCF7F3", "#70503A",
new List
{
new EmployeeBulkUploadSubGroup("Education 01", EducationColumns(), true),
new EmployeeBulkUploadSubGroup("Education 02", EducationColumns(), true)
}),
new EmployeeBulkUploadGroup("Nominee",
"#D7EAE5", "#E9F4F1", "#F4FAF8", "#35584F",
new List
{
new EmployeeBulkUploadSubGroup("Nominee 01", NomineeColumns(), true),
new EmployeeBulkUploadSubGroup("Nominee 02", NomineeColumns(), true)
}),
new EmployeeBulkUploadGroup("Experience",
"#F2DEDB", "#F8ECEA", "#FCF6F5", "#6E4340",
new List
{
new EmployeeBulkUploadSubGroup("Experience 01", ExperienceColumns(), true),
new EmployeeBulkUploadSubGroup("Experience 02", ExperienceColumns(), true)
}),
new EmployeeBulkUploadGroup("Training",
"#EDE6C9", "#F6F1DF", "#FBF9F0", "#5E5230",
new List
{
new EmployeeBulkUploadSubGroup("Training 01", TrainingColumns(), true),
new EmployeeBulkUploadSubGroup("Training 02", TrainingColumns(), true),
new EmployeeBulkUploadSubGroup("Training 03", TrainingColumns(), true)
}),
};
foreach (EmployeeBulkUploadSubGroup subGroup in groups.SelectMany(g => g.SubGroups))
{
foreach (EmployeeBulkUploadColumn column in subGroup.Columns)
{
column.Key = ToKey(subGroup.Name) + "_" + ToKey(column.Label);
if (column.DependsOn != null && !subGroup.Columns.Any(x => x.Label == column.DependsOn))
{
throw new InvalidOperationException("Column \"" + column.Label + "\" of \"" + subGroup.Name
+ "\" depends on \"" + column.DependsOn + "\", which that sub-group does not have.");
}
}
}
return groups;
}
/// Letters and digits only, lower case: "Children 01" → "children01".
private static string ToKey(string text)
{
return new string((text ?? string.Empty).Where(char.IsLetterOrDigit).ToArray()).ToLowerInvariant();
}
///
/// A heading as the uploader compares it: no mandatory " *", single spaces, any case.
///
public static string NormalizeHeading(string heading)
{
string text = (heading ?? string.Empty).Trim().TrimEnd('*').Trim();
return string.Join(" ", text.Split((char[])null, StringSplitOptions.RemoveEmptyEntries))
.ToLowerInvariant();
}
///
/// The layout cut down to the chosen sub-groups (by row-2 band name, e.g. "Children 01").
/// Null or empty means everything. General is always kept, and a group left with no
/// sub-group is dropped.
///
public static List GetGroups(IEnumerable subGroupNames)
{
List groups = GetGroups();
if (subGroupNames == null || !subGroupNames.Any())
{
return groups;
}
HashSet chosen = new HashSet(subGroupNames, StringComparer.OrdinalIgnoreCase);
chosen.Add(AlwaysIncludedSubGroup);
foreach (EmployeeBulkUploadGroup group in groups)
{
group.SubGroups = group.SubGroups.Where(s => chosen.Contains(s.Name)).ToList();
}
return groups.Where(g => g.SubGroups.Count > 0).ToList();
}
///
/// A user's section selection as template band names, in template order, always starting
/// with . Names that are not template sections are
/// returned in and left out.
///
public static List ResolveSelection(IEnumerable subGroupNames, out List unknown)
{
List requested = (subGroupNames ?? Enumerable.Empty())
.Where(x => !string.IsNullOrWhiteSpace(x))
.Select(x => x.Trim())
.ToList();
List known = GetSubGroupNames();
unknown = requested
.Where(x => !known.Contains(x, StringComparer.OrdinalIgnoreCase))
.Distinct(StringComparer.OrdinalIgnoreCase)
.ToList();
return known
.Where(x => x == AlwaysIncludedSubGroup || requested.Contains(x, StringComparer.OrdinalIgnoreCase))
.ToList();
}
/// Every row-2 band name, left to right — the values a selection may contain.
public static List GetSubGroupNames()
{
return GetGroups()
.SelectMany(g => g.SubGroups)
.Select(s => s.Name)
.ToList();
}
/// All columns left to right, flattened — the order of the sheet.
public static List GetColumns()
{
return GetColumns(GetGroups());
}
public static List GetColumns(List groups)
{
return groups
.SelectMany(g => g.SubGroups)
.SelectMany(s => s.Columns)
.ToList();
}
public static int TotalColumnCount
{
get { return GetColumns().Count; }
}
#endregion
}
#endregion
#region Upload read result
public enum EnumBulkUploadCheckStatus
{
NotChecked = 0, // did not run: an earlier check or layer it depends on failed
Passed = 1,
Failed = 2
}
///
/// One validation check of an upload. Every check of a layer is listed even when it passes,
/// so the user sees what was checked; a failed one says what is wrong (Message), where
/// (Details — one line per section, column, ...) and how to put it right (Fix).
///
public class EmployeeBulkUploadCheck
{
public EmployeeBulkUploadCheck(string code, string title)
{
Code = code;
Title = title;
Status = EnumBulkUploadCheckStatus.NotChecked;
Details = new List();
}
/// Stable name of the check, e.g. "SelectedInFile".
public string Code { get; set; }
/// What is checked, phrased as the rule, e.g. "Every ticked section is in the file".
public string Title { get; set; }
public EnumBulkUploadCheckStatus Status { get; set; }
public string Message { get; set; }
public List Details { get; set; }
public string Fix { get; set; }
public bool IsPassed
{
get { return Status == EnumBulkUploadCheckStatus.Passed; }
}
public void Pass()
{
Status = EnumBulkUploadCheckStatus.Passed;
}
public void Fail(string message, IEnumerable details, string fix)
{
Status = EnumBulkUploadCheckStatus.Failed;
Message = message;
Details = details == null ? new List() : details.ToList();
Fix = fix;
}
/// Leaves the check not run; says which earlier check stopped it.
public void Skip(string reason)
{
Status = EnumBulkUploadCheckStatus.NotChecked;
Message = reason;
}
}
///
/// A layer of validation. All checks of a layer run (as far as their own prerequisites
/// allow) and are reported together; the next layer runs only when this one has passed.
///
public class EmployeeBulkUploadLayer
{
public EmployeeBulkUploadLayer(int layerNo, string name, string description)
{
LayerNo = layerNo;
Name = name;
Description = description;
Checks = new List();
}
public int LayerNo { get; set; }
public string Name { get; set; }
public string Description { get; set; }
public List Checks { get; set; }
/// Failed if any check failed, Passed if every check passed, otherwise NotChecked.
public EnumBulkUploadCheckStatus Status
{
get
{
if (Checks.Any(x => x.Status == EnumBulkUploadCheckStatus.Failed))
{
return EnumBulkUploadCheckStatus.Failed;
}
return Checks.Count > 0 && Checks.All(x => x.IsPassed)
? EnumBulkUploadCheckStatus.Passed
: EnumBulkUploadCheckStatus.NotChecked;
}
}
public EmployeeBulkUploadCheck AddCheck(string code, string title)
{
EmployeeBulkUploadCheck check = new EmployeeBulkUploadCheck(code, title);
Checks.Add(check);
return check;
}
/// Marks every check still not run with .
public void SkipRemaining(string reason)
{
foreach (EmployeeBulkUploadCheck check in Checks.Where(x => x.Status == EnumBulkUploadCheckStatus.NotChecked))
{
check.Skip(reason);
}
}
}
///
/// One problem in one cell of an employee row — shown in the page's Problems table so the
/// user can go straight to the cell. Severity is "error" or "warning".
///
public class EmployeeBulkUploadIssue
{
public const string Error = "error";
public const string Warning = "warning";
public string Severity { get; set; }
/// The Excel row number.
public int RowNo { get; set; }
public string EmployeeNo { get; set; }
public string EmployeeName { get; set; }
/// Header row 1, e.g. "Basic Information".
public string Group { get; set; }
/// Header row 2, e.g. "Present Address".
public string SubGroup { get; set; }
/// Header row 3 label, e.g. "District".
public string Column { get; set; }
/// The Excel cell, e.g. "AB12" — where to find the value in the file.
public string Cell { get; set; }
/// What the cell held; empty for a missing value.
public string Value { get; set; }
/// What is wrong and how to put it right.
public string Message { get; set; }
}
/// One employee row of the uploaded sheet; only non-empty cells are carried.
public class EmployeeBulkUploadRow
{
public EmployeeBulkUploadRow()
{
Values = new Dictionary();
Parsed = new Dictionary();
MasterIDs = new Dictionary();
}
/// The Excel row number, so a problem can be traced back to the sheet.
public int RowNo { get; set; }
/// Cell text keyed by .
public Dictionary Values { get; set; }
///
/// The typed value of each filled cell (DateTime, decimal, or the text / listed value),
/// set by the data-type layer and used by the save. Server-side only.
///
[Newtonsoft.Json.JsonIgnore]
public Dictionary Parsed { get; set; }
/// The record ID each master-data cell resolved to (exactly or by a close match). Server-side only.
[Newtonsoft.Json.JsonIgnore]
public Dictionary MasterIDs { get; set; }
/// The existing employee this row updates; 0 when the row creates a new employee.
[Newtonsoft.Json.JsonIgnore]
public int EmployeeID { get; set; }
///
/// What the row's Employee No is in the system — : "new" (not in
/// any payroll type: the row creates an employee), "existing" (an active employee of the
/// current payroll type), "otherPayroll", "inactive"; empty when it could not be looked up.
/// The page marks new employees with it.
///
public string EmployeeState { get; set; }
}
/// The values of .
public static class EmployeeStates
{
public const string New = "new";
public const string Existing = "existing";
public const string OtherPayroll = "otherPayroll";
public const string Inactive = "inactive";
}
///
/// A master-data value that is not an exact match but at least
/// % alike to one record, and is
/// taken as that record. Shown to the user, who confirms it before saving.
///
public class EmployeeBulkUploadCloseMatch
{
public EmployeeBulkUploadCloseMatch()
{
UsedIn = new List();
RowNos = new List();
}
public EnumBulkUploadMasterData MasterData { get; set; }
public string MasterName { get; set; }
/// The value as the file has it.
public string Value { get; set; }
/// The record it is taken as.
public string MatchedName { get; set; }
public int MatchedID { get; set; }
/// How alike the two are, 90–99.
public int Percent { get; set; }
/// The parent from the same block (District of a Thana, ...), as the file has it; null when none.
public string ParentValue { get; set; }
///
/// The parent record that value resolved to (exactly or closely); 0 when there is no parent
/// in the file. Used when the user adds the file's value as a new record instead.
///
public int ParentID { get; set; }
public EnumBulkUploadMasterData ParentMasterData { get; set; }
public string ParentMasterName { get; set; }
/// Institution only: the type its uses suggest — 1 Academic, 2 Institutional, 3 both.
public int SuggestedInstituteType { get; set; }
public List UsedIn { get; set; }
public List RowNos { get; set; }
}
/// What a save did — or, when nothing was saved, why.
public class EmployeeBulkUploadSaveResult
{
public EmployeeBulkUploadSaveResult()
{
Lines = new List();
}
public bool Saved { get; set; }
public string Message { get; set; }
public int CreatedCount { get; set; }
public int UpdatedCount { get; set; }
/// One line per employee: what was created or updated.
public List Lines { get; set; }
/// The validation the save ran first; set when the file no longer passes it.
public EmployeeBulkUploadReadResult Validation { get; set; }
}
///
/// What was read from an uploaded file: the sections the user declared (Template sections),
/// the validation layers in the order they ran, every problem (cell-level and file-level),
/// and — whenever the file could be read — the sections it holds (in template order) and its
/// employee rows, problems or not.
///
public class EmployeeBulkUploadReadResult
{
public EmployeeBulkUploadReadResult()
{
Groups = new List();
Rows = new List();
Layers = new List();
Issues = new List();
MissingMasterData = new List();
CloseMatches = new List();
ExactMatches = new List();
RowLabels = new Dictionary();
SheetColumns = new Dictionary();
SelectedSubGroups = new List();
ReplacedCells = new List();
}
public string FileName { get; set; }
///
/// The cells whose master-data value was replaced by an existing record chosen in the
/// master-data dialog (see ) — the rows already
/// hold the new value; this says what the file had.
///
public List ReplacedCells { get; set; }
///
/// The sections the user said this upload holds, as resolved band names (Employee first).
/// This — not the file — is what the later steps (validation, save) act on.
///
public List SelectedSubGroups { get; set; }
public List Groups { get; set; }
public List Rows { get; set; }
public List Layers { get; set; }
/// Cell-level problems found by the value layers, in row order.
public List Issues { get; set; }
///
/// How many employee rows the file holds — kept even when a later layer fails and
/// is cleared; 0 when the rows were never read.
///
public int RowCount { get; set; }
/// The first layer that failed; 0 when every layer passed.
public int FailedLayerNo
{
get
{
EmployeeBulkUploadLayer failed = Layers
.FirstOrDefault(x => x.Status == EnumBulkUploadCheckStatus.Failed);
return failed == null ? 0 : failed.LayerNo;
}
}
public EmployeeBulkUploadLayer AddLayer(int layerNo, string name, string description)
{
EmployeeBulkUploadLayer layer = new EmployeeBulkUploadLayer(layerNo, name, description);
Layers.Add(layer);
return layer;
}
///
/// Master-data values the file uses that the system does not have (filled by the master
/// data layer) — one entry per distinct value (and parent), whether or not it can be
/// added from the upload page.
///
public List MissingMasterData { get; set; }
/// Master-data values taken as a close (not exact) match — the user confirms them before saving.
public List CloseMatches { get; set; }
/// Master-data values that match a record exactly (Percent 100) — shown in the master-data dialog.
public List ExactMatches { get; set; }
/// "E001 Rahim" per Excel row — how check details name the employee of a row. Server-side only.
[Newtonsoft.Json.JsonIgnore]
public Dictionary RowLabels { get; set; }
/// Sheet column number of each template column (by column key), for the issues' cell addresses. Server-side only.
[Newtonsoft.Json.JsonIgnore]
public Dictionary SheetColumns { get; set; }
/// Rows whose Employee No is not in the current payroll type yet — saved as new employees.
public int NewEmployeeCount { get; set; }
/// Rows whose Employee No is in the current payroll type — saved as updates.
public int ExistingEmployeeCount { get; set; }
}
#endregion
#region Master data
///
/// What the bulk upload knows about each master table: its display name, the parent it
/// belongs under, and whether a missing value may be added from the upload page.
///
public static class EmployeeBulkUploadMasterInfo
{
/// The order the add dialog lists (and so adds) tables in: parents before children.
public static readonly EnumBulkUploadMasterData[] AddOrder =
{
EnumBulkUploadMasterData.Category,
EnumBulkUploadMasterData.Religion,
EnumBulkUploadMasterData.Nationality,
EnumBulkUploadMasterData.Occupation,
EnumBulkUploadMasterData.PersonRelation,
EnumBulkUploadMasterData.District,
EnumBulkUploadMasterData.Thana,
EnumBulkUploadMasterData.Country,
EnumBulkUploadMasterData.EducationType,
EnumBulkUploadMasterData.EducationLevel,
EnumBulkUploadMasterData.Discipline,
EnumBulkUploadMasterData.Institution,
EnumBulkUploadMasterData.ResultType,
EnumBulkUploadMasterData.NominationPurpose,
EnumBulkUploadMasterData.TrainingType,
EnumBulkUploadMasterData.NatureOfTraining,
EnumBulkUploadMasterData.Grades,
EnumBulkUploadMasterData.Employee
};
public static string DisplayName(EnumBulkUploadMasterData masterData)
{
switch (masterData)
{
case EnumBulkUploadMasterData.Grades: return "Grade";
case EnumBulkUploadMasterData.PersonRelation: return "Relation";
case EnumBulkUploadMasterData.EducationType: return "Education Platform";
case EnumBulkUploadMasterData.EducationLevel: return "Education Level";
case EnumBulkUploadMasterData.ResultType: return "Result Type";
case EnumBulkUploadMasterData.NominationPurpose: return "Nomination Purpose";
case EnumBulkUploadMasterData.TrainingType: return "Training Type";
case EnumBulkUploadMasterData.NatureOfTraining: return "Nature of Training";
case EnumBulkUploadMasterData.Employee: return "Line Manager";
default: return masterData.ToString();
}
}
///
/// The table a record of belongs under — the District of a
/// Thana, the Education Platform of an Education Level, the Education Level of a
/// Discipline, the Division of a District — or None.
///
public static EnumBulkUploadMasterData ParentOf(EnumBulkUploadMasterData masterData)
{
switch (masterData)
{
case EnumBulkUploadMasterData.Thana: return EnumBulkUploadMasterData.District;
case EnumBulkUploadMasterData.EducationLevel: return EnumBulkUploadMasterData.EducationType;
case EnumBulkUploadMasterData.Discipline: return EnumBulkUploadMasterData.EducationLevel;
default: return EnumBulkUploadMasterData.None;
}
}
///
/// Null when a missing value of this table may be added from the upload page; otherwise
/// why not. A Grade carries payroll settings (basic range, basic % of gross, pay scale)
/// that cannot be guessed from a name, and a Line Manager must be a real employee.
///
public static string CannotAddReason(EnumBulkUploadMasterData masterData)
{
switch (masterData)
{
case EnumBulkUploadMasterData.Grades:
return "A Grade carries payroll settings (basic range, basic % of gross) that cannot be taken "
+ "from a name. Create it in the Grade screen, or correct the value in the file.";
case EnumBulkUploadMasterData.Employee:
return "A Line Manager must be an existing, live employee of this payroll type. Correct the "
+ "Employee No in the file.";
default:
return null;
}
}
}
/// A master-data value the file uses that the system does not have.
public class EmployeeBulkUploadMissingMaster
{
public EmployeeBulkUploadMissingMaster()
{
UsedIn = new List();
RowNos = new List();
}
public EnumBulkUploadMasterData MasterData { get; set; }
public string MasterName { get; set; }
/// The value as the file spells it (its first occurrence).
public string Value { get; set; }
/// The parent value from the same row block (e.g. the District of a Thana); null when none.
public string ParentValue { get; set; }
public EnumBulkUploadMasterData ParentMasterData { get; set; }
public string ParentMasterName { get; set; }
/// The parent record's ID when the parent value exists; 0 otherwise.
public int ParentID { get; set; }
/// True when the parent value is itself missing — it has to be added first.
public bool ParentIsMissing { get; set; }
public bool CanAdd { get; set; }
public string CannotAddReason { get; set; }
/// Where the value is used, e.g. "Present Address — District".
public List UsedIn { get; set; }
public List RowNos { get; set; }
/// Institution only: the type its uses suggest — 1 Academic, 2 Institutional, 3 both.
public int SuggestedInstituteType { get; set; }
}
/// A request to add missing values to one master table.
public class EmployeeBulkUploadMasterAddRequest
{
public EmployeeBulkUploadMasterAddRequest()
{
Items = new List();
}
public EnumBulkUploadMasterData MasterData { get; set; }
/// The page's current payroll type — used only when the login token has none.
public int PayrollTypeID { get; set; }
public List Items { get; set; }
}
/// One value to add, with what that table needs besides the name.
public class EmployeeBulkUploadMasterAddItem
{
public string Value { get; set; }
/// Thana / Education Level / Discipline: the parent's name from the file, or …
public string ParentValue { get; set; }
/// … the parent picked in the dialog when the file has none. Also the Division of a District.
public int ParentID { get; set; }
/// Education Level, Institution: true = Board Required, false = Fixed Subject (exactly one applies).
public bool? BoardRequired { get; set; }
/// Institution: 1 Academic, 2 Institutional, 3 Academic and Institutional.
public int InstituteType { get; set; }
/// Result Type: 1 GPA/CGPA, 2 Marks, 3 Pass/Fail.
public int ResultType { get; set; }
}
public class EmployeeBulkUploadMasterAddResult
{
public string Value { get; set; }
public string ParentValue { get; set; }
public bool Saved { get; set; }
public bool AlreadyExisted { get; set; }
public int ID { get; set; }
public string Message { get; set; }
}
public class EmployeeBulkUploadMasterOption
{
public int ID { get; set; }
public string Name { get; set; }
/// The District of a Thana, the Education Platform of an Education Level, ...; 0 when none.
public int ParentID { get; set; }
}
///
/// "Use this existing record instead": a master-data value of the file (not found, or only a
/// partial match) is replaced, in every cell of that table that holds it, by the record the
/// user picked in the master-data dialog. The file itself is not changed — the replacements
/// travel with every read and with the save, so both see the same values.
///
public class EmployeeBulkUploadReplacement
{
public EnumBulkUploadMasterData MasterData { get; set; }
/// The value as the file holds it (matched ignoring case and extra spaces).
public string Value { get; set; }
///
/// For a table matched within its parent (Thana, Education Level): the parent value of the
/// same block, so only the cells under that parent are replaced. Null for any other table.
///
public string ParentValue { get; set; }
/// The existing record to use instead.
public int ReplacementID { get; set; }
}
/// One cell whose value was replaced by an .
public class EmployeeBulkUploadReplacedCell
{
public int RowNo { get; set; }
public string ColumnKey { get; set; }
public string SubGroup { get; set; }
public string Column { get; set; }
public EnumBulkUploadMasterData MasterData { get; set; }
/// What the file had.
public string From { get; set; }
/// The chosen record's name, now in the row.
public string To { get; set; }
}
#endregion
#region IEmployeeBulkUploadService
public interface IEmployeeBulkUploadService
{
/// The empty Employee Bulk Upload workbook, as .xlsx bytes.
byte[] GetTemplate();
///
/// The workbook holding only the chosen sub-groups (row-2 band names); null or empty
/// means all of them. Employee (Employee No, Employee Name) is always included.
///
byte[] GetTemplate(List subGroupNames);
///
/// Validates an uploaded workbook layer by layer and, when every layer passes, returns
/// its sections and employee rows. may be null (no file
/// received); is the Template sections selection
/// as posted, unresolved. Every validation problem is reported in the result's layers —
/// only an unexpected failure throws.
///
EmployeeBulkUploadReadResult ReadUpload(Stream stream, string fileName, long fileLength,
List requestedSubGroups, int payrollTypeID, List replacements = null);
///
/// Adds missing values to ONE master table, one record at a time through that table's own
/// service Save — each record in its own transaction, so one failure does not undo the
/// others. A value that exists by now is reported, not added again.
///
List AddMasterData(EmployeeBulkUploadMasterAddRequest request,
int payrollTypeID, int userID);
/// The active records of a master table, for the add dialog's pickers.
List GetMasterOptions(EnumBulkUploadMasterData masterData, int payrollTypeID);
///
/// Validates the file again (every layer) and, when it passes, saves every row in ONE
/// transaction — all rows or none. A row whose Employee No is in the current payroll
/// type updates that employee (only the cells the file fills); any other row creates a
/// new employee. Repeat sections add records, or update the matching one. Close matches
/// must have been confirmed ().
///
EmployeeBulkUploadSaveResult SaveUpload(Stream stream, string fileName, long fileLength,
List requestedSubGroups, int payrollTypeID, int userID, bool closeMatchesConfirmed,
List replacements = null);
}
#endregion
}