Example #1
0
        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);
        }