/// <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); } }
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); }
/// <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); } }