Exemplo n.º 1
0
        private async void ImportBtn_Click(object sender, EventArgs e)
        {
            var importForm = DIContainer.Resolve <ImportMasterForm>();

            if (importForm.ShowDialog() == DialogResult.OK)
            {
                var deletedContractors    = (await _recycleBinService.GetContractorsAsync(Session.Person.ID)).ToDictionary(_ => _.ContractorID);
                var deletedContractorsIds = new List <long>();

                var importResult = (ImportResult)importForm.Tag;
                foreach (var contractor in Mapper.Map <ContractorViewModel[], ContractorModel[]>(importResult.SuccessContractors))
                {
                    if (deletedContractors.TryGetValue(contractor.ContractorID, out var deletedContractor))
                    {
                        deletedContractorsIds.Add(deletedContractor.ID);
                    }

                    if (contractor.ID > 0)
                    {
                        foreach (var transaction in contractor.Transactions)
                        {
                            transaction.ContractorID = contractor.ID;
                            await _transactionService.AddTransactionAsync(transaction);
                        }
                    }
                    else
                    {
                        if (importResult.OverwriteExistingNames && _contractors.TryGetValue(contractor.ContractorID, out var currentContractor))
                        {
                            currentContractor.Name = contractor.Name;
                            await _contractorService.UpdateContractorAsync(Mapper.Map <ContractorViewModel, ContractorModel>(currentContractor));

                            foreach (var transaction in contractor.Transactions)
                            {
                                transaction.ContractorID = currentContractor.ID;
                                await _transactionService.AddTransactionAsync(transaction);
                            }
                        }
                        else
                        {
                            await _contractorService.AddContractorAsync(contractor);
                        }
                    }
                }

                if (deletedContractorsIds.Count > 0)
                {
                    await _recycleBinService.RemoveContractorsAsync(deletedContractorsIds.ToArray());
                }

                DialogResult = DialogResult.OK;
            }
        }
Exemplo n.º 2
0
        private async Task LoadData()
        {
            var deletedContractors = Mapper.Map <IEnumerable <ContractorModel>, List <DeletedContractorViewModel> >(
                await _recycleBinService.GetContractorsAsync(Session.Person.ID));

            contractorsDGView.DataSource = new SortableBindingList <DeletedContractorViewModel>(deletedContractors.OrderBy(_ => _.Name).ToList());
            contractorsDGView.ClearSelection();

            if (contractorsDGView.DataSource != null)
            {
                contractorsDGView.Columns[nameof(DeletedContractorViewModel.ContractorID)].MinimumWidth = 40;
            }

            SetFormsSizes();
        }