<%@Page Language="VB" Inherits="BaseClass" Src="StockTrans.vb" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Register TagPrefix="Hersh" TagName="nav" Src="nav.ascx" %>
<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 qty_in As String 
      Dim qty_out 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 = mytextbox.text
      myTextBox = E.Item.FindControl("edit_ref")
      ref = trim(mytextbox.text)
      myTextBox = E.Item.FindControl("edit_qty_in")
      qty_in = trim(mytextbox.text)
      myTextBox = E.Item.FindControl("edit_qty_out")
      qty_out = trim(mytextbox.text)
      myTextBox = E.Item.FindControl("edit_narr")
      narr = trim(mytextbox.text)
      myTextBox = E.Item.FindControl("edit_code_value")
      code_value = trim(mytextbox.text)
      'Now execute stored procedure 
      sql = "Execute p_stock_trans @date = '" 
      sql = sql + vdate + "' ,@ref= '" + ref + "', @qty_in =" 
      sql = sql + qty_in + ",@qty_out = "+ qty_out +" , " 
      sql = sql + "@id = 'STK', @doc_no = "+doc_no+", @narr= '"+narr+ "',"
      sql = sql +  "@code_value=" + code_value
      'response.write(sql)
      RunSql(sql)
      rebind() 
    End Sub
    Sub add_click(Source As Object, E As EventArgs) 
      Dim sql As string  
      sql = "Execute p_stock_trans @date = '" 
      sql = sql+ adate.text+"',@ref= '"+aref.text+"', @qty_in =" 
      sql = sql+ aqty_in.text+",@qty_out = "+aqty_out.text+" ,"
      sql = sql+ "@id = 'RPT', @doc_no = NULL"+", @narr= '" 
      sql = sql+ anarr.text+"', @code_value = "+acode_value.SelectedItem.value 
      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 stock_detail where doc_no = " + cstr(doc_no)
      sql = sql + " Delete from tr_header where doc_no = " + cstr(doc_no)
      RunSql(sql)
      rebind() 
    End Sub
  </script>
  <head>
    <style>
      a { color:black;
            text-decoration:none;}
      a:hover {color:red;
                    text-decoration:underline;}
    </style>
  </head>
  <body style="font: 10pt verdana; background-color:ivory">
    
    <form runat="server">
    <asp:ValidationSummary runat=server
      headertext="页面上有错误:" />
      <!---- 导航条 开始-------------->
      <Hersh:nav id="menu" runat = server 
        vGridlines = Both
        vBorderColor = "Black"
        vCellPadding = 7 />
      <!---- 导航条 结束-------------->
      <h3><font face="Verdana"> Inventory Movements
      <asp:Label id="title" runat="server"/>  </font></h3>
      <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="删除" 
                  HeaderText="Delete"/>
                <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="Inventory Account" >
                  <ItemTemplate>
                    <asp:Label Text='<%# Container.DataItem("code_display") %>'
                      runat="server"/>
                  </ItemTemplate>
                  <EditItemTemplate>
                    <asp:TextBox id="edit_code_value" 
                      Text='<%# Container.DataItem("code_value") %>'
                      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="Quantity In" >
                  <ItemTemplate>
                    <asp:Label Text='<%# Container.DataItem("qty_in") %>'
                      runat="server"/>
                  </ItemTemplate>
                  <EditItemTemplate>
                    <asp:TextBox id="edit_qty_in"  
                      Text='<%# Container.DataItem("qty_in") %>'
                      runat="server"/>
                  </EditItemTemplate>
                </asp:TemplateColumn>
                <asp:TemplateColumn HeaderText="Quantity Out" >
                  <ItemTemplate>
                    <asp:Label Text='<%# Container.DataItem("qty_out") %>' 
                      runat="server"/>
                  </ItemTemplate>
                  <EditItemTemplate>
                    <asp:TextBox id="edit_qty_out"  
                      Text='<%# Container.DataItem("qty_out") %>'
                      runat="server"/>
                  </EditItemTemplate>
                </asp:TemplateColumn>
              </Columns>
              <HeaderStyle BackColor="DarkRed" 
                ForeColor="White" Font-Bold="true"/>
              <ItemStyle ForeColor="DarkSlateBlue"/>
              <AlternatingItemStyle BackColor="Beige"/>
            </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 (必填): </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="acode_value"
                    runat="server" /></td>
                </tr>
                <tr>
                  <td nowrap>Narration: </td>
                  <td><asp:TextBox id="anarr"  value = "" runat="server" /></td>
                </tr>
                <tr>
                  <td nowrap>Quantity In: </td>
                  <td><asp:TextBox id="aqty_in" value = 0  runat="server" /></td>
                </tr>
                <tr>
                  <td nowrap>Quantity Out: </td>
                  <td><asp:TextBox id="aqty_out" 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>
