Exemple #1
0
        public xml_response Reverse(string docid)
        {
            _res = new xml_response();
            _res.function_name = this.ToString() + string.Format(".Reverse({0})", docid);
            if (logger.IsDebugEnabled)
            {
                logger.Debug(string.Format("Start function: {0}", _res.function_name));
            }

            try
            {
                Tranday_Info tranInfo = _dalTranday.GetOneTranday(docid);
                if (tranInfo == null)
                {
                    _res.SetError("40", string.Format("Transaction {0} does not exists", docid));
                    if (logger.IsErrorEnabled)
                    {
                        logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                    }
                    return(_res);
                }
                if (tranInfo.Verified == false)
                {
                    _res.SetError("42", string.Format("{0} transaction are not verified", docid));
                    if (logger.IsErrorEnabled)
                    {
                        logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                    }
                    return(_res);
                }
                if (string.IsNullOrEmpty(_docId))
                {
                    if (tranInfo.AllowReverse == false)
                    {
                        _res.SetError("44", string.Format("Transaction {0} does not allow reverse", docid));
                        if (logger.IsErrorEnabled)
                        {
                            logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                        }
                        return(_res);
                    }
                }
                Tranday_Info oldTran = tranInfo;
                // cập nhật hồ sơ giao dịch không cho cho phép đảo ngược nữa
                tranInfo.AllowReverse = false;
                _dalTranday.EditOneTranday(tranInfo);
                tranInfo.Descript  = string.Format("REVERSE: {0}", docid);
                tranInfo.DocID     = base.GenerateDocId();
                tranInfo.Trace     = tranInfo.DocID;
                tranInfo.OtherRef  = docid;
                tranInfo.NextDocId = "";
                tranInfo.Status    = TransactionStatus.Approved;

                _transdate = BaseParameters.ToDay().TransDate;   // Lấy thông tin ngày giao dịch

                tranInfo.TransDate     = _transdate;
                tranInfo.ValueDate     = _transdate;
                tranInfo.Verified      = true;
                tranInfo.Verified_User = _channel.UserLogin;
                tranInfo.UserCreate    = _channel.UserLogin;

                _dalTranday.CreateOneTranday(tranInfo);

                Account_Info    acInfo;
                D_TrandayDetail dtd = new D_TrandayDetail();
                foreach (TrandayDetail_Info tdi in tranInfo.TrandayDetails)
                {
                    // đổi dấu số dư hạch toán.
                    tdi.CR_Amount *= -1;
                    tdi.DB_Amount *= -1;
                    // lấy thông tin tài khoản hạch toán
                    acInfo = _dalAc.GetOneAccount(tdi.Account_ID);
                    // kiểm tra luật hạch toán
                    if (tdi.DB_Amount != 0)
                    {
                        if (!CheckAccountRules(acInfo, CreditDebit.DB, tdi.DB_Amount))
                        {
                            _res.SetError("15", _msgRule);
                            if (logger.IsErrorEnabled)
                            {
                                logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                            }
                            _dalTranday.ClearCommand();
                            return(_res);
                        }
                    }
                    if (tdi.CR_Amount != 0)
                    {
                        if (!CheckAccountRules(acInfo, CreditDebit.CR, tdi.CR_Amount))
                        {
                            _res.SetError("15", _msgRule);
                            if (logger.IsErrorEnabled)
                            {
                                logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                            }
                            _dalTranday.ClearCommand();
                            return(_res);
                        }
                    }

                    // ghi có tài khoản.
                    acInfo.y_Credit += tdi.CR_Amount;
                    acInfo.q_Credit += tdi.CR_Amount;
                    acInfo.m_Credit += tdi.CR_Amount;
                    acInfo.w_Credit += tdi.CR_Amount;
                    acInfo.d_Credit += tdi.CR_Amount;
                    // ghi nợ tài khoản
                    acInfo.y_Debit += tdi.DB_Amount;
                    acInfo.q_Debit += tdi.DB_Amount;
                    acInfo.m_Debit += tdi.DB_Amount;
                    acInfo.w_Debit += tdi.DB_Amount;
                    acInfo.d_Debit += tdi.DB_Amount;
                    // cập nhật số dư
                    tdi.BalanceAvaiable = acInfo.BalanceAvaiable;
                    tdi.DocID           = tranInfo.DocID;
                    // cập nhật thời gian
                    acInfo.Last_Date = DateTime.Now;
                    // thực hiện câu lệnh
                    _dalTranday.AddCommand(dtd.CreateOneTrandayDetail(tdi));
                    _dalTranday.AddCommand(_dalAc.EditOneAccount(acInfo));
                }
                if (string.IsNullOrEmpty(_docId))
                {
                    _docId = tranInfo.DocID;
                }
                if (!string.IsNullOrEmpty(oldTran.NextDocId))
                {
                    Reverse(oldTran.NextDocId);
                }
                else
                {
                    if (!_dalTranday.Execute())
                    {
                        _res.SetError("99", _dalTranday.GetException.Message);
                        if (logger.IsErrorEnabled)
                        {
                            logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                        }
                        _docId = String.Empty;
                        _dalTranday.ClearCommand();
                    }
                    else
                    {
                        //_res.doc_id = ti.DocID;
                        _res.Transactions = base.GetTrandayById(_docId).RenderXML();
                        _docId            = String.Empty;
                    }
                }
            }
            catch (Exception ex)
            {
                _res.SetError("99", ex.Message);
                if (logger.IsErrorEnabled)
                {
                    logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                }
                _docId = String.Empty;
                _dalTranday.ClearCommand();
            }
            return(_res);
        }
