import ECPay from "./index";


describe("綠界開立發票", () => {
    const data = `{
    "MerchantID": "2000132",
    "RelateNumber": "e4323209223",
    "CustomerID": "",
    "CustomerIdentifier": "",
    "CustomerName": "綠界科技股份有限公司",
    "CustomerAddr": "106台北市南港區發票一街1號1樓",
    "CustomerPhone": "",
    "CustomerEmail": "test@ecpay.com.tw",
    "ClearanceMark": "1",
    "Print": "1",
    "Donation": "0",
    "LoveCode": "",
    "CarrierType": "",
    "CarrierNum": "",
    "TaxType": "1",
    "SalesAmount": 100,
    "InvoiceRemark": "發票備註",
    "InvType": "07",
    "vat": "1",
    "Items": [
        {
            "ItemSeq": 1,
            "ItemName": "item01",
            "ItemCount": 1,
            "ItemWord": "件",
            "ItemPrice": 50,
            "ItemTaxType": "1",
            "ItemAmount": 50,
            "ItemRemark": "item01_desc"
        },
        {
            "ItemSeq": 2,
            "ItemName": "item02",
            "ItemCount": 1,
            "ItemWord": "個",
            "ItemPrice": 20,
            "ItemTaxType": "1",
            "ItemAmount": 20,
            "ItemRemark": "item02_desc"
        },
        {
            "ItemSeq": 3,
            "ItemName": "item03",
            "ItemCount": 3,
            "ItemWord": "粒",
            "ItemPrice": 10,
            "ItemTaxType": "1",
            "ItemAmount": 30,
            "ItemRemark": "item03_desc"
        }
    ]
}`

    const o = JSON.parse(data)
    const ecpay = ECPay({
        MerchantID: "2000132",
        HashIV: "q9jcZX8Ib9LM8wYk",
        HashKey: "ejCk326UnaZWKisg",
        BaseURL: "https://einvoice-stage.ecpay.com.tw",
    })
    it("issue", async () => {
        const result =  await ecpay.Issue(o)
        console.log(result.DecryptedData)
    })

    it("查詢發票", async () => {
        const r = await ecpay.GetIssueList({
            BeginDate: "2023-10-01", EndDate: "2023-10-19", MerchantID: "2000132", NumPerPage: 200, ShowingPage: 1
        })
        console.log(r)

        const printableInvoice = r.InvoiceData.find(v=>v.IIS_Print_Flag === "1")
        if(!printableInvoice) throw new Error("沒有可列印的發票")


        const printResult = await ecpay.InvoicePrint({
            InvoiceDate: printableInvoice.IIS_Create_Date.slice(0,10),
            MerchantID: "2000132",
            InvoiceNo: printableInvoice.IIS_Number,
        })

        console.log(printResult)


    })



})