Dim e As RequestEventArgs = args(0)
Dim wb As New weui
'删除订单明细
If e.GetValues.ContainsKey("deldid") Then
DataTables("销售明细").SQLDeleteFor("[_Identify] = " & e.GetValues("deldid"))
End If
'订单编辑
Dim pr As DataRow '订单
Dim srs As List(of DataRow) '订单明细集合
Dim Page As Integer '页码变量
If e.GetValues.ContainsKey("page") Then '如果地址中有page参数
Integer.TryParse(e.GetValues("page"), page) '提取page参数
End If
If e.GetValues.ContainsKey("oid") = False Then '如果没有传递订单编号,则新增与一个订单
pr = DataTables("销售单").SQLAddNew() '
pr("销售单号") = Functions.Execute("GetOrderID") '利用自定义函数GetOrderID为新增订单生成编号.
pr("制单时间") = Date.now
Else '如果传递了订单编号,则找出此订单进行编辑
pr = DataTables("销售单").SQLFind("销售单号='" & e.GetValues("oid") & "'")
If pr Is Nothing Then '多用户情况下,必须考虑其他用户删除订单的可能.
wb.InSertHtml("此订单已被其他用户删除!")
e.WriteString(wb.Build)
Return "" '必须返回
End If
srs = DataTables("销售明细").SQLSelect("销售单号='" & pr("销售单号") & "'") '获取订单明细
End If
Dim url As String = "edit.htm?page=" & page & "&oid=" & pr("销售单号") '传递页码和订单编号
wb.AddForm("","form1",url)
With wb.AddInputGroup("form1","ipg1",iif(e.GetValues.ContainsKey("oid"),"编辑订单","新增订单"))
With .AddInput("销售单号","销售单号","text")
.Value = pr("销售单号")
.Readonly = True
End With
.AddInput("所属业务员","所属业务员","text").Value = pr("所属业务员")
.AddInput("部门","部门","text").Value = pr("部门")
.AddInput("客户编号","客户编号","text").Value = pr("客户编号")
.AddInput("客户名称","客户名称","text").Value = pr("客户名称")
.AddInput("联系人","联系人","text").Value = pr("联系人")
.AddInput("电话","电话","text").Value = pr("电话")
.AddInput("已折扣金额","已折扣金额","number").Value = pr("已折扣金额")
.AddInput("优惠金额","优惠金额","number").Value = pr("优惠金额")
.AddInput("应收金额","应收金额","number").Value = pr("应收金额")
.AddInput("现金","现金","number").Value = pr("现金")
.AddInput("转账金额","转账金额","number").Value = pr("转账金额")
.AddInput("实收金额","实收金额","number").Value = pr("实收金额")
.AddInput("未收金额","未收金额","number").Value = pr("未收金额")
.AddInput("销售日期","销售日期","date").Value = pr("销售日期")
.AddInput("送货方式","送货方式","number").Value = pr("送货方式")
.AddInput("运费","运费","number").Value = pr("运费")
.AddInput("备注","备注","text").Value = pr("备注")
End With
'订单明细编辑
Dim mr As DataRow '要编辑的订单明细
Dim IsNew As Boolean '此变量用于标记是否要新增明细
If e.GetValues.ContainsKey("oid") = False OrElse e.GetValues.ContainsKey("addnext") '如果是新增订单,或这包括addnext参数
IsNew = True '将IsNew参数设置为True,表明需要新增订单明细
ElseIf e.GetValues.ContainsKey("did") Then '如果传递了订单明细主键
mr = DataTables("销售明细").SQLFind("[_Identify]=" & e.GetValues("did")) '找出此订单明细进行编辑
End If
If IsNew OrElse mr IsNot Nothing Then
With wb.AddInputGroup("form1","ipg2",iif(IsNew,"新增明细","编辑明细"))
.Attribute = "" '调用js函数,自动计算金额
If IsNew Then '如果是新增订单明细
With .AddInput("药材编码","药材编码","text").Attribute("")
End With
.AddInput("名称","名称","text")
.AddInput("数量","数量","number")
.AddInput("零售价","零售价","number")
.AddInput("零售金额","零售金额","number")
Else
.AddHiddenValue("DetailID",mr("_Identify")) '插入一个隐藏的订单明细主键,此值将随表单数据一并提交到服务器.
.AddInput("药材编码","药材编码","text")
.AddInput("名称","名称","text").Value = mr("名称")
With .AddInput("数量","数量","number")
.Step= "0.01"
.value = mr("数量")
End With
With .AddInput("零售价","零售价","number")
.Step= "0.01"
.value = mr("零售价")
End With
.AddInput("零售金额","零售金额","number").value = mr("零售金额") '后台没有金额列,要通过数量和零售价计算得出
End If
End With
End If
'生成订单明细表格
If e.GetValues.ContainsKey("oid") AndAlso srs.count > 0 Then '如果不是新增订单,且订单明细行数大于0,则生成订单明细表格.
With wb.AddTable("form1","detailTable") '为了区分,明细表的名字设为detailTable
.head.AddRow("药材编码","名称","数量","零售价","零售金额")
.ActiveSheet = "menu" '指定菜单
For Each sr As DataRow In srs
With .Body.AddRow(sr("药材编码"),sr("名称"),sr("数量"),sr("零售价"),sr("零售金额"))
''.AddCell(sr("数量") * sr("零售价"))
.Primarykey = sr("_Identify") '为此行指定主键值
End With
Next
Dim amt As Double
amt = DataTables("销售明细").sqlcompute("sum(零售金额)","销售单号='" & e.GetValues("oid") & "'")
With .Body.AddRow()
.Attribute = "style='background-color:#F0FFFF'"
.AddCell("合计: ","colspan='3'")
.AddCells("",amt)
End With
End With
'设计菜单
With wb.AddActionSheet("","menu")
.Add("mnudAdd", "增加明细").Attribute="" '调用js函数
.Add("mnuEdit", "编辑明细").Attribute =""
.Add("mnuDelete", "删除明细").Attribute ="""
.Add("mnuCancel","取消","",True)
End With
End If
With wb.AddDialog("","dlg1", "删除确认","您确定要删除当前明细吗?")
.AddButton("btnCancel","取消").Kind = 1
.AddButton("btnOK","确定").Attribute = ""
End With
With wb.AddButtonGroup("form1","btg1",False)
.Add("btn1", "增加明细", "submit").FormAction = url & "&addnext=true" '加上addnext参数,表示保存后进入增加明细状态
.Add("btn2", "保存", "submit") '正常提交,保存后进入编辑状态
If e.GetValues.ContainsKey("oid") = False Then '如过是新增订单
.Add("btn3", "取消", "button","list.htm?page=" & page & "&deloid=" & pr("销售单号")) '删除新增订单后返回列表
ElseIf IsNew OrElse mr IsNot Nothing Then '如果在给旧订单新增或修改明细,则直接返回编辑状态
.Add("btn3", "取消", "button",url) '返回编辑状态
Else
.Add("btn3", "返回", "button","list.htm?page=" & page) '返回列表
End If
End With
pr.Save() '必须保存,而且必须在最后保存,因为SQLAddNew增加的行,保存之后就会销毁,无法再调用
If e.PostValues.Count > 0 Then '如果是通过提交按钮访问,则给一个已经保存的提示给用户,时长500毫秒
wb.AddToast("","t1", "已经保存",0).Msec= 500
End If
wb.AppendHTML("<script src='./lib/order.js'></script>") '引入脚本文件
e.WriteString(wb.Build) '生成网页