Example #1
0
        /// <summary>
        /// 申請覆核 電子憑證
        /// </summary>
        /// <param name="insertDatas">資料</param>
        /// <param name="taData">申請單資料</param>
        /// <returns></returns>
        public MSGReturnModel <IEnumerable <ITreaItem> > ApplyAudit(IEnumerable <ITreaItem> insertDatas, TreasuryAccessViewModel taData)
        {
            var result = new MSGReturnModel <IEnumerable <ITreaItem> >();

            result.RETURN_FLAG = false;
            DateTime dt = DateTime.Now;

            try
            {
                if (insertDatas != null)
                {
                    var datas = (List <CAViewModel>)insertDatas;
                    if (datas.Any())
                    {
                        using (TreasuryDBEntities db = new TreasuryDBEntities())
                        {
                            //取得流水號
                            SysSeqDao sysSeqDao = new SysSeqDao();
                            string    logStr    = string.Empty; //log
                            var       item_Seq  = "E5";         //電子憑證流水號開頭編碼

                            String qPreCode = DateUtil.getCurChtDateTime().Split(' ')[0];

                            var _TAR         = new TREA_APLY_REC();                        //申請單號
                            var _APLY_STATUS = Ref.AccessProjectFormStatus.A01.ToString(); //表單申請

                            if (!taData.vAplyNo.IsNullOrWhiteSpace())                      //修改已存在申請單
                            {
                                #region 申請單紀錄檔
                                _TAR = db.TREA_APLY_REC.First(x => x.APLY_NO == taData.vAplyNo);
                                if (CustodyAppr.Contains(_TAR.APLY_STATUS))
                                {
                                    _APLY_STATUS     = CustodyConfirmStatus;
                                    _TAR.CUSTODY_UID = AccountController.CurrentUserId; //保管單位直接帶使用者
                                    _TAR.CUSTODY_DT  = dt;
                                }
                                else
                                {
                                    if (_TAR.APLY_STATUS != _APLY_STATUS)                          //申請紀錄檔狀態不是在表單申請狀態
                                    {
                                        _APLY_STATUS = Ref.AccessProjectFormStatus.A05.ToString(); //為重新申請案例
                                    }
                                }
                                _TAR.APLY_STATUS    = _APLY_STATUS;
                                _TAR.LAST_UPDATE_DT = dt;

                                logStr += _TAR.modelToString(logStr);
                                #endregion

                                #region 申請單歷程檔
                                var _ARH = new APLY_REC_HIS()
                                {
                                    APLY_NO     = taData.vAplyNo,
                                    APLY_STATUS = _APLY_STATUS,
                                    PROC_DT     = dt,
                                    PROC_UID    = taData.vCreateUid
                                };
                                logStr += _ARH.modelToString(logStr);
                                db.APLY_REC_HIS.Add(_ARH);
                                #endregion

                                #region 電子憑證庫存資料檔

                                var            _dept         = intra.getDept_Sect(taData.vAplyUnit);
                                List <string>  oldItemIds    = db.OTHER_ITEM_APLY.Where(x => x.APLY_NO == taData.vAplyNo).Select(x => x.ITEM_ID).ToList(); //原有 itemId
                                List <string>  updateItemIds = new List <string>();                                                                        //更新 itemId
                                List <string>  cancelItemIds = new List <string>();                                                                        //取消 itemId
                                List <ITEM_CA> inserts       = new List <ITEM_CA>();                                                                       //新增資料

                                //抓取有修改註記的
                                foreach (var item in datas)
                                {
                                    var _IC_Item_Id = string.Empty;
                                    if (taData.vAccessType == Ref.AccessProjectTradeType.P.ToString()) //存入
                                    {
                                        //只抓取預約存入
                                        if (item.vStatus == Ref.AccessInventoryType._3.GetDescription())
                                        {
                                            var _IC = new ITEM_CA();

                                            if (item.vItemId.StartsWith(item_Seq)) //舊有資料
                                            {
                                                _IC = db.ITEM_CA.FirstOrDefault(x => x.ITEM_ID == item.vItemId);
                                                if (_IC.LAST_UPDATE_DT > item.vLast_Update_Time)
                                                {
                                                    result.DESCRIPTION = Ref.MessageType.already_Change.GetDescription();
                                                    return(result);
                                                }
                                                _IC.CA_DESC   = item.vCA_Desc;   //電子憑證品項
                                                _IC.CA_USE    = item.vCA_Use;    //電子憑證用途
                                                _IC.BANK      = item.vBank;      //銀行/廠商
                                                _IC.CA_NUMBER = item.vCA_Number; //電子憑證號碼
                                                _IC.MEMO      = item.vMemo;      //備註說明
                                                updateItemIds.Add(item.vItemId);
                                                logStr += _IC.modelToString(logStr);
                                            }
                                            else
                                            {
                                                var item_id = sysSeqDao.qrySeqNo(item_Seq, string.Empty).ToString().PadLeft(8, '0');
                                                _IC = new ITEM_CA()
                                                {
                                                    ITEM_ID          = $@"{item_Seq}{item_id}", //物品編號
                                                    INVENTORY_STATUS = "3",                     //預約存入
                                                    CA_DESC          = item.vCA_Desc,           //電子憑證品項
                                                    CA_USE           = item.vCA_Use,            //電子憑證用途
                                                    BANK             = item.vBank,              //銀行/廠商
                                                    CA_NUMBER        = item.vCA_Number,         //電子憑證號碼
                                                    MEMO             = item.vMemo,              //備註說明
                                                    APLY_DEPT        = _dept.Item1,             //申請人部門
                                                    APLY_SECT        = _dept.Item2,             //申請人科別
                                                    APLY_UID         = taData.vAplyUid,         //申請人
                                                    CHARGE_DEPT      = _dept.Item1,             //權責部門
                                                    CHARGE_SECT      = _dept.Item2,             //權責科別
                                                                                                //PUT_DATE = dt, //存入日期時間
                                                    LAST_UPDATE_DT = dt,                        //最後修改時間
                                                };
                                                _IC_Item_Id = _IC.ITEM_ID;
                                                inserts.Add(_IC);
                                                logStr += _IC.modelToString(logStr);
                                            }
                                        }
                                    }
                                    else if (taData.vAccessType == Ref.AccessProjectTradeType.G.ToString() && (_APLY_STATUS != CustodyConfirmStatus))//取出
                                    {
                                        var _IC = db.ITEM_CA.FirstOrDefault(x => x.ITEM_ID == item.vItemId);
                                        if (_IC.LAST_UPDATE_DT > item.vLast_Update_Time)
                                        {
                                            result.DESCRIPTION = Ref.MessageType.already_Change.GetDescription();
                                            return(result);
                                        }
                                        //預約取出
                                        if (item.vtakeoutFlag)
                                        {
                                            if (_IC.INVENTORY_STATUS == "1") //原先為在庫
                                            {
                                                _IC.INVENTORY_STATUS = "4";  //預約取出
                                                _IC.LAST_UPDATE_DT   = dt;   //最後修改時間
                                                updateItemIds.Add(_IC.ITEM_ID);
                                            }
                                            else if (_IC.INVENTORY_STATUS == "4") //原先為預約取出
                                            {
                                                updateItemIds.Add(_IC.ITEM_ID);
                                            }
                                        }
                                        else
                                        {
                                            if (_IC.INVENTORY_STATUS == "4") //原先為在庫
                                            {
                                                _IC.INVENTORY_STATUS = "1";  //預約取出
                                                _IC.LAST_UPDATE_DT   = dt;   //最後修改時間
                                            }
                                        }
                                    }
                                }

                                if (taData.vAccessType == Ref.AccessProjectTradeType.P.ToString()) //存入
                                {
                                    var delItemId = oldItemIds.Where(x => !updateItemIds.Contains(x)).ToList();
                                    db.OTHER_ITEM_APLY.RemoveRange(db.OTHER_ITEM_APLY.Where(x => x.APLY_NO == taData.vAplyNo && delItemId.Contains(x.ITEM_ID)).ToList());
                                    db.ITEM_CA.RemoveRange(db.ITEM_CA.Where(x => delItemId.Contains(x.ITEM_ID)).ToList());
                                    db.OTHER_ITEM_APLY.AddRange(inserts.Select(x => new OTHER_ITEM_APLY()
                                    {
                                        APLY_NO = taData.vAplyNo,
                                        ITEM_ID = x.ITEM_ID
                                    }));
                                    db.ITEM_CA.AddRange(inserts);
                                }
                                else if (taData.vAccessType == Ref.AccessProjectTradeType.G.ToString() && (_APLY_STATUS != CustodyConfirmStatus))//取出
                                {
                                    db.OTHER_ITEM_APLY.RemoveRange(db.OTHER_ITEM_APLY.Where(x => x.APLY_NO == taData.vAplyNo).ToList());
                                    db.OTHER_ITEM_APLY.AddRange(updateItemIds.Select(x => new OTHER_ITEM_APLY()
                                    {
                                        APLY_NO = taData.vAplyNo,
                                        ITEM_ID = x
                                    }));
                                }
                                #endregion
                            }
                            else
                            {
                                #region 申請單紀錄檔 & 申請單歷程檔

                                var data = SaveTREA_APLY_REC(db, taData, logStr, dt);
                                _TAR.APLY_NO = data.Item1;
                                logStr       = data.Item2;

                                #endregion

                                #region 電子憑證庫存資料檔
                                var _dept = intra.getDept_Sect(taData.vAplyUnit);
                                //抓取有修改註記的
                                foreach (var item in datas)
                                {
                                    var _IC_Item_Id = string.Empty;
                                    if (taData.vAccessType == Ref.AccessProjectTradeType.P.ToString()) //存入
                                    {
                                        //只抓取預約存入
                                        if (item.vStatus == Ref.AccessInventoryType._3.GetDescription())
                                        {
                                            var item_id = sysSeqDao.qrySeqNo(item_Seq, string.Empty).ToString().PadLeft(8, '0');
                                            var _IC     = new ITEM_CA()
                                            {
                                                ITEM_ID          = $@"{item_Seq}{item_id}", //物品編號
                                                INVENTORY_STATUS = "3",                     //預約存入
                                                CA_DESC          = item.vCA_Desc,           //電子憑證品項
                                                CA_USE           = item.vCA_Use,            //電子憑證用途
                                                BANK             = item.vBank,              //銀行/廠商
                                                CA_NUMBER        = item.vCA_Number,         //電子憑證號碼
                                                MEMO             = item.vMemo,              //備註說明
                                                APLY_DEPT        = _dept.Item1,             //申請人部門
                                                APLY_SECT        = _dept.Item2,             //申請人科別
                                                APLY_UID         = taData.vAplyUid,         //申請人
                                                CHARGE_DEPT      = _dept.Item1,             //權責部門
                                                CHARGE_SECT      = _dept.Item2,             //權責科別
                                                                                            //PUT_DATE = dt, //存入日期時間
                                                LAST_UPDATE_DT = dt,                        //最後修改時間
                                            };
                                            _IC_Item_Id = _IC.ITEM_ID;
                                            db.ITEM_CA.Add(_IC);
                                            logStr += _IC.modelToString(logStr);
                                        }
                                    }
                                    else if (taData.vAccessType == Ref.AccessProjectTradeType.G.ToString())//取出
                                    {
                                        //只抓取預約取出
                                        if (item.vStatus == Ref.AccessInventoryType._4.GetDescription())
                                        {
                                            var _IC = db.ITEM_CA.FirstOrDefault(x => x.ITEM_ID == item.vItemId);
                                            _IC_Item_Id = _IC.ITEM_ID;
                                            if (_IC.LAST_UPDATE_DT > item.vLast_Update_Time)
                                            {
                                                result.DESCRIPTION = Ref.MessageType.already_Change.GetDescription();
                                                return(result);
                                            }
                                            _IC.INVENTORY_STATUS = "4"; //預約取出
                                                                        //_IRE.GET_DATE = dt; //取出日期時間
                                            _IC.LAST_UPDATE_DT = dt;    //最後修改時間
                                        }
                                    }


                                    #region 其它存取項目申請資料檔
                                    if (!_IC_Item_Id.IsNullOrWhiteSpace())
                                    {
                                        db.OTHER_ITEM_APLY.Add(
                                            new OTHER_ITEM_APLY()
                                        {
                                            APLY_NO = _TAR.APLY_NO,
                                            ITEM_ID = _IC_Item_Id
                                        });
                                    }
                                    #endregion
                                }
                                #endregion
                            }

                            #region Save Db
                            var validateMessage = db.GetValidationErrors().getValidateString();
                            if (validateMessage.Any())
                            {
                                result.DESCRIPTION = validateMessage;
                            }
                            else
                            {
                                try
                                {
                                    db.SaveChanges();

                                    #region LOG
                                    //新增LOG
                                    Log log = new Log();
                                    log.CFUNCTION = "申請覆核-新增電子憑證";
                                    log.CACTION   = "A";
                                    log.CCONTENT  = logStr;
                                    LogDao.Insert(log, taData.vCreateUid);
                                    #endregion

                                    result.RETURN_FLAG = true;
                                    result.DESCRIPTION = Ref.MessageType.Apply_Audit_Success.GetDescription(null, $@"單號為{_TAR.APLY_NO}");
                                }
                                catch (DbUpdateException ex)
                                {
                                    result.DESCRIPTION = ex.exceptionMessage();
                                }
                            }
                            #endregion
                        }
                    }
                    else
                    {
                        result.DESCRIPTION = Ref.MessageType.not_Find_Audit_Data.GetDescription();
                    }
                }
                else
                {
                    result.DESCRIPTION = Ref.MessageType.not_Find_Audit_Data.GetDescription();
                }
            }
            catch (Exception ex)
            {
                result.DESCRIPTION = ex.exceptionMessage();
            }

            return(result);
        }
