public ExternalTransferOrder MapToModel(ExternalTransferOrderViewModel viewModel)
        {
            ExternalTransferOrder model = new ExternalTransferOrder();

            PropertyCopier <ExternalTransferOrderViewModel, ExternalTransferOrder> .Copy(viewModel, model);

            model.OrderDivisionId      = viewModel.OrderDivision._id;
            model.OrderDivisionCode    = viewModel.OrderDivision.code;
            model.OrderDivisionName    = viewModel.OrderDivision.name;
            model.DeliveryDivisionId   = viewModel.DeliveryDivision._id;
            model.DeliveryDivisionCode = viewModel.DeliveryDivision.code;
            model.DeliveryDivisionName = viewModel.DeliveryDivision.name;
            model.CurrencyId           = viewModel.Currency._id;
            model.CurrencyCode         = viewModel.Currency.code;
            model.CurrencyRate         = viewModel.Currency.rate;
            model.CurrencySymbol       = viewModel.Currency.symbol;
            model.CurrencyDescription  = viewModel.Currency.description;

            model.ExternalTransferOrderItems = new List <ExternalTransferOrderItem>();
            foreach (ExternalTransferOrderItemViewModel externalTransferOrderItemViewModel in viewModel.ExternalTransferOrderItems)
            {
                ExternalTransferOrderItem externalTransferOrderItem = new ExternalTransferOrderItem();
                PropertyCopier <ExternalTransferOrderItemViewModel, ExternalTransferOrderItem> .Copy(externalTransferOrderItemViewModel, externalTransferOrderItem);

                externalTransferOrderItem.UnitId   = externalTransferOrderItemViewModel.Unit._id;
                externalTransferOrderItem.UnitCode = externalTransferOrderItemViewModel.Unit.code;
                externalTransferOrderItem.UnitName = externalTransferOrderItemViewModel.Unit.name;

                externalTransferOrderItem.ExternalTransferOrderDetails = new List <ExternalTransferOrderDetail>();
                foreach (ExternalTransferOrderDetailViewModel externalTransferOrderDetailViewModel in externalTransferOrderItemViewModel.ExternalTransferOrderDetails)
                {
                    ExternalTransferOrderDetail externalTransferOrderDetail = new ExternalTransferOrderDetail();
                    PropertyCopier <ExternalTransferOrderDetailViewModel, ExternalTransferOrderDetail> .Copy(externalTransferOrderDetailViewModel, externalTransferOrderDetail);

                    externalTransferOrderDetail.ProductId      = externalTransferOrderDetailViewModel.Product._id;
                    externalTransferOrderDetail.ProductCode    = externalTransferOrderDetailViewModel.Product.code;
                    externalTransferOrderDetail.ProductName    = externalTransferOrderDetailViewModel.Product.name;
                    externalTransferOrderDetail.DefaultUomId   = externalTransferOrderDetailViewModel.DefaultUom._id;
                    externalTransferOrderDetail.DefaultUomUnit = externalTransferOrderDetailViewModel.DefaultUom.unit;
                    externalTransferOrderDetail.DealUomId      = externalTransferOrderDetailViewModel.DealUom._id;
                    externalTransferOrderDetail.DealUomUnit    = externalTransferOrderDetailViewModel.DealUom.unit;

                    externalTransferOrderItem.ExternalTransferOrderDetails.Add(externalTransferOrderDetail);
                }

                model.ExternalTransferOrderItems.Add(externalTransferOrderItem);
            }

            return(model);
        }
        public override async Task <int> DeleteModel(int Id)
        {
            int Deleted = 0;

            using (var Transaction = this.DbContext.Database.BeginTransaction())
            {
                try
                {
                    //ExternalTransferOrder externalTransferOrder = await this.ReadModelById(Id);
                    ExternalTransferOrderItemService externalTransferOrderItemService = ServiceProvider.GetService <ExternalTransferOrderItemService>();
                    externalTransferOrderItemService.Username = this.Username;
                    ExternalTransferOrderDetailService externalTransferOrderDetailService = ServiceProvider.GetService <ExternalTransferOrderDetailService>();
                    externalTransferOrderDetailService.Username = this.Username;

                    HashSet <int> ExternalTransferOrderItemIds = new HashSet <int>(
                        this.DbContext.ExternalTransferOrderItems
                        .Where(p => p.ETOId.Equals(Id))
                        .Select(p => p.Id)
                        );

                    foreach (int itemId in ExternalTransferOrderItemIds)
                    {
                        HashSet <int> ExternalTransferOrderDetailIds = new HashSet <int>(
                            this.DbContext.ExternalTransferOrderDetails
                            .Where(p => p.ETOItemId.Equals(itemId))
                            .Select(p => p.Id)
                            );

                        foreach (int detailId in ExternalTransferOrderDetailIds)
                        {
                            await externalTransferOrderDetailService.DeleteModel(detailId);

                            ExternalTransferOrderItem item = this.DbContext.ExternalTransferOrderItems
                                                             .Include(d => d.ExternalTransferOrderDetails)
                                                             .FirstOrDefault(p => p.Id.Equals(itemId));

                            if (item != null)
                            {
                                foreach (var detail in item.ExternalTransferOrderDetails)
                                {
                                    TransferRequestDetail transferRequestDetail = this.DbContext.TransferRequestDetails.FirstOrDefault(s => s.Id == detail.TRDetailId);
                                    transferRequestDetail.Status = "Sudah diterima Pembelian";

                                    InternalTransferOrderDetail internalTransferOrderDetail = this.DbContext.InternalTransferOrderDetails.FirstOrDefault(s => s.Id == detail.ITODetailId);
                                    internalTransferOrderDetail.Status = "TO Internal belum diorder";
                                }

                                InternalTransferOrder internalTransferOrder = this.DbContext.InternalTransferOrders.FirstOrDefault(s => s.Id == item.ITOId);
                                internalTransferOrder.IsPost = false;
                            }
                        }

                        await externalTransferOrderItemService.DeleteModel(itemId);
                    }

                    Deleted = await this.DeleteAsync(Id);

                    this.DbContext.SaveChanges();
                    Transaction.Commit();
                }
                catch (Exception)
                {
                    Transaction.Rollback();
                }
            }

            return(Deleted);
        }
        public override async Task <int> UpdateModel(int Id, ExternalTransferOrder Model)
        {
            int Updated = 0;

            using (var Transaction = this.DbContext.Database.BeginTransaction())
            {
                try
                {
                    ExternalTransferOrderItemService externalTransferOrderItemService = ServiceProvider.GetService <ExternalTransferOrderItemService>();
                    externalTransferOrderItemService.Username = this.Username;
                    ExternalTransferOrderDetailService externalTransferOrderDetailService = ServiceProvider.GetService <ExternalTransferOrderDetailService>();
                    externalTransferOrderDetailService.Username = this.Username;

                    HashSet <int> ExternalTransferOrderItemIds = new HashSet <int>(
                        this.DbContext.ExternalTransferOrderItems
                        .Where(p => p.ETOId.Equals(Id))
                        .Select(p => p.Id)
                        );

                    foreach (int itemId in ExternalTransferOrderItemIds)
                    {
                        HashSet <int> ExternalTransferOrderDetailIds = new HashSet <int>(
                            this.DbContext.ExternalTransferOrderDetails
                            .Where(p => p.ETOItemId.Equals(itemId))
                            .Select(p => p.Id)
                            );

                        ExternalTransferOrderItem externalTransferOrderItem = Model.ExternalTransferOrderItems.FirstOrDefault(p => p.Id.Equals(itemId));

                        // cek item apakah dihapus (sesuai data yang diubah)
                        if (externalTransferOrderItem == null)
                        {
                            ExternalTransferOrderItem item = this.DbContext.ExternalTransferOrderItems
                                                             .Include(d => d.ExternalTransferOrderDetails)
                                                             .FirstOrDefault(p => p.Id.Equals(itemId));

                            if (item != null)
                            {
                                foreach (var detail in item.ExternalTransferOrderDetails)
                                {
                                    TransferRequestDetail transferRequestDetail = this.DbContext.TransferRequestDetails.FirstOrDefault(s => s.Id == detail.TRDetailId);
                                    transferRequestDetail.Status = "Sudah diterima Pembelian";

                                    InternalTransferOrderDetail internalTransferOrderDetail = this.DbContext.InternalTransferOrderDetails.FirstOrDefault(s => s.Id == detail.ITODetailId);
                                    internalTransferOrderDetail.Status = "TO Internal belum diorder";
                                }

                                InternalTransferOrder internalTransferOrder = this.DbContext.InternalTransferOrders.FirstOrDefault(s => s.Id == item.ITOId);
                                internalTransferOrder.IsPost = false;
                            }

                            foreach (int detailId in ExternalTransferOrderDetailIds)
                            {
                                ExternalTransferOrderDetail externalTransferOrderDetail = this.DbContext.ExternalTransferOrderDetails.FirstOrDefault(p => p.Id.Equals(detailId));

                                await externalTransferOrderDetailService.DeleteModel(detailId);
                            }

                            await externalTransferOrderItemService.DeleteModel(itemId);
                        }
                        else
                        {
                            await externalTransferOrderItemService.UpdateModel(itemId, externalTransferOrderItem);

                            foreach (int detailId in ExternalTransferOrderDetailIds)
                            {
                                ExternalTransferOrderDetail externalTransferOrderDetail = externalTransferOrderItem.ExternalTransferOrderDetails.FirstOrDefault(p => p.Id.Equals(detailId));

                                await externalTransferOrderDetailService.UpdateModel(detailId, externalTransferOrderDetail);
                            }
                        }
                    }

                    Updated = await this.UpdateAsync(Id, Model);

                    foreach (ExternalTransferOrderItem item in Model.ExternalTransferOrderItems)
                    {
                        if (item.Id == 0)
                        {
                            await externalTransferOrderItemService.CreateModel(item);

                            foreach (var detail in item.ExternalTransferOrderDetails)
                            {
                                TransferRequestDetail transferRequestDetail = this.DbContext.TransferRequestDetails.FirstOrDefault(s => s.Id == detail.TRDetailId);
                                transferRequestDetail.Status = "Sudah dibuat TO Eksternal";

                                InternalTransferOrderDetail internalTransferOrderDetail = this.DbContext.InternalTransferOrderDetails.FirstOrDefault(s => s.Id == detail.ITODetailId);
                                internalTransferOrderDetail.Status = "Sudah dibuat TO Eksternal";
                            }

                            InternalTransferOrder internalTransferOrder = this.DbContext.InternalTransferOrders.FirstOrDefault(s => s.Id == item.ITOId);
                            internalTransferOrder.IsPost = true;
                        }
                    }

                    this.DbContext.SaveChanges();

                    Transaction.Commit();
                }
                catch (Exception)
                {
                    Transaction.Rollback();
                }
            }

            return(Updated);
        }