0% found this document useful (0 votes)
5 views

Engine Code

The document is a Visual Basic .NET application that manages and displays electrical consumption data, including current amperes, payment statistics, and expenses. It connects to an SQLite database to retrieve and update information, and features a user interface with buttons to navigate to different forms. The application includes functionality for real-time updates, drawing graphical representations of data, and logging actions performed by the user.

Uploaded by

bmw15925
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Engine Code

The document is a Visual Basic .NET application that manages and displays electrical consumption data, including current amperes, payment statistics, and expenses. It connects to an SQLite database to retrieve and update information, and features a user interface with buttons to navigate to different forms. The application includes functionality for real-time updates, drawing graphical representations of data, and logging actions performed by the user.

Uploaded by

bmw15925
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 11

Imports System.Data.

SQLite
Imports System.Drawing.Drawing2D
Imports NCalc

Public Class Form1


Private currentAmperes As Integer = 0 ' ‫عدد الأمبيرات الحالي‬

Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load


' ‫ ضبط التوسع التلقائي حسب‬DPI )‫فقط (لم يتغير شيء آخر‬
Me.AutoScaleMode = AutoScaleMode.Dpi

Me.DoubleBuffered = True

' ‫تحديث البيانات من القاعدة عند بدء التشغيل‬


UpdateCurrentAmperes()
LoadPaymentStatistics() ' ‫تحميل إحصائيات الدفع عند بدء التشغيل‬
LoadActions() ' ‫تحديث الأكشنات‬
LoadExpenses() ' ‫تحميل المصروفات عند بدء التشغيل‬

' ‫ إعداد‬Timer
updateTimer.Interval = 3000 ' 3 ‫ثواني‬
updateTimer.Start() ' ‫بدء التحديث التلقائي‬

TextPrice.Text = My.Settings.PricePerAmpere.ToString()
End Sub
Private Sub updateTimer_Tick(sender As Object, e As EventArgs) Handles
updateTimer.Tick
UpdateCurrentAmperes()
LoadPaymentStatistics()
LoadActions()
LoadExpenses()
End Sub
Private Sub Form1_Resize(sender As Object, e As EventArgs) Handles Me.Resize
' )‫إعادة رسم الفورم عند تغيير الحجم (للدائرة والنص‬
Me.Invalidate()
End Sub

Private Sub Guna2GradientButton1_Click(sender As Object, e As EventArgs)


Handles Guna2GradientButton1.Click
Form2.Show()
End Sub

Private Sub Guna2GradientButton3_Click(sender As Object, e As EventArgs)


Handles Guna2GradientButton3.Click
Form3.Show()
End Sub

Private Sub Guna2GradientButton7_Click(sender As Object, e As EventArgs)


Handles Guna2GradientButton7.Click
Me.Show()
End Sub

Private Sub Guna2GradientButton4_Click(sender As Object, e As EventArgs)


Handles Guna2GradientButton4.Click
Form4.Show()
End Sub

Private Sub Guna2GradientButton5_Click(sender As Object, e As EventArgs)


Handles Guna2GradientButton5.Click
Form5.Show()
End Sub

Private Sub UpdateCurrentAmperes()


Using conn As New SQLiteConnection("Data Source=engine.db;Version=3;")
conn.Open()

Dim query As String = "SELECT SUM(field2) AS TotalAmperes FROM


mohanadEngin"
Using cmd As New SQLiteCommand(query, conn)
Dim result = cmd.ExecuteScalar()
If result IsNot DBNull.Value Then
currentAmperes = Convert.ToInt32(result)
Else
currentAmperes = 0
End If
End Using
End Using

Me.Invalidate() ' ‫إعادة رسم الفورم بعد الحصول على القيمة‬


End Sub

Private Sub PanelAmpere_Click(sender As Object, e As EventArgs) Handles


