//---------------------------------------------------------------------------
// 创业HIS：注意本脚本已定义了三个变量！
//---------------------------------------------------------------------------
// String _opacsconnection; 约定的输入变量名
// String _hisconnection; 	约定的输入变量名
// String _modalitytype; 	约定的输入变量名
// TADOQuery _qryHisCommon  外部已定义的对象
//---------------------------------------------------------------------------
// 已实现函数
//---------------------------------------------------------------------------
// String GetBodyPart(float fJCDXH);
// String GetDeptName(String sKDKS);
// float GetPrimaryID();
// double GetPrepayments(float fPatientNo);
// double GetTotalCharges(float fPatientNo);
// double CalcSpaceMoney(float fPatientNo);
// String GetSystemType(float fJCDXH);
// String GetKDKSCode(float fJCDXH);
// String GetZXKSCode(float fJCDXH);
// String GetUserID(String sUserName);
// String GetDoctorID(float fJCDXH);
// bool PatsInHospital(float fPatientNo);
// bool PatsIsConfirm(float fJCDXH);
// bool PatsIsCancel(float fJCDXH);
// bool PatsIsRefund(float fJCDXH);
// bool PatsIsCharging(float fJCDXH, float fPatientNo);
//
// BILL_DETAIL表
// double CalcTotalExamCharge(TADOQuery qryBillDetail);
// double CalcTotalExamCost(TADOQuery qryBillDetail);
// String CalcTotalExamItem(TADOQuery qryBillDetail);
//---------------------------------------------------------------------------

#include "..\\Common\\HisCommon.cpp"

String      _opacsconnection;
String      _hisconnection;
String 		_modalitytype;

//---------------------------------------------------------------------------

String GetBodyPart(float fJCDXH)
{
	// 获取检查部位
	String sHisQuery = "SELECT XMMC FROM YZ_JCSQXM WHERE JCDXH = " + FloatToStr(fJCDXH);
	_qryHisCommon.Close();
	_qryHisCommon.SQL.Text = sHisQuery;
	_qryHisCommon.Open();
	////
	String sBodyPart = "";
	if (!_qryHisCommon.IsEmpty())
	{	
		_qryHisCommon.First();	
		while (!_qryHisCommon.Eof)
		{
			sBodyPart += _qryHisCommon.FieldByName("XMMC").AsString + " ";
			_qryHisCommon.Next();
		}
	}
			
	return sBodyPart;
}
//---------------------------------------------------------------------------

String GetDeptName(String sKSDM)
{
	// 获取科室名称
    String sQuery = "SELECT KSMC FROM GY_KSDM WHERE KSDM = '" + sKSDM + "'";
    _qryHisCommon.Close();
    _qryHisCommon.SQL.Text = sQuery;
    _qryHisCommon.Open();
    String sDeptName = "";
    if (!_qryHisCommon.IsEmpty())
    	sDeptName = _qryHisCommon.FieldByName("KSMC").AsString;
    	
    return sDeptName;
}
//---------------------------------------------------------------------------

float GetPrimaryID()
{
	// 获取唯一ID
	if (IsConnetAccessDB(_hisconnection))
	{
		return 1;
	}
    String sQuery = "SELECT zjhis.seq_l_charge.nextval AS PRIMARYID FROM DUAL";
    _qryHisCommon.Close();
    _qryHisCommon.SQL.Text = sQuery;
    _qryHisCommon.Open();
    ////
    float fPrimaryID = 0;
    if (!_qryHisCommon.IsEmpty())
        fPrimaryID = _qryHisCommon.FieldByName("PRIMARYID").Value;
		
    return fPrimaryID;
}
//---------------------------------------------------------------------------

