Exemplo n.º 1
0
        public override async Task <int> CreateModel(MaterialDistributionNote Model)
        {
            int Created = 0;

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

                    Created = await this.CreateAsync(Model);

                    IMaterialRequestNoteService materialsRequestNoteService = ServiceProvider.GetService <IMaterialRequestNoteService>();

                    List <InventorySummaryViewModel> data = new List <InventorySummaryViewModel>();


                    foreach (MaterialDistributionNoteItem materialDistributionNoteItem in Model.MaterialDistributionNoteItems)
                    {
                        //inventory summary data
                        //if (!(Model.Type.ToUpper().Equals("RE - GRADING")))
                        //{
                        //    foreach (MaterialDistributionNoteDetail materialDistributionNoteDetail in materialDistributionNoteItem.MaterialDistributionNoteDetails)
                        //    {
                        //        var Distributed = DbContext.MaterialsRequestNotes.Single(p => p.Id == materialDistributionNoteItem.MaterialRequestNoteId);
                        //        //InventorySummaryViewModel InventorySummary = new InventorySummaryViewModel();
                        //        //InventorySummary.quantity = Distributed.IsDistributed ? materialDistributionNoteDetail.ReceivedLength * -1 : materialDistributionNoteDetail.ReceivedLength;
                        //        //InventorySummary.productCode = materialDistributionNoteDetail.ProductCode;
                        //        //InventorySummary.productId = materialDistributionNoteDetail.ProductId;
                        //        //InventorySummary.productName = materialDistributionNoteDetail.ProductName;
                        //        //InventorySummary.storageName = Model.UnitName;
                        //        //InventorySummary.uom = "MTR";

                        //        materialDistributionNoteDetail.ReceivedLength = Distributed.IsDistributed ? materialDistributionNoteDetail.ReceivedLength * -1 : materialDistributionNoteDetail.MaterialRequestNoteItemLength - materialDistributionNoteDetail.ReceivedLength;

                        //        data.Add(InventorySummary);
                        //    }
                        //}

                        MaterialsRequestNote materialsRequestNote = await materialsRequestNoteService.ReadByIdAsync(materialDistributionNoteItem.MaterialRequestNoteId);

                        materialsRequestNote.IsDistributed = true;

                        if (Model.Type.ToUpper().Equals("PRODUKSI"))
                        {
                            foreach (MaterialDistributionNoteDetail materialDistributionNoteDetail in materialDistributionNoteItem.MaterialDistributionNoteDetails)
                            {
                                materialsRequestNote.MaterialsRequestNote_Items.Where(w => w.ProductionOrderId.Equals(materialDistributionNoteDetail.ProductionOrderId)).Select(s => { s.DistributedLength += materialDistributionNoteDetail.ReceivedLength; return(s); }).ToList();
                            }
                            materialsRequestNoteService.UpdateDistributedQuantity(materialsRequestNote.Id, materialsRequestNote);

                            //foreach (MaterialsRequestNote_Item materialRequestNoteItem in materialsRequestNote.MaterialsRequestNote_Items)
                            //{
                            //    materialsRequestNote_ItemService.OnUpdating(materialRequestNoteItem.Id, materialRequestNoteItem);
                            //    materialsRequestNote_ItemService.DbSet.Update(materialRequestNoteItem);
                            //}
                        }
                        await materialsRequestNoteService.UpdateAsync(materialsRequestNote.Id, materialsRequestNote);

                        //materialsRequestNoteService.OnUpdating(materialsRequestNote.Id, materialsRequestNote);
                        //materialsRequestNoteService.DbSet.Update(materialsRequestNote);
                    }

                    DbContext.SaveChanges();

                    CreateInventoryDocument(Model, "OUT");

                    //this.UpdateInventorySummary(data);

                    transaction.Commit();
                }
                catch (ServiceValidationExeption e)
                {
                    transaction.Rollback();
                    throw new ServiceValidationExeption(e.ValidationContext, e.ValidationResults);
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw e;
                }
            }
            return(Created);
        }
