ch
2022-05-30 f9ef44a67fbf73f073efe65600bc25e204fde04f
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
 
namespace DAL
{
    public  class ClsCB_ItemMoneyBill:DBUtility.ClsXt_BaseBill
    {
        public Model.ClsCB_ItemMoneyBillMain omodel = new Model.ClsCB_ItemMoneyBillMain();
        public List<Model.ClsCB_ItemMoneyBillSub> DetailColl = new List<Model.ClsCB_ItemMoneyBillSub>();
 
        public ClsCB_ItemMoneyBill()
        {
            base.MvarItemKeySub = "CB_ItemMoneyBillSub";
            base.MvarItemKeySub2 = "";
            base.MvarItemKeySub3 = "";
            base.MvarItemKeySub4 = "";
            base.MvarItemKey="CB_ItemMoneyBillMain";
            base.MvarReportTitle="费用单";
            base.BillType="1205";
            base.HBillSubType = "1205";
 
        }
 
        #region 固定代码
 
        ~ClsCB_ItemMoneyBill()
        {
            DetailColl = null;
        }
       
        #endregion   自定义方法
 
        //删除关联
        public override void DeleteRelation(ref string sReturn, Int64 lngBillKey)
        {
            //收料通知单 退料通知单 关联数量
            try
            {
                if (omodel.HMainSourceBillType == "发货通知单")
                {
                    oCn.RunProc("exec h_p_Xs_UpDateSeOutStockRelation_Del " + lngBillKey.ToString());
                }
                else if (omodel.HMainSourceBillType == "退货通知单")
                {
                    oCn.RunProc("exec h_p_Xs_UpDateSeOutStockRelation_Back_Del " + lngBillKey.ToString());
                }
                else
                {
 
                }
            }
            catch (Exception e)
            {
                throw (e);
            }
        }
 
        //新增关联
        public override void AddNewRelation(ref string sReturn, Int64 lngBillKey)
        {
            //收料通知单 退料通知单 关联数量
            try
            {
                if (omodel.HMainSourceBillType == "发货通知单")
                {
                    oCn.RunProc("exec h_p_Xs_UpDateSeOutStockRelation_Add " + lngBillKey.ToString());
                }
                else if (omodel.HMainSourceBillType == "退货通知单")
                {
                    oCn.RunProc("exec h_p_Xs_UpDateSeOutStockRelation_Back_Add " + lngBillKey.ToString());
                }
                else
                {
 
                }
            }
            catch (Exception e)
            {
                throw (e);
            }
        }
 