Example #2
0
        /// <summary>
        /// 申請覆核動作
        /// </summary>
        /// <param name="insertDatas"></param>
        /// <param name="taData"></param>
        /// <returns></returns>
        public MSGReturnModel <IEnumerable <ITreaItem> > ApplyAudit(IEnumerable <ITreaItem> insertDatas, TreasuryAccessViewModel taData)
        {
            var result = new MSGReturnModel <IEnumerable <ITreaItem> >();

            result.RETURN_FLAG = false;
            DateTime dt = DateTime.Now;

            try
            {
                if (insertDatas != null)
                {
                    //取得流水號
                    SysSeqDao sysSeqDao = new SysSeqDao();
                    String    qPreCode  = DateUtil.getCurChtDateTime().Split(' ')[0];
                    var       datas     = (List <BillViewModel>)insertDatas;
                    string    logStr    = string.Empty;       //log
                    var       item_Seq  = "E2";               //空白票卷流水號開頭編碼
                    if (!taData.vAplyNo.IsNullOrWhiteSpace()) //修改已存在申請單
                    {
                        if (datas.Any())
                        {
                            using (TreasuryDBEntities db = new TreasuryDBEntities())
                            {
                                var _APLY_STATUS = Ref.AccessProjectFormStatus.A01.ToString(); //表單申請

                                #region 申請單紀錄檔
                                var _TAR = db.TREA_APLY_REC.First(x => x.APLY_NO == taData.vAplyNo);
                                if (CustodyAppr.Contains(_TAR.APLY_STATUS))
                                {
                                    _APLY_STATUS     = CustodyConfirmStatus;
                                    _TAR.CUSTODY_UID = AccountController.CurrentUserId; //保管單位直接帶使用者
                                    _TAR.CUSTODY_DT  = dt;
                                }
                                else
                                {
                                    if (_TAR.APLY_STATUS != _APLY_STATUS)                          //申請紀錄檔狀態不是在表單申請狀態
                                    {
                                        _APLY_STATUS = Ref.AccessProjectFormStatus.A05.ToString(); //為重新申請案例
                                    }
                                }
                                _TAR.APLY_STATUS    = _APLY_STATUS;
                                _TAR.LAST_UPDATE_DT = dt;
                                logStr += _TAR.modelToString(logStr);
                                #endregion

                                #region 申請單歷程檔
                                var _ARH = new APLY_REC_HIS()
                                {
                                    APLY_NO     = taData.vAplyNo,
                                    APLY_STATUS = _APLY_STATUS,
                                    PROC_DT     = dt,
                                    PROC_UID    = taData.vCreateUid
                                };
                                logStr += _ARH.modelToString(logStr);
                                db.APLY_REC_HIS.Add(_ARH);
                                #endregion

                                #region 空白票據申請資料檔
                                List <BLANK_NOTE_APLY> updates = new List <BLANK_NOTE_APLY>();
                                List <BLANK_NOTE_APLY> inserts = new List <BLANK_NOTE_APLY>();

                                foreach (var item in datas)
                                {
                                    if (item.vItemId.StartsWith(item_Seq)) // 舊有資料
                                    {
                                        var _BNA = db.BLANK_NOTE_APLY.First(
                                            x => x.ITEM_ID == item.vItemId &&
                                            x.APLY_NO == taData.vAplyNo);
                                        _BNA.CHECK_TYPE     = item.vCheckType;
                                        _BNA.ISSUING_BANK   = item.vIssuingBank;
                                        _BNA.CHECK_NO_TRACK = item.vCheckNoTrack;
                                        _BNA.CHECK_NO_B     = item.vCheckNoB;
                                        _BNA.CHECK_NO_E     = item.vCheckNoE;
                                        updates.Add(_BNA);
                                        logStr += _BNA.modelToString(logStr);
                                    }
                                    else
                                    {
                                        var item_id = sysSeqDao.qrySeqNo(item_Seq, string.Empty).ToString().PadLeft(8, '0');
                                        var _BNA    = new BLANK_NOTE_APLY()
                                        {
                                            APLY_NO        = _TAR.APLY_NO,
                                            ITEM_ID        = $@"{item_Seq}{item_id}",
                                            CHECK_TYPE     = item.vCheckType,
                                            ISSUING_BANK   = item.vIssuingBank,
                                            CHECK_NO_TRACK = item.vCheckNoTrack,
                                            CHECK_NO_B     = item.vCheckNoB,
                                            CHECK_NO_E     = item.vCheckNoE
                                        };
                                        inserts.Add(_BNA);
                                        logStr += _BNA.modelToString(logStr);
                                    }
                                }

                                var ups = updates.Select(x => x.ITEM_ID).ToList();

                                db.BLANK_NOTE_APLY.RemoveRange(
                                    db.BLANK_NOTE_APLY.Where(x => x.APLY_NO == taData.vAplyNo && !ups.Contains(x.ITEM_ID)).ToList());
                                db.BLANK_NOTE_APLY.AddRange(inserts);

                                #endregion

                                #region Save Db

                                var validateMessage = db.GetValidationErrors().getValidateString();
                                if (validateMessage.Any())
                                {
                                    result.DESCRIPTION = validateMessage;
                                }
                                else
                                {
                                    try
                                    {
                                        db.SaveChanges();

                                        #region LOG
                                        //新增LOG
                                        Log log = new Log();
                                        log.CFUNCTION = "申請覆核-修改空白票據";
                                        log.CACTION   = "U";
                                        log.CCONTENT  = logStr;
                                        LogDao.Insert(log, _TAR.CREATE_UID);
                                        #endregion

                                        result.RETURN_FLAG = true;
                                        result.DESCRIPTION = Ref.MessageType.Apply_Audit_Success.GetDescription(null, $@"單號為{_TAR.APLY_NO}");
                                    }
                                    catch (DbUpdateException ex)
                                    {
                                        result.DESCRIPTION = ex.exceptionMessage();
                                    }
                                }
                                #endregion
                            }
                        }
                        else
                        {
                            result.DESCRIPTION = Ref.MessageType.not_Find_Audit_Data.GetDescription();
                        }
                    }
                    else //新增申請單
                    {
                        //取出只抓狀態為預約取出的資料
                        if (taData.vAccessType == Ref.AccessProjectTradeType.G.ToString())
                        {
                            datas = datas.Where(x => x.vStatus == Ref.AccessInventoryType._4.GetDescription()).ToList();
                        }
                        if (datas.Any())
                        {
                            using (TreasuryDBEntities db = new TreasuryDBEntities())
                            {
                                #region 申請單紀錄檔 & 申請單歷程檔

                                var data = SaveTREA_APLY_REC(db, taData, logStr, dt);

                                logStr = data.Item2;

                                #endregion

                                string _ITEM_BLANK_NOTE_ITEM_ID = null; //紀錄空白票據申請資料檔 對應空白票據庫存資料檔 物品編號

                                bool _changFlag = false;
                                datas.ForEach(x =>
                                {
                                    #region 取出時要把空白票據資料 做切段動作
                                    if (taData.vAccessType == Ref.AccessProjectTradeType.G.ToString()) //取出時要把空白票據資料 做切段動作
                                    {
                                        var _blank_Note = db.ITEM_BLANK_NOTE.FirstOrDefault(y => y.ITEM_ID == x.vItemId);
                                        if (_blank_Note != null)
                                        {
                                            if (_blank_Note.LAST_UPDATE_DT > x.vLast_Update_Time || _blank_Note.INVENTORY_STATUS != "1")
                                            {
                                                _changFlag = true;
                                            }
                                            else
                                            {
                                                _ITEM_BLANK_NOTE_ITEM_ID = _blank_Note.ITEM_ID;
                                                //全部取出
                                                if (x.vTakeOutE == _blank_Note.CHECK_NO_E)
                                                {
                                                    _blank_Note.INVENTORY_STATUS = "4"; //預約取出
                                                }
                                                //分段取出
                                                else
                                                {
                                                    _blank_Note.CHECK_NO_B = (TypeTransfer.stringToInt(x.vTakeOutE) + 1).ToString().PadLeft(7, '0');
                                                }
                                                _blank_Note.LAST_UPDATE_DT = dt;
                                            }
                                        }
                                        else
                                        {
                                            _changFlag = true;
                                        }
                                    }
                                    #endregion

                                    #region 空白票據申請資料檔
                                    var item_id = sysSeqDao.qrySeqNo(item_Seq, string.Empty).ToString().PadLeft(8, '0');
                                    var _BNA    = new BLANK_NOTE_APLY()
                                    {
                                        APLY_NO                 = data.Item1,
                                        ITEM_ID                 = $@"{item_Seq}{item_id}",
                                        CHECK_TYPE              = x.vCheckType,
                                        ISSUING_BANK            = x.vIssuingBank,
                                        CHECK_NO_TRACK          = x.vCheckNoTrack,
                                        CHECK_NO_B              = x.vCheckNoB,
                                        CHECK_NO_E              = taData.vAccessType == Ref.AccessProjectTradeType.P.ToString() ? x.vCheckNoE : x.vTakeOutE,
                                        ITEM_BLANK_NOTE_ITEM_ID = _ITEM_BLANK_NOTE_ITEM_ID
                                    };
                                    db.BLANK_NOTE_APLY.Add(_BNA);
                                    logStr += _BNA.modelToString(logStr);
                                    #endregion
                                });
                                if (_changFlag)
                                {
                                    result.DESCRIPTION = Ref.MessageType.already_Change.GetDescription();
                                    return(result);
                                }
                                #region Save Db

                                var validateMessage = db.GetValidationErrors().getValidateString();
                                if (validateMessage.Any())
                                {
                                    result.DESCRIPTION = validateMessage;
                                }
                                else
                                {
                                    try
                                    {
                                        db.SaveChanges();

                                        #region LOG
                                        //新增LOG
                                        Log log = new Log();
                                        log.CFUNCTION = "申請覆核-新增空白票據";
                                        log.CACTION   = "A";
                                        log.CCONTENT  = logStr;
                                        LogDao.Insert(log, taData.vCreateUid);
                                        #endregion

                                        result.RETURN_FLAG = true;
                                        result.DESCRIPTION = Ref.MessageType.Apply_Audit_Success.GetDescription(null, $@"單號為{data.Item1}");
                                    }
                                    catch (DbUpdateException ex)
                                    {
                                        result.DESCRIPTION = ex.exceptionMessage();
                                    }
                                }
                                #endregion
                            }
                        }
                        else
                        {
                            result.DESCRIPTION = Ref.MessageType.not_Find_Audit_Data.GetDescription();
                        }
                    }
                }
                else
                {
                    result.DESCRIPTION = Ref.MessageType.not_Find_Audit_Data.GetDescription();
                }
            }
            catch (Exception ex)
            {
                result.DESCRIPTION = ex.exceptionMessage();
            }

            return(result);
        }