Imports System
Imports System.Data
Imports System.Data.SqlClient
Partial Class BusinessAssociates_sendQuotation
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If (Not IsPostBack) Then
            txtSubject.Text = "Quotation"
            txtVCompanyID.Text = "f6b59677-9946-489c-8628-465872369428"
            txtSSAID.Text = Session("SSAID").ToString

            txtQuotationID.Text = Request("qid").ToString
            If Not Request("qid") = "" Then
                fillQuotationDetail()
            End If

            If Request("type") = "approval" Then
                txtMailTo.Text = "contact@atcomaart.com"
                txtMailTo.ReadOnly = True
                txtSubject.Text = "Approval of Quotation"
            End If

        End If
    End Sub

    Public Sub fillQuotationDetail()
        Dim cn As New System.Data.SqlClient.SqlConnection(ConfigurationManager.AppSettings("Connection4Voffice"))
        Dim objAdapter As SqlDataAdapter
        Dim ObjDS As DataSet
        Dim ObjDR As DataRow

        cn.Open()

        Try
            objAdapter = New SqlDataAdapter("Select_CRMQuotationAndCustomerByQuotationID_Proc", cn)
            objAdapter.SelectCommand.CommandType = CommandType.StoredProcedure

            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@QuotationID", SqlDbType.VarChar))
            objAdapter.SelectCommand.Parameters.Add(New SqlParameter("@CompanyID", SqlDbType.VarChar))

            objAdapter.SelectCommand.Parameters("@QuotationID").Value = Request("qid").ToString
            objAdapter.SelectCommand.Parameters("@CompanyID").Value = txtVCompanyID.Text.Trim

            ObjDS = New DataSet
            objAdapter.Fill(ObjDS, "QuotationBank")


            If ObjDS.Tables(0).Rows.Count > 0 Then
                ObjDR = ObjDS.Tables(0).Rows(0)

                txtMailTo.Text = IIf(IsDBNull(ObjDR("V_CRM_Email")), "", ObjDR("V_CRM_Email"))
                lblAttachment.Text = IIf(IsDBNull(ObjDR("htmlFileName")), "No Attachment", ObjDR("htmlFileName"))

                txtQuotationAmount.Text = IIf(IsDBNull(ObjDR("V_CRM_QuotationAmt")), 0, ObjDR("V_CRM_QuotationAmt"))
                txtReceivedAmount.Text = IIf(IsDBNull(ObjDR("V_CRM_ReceivedAmt")), 0, ObjDR("V_CRM_ReceivedAmt"))
                txtPayment.Text = IIf(IsDBNull(ObjDR("V_CRM_PaymentStatus")), "", ObjDR("V_CRM_PaymentStatus"))
                txtLastDate.Text = IIf(IsDBNull(ObjDR("V_CRM_LastDate")), "", ObjDR("V_CRM_LastDate"))
                txtOrderStatus.Text = IIf(IsDBNull(ObjDR("V_CRM_QuotationStatus")), "", ObjDR("V_CRM_QuotationStatus"))
                txtCustomerID.Text = IIf(IsDBNull(ObjDR("V_CRM_CustomerID")), "", ObjDR("V_CRM_CustomerID").ToString)
                txtQuotationDate.Text = IIf(IsDBNull(ObjDR("V_CRM_QuotationDate")), "", ObjDR("V_CRM_QuotationDate"))
                txtQuotationNo.Text = IIf(IsDBNull(ObjDR("V_CRM_QuotationNo")), "", ObjDR("V_CRM_QuotationNo"))
            End If
            '    Return ObjDS

        Catch ex As Exception
            Throw ex
        Finally
            cn.Close()
            cn.Dispose()
        End Try
    End Sub

    Protected Sub btnSend_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSend.Click
        'Try
        '    Dim mailFrom As String = ""
        '    Dim userid As String = ""
        '    Dim password As String = ""
        '    Dim smtpserver As String = ""
        '    mailFrom = "contact@atcomaart.com"
        '    userid = mailFrom
        '    password = ""
        '    smtpserver = ""

        '    Dim mail As New System.Net.Mail.MailMessage(mailFrom, txtMailTo.Text.Trim, txtSubject.Text.Trim, txtMSG.Text.Trim)

        '    If txtMailCC.Text.Trim <> "" Then
        '        mail.CC.Add(New System.Net.Mail.MailAddress(txtMailCC.Text.Trim))
        '    End If

        '    If Request("type") = "approval" Then
        '    Else
        '        mail.Bcc.Add(New System.Net.Mail.MailAddress("contact@atcomaart.com"))
        '    End If


        '    mail.IsBodyHtml() = True

        '    mail.Priority = Net.Mail.MailPriority.High

        '    mail.Attachments.Add(New System.Net.Mail.Attachment(Server.MapPath("..") & "\quotations\" & lblAttachment.Text.Trim))

        '    Dim smtpClnt As New System.Net.Mail.SmtpClient(smtpserver, 587)

        '    smtpClnt.Credentials = New System.Net.NetworkCredential(userid, password)

        '    smtpClnt.Send(mail)
        '    lblError.Text = "Message Sent Successfully"

        '    If Request("type") = "approval" Then
        '        SendForApproval()
        '    End If
        'Catch ex As Exception
        '    lblError.Text = ex.Message
        'End Try
    End Sub

    Public Sub SendForApproval()
        Dim cmd As New Data.SqlClient.SqlCommand()
        Dim cmdQuotation As New Data.SqlClient.SqlCommand()
        Dim con As New Data.SqlClient.SqlConnection
        con.ConnectionString = System.Configuration.ConfigurationManager.AppSettings("Connection4Voffice")

        Try

            cmdQuotation.CommandText = "Insert_CRMQuotation_Proc"
            cmdQuotation.CommandType = Data.CommandType.StoredProcedure
            cmdQuotation.Connection = con

            con.Open()

            cmdQuotation.Parameters.Add("@QuotationID", Data.SqlDbType.VarChar).Value = txtQuotationID.Text.Trim
            cmdQuotation.Parameters.Add("@QuotationNo", Data.SqlDbType.VarChar).Value = txtQuotationNo.Text.Trim
            cmdQuotation.Parameters.Add("@QuotationDate", Data.SqlDbType.VarChar).Value = txtQuotationDate.Text.Trim
            cmdQuotation.Parameters.Add("@CustomerID", SqlDbType.VarChar).Value = txtCustomerID.Text.Trim
            cmdQuotation.Parameters.Add("@QuotationAmount", SqlDbType.Decimal).Value = txtQuotationAmount.Text.Trim
            cmdQuotation.Parameters.Add("@ReceivedAmount", SqlDbType.Decimal).Value = txtReceivedAmount.Text.Trim
            cmdQuotation.Parameters.Add("@QuotationStatus", SqlDbType.NVarChar, 50).Value = txtOrderStatus.Text.Trim
            cmdQuotation.Parameters.Add("@ApprovalStatus", SqlDbType.Int).Value = 2
            cmdQuotation.Parameters.Add("@QuotationPaymentStatus", SqlDbType.NVarChar, 50).Value = txtPayment.text.trim
            cmdQuotation.Parameters.Add("@QuotationLastDate", SqlDbType.DateTime).Value = txtLastDate.Text
            cmdQuotation.Parameters.Add("@CompanyID", SqlDbType.VarChar).Value = txtVCompanyID.Text.Trim
            cmdQuotation.Parameters.Add("@SSAID", SqlDbType.VarChar).Value = txtSSAID.Text.Trim
            cmdQuotation.Parameters.Add("@QuotationMode", SqlDbType.Int).Value = 1
            cmdQuotation.Parameters.Add("@FormName", SqlDbType.NVarChar, 50).Value = "QuotationBankSSA"
            cmdQuotation.Parameters.Add("@Mode", SqlDbType.VarChar, 10).Value = "Update"

            cmdQuotation.ExecuteNonQuery()

            con.Close()
            con.Dispose()

        Catch ex As Exception
            showmessage("Please Try again Later.", lblError)
            Exit Sub
        End Try
    End Sub
End Class