        //修改单据
        public override bool ModifyBill(Int64 lngBillKey, ref string sReturn)
        {
            try
            {
                //
                oCn.BeginTran();
                //更新主表
                oCn.RunProc("UpDate CB_ItemMoneyBillMain set  " +
                " HBillNo='" + omodel.HBillNo + "'" +  //固定赋值===============
                ",HDate='" + omodel.HDate + "'" +
                ",HYear='" + omodel.HYear.ToString() + "'" +
                ",HPeriod='" + omodel.HPeriod.ToString() + "'" +
                ",HRemark='" + omodel.HRemark + "'" +
                ",HUpDater='" + DBUtility.ClsPub.CurUserName + "'" +
                ",HUpDateDate=getdate()" +
                    //========================================
                ",HEmpID=" + omodel.HEmpID.ToString() +
                ",HDeptID=" + omodel.HDeptID.ToString() +
                ",HICMOInterID=" + omodel.HICMOInterID.ToString() +
                ",HMaterID=" + omodel.HMaterID.ToString() +
                ",HICMOBillNo='" + omodel.HICMOBillNo + "'" +
                ",HExplanation='" + omodel.HExplanation + "'" +
                ",HInnerBillNo='" + omodel.HInnerBillNo + "'" +
                ",HBeginDate='" + omodel.HBeginDate + "'" +
                ",HEndDate='" + omodel.HEndDate + "'" +
                " where HInterID=" + lngBillKey.ToString());
                //删除关联
                DeleteRelation(ref sReturn, lngBillKey);
                //删除子表
                DeleteBillSub(lngBillKey);
                //插入子表
                omodel.HInterID = lngBillKey;
                foreach (Model.ClsCB_ItemMoneyBillSub oSub in DetailColl)
                {
                    oCn.RunProc("Insert into CB_ItemMoneyBillSub " +
                      " (HInterID,HEntryID,HCloseMan,HEntryCloseDate,HCloseType,HRemark" +
                      ",HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HRelationQty,HRelationMoney" +
                      ",HCostItemID,HDeptID,HQty,HPrice,HMoney,HMaterID,HDate" +
                      ") values("
                      + omodel.HInterID.ToString() + "," + oSub.HEntryID.ToString() + ",'" + oSub.HCloseMan + "','" + oSub.HEntryCloseDate.ToShortDateString() + "'," + Convert.ToString(oSub.HCloseType ? 1 : 0) + ",'" + oSub.HRemark + "'" +
                      "," + oSub.HSourceInterID.ToString() + "," + oSub.HSourceEntryID.ToString() + ",'" + oSub.HSourceBillNo + "','" + oSub.HSourceBillType + "'," + oSub.HRelationQty.ToString() + "," + oSub.HRelationMoney.ToString() +
                      "," + oSub.HCostItemID.ToString() + "," + oSub.HDeptID.ToString() + "," + oSub.HQty.ToString() + "," + oSub.HPrice.ToString() + "," + oSub.HMoney.ToString() + "," + oSub.HMaterID.ToString() +
                      ",'" + oSub.HDate + "'" +
                      ") ");
                }
                //更新订单关联数量
                AddNewRelation(ref sReturn, omodel.HInterID);
                //控制关联数量
 
                //
                sReturn = "修改单据成功!";
                oCn.Commit();
                return true;
            }
            catch (Exception e)
            {
                sReturn = e.Message;
                oCn.RollBack();
                throw (e);
            }
        }
        //新增单据
        public override bool AddBill(ref string sReturn)
        {
            try
            {
                //得到mainid
                omodel.HInterID = DBUtility.ClsPub.CreateBillID(BillType, ref DBUtility.ClsPub.sExeReturnInfo);
                omodel.HBillNo = DBUtility.ClsPub.CreateBillCode(BillType, ref DBUtility.ClsPub.sExeReturnInfo, true);
                //若MAINDI重复则重新获取
                if (IsExistBillNo(ref DBUtility.ClsPub.sExeReturnInfo, omodel.HBillNo, Pub_Class.ClsPub.Enum_BillStatus.BillStatus_AddNew, omodel.HInterID))
                {
                    omodel.HBillNo = DBUtility.ClsPub.CreateBillCode(BillType, ref DBUtility.ClsPub.sExeReturnInfo, true);
                }
                if (IsExistMainID(ref DBUtility.ClsPub.sExeReturnInfo, omodel.HInterID, Pub_Class.ClsPub.Enum_BillStatus.BillStatus_AddNew))
                {
                    omodel.HInterID = DBUtility.ClsPub.CreateBillID(BillType, ref DBUtility.ClsPub.sExeReturnInfo);
                }
                //
                oCn.BeginTran();
                //主表
                oCn.RunProc("Insert Into CB_ItemMoneyBillMain   " +
                "(HBillType,HInterID,HBillNo,HDate" +
                ",HYear,HPeriod,HRemark,HMaker,HMakeDate" +
                ",HEmpID,HDeptID,HICMOInterID,HMaterID,HICMOBillNo" +
                ",HBeginDate,HEndDate,HExplanation,HInnerBillNo,HFrom" +
                ") " +
                " values('" + this.BillType + "'," + omodel.HInterID.ToString() + ",'" + omodel.HBillNo + "','" + omodel.HDate + "'" + 
                ", " + omodel.HYear.ToString() + "," + omodel.HPeriod.ToString() + ",'" + omodel.HRemark + "','" + DBUtility.ClsPub.CurUserName + "',getdate()" +
                ", " + omodel.HEmpID.ToString() + "," + omodel.HDeptID.ToString() + "," + omodel.HICMOInterID.ToString() + "," + omodel.HMaterID.ToString() + ",'" + omodel.HICMOBillNo.ToString() +"'"+
                ", '" + omodel.HBeginDate.ToString() + "','" + omodel.HEndDate.ToString() + "','" + omodel.HExplanation + "','" + omodel.HInnerBillNo + "','" +omodel.HFrom+"'"+
                ") ");
                //插入子表
                foreach (Model.ClsCB_ItemMoneyBillSub oSub in DetailColl)
                {
                    oCn.RunProc("Insert into CB_ItemMoneyBillSub " +
                    " (HInterID,HEntryID,HCloseMan,HEntryCloseDate,HCloseType,HRemark" +
                    ",HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HRelationQty,HRelationMoney" +
                    ",HCostItemID,HDeptID,HQty,HPrice,HMoney,HMaterID" +
                    ") values("
                    + omodel.HInterID.ToString() + "," + oSub.HEntryID.ToString() + ",'" + oSub.HCloseMan + "','" + oSub.HEntryCloseDate.ToShortDateString() + "'," + Convert.ToString(oSub.HCloseType ? 1 : 0) + ",'" + oSub.HRemark + "'" +
                    "," + oSub.HSourceInterID.ToString() + "," + oSub.HSourceEntryID.ToString() + ",'" + oSub.HSourceBillNo + "','" + oSub.HSourceBillType + "'," + oSub.HRelationQty.ToString() + "," + oSub.HRelationMoney.ToString() +
                    "," + oSub.HCostItemID.ToString() + "," + oSub.HDeptID.ToString() + "," + oSub.HQty.ToString() + "," + oSub.HPrice.ToString() + "," + oSub.HMoney.ToString() + "," + oSub.HMaterID.ToString() +
                    ") ");
                }
                //更新订单关联数量
                //AddNewRelation(ref sReturn, omodel.HInterID);
                //控制关联数量
 
                //
                sReturn = "新增单据成功!";
                oCn.Commit();
                return true;
            }
            catch (Exception e)
            {
                sReturn = e.Message;
                oCn.RollBack();
                throw (e);
            }
        }
        //显示单据
        public override bool ShowBill(Int64 lngBillKey, ref string sReturn)
        {
            try
            {
                //查询主表
                DataSet Ds ;
                Ds = oCn.RunProcReturn("Select * from CB_ItemMoneyBillMain Where HInterID=" + lngBillKey.ToString(), "CB_ItemMoneyBillMain");
                if(Ds.Tables[0].Rows.Count==0)
                {
                    sReturn = "单据未找到!";
                    return false;
                }
                //固定赋值===========================================
                //omodel.HMainSourceBillType = DBUtility.ClsPub.isStrNull(Ds.Tables[0].Rows[0]["HMainSourceBillType"]);
                omodel.HYear = DBUtility.ClsPub.isLong(Ds.Tables[0].Rows[0]["HYear"]);
                omodel.HPeriod = DBUtility.ClsPub.isLong(Ds.Tables[0].Rows[0]["HPeriod"]);
                omodel.HBillType = DBUtility.ClsPub.isStrNull(Ds.Tables[0].Rows[0]["HBillType"]);
                //omodel.HBillSubType = DBUtility.ClsPub.isStrNull(Ds.Tables[0].Rows[0]["HBillSubType"]);
                omodel.HInterID = DBUtility.ClsPub.isLong(Ds.Tables[0].Rows[0]["HInterID"]);
                omodel.HDate = DBUtility.ClsPub.isDate(Ds.Tables[0].Rows[0]["HDate"]);
                omodel.HBillNo = Ds.Tables[0].Rows[0]["HBillNo"].ToString().Trim();
                omodel.HBillStatus = DBUtility.ClsPub.isInt(Ds.Tables[0].Rows[0]["HBillStatus"]);
                omodel.HCheckItemNowID = DBUtility.ClsPub.isLong(Ds.Tables[0].Rows[0]["HCheckItemNowID"]);
                omodel.HCheckItemNextID = DBUtility.ClsPub.isLong(Ds.Tables[0].Rows[0]["HCheckItemNextID"]);
                omodel.HCheckFlowID = DBUtility.ClsPub.isLong(Ds.Tables[0].Rows[0]["HCheckFlowID"]);
                omodel.HRemark = Ds.Tables[0].Rows[0]["HRemark"].ToString().Trim();
                omodel.HBackDate = Ds.Tables[0].Rows[0]["HBackDate"].ToString().Trim();
                omodel.HBacker = Ds.Tables[0].Rows[0]["HBacker"].ToString().Trim();
                omodel.HCheckDate = Ds.Tables[0].Rows[0]["HCheckDate"].ToString().Trim();
                omodel.HChecker = Ds.Tables[0].Rows[0]["HChecker"].ToString().Trim();
                omodel.HMaker = Ds.Tables[0].Rows[0]["HMaker"].ToString().Trim();
                omodel.HMakeDate = Ds.Tables[0].Rows[0]["HMakeDate"].ToString().Trim();
                omodel.HUpDateDate = Ds.Tables[0].Rows[0]["HUpDateDate"].ToString().Trim();
                omodel.HUpDater = Ds.Tables[0].Rows[0]["HUpDater"].ToString().Trim();
                omodel.HCloseDate = Ds.Tables[0].Rows[0]["HCloseDate"].ToString().Trim();
                omodel.HCloseMan = Ds.Tables[0].Rows[0]["HCloseMan"].ToString().Trim();
                omodel.HCloseType = DBUtility.ClsPub.isBool(Ds.Tables[0].Rows[0]["HCloseType"]);
                omodel.HDeleteDate = Ds.Tables[0].Rows[0]["HDeleteDate"].ToString().Trim();
                omodel.HDeleteMan = Ds.Tables[0].Rows[0]["HDeleteMan"].ToString().Trim();
                //========================================================
                omodel.HEmpID = DBUtility.ClsPub.isLong(Ds.Tables[0].Rows[0]["HEmpID"]);
                omodel.HDeptID = DBUtility.ClsPub.isLong(Ds.Tables[0].Rows[0]["HDeptID"]);
                omodel.HICMOInterID = DBUtility.ClsPub.isLong(Ds.Tables[0].Rows[0]["HICMOInterID"]);
                omodel.HMaterID = DBUtility.ClsPub.isLong(Ds.Tables[0].Rows[0]["HMaterID"]);
                omodel.HICMOBillNo = DBUtility.ClsPub.isStrNull(Ds.Tables[0].Rows[0]["HICMOBillNo"]);
                omodel.HExplanation = DBUtility.ClsPub.isStrNull(Ds.Tables[0].Rows[0]["HExplanation"]);
                omodel.HInnerBillNo = DBUtility.ClsPub.isStrNull(Ds.Tables[0].Rows[0]["HInnerBillNo"]);
                omodel.HBeginDate = DBUtility.ClsPub.isDate(Ds.Tables[0].Rows[0]["HBeginDate"]);
                omodel.HEndDate = DBUtility.ClsPub.isDate(Ds.Tables[0].Rows[0]["HEndDate"]);
                omodel.HFrom = DBUtility.ClsPub.isStrNull(Ds.Tables[0].Rows[0]["HFrom"]);
                //
                //
                
                //循环
                DataSet DsSub ;
                DsSub = oCn.RunProcReturn("Select * from CB_ItemMoneyBillSub Where HInterID=" + lngBillKey.ToString() + " order by HEntryID ", "CB_ItemMoneyBillSub");
                DetailColl.Clear();//清空
                for (int i = 0; i < DsSub.Tables[0].Rows.Count; i++)
                {
                    Model.ClsCB_ItemMoneyBillSub oSub = new Model.ClsCB_ItemMoneyBillSub();
                    // 固定赋值===============================================
                    oSub.HInterID = DBUtility.ClsPub.isLong(DsSub.Tables[0].Rows[i]["HInterID"]);
                    oSub.HEntryID = DBUtility.ClsPub.isLong(DsSub.Tables[0].Rows[i]["HEntryID"]);
                    oSub.HSourceInterID = DBUtility.ClsPub.isLong(DsSub.Tables[0].Rows[i]["HSourceInterID"]);
                    oSub.HSourceEntryID = DBUtility.ClsPub.isLong(DsSub.Tables[0].Rows[i]["HSourceEntryID"]);
                    oSub.HSourceBillType = DsSub.Tables[0].Rows[i]["HSourceBillType"].ToString().Trim();
                    oSub.HSourceBillNo = DsSub.Tables[0].Rows[i]["HSourceBillNo"].ToString().Trim();
                    oSub.HRelationQty = DBUtility.ClsPub.isDoule(DsSub.Tables[0].Rows[i]["HRelationQty"]);
                    oSub.HRelationMoney = DBUtility.ClsPub.isDoule(DsSub.Tables[0].Rows[i]["HRelationMoney"]); 
                    oSub.HCloseMan =  DBUtility.ClsPub.isStrNull(DsSub.Tables[0].Rows[i]["HCloseMan"]);
                    oSub.HCloseType = DBUtility.ClsPub.isBool(DsSub.Tables[0].Rows[i]["HCloseType"]);
                    oSub.HEntryCloseDate = DBUtility.ClsPub.isDate(DsSub.Tables[0].Rows[i]["HEntryCloseDate"]);
                    oSub.HRemark = DsSub.Tables[0].Rows[i]["HRemark"].ToString().Trim();
                    //===================================================
                    oSub.HMaterID = DBUtility.ClsPub.isLong(DsSub.Tables[0].Rows[i]["HMaterID"]);
                    oSub.HCostItemID = DBUtility.ClsPub.isLong(DsSub.Tables[0].Rows[i]["HCostItemID"]);
                    oSub.HDeptID = DBUtility.ClsPub.isLong(DsSub.Tables[0].Rows[i]["HDeptID"]);
                    oSub.HQty = DBUtility.ClsPub.isDoule(DsSub.Tables[0].Rows[i]["HQty"]);
                    oSub.HPrice = DBUtility.ClsPub.isDoule(DsSub.Tables[0].Rows[i]["HPrice"]);
                    oSub.HMoney = DBUtility.ClsPub.isDoule(DsSub.Tables[0].Rows[i]["HMoney"]);
                    oSub.HDate = DBUtility.ClsPub.isDate(DsSub.Tables[0].Rows[i]["HDate"]);
                    DetailColl.Add(oSub);
                }
                sReturn = "显示单据成功!";
                return true;
            }
            catch (Exception e)
            {
                sReturn = e.Message;
                throw (e);
            }
        }
 
