private void AddSalaryButton_Click(object sender, RoutedEventArgs e) { EmplSalary sal = actualEmployee.EmplSalary.OrderByDescending(s => s.salWriteDate).FirstOrDefault(); EmplSalary newSal = new EmplSalary(); newSal.salWriteDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second); newSal.Employee = actualEmployee; newSal.salEmplID = actualEmployee.emplID; if (sal != null) { newSal.salStartingFrom = sal.salStartingFrom; newSal.salSuperMinimoAssorbibile = sal.salSuperMinimoAssorbibile; newSal.salOvertimeHours = sal.salOvertimeHours; newSal.salOvertimeForfait = sal.salOvertimeForfait; newSal.salNetSalaryMonth = sal.salNetSalaryMonth; newSal.salL68 = sal.salL68; newSal.salL104 = sal.salL104; newSal.salGrossSalaryYear = sal.salGrossSalaryYear; newSal.salGrossSalaryMonthFT = sal.salGrossSalaryYear; newSal.salGrossSalaryMonth = sal.salGrossSalaryMonth; newSal.salEmplID = sal.salEmplID; newSal.salCostYear = sal.salCostYear; newSal.salCostWithBonusYear = sal.salCostWithBonusYear; newSal.salCarPolicyClass = sal.salCarPolicyClass; newSal.salCar = sal.salCar; newSal.salBonus = sal.salBonus; foreach (EmplSalaryFringeBenefit bf in sal.EmplSalaryFringeBenefit) { newSal.EmplSalaryFringeBenefit.Add(new EmplSalaryFringeBenefit { benefitID = bf.benefitID, value = bf.value, baFringeBenefit = bf.baFringeBenefit, salEmplID = bf.salEmplID }); } } foreach (baFringeBenefit f in entities.baFringeBenefit) { if (newSal.EmplSalaryFringeBenefit.Where(x => x.benefitID == f.benefitID).Count() == 0) { newSal.EmplSalaryFringeBenefit.Add(new EmplSalaryFringeBenefit() { benefitID = f.benefitID, value = false, EmplSalary = newSal, baFringeBenefit = f }); } } actualEmployee.EmplSalary.Add(newSal); System.Windows.Data.CollectionViewSource myCollectionViewSource = (System.Windows.Data.CollectionViewSource) this.Resources["employeeEmplSalaryViewSource"]; myCollectionViewSource.View.MoveCurrentToFirst(); }
public static void convertDocument(Employee empl, ref byte[] bDoc, ref string nDoc) { if (nDoc.EndsWith(".dotx") || nDoc.EndsWith(".dot")) { try { // Create word document from Template string basePath = System.IO.Path.GetTempPath() + "\\HR"; if (!Directory.Exists(basePath)) { Directory.CreateDirectory(basePath); } String path = basePath + "\\" + Guid.NewGuid().ToString() + "_" + nDoc; File.WriteAllBytes(path, bDoc); Object oMissing = System.Reflection.Missing.Value; Object oTrue = true; Object oFalse = false; Application oWord = new Application(); oWord.Visible = false; Document oWordDoc = new Document(); Object oTemplatePath = path; Object oTemplatePathOut; if (path.EndsWith(".dot")) { oTemplatePathOut = path + ".doc"; } else { oTemplatePathOut = path + ".docx"; } oWordDoc = oWord.Documents.Add(ref oTemplatePath, ref oMissing, ref oMissing, ref oMissing); foreach (Field myMergeField in oWordDoc.Fields) { Range rngFieldCode = myMergeField.Code; String fieldText = rngFieldCode.Text; if (fieldText.StartsWith(" MERGEFIELD")) { String fieldName = fieldText.Substring(11); fieldName = fieldName.Trim(); EmplSalary eSal = empl.EmplSalary.OrderByDescending(o => o.salStartingFrom).First(); var properties = empl.GetType().GetProperty(fieldName); if (properties == null && eSal != null) { properties = eSal.GetType().GetProperty(fieldName); } if (properties != null) { myMergeField.Select(); object val = properties.GetValue(empl); if (val == null) { oWord.Selection.TypeText(""); } else { if (properties.PropertyType == typeof(DateTime)) { oWord.Selection.TypeText(((DateTime)val).ToString("dd/MM/yyyy")); } else { oWord.Selection.TypeText(properties.GetValue(empl).ToString()); } } } } } object saveChanges = WdSaveOptions.wdDoNotSaveChanges; oWordDoc.SaveAs2(ref oTemplatePathOut, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing); oWordDoc.Close(ref saveChanges, ref oMissing, ref oMissing); oWord.Quit(ref oMissing, ref oMissing, ref oMissing); bDoc = File.ReadAllBytes(oTemplatePathOut.ToString()); if (nDoc.EndsWith(".dot")) { nDoc = nDoc.Substring(0, nDoc.Length - 3) + "doc"; } if (nDoc.EndsWith(".dotx")) { nDoc = nDoc.Substring(0, nDoc.Length - 4) + "docx"; } } catch (Exception) { int a = 0; } } }