public async Task <IActionResult> Post([FromBody] AddNewInvoiceRequest request) { AddNewInvoiceResponse result = await _InvoiceAppService.addNewInvoice(request); if (result.Success) { return(Ok(result)); } return(NotFound(result)); }
public Task <AddNewInvoiceResponse> addNewInvoice(AddNewInvoiceRequest request) { DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc); dtDateTime = dtDateTime.AddSeconds(request.DeliveryTime).ToLocalTime(); List <InvoiceItem> items = new List <InvoiceItem>(); int totalPrice = 0; decimal totalWeight = 0; foreach (InvoiceItemModel invoiceItem in request.Items) { totalPrice += invoiceItem.TotalPrice; decimal weightPerUnit = _ProductRepository.GetWeightPerUnit(invoiceItem.ProductID, invoiceItem.UnitID); invoiceItem.Weight = weightPerUnit * invoiceItem.Quantity; totalWeight += invoiceItem.Weight; } AddNewInvoiceCommand command = new AddNewInvoiceCommand() { Address = request.Address, CustomerCode = request.CustomerCode, CustomerID = request.CustomerID, CustomerName = request.CustomerName, DeliveryTime = dtDateTime, Code = Guid.NewGuid().ToString().Substring(0, 8), TotalPrice = totalPrice, WeightTotal = totalWeight, Items = request.Items }; Task <object> Invoice = (Task <object>)Bus.SendCommand(command); RabbitMQBus.Publish(command); AddNewInvoiceResponse response = new AddNewInvoiceResponse(); response = Common <AddNewInvoiceResponse> .checkHasNotification(_notifications, response); if (response.Success) { response.Data = (int)Invoice.Result; } return(Task.FromResult(response)); }
public async Task <IActionResult> ImportExcel(DateTime date) { IFormFile file = Request.Form.Files[0]; ISheet sheet = ImportExcelCommon.GetSheetFromFile(file, "Product"); if (sheet != null) { IRow headerRow = sheet.GetRow(0); //Get Header Row int cellCount = headerRow.LastCellNum; int startRow = sheet.FirstRowNum + 1; int lastRow = sheet.LastRowNum; List <int> customerIds = new List <int>(); DateTime Epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc); TimeSpan elapsedTime = date.AddDays(-1) - Epoch; double dateDelvery = elapsedTime.TotalSeconds; //lay danh sach khach hang import for (int j = 3; j < cellCount; j++) { ICell cell = headerRow.GetCell(j); string[] value = ImportExcelCommon.GetRowCellValue(headerRow, j).Split('_'); if (value.Length == 0) { continue; } string customerCode = value[0].Trim(); CustomerModel customerModel = _CustomerAppService.GetByCode(customerCode); if (customerModel == null) { continue; } customerIds.Add(customerModel.ID); AddNewInvoiceRequest request = new AddNewInvoiceRequest() { Address = null, CustomerCode = customerCode, CustomerID = customerModel.ID, CustomerName = "", DeliveryTime = dateDelvery }; List <InvoiceItemModel> lstItem = new List <InvoiceItemModel>(); for (int i = startRow; i <= lastRow; i++) //Read Excel File { IRow row = sheet.GetRow(i); if (row == null) { continue; } if (row.Cells.All(d => d.CellType == CellType.Blank)) { continue; } InvoiceItemModel item = new InvoiceItemModel(); int startCell = row.FirstCellNum; string productCode = ImportExcelCommon.GetRowCellValue(row, startCell++); ProductModel product = _ProductRepository.GetByCode(productCode); if (product == null) { continue; } item.ProductID = product.Id; item.Deliveried = false; item.DeliveriedQuantity = 0; item.Note = ""; item.ProductName = ImportExcelCommon.GetRowCellValue(row, startCell++); UnitModel unit = _UnitRepository.GetByName(row.GetCell(startCell++).ToString().ToLower()); if (unit == null) { continue; } item.UnitID = unit.ID; item.Quantity = int.Parse(ImportExcelCommon.GetRowCellValue(row, startCell++).Trim()); lstItem.Add(item); } request.Items = lstItem; await _InvoiceAppService.addNewInvoice(request); } return(Ok()); } return(BadRequest()); }