public ActionResult SaveJournalEntry(Models.ViewModels.Financials.AddJournalEntry model)
        {
            if (model.AddJournalEntryLines.Count < 2)
            {
                return(View(model));
            }
            var journalEntry = new JournalEntryHeader()
            {
                Date        = model.Date,
                Memo        = model.Memo,
                ReferenceNo = model.ReferenceNo,
                CreatedBy   = User.Identity.Name,
                CreatedOn   = DateTime.Now,
                ModifiedBy  = User.Identity.Name,
                ModifiedOn  = DateTime.Now,
            };

            foreach (var line in model.AddJournalEntryLines)
            {
                journalEntry.JournalEntryLines.Add(new JournalEntryLine()
                {
                    AccountId = line.AccountId,
                    DrCr      = line.DrCr,
                    Amount    = line.Amount,
                    Memo      = line.Memo
                });
            }
            _financialService.AddJournalEntry(journalEntry);
            return(RedirectToAction("JournalEntries"));
        }
        public ActionResult SaveJournalEntry(Models.ViewModels.Financials.AddJournalEntry model)
        {
            if (model.AddJournalEntryLines.Count < 2)
            {
                return(View(model));
            }
            var journalEntry = new JournalEntryHeader()
            {
                Date        = model.Date,
                Memo        = model.Memo,
                ReferenceNo = model.ReferenceNo,
                VoucherType = model.JournalVoucherType
            };

            foreach (var line in model.AddJournalEntryLines)
            {
                journalEntry.JournalEntryLines.Add(new JournalEntryLine()
                {
                    AccountId = line.AccountId,
                    DrCr      = line.DrCr,
                    Amount    = line.Amount,
                    Memo      = line.Memo
                });
            }
            _financialService.AddJournalEntry(journalEntry);
            return(RedirectToAction("JournalEntries"));
        }
        public ActionResult AddJournalEntry(AddJournalEntry model)
        {
            JournalEntryLine journal = new JournalEntryLine()
            {
                AccountId     = model.AccountId,
                ReferenceNo   = model.ReferenceNo,
                DrCr          = model.DrCr,
                Date          = model.Date,
                Amount        = model.Amount,
                Memo          = model.Memo,
                SubCategoryId = model.SubCategoryId
            };

            _financialService.AddJournalEntry(journal);

            return(RedirectToAction("JournalEntries"));
        }
        public IActionResult SaveJournalEntry([FromBody] JournalEntry journalEntryDto)
        {
            string[] errors = null;

            try
            {
                if (!ModelState.IsValid)
                {
                    errors = new string[ModelState.ErrorCount];
                    foreach (var val in ModelState.Values)
                    {
                        for (int i = 0; i < ModelState.ErrorCount; i++)
                        {
                            errors[i] = val.Errors[i].ErrorMessage;
                        }
                    }

                    return(new BadRequestObjectResult(errors));
                }

                var anyDuplicate = journalEntryDto.JournalEntryLines.GroupBy(x => x.AccountId).Any(g => g.Count() > 1);
                if (anyDuplicate)
                {
                    throw new Exception("One or more journal entry lines has duplicate account.");
                }

                bool isNew = journalEntryDto.Id == 0;
                Core.Domain.Financials.JournalEntryHeader journalEntry = null;

                if (isNew)
                {
                    journalEntry = new Core.Domain.Financials.JournalEntryHeader();
                }
                else
                {
                    journalEntry = _financialService.GetJournalEntry(journalEntryDto.Id, false);
                }

                journalEntry.Date        = journalEntryDto.JournalDate;
                journalEntry.VoucherType = (Core.Domain.JournalVoucherTypes)journalEntryDto.VoucherType.GetValueOrDefault();
                journalEntry.ReferenceNo = journalEntryDto.ReferenceNo;
                journalEntry.Memo        = journalEntryDto.Memo;

                foreach (var line in journalEntryDto.JournalEntryLines)
                {
                    if (!isNew)
                    {
                        var existingLine = journalEntry.JournalEntryLines
                                           .Where(j => j.Id == line.Id && line.Id != 0)
                                           .FirstOrDefault();

                        if (existingLine != null)
                        {
                            existingLine.AccountId = line.AccountId.GetValueOrDefault();
                            existingLine.DrCr      = (Core.Domain.DrOrCrSide)line.DrCr;
                            existingLine.Amount    = line.Amount.GetValueOrDefault();
                            existingLine.Memo      = line.Memo;
                        }
                        else
                        {
                            var journalLine = new Core.Domain.Financials.JournalEntryLine();
                            journalLine.AccountId = line.AccountId.GetValueOrDefault();
                            journalLine.DrCr      = (Core.Domain.DrOrCrSide)line.DrCr;
                            journalLine.Amount    = line.Amount.GetValueOrDefault();
                            journalLine.Memo      = line.Memo;
                            journalEntry.JournalEntryLines.Add(journalLine);
                        }
                    }
                    else
                    {
                        var journalLine = new Core.Domain.Financials.JournalEntryLine();
                        journalLine.AccountId = line.AccountId.GetValueOrDefault();
                        journalLine.DrCr      = (Core.Domain.DrOrCrSide)line.DrCr;
                        journalLine.Amount    = line.Amount.GetValueOrDefault();
                        journalLine.Memo      = line.Memo;
                        journalEntry.JournalEntryLines.Add(journalLine);
                    }
                }

                if (isNew)
                {
                    _financialService.AddJournalEntry(journalEntry);
                }
                else
                {
                    _financialService.UpdateJournalEntry(journalEntry, false);
                }

                return(new OkObjectResult(Ok()));
            }
            catch (Exception ex)
            {
                errors = new string[1] {
                    ex.InnerException != null ? ex.InnerException.Message : ex.Message
                };
                return(new BadRequestObjectResult(errors));
            }
        }