Exemple #2
0
        public xml_response Retail(string trancode, string accountId, decimal amount, string descript)
        {
            try
            {
                _res = new xml_response();
                _res.function_name = this.ToString() +
                                     string.Format(".Retail({0}, {1}, {2},{3})", trancode, accountId, amount, descript);
                if (logger.IsDebugEnabled)
                {
                    logger.Debug(string.Format("Start function: {0}", _res.function_name));
                }
                // kiểm tra trancode
                Trancode_Info trancodeInfo = _dalTrancode.GetOneTranCode(trancode);
                if (trancodeInfo == null)
                {
                    _res.SetError("99", string.Format("Trancode Id: {0} not found", trancode));
                    if (logger.IsErrorEnabled)
                    {
                        logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                    }
                    return(_res);
                }
                if (trancodeInfo.CodeType == CodeType.FundTransfer)
                {
                    _res.SetError("99", string.Format("Trancode Id: {0} invalid code type", trancode));
                    if (logger.IsErrorEnabled)
                    {
                        logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                    }
                    return(_res);
                }

                Tranday_Info trandayInfo = new Tranday_Info();
                _transdate               = BaseParameters.ToDay().TransDate;
                trandayInfo.DocID        = GenerateDocId(); // tạo số chứng từ mới
                trandayInfo.Trace        = trandayInfo.DocID;
                trandayInfo.TransDate    = _transdate;
                trandayInfo.ValueDate    = _transdate;
                trandayInfo.Status       = TransactionStatus.Approved;
                trandayInfo.Branch_ID    = _channel.Branch;
                trandayInfo.AllowReverse = trancodeInfo.AllowReverse;
                if (string.IsNullOrEmpty(descript))
                {
                    trandayInfo.Descript = string.Format("Retail: {0} with amount={1}", accountId, amount);
                }
                else
                {
                    trandayInfo.Descript = descript;
                }
                trandayInfo.NextDocId     = "";
                trandayInfo.OtherRef      = "";
                trandayInfo.TranCode      = trancode;
                trandayInfo.UserCreate    = _channel.UserLogin;
                trandayInfo.Verified      = true;
                trandayInfo.Verified_User = _channel.UserLogin;

                _dalTranday.CreateOneTranday(trandayInfo);

                // lấy mã giao dịch được định nghĩa cách hạch toán
                List <TranCodeDetail_Info> list = _dalTrancode.GetTranCodeDetailByCode(trancode);
                if (list == null)
                {
                    _res.SetError("30", string.Format("Can't load trancodedetail: {0}", trancode));
                    if (logger.IsErrorEnabled)
                    {
                        logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                    }
                    _dalTranday.ClearCommand();
                    return(_res);
                }
                D_Account           dalAc = new D_Account();
                TrandayDetail_Info  tid;
                List <Account_Info> AcList;
                Account_Info        tmpAc;
                D_TrandayDetail     trand       = new D_TrandayDetail();
                decimal             remain_amnt = 0; // số tiền còn lại
                decimal             db_amnt;
                decimal             cr_amnt;
                decimal             sum_db_amnt = 0;
                decimal             sum_cr_amnt = 0;
                bool find_ac = false;

                tmpAc = dalAc.GetOneAccount(accountId);
                if (tmpAc == null)
                {
                    _res.SetError("11", string.Format("{0} does not exists", accountId));
                    if (logger.IsErrorEnabled)
                    {
                        logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                    }
                    _dalTranday.ClearCommand();
                    return(_res);
                }
                #region Build Trandaydetail

                foreach (TranCodeDetail_Info tcdi in list)
                {
                    AcList  = new List <Account_Info>();
                    tid     = new TrandayDetail_Info();
                    db_amnt = 0;
                    cr_amnt = 0;

                    #region Find Account_ID

                    if (tcdi.Is_Account_Cust)
                    {
                        // là tài khoản khách hàng
                        AcList = dalAc.GetListAccountLike(tcdi.Account_ID);
                        foreach (Account_Info acInfo in AcList)
                        {
                            if (accountId == acInfo.Account_ID)
                            {
                                find_ac = true;
                                break;
                            }
                        }
                        if (find_ac == false)
                        {
                            // không xác định được nhóm tài khoản khách hàng
                            _res.SetError("31", string.Format("{0} not in group {1}", accountId, tcdi.Account_ID));
                            if (logger.IsErrorEnabled)
                            {
                                logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                            }
                            _dalTranday.ClearCommand();
                            return(_res);
                        }
                    }
                    else
                    {
                        // là tài khoản đã được định danh chi tiết
                        AcList = dalAc.GetListAccountLike(tcdi.Account_ID);
                        if (AcList.Count > 1)
                        {
                            // nếu xác định được nhiều tài khoản thì lỗi
                            _res.SetError("31", string.Format("{0} does too many accounts", tcdi.Account_ID));
                            if (logger.IsErrorEnabled)
                            {
                                logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                            }
                            _dalTranday.ClearCommand();
                            return(_res);
                        }
                        if (AcList.Count == 0)
                        {
                            // không xác định được tài khoản
                            _res.SetError("31", string.Format("{0} Invalid account id", tcdi.Account_ID));
                            if (logger.IsErrorEnabled)
                            {
                                logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                            }
                            _dalTranday.ClearCommand();
                            return(_res);
                        }
                        tmpAc = AcList[0];
                    }

                    #endregion

                    tid.Account_ID = tmpAc.Account_ID;
                    tid.Ccy        = tmpAc.Ccy;
                    tid.SEQ        = tcdi.SEQ;

                    if (!tcdi.Master)
                    {
                        switch (tcdi.NumberType)
                        {
                        case NumberType.FixAmount:
                            // Kiểm tra số dư fix
                            if ((decimal)tcdi.NumberValue > amount - remain_amnt)
                            {
                                _res.SetError("",
                                              string.Format("fix amount is {0} greater than {1}", tcdi.NumberValue,
                                                            amount - remain_amnt));
                                if (logger.IsErrorEnabled)
                                {
                                    logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                                }
                                _dalTranday.ClearCommand();
                                return(_res);
                            }
                            if (tcdi.CreditDebit == CreditDebit.DB)
                            {
                                db_amnt = (decimal)tcdi.NumberValue;
                            }
                            else
                            {
                                cr_amnt = (decimal)tcdi.NumberValue;
                            }
                            break;

                        case NumberType.Percentage:
                            if (tcdi.CreditDebit == CreditDebit.DB)
                            {
                                db_amnt = amount * (decimal)tcdi.NumberValue;
                            }
                            else
                            {
                                cr_amnt = amount * (decimal)tcdi.NumberValue;
                            }
                            break;

                        default:
                            if (tcdi.CreditDebit == CreditDebit.DB)
                            {
                                db_amnt = amount - remain_amnt;
                            }
                            else
                            {
                                cr_amnt = amount - remain_amnt;
                            }
                            break;
                        }
                        if (tcdi.CreditDebit == CreditDebit.DB)
                        {
                            remain_amnt += db_amnt;
                        }
                        else
                        {
                            remain_amnt += cr_amnt;
                        }
                    }
                    else
                    {
                        if (tcdi.CreditDebit == CreditDebit.DB)
                        {
                            db_amnt = amount;
                        }
                        else
                        {
                            cr_amnt = amount;
                        }
                    }
                    tid.DB_Amount = db_amnt;
                    tid.CR_Amount = cr_amnt;
                    tid.DocID     = trandayInfo.DocID;
                    // ===============================
                    // kiểm tra điều kiện hạch toán ==
                    // ===============================
                    decimal temp_amnt = 0;
                    if (tcdi.CreditDebit == CreditDebit.DB)
                    {
                        temp_amnt = tid.DB_Amount;
                    }
                    else
                    {
                        temp_amnt = tid.CR_Amount;
                    }
                    if (!CheckAccountRules(tmpAc, tcdi.CreditDebit, temp_amnt))
                    {
                        _res.SetError("15", _msgRule);
                        if (logger.IsErrorEnabled)
                        {
                            logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                        }
                        _dalTranday.ClearCommand();
                        return(_res);
                    }
                    // ===============================
                    // ghi nợ tài khoản
                    tmpAc.d_Debit += tid.DB_Amount;
                    tmpAc.w_Debit += tid.DB_Amount;
                    tmpAc.m_Debit += tid.DB_Amount;
                    tmpAc.q_Debit += tid.DB_Amount;
                    tmpAc.y_Debit += tid.DB_Amount;
                    // ghi có tài khoản.
                    tmpAc.d_Credit += tid.CR_Amount;
                    tmpAc.w_Credit += tid.CR_Amount;
                    tmpAc.m_Credit += tid.CR_Amount;
                    tmpAc.q_Credit += tid.CR_Amount;
                    tmpAc.y_Credit += tid.CR_Amount;

                    tmpAc.Last_Date = DateTime.Now;
                    // Cập nhật số dư mới
                    tid.BalanceAvaiable = tmpAc.BalanceAvaiable;
                    // thực hiện câu lệnh
                    // Kiểm tra điều kiện số dư hạch toán nợ/có
                    if ((tid.DB_Amount == 0) && (tid.CR_Amount == 0))
                    {
                        break;
                    }
                    else
                    {
                        _dalTranday.AddCommand(trand.CreateOneTrandayDetail(tid));
                        _dalTranday.AddCommand(dalAc.EditOneAccount(tmpAc));
                        // ghi nhận tổng nợ và tổng có
                        sum_db_amnt += db_amnt;
                        sum_cr_amnt += cr_amnt;
                    }
                } // end forearch

                #endregion

                // với bút toán nhiều chân thì kiểm tra cân đối tổng nợ và tổng có
                if (list.Count > 1)
                {
                    if (sum_cr_amnt != sum_db_amnt)
                    {
                        // không cân đối giữa tổng nợ và tổng có
                        _res.SetError("52",
                                      string.Format("total debit ({0}) and total credit ({1}) is difference",
                                                    sum_db_amnt,
                                                    sum_cr_amnt));
                        if (logger.IsErrorEnabled)
                        {
                            logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                        }
                        _dalTranday.ClearCommand();
                        return(_res);
                    }
                }
                if (string.IsNullOrEmpty(_docId))
                {
                    _docId = trandayInfo.DocID;
                }
                if (!string.IsNullOrEmpty(trancodeInfo.NextCode))
                {
                    Retail(trancodeInfo.NextCode, accountId, amount, descript);
                }
                else
                // ghi nhận hạch toán
                if (_dalTranday.Execute())
                // Lấy thông tin giao dịch
                {
                    _res.Transactions = base.GetTrandayById(_docId).RenderXML();
                    _docId            = String.Empty;
                }
                else
                {
                    _res.SetError("99", _dalTranday.GetException.Message);
                    if (logger.IsErrorEnabled)
                    {
                        logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                    }
                    _docId = String.Empty;
                    _dalTranday.ClearCommand();
                }
            }
            catch (Exception ex)
            {
                _res.SetError("99", ex.Message);
                if (logger.IsErrorEnabled)
                {
                    logger.Error(ex);
                }
                _docId = String.Empty;
                _dalTranday.ClearCommand();
            }
            return(_res);
        }
