private async Task <BaseResult> Update(InventoryAttribute InventoryAttribute, int updateBy = 0, string updateByUserName = "")
        {
            var result = new BaseResult()
            {
                Result = Result.Success
            };
            var InventoryAttributeForUpdate = _inventoryAttributeRepository.Query().FirstOrDefault(p => p.Id == InventoryAttribute.Id);

            if (InventoryAttributeForUpdate == null || InventoryAttribute.Id <= 0)
            {
                result.Result  = Result.Failed;
                result.Message = "Không tìm thấy sản phẩm yêu cầu!";
                return(result);
            }
            try
            {
                InventoryAttributeForUpdate = InventoryAttribute.ToInventoryAttribute(InventoryAttributeForUpdate);

                //Cập nhật thông tin chung của thực thể
                InventoryAttributeForUpdate = InventoryAttributeForUpdate.UpdateCommonInt(updateBy, updateByUserName);

                await _inventoryAttributeRepository.UpdateAsync(InventoryAttributeForUpdate);
            }
            catch (Exception e)
            {
                result.Result  = Result.SystemError;
                result.Message = e.ToString();
            }
            return(result);
        }
Example #2
0
        public async Task <IHttpActionResult> PostInventoryAttributeWith(InventoryAttribute inventoryAttribute)
        {
            db.InventoryAttributes.Add(inventoryAttribute);
            await db.SaveChangesAsync();

            await db.Entry(inventoryAttribute).GetDatabaseValuesAsync();

            return(Ok(inventoryAttribute));
        }
Example #3
0
        public async Task <IHttpActionResult> PostWorkOrder(InventoryWithAttributes workOrderWithAttributes)
        {
            ICollection <InventoryAttribute> inventoryAttributes = workOrderWithAttributes.Attributes;
            Guid      recipeId = Guid.Parse(inventoryAttributes.FirstOrDefault(x => x.TargetId == Guid.Parse("a8ba7e62-8feb-4285-aca4-ed571de603e2")).Value);
            Inventory recipe   = db.Inventories.Find(recipeId);
            Guid      mainOutput_attributeId = Guid.Parse("1873a981-eed6-4188-97de-30bcc08a3f77");//主產品
            Item      outputItem             = await FindItem(recipe.Id, mainOutput_attributeId);

            List <Inventory> workOrders = new List <Inventory>();

            if (outputItem != null)
            {
                Guid    quantity_attributeId = Guid.Parse("28597e59-ce72-4ff4-8a79-676d3546b13e");//產量(工單)
                decimal?outputNumber         = decimal.Parse(inventoryAttributes.FirstOrDefault(x => x.TargetId == Guid.Parse("28597e59-ce72-4ff4-8a79-676d3546b13e")).Value);
                workOrders = await InsertWorkOrder(outputItem, outputNumber, 1, workOrderWithAttributes.Id);
            }
            decimal?max = 0;

            foreach (Inventory workOrder in workOrders)
            {
                Guid    sort_attributeId = Guid.Parse("e2c306dd-e5b0-46ff-9964-ad36312fb8ac");
                decimal?sort             = decimal.Parse(await SelectAttributeValueByAttributeId(workOrder.Id, sort_attributeId));
                if (sort > max)
                {
                    max = sort;
                }
            }
            max++;
            foreach (Inventory workOrder in workOrders)
            {
                Guid               sort_attributeId = Guid.Parse("e2c306dd-e5b0-46ff-9964-ad36312fb8ac");
                decimal?           newSort          = max - decimal.Parse(await SelectAttributeValueByAttributeId(workOrder.Id, sort_attributeId));
                InventoryAttribute sort             = db.InventoryAttributes.FirstOrDefault(x => x.OwnerId == workOrder.Id && x.TargetId == sort_attributeId);
                sort.Value = newSort.ToString();
                InventoryAttribute existedSort = db.InventoryAttributes.Find(sort.Id);
                db.Entry(existedSort).CurrentValues.SetValues(sort);
            }
            await db.SaveChangesAsync();

            return(Ok(db.Inventories.Where(x => x.Id == workOrderWithAttributes.Id).Select(x => new
            {
                id = x.Id,
                no = x.No,
                value = x.Value - (db.Segmentations.Where(y => y.OwnerId == x.Id).Sum(y => y.Quantity) ?? 0),
                photo = x.Photo != null ? x.Photo.Target.Path : x.Item.Photo.Target.Path,
                itemId = x.ItemId,
                itemValue = x.Item.Value,
                positionId = x.PositionId,
                positionTargetId = x.Position.TargetId,
                positionTargetNo = x.Position.Target.No,
                positionCreatedById = x.Position.CreatedById,
                positionCreatedByNo = x.Position.CreatedBy.No,
                positionPreOwnerId = x.Position.PreOwnerId,
                positionPreOwnerNo = x.Position.PreOwner.No,
                positionStartDate = x.Position.StartDate
            }).FirstOrDefault(x => x.id == workOrderWithAttributes.Id)));
        }
