2024-11-27 23:43:53 +04:00

279 lines
10 KiB
C#

using Components;
using Contracts.BindingModels;
using Contracts.BusinessLogicContracts;
namespace COP3_
{
public partial class FormMain : Form
{
private readonly IOrderLogic _orderLogic;
public FormMain(IOrderLogic orderLogic)
{
InitializeComponent();
_orderLogic = orderLogic;
this.KeyPreview = true;
LoadData();
}
private void FormMain_Load(object sender, EventArgs e)
{
LoadData();
}
private void LoadData()
{
try
{
var orders = _orderLogic.ReadList(null);
}
catch (Exception ex)
{
MessageBox.Show($"{ex.Message}");
}
}
private void Create(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormEdit));
if (!(service is FormEdit form))
{
return;
}
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
private void Open(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormEdit));
if (!(service is FormEdit form))
{
return;
}
var selectedOrder = listComponent1.GetObjectFromSelectedRow<OrderBindingModel>();
form.Id = Convert.ToInt32(selectedOrder.Id);
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
private void Delete(object sender, EventArgs e)
{
try
{
var selectedOrder = listComponent1.GetObjectFromSelectedRow<OrderBindingModel>();
if (selectedOrder != null)
{
var result = MessageBox.Show(
"Ïîäòâåðäèòå óäàëåíèå çàïèñè",
"Ïîäòâåðæäåíèå",
MessageBoxButtons.YesNo,
MessageBoxIcon.Question
);
if (result == DialogResult.Yes)
{
_orderLogic.Delete(new OrderBindingModel { Id = selectedOrder.Id });
MessageBox.Show("Óäàëåíî");
LoadData();
}
else if (result == DialogResult.No)
{
MessageBox.Show("Óäàëåíèå îòìåíåíî", "Èíôîðìàöèÿ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
LoadData();
}
LoadData();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void CreateDoc(object sender, EventArgs e)
{
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
saveFileDialog.Filter = "Word Documents (*.docx)|*.docx";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = saveFileDialog.FileName;
string title = "çàêàçû, êîòîðûå îïëà÷åíû ïîëíîñòüþ çà ñ÷åò ñêèäîê :D";
string[] data;
try
{
var orders = _orderLogic.ReadList(null);
if (orders != null)
{
/* // Ôèëüòðóåì çàêàçû, ãäå ñóììà çàêàçà ðàâíà null èëè ïóñòîé ñòðîêå
var freeOrders = orders
.Where(x => string.IsNullOrEmpty(x.OrderDeliveryTime))
.Select(x => $"ID: {x.Id}, ÔÈÎ: {x.FIO}, Ñòàòóñ çàêàçà: {x.OrderDestination}, Îïèñàíèå çàêàçà: {x.OrderPath}")
.ToList();
if (freeOrders.Count == 0)
{
MessageBox.Show("Íåò çàêàçîâ, îïëà÷åííûõ ñêèäêàìè.");
return;
}
*/
// Ïðåîáðàçóåì ñïèñîê â ìàññèâ ñòðîê äëÿ ïåðåäà÷è â ìåòîä
//data = freeOrders.ToArray();
// Ñîçäàåì äîêóìåíò Word
// bigTextWordNoVisibleComponent1.CreateAndSaveDocument(filePath, title, data);
MessageBox.Show("Óñïåøíî :D ïðîâåðÿé íàëè÷èå ôàéëà...");
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
}
}
private void CreateTableDoc(object sender, EventArgs e)
{
using (SaveFileDialog saveFileDialog = new SaveFileDialog())
{
saveFileDialog.Filter = "Excel Files (*.xlsx)|*.xlsx";
saveFileDialog.Title = "Ñîõðàíèòü äèàãðàììó â Excel";
if (saveFileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = saveFileDialog.FileName;
try
{
var orders = _orderLogic.ReadList(null);
if (orders == null || !orders.Any())
{
MessageBox.Show("Íåò äàííûõ äëÿ ñîçäàíèÿ äèàãðàììû.");
return;
}
/* var groupedData = orders
.Where(o => o.OrderDeliveryTime != null && o.OrderDeliveryTime != "" && o.OrderDeliveryTime != string.Empty)
.GroupBy(o => o.OrderDestination)
.Select(g => new ComponentExcelWithPieDiagram.ChartData
{
SeriesName = g.Key,
SeriesValue = g.Count()
})
.ToList();
if (!groupedData.Any())
{
MessageBox.Show("Íåò îïëà÷åííûõ çàêàçîâ.");
return;
}
string title = "Îïëà÷åííûå çàêàçû ïî ñòàòóñàì";
string diagramTitle = "Ðàñïðåäåëåíèå îïëà÷åííûõ çàêàçîâ";
componentExcelWithPieDiagram1.CreateExcelWithPieChart(
filePath,
title,
diagramTitle,
ComponentExcelWithPieDiagram.LegendPosition.Left,
groupedData
);*/
MessageBox.Show("Ôàéë óñïåøíî ñîçäàí.", "Èíôîðìàöèÿ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Îøèáêà ïðè ñîçäàíèè ôàéëà: {ex.Message}", "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void CreateDiagramDoc(object sender, EventArgs e)
{
using (SaveFileDialog fileDialog = new SaveFileDialog())
{
fileDialog.Filter = "PDF Files (*.pdf)|*.pdf";
fileDialog.Title = "Ñîõðàíèòü äèàãðàììó â PDF";
if (fileDialog.ShowDialog() == DialogResult.OK)
{
string filePath = fileDialog.FileName;
string fileTitle = "Èíôîðìàöèÿ î çàêàçàõ";
try
{
var orders = _orderLogic.ReadList(null);
/* if (orders == null || !orders.Any())
{
MessageBox.Show("Íåò äàííûõ");
return;
}
List<UserComponentsOption19.DiagramWordNoVisibleComponent.ChartSeries> tables = new List<UserComponentsOption19.DiagramWordNoVisibleComponent.ChartSeries>();
string[,] tableData = new string[orders.Count + 1, 5];
// Øàïêà òàáëèöû
tableData[0, 0] = "Èäåíòèôèêàòîð";
tableData[0, 1] = "ÔÈÎ";
tableData[0, 2] = "Ñòàòóñ çàêàçà";
tableData[0, 3] = "Ñóììà çàêàçîâ";
tableData[0, 4] = "Îïèñàíèå";
for (int i = 0; i < orders.Count; i++)
{
var order = orders[i];
tableData[i + 1, 0] = order.Id.ToString();
tableData[i + 1, 1] = order.FIO;
tableData[i + 1, 2] = order.OrderDestination;
tableData[i + 1, 3] = string.IsNullOrWhiteSpace(order.OrderDeliveryTime)
? "Îïëà÷åí ñêèäêàìè"
: order.OrderDeliveryTime;
tableData[i + 1, 4] = order.OrderPath;
}
diagramWordNoVisibleComponent1.CreateDocumentWithChart(filePath, fileTitle, "", UserComponentsOption19.DiagramWordNoVisibleComponent.LegendPosition.Bottom, tables);
*/
MessageBox.Show("Ôàéë óñïåøíî ñîçäàí.", "Èíôîðìàöèÿ", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
catch (Exception ex)
{
MessageBox.Show($"Îøèáêà ïðè ñîçäàíèè ôàéëà: {ex.Message}", "Îøèáêà", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
}
}
}
private void contextMenuStrip1_Opening(object sender, System.ComponentModel.CancelEventArgs e)
{
}
private void ñïðàâî÷íèêToolStripMenuItem_Click(object sender, EventArgs e)
{
var service = Program.ServiceProvider?.GetService(typeof(FormGuide));
if (!(service is FormGuide form))
{
return;
}
if (form.ShowDialog() == DialogResult.OK)
{
LoadData();
}
}
}
}