Exemple #3
0
        public xml_response Fundtransfer(string trancode, string fromAccount, string toAccount, decimal amount, string descript)
        {
            try
            {
                _res = new xml_response();
                _res.function_name = this.ToString() +
                                     string.Format(".Fundtransfer({0}, {1}, {2}, {3},{4})", trancode, fromAccount,
                                                   toAccount, amount, descript);
                if (logger.IsDebugEnabled)
                {
                    logger.Debug(string.Format("Start function: {0}", _res.function_name));
                }
                // kiểm tra trancode
                Trancode_Info trancodeInfo = _dalTrancode.GetOneTranCode(trancode);
                if (trancodeInfo == null)
                {
                    _res.SetError("99", string.Format("Trancode Id: {0} not found", trancode));
                    if (logger.IsErrorEnabled)
                    {
                        logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                    }
                    return(_res);
                }
                if (trancodeInfo.CodeType != CodeType.FundTransfer)
                {
                    _res.SetError("99", string.Format("Trancode Id: {0} invalid code type", trancode));
                    if (logger.IsErrorEnabled)
                    {
                        logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                    }
                    return(_res);
                }

                // kiểm tra tài khoản đã có hay chưa
                string db_account = fromAccount;
                string cr_account = toAccount;

                if (_dalAc.GetOneAccount(db_account) == null)
                {
                    _res.SetError("11", string.Format("From account {0} does not found", db_account));
                    if (logger.IsErrorEnabled)
                    {
                        logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                    }
                    return(_res);
                }
                if (_dalAc.GetOneAccount(cr_account) == null)
                {
                    _res.SetError("11", string.Format("From account {0} does not found", cr_account));
                    if (logger.IsErrorEnabled)
                    {
                        logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                    }
                    return(_res);
                }
                // kiểm tra số dư chuyển khoản.
                Tranday_Info trandayInfo = new Tranday_Info();
                trandayInfo.AllowReverse = trancodeInfo.AllowReverse;
                trandayInfo.Branch_ID    = _channel.Branch;
                if (string.IsNullOrEmpty(descript))
                {
                    trandayInfo.Descript = string.Format("transfer from {0} to {1} with amount = {2}", db_account, cr_account, amount);
                }
                else
                {
                    trandayInfo.Descript = descript;
                }
                trandayInfo.NextDocId = "";
                trandayInfo.Status    = TransactionStatus.Approved;

                _transdate = BaseParameters.ToDay().TransDate;

                trandayInfo.TranCode      = trancode;
                trandayInfo.TransDate     = _transdate;
                trandayInfo.UserCreate    = _channel.UserLogin;
                trandayInfo.ValueDate     = _transdate;
                trandayInfo.Verified      = true;
                trandayInfo.Verified_User = _channel.UserLogin;
                trandayInfo.DocID         = base.GenerateDocId();
                trandayInfo.Trace         = trandayInfo.DocID;

                _dalTranday.CreateOneTranday(trandayInfo);

                D_TrandayDetail     dalTrandaydetail;
                TrandayDetail_Info  trandaydetailInfo;
                Account_Info        ai                       = new Account_Info();
                List <Account_Info> ac_list                  = new List <Account_Info>();
                bool                       find_ac           = false;
                D_TranCodeDetail           dalTrancodedetail = new D_TranCodeDetail();
                List <TranCodeDetail_Info> list              = dalTrancodedetail.GetTranCodeDetailByCode(trancode);
                if (list == null)
                {
                    _res.SetError("30", string.Format("Can't load trancodedetail: {0}", trancode));
                    if (logger.IsErrorEnabled)
                    {
                        logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                    }
                    _dalTranday.ClearCommand();
                    return(_res);
                }
                foreach (TranCodeDetail_Info tcdi in list)
                {
                    dalTrandaydetail  = new D_TrandayDetail();
                    trandaydetailInfo = new TrandayDetail_Info();
                    switch (tcdi.CreditDebit)
                    {
                    case  CreditDebit.DB:
                        ac_list = _dalAc.GetListAccountLike(tcdi.Account_ID);
                        foreach (Account_Info acInfo in ac_list)
                        {
                            if (db_account == acInfo.Account_ID)
                            {
                                find_ac = true;
                                break;
                            }
                        }
                        if (find_ac == false)
                        {
                            // không xác định được nhóm tài khoản khách hàng
                            _res.SetError("31", string.Format("{0} not in group {1}", db_account, tcdi.Account_ID));
                            if (logger.IsErrorEnabled)
                            {
                                logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                            }
                            _dalTranday.ClearCommand();
                            return(_res);
                        }
                        trandaydetailInfo.Account_ID = db_account;
                        trandaydetailInfo.DB_Amount  = amount;
                        ai = _dalAc.GetOneAccount(trandaydetailInfo.Account_ID);
                        // kiểm tra luật hạch toán
                        if (!CheckAccountRules(ai, CreditDebit.DB, amount))
                        {
                            _res.SetError("15", _msgRule);
                            _dalTranday.ClearCommand();
                            return(_res);
                        }
                        // ghi nợ tài khoản.
                        ai.y_Debit           += amount;
                        ai.q_Debit           += amount;
                        ai.m_Debit           += amount;
                        ai.w_Debit           += amount;
                        ai.d_Debit           += amount;
                        trandaydetailInfo.Ccy = ai.Ccy;
                        break;

                    case CreditDebit.CR:
                        ac_list = _dalAc.GetListAccountLike(tcdi.Account_ID);
                        foreach (Account_Info acInfo in ac_list)
                        {
                            if (cr_account == acInfo.Account_ID)
                            {
                                find_ac = true;
                                break;
                            }
                        }
                        if (find_ac == false)
                        {
                            // không xác định được nhóm tài khoản khách hàng
                            _res.SetError("31", string.Format("{0} not in group {1}", cr_account, tcdi.Account_ID));
                            if (logger.IsErrorEnabled)
                            {
                                logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                            }
                            _dalTranday.ClearCommand();
                            return(_res);
                        }
                        trandaydetailInfo.Account_ID = cr_account;
                        trandaydetailInfo.CR_Amount  = amount;
                        ai = _dalAc.GetOneAccount(trandaydetailInfo.Account_ID);
                        // kiểm tra luật hạch toán
                        if (!CheckAccountRules(ai, CreditDebit.CR, amount))
                        {
                            _res.SetError("15", _msgRule);
                            _dalTranday.ClearCommand();
                            return(_res);
                        }
                        // ghi có tài khoản.
                        ai.y_Credit          += amount;
                        ai.q_Credit          += amount;
                        ai.m_Credit          += amount;
                        ai.w_Credit          += amount;
                        ai.d_Credit          += amount;
                        trandaydetailInfo.Ccy = ai.Ccy;
                        break;
                    }
                    trandaydetailInfo.SEQ   = tcdi.SEQ;
                    trandaydetailInfo.DocID = trandayInfo.DocID;
                    // cập nhật số dư mới
                    trandaydetailInfo.BalanceAvaiable = ai.BalanceAvaiable;
                    // cập nhật thời gian
                    ai.Last_Date = DateTime.Now;
                    // thực hiện câu lệnh
                    _dalTranday.AddCommand(dalTrandaydetail.CreateOneTrandayDetail(trandaydetailInfo));
                    _dalTranday.AddCommand(_dalAc.EditOneAccount(ai));
                }
                if (string.IsNullOrEmpty(_docId))
                {
                    _docId = trandayInfo.DocID;
                }
                if (!string.IsNullOrEmpty(trancodeInfo.NextCode))
                {
                    Fundtransfer(trancodeInfo.NextCode, fromAccount, toAccount, amount, descript);
                }
                else
                // ghi nhận hạch toán
                if (!_dalTranday.Execute())
                {
                    _res.SetError("99", _dalTranday.GetException.Message);
                    if (logger.IsErrorEnabled)
                    {
                        logger.Error(string.Format("{0}\t{1}", _res.error_code, _res.error_msg));
                    }
                    _docId = String.Empty;
                    _dalTranday.ClearCommand();
                }
                else
                // Lấy thông tin giao dịch
                {
                    _res.Transactions = base.GetTrandayById(_docId).RenderXML();
                    _docId            = String.Empty;
                }
            }
            catch (Exception ex)
            {
                _res.SetError("99", ex.Message);
                if (logger.IsErrorEnabled)
                {
                    logger.Error(ex);
                }
                _docId = String.Empty;
                _dalTranday.ClearCommand();
            }
            return(_res);
        }
