YNICTE/Base/Popbill/PopbillService.cs

532 lines
23 KiB
C#
Raw Normal View History

2021-04-22 18:28:05 +09:00
using NP.Base;
using NP.Dao;
using NP.Model;
2021-04-22 18:28:05 +09:00
using Popbill;
using Popbill.Taxinvoice;
using System;
using System.Collections;
2021-04-22 18:28:05 +09:00
using System.Collections.Generic;
using System.Linq;
2021-04-28 18:23:25 +09:00
using System.Web;
2021-04-22 18:28:05 +09:00
using System.Web.Configuration;
namespace NP.Base.Popbill
{
public class PopbillService
{
#region mgtKey()
/// <summary>
/// 세금계산서 mgtKey(문서번호) 사용여부
/// </summary>
/// <param name="mgtkey">mgtkey</param>
/// <param name="keyType">세금계산서 유형 : SELL = 매출, BUY = 매입, TRUSTEE = 위수탁</param>
/// <returns>bool</returns>
public static Result<bool> CheckMgtKeyInUse(string mgtkey, MgtKeyType keyType = MgtKeyType.SELL)
{
Result<bool> result = new Result<bool>();
try
{
result.IsUse = PopbillConfig.taxinvoiceService.CheckMgtKeyInUse(PopbillConfig.CorpNum, keyType, mgtkey);
}
catch (PopbillException ex)
{
result.IsSuccess = false;
result.Code = ex.code;
result.Message = ex.Message;
Logger.TryError(ex.Message, ex);
}
return result;
}
#endregion
2021-04-26 18:19:17 +09:00
#region mgtKey()
2021-04-22 18:28:05 +09:00
/// <summary>
2021-04-26 18:19:17 +09:00
/// 세금계산서 mgtKey(문서번호) 생성
2021-04-22 18:28:05 +09:00
/// </summary>
/// <param name="Dao">CommonDao</param>
/// <param name="taxno">PayTax.taxno</param>
2021-04-26 18:19:17 +09:00
/// <param name="writeDate">발행일</param>
/// <param name="keyType">세금계산서 유형 : SELL = 매출, BUY = 매입, TRUSTEE = 위수탁</param>
/// <returns>mgtKey</returns>
public static string MakeMgtKey(CommonDao Dao, long taxno, DateTime writeDate, MgtKeyType keyType = MgtKeyType.SELL)
2021-04-26 18:19:17 +09:00
{
int no = 1;
string mgtkey = string.Empty;
var nowPayTax = Dao.Get<PayTax>("cr.paytaxes", new Hashtable() { { "taxno", taxno } }).FirstOrDefault();
//이미 mgtkey 발급되어 있으면 발급된 mgtkey 사용
if (nowPayTax != null && !string.IsNullOrEmpty(nowPayTax.mgtkey))
2021-04-22 18:28:05 +09:00
{
mgtkey = nowPayTax.mgtkey;
}
else
{
var payTax = Dao.Get<PayTax>("cr.paytax.formgtkey", new Hashtable() { { "writeDate", writeDate } }).FirstOrDefault();
if (payTax != null && !string.IsNullOrEmpty(payTax.mgtkey))
{
var strNo = payTax.mgtkey.Substring(payTax.mgtkey.IndexOf("L") + 1);
if (int.TryParse(strNo, out no))
{
++no;
}
}
2021-04-26 18:19:17 +09:00
mgtkey = $"{writeDate:yyyy-MM-dd}-L{no:D5}";
2021-04-26 18:19:17 +09:00
bool isUse = true;
int checkLimit = 50;
while (isUse && checkLimit > 0)
{
isUse = PopbillConfig.taxinvoiceService.CheckMgtKeyInUse(PopbillConfig.CorpNum, keyType, mgtkey);
if (isUse)
{
++no;
mgtkey = $"{writeDate:yyyy-MM-dd}-L{no:D5}";
}
else
{
break;
}
--checkLimit;
}
}
2021-04-26 18:19:17 +09:00
return mgtkey;
}
#endregion
2021-04-26 18:19:17 +09:00
#region Taxinvoice
/// <summary>
/// 세금계산서 Taxinvoice 생성
/// </summary>
/// <param name="taxinvoice">Taxinvoice</param>
/// <param name="payTax">PayTax</param>
/// <param name="payItems">payItems</param>
/// <returns>Taxinvoice</returns>
public static Taxinvoice MakeTaxinvoice(Taxinvoice taxinvoice, PayTax payTax, IList<PayItem> payItems)
{
taxinvoice.issueType = "정발행"; //"정발행" / "역발행" / "위수탁" 중 택 1
taxinvoice.taxType = "면세"; //"과세" / "영세" / "면세" 중 택 1
taxinvoice.chargeDirection = "정과금"; //"정과금" / "역과금" 중 택 1
taxinvoice.writeDate = payTax.taxdate?.ToString("yyyyMMdd");
2021-04-26 18:19:17 +09:00
taxinvoice.purposeType = payTax.isreceipt == 0 ? "청구" : "영수"; //0:청구, 1:영수 {"영수" / "청구" / "없음" 중 택 1}
taxinvoice.supplyCostTotal = payTax.taxamt.ToString();
taxinvoice.taxTotal = "0";
taxinvoice.totalAmount = payTax.taxamt.ToString();
taxinvoice.invoiceeType = "사업자";
taxinvoice.invoiceeCorpNum = payTax.brno;
taxinvoice.invoiceeCorpName = payTax.asname;
taxinvoice.invoiceeCEOName = payTax.ceoname;
taxinvoice.invoiceeAddr = payTax.asaddr;
taxinvoice.invoiceeBizType = payTax.btype;
taxinvoice.invoiceeBizClass = payTax.bkind;
taxinvoice.invoiceeContactName1 = payTax.manname;
taxinvoice.invoiceeTEL1 = payTax.telno;
taxinvoice.invoiceeEmail1 = payTax.email;
//taxinvoice.serialNum = payTax.taxno.ToString();
2021-04-26 18:19:17 +09:00
#region ()
if (payItems != null)
{
/**************************************************************************
* () *
* - *
* . *
* - 99 *
**************************************************************************/
2021-04-26 18:19:17 +09:00
taxinvoice.detailList = new List<TaxinvoiceDetail>();
var serialNum = 1;
foreach (var payItem in payItems)
{
2021-04-26 18:19:17 +09:00
TaxinvoiceDetail detail = new TaxinvoiceDetail();
detail.serialNum = serialNum; // 일련번호, 1부터 순차기재
detail.purchaseDT = payTax.taxdate?.ToString("yyyyMMdd"); // 거래일자
2021-04-26 18:19:17 +09:00
detail.itemName = payItem.itemname; // 품목명
detail.spec = ""; // 규격
detail.qty = payItem.pcnt.ToString(); // 수량
detail.unitCost = payItem.payamt.ToString(); // 단가
detail.supplyCost = payItem.payamt.ToString(); // 공급가액
detail.tax = "0"; // 세액
detail.remark = ""; // 비고
taxinvoice.detailList.Add(detail);
++serialNum;
}
2021-04-22 18:28:05 +09:00
}
2021-04-26 18:19:17 +09:00
#endregion
return taxinvoice;
2021-04-22 18:28:05 +09:00
}
2021-04-26 18:19:17 +09:00
#endregion
2021-04-22 18:28:05 +09:00
2021-04-28 18:23:25 +09:00
#region
/// <summary>
/// 세금계산서 상태
/// </summary>
/// <param name="stateCode">상태코드</param>
/// <returns>상태</returns>
public static string GetTaxinvoiceState(int stateCode)
{
string state = string.Empty;
switch (stateCode)
{
case 100:
state = "임시저장";
break;
case 300:
state = "발행완료";
break;
case 301:
state = "국세청-전송전";
2021-04-28 18:23:25 +09:00
break;
case 302:
state = "국세청-전송대기";
2021-04-28 18:23:25 +09:00
break;
case 303:
state = "국세청-전송중";
2021-04-28 18:23:25 +09:00
break;
case 304:
state = "국세청-전송성공";
2021-04-28 18:23:25 +09:00
break;
case 305:
state = "국세청-전송실패";
2021-04-28 18:23:25 +09:00
break;
case 600:
state = "발행취소";
break;
default:
state = $"stateCode:{stateCode}";
break;
}
return state;
}
#endregion
2021-04-26 18:19:17 +09:00
#region
2021-04-22 18:28:05 +09:00
/// <summary>
/// 세금계산서 상세정보
/// </summary>
2021-04-26 18:19:17 +09:00
/// <param name="payTax">PayTax</param>
/// <param name="payItems">payItems</param>
/// <param name="keyType">세금계산서 유형 : SELL = 매출, BUY = 매입, TRUSTEE = 위수탁</param>
/// <returns>Taxinvoice</returns>
2021-04-26 18:19:17 +09:00
public static Result<Taxinvoice> GetTaxinvoice(PayTax payTax, IList<PayItem> payItems = null, MgtKeyType keyType = MgtKeyType.SELL)
{
Result<Taxinvoice> result = new Result<Taxinvoice>();
2021-04-22 18:28:05 +09:00
try
{
2021-04-26 18:19:17 +09:00
if (!string.IsNullOrEmpty(payTax.mgtkey) && PopbillConfig.taxinvoiceService.CheckMgtKeyInUse(PopbillConfig.CorpNum, keyType, payTax.mgtkey))
{
result.Data = PopbillConfig.taxinvoiceService.GetDetailInfo(PopbillConfig.CorpNum, keyType, payTax.mgtkey);
}
else
{
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 = payTax.mgtkey,
invoicerCorpNum = PopbillConfig.CorpNum,
invoicerBizClass = corpInfo?.bizClass,
invoicerBizType = corpInfo?.bizType,
invoicerContactName = contact?.personName,
invoicerEmail = contact?.email,
invoicerHP = contact?.hp,
invoicerTEL = contact?.tel
};
}
result.Data = MakeTaxinvoice(result.Data, payTax, payItems);
2021-04-22 18:28:05 +09:00
}
catch (PopbillException ex)
{
2021-04-26 18:19:17 +09:00
result.IsSuccess = false;
result.Code = ex.code;
result.Message = ex.Message;
2021-04-22 18:28:05 +09:00
Logger.TryError(ex.Message, ex);
}
return result;
2021-04-22 18:28:05 +09:00
}
2021-04-26 18:19:17 +09:00
#endregion
2021-04-22 18:28:05 +09:00
2021-04-28 18:23:25 +09:00
#region
/// <summary>
/// 세금계산서 상태정보
/// </summary>
/// <param name="payTax">PayTax</param>
/// <param name="keyType">세금계산서 유형 : SELL = 매출, BUY = 매입, TRUSTEE = 위수탁</param>
/// <returns>Taxinvoice</returns>
public static Result<TaxinvoiceInfo> GetTaxinvoiceInfo(PayTax payTax, MgtKeyType keyType = MgtKeyType.SELL)
{
Result<TaxinvoiceInfo> result = new Result<TaxinvoiceInfo>();
try
{
result.Data = PopbillConfig.taxinvoiceService.GetInfo(PopbillConfig.CorpNum, keyType, payTax.mgtkey);
}
catch (PopbillException ex)
{
result.IsSuccess = false;
result.Code = ex.code;
result.Message = ex.Message;
Logger.TryError(ex.Message, ex);
}
return result;
}
#endregion
#region ()
/// <summary>
/// 세금계산서 파일등록(사업자등록증)
/// </summary>
/// <param name="Dao"></param>
/// <param name="payTax"></param>
/// <param name="keyType"></param>
/// <returns></returns>
public static Response AttachFile(CommonDao Dao, PayTax payTax, MgtKeyType keyType = MgtKeyType.SELL)
{
Response response = new Response();
if (payTax.fgno != null && payTax.fgno != 0)
{
IList<File> files = Dao.GetFiles(payTax.fgno.Value);
#region ()
if (files.Count > 0)
{
var attachedFiles = PopbillConfig.taxinvoiceService.GetFiles(PopbillConfig.CorpNum, keyType, payTax.mgtkey);
foreach (var attachedFile in attachedFiles)
{
response = PopbillConfig.taxinvoiceService.DeleteFile(PopbillConfig.CorpNum, keyType, payTax.mgtkey, attachedFile.attachedFile, PopbillConfig.UserID);
}
}
#endregion
#region #region ()
foreach (var file in files)
{
var filePath = HttpContext.Current.Server.MapPath(file.fullurl);
if (System.IO.File.Exists(filePath))
{
response = PopbillConfig.taxinvoiceService.AttachFile(PopbillConfig.CorpNum, keyType, payTax.mgtkey, filePath, PopbillConfig.UserID);
}
}
#endregion
}
return response;
}
#endregion
2021-04-26 18:19:17 +09:00
#region ()
2021-04-22 18:28:05 +09:00
/// <summary>
2021-04-26 18:19:17 +09:00
/// 세금계산서 임시저장(수정)
2021-04-22 18:28:05 +09:00
/// </summary>
2021-04-28 18:23:25 +09:00
/// <param name="Dao">CommonDao</param>
2021-04-26 18:19:17 +09:00
/// <param name="payTax">payTax</param>
/// <param name="payItems">payItems</param>
/// <param name="keyType">세금계산서 유형 : SELL = 매출, BUY = 매입, TRUSTEE = 위수탁</param>
/// <returns>mgtkey</returns>
/// <remarks>작성된 세금계산서 데이터를 팝빌에 저장합니다.</remarks>
2021-04-28 18:23:25 +09:00
public static Result<Response> TempSave(CommonDao Dao, PayTax payTax, IList<PayItem> payItems, MgtKeyType keyType = MgtKeyType.SELL)
2021-04-26 18:19:17 +09:00
{
2021-04-28 18:23:25 +09:00
Result<Response> result = new Result<Response>();
2021-04-22 18:28:05 +09:00
try
2021-04-28 18:23:25 +09:00
{
2021-04-26 18:19:17 +09:00
var taxinvoice = GetTaxinvoice(payTax, payItems).Data;
if (PopbillConfig.taxinvoiceService.CheckMgtKeyInUse(PopbillConfig.CorpNum, keyType, payTax.mgtkey))
{
2021-04-28 18:23:25 +09:00
result.Data = PopbillConfig.taxinvoiceService.Update(PopbillConfig.CorpNum, keyType, payTax.mgtkey, taxinvoice, PopbillConfig.UserID);
2021-04-26 18:19:17 +09:00
}
else
2021-04-22 18:28:05 +09:00
{
2021-04-28 18:23:25 +09:00
result.Data = PopbillConfig.taxinvoiceService.Register(PopbillConfig.CorpNum, taxinvoice);
2021-04-26 18:19:17 +09:00
}
2021-04-28 18:23:25 +09:00
if (result.Data.code == 1)
{
AttachFile(Dao, payTax, keyType);
}
result.Code = result.Data.code;
result.Message = result.Data.message;
2021-04-22 18:28:05 +09:00
}
catch (PopbillException ex)
{
2021-04-26 18:19:17 +09:00
result.IsSuccess = false;
result.Code = ex.code;
result.Message = ex.Message;
Logger.TryError(ex.Message, ex);
}
return result;
}
2021-04-26 18:19:17 +09:00
#endregion
2021-04-26 18:19:17 +09:00
#region
/// <summary>
2021-04-26 18:19:17 +09:00
/// 세금계산서 발행
/// </summary>
2021-04-26 18:19:17 +09:00
/// <param name="mgtkey">파트너가 할당한 문서번호</param>
/// <param name="keyType">세금계산서 유형 : SELL = 매출, BUY = 매입, TRUSTEE = 위수탁</param>
/// <param name="forceIssue">지연발행 가능여부</param>
/// <param name="memo">메모</param>
/// <returns>IssueResponse</returns>
2021-04-26 18:19:17 +09:00
/// <remarks>"임시저장" 또는 "(역)발행대기" 상태의 세금계산서를 발행(전자서명)하며, "발행완료" 상태로 처리합니다</remarks>
public static Result<IssueResponse> Issue(string mgtkey, MgtKeyType keyType = MgtKeyType.SELL, bool forceIssue = false, string memo = "")
{
2021-04-26 18:19:17 +09:00
Result<IssueResponse> result = new Result<IssueResponse>();
try
{
2021-04-26 18:19:17 +09:00
result.Data = PopbillConfig.taxinvoiceService.Issue(PopbillConfig.CorpNum, keyType, mgtkey, memo, forceIssue, PopbillConfig.UserID);
result.Code = result.Data.code;
result.Message = result.Data.message;
}
catch (PopbillException ex)
{
2021-04-26 18:19:17 +09:00
result.IsSuccess = false;
result.Code = ex.code;
result.Message = ex.Message;
2021-04-22 18:28:05 +09:00
Logger.TryError(ex.Message, ex);
}
return result;
2021-04-22 18:28:05 +09:00
}
2021-04-26 18:19:17 +09:00
#endregion
2021-04-22 18:28:05 +09:00
2021-04-26 18:19:17 +09:00
#region
/// <summary>
2021-04-26 18:19:17 +09:00
/// 세금계산서 발행취소
/// </summary>
/// <param name="mgtkey">파트너가 할당한 문서번호</param>
/// <param name="memo">메모</param>
2021-04-26 18:19:17 +09:00
/// <param name="keyType">세금계산서 유형 : SELL = 매출, BUY = 매입, TRUSTEE = 위수탁</param>
/// <returns>IssueResponse</returns>
2021-04-26 18:19:17 +09:00
/// <remarks>국세청 전송 이전 "발행완료" 상태의 전자세금계산서를 "발행취소"하고, 해당 건은 국세청 신고 대상에서 제외됩니다.</remarks>
public static Result<Response> CancelIssue(string mgtkey, string memo = "", MgtKeyType keyType = MgtKeyType.SELL)
{
2021-04-26 18:19:17 +09:00
Result<Response> result = new Result<Response>();
try
{
2021-04-26 18:19:17 +09:00
result.Data = PopbillConfig.taxinvoiceService.CancelIssue(PopbillConfig.CorpNum, keyType, mgtkey, memo, PopbillConfig.UserID);
result.Code = result.Data.code;
result.Message = result.Data.message;
}
catch (PopbillException ex)
{
2021-04-26 18:19:17 +09:00
result.IsSuccess = false;
result.Code = ex.code;
result.Message = ex.Message;
Logger.TryError(ex.Message, ex);
}
return result;
}
2021-04-26 18:19:17 +09:00
#endregion
2021-04-26 18:19:17 +09:00
#region
2021-04-22 18:28:05 +09:00
/// <summary>
/// 세금계산서 즉시 발행
/// </summary>
2021-04-28 18:23:25 +09:00
/// <param name="Dao">Dao</param>
2021-04-26 18:19:17 +09:00
/// <param name="payTax">payTax</param>
/// <param name="payItems">payItems</param>
/// <param name="forceIssue">지연발행 가능여부</param>
/// <param name="memo">메모</param>
/// <returns>IssueResponse</returns>
2021-04-26 18:19:17 +09:00
/// <remarks>작성된 세금계산서 데이터를 팝빌에 저장과 동시에 발행(전자서명)하여 "발행완료" 상태로 처리합니다.</remarks>
2021-04-28 18:23:25 +09:00
public static Result<IssueResponse> RegistIssue(CommonDao Dao, PayTax payTax, IList<PayItem> payItems, MgtKeyType keyType = MgtKeyType.SELL, bool forceIssue = false, string memo = "")
2021-04-22 18:28:05 +09:00
{
2021-04-26 18:19:17 +09:00
Result<IssueResponse> result = new Result<IssueResponse>(); ;
2021-04-22 18:28:05 +09:00
try
{
//첨부파일이 있으면 임시저장 후 발행
if (payTax.fgno != null && payTax.fgno != 0)
{
var resultResponse = TempSave(Dao, payTax, payItems, keyType);
if (resultResponse.IsSuccess)
{
result = Issue(payTax.mgtkey, keyType, forceIssue, memo);
}
else
{
result.IsSuccess = false;
result.Code = resultResponse.Code;
result.Message = resultResponse.Message;
}
}
else
{
var taxinvoice = GetTaxinvoice(payTax, payItems, keyType).Data;
result.Data = PopbillConfig.taxinvoiceService.RegistIssue(PopbillConfig.CorpNum, taxinvoice, forceIssue, memo);
result.Code = result.Data.code;
result.Message = result.Data.message;
}
2021-04-22 18:28:05 +09:00
}
catch (PopbillException ex)
{
2021-04-26 18:19:17 +09:00
result.IsSuccess = false;
result.Code = ex.code;
result.Message = ex.Message;
2021-04-22 18:28:05 +09:00
Logger.TryError(ex.Message, ex);
}
return result;
2021-04-26 18:19:17 +09:00
}
#endregion
2021-04-26 18:19:17 +09:00
#region URL
/// <summary>
/// 세금계산서 인쇄 팝업 URL 확인
/// </summary>
/// <param name="mgtkey">파트너가 할당한 문서번호</param>
2021-04-26 18:19:17 +09:00
/// <param name="keyType">세금계산서 유형 : SELL = 매출, BUY = 매입, TRUSTEE = 위수탁</param>
/// <returns>URL</returns>
2021-04-26 18:19:17 +09:00
public static Result<string> GetPrintURL(string mgtkey, MgtKeyType keyType = MgtKeyType.SELL)
{
Result<string> result = new Result<string>(); ;
try
{
2021-04-26 18:19:17 +09:00
result.Data = PopbillConfig.taxinvoiceService.GetPrintURL(PopbillConfig.CorpNum, keyType, mgtkey, PopbillConfig.UserID);
}
catch (PopbillException ex)
{
2021-04-26 18:19:17 +09:00
result.IsSuccess = false;
result.Code = ex.code;
result.Message = ex.Message;
Logger.TryError(ex.Message, ex);
}
return result;
2021-04-26 18:19:17 +09:00
}
#endregion
2021-04-26 18:19:17 +09:00
#region URL -
/// <summary>
/// 세금계산서 인쇄 팝업 URL 확인 - 공급받는자용
/// </summary>
/// <param name="mgtkey">파트너가 할당한 문서번호</param>
2021-04-26 18:19:17 +09:00
/// <param name="keyType">세금계산서 유형 : SELL = 매출, BUY = 매입, TRUSTEE = 위수탁</param>
/// <returns>URL</returns>
2021-04-26 18:19:17 +09:00
public static Result<string> GetEPrintURL(string mgtkey, MgtKeyType keyType = MgtKeyType.SELL)
{
Result<string> result = new Result<string>(); ;
try
{
2021-04-26 18:19:17 +09:00
result.Data = PopbillConfig.taxinvoiceService.GetEPrintURL(PopbillConfig.CorpNum, keyType, mgtkey, PopbillConfig.UserID);
}
catch (PopbillException ex)
{
2021-04-26 18:19:17 +09:00
result.IsSuccess = false;
result.Code = ex.code;
result.Message = ex.Message;
Logger.TryError(ex.Message, ex);
}
return result;
2021-04-26 18:19:17 +09:00
}
#endregion
2021-04-22 18:28:05 +09:00
}
public class Result<T>
{
2021-04-26 18:19:17 +09:00
public bool IsSuccess = true;
public long Code = 1;
public String Message = string.Empty;
public T Data;
public bool IsUse = false;
}
2021-04-26 18:19:17 +09:00
}