String GetSystemType(float fJCDXH)
{
	String sQuery = "";
	String sDeptCode = GetZXKSCode(fJCDXH); // 执行科室代码
	if (IsConnetAccessDB(_hisconnection))
	{
		sQuery = "SELECT MID(CSMC,4,2) AS SYSTEMTYPE FROM GY_XTCS WHERE XTXH = 11 AND CSZ2 = " + sDeptCode;
	}
	else
	{
		sQuery = "SELECT SUBSTR(CSMC,4,2) AS SYSTEMTYPE FROM GY_XTCS WHERE XTXH = 11 AND CSZ2 = " + sDeptCode;
	}
	_qryHisCommon.Close();
	_qryHisCommon.SQL.Text = sQuery;
	_qryHisCommon.Open();
	String sSystemType = "";
	if (!_qryHisCommon.IsEmpty())
		sSystemType = _qryHisCommon.FieldByName("SYSTEMTYPE").AsString;
	
	return sSystemType;
} 
//---------------------------------------------------------------------------

String GetKDKSCode(float fJCDXH)
{
	// 获取开单科室代码
	String sQuery = "SELECT KDKS FROM YZ_JCSQD WHERE JCDXH=" + FloatToStr(fJCDXH);
	_qryHisCommon.Close();
	_qryHisCommon.SQL.Text = sQuery;
	_qryHisCommon.Open();
	String sDeptID = "";
	if (!_qryHisCommon.IsEmpty())
		sDeptID = _qryHisCommon.FieldByName("KDKS").AsString;
    return sDeptID;
}
//---------------------------------------------------------------------------

String GetZXKSCode(float fJCDXH)
{
	// 获取执行科室代码
	String sQuery = "SELECT ZXKS FROM YZ_JCSQD WHERE JCDXH=" + FloatToStr(fJCDXH);
	_qryHisCommon.Close();
	_qryHisCommon.SQL.Text = sQuery;
	_qryHisCommon.Open();
	String sDeptID = "";
	if (!_qryHisCommon.IsEmpty())
		sDeptID = _qryHisCommon.FieldByName("ZXKS").AsString;
    return sDeptID;
}
//---------------------------------------------------------------------------

String GetUserID(String sUserName)
{
    // 获取USER_ID
    String sQuery = "SELECT ZGID FROM GY_ZGXX WHERE XM = '" + sUserName + "'";
    _qryHisCommon.Close();
    _qryHisCommon.SQL.Text = sQuery;
    _qryHisCommon.Open();
    String sUserID = "0000";
    if (!_qryHisCommon.IsEmpty())
        sUserID = _qryHisCommon.FieldByName("ZGID").AsString;
        
    return sUserID;
}
//---------------------------------------------------------------------------

String GetDoctorID(float fJCDXH)
{
	// 获取开单医生ID
	String sQuery = "SELECT KDYSGH FROM YZ_JCSQD WHERE JCDXH = " + FloatToStr(fJCDXH);
    _qryHisCommon.Close();
    _qryHisCommon.SQL.Text = sQuery;
    _qryHisCommon.Open();
    String sDoctorID = "0000";
    if (!_qryHisCommon.IsEmpty())
        sDoctorID = _qryHisCommon.FieldByName("KDYSGH").AsString;
        
    return sDoctorID;
}
//---------------------------------------------------------------------------

bool PatsInHospital(float fPatientNo)
{
    // 病人是否出院
	String sQuery = "";
	if (IsConnetAccessDB(_hisconnection))
	{
 		sQuery = "SELECT * FROM ZY_PATIENT_INFORMATION WHERE OUT_DATE IS NULL AND PATIENT_NO=" + FloatToStr(fPatientNo);
 	}
 	else
 	{
    	sQuery = "SELECT * FROM ZJHIS.ZY_PATIENT_INFORMATION WHERE OUT_DATE IS NULL AND PATIENT_NO=" + FloatToStr(fPatientNo);
    }
    _qryHisCommon.Close();
    _qryHisCommon.SQL.Text = sQuery;
    _qryHisCommon.Open();
    bool bInHospital = false;
    if (!_qryHisCommon.IsEmpty())
        bInHospital = true;

    return bInHospital;
}
//---------------------------------------------------------------------------