Exemple #4
0
        /// <summary>
        /// tạo mới giao dịch mua hàng
        /// </summary>
        /// <param name="docid">số chứng từ hạch toán (phải duy nhất trong giai đoạn mở sổ)</param>
        /// <param name="account_id">tài khoản ghi nợ của khách hàng mua hàng</param>
        /// <param name="amount">số tiền mua hàng</param>
        /// <param name="descript">mô tả chi tiết giao dịch mua hàng</param>
        /// <returns>trả về 1 nếu thành công và 0 nếu không thành công</returns>
        public ResponseMessage Retail(string trace, string pan, decimal amount, string descript)
        {
            ResponseMessage msg = new ResponseMessage();

            msg.LocalDate   = DateTime.Now.ToString("MMdd");
            msg.LocalTime   = DateTime.Now.ToString("hhmmss");
            msg.SystemTrace = trace;

            D_Tranday dt = new D_Tranday();

            // kiểm tra số chứng từ đã có hay chưa
            if (dt.IsExists("", trace))
            {
                logger.Error(string.Format("Transaction {0} does exists", trace));
                msg.ResponseCode = "12";
                return(msg);
            }

            Tranday_Info ti = new Tranday_Info();

            // tạo lại số chứng từ mới
            ti.DocID = base.GenerateDocId();

            msg.RetrievalRefNum = ti.DocID;

            ti.Trace         = trace;
            ti.TransDate     = transdate;
            ti.ValueDate     = transdate;
            ti.Status        = TransactionStatus.Approved;
            ti.Branch_ID     = branch_id;
            ti.AllowReverse  = true;
            ti.Descript      = descript;
            ti.NextDocId     = "";
            ti.OtherRef      = "";
            ti.TranCode      = retail_trancode;
            ti.UserCreate    = user_id;
            ti.Verified      = true;
            ti.Verified_User = user_id;
            dt.CreateOneTranday(ti);
            // lấy mã giao dịch được định nghĩa cách hạch toán
            D_TranCodeDetail           dtd  = new D_TranCodeDetail();
            List <TranCodeDetail_Info> list = dtd.GetTranCodeDetailByCode(retail_trancode);

            if (list == null)
            {
                if (logger.IsErrorEnabled)
                {
                    logger.Error(string.Format("Can't load trancodedetail: {0}", retail_trancode));
                }
                msg.ResponseCode = "06";
                return(msg);
            }
            D_Account           da = new D_Account();
            TrandayDetail_Info  tid;
            List <Account_Info> ac_list;
            Account_Info        tmp_ac;
            D_TrandayDetail     trand = new D_TrandayDetail();

            #region Build Trandaydetail
            foreach (TranCodeDetail_Info tcdi in list)
            {
                ac_list = new List <Account_Info>();
                tid     = new TrandayDetail_Info();
                #region Xác định tài khoản hạch toán
                if (tcdi.Is_Account_Cust)
                {
                    // là tài khoản khách hàng
                    // so sánh tài khoản khách hàng có thuộc nhóm tài khoản định nghĩa.
                    // tài khoản chi tiết: 10100099992222
                    // thuộc nhóm tài khoản 101000
                    if (pan.IndexOf(tcdi.Account_ID) != 0)
                    {
                        // không xác định được nhóm tài khoản khách hàng
                        if (logger.IsErrorEnabled)
                        {
                            logger.Error(string.Format("{0} not in group {1}", pan, tcdi.Account_ID));
                        }
                        msg.ResponseCode = "14";    // Invalid card number
                        return(msg);
                    }
                    tmp_ac = da.GetOneAccount(pan);
                    if (tmp_ac == null)
                    {
                        if (logger.IsErrorEnabled)
                        {
                            logger.Error(string.Format("{0} does not exists", pan));
                        }
                        msg.ResponseCode = "14";    // Invalid card number
                        return(msg);
                    }
                }
                else
                {
                    // là tài khoản nội bộ
                    ac_list = da.GetListAccountLike(tcdi.Account_ID);
                    // nếu xác định được nhiều tài khoản thì lỗi
                    if (ac_list.Count > 1)
                    {
                        if (logger.IsErrorEnabled)
                        {
                            logger.Error(string.Format("{0} does too many accounts", tcdi.Account_ID));
                        }
                        msg.ResponseCode = "14";    // Invalid card number
                        return(msg);
                    }
                    tmp_ac = ac_list[0];
                }
                #endregion
                tid.Account_ID = tmp_ac.Account_ID;
                tid.Ccy        = tmp_ac.Ccy;
                tid.SEQ        = tcdi.SEQ;
                if (!string.IsNullOrEmpty(tcdi.NumberType.ToString()))
                {
                    switch (tcdi.NumberType)
                    {
                    case NumberType.FixAmount:
                        if (tcdi.CreditDebit == CreditDebit.DB)
                        {
                            tid.DB_Amount = (decimal)tcdi.NumberValue;
                        }
                        else
                        {
                            tid.CR_Amount = (decimal)tcdi.NumberValue;
                        }
                        break;

                    case NumberType.Percentage:
                        if (tcdi.CreditDebit == CreditDebit.DB)
                        {
                            tid.DB_Amount = amount * (decimal)tcdi.NumberValue;
                        }
                        else
                        {
                            tid.CR_Amount = amount * (decimal)tcdi.NumberValue;
                        }
                        break;

                    default:
                        if (tcdi.CreditDebit == CreditDebit.DB)
                        {
                            tid.DB_Amount = amount;
                        }
                        else
                        {
                            tid.CR_Amount = amount;
                        }
                        break;
                    }
                }
                else
                {
                    if (tcdi.CreditDebit == CreditDebit.DB)
                    {
                        tid.DB_Amount = amount;
                    }
                    else
                    {
                        tid.CR_Amount = amount;
                    }
                }
                tid.DocID = ti.DocID;
                // cập nhật thông tin tài khoản
                tmp_ac.d_Debit += tid.DB_Amount;
                tmp_ac.w_Debit += tid.DB_Amount;
                tmp_ac.m_Debit += tid.DB_Amount;
                tmp_ac.q_Debit += tid.DB_Amount;
                tmp_ac.y_Debit += tid.DB_Amount;

                tmp_ac.d_Credit += tid.CR_Amount;
                tmp_ac.w_Credit += tid.CR_Amount;
                tmp_ac.m_Credit += tid.CR_Amount;
                tmp_ac.q_Credit += tid.CR_Amount;
                tmp_ac.y_Credit += tid.CR_Amount;

                dt.AddCommand(trand.CreateOneTrandayDetail(tid));
                dt.AddCommand(da.EditOneAccount(tmp_ac));
            }
            #endregion
            if (dt.Execute() == true)
            {
                // thực hiện giao dịch thành công
                if (logger.IsDebugEnabled)
                {
                    logger.Debug(string.Format("Done create new one retail transaction: {0}", ti.DocID));
                }
                msg.ResponseCode = "00";    // Approved
                return(msg);
            }
            else
            {
                // thực hiện giao dịch không thành công
                if (logger.IsErrorEnabled)
                {
                    logger.Error(string.Format("Fails create new one retail transaction: {0}", ti.DocID));
                }
                msg.ResponseCode = "06";    // Error
                return(msg);
            }
        }