Example #4
0
        public async Task <IHttpActionResult> GetInventoryAttribute(Guid id)
        {
            InventoryAttribute inventoryAttribute = await db.InventoryAttributes.FindAsync(id);

            if (inventoryAttribute == null)
            {
                return(NotFound());
            }

            return(Ok(inventoryAttribute));
        }
Example #5
0
        public async Task <IHttpActionResult> DeleteInventoryAttribute(Guid id)
        {
            InventoryAttribute inventoryAttribute = await db.InventoryAttributes.FindAsync(id);

            if (inventoryAttribute == null)
            {
                return(NotFound());
            }
            db.InventoryAttributes.Remove(inventoryAttribute);
            await db.SaveChangesAsync();

            return(Ok());
        }
Example #6
0
        private async Task <string> SelectAttributeValueByAttributeId(Guid inventoryId, Guid attributeId)
        {
            InventoryAttribute inventoryAttribute = db.InventoryAttributes.FirstOrDefault(x => x.TargetId == attributeId && x.OwnerId == inventoryId);

            if (inventoryAttribute == null)
            {
                return(null);
            }
            else
            {
                return(inventoryAttribute.Value);
            }
        }
Example #7
0
        private async Task <Item> FindItem(Guid inventoryId, Guid attributeId)
        {
            InventoryAttribute inventoryAttribute = db.InventoryAttributes.FirstOrDefault(x => x.OwnerId == inventoryId && x.TargetId == attributeId);

            if (inventoryAttribute == null)
            {
                return(null);
            }
            else
            {
                Guid id = Guid.Parse(inventoryAttribute.Value);
                return(db.Items.Find(id));
            }
        }
Example #8
0
        private async Task <Inventory> FindRecipe(Guid itemId)
        {
            Guid attributeId_mainOutput = Guid.Parse("1873a981-eed6-4188-97de-30bcc08a3f77");

            InventoryAttribute inventoryAttribute = db.InventoryAttributes.FirstOrDefault(x => x.TargetId == attributeId_mainOutput && x.Value == itemId.ToString());

            if (inventoryAttribute == null)
            {
                return(null);
            }
            else
            {
                return(db.Inventories.Find(inventoryAttribute.OwnerId));
            }
        }
        private async Task <BaseResult> Create(InventoryAttribute InventoryAttribute)
        {
            var result = new BaseResult();

            InventoryAttribute.Name = InventoryAttribute.Name.Trim();
            try
            {
                await _inventoryAttributeRepository.InsertAsync(InventoryAttribute);
            }
            catch (Exception e)
            {
                result.Result  = Result.SystemError;
                result.Message = e.ToString();
            }
            return(result);
        }
Example #10
0
        public async Task <IHttpActionResult> PutAttributeWithInventory(Guid id, InventoryAttribute inventoryAttribute)
        {
            Entities.Attribute attribute = await db.Attributes.FirstOrDefaultAsync(x => x.Value == inventoryAttribute.Target.Value);

            if (attribute == null)
            {
                db.Attributes.Add(inventoryAttribute.Target);
                await db.SaveChangesAsync();

                attribute = await db.Attributes.FindAsync(inventoryAttribute.Target.Id);
            }
            inventoryAttribute.TargetId = attribute.Id;
            InventoryAttribute existedInventoryAttribute = await db.InventoryAttributes.FindAsync(id);

            db.Entry(existedInventoryAttribute).CurrentValues.SetValues(inventoryAttribute);
            await db.SaveChangesAsync();

            return(Ok(existedInventoryAttribute));
        }
