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
{
///
/// 세금계산서 문서번호{mgtKey} 생성
///
/// CommonDao
/// 발행일
/// 세금계산서 유형 : SELL = 매출, BUY = 매입, TRUSTEE = 위수탁
/// mgtKey
public static Result MakeMgtKey(CommonDao Dao, DateTime taxdate, MgtKeyType mgtKeyType = MgtKeyType.SELL)
{
Result result = new Result();
try
{
string mgtkey = string.Empty;
int no = 1;
var payTax = Dao.Get("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;
}
///
/// 세금계산서 상세정보
///
/// 파트너가 할당한 문서번호
/// 세금계산서 유형 : SELL = 매출, BUY = 매입, TRUSTEE = 위수탁
/// Taxinvoice
public static Result GetTaxinvoice(string mgtkey, MgtKeyType mgtKeyType = MgtKeyType.SELL)
{
Result result = new Result();
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;
}
///
/// 세금계산서 공급자정보
///
/// 파트너가 할당한 문서번호
/// Taxinvoice
public static Result GetTaxinvoiceR(string mgtkey)
{
Result result = new Result();
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;
}
///
/// 세금계산서 임시저장
///
/// Taxinvoice
/// IssueResponse
public static Result Register(Taxinvoice taxinvoice)
{
Result result = new Result();
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;
}
///
/// 세금계산서 즉시 발행
///
/// Taxinvoice
/// 지연발행 가능여부
/// 메모
/// IssueResponse
public static Result RegistIssue(Taxinvoice taxinvoice, bool forceIssue = false, string memo = "")
{
Result result = new Result();;
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
{
public String Code = null;
public String Message = null;
public T Data;
}
}