<%@Page Language="VB" Inherits="BaseClass" Src="transactions.vb" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<html>
  <script language="VB" runat="server">
    Sub Grid1_Update(sender As Object, e As DataGridCommandEventArgs)
      Dim sql As string
      Dim vdate As String 
      Dim ref As String 
      Dim code_value As String 
      Dim dr_amt As String 
      Dim cr_amt As String 
      Dim posted_display As String 
      Dim id As String 
      Dim narr As String 
      Dim myTextBox As TextBox
      '主要区域：只读
      Dim doc_no as string   = Grid1.DataKeys.Item(E.Item.ItemIndex).ToString
      myTextBox = E.Item.FindControl("edit_date")
      vdate = trim(mytextbox.text)
      myTextBox = E.Item.FindControl("edit_ref")
      ref = trim(mytextbox.text)
      myTextBox = E.Item.FindControl("edit_dr_amt")
      dr_amt = trim(mytextbox.text)
      myTextBox = E.Item.FindControl("edit_cr_amt")
      cr_amt = trim(mytextbox.text)
      myTextBox = E.Item.FindControl("edit_posted_display")
      posted_display= trim(mytextbox.text)
      myTextBox = E.Item.FindControl("edit_narr")
      narr = trim(mytextbox.text)
      '执行存储过程

      sql = "Execute p_trans @date = '" + vdate+ "' ,@ref= '" 
      sql = sql + ref + "', @dr_amount =" 
      sql = sql + dr_amt + ",@cr_amount = " 
      sql = sql + cr_amt  +" , @posted_to = " + posted_display + " ," 
      sql = sql + "@id = 'RPT', @doc_no = " + doc_no + ", @narr= '" + narr+ "'"
      RunSql(sql)
      rebind() 
    End Sub

    Sub add_click(Source As Object, E As EventArgs) 
      Dim sql As string  
      sql = "Execute p_trans @date = '" + adate.text + "' ," 
      sql = sql + "@ref= '" + aref.text + "', @dr_amount = " 
      sql = sql + adr_amount.text + ",@cr_amount = " 
      sql = sql + acr_amount.text  +" , @posted_to = '" 
      sql = sql + aposted_display.SelectedItem.value + "' ," 
      sql = sql + "@id = 'RPT', @doc_no = NULL" +  ", @narr= '" 
      sql = sql + anarr.text + "'"
      RunSql(sql) 
      rebind() 
      hidePanel()
    End Sub
    Sub add_show(Source As Object, E As EventArgs) 
      AddPanel.visible = true
    End Sub
    Sub Grid1_delete(sender As Object, e As DataGridCommandEventArgs)
      Dim doc_no As string = Grid1.DataKeys.Item(E.Item.ItemIndex).ToString
      Dim sql As string
      sql = " Delete from transactions where doc_no = " + cstr(doc_no)
      sql = sql + " Delete from tr_header where doc_no = " + cstr(doc_no)
      RunSql(sql)
      rebind() 
    End Sub
  </script>
  <body style="font: 10pt verdana">
    
    <form runat="server">
    <asp:ValidationSummary runat=server
    headertext="页面上有错误:" />
      <h3><font face="Verdana">事务
      <asp:Label id="title" runat="server"/>  
      </font></h3>
      <asp:HyperLink runat="server" Text="Financial Account Selection "
          NavigateUrl="selection.aspx"></asp:HyperLink>&nbsp
      <asp:HyperLink runat="server" Text="Masters" NavigateUrl="masters3.aspx">
      </asp:HyperLink>&nbsp
      <asp:HyperLink runat="server" Text="Trial Balance" NavigateUrl="TrialBalance.aspx">
      </asp:HyperLink>&nbsp
      <asp:HyperLink runat="server" Text="Home" NavigateUrl="default.aspx">
      </asp:HyperLink>
      <table width="95%">
        <tr><td>
          <asp:Button id="Addshow" visible = "false" text="新事务"
            onclick="add_show" runat="server" />
        </td></tr> 
        <hr>
        <tr>
        <td valign="top">
        <asp:DataGrid id="Grid1" runat="server"
          AutoGenerateColumns="false"
          BackColor="White"
          BorderWidth="1px" BorderStyle="Solid" BorderColor="Tan"
          CellPadding="2" CellSpacing="0"
            Font-Name="Verdana" Font-Size="8pt"
            OnEditCommand="Grid1_Edit"
            OnCancelCommand="Grid1_Cancel"
            OnUpdateCommand="Grid1_Update"
            OnDeleteCommand = "Grid1_delete"
            DataKeyField="doc_no">
          <Columns>
            <asp:EditCommandColumn
              EditText="编辑"
              CancelText="取消"
              UpdateText="确定"
              ItemStyle-Wrap="false"
              HeaderText="编辑"
              HeaderStyle-Wrap="false"/>
          <asp:ButtonColumn Text="删除" CommandName="Delete" HeaderText="删除"/>
          <asp:BoundColumn HeaderText="Doc #" ReadOnly="true" DataField="doc_no"/>
          <asp:TemplateColumn HeaderText="Ref" >
            <ItemTemplate>
              <asp:Label Text='<%# Container.DataItem("ref") %>' runat="server"/>
            </ItemTemplate>
            <EditItemTemplate>
              <asp:TextBox id="edit_ref" Text='<%# Container.DataItem("ref") %>'
                runat="server"/>
            </EditItemTemplate>
          </asp:TemplateColumn>
          <asp:TemplateColumn HeaderText="Date" >
            <ItemTemplate>
              <asp:Label Text='<%# Container.DataItem("date") %>' runat="server" />
            </ItemTemplate>
            <EditItemTemplate >
              <asp:TextBox id="edit_date" BorderStyle="None" Readonly="True" 
                Text='<%# Container.DataItem("date") %>' runat="server"  />
            </EditItemTemplate>
          </asp:TemplateColumn>
          <asp:TemplateColumn HeaderText="Payee" >
            <ItemTemplate>
              <asp:Label Text='<%# Container.DataItem("posted_display") %>' 
                runat="server"/>
            </ItemTemplate>
            <EditItemTemplate>
              <asp:TextBox id="edit_posted_display"  
                Text='<%# Container.DataItem("posted_to") %>'
                runat="server" ReadOnly="true" BorderStyle="None" />    
            </EditItemTemplate>
          </asp:TemplateColumn>
          <asp:TemplateColumn HeaderText="Narration" >
            <ItemTemplate>
                <asp:Label Text='<%# Container.DataItem("narr") %>' runat="server"/>
            </ItemTemplate>
            <EditItemTemplate>
              <asp:TextBox id="edit_narr"  Text='<%# Container.DataItem("narr") %>'
                runat="server"/>    
            </EditItemTemplate>
          </asp:TemplateColumn>
          <asp:TemplateColumn HeaderText="Deposit" >
            <ItemTemplate>
              <asp:Label Text='<%# Container.DataItem("dr_amount") %>' 
                runat="server"/>
            </ItemTemplate>
            <EditItemTemplate>
              <asp:TextBox id="edit_dr_amt"   
                Text='<%# Container.DataItem("dr_amount") %>' runat="server"/>
            </EditItemTemplate>
          </asp:TemplateColumn>
          <asp:TemplateColumn HeaderText="Payment" >
            <ItemTemplate>
              <asp:Label Text='<%# Container.DataItem("cr_amount") %>' 
                runat="server"/>
            </ItemTemplate>
            <EditItemTemplate>
              <asp:TextBox id="edit_cr_amt"  
                Text='<%# Container.DataItem("cr_amount") %>' runat="server"/>
            </EditItemTemplate>
          </asp:TemplateColumn>
          </Columns>
          <HeaderStyle BackColor="DarkRed" ForeColor="White" Font-Bold="true">
          </HeaderStyle>
          <ItemStyle ForeColor="DarkSlateBlue">
          </ItemStyle>
          <AlternatingItemStyle BackColor="Beige">
          </AlternatingItemStyle>
        </asp:DataGrid>
        </td>

        <td valign="top">
          <!---- 插入行 逻辑-------------->
          <asp:Panel id="AddPanel" runat="server" Visible="false">
            <table style="font: 8pt verdana">
              <tr>
                <td colspan="2" bgcolor="#aaaadd" style="font:10pt verdana">
                 添加一个新事务:</td>
              </tr>
              <tr>
                <td nowrap>Date (Required): </td>
                <td><asp:TextBox id="adate"  runat="server" value = ""  /></td>
                <td> <asp:RequiredFieldValidator runat=server 
                  controltovalidate=adate
                  errormessage="日期不能为空.">*
                  </asp:RequiredFieldValidator></td>
              </tr>
              <tr>
                <td nowrap>Reference (必填/必须唯一): </td>
                <td><asp:TextBox id="aref" value = "" runat="server" /></td>
                <td> <asp:RequiredFieldValidator runat=server 
                  controltovalidate=aref
                  errormessage="引用必须唯一.">*
                  </asp:RequiredFieldValidator></td>
              </tr>
              <tr>
                <td nowrap>Account Posted To: </td>
                <td><asp:DropDownList DataTextField = "code_display"
                  DataValueField = "code_value" id="aposted_display" runat="server" /></td>
              </tr>
              <tr>
                <td nowrap>Narration: </td>
                <td><asp:TextBox id="anarr"  value = "" runat="server" /></td>
              </tr>
              <tr>
                <td nowrap>Deposit Amount: </td>
                <td><asp:TextBox id="adr_amount" value = 0  runat="server" /></td>
              </tr>
              <tr>
                <td nowrap>Payment Amount: </td>
                <td><asp:TextBox id="acr_amount" value = 0  runat="server" /></td>
              </tr>
              <tr>
                <td style="padding-top:15">
                  <asp:Button id="SubmitDetailsBtn" text="提交" onclick="add_Click"
                      runat="server" />
                </td>
              </tr>
            </table>
          </asp:Panel>
          <!-------------插入逻辑 结束 -------->
        </td>
        </tr>
      </table>
      <hr>
      <asp:Label id="Message" runat="server"/>  
    </form>
  </body>
</html>