Exemple #5
0
        /// <summary>
        /// tạo mới một giao dịch đảo ngược
        /// thêm một bản ghi vào bảng tranday
        /// thêm một bản ghi vào bảng trandaydetail
        /// nhưng với số tiền ghi âm mục đich triệt tiêu doanh số
        /// </summary>
        /// <param name="docid">
        /// số chứng từ hay mã số giao dịch cần đảo ngược
        /// hệ thống sẽ sinh ra số chứng từ mới hạch toán âm
        /// với số chứng từ là số chứng từ cần đảo ngược cộng với 2 ký tự ".1"
        /// </param>
        /// <returns>
        /// giá trị trả về là số giao dịch được tạo (1)
        /// nếu lỗi trả về giá trị 0
        /// </returns>
        public ResponseMessage Reverse(string trace, string docid)
        {
            ResponseMessage msg = new ResponseMessage();

            try
            {
                msg.LocalDate = DateTime.Now.ToString("MMdd");
                msg.LocalTime = DateTime.Now.ToString("hhmmss");

                D_Tranday    dt = new D_Tranday();
                Tranday_Info ti = dt.GetOneTranday(trace);
                if (ti == null)
                {
                    logger.Error(string.Format("Transaction {0} does not exists", trace));
                    msg.ResponseCode = "12";  // Invalid transaction
                    return(msg);
                }
                if (ti.AllowReverse == false)
                {
                    logger.Error(string.Format("Transaction {0} does not allow reverse", trace));
                    msg.ResponseCode = "06";  // Error
                    return(msg);
                }
                // cập nhật hồ sơ giao dịch không cho cho phép đảo ngược nữa
                ti.AllowReverse = false;
                dt.EditOneTranday(ti);
                ti.Descript      = string.Format("REVERSE: {0}", trace);
                ti.DocID         = base.GenerateDocId();
                ti.Trace         = trace;
                ti.OtherRef      = docid;
                ti.NextDocId     = "";
                ti.Status        = TransactionStatus.Approved;
                ti.TransDate     = transdate;
                ti.ValueDate     = transdate;
                ti.Verified      = true;
                ti.Verified_User = user_id;
                ti.UserCreate    = user_id;
                dt.CreateOneTranday(ti);
                D_Account       da = new D_Account();
                Account_Info    ai;
                D_TrandayDetail dtd = new D_TrandayDetail();
                foreach (TrandayDetail_Info tdi in ti.TrandayDetails)
                {
                    tdi.CR_Amount *= -1;
                    tdi.DB_Amount *= -1;
                    tdi.DocID      = ti.DocID;
                    dt.AddCommand(dtd.CreateOneTrandayDetail(tdi));

                    ai = da.GetOneAccount(tdi.Account_ID);
                    // ghi có tài khoản.
                    ai.y_Credit += tdi.CR_Amount;
                    ai.q_Credit += tdi.CR_Amount;
                    ai.m_Credit += tdi.CR_Amount;
                    ai.w_Credit += tdi.CR_Amount;
                    ai.d_Credit += tdi.CR_Amount;
                    // ghi nợ tài khoản.
                    ai.y_Debit += tdi.DB_Amount;
                    ai.q_Debit += tdi.DB_Amount;
                    ai.m_Debit += tdi.DB_Amount;
                    ai.w_Debit += tdi.DB_Amount;
                    ai.d_Debit += tdi.DB_Amount;
                    dt.AddCommand(da.EditOneAccount(ai));
                }
                if (dt.Execute())
                {
                    // nếu thực hiện thành công
                    if (logger.IsDebugEnabled)
                    {
                        logger.Debug(string.Format("Done create new merchant transaction: {0}", ti.DocID));
                    }
                    msg.ResponseCode = "00";  // Approved
                    return(msg);
                }
                else
                {
                    if (logger.IsErrorEnabled)
                    {
                        logger.Error(string.Format("Fails create new one merchant transaction: {0}", ti.DocID));
                    }
                    msg.ResponseCode = "06";  // Error
                    return(msg);
                }
            }
            catch (Exception ex)
            {
                logger.Error(ex);
                msg.ResponseCode = "06";  // Error
                return(msg);
            }
        }
