jt
2021-06-10 5d0d028456874576560552f5a5c4e8b801786f11
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
 
using HH.WMS.CoreServer.DAL;
using HH.WMS.DAL;
using HH.WMS.Entitys;
using HH.WMS.Entitys.Algorithm;
using HH.WMS.Entitys.Basic;
using HH.WMS.Entitys.Common;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace HH.WMS.CoreServer.BLL
{
    public class InDataBLL : DapperBaseBLL
    {
        /// <summary>
        /// 
        /// </summary>
        /// <param name="tableName"></param>
        /// <returns></returns>
        public DataTable GetInMstData(string tableName)
        {
            return CreateDAL<InDataDAL>().GetInMstData(tableName);
        }
 
        public DataTable GetInDtlData(string collectionName, string cCode)
        {
            return CreateDAL<InDataDAL>().GetInDtlData(collectionName, cCode);
        }
 
        public bool AddRdrecord(DataTable RdrecordMstEntityTable, string dtlTableName, string opType)
        {
 
            //到货批次-自动生成
            List<TN_WM_LOT_INFOEntity> lotInfoList = new List<TN_WM_LOT_INFOEntity>();
            //主表集合
            List<TN_WM_INCREASE_INVENTORY_MSTEntity> inMstEntityList = new List<TN_WM_INCREASE_INVENTORY_MSTEntity>();
 
            string stockStr = WebApiManager.HttpWMS_Get("api/Common/NoTokenStockList");
            OperateResult or = JsonConvert.DeserializeObject<OperateResult>(stockStr);
            List<TN_AB_STOCKEntity> stockList = JsonConvert.DeserializeObject<List<TN_AB_STOCKEntity>>(or.Data.ToString());
            for (int i = 0; i < RdrecordMstEntityTable.Rows.Count; i++)
            {
                #region 拼接到货批次
                TN_WM_LOT_INFOEntity lotInfo = new TN_WM_LOT_INFOEntity();
                string postData = "{\"appCode\":\"WMS\",\"ruleName\":\"" + Constants.BatchNo + "\",\"orgId\":\"\",\"orgFlag\":\"0\"}";
                string lotCode = WebApiManager.HttpAutoBom_Post("Api/BillRule/GenBillNo", postData);
 
                lotInfo.CN_GUID = System.Guid.NewGuid().ToString().ToUpper();
                lotInfo.CN_S_LOT_CODE = lotCode;
                if (dtlTableName == "Rdrecords01")//只有采购入库才有供应商
                {
                    lotInfo.CN_S_VENDOR_NO = RdrecordMstEntityTable.Rows[i]["cVenCode"].ToString();
                }
                else
                {
                    lotInfo.CN_S_VENDOR_NO = "";
                }
                lotInfo.CN_S_CREATOR = "hh";
                lotInfo.CN_S_CREATOR_BY = "汉和信息";
                lotInfo.CN_T_CREATE = DateTime.Now;
                lotInfoList.Add(lotInfo);
                #endregion
                #region 拼接入库单主表
                string postDataOpNo = "{\"appCode\":\"WMS\",\"ruleName\":\"" + Constants.Rule_In + "\",\"orgId\":\"\",\"orgFlag\":\"0\"}";
                string opNo = WebApiManager.HttpAutoBom_Post("Api/BillRule/GenBillNo", postDataOpNo);
                if (opNo == "")
                {
                    Log.Info("生成入库单号失败!", "AutoBom接口GenBillNo未调通!");
                    return false;
                }
 
                TN_WM_INCREASE_INVENTORY_MSTEntity inMstEntity = new TN_WM_INCREASE_INVENTORY_MSTEntity();
                inMstEntity.CN_GUID = System.Guid.NewGuid().ToString().ToUpper();
                inMstEntity.CN_S_OP_NO = opNo;
                inMstEntity.CN_T_OPERATE = Convert.ToDateTime(RdrecordMstEntityTable.Rows[i]["dDate"].ToString());
                if (dtlTableName == "Rdrecords01")//只有采购入库才有供应商
                {
                    inMstEntity.CN_S_VENDOR_NO = RdrecordMstEntityTable.Rows[i]["cVenCode"].ToString();
                    inMstEntity.CN_S_VENDOR_NAME = RdrecordMstEntityTable.Rows[i]["cVenName"].ToString();
                }
                else
                {
                    inMstEntity.CN_S_VENDOR_NO = "";
                    inMstEntity.CN_S_VENDOR_NAME = "";
                }
                inMstEntity.CN_S_STOCK_CODE = stockList[0].CN_S_STOCK_CODE; //RdrecordMstEntityTable.Rows[i]["cWhCode"].ToString();
                inMstEntity.CN_S_FROM_NO = RdrecordMstEntityTable.Rows[i]["MainId"].ToString();
                inMstEntity.CN_S_AUDITOR = RdrecordMstEntityTable.Rows[i]["cChecker_Erp"].ToString();
                inMstEntity.CN_T_AUDIT_TIME = string.IsNullOrEmpty(RdrecordMstEntityTable.Rows[i]["dCheckDate_Erp"].ToString()) ? DateTime.Now : Convert.ToDateTime(RdrecordMstEntityTable.Rows[i]["dCheckDate_Erp"].ToString());
                inMstEntity.CN_T_CREATE = Convert.ToDateTime(RdrecordMstEntityTable.Rows[i]["dCreateDate"].ToString());
 
                //未传递     默认值
                inMstEntity.CN_S_LOT_NO = lotCode;
                inMstEntity.CN_S_STATE = Constants.State_Audit;
                inMstEntity.CN_S_OWNER = Constants.DefaultU8Owner;
                inMstEntity.CN_S_OP_TYPE = opType;
                inMstEntity.CN_S_OP_FROM = "U8";
                inMstEntity.CN_S_FROM_NO = RdrecordMstEntityTable.Rows[i]["cCode"].ToString();
                inMstEntity.CN_S_AUDIT_REMARK = "";
                inMstEntity.CN_S_OPERATOR = "汉和信息";
                inMstEntity.CN_S_CREATOR = "hh";
                inMstEntity.CN_S_CREATOR_BY = "汉和信息";
                inMstEntity.CN_S_MODIFY = "hh";
                inMstEntity.CN_S_MODIFY_BY = "汉和信息";
                inMstEntity.CN_T_MODIFY = DateTime.Now;
                inMstEntity.CN_S_NOTE = "U8对接导入";
                #endregion
 
                #region 拼接入库单子表
                //子表集合
                List<TN_WM_INCREASE_INVENTORY_DTLEntity> inDtlEntityList = new List<TN_WM_INCREASE_INVENTORY_DTLEntity>();
 
                DataTable RdrecordDtlEntityTable = BLLCreator.Create<InDataBLL>().GetInDtlData(dtlTableName, inMstEntity.CN_S_FROM_NO);
                for (int j = 0; j < RdrecordDtlEntityTable.Rows.Count; j++)
                {
                    AutoBomItemEntity itemenity = BLLCreator.Create<MongoBLL>().GetItemEntity(RdrecordDtlEntityTable.Rows[j]["cInvCode"].ToString()); ;
                    if (itemenity == null)
                    {
                        Log.Info("拼接子表失败!", "物料编码 " + RdrecordDtlEntityTable.Rows[j]["cInvCode"].ToString() + " 不存在或者未定版!");
                        return false;
                    }
 
                    TN_WM_INCREASE_INVENTORY_DTLEntity dtlEntity = new TN_WM_INCREASE_INVENTORY_DTLEntity();
                    dtlEntity.CN_GUID = System.Guid.NewGuid().ToString().ToUpper();
                    dtlEntity.CN_T_PRODUCTION = Convert.ToDateTime(RdrecordDtlEntityTable.Rows[j]["dMadeDate"].ToString());
                    dtlEntity.CN_T_MODIFY = DateTime.Now;
                    dtlEntity.CN_T_CREATE = DateTime.Now;
                    dtlEntity.CN_T_EXPIRATION = Convert.ToDateTime(RdrecordDtlEntityTable.Rows[j]["dvDate"].ToString());
 
                    //计算物料质保期
                    if (!string.IsNullOrEmpty(dtlEntity.CN_T_PRODUCTION.ToString()))
                    {
                        TimeSpan span = TimeSpan.Parse((dtlEntity.CN_T_EXPIRATION - dtlEntity.CN_T_PRODUCTION).ToString());
                        int dayDiff = span.Days;
                        dtlEntity.CN_F_WARRNTY_PARTS = dayDiff;
                        bool setPart = CreateDAL<MongoDAL>().UpdateWarrantyPart<AutoBomItemEntity>(itemenity.CN_S_ITEM_CODE, dayDiff, "TN_AB_ITEM");
                        if (!setPart)
                        {
                            Log.Info("UpdateWarrantyPart更新质保期失败!", "");
                            return false;
                        }
                    }
                    dtlEntity.CN_S_STATE = Constants.State_Audit;
                    dtlEntity.CN_S_SERIAL_NO = "";
                    dtlEntity.CN_S_PRODUCTION_BATCH = RdrecordDtlEntityTable.Rows[j]["cBatch"].ToString();
                    dtlEntity.CN_S_OP_NO = inMstEntity.CN_S_OP_NO;
                    dtlEntity.CN_S_MSTGUID = inMstEntity.CN_GUID;
                    dtlEntity.CN_S_MODEL = itemenity.CN_S_MODEL;
                    dtlEntity.CN_S_ITEM_NAME = itemenity.CN_S_ITEM_NAME;
                    dtlEntity.CN_S_ITEM_CODE = itemenity.CN_S_ITEM_CODE;
                    dtlEntity.CN_N_ROW_NO = Convert.ToInt32(RdrecordDtlEntityTable.Rows[j]["iRowNo"].ToString());
 
                    string unit = RdrecordDtlEntityTable.Rows[j]["cComUnitName"].ToString();
                    decimal qty = RdrecordDtlEntityTable.Rows[j]["iQuantity"].ToString() == null ? 0 : Convert.ToDecimal(RdrecordDtlEntityTable.Rows[j]["iQuantity"].ToString());
 
                    //计算单据的单位和入库数量
                    if (unit != itemenity.CN_S_MEASURE_UNIT)
                    {
                        if (string.IsNullOrEmpty(itemenity.CN_S_CONERSION))
                        {
                            Log.Info("拼接子表失败!", "请维护物料 " + itemenity.CN_S_ITEM_CODE + " 的换算比例!");
                            return false;
                        }
 
                        unit = itemenity.CN_S_MEASURE_UNIT;
                        decimal jishu = Convert.ToDecimal(itemenity.CN_S_CONERSION.Substring(0, itemenity.CN_S_CONERSION.IndexOf("-")));
                        if (jishu == 0) jishu = 1;
                        dtlEntity.CN_F_QUANTITY = Math.Floor(qty / jishu);
                    }
                    else
                    {
                        dtlEntity.CN_F_QUANTITY = qty;
                    }
                    dtlEntity.CN_S_MEASURE_UNIT = unit;
                    dtlEntity.CN_S_ARRIVAL_QTY = Math.Round(dtlEntity.CN_F_QUANTITY, 2).ToString();
                    dtlEntity.CN_C_IS_SCRAP = Constants.N;
                    dtlEntity.CN_C_IS_BALANCE = Constants.N;
                    dtlEntity.CN_S_ITEM_STATE = Constants.ItemState_WaitCheck;
 
                    dtlEntity.CN_S_CREATOR = "hh";
                    dtlEntity.CN_S_CREATOR_BY = "汉和信息";
                    dtlEntity.CN_S_MODIFY = "hh";
                    dtlEntity.CN_S_MODIFY_BY = "汉和信息";
                    dtlEntity.CN_T_MODIFY = DateTime.Now;
                    dtlEntity.CN_S_FIGURE_NO = "";
                    dtlEntity.CN_F_PRINTING_QTY = 0;
                    dtlEntity.CN_F_PLAN_PRICE = 0;
                    dtlEntity.CN_F_PLAN_MONEY = 0;
                    dtlEntity.CN_F_ALLOT_QTY = 0;
                    dtlEntity.CN_F_ADD_ONTRAY_QTY = 0;
                    dtlEntity.CN_F_WARRNTY_PARTS = 0;
                    dtlEntity.CN_F_UPSHELF_QTY = 0;
                    dtlEntity.CN_S_NOTE = "U8对接导入";
                    dtlEntity.CN_F_ACTUAL_PRICE = 0;
                    dtlEntity.CN_F_ACTUAL_MONEY = 0;
 
                    inDtlEntityList.Add(dtlEntity);
                }
                inMstEntity.DTLEntity = inDtlEntityList;
 
                inMstEntityList.Add(inMstEntity);
 
                #endregion
            }
 
            string resultStrMst = WebApiManager.HttpWMS_Post("Api/In/AddU8InData", JsonConvert.SerializeObject(inMstEntityList));
            if (resultStrMst == "")
            {
                Log.Info("保存主子表、量表接口未调通!", "Api/In/AddU8InData");
                return false;
            }
            OperateResult orMst = JsonConvert.DeserializeObject<OperateResult>(resultStrMst);
            if (!orMst.Success)
            {
                Log.Info("保存主子表、量表接口调用失败!", orMst.Msg);
                return false;
            }
 
            string resultStrLot = WebApiManager.HttpWMS_Post("Api/LotInfo/AddLotInfo", JsonConvert.SerializeObject(lotInfoList));
            if (resultStrLot == "")
            {
                Log.Info("批次接口未调通!", "Api/LotInfo/AddLotInfo");
                return false;
            }
            OperateResult orLot = JsonConvert.DeserializeObject<OperateResult>(resultStrLot);
            if (!orMst.Success)
            {
                Log.Info("批次接口调用失败!", orMst.Msg);
                return false;
            }
 
            return orMst.Success && orLot.Success;
        }
 
        /// <summary>
        /// 更新单据表WMS确认状态
        /// </summary>
        /// <param name="RdrecordMstEntityTable"></param>
        /// <param name="collectionName"></param>
        /// <returns></returns>
        public SqlExecuteResult UpdateRdrecordCheckState(DataTable RdrecordMstEntityTable, string collectionName)
        {
            return CreateDAL<InDataDAL>().UpdateRdrecordCheckState(RdrecordMstEntityTable, collectionName);
        }
    }
}