bool PatsIsConfirm(float fJCDXH)
{
	String sHisQuery = "SELECT * FROM YZ_JCSQD WHERE STATUS=4 AND DJBZ=1 AND (BINGRENLX='I' OR BINGRENLX='Y')";
	sHisQuery = AddWhereCondition(sHisQuery, "JCDXH=" + FloatToStr(fJCDXH));
	_qryHisCommon.Close();
	_qryHisCommon.SQL.Text = sHisQuery;
	_qryHisCommon.Open();
	if (_qryHisCommon.IsEmpty())
		return false;

	return true;
}
//---------------------------------------------------------------------------

bool PatsIsCancel(float fJCDXH)
{
	String sHisQuery = "SELECT * FROM YZ_JCSQD WHERE DJBZ=2 AND (BINGRENLX='I' OR BINGRENLX='Y')";
	sHisQuery = AddWhereCondition(sHisQuery, "JCDXH=" + FloatToStr(fJCDXH));
	_qryHisCommon.Close();
	_qryHisCommon.SQL.Text = sHisQuery;
	_qryHisCommon.Open();
	if (_qryHisCommon.IsEmpty())
		return false;

	return true;
}
//---------------------------------------------------------------------------

bool PatsIsRefund(float fJCDXH)
{
	String sHisQuery = "SELECT * FROM YZ_JCSQD WHERE DJBZ=9 AND (BINGRENLX='I' OR BINGRENLX='Y')";
	sHisQuery = AddWhereCondition(sHisQuery, "JCDXH=" + FloatToStr(fJCDXH));
	_qryHisCommon.Close();
	_qryHisCommon.SQL.Text = sHisQuery;
	_qryHisCommon.Open();
	if (_qryHisCommon.IsEmpty())
		return false;

	return true;
}
//---------------------------------------------------------------------------

bool PatsIsCharging(float fJCDXH, float fPatientNo)
{
	String sSystemType = GetSystemType(fJCDXH);
	String sHisQuery = "SELECT * FROM GY_CHARGE_INTERFACE";
	sHisQuery = AddWhereCondition(sHisQuery, "JCDXH=" + FloatToStr(fJCDXH));
	sHisQuery = AddWhereCondition(sHisQuery, "PATIENTNO=" + FloatToStr(fPatientNo));
	sHisQuery = AddWhereCondition(sHisQuery, "SYSTEMTYPE='" + sSystemType + "'");
	_qryHisCommon.Close();
	_qryHisCommon.SQL.Text = sHisQuery;
	_qryHisCommon.Open();
	if (_qryHisCommon.IsEmpty())
		return false;
        
	return true;
}
//---------------------------------------------------------------------------

double GetPrepayments(float fPatientNo)
{
    // 获取预交款
    String sQuery = "SELECT SUM(AMOUNT) AS PREPAYMENTS FROM ZY_DEPOSIT_FILE "+
		"WHERE PATIENT_NO = " + FloatToStr(fPatientNo) + " AND JS_NO = 0";
    _qryHisCommon.Close();
    _qryHisCommon.SQL.Text = sQuery;
    _qryHisCommon.Open();
    ////
    double dbPrepayments = 0.0;
    if (!_qryHisCommon.IsEmpty())
        dbPrepayments = _qryHisCommon.FieldByName("PREPAYMENTS").AsFloat;
		
    return dbPrepayments;
}
//---------------------------------------------------------------------------

double GetTotalCharges(float fPatientNo)
{
    // 消费总额
    String sQuery = "SELECT SUM(ROUND(DJ*SL, 2)) AS TOTAL_CHARGES FROM ZY_DETAIL_CHARGE "+
		"WHERE PATIENT_NO = " + FloatToStr(fPatientNo) + " AND JS_NO = 0";
    _qryHisCommon.Close();
    _qryHisCommon.SQL.Text = sQuery;
    _qryHisCommon.Open();
    ////
    double dbTotalCharges = 0.0;
    if (!_qryHisCommon.IsEmpty())
        dbTotalCharges = _qryHisCommon.FieldByName("TOTAL_CHARGES").AsFloat;
		
    return dbTotalCharges;
}
//---------------------------------------------------------------------------

