2024-03-22 15:57:40 +04:00
using Microsoft.Extensions.Logging ;
2024-03-22 01:29:50 +04:00
using Microsoft.Reporting.WinForms ;
2024-03-22 15:57:40 +04:00
using TravelCompanyContracts.BindingModels ;
using TravelCompanyContracts.BusinessLogicsContracts ;
2024-03-22 01:29:50 +04:00
using System ;
using System.Collections.Generic ;
using System.ComponentModel ;
using System.Data ;
using System.Drawing ;
using System.Linq ;
using System.Text ;
using System.Threading.Tasks ;
using System.Windows.Forms ;
namespace TravelCompany.Forms
{
public partial class FormReportOrders : Form
{
private readonly ReportViewer reportViewer ;
private readonly ILogger _logger ;
private readonly IReportLogic _logic ;
2024-03-22 15:57:40 +04:00
2024-03-22 01:29:50 +04:00
public FormReportOrders ( ILogger < FormReportOrders > logger , IReportLogic logic )
{
InitializeComponent ( ) ;
_logger = logger ;
_logic = logic ;
reportViewer = new ReportViewer
{
Dock = DockStyle . Fill
} ;
2024-03-22 19:04:16 +04:00
reportViewer . LocalReport . LoadReportDefinition ( new FileStream ( "C:\\Users\\goldfest\\Desktop\\TravelCompany\\PrecastConcretePlant\\PrecastConcretePlantView\\ReportOrders.rdlc" , FileMode . Open ) ) ;
2024-03-22 01:29:50 +04:00
Controls . Clear ( ) ;
Controls . Add ( reportViewer ) ;
Controls . Add ( panel ) ;
}
2024-03-22 15:57:40 +04:00
private void buttonCreateReport_Click ( object sender , EventArgs e )
2024-03-22 01:29:50 +04:00
{
2024-03-22 19:04:16 +04:00
if ( dateTimePickerStart . Value . Date > = dateTimePickerEnd . Value . Date )
2024-03-22 01:29:50 +04:00
{
2024-03-22 15:57:40 +04:00
MessageBox . Show ( "Дата начала должна быть меньше даты окончания" , "Ошибка" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
2024-03-22 01:29:50 +04:00
return ;
}
try
{
var dataSource = _logic . GetOrders ( new ReportBindingModel
{
2024-03-22 19:04:16 +04:00
DateFrom = dateTimePickerStart . Value ,
DateTo = dateTimePickerEnd . Value
2024-03-22 01:29:50 +04:00
} ) ;
var source = new ReportDataSource ( "DataSetOrders" , dataSource ) ;
reportViewer . LocalReport . DataSources . Clear ( ) ;
reportViewer . LocalReport . DataSources . Add ( source ) ;
2024-03-22 19:04:16 +04:00
var parameters = new [ ] { new ReportParameter ( "ReportParameterPeriod" , $"c{dateTimePickerStart.Value.ToShortDateString()} по {dateTimePickerEnd.Value.ToShortDateString()}" ) } ;
2024-03-22 15:57:40 +04:00
reportViewer . LocalReport . SetParameters ( parameters ) ;
2024-03-22 01:29:50 +04:00
reportViewer . RefreshReport ( ) ;
2024-03-22 19:04:16 +04:00
_logger . LogInformation ( "Загрузка списка заказов на период {From}-{ To}" , dateTimePickerStart . Value . ToShortDateString ( ) , dateTimePickerEnd . Value . ToShortDateString ( ) ) ;
2024-03-22 01:29:50 +04:00
}
catch ( Exception ex )
{
_logger . LogError ( ex , "Ошибка загрузки списка заказов на период" ) ;
MessageBox . Show ( ex . Message , "Ошибка" , MessageBoxButtons . OK ,
MessageBoxIcon . Error ) ;
}
2024-03-22 15:57:40 +04:00
2024-03-22 01:29:50 +04:00
}
2024-03-22 15:57:40 +04:00
private void buttonCreateToPdf_Click ( object sender , EventArgs e )
2024-03-22 01:29:50 +04:00
{
2024-03-22 19:04:16 +04:00
if ( dateTimePickerStart . Value . Date > = dateTimePickerEnd . Value . Date )
2024-03-22 01:29:50 +04:00
{
2024-03-22 15:57:40 +04:00
MessageBox . Show ( "Дата начала должна быть меньше даты окончания" , "Ошибка" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
2024-03-22 01:29:50 +04:00
return ;
}
using var dialog = new SaveFileDialog
{
Filter = "pdf|*.pdf"
} ;
if ( dialog . ShowDialog ( ) = = DialogResult . OK )
{
try
{
_logic . SaveOrdersToPdfFile ( new ReportBindingModel
{
FileName = dialog . FileName ,
2024-03-22 19:04:16 +04:00
DateFrom = dateTimePickerStart . Value ,
DateTo = dateTimePickerEnd . Value
2024-03-22 01:29:50 +04:00
} ) ;
2024-03-22 19:04:16 +04:00
_logger . LogInformation ( "Сохранение списка заказов на период { From} -{ To}" , dateTimePickerStart . Value . ToShortDateString ( ) , dateTimePickerEnd . Value . ToShortDateString ( ) ) ;
2024-03-22 15:57:40 +04:00
MessageBox . Show ( "Выполнено" , "Успех" , MessageBoxButtons . OK , MessageBoxIcon . Information ) ;
2024-03-22 01:29:50 +04:00
}
catch ( Exception ex )
{
_logger . LogError ( ex , "Ошибка сохранения списка заказов на период" ) ;
2024-03-22 15:57:40 +04:00
MessageBox . Show ( ex . Message , "Ошибка" , MessageBoxButtons . OK , MessageBoxIcon . Error ) ;
2024-03-22 01:29:50 +04:00
}
}
}
2024-03-22 15:57:40 +04:00
2024-03-22 01:29:50 +04:00
}
}