コード例 #1
0
        public override void OnCreating(ExternalTransferOrderItem model)
        {
            base.OnCreating(model);

            model._CreatedAgent      = "Service";
            model._CreatedBy         = this.Username;
            model._LastModifiedAgent = "Service";
            model._LastModifiedBy    = this.Username;

            ExternalTransferOrderDetailService externalTransferOrderDetailService = ServiceProvider.GetService <ExternalTransferOrderDetailService>();

            externalTransferOrderDetailService.Username = this.Username;

            foreach (ExternalTransferOrderDetail externalTransferOrderDetail in model.ExternalTransferOrderDetails)
            {
                externalTransferOrderDetailService.OnCreating(externalTransferOrderDetail);
            }
        }
        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);
        }