<?xml version="1.0" encoding="UTF-8"?>
<!--
        $Author:zhuxianfei
        $Date:2018年08月16日 17:37:51
        $Revision:1.0
        $Purpose:金税纸质发票开票接口
        -->
<a:service xmlns:p="uncertain.proc" xmlns:a="http://www.leaf-framework.org/application" xmlns:s="leaf.plugin.script"
           trace="true">
    <a:init-procedure>
        <s:server-script><![CDATA[
            importPackage(Packages.com.hand.hls.hlcm.jinsui.service);

            var iService = $spring(IMakeInvService);
            var successCode = '4011'; //成功返回code

            var invoiceHdId = $ctx.parameter.invoiceHdId;

            //开票行信息
            //var query_invoiceLn_bm = $bm('acr.ACR515.acr515_invoice_ln_lv');
            //var query_invoiceLn = query_invoiceLn_bm.queryAsMap({
            //    "invoice_hd_id": $ctx.parameter.invoiceHdId
            //});
            //var list_query_invoiceLn = query_invoiceLn.getChildren();

            var query_invoiceLn_change_bm = $bm('acr.ACR515.acr515_invoice_ln_lv_change');
            var query_invoiceLn_change = query_invoiceLn_change_bm.queryAsMap({
                "invoice_hd_id": $ctx.parameter.invoiceHdId
            });
            var list_query_invoiceLn_change = query_invoiceLn_change.getChildren();

            //封装数据
            var param = {};
            var Data_list = [];

            println(list_query_invoiceLn_change.length);

            //行表有多条数据
            for (var i = 0;i < list_query_invoiceLn_change.length;i++) {
                    param = {
                    ProdName: list_query_invoiceLn_change[i].product_name,
                    TaxItem: list_query_invoiceLn_change[i].line_type_n,
                    Spec: '',
                    Unit: '',
                    Quantity: list_query_invoiceLn_change[i].quantity,
                    Price: list_query_invoiceLn_change[i].total_amount,
                    Amount: list_query_invoiceLn_change[i].total_amount,
                    TaxAmount: ((list_query_invoiceLn_change[i].total_amount)*(list_query_invoiceLn_change[i].tax_type_rate))/100,
                    DisAmount: '0',
                    DisTaxAmount: '0',
                    TaxRate: list_query_invoiceLn_change[i].tax_type_rate,
                    GoodsNoVer: '12.0',
                    GoodsTaxNo: '10301020101',
                    TaxPre: '',
                    TaxPreCon: '',
                    ZeroTax: '',
                    TaxDeduction: ''
                };
                Data_list.push(param);
            }
            //开票头信息
            var query_invoiceHd_bm = $bm('acr.ACR515.acr515_invoice_hd_lv');
            var query_invoiceHd = query_invoiceHd_bm.queryAsMap({
                "invoice_hd_id": $ctx.parameter.invoiceHdId
            });
            var list_query_invoiceHd = query_invoiceHd.getChildren();

            var bp_class =  list_query_invoiceHd[0].bp_class;

            if(bp_class == 'NP'){
                var Json = {
                    //客户名称
                    CustName: list_query_invoiceHd[0].bp_name,
                    //客户税号
                    CustTaxNr: '',
                    //客户地址电话
                    CustAddrTel: '',
                    //客户银行及帐号
                    CustBankAcct: '',
                    //销方地址电话
                    SellerBankAcct: "",
                    //销方银行及帐号
                    SellerAddrTel: "",
                    //开票人
                    KPR: list_query_invoiceHd[0].created_by_name,
                    //复核人
                    FHR: list_query_invoiceHd[0].confirmed_by_name,
                    //收款人
                    SKR: '',
                    //发票类型
                    InvKind: list_query_invoiceHd[0].invoice_kind,
                    //备注
                    Memo: list_query_invoiceHd[0].description,

                    DetailList: Data_list
                };
            }
            else{
                var Json = {
                    //客户名称
                    CustName: list_query_invoiceHd[0].bp_name,
                    //客户税号
                    CustTaxNr: list_query_invoiceHd[0].bp_tax_registry_num,
                    //客户地址电话
                    CustAddrTel: list_query_invoiceHd[0].bp_address_phone_num,
                    //客户银行及帐号
                    CustBankAcct: list_query_invoiceHd[0].bp_bank_account,
                    //销方地址电话
                    SellerBankAcct: "",
                    //销方银行及帐号
                    SellerAddrTel: "",
                    //开票人
                    KPR: list_query_invoiceHd[0].created_by_name,
                    //复核人
                    FHR: list_query_invoiceHd[0].confirmed_by_name,
                    //收款人
                    SKR: '',
                    //发票类型
                    InvKind: list_query_invoiceHd[0].invoice_kind,
                    //备注
                    Memo: list_query_invoiceHd[0].description,

                    DetailList: Data_list
                    };
                 }
            try {
                //将json数组转换成字符串
                var jsonString = JSON.stringify(Json);
                println('----------请求数据-----------');
                println(jsonString);
                println('----------请求数据-----------');

                //发起开票请求
                var resultString = iService.doMakeInv(jsonString);
                println('----------返回结果-----------');
                println(resultString);
                println('----------返回结果-----------');

                //转成Json数组
                var resData = JSON.parse(resultString);

                var InfoAmount = resData.InfoAmount;
                var InfoTaxAmount = resData.InfoTaxAmount;
                var InfoNumber = resData.InfoNumber;
                var InfoInvKind = resData.InfoInvKind;
                var RetCode = resData.RetCode;
                var RetMsg = resData.RetMsg;
                var InvStock = resData.InvStock;
                var InfoDate = resData.InfoDate;
                var InfoTypeCode = resData.InfoTypeCode;
                var TaxCode = resData.TaxCode;
                //println(RetCode);
                //println(RetMsg);

                $ctx.parameter.resCode = RetCode;

                if (RetCode != successCode) {
                    raise_app_error(RetMsg);
                }
            } catch (e) {
                //抛出异常
                raise_app_error(RetMsg);
            }

            //开票成功后回写发票号码以及金税状态
            $bm('acr.ACR515.acr515_invoice_hd_lv').update({
                invoice_number: InfoNumber,
                invoice_hd_id: $ctx.parameter.invoiceHdId,
                vat_interface_status: 'BACK'
            });


            ]]></s:server-script>
    </a:init-procedure>
    <a:service-output output="/parameter/@resCode"/>
</a:service>