public override async Task <int> CreateModel(TransferShippingOrder Model)
        {
            int Created = 0;

            using (var Transaction = this.DbContext.Database.BeginTransaction())
            {
                try
                {
                    Model.SONo = await this.CustomCodeGenerator(Model);

                    Created = await this.CreateAsync(Model);

                    foreach (var item in Model.TransferShippingOrderItems)
                    {
                        foreach (var detail in item.TransferShippingOrderDetails)
                        {
                            TransferDeliveryOrderDetail transferDeliveryOrderDetail = this.DbContext.TransferDeliveryOrderDetails.FirstOrDefault(s => s.Id == detail.DODetailId);
                            transferDeliveryOrderDetail.ShippingOrderQuantity += (int)detail.DeliveryQuantity;
                            transferDeliveryOrderDetail.RemainingQuantity     -= (int)detail.DeliveryQuantity;

                            TransferRequestDetail transferRequestDetail = this.DbContext.TransferRequestDetails.FirstOrDefault(s => s.Id == detail.TRDetailId);
                            transferRequestDetail.Status             = transferDeliveryOrderDetail.RemainingQuantity > 0 ? "Barang Sudah dikirim sebagian" : "Barang Sudah dikirim semua";
                            transferRequestDetail._LastModifiedUtc   = DateTime.UtcNow;
                            transferRequestDetail._LastModifiedAgent = "Service";
                            transferRequestDetail._LastModifiedBy    = this.Username;

                            InternalTransferOrderDetail internalTransferOrderDetail = this.DbContext.InternalTransferOrderDetails.FirstOrDefault(s => s.Id == detail.ITODetailId);
                            internalTransferOrderDetail.Status             = transferDeliveryOrderDetail.RemainingQuantity > 0 ? "Barang Sudah dikirim sebagian" : "Barang Sudah dikirim semua";
                            internalTransferOrderDetail._LastModifiedUtc   = DateTime.UtcNow;
                            internalTransferOrderDetail._LastModifiedAgent = "Service";
                            internalTransferOrderDetail._LastModifiedBy    = this.Username;
                        }
                    }
                    this.DbContext.SaveChanges();

                    Transaction.Commit();
                }
                catch (ServiceValidationExeption e)
                {
                    throw new ServiceValidationExeption(e.ValidationContext, e.ValidationResults);
                }
                catch (Exception)
                {
                    Transaction.Rollback();
                }
            }

            return(Created);
        }
Exemplo n.º 2
0
        public override async Task <int> UpdateModel(int Id, TransferDeliveryOrder Model)
        {
            int Updated = 0;

            using (var Transaction = this.DbContext.Database.BeginTransaction())
            {
                try
                {
                    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)
                            );

                        TransferDeliveryOrderItem transferDeliveryOrderItem = Model.TransferDeliveryOrderItem.FirstOrDefault(p => p.Id.Equals(itemId));

                        // cek item apakah dihapus (sesuai data yang diubah)
                        if (transferDeliveryOrderItem == null)
                        {
                            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);

                                    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;
                                    }
                                }
                            }

                            foreach (int detailId in TransferDeliveryOrderDetailIds)
                            {
                                TransferDeliveryOrderDetail transferDeliveryOrderDetail = this.DbContext.TransferDeliveryOrderDetails.FirstOrDefault(p => p.Id.Equals(detailId));

                                await transferDeliveryOrderDetailService.DeleteModel(detailId);
                            }

                            await transferDeliveryOrderItemService.DeleteModel(itemId);
                        }
                        else
                        {
                            await transferDeliveryOrderItemService.UpdateModel(itemId, transferDeliveryOrderItem);

                            foreach (int detailId in TransferDeliveryOrderDetailIds)
                            {
                                TransferDeliveryOrderDetail transferDeliveryOrderDetail = transferDeliveryOrderItem.transferDeliveryOrderDetail.FirstOrDefault(p => p.Id.Equals(detailId));

                                await transferDeliveryOrderDetailService.UpdateModel(detailId, transferDeliveryOrderDetail);
                            }

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

                            foreach (var detail in transferDeliveryOrderItem.transferDeliveryOrderDetail)
                            {
                                ExternalTransferOrderDetail externalTransferOrderDetail = this.DbContext.ExternalTransferOrderDetails.FirstOrDefault(s => s.Id == detail.ETODetailId);
                                if (item != null)
                                {
                                    externalTransferOrderDetail.DOQuantity        = (externalTransferOrderDetail.DOQuantity) - (detail.RemainingQuantity) + (detail.DOQuantity);
                                    externalTransferOrderDetail.RemainingQuantity = (externalTransferOrderDetail.RemainingQuantity) + (detail.RemainingQuantity) - (detail.DOQuantity);
                                }
                                else
                                {
                                    externalTransferOrderDetail.DOQuantity        = (externalTransferOrderDetail.DOQuantity) + (detail.DOQuantity);
                                    externalTransferOrderDetail.RemainingQuantity = (externalTransferOrderDetail.RemainingQuantity) - (detail.DOQuantity);
                                }

                                externalTransferOrderDetail._LastModifiedUtc   = DateTime.UtcNow;
                                externalTransferOrderDetail._LastModifiedAgent = "Service";
                                externalTransferOrderDetail._LastModifiedBy    = this.Username;
                                detail.RemainingQuantity = detail.DOQuantity;

                                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;
                                }
                            }
                        }
                    }

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

                    this.DbContext.SaveChanges();

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

            return(Updated);
        }
