public int CreateSKU(FabricSKUFormDto form)
        {
            var code = GenerateCode(form);

            if (_dbContext.ProductSKUs.Any(entity => entity.Code == code))
            {
                return(0);
            }

            var category = _dbContext.IPCategories.FirstOrDefault(entity => entity.Name == TYPE);

            var product = new ProductSKUModel(code, code, form.UOMId.GetValueOrDefault(), category.Id, "");

            _unitOfWork.ProductSKUs.Insert(product);
            _unitOfWork.Commit();

            var fabric = new FabricProductSKUModel(code, product.Id, form.WovenTypeId.GetValueOrDefault(), form.ConstructionId.GetValueOrDefault(), form.WidthId.GetValueOrDefault(), form.WarpId.GetValueOrDefault(), form.WeftId.GetValueOrDefault(), form.ProcessTypeId.GetValueOrDefault(), form.YarnTypeId.GetValueOrDefault(), form.GradeId.GetValueOrDefault(), form.UOMId.GetValueOrDefault());

            _unitOfWork.FabricSKUProducts.Insert(fabric);
            _unitOfWork.Commit();

            return(fabric.Id);
        }
        private string GenerateCode(FabricSKUFormDto form)
        {
            var wovenType = _dbContext.IPWovenType.FirstOrDefault(entity => entity.Id == form.WovenTypeId.GetValueOrDefault());
            var wovenCode = "000";

            if (wovenType != null)
            {
                wovenCode = wovenType.Code.PadLeft(3, '0');
            }

            var construction     = _dbContext.IPMaterialConstructions.FirstOrDefault(entity => entity.Id == form.ConstructionId.GetValueOrDefault());
            var constructionCode = "000";

            if (construction != null)
            {
                constructionCode = construction.Code.PadLeft(3, '0');
            }

            var width     = _dbContext.IPWidthType.FirstOrDefault(entity => entity.Id == form.WidthId.GetValueOrDefault());
            var widthCode = "00";

            if (width != null)
            {
                widthCode = width.Code.PadLeft(2, '0');
            }

            var warp     = _dbContext.IPWarpTypes.FirstOrDefault(entity => entity.Id == form.WarpId.GetValueOrDefault());
            var warpCode = "00";

            if (warp != null)
            {
                warpCode = warp.Code.PadLeft(2, '0');
            }

            var weft     = _dbContext.IPWeftTypes.FirstOrDefault(entity => entity.Id == form.WeftId.GetValueOrDefault());
            var weftCode = "00";

            if (weft != null)
            {
                weftCode = weft.Code.PadLeft(2, '0');
            }

            var processType     = _dbContext.IPProcessType.FirstOrDefault(entity => entity.Id == form.ProcessTypeId.GetValueOrDefault());
            var processTypeCode = "0";

            if (processType != null)
            {
                processTypeCode = processType.Code;
            }

            var yarnType     = _dbContext.IPYarnType.FirstOrDefault(entity => entity.Id == form.YarnTypeId.GetValueOrDefault());
            var yarnTypeCode = "000";

            if (yarnType != null)
            {
                yarnTypeCode = yarnType.Code.PadLeft(3, '0');
            }

            var grade     = _dbContext.IPGrades.FirstOrDefault(entity => entity.Id == form.GradeId.GetValueOrDefault());
            var gradeCode = "0";

            if (grade != null)
            {
                gradeCode = grade.Code;
            }

            var uom            = _dbContext.IPUnitOfMeasurements.FirstOrDefault(entity => entity.Id == form.UOMId.GetValueOrDefault());
            var uomCode        = "0";
            var uomMeterString = new List <string>()
            {
                "MTR", "METER"
            };
            var uomYardString = new List <string>()
            {
                "YDS", "YRD", "YARD"
            };

            if (grade != null)
            {
                if (uomMeterString.Contains(uom.Unit.ToUpper()))
                {
                    uomCode = "1";
                }
                else if (uomYardString.Contains(uom.Unit.ToUpper()))
                {
                    uomCode = "2";
                }
            }

            var monthCode = DateTime.Now.Month.ToString("d2");
            var yearCode  = DateTime.Now.Year.ToString().Substring(2);

            return(string.Concat(wovenCode, constructionCode, widthCode, warpCode, weftCode, processTypeCode, yarnTypeCode, gradeCode, uomCode, monthCode, yearCode));
        }
 public Task <int> UpdateSKU(int id, FabricSKUFormDto form)
 {
     throw new NotImplementedException();
 }