/// <summary>
 /// 更新保存单据的表头和内容
 /// </summary>
 /// <param name="e">待更新的数据内容</param>
 private bool UpdateBill(FormViewUpdateEventArgs e)
 {
     //当前单据号
     string billNum = e.Keys["bill_num"].ToString();
     //实例化数据适配器
     using (var daHead = new t_delivery_bill_head_sampleTableAdapter())
     using (var daContent = new t_delivery_bill_content_sampleTableAdapter())
     using (var daLotCard = new t_delivery_bill_lot_card_sampleTableAdapter())
     //取得数据库连接
     using (var conn = daHead.Connection)
     {
         //打开数据库连接
         conn.Open();
         //事务
         using (var tran = conn.BeginTransaction())
         {
             //设置数据库连接
             daHead.Connection = conn;
             daContent.Connection = conn;
             daLotCard.Connection = conn;
             //设置事务
             daHead.Transaction = tran;
             daContent.Transaction = tran;
             daLotCard.Transaction = tran;
             try
             {
                 //获取单据清单内容数据
                 var tabContent = daContent.GetDataByBillNum(billNum);
                 //执行更新订单已交货数和未交货数
                 foreach (DataSetSampleDeliveryBillMgr.t_delivery_bill_content_sampleRow row in tabContent.Rows)
                 {
                     daContent.UpdateOrderBalanceQty(-row.pcs_qty, row.order_id);
                 }
                 //先执行删除原来单据
                 daHead.Delete(billNum);
                 daLotCard.DeleteByBillNum(billNum);
                 //保存表头内容
                 daHead.Insert(
                     Convert.ToDateTime(e.NewValues["bill_date"]),
                     Convert.ToString(e.NewValues["cust_name"]),
                     billNum,
                     Convert.ToString(e.NewValues["remark"]),
                     Convert.ToString(e.NewValues["add_person"]),
                     Convert.ToDateTime(e.NewValues["add_time"]),
                     Convert.ToDateTime(e.NewValues["last_change_time"])
                 );
                 //循环保存表身内容
                 for (int i = 0; i < 6; i++)
                 {
                     //当前行号
                     byte rowId = Convert.ToByte(i + 1);
                     //取得之前送货单当前行内容
                     var tab = daContent.GetDataByBillNumAndRowId(billNum, rowId);
                     //检测是否存在数据
                     if (tab.Rows.Count > 0)
                     {
                         var row = (DataSetSampleDeliveryBillMgr.t_delivery_bill_content_sampleRow)tab.Rows[0];
                         //取得之前写入的lot卡清单
                         string strLotCardListOld = row.lot_id_list.Trim();
                         if (strLotCardListOld.Length > 0)
                         {
                             //取得之前写入的lot卡清单
                             string productNumOld = row.product_num.Trim();
                             //之前的lot卡清单写回
                             LotCardListToWaitInStoreBalance(tran, strLotCardListOld, productNumOld);
                             //删除该条单据清单行数据
                             daContent.DeleteByBillNumAndRowId(billNum, rowId);
                         }
                     }
                     //当前数据所在的行
                     var trContentRow = (TableRow)tabDataListSon.FindControl("trContentRow" + i);
                     //获取该行样板编号
                     var txtProductNum = (TextBox)trContentRow.Cells[3].FindControl("txtProductNum" + i);
                     //检测样板编号存在就保存该行
                     if (txtProductNum == null)
                     {
                         throw new Exception("在第 " + rowId.ToString() + " 行未找到样板编号文本框!");
                     }
                     //交货数量文本框
                     var txtPcsQty = (TextBox)trContentRow.Cells[5].FindControl("txtPcsQty" + i);
                     int iPcsQty;
                     if (!int.TryParse(txtPcsQty.Text, out iPcsQty))
                     {
                         iPcsQty = 0;
                     }
                     var txtFocPcsQty = (TextBox)trContentRow.Cells[6].FindControl("txtFocPcsQty" + i);
                     int iFocPcsQty;
                     if (!int.TryParse(txtFocPcsQty.Text.Trim(), out iFocPcsQty))
                     {
                         iFocPcsQty = 0;
                     }
                     if (iPcsQty + iFocPcsQty <= 0)
                     {
                         continue;
                     }
                     //保存样板编号到变量
                     string productNum = txtProductNum.Text;
                     //输入的样板编号
                     if (productNum.Length <= 0)
                     {
                         throw new Exception("第 " + rowId.ToString() + " 行的样板编号文本框未输入内容!");
                     }
                     //其他各参数
                     var txtOrderId = (TextBox)trContentRow.Cells[1].FindControl("txtOrderId" + i);
                     var txtOrderNum = (TextBox)trContentRow.Cells[2].FindControl("txtOrderNum" + i);
                     var txtCustNum = (TextBox)trContentRow.Cells[4].FindControl("txtCustNum" + i);
                     //当前lot卡清单内容保存到的隐藏对象
                     var hdLotCardList = (HtmlInputHidden)tabDataListSon.FindControl("hdLotCardList" + i);
                     string strLotCardList = hdLotCardList.Value.Replace("|l|", "<").Replace("|g|", ">");
                     var txtRemark = (TextBox)trContentRow.Cells[7].FindControl("txtRemark" + i);
                     //执行扣减待入仓结存并将记录写入lot卡清单
                     DeductionWaitInStoreBalance(
                         tran,
                         strLotCardList,
                         billNum,
                         rowId,
                         productNum
                     );
                     //执行插入行
                     daContent.Insert(
                         billNum,
                         rowId,
                         txtOrderId.Text,
                         txtOrderNum.Text,
                         productNum,
                         Convert.ToString(txtCustNum.Text),
                         iPcsQty,
                         iFocPcsQty == 0 ? null : (int?)iFocPcsQty,
                         Convert.ToString(txtRemark.Text),
                         strLotCardList
                     );
                     //执行更新订单已交货数和未交货数
                     daContent.UpdateOrderBalanceQty(iPcsQty, txtOrderId.Text);
                 }
                 //提交事务
                 tran.Commit();
                 //返回成功
                 return true;
             }
             catch (Exception ex)
             {
                 //回滚事务
                 tran.Rollback();
                 //抛出错误
                 throw ex;
             }
         }
     }
 }
 /// <summary>
 /// 根据单号从数据库中删除单据的表头和内容
 /// </summary>
 /// <param name="billNum">单据号</param>
 private bool DeleteBillByBillNum(string billNum)
 {
     //实例化数据适配器
     using (var daHead = new t_delivery_bill_head_sampleTableAdapter())
     using (var daContent = new t_delivery_bill_content_sampleTableAdapter())
     //取得数据库连接
     using (var conn = daHead.Connection)
     {
         //打开数据库连接
         conn.Open();
         //事务
         using (var tran = conn.BeginTransaction())
         {
             //设置数据库连接
             daContent.Connection = conn;
             //设置事务
             daHead.Transaction = tran;
             daContent.Transaction = tran;
             try
             {
                 //取得之前单据清单记录
                 var tab = daContent.GetDataByBillNum(billNum);
                 //遍历各行
                 foreach (DataSetSampleDeliveryBillMgr.t_delivery_bill_content_sampleRow row in tab.Rows)
                 {
                     //取得之前写入的lot卡清单
                     string strLotCardListOld = row.lot_id_list.Trim();
                     //是否存在lot卡清单
                     if (strLotCardListOld.Length > 0)
                     {
                         //当前样板编号
                         string productNum = row.product_num;
                         //之前的清单写回
                         LotCardListToWaitInStoreBalance(tran, strLotCardListOld, productNum);
                     }
                     //当前交货的数量
                     var pcsQty = row.pcs_qty;
                     if (!row.Isfoc_pcs_qtyNull())
                     {
                         pcsQty += row.foc_pcs_qty;
                     }
                     //执行更新订单已交货数和未交货数
                     daContent.UpdateOrderBalanceQty(-pcsQty, row.order_id);
                 }
                 //执行删除
                 daHead.Delete(billNum);
                 daContent.DeleteByBillNum(billNum);
                 //提交事务
                 tran.Commit();
                 //返回成功
                 return true;
             }
             catch (Exception ex)
             {
                 //回滚事务
                 tran.Rollback();
                 //抛出错误
                 throw ex;
             }
         }
     }
 }
 /// <summary>
 /// 根据单号从数据库中删除单据的表头和内容
 /// </summary>
 /// <param name="billNum">单据号</param>
 private bool DeleteBillByBillNum(string billNum)
 {
     //实例化数据适配器
     using (var daHead = new t_delivery_bill_head_sampleTableAdapter())
     using (var daContent = new t_delivery_bill_content_sampleTableAdapter())
     using (var daLotCard = new t_delivery_bill_lot_card_sampleTableAdapter())
     //取得数据库连接
     using (var conn = daHead.Connection)
     {
         //打开数据库连接
         conn.Open();
         //事务
         using (var tran = conn.BeginTransaction())
         {
             //设置数据库连接
             daHead.Connection = daContent.Connection = daLotCard.Connection = conn;
             //设置事务
             daHead.Transaction = daContent.Transaction = daLotCard.Transaction = tran;
             try
             {
                 //记录需要检测更新首次送货记录工具费的订单序号清单
                 var lOrderId = new List<string>();
                 //取得之前单据清单记录
                 var tabContent = daContent.GetDataByBillNum(billNum);
                 //遍历各行
                 foreach (DataSetSampleDeliveryBillMgr.t_delivery_bill_content_sampleRow row in tabContent.Rows)
                 {
                     //之前是否有设置为需要记录工具费
                     if (!row.Isneed_tool_amountNull() && row.need_tool_amount)
                     {
                         //记录到清单
                         if (!lOrderId.Contains(row.order_id))
                         {
                             lOrderId.Add(row.order_id);
                         }
                     };
                     //取得之前写入的批量卡清单
                     string strLotCardListOld = row.lot_id_list.Trim();
                     //是否存在批量卡清单
                     if (strLotCardListOld.Length > 0)
                     {
                         //当前样板编号
                         string productNum = row.product_num;
                         //之前的清单写回
                         LotCardListToWaitInStoreBalance(tran, billNum, null, strLotCardListOld, productNum);
                     }
                     //当前交货的数量
                     var pcsQty = row.pcs_qty;
                     if (!row.Isfoc_pcs_qtyNull())
                     {
                         pcsQty += row.foc_pcs_qty;
                     }
                     //执行更新订单已交货数和未交货数
                     daContent.UpdateOrderBalanceQty(-pcsQty, row.order_id);
                 }
                 //执行删除
                 daHead.Delete(billNum);
                 daContent.DeleteByBillNum(billNum);
                 daLotCard.DeleteByBillNum(billNum);
                 //更新需要显示工具费的行
                 UpdateNeedToolAmount(daContent, lOrderId);
                 //提交事务
                 tran.Commit();
                 //返回成功
                 return true;
             }
             catch (Exception ex)
             {
                 //回滚事务
                 tran.Rollback();
                 //抛出错误
                 throw ex;
             }
         }
     }
 }
 /// <summary>
 /// 新增保存单据的表头和内容
 /// </summary>
 /// <param name="e">待插入的数据内容</param>
 private bool InsertBill(FormViewInsertEventArgs e)
 {
     //当前单据号
     string billNum = e.Values["bill_num"].ToString();
     //实例化数据适配器
     using (var daHead = new t_delivery_bill_head_sampleTableAdapter())
     using (var daContent = new t_delivery_bill_content_sampleTableAdapter())
     //取得数据库连接
     using (var conn = daHead.Connection)
     {
         //打开数据库连接
         conn.Open();
         //事务
         using (var tran = conn.BeginTransaction())
         {
             //设置数据库连接
             daHead.Connection = daContent.Connection = conn;
             //设置事务
             daHead.Transaction = daContent.Transaction = tran;
             try
             {
                 //记录需要检测更新首次送货记录工具费的订单序号清单
                 var lOrderId = new List<string>();
                 //取得之前单据清单记录
                 var tabContent = daContent.GetDataByBillNum(billNum);
                 //遍历各行
                 foreach (DataSetSampleDeliveryBillMgr.t_delivery_bill_content_sampleRow row in tabContent.Rows)
                 {
                     //执行更新订单已交货数和未交货数
                     daContent.UpdateOrderBalanceQty(-row.pcs_qty, row.order_id);
                     //之前是否有设置为需要记录工具费
                     if (!row.Isneed_tool_amountNull() && row.need_tool_amount)
                     {
                         //记录到清单
                         if (!lOrderId.Contains(row.order_id))
                         {
                             lOrderId.Add(row.order_id);
                         }
                     }
                 }
                 //先执行删除单据
                 daHead.Delete(billNum);
                 daContent.DeleteByBillNum(billNum);
                 //保存表头内容
                 daHead.Insert(
                     Convert.ToDateTime(e.Values["bill_date"]),
                     Convert.ToString(e.Values["cust_name"]),
                     billNum,
                     Convert.ToString(e.Values["remark"]),
                     Convert.ToString(e.Values["add_person"]),
                     Convert.ToDateTime(e.Values["add_time"]),
                     Convert.ToDateTime(e.Values["last_change_time"])
                 );
                 //循环保存表身内容
                 for (int i = 0; i < 6; i++)
                 {
                     //当前行号
                     byte rowId = Convert.ToByte(i + 1);
                     //当前数据所在的行
                     var trContentRow = (TableRow)tabDataListSon.FindControl("trContentRow" + i);
                     //获取该行样板编号
                     var txtProductNum = (TextBox)trContentRow.FindControl("txtProductNum" + i);
                     //检测样板编号存在就保存该行
                     if (txtProductNum == null)
                     {
                         throw new Exception("在第 " + rowId.ToString() + " 行未找到样板编号文本框!");
                     }
                     //交货数量文本框
                     var txtPcsQty = (TextBox)trContentRow.FindControl("txtPcsQty" + i);
                     int iPcsQty;
                     if (!int.TryParse(txtPcsQty.Text, out iPcsQty))
                     {
                         iPcsQty = 0;
                     }
                     var txtFocPcsQty = (TextBox)trContentRow.FindControl("txtFocPcsQty" + i);
                     int iFocPcsQty;
                     if (!int.TryParse(txtFocPcsQty.Text.Trim(), out iFocPcsQty))
                     {
                         iFocPcsQty = 0;
                     }
                     if (iPcsQty + iFocPcsQty <= 0)
                     {
                         continue;
                     }
                     //保存样板编号到变量
                     string productNum = txtProductNum.Text;
                     //输入的样板编号
                     if (productNum.Length <= 0)
                     {
                         throw new Exception("第 " + rowId.ToString() + " 行的样板编号文本框未输入内容!");
                     }
                     //其他各控件
                     var txtOrderId = (TextBox)trContentRow.FindControl("txtOrderId" + i);
                     var txtCustNum = (TextBox)trContentRow.FindControl("txtCustNum" + i);
                     var txtOrderNum = (TextBox)trContentRow.FindControl("txtOrderNum" + i);
                     var txtRemark = (TextBox)trContentRow.FindControl("txtRemark" + i);
                     //当前批量卡清单内容保存到的隐藏对象
                     var hdLotCardList = (HtmlInputHidden)tabDataListSon.FindControl("hdLotCardList" + i);
                     string strLotCardList = hdLotCardList.Value.Replace("|l|", "<").Replace("|g|", ">");
                     //订单序号
                     var orderId = txtOrderId.Text.Trim();
                     //执行插入行
                     daContent.Insert(
                         billNum,
                         rowId,
                         orderId,
                         null,
                         productNum,
                         txtCustNum.Text,
                         txtOrderNum.Text,
                         null,
                         null,
                         iPcsQty,
                         iFocPcsQty == 0 ? null : (int?)iFocPcsQty,
                         null,
                         txtRemark.Text,
                         strLotCardList,
                         false
                     );
                     //执行扣减待入仓结存并将记录写入批量卡清单
                     DeductionWaitInStoreBalance(
                         tran,
                         strLotCardList,
                         billNum,
                         rowId,
                         productNum
                     );
                     //执行更新订单已交货数和未交货数
                     daContent.UpdateOrderBalanceQty(iPcsQty, orderId);
                     //记录到清单
                     if (!lOrderId.Contains(orderId))
                     {
                         lOrderId.Add(orderId);
                     }
                 }
                 //更新需要显示工具费的行
                 UpdateNeedToolAmount(daContent, lOrderId);
                 //提交事务
                 tran.Commit();
                 //返回成功
                 return true;
             }
             catch (Exception ex)
             {
                 //回滚事务
                 tran.Rollback();
                 //抛出错误
                 throw ex;
             }
         }
     }
 }
