Option Explicit On
Option Strict On
Imports GAF
Imports GAF.Biz.Accounting
Imports System.Transactions
Imports System.Drawing.Printing
Public Class frmPO
Dim db As New dbMpiBizDataContext()
Dim CreditTerm As String = ""
Dim ICompany As IQueryable(Of Company)
Dim IUnit As IQueryable(Of Unit)
Dim IPurchaseRequisition As IOrderedQueryable(Of PurchaseRequisition)
Dim anyData() As String
Dim QuantityPerUnit As Integer = 0
Dim CurrentProduct(8) As String
Dim sw As New Stopwatch()
Dim ProcessTime As Double
Dim _ToolStripStatusLabel As New ToolStripStatusLabel()
Dim UseFont As New Font("MS Sans Serif", 10)
Dim prDoc As New PrintDocument()
Dim prDlg As New PrintDialog()
Public Sub New(ByRef refToolStripStatusLabel As ToolStripStatusLabel)
InitializeComponent()
_ToolStripStatusLabel = refToolStripStatusLabel
End Sub
Private Sub frmPO_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Me.WindowState = FormWindowState.Maximized
sw.Reset()
sw.Start()
With lsvProductLists
.Columns.Add("รหัสสินค้า", 70, HorizontalAlignment.Left)
.Columns.Add("ชื่อสินค้า", 350, HorizontalAlignment.Left)
.Columns.Add("ต้นทุนต่อชิ้น", 90, HorizontalAlignment.Right)
.Columns.Add("จำนวน", 50, HorizontalAlignment.Right)
.Columns.Add("หน่วย", 135, HorizontalAlignment.Left)
.Columns.Add("รวมเป็นเงิน", 75, HorizontalAlignment.Right)
.Columns.Add("", 0, HorizontalAlignment.Right)
.Columns.Add("", 0, HorizontalAlignment.Right)
.Columns.Add("", 0, HorizontalAlignment.Right)
.View = View.Details
.GridLines = True
End With
ICompany = From c In db.Companies Where c.CompanyID <> "000"
If ICompany.Count > 0 Then
With cboCompany
.BeginUpdate()
.DisplayMember = "CompanyName"
.ValueMember = "CompanyID"
.DataSource = ICompany.ToList()
.EndUpdate()
End With
End If
RemoveHandler cboUnit.SelectedValueChanged, AddressOf cboUnit_SelectedValueChanged
IUnit = From u In db.Units
If IUnit.Count > 0 Then
With cboUnit
.BeginUpdate()
.DisplayMember = "UnitName"
.ValueMember = "UnitID"
.DataSource = IUnit.ToList()
.EndUpdate()
End With
End If
AddHandler cboUnit.SelectedValueChanged, AddressOf cboUnit_SelectedValueChanged
cmdSave.Enabled = False
dtpStart.Value = DateTime.Today.AddMonths(-1)
dtpEnd.Value = DateTime.Today
txtDCCash.ContextMenu = New ContextMenu()
txtDcDateDiscountReceivedCreditTerm.ContextMenu = New ContextMenu()
txtDcRateDiscountReceivedCreditTerm.ContextMenu = New ContextMenu()
txtCreditDateDiscountReceivedCreditTerm.ContextMenu = New ContextMenu()
cboCompany.ContextMenu = New ContextMenu()
cboUnit.ContextMenu = New ContextMenu()
txtLastCost.ContextMenu = New ContextMenu()
txtAmount.ContextMenu = New ContextMenu()
CheckCreditTerm()
txtDCCash.Text = "0"
ClearProduct()
ClearPODetails()
ClearNet()
AddHandler prDoc.PrintPage, New PrintPageEventHandler(AddressOf Me.StringToPrint_Print)
prDoc.DocumentName = "สั่งซื้อสินค้า"
prDlg.Document = prDoc
sw.Stop()
ProcessTime = sw.ElapsedMilliseconds / 1000
_ToolStripStatusLabel.Text = "เวลาที่ใช้ : " & ProcessTime.ToString("0.0000") & " วินาที"
End Sub
Private Sub ClearProduct()
lblProductID.Text = ""
lblProductName.Text = ""
txtLastCost.Text = "1"
txtAmount.Text = "1"
QuantityPerUnit = 0
lblProductTotal.Text = "0"
Array.Clear(CurrentProduct, 0, CurrentProduct.Length)
End Sub
Private Sub ClearPODetails()
txtPONo.Text = ""
dtpReceiveDate.Value = DateTime.Today
dtpPaidDate.Value = DateTime.Today
End Sub
Private Sub ClearCreditTerm()
CreditTerm = ""
txtDCCash.Text = "0"
txtDcDateDiscountReceivedCreditTerm.Text = "0"
txtDcRateDiscountReceivedCreditTerm.Text = "0"
txtCreditDateDiscountReceivedCreditTerm.Text = "0"
End Sub
Private Sub ClearNet()
lblTotal.Text = "0.0"
lblDiscountReceived.Text = "0.0"
lblInputVAT.Text = "0.0"
lblNet.Text = "0.0"
lblNetInText.Text = ""
End Sub
Private Sub cboCompany_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboCompany.SelectedValueChanged
ClearProduct()
ClearPODetails()
ClearNet()
dgvProductLists.DataSource = Nothing
dgvPRList.DataSource = Nothing
dgvPRDList.DataSource = Nothing
lsvProductLists.Items.Clear()
CheckCreditTerm()
If CreditTerm = "" Then Exit Sub
If CreditTerm.ToLower() <> "c" Then
Dim ct As New CreditTerm(CreditTerm) 'Error บรรทัดนี้
optCredit.Checked = True
txtDcDateDiscountReceivedCreditTerm.Text = ct.GetDcDate.ToString()
txtDcRateDiscountReceivedCreditTerm.Text = ct.GetDcRate.ToString()
txtCreditDateDiscountReceivedCreditTerm.Text = ct.GetCreditDate.ToString()
dtpPaidDate.Value = DateTime.Today.AddDays(Double.Parse(txtCreditDateDiscountReceivedCreditTerm.Text)).NextWorkingDay(HolidayMode.SundayOnly)
Else
optCash.Checked = True
txtDCCash.Focus()
txtDCCash.SelectAll()
End If
txtSearch.Focus()
txtSearch.SelectAll()
End Sub
Private Sub CheckCreditTerm()
If cboCompany.SelectedValue IsNot Nothing Then
For Each c In ICompany
If c.CompanyID = cboCompany.SelectedValue.ToString() Then
CreditTerm = c.DiscountReceivedCreditTerm
Exit For
End If
Next
End If
End Sub
Private Sub txtSearch_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSearch.KeyDown
If txtSearch.Text.Trim() = "" Then Exit Sub
If e.KeyCode = Keys.Enter Then
sw.Reset()
sw.Start()
Dim ps = From p In db.Products _
Where p.ProductID.Contains(txtSearch.Text.Trim()) _
OrElse p.ProductNameTH.Contains(txtSearch.Text.Trim()) _
AndAlso p.ProductTypeID <> "2" _
AndAlso p.IsSaleMix = "0" _
AndAlso p.CompanyID = cboCompany.SelectedValue.ToString() _
Order By p.ProductNameTH _
Select New With { _
.ProductID = p.ProductID, _
.ProductName = p.ProductNameTH, _
.LastCost = p.LastCost _
}
If ps.Count() > 0 Then
dgvProductLists.DataSource = ps.ToList()
FormatDgvProductLists()
Else
dgvProductLists.DataSource = Nothing
MessageBox.Show("เงื่อนไขที่คุณป้อน ไม่ตรงกับรายการสินค้าที่มีอยู่ !!!", "ผลการค้นหา", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtSearch.Focus()
txtSearch.SelectAll()
End If
sw.Stop()
ProcessTime = sw.ElapsedMilliseconds / 1000
_ToolStripStatusLabel.Text = "เวลาที่ใช้ : " & ProcessTime.ToString("0.0000") & " วินาที"
End If
End Sub
Private Sub FormatDgvProductLists()
With dgvProductLists
If .RowCount > 0 Then
.Columns(0).HeaderText = "รหัสสินค้า"
.Columns(1).HeaderText = "ชื่อสินค้า"
.Columns(2).HeaderText = "ต้นทุนสินค้าต่อชิ้น"
.Columns(0).Width = 100
.Columns(1).Width = 280
.Columns(2).Width = 115
End If
End With
End Sub
Private Sub dgvProductLists_CellMouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvProductLists.CellMouseUp
If e.RowIndex = -1 Then Exit Sub
With dgvProductLists
lblProductID.Text = .Rows.Item(e.RowIndex).Cells("ProductID").Value.ToString()
lblProductName.Text = .Rows.Item(e.RowIndex).Cells("ProductNameTH").Value.ToString()
txtLastCost.Text = .Rows.Item(e.RowIndex).Cells("LastCost").Value.ToString()
txtLastCost.Focus()
txtLastCost.SelectAll()
End With
CalculateTotal()
End Sub
Private Sub cboUnit_SelectedValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cboUnit.SelectedValueChanged
CalculateTotal()
End Sub
Private Sub CalculateTotal()
If txtLastCost.Text.Trim() = "" Then Exit Sub
If txtAmount.Text.Trim() = "" Then Exit Sub
For Each u In IUnit
If u.UnitID = cboUnit.SelectedValue.ToString() Then
QuantityPerUnit = Convert.ToInt32(u.QuantityPerUnit)
Exit For
End If
Next
lblProductTotal.Text = (Integer.Parse(txtLastCost.Text) * Integer.Parse(txtAmount.Text) * QuantityPerUnit).ToString()
End Sub
Private Sub dgvProductLists_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvProductLists.CellFormatting
If dgvProductLists.Columns(e.ColumnIndex).Name = "LastCost" Then
e.CellStyle.Format = "#,##0"
e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleRight
End If
End Sub
Private Sub cmdAddProduct_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAddProduct.Click
If (lblProductID.Text.Trim() = "") OrElse (lblProductName.Text.Trim() = "") Then
txtSearch.Focus()
txtSearch.SelectAll()
Exit Sub
End If
If Double.Parse(txtLastCost.Text) = 0.0 Then
txtSearch.Focus()
txtSearch.SelectAll()
Exit Sub
End If
If CInt(txtAmount.Text) = 0 Then
txtAmount.Focus()
txtAmount.SelectAll()
Exit Sub
End If
Dim i As Integer = 0
Dim lvi As ListViewItem
Dim ProductID As String = ""
For i = 0 To lsvProductLists.Items.Count - 1
ProductID = lsvProductLists.Items(i).SubItems(0).Text
If lblProductID.Text.Trim() = ProductID Then
MessageBox.Show("คุณเลือกรายการสินค้า ซ้ำกัน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
ClearProduct()
txtSearch.Focus()
Exit Sub
End If
Next
For Each u In IUnit
If u.UnitID = cboUnit.SelectedValue.ToString() Then
QuantityPerUnit = Convert.ToInt32(u.QuantityPerUnit)
Exit For
End If
Next
anyData = New String() { _
lblProductID.Text.Trim(), _
lblProductName.Text, _
txtLastCost.Text, _
txtAmount.Text, _
cboUnit.Text, _
lblProductTotal.Text, _
cboUnit.SelectedValue.ToString(), _
" ", _
QuantityPerUnit.ToString() _
}
lvi = New ListViewItem(anyData)
lsvProductLists.Items.Add(lvi)
ClearProduct()
CalculateNet()
cmdSave.Enabled = True
Array.Clear(CurrentProduct, 0, CurrentProduct.Length)
End Sub
Private Sub cmdClearProduct_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClearProduct.Click
ClearProduct()
txtSearch.Focus()
txtSearch.SelectAll()
End Sub
Private Sub dgvProductLists_CellMouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvProductLists.CellMouseDown
If e.RowIndex = -1 Then Exit Sub
For Each u In IUnit
If u.UnitID = cboUnit.SelectedValue.ToString() Then
QuantityPerUnit = Convert.ToInt32(u.QuantityPerUnit)
Exit For
End If
Next
With dgvProductLists
CurrentProduct(0) = .Rows.Item(e.RowIndex).Cells("ProductID").Value.ToString()
CurrentProduct(1) = .Rows.Item(e.RowIndex).Cells("ProductNameTH").Value.ToString()
CurrentProduct(2) = .Rows.Item(e.RowIndex).Cells("LastCost").Value.ToString()
CurrentProduct(3) = txtAmount.Text
CurrentProduct(4) = cboUnit.Text
CurrentProduct(5) = (Integer.Parse(CurrentProduct(2)) * Integer.Parse(CurrentProduct(3)) * QuantityPerUnit).ToString()
CurrentProduct(6) = cboUnit.SelectedValue.ToString()
CurrentProduct(7) = ""
CurrentProduct(8) = QuantityPerUnit.ToString()
End With
End Sub
Private Sub dgvProductLists_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvProductLists.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim AnyData As DataObject
AnyData = New DataObject()
AnyData.SetData(DataFormats.Text, CurrentProduct)
Dim dde As DragDropEffects
dde = dgvProductLists.DoDragDrop(AnyData, DragDropEffects.Copy)
End If
End Sub
Private Sub lsvProductLists_DragEnter(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lsvProductLists.DragEnter
If (CurrentProduct(0) = "") OrElse (CurrentProduct(1) = "") Then Exit Sub
If (e.Data.GetDataPresent(DataFormats.Text, True)) Then
e.Effect = DragDropEffects.Copy
End If
End Sub
Private Sub lsvProductLists_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles lsvProductLists.DragDrop
CurrentProduct = DirectCast(e.Data.GetData(DataFormats.Text, True), String())
Dim i As Integer = 0
Dim lvi As ListViewItem
Dim ProductID As String = ""
For i = 0 To lsvProductLists.Items.Count - 1
ProductID = lsvProductLists.Items(i).SubItems(0).Text
If CurrentProduct(0) = ProductID Then
MessageBox.Show("คุณเลือกรายการสินค้า ซ้ำกัน !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
Exit Sub
End If
Next
lvi = New ListViewItem(CurrentProduct)
lsvProductLists.Items.Add(lvi)
CalculateNet()
cmdSave.Enabled = True
Array.Clear(CurrentProduct, 0, CurrentProduct.Length)
End Sub
Private Sub CalculateNet()
Dim i As Integer
Dim Total As Double = 0.0
Dim DiscountReceived As Double = 0.0
Dim VATRate As Double = BizConfig.VATRate / 100
With lsvProductLists
For i = 0 To .Items.Count - 1
Total += Double.Parse(lsvProductLists.Items(i).SubItems(5).Text)
Next
End With
If optCash.Checked = True Then
DiscountReceived = Double.Parse(txtDCCash.Text) / 100
Else
DiscountReceived = 0.0
End If
lblTotal.Text = Total.ToString("#,##0")
lblDiscountReceived.Text = (Double.Parse(lblTotal.Text) * DiscountReceived).ToString("#,##0.00")
Dim TotalBeforeVAT = Double.Parse(lblTotal.Text) - Double.Parse(lblDiscountReceived.Text)
lblInputVAT.Text = (TotalBeforeVAT * VATRate).ToString("#,##0.00")
Dim Net = Double.Parse(lblTotal.Text) - Double.Parse(lblDiscountReceived.Text) + Double.Parse(lblInputVAT.Text)
lblNet.Text = Net.ToString("#,##0.00")
lblNetInText.Text = Net.ToThaiWord()
End Sub
Private Sub txtSearchPR_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles txtSearchPR.KeyDown
If txtSearchPR.Text.Trim() = "" Then Exit Sub
If e.KeyCode = Keys.Enter Then
SearchPR()
End If
End Sub
Private Sub cmdSearch_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSearch.Click
If txtSearchPR.Text.Trim() = "" AndAlso optPRNo.Checked = True Then Exit Sub
SearchPR()
End Sub
Private Sub SearchPR()
sw.Reset()
sw.Start()
If optPRNo.Checked = True Then
IPurchaseRequisition = From pr In db.PurchaseRequisitions _
Where pr.PRNo = txtSearchPR.Text.Trim() _
AndAlso pr.IsComplete = "0" _
Order By pr.PRDate
ElseIf optDuration.Checked = True Then
IPurchaseRequisition = From pr In db.PurchaseRequisitions _
Where pr.PRDate >= dtpStart.Value AndAlso pr.PRDate <= dtpEnd.Value _
AndAlso pr.IsComplete = "0" _
Order By pr.PRDate
End If
dgvPRDList.DataSource = Nothing
If IPurchaseRequisition.Count() > 0 Then
dgvPRList.DataSource = IPurchaseRequisition.ToList()
FormatDgvPRList()
cmdSave.Enabled = True
sw.Stop()
ProcessTime = sw.ElapsedMilliseconds / 1000
_ToolStripStatusLabel.Text = "เวลาที่ใช้ : " & ProcessTime.ToString("0.0000") & " วินาที"
Else
dgvPRList.DataSource = Nothing
cmdSave.Enabled = False
MessageBox.Show("เงื่อนไขที่คุณป้อน ไม่ตรงกับรายการขอซื้อที่มีอยู่ !!!", "ผลการค้นหา", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtSearchPR.Focus()
txtSearchPR.SelectAll()
End If
End Sub
Private Sub FormatDgvPRList()
With dgvPRList
If .RowCount > 0 Then
.Columns(3).HeaderText = "เลขที่ใบขอซื้อ"
.Columns(4).HeaderText = "วันที่ขอซื้อ"
.Columns(5).HeaderText = "วันที่ต้องการสินค้า"
.Columns(3).ReadOnly = True
.Columns(4).ReadOnly = True
.Columns(5).ReadOnly = True
.Columns(0).Visible = False
.Columns(1).Visible = False
.Columns(2).Visible = False
.Columns(3).Width = 150
.Columns(4).Width = 190
.Columns(5).Width = 190
.Columns(6).Visible = False
.Columns(7).Visible = False
.Columns(8).Visible = False
.Columns(9).Visible = False
End If
End With
End Sub
Private Sub dgvPRList_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvPRList.CellFormatting
If e.ColumnIndex = -1 Then Exit Sub
If dgvPRList.Columns(e.ColumnIndex).Name = "PRDate" Then
e.CellStyle.Format = "d MMMM yyyy"
End If
If dgvPRList.Columns(e.ColumnIndex).Name = "RequiredDate" Then
e.CellStyle.Format = "d MMMM yyyy"
End If
End Sub
Private Sub dgvPRList_CellMouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvPRList.CellMouseUp
If e.RowIndex = -1 Then Exit Sub
Dim CurrentPRID = dgvPRList.Rows.Item(e.RowIndex).Cells("PRID").Value.ToString()
Dim PRDList = From prd In db.PurchaseRequisitionDetails _
From p In db.Products _
From u In db.Units _
Where prd.ProductID = p.ProductID _
AndAlso prd.UnitID = u.UnitID _
AndAlso prd.PRID = CurrentPRID _
AndAlso p.CompanyID = cboCompany.SelectedValue.ToString() _
AndAlso prd.PRStatus = "ยังไม่ดำเนินการ" _
Order By p.ProductNameTH _
Select New With { _
.PRID = prd.PRID, _
.ProductID = prd.ProductID, _
.ProductName = p.ProductNameTH, _
.NumberToOrder = prd.NumberToOrder, _
.UnitName = u.UnitName, _
.PRStatus = prd.PRStatus, _
.UnitID = prd.UnitID, _
.LastCost = p.LastCost _
}
If PRDList.Count() > 0 Then
dgvPRDList.DataSource = PRDList.ToList()
FormatDgvPRDList()
Else
dgvPRDList.DataSource = Nothing
MessageBox.Show("ไม่มีรายการขอซื้อสินค้าของบริษัทนี้ !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
End If
End Sub
Private Sub FormatDgvPRDList()
With dgvPRDList
If .RowCount > 0 Then
.Columns(1).HeaderText = "รหัสสินค้า"
.Columns(2).HeaderText = "ชื่อสินค้า"
.Columns(3).HeaderText = "จำนวน"
.Columns(4).HeaderText = "หน่วย"
.Columns(5).HeaderText = "สถานะขอซื้อ"
.Columns(0).Visible = False
.Columns(1).Width = 100
.Columns(2).Width = 240
.Columns(3).Width = 60
.Columns(4).Width = 120
.Columns(5).Width = 110
.Columns(6).Visible = False
.Columns(7).Visible = False
End If
End With
End Sub
Private Sub dgvPRDList_CellFormatting(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellFormattingEventArgs) Handles dgvPRDList.CellFormatting
If dgvPRDList.Columns(e.ColumnIndex).Name = "NumberToOrder" Then
e.CellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter
End If
End Sub
Private Sub dgvPRDList_CellMouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvPRDList.CellMouseDown
If e.RowIndex = -1 Then Exit Sub
For Each u In IUnit
If u.UnitID = dgvPRDList.Rows.Item(e.RowIndex).Cells("UnitID").Value.ToString() Then
QuantityPerUnit = Convert.ToInt32(u.QuantityPerUnit)
Exit For
End If
Next
With dgvPRDList
CurrentProduct(0) = .Rows.Item(e.RowIndex).Cells("ProductID").Value.ToString()
CurrentProduct(1) = .Rows.Item(e.RowIndex).Cells("ProductNameTH").Value.ToString()
CurrentProduct(2) = .Rows.Item(e.RowIndex).Cells("LastCost").Value.ToString()
CurrentProduct(3) = .Rows.Item(e.RowIndex).Cells("NumberToOrder").Value.ToString()
CurrentProduct(4) = .Rows.Item(e.RowIndex).Cells("UnitName").Value.ToString()
CurrentProduct(5) = (Integer.Parse(CurrentProduct(2)) * Integer.Parse(CurrentProduct(3)) * QuantityPerUnit).ToString()
CurrentProduct(6) = .Rows.Item(e.RowIndex).Cells("UnitID").Value.ToString()
CurrentProduct(7) = .Rows.Item(e.RowIndex).Cells("PRID").Value.ToString()
CurrentProduct(8) = QuantityPerUnit.ToString()
End With
End Sub
Private Sub dgvPRDList_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles dgvPRDList.MouseMove
If e.Button = Windows.Forms.MouseButtons.Left Then
Dim AnyData As DataObject
AnyData = New DataObject()
AnyData.SetData(DataFormats.Text, CurrentProduct)
Dim dde As DragDropEffects
dde = dgvProductLists.DoDragDrop(AnyData, DragDropEffects.Copy)
End If
End Sub
Private Sub dgvPRDList_CellMouseUp(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DataGridViewCellMouseEventArgs) Handles dgvPRDList.CellMouseUp
With dgvPRDList
lblProductID.Text = .Rows.Item(e.RowIndex).Cells("ProductID").Value.ToString()
lblProductName.Text = .Rows.Item(e.RowIndex).Cells("ProductNameTH").Value.ToString()
txtLastCost.Text = .Rows.Item(e.RowIndex).Cells("LastCost").Value.ToString()
End With
CalculateTotal()
End Sub
Private Sub lsvProductLists_DoubleClick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles lsvProductLists.DoubleClick
Dim i As Integer = 0
For i = 0 To lsvProductLists.SelectedItems.Count - 1
Dim lvi As ListViewItem
lvi = lsvProductLists.SelectedItems(i)
lsvProductLists.Items.Remove(lvi)
Next
If (lsvProductLists.Items.Count = 0) Then cmdSave.Enabled = False
CalculateNet()
End Sub
Private Sub dtpReceiveDate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpReceiveDate.ValueChanged
If dtpReceiveDate.Value < DateTime.Today Then dtpReceiveDate.Value = DateTime.Today
dtpReceiveDate.Value = dtpReceiveDate.Value.NextWorkingDay(HolidayMode.SundayOnly)
End Sub
Private Sub dtpPaidDate_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpPaidDate.ValueChanged
If dtpPaidDate.Value < DateTime.Today Then dtpPaidDate.Value = DateTime.Today
dtpPaidDate.Value = dtpPaidDate.Value.NextWorkingDay(HolidayMode.SundayOnly)
End Sub
Private Sub txtCreditDateDiscountReceivedCreditTerm_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtCreditDateDiscountReceivedCreditTerm.TextChanged
If txtCreditDateDiscountReceivedCreditTerm.Text.Trim() = "" Then txtCreditDateDiscountReceivedCreditTerm.Text = "0"
dtpPaidDate.Value = DateTime.Today.AddDays(Double.Parse(txtCreditDateDiscountReceivedCreditTerm.Text)).NextWorkingDay(HolidayMode.SundayOnly)
End Sub
Private Sub cmdSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdSave.Click
If txtPONo.Text.Trim() = "" Then
MessageBox.Show("กรุณาใส่หมายเลขใบสั่งซื้อด้วย !!!", "ผลการตรวจสอบ", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtPONo.Focus()
Exit Sub
End If
If MessageBox.Show("คุณต้องการบันทึกรายการสั่งซื้อสินค้า ใช่หรือไม่?", "คำยืนยัน", MessageBoxButtons.YesNo, MessageBoxIcon.Question) = Windows.Forms.DialogResult.Yes Then
sw.Reset()
sw.Start()
Try
Using ts As New TransactionScope()
Dim FirstID As String = ""
Dim SecondID As Integer = 0
Dim oids = From oid In db.Orders _
Order By oid.OrderID Descending Take (1)
Dim LastOrderID As String = ""
If oids.Count() = 1 Then
FirstID = oids.SingleOrDefault().OrderID.Left(4)
SecondID = Integer.Parse(oids.SingleOrDefault().OrderID.Right(6))
SecondID += 1
LastOrderID = String.Format(FirstID, "0000") & SecondID.ToString("000000")
Else
LastOrderID = BizConfig.CurrentBranchID & "000001"
End If
Dim o As New Order()
o.OrderID = LastOrderID
o.BranchID = BizConfig.CurrentBranchID
o.CompanyID = cboCompany.SelectedValue.ToString()
o.PONo = txtPONo.Text.Trim()
If optCash.Checked = True Then
o.CreditTerm = "c"
ElseIf optCredit.Checked = True Then
o.CreditTerm = txtDcRateDiscountReceivedCreditTerm.Text & "/" & txtDcDateDiscountReceivedCreditTerm.Text & ",n/" & txtCreditDateDiscountReceivedCreditTerm.Text
End If
o.FreightIn = 0.0
o.FID = "000"
o.OrderDate = DateTime.Today
o.ReceiveDate = dtpReceiveDate.Value
o.PaidDate = dtpPaidDate.Value
o.Total = Double.Parse(lblTotal.Text)
o.DiscountReceived = Double.Parse(lblDiscountReceived.Text)
o.VATRate = BizConfig.VATRate
o.InputVAT = Double.Parse(lblInputVAT.Text)
o.Net = Double.Parse(lblNet.Text)
o.IsComplete = "0"
o.IsPaid = "0"
o.IsNormal = "1"
o.InventorySystems = BizConfig.InventorySystem
o.IsEntryToJournal = "0"
o.OrderBy = UserAuthentication.UserName
o.ReceivedBy = ""
o.PaidBy = ""
Dim i As Integer
Dim od As OrdersDetail = Nothing
Dim pb As ProductOnBranch = Nothing
Dim ProductID As String = ""
Dim ProductIDList(lsvProductLists.Items.Count) As String
With lsvProductLists
For i = 0 To .Items.Count - 1
ProductID = .Items(i).SubItems(0).Text
ProductIDList(i) = ProductID
od = New OrdersDetail()
od.OrderID = LastOrderID
od.ProductID = ProductID
od.NumberToOrder = Integer.Parse(.Items(i).SubItems(3).Text)
od.UnitID = .Items(i).SubItems(6).Text
od.NumberToReceived = 0
If optCash.Checked = True Then
If Integer.Parse(txtDCCash.Text) > 0 Then
Dim Cost = Double.Parse(.Items(i).SubItems(2).Text)
Dim Amount = Integer.Parse(.Items(i).SubItems(3).Text)
Dim QuantityPerUnit = Integer.Parse(.Items(i).SubItems(8).Text)
Dim DiscountPerPiece = ((Integer.Parse(txtDCCash.Text) / 100) * Cost)
od.Cost = Cost - DiscountPerPiece
od.DiscountPerUnit = DiscountPerPiece
od.Total = ((Cost - DiscountPerPiece) * Amount * QuantityPerUnit)
Else
od.Cost = Double.Parse(.Items(i).SubItems(2).Text)
od.DiscountPerUnit = 0.0
od.Total = Double.Parse(.Items(i).SubItems(5).Text)
End If
ElseIf optCredit.Checked = True Then
od.Cost = Double.Parse(.Items(i).SubItems(2).Text)
od.DiscountPerUnit = 0.0
od.Total = Double.Parse(.Items(i).SubItems(5).Text)
End If
od.OrderStatusID = "0"
o.OrdersDetails.Add(od)
Dim ProductInOrder = Integer.Parse(.Items(i).SubItems(3).Text) * Integer.Parse(.Items(i).SubItems(8).Text)
Dim ps = db.ProductOnBranches.FirstOrDefault(Function(id) id.ProductID = ProductID)
If ps IsNot Nothing Then
Dim OldProductInOrder = ps.ProductInOrder
ps.ProductInOrder = OldProductInOrder + ProductInOrder
Else
pb = New ProductOnBranch()
pb.ProductID = ProductID
pb.BranchID = BizConfig.CurrentBranchID
pb.UnitID = .Items(i).SubItems(6).Text
pb.QuantityWithUnit = 0.0
pb.RealQuantity = 0
pb.ProductInOrder = ProductInOrder
pb.LowLimitToOrder = 0
db.ProductOnBranches.InsertOnSubmit(pb)
End If
Next
Dim prds = From prd In db.PurchaseRequisitionDetails _
Where ProductIDList.Contains(prd.ProductID)
For Each p In prds
If p.PRStatus = "ยังไม่ดำเนินการ" Then
p.PRStatus = "อยู่ระหว่างสั่งซื้อ"
End If
Next
End With
db.Orders.InsertOnSubmit(o)
db.SubmitChanges()
ts.Complete()
End Using
sw.Stop()
ProcessTime = sw.ElapsedMilliseconds / 1000
_ToolStripStatusLabel.Text = "เวลาที่ใช้ : " & ProcessTime.ToString("0.0000") & " วินาที"
If prDlg.ShowDialog = Windows.Forms.DialogResult.OK Then
prDoc.Print()
End If
ClearPODetails()
ClearCreditTerm()
ClearProduct()
ClearNet()
lsvProductLists.Items.Clear()
dgvProductLists.DataSource = Nothing
dgvPRList.DataSource = Nothing
dgvPRDList.DataSource = Nothing
MessageBox.Show("บันทึกรายการสั่งซื้อสินค้า เรียบร้อยแล้ว !!!", "ผลการทำงาน", MessageBoxButtons.OK, MessageBoxIcon.Information)
txtSearch.Focus()
txtSearch.SelectAll()
Catch ex As Exception
MessageBox.Show("ไม่สามารถบันทึกรายการสั่งซื้อได้ เนื่องจาก " & ex.Message, "ข้อผิดพลาด", MessageBoxButtons.OK, MessageBoxIcon.Warning)
End Try
End If
End Sub
Private Sub StringToPrint_Print(ByVal sender As Object, ByVal e As PrintPageEventArgs)
AnyString(e.Graphics, cboCompany.Text, 240, 170)
AnyString(e.Graphics, txtPONo.Text, 550, 125)
AnyString(e.Graphics, DateTime.Today.ToLongDateString(), 550, 170)
AnyString(e.Graphics, dtpPaidDate.Text, 550, 195)
AnyString(e.Graphics, dtpReceiveDate.Text, 550, 220)
Dim PrintCreditTerm As String = ""
If optCash.Checked = True Then
PrintCreditTerm = "เงินสด"
ElseIf optCredit.Checked = True Then
PrintCreditTerm = txtDcRateDiscountReceivedCreditTerm.Text & "/" & txtDcDateDiscountReceivedCreditTerm.Text & ",n/" & txtCreditDateDiscountReceivedCreditTerm.Text
End If
AnyString(e.Graphics, PrintCreditTerm, 195, 195)
Dim i As Integer = 0
Dim CurrentYPosition As Integer = 330
Dim strColumn1 As String = ""
Dim strColumn2 As String = ""
Dim strColumn3 As Double = 0.0
Dim strColumn4 As Double = 0.0
Dim strColumn5 As String = ""
Dim strColumn6 As Double = 0.0
For i = 0 To lsvProductLists.Items.Count - 1
strColumn1 = lsvProductLists.Items(i).SubItems(0).Text
strColumn2 = lsvProductLists.Items(i).SubItems(1).Text
strColumn3 = CDbl(lsvProductLists.Items(i).SubItems(2).Text)
strColumn4 = CDbl(lsvProductLists.Items(i).SubItems(3).Text)
strColumn5 = lsvProductLists.Items(i).SubItems(4).Text
strColumn6 = CDbl(lsvProductLists.Items(i).SubItems(5).Text)
AnyString(e.Graphics, strColumn1, 135, CurrentYPosition)
If strColumn2.Length > 25 Then
AnyString(e.Graphics, strColumn2.Left(25) & "...", 230, CurrentYPosition)
Else
AnyString(e.Graphics, strColumn2, 230, CurrentYPosition)
End If
AnyString(e.Graphics, strColumn3.ToString("#,##0"), 435, CurrentYPosition)
AnyString(e.Graphics, strColumn4.ToString(), 500, CurrentYPosition)
AnyString(e.Graphics, strColumn5, 540, CurrentYPosition)
AnyString(e.Graphics, strColumn6.ToString(), 625, CurrentYPosition)
CurrentYPosition = CurrentYPosition + 20
Next
AnyString(e.Graphics, BizConfig.VATRate.ToString(), 235, 590)
AnyString(e.Graphics, lblDiscountReceived.Text, 585, 565)
AnyString(e.Graphics, lblInputVAT.Text, 585, 590)
AnyString(e.Graphics, lblNet.Text, 585, 615)
End Sub
Private Sub AnyString(ByVal g As Graphics, ByVal printString As String, ByVal xPos As Integer, ByVal yPos As Integer)
Dim anyPoint As New PointF(xPos, yPos)
g.DrawString(printString, UseFont, Brushes.Black, anyPoint)
End Sub
Private Sub dtpEnd_ValueChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles dtpEnd.ValueChanged
If dtpEnd.Value > DateTime.Today Then
dtpEnd.Value = DateTime.Today
End If
End Sub
Private Sub optPRNo_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optPRNo.CheckedChanged
If optPRNo.Checked = True Then
txtSearchPR.Enabled = True
dtpStart.Enabled = False
dtpEnd.Enabled = False
dgvPRList.Columns.Clear()
dgvPRList.DataSource = Nothing
dgvPRDList.DataSource = Nothing
txtSearchPR.Focus()
End If
End Sub
Private Sub optDuration_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optDuration.CheckedChanged
If optDuration.Checked = True Then
txtSearchPR.Enabled = False
dtpStart.Enabled = True
dtpEnd.Enabled = True
dtpStart.Value = DateTime.Today.AddMonths(-1)
dtpEnd.Value = DateTime.Today
dgvPRList.Columns.Clear()
dgvPRList.DataSource = Nothing
dgvPRDList.DataSource = Nothing
dtpStart.Focus()
End If
End Sub
Private Sub frmPO_FormClosed(ByVal sender As System.Object, ByVal e As System.Windows.Forms.FormClosedEventArgs) Handles MyBase.FormClosed
db.Connection.Close()
End Sub
Private Sub cboCompany_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cboCompany.KeyPress
e.Handled = True
End Sub
Private Sub cboCompany_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cboCompany.KeyDown
e.Handled = True
End Sub
Private Sub cboUnit_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles cboUnit.KeyPress
e.Handled = True
End Sub
Private Sub cboUnit_KeyDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyEventArgs) Handles cboUnit.KeyDown
e.Handled = True
End Sub
Private Sub txtDCCash_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDCCash.KeyPress
If e.KeyChar < "0" Or e.KeyChar > "9" Then
e.Handled = True
End If
End Sub
Private Sub txtDcDateDiscountReceivedCreditTerm_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDcDateDiscountReceivedCreditTerm.KeyPress
If e.KeyChar < "0" Or e.KeyChar > "9" Then
e.Handled = True
End If
End Sub
Private Sub txtDcRateDiscountReceivedCreditTerm_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtDcRateDiscountReceivedCreditTerm.KeyPress
If e.KeyChar < "0" Or e.KeyChar > "9" Then
e.Handled = True
End If
End Sub
Private Sub txtCreditDateDiscountReceivedCreditTerm_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtCreditDateDiscountReceivedCreditTerm.KeyPress
If e.KeyChar < "0" Or e.KeyChar > "9" Then
e.Handled = True
End If
End Sub
Private Sub txtLastCost_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtLastCost.KeyPress
If e.KeyChar < "0" Or e.KeyChar > "9" Then
e.Handled = True
End If
End Sub
Private Sub txtAmount_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtAmount.KeyPress
If e.KeyChar < "0" Or e.KeyChar > "9" Then
e.Handled = True
End If
End Sub
Private Sub optCash_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optCash.CheckedChanged
If optCash.Checked = True Then
ClearCreditTerm()
txtDCCash.Enabled = True
txtDcDateDiscountReceivedCreditTerm.Enabled = False
txtDcRateDiscountReceivedCreditTerm.Enabled = False
txtCreditDateDiscountReceivedCreditTerm.Enabled = False
dtpPaidDate.Value = DateTime.Today.NextWorkingDay(HolidayMode.SundayOnly)
txtDCCash.Focus()
txtDCCash.SelectAll()
End If
End Sub
Private Sub optCredit_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles optCredit.CheckedChanged
If optCredit.Checked = True Then
txtDCCash.Text = "0"
txtDCCash.Enabled = False
txtDcDateDiscountReceivedCreditTerm.Enabled = True
txtDcRateDiscountReceivedCreditTerm.Enabled = True
txtCreditDateDiscountReceivedCreditTerm.Enabled = True
txtDcDateDiscountReceivedCreditTerm.Focus()
txtDcDateDiscountReceivedCreditTerm.SelectAll()
End If
End Sub
Private Sub cmdClear_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClear.Click
lsvProductLists.Items.Clear()
ClearNet()
cmdSave.Enabled = False
txtSearch.Focus()
txtSearch.SelectAll()
End Sub
Private Sub txtDCCash_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDCCash.TextChanged
If txtDCCash.Text.Trim() = "" Then txtDCCash.Text = "0"
CalculateNet()
End Sub
Private Sub txtDcDateDiscountReceivedCreditTerm_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDcDateDiscountReceivedCreditTerm.TextChanged
If txtDcDateDiscountReceivedCreditTerm.Text.Trim() = "" Then txtDcDateDiscountReceivedCreditTerm.Text = "0"
End Sub
Private Sub txtDcRateDiscountReceivedCreditTerm_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtDcRateDiscountReceivedCreditTerm.TextChanged
If txtDcRateDiscountReceivedCreditTerm.Text.Trim() = "" Then txtDcRateDiscountReceivedCreditTerm.Text = "0"
End Sub
Private Sub txtLastCost_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtLastCost.TextChanged
If txtLastCost.Text.Trim() = "" Then txtLastCost.Text = "1"
If Integer.Parse(txtLastCost.Text) = 0 Then txtLastCost.Text = "1"
End Sub
Private Sub txtAmount_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles txtAmount.TextChanged
If txtAmount.Text.Trim() = "" Then txtAmount.Text = "1"
If Integer.Parse(txtAmount.Text) = 0 Then txtAmount.Text = "1"
End Sub
End Class
Tag : .NET, Ms SQL Server 2008, Win (Windows App), VB.NET, VS 2008 (.NET 3.x)