Imports Store_Managment.Messages

Public Class frmPurchases
#Region "FrmSettings"
    Private MouseDown As Boolean = False
    Private LastLoc As Point
    Private Sub pnlMain_MouseUp(sender As Object, e As MouseEventArgs) Handles pnlMain.MouseUp
        MouseDown = False
        pnlMain.Cursor = Cursors.Default
    End Sub

    Private Sub pnlMain_MouseDown(sender As Object, e As MouseEventArgs) Handles pnlMain.MouseDown
        MouseDown = True
        pnlMain.Cursor = Cursors.NoMove2D
        LastLoc = e.Location
    End Sub

    Private Sub pnlMain_MouseMove(sender As Object, e As MouseEventArgs) Handles pnlMain.MouseMove
        If (MouseDown) Then
            Dim NewX As Integer = (Me.Location.X - LastLoc.X) + e.X
            Dim NewY As Integer = (Me.Location.Y - LastLoc.Y) + e.Y
            Me.Location = New Point(NewX, NewY)
        End If
    End Sub

    Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
        Me.Hide()
    End Sub

    Private Sub btnMinimuz_Click(sender As Object, e As EventArgs) Handles btnMinimuz.Click
        WindowState = FormWindowState.Minimized
    End Sub
#End Region


    Private Sub frmPurchases_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Me.Icon = frmMain.Icon
        dgvInvoicePurchases.DataSource = DB.GetData("Select * from Purchases")
        dgvInvoicePurchases.ClearSelection()
        ClearPurchasesNo()

    End Sub

    Private Sub ClearPurchasesNo()
        txtInvoiceNumber.Text = DB.GetData("select max(Invoice_Number)+1 from Purchases").Rows(0)(0).ToString()
        If (txtInvoiceNumber.Text.Trim() = "") Then txtInvoiceNumber.Text = "1"
        txtItemNumber.Clear()
        txtItemNumber.Select()
        txtGetItemName.Clear()
        txtItemBuyPrice.Text = "0"
        txtTotalPrice.Text = "0"
        txtPurchasesQuantity.Text = "0"
        tsAdd.Enabled = True
        tsDelete.Enabled = False
        tsEdit.Enabled = False
    End Sub


    Private Sub txtItemNumber_TextChanged(sender As Object, e As EventArgs) Handles txtItemNumber.TextChanged
        Try
            If (Not String.IsNullOrEmpty(txtItemNumber.Text)) Then
                Dim tbl As DataTable = DB.GetData("Select Item_Name , Buy_Price from Item Where Item_Number =" + txtItemNumber.Text)
                If (tbl.Rows.Count >= 1) Then
                    txtGetItemName.Text = tbl.Rows(0)(0).ToString()
                    txtItemBuyPrice.Text = tbl.Rows(0)(1).ToString()
                    txtTotalPrice.Text = "0"
                    txtPurchasesQuantity.Text = "0"
                Else
                    Messages.MsgInformation("رقم الصنف غير موجود")
                    ClearPurchasesNo()
                End If
            Else
                ClearPurchasesNo()
            End If

        Catch ex As Exception
            MsgError(ex.Message)
        End Try
    End Sub


    Private Sub txtPurchasesQuantity_TextChanged(sender As Object, e As EventArgs) Handles txtPurchasesQuantity.TextChanged
        If (txtPurchasesQuantity.Text = "") Then txtPurchasesQuantity.Text = "0"

        If (Not String.IsNullOrEmpty(txtItemBuyPrice.Text)) Then
            Dim BuyPrice As Integer
            BuyPrice = Convert.ToInt32(txtItemBuyPrice.Text)
            Dim PurchasesQuantity As Integer
            PurchasesQuantity = Convert.ToInt32(txtPurchasesQuantity.Text)
            Dim TotalPrice As Integer
            TotalPrice = Convert.ToInt32(txtItemBuyPrice.Text) * Convert.ToInt32(txtPurchasesQuantity.Text)
            txtTotalPrice.Text = TotalPrice
        End If

    End Sub

    Private Sub txtItemNumber_KeyPress(sender As Object, e As KeyPressEventArgs) Handles txtItemNumber.KeyPress, txtTotalPrice.KeyPress, txtPurchasesQuantity.KeyPress, txtItemBuyPrice.KeyPress
        If (Not Char.IsDigit(e.KeyChar) And Not Char.IsControl(e.KeyChar)) Then e.Handled = True
    End Sub


    Private Sub dgvInvoicePurchases_SelectionChanged(sender As Object, e As EventArgs) Handles dgvInvoicePurchases.SelectionChanged
        tsAdd.Enabled = False
        tsEdit.Enabled = True
        tsDelete.Enabled = True
        If (dgvInvoicePurchases.SelectedRows.Count > 0) Then
            txtInvoiceNumber.Text = dgvInvoicePurchases.SelectedRows(0).Cells(0).Value.ToString()
            txtItemNumber.Text = dgvInvoicePurchases.SelectedRows(0).Cells(1).Value.ToString()
            txtPurchasesQuantity.Text = dgvInvoicePurchases.SelectedRows(0).Cells(2).Value.ToString()
            txtTotalPrice.Text = dgvInvoicePurchases.SelectedRows(0).Cells(3).Value.ToString()
            dtpOperationDate.Value = dgvInvoicePurchases.SelectedRows(0).Cells(4).Value.ToString()
        End If
    End Sub


    Private Sub tsNew_Click(sender As Object, e As EventArgs) Handles tsNew.Click
        dgvInvoicePurchases.ClearSelection()
        ClearPurchasesNo()
    End Sub


    Private Sub tsAdd_Click(sender As Object, e As EventArgs) Handles tsAdd.Click
        If (txtInvoiceNumber.Text = "") Then
            MsgError("حدث خطأ ما سيتم إصلاحة وإذا استمر العطل من فضلك تواصل مع المبرمج")
            tsNew.PerformClick()
        ElseIf (txtItemNumber.Text = "") Then
            MsgInformation("من فضلك قم بإدخال رقم الصنف")
        ElseIf (txtGetItemName.Text = "") Then
            MsgError("يوجد خلل ما في البيانات سيتم إيقاف العملية من فضلك ابدأ من جديد")
            tsNew.PerformClick()
        ElseIf (txtPurchasesQuantity.Text = "" Or txtPurchasesQuantity.Text = "0") Then
            MsgInformation("من فضلك قم بإدخال الكمية")
            txtPurchasesQuantity.Select()
        ElseIf (txtTotalPrice.Text = "" Or txtTotalPrice.Text = "0") Then
            MsgInformation("لا يمكنك ترك اجمالي المشتريات فارغ")
            txtTotalPrice.Select()
        Else
            DB.Run("Insert Into Purchases Values(" + txtInvoiceNumber.Text + "," + txtItemNumber.Text + "," + txtPurchasesQuantity.Text + "," + txtTotalPrice.Text + ",'" + dtpOperationDate.Value.ToShortDateString() + "')")
            DB.Run("Update Item Set Item_Quantity = Item_Quantity + " + txtPurchasesQuantity.Text + " where Item_Number = " + txtItemNumber.Text)
            MsgInformation("تم عملية الشراء بنجاح")
            dgvInvoicePurchases.DataSource = DB.GetData("Select * from Purchases")
            dgvInvoicePurchases.ClearSelection()
            ClearPurchasesNo()
        End If
    End Sub

    Private Sub tsEdit_Click(sender As Object, e As EventArgs) Handles tsEdit.Click
        If (txtInvoiceNumber.Text = "") Then
            MsgError("حدث خطأ ما سيتم إصلاحة وإذا استمر العطل من فضلك تواصل مع المبرمج")
            tsNew.PerformClick()
        ElseIf (txtItemNumber.Text = "") Then
            MsgInformation("من فضلك قم بإدخال رقم الصنف")
        ElseIf (txtGetItemName.Text = "") Then
            MsgError("يوجد خلل ما في البيانات سيتم إيقاف العملية من فضلك ابدأ من جديد")
            tsNew.PerformClick()
        ElseIf (txtPurchasesQuantity.Text = "" Or txtPurchasesQuantity.Text = "0") Then
            MsgInformation("من فضلك قم بإدخال الكمية")
            txtPurchasesQuantity.Select()
        ElseIf (txtTotalPrice.Text = "" Or txtTotalPrice.Text = "0") Then
            MsgInformation("لا يمكنك ترك اجمالي المشتريات فارغ")
            txtTotalPrice.Select()
        Else
            'DB.Run("Update Purchases Set Invoice_Number=" + txtInvoiceNumber.Text + ", Item_Number=" + txtItemNumber.Text + ", purchased_Quantity=" + txtPurchasesQuantity.Text + ", Total_Price=" + txtTotalPrice.Text + ", Operation_Date='" + dtpOperationDate.Value.ToShortDateString() + "' where Invoice_Number=" + txtInvoiceNumber.Text)
            'tsDelete.PerformClick()
            'tsAdd.PerformClick()
            Dim tbl As DataTable = DB.GetData("Select purchased_Quantity from Purchases where Invoice_Number = " + txtInvoiceNumber.Text + " and Item_Number=" + txtItemNumber.Text)
            DB.Run("Update Item Set Item_Quantity = Item_Quantity - " + tbl.Rows(0)(0).ToString() + " where Item_Number = " + txtItemNumber.Text)
            DB.Run("Delete from Purchases Where Invoice_Number=" + txtInvoiceNumber.Text)
            DB.Run("Insert Into Purchases Values(" + txtInvoiceNumber.Text + "," + txtItemNumber.Text + "," + txtPurchasesQuantity.Text + "," + txtTotalPrice.Text + ",'" + dtpOperationDate.Value.ToShortDateString() + "')")
            DB.Run("Update Item Set Item_Quantity = Item_Quantity + " + txtPurchasesQuantity.Text + " where Item_Number = " + txtItemNumber.Text)
            MsgInformation("تم عملية التعديل بنجاح")
            dgvInvoicePurchases.DataSource = DB.GetData("Select * from Purchases")
            dgvInvoicePurchases.ClearSelection()
            ClearPurchasesNo()
        End If

    End Sub

    Private Sub tsDelete_Click(sender As Object, e As EventArgs) Handles tsDelete.Click
        Dim Delete As DialogResult
        Delete = MessageBox.Show("هل متأكد من استمرار عملية الحذف؟", "حذف الفاتورة", MessageBoxButtons.YesNo, MessageBoxIcon.Question, MessageBoxDefaultButton.Button2, MessageBoxOptions.RtlReading)
        If (Delete = DialogResult.Yes) Then
            DB.Run("Update Item Set Item_Quantity = Item_Quantity - " + txtPurchasesQuantity.Text + " where Item_Number = " + txtItemNumber.Text)
            DB.Run("Delete from Purchases Where Invoice_Number=" + txtInvoiceNumber.Text)
            MsgInformation("تم حذف العملية بنجاح")
            dgvInvoicePurchases.DataSource = DB.GetData("Select * from Purchases")
            dgvInvoicePurchases.ClearSelection()
            ClearPurchasesNo()
        Else
            Return
        End If
    End Sub


End Class