Ejemplo n.º 5
0
 /// <summary>
 /// 执行删除数据并处理相关数据一致性
 /// </summary>
 /// <param name="e">传入的带有数据的事件参数</param>
 /// <returns></returns>
 private bool DeleteData(ListViewDeleteEventArgs e)
 {
     //实例化数据适配器
     using (var daContent = new t_delivery_bill_content_sampleTableAdapter())
     //取得数据库连接
     using (var conn = daContent.Connection)
     {
         //打开数据库连接
         conn.Open();
         //事务
         using (var tran = conn.BeginTransaction())
         {
             //设置事务
             daContent.Transaction = tran;
             try
             {
                 //当前样板送货单号
                 string billNum = e.Keys[0].ToString();
                 //取得之前样板送货单清单记录
                 var tab = daContent.GetDataByBillNum(billNum);
                 //检测总共含有的清单行数
                 if (tab.Rows.Count <= 1)
                 {
                     //执行删除表头数据
                     using (var daHead = new t_delivery_bill_head_sampleTableAdapter())
                     {
                         //设置数据库连接
                         daHead.Connection = conn;
                         //设置事务
                         daHead.Transaction = tran;
                         //执行删除
                         daHead.Delete(billNum);
                     }
                 }
                 //当前行号
                 byte rowId = Convert.ToByte(e.Keys[1]);
                 //获取当前行的数据内容
                 tab = daContent.GetDataByBillNumAndRowId(billNum, rowId);
                 //检测是否存在数据
                 if (tab.Rows.Count > 0)
                 {
                     //首行数据
                     var row = (DataSetSampleDeliveryBillMgr.t_delivery_bill_content_sampleRow)tab.Rows[0];
                     //取得之前写入的批量卡清单
                     string strLotCardListOld = row.lot_id_list.Trim();
                     //是否存在批量卡清单
                     if (strLotCardListOld.Length > 0)
                     {
                         //当前生产编号
                         string productNum = row.product_num;
                         //之前的批量卡清单写回
                         LotCardListToWaitInStoreBalance(tran, billNum, rowId, strLotCardListOld, productNum);
                     }
                     //当前交货的数量
                     var pcsQty = row.pcs_qty;
                     if (!row.Isfoc_pcs_qtyNull())
                     {
                         pcsQty += row.foc_pcs_qty;
                     }
                     //执行更新订单已交货数和未交货数
                     daContent.UpdateOrderBalanceQty(-pcsQty, row.order_id);
                     //执行删除
                     daContent.DeleteByBillNumAndRowId(billNum, rowId);
                     //记录需要检测更新首次送货记录工具费的订单序号清单
                     var lOrderId = new List<string>();
                     //之前是否有设置为需要记录工具费
                     if (!row.Isneed_tool_amountNull() && row.need_tool_amount)
                     {
                         //记录到清单
                         if (!lOrderId.Contains(row.order_id))
                         {
                             lOrderId.Add(row.order_id);
                         }
                     };
                     //更新需要显示工具费的行
                     UpdateNeedToolAmount(daContent, lOrderId);
                 }
                 //提交事务
                 tran.Commit();
                 //返回成功
                 return true;
             }
             catch (Exception ex)
             {
                 //回滚事务
                 tran.Rollback();
                 //抛出错误
                 throw ex;
             }
         }
     }
 }