Exemple #6
0
        /// <summary>
        /// chuyển khoản từ tài khoản db_account sang tài khoản cr_account
        /// (ghi nợ tài khoản db_account và ghi có tài khoản cr_account)
        /// </summary>
        /// <param name="amount">số tiền chuyển khoản</param>
        /// <param name="from_account">tài khoản ghi nợ</param>
        /// <param name="to_account">tài khoản ghi có</param>
        /// <returns>
        /// 0 không thành công
        /// 1 thành công
        /// </returns>
        public ResponseMessage FundTransfer(string trace, decimal amount, string db_account, string cr_account)
        {
            ResponseMessage msg = new ResponseMessage();

            msg.LocalDate = DateTime.Now.ToString("MMdd");
            msg.LocalTime = DateTime.Now.ToString("hhmmss");

            D_Tranday dt = new D_Tranday();

            // kiểm tra số chứng từ đã tồn tại hay chưa
            if (dt.IsExists("", trace))
            {
                logger.Error(string.Format("Transaction {0} does exists", trace));
                msg.ResponseCode = "12";    // Invalid transaction
                return(msg);
            }
            // kiểm tra hai tài khoản đã có hay chưa
            D_Account da = new D_Account();

            if (da.GetOneAccount(db_account) == null)
            {
                logger.Error(string.Format("From account {0} does not found", db_account));
                msg.ResponseCode = "78";    //No Account
                return(msg);
            }
            if (da.GetOneAccount(cr_account) == null)
            {
                logger.Error(string.Format("To account {0} does not found", cr_account));
                msg.ResponseCode = "78";    //No Account
                return(msg);
            }
            // kiểm tra số dư chuyển khoản.
            Tranday_Info ti = new Tranday_Info();

            ti.AllowReverse  = true;
            ti.Branch_ID     = branch_id;
            ti.Descript      = string.Format("transfer from {0} to {1} with amount = {2}", db_account, cr_account, amount);
            ti.NextDocId     = "";
            ti.Status        = TransactionStatus.Approved;
            ti.TranCode      = fundtransfer_trancode;
            ti.TransDate     = transdate;
            ti.UserCreate    = user_id;
            ti.ValueDate     = transdate;
            ti.Verified      = true;
            ti.Verified_User = user_id;
            ti.Trace         = trace;
            ti.DocID         = base.GenerateDocId();
            dt.CreateOneTranday(ti);
            D_TrandayDetail            dtd;
            TrandayDetail_Info         tdi;
            Account_Info               ai   = new Account_Info();
            D_TranCodeDetail           dcd  = new D_TranCodeDetail();
            List <TranCodeDetail_Info> list = dcd.GetTranCodeDetailByCode(fundtransfer_trancode);

            foreach (TranCodeDetail_Info tcdi in list)
            {
                dtd = new D_TrandayDetail();
                tdi = new TrandayDetail_Info();
                switch (tcdi.CreditDebit)
                {
                case  CreditDebit.DB:
                    if (db_account.IndexOf(tcdi.Account_ID) != 0)
                    {
                        // không xác định được nhóm tài khoản khách hàng
                        if (logger.IsErrorEnabled)
                        {
                            logger.Error(string.Format("{0} not in group {1}", db_account, tcdi.Account_ID));
                        }
                        msg.ResponseCode = "78";        //No Account
                        return(msg);
                    }
                    tdi.Account_ID = db_account;
                    tdi.DB_Amount  = amount;
                    ai             = da.GetOneAccount(tdi.Account_ID);
                    ai.y_Debit    += amount;
                    ai.q_Debit    += amount;
                    ai.m_Debit    += amount;
                    ai.w_Debit    += amount;
                    ai.d_Debit    += amount;
                    tdi.Ccy        = ai.Ccy;
                    break;

                case CreditDebit.CR:
                    if (cr_account.IndexOf(tcdi.Account_ID) != 0)
                    {
                        // không xác định được nhóm tài khoản khách hàng
                        if (logger.IsErrorEnabled)
                        {
                            logger.Error(string.Format("{0} not in group {1}", cr_account, tcdi.Account_ID));
                        }
                        msg.ResponseCode = "78";        //No Account
                        return(msg);
                    }
                    tdi.Account_ID = cr_account;
                    tdi.CR_Amount  = amount;
                    ai             = da.GetOneAccount(tdi.Account_ID);
                    ai.y_Credit   += amount;
                    ai.q_Credit   += amount;
                    ai.m_Credit   += amount;
                    ai.w_Credit   += amount;
                    ai.d_Credit   += amount;
                    tdi.Ccy        = ai.Ccy;
                    break;
                }
                tdi.SEQ   = tcdi.SEQ;
                tdi.DocID = ti.DocID;
                dt.AddCommand(dtd.CreateOneTrandayDetail(tdi));
                dt.AddCommand(da.EditOneAccount(ai));
            }
            if (dt.Execute() == true)
            {
                if (logger.IsDebugEnabled)
                {
                    logger.Debug(string.Format("Done transfer from {0} to {1}", db_account, cr_account));
                }
                msg.ResponseCode = "00";    //Approved
                return(msg);
            }
            else
            {
                if (logger.IsErrorEnabled)
                {
                    logger.Error(string.Format("Fails transfer from {0} to {1}", db_account, cr_account));
                }
                msg.ResponseCode = "06";    //Error
                return(msg);
            }
        }