Exemplo n.º 3
0
        public TransferDeliveryOrder MapToModel(TransferDeliveryOrderViewModel viewModel)
        {
            TransferDeliveryOrder model = new TransferDeliveryOrder();

            PropertyCopier <TransferDeliveryOrderViewModel, TransferDeliveryOrder> .Copy(viewModel, model);

            model.DONo              = viewModel.DONo;
            model.DOdate            = (DateTime)viewModel.DODate;
            model.SupplierId        = viewModel.Supplier._id ?? "";
            model.SupplierCode      = viewModel.Supplier.code;
            model.SupplierName      = viewModel.Supplier.name;
            model.OrderDivisionId   = viewModel.Division._id;
            model.OrderDivisionCode = viewModel.Division.code;
            model.OrderDivisionName = viewModel.Division.name;
            model.Remark            = viewModel.Remark;
            model.IsPosted          = viewModel.IsPosted;

            model.TransferDeliveryOrderItem = new List <TransferDeliveryOrderItem>();

            foreach (TransferDeliveryOrderItemViewModel transferDeliveryOrderItemViewModel in viewModel.items)
            {
                TransferDeliveryOrderItem transferDeliveryOrderItem = new TransferDeliveryOrderItem();

                PropertyCopier <TransferDeliveryOrderItemViewModel, TransferDeliveryOrderItem> .Copy(transferDeliveryOrderItemViewModel, transferDeliveryOrderItem);

                transferDeliveryOrderItem.DOId  = transferDeliveryOrderItemViewModel.DOId;
                transferDeliveryOrderItem.ETOId = transferDeliveryOrderItemViewModel.ETOId;
                transferDeliveryOrderItem.ETONo = transferDeliveryOrderItemViewModel.ETONo;
                transferDeliveryOrderItem.ITOId = transferDeliveryOrderItemViewModel.ITOId;
                transferDeliveryOrderItem.ITONo = transferDeliveryOrderItemViewModel.ITONo;

                transferDeliveryOrderItem.transferDeliveryOrderDetail = new List <TransferDeliveryOrderDetail>();
                foreach (TransferDeliveryOrderDetailViewModel transferDeliveryOrderDetailViewModel in transferDeliveryOrderItemViewModel.details)
                {
                    TransferDeliveryOrderDetail transferDeliveryOrderDetail = new TransferDeliveryOrderDetail();
                    PropertyCopier <TransferDeliveryOrderDetailViewModel, TransferDeliveryOrderDetail> .Copy(transferDeliveryOrderDetailViewModel, transferDeliveryOrderDetail);

                    transferDeliveryOrderDetail.DOItemId              = transferDeliveryOrderDetailViewModel.DOItemId;
                    transferDeliveryOrderDetail.ETODetailId           = transferDeliveryOrderDetailViewModel.ETODetailId;
                    transferDeliveryOrderDetail.ITODetailId           = transferDeliveryOrderDetailViewModel.ITODetailId;
                    transferDeliveryOrderDetail.TRDetailId            = transferDeliveryOrderDetailViewModel.TRDetailId;
                    transferDeliveryOrderDetail.TRId                  = transferDeliveryOrderDetailViewModel.TRId;
                    transferDeliveryOrderDetail.TRNo                  = transferDeliveryOrderDetailViewModel.TRNo;
                    transferDeliveryOrderDetail.UnitId                = transferDeliveryOrderDetailViewModel.UnitId;
                    transferDeliveryOrderDetail.UnitCode              = transferDeliveryOrderDetailViewModel.UnitCode;
                    transferDeliveryOrderDetail.UnitName              = transferDeliveryOrderDetailViewModel.UnitName;
                    transferDeliveryOrderDetail.ProductId             = transferDeliveryOrderDetailViewModel.ProductId;
                    transferDeliveryOrderDetail.ProductCode           = transferDeliveryOrderDetailViewModel.ProductCode;
                    transferDeliveryOrderDetail.ProductName           = transferDeliveryOrderDetailViewModel.ProductName;
                    transferDeliveryOrderDetail.Grade                 = transferDeliveryOrderDetailViewModel.Grade;
                    transferDeliveryOrderDetail.ProductRemark         = transferDeliveryOrderDetailViewModel.ProductRemark;
                    transferDeliveryOrderDetail.RequestedQuantity     = transferDeliveryOrderDetailViewModel.RequestedQuantity;
                    transferDeliveryOrderDetail.UomId                 = transferDeliveryOrderDetailViewModel.UomId;
                    transferDeliveryOrderDetail.UomUnit               = transferDeliveryOrderDetailViewModel.UomUnit;
                    transferDeliveryOrderDetail.DOQuantity            = transferDeliveryOrderDetailViewModel.DOQuantity;
                    transferDeliveryOrderDetail.ShippingOrderQuantity = transferDeliveryOrderDetailViewModel.ShippingOrderQuantity;
                    transferDeliveryOrderDetail.RemainingQuantity     = transferDeliveryOrderDetailViewModel.RemainingQuantity;

                    transferDeliveryOrderItem.transferDeliveryOrderDetail.Add(transferDeliveryOrderDetail);
                }
                model.TransferDeliveryOrderItem.Add(transferDeliveryOrderItem);
            }

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

            using (var Transaction = this.DbContext.Database.BeginTransaction())
            {
                try
                {
                    TransferShippingOrderItemService transferShippingOrderItemService = ServiceProvider.GetService <TransferShippingOrderItemService>();
                    transferShippingOrderItemService.Username = this.Username;
                    TransferShippingOrderDetailService transferShippingOrderDetailService = ServiceProvider.GetService <TransferShippingOrderDetailService>();
                    transferShippingOrderDetailService.Username = this.Username;

                    HashSet <int> TransferShippingOrderItemIds = new HashSet <int>(
                        this.DbContext.TransferShippingOrderItems
                        .Where(p => p.SOId.Equals(Id))
                        .Select(p => p.Id)
                        );

                    foreach (int itemId in TransferShippingOrderItemIds)
                    {
                        HashSet <int> TransferShippingOrderDetailIds = new HashSet <int>(
                            this.DbContext.TransferShippingOrderDetails
                            .Where(p => p.SOItemId.Equals(itemId))
                            .Select(p => p.Id)
                            );

                        foreach (int detailId in TransferShippingOrderDetailIds)
                        {
                            await transferShippingOrderDetailService.DeleteModel(detailId);

                            TransferShippingOrderItem item = this.DbContext.TransferShippingOrderItems
                                                             .Include(d => d.TransferShippingOrderDetails)
                                                             .FirstOrDefault(p => p.Id.Equals(itemId));

                            if (item != null)
                            {
                                foreach (var detail in item.TransferShippingOrderDetails)
                                {
                                    TransferDeliveryOrderDetail transferDeliveryOrderDetail = this.DbContext.TransferDeliveryOrderDetails.FirstOrDefault(s => s.Id == detail.DODetailId);
                                    transferDeliveryOrderDetail.ShippingOrderQuantity -= (int)detail.DeliveryQuantity;
                                    transferDeliveryOrderDetail.RemainingQuantity     += (int)detail.DeliveryQuantity;

                                    TransferRequestDetail transferRequestDetail = this.DbContext.TransferRequestDetails.FirstOrDefault(s => s.Id == detail.TRDetailId);
                                    transferRequestDetail._LastModifiedUtc   = DateTime.UtcNow;
                                    transferRequestDetail._LastModifiedAgent = "Service";
                                    transferRequestDetail._LastModifiedBy    = this.Username;

                                    InternalTransferOrderDetail internalTransferOrderDetail = this.DbContext.InternalTransferOrderDetails.FirstOrDefault(s => s.Id == detail.ITODetailId);
                                    internalTransferOrderDetail._LastModifiedUtc   = DateTime.UtcNow;
                                    internalTransferOrderDetail._LastModifiedAgent = "Service";
                                    internalTransferOrderDetail._LastModifiedBy    = this.Username;

                                    if (transferDeliveryOrderDetail.RemainingQuantity == transferDeliveryOrderDetail.DOQuantity)
                                    {
                                        ExternalTransferOrderDetail externalTransferOrderDetail = this.DbContext.ExternalTransferOrderDetails.FirstOrDefault(s => s.Id == detail.ETODetailId);

                                        transferRequestDetail.Status       = externalTransferOrderDetail.RemainingQuantity > 0 ? "Barang Sudah dikirim sebagian ke Unit Pengirim" : "Barang Sudah dikirim semua ke Unit Pengirim";
                                        internalTransferOrderDetail.Status = externalTransferOrderDetail.RemainingQuantity > 0 ? "Barang Sudah dikirim sebagian ke Unit Pengirim" : "Barang Sudah dikirim semua ke Unit Pengirim";
                                    }
                                    else
                                    {
                                        transferRequestDetail.Status       = transferDeliveryOrderDetail.RemainingQuantity > 0 ? "Barang Sudah dikirim sebagian" : "Barang Sudah dikirim semua";
                                        internalTransferOrderDetail.Status = transferDeliveryOrderDetail.RemainingQuantity > 0 ? "Barang Sudah dikirim sebagian" : "Barang Sudah dikirim semua";
                                    }
                                }
                            }
                        }

                        await transferShippingOrderItemService.DeleteModel(itemId);
                    }

                    Deleted = await this.DeleteAsync(Id);

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

            return(Deleted);
        }