using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SyntacticSugar.constant;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using WebAPI.Models;
namespace WebAPI.Controllers
{
///
/// 在库检验单控制器
///
public class QC_InStockCheckBillController : ApiController
{
public DataSet ds = new DataSet();
private json objJsonResult = new json();
SQLHelper.ClsCN oCN = new SQLHelper.ClsCN();
Pub_Class.ClsXt_SystemParameter oSystemParameter = new Pub_Class.ClsXt_SystemParameter();
#region 在库检验单提交单据
[Route("QC_InStockCheckBill/saveBill")]
[HttpPost]
public Object saveBill([FromBody] JObject sMainSub)
{
// 此处 表单结构: {sMain: sMain, sSub: {materNum: [BarCodeInfo]}}
JObject sMain = sMainSub["sMain"] as JObject;
JObject sSub = sMainSub["sSub"] as JObject;
oCN.BeginTran();
try
{
int i = 1;
// 插入主表
oCN.RunProc($@"
insert into QC_InStockCheckBillMain(HYear, HPeriod, HBillType, HInterID, HDate,
HBillNo, HBillStatus, HMaker, HMakeDate)
values({sMain["HYear"]}, {sMain["HPeriod"]}, '{sMain["HBillType"]}', {sMain["HInterID"]}, CONVERT(DATE, '{sMain["HDate"]}'),
'{sMain["HBillNo"]}', 1, '{sMain["HUser"]}', getdate());
");
foreach (var sSubKVP in sSub)
{
JObject sSubValue = sSubKVP.Value as JObject;
// 插入子表
oCN.RunProc($@"
insert into QC_InStockCheckBillSub(HInterID, HBillNo_bak, HEntryID, HInStockCheckEmp, HInStockCheckTime,
HMaterID, HRightQty, HBadQty)
values({sMain["HInterID"]}, '{sMain["HBillNo"]}', {i++}, '{sMain["HUser"]}', getdate(), {sSubValue["HMaterID"]}
, {sSubValue["HRightQty"]}, {sSubValue["HBadQty"]})
");
// 反写条码主档
JArray HBarCodeCache = sSubValue["HBarCodeCache"] as JArray;
foreach (var HBarCode in HBarCodeCache)
{
LogService.Write($@"update Gy_BarCodeBill set HBarCodeStatus='{HBarCode["HBarCodeState"]}' where HBarCode='{HBarCode["HBarCode"]}' ");
oCN.RunProc($@"update Gy_BarCodeBill set HBarCodeStatus='{HBarCode["HBarCodeState"]}' where HBarCode='{HBarCode["HBarCode"]}' ");
}
}
oCN.Commit();
objJsonResult.code = "1";
objJsonResult.count = 1;
objJsonResult.Message = "Success!";
objJsonResult.data = null;
return objJsonResult;
}
catch(Exception e)
{
oCN.RollBack();
objJsonResult.code = "0";
objJsonResult.count = 0;
objJsonResult.Message = e.ToString();
objJsonResult.data = null;
return objJsonResult;
}
}
#endregion
#region 在库检验单 扫物料码 带出物料/条码临时表信息
internal class ClsKF_PonderationBillMain_Temp_InStockCheckBill
{
public long HItemID;
public long HSourceEntryID;
public string HSourceBillNo;
public string HSourceBillType;
public long HRelationInterID;
public long HRelationEntryID;
public string HRelationBillNo;
public long HErpClsID;
public bool HRedBlueFlag;
public string HBatchNo;
public long HInstructID;
public string HInstructNo;
public long HSeOrderBillID;
public string HSeOrderBillNo;
public string HNordan;
public long HAuxPropID;
public string HMTONo;
public long HSTOCKORGID;
public long HOWNERID;
public string HCusBarCode;
public string HSubBillType;
public long HMulSourceBill;
public string HBeginDate;
public string HEndDate;
public long HSourceInterID;
public long HUnitID;
public DateTime HMakeDate;
public string HAddr;
public long HInterID;
public string HBillNo;
public string HBillType;
public long HMaterID;
public string HMaterNumber;
public string HMaterName;
public string HMaterModel;
public long HProcID;
public string HProcName;
public long HWhID;
public string HWhName;
public long HSCWHID;
public string HSCWHName;
public long HStockPlaceID;
public string HStockPlaceName;
public long HOutStockPlaceID;
public string HOutStockPlaceName;
public long HGroupID;
public string HGroupName;
public double HQtyMust;
public double HQty;
public double HPieceQty;
public string HBarCode;
public string HMaker;
public string HBarCodeStatus;
public double HBadQty;
public double HRightQty;
}
[Route("QC_InStockCheckBill/getBarCode")]
[HttpGet]
public Object getBarCode(string sBarCode, Int64 HInterID, string HBillType, string HBillNo, string HMaker, Int64 HWhID, Int64 HSPID, Double HQty, bool HRedBlueFlag, bool SourceFlag, string HSourceBillNo, string HSourceBillType, Int64 HStockOrgID, string HScanStyle, string HCustom1, string HCustom2, string sBarCodeStatus)
{
ClsKF_PonderationBillMain_Temp_InStockCheckBill oMain = new ClsKF_PonderationBillMain_Temp_InStockCheckBill();
string sErrMsg = "";
oCN.BeginTran();
try
{
if (oSystemParameter.ShowBillByOrgID(HStockOrgID, ref sErrMsg) == false)
{
sErrMsg = "获取系统参数失败! " + sErrMsg;
return null;
}
// 此控制器单据类型固定为 7526
var ds2 = oCN.RunProcReturn("select * from KF_PonderationBillMain_Temp_InStockCheckBill where HBarCode='" + sBarCode + "'and HInterID = " + HInterID, "KF_PonderationBillMain_Temp_InStockCheckBill");
ds = oCN.RunProcReturn("select * from h_v_IF_BarCodeBillList where 条码编号='" + sBarCode + "'", "h_v_IF_BarCodeBillList");
if (ds == null || ds.Tables[0].Rows.Count == 0)
{
objJsonResult.code = "0";
objJsonResult.count = 0;
objJsonResult.Message = "扫码失败! 该条码不在条码档案中...";
objJsonResult.data = null;
return objJsonResult;
}
if (ds2.Tables[0].Rows.Count > 0)
{
objJsonResult.code = "0";
objJsonResult.count = 0;
objJsonResult.Message = "扫码失败! 此单据已扫过该条码...";
objJsonResult.data = null;
return objJsonResult;
}
var rowOne = ds.Tables[0].Rows[0];
oMain.HBillNo = HBillNo;
oMain.HBillType = HBillType;
oMain.HMaker = HMaker;
oMain.HMaterID = long.Parse(rowOne["HMaterID"].ToString());
oMain.HQtyMust = 0;
oMain.HQty = double.Parse(rowOne["数量"].ToString());
oMain.HMTONo = rowOne["计划跟踪号"].ToString();
oMain.HWhID = HWhID;
oMain.HStockPlaceID = HSPID;
oMain.HSCWHID = 0;
oMain.HOutStockPlaceID = 0;
oMain.HPieceQty = 1;
oMain.HSourceInterID = DBUtility.ClsPub.isLong(ds.Tables[0].Rows[0]["HSourceInterID"]);
oMain.HSourceEntryID = DBUtility.ClsPub.isLong(ds.Tables[0].Rows[0]["HSourceEntryID"]);
oMain.HSourceBillNo = DBUtility.ClsPub.isStrNull(ds.Tables[0].Rows[0]["源单单号"]);
oMain.HSourceBillType = DBUtility.ClsPub.isStrNull(ds.Tables[0].Rows[0]["HSourceBillType"]);
oMain.HSTOCKORGID = HStockOrgID;
oMain.HOWNERID = HStockOrgID;
oMain.HCusBarCode = HCustom1;
oMain.HBarCode = sBarCode;
oMain.HBarCodeStatus = sBarCodeStatus;
oMain.HBadQty = 0;
oMain.HRightQty = 0;
if(sBarCodeStatus == "正常")
{
oMain.HRightQty = oMain.HQty;
}
else if (sBarCodeStatus == "异常")
{
oMain.HBadQty = oMain.HQty;
}
// 写入临时表
oCN.RunProc("Insert into KF_PonderationBillMain_Temp_InStockCheckBill " +
"(HInterID,HBillNo,HBillType,HMaterID,HUnitID,HAuxPropID,HProcID" +
",HWhID,HSCWHID,HStockPlaceID,HOutStockPlaceID,HGroupID,HAddr" +
",HQtyMust,HQty,HPieceQty,HBatchNo,HBarCode,HBarCode_Pack" +
",HMaker,HMakeDate,HSourceInterID,HSourceEntryID,HSourceBillType,HSourceBillNo" +
",HRelationInterID,HRelationEntryID,HRelationBillNo,HRedBlueFlag,HMTONo,HPlanMode" +
",HSTOCKORGID,HOtherOrgID,HOWNERID,HOWNERTYPEID,HExpressNumber,HSubBillType" +
",HCusID,HDeptID,HCusBarCode,HMulSourceBill,HBeginDate,HEndDate, HBadQty, HRightQty, HBarCodeStatus) " +
" Values(" + HInterID + ",'" + oMain.HBillNo + "','" + oMain.HBillType + "'," + oMain.HMaterID.ToString() + "," + oMain.HUnitID.ToString() + "," + oMain.HAuxPropID.ToString() + "," + oMain.HProcID.ToString() +
"," + oMain.HWhID.ToString() + "," + oMain.HSCWHID.ToString() + "," + oMain.HStockPlaceID.ToString() + "," + oMain.HOutStockPlaceID.ToString() + "," + oMain.HGroupID.ToString() + ",'" + oMain.HAddr + "'" +
"," + oMain.HQtyMust.ToString() + "," + oMain.HQty.ToString() + "," + oMain.HPieceQty.ToString() + ",'" + oMain.HBatchNo + "','" + oMain.HBarCode + "',''" +
",'" + oMain.HMaker + "',getdate()," + oMain.HSourceInterID.ToString() + "," + oMain.HSourceEntryID.ToString() + ",'" + oMain.HSourceBillType + "','" + oMain.HSourceBillNo + "'" +
"," + oMain.HRelationInterID.ToString() + "," + oMain.HRelationEntryID.ToString() + ",'" + oMain.HRelationBillNo + "'," + Convert.ToString(oMain.HRedBlueFlag ? 1 : 0) + ",'" + oMain.HMTONo + "',0" +
"," + oMain.HSTOCKORGID.ToString() + "," + oMain.HOWNERID.ToString() + "," + oMain.HSTOCKORGID.ToString() + ",'','','" + oMain.HSubBillType + "'" +
",0,0,'" + oMain.HCusBarCode + "'," + oMain.HMulSourceBill.ToString() + "," + (oMain.HBeginDate == "" ? "NULL" : "'" + oMain.HBeginDate + "'") +
"," + (oMain.HEndDate == "" ? "NULL" : "'" + oMain.HEndDate + "'") + $@",{oMain.HBadQty}, {oMain.HRightQty}, '{oMain.HBarCodeStatus}')");
oCN.Commit();
objJsonResult.code = "1";
objJsonResult.count = 1;
objJsonResult.Message = "";
objJsonResult.data = ds.Tables[0];
return objJsonResult;
}
catch(Exception e)
{
oCN.RollBack();
objJsonResult.code = "0";
objJsonResult.count = 0;
objJsonResult.Message = e.ToString();
objJsonResult.data = null;
return objJsonResult;
}
}
#endregion
#region 返回物料/条码明细
[Route("QC_InStockCheckBillController/GetBillEntryTmpList_Json")]
[HttpGet]
public object GetBillEntryTmpList_Json(long HInterID, string HBillNo, int HBillType, int HStockOrgID)
{
try
{
DataSet DS = oCN.RunProcReturn("exec h_p_WMS_BillEntryTmpList_InStockCheckBill " + HInterID.ToString() + ",'" + HBillType + "'", "h_p_WMS_BillEntryTmpList_InStockCheckBill");
objJsonResult.code = "1";
objJsonResult.count = 1;
objJsonResult.Message = "";
objJsonResult.data = DS.Tables;
return objJsonResult;
}
catch(Exception e)
{
objJsonResult.code = "0";
objJsonResult.count = 0;
objJsonResult.Message = e.ToString();
objJsonResult.data = null;
return objJsonResult;
}
}
#endregion
#region 在库检验单 删除条码缓存
[Route("QC_InStockCheckBillController/set_DelPonderationBillMain_Temp_InterIDAndSource_Json")]
[HttpGet]
public object set_DelPonderationBillMain_Temp_InterIDAndSource_Json(long HItemID, long HInterID, long HMaterID, long HAuxPropID, string HMTONo, long HSourceInterID, long HSourceEntryID, string HBillType)
{
try
{
oCN.RunProc("Delete from KF_PonderationBillMain_Temp_InStockCheckBill where HItemID="+HItemID+" and HInterID=" + HInterID.ToString() + " and HMaterID= " + HMaterID.ToString() + " and HAuxPropID= " + HAuxPropID.ToString() + " and HBillType='" + HBillType + "' and HSourceInterID=" + HSourceInterID.ToString() + " and HSourceEntryID=" + HSourceEntryID.ToString() + " and HQty<>0 ", ref DBUtility.ClsPub.sExeReturnInfo);
objJsonResult.code = "0";
objJsonResult.count = 1;
objJsonResult.Message = "删除成功";
objJsonResult.data = null;
return objJsonResult;
}
catch (Exception e)
{
objJsonResult.code = "0";
objJsonResult.count = 0;
objJsonResult.Message = "删除所选行条码出入库临时表记录失败!" + e.ToString();
objJsonResult.data = null;
return objJsonResult;
}
}
#endregion
#region 在库检验单 获取单据列表
[Route("QC_InStockCheckBillController/getList")]
[HttpGet]
public Object getList(string sWhere, string user)
{
try {
if (string.IsNullOrWhiteSpace(sWhere))
{
ds = oCN.RunProcReturn($@"select * from h_v_QC_InStockCheckBillList", "h_v_QC_InStockCheckBillList");
}else
{
ds = oCN.RunProcReturn($@"select * from h_v_QC_InStockCheckBillList where 1 = 1 and {sWhere}", "");
}
objJsonResult.code = "1";
objJsonResult.count = 1;
objJsonResult.Message = "获取单据列表成功!";
objJsonResult.data = ds.Tables[0];
return objJsonResult;
}
catch (Exception e)
{
objJsonResult.code = "0";
objJsonResult.count = 0;
objJsonResult.Message = "获取单据列表失败!" + e.ToString();
objJsonResult.data = null;
return objJsonResult;
}
}
#endregion
#region 在库检验单 获取单据列表分页
[Route("QC_InStockCheckBillController/getListPage")]
[HttpGet]
public object getListPage(string sWhere, string user, string page, string size)
{
try
{
List