public async Task <object> ScanQRCodeFromChemialWareHouse(string qrCode, string building, int userid) { var obj = qrCode.Split('-'); var Barcode = obj[2]; var ProductionDate = obj[0]; var Batch = obj[1]; var model = _repoIngredient.FindAll().FirstOrDefault(x => x.MaterialNO.Equals(Barcode)); var supModel = _repoSupplier.GetAll(); var ProductionDates = Convert.ToDateTime(ProductionDate.Substring(0, 4) + "/" + ProductionDate.Substring(4, 2) + "/" + ProductionDate.Substring(6, 2)); var exp = ProductionDates.AddMonths(3); var currentDate = DateTime.Now; var data = await CreateIngredientInfo(new IngredientInfo { Name = model.Name, ExpiredTime = exp.Date, ManufacturingDate = ProductionDates.Date, SupplierName = supModel.FirstOrDefault(s => s.ID == model.SupplierID).Name, Qty = model.Unit.ToInt(), Batch = Batch, Consumption = "0", Code = model.MaterialNO, IngredientID = model.ID, UserID = userid, BuildingName = building }); // check trong bang ingredientReport xem đã tồn tại code hay chưa , nếu có tồn tại if (await _repoIngredientInfoReport.CheckBarCodeExists(Barcode)) { // check tiep trong bang ingredientReport xem co du lieu chua var result = _repoIngredientInfoReport.FindAll().FirstOrDefault(x => x.Code == Barcode && x.Batch == Batch && x.CreatedDate.Date == currentDate.Date); // nếu khác Null thi update lai if (result != null) { result.Qty = model.Unit.ToInt() + result.Qty; await UpdateIngredientInfoReport(result); } // nếu bằng null thì tạo mới IngredientReport else { await CreateIngredientInfoReport(new IngredientInfoReport { Name = model.Name, ExpiredTime = ProductionDates.Date.AddMonths(3), ManufacturingDate = ProductionDates.Date, SupplierName = supModel.FirstOrDefault(s => s.ID == model.SupplierID).Name, Qty = model.Unit.ToInt(), Consumption = "0", Code = model.MaterialNO, Batch = Batch, IngredientInfoID = data.ID, UserID = userid, BuildingName = building }); } } // nếu chưa tồn tại thì thêm mới else { await CreateIngredientInfoReport(new IngredientInfoReport { Name = model.Name, ExpiredTime = ProductionDates.Date.AddMonths(3), ManufacturingDate = ProductionDates.Date, SupplierName = supModel.FirstOrDefault(s => s.ID == model.SupplierID).Name, Qty = model.Unit.ToInt(), Batch = Batch, Consumption = "0", Code = model.MaterialNO, IngredientInfoID = data.ID, UserID = userid, BuildingName = building }); } return(true); }
public async Task <object> ScanQRCodeFromChemialWareHouse(string qrCode, string building, int userid) { // load tat ca supplier var supModel = _repoSupplier.GetAll(); // lay gia tri "barcode" trong chuỗi qrcode được chuyền lên var Barcode = qrCode.Split('-', '-')[2]; // tim ID của ingredient var ingredientID = _repoIngredient.FindAll().FirstOrDefault(x => x.Code.Equals(Barcode)).ID; // Find ingredient theo ingredientID vừa tìm được ở trên var model = _repoIngredient.FindById(ingredientID); // lấy giá trị "ProductionDate" trong chuỗi qrcode được chuyền lên var ProductionDate = qrCode.Split('-')[0]; // lấy giá trị "Batch" trong chuỗi qrcode được chuyền lên var Batch = qrCode.Split('-', '-')[1]; // sau đó convert sang kiểu date time var ProductionDates = Convert.ToDateTime(ProductionDate.Substring(0, 4) + "/" + ProductionDate.Substring(4, 2) + "/" + ProductionDate.Substring(6, 2)); var exp = ProductionDates.AddMonths(3); // khai báo biến start = ngày hiện tại var resultStart = DateTime.Now; // khai báo biến end = ngày hiện tại var resultEnd = DateTime.Now; // tạo ingredientInfo mới var data = await CreateIngredientInfo(new IngredientInfo { Name = model.Name, ExpiredTime = exp.Date, ManufacturingDate = ProductionDates.Date, SupplierName = supModel.FirstOrDefault(s => s.ID == model.SupplierID).Name, Qty = model.Unit.ToInt(), Batch = Batch, Consumption = "0", Code = model.Code, IngredientID = model.ID, UserID = userid, BuildingName = building }); // check trong bang ingredientReport xem đã tồn tại code hay chưa , nếu có tồn tại if (await _repoIngredientInfoReport.CheckBarCodeExists(Barcode)) { // check tiep trong bang ingredientReport xem co du lieu chua var result = _repoIngredientInfoReport.FindAll().FirstOrDefault(x => x.Code == Barcode && x.Batch == Batch && x.CreatedDate <= resultEnd.Date && x.CreatedDate >= resultStart.Date); // nếu khác Null thi update lai if (result != null) { result.Qty = model.Unit.ToInt() + result.Qty; await UpdateIngredientInfoReport(result); } // nếu bằng null thì tạo mới IngredientReport else { await CreateIngredientInfoReport(new IngredientInfoReport { Name = model.Name, ExpiredTime = ProductionDates.Date.AddMonths(3), ManufacturingDate = ProductionDates.Date, SupplierName = supModel.FirstOrDefault(s => s.ID == model.SupplierID).Name, Qty = model.Unit.ToInt(), Consumption = "0", Code = model.Code, Batch = Batch, IngredientInfoID = data.ID, UserID = userid, BuildingName = building }); } } // nếu chưa tồn tại thì thêm mới else { await CreateIngredientInfoReport(new IngredientInfoReport { Name = model.Name, ExpiredTime = ProductionDates.Date.AddMonths(3), ManufacturingDate = ProductionDates.Date, SupplierName = supModel.FirstOrDefault(s => s.ID == model.SupplierID).Name, Qty = model.Unit.ToInt(), Batch = Batch, Consumption = "0", Code = model.Code, IngredientInfoID = data.ID, UserID = userid, BuildingName = building }); } return(true); }