
/*
  (ok)bpmm01 簽核設定
  (ok)bpmm02 表單簽核流程
  -- 如果某一關有多人待簽, 如何實作一個 主管派工 的關卡?
  bpmm11 簽核同意
  bpmm12 簽核駁回
  bpmm13 簽核作廢
    -- 作廢的 SOP
    1.(ok)必須先駁回到原申請人(駁回可以指定前面曾經簽過的人)
    2.(ok)申請人才能作廢
  
  -- 考慮用 Tab 三合一, bpm 的登入預設頁面
  (ok)bpmq01 待簽核表單
  (ok)bpmq02 已簽核表單 - aside 選單別(只能加在 page, 所以改為 picker 查詢)
  (ok)bpmq03 已結案表單
  
  (ok)bpmq11 簽核歷程
  (ok)bpmq12 後續簽核人
  
  todo:
  1.(ok)assign_to 要注意駁回及作廢時要如何 update (檢查並測試相關的 sp)
  2.(ok)已簽核表單要加 aside 查詢機制
  3.
  
  (ok)整理一下 users 的相關資料，劃出組織架構(方便說明) -> 編號要易記
  (ok)並設定群組內含多人, 流程關卡可以在複雜一點
  (xx)是否要加入會簽可以考慮一下
  
  (ok)Azure 紀錄的一定是格林威治時間, 和台北時間差 8 hours
      考慮寫一支 udf(cloud_addtime), 只要是含時間的欄位, 就 +8 hrs 再傳出(TBD) --> 僅顯示時用
	  測試 ok, 目前先用此法解決, 所有 sql 語法, 只要是要顯示時間, 該欄位就要呼叫本 udf
	  列出所有需要顯示出時間的 sp 並測試
	  ======================== ================
	  bpm_sign_todo            OK
	  bpm_sign_pass            OK
	  bpm_sign_history         OK
	  bpm_sign_finish          OK
	  bpm_sign_signers         OK
	  
	  eip_getCalendar          OK
	  get_Schedule             應已不使用
	  get_gantt_data           OK
	  
	  orm_api 底層的 select    是否要加入(傾向 no)
*/

0.key-value 定義
-- [sign_status] [varchar](2) not null,   -- N:待簽核 P:簽核中 R:拒絶 A:同意 C:作廢
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','sign_status','N','待簽核');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','sign_status','P','簽核中');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','sign_status','R','拒絕');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','sign_status','A','同意');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','sign_status','C','作廢');

-- [flow_status] [varchar](2) not null,    -- N:待簽核 P:簽核中 Z:結案 C:作廢
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','flow_status','N','待簽核');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','flow_status','P','簽核中');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','flow_status','Z','結案');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','flow_status','C','作廢');


1.先將 flow 的 json 寫到 db, 注意有單引號

2.再將 json 解析後, 寫到 flow setup 的相關 tables

/*
訊光系統資料表 SYS_TODOHIS(SYS_TODOLIST)的STATUS欄位新增寫入狀態的功能, 如下:
結案時: 寫入 'Z',
前進(上呈)時: 寫入 'N', 
退回時: 寫入 'NR', 
取回時: 寫入 'NF'
作廢時: 寫入 'X', 
加簽時: 寫入 'A'。
任意加簽：'AA'
檢核(Validate)寫入: 'V'
'F':通知
暫停時：寫入'NP'
============================
flowflag
N:新起,
P:進行中
Z:結案
X:作廢
N:取回 ??
*/

-- flow 列表
/*
alter table bpm_list add source_table varchar(100);
alter table bpm_list add source_pk varchar(100);
*/
create table bpm_list (
  flow_id varchar(100) not null,
  flow_name nvarchar(100),
  functiontag varchar(100),         -- 對應的功能, 如請假單 (hrsm11 ?)
  -- New fields
  source_table varchar(100),        -- 來源單主 Tables
  source_pk varchar(100),           -- 來源單 pk, 如果是複合主鍵, 用逗號區隔
  source_note varchar(100),         -- 來源單的主旨欄位名稱
  source_applicant varchar(20),     -- 來源單的上呈人欄位名稱(預設 personid)
  --
  flow_json nvarchar(max),
  flow_json_org nvarchar(max)
  primary key (flow_id)
)
	
