Beispiel #1
0
        public async Task <IActionResult> UploadFile()
        {
            IFormFile file  = Request.Form.Files[0];
            ISheet    sheet = ImportExcelCommon.GetSheetFromFile(file, "Customer");

            if (sheet != null)
            {
                IRow headerRow = sheet.GetRow(1); //Get Header Row
                int  cellCount = headerRow.LastCellNum;
                int  startRow  = sheet.FirstRowNum + 2;
                int  lastRow   = sheet.LastRowNum;

                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;
                    }
                    int startCell = row.FirstCellNum;

                    //lay cac gia tri tren row
                    string[] rowrequests = ImportExcelCommon.GetRowValue(row, startCell, cellCount);

                    string Code         = rowrequests[0];
                    string CartCode     = rowrequests[1];
                    string Name         = rowrequests[2];
                    string PhoneNumber  = rowrequests[3];
                    string Email        = rowrequests[4];
                    string StreetNumber = rowrequests[5];
                    string Street       = rowrequests[6];
                    string District     = rowrequests[7];
                    string City         = rowrequests[8];
                    string Country      = rowrequests[9];
                    string Lat          = rowrequests[10];
                    string Lng          = rowrequests[11];

                    CustomerModel customerModel = _customerRepository.GetByCode(Code);
                    if (customerModel != null)
                    {
                        UpdateCustomerRequest updateCustomerRequest = new UpdateCustomerRequest()
                        {
                            Id          = customerModel.ID,
                            Code        = Code,
                            CartCode    = CartCode,
                            Name        = Name,
                            PhoneNumber = PhoneNumber,
                            Email       = Email,
                            Address     = new AddressModel()
                            {
                                StreetNumber = StreetNumber,
                                Street       = Street,
                                District     = District,
                                City         = City,
                                Country      = Country,
                                Lat          = Double.Parse(Lat.Trim()),
                                Lng          = Double.Parse(Lng.Trim())
                            }
                        };
                        await _CustomerAppService.updateCustomer(updateCustomerRequest);
                    }
                    else
                    {
                        AddNewCustomerRequest addNewCustomerRequest = new AddNewCustomerRequest()
                        {
                            Code        = Code,
                            CartCode    = CartCode,
                            Name        = Name,
                            PhoneNumber = PhoneNumber,
                            Email       = Email,
                            Address     = new AddressModel()
                            {
                                StreetNumber = StreetNumber,
                                Street       = Street,
                                District     = District,
                                City         = City,
                                Country      = Country,
                                Lat          = Double.Parse(Lat.Trim()),
                                Lng          = Double.Parse(Lng.Trim())
                            }
                        };
                        await _CustomerAppService.addNewCustomer(addNewCustomerRequest);
                    }
                }
                return(Ok());
            }
            return(BadRequest());
        }
Beispiel #2
0
        public async Task <IActionResult> UploadFile()
        {
            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;

                for (int j = 0; j < cellCount; j++)
                {
                    NPOI.SS.UserModel.ICell cell = headerRow.GetCell(j);
                    if (cell == null || string.IsNullOrWhiteSpace(cell.ToString()))
                    {
                        continue;
                    }
                }
                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;
                    }
                    int startCell = row.FirstCellNum;

                    ProductModel p = new ProductModel();
                    p.Code               = row.GetCell(startCell++).ToString();
                    p.Name               = row.GetCell(startCell++).ToString();
                    p.Note               = "";
                    p.Preservation       = new PreservationModel();
                    p.OtherUnitOfProduct = new List <ProductUnitModel>();
                    UnitModel unit = _UnitRepository.GetByName(row.GetCell(startCell++).ToString().ToLower());
                    if (unit == null)
                    {
                        continue;
                    }
                    p.UnitId = unit.ID;
                    int wpu = -1;
                    Int32.TryParse(row.GetCell(startCell).ToString(), out wpu);
                    if (wpu == -1)
                    {
                        continue;
                    }
                    p.WeightPerUnit = wpu;
                    AddNewProductRequest request = new AddNewProductRequest()
                    {
                        Product = p
                    };
                    await _ProductAppService.addNewProduct(request);
                }
                return(Ok());
            }
            return(BadRequest());
        }
Beispiel #3
0
        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());
        }
Beispiel #4
0
        public async Task <IActionResult> ImportQuotation([FromQuery] AddNewQuotationRequest request)
        {
            IFormFile file  = Request.Form.Files[0];
            ISheet    sheet = ImportExcelCommon.GetSheetFromFile(file, "Quotation\\Customer_" + request.customer_code);

            if (sheet != null)
            {
                IRow headerRow = sheet.GetRow(11); //Get Header Row
                int  cellCount = headerRow.LastCellNum;
                int  startRow  = 12;
                int  lastRow   = sheet.LastRowNum;
                //thuc hien them moi dot bao gia cho khach hang
                int QuotationId = (await _QuotationAppService.addNewQuotation(request)).Data;
                if (QuotationId <= 0)
                {
                    return(BadRequest());
                }
                //them cac san pham vao dot bao gia
                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;
                    }
                    int startCell = row.FirstCellNum + 1;

                    AddNewQuotationItemRequest itemRequest = new AddNewQuotationItemRequest();
                    string code = row.GetCell(startCell++).ToString();
                    if (string.IsNullOrWhiteSpace(code))
                    {
                        continue;
                    }
                    itemRequest.ProductCode = code;
                    itemRequest.QuotationID = QuotationId;
                    //gia
                    int price = -1;
                    Int32.TryParse(row.GetCell(4).ToString(), out price);
                    if (price == -1)
                    {
                        continue;
                    }
                    itemRequest.Price = price;


                    UnitModel unit = _UnitRepository.GetByName(row.GetCell(3).ToString().ToLower());
                    if (unit == null)
                    {
                        continue;
                    }
                    itemRequest.UnitID = unit.ID;


                    await _QuotationItemAppService.addNewQuotation(itemRequest);
                }
                return(Ok());
            }
            return(BadRequest());
        }