        public bool IsSave(Int64 HMaterID, string sBatchNo, ref string sReturn)
        {
            try
            {
                //查询主表
                DataSet Ds;
                Ds = oCn.RunProcReturn("Select * from Crm_CusAppealBillSub Where HMaterID=" + HMaterID.ToString() + " and HBatchNo='" + sBatchNo + "'", "Crm_CusAppealBillSub");
                if (Ds.Tables[0].Rows.Count == 0)
                {
                    sReturn = "投诉记录单据未找到!";
                    return true;
                }
                DataSet DsSub;
                DsSub = oCn.RunProcReturn("Select HBackType from Crm_CusAppealDisposeBillMain Where HSourceInterID=" + DBUtility.ClsPub.isLong(Ds.Tables[0].Rows[0]["HInterID"]).ToString() + " and HSourceEntryID=" + DBUtility.ClsPub.isLong(Ds.Tables[0].Rows[0]["HEntryID"]).ToString(), "Crm_CusAppealDisposeBillMain");
                if (DsSub.Tables[0].Rows.Count == 0)
                {
                    sReturn = "投诉处理单据未找到!";
                    return true;
                }
                if (DBUtility.ClsPub.isStrNull(DsSub.Tables[0].Rows[0][0]) == "订错退回")
                {
                    sReturn = "投诉处理单 类型为 订错退回!";
                    return false;
                }
                sReturn = "显示单据成功!";
                return true;
            }
            catch (Exception e)
            {
                sReturn = e.Message;
                throw (e);
            }
        }
 