double CalcSpaceMoney(float fPatientNo)
{
    // 获取预交款余额
    double fSpaceMoney = GetPrepayments(fPatientNo) - GetTotalCharges(fPatientNo);

    return fSpaceMoney;
}

//---------------------------------------------------------------------------
// BILL_DETAIL表
//---------------------------------------------------------------------------

double CalcTotalExamCharge(TADOQuery qryBillDetail)
{
	// 获取“费用明细”中检查项目的总应付款
	double fExamCharge = 0.0;
    for (int i = 0; i < GetRecordCount(qryBillDetail); i++)
    {
    	SetRecNo(qryBillDetail, i+1);
        fExamCharge += qryBillDetail.FieldByName("bill_examcharge").AsFloat;
    }
    return fExamCharge;
} 
//---------------------------------------------------------------------------

double CalcTotalExamCost(TADOQuery qryBillDetail)
{
	// 获取“费用明细”中检查项目的总应付款
	double fExamCost = 0.0;
    for (int i = 0; i < GetRecordCount(qryBillDetail); i++)
    {
    	SetRecNo(qryBillDetail, i+1);
        fExamCost += qryBillDetail.FieldByName("bill_examcost").AsFloat;
    }
    return fExamCost;
} 
//---------------------------------------------------------------------------

String CalcTotalExamItem(TADOQuery qryBillDetail)
{
	// 获取“费用明细”中检查项目的总应付款
	String sExamItem = "";
    for (int i = 0; i < GetRecordCount(qryBillDetail); i++)
    {
    	SetRecNo(qryBillDetail, i+1);
        sExamItem = sExamItem +  " "  + qryBillDetail.FieldByName("bill_examname").AsString;
    }
    return Trim(sExamItem);
} 
//---------------------------------------------------------------------------

String ConvertJZKH(String sJCKH)
{
	/*
	1.	老医保卡磁条信息37位字符：636014270355943788=030453000000000000
	方法：取=号前8位字符。
	2.	新医保卡：000050197912100022=000000000000000000
	方法：取=号前12位字符。
	注意：新老医保卡的磁条信息都是37位字符，但=号前后的位数不一样：新的左边3位都是0，=号前12位可以保证唯一（8位生日+4位区别号）；
	关键：如何区分新、老医保卡，并分别自动取号。医院认为左边前3位都是000则为新卡。
	3.	医院自制就诊卡8位字符：00473231
	方法：全取。
	4.	城镇居民医疗保险卡34位字符：3305010000170761200501000017076120
	方法：从左边第三位开始取16位字符。（注意：后面的16位其实和中间是重复的）
	5.	农保卡是用医院自制就诊卡8位字符进行绑定。
	方法：和3医院就诊卡相同。
	*/
	String sResult = "";
	int iPos = Pos("=", sJCKH);
	if (iPos > 0)
	{
		sJCKH = Copy(sJCKH, 1, iPos -1);	
	}
	int iLength = Length(sJCKH);
	String sPreFix = "";
	if (iLength == 18)			// 	37位新老卡
	{
		sPreFix = Copy(sJCKH, 1, 3);
		if (sPreFix == "000")	//	新卡
		{
			sResult = Copy(sJCKH, 7, 12);	
		}
		else
		{
			sResult = Copy(sJCKH, 11, 8);
		}
	}
	else if (iLength == 19)			
	{
		sPreFix = Copy(sJCKH, 1, 3);
		if (sPreFix == "000")	//	新卡
		{
			sResult = Copy(sJCKH, 8, 12);	
		}
		else
		{
			sResult = Copy(sJCKH, 11, 8);
		}
	}
	else if (iLength == 34)
	{
		sResult = Copy(sJCKH, 3, 16);
	}
	else 
	{
		sResult = sJCKH;
	}
	return sResult;	
	
}
//---------------------------------------------------------------------------

// 主程序
{
    // Nothing need to do.
}
//---------------------------------------------------------------------------
