YNICTE/Base/Popbill/PopbillService.cs

194 lines
7.1 KiB
C#

using NP.Base;
using NP.Dao;
using NP.Model;
using Popbill;
using Popbill.Taxinvoice;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Web.Configuration;
namespace NP.Base.Popbill
{
public class PopbillService
{
/// <summary>
/// 세금계산서 문서번호{mgtKey} 생성
/// </summary>
/// <param name="Dao">CommonDao</param>
/// <param name="taxdate">발행일</param>
/// <param name="mgtKeyType">세금계산서 유형 : SELL = 매출, BUY = 매입, TRUSTEE = 위수탁</param>
/// <returns>mgtKey</returns>
public static Result<String> MakeMgtKey(CommonDao Dao, DateTime taxdate, MgtKeyType mgtKeyType = MgtKeyType.SELL)
{
Result<String> result = new Result<string>();
try
{
string mgtkey = string.Empty;
int no = 1;
var payTax = Dao.Get<PayTax>("cr.paytax.formgtkey", new Hashtable() { { "taxdate", taxdate } }).FirstOrDefault();
if (payTax == null)
{
payTax = new PayTax() { };
}
if (payTax.taxdate == null)
{
payTax.taxdate = taxdate;
}
if (!string.IsNullOrEmpty(payTax.mgtkey))
{
var strNo = payTax.mgtkey.Substring(payTax.mgtkey.IndexOf("L") + 1);
if (int.TryParse(strNo, out no))
{
++no;
}
}
mgtkey = $"{taxdate:yyyy-MM-dd}-L{no:D5}";
bool isUse = true;
int checkLimit = 10;
while (isUse && checkLimit > 0)
{
isUse = PopbillConfig.taxinvoiceService.CheckMgtKeyInUse(PopbillConfig.CorpNum, mgtKeyType, mgtkey);
if (isUse)
{
++no;
mgtkey = $"{taxdate:yyyy-MM-dd}-L{no:D5}";
}
--checkLimit;
}
result.Data = mgtkey;
}
catch (PopbillException ex)
{
result.Code = ex.code.ToString();
result.Message = ex.Message;
Logger.TryError(ex.Message, ex);
}
return result;
}
/// <summary>
/// 세금계산서 상세정보
/// </summary>
/// <param name="mgtkey">파트너가 할당한 문서번호</param>
/// <param name="mgtKeyType">세금계산서 유형 : SELL = 매출, BUY = 매입, TRUSTEE = 위수탁</param>
/// <returns>Taxinvoice</returns>
public static Result<Taxinvoice> GetTaxinvoice(string mgtkey, MgtKeyType mgtKeyType = MgtKeyType.SELL)
{
Result<Taxinvoice> result = new Result<Taxinvoice>();
try
{
result.Data = PopbillConfig.taxinvoiceService.GetDetailInfo(PopbillConfig.CorpNum, mgtKeyType, mgtkey);
}
catch (PopbillException ex)
{
result.Code = ex.code.ToString();
result.Message = ex.Message;
Logger.TryError(ex.Message, ex);
}
finally
{
if (result.Data == null)
{
result.Data = new Taxinvoice() { invoicerMgtKey = mgtkey };
}
}
return result;
}
/// <summary>
/// 세금계산서 공급자정보
/// </summary>
/// <param name="mgtkey">파트너가 할당한 문서번호</param>
/// <returns>Taxinvoice</returns>
public static Result<Taxinvoice> GetTaxinvoiceR(string mgtkey)
{
Result<Taxinvoice> result = new Result<Taxinvoice>();
try
{
CorpInfo corpInfo = PopbillConfig.taxinvoiceService.GetCorpInfo(PopbillConfig.CorpNum, PopbillConfig.UserID);
Contact contact = PopbillConfig.taxinvoiceService.ListContact(PopbillConfig.CorpNum, PopbillConfig.UserID).Where(w => w.mgrYN == true).FirstOrDefault();
result.Data = new Taxinvoice
{
invoicerAddr = corpInfo?.addr,
invoicerCEOName = corpInfo?.ceoname,
invoicerCorpName = corpInfo?.corpName,
invoicerMgtKey = mgtkey,
invoicerCorpNum = PopbillConfig.CorpNum,
invoicerBizClass = corpInfo?.bizClass,
invoicerBizType = corpInfo?.bizType,
invoicerContactName = contact?.personName,
invoicerEmail = contact?.email,
invoicerHP = contact?.hp,
invoicerTEL = contact?.tel
};
}
catch (PopbillException ex)
{
result.Code = ex.code.ToString();
result.Message = ex.Message;
Logger.TryError(ex.Message, ex);
}
return result;
}
/// <summary>
/// 세금계산서 임시저장
/// </summary>
/// <param name="taxinvoice">Taxinvoice</param>
/// <returns>IssueResponse</returns>
public static Result<Response> Register(Taxinvoice taxinvoice)
{
Result<Response> result = new Result<Response>();
try
{
result.Data = PopbillConfig.taxinvoiceService.Register(PopbillConfig.CorpNum, taxinvoice);
}
catch (PopbillException ex)
{
result.Code = ex.code.ToString();
result.Message = ex.Message;
Logger.TryError(ex.Message, ex);
}
return result;
}
/// <summary>
/// 세금계산서 즉시 발행
/// </summary>
/// <param name="taxinvoice">Taxinvoice</param>
/// <param name="forceIssue">지연발행 가능여부</param>
/// <param name="memo">메모</param>
/// <returns>IssueResponse</returns>
public static Result<IssueResponse> RegistIssue(Taxinvoice taxinvoice, bool forceIssue = false, string memo = "")
{
Result<IssueResponse> result = new Result<IssueResponse>();;
try
{
result.Data = PopbillConfig.taxinvoiceService.RegistIssue(PopbillConfig.CorpNum, taxinvoice, forceIssue, memo);
}
catch (PopbillException ex)
{
result.Code = ex.code.ToString();
result.Message = ex.Message;
Logger.TryError(ex.Message, ex);
}
return result;
}
}
public class Result<T>
{
public String Code = null;
public String Message = null;
public T Data;
}
}