A bunch of fixes + saving files logic was changed.

This commit is contained in:
Yuee Shiness 2023-05-20 02:22:42 +04:00
parent e32bdbf76a
commit 2cc83f06cb
8 changed files with 75 additions and 43 deletions

View File

@ -109,11 +109,11 @@ namespace ComputerStoreBusinessLogic.BusinessLogic
} }
} }
foreach(var consignment in specifiedConsignments.Values) foreach(var consignment in consignments)
{ {
if(!consignments.Contains(consignment)) if (!specifiedConsignments.ContainsKey(consignment.ID))
{ {
specifiedConsignments.Remove(consignment.ID); specifiedConsignments.Add(consignment.ID,consignment);
} }
} }
@ -146,7 +146,7 @@ namespace ComputerStoreBusinessLogic.BusinessLogic
}); });
} }
public byte[] SaveConsignmentsToExcelFile(ReportComponentsBindingModel model) public MemoryStream SaveConsignmentsToExcelFile(ReportComponentsBindingModel model)
{ {
return _saveToExcel.CreateReport(new ExcelInfo return _saveToExcel.CreateReport(new ExcelInfo
{ {
@ -156,7 +156,7 @@ namespace ComputerStoreBusinessLogic.BusinessLogic
}); });
} }
public byte[] SaveConsignmentsToWordFile(ReportComponentsBindingModel model) public MemoryStream SaveConsignmentsToWordFile(ReportComponentsBindingModel model)
{ {
return _saveToWord.CreateDoc(new WordInfo return _saveToWord.CreateDoc(new WordInfo
{ {

View File

@ -10,7 +10,7 @@ namespace ComputerStoreBusinessLogic.OfficePackage
{ {
public abstract class AbstractSaveToExcel public abstract class AbstractSaveToExcel
{ {
public byte[] CreateReport(ExcelInfo info) public MemoryStream CreateReport(ExcelInfo info)
{ {
CreateExcel(info); CreateExcel(info);
@ -27,49 +27,64 @@ namespace ComputerStoreBusinessLogic.OfficePackage
CellToName = "C1" CellToName = "C1"
}); });
uint rowIndex = 2; uint rowIndex = 2;
foreach (var product in info.ConsignmentProducts) foreach (var consignment in info.ConsignmentProducts)
{ {
InsertCellInWorksheet(new ExcelCellParameters InsertCellInWorksheet(new ExcelCellParameters
{ {
ColumnName = "A", ColumnName = "A",
RowIndex = rowIndex, RowIndex = rowIndex,
Text = product.ID.ToString(), Text = $"ConsignmentID: {consignment.ID}",
StyleInfo = ExcelStyleInfoType.Text StyleInfo = ExcelStyleInfoType.Text
}); });
rowIndex++; rowIndex++;
foreach (var (Product, Count) in product.Products) foreach (var (Product, Count) in consignment.Products)
{ {
InsertCellInWorksheet(new ExcelCellParameters InsertCellInWorksheet(new ExcelCellParameters
{ {
ColumnName = "B", ColumnName = "B",
RowIndex = rowIndex, RowIndex = rowIndex,
Text = "ProductID:",
StyleInfo = ExcelStyleInfoType.TextWithBroder
});
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "C",
RowIndex = rowIndex,
Text = Product, Text = Product,
StyleInfo = ExcelStyleInfoType.TextWithBroder StyleInfo = ExcelStyleInfoType.TextWithBroder
}); });
InsertCellInWorksheet(new ExcelCellParameters InsertCellInWorksheet(new ExcelCellParameters
{ {
ColumnName = "C", ColumnName = "E",
RowIndex = rowIndex,
Text = "Count:",
StyleInfo = ExcelStyleInfoType.TextWithBroder
});
InsertCellInWorksheet(new ExcelCellParameters
{
ColumnName = "F",
RowIndex = rowIndex, RowIndex = rowIndex,
Text = Count.ToString(), Text = Count.ToString(),
StyleInfo = ExcelStyleInfoType.TextWithBroder StyleInfo = ExcelStyleInfoType.TextWithBroder
}); });
rowIndex++; rowIndex+=2;
} }
rowIndex++;
InsertCellInWorksheet(new ExcelCellParameters InsertCellInWorksheet(new ExcelCellParameters
{ {
ColumnName = "A", ColumnName = "B",
RowIndex = rowIndex, RowIndex = rowIndex,
Text = "Total", Text = "Total products:",
StyleInfo = ExcelStyleInfoType.Text StyleInfo = ExcelStyleInfoType.Text
}); });
InsertCellInWorksheet(new ExcelCellParameters InsertCellInWorksheet(new ExcelCellParameters
{ {
ColumnName = "C", ColumnName = "C",
RowIndex = rowIndex, RowIndex = rowIndex,
Text = product.ID.ToString(), Text = consignment.ID.ToString(),
StyleInfo = ExcelStyleInfoType.Text StyleInfo = ExcelStyleInfoType.Text
}); });
rowIndex++; rowIndex+=3;
} }
return SaveExcel(info); return SaveExcel(info);
@ -82,6 +97,6 @@ namespace ComputerStoreBusinessLogic.OfficePackage
protected abstract void MergeCells(ExcelMergeParameters excelParams); protected abstract void MergeCells(ExcelMergeParameters excelParams);
protected abstract byte[] SaveExcel(ExcelInfo info); protected abstract MemoryStream SaveExcel(ExcelInfo info);
} }
} }

View File

@ -10,7 +10,7 @@ namespace ComputerStoreBusinessLogic.OfficePackage
{ {
public abstract class AbstractSaveToWord public abstract class AbstractSaveToWord
{ {
public byte[] CreateDoc(WordInfo info) public MemoryStream CreateDoc(WordInfo info)
{ {
CreateWord(info); CreateWord(info);
CreateParagraph(new WordParagraph CreateParagraph(new WordParagraph
@ -26,7 +26,7 @@ namespace ComputerStoreBusinessLogic.OfficePackage
{ {
CreateParagraph(new WordParagraph CreateParagraph(new WordParagraph
{ {
Texts = new List<(string, WordTextProperties)> { (consigment.ID.ToString(), new WordTextProperties { Bold = true, Size = "24", }) }, Texts = new List<(string, WordTextProperties)> { ($"ConsignmentID: {consigment.ID}", new WordTextProperties { Bold = true, Size = "24", }) },
TextProperties = new WordTextProperties TextProperties = new WordTextProperties
{ {
Size = "24", Size = "24",
@ -38,7 +38,8 @@ namespace ComputerStoreBusinessLogic.OfficePackage
{ {
CreateParagraph(new WordParagraph CreateParagraph(new WordParagraph
{ {
Texts = new List<(string, WordTextProperties)> { (products.product, new WordTextProperties { Bold = true, Size = "24", }), ($" {products.count}", new WordTextProperties { Bold = true, Size = "24", }) }, Texts = new List<(string, WordTextProperties)> {
($"ProductID: {products.product} ", new WordTextProperties { Bold = false, Size = "24", }), ($"ProductCount: {products.count}", new WordTextProperties { Bold = false, Size = "24", }) },
TextProperties = new WordTextProperties TextProperties = new WordTextProperties
{ {
Size = "24", Size = "24",
@ -53,6 +54,6 @@ namespace ComputerStoreBusinessLogic.OfficePackage
protected abstract void CreateWord(WordInfo info); protected abstract void CreateWord(WordInfo info);
protected abstract void CreateParagraph(WordParagraph paragraph); protected abstract void CreateParagraph(WordParagraph paragraph);
protected abstract byte[] SaveWord(WordInfo info); protected abstract MemoryStream SaveWord(WordInfo info);
} }
} }

View File

@ -18,7 +18,7 @@ namespace ComputerStoreBusinessLogic.OfficePackage.Implements
private SpreadsheetDocument? _spreadsheetDocument; private SpreadsheetDocument? _spreadsheetDocument;
private SharedStringTablePart? _shareStringPart; private SharedStringTablePart? _shareStringPart;
private Worksheet? _worksheet; private Worksheet? _worksheet;
private MemoryStream? mem; private MemoryStream mem;
private static void CreateStyles(WorkbookPart workbookpart) private static void CreateStyles(WorkbookPart workbookpart)
{ {
var sp = workbookpart.AddNewPart<WorkbookStylesPart>(); var sp = workbookpart.AddNewPart<WorkbookStylesPart>();
@ -244,7 +244,7 @@ namespace ComputerStoreBusinessLogic.OfficePackage.Implements
Id = Id =
_spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart), _spreadsheetDocument.WorkbookPart.GetIdOfPart(worksheetPart),
SheetId = 1, SheetId = 1,
Name = "Лист" Name = "List"
}; };
sheets.Append(sheet); sheets.Append(sheet);
_worksheet = worksheetPart.Worksheet; _worksheet = worksheetPart.Worksheet;
@ -341,7 +341,7 @@ namespace ComputerStoreBusinessLogic.OfficePackage.Implements
mergeCells.Append(mergeCell); mergeCells.Append(mergeCell);
} }
protected override byte[] SaveExcel(ExcelInfo info) protected override MemoryStream SaveExcel(ExcelInfo info)
{ {
if (_spreadsheetDocument == null) if (_spreadsheetDocument == null)
{ {
@ -350,7 +350,8 @@ namespace ComputerStoreBusinessLogic.OfficePackage.Implements
_spreadsheetDocument.WorkbookPart!.Workbook.Save(); _spreadsheetDocument.WorkbookPart!.Workbook.Save();
_spreadsheetDocument.Dispose(); _spreadsheetDocument.Dispose();
return mem.ToArray(); mem.Seek(0,SeekOrigin.Begin);
return mem;
} }
} }
} }

View File

@ -103,7 +103,7 @@ namespace ComputerStoreBusinessLogic.OfficePackage.Implements
_docBody = mainPart.Document.AppendChild(new Body()); _docBody = mainPart.Document.AppendChild(new Body());
} }
protected override byte[] SaveWord(WordInfo info) protected override MemoryStream SaveWord(WordInfo info)
{ {
if (_docBody == null || _wordDocument == null) if (_docBody == null || _wordDocument == null)
{ {
@ -112,7 +112,9 @@ namespace ComputerStoreBusinessLogic.OfficePackage.Implements
_docBody.AppendChild(CreateSectionProperties()); _docBody.AppendChild(CreateSectionProperties());
_wordDocument.MainDocumentPart!.Document.Save(); _wordDocument.MainDocumentPart!.Document.Save();
_wordDocument.Dispose(); _wordDocument.Dispose();
return mem.ToArray();
mem.Seek(0, SeekOrigin.Begin);
return mem;
} }
} }
} }

View File

@ -15,7 +15,7 @@ namespace ComputerStoreContracts.BusinessLogicContracts
List<ReportConsignmentsViewModel> GetConsignmentsByComponents(ReportComponentsBindingModel model); List<ReportConsignmentsViewModel> GetConsignmentsByComponents(ReportComponentsBindingModel model);
void SavePCsAndProductsToPdfFile(ReportDateBindingModel model); void SavePCsAndProductsToPdfFile(ReportDateBindingModel model);
byte[] SaveConsignmentsToExcelFile(ReportComponentsBindingModel model); MemoryStream SaveConsignmentsToExcelFile(ReportComponentsBindingModel model);
byte[] SaveConsignmentsToWordFile(ReportComponentsBindingModel model); MemoryStream SaveConsignmentsToWordFile(ReportComponentsBindingModel model);
} }
} }

View File

@ -21,7 +21,7 @@ namespace ComputerStoreDatabaseImplement.Implements
if (string.IsNullOrEmpty(model.Name) && !model.ID.HasValue) { return null; } if (string.IsNullOrEmpty(model.Name) && !model.ID.HasValue) { return null; }
using var context = new ComputerStoreDatabase(); using var context = new ComputerStoreDatabase();
return context.PCS.Include(x => x.Employee).Include(x => x.Request).FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && model.Name.Equals(x.Name)) || (model.ID.HasValue && model.ID == x.ID))?.GetViewModel; return context.PCS.Include(x => x.Employee).Include(x => x.Request).ThenInclude(x => x.PCs).ThenInclude(x => x.Component).FirstOrDefault(x => (!string.IsNullOrEmpty(model.Name) && model.Name.Equals(x.Name)) || (model.ID.HasValue && model.ID == x.ID))?.GetViewModel;
} }
@ -35,9 +35,9 @@ namespace ComputerStoreDatabaseImplement.Implements
if(model.EmployeeID.HasValue) if(model.EmployeeID.HasValue)
{ {
return context.PCS.Include(x => x.Employee).Where(x => x.EmployeeID == model.EmployeeID).Select(x => x.GetViewModel).ToList(); return context.PCS.Include(x => x.Employee).Include(x => x.Request).ThenInclude(x => x.PCs).ThenInclude(x => x.Component).Where(x => x.EmployeeID == model.EmployeeID).Select(x => x.GetViewModel).ToList();
} }
return context.PCS.Include(x => x.Employee).Include(x => x.Request).Where(p => context.Requests.Where(r => context.Orders.Where(o => o.DateCreate >= model.DateFrom && o.DateCreate <= model.DateTo).Select(o => o.ID).Contains(r.OrderID)).Select(r => r.ID).Contains(p.RequestID)).ToList().Select(x => x.GetViewModel).ToList(); return context.PCS.Include(x => x.Employee).Include(x => x.Request).ThenInclude(x => x.PCs).ThenInclude(x => x.Component).Where(p => context.Requests.Where(r => context.Orders.Where(o => o.DateCreate >= model.DateFrom && o.DateCreate <= model.DateTo).Select(o => o.ID).Contains(r.OrderID)).Select(r => r.ID).Contains(p.RequestID)).ToList().Select(x => x.GetViewModel).ToList();
} }
public List<PCViewModel> GetFullList() public List<PCViewModel> GetFullList()
@ -72,7 +72,7 @@ namespace ComputerStoreDatabaseImplement.Implements
using var transaction = context.Database.BeginTransaction(); using var transaction = context.Database.BeginTransaction();
try try
{ {
var specPC = context.PCS.FirstOrDefault(x => x.ID == model.ID); var specPC = context.PCS.Include(x => x.Request).Include(x => x.Employee).FirstOrDefault(x => x.ID == model.ID);
if(specPC == null) { return null; } if(specPC == null) { return null; }
specPC.Update(model); specPC.Update(model);
@ -91,7 +91,7 @@ namespace ComputerStoreDatabaseImplement.Implements
public PCViewModel? Delete(PCBindingModel model) public PCViewModel? Delete(PCBindingModel model)
{ {
using var context = new ComputerStoreDatabase(); using var context = new ComputerStoreDatabase();
var specPC = context.PCS.Include(x => x.Request).FirstOrDefault(x => x.ID == model.ID); var specPC = context.PCS.Include(x => x.Request).Include(x => x.Employee).FirstOrDefault(x => x.ID == model.ID);
if(specPC == null) { return null;} if(specPC == null) { return null;}
context.PCS.Remove(specPC); context.PCS.Remove(specPC);
context.SaveChanges(); context.SaveChanges();

View File

@ -505,29 +505,42 @@ namespace ComputerStoreRestAPI.Controllers
#endregion #endregion
[HttpGet] [HttpGet]
public List<ReportConsignmentsViewModel> GetReportConsignments(ReportComponentsBindingModel model) public List<ReportConsignmentsViewModel> GetReportConsignments(string str)
{ {
var model = new ReportComponentsBindingModel {};
var components = str.Split(";", StringSplitOptions.RemoveEmptyEntries);
foreach (var component in components)
{
var fields = component.Split("|");
model.Components.Add(new ComponentSearchModel { ID = Convert.ToInt32(fields[0]), Name = fields[1] });
}
return _employeeReportLogic.GetConsignmentsByComponents(model); return _employeeReportLogic.GetConsignmentsByComponents(model);
} }
[HttpGet] [HttpPost]
public byte[] SaveReportConsignmentsToExcel(ReportComponentsBindingModel model) public ActionResult SaveReportConsignmentsToExcel(ReportComponentsBindingModel model)
{ {
return _employeeReportLogic.SaveConsignmentsToExcelFile(new ReportComponentsBindingModel
var stream = _employeeReportLogic.SaveConsignmentsToExcelFile(new ReportComponentsBindingModel
{ {
FileName = "ReportConsignments", FileName = model.FileName,
Components = model.Components Components = model.Components
}); });
return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", model.FileName);
} }
[HttpGet] [HttpPost]
public byte[] SaveReportConsignmentsToWord(ReportComponentsBindingModel model) public ActionResult SaveReportConsignmentsToWord(ReportComponentsBindingModel model)
{ {
return _employeeReportLogic.SaveConsignmentsToWordFile(new ReportComponentsBindingModel var stream = _employeeReportLogic.SaveConsignmentsToWordFile(new ReportComponentsBindingModel
{ {
FileName = "ReportConsignments", FileName = "ReportConsignments",
Components = model.Components Components = model.Components
}); });
return File(stream, "application/vnd.openxmlformats-officedocument.spreadsheetml.document", model.FileName);
} }
} }
} }