public async Task <int> Update(int id, FormDto form)
        {
            var model = _dbContext
                        .DPPVATBankExpenditureNotes
                        .FirstOrDefault(entity => entity.Id == id);

            model.UpdateData(form.Amount, form.Supplier.Id, form.Supplier.IsImport, form.Supplier.Name, form.BGCheckNo, form.Date.GetValueOrDefault(), form.Currency.Rate);
            EntityExtension.FlagForUpdate(model, _identityService.Username, UserAgent);
            _dbContext.DPPVATBankExpenditureNotes.Update(model);

            var items = _dbContext.DPPVATBankExpenditureNoteItems
                        .Where(entity => entity.DPPVATBankExpenditureNoteId == id)
                        .ToList()
                        .Select(element =>
            {
                EntityExtension.FlagForDelete(element, _identityService.Username, UserAgent);
                return(element);
            })
                        .ToList();

            _dbContext.DPPVATBankExpenditureNoteItems.UpdateRange(items);

            var details = _dbContext.DPPVATBankExpenditureNoteDetails
                          .Where(entity => entity.DPPVATBankExpenditureNoteId == id)
                          .ToList()
                          .Select(element =>
            {
                EntityExtension.FlagForDelete(element, _identityService.Username, UserAgent);
                return(element);
            })
                          .ToList();

            _dbContext.DPPVATBankExpenditureNoteDetails.UpdateRange(details);

            var detailsDo = _dbContext.DPPVATBankExpenditureNoteDetailDos
                            .Where(entity => entity.DPPVATBankExpenditureNoteId == id)
                            .ToList()
                            .Select(element =>
            {
                EntityExtension.FlagForDelete(element, _identityService.Username, UserAgent);
                return(element);
            })
                            .ToList();

            _dbContext.DPPVATBankExpenditureNoteDetailDos.UpdateRange(detailsDo);

            var existingInternalNoteIds = items.Select(element => element.InternalNoteId).ToList();
            var existingInvoiceNoteIds  = details.Select(element => element.InvoiceId).ToList();

            await UpdateInternalNoteInvoiceNoteIsPaid(false, model.Id, model.DocumentNo, existingInternalNoteIds, existingInvoiceNoteIds);

            var formItems = form.Items.Where(item => item.Select);

            if (formItems != null)
            {
                foreach (var formItem in formItems)
                {
                    var item = new DPPVATBankExpenditureNoteItemModel(model.Id, formItem.InternalNote.Id, formItem.InternalNote.DocumentNo, formItem.InternalNote.Date, formItem.InternalNote.DueDate, formItem.InternalNote.Supplier.Id, formItem.InternalNote.Supplier.Name, formItem.InternalNote.Supplier.IsImport, formItem.InternalNote.VATAmount, formItem.InternalNote.IncomeTaxAmount, formItem.InternalNote.DPP, formItem.InternalNote.TotalAmount, formItem.InternalNote.Currency.Id, formItem.InternalNote.Currency.Code, formItem.OutstandingAmount, formItem.InternalNote.Supplier.Code);
                    EntityExtension.FlagForCreate(item, _identityService.Username, UserAgent);
                    _dbContext.DPPVATBankExpenditureNoteItems.Add(item);
                    _dbContext.SaveChanges();

                    var formDetails = formItem.InternalNote.Items.Where(invoiceItem => invoiceItem.SelectInvoice);

                    if (formDetails != null)
                    {
                        foreach (var formDetail in formItem.InternalNote.Items.Where(invoiceItem => invoiceItem.SelectInvoice))
                        {
                            var detailDoJson = JsonConvert.SerializeObject(formDetail.Invoice.DetailDO);
                            var detail       = new DPPVATBankExpenditureNoteDetailModel(model.Id, item.Id, formDetail.Invoice.Id, formDetail.Invoice.DocumentNo, formDetail.Invoice.Date, formDetail.Invoice.ProductNames, formDetail.Invoice.Category.Id, formDetail.Invoice.Category.Name, formDetail.Invoice.Amount, formDetail.Invoice.PaymentMethod, formDetail.Invoice.DeliveryOrdersNo, formDetail.Invoice.PaymentBills, formDetail.Invoice.BillsNo, detailDoJson);
                            EntityExtension.FlagForCreate(detail, _identityService.Username, UserAgent);
                            _dbContext.DPPVATBankExpenditureNoteDetails.Add(detail);
                            _dbContext.SaveChanges();

                            var detailDos = formDetail.Invoice.DetailDO;

                            if (detailDos != null)
                            {
                                foreach (var detailDo in formDetail.Invoice.DetailDO)
                                {
                                    var detailDoDd = new DPPVATBankExpenditureNoteDetailDoModel(detailDo.DONo, detailDo.TotalAmount, detailDo.PaymentBill, detailDo.BillNo, detailDo.DOId, detailDo.CurrencyRate);
                                    EntityExtension.FlagForCreate(detailDoDd, _identityService.Username, UserAgent);
                                    _dbContext.DPPVATBankExpenditureNoteDetailDos.Add(detailDoDd);
                                    _dbContext.SaveChanges();
                                }
                            }
                        }
                    }
                }
            }

            var internalNoteIds = form.Items.Where(element => element.Select).Select(element => element.InternalNote.Id).ToList();
            var invoiceNoteIds  = form.Items.Where(element => element.Select).SelectMany(element => element.InternalNote.Items).Where(element => element.SelectInvoice).Select(element => element.Invoice.Id).ToList();

            await UpdateInternalNoteInvoiceNoteIsPaid(true, model.Id, model.DocumentNo, internalNoteIds, invoiceNoteIds);

            return(model.Id);
        }