public int CreateOrUpdate(ContractWorkPaymentViewModel item) { var data = (ContractWorkPayment)null; if (item.Id > 0) { data = Repository.FirstOrDefault(x => x.Id == item.Id); } data = ToDomain(item, data); Repository.Store(data); Repository.SaveChanges(); return(data.Id); }
private int SaveContract() { ContractModel.Validate(); if (ContractModel.HasErrors) { System.Windows.Forms.MessageBox.Show(@"Пожалуйста, введите все красные цветовые поля", @"Проверка", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(0); } if (ContractModel.IsTreaguare != true && ContractModel.ContractDate == null) { System.Windows.Forms.MessageBox.Show(@"Выберите дату контракта", @"Дата", MessageBoxButtons.OK, MessageBoxIcon.Warning); return(0); } try { var contractViewModel = new ContractViewModel { Id = ContractModel.Id, BranchId = ContractModel.BranchId, CategoryId = ContractModel.CategoryId, ContractNumber = ContractModel.ContractNumber, OrganizationId = ContractModel.Organization.Id, ContractAmount = ContractModel.ContractAmount, ContractDate = ContractModel.ContractDate, ObjectName = ContractModel.ObjectName }; var contractId = _contractService.CreateOrUpdate(contractViewModel); //category if (contractViewModel.Id == 0) { var category = _categoryService.GetById(contractViewModel.CategoryId ?? 0); category.Counter = category.Counter + 1; _categoryService.CreateOrUpdate(category); } //work types var workTypes = new ContractWorkPaymentViewModel[] { #region items new ContractWorkPaymentViewModel { Amount = ContractModel.Amount1, PeriodId = ContractModel.PeriodId1, ContractId = contractId, WorkTypeId = ContractModel.WorkTypeId1 }, new ContractWorkPaymentViewModel { Amount = ContractModel.Amount2, PeriodId = ContractModel.PeriodId2, ContractId = contractId, WorkTypeId = ContractModel.WorkTypeId2 }, new ContractWorkPaymentViewModel { Amount = ContractModel.Amount3, PeriodId = ContractModel.PeriodId3, ContractId = contractId, WorkTypeId = ContractModel.WorkTypeId3 }, new ContractWorkPaymentViewModel { Amount = ContractModel.Amount4, PeriodId = ContractModel.PeriodId4, ContractId = contractId, WorkTypeId = ContractModel.WorkTypeId4 }, new ContractWorkPaymentViewModel { Amount = ContractModel.Amount5, PeriodId = ContractModel.PeriodId5, ContractId = contractId, WorkTypeId = ContractModel.WorkTypeId5 } #endregion }; //work type foreach (var workType in workTypes) { var workTypeItem = _workPaymentService.Get(f => f.ContractId == ContractModel.Id && f.WorkTypeId == workType.WorkTypeId); if (workTypeItem != null) { workType.Id = workTypeItem.Id; } _workPaymentService.CreateOrUpdate(workType); } if (ContractModel.IsTreaguare == true && contractViewModel.Id == 0) { //payment var payment = new PaymentViewModel { Amount = ContractModel.ContractAmount * ContractUIViewModel.TREAGUARE_PERCENT_FOR_AMOUNT, Date = ContractModel.ContractDate, ContractId = contractId }; _paymentService.CreateOrUpdate(payment); } //nakladnoy if (ContractModel.HasNakladnoy == true) { var nakladnoy = new NakladnoyViewModel { ContractId = contractId, Date = ContractModel.ContractDate, Number = ContractModel.ContractNumber }; var nakladnoyItem = _nakladnoyService.Get(f => f.ContractId == ContractModel.Id); if (nakladnoyItem != null) { nakladnoy.Id = nakladnoyItem.Id; } _nakladnoyService.CreateOrUpdate(nakladnoy); } // act invoice if (ContractModel.HasActInvoice == true) { var actInvoice = new ActInvoiceViewModel { ContractId = contractId, Number = ContractModel.ContractNumber, Amount = ContractModel.ContractAmount }; var actInvoiceItem = _actInvoiceService.Get(f => f.ContractId == ContractModel.Id); if (actInvoiceItem != null) { actInvoice.Id = actInvoiceItem.Id; } _actInvoiceService.CreateOrUpdate(actInvoice); } System.Windows.Forms.MessageBox.Show(@"Успешно сохранено", @"Сохранить", MessageBoxButtons.OK, MessageBoxIcon.Information); Grid1.DataContext = new ContractUIViewModel(); DialogResult = true; return(contractId); } catch (Exception exception) { System.Windows.Forms.MessageBox.Show(exception.Message, @"Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error); } return(0); }
public ImportStatus DoImportModel(ImportModel model) { ImportStatus status; try { /* * Service */ var organizationService = DiConfig.Resolve <IOrganizationService>(); var contractService = DiConfig.Resolve <IContractService>(); var contractPaymentService = DiConfig.Resolve <IContractWorkPaymentService>(); var workTypeService = DiConfig.Resolve <IWorkTypeService>(); var paymentService = DiConfig.Resolve <IPaymentService>(); var contractDepartmentService = DiConfig.Resolve <IContractDepartmentService>(); var departmentService = DiConfig.Resolve <IDepartmentService>(); var actInvoiceService = DiConfig.Resolve <IActInvoiceService>(); var nakladService = DiConfig.Resolve <INakladnoyService>(); var categoryService = DiConfig.Resolve <ICategoryService>(); var branchService = DiConfig.Resolve <IBranchService>(); /* * Data */ var workItems = new Dictionary <WorkTypeCode, double?> { { WorkTypeCode.Col13, model.V13 }, { WorkTypeCode.Col14, model.V14 }, { WorkTypeCode.Col15, model.V15 }, { WorkTypeCode.Col16, model.V16 }, { WorkTypeCode.Col17, model.V17 }, { WorkTypeCode.Col18, model.V18 }, { WorkTypeCode.Col19, model.V19 }, { WorkTypeCode.Col20, model.V20 }, { WorkTypeCode.Col21, model.V21 }, { WorkTypeCode.Col22, model.V22 }, { WorkTypeCode.Col23, model.V23 }, { WorkTypeCode.Col24, model.V24 } }; var departmentItems = new Dictionary <DepartmentCode, DateTime?> { { DepartmentCode.Col27, model.V27 }, { DepartmentCode.Col28, model.V28 }, { DepartmentCode.Col29, model.V29 }, { DepartmentCode.Col30, model.V30 }, { DepartmentCode.Col31, model.V31 } }; /* * Model */ var organizationModel = organizationService.Get(viewModel => viewModel.Inn.Equals(model.V4, StringComparison.InvariantCultureIgnoreCase)); /* * Organization */ if (organizationModel == null) { organizationModel = new OrganizationViewModel { Inn = model.V4, Name = model.V2, BankName1 = model.V9, AccountNumber = model.V5, Mfo1 = model.V6, Okohx = model.V7, PhoneNumbers = model.V8 }; organizationModel.Id = organizationService.CreateOrUpdate(organizationModel); } else { organizationModel.Name = model.V2; organizationModel.BankName1 = model.V9; organizationModel.AccountNumber = model.V5; organizationModel.Mfo1 = model.V6; organizationModel.Okohx = model.V7; organizationModel.PhoneNumbers = model.V8; organizationService.CreateOrUpdate(organizationModel); } /* * Contract */ var contractModel = contractService.Get(contract => contract.OrganizationId == organizationModel.Id && contract.ContractNumber == model.V10); var categoryModel = categoryService.Get(a => a.Code == model.Category); var branchModel = branchService.Get(a => a.Code == model.Branch); if (contractModel == null) { #region Create contractModel = new ContractViewModel { ContractAmount = model.V12, ContractDate = model.V11, ContractNumber = model.V10, ObjectName = model.V3, DeadlinesContract = model.V32, DeadlinesFact = model.V33, Limitation = model.V38, CategoryId = categoryModel.Id, BranchId = branchModel.Id, OrganizationId = organizationModel.Id }; contractModel.Id = contractService.CreateOrUpdate(contractModel); /* * Contract & Payment */ foreach (var workItem in workItems) { var workName = Enum.GetName(typeof(WorkTypeCode), workItem.Key); var workType = workTypeService.Get(type => type.Name.Equals(workName, StringComparison.InvariantCultureIgnoreCase)); if (workType == null) { continue; } var contractPaymentModel = new ContractWorkPaymentViewModel { Amount = workItem.Value, ContractId = contractModel.Id, WorkTypeId = workType.Id }; contractPaymentService.CreateOrUpdate(contractPaymentModel); } /* * Payment */ var peymentModel = new PaymentViewModel { ContractId = contractModel.Id, Amount = model.V26, Date = model.V25 }; paymentService.CreateOrUpdate(peymentModel); /* * Contract & Department */ foreach (var departmentItem in departmentItems) { var departmentName = Enum.GetName(typeof(DepartmentCode), departmentItem.Key); var department = departmentService.Get(department1 => department1.Name.Equals(departmentName, StringComparison.InvariantCultureIgnoreCase)); if (department == null) { continue; } var contractDepartmentModel = new ContractDepartmentViewModel { ContractId = contractModel.Id, DepartmentId = department.Id, Date = departmentItem.Value, Code = departmentItem.Key }; contractDepartmentService.CreateOrUpdate(contractDepartmentModel); } /* * Act Invoice */ var actInvoiceModel = new ActInvoiceViewModel { ContractId = contractModel.Id, Number = model.V34 }; actInvoiceService.CreateOrUpdate(actInvoiceModel); /* * Nakladnoy */ var nakladModel = new NakladnoyViewModel { ContractId = contractModel.Id, Date = model.V37, Number = model.V36 }; nakladService.CreateOrUpdate(nakladModel); #endregion status = ImportStatus.Created; } else { #region Update contractModel.ContractAmount = model.V12; contractModel.ContractDate = model.V11; contractModel.ObjectName = model.V3; contractModel.DeadlinesContract = model.V32; contractModel.DeadlinesFact = model.V33; contractModel.Limitation = model.V38; contractModel.CategoryId = categoryModel.Id; contractModel.BranchId = branchModel.Id; contractService.CreateOrUpdate(contractModel); /* * Contract & Payment */ foreach (var workItem in workItems) { var workName = Enum.GetName(typeof(WorkTypeCode), workItem.Key); var workType = workTypeService.Get(type => type.Name.Equals(workName, StringComparison.InvariantCultureIgnoreCase)); if (workType == null) { continue; } var contractPaymentModel = contractPaymentService.Get(g => g.WorkTypeId == workType.Id && g.ContractId == contractModel.Id); if (contractPaymentModel == null) { continue; } contractPaymentModel.Amount = workItem.Value; contractPaymentService.CreateOrUpdate(contractPaymentModel); } /* * Contract & Department */ foreach (var departmentItem in departmentItems) { var departmentName = Enum.GetName(typeof(DepartmentCode), departmentItem.Key); var department = departmentService.Get(department1 => department1.Name.Equals(departmentName, StringComparison.InvariantCultureIgnoreCase)); if (department == null) { continue; } var contractDepartmentModel = contractDepartmentService.Get(g => g.ContractId == contractModel.Id && g.DepartmentId == department.Id); if (contractDepartmentModel == null) { continue; } contractDepartmentModel.Date = departmentItem.Value; contractDepartmentModel.Code = departmentItem.Key; contractDepartmentService.CreateOrUpdate(contractDepartmentModel); } /* * Act Invoice */ var actInvoiceModel = actInvoiceService.Get(invoice => invoice.ContractId == contractModel.Id && invoice.Number.Equals(model.V34, StringComparison.InvariantCultureIgnoreCase)); if (actInvoiceModel != null) { actInvoiceService.CreateOrUpdate(actInvoiceModel); } /* * Nakladnoy */ var nakladModel = nakladService.Get(nakladnoy => nakladnoy.ContractId == contractModel.Id && nakladnoy.Number.Equals(model.V36, StringComparison.InvariantCultureIgnoreCase)); if (nakladModel != null) { nakladModel.Date = model.V37; nakladService.CreateOrUpdate(nakladModel); } #endregion status = ImportStatus.Updated; } } catch (Exception exp) { Trace.TraceError(exp.Message); status = ImportStatus.Failed; } return(status); }