//已开发票作废
UINT8 INVM_WstInv(CInvHead* pInvHead, string &strErr)
{
    DBG_ASSERT_EXIT((pInvHead != NULL), (" pInvHead == NULL!"));
    INT32 ret = SUCCESS;
    INT8 val[256];
    string strsql("");
    UINT8 waste_type = 0;
    UINT8 tmpType = pInvHead->m_kplx;

    switch(pInvHead->m_kplx)
    {
    case NORMAL_INV:
        waste_type = WASTE_NOR;//正票可以作废,成为正废票
        break;
    case RETURN_INV:
    case RET_MANUAL_INV:
    case RET_SPECIAL_INV:
        waste_type = WASTE_RET;//红票(负票)也可以作废,成为负废票
        break;
    default:
        ;
    }

    UINT8 zflx= 1; //已开发票作废
    ret= g_pAPIBase->MakeWasteInv_API(*g_YwXmlArg, pInvHead, zflx, strErr);

    if (SUCCESS != ret)
    {
        DBG_PRINT(("strErr = %s", strErr.c_str()));
        return FAILURE;
    }


    POWOFF_DISABLE(); //屏蔽掉电中断
    //更新INV_HEAD发票类型

    memset((void*)val, 0x00, sizeof(val));
    sprintf(val, "where FPDM='%s' AND FPHM=%u", pInvHead->m_fpdm.c_str(), pInvHead->m_fphm);
    strsql = val;

    pInvHead->m_kplx = waste_type;
    pInvHead->m_kprq = pInvHead->m_zfrq;
    pInvHead->m_kpsj = pInvHead->m_zfsj;

    ret = pInvHead->Update(strsql, &(pInvHead->m_kplx), &(pInvHead->m_kprq), &(pInvHead->m_kpsj), NULL);

    if ( ret!= SQLITE_OK)
    {
        strErr = "更新发票信息表错误";
        return FAILURE;
    }

    DBG_PRINT(("更新INV_HEAD发票类型strsql = %s", strsql.c_str()));

    //更新INV_DET发票类型
    CInvDet pInvDet;

    pInvDet.m_kplx = waste_type;
    pInvDet.m_kprq = pInvHead->m_zfrq;
    pInvDet.m_kpsj = pInvHead->m_zfsj;

    ret = pInvDet.Update(strsql, &(pInvDet.m_kplx), &(pInvDet.m_kprq), &(pInvDet.m_kpsj), NULL);

    if ( ret!= SQLITE_OK)
    {
        strErr = "更新发票信息表错误";
        return FAILURE;
    }

    DBG_PRINT(("更新INV_DET发票类型strsql = %s", strsql.c_str()));

    //删除RT_INV表中,红票对应的原发票代码和号码
    DBG_PRINT(("pInvHead->m_kplx = %d", pInvHead->m_kplx));//开票类型并不从金税盘中返回
    if(tmpType == RETURN_INV)
    {
        CRtInv rtInv;
        memset(val, 0, sizeof(val));
        sprintf(val, "where FPDM = '%s' and FPHM = %u ", pInvHead->m_yfpdm.c_str(), pInvHead->m_yfphm);
        DBG_PRINT(("val = %s", val));
        rtInv.m_filter = val;
        if( rtInv.Delete() != SQLITE_OK )
        {
            DBG_PRINT(("Delete RT_INV info error"));
        }
        DBG_PRINT(("Delete RT_INV info succeed"));
    }

    POWOFF_ENABLE(); //开掉电中断

    DBG_PRINT(("WstInv() SUCCESS"));

    return SUCCESS;
}
//按发票号码查询发票明细
UINT8 INVM_InvDetailNOQuery(string invCode,UINT32 invNO,UINT8 uFlag)
{
    INT32 Ret=SUCCESS;
    string strsql("");
    string strErr="";
    string strBar="";
    INT32 ret=0;

    //发票明细查询
    CInvHead Invhead;
    UINT32 invNum=0;//发票张数
    UINT8 cxfs=0;  //0:按发票号码段查询
    string cxtj(""); // cxfs为0时:12发票代码+8位起始号码+8位终止号码
    cxtj =invCode;
    INT8 tmpChar[16];
    memset((void *)tmpChar,0x00,sizeof(tmpChar));
    sprintf(tmpChar,"%08u",invNO);
    cxtj.append(tmpChar);
    cxtj.append(tmpChar);
    DBG_PRINT(("cxtj= %s",cxtj.c_str()));

    if (uFlag ==0)
    {
        strBar="发票信息查询中.....";
    }
    else
    {
        strBar="发票信息补传中.....";
    }

    CaProgressBar proBar(strBar.c_str());
    proBar.ReFresh();

    Ret=g_pAPIBase->GetInvHeadInfo_API(*g_YwXmlArg, cxfs, cxtj, invNum, &Invhead, strErr);
    DBG_PRINT(("Ret= %d",Ret));
    if (Ret !=SUCCESS)
    {
        CaMsgBox::ShowMsg(strErr);
        return FAILURE;
    }

    if (uFlag ==1)
    {
        CaMsgBox::ShowMsg("发票信息等待上传!");
        g_globalArg->m_pthreadFlag = 1;
        return SUCCESS;
    }

    //从盘上获取的明细编码为空,则置为 "0000000000000",便于报表统计
    CInvDet *p = Invhead.pHead;
    while (p)
    {
        DBG_PRINT(("p->m_spbm = %u", p->m_spbm.c_str()));
        if ("" == p->m_spbm)
        {
            p->m_spbm = "0000000000000";
        }
        p = p->pNext;
        DBG_PRINT(("INV_DET AddNew()"));
    }

    //查看本地是否有该发票信息
    CInvHead invheadtmp;
    INT8 sqlbuf[128];
    memset(sqlbuf, 0x00, sizeof(sqlbuf));
    sprintf(sqlbuf, "where FPDM = '%s' and FPHM = %u", invCode.c_str(), invNO);
    invheadtmp.m_filter = sqlbuf;
    invheadtmp.Requery();
    if (invheadtmp.LoadOneRecord() == SQLITE_OK)
    {
        //判断本地数据库的数据类型是否与转换器一致
        DBG_PRINT(("invheadtmp.m_kplx= %u",invheadtmp.m_kplx));
        DBG_PRINT(("Invhead.m_kplx= %u",Invhead.m_kplx));
        if (invheadtmp.m_kplx != Invhead.m_kplx )
        {
            strsql = sqlbuf;
            invheadtmp.m_kplx = Invhead.m_kplx;
            ret = invheadtmp.Update(strsql, &(invheadtmp.m_kplx), NULL);
            DBG_PRINT(("ret= %d",ret));
            if ( ret!= SQLITE_OK)
            {
                CaMsgBox::ShowMsg("更新发票头信息表错误");
                return FAILURE;
            }

            //更新INV_DET发票类型
            CInvDet invDet;
            invDet.m_kplx =invheadtmp.m_kplx;
            ret = invDet.Update(strsql, &(invDet.m_kplx), NULL);
            DBG_PRINT(("ret= %d",ret));
            if ( ret!= SQLITE_OK)
            {
                CaMsgBox::ShowMsg("更新发票明细信息表错误");
                return FAILURE;
            }

        }

    }
    else
    {
        //发票信息保存
        if (SUCCESS != Invhead.Save())
        {
            CaMsgBox::ShowMsg("查询发票存储失败!");
            return FAILURE;
        }
        DBG_PRINT(("Invhead.m_kplx= %u",Invhead.m_kplx));
        if(Invhead.m_kplx == RETURN_INV)
        {
            CRtInv rtInv;
            rtInv.m_fpdm =Invhead.m_yfpdm;
            rtInv.m_fphm =Invhead.m_yfphm;
            ret = rtInv.AddNew();				//写已退发票信息表
            DBG_PRINT(("ret= %d",ret));
// 			if ( ret!= SQLITE_OK)
// 			{
// 				CaMsgBox::ShowMsg("查询红票,存储蓝票信息表错误");
// 				return FAILURE;
// 			}

        }
    }

    CaMsgBox::ShowMsg("查询发票成功");

    return SUCCESS;
}
INT32 CJSKMakeInvoice::FPZF_Proc(CInvHead *pInvhead, UINT8 zflx, string &strErr)
{
	DBG_PRINT(("===== FPZF_Proc Begin() ======"));
	DBG_ASSERT_EXIT((pInvhead != NULL), (" pInvhead == NULL!"));
	INT32 retcode = JSK_SUCCESS;

	if(zflx == 0)	//空白票作废
	{
		pInvhead->m_kplx = JSK_WASTE_INV;
		pInvhead->m_xfmc = g_globalArgLib->m_corpInfo->m_Nsrmc;
		retcode = FPKJ_Proc(pInvhead, strErr, 1);

		return retcode;
	}

	//////////////////////////////////////////////////////////////////////////
	if(zflx != 1)
	{
		strErr = "发票作废类型错误!";
		return JSK_FAILURE;
	}
	CJSKInvManageProc::fplxdm2fplx(pInvhead->m_fplxdm, pInvhead->m_fplb);
	//已开票作废
	//查询数据库中已开发票信息
	INT8 sqlbuf[256];
	UINT32  errorcode;
	CInvHead tmpInvHead;
	memset(sqlbuf, 0, sizeof(sqlbuf));
	sprintf(sqlbuf, "where FPDM='%s' and FPHM=%u", pInvhead->m_fpdm.c_str(), pInvhead->m_fphm);
	DBG_PRINT(("sqlbuf = %s", sqlbuf));
	pInvhead->m_filter.append(sqlbuf);
	if(pInvhead->Load() != SQLITE_OK)
	{
		DBG_PRINT(("here find by jsp"));
		//数据库查询不到,从盘中查询
		retcode = CJSKInfoFunc::GetInvDetailInfoNO(pInvhead, strErr);
		if(retcode != JSK_SUCCESS)
		{
			return retcode;
		}

		pInvhead->Save();
	}

	//已开发票作废控制
	if(pInvhead->m_kplx == JSK_NORMAL_INV)
	{
		pInvhead->m_kplx = JSK_WASTE_NOR;
	}
	else if(pInvhead->m_kplx == JSK_RETURN_INV)
	{
		pInvhead->m_kplx = JSK_WASTE_RET;
	}
	else
	{
		retcode = JSK_COMMON_ERR_NO;
		strErr = "废票不能再次作废!";
		return retcode;
	}

	//权限控制
	retcode = MakeInvAuthority(pInvhead, strErr);
	if( retcode != JSK_SUCCESS )
	{
		return retcode;
	}

	retcode = CJSKInfoFunc::WasteInvHandInfo(pInvhead, strErr);
	if( retcode != JSK_SUCCESS )
	{
		return retcode;
	}

	//数据库事务开始
	pInvhead->BeginTransaction();

	//修改数据库对应信息
	tmpInvHead.m_kplx = pInvhead->m_kplx;
	tmpInvHead.m_fpsyh = pInvhead->m_fpsyh;
	tmpInvHead.m_casign = pInvhead->m_casign;
	tmpInvHead.m_zfsj = pInvhead->m_zfsj;
	retcode = tmpInvHead.Update(sqlbuf, &tmpInvHead.m_kplx, &tmpInvHead.m_fpsyh, &tmpInvHead.m_zfsj, &tmpInvHead.m_casign, NULL);
	DBG_PRINT(("更新DB的INV_HEAD:retcode = %d", retcode));
	CInvDet invDet;
	invDet.m_kplx = pInvhead->m_kplx;
	retcode = invDet.Update(sqlbuf, &invDet.m_kplx, NULL);
	DBG_PRINT(("更新DB的INV_DET:retcode = %d", retcode));

	if(pInvhead->m_kplx == JSK_WASTE_RET )
	{
		DBG_PRINT(("更新RT_INV表"));
		CRtInv curRtInv;
		CRtInv *pRtInv = &curRtInv;
		memset(sqlbuf, 0, sizeof(sqlbuf));
		sprintf(sqlbuf, "where FPDM='%s' and FPHM=%u ", pInvhead->m_yfpdm.c_str(), pInvhead->m_yfphm);
		DBG_PRINT(("sqlbuf = %s", sqlbuf));
		pRtInv->m_filter.append(sqlbuf);
		pRtInv->Delete();
	}

	//处理INV_SERV表,如果已上传发票,需要重新记录INV_SERV???
	CInvServ invServ;
	errorcode = invServ.Save(pInvhead, g_globalArgLib->m_InvServNum, g_globalArgLib->m_InvServSum, strErr);	
	DBG_PRINT(("存储到DB的INV_SERV, errorcode = %u", errorcode));
	
	//数据库事务结束
	pInvhead->EndTransaction();

	return JSK_SUCCESS;
}