elfisa-pharmacy/src/ElectronicPharmacy/HelpForms/HF_Registration.cs
Magomed 9ee5561f3d Add invoice export folder setting and CSV export from context menu.
Users can choose the export directory in auth settings; right-click on an invoice saves a UTF-8 CSV into that folder.

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-07-18 13:34:48 +03:00

136 lines
6.0 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using System;
using System.Net.Http;
using System.Windows.Forms;
using Elfisa.UI.Helpers;
using Elfisa.UI.Theming;
using Электроннаяармация.Classes;
using Электроннаяармация.Properties;
namespace Электроннаяармация.HelpForms
{
public partial class HF_Registration : Form
{
public HF_Registration()
{
InitializeComponent();
Shown += (_, __) => WindowChromeHelper.ApplyTitleBarTheme(this, ThemeManager.Colors);
}
string login = Settings.Default.stringLogin.ToString();
string password = Settings.Default.stringPassword.ToString();
string doWeHaveAToken = Settings.Default.stringToken.ToString();
private void btnCloseThis_Click(object sender, EventArgs e)
{
Close();
}
private void btnBrowseExportPath_Click(object sender, EventArgs e)
{
using (var dialog = new FolderBrowserDialog())
{
dialog.Description = "Выберите папку для экспорта накладных";
dialog.ShowNewFolderButton = true;
if (!string.IsNullOrWhiteSpace(txtInvoiceExportPath.Text)
&& System.IO.Directory.Exists(txtInvoiceExportPath.Text.Trim()))
{
dialog.SelectedPath = txtInvoiceExportPath.Text.Trim();
}
if (dialog.ShowDialog(this) == DialogResult.OK)
{
txtInvoiceExportPath.Text = dialog.SelectedPath;
AppConfig.SetInvoiceExportPath(dialog.SelectedPath);
ToastNotification.ShowSuccess("Папка экспорта накладных сохранена");
}
}
}
private async void btnConfirmRegistration_Click(object sender, EventArgs e)
{
btnConfirmRegistration.Enabled = false;
btnCloseThis.Enabled = false;
UseWaitCursor = true;
string strLogin = txtLogin.Text;
string strPassword = txtPassword.Text;
if (strLogin.Length > 0 && strPassword.Length > 0)
{
// Адрес API берём из поля и сохраняем ДО входа: иначе логин уходит
// на старый сохранённый/дефолтный адрес, а не на введённый пользователем.
string apiUrl = txtApiUrl.Text.Trim();
Settings.Default.ApiBaseUrl = apiUrl;
AppConfig.SetInvoiceExportPath(txtInvoiceExportPath.Text);
Settings.Default.Save();
LoginRequest loginRequest = new LoginRequest();
loginRequest.Username = strLogin.Trim();
loginRequest.Password = strPassword.Trim();
var client = new ApiClient(AppConfig.NormalizeApiBaseUrl(apiUrl));
AppDebugLog.Info("Auth", $"Попытка авторизации. Сервер={client.BaseUrl}, login={loginRequest.Username}");
try
{
string token = await client.LoginAsync(loginRequest.Username, loginRequest.Password);
Settings.Default.stringLogin = strLogin;
Settings.Default.stringPassword = strPassword;
Settings.Default.stringToken = token;
Settings.Default.ApiBaseUrl = client.BaseUrl;
AppConfig.SetInvoiceExportPath(txtInvoiceExportPath.Text);
Settings.Default.Save();
AppDebugLog.Info("Auth", $"Авторизация успешна. token={AppDebugLog.MaskToken(token)}");
ToastNotification.ShowSuccess("Авторизация успешно выполнена");
// После входа обязательно запрашиваем Location ID для отправки заказов.
if (!HF_LocationId.PromptAndSave(this))
{
ToastNotification.ShowCustom(
"Location ID не указан. Отправка заказов будет недоступна, пока его не заполните.",
System.Drawing.Color.DarkOrange,
System.Drawing.Color.White);
}
DialogResult = DialogResult.OK;
Close();
return;
}
catch (UnauthorizedAccessException ex)
{
AppDebugLog.Error("Auth", "Ошибка авторизации", ex);
ToastNotification.ShowError($"Ошибка авторизации: {ex.Message}");
}
catch (HttpRequestException ex)
{
AppDebugLog.Error("Auth", "Ошибка сети при авторизации", ex);
ToastNotification.ShowError($"Ошибка сети: {ex.Message}");
}
}
else
{
UiDialogs.ShowError("Есть незаполненные данные. Регистрация программы не пройдена", "Ошибка ввода", this);
}
btnConfirmRegistration.Enabled = true;
btnCloseThis.Enabled = true;
UseWaitCursor = false;
}
private void HF_Registration_Load(object sender, EventArgs e)
{
UiThemeHelper.ApplyToControlTree(this);
WindowChromeHelper.ApplyDialogChrome(this, panel1, label3);
txtLogin.Text = login;
txtPassword.Text = password;
txtApiUrl.Text = string.IsNullOrWhiteSpace(Settings.Default.ApiBaseUrl)
? AppConfig.DefaultApiBaseUrl
: Settings.Default.ApiBaseUrl;
txtInvoiceExportPath.Text = AppConfig.InvoiceExportPath;
checkTokenGained.Checked = doWeHaveAToken.Length > 0;
}
}
}