PanelAmpere.Click
Dim currentAmpers As Decimal = My.Settings.Ampers
Dim input As String = InputBox(" ‫ "تحديث‬,":‫أدخل عدد الأمبيرات الكلي الجديد‬
‫"عدد الأمبيرات‬, currentAmpers.ToString())

Dim newAmpers As Decimal


If Decimal.TryParse(input, newAmpers) Then
My.Settings.Ampers = newAmpers
My.Settings.Save()

MessageBox.Show("‫تم تحديث عدد الأمبيرات الكلي إلى‬: " &


newAmpers.ToString())
Me.Invalidate() ' ‫تحديث الرسم بعد التغيير‬
Else
MessageBox.Show("‫يرجى إدخال قيمة رقمية صحيحة‬.")
End If
End Sub

Private Sub Form1_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint


Dim g As Graphics = e.Graphics
g.SmoothingMode = SmoothingMode.AntiAlias

' ‫إحداثيات المركز للدائرة‬


Dim centerX As Integer = Me.ClientSize.Width / 2 + 105 ' ‫تحريك إلى اليمين‬
Dim centerY As Integer = (Me.ClientSize.Height / 2) - 165 ' ‫رفع إلى الأعلى‬
Dim radius As Integer = 100
Dim penWidth As Integer = 15

Dim percentageComplete As Double = currentAmperes / My.Settings.Ampers

' ‫رسم الدائرة الخلفية‬


Dim backPen As New Pen(Color.LightGray, penWidth)
g.DrawEllipse(backPen, centerX - radius, centerY - radius, radius * 2,
radius * 2)

' ‫رسم الدائرة النسبية‬


Dim progressPen As New Pen(Color.DarkOrange, penWidth)
progressPen.StartCap = LineCap.Round
progressPen.EndCap = LineCap.Round
g.DrawArc(progressPen, centerX - radius, centerY - radius, radius * 2,
radius * 2, -90, CSng(-360 * percentageComplete))

' ‫عرض النسبة المئوية داخل الدائرة‬


Dim percentageText As String = $"{Math.Round(percentageComplete * 100)}%"
Using textFont As New Font("Arial", 16, FontStyle.Bold)
Dim textSize As SizeF = g.MeasureString(percentageText, textFont)
g.DrawString(percentageText, textFont, Brushes.Black, centerX -
textSize.Width / 2, centerY - textSize.Height / 2)
End Using

' ‫إضافة النص أسفل الدائرة‬


Dim totalAmperesText As String = $"‫عدد الأمبيرات الكلي‬:
{My.Settings.Ampers}"
Dim currentAmperesText As String = $"‫عدد الأمبيرات المستخدمة‬:
{currentAmperes}"

Using font As New Font("Arial", 12, FontStyle.Bold)


Dim totalSize As SizeF = g.MeasureString(totalAmperesText, font)
Dim currentSize As SizeF = g.MeasureString(currentAmperesText, font)
g.DrawString(totalAmperesText, font, Brushes.Black, centerX -
totalSize.Width / 2, centerY + radius + 30) ' ‫تحت الدائرة‬
g.DrawString(currentAmperesText, font, Brushes.Black, centerX -
currentSize.Width / 2, centerY + radius + 50) ' ‫تحت الكل‬
End Using
End Sub

Private Sub LoadPaymentStatistics()


Using conn As New SQLiteConnection("Data Source=engine.db;Version=3;")
conn.Open()

Dim currentMonthYear As String = DateTime.Now.ToString("yyyy-MM")

' )‫حساب عدد الدافعين للشهر الحالي (عدد المشتركين الذين دفعوا‬
Dim paidCount As Integer
Dim queryPaidCount As String = "SELECT COUNT(DISTINCT SubscriberName)
FROM Payments WHERE IsPaid = 1 AND strftime('%Y-%m', PaymentDate) = @monthYear"

Using cmdPaidCount As New SQLiteCommand(queryPaidCount, conn)


cmdPaidCount.Parameters.AddWithValue("@monthYear",
currentMonthYear)
paidCount = Convert.ToInt32(cmdPaidCount.ExecuteScalar())
End Using

' ‫ حساب عدد المشتركين الكلي من‬mohanadEngin ‫الذين لم يدفعوا للشهر‬


‫الحالي‬
Dim unpaidCount As Integer
Dim queryUnpaidCount As String = "
SELECT COUNT(*) FROM mohanadEngin m
WHERE NOT EXISTS (
SELECT 1 FROM Payments p
WHERE p.SubscriberName = m.field1
AND p.IsPaid = 1
AND strftime('%Y-%m', p.PaymentDate) = @monthYear
)
"

Using cmdUnpaidCount As New SQLiteCommand(queryUnpaidCount, conn)


cmdUnpaidCount.Parameters.AddWithValue("@monthYear",
currentMonthYear)
unpaidCount = Convert.ToInt32(cmdUnpaidCount.ExecuteScalar())
End Using

' ‫تحديث مربعات النص في الواجهة‬


Guna2TextBox1.Text = paidCount.ToString() ' ‫عدد الدافعين‬
Guna2TextBox2.Text = unpaidCount.ToString() ' ‫عدد غير الدافعين‬

' ‫ حساب مجموع الأمبيرات لجميع المشتركين الحاليين في جدول‬mohanadEngin (


‫)للمبلغ المستحق‬
Dim totalAmperes As Decimal
Dim pricePerAmpere As Decimal = My.Settings.PricePerAmpere