-- flow 內容設定
/*
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','elementId','startNode','startNode');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','elementId','nodeNode','nodeNode');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','elementId','conditionNode','conditionNode');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','elementId','switchNode','switchNode');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','elementId','stopNode','stopNode');

alter table bpm_setup add typeName varchar(10);

FlowStatus as 簽核中,拒絶/同意
*/
create table bpm_setup (
  flow_id varchar(100) not null,
  elementId varchar(20) not null,   -- startNode,nodeNode,conditionNode,switchNode,stopNode
  stepId varchar(100),              -- 識別代號
  filterSet varchar(1000),          -- 條件(condition only) 
  nextStep varchar(1000),
  nextStep_false varchar(100),
  stepName nvarchar(100),
  -- new
  typeName varchar(10),             -- 類型 fields / users / roles ==> SignerType as 部門主管,指定欄位,指定角色,指定用戶,用戶主管
  roleName varchar(100)
  primary key (flow_id,stepId)
)

-- sign 步驟(spread from bpm_setup) 上呈時寫入(bpmm02_spread)
create table [dbo].[bpm_flow_sign](
	[id] int identity(1,1) not null,
	[flow_id] [varchar](100) not null,
	[sourceid] [varchar](100) not null,    -- 原單的 pk(請假單-單號)
	[stepName] [nvarchar](30) not null,
	[flow_level] [smallint] not null,      -- 次序
	--[totallevel] [smallint] not null,    -- 沒有用(count 即可)
	[signerid] [varchar](10) not null,
	--[signername] [nvarchar](20) not null,
	[sign_note] [nvarchar](600) null,      -- 主旨(請假原因)
	[sign_status] [varchar](2) not null,   -- N:待簽核 P:簽核中 R:拒絶 A:同意 C:作廢
	--[signtime] [datetime] null,          -- 建檔日期 
	create_date datetime null,             -- 上呈日期
	-- 考慮加入 3 個欄位(已加入), 在 同意/駁回 UX 上新增下一關的人員的選項,  如果下一關是多人, 就可以在此指派
	assign_to char(1),                     -- 如果某一關指定的 group 有多人, 用此欄位紀錄指派給誰(多人 default=N, 單人=Y)
	estimate_date datetime null,           -- 預計處理日(暫未使用)
	real_date datetime null,               -- 實際處理日(暫未使用)
	-- the end
    primary key (id)
)

-- 暫存 spread 時 condition 的 nextStep (內容應僅保留最近 7 天資料即可)
create table [dbo].[bpm_flow_spread_log](
	[id] int identity(1,1) not null,
	[flow_id] [varchar](100) not null,
	[sourceid] [varchar](100) not null,
	stepId varchar(100),
    nextStep varchar(1000),
	create_date datetime null,               -- 寫入日期
	primary key (id)
)

-- sign 紀錄 log
create table bpm_flow_sign_log (
	id int identity(1,1) not null,
	flow_id varchar(100) not null,           -- 關連到原單(請假單)
	sourceid varchar(100) not null,          -- 同上
	create_date datetime null,               -- 寫入日期
	signerid varchar(10) not null,
	sign_status [varchar](1) not null,       -- R:拒絶 A:同意
	sign_note nvarchar(600) null,            -- 簽核意見(同意或拒絕意見)
	create_date datetime null,               -- 寫入日期
	primary key (id)
)

/*
-- 發送 mail 機制(master 的 zen_mail_list)
use zenclouddb;
create table zen_mail_list (
	id int identity(1,1) not null,
	corp_db varchar(20) null,                -- 公司代號(統編)
	recipient varchar(100) null,             -- mail 收件人
	mail_subject datetime null,              -- 主旨
	content varchar(10) null,                -- 內容
	mail_status smallint null,               -- 0:未發送 1:已發送
    create_date datetime null,               -- 寫入日期
	primary key (id)
)
*/


