171 lines
6.2 KiB
C#
171 lines
6.2 KiB
C#
using HotelContracts.BindingModels;
|
||
using HotelContracts.BusinessLogicsContracts;
|
||
using HotelContracts.SearchModels;
|
||
using HotelDatabaseImplement.Models;
|
||
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 HotelView
|
||
{
|
||
public partial class FormBooking : Form
|
||
{
|
||
|
||
private readonly IBookingLogic _logic;
|
||
private readonly IRoomLogic _logicR;
|
||
private readonly IClientLogic _logicC;
|
||
private int? _id;
|
||
public int Id { set { _id = value; } }
|
||
|
||
public FormBooking(IBookingLogic logic, IRoomLogic logicR, IClientLogic logicC)
|
||
{
|
||
InitializeComponent();
|
||
_logic = logic;
|
||
_logicR = logicR;
|
||
_logicC = logicC;
|
||
}
|
||
|
||
private void buttonSave_Click(object sender, EventArgs e)
|
||
{
|
||
if (comboBoxClient.SelectedValue == null)
|
||
{
|
||
MessageBox.Show("Выберите клиента", "Ошибка",
|
||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
if (comboBoxRoom.SelectedValue == null)
|
||
{
|
||
MessageBox.Show("Выберите комнату", "Ошибка",
|
||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
if(Convert.ToDouble(textBoxTotalHour.Text) < 1)
|
||
{
|
||
MessageBox.Show("Бронь не может быть меньше часа", "Ошибка",
|
||
MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
return;
|
||
}
|
||
try
|
||
{
|
||
var model = new BookingBindingModel
|
||
{
|
||
Id = _id ?? 0,
|
||
RoomId = Convert.ToInt32(comboBoxRoom.SelectedValue),
|
||
ClientId = Convert.ToInt32(comboBoxClient.SelectedValue),
|
||
ArrivalDate = dateTimePickerArriv.Value,
|
||
DepartureDate = dateTimePickerDep.Value,
|
||
NumberHoursSpent = Convert.ToDouble(textBoxTotalHour.Text),
|
||
TotalCost = Convert.ToDouble(textBoxTotalPrice.Text),
|
||
Status = HotelDataModels.Enums.AcceptanceStatus.Неизвестен,
|
||
};
|
||
var operationResult = _id.HasValue ? _logic.Update(model) : _logic.Create(model);
|
||
if (!operationResult)
|
||
{
|
||
throw new Exception("Ошибка при сохранении. Доп информация в логах.");
|
||
}
|
||
MessageBox.Show("Сохранение прошло успешно", "Сообщение", MessageBoxButtons.OK, MessageBoxIcon.Information);
|
||
DialogResult = DialogResult.OK;
|
||
Close();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
private void CalcHour()
|
||
{
|
||
if(dateTimePickerDep.Value.Date != dateTimePickerArriv.Value)
|
||
{
|
||
try
|
||
{
|
||
var start = dateTimePickerArriv.Value;
|
||
var end = dateTimePickerDep.Value;
|
||
var Day = (end.Day - start.Day) * 24;
|
||
var Hour = (end.Hour - start.Hour);
|
||
double Minute = (end.Minute - start.Minute) / 60;
|
||
textBoxTotalHour.Text = (Day + Hour + Minute).ToString();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
|
||
MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
private void CalcSum()
|
||
{
|
||
if(Convert.ToDouble(textBoxTotalHour.Text) >= 1)
|
||
{
|
||
try
|
||
{
|
||
int id = Convert.ToInt32(comboBoxRoom.SelectedValue);
|
||
var roomPrice = _logicR.ReadElement(new RoomSearchModel
|
||
{
|
||
Id
|
||
= id
|
||
});
|
||
var Sum = (roomPrice?.Cost ?? 0)* Convert.ToDouble(textBoxTotalHour.Text);
|
||
textBoxTotalPrice.Text = Sum.ToString();
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK,
|
||
MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
private void textBoxdateTimePickerArriv_TextChanged(object sender, EventArgs e)
|
||
{
|
||
CalcHour();
|
||
CalcSum();
|
||
}
|
||
private void textBoxdateTimePickerDep_TextChanged(object sender, EventArgs e)
|
||
{
|
||
CalcHour();
|
||
CalcSum();
|
||
}
|
||
private void comboBoxClient_SelectedIndexChanged(object sender, EventArgs e)
|
||
{
|
||
CalcHour();
|
||
CalcSum();
|
||
}
|
||
private void buttonCancel_Click(object sender, EventArgs e)
|
||
{
|
||
DialogResult = DialogResult.Cancel;
|
||
Close();
|
||
}
|
||
private void FormBooking_Load(object sender, EventArgs e)
|
||
{
|
||
try
|
||
{
|
||
var roomList = _logicR.ReadList(null);
|
||
var clientList = _logicC.ReadList(null);
|
||
if (roomList != null)
|
||
{
|
||
comboBoxRoom.DisplayMember = "Number";
|
||
comboBoxRoom.ValueMember = "Id";
|
||
comboBoxRoom.DataSource = roomList;
|
||
comboBoxRoom.SelectedItem = null;
|
||
}
|
||
if (clientList != null)
|
||
{
|
||
comboBoxClient.DisplayMember = "Name";
|
||
comboBoxClient.ValueMember = "Id";
|
||
comboBoxClient.DataSource = clientList;
|
||
comboBoxClient.SelectedItem = null;
|
||
}
|
||
}
|
||
catch (Exception ex)
|
||
{
|
||
MessageBox.Show(ex.Message, "Ошибка", MessageBoxButtons.OK, MessageBoxIcon.Error);
|
||
}
|
||
}
|
||
}
|
||
}
|