public override void OnCreating(TransferDeliveryOrderItem model)
        {
            base.OnCreating(model);

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

            TransferDeliveryOrderDetailService transferDeliveryOrderDetailService = ServiceProvider.GetService <TransferDeliveryOrderDetailService>();

            transferDeliveryOrderDetailService.Username = this.Username;

            foreach (TransferDeliveryOrderDetail transferDeliveryOrderDetail in model.transferDeliveryOrderDetail)
            {
                transferDeliveryOrderDetailService.OnCreating(transferDeliveryOrderDetail);
            }
        }
예제 #2
0
        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);
                    TransferDeliveryOrderItemService transferDeliveryOrderItemService = ServiceProvider.GetService <TransferDeliveryOrderItemService>();
                    transferDeliveryOrderItemService.Username = this.Username;
                    TransferDeliveryOrderDetailService transferDeliveryOrderDetailService = ServiceProvider.GetService <TransferDeliveryOrderDetailService>();
                    transferDeliveryOrderDetailService.Username = this.Username;

                    HashSet <int> TransferDeliveryOrderItemIds = new HashSet <int>(
                        this.DbContext.TransferDeliveryOrderItems
                        .Where(p => p.DOId.Equals(Id))
                        .Select(p => p.Id)
                        );

                    foreach (int itemId in TransferDeliveryOrderItemIds)
                    {
                        HashSet <int> TransferDeliveryOrderDetailIds = new HashSet <int>(
                            this.DbContext.TransferDeliveryOrderDetails
                            .Where(p => p.DOItemId.Equals(itemId))
                            .Select(p => p.Id)
                            );

                        foreach (int detailId in TransferDeliveryOrderDetailIds)
                        {
                            await transferDeliveryOrderDetailService.DeleteModel(detailId);

                            TransferDeliveryOrderItem item = this.DbContext.TransferDeliveryOrderItems
                                                             .Include(d => d.transferDeliveryOrderDetail)
                                                             .FirstOrDefault(p => p.Id.Equals(itemId));

                            if (item != null)
                            {
                                foreach (var detail in item.transferDeliveryOrderDetail)
                                {
                                    ExternalTransferOrderDetail externalTransferOrderDetail = this.DbContext.ExternalTransferOrderDetails.FirstOrDefault(s => s.Id == detail.ETODetailId);
                                    externalTransferOrderDetail.DOQuantity         = (externalTransferOrderDetail.DOQuantity) - (detail.DOQuantity);
                                    externalTransferOrderDetail.RemainingQuantity  = (externalTransferOrderDetail.RemainingQuantity) + (detail.DOQuantity);
                                    externalTransferOrderDetail._LastModifiedUtc   = DateTime.UtcNow;
                                    externalTransferOrderDetail._LastModifiedAgent = "Service";
                                    externalTransferOrderDetail._LastModifiedBy    = this.Username;

                                    if (externalTransferOrderDetail.DealQuantity == detail.RemainingQuantity)
                                    {
                                        TransferRequestDetail transRequestDetail = this.DbContext.TransferRequestDetails.FirstOrDefault(s => s.Id == detail.TRDetailId);
                                        transRequestDetail.Status             = "Sudah diorder ke Penjualan";
                                        transRequestDetail._LastModifiedUtc   = DateTime.UtcNow;
                                        transRequestDetail._LastModifiedAgent = "Service";
                                        transRequestDetail._LastModifiedBy    = this.Username;

                                        InternalTransferOrderDetail internTransferOrderDetail = this.DbContext.InternalTransferOrderDetails.FirstOrDefault(s => s.Id == detail.ITODetailId);
                                        transRequestDetail.Status             = "Sudah diorder ke Penjualan";
                                        transRequestDetail._LastModifiedUtc   = DateTime.UtcNow;
                                        transRequestDetail._LastModifiedAgent = "Service";
                                        transRequestDetail._LastModifiedBy    = this.Username;
                                    }
                                    else if (externalTransferOrderDetail.RemainingQuantity <= 0)
                                    {
                                        TransferRequestDetail transRequestDetail = this.DbContext.TransferRequestDetails.FirstOrDefault(s => s.Id == detail.TRDetailId);
                                        transRequestDetail.Status             = "Sudah diorder semua ke Unit Pengirim";
                                        transRequestDetail._LastModifiedUtc   = DateTime.UtcNow;
                                        transRequestDetail._LastModifiedAgent = "Service";
                                        transRequestDetail._LastModifiedBy    = this.Username;

                                        InternalTransferOrderDetail internTransferOrderDetail = this.DbContext.InternalTransferOrderDetails.FirstOrDefault(s => s.Id == detail.ITODetailId);
                                        transRequestDetail.Status             = "Sudah diorder semua ke Unit Pengirim";
                                        transRequestDetail._LastModifiedUtc   = DateTime.UtcNow;
                                        transRequestDetail._LastModifiedAgent = "Service";
                                        transRequestDetail._LastModifiedBy    = this.Username;
                                    }
                                    else if (externalTransferOrderDetail.RemainingQuantity > 0)
                                    {
                                        TransferRequestDetail transRequestDetail = this.DbContext.TransferRequestDetails.FirstOrDefault(s => s.Id == detail.TRDetailId);
                                        transRequestDetail.Status             = "Sudah diorder sebagian ke Unit Pengirim";
                                        transRequestDetail._LastModifiedUtc   = DateTime.UtcNow;
                                        transRequestDetail._LastModifiedAgent = "Service";
                                        transRequestDetail._LastModifiedBy    = this.Username;

                                        InternalTransferOrderDetail internTransferOrderDetail = this.DbContext.InternalTransferOrderDetails.FirstOrDefault(s => s.Id == detail.ITODetailId);
                                        transRequestDetail.Status             = "Sudah diorder sebagian ke Unit Pengirim";
                                        transRequestDetail._LastModifiedUtc   = DateTime.UtcNow;
                                        transRequestDetail._LastModifiedAgent = "Service";
                                        transRequestDetail._LastModifiedBy    = this.Username;
                                    }
                                }
                            }
                        }

                        await transferDeliveryOrderItemService.DeleteModel(itemId);
                    }

                    Deleted = await this.DeleteAsync(Id);

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

            return(Deleted);
        }