3.請假單相關 Tables
/*
  hrs_leave      請假單
  hrs_leave_date 請假日期(by date)
  hrs_leavetype  假別
*/
create table [dbo].[hrs_leave](
    [billdate] [datetime] not null,         -- 申請日
	[billno] [varchar](20) not null,
	[personid] [varchar](10) not null,      -- 關聯 basperson
	[agentid] [varchar](10) not null,       -- 同上
	[leavetype] [char](10) not null,        -- 關聯 hrs_leavetype
	[starttime] [datetime] not null,
	[endtime] [datetime] not null,
	[days] [decimal](5, 1),
	[hours] [decimal](5, 1),
	[filename] [nvarchar](100),
	[leave_status] [char](1),      -- y/n 有效/無效  ??
	[leave_note] [nvarchar](200) null,  -- 請假原因
	[create_user] [nvarchar](20) null,
	[create_date] [datetime] null,
	[update_user] [nvarchar](20) null,
	[update_date] [datetime] null,
	-- 暫時觀察
	--[flow_step] [int] not null,           -- bpm_flow_sign.id (目前卡在哪一關) => 最終決定取消
	[flow_status] [varchar](2) not null,    -- N:待簽核 P:簽核中 Z:結案 C:作廢
	flow_signer varchar(200),               -- 目前簽到誰? 系統控制, 並在簽核過程中使用
    primary key(billno)
)

-- 將區間的請假, 展開到以天為單位
create table [dbo].[hrs_leave_date](
	[billno] [varchar](12) not null,
	[billdate] [datetime] not null,
	[stime] [varchar](10) not null,
	[etime] [varchar](10) null,
	[leaveday] [decimal](5, 1) null,
	[leavehour] [decimal](5, 1) null,
    primary key(billno,billdate)
)

create table [dbo].[hrs_leavetype](
	[leavetype_id] [varchar](10) not null,
	[leavetype_name] [nvarchar](10) null,
	[leavetype_desc] [varchar](20) null,
	[isyearage] [varchar](1) null,
	[ispayed] [varchar](1) null,
	[descrate] [decimal](12, 2) null,
	[isdescfullentry] [varchar](1) null,
	[type] [smallint] null,
	[maxtimes_month] [int] null,
	[maxtimes_year] [int] null,
	[showoffwork] [varchar](1) null,
	[sex] [varchar](2) null,
	[permitsex] [varchar](2) null,
	[isdelay] [char](1) null,
    primary key(leavetype_id)
)

-- 
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','sa_category','ON','上班');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','sa_category','OFF','下班');

-- 打卡調整單(hrsm12)
create table [dbo].[hrs_swipecard_adjustment](
    [billdate] [datetime] not null,         -- 出勤日期	
	[billno] [varchar](20) not null,
	[personid] [varchar](10) not null,      -- 申請人(關聯 basperson)
	--
	sa_title [nvarchar](200) null,          -- 主旨  
	sa_category [varchar](10) null,         -- 打卡調整類型(on:上班/off:下班)
	sa_description [nvarchar](max) null,    -- 打卡調整原因
	--
	[create_user] [nvarchar](20) null,
	[create_date] [datetime] null,
	[update_user] [nvarchar](20) null,
	[update_date] [datetime] null,
	-- flow system fields
	[flow_status] [varchar](2) not null,    -- N:待簽核 P:簽核中 Z:結案 C:作廢
	flow_signer varchar(200),               -- 目前簽到誰? 系統控制, 並在簽核過程中使用
    primary key(billno)
)
GO


/*
  資訊處理單及
  系統變更需求確認書
*/
-- ok
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','sr_category','net','網路問題');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','sr_category','permission','權限問題');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','sr_category','report','BI 報表問題');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','sr_category','bpm','簽核表單');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','sr_category','erp','企業管理軟體');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','sr_category','others','其他');