Dim queryTotalAmperes As String = "SELECT SUM(field2) FROM


mohanadEngin"

Using cmdTotalAmperes As New SQLiteCommand(queryTotalAmperes, conn)


Dim result = cmdTotalAmperes.ExecuteScalar()
totalAmperes = If(IsDBNull(result), 0, Convert.ToDecimal(result))
End Using

' ‫حساب المبلغ المستحق بناًء على الأمبيرات والسعر لكل أمبير‬
Dim amountDue As Decimal = totalAmperes * pricePerAmpere
Guna2TextBox4.Text = amountDue.ToString("N0") ' ‫تنسيق رقم المبلغ‬

' ‫حساب المبلغ الكلي المستلم من المدفوعات في الشهر الحالي‬


Dim totalReceived As Decimal
Dim queryTotalReceived As String = "SELECT SUM(Amount) FROM Payments
WHERE IsPaid = 1 AND strftime('%Y-%m', PaymentDate) = @monthYear"

Using cmdTotalReceived As New SQLiteCommand(queryTotalReceived, conn)


cmdTotalReceived.Parameters.AddWithValue("@monthYear",
currentMonthYear)
Dim resultReceived = cmdTotalReceived.ExecuteScalar()
totalReceived = If(IsDBNull(resultReceived), 0,
Convert.ToDecimal(resultReceived))
End Using

' )‫ أخرى‬+ ‫ صيانة‬+ ‫حساب مجموع المصروفات الشهرية (وقود‬


Dim totalExpenses As Decimal
Dim currentMonth As Integer = DateTime.Now.Month
Dim currentYear As Integer = DateTime.Now.Year

Dim queryExpenses As String = "SELECT SUM(Fuel + Maintenance + Other)


FROM Expenses WHERE Month = @month AND Year = @year"

Using cmdExpenses As New SQLiteCommand(queryExpenses, conn)


cmdExpenses.Parameters.AddWithValue("@month", currentMonth)
cmdExpenses.Parameters.AddWithValue("@year", currentYear)
Dim resultExpenses = cmdExpenses.ExecuteScalar()
totalExpenses = If(IsDBNull(resultExpenses), 0,
Convert.ToDecimal(resultExpenses))
End Using
' ‫عرض المبلغ الكلي المستلم بعد خصم المصروفات‬
Guna2TextBox3.Text = (totalReceived - totalExpenses).ToString("N0")
End Using
End Sub