        public long Get_CusBySeOutStockBackBillNo(string sBatchNo)
        {
            try
            {
                //查询主表
                DataSet Ds;
                Ds = oCn.RunProcReturn("Select * from h_v_Xs_SeOutStockBackBillList_K3 Where 单据号='" + sBatchNo + "'", "h_v_Xs_SeOutStockBackBillList_K3");
                if (Ds.Tables[0].Rows.Count == 0)
                {
                    return 0;
                }
                //固定赋值===========================================
                //
                long HCusID = DBUtility.ClsPub.isLong(Ds.Tables[0].Rows[0]["HCusID"]);
                return HCusID;
            }
            catch (Exception e)
            {
                throw (e);
            }
        }
 
        public bool CheckStockQty(long HMaterID, long HWhID, string sBatchNo)
        {
            try
            {
                //查询主表
                DataSet Ds;
                Ds = oCn.RunProcReturn("Select 即时库存 from h_v_KF_ICInventoryList Where HMaterID=" + HMaterID.ToString() + " and HWHID=" + HWhID.ToString() + " and 批次='" + sBatchNo + "'", "h_v_KF_ICInventoryList");
                if (Ds.Tables[0].Rows.Count == 0)
                {
                    return false;
                }
                //判断是否负库存或0库存
                if (DBUtility.ClsPub.isDoule(Ds.Tables[0].Rows[0][0]) <= 0)
                {
                    return false;
                }
                return true;
            }
            catch (Exception e)
            {
                throw (e);
            }
        }
 