-- 資訊問題處理單(eipm21)
-- 上呈 -> 直屬主管 -> IT主管 -> 派工 -> 驗收測試 -> 原提案人 -> 結案
create table [dbo].[eip_service_request](
	[billdate] [datetime] not null,         -- 申請日
	[billno] [varchar](20) not null,
	[personid] [varchar](10) not null,      -- 申請人(關聯 basperson)
	--
	sr_title [nvarchar](200) null,          -- 主旨   
	sr_category [varchar](10) null,         -- 問題類別(根據類別跑不同的 Group), 派工時根據相關設定判斷 Group, 派工設定流程要如何設定
	sr_description [nvarchar](max) null,    -- 問題和用途描述
	[filename] [nvarchar](100),
	--
	[create_user] [nvarchar](20) null,
	[create_date] [datetime] null,
	[update_user] [nvarchar](20) null,
	[update_date] [datetime] null,
	-- flow system fields
	[flow_status] [varchar](2) not null,    -- N:待簽核 P:簽核中 Z:結案 C:作廢
	flow_signer varchar(200),               -- 目前簽到誰? 系統控制, 並在簽核過程中使用
    primary key(billno)
)
GO

-- 系統變更需求確認書(eipm22)
create table [dbo].[eip_change_requirement](
	[billdate] [datetime] not null,         -- 申請日
	[billno] [varchar](20) not null,
	[personid] [varchar](10) not null,      -- 申請人(關聯 basperson)
	--
	cr_title [nvarchar](200) null,          -- 主旨
	cr_module [varchar](10) null,           -- 系統模組(hrs, eip, pms ...), 根據 module 跑不同的 Group --> 關聯 sys_module
	cr_description [nvarchar](max) null,    -- 需求說明
	[filename] [nvarchar](100),
	--
	[create_user] [nvarchar](20) null,
	[create_date] [datetime] null,
	[update_user] [nvarchar](20) null,
	[update_date] [datetime] null,
	-- flow system fields
	[flow_status] [varchar](2) not null,    -- N:待簽核 P:簽核中 Z:結案 C:作廢
	flow_signer varchar(200),               -- 目前簽到誰? 系統控制, 並在簽核過程中使用
    primary key(billno)
)
GO

-- 辦公用品請領(eipm23)
create table [dbo].[eip_apply_receive](
	[billdate] [datetime] not null,         -- 申請日
	[billno] [varchar](20) not null,
	[personid] [varchar](10) not null,      -- 申請人(關聯 basperson)
	--
	ar_title [nvarchar](200) null,          -- 主旨
	ar_category [varchar](10) null,         -- 需求類別(根據類別跑不同的 Group), 派工時根據相關設定判斷 Group, 派工設定流程要如何設定
	ar_description [nvarchar](max) null,    -- 需求說明
	--
	[create_user] [nvarchar](20) null,
	[create_date] [datetime] null,
	[update_user] [nvarchar](20) null,
	[update_date] [datetime] null,
	-- flow system fields
	[flow_status] [varchar](2) not null,    -- N:待簽核 P:簽核中 Z:結案 C:作廢
	flow_signer varchar(200),               -- 目前簽到誰? 系統控制, 並在簽核過程中使用
    primary key(billno)
)
GO

