Exemple #1
0
        public override async Task <int> UpdateModel(int Id, MaterialsRequestNote Model)
        {
            MaterialsRequestNote_ItemService materialsRequestNote_ItemService = this.ServiceProvider.GetService <MaterialsRequestNote_ItemService>();

            materialsRequestNote_ItemService.Username = this.Username;

            int Updated = 0;

            using (var transaction = this.DbContext.Database.BeginTransaction())
            {
                try
                {
                    HashSet <int> materialsRequestNote_Items = new HashSet <int>(materialsRequestNote_ItemService.DbSet
                                                                                 .Where(w => w.MaterialsRequestNoteId.Equals(Id))
                                                                                 .Select(s => s.Id));
                    Updated = await this.UpdateAsync(Id, Model);


                    foreach (int materialsRequestNote_Item in materialsRequestNote_Items)
                    {
                        MaterialsRequestNote_Item model = Model.MaterialsRequestNote_Items.FirstOrDefault(prop => prop.Id.Equals(materialsRequestNote_Item));
                        if (model == null)
                        {
                            await materialsRequestNote_ItemService.DeleteModel(materialsRequestNote_Item);
                        }
                        else
                        {
                            await materialsRequestNote_ItemService.UpdateModel(materialsRequestNote_Item, model);
                        }
                    }

                    foreach (MaterialsRequestNote_Item materialsRequestNote_Item in Model.MaterialsRequestNote_Items)
                    {
                        if (materialsRequestNote_Item.Id.Equals(0))
                        {
                            await materialsRequestNote_ItemService.CreateModel(materialsRequestNote_Item);
                        }
                    }

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

            return(Updated);
        }
        public override async Task <int> UpdateModel(int Id, MaterialsRequestNote Model)
        {
            MaterialsRequestNote_ItemService materialsRequestNote_ItemService = this.ServiceProvider.GetService <MaterialsRequestNote_ItemService>();

            materialsRequestNote_ItemService.Username = this.Username;

            int Updated = 0;

            using (var transaction = this.DbContext.Database.BeginTransaction())
            {
                try
                {
                    var dbModel = await ReadModelById(Id);

                    dbModel.Remark = Model.Remark;
                    Updated        = await UpdateAsync(dbModel.Id, dbModel);

                    var           deletedDetails            = dbModel.MaterialsRequestNote_Items.Where(x => !Model.MaterialsRequestNote_Items.Any(y => x.Id == y.Id));
                    var           updatedDetails            = dbModel.MaterialsRequestNote_Items.Where(x => Model.MaterialsRequestNote_Items.Any(y => x.Id == y.Id));
                    var           addedDetails              = Model.MaterialsRequestNote_Items.Where(x => !dbModel.MaterialsRequestNote_Items.Any(y => y.Id == x.Id));
                    List <string> deletedProductionOrderIds = new List <string>();
                    List <string> newProductionOrderIds     = new List <string>();
                    foreach (var item in deletedDetails)
                    {
                        Updated += await materialsRequestNote_ItemService.DeleteModel(item.Id);

                        deletedProductionOrderIds.Add(item.ProductionOrderId);
                    }

                    foreach (var item in updatedDetails)
                    {
                        var selectedDetail = Model.MaterialsRequestNote_Items.FirstOrDefault(x => x.Id == item.Id);

                        if (item.ProductionOrderId != selectedDetail.ProductionOrderId)
                        {
                            newProductionOrderIds.Add(selectedDetail.ProductionOrderId);
                            deletedProductionOrderIds.Add(item.ProductionOrderId);
                        }

                        item.ProductionOrderId          = selectedDetail.ProductionOrderId;
                        item.ProductionOrderIsCompleted = selectedDetail.ProductionOrderIsCompleted;
                        item.ProductionOrderNo          = selectedDetail.ProductionOrderNo;
                        item.ProductId         = selectedDetail.ProductId;
                        item.ProductCode       = selectedDetail.ProductCode;
                        item.ProductName       = selectedDetail.ProductName;
                        item.Grade             = selectedDetail.Grade;
                        item.Length            = selectedDetail.Length;
                        item.Remark            = selectedDetail.Remark;
                        item.DistributedLength = selectedDetail.DistributedLength;
                        item.OrderQuantity     = selectedDetail.OrderQuantity;
                        item.OrderTypeCode     = selectedDetail.OrderTypeCode;
                        item.OrderTypeId       = selectedDetail.OrderTypeId;
                        item.OrderTypeName     = selectedDetail.OrderTypeName;

                        Updated += await materialsRequestNote_ItemService.UpdateModel(item.Id, item);
                    }

                    foreach (var item in addedDetails)
                    {
                        item.MaterialsRequestNoteId = Id;
                        Updated += await materialsRequestNote_ItemService.CreateModel(item);

                        newProductionOrderIds.Add(item.ProductionOrderId);
                    }
                    UpdateIsRequestedProductionOrder(deletedProductionOrderIds, "DELETE");
                    UpdateIsRequestedProductionOrder(newProductionOrderIds, "CREATE");

                    //HashSet<object> materialsRequestNote_Items = new HashSet<object>(materialsRequestNote_ItemService.DbSet
                    //    .Where(w => w.MaterialsRequestNoteId.Equals(Id))
                    //    .AsNoTracking());
                    ////.Select(s => new { Id = s.Id, ProductionOrderId = s.ProductionOrderId }));

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


                    //List<MaterialsRequestNote_Item> itemForUpdateInventory = new List<MaterialsRequestNote_Item>();

                    //foreach (dynamic materialsRequestNote_Item in materialsRequestNote_Items)
                    //{
                    //    MaterialsRequestNote_Item model = Model.MaterialsRequestNote_Items.FirstOrDefault<dynamic>(prop => prop.Id.Equals(materialsRequestNote_Item.Id));
                    //    if (model == null)
                    //    {
                    //        await materialsRequestNote_ItemService.DeleteModel(materialsRequestNote_Item.Id);
                    //        productionOrderIds.Add(materialsRequestNote_Item.ProductionOrderId);
                    //        itemForUpdateInventory.Add(materialsRequestNote_Item);
                    //    }
                    //    else
                    //    {
                    //        double length = materialsRequestNote_Item.Length - model.Length;
                    //        materialsRequestNote_Item.Length = length;
                    //        itemForUpdateInventory.Add(materialsRequestNote_Item);
                    //        await materialsRequestNote_ItemService.UpdateModel(materialsRequestNote_Item.Id, model);
                    //    }
                    //}

                    //foreach (MaterialsRequestNote_Item materialsRequestNote_Item in Model.MaterialsRequestNote_Items)
                    //{
                    //    if (materialsRequestNote_Item.Id.Equals(0))
                    //    {
                    //        await materialsRequestNote_ItemService.CreateModel(materialsRequestNote_Item);
                    //        newProductionOrderIds.Add(materialsRequestNote_Item.ProductionOrderId);
                    //        double length = materialsRequestNote_Item.Length * -1;
                    //        materialsRequestNote_Item.Length = length;
                    //        itemForUpdateInventory.Add(materialsRequestNote_Item);
                    //    }
                    //}

                    ////if (Model.RequestType != "PEMBELIAN")
                    ////{
                    ////    Model.MaterialsRequestNote_Items = itemForUpdateInventory;
                    ////    await this.CreateInventoryDocument(Model, "IN");
                    ////}

                    //UpdateIsRequestedProductionOrder(productionOrderIds, "UPDATE");
                    //UpdateIsRequestedProductionOrder(newProductionOrderIds, "CREATE");
                    transaction.Commit();
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                    throw e;
                }
            }

            return(Updated);
        }
Exemple #3
0
        public override async Task <int> UpdateModel(int Id, MaterialsRequestNote Model)
        {
            MaterialsRequestNote_ItemService materialsRequestNote_ItemService = this.ServiceProvider.GetService <MaterialsRequestNote_ItemService>();

            materialsRequestNote_ItemService.Username = this.Username;

            int Updated = 0;

            using (var transaction = this.DbContext.Database.BeginTransaction())
            {
                try
                {
                    HashSet <object> materialsRequestNote_Items = new HashSet <object>(materialsRequestNote_ItemService.DbSet
                                                                                       .Where(w => w.MaterialsRequestNoteId.Equals(Id))
                                                                                       .AsNoTracking());
                    //.Select(s => new { Id = s.Id, ProductionOrderId = s.ProductionOrderId }));

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

                    List <string> productionOrderIds    = new List <string>();
                    List <string> newProductionOrderIds = new List <string>();
                    List <MaterialsRequestNote_Item> itemForUpdateInventory = new List <MaterialsRequestNote_Item>();

                    foreach (dynamic materialsRequestNote_Item in materialsRequestNote_Items)
                    {
                        MaterialsRequestNote_Item model = Model.MaterialsRequestNote_Items.FirstOrDefault <dynamic>(prop => prop.Id.Equals(materialsRequestNote_Item.Id));
                        if (model == null)
                        {
                            await materialsRequestNote_ItemService.DeleteModel(materialsRequestNote_Item.Id);

                            productionOrderIds.Add(materialsRequestNote_Item.ProductionOrderId);
                            itemForUpdateInventory.Add(materialsRequestNote_Item);
                        }
                        else
                        {
                            double length = materialsRequestNote_Item.Length - model.Length;
                            materialsRequestNote_Item.Length = length;
                            itemForUpdateInventory.Add(materialsRequestNote_Item);
                            await materialsRequestNote_ItemService.UpdateModel(materialsRequestNote_Item.Id, model);
                        }
                    }

                    foreach (MaterialsRequestNote_Item materialsRequestNote_Item in Model.MaterialsRequestNote_Items)
                    {
                        if (materialsRequestNote_Item.Id.Equals(0))
                        {
                            await materialsRequestNote_ItemService.CreateModel(materialsRequestNote_Item);

                            newProductionOrderIds.Add(materialsRequestNote_Item.ProductionOrderId);
                            double length = materialsRequestNote_Item.Length * -1;
                            materialsRequestNote_Item.Length = length;
                            itemForUpdateInventory.Add(materialsRequestNote_Item);
                        }
                    }

                    if (Model.RequestType != "PEMBELIAN")
                    {
                        Model.MaterialsRequestNote_Items = itemForUpdateInventory;
                        this.CreateInventoryDocument(Model, "IN");
                    }

                    UpdateIsRequestedProductionOrder(productionOrderIds, "UPDATE");
                    UpdateIsRequestedProductionOrder(newProductionOrderIds, "CREATE");
                    transaction.Commit();
                }
                catch (Exception e)
                {
                    transaction.Rollback();
                }
            }

            return(Updated);
        }