        public bool SplitQty(long HMainID, long HSubID, string HBillNo, double HSplitQty)
        {
            try
            {
                long HNewInterID = DBUtility.ClsPub.CreateBillID(BillType, ref DBUtility.ClsPub.sExeReturnInfo);
                string HMaker = DBUtility.ClsPub.CurUserName;
                //查询主表
                oCn.RunProc("exec h_p_CB_ItemMoneyBill_Split " + HMainID.ToString() + "," + HSubID.ToString() + ",'" + HBillNo + "'," + HSplitQty.ToString() + "," + HNewInterID.ToString() + ",'" + HMaker + "'");
                //
                return true;
            }
            catch (Exception e)
            {
                throw (e);
            }
        }
 
        //拆单
        public  bool HOrgBill(Int64 lngBillKey,ref string sReturn)
        {
            DataSet oDs = new DataSet();
            try
            {
                //得到mainid
                omodel.HInterID = DBUtility.ClsPub.CreateBillID(BillType, ref DBUtility.ClsPub.sExeReturnInfo);
                //oDs = oCn.RunProcReturn("select '" + omodel.HBillNo + "'+'-'+ltrim(COUNT(1)) from CB_ItemMoneyBillMain where HBillNo like '%" + omodel.HBillNo + "%'", "CB_ItemMoneyBillMain");   
                //if (oDs != null && oDs.Tables[0].Rows.Count != 0)
                //{
                //    omodel.HBillNo = oDs.Tables[0].Rows[0][0].ToString();
                //}
                oDs = oCn.RunProcReturn("select isnull(max(SUBSTRING(HBillNo, CHARINDEX ('-',HBillNo)+1,len(HBillNo)-CHARINDEX ('-',HBillNo))),0)+1 from CB_ItemMoneyBillMain where HBillNo like '%"+omodel.HBillNo+"-%'", "CB_ItemMoneyBillMain");
                if (oDs != null)
                {
                    if (oDs.Tables[0].Rows.Count != 0)
                    {
                        omodel.HBillNo = omodel.HBillNo + "-" + oDs.Tables[0].Rows[0][0].ToString();
                    }
                }
                //若MAINDI重复则重新获取
                if (IsExistBillNo(ref DBUtility.ClsPub.sExeReturnInfo, omodel.HBillNo, Pub_Class.ClsPub.Enum_BillStatus.BillStatus_AddNew, omodel.HInterID))
                {
                    omodel.HBillNo = DBUtility.ClsPub.CreateBillCode(BillType, ref DBUtility.ClsPub.sExeReturnInfo, true);
                }
                if (IsExistMainID(ref DBUtility.ClsPub.sExeReturnInfo, omodel.HInterID, Pub_Class.ClsPub.Enum_BillStatus.BillStatus_AddNew))
                {
                    omodel.HInterID = DBUtility.ClsPub.CreateBillID(BillType, ref DBUtility.ClsPub.sExeReturnInfo);
                }
                //
                oCn.BeginTran();
 
                oCn.RunProc("update CB_ItemMoneyBillMain set HOrgBillInterID=-1 where HInterID=" + lngBillKey);
                //主表
 
                oCn.RunProc("Insert Into CB_ItemMoneyBillMain   " +
                "(HBillType,HInterID,HBillNo,HDate,HOrgBillInterID" +
                ",HYear,HPeriod,HRemark,HMaker,HMakeDate" +
                ",HEmpID,HDeptID,HICMOInterID,HMaterID,HICMOBillNo" +
                ",HBeginDate,HEndDate,HExplanation,HInnerBillNo,HFrom" +
                ") " +
                " values('" + this.BillType + "'," + omodel.HInterID.ToString() + ",'" + omodel.HBillNo + "','" + omodel.HDate + "'," +lngBillKey+
                ", " + omodel.HYear.ToString() + "," + omodel.HPeriod.ToString() + ",'" + omodel.HRemark + "','" + DBUtility.ClsPub.CurUserName + "',getdate()" +
                ", " + omodel.HEmpID.ToString() + "," + omodel.HDeptID.ToString() + "," + omodel.HICMOInterID.ToString() + "," + omodel.HMaterID.ToString() + ",'" + omodel.HICMOBillNo.ToString() + "'" +
                ", '" + omodel.HBeginDate.ToString() + "','" + omodel.HEndDate.ToString() + "','" + omodel.HExplanation + "','" + omodel.HInnerBillNo + "','" + omodel.HFrom + "'" +
                ") ");
                //插入子表
                foreach (Model.ClsCB_ItemMoneyBillSub oSub in DetailColl)
                {
                    if (oSub.HOrgQty == 0)
                    {
                        continue;
                    }
                    if (oSub.HQty == oSub.HOrgQty)
                    {
                        oCn.RunProc("delete CB_ItemMoneyBillSub where HInterID=" + lngBillKey + " and HEntryID=" + oSub.HOrgBillEntryID);
                    }
                    else
                    {
                        oCn.RunProc("UPDATE CB_ItemMoneyBillSub set HOrgBillEntryID=" + oSub.HOrgBillEntryID + " ,HQty=" + (oSub.HQty - oSub.HOrgQty) + ",HMoney=HPrice*" + (oSub.HQty - oSub.HOrgQty) + " where HInterID=" + lngBillKey + " and HEntryID=" + oSub.HOrgBillEntryID);
                    }
 
                    oCn.RunProc("Insert into CB_ItemMoneyBillSub " +
                    " (HInterID,HEntryID,HCloseMan,HEntryCloseDate,HCloseType,HRemark" +
                    ",HSourceInterID,HSourceEntryID,HSourceBillNo,HSourceBillType,HRelationQty,HRelationMoney" +
                    ",HCostItemID,HDeptID,HQty,HPrice,HMoney,HMaterID,HDate,HOrgBillEntryID" +
                    ") values("
                    + omodel.HInterID.ToString() + "," + oSub.HEntryID.ToString() + ",'" + oSub.HCloseMan + "','" + oSub.HEntryCloseDate.ToShortDateString() + "'," + Convert.ToString(oSub.HCloseType ? 1 : 0) + ",'" + oSub.HRemark + "'" +
                    "," + oSub.HSourceInterID.ToString() + "," + oSub.HSourceEntryID.ToString() + ",'" + oSub.HSourceBillNo + "','" + oSub.HSourceBillType + "'," + oSub.HRelationQty.ToString() + "," + oSub.HRelationMoney.ToString() +
                    "," + oSub.HCostItemID.ToString() + "," + oSub.HDeptID.ToString() + "," + oSub.HOrgQty.ToString() + "," + oSub.HPrice.ToString() + "," + oSub.HOrgQty * oSub.HPrice + "," + oSub.HMaterID.ToString() +
                    ",'" + oSub.HDate + "'," +oSub.HOrgBillEntryID+
                    ") ");
                }
                //更新订单关联数量
                //AddNewRelation(ref sReturn, omodel.HInterID);
                //控制关联数量
 
                //
                sReturn = "拆单成功!";
                oCn.Commit();
                return true;
            }
            catch (Exception e)
            {
                sReturn = e.Message;
                oCn.RollBack();
                throw (e);
            }
        }
 
        
    }
}