public FabricSKUIdCodeDto AutoCreateSKU(FabricSKUAutoCreateFormDto form)
        {
            var code          = "";
            var processType   = _dbContext.IPProcessType.FirstOrDefault(entity => entity.ProcessType == form.ProcessType);
            var processTypeId = 0;

            if (processType != null)
            {
                code         += processType.Code;
                processTypeId = processType.Id;
            }
            else
            {
                code         += "00";
                processTypeId = 0;
            }

            var yearCode = CodeConstructionHelper.GetYearCode(DateTime.Now.Year);

            code += yearCode;

            var sppNo = form.ProductionOrderNo.Substring(form.ProductionOrderNo.Length - 4);

            code += sppNo;

            var grade   = _dbContext.IPGrades.FirstOrDefault(entity => entity.Type == form.Grade);
            var gradeId = 0;

            if (grade != null)
            {
                code   += grade.Code;
                gradeId = grade.Id;
            }
            else
            {
                code   += "0";
                gradeId = 0;
            }

            var uom      = _dbContext.IPUnitOfMeasurements.FirstOrDefault(entity => entity.Unit == form.UOM);
            var category = _dbContext.IPCategories.FirstOrDefault(entity => entity.Name == "FABRIC");

            var model            = new ProductSKUModel();
            var productFabricSKU = new FabricProductSKUModel();

            if (uom != null && category != null)
            {
                model = new ProductSKUModel(code, code, uom.Id, category.Id, "");
                _unitOfWork.ProductSKUs.Insert(model);
                _unitOfWork.Commit();

                productFabricSKU = new FabricProductSKUModel(code, model.Id, 0, 0, 0, 0, 0, processTypeId, 0, gradeId, uom.Id);
                _unitOfWork.FabricSKUProducts.Insert(productFabricSKU);
                _unitOfWork.Commit();
            }
            return(new FabricSKUIdCodeDto()
            {
                FabricSKUId = productFabricSKU.Id, ProductSKUCode = code, ProductSKUId = model.Id
            });
        }
        public FabricSKUIdCodeDto AutoCreateSKU(FabricSKUAutoCreateFormDto form)
        {
            var code          = "";
            var processType   = _dbContext.IPProcessType.FirstOrDefault(entity => entity.ProcessType == form.ProcessType);
            var processTypeId = 0;

            if (processType != null)
            {
                code         += processType.Code;
                processTypeId = processType.Id;
            }
            else
            {
                code         += "00";
                processTypeId = 0;
            }

            var yearCode = CodeConstructionHelper.GetYearCode(DateTime.Now.Year);

            code += yearCode;

            var sppNo = form.ProductionOrderNo.Substring(form.ProductionOrderNo.Length - 4);

            code += sppNo;

            var grade   = _dbContext.IPGrades.FirstOrDefault(entity => entity.Type == form.Grade);
            var gradeId = 0;

            if (grade != null)
            {
                code   += grade.Code;
                gradeId = grade.Id;
            }
            else
            {
                code   += "0";
                gradeId = 0;
            }

            var uom      = _dbContext.IPUnitOfMeasurements.FirstOrDefault(entity => entity.Unit == form.UOM);
            var category = _dbContext.IPCategories.FirstOrDefault(entity => entity.Name == "FABRIC");

            var model            = new ProductSKUModel();
            var productFabricSKU = new FabricProductSKUModel();

            if (uom != null && category != null)
            {
                model = new ProductSKUModel(code, code, uom.Id, category.Id, "");
                _unitOfWork.ProductSKUs.Insert(model);
                _unitOfWork.Commit();

                productFabricSKU = new FabricProductSKUModel(code, model.Id, 0, 0, 0, 0, 0, processTypeId, form.yarnMaterialId, gradeId, uom.Id,
                                                             form.materialId, form.materialName, form.materialConstructionId, form.materialConstructionName,
                                                             form.yarnMaterialId, form.yarnMaterialName, form.ProductionOrderNo, form.uomUnit, form.motif, form.color, form.Grade, form.Width);

                _unitOfWork.FabricSKUProducts.Insert(productFabricSKU);
                _unitOfWork.Commit();
            }
            else
            {
                var errorResult = new List <ValidationResult>()
                {
                    new ValidationResult("Satuan dan Kategori SKU tidak ditemukan periksa data master", new List <string> {
                        "UOMCategory"
                    })
                };
                var validationContext = new ValidationContext(model, _serviceProvider, null);
                throw new ServiceValidationException(validationContext, errorResult);
            }
            return(new FabricSKUIdCodeDto()
            {
                FabricSKUId = productFabricSKU.Id, ProductSKUCode = code, ProductSKUId = model.Id
            });
        }