-- 產品測試紀錄表(crmm21)
/*
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','package_method','Normal','正常包裝');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','package_method','Others','其他');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','final_process','Finished','成品銷售');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','final_process','Sample','樣品');
insert into zen_keyvalue (tablename,fieldname,keyid,keyvalue) values ('xxxx','final_process','Others','其他');
*/
create table [dbo].[crm_product_testing](
	[billdate] [datetime] not null,         -- 申請日
	[billno] [varchar](20) not null,
	[personid] [varchar](10) not null,      -- 申請人(關聯 basperson)
	--
	custid varchar(10) not null,            -- 客戶編號(關聯 bascustomer)
	productid varchar(20),                  -- 產品編號
	-- prodcname nvarchar(100),                -- 產品名稱
	purpose nvarchar(600),                  -- 目的
	test_date datetime null,                -- 試做日期
	need_qty decimal(10,2),                 -- 需求量
	package_method varchar(10),             -- 包裝方式(正常包裝/其他)
	Package_desc nvarchar(200),             -- 包裝描述
	final_process varchar(10),              -- 最終處理(成品銷售/樣品/其他)
	final_desc nvarchar(200),               -- 最終處理描述
	purpose_desc nvarchar(max),             -- 製程需求/訴求  
	spec_desc1 nvarchar(100),               -- 巧克力 型態(軟質、硬質)
	spec_desc2 nvarchar(100),               -- 巧克力 融點(℃)
	spec_desc3 nvarchar(100),               -- 乳酪 融解性(高融點、可融)
	spec_desc4 nvarchar(100),               -- 乳酪 型態(片、丁、塊、絲等)
	real_desc nvarchar(max),                -- 試作情形說明   
	response_desc nvarchar(max),            -- 意見回覆
	followup_work nvarchar(max),            -- 後續作業
	--
	[create_user] [nvarchar](20) null,
	[create_date] [datetime] null,
	[update_user] [nvarchar](20) null,
	[update_date] [datetime] null,
	close_date datetime null,               -- 結案日期
	-- flow system fields
	[flow_status] [varchar](2) not null,    -- N:待簽核 P:簽核中 Z:結案 C:作廢
	flow_signer varchar(200),               -- 目前簽到誰? 系統控制, 並在簽核過程中使用
    primary key(billno)
)
GO


4.加入上呈 sp 機制, 新增簽核紀錄檔
  exec bpmm02_spread @flod_id,@signerid,@billno;

5.加入同意(核可) sp 機制
  exec bpmm02_sign_active @token,'A','hrsm11','221030-001','gm ok'
  
6.加入駁回(reject) sp 機制
  exec bpmm02_sign_active @token,'R','hrsm11','221030-001','gm ok'
  
7.加入作廢 sp 機制
  exec bpmm02_sign_active @token,'C','hrsm11','221030-001','gm ok'


A.待簽核表單
A.1 (List)
  表單名稱	表單編號	 主旨		申請人部門	申請人	進行步驟	填單時間
  ========	============ ==========	==========	======	===========	===================
  請假單	2022-08-0018 請假		資訊處		Michael	代理人		2022-08-22 14:20:54
  
select 
  b.flow_name,a.sourceid,a.sign_note,d.departmentcname,p.personcname,a.stepName,a.signtime
from bpm_flow_sign A
left join bpm_list B on A.flow_id = B.flow_id
left join basperson P on A.signerid = P.personid
left join basdepartment D on D.departmentid = P.departmentid
where a.signerid = @login_user and a.sign_status = 'P'  -- 目前簽核關卡
  
A.2 (單筆顯示)
  Form
  [原單]
  [簽核意見]
  [簽核歷程]
  
  [同意] [駁回] [檢視後續簽核人]
  
B.已簽核表單(List)
  表單編號		主旨	申請人部門	申請人	簽核步驟	表單送達時間		簽核結果	簽核時間			表單目前狀況
  ============	=======	==========	======	===========	===================	==========	===================	============
  2022-08-0018	請假	資訊處		Michael	代理人		2022-08-22 14:20:54 同意		2022-08-22 14:20:54 同意結束
  
select
  b.flow_name,a.sourceid,a.sign_note,d.departmentcname,p.personcname,a.stepName,a.signtime
from bpm_flow_sign A
left join bpm_list B on A.flow_id = B.flow_id
left join basperson P on A.signerid = P.personid
left join basdepartment D on D.departmentid = P.departmentid
-- where a.signerid = xxx and a.sign_status = 'New'  -- 待確認
  

C.已結案表單(List)
  表單編號		主旨	申請時間			簽核完成時間					歸檔資訊
  ============	=======	===================	============================	==========
  2022-08-0018	請假	2022-08-22 14:20:54 2022-08-22 14:20:54(駁回結束) 	自動歸檔
  