Private Sub TextPrice_TextChanged(sender As Object, e As EventArgs) Handles


TextPrice.TextChanged
Dim price As Decimal
If Decimal.TryParse(TextPrice.Text, price) Then
My.Settings.PricePerAmpere = price ' ‫حفظ السعر في الإعدادات‬
My.Settings.Save() ' ‫تأكد من حفظ الإعدادات‬
End If
End Sub

Private Sub Guna2GradientButton6_Click(sender As Object, e As EventArgs)


Handles Guna2GradientButton6.Click
Form6.Show()
End Sub

Private Sub Guna2GradientButton2_Click(sender As Object, e As EventArgs)


Handles Guna2GradientButton2.Click
Form7.Show()
End Sub

Public Sub LogAction(actionType As String, details As String)


Using conn As New SQLiteConnection("Data Source=engine.db;Version=3;")
conn.Open()
Dim query As String = "INSERT INTO Actions (ActionType, Details,
Timestamp) VALUES (@actionType, @details, @timestamp)"
Using cmd As New SQLiteCommand(query, conn)
cmd.Parameters.AddWithValue("@actionType", actionType)
cmd.Parameters.AddWithValue("@details", details)
cmd.Parameters.AddWithValue("@timestamp",
DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"))
cmd.ExecuteNonQuery()
End Using
End Using
End Sub

Private Sub LoadActions()


Using conn As New SQLiteConnection("Data Source=engine.db;Version=3;")
conn.Open()
Dim query As String = "SELECT ActionType, Details, Timestamp FROM
Actions ORDER BY Timestamp DESC"
Using cmd As New SQLiteCommand(query, conn)
Dim dt As New DataTable()
Using reader As SQLiteDataReader = cmd.ExecuteReader()
dt.Load(reader)
End Using

Guna2DataGridView1.DataSource = dt ' ‫تعيين مصدر البيانات لـ‬


DataGridView

' ‫ تنسيق‬DataGridView ‫ليتناسب مع الجداول الأخرى‬


Guna2DataGridView1.Columns(0).HeaderText = "‫"نوع الإجراء‬
Guna2DataGridView1.Columns(1).HeaderText = "‫"التفاصيل‬
Guna2DataGridView1.Columns(2).HeaderText = "‫"التاريخ والوقت‬
' ‫ إعداد خصائص الجدول مثل‬Form4 ‫و غيرها‬
Guna2DataGridView1.RightToLeft = RightToLeft.Yes
Guna2DataGridView1.DefaultCellStyle.Font = New Font("Arial", 12)
Guna2DataGridView1.ColumnHeadersDefaultCellStyle.Font = New
Font("Arial", 12, FontStyle.Bold)
Guna2DataGridView1.ColumnHeadersHeight = 40
Guna2DataGridView1.RowTemplate.Height = 30
End Using
End Using
End Sub

Private Sub LoadExpenses()


Using conn As New SQLiteConnection("Data Source=engine.db;Version=3;")
conn.Open()

Dim currentMonth As Integer = DateTime.Now.Month


Dim currentYear As Integer = DateTime.Now.Year

Dim query As String = "SELECT SUM(Fuel) AS TotalFuel, SUM(Maintenance)


AS TotalMaintenance, SUM(Other) AS TotalOther FROM Expenses WHERE Month = @month
AND Year = @year"

Using cmd As New SQLiteCommand(query, conn)


cmd.Parameters.AddWithValue("@month", currentMonth)
cmd.Parameters.AddWithValue("@year", currentYear)

Using reader As SQLiteDataReader = cmd.ExecuteReader()


If reader.Read() Then
Guna2TextBox5.Text = If(IsDBNull(reader("TotalFuel")), 0,
reader("TotalFuel")).ToString()
Guna2TextBox6.Text =
If(IsDBNull(reader("TotalMaintenance")), 0, reader("TotalMaintenance")).ToString()
Guna2TextBox7.Text = If(IsDBNull(reader("TotalOther")), 0,
reader("TotalOther")).ToString()
End If
End Using
End Using
End Using
End Sub

Private Sub ResetExpensesIfNewMonth()


Dim currentMonth As Integer = DateTime.Now.Month
Dim currentYear As Integer = DateTime.Now.Year

' ‫تحقق مما إذا كان الشهر قد تغير‬


Using conn As New SQLiteConnection("Data Source=engine.db;Version=3;")
conn.Open()

Dim query As String = "SELECT COUNT(*) FROM Expenses WHERE Month =


@month AND Year = @year"
Using cmd As New SQLiteCommand(query, conn)
cmd.Parameters.AddWithValue("@month", currentMonth)
cmd.Parameters.AddWithValue("@year", currentYear)

If Convert.ToInt32(cmd.ExecuteScalar()) = 0 Then
' ‫ إذا‬،‫هنا يمكنك إعادة تعيين المصروفات أو القيام بأي إجراء آخر‬
‫لزم الأمر‬.
End If
End Using
End Using
End Sub

Private Sub Guna2Panel1_Click(sender As Object, e As EventArgs) Handles


Guna2Panel1.Click
Form8.Show()
End Sub

Private Sub Panel2_Paint(sender As Object, e As PaintEventArgs) Handles


Panel2.Paint
Dim g As Graphics = e.Graphics
Dim currentDate As DateTime = DateTime.Now

' ‫الحصول على الشهر والسنة الحالية‬


Dim currentMonth As Integer = currentDate.Month
Dim currentYear As Integer = currentDate.Year
Dim currentDay As Integer = currentDate.Day ' ‫الحصول على اليوم الحالي‬

' ‫رسم عنوان الشهر والسنة‬


Dim monthName As String = currentDate.ToString("MMMM")
Dim yearString As String = currentYear.ToString()
Dim titleFont As New Font("Arial", 16, FontStyle.Bold)
g.DrawString($"{monthName} {yearString}", titleFont, Brushes.Black, 20, 10)

' ‫إعداد شبكة الأيام‬


Dim startDayOfWeekAsInt As Integer =
CInt(New DateTime(currentYear,
currentMonth,
1).DayOfWeek)

Dim daysInMonth =
DateTime.DaysInMonth(currentYear,
currentMonth)

Const cellWidth =
45

Const cellHeight =
45

Const startXOffset =
15

Const startYOffset =
65

For dayIndex =
1 To daysInMonth

' )0=‫تحديد العمود والصف حسب اليوم الأول في الأسبوع (الأحد‬:


Dim columnIndex =
(dayIndex + startDayOfWeekAsInt - 1) Mod 7

Dim rowIndex =
(dayIndex + startDayOfWeekAsInt - 1) \ 7

Dim xPos =
startXOffset + columnIndex * cellWidth

Dim yPos =
startYOffset + rowIndex * cellHeight

' ‫تمييز اليوم الحالي بلون خلفية مميز‬:


If dayIndex =
currentDay Then

g.FillRectangle(Brushes.LightBlue,
xPos,
yPos,
cellWidth,
cellHeight)

End If

g.DrawRectangle(Pens.Black,
xPos,
yPos,
cellWidth,
cellHeight)

g.DrawString(dayIndex.ToString(),
New Font("Arial",
12),
Brushes.Black,
xPos + cellWidth / 3,
yPos + cellHeight / 3)

Next

' ‫أسماء أيام الأسبوع‬:

Dim daysOfWeek() =
{"‫"الأحد‬,
"‫"الإثنين‬,
"‫"الثالثاء‬,
"‫"الأربعاء‬,
"‫"الخميس‬,
"‫"الجمعة‬,
"‫}"السبت‬

For iDayNameIndex =
0 To daysOfWeek.Length - 1

g.DrawString(daysOfWeek(iDayNameIndex),
New Font("Arial",
10,
FontStyle.Bold),
Brushes.Black,
startXOffset + iDayNameIndex * cellWidth + cellWidth \ 5,
startYOffset - 25)

Next

End Sub

Private expression As String = ""

Private Sub NumberButton_Click(sender As Object, e As EventArgs)


Dim button As Guna.UI2.WinForms.Guna2Button =
CType(sender,
Guna.UI2.WinForms.Guna2Button)

expression &= button.Text


Guna2TextBox8.Text =
expression

End Sub

Private Sub OperationButton_Click(sender As Object, e As EventArgs)


Dim button As Guna.UI2.WinForms.Guna2Button =
CType(sender, Guna.UI2.WinForms.Guna2Button)

expression &= " " & button.Text & " "


Guna2TextBox8.Text =
expression

End Sub

Private Sub btnEquals_Click(sender As Object, e As EventArgs) Handles


btnEquals.Click

Try

Dim eMath As Expression =


New Expression(expression)

Dim result As Object =


eMath.Evaluate()

Guna2TextBox8.Text =
result.ToString()

expression =
result.ToString()

Catch ex As Exception

MessageBox.Show("‫خطأ في التعبير‬: " & ex.Message)

End Try
End Sub

Private Sub btnClear_Click(sender As Object, e As EventArgs) Handles


btnClear.Click
expression = ""
Guna2TextBox8.Text = ""
End Sub

Private Sub btnZero_Click(sender As Object, e As EventArgs) Handles


btnZero.Click
NumberButton_Click(sender, e)
End Sub
Private Sub btnOne_Click(sender As Object, e As EventArgs) Handles btnOne.Click
NumberButton_Click(sender, e)
End Sub
Private Sub btnTwo_Click(sender As Object, e As EventArgs) Handles btnTwo.Click
NumberButton_Click(sender, e)
End Sub
Private Sub btnThree_Click(sender As Object, e As EventArgs) Handles
btnThree.Click
NumberButton_Click(sender, e)
End Sub
Private Sub btnFour_Click(sender As Object, e As EventArgs) Handles
btnFour.Click
NumberButton_Click(sender, e)
End Sub
Private Sub btnFive_Click(sender As Object, e As EventArgs) Handles
btnFive.Click
NumberButton_Click(sender, e)
End Sub
Private Sub btnSix_Click(sender As Object, e As EventArgs) Handles btnSix.Click
NumberButton_Click(sender, e)
End Sub
Private Sub btnSeven_Click(sender As Object, e As EventArgs) Handles
btnSeven.Click
NumberButton_Click(sender, e)
End Sub
Private Sub btnEight_Click(sender As Object, e As EventArgs) Handles
btnEight.Click
NumberButton_Click(sender, e)
End Sub
Private Sub btnNine_Click(sender As Object, e As EventArgs) Handles
btnNine.Click
NumberButton_Click(sender, e)
End Sub

Private Sub btnAdd_Click(sender As Object, e As EventArgs) Handles btnAdd.Click


OperationButton_Click(sender, e)
End Sub
Private Sub btnSubtract_Click(sender As Object, e As EventArgs) Handles
btnSubtract.Click
OperationButton_Click(sender, e)
End Sub
Private Sub btnMultiply_Click(sender As Object, e As EventArgs) Handles
btnMultiply.Click
OperationButton_Click(sender, e)
End Sub
Private Sub btnDivide_Click(sender As Object, e As EventArgs) Handles
btnDivide.Click
OperationButton_Click(sender, e)
End Sub

Private Sub btnDeleteOne_Click_1(sender As Object, e As EventArgs) Handles


btnDeleteOne.Click
If expression.Length > 0 Then
expression = expression.Substring(0, expression.Length - 1)
Guna2TextBox8.Text = expression
End If
End Sub

Private Sub Guna2Panel1_Paint(sender As Object, e As PaintEventArgs) Handles


Guna2Panel1.Paint
' ‫ال تغييرات هنا بحسب طلبك‬
End Sub

End Class

You might also like