Example #11
0
        public async Task <IHttpActionResult> PostRecipe(RecipeOption recipeOption)
        {
            Inventory recipe = new Inventory("520934b7-82ed-457e-992f-1bb0cfd3749f")
            {
                No = recipeOption.Item.Value
            };

            db.Inventories.Add(recipe);
            InventoryAttribute mainOutput = new InventoryAttribute("1873a981-eed6-4188-97de-30bcc08a3f77")
            {
                OwnerId = recipe.Id,
                Value   = recipeOption.Item.Id.ToString()
            };

            db.InventoryAttributes.Add(mainOutput);
            await db.SaveChangesAsync();

            return(Ok(recipe));
        }
Example #12
0
        //產生連鎖工單
        private async Task <List <Inventory> > InsertWorkOrder(Item outputItem, decimal?outputNumber, int sort, Guid workOrderId)
        {
            List <Inventory> workOrders = new List <Inventory>();
            Inventory        recipe     = await FindRecipe(outputItem.Id);

            if (recipe != null)
            {
                Inventory workOrder = new Inventory("a8867dc9-ae34-48e4-841b-bcbfc826f23b")
                {
                    Id    = workOrderId,
                    Value = 1,
                    No    = "生產" + outputNumber.ToString() + "單位的【" + outputItem.Value + "】"
                };
                db.Inventories.Add(workOrder);
                db.SaveChanges();

                //配方
                InventoryAttribute workOrderRecipe = new InventoryAttribute("a8ba7e62-8feb-4285-aca4-ed571de603e2")
                {
                    OwnerId = workOrder.Id,
                    Value   = recipe.Id.ToString()
                };
                db.InventoryAttributes.Add(workOrderRecipe);
                //產量
                InventoryAttribute workOrderQuantity = new InventoryAttribute("28597e59-ce72-4ff4-8a79-676d3546b13e")
                {
                    OwnerId = workOrder.Id,
                    Value   = outputNumber.ToString()
                };
                db.InventoryAttributes.Add(workOrderQuantity);

                //排序
                InventoryAttribute workOrderSort = new InventoryAttribute("e2c306dd-e5b0-46ff-9964-ad36312fb8ac")
                {
                    OwnerId = workOrder.Id,
                    Value   = sort.ToString()
                };

                db.InventoryAttributes.Add(workOrderSort);
                db.SaveChanges();
                workOrders.Add(workOrder);
                Guid inputItem_attributeId = Guid.Parse("10879135-3f44-496c-931e-3f56af51c771");
                Guid quantity2_attributeId = Guid.Parse("788cc8d4-fa80-471e-996f-3f33827bfa05");//數量(輸入 輸出)
                var  list = SelectInputsByRecipeId(recipe.Id.ToString()).ToList();
                //底下的子材料
                foreach (Inventory input in list)
                {
                    Item inputItem = await FindItem(input.Id, inputItem_attributeId);

                    decimal?         inputNumber = decimal.Parse(await SelectAttributeValueByAttributeId(input.Id, quantity2_attributeId));
                    List <Inventory> subOrders   = await InsertWorkOrder(inputItem, inputNumber *outputNumber, sort + 1, Guid.NewGuid());

                    workOrders = workOrders.Concat(subOrders).ToList();
                }
            }
            else
            {
                //無配方 不開工單
                //需開採購單
            }
            return(workOrders);
        }
 public static InventoryAttribute ToInventoryAttribute(this InventoryAttribute entity, InventoryAttribute destination)
 {
     return(entity.MapTo(destination));
 }
 public static InventoryAttributeItem ToItem(this InventoryAttribute entity)
 {
     return(entity.MapTo <InventoryAttribute, InventoryAttributeItem>());
 }
 public static InventoryAttributeModel ToModel(this InventoryAttribute entity)
 {
     return(entity.MapTo <InventoryAttribute, InventoryAttributeModel>());
 }