public OutputInvoiceResponse Update(OutputInvoiceViewModel oi)
        {
            OutputInvoiceResponse response = new OutputInvoiceResponse();

            try
            {
                //List<OutputInvoiceItem> outputInvoiceItems = oi.ConvertToOutputInvoice().InvoiceItems;
                //oi.SumOfBase = outputInvoiceItems.Sum(x => x.Base);
                //oi.SumOfPdvValue = outputInvoiceItems.Sum(x => x.Pdv);
                //oi.SumOfTotalValue = outputInvoiceItems.Sum(x => x.TotalPrice);
                //oi.OutputInvoiceSubItems = null;

                OutputInvoice updatedOutputInvoice = unitOfWork.GetOutputInvoiceRepository().Update(oi.ConvertToOutputInvoice());

                //unitOfWork.GetOutputInvoiceItemRepository().UpdateOutputInvoiceItems(outputInvoiceItems ?? new List<OutputInvoiceItem>(), updatedOutputInvoice);

                unitOfWork.Save();

                response.OutputInvoice = updatedOutputInvoice.ConvertToOutputInvoiceViewModel();
                response.Success       = true;
            }
            catch (Exception ex)
            {
                response.OutputInvoice = new OutputInvoiceViewModel();
                response.Success       = false;
                response.Message       = ex.Message;
            }

            return(response);
        }
        public OutputInvoiceResponse Delete(int id)
        {
            OutputInvoiceResponse response = new OutputInvoiceResponse();

            try
            {
                OutputInvoice deletedOutputInvoice = unitOfWork.GetOutputInvoiceRepository().Delete(id);

                unitOfWork.Save();

                response.OutputInvoice = deletedOutputInvoice.ConvertToOutputInvoiceViewModel();
                response.Success       = true;
            }
            catch (Exception ex)
            {
                response.OutputInvoice = new OutputInvoiceViewModel();
                response.Success       = false;
                response.Message       = ex.Message;
            }

            return(response);
        }
        public OutputInvoiceResponse CancelOutputInvoice(int id)
        {
            OutputInvoiceResponse response = new OutputInvoiceResponse();

            try
            {
                OutputInvoice outputInvoice = unitOfWork.GetOutputInvoiceRepository().CancelOutputInvoice(id);
                if (outputInvoice != null && outputInvoice.Active != false)
                {
                    unitOfWork.Save();
                }

                response.OutputInvoice = outputInvoice.ConvertToOutputInvoiceViewModel();
                response.Success       = true;
            }
            catch (Exception ex)
            {
                response.OutputInvoice = new OutputInvoiceViewModel();
                response.Success       = false;
                response.Message       = ex.Message;
            }

            return(response);
        }
        public OutputInvoiceResponse Create(OutputInvoiceViewModel outputInvoice)
        {
            OutputInvoiceResponse response = new OutputInvoiceResponse();

            try
            {
                //Backup items
                List <OutputInvoiceNoteViewModel> outputInvoiceNotes = outputInvoice
                                                                       .OutputInvoiceNotes?.ToList() ?? new List <OutputInvoiceNoteViewModel>();
                outputInvoice.OutputInvoiceNotes = null;

                List <OutputInvoiceDocumentViewModel> outputInvoiceDocuments = outputInvoice
                                                                               .OutputInvoiceDocuments?.ToList() ?? new List <OutputInvoiceDocumentViewModel>();
                outputInvoice.OutputInvoiceDocuments = null;

                OutputInvoice createdOutputInvoice = unitOfWork.GetOutputInvoiceRepository()
                                                     .Create(outputInvoice.ConvertToOutputInvoice());

                // Update items
                if (outputInvoiceNotes != null && outputInvoiceNotes.Count > 0)
                {
                    foreach (OutputInvoiceNoteViewModel item in outputInvoiceNotes
                             .Where(x => x.ItemStatus == ItemStatus.Added || x.ItemStatus == ItemStatus.Edited)?.ToList() ?? new List <OutputInvoiceNoteViewModel>())
                    {
                        item.OutputInvoice = new OutputInvoiceViewModel()
                        {
                            Id = createdOutputInvoice.Id
                        };
                        item.ItemStatus = ItemStatus.Submited;
                        var createdItem = unitOfWork.GetOutputInvoiceNoteRepository().Create(item.ConvertToOutputInvoiceNote());
                    }

                    foreach (OutputInvoiceNoteViewModel item in outputInvoiceNotes
                             .Where(x => x.ItemStatus == ItemStatus.Deleted)?.ToList() ?? new List <OutputInvoiceNoteViewModel>())
                    {
                        item.OutputInvoice = new OutputInvoiceViewModel()
                        {
                            Id = createdOutputInvoice.Id
                        };
                        unitOfWork.GetOutputInvoiceNoteRepository().Create(item.ConvertToOutputInvoiceNote());

                        unitOfWork.GetOutputInvoiceNoteRepository().Delete(item.Identifier);
                    }
                }

                // Update items
                if (outputInvoiceDocuments != null && outputInvoiceDocuments.Count > 0)
                {
                    foreach (OutputInvoiceDocumentViewModel item in outputInvoiceDocuments
                             .Where(x => x.ItemStatus == ItemStatus.Added || x.ItemStatus == ItemStatus.Edited)?.ToList() ?? new List <OutputInvoiceDocumentViewModel>())
                    {
                        item.OutputInvoice = new OutputInvoiceViewModel()
                        {
                            Id = createdOutputInvoice.Id
                        };
                        item.ItemStatus = ItemStatus.Submited;
                        var createdItem = unitOfWork.GetOutputInvoiceDocumentRepository().Create(item.ConvertToOutputInvoiceDocument());
                    }

                    foreach (OutputInvoiceDocumentViewModel item in outputInvoiceDocuments
                             .Where(x => x.ItemStatus == ItemStatus.Deleted)?.ToList() ?? new List <OutputInvoiceDocumentViewModel>())
                    {
                        item.OutputInvoice = new OutputInvoiceViewModel()
                        {
                            Id = createdOutputInvoice.Id
                        };
                        unitOfWork.GetOutputInvoiceDocumentRepository().Create(item.ConvertToOutputInvoiceDocument());

                        unitOfWork.GetOutputInvoiceDocumentRepository().Delete(item.Identifier);
                    }
                }

                unitOfWork.Save();

                response.OutputInvoice = createdOutputInvoice.ConvertToOutputInvoiceViewModel();
                response.Success       = true;
            }
            catch (Exception ex)
            {
                response.OutputInvoice = new OutputInvoiceViewModel();
                response.Success       = false;
                response.Message       = ex.Message;
            }

            return(response);
        }