public async Task <int> Update(InventoryDailyReport inventoryDailyReport) { return(await Context.SaveChangesAsync()); }
public async Task <int> Insert(InventoryDailyReport inventoryDailyReport) { _inventoryDailyReportRepository.Create(inventoryDailyReport); return(await Context.SaveChangesAsync()); }
/// <summary> /// Update theo sản phẩm. /// </summary> /// <param name="tenantId">Mã khách hàng</param> /// <param name="warehouseId">Mã kho</param> /// <param name="productId">Mã sản phẩm</param> /// <param name="entryDate">Ngày Nhập/Xuất hàng</param> /// <returns></returns> public async Task <ActionResultResponse> Update(string tenantId, string warehouseId, string productId, DateTime entryDate) { // Get opening stock. var openingStock = await GetOpeningStock(); // Get inventory calculator method. var inventoryCalculatorMethod = await _warehouseConfigRepository.GetInventoryCalculatorMethod(tenantId, warehouseId); if (inventoryCalculatorMethod == null) { return(new ActionResultResponse(-1, _resourceService.GetString("Please config inventory calculator method."))); } // Get goods receipt quantity today. var goodsReceiptNoteDetails = await _goodsReceiptNoteDetailRepository.GetsProductInfoForinventoryDailyReportByDay(tenantId, warehouseId, productId, entryDate); // Get goods delivery quantity today. var goodsDeliveryNoteDetails = await _goodsDeliveryNoteDetailRepository.GetsProductInfoForinventoryDailyReportByDay(tenantId, warehouseId, productId, entryDate); decimal openingStockQuantity = openingStock?.ClosingStockQuantity ?? 0; decimal openingStockValue = openingStock?.ClosingStockValue ?? 0; var closingStockQuantity = (openingStock?.ClosingStockQuantity ?? 0) + goodsReceiptNoteDetails.Sum(x => x.Quantity) - goodsDeliveryNoteDetails.Sum(x => x.Quantity); decimal closingStockValue = 0; decimal closingStockPrice = 0; decimal totalReceiptQuantity = 0; decimal totalReceiptValue = 0; decimal totalDeliveryQuantity = 0; decimal totalDeliveryValue = 0; if (goodsReceiptNoteDetails.Any()) { totalReceiptQuantity = goodsReceiptNoteDetails.Sum(x => x.Quantity); totalReceiptValue = goodsReceiptNoteDetails.Sum(x => x.Price * x.Quantity); } if (goodsDeliveryNoteDetails.Any()) { totalDeliveryQuantity = goodsReceiptNoteDetails.Sum(x => x.Quantity); totalDeliveryValue = goodsReceiptNoteDetails.Sum(x => x.Price * x.Quantity); } var inventoryQuantity = openingStockQuantity + totalReceiptQuantity; // TODO: Thêm update phương pháp tính giá trị tồn kho. // Tính giá trị tồn dựa theo phương pháp. switch (inventoryCalculatorMethod) { // Bình quân ra quyền. case InventoryCalculatorMethod.WeightedAverage: // Cập nhật lại giá trong sản phẩm phục vụ cho xuất kho. //await _productRepository.UpdateExWarehousePrice(tenantId, productId, closingStockPrice); break; // Bình quân ra quyền (Thời điểm tức thời). case InventoryCalculatorMethod.WeightedAverageImmediately: closingStockPrice = (openingStockQuantity + totalReceiptValue) / (inventoryQuantity == 0 ? 1 : inventoryQuantity); closingStockValue = closingStockQuantity * closingStockPrice; // Cập nhật lại giá trong sản phẩm phục vụ cho xuất kho. //await _productRepository.UpdateExWarehousePrice(tenantId, productId, closingStockPrice); break; // Nhập trước xuất trước. case InventoryCalculatorMethod.FIFO: break; // Nhập sau xuất trước. case InventoryCalculatorMethod.LIFO: break; // Thực tế giá đích danh case InventoryCalculatorMethod.SpecificIdentificationMethod: break; default: break; } // Check exists report by date. //var inventoryDailReportInfo = await _inventoryDailyReportRepository.GetInfo(tenantId, warehouseId, productId, entryDate); //if (inventoryDailReportInfo == null) //{ // Thêm báo cáo tồn kho cho ngày nhập nếu chưa có. var inventoryDailReportInfo = new InventoryDailyReport { OpeningStockQuantity = openingStockQuantity, OpeningStockValue = openingStockValue, WarehouseId = warehouseId, ClosingStockValue = closingStockValue, ClosingStockQuantity = closingStockQuantity, TenantId = tenantId, ReceivingQuantity = totalReceiptQuantity, ReceivingValue = totalReceiptValue, DeliveringQuantity = totalDeliveryQuantity, DeliveringValue = totalDeliveryValue, ProductId = productId, EntryDate = entryDate, Day = (byte)entryDate.Day, Month = (byte)entryDate.Month, Year = entryDate.Year, Quarter = entryDate.GetQuarter(), UnitId = await _productUnitRepository.GetDefaultUnitId(productId, tenantId) }; var result = await _inventoryDailyReportRepository.Insert(inventoryDailReportInfo); return(new ActionResultResponse(result, result <= 0 ? _sharedResourceService.GetString(ErrorMessage.SomethingWentWrong) : _resourceService.GetString("Add new inventory daily report successful."))); //} // Cập nhật lại thông tin báo cáo tồn trong trường hợp đã tồn tại. //inventoryDailReportInfo.OpeningStockQuantity = openingStockQuantity; //inventoryDailReportInfo.OpeningStockValue = openingStockValue; //inventoryDailReportInfo.ClosingStockQuantity = closingStockQuantity; //inventoryDailReportInfo.ClosingStockValue = closingStockValue; //inventoryDailReportInfo.OpeningStockQuantity = openingStockQuantity; //inventoryDailReportInfo.ReceivingQuantity = totalReceiptQuantity; //inventoryDailReportInfo.ReceivingValue = totalReceiptValue; //inventoryDailReportInfo.DeliveringQuantity = totalDeliveryQuantity; //inventoryDailReportInfo.DeliveringValue = totalDeliveryValue; //var resultUpdate = await _inventoryDailyReportRepository.Update(inventoryDailReportInfo); //return new ActionResultResponse(resultUpdate, resultUpdate <= 0 ? _sharedResourceService.GetString(ErrorMessage.SomethingWentWrong) // : _resourceService.GetString("Update inventory daily report successful.")); async Task <InventoryDailyReport> GetOpeningStock() { return(await _inventoryDailyReportRepository.GetOpeningStock(tenantId, warehouseId, productId, entryDate)); } }