hesabixBox/Form1.vb

242 lines
9.8 KiB
VB.net
Raw Permalink Normal View History

2025-10-02 19:12:22 +03:30
Imports System.Text
Imports System.Threading
Imports System.Windows.Forms.VisualStyles.VisualStyleElement
Imports Microsoft.VisualBasic.FileIO
Imports System.Threading.Tasks
Public Class Form1
Dim api As New ApiInterface
Public isLogin As Boolean = False
Private isLogVisible As Boolean = False
Private isProcessingPrint As Boolean = False ' نشانگر وضعیت پردازش چاپ
Private Sub Form1_Resize(ByVal sender As Object, ByVal e As EventArgs) Handles MyBase.Resize
If WindowState = FormWindowState.Minimized Then
NotifyIcon1.Visible = True
Hide()
NotifyIcon1.ShowBalloonTip(100)
End If
End Sub
Private Sub NotifyIcon1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles NotifyIcon1.DoubleClick
Me.Show()
Me.WindowState = FormWindowState.Normal
NotifyIcon1.Visible = False
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
MenuStrip1.Enabled = False
' تنظیم سیستم لاگ
SetupLogSystem()
Dim FrmLogin As New FrmLogin
FrmLogin.StartPosition = FormStartPosition.CenterScreen
FrmLogin.MdiParent = Me
FrmLogin.Show()
End Sub
Private Sub btnCloseApplication_Click(sender As Object, e As EventArgs) Handles btnCloseApplication.Click
Application.Exit()
End Sub
Private Sub btnPrinters_Click(sender As Object, e As EventArgs) Handles btnPrinters.Click
Dim frm As New FrmPrinters
frm.StartPosition = FormStartPosition.CenterScreen
frm.MdiParent = Me
frm.Show()
End Sub
Private Sub btnShowLog_Click(sender As Object, e As EventArgs) Handles btnShowLog.Click
ToggleLogVisibility()
End Sub
Private Sub دربارهبرنامهToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles دربارهبرنامهToolStripMenuItem.Click
Dim frm As New FrmAboutHesabix
frm.StartPosition = FormStartPosition.CenterScreen
frm.MdiParent = Me
frm.Show()
End Sub
Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
' جلوگیری از اجرای همزمان چندین عملیات چاپ
If isProcessingPrint Then
Return
End If
' اجرای عملیات چاپ در thread جداگانه تا UI مسدود نشود
AsyncHelper.RunAsync(Function() ProcessPrintingAsyncTask())
End Sub
Private Async Function ProcessPrintingAsyncTask() As Task
Try
' تنظیم نشانگر وضعیت
isProcessingPrint = True
' دریافت آخرین چاپ به صورت async با retry
Dim file As String = ""
Dim success As Boolean = Await AsyncHelper.RunWithRetry(
Async Function()
file = Await api.getLastPrintAsync()
End Function, 3, 2000) ' 3 تلاش با تاخیر 2 ثانیه
If Not success OrElse String.IsNullOrEmpty(file) Then
Return
End If
Dim SPrinter As String = ""
Dim res() As String = file.Split(",")
Dim canPrint As Boolean = False
If file <> "" Then
If (res(0) = "fastSellPosInvoice" And My.Settings.printerFastSellInvoice <> "Off") Then
SPrinter = My.Settings.printerFastSellInvoice
canPrint = True
ElseIf res(0) = "fastSellCashdesk" And My.Settings.printerFastSellCashdeskInvoice <> "Off" Then
SPrinter = My.Settings.printerFastSellCashdeskInvoice
canPrint = True
ElseIf res(0) = "fastSellInvoice" And My.Settings.printerSell <> "Off" Then
SPrinter = My.Settings.printerSell
canPrint = True
End If
If canPrint AndAlso SPrinter <> "" Then
' دانلود فایل به صورت async با retry
Dim downloadSuccess As Boolean = Await AsyncHelper.RunWithRetry(
Async Function()
Return Await api.downloadFileAsync(res(1), res(0))
End Function, 2, 3000) ' 2 تلاش با تاخیر 3 ثانیه
If downloadSuccess Then
' اجرای چاپ در UI thread
AsyncHelper.RunOnUIThread(Me, Sub()
Dim sReport = SpecialDirectories.CurrentUserApplicationData.ToString + "\" + res(0) + ".pdf"
Try
Using document = PdfiumViewer.PdfDocument.Load(sReport)
Using printDocument = document.CreatePrintDocument()
printDocument.PrinterSettings = New System.Drawing.Printing.PrinterSettings() With {.PrinterName = SPrinter}
printDocument.PrintController = New System.Drawing.Printing.StandardPrintController()
printDocument.Print()
End Using
End Using
Catch ex As Exception
Logger.Instance.LogError("خطا در چاپ مستقیم PDF", ex)
End Try
End Sub)
End If
End If
End If
Catch ex As Exception
' ثبت خطا در لاگ
Logger.Instance.LogError("خطا در فرآیند چاپ", ex)
Finally
' بازنشانی نشانگر وضعیت
isProcessingPrint = False
End Try
End Function
Private Sub btnHideWindow_Click(sender As Object, e As EventArgs) Handles btnHideWindow.Click
Me.WindowState = FormWindowState.Minimized
End Sub
' متدهای سیستم لاگ
Private Sub SetupLogSystem()
' تنظیم layout پنل لاگ
LogPanel.Dock = DockStyle.Bottom
LogPanel.Height = 200
LogPanel.Visible = False
' تنظیم layout کنترل‌ها
LogLabel.Dock = DockStyle.Top
LogLabel.Height = 25
LogLabel.Text = "لاگ سیستم - درخواست‌ها و پاسخ‌های سرور"
LogLabel.TextAlign = ContentAlignment.MiddleCenter
LogControlsPanel.Dock = DockStyle.Bottom
LogControlsPanel.Height = 35
LogTextBox.Dock = DockStyle.Fill
LogTextBox.Multiline = True
LogTextBox.WordWrap = True
' تنظیم layout دکمه‌ها
ClearLogButton.Dock = DockStyle.Right
ClearLogButton.Width = 80
ClearLogButton.Text = "پاک کردن"
SaveLogButton.Dock = DockStyle.Right
SaveLogButton.Width = 80
SaveLogButton.Text = "ذخیره"
ToggleLogButton.Dock = DockStyle.Right
ToggleLogButton.Width = 80
ToggleLogButton.Text = "مخفی کردن"
' تنظیم Logger
Logger.Instance.SetLogTextBox(LogTextBox)
' اضافه کردن event handlers
AddHandler ClearLogButton.Click, AddressOf ClearLogButton_Click
AddHandler SaveLogButton.Click, AddressOf SaveLogButton_Click
AddHandler ToggleLogButton.Click, AddressOf ToggleLogButton_Click
' اضافه کردن منوی لاگ
AddLogMenuItems()
Logger.Instance.LogInfo("سیستم لاگ راه‌اندازی شد")
End Sub
Private Sub AddLogMenuItems()
' تنظیم متن دکمه نمایش لاگ
btnShowLog.Text = "نمایش لاگ سیستم"
' اضافه کردن منوی لاگ اضافی
Dim logMenuItem As New ToolStripMenuItem("مدیریت لاگ")
logMenuItem.DropDownItems.Add("پاک کردن لاگ", Nothing, AddressOf ClearLogMenuItem_Click)
logMenuItem.DropDownItems.Add("ذخیره لاگ", Nothing, AddressOf SaveLogMenuItem_Click)
تنظیماتToolStripMenuItem.DropDownItems.Add(logMenuItem)
End Sub
Private Sub ToggleLogButton_Click(sender As Object, e As EventArgs)
ToggleLogVisibility()
End Sub
Private Sub ClearLogButton_Click(sender As Object, e As EventArgs)
Logger.Instance.ClearLog()
End Sub
Private Sub SaveLogButton_Click(sender As Object, e As EventArgs)
Logger.Instance.SaveLogToFile()
End Sub
Private Sub ToggleLogMenuItem_Click(sender As Object, e As EventArgs)
ToggleLogVisibility()
End Sub
Private Sub ClearLogMenuItem_Click(sender As Object, e As EventArgs)
Logger.Instance.ClearLog()
End Sub
Private Sub SaveLogMenuItem_Click(sender As Object, e As EventArgs)
Logger.Instance.SaveLogToFile()
End Sub
Private Sub ToggleLogVisibility()
isLogVisible = Not isLogVisible
LogPanel.Visible = isLogVisible
If isLogVisible Then
ToggleLogButton.Text = "مخفی کردن"
btnShowLog.Text = "مخفی کردن لاگ"
Logger.Instance.LogInfo("پنل لاگ نمایش داده شد")
Else
ToggleLogButton.Text = "نمایش لاگ"
btnShowLog.Text = "نمایش لاگ سیستم"
Logger.Instance.LogInfo("پنل لاگ مخفی شد")
End If
End Sub
Private Sub ToolStripStatusLabel1_Click(sender As Object, e As EventArgs) Handles ToolStripStatusLabel1.Click
End Sub
End Class