You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
5929 lines
262 KiB
5929 lines
262 KiB
using Microsoft.AspNetCore.Mvc;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using AMESCoreStudio.WebApi.Models.AMES;
|
|
using AMESCoreStudio.WebApi.DTO.AMES;
|
|
using AMESCoreStudio.CommonTools.Result;
|
|
using System.Data.Common;
|
|
using System.Data;
|
|
using System.Dynamic;
|
|
using Microsoft.Extensions.Configuration;
|
|
using System.IO;
|
|
using System.Collections.ObjectModel;
|
|
using AMESCoreStudio.WebApi.Controllers.BLL;
|
|
using System.Text;
|
|
using System.Data.SqlClient;
|
|
using Dapper;
|
|
using OfficeOpenXml;
|
|
using OfficeOpenXml.Style;
|
|
|
|
namespace AMESCoreStudio.WebApi.Controllers.AMES
|
|
{
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
[Route("api/[controller]")]
|
|
[ApiController]
|
|
public class WipInfosController : Controller
|
|
{
|
|
private readonly AMESContext _context;
|
|
private readonly ESUNContext _esun_context;
|
|
private readonly IConfiguration _config;
|
|
private readonly string _PTDContext;
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="context"></param>
|
|
public WipInfosController(AMESContext context)
|
|
{
|
|
_config = new ConfigurationBuilder().SetBasePath(Environment.CurrentDirectory).AddJsonFile("appsettings.json").Build();
|
|
_context = context;
|
|
_PTDContext = _config.GetConnectionString("PTDContext");
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢工單資料Info
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
// GET: api/SystemInfoes
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<WipInfo>>> GetWipInfo()
|
|
{
|
|
IQueryable<WipInfo> q = _context.WipInfos;
|
|
|
|
q = q.OrderBy(p => p.WipNO);
|
|
|
|
//q = q.OrderByDescending(p => p.SystemID);
|
|
|
|
var WipInfo = await q.ToListAsync();
|
|
|
|
//return await _context.SystemInfoes.ToListAsync();
|
|
|
|
return WipInfo;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢工單資料QRS009
|
|
/// </summary>
|
|
/// <param name="unitNo"></param>
|
|
/// <param name="factoryNo"></param>
|
|
/// <returns></returns>
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetWipInfo4QRS009(string unitNo, string factoryNo)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
var q = from q1 in _context.WipInfos
|
|
join q2 in _context.WipAtts on q1.WipNO equals q2.WipNO
|
|
join q3 in _context.LineInfoes on q1.WipID equals q3.WipNo
|
|
join q4 in _context.FactoryUnits on q1.UnitNO equals q4.UnitNo
|
|
select new
|
|
{
|
|
q1.WipID,
|
|
q1.WipNO,
|
|
q1.PlanQTY,
|
|
q1.CompleteQTY,
|
|
q1.UnitNO,
|
|
q1.LineID,
|
|
q1.FlowRuleID,
|
|
q1.StatusNO,
|
|
q1.CreateDate,
|
|
q2.ItemNO,
|
|
q3.LineDesc,
|
|
q4.UnitName,
|
|
q1.WerksNO
|
|
};
|
|
|
|
q = q.Where(w => w.StatusNO == "A");
|
|
if (unitNo != null)
|
|
{
|
|
q = q.Where(w => w.UnitNO.Equals(unitNo));
|
|
}
|
|
if (factoryNo != null)
|
|
{
|
|
q = q.Where(w => w.WerksNO.Equals(factoryNo));
|
|
}
|
|
|
|
//紀錄筆數
|
|
result.DataTotal = q.Count();
|
|
|
|
result.Data = await q.ToListAsync();
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢工單資料PDS003
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetWipInfo4PDS003(string wipNO)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
var q = from q1 in _context.WipInfos
|
|
join q2 in _context.WipAtts on q1.WipNO equals q2.WipNO into wip_att
|
|
from x in wip_att.DefaultIfEmpty()
|
|
join q3 in _context.LineInfoes on q1.LineID equals q3.LineID
|
|
join q4 in _context.FactoryUnits on q1.UnitNO equals q4.UnitNo
|
|
select new
|
|
{
|
|
q1.WipID,
|
|
q1.WipNO,
|
|
q1.PlanQTY,
|
|
q1.CompleteQTY,
|
|
q1.UnitNO,
|
|
q1.LineID,
|
|
q1.FlowRuleID,
|
|
q1.StatusNO,
|
|
q1.CreateDate,
|
|
x.ItemNO,
|
|
q3.LineDesc,
|
|
q4.UnitName
|
|
};
|
|
|
|
if (wipNO != null && wipNO != "")
|
|
{
|
|
q = q.Where(w => w.WipNO == wipNO);
|
|
}
|
|
|
|
//紀錄筆數
|
|
result.DataTotal = q.Count();
|
|
|
|
result.Data = await q.ToListAsync();
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢工單資料QRS011
|
|
/// </summary>
|
|
/// <param name="unitNo">生產製程</param>
|
|
/// <param name="itemNO">料號</param>
|
|
/// <param name="wipNO">工單號碼</param>
|
|
/// <param name="wipStatus">工單狀態:N(未完工) E(已完工)</param>
|
|
/// <returns></returns>
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetWipInfo4QRS011(string unitNo, string itemNO, string wipNO, string wipStatus = "A")
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
var q = from q1 in _context.WipInfos
|
|
join q2 in _context.WipAtts on q1.WipNO equals q2.WipNO
|
|
join q3 in _context.LineInfoes on q1.LineID equals q3.LineID
|
|
join q4 in _context.FactoryUnits on q1.UnitNO equals q4.UnitNo
|
|
select new
|
|
{
|
|
q1.WipID,
|
|
q1.WipNO,
|
|
q1.PlanQTY,
|
|
q1.CompleteQTY,
|
|
q1.UnitNO,
|
|
q1.LineID,
|
|
q1.FlowRuleID,
|
|
q1.StatusNO,
|
|
q1.CreateDate,
|
|
q2.ItemNO,
|
|
q3.LineDesc,
|
|
q4.UnitName
|
|
};
|
|
|
|
|
|
q = q.Where(w => w.StatusNO == wipStatus);
|
|
if (unitNo != "*")
|
|
{
|
|
q = q.Where(w => w.UnitNO == unitNo);
|
|
}
|
|
if (itemNO != null && itemNO != "")
|
|
{
|
|
q = q.Where(w => w.ItemNO == itemNO);
|
|
}
|
|
if (wipNO != null && wipNO != "")
|
|
{
|
|
q = q.Where(w => w.WipNO == wipNO);
|
|
}
|
|
|
|
//紀錄筆數
|
|
result.DataTotal = q.Count();
|
|
|
|
result.Data = await q.ToListAsync();
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="reader"></param>
|
|
/// <returns></returns>
|
|
public static DataTable DataReaderToDataTable(DbDataReader reader)
|
|
{
|
|
try
|
|
{
|
|
DataTable dt = new DataTable();
|
|
int fieldCount = reader.FieldCount;
|
|
for (int fieldIndex = 0; fieldIndex < fieldCount; ++fieldIndex)
|
|
{
|
|
dt.Columns.Add(reader.GetName(fieldIndex), reader.GetFieldType(fieldIndex));
|
|
}
|
|
|
|
dt.BeginLoadData();
|
|
|
|
object[] rowValues = new object[fieldCount];
|
|
while (reader.Read())
|
|
{
|
|
reader.GetValues(rowValues);
|
|
dt.LoadDataRow(rowValues, true);
|
|
}
|
|
reader.Close();
|
|
dt.EndLoadData();
|
|
|
|
return dt;
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw new Exception("DataReader Convert DataTable Error!", ex);
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
///
|
|
/// </summary>
|
|
/// <param name="barcodeNo"></param>
|
|
/// <returns></returns>
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> ESUNConnectTest(string barcodeNo)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
|
|
result.Msg = "OK";
|
|
result.Success = true;
|
|
|
|
ESUNContext _esun_context = new ESUNContext();
|
|
|
|
try
|
|
{
|
|
//读取MAC
|
|
DbConnection esun_conn = _esun_context.Database.GetDbConnection();
|
|
if (esun_conn.State != System.Data.ConnectionState.Open)
|
|
{
|
|
await esun_conn.OpenAsync();
|
|
}
|
|
|
|
/*
|
|
string mac_sql = string.Format(@"select distinct a.工單編號 as mo_id,e.客戶工單編號 as customer_moid,a.工單序號 as product_sn,
|
|
case substring(f.avalue_kp_typeid, 1, 3) when 'MAC' then isnull(z.item_prefix,'')+b.SUB_ITEM_SN else b.SUB_ITEM_SN end as part_barcode,
|
|
f.avalue_kp_typeid as class,c.material_id,d.r_stn as routeid,'' as workerid,rtrim(g.sn_date) + ' ' + rtrim(g.sn_time) as create_date
|
|
from or_sn_story a
|
|
left join sub_item_db b on a.工單編號 = b.or_sn and a.工單序號 = b.or_sal
|
|
left join jh_sub_item c on a.工單編號 = c.mo_id and b.class = c.part_typeid
|
|
left join jh_sub_item_prefix z on c.mo_id = z.mo_id
|
|
left join or_sub_db d on a.工單編號 = d.or_sn and b.class = d.class
|
|
join or_list e on a.工單編號 = e.工單編號 and e.[客戶] = 'EV'
|
|
left join jh_sub_item_mapping f on b.class = f.eversun_kp_typeid
|
|
join jh_sn_list g on a.工單序號 = g.sn
|
|
where g.sn_result = 'OK'
|
|
AND a.工單序號 = '{0}'
|
|
AND c.material_id LIKE 'MAC%'", barcodeNo);
|
|
*/
|
|
|
|
string mac_sql = string.Format(@"SELECT B.[ProductSN], B.[PartBarcode], B.[MFID], B.[MOID], B.[PartTypeID], B.[MaterialID], B.[IsActive]
|
|
FROM [SFIS].[dbo].[ZPDKeyPart] B WHERE B.[IsActive] = 1 AND B.[ProductSN] = (SELECT A.[PartBarcode] FROM [SFIS].[dbo].[ZPDKeyPart] A WHERE A.[IsActive] = 1 AND A.[PartTypeID] = 'MAC' AND A.[PartBarcode] = '{0}')", barcodeNo);
|
|
|
|
try
|
|
{
|
|
using (var esun_cmd = esun_conn.CreateCommand())
|
|
{
|
|
esun_cmd.CommandText = mac_sql;
|
|
|
|
using (var esun_reader = await esun_cmd.ExecuteReaderAsync())
|
|
{
|
|
if (esun_reader.HasRows)
|
|
{
|
|
List<dynamic> esun_list = new List<dynamic>();
|
|
DataTable esun_table = new DataTable();
|
|
|
|
esun_table = DataReaderToDataTable(esun_reader);
|
|
|
|
if (esun_table.Rows.Count > 0)
|
|
{
|
|
string mac_list = "";
|
|
for (int k = 0; k < esun_table.Rows.Count; k++)
|
|
{
|
|
mac_list = mac_list + esun_table.Rows[k]["part_barcode"].ToString().Trim() + ",";
|
|
}
|
|
|
|
if (mac_list != "")
|
|
{
|
|
string mac = mac_list.Substring(0, mac_list.Length - 1);
|
|
result.Msg = "OK:" + mac;
|
|
result.Success = true;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e2)
|
|
{
|
|
result.Msg = "NG:" + e2.Message;
|
|
result.Success = false;
|
|
}
|
|
}
|
|
catch (Exception e1)
|
|
{
|
|
result.Msg = "NG:" + e1.Message;
|
|
result.Success = false;
|
|
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢工單KeyParts資料QRS013
|
|
/// </summary>
|
|
/// <param name="wipNO"></param>
|
|
/// <param name="factoryNo"></param>
|
|
/// <param name="page"></param>
|
|
/// <param name="limit"></param>
|
|
/// <returns></returns>
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetWipInfo4QRS013(string wipNO, string factoryNo, int page, int limit)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
Helper helper = new Helper(_context);
|
|
|
|
DbConnection conn = _context.Database.GetDbConnection();
|
|
if (conn.State != ConnectionState.Open)
|
|
{
|
|
await conn.OpenAsync();
|
|
}
|
|
|
|
string kp_sql = "select kp_no, count(kp_seq) as kp_qty from jhames.wip_kp where wip_no = '" + wipNO + "' group by kp_no";
|
|
|
|
DataTable dtKp = new DataTable();
|
|
|
|
using (var cmd = conn.CreateCommand())
|
|
{
|
|
cmd.CommandText = kp_sql;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
dtKp = DataReaderToDataTable(reader);
|
|
}
|
|
}
|
|
}
|
|
|
|
string sql = @" select c.wip_no as WipNo,b.barcode_no as BarcodeNo,b.extra_barcode_no as ExtraBarcodeNo,d.model_no as ModelNO,d.item_no as ItemNO";
|
|
sql = sql + " from jhames.barcode_info b,jhames.wip_info c,jhames.wip_att d";
|
|
sql = sql + " where b.wip_id = c.wip_id and c.wip_no = d.wip_no";
|
|
sql = sql + " and c.wip_no = '" + wipNO + "'";
|
|
|
|
using (var barcode_cmd = conn.CreateCommand())
|
|
{
|
|
barcode_cmd.CommandText = sql;
|
|
|
|
using (var barcode_reader = await barcode_cmd.ExecuteReaderAsync())
|
|
{
|
|
if (barcode_reader.HasRows)
|
|
{
|
|
List<dynamic> list = new List<dynamic>();
|
|
DataTable dtBarcode = new DataTable();
|
|
|
|
dtBarcode = DataReaderToDataTable(barcode_reader);
|
|
|
|
if (dtKp.Rows.Count > 0)
|
|
{
|
|
for (int i = 0; i < dtKp.Rows.Count; i++)
|
|
{
|
|
int kp_qty = int.Parse(dtKp.Rows[i]["KP_QTY"].ToString());
|
|
string kp_no = dtKp.Rows[i]["KP_NO"].ToString();
|
|
if (kp_qty > 1)
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Columns.Add("KP_" + kp_no);
|
|
}
|
|
catch { }
|
|
|
|
for (int j = 1; j < kp_qty; j++)
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Columns.Add("KP_" + kp_no + "_" + j.ToString());
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Columns.Add("KP_" + kp_no);
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
dtBarcode.AcceptChanges();
|
|
|
|
for (int i = 0; i < dtBarcode.Rows.Count; i++)
|
|
{
|
|
string barcode_no = dtBarcode.Rows[i]["BarcodeNo"].ToString();
|
|
|
|
//读取组件
|
|
for (int j = 0; j < dtKp.Rows.Count; j++)
|
|
{
|
|
string kp_no = dtKp.Rows[j]["KP_NO"].ToString();
|
|
|
|
string barcode_item_sql = string.Format(@"select part_no from jhames.barcode_info a,jhames.barcode_item b where a.barcode_id = b.barcode_id
|
|
and b.item_no = '{0}' and a.barcode_no = '{1}'", kp_no, barcode_no);
|
|
|
|
using (var item_cmd = conn.CreateCommand())
|
|
{
|
|
item_cmd.CommandText = barcode_item_sql;
|
|
|
|
using (var item_reader = await item_cmd.ExecuteReaderAsync())
|
|
{
|
|
if (item_reader.HasRows)
|
|
{
|
|
DataTable dtItem = new DataTable();
|
|
dtItem = DataReaderToDataTable(item_reader);
|
|
|
|
for (int k = 0; k < dtItem.Rows.Count; k++)
|
|
{
|
|
string part_no = dtItem.Rows[k]["PART_NO"].ToString();
|
|
try
|
|
{
|
|
dtBarcode.Rows[i]["KP_" + kp_no + "_" + k.ToString()] = part_no;
|
|
}
|
|
catch
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Rows[i]["KP_" + kp_no] = part_no;
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
dtBarcode.AcceptChanges();
|
|
|
|
try
|
|
{
|
|
if (kp_no.StartsWith("MB") && !kp_no.StartsWith("MB_MAC"))
|
|
{
|
|
ESUNContext _esun_context = new ESUNContext();
|
|
|
|
//读取MAC
|
|
DbConnection esun_conn = _esun_context.Database.GetDbConnection();
|
|
if (esun_conn.State != ConnectionState.Open)
|
|
{
|
|
await esun_conn.OpenAsync();
|
|
}
|
|
|
|
bool createMacCol = true;
|
|
|
|
|
|
//part_no = "91000237320038";
|
|
|
|
string mac_sql = string.Format(@"select distinct a.工單編號 as mo_id,e.客戶工單編號 as customer_moid,a.工單序號 as product_sn,
|
|
case substring(f.avalue_kp_typeid, 1, 3) when 'MAC' then isnull(z.item_prefix,'')+b.SUB_ITEM_SN else b.SUB_ITEM_SN end as part_barcode,
|
|
f.avalue_kp_typeid as class,c.material_id,d.r_stn as routeid,'' as workerid,rtrim(g.sn_date) + ' ' + rtrim(g.sn_time) as create_date
|
|
from or_sn_story a
|
|
left join sub_item_db b on a.工單編號 = b.or_sn and a.工單序號 = b.or_sal
|
|
left join jh_sub_item c on a.工單編號 = c.mo_id and b.class = c.part_typeid
|
|
left join jh_sub_item_prefix z on c.mo_id = z.mo_id
|
|
left join or_sub_db d on a.工單編號 = d.or_sn and b.class = d.class
|
|
join or_list e on a.工單編號 = e.工單編號 and e.[客戶] = 'EV'
|
|
left join jh_sub_item_mapping f on b.class = f.eversun_kp_typeid
|
|
join jh_sn_list g on a.工單序號 = g.sn
|
|
where g.sn_result = 'OK'
|
|
AND a.工單序號 = '{0}'
|
|
AND c.material_id LIKE 'MAC%'", part_no);
|
|
|
|
using (var esun_cmd = esun_conn.CreateCommand())
|
|
{
|
|
esun_cmd.CommandText = mac_sql;
|
|
esun_cmd.CommandTimeout = 0;
|
|
|
|
using (var esun_reader = await esun_cmd.ExecuteReaderAsync())
|
|
{
|
|
if (esun_reader.HasRows)
|
|
{
|
|
List<dynamic> esun_list = new List<dynamic>();
|
|
DataTable esun_table = new DataTable();
|
|
|
|
esun_table = DataReaderToDataTable(esun_reader);
|
|
|
|
if (esun_table.Rows.Count > 0)
|
|
{
|
|
if (createMacCol)
|
|
{
|
|
for (int m = 0; m < esun_table.Rows.Count; m++)
|
|
{
|
|
if (dtItem.Rows.Count == 1)
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Columns.Add("KP_" + kp_no + "_MAC" + (m + 1).ToString());
|
|
}
|
|
catch { }
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Columns.Add("KP_" + kp_no + "_" + (k + 1).ToString() + "_MAC" + (m + 1).ToString());
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
dtBarcode.AcceptChanges();
|
|
createMacCol = false;
|
|
}
|
|
|
|
string mac = "";
|
|
for (int m = 0; m < esun_table.Rows.Count; m++)
|
|
{
|
|
mac = esun_table.Rows[m]["part_barcode"].ToString().Trim();
|
|
if (dtItem.Rows.Count == 1)
|
|
{
|
|
dtBarcode.Rows[i]["KP_" + kp_no + "_MAC" + (m + 1).ToString()] = mac;
|
|
}
|
|
else
|
|
{
|
|
dtBarcode.Rows[i]["KP_" + kp_no + "_" + (k + 1).ToString() + "_MAC" + (m + 1).ToString()] = mac;
|
|
}
|
|
dtBarcode.AcceptChanges();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
catch (Exception e1)
|
|
{
|
|
string err = e1.Message;
|
|
}
|
|
}
|
|
dtBarcode.AcceptChanges();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (DataRow row in dtBarcode.Rows)
|
|
{
|
|
dynamic dyn = new ExpandoObject();
|
|
list.Add(dyn);
|
|
foreach (DataColumn column in dtBarcode.Columns)
|
|
{
|
|
var dic = (IDictionary<string, object>)dyn;
|
|
dic[column.ColumnName] = row[column];
|
|
}
|
|
}
|
|
|
|
result.DataTotal = list.Count();
|
|
result.Data = list;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
|
|
private void WriteTraceLog(string logDesc)
|
|
{
|
|
try
|
|
{
|
|
StreamWriter writer;
|
|
string path = AppDomain.CurrentDomain.BaseDirectory + "TraceLog";
|
|
if (!Directory.Exists(path))
|
|
{
|
|
Directory.CreateDirectory(path);
|
|
}
|
|
string logFileName = path + @"\" + DateTime.Now.ToString("yyyyMMdd") + ".txt";
|
|
if (!System.IO.File.Exists(logFileName))
|
|
{
|
|
writer = System.IO.File.CreateText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " - " + logFileName);
|
|
}
|
|
else
|
|
{
|
|
writer = System.IO.File.AppendText(DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss.fff") + " - " + logFileName);
|
|
}
|
|
writer.WriteLine(logDesc);
|
|
writer.Close();
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢工單KeyParts資料QRS017
|
|
/// </summary>
|
|
/// <param name="wipNO"></param>
|
|
/// <returns></returns>
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetWipInfo4QRS017(string wipNO)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
Helper helper = new Helper(_context);
|
|
|
|
DbConnection conn = _context.Database.GetDbConnection();
|
|
if (conn.State != ConnectionState.Open)
|
|
{
|
|
await conn.OpenAsync();
|
|
}
|
|
|
|
string wip_sql = "select b.barcode_id,a.wip_type from jhames.wip_info a,jhames.barcode_info b where a.wip_id = b.wip_id and a.wip_no = '" + wipNO + "'";
|
|
DataTable dtWip = new DataTable();
|
|
using (var cmd = conn.CreateCommand())
|
|
{
|
|
cmd.CommandText = wip_sql;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
dtWip = DataReaderToDataTable(reader);
|
|
}
|
|
}
|
|
}
|
|
|
|
string wipType = "";
|
|
int barcode_id = -1;
|
|
//if (dtWip.Rows.Count > 0)
|
|
if (false)
|
|
{
|
|
wipType = dtWip.Rows[0]["WIP_TYPE"].ToString();
|
|
barcode_id = int.Parse(dtWip.Rows[0]["BARCODE_ID"].ToString());
|
|
}
|
|
else
|
|
{
|
|
|
|
wip_sql = "select b.barcode_id,a.wip_type from jhames.wip_info a,jhames.barcode_item b where a.wip_id = b.wip_id and a.wip_no = '" + wipNO + "'";
|
|
|
|
dtWip = new DataTable();
|
|
using (var cmd = conn.CreateCommand())
|
|
{
|
|
cmd.CommandText = wip_sql;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
dtWip = DataReaderToDataTable(reader);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (dtWip.Rows.Count > 0)
|
|
{
|
|
wipType = dtWip.Rows[0]["WIP_TYPE"].ToString();
|
|
barcode_id = int.Parse(dtWip.Rows[0]["BARCODE_ID"].ToString());
|
|
}
|
|
|
|
}
|
|
|
|
string kp_sql = "select kp_no, count(kp_seq) as kp_qty, min(kp_seq) kp_seq from jhames.wip_kp where wip_no = '" + wipNO + "' group by kp_no order by kp_seq ";
|
|
|
|
DataTable dtKp = new DataTable();
|
|
|
|
if (wipType == "R")
|
|
{
|
|
kp_sql = "select item_no as kp_no,count(item_no) kp_qty from jhames.barcode_item where barcode_id = " + barcode_id + " group by item_no";
|
|
}
|
|
else
|
|
{
|
|
kp_sql = "select item_no as kp_no,count(item_no) kp_qty from jhames.barcode_item where barcode_id = " + barcode_id + " group by item_no";
|
|
}
|
|
|
|
using (var cmd = conn.CreateCommand())
|
|
{
|
|
cmd.CommandText = kp_sql;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
dtKp = DataReaderToDataTable(reader);
|
|
}
|
|
}
|
|
}
|
|
|
|
WriteTraceLog("query wip_kp sql:" + kp_sql);
|
|
|
|
if (dtKp != null)
|
|
{
|
|
WriteTraceLog("query wip_kp rows qty:" + dtKp.Rows.Count);
|
|
}
|
|
|
|
/*
|
|
string sql = @" select c.wip_no as WipNo,b.barcode_no as BarcodeNo,b.extra_barcode_no as ExtraBarcodeNo,d.model_no as ModelNO,d.item_no as ItemNO";
|
|
sql = sql + " from jhames.barcode_info b,jhames.wip_info c,jhames.wip_att d";
|
|
sql = sql + " where b.wip_id = c.wip_id and c.wip_no = d.wip_no";
|
|
sql = sql + " and c.wip_no = '" + wipNO + "'";
|
|
*/
|
|
|
|
string sql = @" select c.wip_no as WipNo,b.barcode_no as BarcodeNo,b.extra_barcode_no as ExtraBarcodeNo,e.station_name as StationName,b.box_no as BoxNo,d.model_no as ModelNO,d.item_no as ItemNO";
|
|
sql = sql + " from jhames.barcode_info b,jhames.wip_barcode c,jhames.wip_att d,jhames.stations e";
|
|
sql = sql + " where b.barcode_no between c.start_no and c.end_no and c.wip_no = d.wip_no and b.station_id = e.station_id";
|
|
sql = sql + " and c.wip_no = '" + wipNO + "'";
|
|
sql = sql + " order by b.barcode_no ";
|
|
|
|
using (var barcode_cmd = conn.CreateCommand())
|
|
{
|
|
barcode_cmd.CommandText = sql;
|
|
|
|
using (var barcode_reader = await barcode_cmd.ExecuteReaderAsync())
|
|
{
|
|
if (barcode_reader.HasRows)
|
|
{
|
|
List<dynamic> list = new List<dynamic>();
|
|
DataTable dtBarcode = new DataTable();
|
|
|
|
WriteTraceLog("query barcode sql:" + sql);
|
|
|
|
dtBarcode = DataReaderToDataTable(barcode_reader);
|
|
|
|
if (dtBarcode != null)
|
|
{
|
|
WriteTraceLog("query barcode rows qty:" + dtBarcode.Rows.Count);
|
|
}
|
|
|
|
if (dtKp.Rows.Count > 0)
|
|
{
|
|
for (int i = 0; i < dtKp.Rows.Count; i++)
|
|
{
|
|
int kp_qty = int.Parse(dtKp.Rows[i]["KP_QTY"].ToString());
|
|
string kp_no = dtKp.Rows[i]["KP_NO"].ToString();
|
|
if (kp_qty > 1)
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Columns.Add("KP_" + kp_no);
|
|
}
|
|
catch { }
|
|
|
|
for (int j = 1; j < kp_qty; j++)
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Columns.Add("KP_" + kp_no + "#" + j.ToString());
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Columns.Add("KP_" + kp_no);
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
dtBarcode.AcceptChanges();
|
|
|
|
for (int i = 0; i < dtBarcode.Rows.Count; i++)
|
|
{
|
|
string barcode_no = dtBarcode.Rows[i]["BarcodeNo"].ToString();
|
|
|
|
//读取组件
|
|
for (int j = 0; j < dtKp.Rows.Count; j++)
|
|
{
|
|
string kp_no = dtKp.Rows[j]["KP_NO"].ToString();
|
|
|
|
string barcode_item_sql = string.Format(@"select part_no from jhames.barcode_info a,jhames.barcode_item b where a.barcode_id = b.barcode_id
|
|
and b.item_no = '{0}' and a.barcode_no = '{1}'", kp_no, barcode_no);
|
|
|
|
using (var item_cmd = conn.CreateCommand())
|
|
{
|
|
item_cmd.CommandText = barcode_item_sql;
|
|
|
|
using (var item_reader = await item_cmd.ExecuteReaderAsync())
|
|
{
|
|
if (item_reader.HasRows)
|
|
{
|
|
DataTable dtItem = new DataTable();
|
|
dtItem = DataReaderToDataTable(item_reader);
|
|
|
|
for (int k = 0; k < dtItem.Rows.Count; k++)
|
|
{
|
|
string part_no = dtItem.Rows[k]["PART_NO"].ToString();
|
|
try
|
|
{
|
|
dtBarcode.Rows[i]["KP_" + kp_no + "#" + k.ToString()] = part_no;
|
|
}
|
|
catch
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Rows[i]["KP_" + kp_no] = part_no;
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
dtBarcode.AcceptChanges();
|
|
|
|
try
|
|
{
|
|
if (kp_no.StartsWith("MB") && !kp_no.StartsWith("MB_MAC"))
|
|
{
|
|
/*
|
|
WriteTraceLog("query mac by mb trace 001");
|
|
|
|
ESUNContext _esun_context = new ESUNContext();
|
|
|
|
WriteTraceLog("query mac by mb trace 002");
|
|
|
|
//读取MAC
|
|
DbConnection esun_conn = _esun_context.Database.GetDbConnection();
|
|
|
|
WriteTraceLog("query mac by mb trace 003");
|
|
|
|
if (esun_conn.State != ConnectionState.Open)
|
|
{
|
|
await esun_conn.OpenAsync();
|
|
}
|
|
|
|
WriteTraceLog("query mac by mb trace 004");
|
|
*/
|
|
|
|
bool createMacCol = true;
|
|
|
|
|
|
//part_no = "91000237320038";
|
|
|
|
/*
|
|
string mac_sql = string.Format(@"select distinct a.工單編號 as mo_id,e.客戶工單編號 as customer_moid,a.工單序號 as product_sn,
|
|
case substring(f.avalue_kp_typeid, 1, 3) when 'MAC' then isnull(z.item_prefix,'')+b.SUB_ITEM_SN else b.SUB_ITEM_SN end as part_barcode,
|
|
f.avalue_kp_typeid as class,c.material_id,d.r_stn as routeid,'' as workerid,rtrim(g.sn_date) + ' ' + rtrim(g.sn_time) as create_date
|
|
from or_sn_story a
|
|
left join sub_item_db b on a.工單編號 = b.or_sn and a.工單序號 = b.or_sal
|
|
left join jh_sub_item c on a.工單編號 = c.mo_id and b.class = c.part_typeid
|
|
left join jh_sub_item_prefix z on c.mo_id = z.mo_id
|
|
left join or_sub_db d on a.工單編號 = d.or_sn and b.class = d.class
|
|
join or_list e on a.工單編號 = e.工單編號 and e.[客戶] = 'EV'
|
|
left join jh_sub_item_mapping f on b.class = f.eversun_kp_typeid
|
|
join jh_sn_list g on a.工單序號 = g.sn
|
|
where g.sn_result = 'OK'
|
|
AND a.工單序號 = '{0}'
|
|
AND c.material_id LIKE 'MAC%'", part_no);
|
|
*/
|
|
|
|
/*
|
|
string mac_sql = string.Format(@"SELECT B.[ProductSN], B.[PartBarcode], B.[MFID], B.[MOID], B.[PartTypeID], B.[MaterialID], B.[IsActive]
|
|
FROM [SFIS].[dbo].[ZPDKeyPart] B WHERE B.[IsActive] = 1 AND B.[ProductSN] = (SELECT A.[PartBarcode] FROM [SFIS].[dbo].[ZPDKeyPart] A WHERE A.[IsActive] = 1 AND A.[PartTypeID] = 'MAC' AND A.[PartBarcode] = '{0}')", part_no);
|
|
|
|
WriteTraceLog("query mac by mb sql:" + mac_sql);
|
|
*/
|
|
|
|
bool findFlag = false;
|
|
|
|
string mac_sql = string.Format("select partbarcode from jhames.c_sfis_keyparts where productsn = '{0}' and parttypeid='MAC'", part_no);
|
|
|
|
using (var esun_cmd = conn.CreateCommand())
|
|
{
|
|
esun_cmd.CommandText = mac_sql;
|
|
esun_cmd.CommandTimeout = 0;
|
|
|
|
using (var esun_reader = await esun_cmd.ExecuteReaderAsync())
|
|
{
|
|
if (esun_reader.HasRows)
|
|
{
|
|
findFlag = true;
|
|
|
|
List<dynamic> esun_list = new List<dynamic>();
|
|
DataTable esun_table = new DataTable();
|
|
|
|
esun_table = DataReaderToDataTable(esun_reader);
|
|
|
|
if (esun_table != null)
|
|
{
|
|
WriteTraceLog("query mac by mb trace 005:esun_talbe rows qty:" + esun_table.Rows.Count);
|
|
}
|
|
|
|
if (esun_table.Rows.Count > 0)
|
|
{
|
|
if (createMacCol)
|
|
{
|
|
for (int m = 0; m < esun_table.Rows.Count; m++)
|
|
{
|
|
if (dtItem.Rows.Count == 1)
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Columns.Add("KP_" + kp_no + "#MAC" + (m + 1).ToString());
|
|
}
|
|
catch { }
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Columns.Add("KP_" + kp_no + "#" + (k + 1).ToString() + "_MAC" + (m + 1).ToString());
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
dtBarcode.AcceptChanges();
|
|
createMacCol = false;
|
|
}
|
|
|
|
WriteTraceLog("query mac by mb trace 006");
|
|
|
|
string mac = "";
|
|
for (int m = 0; m < esun_table.Rows.Count; m++)
|
|
{
|
|
//mac = esun_table.Rows[m]["part_barcode"].ToString().Trim();
|
|
mac = esun_table.Rows[m]["PARTBARCODE"].ToString().Trim();
|
|
if (dtItem.Rows.Count == 1)
|
|
{
|
|
dtBarcode.Rows[i]["KP_" + kp_no + "#MAC" + (m + 1).ToString()] = mac;
|
|
}
|
|
else
|
|
{
|
|
dtBarcode.Rows[i]["KP_" + kp_no + "#" + (k + 1).ToString() + "_MAC" + (m + 1).ToString()] = mac;
|
|
}
|
|
dtBarcode.AcceptChanges();
|
|
}
|
|
|
|
WriteTraceLog("query mac by mb trace 007");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//从barcode_item读取结转数据
|
|
if (!findFlag)
|
|
{
|
|
string mac_sql2 = string.Format("select a.part_no from jhames.barcode_item a,jhames.barcode_info b where a.barcode_id = b.barcode_id and b.barcode_no = '{0}' and item_no like 'MAC%'", part_no);
|
|
|
|
using (var esun_cmd = conn.CreateCommand())
|
|
{
|
|
esun_cmd.CommandText = mac_sql2;
|
|
esun_cmd.CommandTimeout = 0;
|
|
|
|
using (var esun_reader = await esun_cmd.ExecuteReaderAsync())
|
|
{
|
|
if (esun_reader.HasRows)
|
|
{
|
|
findFlag = true;
|
|
|
|
List<dynamic> esun_list = new List<dynamic>();
|
|
DataTable esun_table = new DataTable();
|
|
|
|
esun_table = DataReaderToDataTable(esun_reader);
|
|
|
|
if (esun_table != null)
|
|
{
|
|
WriteTraceLog("query mac by mb trace 005:esun_talbe rows qty:" + esun_table.Rows.Count);
|
|
}
|
|
|
|
if (esun_table.Rows.Count > 0)
|
|
{
|
|
if (createMacCol)
|
|
{
|
|
for (int m = 0; m < esun_table.Rows.Count; m++)
|
|
{
|
|
if (dtItem.Rows.Count == 1)
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Columns.Add("KP_" + kp_no + "#MAC" + (m + 1).ToString());
|
|
}
|
|
catch { }
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Columns.Add("KP_" + kp_no + "#" + (k + 1).ToString() + "_MAC" + (m + 1).ToString());
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
dtBarcode.AcceptChanges();
|
|
createMacCol = false;
|
|
}
|
|
|
|
string mac = "";
|
|
for (int m = 0; m < esun_table.Rows.Count; m++)
|
|
{
|
|
//mac = esun_table.Rows[m]["part_barcode"].ToString().Trim();
|
|
mac = esun_table.Rows[m]["PART_NO"].ToString().Trim();
|
|
if (dtItem.Rows.Count == 1)
|
|
{
|
|
dtBarcode.Rows[i]["KP_" + kp_no + "#MAC" + (m + 1).ToString()] = mac;
|
|
}
|
|
else
|
|
{
|
|
dtBarcode.Rows[i]["KP_" + kp_no + "#" + (k + 1).ToString() + "_MAC" + (m + 1).ToString()] = mac;
|
|
}
|
|
dtBarcode.AcceptChanges();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e1)
|
|
{
|
|
string err = e1.Message;
|
|
WriteTraceLog("query mac by mb trace 999:" + e1.Message);
|
|
}
|
|
}
|
|
|
|
WriteTraceLog("query mac by mb trace 008");
|
|
dtBarcode.AcceptChanges();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
foreach (DataRow row in dtBarcode.Rows)
|
|
{
|
|
dynamic dyn = new ExpandoObject();
|
|
list.Add(dyn);
|
|
foreach (DataColumn column in dtBarcode.Columns)
|
|
{
|
|
var dic = (IDictionary<string, object>)dyn;
|
|
dic[column.ColumnName] = row[column];
|
|
}
|
|
}
|
|
|
|
WriteTraceLog("query mac by mb trace 009");
|
|
|
|
result.DataTotal = list.Count();
|
|
result.Data = list;
|
|
|
|
WriteTraceLog("query mac by mb trace 010");
|
|
}
|
|
}
|
|
}
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢工單KeyParts資料QRS013
|
|
/// </summary>
|
|
/// <param name="wipNO"></param>
|
|
/// <param name="factoryNo"></param>
|
|
/// <param name="page"></param>
|
|
/// <param name="limit"></param>
|
|
/// <returns></returns>
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetWipInfo4QRS013V4(string wipNO, string factoryNo, int page, int limit)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
Helper helper = new Helper(_context);
|
|
|
|
var wip_kp = await _context.WipKps.FromSqlRaw("SELECT * FROM JHAMES.WIP_KP WHERE WIP_NO = '" + wipNO + "' ORDER BY KP_NO").ToListAsync();
|
|
|
|
string sql = @" select c.wip_no as WipNo,b.barcode_no as BarcodeNo,b.extra_barcode_no as ExtraBarcodeNo,d.model_no as ModelNO,d.item_no as ItemNO";
|
|
string sql1 = "", sql2 = "", sql3 = "";
|
|
if (wip_kp.Count > 0)
|
|
{
|
|
for (int i = 0; i < wip_kp.Count; i++)
|
|
{
|
|
sql1 = sql1 + ",k" + (i + 1).ToString() + ".part_no as KP_" + wip_kp[i].KpNo;
|
|
sql2 = sql2 + ",(select barcode_id,part_no from jhames.barcode_item where item_no = '" + wip_kp[i].KpNo + "') k" + (i + 1).ToString();
|
|
sql3 = sql3 + " and b.barcode_id = k" + (i + 1).ToString() + ".barcode_id(+)";
|
|
}
|
|
}
|
|
|
|
sql = sql + sql1;
|
|
sql = sql + " from jhames.barcode_info b,jhames.wip_info c,jhames.wip_att d";
|
|
sql = sql + sql2;
|
|
|
|
sql = sql + " where b.wip_id = c.wip_id and c.wip_no = d.wip_no";
|
|
|
|
sql = sql + sql3;
|
|
|
|
sql = sql + " and c.wip_no = '" + wipNO + "'";
|
|
|
|
DbConnection conn = _context.Database.GetDbConnection();
|
|
if (conn.State != System.Data.ConnectionState.Open)
|
|
{
|
|
await conn.OpenAsync();
|
|
}
|
|
|
|
using (var cmd = conn.CreateCommand())
|
|
{
|
|
cmd.CommandText = sql;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
List<dynamic> list = new List<dynamic>();
|
|
DataTable table = new DataTable();
|
|
|
|
table = DataReaderToDataTable(reader);
|
|
|
|
//判断MB组件增加查询MAC
|
|
try
|
|
{
|
|
if (wip_kp.Count > 0)
|
|
{
|
|
bool mbFlag = false;
|
|
for (int i = 0; i < wip_kp.Count; i++)
|
|
{
|
|
if (wip_kp[i].KpNo.StartsWith("MB"))
|
|
{
|
|
mbFlag = true;
|
|
break;
|
|
}
|
|
}
|
|
|
|
if (mbFlag)
|
|
{
|
|
ESUNContext _esun_context = new ESUNContext();
|
|
|
|
//读取MAC
|
|
DbConnection esun_conn = _esun_context.Database.GetDbConnection();
|
|
if (esun_conn.State != System.Data.ConnectionState.Open)
|
|
{
|
|
await esun_conn.OpenAsync();
|
|
}
|
|
|
|
bool createMacCol = true;
|
|
|
|
for (int j = 0; j < table.Rows.Count; j++)
|
|
{
|
|
string barcode_no = "";
|
|
|
|
try
|
|
{
|
|
barcode_no = table.Rows[j]["KP_MB"].ToString();
|
|
}
|
|
catch { }
|
|
|
|
string mac_sql = string.Format(@"select distinct a.工單編號 as mo_id,e.客戶工單編號 as customer_moid,a.工單序號 as product_sn,
|
|
case substring(f.avalue_kp_typeid, 1, 3) when 'MAC' then isnull(z.item_prefix,'')+b.SUB_ITEM_SN else b.SUB_ITEM_SN end as part_barcode,
|
|
f.avalue_kp_typeid as class,c.material_id,d.r_stn as routeid,'' as workerid,rtrim(g.sn_date) + ' ' + rtrim(g.sn_time) as create_date
|
|
from or_sn_story a
|
|
left join sub_item_db b on a.工單編號 = b.or_sn and a.工單序號 = b.or_sal
|
|
left join jh_sub_item c on a.工單編號 = c.mo_id and b.class = c.part_typeid
|
|
left join jh_sub_item_prefix z on c.mo_id = z.mo_id
|
|
left join or_sub_db d on a.工單編號 = d.or_sn and b.class = d.class
|
|
join or_list e on a.工單編號 = e.工單編號 and e.[客戶] = 'EV'
|
|
left join jh_sub_item_mapping f on b.class = f.eversun_kp_typeid
|
|
join jh_sn_list g on a.工單序號 = g.sn
|
|
where g.sn_result = 'OK'
|
|
AND a.工單序號 = '{0}'
|
|
AND c.material_id LIKE 'MAC%'", barcode_no);
|
|
|
|
using (var esun_cmd = esun_conn.CreateCommand())
|
|
{
|
|
esun_cmd.CommandText = mac_sql;
|
|
|
|
using (var esun_reader = await esun_cmd.ExecuteReaderAsync())
|
|
{
|
|
if (esun_reader.HasRows)
|
|
{
|
|
List<dynamic> esun_list = new List<dynamic>();
|
|
DataTable esun_table = new DataTable();
|
|
|
|
esun_table = DataReaderToDataTable(esun_reader);
|
|
|
|
if (esun_table.Rows.Count > 0)
|
|
{
|
|
if (createMacCol)
|
|
{
|
|
for (int k = 0; k < esun_table.Rows.Count; k++)
|
|
{
|
|
table.Columns.Add("KP_MAC" + (k + 1).ToString());
|
|
}
|
|
table.AcceptChanges();
|
|
createMacCol = false;
|
|
}
|
|
|
|
string mac = "";
|
|
for (int k = 0; k < esun_table.Rows.Count; k++)
|
|
{
|
|
mac = esun_table.Rows[k]["part_barcode"].ToString().Trim();
|
|
table.Rows[j]["KP_MAC" + (k + 1).ToString()] = mac;
|
|
table.AcceptChanges();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch { }
|
|
|
|
|
|
foreach (DataRow row in table.Rows)
|
|
{
|
|
dynamic dyn = new ExpandoObject();
|
|
list.Add(dyn);
|
|
foreach (DataColumn column in table.Columns)
|
|
{
|
|
var dic = (IDictionary<string, object>)dyn;
|
|
dic[column.ColumnName] = row[column];
|
|
}
|
|
}
|
|
|
|
result.DataTotal = list.Count();
|
|
result.Data = list;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢工單KeyParts資料QRS013
|
|
/// </summary>
|
|
/// <param name="wipNO"></param>
|
|
/// <param name="factoryNo"></param>
|
|
/// <param name="page"></param>
|
|
/// <param name="limit"></param>
|
|
/// <returns></returns>
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetWipInfo4QRS013V3(string wipNO, string factoryNo, int page, int limit)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
Helper helper = new Helper(_context);
|
|
|
|
var wip_kp = await _context.WipKps.FromSqlRaw("SELECT * FROM JHAMES.WIP_KP WHERE WIP_NO = '" + wipNO + "' ORDER BY KP_NO").ToListAsync();
|
|
|
|
string sql = @" select c.wip_no as WipNo,b.barcode_no as BarcodeNo,b.extra_barcode_no as ExtraBarcodeNo,d.model_no as ModelNO,d.item_no as ItemNO";
|
|
string sql1 = "", sql2 = "", sql3 = "";
|
|
if (wip_kp.Count > 0)
|
|
{
|
|
for (int i = 0; i < wip_kp.Count; i++)
|
|
{
|
|
sql1 = sql1 + ",k" + (i + 1).ToString() + ".part_no as KP_" + wip_kp[i].KpNo;
|
|
sql2 = sql2 + ",(select barcode_id,part_no from jhames.barcode_item where item_no = '" + wip_kp[i].KpNo + "') k" + (i + 1).ToString();
|
|
sql3 = sql3 + " and b.barcode_id = k" + (i + 1).ToString() + ".barcode_id(+)";
|
|
}
|
|
}
|
|
|
|
sql = sql + sql1;
|
|
sql = sql + " from jhames.barcode_info b,jhames.wip_info c,jhames.wip_att d";
|
|
sql = sql + sql2;
|
|
|
|
sql = sql + " where b.wip_id = c.wip_id and c.wip_no = d.wip_no";
|
|
|
|
sql = sql + sql3;
|
|
|
|
sql = sql + " and c.wip_no = '" + wipNO + "'";
|
|
|
|
DbConnection conn = _context.Database.GetDbConnection();
|
|
if (conn.State != System.Data.ConnectionState.Open)
|
|
{
|
|
await conn.OpenAsync();
|
|
}
|
|
|
|
using (var cmd = conn.CreateCommand())
|
|
{
|
|
cmd.CommandText = sql;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
List<dynamic> list = new List<dynamic>();
|
|
DataTable table = new DataTable();
|
|
|
|
table = DataReaderToDataTable(reader);
|
|
|
|
//判断MB组件增加查询MAC
|
|
try
|
|
{
|
|
if (wip_kp.Count > 0)
|
|
{
|
|
/*
|
|
for (int i = 0; i < wip_kp.Count; i++)
|
|
{
|
|
if (wip_kp[i].KpNo.StartsWith("MB"))
|
|
{
|
|
table.Columns.Add("MAC");
|
|
table.AcceptChanges();
|
|
break;
|
|
}
|
|
}
|
|
*/
|
|
|
|
ESUNContext _esun_context = new ESUNContext();
|
|
|
|
//读取MAC
|
|
DbConnection esun_conn = _esun_context.Database.GetDbConnection();
|
|
if (esun_conn.State != System.Data.ConnectionState.Open)
|
|
{
|
|
await esun_conn.OpenAsync();
|
|
}
|
|
|
|
bool createMacCol = true;
|
|
|
|
for (int j = 0; j < table.Rows.Count; j++)
|
|
{
|
|
string barcode_no = table.Rows[j]["BarcodeNo"].ToString();
|
|
|
|
string mac_sql = string.Format(@"select distinct a.工單編號 as mo_id,e.客戶工單編號 as customer_moid,a.工單序號 as product_sn,
|
|
case substring(f.avalue_kp_typeid, 1, 3) when 'MAC' then isnull(z.item_prefix,'')+b.SUB_ITEM_SN else b.SUB_ITEM_SN end as part_barcode,
|
|
f.avalue_kp_typeid as class,c.material_id,d.r_stn as routeid,'' as workerid,rtrim(g.sn_date) + ' ' + rtrim(g.sn_time) as create_date
|
|
from or_sn_story a
|
|
left join sub_item_db b on a.工單編號 = b.or_sn and a.工單序號 = b.or_sal
|
|
left join jh_sub_item c on a.工單編號 = c.mo_id and b.class = c.part_typeid
|
|
left join jh_sub_item_prefix z on c.mo_id = z.mo_id
|
|
left join or_sub_db d on a.工單編號 = d.or_sn and b.class = d.class
|
|
join or_list e on a.工單編號 = e.工單編號 and e.[客戶] = 'EV'
|
|
left join jh_sub_item_mapping f on b.class = f.eversun_kp_typeid
|
|
join jh_sn_list g on a.工單序號 = g.sn
|
|
where g.sn_result = 'OK'
|
|
AND a.工單序號 = '{0}'
|
|
AND c.material_id LIKE 'MAC%'", barcode_no);
|
|
|
|
using (var esun_cmd = esun_conn.CreateCommand())
|
|
{
|
|
esun_cmd.CommandText = mac_sql;
|
|
|
|
using (var esun_reader = await esun_cmd.ExecuteReaderAsync())
|
|
{
|
|
if (esun_reader.HasRows)
|
|
{
|
|
List<dynamic> esun_list = new List<dynamic>();
|
|
DataTable esun_table = new DataTable();
|
|
|
|
esun_table = DataReaderToDataTable(esun_reader);
|
|
|
|
if (esun_table.Rows.Count > 0)
|
|
{
|
|
if (createMacCol)
|
|
{
|
|
for (int k = 0; k < esun_table.Rows.Count; k++)
|
|
{
|
|
table.Columns.Add("KP_MAC" + (k + 1).ToString());
|
|
}
|
|
table.AcceptChanges();
|
|
createMacCol = false;
|
|
}
|
|
|
|
string mac = "";
|
|
for (int k = 0; k < esun_table.Rows.Count; k++)
|
|
{
|
|
mac = esun_table.Rows[k]["part_barcode"].ToString().Trim();
|
|
table.Rows[j]["KP_MAC" + (k + 1).ToString()] = mac;
|
|
table.AcceptChanges();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
catch { }
|
|
|
|
|
|
foreach (DataRow row in table.Rows)
|
|
{
|
|
dynamic dyn = new ExpandoObject();
|
|
list.Add(dyn);
|
|
foreach (DataColumn column in table.Columns)
|
|
{
|
|
var dic = (IDictionary<string, object>)dyn;
|
|
dic[column.ColumnName] = row[column];
|
|
}
|
|
}
|
|
|
|
result.DataTotal = list.Count();
|
|
result.Data = list;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢工單KeyParts資料QRS013
|
|
/// </summary>
|
|
/// <param name="wipNO"></param>
|
|
/// <param name="factoryNo"></param>
|
|
/// <param name="page"></param>
|
|
/// <param name="limit"></param>
|
|
/// <returns></returns>
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetWipInfo4QRS013V2(string wipNO, string factoryNo, int page, int limit)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
Helper helper = new Helper(_context);
|
|
|
|
var wip_kp = await _context.WipKps.FromSqlRaw("SELECT * FROM JHAMES.WIP_KP WHERE WIP_NO = '" + wipNO + "' ORDER BY KP_NO").ToListAsync();
|
|
|
|
string sql = @" select c.wip_no as WipNo,b.barcode_no as BarcodeNo,b.extra_barcode_no as ExtraBarcodeNo,d.model_no as ModelNO,d.item_no as ItemNO";
|
|
string sql1 = "", sql2 = "", sql3 = "";
|
|
if (wip_kp.Count > 0)
|
|
{
|
|
for (int i = 0; i < wip_kp.Count; i++)
|
|
{
|
|
sql1 = sql1 + ",k" + (i + 1).ToString() + ".part_no as KP_" + wip_kp[i].KpNo;
|
|
sql2 = sql2 + ",(select barcode_id,part_no from jhames.barcode_item where item_no = '" + wip_kp[i].KpNo + "') k" + (i + 1).ToString();
|
|
sql3 = sql3 + " and b.barcode_id = k" + (i + 1).ToString() + ".barcode_id(+)";
|
|
}
|
|
}
|
|
|
|
sql = sql + sql1;
|
|
sql = sql + " from jhames.barcode_info b,jhames.wip_info c,jhames.wip_att d";
|
|
sql = sql + sql2;
|
|
|
|
sql = sql + " where b.wip_id = c.wip_id and c.wip_no = d.wip_no";
|
|
|
|
sql = sql + sql3;
|
|
|
|
sql = sql + " and c.wip_no = '" + wipNO + "'";
|
|
|
|
DbConnection conn = _context.Database.GetDbConnection();
|
|
if (conn.State != System.Data.ConnectionState.Open)
|
|
{
|
|
await conn.OpenAsync();
|
|
}
|
|
|
|
using (var cmd = conn.CreateCommand())
|
|
{
|
|
cmd.CommandText = sql;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
List<dynamic> list = new List<dynamic>();
|
|
DataTable table = new DataTable();
|
|
|
|
table = DataReaderToDataTable(reader);
|
|
|
|
table.Columns.Add("KP_MAC");
|
|
table.AcceptChanges();
|
|
|
|
//判断MB组件增加查询MAC
|
|
try
|
|
{
|
|
if (wip_kp.Count > 0)
|
|
{
|
|
/*
|
|
for (int i = 0; i < wip_kp.Count; i++)
|
|
{
|
|
if (wip_kp[i].KpNo.StartsWith("MB"))
|
|
{
|
|
table.Columns.Add("MAC");
|
|
table.AcceptChanges();
|
|
break;
|
|
}
|
|
}
|
|
*/
|
|
|
|
ESUNContext _esun_context = new ESUNContext();
|
|
|
|
//读取MAC
|
|
DbConnection esun_conn = _esun_context.Database.GetDbConnection();
|
|
if (esun_conn.State != System.Data.ConnectionState.Open)
|
|
{
|
|
await esun_conn.OpenAsync();
|
|
}
|
|
|
|
for (int j = 0; j < table.Rows.Count; j++)
|
|
{
|
|
string barcode_no = table.Rows[j]["BarcodeNo"].ToString();
|
|
|
|
string mac_sql = string.Format(@"select distinct a.工單編號 as mo_id,e.客戶工單編號 as customer_moid,a.工單序號 as product_sn,
|
|
case substring(f.avalue_kp_typeid, 1, 3) when 'MAC' then isnull(z.item_prefix,'')+b.SUB_ITEM_SN else b.SUB_ITEM_SN end as part_barcode,
|
|
f.avalue_kp_typeid as class,c.material_id,d.r_stn as routeid,'' as workerid,rtrim(g.sn_date) + ' ' + rtrim(g.sn_time) as create_date
|
|
from or_sn_story a
|
|
left join sub_item_db b on a.工單編號 = b.or_sn and a.工單序號 = b.or_sal
|
|
left join jh_sub_item c on a.工單編號 = c.mo_id and b.class = c.part_typeid
|
|
left join jh_sub_item_prefix z on c.mo_id = z.mo_id
|
|
left join or_sub_db d on a.工單編號 = d.or_sn and b.class = d.class
|
|
join or_list e on a.工單編號 = e.工單編號 and e.[客戶] = 'EV'
|
|
left join jh_sub_item_mapping f on b.class = f.eversun_kp_typeid
|
|
join jh_sn_list g on a.工單序號 = g.sn
|
|
where g.sn_result = 'OK'
|
|
AND a.工單序號 = '{0}'
|
|
AND c.material_id LIKE 'MAC%'", barcode_no);
|
|
|
|
using (var esun_cmd = esun_conn.CreateCommand())
|
|
{
|
|
esun_cmd.CommandText = mac_sql;
|
|
|
|
using (var esun_reader = await esun_cmd.ExecuteReaderAsync())
|
|
{
|
|
if (esun_reader.HasRows)
|
|
{
|
|
List<dynamic> esun_list = new List<dynamic>();
|
|
DataTable esun_table = new DataTable();
|
|
|
|
esun_table = DataReaderToDataTable(esun_reader);
|
|
|
|
if (esun_table.Rows.Count > 0)
|
|
{
|
|
string mac_list = "";
|
|
for (int k = 0; k < esun_table.Rows.Count; k++)
|
|
{
|
|
mac_list = mac_list + esun_table.Rows[k]["part_barcode"].ToString().Trim() + ",";
|
|
}
|
|
|
|
if (mac_list != "")
|
|
{
|
|
string mac = mac_list.Substring(0, mac_list.Length - 1);
|
|
table.Rows[j]["KP_MAC"] = mac;
|
|
table.AcceptChanges();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
}
|
|
catch { }
|
|
|
|
|
|
foreach (DataRow row in table.Rows)
|
|
{
|
|
dynamic dyn = new ExpandoObject();
|
|
list.Add(dyn);
|
|
foreach (DataColumn column in table.Columns)
|
|
{
|
|
var dic = (IDictionary<string, object>)dyn;
|
|
dic[column.ColumnName] = row[column];
|
|
}
|
|
}
|
|
|
|
result.DataTotal = list.Count();
|
|
result.Data = list;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢工單KeyParts資料QRS013
|
|
/// </summary>
|
|
/// <param name="wipNO"></param>
|
|
/// <param name="factoryNo"></param>
|
|
/// <param name="page"></param>
|
|
/// <param name="limit"></param>
|
|
/// <returns></returns>
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetWipInfo4QRS013V1(string wipNO, string factoryNo, int page, int limit)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
Helper helper = new Helper(_context);
|
|
|
|
var q = from q1 in _context.WipInfos
|
|
join q2 in _context.BarcodeInfoes on q1.WipID equals q2.WipID
|
|
join q3 in _context.BarcodeItems on q2.BarcodeID equals q3.BarcodeID into item_data
|
|
from x in item_data.DefaultIfEmpty()
|
|
join q4 in _context.Items on x.ItemNo equals q4.ItemNo into item_base
|
|
from y in item_base.DefaultIfEmpty()
|
|
join q5 in _context.WipAtts on q1.WipNO equals q5.WipNO
|
|
select new
|
|
{
|
|
q1.WipID,
|
|
q1.WipNO,
|
|
q1.WerksNO,
|
|
q2.BarcodeNo,
|
|
q2.ExtraBarcodeNo,
|
|
q5.ModelNO,
|
|
q5.ItemNO,
|
|
KeyPartNo = x.ItemNo,
|
|
KeyPartItem = x.KpItemNo,
|
|
KeyPartSN = x.PartNo,
|
|
KeyPartUserNo = helper.GetUserNo(x.CreateUserID).Result,
|
|
KeyPartUserName = helper.GetUserName(x.CreateUserID).Result,
|
|
KeyPartDate = x.CreateDate
|
|
};
|
|
|
|
if (wipNO != null && wipNO != "")
|
|
{
|
|
q = q.Where(w => w.WipNO == wipNO);
|
|
}
|
|
if (factoryNo != null && factoryNo != "")
|
|
{
|
|
q = q.Where(w => w.WerksNO == factoryNo);
|
|
}
|
|
//紀錄筆數
|
|
result.DataTotal = q.Count();
|
|
|
|
// Table 頁數
|
|
if (page > 0)
|
|
{
|
|
q = q.Skip((page - 1) * limit).Take(limit);
|
|
}
|
|
|
|
result.Data = await q.ToListAsync();
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢工單資料QRS014
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetWipInfo4QRS014(string unitNo, string itemNO, string wipNO, string dateStart, string dateEnd, string modelNO, string werksNo, string dateType)
|
|
{
|
|
string fpyStation = _config["FPYStation"].ToString();
|
|
if (unitNo == "I")
|
|
{
|
|
fpyStation = _config["I_FPYStation"].ToString();
|
|
}
|
|
|
|
string[] fpy_station = fpyStation.Split(',');
|
|
|
|
var s = from q1 in _context.Stationses.Where(y => fpy_station.Contains(y.StationName))
|
|
select new
|
|
{
|
|
q1.StationID,
|
|
q1.StationName
|
|
};
|
|
|
|
var sdata = await s.ToListAsync();
|
|
|
|
int[] slist = new int[sdata.Count];
|
|
for (int i = 0; i < slist.Length; i++)
|
|
{
|
|
slist[i] = sdata[i].StationID;
|
|
}
|
|
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
var q = from q1 in _context.WipInfos
|
|
join q2 in _context.WipAtts on q1.WipNO equals q2.WipNO
|
|
join q3 in _context.LineInfoes on q1.LineID equals q3.LineID
|
|
join q4 in _context.FactoryUnits on q1.UnitNO equals q4.UnitNo
|
|
join q5 in _context.WipStations.Where(y => slist.Contains(y.StationID) && y.CreateDate >= DateTime.Parse(dateStart) && y.CreateDate <= DateTime.Parse(dateEnd).AddDays(1)).GroupBy(x => new { x.WipID }).Select(x => new { WipID = x.Key.WipID, CreateDate = x.Min(o => o.CreateDate) }) on q1.WipID equals q5.WipID
|
|
//join q5 in _context.WipStations.Where(y => slist.Contains(y.StationID) && y.CreateDate >= DateTime.Parse(dateStart) && y.CreateDate <= DateTime.Parse(dateEnd).AddDays(1)).GroupBy(x => new { x.WipID }).Select(x => new { WipID = x.Key.WipID, CreateDate = x.Min(o => o.CreateDate) }) on q1.WipID equals q5.WipID into wip_data
|
|
//from x1 in wip_data.DefaultIfEmpty()
|
|
//join q6 in _context.FqcResultMasters.GroupBy(x => new { x.WipNo }).Select(x => new { WipNo = x.Key.WipNo, FqcDate = x.Max(o => o.EndTime) }) on q2.WipNO equals q6.WipNo
|
|
join q6 in _context.FqcResultMasters.GroupBy(x => new { x.WipNo }).Select(x => new { WipNo = x.Key.WipNo, FqcDate = x.Max(o => o.EndTime) }) on q2.WipNO equals q6.WipNo into fqc_data
|
|
from x2 in fqc_data.DefaultIfEmpty()
|
|
select new
|
|
{
|
|
q1.WipID,
|
|
q1.WipNO,
|
|
q1.WerksNO,
|
|
q1.CustomerMedical,
|
|
q1.PlanQTY,
|
|
q1.CompleteQTY,
|
|
q1.UnitNO,
|
|
q1.LineID,
|
|
q1.FlowRuleID,
|
|
q1.StatusNO,
|
|
q1.WipScheduleDate,
|
|
//CreateDate = (x1.CreateDate == null ? DateTime.Now : x1.CreateDate),
|
|
q5.CreateDate,
|
|
q2.ItemNO,
|
|
q2.ModelNO,
|
|
q3.LineDesc,
|
|
q4.UnitName,
|
|
FqcDate = (x2.FqcDate == null ? DateTime.Now : x2.FqcDate)
|
|
};
|
|
|
|
q = q.Where(w => w.CompleteQTY > 0);
|
|
|
|
if (unitNo != "*")
|
|
{
|
|
q = q.Where(w => w.UnitNO == unitNo);
|
|
}
|
|
if (werksNo != "*" && werksNo != null)
|
|
{
|
|
q = q.Where(w => w.WerksNO == werksNo);
|
|
}
|
|
if (itemNO != null && itemNO != "")
|
|
{
|
|
q = q.Where(w => w.ItemNO == itemNO);
|
|
}
|
|
if (modelNO != null && modelNO != "")
|
|
{
|
|
q = q.Where(w => w.ModelNO == modelNO);
|
|
}
|
|
if (wipNO != null && wipNO != "")
|
|
{
|
|
q = q.Where(w => w.WipNO == wipNO);
|
|
}
|
|
|
|
if (dateType == "WipDate" || dateType == null)
|
|
{
|
|
if (dateStart != null && dateStart != "" && dateEnd != null && dateEnd != "")
|
|
{
|
|
q = q.Where(w => w.CreateDate >= DateTime.Parse(dateStart) && w.CreateDate <= DateTime.Parse(dateEnd).AddDays(1));
|
|
}
|
|
}
|
|
else
|
|
{
|
|
if (dateStart != null && dateStart != "" && dateEnd != null && dateEnd != "")
|
|
{
|
|
q = q.Where(w => w.FqcDate >= DateTime.Parse(dateStart) && w.FqcDate <= DateTime.Parse(dateEnd).AddDays(1));
|
|
}
|
|
}
|
|
|
|
//紀錄筆數
|
|
result.DataTotal = q.Distinct().ToList().Count();
|
|
|
|
var data1 = await q.Distinct().ToListAsync();
|
|
var data2 = data1.OrderByDescending(x => x.WipNO).ToList();
|
|
|
|
//result.Data = await q.Distinct().ToListAsync();
|
|
result.Data = data2;
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢工單資料QRS014
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetWipInfo4QRS014B(string itemNO, string wipNO, string dateStart, string dateEnd, string modelNO, string werksNo)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
var q = from q1 in _context.WipInfos
|
|
join q2 in _context.WipAtts on q1.WipNO equals q2.WipNO
|
|
join q3 in _context.LineInfoes on q1.LineID equals q3.LineID
|
|
join q4 in _context.FactoryUnits on q1.UnitNO equals q4.UnitNo
|
|
select new
|
|
{
|
|
q1.WipID,
|
|
q1.WipNO,
|
|
q1.WerksNO,
|
|
q1.CustomerMedical,
|
|
q1.PlanQTY,
|
|
q1.CompleteQTY,
|
|
q1.UnitNO,
|
|
q1.LineID,
|
|
q1.FlowRuleID,
|
|
q1.StatusNO,
|
|
q1.CreateDate,
|
|
q2.ItemNO,
|
|
q2.ModelNO,
|
|
q3.LineDesc,
|
|
q4.UnitName
|
|
};
|
|
|
|
q = q.Where(w => w.CompleteQTY > 0);
|
|
|
|
q = q.Where(w => w.UnitNO == "B" && w.CustomerMedical == "N");
|
|
|
|
if (werksNo != "*")
|
|
{
|
|
q = q.Where(w => w.WerksNO == werksNo);
|
|
}
|
|
if (itemNO != null && itemNO != "")
|
|
{
|
|
q = q.Where(w => w.ItemNO == itemNO);
|
|
}
|
|
if (modelNO != null && modelNO != "")
|
|
{
|
|
q = q.Where(w => w.ModelNO == modelNO);
|
|
}
|
|
if (wipNO != null && wipNO != "")
|
|
{
|
|
q = q.Where(w => w.WipNO == wipNO);
|
|
}
|
|
if (dateStart != null && dateStart != "" && dateEnd != null && dateEnd != "")
|
|
{
|
|
q = q.Where(w => w.CreateDate >= DateTime.Parse(dateStart) && w.CreateDate <= DateTime.Parse(dateEnd).AddDays(1));
|
|
}
|
|
|
|
//紀錄筆數
|
|
result.DataTotal = q.Count();
|
|
|
|
result.Data = await q.ToListAsync();
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢工單資料QRS014
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetWipInfo4QRS014S(string itemNO, string wipNO, string dateStart, string dateEnd, string modelNO, string werksNo)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
var q = from q1 in _context.WipInfos
|
|
join q2 in _context.WipAtts on q1.WipNO equals q2.WipNO
|
|
join q3 in _context.LineInfoes on q1.LineID equals q3.LineID
|
|
join q4 in _context.FactoryUnits on q1.UnitNO equals q4.UnitNo
|
|
select new
|
|
{
|
|
q1.WipID,
|
|
q1.WipNO,
|
|
q1.WerksNO,
|
|
q1.CustomerMedical,
|
|
q1.PlanQTY,
|
|
q1.CompleteQTY,
|
|
q1.UnitNO,
|
|
q1.LineID,
|
|
q1.FlowRuleID,
|
|
q1.StatusNO,
|
|
q1.CreateDate,
|
|
q2.ItemNO,
|
|
q2.ModelNO,
|
|
q3.LineDesc,
|
|
q4.UnitName
|
|
};
|
|
|
|
q = q.Where(w => w.CompleteQTY > 0);
|
|
|
|
q = q.Where(w => w.UnitNO == "S" && w.CustomerMedical == "N");
|
|
|
|
if (werksNo != "*")
|
|
{
|
|
q = q.Where(w => w.WerksNO == werksNo);
|
|
}
|
|
if (itemNO != null && itemNO != "")
|
|
{
|
|
q = q.Where(w => w.ItemNO == itemNO);
|
|
}
|
|
if (modelNO != null && modelNO != "")
|
|
{
|
|
q = q.Where(w => w.ModelNO == modelNO);
|
|
}
|
|
if (wipNO != null && wipNO != "")
|
|
{
|
|
q = q.Where(w => w.WipNO == wipNO);
|
|
}
|
|
if (dateStart != null && dateStart != "" && dateEnd != null && dateEnd != "")
|
|
{
|
|
q = q.Where(w => w.CreateDate >= DateTime.Parse(dateStart) && w.CreateDate <= DateTime.Parse(dateEnd).AddDays(1));
|
|
}
|
|
|
|
//紀錄筆數
|
|
result.DataTotal = q.Count();
|
|
|
|
result.Data = await q.ToListAsync();
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢工單資料QRS014
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetWipInfo4QRS014M(string unitNo, string itemNO, string wipNO, string dateStart, string dateEnd, string modelNO, string werksNo)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
var q = from q1 in _context.WipInfos
|
|
join q2 in _context.WipAtts on q1.WipNO equals q2.WipNO
|
|
join q3 in _context.LineInfoes on q1.LineID equals q3.LineID
|
|
join q4 in _context.FactoryUnits on q1.UnitNO equals q4.UnitNo
|
|
select new
|
|
{
|
|
q1.WipID,
|
|
q1.WipNO,
|
|
q1.WerksNO,
|
|
q1.CustomerMedical,
|
|
q1.PlanQTY,
|
|
q1.CompleteQTY,
|
|
q1.UnitNO,
|
|
q1.LineID,
|
|
q1.FlowRuleID,
|
|
q1.StatusNO,
|
|
q1.CreateDate,
|
|
q2.ItemNO,
|
|
q2.ModelNO,
|
|
q3.LineDesc,
|
|
q4.UnitName
|
|
};
|
|
|
|
q = q.Where(w => w.CompleteQTY > 0 && w.CustomerMedical == "Y");
|
|
|
|
if (unitNo != "*")
|
|
{
|
|
q = q.Where(w => w.UnitNO == unitNo);
|
|
}
|
|
if (werksNo != "*")
|
|
{
|
|
q = q.Where(w => w.WerksNO == werksNo);
|
|
}
|
|
if (itemNO != null && itemNO != "")
|
|
{
|
|
q = q.Where(w => w.ItemNO == itemNO);
|
|
}
|
|
if (modelNO != null && modelNO != "")
|
|
{
|
|
q = q.Where(w => w.ModelNO == modelNO);
|
|
}
|
|
if (wipNO != null && wipNO != "")
|
|
{
|
|
q = q.Where(w => w.WipNO == wipNO);
|
|
}
|
|
if (dateStart != null && dateStart != "" && dateEnd != null && dateEnd != "")
|
|
{
|
|
q = q.Where(w => w.CreateDate >= DateTime.Parse(dateStart) && w.CreateDate <= DateTime.Parse(dateEnd).AddDays(1));
|
|
}
|
|
|
|
//紀錄筆數
|
|
result.DataTotal = q.Count();
|
|
|
|
result.Data = await q.ToListAsync();
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
/// <summary>
|
|
/// 查詢工單資料 by SelectParameter
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetWipInfoSelectParameter([FromQuery] WipInfoDto value, int page = 0, int limit = 10)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
var q = from q1 in _context.WipInfos
|
|
join q2 in _context.WipAtts on q1.WipNO equals q2.WipNO
|
|
join q3 in _context.LineInfoes on q1.LineID equals q3.LineID
|
|
join q4 in _context.FactoryUnits on q1.UnitNO equals q4.UnitNo
|
|
join q5 in _context.FactoryInfos on q1.Werks equals q5.FactoryID.ToString()
|
|
select new WipQueryDto
|
|
{
|
|
wipID = q1.WipID,
|
|
wipNo = q1.WipNO,
|
|
planQTY = q1.PlanQTY,
|
|
unitNo = q1.UnitNO,
|
|
lineID = q1.LineID,
|
|
statusNo = q1.StatusNO,
|
|
wipScheduleDate = q1.WipScheduleDate,
|
|
wipDueDate = q1.WipDueDate,
|
|
factoryNameCh = q5.FactoryNameCh,
|
|
description = q1.Description,
|
|
CreateDate = q1.CreateDate,
|
|
itemNo = q2.ItemNO,
|
|
lineDesc = q3.LineDesc,
|
|
unitName = q4.UnitName,
|
|
wipType = q1.WipType,
|
|
factoryNo = q1.WerksNO
|
|
};
|
|
|
|
|
|
if (!string.IsNullOrWhiteSpace(value.unitno))
|
|
{
|
|
q = q.Where(w => w.unitNo == value.unitno);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(value.wipno))
|
|
{
|
|
q = q.Where(w => w.wipNo == value.wipno);
|
|
}
|
|
|
|
if (value.lineid != 0)
|
|
{
|
|
q = q.Where(w => w.lineID == value.lineid);
|
|
}
|
|
|
|
DateTime dateTime = DateTime.Now;
|
|
if (DateTime.TryParse(value.date_str, out dateTime))
|
|
{
|
|
q = q.Where(w => w.CreateDate >= DateTime.Parse(value.date_str));
|
|
}
|
|
|
|
if (DateTime.TryParse(value.date_end, out dateTime))
|
|
{
|
|
q = q.Where(w => w.CreateDate <= DateTime.Parse(value.date_end));
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(value.itemno))
|
|
{
|
|
q = q.Where(w => w.itemNo.Contains(value.itemno.ToUpper().Trim()));
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(value.wipType))
|
|
{
|
|
q = q.Where(w => w.wipType == value.wipType);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(value.factoryno))
|
|
{
|
|
q = q.Where(w => w.factoryNo == value.factoryno);
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(value.statusNo))
|
|
{
|
|
if (value.statusNo == "N")
|
|
q = q.Where(w => w.statusNo != "E" && w.statusNo != "C");
|
|
else
|
|
q = q.Where(w => w.statusNo == value.statusNo);
|
|
}
|
|
|
|
q.Take(1000);
|
|
// 紀錄筆數
|
|
result.DataTotal = q.Count();
|
|
|
|
// Table 頁數
|
|
if (page > 0)
|
|
{
|
|
q = q.Skip((page - 1) * limit).Take(limit);
|
|
}
|
|
|
|
var qq = await q.ToListAsync();
|
|
|
|
// 塞入開工日
|
|
foreach (var item in qq)
|
|
{
|
|
var workDate = _context.WipStations.Where(w => w.WipID == item.wipID)
|
|
.OrderBy(s => s.CreateDate)
|
|
.FirstOrDefault();
|
|
item.workDate = workDate == null ? "" : workDate.CreateDate.ToString("yyyy/MM/dd");
|
|
}
|
|
|
|
// 塞檢驗完成日期
|
|
foreach (var item in qq)
|
|
{
|
|
var fqcDate = _context.FqcResultMasters.Where(w => w.WipNo == item.wipNo && w.QaResult != "A")
|
|
.OrderBy(s => s.EndTime)
|
|
.FirstOrDefault();
|
|
item.fqcDate = fqcDate == null ? "" : fqcDate.EndTime.ToString("yyyy/MM/dd");
|
|
}
|
|
|
|
result.Data = qq;
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢工單資料 未結工單查詢
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetWipInfoPCS008([FromQuery] WipInfoDto value, string statusNo = null)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
var q = from q1 in _context.WipInfos
|
|
where q1.CompleteQTY != 0
|
|
join q2 in _context.WipAtts on q1.WipNO equals q2.WipNO
|
|
join q3 in _context.LineInfoes on q1.LineID equals q3.LineID
|
|
join q4 in _context.FactoryUnits on q1.UnitNO equals q4.UnitNo
|
|
select new
|
|
{
|
|
q1.WipID,
|
|
q1.WipNO,
|
|
q1.PlanQTY,
|
|
q1.CompleteQTY,
|
|
q1.UnitNO,
|
|
q1.LineID,
|
|
q1.FlowRuleID,
|
|
q1.StatusNO,
|
|
q1.CreateDate,
|
|
q2.ItemNO,
|
|
q3.LineDesc,
|
|
q4.UnitName,
|
|
q1.WerksNO
|
|
};
|
|
|
|
if (statusNo == null)
|
|
{
|
|
q = q.Where(w => w.StatusNO != "E");
|
|
}
|
|
|
|
if (value.wipno != null)
|
|
{
|
|
q = q.Where(w => w.WipNO.Equals(value.wipno));
|
|
}
|
|
|
|
if (value.itemno != null)
|
|
{
|
|
q = q.Where(w => w.ItemNO.Equals(value.itemno));
|
|
}
|
|
|
|
if (value.unitno != null)
|
|
{
|
|
q = q.Where(w => w.UnitNO.Equals(value.unitno));
|
|
}
|
|
if (value.factoryno != null)
|
|
{
|
|
q = q.Where(w => w.WerksNO.Equals(value.factoryno));
|
|
}
|
|
|
|
DateTime dateTime = DateTime.Now;
|
|
if (DateTime.TryParse(value.date_str, out dateTime))
|
|
{
|
|
q = q.Where(w => w.CreateDate >= DateTime.Parse(value.date_str));
|
|
}
|
|
|
|
if (DateTime.TryParse(value.date_end, out dateTime))
|
|
{
|
|
q = q.Where(w => w.CreateDate <= DateTime.Parse(value.date_end));
|
|
}
|
|
|
|
//紀錄筆數
|
|
result.DataTotal = q.Count();
|
|
|
|
result.Data = await q.ToListAsync();
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢工單資料Info By WipID
|
|
/// </summary>
|
|
/// <param name="id">WipID</param>
|
|
/// <returns></returns>
|
|
// GET: api/RoleModules/Role/5
|
|
[HttpGet("{id}")]
|
|
public async Task<ActionResult<IEnumerable<WipInfo>>> GetWipInfo(int id)
|
|
{
|
|
IQueryable<WipInfo> q = _context.WipInfos.Where(w => w.WipID == id);
|
|
|
|
var WipInfo = await q.ToListAsync();
|
|
|
|
if (WipInfo == null)
|
|
{
|
|
return NotFound();
|
|
}
|
|
|
|
return WipInfo;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢工單資料Info By WipNO
|
|
/// </summary>
|
|
/// <param name="wipno">工單號碼</param>
|
|
/// <returns></returns>
|
|
// GET: api/RoleModules/Role/5
|
|
[HttpGet("WipInfoByWipNo/{wipno}")]
|
|
public async Task<ActionResult<IEnumerable<WipInfo>>> GetWipInfoByWipNo(string wipno)
|
|
{
|
|
IQueryable<WipInfo> q = _context.WipInfos.Where(w => w.WipNO.ToUpper().Trim() == wipno.ToUpper().Trim());
|
|
var WipInfo = await q.ToListAsync();
|
|
return WipInfo;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢工單是否投入
|
|
/// </summary>
|
|
/// <param name="wipno">工單號碼</param>
|
|
/// <returns>Y:投入N:未投入</returns>
|
|
[HttpGet("CheckStart/{wipno}")]
|
|
public ActionResult<string> GetWipInfoCheckStart(string wipno)
|
|
{
|
|
// 判斷是否有投產紀錄
|
|
var q = from q1 in _context.WipInfos
|
|
join q2 in _context.BarcodeInfoes on q1.WipID equals q2.WipID
|
|
where q1.WipNO == wipno
|
|
select q2;
|
|
|
|
if (q.Count() != 0)
|
|
return "Y";
|
|
else
|
|
return "N";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢工單是否開線
|
|
/// </summary>
|
|
/// <param name="wipno">工單號碼</param>
|
|
/// <returns>Y:已開線N:未開線</returns>
|
|
[HttpGet("CheckStartLine/{wipno}")]
|
|
public ActionResult<string> GetWipInfoCheckStartLine(string wipno)
|
|
{
|
|
// 判斷是否已經開線
|
|
var q = from q1 in _context.WipInfos
|
|
join q2 in _context.LineInfoes on q1.WipID equals q2.WipNo
|
|
where q1.WipNO == wipno
|
|
select q2;
|
|
|
|
if (q.Count() != 0)
|
|
return "Y";
|
|
else
|
|
return "N";
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增工單資料
|
|
/// </summary>
|
|
/// <param name="WipInfo"></param>
|
|
/// <returns></returns>
|
|
[HttpPost]
|
|
public async Task<ResultModel<WipInfo>> PostWipInfo([FromBody] WipInfo WipInfo)
|
|
{
|
|
ResultModel<WipInfo> result = new ResultModel<WipInfo>();
|
|
Helper helper = new Helper(_context);
|
|
WipInfo.WipID = helper.GetIDKey("WIP_ID").Result;
|
|
|
|
// 工單號碼去空白
|
|
WipInfo.WipNO = WipInfo.WipNO.Trim().ToUpper();
|
|
|
|
// 委外廠編號抓WERKS廠別代碼
|
|
var q = _context.FactoryInfos.Where(w => w.FactoryID.ToString() == WipInfo.Werks).ToList();
|
|
if (q.Count() != 0)
|
|
{
|
|
WipInfo.WerksNO = q.FirstOrDefault().FactoryNo;
|
|
}
|
|
|
|
_context.WipInfos.Add(WipInfo);
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
result.Success = true;
|
|
result.Msg = WipInfo.WipID.ToString();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.InnerException.Message;
|
|
}
|
|
return result;
|
|
|
|
//return CreatedAtAction("GetWipInfo", new { id = WipInfo.WipID }, WipInfo);
|
|
}
|
|
|
|
/// <summary>
|
|
/// 新增 工單相關資料
|
|
/// </summary>
|
|
/// <param name="wipDataView"></param>
|
|
/// <returns></returns>
|
|
[HttpPost("AddNewWipData")]
|
|
public async Task<ResultModel<dynamic>> PostWipInfo([FromBody] WipDataViewModel wipDataView)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
Helper helper = new Helper(_context);
|
|
|
|
using (var tran = _context.Database.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
int changes = 0;
|
|
|
|
// 字串調整
|
|
wipDataView.wipInfo.WipNO = wipDataView.wipInfo.WipNO.Trim().ToUpper();
|
|
wipDataView.wipAtt.ItemNO = wipDataView.wipAtt.ItemNO.Trim().ToUpper();
|
|
var wipNo = wipDataView.wipInfo.WipNO;
|
|
var itemNo = wipDataView.wipAtt.ItemNO;
|
|
var userid = wipDataView.wipInfo.CreateUserID;
|
|
|
|
#region 工單基本資料
|
|
|
|
wipDataView.wipInfo.WipID = helper.GetIDKey("WIP_ID").Result;
|
|
|
|
var q = _context.FactoryInfos.Where(w => w.FactoryID.ToString() == wipDataView.wipInfo.Werks).ToList();
|
|
if (q.Any())
|
|
wipDataView.wipInfo.WerksNO = q.FirstOrDefault().FactoryNo;
|
|
|
|
_context.WipInfos.Add(wipDataView.wipInfo);
|
|
changes = await _context.SaveChangesAsync();
|
|
if (changes == 0)
|
|
throw new Exception("Insert WipInfo failed.");
|
|
|
|
#endregion
|
|
|
|
#region 工單Log
|
|
|
|
WipLog wiplog = new WipLog
|
|
{
|
|
WipID = wipDataView.wipInfo.WipID,
|
|
StatusNO = "N",
|
|
WipDesc = ".",
|
|
CreateUserID = userid
|
|
};
|
|
_context.WipLogs.Add(wiplog);
|
|
changes = await _context.SaveChangesAsync();
|
|
if (changes == 0)
|
|
throw new Exception("Insert WipLog failed.");
|
|
|
|
#endregion
|
|
|
|
#region 工單Att
|
|
|
|
wipDataView.wipAtt.WipNO = wipNo;
|
|
wipDataView.wipAtt.CreateUserID = userid;
|
|
// 判斷工單號碼是否存在
|
|
if (_context.WipAtts.Where(w => w.WipNO == wipNo).Any())
|
|
{
|
|
_context.Entry(wipDataView.wipAtt).State = EntityState.Modified;
|
|
_context.Entry<WipAtt>(wipDataView.wipAtt).Property("CreateDate").IsModified = false;
|
|
_context.Entry<WipAtt>(wipDataView.wipAtt).Property("CreateUserID").IsModified = false;
|
|
}
|
|
else
|
|
{
|
|
_context.WipAtts.Add(wipDataView.wipAtt);
|
|
}
|
|
|
|
changes = await _context.SaveChangesAsync();
|
|
if (changes == 0)
|
|
throw new Exception("Insert||Update WipAtts failed.");
|
|
|
|
#endregion
|
|
|
|
#region 料號判斷 MaterialItem
|
|
|
|
// 料號ID
|
|
var materialid = 0;
|
|
// 料號判斷,沒有就Insert
|
|
if (!_context.MaterialItems.Where(w => w.ItemNo == itemNo).Any())
|
|
{
|
|
materialid = helper.GetIDKey("ITEM_ID").Result;
|
|
var materialItem = new MaterialItem
|
|
{
|
|
ItemID = materialid,
|
|
ItemNo = itemNo,
|
|
CreateUserID = userid
|
|
};
|
|
_context.MaterialItems.Add(materialItem);
|
|
changes = await _context.SaveChangesAsync();
|
|
if (changes == 0)
|
|
throw new Exception("Insert MaterialItems failed.");
|
|
}
|
|
else
|
|
{
|
|
materialid = _context.MaterialItems.Where(w => w.ItemNo == itemNo).FirstOrDefault().ItemID;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 工單KeyParts
|
|
|
|
if (wipDataView.WipKps.Any())
|
|
{
|
|
var materialKps = await _context.MaterialKps.Where(m => m.ItemID == materialid).ToListAsync();
|
|
_context.MaterialKps.RemoveRange(materialKps);
|
|
await _context.SaveChangesAsync();
|
|
|
|
// 先比對新舊ID 當舊ID有 新的沒有代表已刪除
|
|
var dbWipKps = _context.WipKps.Where(w => w.WipNo == wipNo).ToList();
|
|
|
|
// 找到要刪除的WipKps
|
|
var delete_WipKp = dbWipKps
|
|
.Where(dbKp => !wipDataView.WipKps.Any(viewKp => viewKp.WipKpID == dbKp.WipKpID))
|
|
.ToList();
|
|
if (delete_WipKp.Any())
|
|
{
|
|
_context.WipKps.RemoveRange(delete_WipKp);
|
|
await _context.SaveChangesAsync();
|
|
}
|
|
|
|
foreach (var item in wipDataView.WipKps.Where(w => w.WipKpID != -1))
|
|
{
|
|
var wipKp = new WipKp
|
|
{
|
|
WipNo = wipNo,
|
|
ItemNo = itemNo,
|
|
UnitNo = item.UnitNo,
|
|
KpName = item.KpName,
|
|
KpNo = item.KpNo,
|
|
KpSeq = item.KpSeq,
|
|
Length = item.Length,
|
|
Title = item.Title,
|
|
CreateUserID = userid,
|
|
UpdateUserID = userid
|
|
};
|
|
|
|
if (item.WipKpID == 0)
|
|
{
|
|
wipKp.WipKpID = helper.GetIDKey("WIP_KP_ID").Result;
|
|
_context.WipKps.Add(wipKp);
|
|
}
|
|
else
|
|
{
|
|
_context.Entry(wipKp).State = EntityState.Modified;
|
|
_context.Entry<WipKp>(wipKp).Property("CreateDate").IsModified = false;
|
|
_context.Entry<WipKp>(wipKp).Property("CreateUserID").IsModified = false;
|
|
}
|
|
|
|
changes = await _context.SaveChangesAsync();
|
|
if (changes == 0)
|
|
throw new Exception("Insert||Update WipKp failed.");
|
|
|
|
// 新增到料號KP檔
|
|
var materialKp = new MaterialKp
|
|
{
|
|
MaterialKpID = helper.GetIDKey("MATERIALKP_ID").Result,
|
|
ItemID = materialid,
|
|
KpName = item.KpName,
|
|
KpNo = item.KpNo,
|
|
KpSeq = Convert.ToInt32(item.KpSeq),
|
|
Length = item.Length,
|
|
StationType = item.UnitNo,
|
|
IsRepeat = "N",
|
|
Title = item.Title,
|
|
CreateUserID = userid,
|
|
UpdateUserID = userid
|
|
};
|
|
_context.MaterialKps.Add(materialKp);
|
|
|
|
changes = await _context.SaveChangesAsync();
|
|
if (changes == 0)
|
|
throw new Exception("Insert MaterialKp failed.");
|
|
}
|
|
}
|
|
// 將料號KP新增到WIP_KP
|
|
else
|
|
{
|
|
var wipKps = new List<WipKp>();
|
|
var materialKps = await _context.MaterialKps.Where(m => m.ItemID == materialid).ToListAsync();
|
|
foreach (var item in materialKps)
|
|
{
|
|
var wipKp = new WipKp
|
|
{
|
|
WipKpID = helper.GetIDKey("WIP_KP_ID").Result,
|
|
WipNo = wipNo,
|
|
ItemNo = itemNo,
|
|
UnitNo = item.StationType,
|
|
KpName = item.KpName,
|
|
KpNo = item.KpNo,
|
|
KpSeq = item.KpSeq,
|
|
Length = item.Length,
|
|
Title = item.Title,
|
|
CreateUserID = userid,
|
|
UpdateUserID = userid
|
|
};
|
|
wipKps.Add(wipKp);
|
|
}
|
|
|
|
if (wipKps.Any())
|
|
{
|
|
_context.WipKps.AddRange(wipKps);
|
|
changes = await _context.SaveChangesAsync();
|
|
if (changes == 0)
|
|
throw new Exception("Insert WipKp failed.");
|
|
}
|
|
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 工單治具 WipOutfit
|
|
|
|
if (!await _context.WipOutfits.Where(w => w.WipNo == wipNo).AnyAsync())
|
|
{
|
|
var materialOutfit = await _context.MaterialOutfits.Where(w => w.ItemID == materialid).ToListAsync();
|
|
foreach (var item in materialOutfit)
|
|
{
|
|
var wipOutfit = new WipOutfit
|
|
{
|
|
WipOutfitID = helper.GetIDKey("WIP_OUTFIT_ID").Result,
|
|
WipNo = wipNo,
|
|
ItemNo = itemNo,
|
|
UnitNo = wipDataView.wipInfo.UnitNO,
|
|
OutfitNo = item.OutfitNo,
|
|
PartNo = item.StationType,
|
|
CreateUserID = userid,
|
|
UpdateUserID = userid
|
|
};
|
|
_context.WipOutfits.Add(wipOutfit);
|
|
changes = await _context.SaveChangesAsync();
|
|
if (changes == 0)
|
|
throw new Exception("Insert WipOutfits failed.");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region 工單SOP WipSop
|
|
|
|
if (!await _context.WipSops.Where(w => w.WipNo == wipNo).AnyAsync())
|
|
{
|
|
var materialSop = await _context.MaterialSops.Where(w => w.ItemNo == itemNo).ToListAsync();
|
|
foreach (var item in materialSop)
|
|
{
|
|
var wipSop = new WipSop
|
|
{
|
|
WipSOPID = helper.GetIDKey("WIP_SOP_ID").Result,
|
|
WipNo = wipNo,
|
|
ItemNo = itemNo,
|
|
UnitNo = item.UnitNo,
|
|
SOPName = item.SopName,
|
|
SOPPath = item.SopPath,
|
|
SOPType = item.SopType,
|
|
CreateUserID = userid,
|
|
UpdateUserID = userid
|
|
};
|
|
_context.WipSops.Add(wipSop);
|
|
changes = await _context.SaveChangesAsync();
|
|
if (changes == 0)
|
|
throw new Exception("Insert WipSops failed.");
|
|
}
|
|
}
|
|
|
|
#endregion
|
|
|
|
await tran.CommitAsync();
|
|
result.Success = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await tran.RollbackAsync();
|
|
result.Success = false;
|
|
result.Msg = ex.InnerException?.Message ?? ex.Message;
|
|
}
|
|
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新工單資本資料-狀態
|
|
/// </summary>
|
|
/// <param name="id">工單ID</param>
|
|
/// <param name="statusno">狀態</param>
|
|
/// <returns></returns>
|
|
[HttpPut("{id}/{statusno}")]
|
|
public async Task<ResultModel<WipInfo>> PutWipinfoToStatusNO(int id = 0, string statusno = null)
|
|
{
|
|
ResultModel<WipInfo> result = new ResultModel<WipInfo>();
|
|
try
|
|
{
|
|
await _context.Database.ExecuteSqlInterpolatedAsync
|
|
($"UPDATE JHAMES.WIP_INFO SET STATUS_NO={statusno} , UPDATE_DATE={DateTime.Now} WHERE WIP_ID={id}");
|
|
|
|
//WipInfo wipinfo = new WipInfo
|
|
//{
|
|
// WipID = id,
|
|
// StatusNO = statusno,
|
|
// UpdateDate = DateTime.Now
|
|
//};
|
|
//_context.WipInfos.Attach(wipinfo);
|
|
//// 指定更新某個欄位
|
|
//_context.Entry(wipinfo).Property(p => p.StatusNO).IsModified = true;
|
|
//_context.Entry(wipinfo).Property(p => p.UpdateDate).IsModified = true;
|
|
|
|
//await _context.SaveChangesAsync();
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.Message;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 更新工單資本資料
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPut]
|
|
public async Task<ResultModel<WipInfo>> PutWipinfo([FromBody] WipInfo wipInfo)
|
|
{
|
|
ResultModel<WipInfo> result = new ResultModel<WipInfo>();
|
|
// 工單號碼去空白
|
|
wipInfo.WipNO = wipInfo.WipNO.Trim().ToUpper();
|
|
|
|
// 委外廠編號抓WERKS廠別代碼
|
|
var q = _context.FactoryInfos.Where(w => w.FactoryID.ToString() == wipInfo.Werks).ToList();
|
|
if (q.Count() != 0)
|
|
{
|
|
wipInfo.WerksNO = q.FirstOrDefault().FactoryNo;
|
|
}
|
|
|
|
_context.Entry(wipInfo).State = EntityState.Modified;
|
|
_context.Entry<WipInfo>(wipInfo).Property("CompleteQTY").IsModified = false;
|
|
_context.Entry<WipInfo>(wipInfo).Property("StatusNO").IsModified = false;
|
|
_context.Entry<WipInfo>(wipInfo).Property("CreateDate").IsModified = false;
|
|
_context.Entry<WipInfo>(wipInfo).Property("CreateUserID").IsModified = false;
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.InnerException.Message;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
//------ 此部分為安勤Alex撰寫 ------
|
|
/// <summary>
|
|
/// 更新工單特定欄位
|
|
/// </summary>
|
|
/// <param name="wipInfo">更新工單特定欄位</param>
|
|
/// <returns>操作結果</returns>
|
|
[HttpPut("UpdateWipinfoSpecificFields")]
|
|
public async Task<ResultModel<WipInfo>> UpdateWipinfoSpecificFields([FromBody] WipInfo wipInfo)
|
|
{
|
|
var result = new ResultModel<WipInfo>();
|
|
|
|
// 去除空白並轉為大寫
|
|
wipInfo.WipNO = wipInfo.WipNO.Trim().ToUpper();
|
|
|
|
// 委外廠編號抓WERKS廠別代碼
|
|
var factoryInfo = await _context.FactoryInfos
|
|
.Where(f => f.FactoryID.ToString() == wipInfo.Werks)
|
|
.Select(f => f.FactoryNo)
|
|
.FirstOrDefaultAsync();
|
|
|
|
if (factoryInfo != null)
|
|
{
|
|
wipInfo.WerksNO = factoryInfo;
|
|
}
|
|
|
|
var existingWipInfo = await _context.WipInfos.FindAsync(wipInfo.WipID);
|
|
if (existingWipInfo == null)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = "WipInfo not found.";
|
|
return result;
|
|
}
|
|
|
|
_context.Entry(existingWipInfo).CurrentValues.SetValues(wipInfo);
|
|
|
|
var propertiesToExclude = new[]
|
|
{
|
|
"WipID", "WipNO", "CompleteQTY", "LineID", "UnitNO", "ProductTypeID",
|
|
"Sales", "OrderNO", "FlowRuleID", "StatusNO", "WipTimes", "CNO",
|
|
"BoxCNT", "SFISFlowCk", "InputFlag", "ModelCheck", "WipSEQ",
|
|
"ECNCheck", "CreateUserID", "CreateDate", "UpdateDate",
|
|
"PrintFlag", "Remarks", "Description", "Priority",
|
|
"Werks", "WipType", "CustomerType", "RelatedWONO", "WerksNO",
|
|
"CustomerNO", "CustomerItemNO", "FlowRemark"
|
|
};
|
|
|
|
foreach (var property in propertiesToExclude)
|
|
{
|
|
_context.Entry(existingWipInfo).Property(property).IsModified = false;
|
|
}
|
|
|
|
var propertiesToUpdate = new[]
|
|
{
|
|
"PlanQTY", "WipScheduleDate", "WipDueDate", "CustomerMedical", "CustomerVIP"
|
|
};
|
|
|
|
foreach (var property in propertiesToUpdate)
|
|
{
|
|
_context.Entry(existingWipInfo).Property(property).IsModified = true;
|
|
}
|
|
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
result.Success = true;
|
|
result.Msg = "Update successful";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = $"An error occurred: {ex.Message}";
|
|
}
|
|
|
|
return result;
|
|
}
|
|
//---------------------------------------
|
|
|
|
/// <summary>
|
|
/// 更新工單第一站完成數量
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPut("UpdateCompleteQty/ById/{id}")]
|
|
public async Task<ResultModel<WipInfo>> PutWipinfoByCompleteQTY(int id)
|
|
{
|
|
ResultModel<WipInfo> result = new ResultModel<WipInfo>();
|
|
result.Success = true;
|
|
var wipInfo = await _context.WipInfos.FindAsync(id);
|
|
|
|
if (wipInfo != null)
|
|
{
|
|
wipInfo.CompleteQTY += 1;
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.InnerException.Message;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// 删除工單相關資料
|
|
/// </summary>
|
|
/// <param name="id">工單號碼</param>
|
|
/// <returns></returns>
|
|
[HttpDelete("{id}")]
|
|
public async Task<ResultModel<WipInfo>> DeleteWipinfo(int id)
|
|
{
|
|
ResultModel<WipInfo> result = new ResultModel<WipInfo>();
|
|
|
|
var wipinfos = await _context.WipInfos.Where(w => w.WipID == id).FirstOrDefaultAsync();
|
|
var WiwipinfoByWipNo = await _context.WipInfos.Where(w => w.WipNO == wipinfos.WipNO).ToListAsync();
|
|
_context.WipInfos.RemoveRange(wipinfos);
|
|
|
|
var wipBarcode = await _context.WipBarcodes.Where(w => w.WipNO == wipinfos.WipNO && w.UnitNO == wipinfos.UnitNO).ToListAsync();
|
|
_context.WipBarcodes.RemoveRange(wipBarcode);
|
|
|
|
if (WiwipinfoByWipNo.Count() == 1)
|
|
{
|
|
var WipNo = WiwipinfoByWipNo.FirstOrDefault().WipNO;
|
|
var wipAtt = await _context.WipAtts.Where(w => w.WipNO == WipNo).ToListAsync();
|
|
_context.WipAtts.RemoveRange(wipAtt);
|
|
|
|
var wipKp = await _context.WipKps.Where(w => w.WipNo == WipNo).ToListAsync();
|
|
_context.WipKps.RemoveRange(wipKp);
|
|
|
|
var wipSop = await _context.WipSops.Where(w => w.WipNo == WipNo).ToListAsync();
|
|
_context.WipSops.RemoveRange(wipSop);
|
|
|
|
var wipOutfit = await _context.WipOutfits.Where(w => w.WipNo == WipNo).ToListAsync();
|
|
_context.WipOutfits.RemoveRange(wipOutfit);
|
|
|
|
var wipLabel = await _context.WipLabels.Where(w => w.WipNO == WipNo).ToListAsync();
|
|
_context.WipLabels.RemoveRange(wipLabel);
|
|
|
|
var wipBarcodeOther = await _context.WipBarcodeOthers.Where(w => w.WipNO == WipNo).ToListAsync();
|
|
_context.WipBarcodeOthers.RemoveRange(wipBarcodeOther);
|
|
|
|
var wipBoard = await _context.WipBoards.Where(w => w.WipNo == WipNo).ToListAsync();
|
|
_context.WipBoards.RemoveRange(wipBoard);
|
|
|
|
var wipSystem = await _context.WipSystems.Where(w => w.WipNo == WipNo).ToListAsync();
|
|
_context.WipSystems.RemoveRange(wipSystem);
|
|
|
|
var wipChecks = await _context.WipChecks.Where(w => w.WipNo == WipNo).ToListAsync();
|
|
_context.WipChecks.RemoveRange(wipChecks);
|
|
|
|
var wipInfoBlob = await _context.WipInfoBlobs.Where(w => w.WipNo == WipNo).ToListAsync();
|
|
_context.WipInfoBlobs.RemoveRange(wipInfoBlob);
|
|
}
|
|
try
|
|
{
|
|
await _context.SaveChangesAsync();
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.InnerException.Message;
|
|
}
|
|
return result;
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 查詢DNInfo --表頭
|
|
/// </summary>
|
|
/// <param name="dnNo"></param>
|
|
/// <param name="lineNo"></param>
|
|
/// <returns></returns>
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetZDNDetail4PTD001(string dnNo, string lineNo)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
string strSQL = $@"select DNNO,DNLineNO,ProductID,ShipQty,ShipCustomerID,SoldCustomerID,ExpectShipDate ,
|
|
CurrentShipDate
|
|
from SFIS_PTD.dbo.ZDNDetail where DNNO ='{dnNo}' ";
|
|
if (!string.IsNullOrEmpty(lineNo))
|
|
{
|
|
strSQL += $@"And DNLineNO ='{lineNo}'";
|
|
}
|
|
DataTable dtZDNDetail = new DataTable();
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
dtZDNDetail = DataReaderToDataTable(reader);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
|
|
List<dynamic> list = new List<dynamic>();
|
|
foreach (DataRow row in dtZDNDetail.Rows)
|
|
{
|
|
dynamic dyn = new ExpandoObject();
|
|
list.Add(dyn);
|
|
foreach (DataColumn column in dtZDNDetail.Columns)
|
|
{
|
|
var dic = (IDictionary<string, object>)dyn;
|
|
dic[column.ColumnName] = row[column];
|
|
}
|
|
}
|
|
|
|
result.DataTotal = list.Count();
|
|
result.Data = list;
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
|
|
|
|
/// <summary>
|
|
/// 查詢DNInfo --細項
|
|
/// </summary>
|
|
/// <param name="dnNo"></param>
|
|
/// <param name="lineNo"></param>
|
|
/// <returns></returns>
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetDNInfo4PTD001(string dnNo, string lineNo)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
string strSQL = $@"select RecordNumber as DN單號,MOID as WipNo,ProductID as ItemNO,SerialNumber as ExtraBarcodeNo,'' as BarcodeNo,
|
|
RecordDate as StartDate
|
|
from SFIS_PTD.dbo.ZProductTrans where RecordNumber ='{dnNo}' and RCLineNO ='{lineNo}'
|
|
order by SerialNumber";
|
|
DataTable dtZProductTrans = new DataTable();
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
dtZProductTrans = DataReaderToDataTable(reader);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
DbConnection conn = _context.Database.GetDbConnection();
|
|
if (conn.State != ConnectionState.Open)
|
|
{
|
|
await conn.OpenAsync();
|
|
}
|
|
|
|
DataTable dtBarcode = new DataTable();
|
|
|
|
try
|
|
{
|
|
foreach (DataRow dr in dtZProductTrans.Rows)
|
|
{
|
|
string wip_sql = $@"SELECT '{dnNo}' as DN單號,WI.WIP_NO as WipNo,WA.ITEM_NO as ItemNO, BI.EXTRA_BARCODE_NO as ExtraBarcodeNo, BI.BARCODE_NO as BarcodeNo, BI.WIP_ID,
|
|
BI.BARCODE_ID,TO_CHAR((select min(create_date) from jhames.wip_station where wip_id = BI.WIP_ID),'YYYY-MM-DD HH24:MI:SS') StartDate
|
|
FROM jhames.BARCODE_INFO BI, jhames.WIP_INFO WI, jhames.WIP_ATT WA
|
|
WHERE BI.WIP_ID = WI.WIP_ID
|
|
AND WI.WIP_NO = WA.WIP_NO
|
|
AND BI.EXTRA_BARCODE_NO = '{dr["ExtraBarcodeNo"]}'";
|
|
|
|
using (var cmd = conn.CreateCommand())
|
|
{
|
|
cmd.CommandText = wip_sql;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (dtBarcode.Columns.Count == 0)
|
|
{
|
|
for (int i = 0; i < reader.FieldCount; i++)
|
|
{
|
|
dtBarcode.Columns.Add(reader.GetName(i), reader.GetFieldType(i));
|
|
}
|
|
}
|
|
|
|
if (reader.HasRows)
|
|
{
|
|
dtBarcode.Merge(DataReaderToDataTable(reader));
|
|
}
|
|
else
|
|
{
|
|
DataRow newRow = dtBarcode.NewRow();
|
|
foreach (DataColumn col in dtZProductTrans.Columns)
|
|
{
|
|
newRow[col.ColumnName] = dr[col.ColumnName];
|
|
}
|
|
dtBarcode.Rows.Add(newRow);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
Console.WriteLine(e.Message);
|
|
}
|
|
//開始取KP資料
|
|
string strbarcode_id = "''";
|
|
foreach (DataRow drbarcode in dtBarcode.Rows)
|
|
{
|
|
if (!string.IsNullOrEmpty(drbarcode["BARCODE_ID"].ToString()))
|
|
strbarcode_id += $",{drbarcode["BARCODE_ID"]}";
|
|
}
|
|
|
|
DataTable dtKp = new DataTable();
|
|
string kp_sql = @"select kp_no, max(kp_qty) kp_qty from(
|
|
select item_no as kp_no,count(item_no) kp_qty,barcode_id from jhames.barcode_item where barcode_id
|
|
in (" + strbarcode_id + ") group by item_no,barcode_id) a group by kp_no order by kp_no";
|
|
|
|
using (var cmd = conn.CreateCommand())
|
|
{
|
|
cmd.CommandText = kp_sql;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
dtKp = DataReaderToDataTable(reader);
|
|
}
|
|
}
|
|
}
|
|
|
|
if (dtKp.Rows.Count > 0)
|
|
{
|
|
for (int i = 0; i < dtKp.Rows.Count; i++)
|
|
{
|
|
int kp_qty = int.Parse(dtKp.Rows[i]["KP_QTY"].ToString());
|
|
string kp_no = dtKp.Rows[i]["KP_NO"].ToString();
|
|
if (kp_qty > 1)
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Columns.Add("KP_" + kp_no);
|
|
}
|
|
catch { }
|
|
|
|
for (int j = 1; j < kp_qty; j++)
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Columns.Add("KP_" + kp_no + "#" + j.ToString());
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Columns.Add("KP_" + kp_no);
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
dtBarcode.AcceptChanges();
|
|
|
|
for (int i = 0; i < dtBarcode.Rows.Count; i++)
|
|
{
|
|
string barcode_no = dtBarcode.Rows[i]["BarcodeNo"].ToString();
|
|
|
|
//读取组件
|
|
for (int j = 0; j < dtKp.Rows.Count; j++)
|
|
{
|
|
string kp_no = dtKp.Rows[j]["KP_NO"].ToString();
|
|
|
|
string barcode_item_sql = string.Format(@"select part_no from jhames.barcode_info a,jhames.barcode_item b where a.barcode_id = b.barcode_id
|
|
and b.item_no = '{0}' and a.barcode_no = '{1}'", kp_no, barcode_no);
|
|
|
|
using (var item_cmd = conn.CreateCommand())
|
|
{
|
|
item_cmd.CommandText = barcode_item_sql;
|
|
|
|
using (var item_reader = await item_cmd.ExecuteReaderAsync())
|
|
{
|
|
if (item_reader.HasRows)
|
|
{
|
|
DataTable dtItem = new DataTable();
|
|
dtItem = DataReaderToDataTable(item_reader);
|
|
|
|
for (int k = 0; k < dtItem.Rows.Count; k++)
|
|
{
|
|
string part_no = dtItem.Rows[k]["PART_NO"].ToString();
|
|
try
|
|
{
|
|
dtBarcode.Rows[i]["KP_" + kp_no + "#" + k.ToString()] = part_no;
|
|
}
|
|
catch
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Rows[i]["KP_" + kp_no] = part_no;
|
|
}
|
|
catch { }
|
|
}
|
|
|
|
dtBarcode.AcceptChanges();
|
|
|
|
try
|
|
{
|
|
if (kp_no.StartsWith("MB") && !kp_no.StartsWith("MB_MAC"))
|
|
{
|
|
|
|
bool createMacCol = true;
|
|
|
|
bool findFlag = false;
|
|
|
|
string mac_sql = string.Format("select partbarcode from jhames.c_sfis_keyparts where productsn = '{0}' and parttypeid='MAC'", part_no);
|
|
|
|
using (var esun_cmd = conn.CreateCommand())
|
|
{
|
|
esun_cmd.CommandText = mac_sql;
|
|
esun_cmd.CommandTimeout = 0;
|
|
|
|
using (var esun_reader = await esun_cmd.ExecuteReaderAsync())
|
|
{
|
|
if (esun_reader.HasRows)
|
|
{
|
|
findFlag = true;
|
|
|
|
List<dynamic> esun_list = new List<dynamic>();
|
|
DataTable esun_table = new DataTable();
|
|
|
|
esun_table = DataReaderToDataTable(esun_reader);
|
|
|
|
if (esun_table != null)
|
|
{
|
|
WriteTraceLog("query mac by mb trace 005:esun_talbe rows qty:" + esun_table.Rows.Count);
|
|
}
|
|
|
|
if (esun_table.Rows.Count > 0)
|
|
{
|
|
if (createMacCol)
|
|
{
|
|
for (int m = 0; m < esun_table.Rows.Count; m++)
|
|
{
|
|
if (dtItem.Rows.Count == 1)
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Columns.Add("KP_" + kp_no + "#MAC" + (m + 1).ToString());
|
|
}
|
|
catch { }
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Columns.Add("KP_" + kp_no + "#" + (k + 1).ToString() + "_MAC" + (m + 1).ToString());
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
dtBarcode.AcceptChanges();
|
|
createMacCol = false;
|
|
}
|
|
|
|
WriteTraceLog("query mac by mb trace 006");
|
|
|
|
string mac = "";
|
|
for (int m = 0; m < esun_table.Rows.Count; m++)
|
|
{
|
|
//mac = esun_table.Rows[m]["part_barcode"].ToString().Trim();
|
|
mac = esun_table.Rows[m]["PARTBARCODE"].ToString().Trim();
|
|
if (dtItem.Rows.Count == 1)
|
|
{
|
|
dtBarcode.Rows[i]["KP_" + kp_no + "#MAC" + (m + 1).ToString()] = mac;
|
|
}
|
|
else
|
|
{
|
|
dtBarcode.Rows[i]["KP_" + kp_no + "#" + (k + 1).ToString() + "_MAC" + (m + 1).ToString()] = mac;
|
|
}
|
|
dtBarcode.AcceptChanges();
|
|
}
|
|
|
|
WriteTraceLog("query mac by mb trace 007");
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
//从barcode_item读取结转数据
|
|
if (!findFlag)
|
|
{
|
|
string mac_sql2 = string.Format("select a.part_no from jhames.barcode_item a,jhames.barcode_info b where a.barcode_id = b.barcode_id and b.barcode_no = '{0}' and item_no like 'MAC%'", part_no);
|
|
|
|
using (var esun_cmd = conn.CreateCommand())
|
|
{
|
|
esun_cmd.CommandText = mac_sql2;
|
|
esun_cmd.CommandTimeout = 0;
|
|
|
|
using (var esun_reader = await esun_cmd.ExecuteReaderAsync())
|
|
{
|
|
if (esun_reader.HasRows)
|
|
{
|
|
findFlag = true;
|
|
|
|
List<dynamic> esun_list = new List<dynamic>();
|
|
DataTable esun_table = new DataTable();
|
|
|
|
esun_table = DataReaderToDataTable(esun_reader);
|
|
|
|
if (esun_table != null)
|
|
{
|
|
WriteTraceLog("query mac by mb trace 005:esun_talbe rows qty:" + esun_table.Rows.Count);
|
|
}
|
|
|
|
if (esun_table.Rows.Count > 0)
|
|
{
|
|
if (createMacCol)
|
|
{
|
|
for (int m = 0; m < esun_table.Rows.Count; m++)
|
|
{
|
|
if (dtItem.Rows.Count == 1)
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Columns.Add("KP_" + kp_no + "#MAC" + (m + 1).ToString());
|
|
}
|
|
catch { }
|
|
}
|
|
else
|
|
{
|
|
try
|
|
{
|
|
dtBarcode.Columns.Add("KP_" + kp_no + "#" + (k + 1).ToString() + "_MAC" + (m + 1).ToString());
|
|
}
|
|
catch { }
|
|
}
|
|
}
|
|
dtBarcode.AcceptChanges();
|
|
createMacCol = false;
|
|
}
|
|
|
|
string mac = "";
|
|
for (int m = 0; m < esun_table.Rows.Count; m++)
|
|
{
|
|
//mac = esun_table.Rows[m]["part_barcode"].ToString().Trim();
|
|
mac = esun_table.Rows[m]["PART_NO"].ToString().Trim();
|
|
if (dtItem.Rows.Count == 1)
|
|
{
|
|
dtBarcode.Rows[i]["KP_" + kp_no + "#MAC" + (m + 1).ToString()] = mac;
|
|
}
|
|
else
|
|
{
|
|
dtBarcode.Rows[i]["KP_" + kp_no + "#" + (k + 1).ToString() + "_MAC" + (m + 1).ToString()] = mac;
|
|
}
|
|
dtBarcode.AcceptChanges();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
catch (Exception e1)
|
|
{
|
|
string err = e1.Message;
|
|
WriteTraceLog("query mac by mb trace 999:" + e1.Message);
|
|
}
|
|
}
|
|
dtBarcode.AcceptChanges();
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
|
|
|
|
if (dtBarcode.Rows.Count > 0)
|
|
{
|
|
//整理欄位
|
|
dtBarcode.Columns.Remove("WIP_ID");
|
|
dtBarcode.Columns.Remove("BARCODE_ID");
|
|
|
|
//dtBarcode.Columns["WipNo"].ColumnName = "MOID";
|
|
//dtBarcode.Columns["ItemNO"].ColumnName = "Material(料號)";
|
|
//dtBarcode.Columns["ExtraBarcodeNo"].ColumnName = "出貨料號";
|
|
//dtBarcode.Columns["BarcodeNo"].ColumnName = "生產序號";
|
|
dtBarcode.AcceptChanges();
|
|
}
|
|
|
|
List<dynamic> list = new List<dynamic>();
|
|
foreach (DataRow row in dtBarcode.Rows)
|
|
{
|
|
dynamic dyn = new ExpandoObject();
|
|
list.Add(dyn);
|
|
foreach (DataColumn column in dtBarcode.Columns)
|
|
{
|
|
var dic = (IDictionary<string, object>)dyn;
|
|
dic[column.ColumnName] = row[column];
|
|
}
|
|
}
|
|
|
|
result.DataTotal = list.Count();
|
|
result.Data = list;
|
|
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
|
|
/// <summary>
|
|
/// PTD 通知 寄信
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
private async Task<string> SendMail(string recordType, dynamic data)
|
|
{
|
|
if (recordType == "601")
|
|
{
|
|
string customer = data.Customer;
|
|
string strDNNo = data.TableData[0].DnNo;
|
|
var pdtMail = await new PTD.PTDController().GetCustomerItemMailGroup(customer);
|
|
|
|
if (pdtMail.Any(a => a.StatusNo != "S"))
|
|
{
|
|
byte[] fileContents = null;
|
|
// 設定非商業用途的許可模式
|
|
ExcelPackage.LicenseContext = LicenseContext.NonCommercial;
|
|
using (var package = new ExcelPackage())
|
|
{
|
|
var worksheet = package.Workbook.Worksheets.Add("sheet1");
|
|
|
|
// 設定標題行
|
|
worksheet.Cells[1, 1].Value = "DN單號";
|
|
worksheet.Cells[1, 2].Value = "LineNo";
|
|
worksheet.Cells[1, 3].Value = "Material(料號)";
|
|
worksheet.Cells[1, 4].Value = "SN";
|
|
worksheet.Row(1).Style.Font.Bold = true;
|
|
|
|
int i = 0;
|
|
foreach (var SNData in data.TableData)
|
|
{
|
|
// 填入資料
|
|
worksheet.Cells[i + 2, 1].Value = SNData.DnNo;
|
|
worksheet.Cells[i + 2, 2].Value = SNData.LineNo;
|
|
worksheet.Cells[i + 2, 3].Value = SNData.Material;
|
|
worksheet.Cells[i + 2, 4].Value = SNData.Sn;
|
|
i++;
|
|
}
|
|
|
|
// 自動調整欄寬
|
|
worksheet.Cells[worksheet.Dimension.Address].AutoFitColumns();
|
|
fileContents = package.GetAsByteArray();
|
|
}
|
|
|
|
var firstItem = pdtMail.FirstOrDefault();
|
|
string toMail = firstItem.MailGroup;
|
|
string Subject = $"[AMES系統通知] 出貨單號:" + strDNNo;
|
|
string emailBody = "";
|
|
await new MailController(_context, _config).PostMailByByteFile(Subject, emailBody, "", toMail, false, fileContents: fileContents, fileName: $"{strDNNo}.xlsx");
|
|
}
|
|
}
|
|
|
|
return "OK";
|
|
}
|
|
|
|
#region "PTD002 PTD002 一段式入出庫作業"
|
|
[Route("[action]")]
|
|
[HttpPost]
|
|
public async Task<ResultModel<dynamic>> PostPTD002Commit(PTD002CommitDataModel Data)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
List<string> SNList = new List<string>();
|
|
Collection<string> colSQL = new Collection<string>();
|
|
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
|
|
try
|
|
{
|
|
// RecordDate 加入目前時間
|
|
Data.RecordDate += $" {DateTime.Now:HH:mm:ss}";
|
|
|
|
string strSQL = "";
|
|
string strRecordType = Data.RecordType;
|
|
string strRmaNo = Data.RmaNo;
|
|
string strRbu = Data.Rbu;
|
|
string strWorkCenter = Data.WorkCenter;
|
|
string strEmpId = Data.loginNo;
|
|
string strRecordDate = TimeZoneInfo.ConvertTimeToUtc(DateTime.Parse(Data.RecordDate)).ToString("yyyy-MM-dd HH:mm:ss");
|
|
string strLocation = Data.Location;
|
|
string eRP_CustomerID = Data.Customer;
|
|
string strCreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
string strMaterial = "";
|
|
|
|
//確認備貨數量
|
|
string strDNNo = Data.TableData[0].DnNo;
|
|
string strLineNo = Data.TableData[0].LineNo;
|
|
int intAddQty = Data.TableData.Count();
|
|
|
|
CheckShipQty(strDNNo, strLineNo, intAddQty);
|
|
|
|
foreach (SNDataModel SNData in Data.TableData)
|
|
{
|
|
// 601 631 633 狀態時要填入 去對應ZDNDetail
|
|
string salesOrderNumber = "";
|
|
string sOLineNO = "";
|
|
if (strRecordType == "601" || strRecordType == "631" || strRecordType == "633")
|
|
{
|
|
var dnDetail = await new PTD.PTDController().GetZDNDetail(SNData.DnNo, SNData.LineNo);
|
|
if (dnDetail.Any())
|
|
{
|
|
salesOrderNumber = dnDetail.FirstOrDefault().OrderNo;
|
|
sOLineNO = dnDetail.FirstOrDefault().OrderLineNO;
|
|
eRP_CustomerID = dnDetail.FirstOrDefault().ShipCustomerID;
|
|
}
|
|
}
|
|
|
|
//CheckFlowRules(strRecordType, SNData.Sn);
|
|
strMaterial = SNData.Material;
|
|
string strLatest;
|
|
//確認此出貨序號是否有ZHistoryKeyDefine
|
|
strSQL = $@"select Seed FROM [SFIS_PTD].[dbo].[ZHistoryKeyDefine] where serialnumber ='{SNData.Sn}'";
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
int seed = Convert.ToInt32(reader["Seed"]);
|
|
int nIdx = seed + 1;
|
|
strLatest = SNData.Sn + nIdx.ToString().PadLeft(4, '0');//組成HistoryID
|
|
//HistoryKeyDefine
|
|
strSQL = @$"UPDATE [SFIS_PTD].[dbo].[ZHistoryKeyDefine]
|
|
SET [Seed] = {nIdx} where [SerialNumber]='{SNData.Sn}'";
|
|
Console.WriteLine(strSQL);
|
|
colSQL.Add(strSQL);
|
|
//ZSNInfo
|
|
strSQL = @$"Update SFIS_PTD..ZSNInfo SET LatestHistoryID='{strLatest}',RecordTypeID='{strRecordType}',
|
|
CurrentProductID='{SNData.Material}'
|
|
where SerialNumber='{SNData.Sn}'";
|
|
Console.WriteLine(strSQL);
|
|
colSQL.Add(strSQL);
|
|
}
|
|
else
|
|
{
|
|
strLatest = SNData.Sn + 1.ToString().PadLeft(4, '0');//組成HistoryID
|
|
//ZHistoryKeyDefine
|
|
strSQL = @$"INSERT INTO [SFIS_PTD].[dbo].[ZHistoryKeyDefine] ([SerialNumber],[Seed])
|
|
VALUES ('{SNData.Sn}',1)";
|
|
Console.WriteLine(strSQL);
|
|
colSQL.Add(strSQL);
|
|
//ZSNInfo
|
|
strSQL = @$"INSERT INTO [SFIS_PTD].[dbo].[ZSNInfo] ([SerialNumber],[CurrentProductID],[OriginalProductID],
|
|
[LocationID],[LatestHistoryID],[RecordTypeID],
|
|
[OwnerPlantID],[OwnerCompanyID],[CreatePlantID],[CreateCompanyID],
|
|
[ModifyDate]) VALUES
|
|
('{SNData.Sn}','{SNData.Material}','{SNData.Material}',
|
|
'{strLocation}','{strLatest}','{strRecordType}',
|
|
'{strWorkCenter}','{strRbu}','{strWorkCenter}','{strRbu}',
|
|
'{strCreateDate}')";
|
|
Console.WriteLine(strSQL);
|
|
colSQL.Add(strSQL);
|
|
}
|
|
//ZProductTrans
|
|
strSQL = $@"INSERT INTO [SFIS_PTD].[dbo].[ZProductTrans]
|
|
([HistoryID],[SerialNumber],[RecordTypeID],[RecordNumber],[RCLineNO],[RecordDate],
|
|
[ProductID],[IsChangeID],[LocationID],[EmplID],[IsDelete],
|
|
[OwnerPlantID],[OwnerCompanyID],[CreatePlantID],[CreateCompanyID],
|
|
[CreatorID],[CreateDate],[ModifierID],[ModifyDate],
|
|
[chkflag],[IsBranchCoReceived],[MOID],
|
|
[SalesOrderNumber],[SOLineNO],[ERP_CustomerID]) VALUES
|
|
('{strLatest}','{SNData.Sn}','{strRecordType}','{SNData.DnNo}','{SNData.LineNo}','{strRecordDate}',
|
|
'{SNData.Material}',0,'{strLocation}','{strEmpId}',0,
|
|
'{strWorkCenter}','{strRbu}','{strWorkCenter}','{strRbu}',
|
|
'WH00','{strCreateDate}','WH00','{strCreateDate}',0,0,'{strRmaNo}',
|
|
'{salesOrderNumber}','{sOLineNO}','{eRP_CustomerID}')";
|
|
Console.WriteLine(strSQL);
|
|
colSQL.Add(strSQL);
|
|
SNList.Add(SNData.Sn);
|
|
}
|
|
}
|
|
}
|
|
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
using (var transaction = connPTD.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
foreach (string query in colSQL)
|
|
{
|
|
cmd.CommandText = query;
|
|
cmd.Transaction = transaction;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
// Commit the transaction
|
|
transaction.Commit();
|
|
await SendMail(strRecordType, Data);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Rollback the transaction in case of an exception
|
|
transaction.Rollback();
|
|
throw ex; // Rethrow the exception after rollback
|
|
}
|
|
}
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
result.Data = SNList;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.Message;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
return result;
|
|
|
|
}
|
|
|
|
private bool CheckShipQty(string recordNumber, string lineNo, int addQty)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
connPTD.Open();
|
|
}
|
|
|
|
try
|
|
{
|
|
//查剩餘數
|
|
string strSQL = $@"select ShipQty -
|
|
(SELECT count(1)
|
|
FROM [SFIS_PTD].[dbo].[ZWHPickListDetail]
|
|
where RecordNumber =DNNO and RCLineNO =DNLineNO) as surplusQty
|
|
from [SFIS_PTD].[dbo].ZDNDetail where DNNO ='{recordNumber}' and DNLineNO ='{lineNo}'";
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
|
|
using (var reader = cmd.ExecuteReader())
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
if (addQty > Convert.ToInt32(reader["surplusQty"]))
|
|
throw new Exception("數量已超過,不允許新增!");
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
private bool CheckFlowRules(string recordType, string serialNumber)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
connPTD.Open();
|
|
}
|
|
try
|
|
{
|
|
//用ZProductTrans最新RecordType 檢查PTDFlowRules邏輯
|
|
bool bolChkFlow = false;
|
|
string strSQL = $@"Select [FormID] FROM [SFIS_PTD].[dbo].[PTDFlowRules]
|
|
where [ToID] ='{recordType}'";
|
|
DataTable dtPTDFlowRules = new DataTable();
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
|
|
using (var reader = cmd.ExecuteReader())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
dtPTDFlowRules = DataReaderToDataTable(reader);
|
|
}
|
|
}
|
|
}
|
|
|
|
strSQL = @$"Select [RecordTypeID] FROM [SFIS_PTD].[dbo].[ZProductTrans]
|
|
where [SerialNumber] ='{serialNumber}'
|
|
Order by [RecordDate] Desc";
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
using (var reader = cmd.ExecuteReader())
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
foreach (DataRow dr in dtPTDFlowRules.Rows)
|
|
{
|
|
if (reader["RecordTypeID"].ToString() == dr["FormID"].ToString())
|
|
{
|
|
bolChkFlow = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else if (recordType == "101" || recordType == "292") //101、292可能沒有前身資訊
|
|
{
|
|
bolChkFlow = true;
|
|
}
|
|
}
|
|
}
|
|
|
|
if (!bolChkFlow)
|
|
throw new Exception("序號:" + serialNumber + " 流程狀態不允許!");
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
return true;
|
|
}
|
|
|
|
//[Route("[action]")]
|
|
//[HttpGet]
|
|
//public async Task<ResultModel<dynamic>> CheckShipQtyAsyn(string recordNumber, string lineNo, int addQty)
|
|
//{
|
|
// ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
// try
|
|
// {
|
|
// //安勤連線
|
|
// PTDContext _ptd_context = new PTDContext();
|
|
// DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
// if (connPTD.State != ConnectionState.Open)
|
|
// {
|
|
// await connPTD.OpenAsync();
|
|
// }
|
|
// //查剩餘數
|
|
// string strSQL = $@"select ShipQty -
|
|
// (SELECT count(1)
|
|
// FROM [SFIS_PTD].[dbo].[ZWHPickListDetail]
|
|
// where RecordNumber =DNNO and RCLineNO =DNLineNO and TaskStatus ='Picked') as surplusQty
|
|
// from [SFIS_PTD].[dbo].ZDNDetail where DNNO ='{recordNumber}' and DNLineNO ='{lineNo}'";
|
|
// using (var cmd = connPTD.CreateCommand())
|
|
// {
|
|
// cmd.CommandText = strSQL;
|
|
|
|
// using (var reader = await cmd.ExecuteReaderAsync())
|
|
// {
|
|
// if (reader.Read())
|
|
// {
|
|
// if (addQty > Convert.ToInt32(reader["surplusQty"]))
|
|
// throw new Exception("數量已超過,不允許新增!");
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// result.Success = true;
|
|
// result.Msg = "OK";
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// result.Success = false;
|
|
// result.Msg = ex.Message;
|
|
// }
|
|
// return result;
|
|
//}
|
|
|
|
//[Route("[action]")]
|
|
//[HttpGet]
|
|
//public async Task<ResultModel<dynamic>> CheckFlowRulesAsyn(string recordType, string serialNumber)
|
|
//{
|
|
// ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
// try
|
|
// {
|
|
// //安勤連線
|
|
// PTDContext _ptd_context = new PTDContext();
|
|
// DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
// if (connPTD.State != ConnectionState.Open)
|
|
// {
|
|
// await connPTD.OpenAsync();
|
|
// }
|
|
// //用ZProductTrans最新RecordType 檢查PTDFlowRules邏輯
|
|
// bool bolChkFlow = false;
|
|
// string strSQL = $@"Select [FormID] FROM [SFIS_PTD].[dbo].[PTDFlowRules]
|
|
// where [ToID] ='{recordType}'";
|
|
// DataTable dtPTDFlowRules = new DataTable();
|
|
// using (var cmd = connPTD.CreateCommand())
|
|
// {
|
|
// cmd.CommandText = strSQL;
|
|
|
|
// using (var reader = await cmd.ExecuteReaderAsync())
|
|
// {
|
|
// if (reader.HasRows)
|
|
// {
|
|
// dtPTDFlowRules = DataReaderToDataTable(reader);
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// strSQL = @$"Select [RecordTypeID] FROM [SFIS_PTD].[dbo].[ZProductTrans]
|
|
// where [SerialNumber] ='{serialNumber}'
|
|
// Order by [RecordDate] Desc";
|
|
// using (var cmd = connPTD.CreateCommand())
|
|
// {
|
|
// cmd.CommandText = strSQL;
|
|
// using (var reader = await cmd.ExecuteReaderAsync())
|
|
// {
|
|
// if (reader.Read())
|
|
// {
|
|
// foreach (DataRow dr in dtPTDFlowRules.Rows)
|
|
// {
|
|
// if (reader["RecordTypeID"].ToString() == dr["FormID"].ToString())
|
|
// {
|
|
// bolChkFlow = true;
|
|
// break;
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
// }
|
|
|
|
// if (!bolChkFlow)
|
|
// throw new Exception("流程狀態不允許!");
|
|
|
|
// result.Success = true;
|
|
// result.Msg = "OK";
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// result.Success = false;
|
|
// result.Msg = ex.Message;
|
|
// }
|
|
// return result;
|
|
//}
|
|
#endregion
|
|
|
|
#region "PTD003 作業查詢"
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<List<RecordTypeInfo>> GetRecordTypes()
|
|
{
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
try
|
|
{
|
|
string strSQL = $@"select a.RecordTypeID,(select distinct TypeName from SFIS_PTD.dbo.RecordTypeInfo where ID = a.RecordTypeID ) as TypeName
|
|
from SFIS_PTD.dbo.ZProductTrans a group by a.RecordTypeID";
|
|
DataTable dtRecordTypes = new DataTable();
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
dtRecordTypes = DataReaderToDataTable(reader);
|
|
}
|
|
}
|
|
}
|
|
|
|
List<RecordTypeInfo> list = new List<RecordTypeInfo>();
|
|
foreach (DataRow row in dtRecordTypes.Rows)
|
|
{
|
|
list.Add(new RecordTypeInfo
|
|
{
|
|
ID = Convert.ToString(row["RecordTypeID"]),
|
|
TypeName = Convert.ToString(row["TypeName"])
|
|
});
|
|
}
|
|
|
|
return list;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw e;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetData4PTD003_Old(string recordType, string recordNumber, string lineNo, string materialNo, string shippingSN, string dateStart, string dateEnd)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
|
|
try
|
|
{
|
|
string strSQL = $@"SELECT a.RecordTypeID ,a.RecordNumber ,a.RCLineNO 'LineNo',
|
|
a.SerialNumber 'SN',a.ProductID ,a.EmplID 'Customer',a.LocationID 'Location',
|
|
a.EmplID as empID , a.CreateDate , a.ExtNotes , CONVERT(datetime,
|
|
SWITCHOFFSET(CONVERT(datetimeoffset,
|
|
a.RecordDate),
|
|
DATENAME(TzOffset, SYSDATETIMEOFFSET()))) as RecordDate
|
|
FROM SFIS_PTD.dbo.ZProductTrans a where 1=1 ";
|
|
if (recordType != null && recordType != "" && recordType != "全部")
|
|
{
|
|
strSQL += $" And a.RecordTypeID ='{recordType}'";
|
|
}
|
|
|
|
if (recordNumber != null && recordNumber != "")
|
|
{
|
|
strSQL += $" And a.RecordNumber ='{recordNumber}'";
|
|
}
|
|
|
|
if (lineNo != null && lineNo != "")
|
|
{
|
|
strSQL += $" And a.RCLineNO ='{lineNo}'";
|
|
}
|
|
|
|
if (materialNo != null && materialNo != "")
|
|
{
|
|
strSQL += $" And a.ProductID like '{materialNo}%'";
|
|
}
|
|
|
|
if (shippingSN != null && shippingSN != "")
|
|
{
|
|
strSQL += $" And a.SerialNumber like '{shippingSN}%'";
|
|
}
|
|
|
|
if (dateStart != null && dateEnd != null)
|
|
{
|
|
if (DateTime.Parse(dateStart) > DateTime.Parse(dateEnd))
|
|
{
|
|
result.Msg = "起 不可大於 迄!";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
strSQL += $" And a.RecordDate Between '{dateStart.Replace("/", "-") + " 00:00:00"}' And '{dateEnd.Replace("/", "-") + " 23:59:59"}'";
|
|
}
|
|
|
|
//排序:ZProductTrans.CreateDate DESC
|
|
strSQL += " Order by a.RecordDate DESC";
|
|
|
|
DataTable dtZProductTrans = new DataTable();
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
dtZProductTrans = DataReaderToDataTable(reader);
|
|
}
|
|
}
|
|
}
|
|
|
|
List<dynamic> list = new List<dynamic>();
|
|
foreach (DataRow row in dtZProductTrans.Rows)
|
|
{
|
|
dynamic dyn = new ExpandoObject();
|
|
list.Add(dyn);
|
|
foreach (DataColumn column in dtZProductTrans.Columns)
|
|
{
|
|
var dic = (IDictionary<string, object>)dyn;
|
|
dic[column.ColumnName] = row[column];
|
|
}
|
|
}
|
|
|
|
result.DataTotal = list.Count();
|
|
result.Data = list;
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw e;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetData4PTD003(string recordType, string recordNumber, string lineNo, string materialNo, string shippingSN,
|
|
string dateStart, string dateEnd, string RBU, string soNumber, string soLineNo, string customer)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
|
|
try
|
|
{
|
|
string strSQL = $@"SELECT a.RecordTypeID ,a.RecordNumber ,a.RCLineNO 'LineNo',
|
|
a.SerialNumber 'SN',a.ProductID ,a.LocationID 'Location',
|
|
a.EmplID as empID , a.CreateDate , a.ExtNotes ,a.RecordDate ,
|
|
a.SalesOrderNumber AS soNumber , a.SOLineNO AS soLineNo ,a.ERP_CustomerID 'Customer'
|
|
FROM SFIS_PTD.dbo.ZProductTrans a where 1=1 ";
|
|
|
|
if (recordType != null && recordType != "" && recordType != "全部")
|
|
{
|
|
strSQL += $" And a.RecordTypeID ='{recordType}'";
|
|
}
|
|
|
|
if (recordNumber != null && recordNumber != "")
|
|
{
|
|
strSQL += $" And a.RecordNumber ='{recordNumber}'";
|
|
}
|
|
|
|
if (lineNo != null && lineNo != "")
|
|
{
|
|
strSQL += $" And a.RCLineNO ='{lineNo}'";
|
|
}
|
|
|
|
if (materialNo != null && materialNo != "")
|
|
{
|
|
strSQL += $" And a.ProductID like '{materialNo}%'";
|
|
}
|
|
|
|
if (shippingSN != null && shippingSN != "")
|
|
{
|
|
strSQL += $" And a.SerialNumber like '{shippingSN}%'";
|
|
}
|
|
|
|
if (dateStart != null && dateEnd != null)
|
|
{
|
|
if (DateTime.Parse(dateStart) > DateTime.Parse(dateEnd))
|
|
{
|
|
result.Msg = "起 不可大於 迄!";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
strSQL += $" And a.RecordDate Between '{dateStart.Replace("/", "-") + " 00:00:00"}' And '{dateEnd.Replace("/", "-") + " 23:59:59"}'";
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(soNumber))
|
|
{
|
|
strSQL += $" And a.SalesOrderNumber like '{soNumber}%'";
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(soLineNo))
|
|
{
|
|
strSQL += $" And a.SOLineNO like '{soLineNo}%'";
|
|
}
|
|
|
|
if (!string.IsNullOrWhiteSpace(customer))
|
|
{
|
|
strSQL += $" And a.ERP_CustomerID like '{customer}%'";
|
|
}
|
|
|
|
//排序:ZProductTrans.CreateDate DESC
|
|
strSQL += " ORDER BY a.HistoryID ,a.RecordNumber ,a.RCLineNO";
|
|
|
|
DataTable dtZProductTrans = new DataTable();
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
dtZProductTrans = DataReaderToDataTable(reader);
|
|
}
|
|
}
|
|
}
|
|
|
|
List<dynamic> list = new List<dynamic>();
|
|
foreach (DataRow row in dtZProductTrans.Rows)
|
|
{
|
|
dynamic dyn = new ExpandoObject();
|
|
list.Add(dyn);
|
|
foreach (DataColumn column in dtZProductTrans.Columns)
|
|
{
|
|
var dic = (IDictionary<string, object>)dyn;
|
|
dic[column.ColumnName] = row[column];
|
|
}
|
|
}
|
|
|
|
// EV01 RecordDate 要改成GMT時區(台灣+8小時)
|
|
if (RBU == "EV01")
|
|
list.ToList().ForEach(s => s.RecordDate = DateTime.Parse(s.RecordDate).AddHours(8).ToString("yyyy-MM-dd HH:mm:ss"));
|
|
|
|
result.DataTotal = list.Count();
|
|
result.Data = list;
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw e;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
[HttpGet("GetCustomer/{recordNumber}")]
|
|
public async Task<string> GetCustomer(string recordNumber)
|
|
{
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
try
|
|
{
|
|
string strSQL = $@"Select top 1 SoldCustomerID 'Customer' from SFIS_PTD.dbo.ZDNDetail where DNNo ='{recordNumber}'";
|
|
string strCustomer = "";
|
|
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
|
|
strCustomer = reader.GetString(0);
|
|
}
|
|
}
|
|
}
|
|
|
|
return strCustomer;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw e;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
}
|
|
#endregion
|
|
|
|
#region "PTD004 單頭說明維護"
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<RecordTypeInfo>>> GetRecordTypeInfo()
|
|
{
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
try
|
|
{
|
|
string strSQL = $@"select * from SFIS_PTD.dbo.RecordTypeInfo";
|
|
|
|
DataTable dtRecordTypeInfo = new DataTable();
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
dtRecordTypeInfo = DataReaderToDataTable(reader);
|
|
}
|
|
}
|
|
}
|
|
List<RecordTypeInfo> RecordTypeInfo = new List<RecordTypeInfo>();
|
|
foreach (DataRow row in dtRecordTypeInfo.Rows)
|
|
{
|
|
RecordTypeInfo.Add(new RecordTypeInfo
|
|
{
|
|
RBU = Convert.ToString(row["RBU"]),
|
|
ID = Convert.ToString(row["ID"]),
|
|
TypeName = Convert.ToString(row["TypeName"]),
|
|
TypeDesc = Convert.ToString(row["TypeDesc"]),
|
|
Source = Convert.ToString(row["Source"]),
|
|
PrefixCode = Convert.ToString(row["PrefixCode"]),
|
|
Length = row["Length"] == DBNull.Value ? (int?)null : Convert.ToInt32(row["Length"]),
|
|
Status = Convert.ToString(row["Status"]),
|
|
SEQ = Convert.ToString(row["SEQ"])
|
|
});
|
|
}
|
|
return RecordTypeInfo;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw e;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
[HttpGet("GetRecordTypeInfo/{id}")]
|
|
public async Task<ActionResult<IEnumerable<RecordTypeInfo>>> GetRecordTypeInfo(int id)
|
|
{
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
try
|
|
{
|
|
string strSQL = $@"select * from SFIS_PTD.dbo.RecordTypeInfo Where RecordTypeID ='{id}'";
|
|
|
|
DataTable dtRecordTypeInfo = new DataTable();
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
dtRecordTypeInfo = DataReaderToDataTable(reader);
|
|
}
|
|
}
|
|
}
|
|
List<RecordTypeInfo> RecordTypeInfo = new List<RecordTypeInfo>();
|
|
foreach (DataRow row in dtRecordTypeInfo.Rows)
|
|
{
|
|
RecordTypeInfo.Add(new RecordTypeInfo
|
|
{
|
|
RBU = Convert.ToString(row["RBU"]),
|
|
ID = Convert.ToString(row["ID"]),
|
|
TypeName = Convert.ToString(row["TypeName"]),
|
|
TypeDesc = Convert.ToString(row["TypeDesc"]),
|
|
Source = Convert.ToString(row["Source"]),
|
|
PrefixCode = Convert.ToString(row["PrefixCode"]),
|
|
Length = row["Length"] == DBNull.Value ? (int?)null : Convert.ToInt32(row["Length"]),
|
|
Status = Convert.ToString(row["Status"]),
|
|
SEQ = Convert.ToString(row["SEQ"])
|
|
});
|
|
}
|
|
return RecordTypeInfo;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw e;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
}
|
|
|
|
[Route("[action]")]
|
|
[HttpPost]
|
|
public async Task<ResultModel<RecordTypeInfo>> PostRecordTypeInfo(RecordTypeInfo RecordTypeInfo)
|
|
{
|
|
ResultModel<RecordTypeInfo> result = new ResultModel<RecordTypeInfo>();
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
try
|
|
{
|
|
string strSQL = $@"INSERT INTO SFIS_PTD.dbo.RecordTypeInfo (ID, RBU, TypeName, TypeDesc, Source, PrefixCode, Length,Status)
|
|
VALUES ('{RecordTypeInfo.ID}','{RecordTypeInfo.RBU}', '{RecordTypeInfo.TypeName}', '{RecordTypeInfo.TypeDesc}',
|
|
'{RecordTypeInfo.Source}', '{RecordTypeInfo.PrefixCode}', {RecordTypeInfo.Length}
|
|
,'{RecordTypeInfo.Status}' )";
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.InnerException.Message;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
return result;
|
|
|
|
}
|
|
|
|
[HttpPut("PutRecordTypeInfo/{id}")]
|
|
public async Task<ResultModel<RecordTypeInfo>> PutRecordTypeInfo(string id, [FromBody] RecordTypeInfo RecordTypeInfo)
|
|
{
|
|
ResultModel<RecordTypeInfo> result = new ResultModel<RecordTypeInfo>();
|
|
|
|
if (id != RecordTypeInfo.ID)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = "ID錯誤";
|
|
return result;
|
|
}
|
|
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
|
|
try
|
|
{
|
|
string strSQL = $@"UPDATE SFIS_PTD.dbo.RecordTypeInfo
|
|
SET RBU = '{RecordTypeInfo.RBU}',
|
|
ID = '{RecordTypeInfo.ID}',
|
|
TypeName = '{RecordTypeInfo.TypeName}',
|
|
TypeDesc = '{RecordTypeInfo.TypeDesc}',
|
|
Source = '{RecordTypeInfo.Source}',
|
|
PrefixCode = '{RecordTypeInfo.PrefixCode}',
|
|
Length = {RecordTypeInfo.Length},
|
|
Status = '{RecordTypeInfo.Status}'
|
|
WHERE SEQ = '{RecordTypeInfo.SEQ}'";
|
|
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.InnerException.Message;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
return result;
|
|
|
|
}
|
|
|
|
[HttpDelete("DeleteRecordTypeInfo/{id}")]
|
|
public async Task<ResultModel<RecordTypeInfo>> DeleteRecordTypeInfo(int id)
|
|
{
|
|
ResultModel<RecordTypeInfo> result = new ResultModel<RecordTypeInfo>();
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
try
|
|
{
|
|
string strSQL = $@"DELETE FROM SFIS_PTD.dbo.RecordTypeInfo
|
|
WHERE SEQ = '{id}'";
|
|
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.InnerException.Message;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region "PTD005 設定 ERP 客戶代號和客戶收件人名單"
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ActionResult<IEnumerable<CustomerItemMailGroupModel>>> GetCustomerItemMailGroup()
|
|
{
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
try
|
|
{
|
|
string strSQL = $@"select * from SFIS_PTD.dbo.CustomerItemMailGroup";
|
|
DataTable dtCustomerItemMailGroup = new DataTable();
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
dtCustomerItemMailGroup = DataReaderToDataTable(reader);
|
|
}
|
|
}
|
|
}
|
|
List<CustomerItemMailGroupModel> CustomerItemMailGroup = new List<CustomerItemMailGroupModel>();
|
|
foreach (DataRow row in dtCustomerItemMailGroup.Rows)
|
|
{
|
|
CustomerItemMailGroup.Add(new CustomerItemMailGroupModel
|
|
{
|
|
ItemNumber = Convert.ToString(row["ItemNumber"]),
|
|
CustomerCode = Convert.ToString(row["CustomerCode"]),
|
|
MailGroup = Convert.ToString(row["MailGroup"]),
|
|
StatusNo = Convert.ToString(row["StatusNo"])
|
|
});
|
|
}
|
|
return CustomerItemMailGroup;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw e;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
}
|
|
[HttpGet("GetCustomerItemMailGroup/{id}")]
|
|
public async Task<ActionResult<IEnumerable<CustomerItemMailGroupModel>>> GetCustomerItemMailGroup(string id)
|
|
{
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
try
|
|
{
|
|
|
|
string strSQL = $@"select * from SFIS_PTD.dbo.CustomerItemMailGroup Where 1=1";
|
|
if (id != null && id != "")
|
|
{
|
|
strSQL += $@" And CustomerCode ='{id}'";
|
|
}
|
|
DataTable dtCustomerItemMailGroup = new DataTable();
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
dtCustomerItemMailGroup = DataReaderToDataTable(reader);
|
|
}
|
|
}
|
|
}
|
|
List<CustomerItemMailGroupModel> CustomerItemMailGroup = new List<CustomerItemMailGroupModel>();
|
|
foreach (DataRow row in dtCustomerItemMailGroup.Rows)
|
|
{
|
|
CustomerItemMailGroup.Add(new CustomerItemMailGroupModel
|
|
{
|
|
ItemNumber = Convert.ToString(row["ItemNumber"]),
|
|
CustomerCode = Convert.ToString(row["CustomerCode"]),
|
|
MailGroup = Convert.ToString(row["MailGroup"]),
|
|
StatusNo = Convert.ToString(row["StatusNo"])
|
|
});
|
|
}
|
|
return CustomerItemMailGroup;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw e;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
}
|
|
[Route("[action]")]
|
|
[HttpPost]
|
|
public async Task<ResultModel<CustomerItemMailGroupModel>> PostCustomerItemMailGroup(CustomerItemMailGroupModel CustomerItemMailGroup)
|
|
{
|
|
ResultModel<CustomerItemMailGroupModel> result = new ResultModel<CustomerItemMailGroupModel>();
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
try
|
|
{
|
|
string strSQL = $@"INSERT INTO SFIS_PTD.dbo.[CustomerItemMailGroup] ([ItemNumber],[CustomerCode],[MailGroup],[StatusNo])
|
|
VALUES ('{CustomerItemMailGroup.ItemNumber}','{CustomerItemMailGroup.CustomerCode}',
|
|
'{CustomerItemMailGroup.MailGroup}', '{CustomerItemMailGroup.StatusNo}')";
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.InnerException.Message;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
return result;
|
|
|
|
}
|
|
|
|
[HttpPut("PutCustomerItemMailGroup/{id}")]
|
|
public async Task<ResultModel<CustomerItemMailGroupModel>> PutCustomerItemMailGroup(string id, [FromBody] CustomerItemMailGroupModel CustomerItemMailGroup)
|
|
{
|
|
ResultModel<CustomerItemMailGroupModel> result = new ResultModel<CustomerItemMailGroupModel>();
|
|
if (id != CustomerItemMailGroup.CustomerCode)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = "ID錯誤";
|
|
return result;
|
|
}
|
|
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
|
|
try
|
|
{
|
|
string strSQL = $@"UPDATE SFIS_PTD.dbo.CustomerItemMailGroup
|
|
SET ItemNumber = '{CustomerItemMailGroup.ItemNumber}',
|
|
MailGroup = '{CustomerItemMailGroup.MailGroup}'
|
|
WHERE CustomerCode = '{id}'";
|
|
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.InnerException.Message;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
return result;
|
|
|
|
}
|
|
|
|
[HttpDelete("DeleteCustomerItemMailGroup/{id}")]
|
|
public async Task<ResultModel<CustomerItemMailGroupModel>> DeleteCustomerItemMailGroup(string id)
|
|
{
|
|
ResultModel<CustomerItemMailGroupModel> result = new ResultModel<CustomerItemMailGroupModel>();
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
try
|
|
{
|
|
string strSQL = $@"UPDATE SFIS_PTD.dbo.CustomerItemMailGroup
|
|
SET StatusNo =
|
|
CASE
|
|
WHEN StatusNo = 'A' THEN 'S'
|
|
WHEN StatusNo = 'S' THEN 'A'
|
|
ELSE 'A'
|
|
END
|
|
WHERE CustomerCode = '{id}'";
|
|
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.InnerException.Message;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
#endregion
|
|
|
|
#region "PTD006 備貨作業"
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetZWHPickListDetail(string recordNumber, string? LineNO)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
|
|
try
|
|
{
|
|
|
|
string strSQL = $@"select [RecordTypeID] as 'recordType',
|
|
[RecordNumber] as 'dnNo',
|
|
[RCLineNO] as 'lineNo',
|
|
[ProductID] as 'material',
|
|
[InputSN] as 'sn'
|
|
from dbo.[ZWHPickListDetail] where TaskStatus ='Picked' And RecordNumber ='{recordNumber}'";
|
|
if (!string.IsNullOrEmpty(LineNO))
|
|
{
|
|
strSQL += $@" And RCLineNO ='{LineNO}'";
|
|
}
|
|
DataTable dtZDNDetail = new DataTable();
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
dtZDNDetail = DataReaderToDataTable(reader);
|
|
}
|
|
}
|
|
}
|
|
|
|
List<dynamic> list = new List<dynamic>();
|
|
foreach (DataRow row in dtZDNDetail.Rows)
|
|
{
|
|
dynamic dyn = new ExpandoObject();
|
|
list.Add(dyn);
|
|
foreach (DataColumn column in dtZDNDetail.Columns)
|
|
{
|
|
var dic = (IDictionary<string, object>)dyn;
|
|
dic[column.ColumnName] = row[column];
|
|
}
|
|
}
|
|
|
|
result.DataTotal = list.Count();
|
|
result.Data = list;
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.InnerException.Message;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
|
|
[Route("[action]")]
|
|
[HttpPost]
|
|
public async Task<ResultModel<dynamic>> PostPTD006Commit(PTD006CommitDataModel Data)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
|
|
try
|
|
{
|
|
string strSQL = "";
|
|
string strCreateDate = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
CheckShipQty(Data.RecordNumber, Data.RCLineNO, 1);
|
|
|
|
//ZWHPickListDetail
|
|
strSQL = @$"INSERT INTO [dbo].[ZWHPickListDetail]([RecordTypeID],[RecordNumber],[RCLineNO],[InputSN],[SNFunction]
|
|
,[ShipmentSN],[ProductID],[TaskStatus],[EmplID],[ExtNotes],[OwnerPlantID],[OwnerCompanyID]
|
|
,[CreateDeptID],[CreatorID],[CreateDate],[ModifyDeptID],[ModifierID],[ModifyDate],[chkflag])
|
|
VALUES ('{Data.RecordTypeID}','{Data.RecordNumber}','{Data.RCLineNO}','{Data.InputSN}','ShipmentSN',
|
|
'{Data.InputSN}','{Data.ProductID}','Picked','{Data.EmplID}',N'{Data.ExtNotes}','{Data.OwnerPlantID}','{Data.OwnerCompanyID}',
|
|
'{Data.CreateDeptID}','{Data.CreatorID}','{strCreateDate}','{Data.ModifyDeptID}','{Data.ModifierID}','{strCreateDate}','0')";
|
|
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.Message;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
return result;
|
|
|
|
}
|
|
|
|
[Route("[action]")]
|
|
[HttpPost]
|
|
public async Task<ResultModel<dynamic>> PostPTD006CommitOld(PTD006CommitDataModel Data)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
List<string> SNList = new List<string>();
|
|
Collection<string> colSQL = new Collection<string>();
|
|
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
|
|
try
|
|
{
|
|
string strSQL = "";
|
|
string strCreateDate = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
CheckShipQty(Data.RecordNumber, Data.RCLineNO, 1);
|
|
|
|
//用ZProductTrans最新RecordType 檢查PTDFlowRules邏輯
|
|
bool bolChkFlow = false;
|
|
strSQL = $@"Select [FormID] FROM [SFIS_PTD].[dbo].[PTDFlowRules]
|
|
where [ToID] ='{Data.RecordTypeID}'";
|
|
DataTable dtPTDFlowRules = new DataTable();
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
dtPTDFlowRules = DataReaderToDataTable(reader);
|
|
}
|
|
}
|
|
}
|
|
|
|
strSQL = @$"Select [RecordTypeID] FROM [SFIS_PTD].[dbo].[ZProductTrans]
|
|
where [SerialNumber] ='{Data.ShipmentSN}'
|
|
Order by [RecordDate] Desc";
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
foreach (DataRow dr in dtPTDFlowRules.Rows)
|
|
{
|
|
if (reader["RecordTypeID"].ToString() == dr["FormID"].ToString())
|
|
{
|
|
bolChkFlow = true;
|
|
break;
|
|
}
|
|
}
|
|
}
|
|
else if (Data.RecordTypeID == "101" || Data.RecordTypeID == "292") //101、292可能沒有前身資訊
|
|
{
|
|
bolChkFlow = true;
|
|
}
|
|
}
|
|
}
|
|
if (!bolChkFlow)
|
|
throw new Exception("流程狀態不允許!");
|
|
|
|
//ZWHPickListDetail
|
|
strSQL = @$"INSERT INTO [dbo].[ZWHPickListDetail]([RecordTypeID],[RecordNumber],[RCLineNO],[InputSN],[SNFunction]
|
|
,[ShipmentSN],[ProductID],[TaskStatus],[EmplID],[ExtNotes],[OwnerPlantID],[OwnerCompanyID]
|
|
,[CreateDeptID],[CreatorID],[CreateDate],[ModifyDeptID],[ModifierID],[ModifyDate],[chkflag])
|
|
VALUES ('{Data.RecordTypeID}','{Data.RecordNumber}','{Data.RCLineNO}','{Data.InputSN}','ShipmentSN',
|
|
'{Data.InputSN}','{Data.ProductID}','Picked','{Data.EmplID}',N'{Data.ExtNotes}','{Data.OwnerPlantID}','{Data.OwnerCompanyID}',
|
|
'{Data.CreateDeptID}','{Data.CreatorID}','{strCreateDate}','{Data.ModifyDeptID}','{Data.ModifierID}','{strCreateDate}','0')";
|
|
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.Message;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
[HttpDelete("DeleteZWHPickListDetail/{sn}")]
|
|
public async Task<ResultModel<RecordTypeInfo>> DeleteZWHPickListDetail(string sn)
|
|
{
|
|
ResultModel<RecordTypeInfo> result = new ResultModel<RecordTypeInfo>();
|
|
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
try
|
|
{
|
|
string strSQL = $@"DELETE FROM SFIS_PTD.dbo.[ZWHPickListDetail]
|
|
WHERE [TaskStatus] = 'Picked' And [ShipmentSN] ='{sn}'";
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.InnerException.Message;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
[HttpDelete("DeleteZWHPickListDetailAll/{recordNo}/{lineNo}")]
|
|
public async Task<ResultModel<RecordTypeInfo>> DeleteZWHPickListDetailAll(string recordNo, string lineNo)
|
|
{
|
|
ResultModel<RecordTypeInfo> result = new ResultModel<RecordTypeInfo>();
|
|
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
try
|
|
{
|
|
string strSQL = $@"DELETE FROM SFIS_PTD.dbo.[ZWHPickListDetail]
|
|
WHERE [TaskStatus] = 'Picked' And [RecordNumber] ='{recordNo}' AND [RCLineNO] = '{lineNo}'";
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.InnerException.Message;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
#endregion
|
|
|
|
#region "PTD007 扣帳作業"
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetZDNDetailJoinZWHPickListDetail(string recordType, string recordNumber)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
|
|
try
|
|
{
|
|
string strSQL = $@"SELECT b.RecordNumber as DNNO
|
|
,b.RCLineNO as DNLineNO
|
|
,b.ProductID
|
|
,b.EmplID as SoldCustomerID
|
|
,a.ExpectShipDate
|
|
,a.ShipQty
|
|
,b.Qty
|
|
FROM [dbo].ZDNDetail a right join (select sum(1) as Qty,RecordNumber,RCLineNO,RecordTypeID,ProductID,EmplID
|
|
from dbo.[ZWHPickListDetail] where TaskStatus ='Picked' group by RecordTypeID,RecordNumber,RCLineNO,ProductID,EmplID)b
|
|
on (b.RecordNumber = a.DNNO and b.RCLineNO = a.DNLineNO)
|
|
where b.RecordNumber = '{recordNumber}' and b.RecordTypeID = '{recordType}'";
|
|
DataTable dtZDNDetail = new DataTable();
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
dtZDNDetail = DataReaderToDataTable(reader);
|
|
}
|
|
}
|
|
}
|
|
|
|
List<dynamic> list = new List<dynamic>();
|
|
foreach (DataRow row in dtZDNDetail.Rows)
|
|
{
|
|
dynamic dyn = new ExpandoObject();
|
|
list.Add(dyn);
|
|
foreach (DataColumn column in dtZDNDetail.Columns)
|
|
{
|
|
var dic = (IDictionary<string, object>)dyn;
|
|
dic[column.ColumnName] = row[column];
|
|
}
|
|
}
|
|
|
|
result.DataTotal = list.Count();
|
|
result.Data = list;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.InnerException.Message;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
|
|
[Route("[action]")]
|
|
[HttpPost]
|
|
public async Task<ResultModel<dynamic>> PostPTD007Commit(PTD007CommitDataModel Data)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
List<string> SNList = new List<string>();
|
|
Collection<string> colSQL = new Collection<string>();
|
|
List<PTD002CommitDataModel> mailData = new List<PTD002CommitDataModel>();
|
|
|
|
try
|
|
{
|
|
// RecordDate 加入目前時間
|
|
Data.RecordDate += $" {DateTime.Now:HH:mm:ss}";
|
|
|
|
string strSQL = "";
|
|
string strRecordType = Data.RecordType;
|
|
string strRbu = Data.Rbu;
|
|
string strWorkCenter = Data.WorkCenter;
|
|
string strEmpId = Data.loginNo;
|
|
string strRecordDate = TimeZoneInfo.ConvertTimeToUtc(DateTime.Parse(Data.RecordDate)).ToString("yyyy-MM-dd HH:mm:ss");
|
|
string strCreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
using (IDbConnection ptdConnection = new SqlConnection(_PTDContext))
|
|
{
|
|
if (ptdConnection.State != ConnectionState.Open)
|
|
{
|
|
ptdConnection.Open();
|
|
}
|
|
foreach (PTD007TableDataModel SNData in Data.TableData)
|
|
{
|
|
// 601 631 633 狀態時要填入 去對應ZDNDetail
|
|
string salesOrderNumber = "";
|
|
string sOLineNO = "";
|
|
string eRP_CustomerID = SNData.soldCustomerID;
|
|
if (strRecordType == "601" || strRecordType == "631" || strRecordType == "633")
|
|
{
|
|
var dnDetail = await new PTD.PTDController().GetZDNDetail(SNData.dnno, SNData.dnLineNO);
|
|
if (dnDetail.Any())
|
|
{
|
|
salesOrderNumber = dnDetail.FirstOrDefault().OrderNo;
|
|
sOLineNO = dnDetail.FirstOrDefault().OrderLineNO;
|
|
eRP_CustomerID = dnDetail.FirstOrDefault().ShipCustomerID;
|
|
}
|
|
}
|
|
|
|
var mailDataItem = new List<SNDataModel>();
|
|
|
|
//ZWHPickListDetail查SN資訊
|
|
strSQL = $@"Select * FROM [SFIS_PTD].[dbo].[ZWHPickListDetail]
|
|
where RecordNumber='{SNData.dnno}' and RCLineNO='{SNData.dnLineNO}'";
|
|
|
|
var q = await ptdConnection.QueryAsync<dynamic>(strSQL);
|
|
|
|
foreach (var row in q)
|
|
{
|
|
string strLatest;
|
|
//確認此出貨序號是否有ZHistoryKeyDefine
|
|
strSQL = $@"select Seed FROM [SFIS_PTD].[dbo].[ZHistoryKeyDefine] where serialnumber ='{row.ShipmentSN}'";
|
|
|
|
var q1 = await ptdConnection.QueryAsync<dynamic>(strSQL);
|
|
|
|
if (q1.Any())
|
|
{
|
|
int seed = Convert.ToInt32(q1.FirstOrDefault().Seed);
|
|
int nIdx = seed + 1;
|
|
strLatest = row.ShipmentSN + nIdx.ToString().PadLeft(4, '0');//組成HistoryID
|
|
//HistoryKeyDefine
|
|
strSQL = @$"UPDATE [SFIS_PTD].[dbo].[ZHistoryKeyDefine]
|
|
SET [Seed] = {nIdx} where [SerialNumber]='{row.ShipmentSN}'";
|
|
colSQL.Add(strSQL);
|
|
//ZSNInfo
|
|
strSQL = @$"Update [SFIS_PTD].[dbo].[ZSNInfo] SET LatestHistoryID='{strLatest}',RecordTypeID='{strRecordType}',
|
|
CurrentProductID='{row.ProductID}'
|
|
where SerialNumber='{row.ShipmentSN}'";
|
|
colSQL.Add(strSQL);
|
|
}
|
|
else
|
|
{
|
|
strLatest = row.ShipmentSN + 1.ToString().PadLeft(4, '0');//組成HistoryID
|
|
//ZHistoryKeyDefine
|
|
strSQL = @$"INSERT INTO [SFIS_PTD].[dbo].[ZHistoryKeyDefine] ([SerialNumber],[Seed])
|
|
VALUES ('{row.ShipmentSN}',1)";
|
|
colSQL.Add(strSQL);
|
|
//ZSNInfo
|
|
strSQL = @$"INSERT INTO [SFIS_PTD].[dbo].[ZSNInfo] ([SerialNumber],[CurrentProductID],[OriginalProductID],
|
|
[LocationID],[LatestHistoryID],[RecordTypeID],
|
|
[OwnerPlantID],[OwnerCompanyID],[CreatePlantID],[CreateCompanyID],
|
|
[ModifyDate]) VALUES
|
|
('{row.ShipmentSN}','{row.ProductID}','{row.ProductID}',
|
|
'9000','{strLatest}','{strRecordType}',
|
|
'{strWorkCenter}','{strRbu}','{strWorkCenter}','{strRbu}',
|
|
'{strCreateDate}')";
|
|
colSQL.Add(strSQL);
|
|
}
|
|
//ZProductTrans
|
|
strSQL = $@"INSERT INTO [SFIS_PTD].[dbo].[ZProductTrans]
|
|
([HistoryID],[SerialNumber],[RecordTypeID],[RecordNumber],[RCLineNO],[RecordDate],
|
|
[ProductID],[IsChangeID],[LocationID],[EmplID],[IsDelete],
|
|
[OwnerPlantID],[OwnerCompanyID],[CreatePlantID],[CreateCompanyID],
|
|
[CreatorID],[CreateDate],[ModifierID],[ModifyDate],
|
|
[chkflag],[IsBranchCoReceived],[MOID],
|
|
[SalesOrderNumber],[SOLineNO],[ERP_CustomerID]) VALUES
|
|
('{strLatest}','{row.ShipmentSN}','{strRecordType}','{SNData.dnno}','{SNData.dnLineNO}','{strRecordDate}',
|
|
'{SNData.productID}',0,'9000','{strEmpId}',0,
|
|
'{strWorkCenter}','{strRbu}','{strWorkCenter}','{strRbu}',
|
|
'WH00','{strCreateDate}','WH00','{strCreateDate}',0,0,'',
|
|
'{salesOrderNumber}','{sOLineNO}','{eRP_CustomerID}')";
|
|
colSQL.Add(strSQL);
|
|
|
|
mailDataItem.Add(new SNDataModel
|
|
{
|
|
DnNo = SNData.dnno,
|
|
LineNo = SNData.dnLineNO,
|
|
Material = SNData.productID,
|
|
Sn = row.ShipmentSN
|
|
});
|
|
}
|
|
strSQL = $@"Update [SFIS_PTD].[dbo].[ZWHPickListDetail] set TaskStatus ='Recorded'
|
|
where RecordNumber='{SNData.dnno}' and RCLineNO='{SNData.dnLineNO}'";
|
|
colSQL.Add(strSQL);
|
|
|
|
// 紀錄寄信
|
|
mailData.Add(new PTD002CommitDataModel
|
|
{
|
|
RecordType = strRecordType,
|
|
Customer = SNData.soldCustomerID,
|
|
TableData = mailDataItem
|
|
});
|
|
}
|
|
|
|
using (var transaction = ptdConnection.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
foreach (string query in colSQL)
|
|
{
|
|
await ptdConnection.ExecuteAsync(query, transaction: transaction);
|
|
}
|
|
|
|
// Commit the transaction
|
|
transaction.Commit();
|
|
|
|
foreach (var item in mailData)
|
|
{
|
|
await SendMail(strRecordType, item);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Rollback the transaction in case of an exception
|
|
transaction.Rollback();
|
|
throw ex; // Rethrow the exception after rollback
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.Message;
|
|
}
|
|
return result;
|
|
|
|
}
|
|
|
|
[Route("[action]")]
|
|
[HttpPost]
|
|
public async Task<ResultModel<dynamic>> PostPTD007CommitOld(PTD007CommitDataModel Data)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
List<string> SNList = new List<string>();
|
|
Collection<string> colSQL = new Collection<string>();
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
try
|
|
{
|
|
// RecordDate 加入目前時間
|
|
Data.RecordDate += $" {DateTime.Now:HH:mm:ss}";
|
|
|
|
string strSQL = "";
|
|
string strRecordType = Data.RecordType;
|
|
string strRbu = Data.Rbu;
|
|
string strWorkCenter = Data.WorkCenter;
|
|
string strRecordDate = TimeZoneInfo.ConvertTimeToUtc(DateTime.Parse(Data.RecordDate)).ToString("yyyy-MM-dd HH:mm:ss");
|
|
string strCreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
foreach (PTD007TableDataModel SNData in Data.TableData)
|
|
{
|
|
//ZWHPickListDetail查SN資訊
|
|
strSQL = $@"Select * FROM [SFIS_PTD].[dbo].[ZWHPickListDetail]
|
|
where RecordNumber='{SNData.dnno}' and RCLineNO='{SNData.dnLineNO}'";
|
|
DataTable dtSNData = new DataTable();
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
dtSNData = DataReaderToDataTable(reader);
|
|
}
|
|
}
|
|
}
|
|
foreach (DataRow row in dtSNData.Rows)
|
|
{
|
|
string strLatest;
|
|
//確認此出貨序號是否有ZHistoryKeyDefine
|
|
strSQL = $@"select Seed FROM [SFIS_PTD].[dbo].[ZHistoryKeyDefine] where serialnumber ='{row["ShipmentSN"]}'";
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
int seed = Convert.ToInt32(reader["Seed"]);
|
|
int nIdx = seed + 1;
|
|
strLatest = row["ShipmentSN"].ToString() + nIdx.ToString().PadLeft(4, '0');//組成HistoryID
|
|
//HistoryKeyDefine
|
|
strSQL = @$"UPDATE [SFIS_PTD].[dbo].[ZHistoryKeyDefine]
|
|
SET [Seed] = {nIdx} where [SerialNumber]='{row["ShipmentSN"]}'";
|
|
colSQL.Add(strSQL);
|
|
//ZSNInfo
|
|
strSQL = @$"Update [SFIS_PTD].[dbo].[ZSNInfo] SET LatestHistoryID='{strLatest}',RecordTypeID='{strRecordType}',
|
|
CurrentProductID='{row["ProductID"]}'
|
|
where SerialNumber='{row["ShipmentSN"]}'";
|
|
colSQL.Add(strSQL);
|
|
}
|
|
else
|
|
{
|
|
strLatest = row["ShipmentSN"].ToString() + 1.ToString().PadLeft(4, '0');//組成HistoryID
|
|
//ZHistoryKeyDefine
|
|
strSQL = @$"INSERT INTO [SFIS_PTD].[dbo].[ZHistoryKeyDefine] ([SerialNumber],[Seed])
|
|
VALUES ('{row["ShipmentSN"]}',1)";
|
|
colSQL.Add(strSQL);
|
|
//ZSNInfo
|
|
strSQL = @$"INSERT INTO [SFIS_PTD].[dbo].[ZSNInfo] ([SerialNumber],[CurrentProductID],[OriginalProductID],
|
|
[LocationID],[LatestHistoryID],[RecordTypeID],
|
|
[OwnerPlantID],[OwnerCompanyID],[CreatePlantID],[CreateCompanyID],
|
|
[ModifyDate]) VALUES
|
|
('{row["ShipmentSN"]}','{row["ProductID"]}','{row["ProductID"]}',
|
|
'9000','{strLatest}','{strRecordType}',
|
|
'{strWorkCenter}','{strRbu}','{strWorkCenter}','{strRbu}',
|
|
'{strCreateDate}')";
|
|
colSQL.Add(strSQL);
|
|
}
|
|
//ZProductTrans
|
|
strSQL = $@"INSERT INTO [SFIS_PTD].[dbo].[ZProductTrans]
|
|
([HistoryID],[SerialNumber],[RecordTypeID],[RecordNumber],[RCLineNO],[RecordDate],
|
|
[ProductID],[IsChangeID],[LocationID],[EmplID],[IsDelete],
|
|
[OwnerPlantID],[OwnerCompanyID],[CreatePlantID],[CreateCompanyID],
|
|
[CreatorID],[CreateDate],[ModifierID],[ModifyDate],
|
|
[chkflag],[IsBranchCoReceived],[MOID]) VALUES
|
|
('{strLatest}','{row["ShipmentSN"]}','{strRecordType}','{SNData.dnno}','{SNData.dnLineNO}','{strRecordDate}',
|
|
'{SNData.productID}',0,'9000','{SNData.soldCustomerID.ToString()}',0,
|
|
'{strWorkCenter}','{strRbu}','{strWorkCenter}','{strRbu}',
|
|
'WH00','{strCreateDate}','WH00','{strCreateDate}',0,0,'')";
|
|
colSQL.Add(strSQL);
|
|
}
|
|
}
|
|
}
|
|
strSQL = $@"Update [SFIS_PTD].[dbo].[ZWHPickListDetail] set TaskStatus ='Recorded'
|
|
where RecordNumber='{SNData.dnno}' and RCLineNO='{SNData.dnLineNO}'";
|
|
colSQL.Add(strSQL);
|
|
}
|
|
|
|
foreach (string query in colSQL)
|
|
{
|
|
Console.WriteLine(query);
|
|
}
|
|
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
using (var transaction = connPTD.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
foreach (string query in colSQL)
|
|
{
|
|
cmd.CommandText = query;
|
|
cmd.Transaction = transaction;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
// Commit the transaction
|
|
transaction.Commit();
|
|
await SendMail(strRecordType, Data);
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Rollback the transaction in case of an exception
|
|
transaction.Rollback();
|
|
throw ex; // Rethrow the exception after rollback
|
|
}
|
|
}
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.Message;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
return result;
|
|
|
|
}
|
|
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetPTD007QueryByRBU(string recordNumber, string RBU)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
try
|
|
{
|
|
using (IDbConnection ptdConnection = new SqlConnection(_PTDContext))
|
|
{
|
|
if (ptdConnection.State != ConnectionState.Open)
|
|
{
|
|
ptdConnection.Open();
|
|
}
|
|
|
|
// 判斷ERP_CustomerID
|
|
var erpCustomerId = new List<string>();
|
|
if (RBU == "AUS")
|
|
erpCustomerId.AddRange(new[] { "81US0023" });
|
|
else if (RBU == "ASH")
|
|
erpCustomerId.AddRange(new[] { "35CH0001" });
|
|
else
|
|
erpCustomerId.AddRange(new[] { "35CH0692", "30TW0001" });
|
|
|
|
var query = @$" SELECT A.* , B.ExpectShipDate , B.ShipQty FROM
|
|
(SELECT RecordNumber AS DNNO ,
|
|
RCLineNO AS DNLineNO ,
|
|
ProductID ,
|
|
EmplID AS SoldCustomerID ,
|
|
count(*) as Qty ,
|
|
OwnerPlantID
|
|
FROM SFIS_PTD.dbo.ZProductTrans
|
|
WHERE ERP_CustomerID IN ({string.Join(", ", erpCustomerId.ConvertAll(id => $"N'{id}'"))})
|
|
AND RecordNumber = @RecordNumber
|
|
AND IsBranchCoReceived = 0
|
|
AND RecordTypeID in (N'601',N'633',N'601DB')
|
|
GROUP BY RecordNumber, RCLineNO, ProductID, OwnerPlantID ,EmplID
|
|
) A
|
|
LEFT JOIN SFIS_PTD.dbo.ZDNDetail B on A.DNNO = B.DNNO AND A.DNLineNO = B.DNLineNO
|
|
ORDER BY A.DNNO, A.DNLineNO ";
|
|
|
|
DynamicParameters p = new DynamicParameters();
|
|
p.Add("RecordNumber", recordNumber, DbType.String);
|
|
var q = await ptdConnection.QueryAsync<dynamic>(query, p);
|
|
result.DataTotal = q.Count();
|
|
result.Data = q;
|
|
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.InnerException.Message;
|
|
}
|
|
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
|
|
[Route("[action]")]
|
|
[HttpPost]
|
|
public async Task<ResultModel<dynamic>> PostPTD007CommitByRBU(PTD007CommitDataModel Data)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
List<string> SNList = new List<string>();
|
|
Collection<string> colSQL = new Collection<string>();
|
|
List<PTD002CommitDataModel> mailData = new List<PTD002CommitDataModel>();
|
|
|
|
try
|
|
{
|
|
// RecordDate 加入目前時間
|
|
Data.RecordDate += $" {DateTime.Now:HH:mm:ss}";
|
|
|
|
string strSQL = "";
|
|
string strRecordType = Data.RecordType;
|
|
string strRbu = Data.Rbu;
|
|
string strWorkCenter = Data.WorkCenter;
|
|
string strRecordDate = TimeZoneInfo.ConvertTimeToUtc(DateTime.Parse(Data.RecordDate)).ToString("yyyy-MM-dd HH:mm:ss");
|
|
string strCreateDate = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
// 判斷ERP_CustomerID
|
|
var erpCustomerId = new List<string>();
|
|
if (strRbu == "AUS")
|
|
erpCustomerId.AddRange(new[] { "81US0023" });
|
|
else if (strRbu == "ASH")
|
|
erpCustomerId.AddRange(new[] { "35CH0001" });
|
|
else
|
|
erpCustomerId.AddRange(new[] { "35CH0692", "30TW0001" });
|
|
|
|
using (IDbConnection ptdConnection = new SqlConnection(_PTDContext))
|
|
{
|
|
if (ptdConnection.State != ConnectionState.Open)
|
|
{
|
|
ptdConnection.Open();
|
|
}
|
|
foreach (PTD007TableDataModel SNData in Data.TableData)
|
|
{
|
|
var mailDataItem = new List<SNDataModel>();
|
|
|
|
//ZProductTrans
|
|
strSQL = $@" SELECT * FROM SFIS_PTD.dbo.ZProductTrans
|
|
WHERE ERP_CustomerID IN ({string.Join(", ", erpCustomerId.ConvertAll(id => $"N'{id}'"))})
|
|
AND IsBranchCoReceived = 0
|
|
AND RecordTypeID in (N'601',N'633',N'601DB')
|
|
AND RecordNumber = '{SNData.dnno}'
|
|
AND RCLineNO = '{SNData.dnLineNO}' ";
|
|
|
|
var q = await ptdConnection.QueryAsync<dynamic>(strSQL);
|
|
|
|
foreach (var row in q)
|
|
{
|
|
string strLatest;
|
|
//確認此出貨序號是否有ZHistoryKeyDefine
|
|
strSQL = $@"select Seed FROM [SFIS_PTD].[dbo].[ZHistoryKeyDefine] where serialnumber ='{row.SerialNumber}'";
|
|
|
|
var q1 = await ptdConnection.QueryAsync<dynamic>(strSQL);
|
|
if (q1.Any())
|
|
{
|
|
int seed = Convert.ToInt32(q1.FirstOrDefault().Seed);
|
|
int nIdx = seed + 1;
|
|
strLatest = row.SerialNumber + nIdx.ToString().PadLeft(4, '0');//組成HistoryID
|
|
//HistoryKeyDefine
|
|
strSQL = @$"UPDATE [SFIS_PTD].[dbo].[ZHistoryKeyDefine]
|
|
SET [Seed] = {nIdx} where [SerialNumber]='{row.SerialNumber}'";
|
|
colSQL.Add(strSQL);
|
|
//ZSNInfo
|
|
strSQL = @$"Update [SFIS_PTD].[dbo].[ZSNInfo] SET LatestHistoryID='{strLatest}',RecordTypeID='{strRecordType}',
|
|
CurrentProductID='{row.ProductID}'
|
|
where SerialNumber='{row.SerialNumber}'";
|
|
colSQL.Add(strSQL);
|
|
}
|
|
else
|
|
{
|
|
strLatest = row.SerialNumber + 1.ToString().PadLeft(4, '0');//組成HistoryID
|
|
//ZHistoryKeyDefine
|
|
strSQL = @$"INSERT INTO [SFIS_PTD].[dbo].[ZHistoryKeyDefine] ([SerialNumber],[Seed])
|
|
VALUES ('{row.SerialNumber}',1)";
|
|
colSQL.Add(strSQL);
|
|
//ZSNInfo
|
|
strSQL = @$"INSERT INTO [SFIS_PTD].[dbo].[ZSNInfo] ([SerialNumber],[CurrentProductID],[OriginalProductID],
|
|
[LocationID],[LatestHistoryID],[RecordTypeID],
|
|
[OwnerPlantID],[OwnerCompanyID],[CreatePlantID],[CreateCompanyID],
|
|
[ModifyDate]) VALUES
|
|
('{row.SerialNumber}','{row.ProductID}','{row.ProductID}',
|
|
'9000','{strLatest}','{strRecordType}',
|
|
'{strWorkCenter}','{strRbu}','{strWorkCenter}','{strRbu}',
|
|
'{strCreateDate}')";
|
|
colSQL.Add(strSQL);
|
|
}
|
|
//ZProductTrans
|
|
strSQL = $@"INSERT INTO [SFIS_PTD].[dbo].[ZProductTrans]
|
|
([HistoryID],[SerialNumber],[RecordTypeID],[RecordNumber],[RCLineNO],[RecordDate],
|
|
[ProductID],[IsChangeID],[LocationID],[EmplID],[IsDelete],
|
|
[OwnerPlantID],[OwnerCompanyID],[CreatePlantID],[CreateCompanyID],
|
|
[CreatorID],[CreateDate],[ModifierID],[ModifyDate],
|
|
[chkflag],[IsBranchCoReceived],[MOID]) VALUES
|
|
('{strLatest}','{row.SerialNumber}','{strRecordType}','{SNData.dnno}','{SNData.dnLineNO}','{strRecordDate}',
|
|
'{SNData.productID}',0,'9000','{SNData.soldCustomerID.ToString()}',0,
|
|
'{strWorkCenter}','{strRbu}','{strWorkCenter}','{strRbu}',
|
|
'WH00','{strCreateDate}','WH00','{strCreateDate}',0,0,'')";
|
|
colSQL.Add(strSQL);
|
|
|
|
mailDataItem.Add(new SNDataModel
|
|
{
|
|
DnNo = SNData.dnno,
|
|
LineNo = SNData.dnLineNO,
|
|
Material = SNData.productID,
|
|
Sn = row.SerialNumber
|
|
});
|
|
}
|
|
strSQL = $@" Update SFIS_PTD.dbo.ZProductTrans set IsBranchCoReceived ='1'
|
|
WHERE ERP_CustomerID IN ({string.Join(", ", erpCustomerId.ConvertAll(id => $"N'{id}'"))})
|
|
AND IsBranchCoReceived = 0
|
|
AND RecordTypeID in (N'601',N'633',N'601DB')
|
|
AND RecordNumber='{SNData.dnno}'
|
|
AND RCLineNO='{SNData.dnLineNO}' ";
|
|
colSQL.Add(strSQL);
|
|
}
|
|
|
|
using (var transaction = ptdConnection.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
foreach (string query in colSQL)
|
|
{
|
|
await ptdConnection.ExecuteAsync(query, transaction: transaction);
|
|
}
|
|
|
|
// Commit the transaction
|
|
transaction.Commit();
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Rollback the transaction in case of an exception
|
|
transaction.Rollback();
|
|
throw ex; // Rethrow the exception after rollback
|
|
}
|
|
}
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.Message;
|
|
}
|
|
return result;
|
|
|
|
}
|
|
#endregion
|
|
|
|
#region "PTD008 還原作業"
|
|
[Route("[action]")]
|
|
[HttpGet]
|
|
public async Task<ResultModel<dynamic>> GetData4PTD008(string recordNumber, string lineNo, string materialNo, string shippingSN)
|
|
{
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
try
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
|
|
string strSQL = $@"SELECT RecordTypeID,RecordNumber,RCLineNO 'LineNo',
|
|
SerialNumber 'SN',ProductID , CreateDate
|
|
FROM SFIS_PTD.dbo.ZProductTrans where RecordNumber ='{recordNumber}' And RCLineNO ='{lineNo}'
|
|
And ProductID = '{materialNo}'";
|
|
if (shippingSN != null && shippingSN != "")
|
|
{
|
|
strSQL += $" And SerialNumber = '{shippingSN}'";
|
|
}
|
|
|
|
strSQL += " ORDER BY CreateDate ";
|
|
|
|
DataTable dtZProductTrans = new DataTable();
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.HasRows)
|
|
{
|
|
dtZProductTrans = DataReaderToDataTable(reader);
|
|
}
|
|
}
|
|
}
|
|
|
|
List<dynamic> list = new List<dynamic>();
|
|
foreach (DataRow row in dtZProductTrans.Rows)
|
|
{
|
|
dynamic dyn = new ExpandoObject();
|
|
list.Add(dyn);
|
|
foreach (DataColumn column in dtZProductTrans.Columns)
|
|
{
|
|
var dic = (IDictionary<string, object>)dyn;
|
|
dic[column.ColumnName] = row[column];
|
|
}
|
|
}
|
|
|
|
result.DataTotal = list.Count();
|
|
result.Data = list;
|
|
|
|
if (result == null)
|
|
{
|
|
result.Msg = "查無資料";
|
|
result.Success = false;
|
|
return result;
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
return result;
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
throw e;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
}
|
|
[Route("[action]")]
|
|
[HttpPost]
|
|
public async Task<ResultModel<dynamic>> PostPTD008Commit(PTD008CommitDataModel Data)
|
|
{
|
|
ResultModel<dynamic> result = new ResultModel<dynamic>();
|
|
Collection<string> colSQL = new Collection<string>();
|
|
//安勤連線
|
|
PTDContext _ptd_context = new PTDContext();
|
|
DbConnection connPTD = _ptd_context.Database.GetDbConnection();
|
|
if (connPTD.State != ConnectionState.Open)
|
|
{
|
|
await connPTD.OpenAsync();
|
|
}
|
|
try
|
|
{
|
|
string strSQL = "";
|
|
string strCreateDate = DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss");
|
|
|
|
|
|
foreach (PTD008TableDataModel SNData in Data.tableData)
|
|
{
|
|
|
|
if (Data.type == 0) //還原
|
|
{
|
|
//SQL Server 2012後才能使用
|
|
//strSQL = $"Select HistoryID,RecordTypeID,ModifyDate from [SFIS_PTD].[dbo].[ZProductTrans] " +
|
|
// $"where [SerialNumber] = '{SNData.sn}' " +
|
|
// $"Order by HistoryID desc offset 1 row fetch next 1 rows only ";
|
|
|
|
strSQL = $@"WITH RankedResults AS (
|
|
SELECT
|
|
HistoryID,
|
|
RecordTypeID,
|
|
ModifyDate,
|
|
ROW_NUMBER() OVER (ORDER BY HistoryID DESC) AS RowNum
|
|
FROM [SFIS_PTD].[dbo].[ZProductTrans]
|
|
WHERE [SerialNumber] = '{SNData.sn}'
|
|
)
|
|
SELECT
|
|
HistoryID,
|
|
RecordTypeID,
|
|
ModifyDate
|
|
FROM RankedResults
|
|
WHERE RowNum = 2";
|
|
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
cmd.CommandText = strSQL;
|
|
using (var reader = await cmd.ExecuteReaderAsync())
|
|
{
|
|
if (reader.Read())
|
|
{
|
|
strSQL = $"Update [SFIS_PTD].[dbo].[ZSNInfo] set RecordTypeID ='{reader["RecordTypeID"]}',ModifyDate ='{reader["ModifyDate"]}',LatestHistoryID ='{reader["HistoryID"]}' " +
|
|
$"where SerialNumber ='{SNData.sn}'";
|
|
colSQL.Add(strSQL);
|
|
|
|
int seed = int.Parse(reader["HistoryID"].ToString().Substring(reader["HistoryID"].ToString().Length - 4));
|
|
strSQL = @$"UPDATE [SFIS_PTD].[dbo].[ZHistoryKeyDefine]
|
|
SET [Seed] = {seed} where [SerialNumber]='{SNData.sn}'";
|
|
colSQL.Add(strSQL);
|
|
}
|
|
else
|
|
{
|
|
strSQL = $"Delete [SFIS_PTD].[dbo].[ZSNInfo] where SerialNumber ='{SNData.sn}'";
|
|
colSQL.Add(strSQL);
|
|
|
|
strSQL = $"Delete [SFIS_PTD].[dbo].[ZHistoryKeyDefine] where SerialNumber ='{SNData.sn}'";
|
|
colSQL.Add(strSQL);
|
|
}
|
|
}
|
|
}
|
|
}
|
|
else if (SNData.recordTypeID != "292"
|
|
&& SNData.recordTypeID != "101") //狀態為 292 樣品入庫、101 樣品入庫 才能刪除
|
|
{
|
|
throw new Exception(SNData.recordTypeID + "狀態不能刪除!");
|
|
}
|
|
|
|
strSQL = $"Delete [SFIS_PTD].[dbo].[ZProductTrans] " +
|
|
$"where [SerialNumber] = '{SNData.sn}' " +
|
|
$"and RecordNumber = '{SNData.recordNumber}' and RCLineNO ='{SNData.lineNo}' " +
|
|
$"and ProductID ='{SNData.productID}'";
|
|
colSQL.Add(strSQL);
|
|
|
|
strSQL = $@"Delete [SFIS_PTD].[dbo].[ZWHPickListDetail]
|
|
where InputSN = '{SNData.sn}'
|
|
and RecordNumber='{SNData.recordNumber}'
|
|
and RCLineNO='{SNData.lineNo}'";
|
|
colSQL.Add(strSQL);
|
|
}
|
|
|
|
using (var cmd = connPTD.CreateCommand())
|
|
{
|
|
using (var transaction = connPTD.BeginTransaction())
|
|
{
|
|
try
|
|
{
|
|
foreach (string query in colSQL)
|
|
{
|
|
cmd.CommandText = query;
|
|
cmd.Transaction = transaction;
|
|
cmd.ExecuteNonQuery();
|
|
}
|
|
|
|
// Commit the transaction
|
|
transaction.Commit();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
// Rollback the transaction in case of an exception
|
|
transaction.Rollback();
|
|
throw ex; // Rethrow the exception after rollback
|
|
}
|
|
}
|
|
}
|
|
|
|
result.Success = true;
|
|
result.Msg = "OK";
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
result.Success = false;
|
|
result.Msg = ex.Message;
|
|
}
|
|
finally
|
|
{
|
|
if (connPTD.State == ConnectionState.Open)
|
|
{
|
|
connPTD.Close();
|
|
connPTD.Dispose();
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
#endregion
|
|
}
|
|
}
|
|
|