Exemplo n.º 2
0
        //public async Task<int> UpdateIsCompleted(int Id, MaterialDistributionNote Model)
        //{
        //    int IsSucceed = 0;

        //    using (var Transaction = this.DbContext.Database.BeginTransaction())
        //    {
        //        try
        //        {
        //            IsSucceed = await UpdateModel(Id, Model);
        //            MaterialDistributionNoteItemService materialDistributionNoteItemService = ServiceProvider.GetService<MaterialDistributionNoteItemService>();
        //            MaterialDistributionNoteDetailService materialDistributionNoteDetailService = ServiceProvider.GetService<MaterialDistributionNoteDetailService>();
        //            MaterialsRequestNoteService materialsRequestNoteService = ServiceProvider.GetService<MaterialsRequestNoteService>();

        //            materialDistributionNoteItemService.Username = this.Username;
        //            materialDistributionNoteDetailService.Username = this.Username;
        //            materialsRequestNoteService.Username = Username;
        //            materialsRequestNoteService.Token = Token;

        //            foreach (MaterialDistributionNoteItem materialDistributionNoteItem in Model.MaterialDistributionNoteItems)
        //            {
        //                MaterialsRequestNote materialsRequestNote = await materialsRequestNoteService.ReadModelById(materialDistributionNoteItem.MaterialRequestNoteId);
        //                await materialDistributionNoteItemService.UpdateModel(materialDistributionNoteItem.Id, materialDistributionNoteItem);

        //                foreach (MaterialDistributionNoteDetail materialDistributionNoteDetail in materialDistributionNoteItem.MaterialDistributionNoteDetails)
        //                {
        //                    await materialDistributionNoteDetailService.UpdateModel(materialDistributionNoteDetail.Id, materialDistributionNoteDetail);
        //                    if (materialDistributionNoteDetail.IsCompleted)
        //                    {
        //                        materialsRequestNote.MaterialsRequestNote_Items.Where(w => w.ProductionOrderId.Equals(materialDistributionNoteDetail.ProductionOrderId)).Select(s => { s.ProductionOrderIsCompleted = true; return s; }).ToList();
        //                    }
        //                }

        //                await materialsRequestNoteService.UpdateIsComplete(materialsRequestNote.Id, materialsRequestNote);

        //            }

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

        //    return IsSucceed;
        //}

        public override async Task <int> DeleteModel(int Id)
        {
            int Count = 0;

            using (var Transaction = this.DbContext.Database.BeginTransaction())
            {
                try
                {
                    MaterialDistributionNote materialDistributionNote = await ReadModelById(Id);

                    Count = this.Delete(Id);

                    MaterialDistributionNoteItemService   materialDistributionNoteItemService   = ServiceProvider.GetService <MaterialDistributionNoteItemService>();
                    MaterialDistributionNoteDetailService materialDistributionNoteDetailService = ServiceProvider.GetService <MaterialDistributionNoteDetailService>();
                    materialDistributionNoteItemService.Username   = this.Username;
                    materialDistributionNoteDetailService.Username = this.Username;

                    HashSet <int> MaterialDistributionNoteItems = new HashSet <int>(this.DbContext.MaterialDistributionNoteItems.Where(p => p.MaterialDistributionNoteId.Equals(Id)).Select(p => p.Id));

                    List <InventorySummaryViewModel> data = new List <InventorySummaryViewModel>();

                    foreach (int item in MaterialDistributionNoteItems)
                    {
                        HashSet <int> MaterialDistributionNoteDetails = new HashSet <int>(this.DbContext.MaterialDistributionNoteDetails.Where(p => p.MaterialDistributionNoteItemId.Equals(item)).Select(p => p.Id));

                        foreach (int detail in MaterialDistributionNoteDetails)
                        {
                            await materialDistributionNoteDetailService.DeleteAsync(detail);
                        }

                        await materialDistributionNoteItemService.DeleteAsync(item);
                    }

                    IMaterialRequestNoteService materialsRequestNoteService = ServiceProvider.GetService <IMaterialRequestNoteService>();
                    //MaterialsRequestNote_ItemService materialsRequestNote_ItemService = ServiceProvider.GetService<MaterialsRequestNote_ItemService>();
                    //materialsRequestNoteService.Username = Username;
                    //materialsRequestNoteService.Token = Token;
                    //materialsRequestNote_ItemService.Username = Username;

                    foreach (MaterialDistributionNoteItem materialDistributionNoteItem in materialDistributionNote.MaterialDistributionNoteItems)
                    {
                        MaterialsRequestNote materialsRequestNote = await materialsRequestNoteService.ReadByIdAsync(materialDistributionNoteItem.MaterialRequestNoteId);

                        materialsRequestNote.IsDistributed = true;

                        //inventory summary data

                        if (materialDistributionNote.Type.ToUpper().Equals("PRODUKSI"))
                        {
                            foreach (MaterialDistributionNoteDetail materialDistributionNoteDetail in materialDistributionNoteItem.MaterialDistributionNoteDetails)
                            {
                                materialsRequestNote.MaterialsRequestNote_Items.Where(w => w.ProductionOrderId.Equals(materialDistributionNoteDetail.ProductionOrderId)).Select(s => { s.DistributedLength -= materialDistributionNoteDetail.ReceivedLength; return(s); }).ToList();
                            }
                            materialsRequestNoteService.UpdateDistributedQuantity(materialsRequestNote.Id, materialsRequestNote);

                            //foreach (MaterialsRequestNote_Item materialRequestNoteItem in materialsRequestNote.MaterialsRequestNote_Items)
                            //{
                            //    materialsRequestNote_ItemService.OnUpdating(materialRequestNoteItem.Id, materialRequestNoteItem);
                            //    materialsRequestNote_ItemService.DbSet.Update(materialRequestNoteItem);
                            //}
                        }
                        await materialsRequestNoteService.UpdateAsync(materialsRequestNote.Id, materialsRequestNote);

                        //materialsRequestNoteService.OnUpdating(materialsRequestNote.Id, materialsRequestNote);
                        //materialsRequestNoteService.DbSet.Update(materialsRequestNote);
                    }

                    DbContext.SaveChanges();

                    CreateInventoryDocument(materialDistributionNote, "IN");
                    Transaction.Commit();
                }
                catch (DbUpdateConcurrencyException)
                {
                    Transaction.Rollback();
                    throw;
                }
            }

            return(Count);
        }
        public async Task <int> CreateAsync(MaterialDistributionNote model)
        {
            int Created = 0;

            using (var transaction = this.DbContext.Database.BeginTransaction())
            {
                try
                {
                    model = await this.CustomCodeGenerator(model);

                    model.FlagForCreate(IdentityService.Username, UserAgent);
                    model.FlagForUpdate(IdentityService.Username, UserAgent);
                    foreach (var item in model.MaterialDistributionNoteItems)
                    {
                        item.FlagForCreate(IdentityService.Username, UserAgent);
                        item.FlagForUpdate(IdentityService.Username, UserAgent);
                        foreach (var detail in item.MaterialDistributionNoteDetails)
                        {
                            detail.FlagForCreate(IdentityService.Username, UserAgent);
                            detail.FlagForUpdate(IdentityService.Username, UserAgent);
                        }
                    }

                    DbSet.Add(model);

                    Created = await DbContext.SaveChangesAsync();

                    IMaterialRequestNoteService materialsRequestNoteService = ServiceProvider.GetService <IMaterialRequestNoteService>();

                    List <ViewModels.InventoryViewModel.InventorySummaryViewModel> data = new List <ViewModels.InventoryViewModel.InventorySummaryViewModel>();


                    foreach (MaterialDistributionNoteItem materialDistributionNoteItem in model.MaterialDistributionNoteItems)
                    {
                        MaterialsRequestNote materialsRequestNote = await materialsRequestNoteService.ReadByIdAsync(materialDistributionNoteItem.MaterialRequestNoteId);

                        materialsRequestNote.IsDistributed = true;

                        if (model.Type.ToUpper().Equals("PRODUKSI"))
                        {
                            foreach (MaterialDistributionNoteDetail materialDistributionNoteDetail in materialDistributionNoteItem.MaterialDistributionNoteDetails)
                            {
                                materialsRequestNote.MaterialsRequestNote_Items.Where(w => w.ProductionOrderId.Equals(materialDistributionNoteDetail.ProductionOrderId)).Select(s => { s.DistributedLength += materialDistributionNoteDetail.ReceivedLength; return(s); }).ToList();
                            }
                            materialsRequestNoteService.UpdateDistributedQuantity(materialsRequestNote.Id, materialsRequestNote);
                        }
                        await materialsRequestNoteService.UpdateAsync(materialsRequestNote.Id, materialsRequestNote);
                    }

                    DbContext.SaveChanges();

                    await CreateInventoryDocument(model, "OUT");


                    transaction.Commit();
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw e;
                }
            }
            return(Created);
        }