/* 
** file name: loadtl.c
**
** Restructure tariff buffer to private shared memory.
**
*/

#include    "../include/includes.h"
#include    "../include/ramdata.h"
#include    "../include/ppsinf.h"
#include    "../include/ppsm.h"
#include    "../include/param.h"

_pps_prefix_node pps_prefix_table[PPS_PREFIX_NODE_NUM];

char	HolidayDes[12][31][32];
extern  _tariff_private	*blf_shmp;
extern  _ram_data	*ramshm_ptr;
static char		prog_name[16];

extern	PPSParamStruct	ppsParam;
extern	_state_machine	*smshm_ptr;

extern int GetMOTariffData();

#ifdef OCS_WITH_CRM
int ocs_create_crm_base_tariff_tree();
#endif
int load_tariff_main()
{
	int	i,j,len,tariffID;
	FILE	*fp=NULL,*fp1=NULL;
	char	bill_str[32];

	_bill_tariff	  *shmp = NULL;


	PutLogFunID("load_tariff_main");

	sprintf(prog_name,"load tariff");

/*
#ifdef OCS_WITH_CRM
	ocs_create_crm_base_tariff_tree();
	return 1;
#endif
*/
	if(OCS_with_external_CRM()==1)
	{
		ocs_create_crm_base_tariff_tree();
		return 1;
	}

		
	memcpy(&blf_shmp->comm_tariff,
		&ramshm_ptr->tariff_data.comm_tariff,
		sizeof(_comm_tariff)*MAX_ZONE_NUM);
	fp = fopen(COMM_TARIFF,"w");
	if(fp !=NULL)
	{
		for(i=0;i<MAX_ZONE_NUM;i++)
		{  
			fprintf(fp,"%d,%d,%f,%d,%f,%d,%f,%d,%f,%f,%d,%f,%d,%f,%d,%f,%d,%f,%d,%f,%f,%d,%f,%f,%f\n",i,
				ramshm_ptr->tariff_data.comm_tariff[i].cfChargeUnit,
				ramshm_ptr->tariff_data.comm_tariff[i].cfChargeAmount,
				ramshm_ptr->tariff_data.comm_tariff[i].roamingChargeUnit,
				ramshm_ptr->tariff_data.comm_tariff[i].roamingChargeAmount,
				ramshm_ptr->tariff_data.comm_tariff[i].ccChargeUnit,
				ramshm_ptr->tariff_data.comm_tariff[i].ccChargeAmount,
				ramshm_ptr->tariff_data.comm_tariff[i].cugChargeUnit,
				ramshm_ptr->tariff_data.comm_tariff[i].cugChargeAmount,
				ramshm_ptr->tariff_data.comm_tariff[i].favEditChargeAmount,
				ramshm_ptr->tariff_data.comm_tariff[i].favChargeUnit,
				ramshm_ptr->tariff_data.comm_tariff[i].favChargeAmount,
				ramshm_ptr->tariff_data.comm_tariff[i].cf_cugChargeUnit,
				ramshm_ptr->tariff_data.comm_tariff[i].cf_cugChargeAmount,
				ramshm_ptr->tariff_data.comm_tariff[i].cf_favChargeUnit,
				ramshm_ptr->tariff_data.comm_tariff[i].cf_favChargeAmount,
				ramshm_ptr->tariff_data.comm_tariff[i].cug_favChargeUnit,
				ramshm_ptr->tariff_data.comm_tariff[i].cug_favChargeAmount,
				ramshm_ptr->tariff_data.comm_tariff[i].cf_cug_favChargeUnit,
				ramshm_ptr->tariff_data.comm_tariff[i].cf_cug_favChargeAmount,
				ramshm_ptr->tariff_data.comm_tariff[i].childChangeChargeAmount,
				ramshm_ptr->tariff_data.comm_tariff[i].poolAccountChargeUnit,
				ramshm_ptr->tariff_data.comm_tariff[i].poolAccountChargeAmount,
				ramshm_ptr->tariff_data.comm_tariff[i].activeFreeSMSCharegeAmount,
				ramshm_ptr->tariff_data.comm_tariff[i].deactiveFreeSMSCharegeAmount
				);
		}
 		fclose(fp);
	}
	
	memcpy(blf_shmp->rent_tariff, ramshm_ptr->tariff_data.rent_tariff, 
		MAX_RENT_TARIFF * sizeof(_rent_tariff)*MAX_ZONE_NUM);

	fp = fopen(PARAM_CSV,"w");
	if(fp !=NULL)
	{
		for(i=0;i<MAX_ZONE_NUM;i++)
		for(j=0;j<MAX_RENT_TARIFF;j++)
		{
			if(ramshm_ptr->tariff_data.rent_tariff[i][j].id !=0)
			{
				fprintf(fp,"%d,%d,%s,%d,%d,%f\n",i,
					ramshm_ptr->tariff_data.rent_tariff[i][j].id,
					ramshm_ptr->tariff_data.rent_tariff[i][j].description,
					ramshm_ptr->tariff_data.rent_tariff[i][j].rent_type,
					ramshm_ptr->tariff_data.rent_tariff[i][j].charge_date,
					ramshm_ptr->tariff_data.rent_tariff[i][j].rent_amount);
			}
		}
		fclose(fp);
	}


	memcpy(&blf_shmp->bill_tariff,
		&ramshm_ptr->tariff_data.bill_tariff,
		sizeof(_bill_tariff)*MAX_ZONE_NUM*BILLING_BASE_NUM);
	fp = fopen(BASICTARIFF_CSV,"w");
	
	for(i=0;i<MAX_ZONE_NUM;i++)
	for(j=0;j<BILLING_BASE_NUM;j++)
	{		
		shmp = &ramshm_ptr->tariff_data.bill_tariff[i][j];
		len = strlen(shmp->bill_id);
		if(len == 0 ) break;
 		if(ppsParam.serviceControl.cos_group_flag)
			 tariffID = shmp->tariffID/COS_GROUP_NUM;
		 else
			 tariffID = shmp->tariffID;		
		if(fp !=NULL)
		{
			fprintf(fp,"%s,%s,%d,%d,%d,%d,%d,%d,%f,%d,%d,%ld,%f,%f,%ld,%f,%f,%ld,%f,%f\n",
				shmp->bill_id,
				shmp->area,
				tariffID, //shmp->tariffID,
				shmp->call_type,
				shmp->discountID,
				shmp->free_time,
				shmp->unit_time,
				shmp->mini_unit,				
				shmp->adjustment,
				shmp->cap_flag,
				shmp->tierRating.tierNumber,
				shmp->tierRating.rating[0].units,
				shmp->tierRating.rating[0].base_fee,
				shmp->tierRating.rating[0].long_distance_fee,
				shmp->tierRating.rating[1].units,
				shmp->tierRating.rating[1].base_fee,
				shmp->tierRating.rating[1].long_distance_fee,
				shmp->tierRating.rating[2].units,
				shmp->tierRating.rating[2].base_fee,
				shmp->tierRating.rating[2].long_distance_fee
				);
		}
	}
	if(fp !=NULL) fclose(fp);

    fp = fopen(DISCOUNT_CSV,"w");

	for(i=0;i<MAX_DISCOUNT_ID;i++)
	for(j=0;j<PERIODIC_OF_DAY;j++)
	{
	    memcpy(&blf_shmp->bill_discount[i][j],
			ramshm_ptr->tariff_data.bill_discount[i][j].days,8);
		if(fp !=NULL && ramshm_ptr->tariff_data.bill_discount[i][j].discount_id ==i){
		  fprintf(fp,"%d,%d,%d,%d,%d,%d,%d,%d,%d,%d\n",
				  ramshm_ptr->tariff_data.bill_discount[i][j].discount_id,
				  j,
				  ramshm_ptr->tariff_data.bill_discount[i][j].days[1],
				  ramshm_ptr->tariff_data.bill_discount[i][j].days[2],
				  ramshm_ptr->tariff_data.bill_discount[i][j].days[3],
				  ramshm_ptr->tariff_data.bill_discount[i][j].days[4],
				  ramshm_ptr->tariff_data.bill_discount[i][j].days[5],
				  ramshm_ptr->tariff_data.bill_discount[i][j].days[6],
				  ramshm_ptr->tariff_data.bill_discount[i][j].days[0],
				  ramshm_ptr->tariff_data.bill_discount[i][j].days[7]);
		}
	}
    if(fp !=NULL) fclose(fp);

	for(i=0;i<12;i++)
	for(j=0;j<31;j++)
	{
		blf_shmp->holiday_flag[i][j] = ramshm_ptr->tariff_data.holiday_flag[i][j].mflag;
	}

	memcpy(&blf_shmp->called_tariff,
			&ramshm_ptr->tariff_data.called_tariff,
			MAX_ZONE_NUM*CALLED_BASE_NUM*sizeof(_bill_tariff));
	
	fp1 = fopen(BASICMTTARIFF_CSV,"w");

	if(fp1 !=NULL)
	{
		for(i=0;i<MAX_ZONE_NUM;i++)
			for(j=0;j<CALLED_BASE_NUM;j++)
			{
				shmp = (_bill_tariff *) &ramshm_ptr->tariff_data.called_tariff[i];
				shmp += j;
				sprintf(bill_str, "%s",shmp->bill_id);
				bill_str[16]=0;
				len = strlen(bill_str);
				if (len == 0 && j!=0) continue;
				fprintf(fp1,
					"%s,%s,%d,%d,%d,%d,%d,%d,%f,%d,%ld,%f,%f,%ld,%f,%f,%ld,%f,%f,%d\n",
					shmp->bill_id,
					shmp->area,
					shmp->tariffID,
					shmp->call_type,
					shmp->discountID,
					shmp->free_time,
					shmp->unit_time,
					shmp->mini_unit,					
					shmp->adjustment,
					shmp->tierRating.tierNumber,
					shmp->tierRating.rating[0].units,
					shmp->tierRating.rating[0].base_fee,
					shmp->tierRating.rating[0].long_distance_fee,
					shmp->tierRating.rating[1].units,
					shmp->tierRating.rating[1].base_fee,
					shmp->tierRating.rating[1].long_distance_fee,
					shmp->tierRating.rating[2].units,
					shmp->tierRating.rating[2].base_fee,
					shmp->tierRating.rating[2].long_distance_fee,
					shmp->cap_flag);
			}
	}
	if(fp1 !=NULL) fclose(fp1);

	memcpy(&blf_shmp->bill_sms,
			&ramshm_ptr->tariff_data.bill_sms,
			sizeof(_bill_sms)*MAX_ZONE_NUM*MAX_SMS_PREFIX);
	
	fp = fopen(BILL_SMS_CSV,"w");
	if(fp !=NULL)
	{
		for(i=0;i<MAX_ZONE_NUM;i++)
		for(j=0;j<MAX_SMS_PREFIX;j++)
		{
			if(ramshm_ptr->tariff_data.bill_sms[i][j].id == j+1)
			{
				fprintf(fp,"%d,%s,%d,%s,%d,%d,%f,%d\n",
					ramshm_ptr->tariff_data.bill_sms[i][j].id,
					ramshm_ptr->tariff_data.bill_sms[i][j].bill_id,	
					ramshm_ptr->tariff_data.bill_sms[i][j].call_type,
					ramshm_ptr->tariff_data.bill_sms[i][j].area,
					ramshm_ptr->tariff_data.bill_sms[i][j].tariffID,
					ramshm_ptr->tariff_data.bill_sms[i][j].discountID,
					ramshm_ptr->tariff_data.bill_sms[i][j].base_fee,
					ramshm_ptr->tariff_data.bill_sms[i][j].cap_flag);
			}
		}
	}
	if(fp !=NULL) fclose(fp);

	memcpy(&blf_shmp->gprs_tariff,
			&ramshm_ptr->tariff_data.gprs_tariff,
			sizeof(_gprs_tariff)*MAX_ZONE_NUM*MAX_GPRS_PREFIX);
	
	fp1 = fopen(GPRS_TARIFF_CSV,"w");
	if(fp1 !=NULL)
	{
		struct in_addr	ip_addr;
		u_char ipAsc[64];
					
		for(i=0;i<MAX_ZONE_NUM;i++)
		for(j=0;j<MAX_GPRS_PREFIX;j++)
		{			
			ip_addr.s_addr = ramshm_ptr->tariff_data.gprs_tariff[i][j].destIp;	
			sprintf((char *)ipAsc, "%s", inet_ntoa(ip_addr));
			fprintf(fp1,"%d,%d,%s,%d,%f,%d,%d,%d,%d\n",
				ramshm_ptr->tariff_data.gprs_tariff[i][j].tariffID,
				ramshm_ptr->tariff_data.gprs_tariff[i][j].gprsID,	
				ipAsc,
				ramshm_ptr->tariff_data.gprs_tariff[i][j].chargeUnit,
				ramshm_ptr->tariff_data.gprs_tariff[i][j].basicFee,
				ramshm_ptr->tariff_data.gprs_tariff[i][j].miniChargeUnit,
				ramshm_ptr->tariff_data.gprs_tariff[i][j].initialAdjustment,
				ramshm_ptr->tariff_data.gprs_tariff[i][j].cap_flag,
				ramshm_ptr->tariff_data.gprs_tariff[i][j].discountID);
		}
	}
	if(fp1 !=NULL) fclose(fp1);


	memcpy(&blf_shmp->vas_tariff,
			&ramshm_ptr->tariff_data.vas_tariff,
			sizeof(VAS_TARIFF)*MAX_ZONE_NUM);
	
	fp = fopen(VAS_MMS_SEND_CSV,"w");
	if(fp !=NULL)
	{
		for(i=0;i<MAX_ZONE_NUM;i++)
		for(j=0;j<MAX_VAS_MMS_SEND_TARIFF;j++)
		{			
			fprintf(fp,"%d,%d,%d,%s,%d,%s,%d,%f,%d\n",
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsSendTariff[j].tariffID,
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsSendTariff[j].mmsSendID,	
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsSendTariff[j].mmsType,
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsSendTariff[j].prefix,
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsSendTariff[j].callType,
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsSendTariff[j].destName,
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsSendTariff[j].discountID,
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsSendTariff[j].chargeFee,
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsSendTariff[j].capFlag);
		}
	}
	if(fp !=NULL) fclose(fp);

	fp1 = fopen(VAS_MMS_RETRIEVE_CSV,"w");
	if(fp1 !=NULL)
	{
		for(i=0;i<MAX_ZONE_NUM;i++)
		for(j=0;j<MAX_VAS_MMS_RETRIEVE_TARIFF;j++)
		{			
			fprintf(fp1,"%d,%d,%s,%d,%s,%d,%f,%d\n",
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsRetrieveTariff[j].tariffID,
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsRetrieveTariff[j].mmsRetrieveID,	
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsRetrieveTariff[j].prefix,
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsRetrieveTariff[j].callType,
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsRetrieveTariff[j].destName,
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsRetrieveTariff[j].discountID,
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsRetrieveTariff[j].chargeFee,
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsRetrieveTariff[j].capFlag);
		}
	}
	if(fp1 !=NULL) fclose(fp1);
	
	fp = fopen(VAS_MMS_EMAIL_CSV,"w");
	if(fp !=NULL)
	{
		for(i=0;i<MAX_ZONE_NUM;i++)
		for(j=0;j<MAX_VAS_MMSEMAIL_DELIVER_TARIFF;j++)
		{			
			fprintf(fp,"%d,%d,%s,%d,%s,%d,%f,%d\n",
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsEmailDeliverTariff[j].tariffID,
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsEmailDeliverTariff[j].emailID,	
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsEmailDeliverTariff[j].prefix,
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsEmailDeliverTariff[j].callType,
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsEmailDeliverTariff[j].destName,
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsEmailDeliverTariff[j].discountID,
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsEmailDeliverTariff[j].chargeFee,
				ramshm_ptr->tariff_data.vas_tariff[i].vasMmsEmailDeliverTariff[j].capFlag);
		}
	}
	if(fp !=NULL) fclose(fp);
	
	if(ppsParam.serviceControl.cos_group_flag)
        pps_create_tariff_table();

	return 1;
}

void ListTariffPlan(int tariff_id)
{
    char buf[1024],bill_str[20],area[32];
    _bill_tariff    *shmp = NULL;
	int  num,i=0,j=0,len=0;

	PutLogFunID("ListTariffPlan");
	if(tariff_id>=MAX_ZONE_NUM || tariff_id<0)
	{
		DebugMsg(debugmib.display.asciiOut,"Error tariff ID: %d, should be 0-%d",
			tariff_id,MAX_ZONE_NUM-1);
		return ;
	}
	AsciiTime(buf,ppsParam.fileOperation.nextTariffStartTime);
	DebugMsgDelimiter();
   	DebugMsg(debugmib.display.asciiOut,"New tariff start time: %s!",buf);
	DebugMsgDelimiter();
	DebugMsg(debugmib.display.asciiOut,"Comm tariff:");
	DebugMsg(debugmib.display.asciiOut,"Call forwarded unit: %02d",
	   ramshm_ptr->tariff_data.comm_tariff[tariff_id].cfChargeUnit);
	DebugMsg(debugmib.display.asciiOut,"Call forwarded charge: %f",
	   ramshm_ptr->tariff_data.comm_tariff[tariff_id].cfChargeAmount);
	DebugMsg(debugmib.display.asciiOut,"Roaming charge unit: %02d",
	   ramshm_ptr->tariff_data.comm_tariff[tariff_id].roamingChargeUnit);
	DebugMsg(debugmib.display.asciiOut,"Roaming charge charge: %f",
	   ramshm_ptr->tariff_data.comm_tariff[tariff_id].roamingChargeAmount);
	DebugMsg(debugmib.display.asciiOut,"Conference call unit: %02d",
	   ramshm_ptr->tariff_data.comm_tariff[tariff_id].ccChargeAmount);
	DebugMsg(debugmib.display.asciiOut,"Conference call charge: %f",
	   ramshm_ptr->tariff_data.comm_tariff[tariff_id].ccChargeAmount);
	DebugMsg(debugmib.display.asciiOut,"Closed User Group unit: %02d",
	   ramshm_ptr->tariff_data.comm_tariff[tariff_id].cugChargeUnit);
	DebugMsg(debugmib.display.asciiOut,"Closed User Group charge: %f",
	   ramshm_ptr->tariff_data.comm_tariff[tariff_id].cugChargeAmount);
	DebugMsg(debugmib.display.asciiOut,"Favortie edit charge: %f",
	   ramshm_ptr->tariff_data.comm_tariff[tariff_id].favEditChargeAmount);
	DebugMsg(debugmib.display.asciiOut,"Favorite call unit: %02d",
	   ramshm_ptr->tariff_data.comm_tariff[tariff_id].favChargeUnit);
	DebugMsg(debugmib.display.asciiOut,"Favorite call charge: %f",
	   ramshm_ptr->tariff_data.comm_tariff[tariff_id].favChargeAmount);
	DebugMsg(debugmib.display.asciiOut,"Pool account change charge:	%.04f",
	    ramshm_ptr->tariff_data.comm_tariff[tariff_id].childChangeChargeAmount);
	DebugMsg(debugmib.display.asciiOut,"Pool call unit:		%.02d",
	    ramshm_ptr->tariff_data.comm_tariff[tariff_id].poolAccountChargeUnit);
	DebugMsg(debugmib.display.asciiOut,"Pool call charge:		%.04f",
	    ramshm_ptr->tariff_data.comm_tariff[tariff_id].poolAccountChargeAmount);
	DebugMsg(debugmib.display.asciiOut,"Activate Free SMS charge:	%.04f",
	    ramshm_ptr->tariff_data.comm_tariff[tariff_id].activeFreeSMSCharegeAmount);
	DebugMsg(debugmib.display.asciiOut,"Deactivate Free SMS charge:	%.04f",
	    ramshm_ptr->tariff_data.comm_tariff[tariff_id].deactiveFreeSMSCharegeAmount);
	DebugMsgDelimiter();
    DebugMsg(debugmib.display.asciiOut,"Rent:");
	  for(num=0;num<MAX_RENT_TARIFF;num++)
	  {
	    if(ramshm_ptr->tariff_data.rent_tariff[tariff_id][num].id !=0){
		  DebugMsg(debugmib.display.asciiOut,"%d,%s,%d,%d,%f",
				    ramshm_ptr->tariff_data.rent_tariff[tariff_id][num].id,
					ramshm_ptr->tariff_data.rent_tariff[tariff_id][num].description,
					ramshm_ptr->tariff_data.rent_tariff[tariff_id][num].rent_type,
					ramshm_ptr->tariff_data.rent_tariff[tariff_id][num].charge_date,
					ramshm_ptr->tariff_data.rent_tariff[tariff_id][num].rent_amount);
		}
	  }

   	DebugMsgDelimiter();
 	DebugMsg(debugmib.display.asciiOut,"MO Basic tariff");

	//for(i=0;i<1;i++)
	{
		DebugMsgDelimiter();
		DebugMsg(debugmib.display.asciiOut,"Tariff ID : %02d",tariff_id);		
		for (num=0; num<BILLING_BASE_NUM;++num) {
			shmp = (_bill_tariff *) &ramshm_ptr->tariff_data.bill_tariff[tariff_id];
			shmp += num;
			sprintf(bill_str, "%s",shmp->bill_id);
			bill_str[16]=0;

			len = strlen(bill_str);

			if (len == 0 ) continue;
			sprintf(buf,"Prefix		CT		Area		DID	Free	CUnit	miniCU	Adj.");
			DebugMsg(debugmib.display.asciiOut,"%s",buf);
			
			StringCat(bill_str," ",7);
			strcpy(area,shmp->area);
			StringCat(area," ",8);
			DebugMsg(debugmib.display.asciiOut,
				 "%s	%d	%s	%d	%d	%02d	%d	%f	%d",
				bill_str,
				shmp->call_type,
				area,
				shmp->discountID,
				shmp->free_time,
				shmp->unit_time,
				shmp->mini_unit,				
				shmp->adjustment,
				shmp->cap_flag			
				);

			DebugMsg(debugmib.display.asciiOut,
				 "tierInfo:%d,%d,%.04f,%.04f,%d,%.04f,%.04f,%d,%.04f,%.04f",				
				shmp->tierRating.tierNumber,
				shmp->tierRating.rating[0].units,
				shmp->tierRating.rating[0].base_fee,
				shmp->tierRating.rating[0].long_distance_fee,
				shmp->tierRating.rating[1].units,
				shmp->tierRating.rating[1].base_fee,
				shmp->tierRating.rating[1].long_distance_fee,
				shmp->tierRating.rating[2].units,
				shmp->tierRating.rating[2].base_fee,
				shmp->tierRating.rating[2].long_distance_fee);
		}
    }
	DebugMsgDelimiter();
    DebugMsg(debugmib.display.asciiOut,"SMS Basic tariff");
	DebugMsg(debugmib.display.asciiOut,"ID	Prefix	CT	Area		Discount	BaseFee");
	for(i=0;i<MAX_SMS_PREFIX;i++)
	{
		if(ramshm_ptr->tariff_data.bill_sms[tariff_id][i].id == i+1)
		{
			DebugMsg(debugmib.display.asciiOut,"%d	%d	%s	%d %s	%d	%f	%d",
				ramshm_ptr->tariff_data.bill_sms[tariff_id][i].id,
				ramshm_ptr->tariff_data.bill_sms[tariff_id][i].call_type,
				ramshm_ptr->tariff_data.bill_sms[tariff_id][i].bill_id,	
				ramshm_ptr->tariff_data.bill_sms[i][j].call_type,
				ramshm_ptr->tariff_data.bill_sms[tariff_id][i].area,
				ramshm_ptr->tariff_data.bill_sms[tariff_id][i].discountID,
				ramshm_ptr->tariff_data.bill_sms[tariff_id][i].base_fee,
				ramshm_ptr->tariff_data.bill_sms[tariff_id][i].cap_flag);
		}
	}

	DebugMsgDelimiter();
    DebugMsg(debugmib.display.asciiOut,"MT Basic tariff");

	for(i=0;i<1;i++)
	{
		DebugMsg(debugmib.display.asciiOut,"Tariff ID : %02d",tariff_id);
		DebugMsg(debugmib.display.asciiOut,
			     "Prefix	Area		DID	FreeT	UnitT	MiniU	BaseF	LongF	Adjust");
		for (num=0; num<CALLED_BASE_NUM;++num) 
		{
			shmp = (_bill_tariff *) &ramshm_ptr->tariff_data.called_tariff[tariff_id];
			shmp += num;
			sprintf(bill_str, "%s",shmp->bill_id);
			bill_str[16]=0;

			len = strlen(bill_str);
			
			if (len == 0 && num !=0) continue;

			StringCat(bill_str," ",7);
			strcpy(area,shmp->area);
			StringCat(area," ",8);
			sprintf(buf,"Prefix		CT		Area		DID	Free	CUnit	miniCU	Adj.");
			DebugMsg(debugmib.display.asciiOut,
				 "%s	%d	%s	%d	%d	%02d	%d	%f	%d",
				bill_str,
				shmp->call_type,
				area,
				shmp->discountID,
				shmp->free_time,
				shmp->unit_time,
				shmp->mini_unit,				
				shmp->adjustment,
				shmp->cap_flag);
		}
		
		DebugMsg(debugmib.display.asciiOut,
			 "Tier info:%d,%.04f,%.04f,%d,%.04f,%.04f,%d,%.04f,%.04f",							
			shmp->tierRating.rating[0].units,
			shmp->tierRating.rating[0].base_fee,
			shmp->tierRating.rating[0].long_distance_fee,
			shmp->tierRating.rating[1].units,
			shmp->tierRating.rating[1].base_fee,
			shmp->tierRating.rating[1].long_distance_fee,
			shmp->tierRating.rating[2].units,
			shmp->tierRating.rating[2].base_fee,
			shmp->tierRating.rating[2].long_distance_fee);
    }
 	
	DebugMsgDelimiter();
	DebugMsg(debugmib.display.asciiOut,"Discount: %d",tariff_id);
	for(j=0;j<PERIODIC_OF_DAY;j++)
	{
		  DebugMsg(debugmib.display.asciiOut,"period %02d-%02d,%03d,%03d,%03d,%03d,%03d,%03d,%03d,%03d",
				  j,(j+1)%24,
				  ramshm_ptr->tariff_data.bill_discount[tariff_id][j].days[1],
				  ramshm_ptr->tariff_data.bill_discount[tariff_id][j].days[2],
				  ramshm_ptr->tariff_data.bill_discount[tariff_id][j].days[3],
				  ramshm_ptr->tariff_data.bill_discount[tariff_id][j].days[4],
				  ramshm_ptr->tariff_data.bill_discount[tariff_id][j].days[5],
				  ramshm_ptr->tariff_data.bill_discount[tariff_id][j].days[6],
				  ramshm_ptr->tariff_data.bill_discount[tariff_id][j].days[0],
				  ramshm_ptr->tariff_data.bill_discount[tariff_id][j].days[7]);

	}
	
	DebugMsgDelimiter();
	DebugMsg(debugmib.display.asciiOut,"Holiday:");
	for(i=0;i<12;i++)
	for(j=0;j<31;j++)
	{
		if(ramshm_ptr->tariff_data.holiday_flag[i][j].mflag ){
		 DebugMsg(debugmib.display.asciiOut,"%d ,%d",
		 i+1,
		 j+1);
		}
	}

} 

int ListCurrentTariff(int zone_id)
{
   int i,j,len;
   char buf[4096];
	_bill_tariff *shmp;
	static int debug_state = 0,num=0,listNum=0;
	
	PutLogFunID("ListCurrentTariff");
    if(debug_state == 0)
    {
	    DebugMsgDelimiter();
		DebugMsg(debugmib.display.asciiOut,"Comm tariff:");
		DebugMsg(debugmib.display.asciiOut,"Call forwarded unit:		%02d",
		   blf_shmp->comm_tariff[zone_id].cfChargeUnit);
		DebugMsg(debugmib.display.asciiOut,"Call forwarded charge:		%.04f",
		   blf_shmp->comm_tariff[zone_id].cfChargeAmount);
		DebugMsg(debugmib.display.asciiOut,"Roaming  unit:			%02d",
		   blf_shmp->comm_tariff[zone_id].roamingChargeUnit);
		DebugMsg(debugmib.display.asciiOut,"Roaming  charge:		%.04f",
		   blf_shmp->comm_tariff[zone_id].roamingChargeAmount);
		DebugMsg(debugmib.display.asciiOut,"Conference call unit:		%02d",
		   blf_shmp->comm_tariff[zone_id].ccChargeUnit);
		DebugMsg(debugmib.display.asciiOut,"Conference call charge:		%.04f",
		   blf_shmp->comm_tariff[zone_id].ccChargeAmount);
		DebugMsg(debugmib.display.asciiOut,"Closed User Group unit:		%02d",
		   blf_shmp->comm_tariff[zone_id].cugChargeUnit);
		DebugMsg(debugmib.display.asciiOut,"Closed User Group charge:	%.04f",
		   blf_shmp->comm_tariff[zone_id].cugChargeAmount);
		DebugMsg(debugmib.display.asciiOut,"Favorite change charge:		%.04f",
		   blf_shmp->comm_tariff[zone_id].favEditChargeAmount);
		DebugMsg(debugmib.display.asciiOut,"Favorite call unit:		%02d",
		   blf_shmp->comm_tariff[zone_id].favChargeUnit);
		DebugMsg(debugmib.display.asciiOut,"Favorite call charge:		%.04f",
		   blf_shmp->comm_tariff[zone_id].favChargeAmount);
		DebugMsg(debugmib.display.asciiOut,"Pool account change charge:	%.04f",
		   blf_shmp->comm_tariff[zone_id].childChangeChargeAmount);
		DebugMsg(debugmib.display.asciiOut,"Pool call unit:			%.02d",
		   blf_shmp->comm_tariff[zone_id].poolAccountChargeUnit);
		DebugMsg(debugmib.display.asciiOut,"Pool call charge:		%.04f",
		   blf_shmp->comm_tariff[zone_id].poolAccountChargeAmount);
		DebugMsg(debugmib.display.asciiOut,"Activate Free SMS charge:	%.04f",
		   blf_shmp->comm_tariff[zone_id].activeFreeSMSCharegeAmount);
		DebugMsg(debugmib.display.asciiOut,"Deactivate Free SMS charge:	%.04f",
		   blf_shmp->comm_tariff[zone_id].deactiveFreeSMSCharegeAmount);
		debug_state = 1;
		return 0;
	}
	else if(debug_state == 1)
	{
		DebugMsgDelimiter();
    	DebugMsg(debugmib.display.asciiOut,"Rent:");
    	DebugMsg(debugmib.display.asciiOut,"ID	Desc.	Type	Date	Rent");
		for(num=0;num<MAX_RENT_TARIFF;num++)
		{
			if(ramshm_ptr->tariff_data.rent_tariff[zone_id][num].id !=0)
			{
				DebugMsg(debugmib.display.asciiOut,"%d	%s	%d	%d	%f",
					blf_shmp->rent_tariff[zone_id][num].id,
					blf_shmp->rent_tariff[zone_id][num].description,
					blf_shmp->rent_tariff[zone_id][num].rent_type,
					blf_shmp->rent_tariff[zone_id][num].charge_date,
					blf_shmp->rent_tariff[zone_id][num].rent_amount);
			}
		}		
		num = 0;
		debug_state = 2;
		DebugMsg(debugmib.display.asciiOut,"********************************************************************************");	
		DebugMsg(debugmib.display.asciiOut,"********************************************************************************");	
		return 0;
	}
	else if(debug_state == 2)
	{
	    DebugMsgDelimiter();		
		DebugMsg(debugmib.display.asciiOut,"Baisc MO tariff:");		
		listNum = 0;
		for (;num<BILLING_BASE_NUM;++num) 
		{
			if(listNum >= 10)
				return 0;	
			
			shmp = (_bill_tariff *) &blf_shmp->bill_tariff[zone_id];
			shmp += num;

			len = strlen(shmp->bill_id);

			if (len == 0 ) continue;
			sprintf(buf,"Prefix		CT		Area		DID	Free	CUnit	miniCU	Adj.");
			DebugMsg(debugmib.display.asciiOut,"%s",buf);
			
			DebugMsg(debugmib.display.asciiOut,
				 "%s		%d	%16s	%d	%d	%02d	%d	%.04f",
				shmp->bill_id,
				shmp->call_type,
				shmp->area,
				shmp->discountID,
				shmp->free_time,
				shmp->unit_time,
				shmp->mini_unit,				
				shmp->adjustment);
			DebugMsg(debugmib.display.asciiOut,
				 "tierInfo:L-%d,A-(units:%d,bf:%.04f,lf:%.04f),B-(units:%d,bf:%.04f,lf:%.04f),C-(units:%d,bf:%.04f,lf:%.04f)",				
				shmp->tierRating.tierNumber,
				shmp->tierRating.rating[0].units,
				shmp->tierRating.rating[0].base_fee,
				shmp->tierRating.rating[0].long_distance_fee,
				shmp->tierRating.rating[1].units,
				shmp->tierRating.rating[1].base_fee,
				shmp->tierRating.rating[1].long_distance_fee,
				shmp->tierRating.rating[2].units,
				shmp->tierRating.rating[2].base_fee,
				shmp->tierRating.rating[2].long_distance_fee);
			listNum++;						
		}
		debug_state = 3;
		num = 0;
		DebugMsg(debugmib.display.asciiOut,"********************************************************************************");	
		DebugMsg(debugmib.display.asciiOut,"********************************************************************************");			
		return 0;
	}
	else if(debug_state == 3)
	{
		DebugMsgDelimiter();	 	
		DebugMsg(debugmib.display.asciiOut,"MT tariff:");
		listNum = 0;
		for(;num<CALLED_BASE_NUM;num++)
		{	
			if(listNum >= 10)
				return 0;	
			if(blf_shmp->called_tariff[zone_id][num].bill_id[0] ==0/* && i!=0*/)
				continue;
			{
				sprintf(buf,"Prefix		CT		Area		DID	Free	CUnit	miniCU	Adj.");
				DebugMsg(debugmib.display.asciiOut,"%s",buf);
				
				sprintf(buf,"%s		%d	%16s	%d	%d	%02d	%d	%.04f",
					blf_shmp->called_tariff[zone_id][num].bill_id,
					blf_shmp->called_tariff[zone_id][num].call_type,
					blf_shmp->called_tariff[zone_id][num].area,
					blf_shmp->called_tariff[zone_id][num].discountID,
					blf_shmp->called_tariff[zone_id][num].free_time,
					blf_shmp->called_tariff[zone_id][num].unit_time,
					blf_shmp->called_tariff[zone_id][num].mini_unit,					
					blf_shmp->called_tariff[zone_id][num].adjustment					
					);
				DebugMsg(debugmib.display.asciiOut,"%s",buf);
				DebugMsg(debugmib.display.asciiOut,
				"tierInfo:L-%d,A-(units:%d,bf:%.04f,lf:%.04f),B-(units:%d,bf:%.04f,lf:%.04f),C-(units:%d,bf:%.04f,lf:%.04f)",			   
				blf_shmp->called_tariff[zone_id][num].tierRating.tierNumber,
				blf_shmp->called_tariff[zone_id][num].tierRating.rating[0].units,
				blf_shmp->called_tariff[zone_id][num].tierRating.rating[0].base_fee,
				blf_shmp->called_tariff[zone_id][num].tierRating.rating[0].long_distance_fee,
				blf_shmp->called_tariff[zone_id][num].tierRating.rating[1].units,
				blf_shmp->called_tariff[zone_id][num].tierRating.rating[1].base_fee,
				blf_shmp->called_tariff[zone_id][num].tierRating.rating[1].long_distance_fee,
				blf_shmp->called_tariff[zone_id][num].tierRating.rating[2].units,
				blf_shmp->called_tariff[zone_id][num].tierRating.rating[2].base_fee,
				blf_shmp->called_tariff[zone_id][num].tierRating.rating[2].long_distance_fee);
			}
			listNum++;			
		 }
		debug_state = 4;
		num = 0;
		DebugMsg(debugmib.display.asciiOut,"********************************************************************************");	
		DebugMsg(debugmib.display.asciiOut,"********************************************************************************");	
		return 0;		 
	}
	else if(debug_state == 4)
	{
		DebugMsgDelimiter();		
		DebugMsg(debugmib.display.asciiOut,"Discount:");
		DebugMsg(debugmib.display.asciiOut,
			"Period	Mon.	Tue.	Wed.	Thu.	Fri.	Sat.	Sun.	Hol.");
		for(j=0;j<PERIODIC_OF_DAY;j++)
		{
			DebugMsg(debugmib.display.asciiOut,"%02d-%02d	%03d	%03d	%03d	%03d	%03d	%03d	%03d	%03d",
					j,(j+1)%24,
					blf_shmp->bill_discount[zone_id][j][1],
					blf_shmp->bill_discount[zone_id][j][2],
					blf_shmp->bill_discount[zone_id][j][3],
					blf_shmp->bill_discount[zone_id][j][4],
					blf_shmp->bill_discount[zone_id][j][5],
					blf_shmp->bill_discount[zone_id][j][6],
					blf_shmp->bill_discount[zone_id][j][0],
					blf_shmp->bill_discount[zone_id][j][7]);	
		}
		debug_state = 5;
		return 0;
	}
	else if(debug_state == 5)
	{
		DebugMsgDelimiter();
	    DebugMsg(debugmib.display.asciiOut,"SMS Basic tariff:");
		DebugMsg(debugmib.display.asciiOut,"ID	Prefix		CT		Area		Discount	BaseFee");
		for(i=0;i<MAX_SMS_PREFIX;i++)
		{
			if(blf_shmp->bill_sms[zone_id][i].id == i+1)
			{
				DebugMsg(debugmib.display.asciiOut,"%d	%8s	%d %16s		%d		%.04f",
					blf_shmp->bill_sms[zone_id][i].id,
					blf_shmp->bill_sms[zone_id][i].bill_id,
					blf_shmp->bill_sms[zone_id][i].call_type,
					blf_shmp->bill_sms[zone_id][i].area,
					blf_shmp->bill_sms[zone_id][i].discountID,
					blf_shmp->bill_sms[zone_id][i].base_fee);
			}
		}
		debug_state = 6;
		return 0;
	}
	else if(debug_state == 6)
	{
		struct in_addr	ip_addr;
		u_char ipAsc[64];
		
		DebugMsgDelimiter();
	    DebugMsg(debugmib.display.asciiOut,"Gprs tariff:");
		DebugMsg(debugmib.display.asciiOut,"ID	destIP			Cunit		BaseFee		miniCUnits	adj	capFlag	Discount");
		for(i=0;i<MAX_GPRS_PREFIX;i++)
		{			
			ip_addr.s_addr = blf_shmp->gprs_tariff[zone_id][i].destIp;	
			sprintf((char *)ipAsc, "%s", inet_ntoa(ip_addr));
			DebugMsg(debugmib.display.asciiOut,"%d	%s		%d		%.04f		%d		%d	%d	%d",
				blf_shmp->gprs_tariff[zone_id][i].gprsID,
				ipAsc,
				blf_shmp->gprs_tariff[zone_id][i].chargeUnit,
				blf_shmp->gprs_tariff[zone_id][i].basicFee,
				blf_shmp->gprs_tariff[zone_id][i].miniChargeUnit,
				blf_shmp->gprs_tariff[zone_id][i].initialAdjustment,
				blf_shmp->gprs_tariff[zone_id][i].cap_flag,
				blf_shmp->gprs_tariff[zone_id][i].discountID
				);
		}
		debug_state = 7;
		num = 0;
		return 0;
	}
	else if(debug_state == 7)
	{
		DebugMsgDelimiter();	 	
		DebugMsg(debugmib.display.asciiOut,"VAS MMS Send Tariff");
		listNum = 0;
		for(;num<MAX_VAS_MMS_SEND_TARIFF;num++)
		{	
			if(listNum >= 10)
				return 0;	
			if(strlen((char *)blf_shmp->vas_tariff[zone_id].vasMmsSendTariff[num].prefix) == 0)
				continue;
			{
				sprintf(buf,"mmsType	prefix		calltype	destName		dsID	fee	capFlag");
				DebugMsg(debugmib.display.asciiOut,"%s",buf);
				
				sprintf(buf,"%d	%s		%d		%s			%d	%0.4f	%d",
					blf_shmp->vas_tariff[zone_id].vasMmsSendTariff[num].mmsType,
					blf_shmp->vas_tariff[zone_id].vasMmsSendTariff[num].prefix,
					blf_shmp->vas_tariff[zone_id].vasMmsSendTariff[num].callType,
					blf_shmp->vas_tariff[zone_id].vasMmsSendTariff[num].destName,
					blf_shmp->vas_tariff[zone_id].vasMmsSendTariff[num].discountID,
					blf_shmp->vas_tariff[zone_id].vasMmsSendTariff[num].chargeFee,
					blf_shmp->vas_tariff[zone_id].vasMmsSendTariff[num].capFlag				
					);				
				DebugMsg(debugmib.display.asciiOut,"%s",buf);
			}
			listNum++;			
		 }
		debug_state = 8;
		num = 0;
		DebugMsg(debugmib.display.asciiOut,"********************************************************************************");	
		return 0;		 
	}
	else if(debug_state == 8)
	{		
		DebugMsgDelimiter();
	    DebugMsg(debugmib.display.asciiOut,"MMS retrieve tariff:");
		for(;num<MAX_VAS_MMS_RETRIEVE_TARIFF;num++)
		{				
			sprintf(buf,"prefix		calltype	destName			dsID	fee	capFlag");
			DebugMsg(debugmib.display.asciiOut,"%s",buf);
				
			sprintf(buf,"%s		%d		%s				%d	%0.4f	%d",
					blf_shmp->vas_tariff[zone_id].vasMmsRetrieveTariff[num].prefix,
					blf_shmp->vas_tariff[zone_id].vasMmsRetrieveTariff[num].callType,
					blf_shmp->vas_tariff[zone_id].vasMmsRetrieveTariff[num].destName,
					blf_shmp->vas_tariff[zone_id].vasMmsRetrieveTariff[num].discountID,
					blf_shmp->vas_tariff[zone_id].vasMmsRetrieveTariff[num].chargeFee,
					blf_shmp->vas_tariff[zone_id].vasMmsRetrieveTariff[num].capFlag				
				);				
			DebugMsg(debugmib.display.asciiOut,"%s",buf);
		 }
		debug_state = 9;
		num = 0;
		return 0;
	}
	else if(debug_state == 9)
	{		
		DebugMsgDelimiter();
	    DebugMsg(debugmib.display.asciiOut,"MMS Email tariff:");
		for(;num<MAX_VAS_MMSEMAIL_DELIVER_TARIFF;num++)
		{				
			sprintf(buf,"prefix		calltype	destName			dsID	fee	capFlag");
			DebugMsg(debugmib.display.asciiOut,"%s",buf);
				
			sprintf(buf,"%s		%d	%s					%d	%0.4f	%d",
					blf_shmp->vas_tariff[zone_id].vasMmsEmailDeliverTariff[num].prefix,
					blf_shmp->vas_tariff[zone_id].vasMmsEmailDeliverTariff[num].callType,
					blf_shmp->vas_tariff[zone_id].vasMmsEmailDeliverTariff[num].destName,
					blf_shmp->vas_tariff[zone_id].vasMmsEmailDeliverTariff[num].discountID,
					blf_shmp->vas_tariff[zone_id].vasMmsEmailDeliverTariff[num].chargeFee,
					blf_shmp->vas_tariff[zone_id].vasMmsEmailDeliverTariff[num].capFlag				
				);				
			DebugMsg(debugmib.display.asciiOut,"%s",buf);
		 }
		debug_state = 10;
		num = 0;
		return 0;
	}
	else if(debug_state == 10)
	{
	    DebugMsgDelimiter();			
		DebugMsg(debugmib.display.asciiOut,"Holiday:");
		DebugMsg(debugmib.display.asciiOut,"Month - Day");
		for(i=0;i<12;i++)
		for(j=0;j<31;j++)
		{
			if(blf_shmp->holiday_flag[i][j]){
			 DebugMsg(debugmib.display.asciiOut,"%02d    - %02d",
			 i+1,
			 j+1);
			}
		}	
		debug_state = 0;
		return 1;
	}
	return 1;
}

void ListCurrentTariffWithMsisdn(char *number)
{
   char buf[4096];

   _bill_tariff  ptar;

   int zone_id;

	PutLogFunID("ListCurrentTariffWithMsisdn");

   for(zone_id=0;zone_id<MAX_ZONE_NUM;zone_id++)
   {
    GetMOTariffData(NULL,&ptar,number,zone_id);
    DebugMsg(debugmib.display.asciiOut,"LAC=%d",zone_id);
	DebugMsg(debugmib.display.asciiOut,"DID	CUnit(s)	miniCU	Adjustment");
	DebugMsg(debugmib.display.asciiOut,"%s",buf);							
			
	sprintf(buf,"%d	%02d	%d	%d	%f",
			ptar.discountID,
			ptar.free_time,
			ptar.unit_time,
			ptar.mini_unit,				  
			ptar.adjustment				
			);
	DebugMsg(debugmib.display.asciiOut,"%s",buf);
	DebugMsg(debugmib.display.asciiOut,
		"tierInfo:L-%d,A-(units:%d,bf:%.04f,lf:%.04f),B-(units:%d,bf:%.04f,lf:%.04f),C-(units:%d,bf:%.04f,lf:%.04f)",			   
			ptar.tierRating.tierNumber,
			ptar.tierRating.rating[0].units,
			ptar.tierRating.rating[0].base_fee,
			ptar.tierRating.rating[0].long_distance_fee,
			ptar.tierRating.rating[1].units,
			ptar.tierRating.rating[1].base_fee,
			ptar.tierRating.rating[1].long_distance_fee,
			ptar.tierRating.rating[2].units,
			ptar.tierRating.rating[2].base_fee,
			ptar.tierRating.rating[2].long_distance_fee);
     DebugMsg(debugmib.display.asciiOut,"%s",buf);
   }//for
}

/* the ZONE NUM is as a prefix or a separate table is for further study,
if the ZONE NUM is as prefix , then should not be larger than 16 */
/*
_pps_prefix_node pps_prefix_table[PPS_PREFIX_NODE_NUM];
*/
static int  pps_prefix_node_ptr =  MAX_ZONE_NUM_NEW+1;

int  pps_add_charge_prefix(u_short iroot, char *prefix_digits, u_char len, u_short table_index)
{
    u_char  digit = prefix_digits[0]-0x30;
    u_short isub;


    if(prefix_digits[0]>=0x41 && prefix_digits[0]<=0x45)
    {
        digit = prefix_digits[0]-0x41+0x0a;
    }
    
    if(len == 0 || digit >= 0x0E)
    {
        return 0;
    }
    else
    {
        if(pps_prefix_table[iroot].sub[digit] == 0)
        {
            if(pps_prefix_node_ptr < PPS_PREFIX_NODE_NUM - 1)
                pps_prefix_table[iroot].sub[digit] = pps_prefix_node_ptr++;
            else
            {
                printf("Fail to build prefix table, resource limit\n");
                return 0;       //resource limit
            }
        }

        isub = pps_prefix_table[iroot].sub[digit];
        pps_prefix_table[iroot].flag |= 0x02;

        if(len == 1 )// || (prefix_digits[1]-0x30) >= 0x0E)
        {
            if((pps_prefix_table[isub].flag & 0x01) == 0x01)
            {
                DebugMsg(debugmib.display.asciiOut,"duplicated prefix found: table index: %d",
                    table_index);
            }
            pps_prefix_table[isub].flag |= 0x01;
            pps_prefix_table[isub].prefix_index = table_index;
            return 1;
        }
        else
        {
            return pps_add_charge_prefix(isub, &prefix_digits[1], len - 1, table_index);
        }
    }
    return 0;
}

int  pps_create_tariff_table()
{
    int iroot=0,len=0;
    int index=0;
    char tmpBuf[128];


    pps_prefix_node_ptr =  MAX_ZONE_NUM_NEW+1;
    memset(&pps_prefix_table,0,sizeof(_pps_prefix_node)*PPS_PREFIX_NODE_NUM);

    for(iroot=0;iroot<MAX_ZONE_NUM;iroot++)
    {
        for(index=0;index<BILLING_BASE_NUM;index++)
        {
            strcpy(tmpBuf,
                ramshm_ptr->tariff_data.bill_tariff[iroot][index].bill_id);

            len = strlen(tmpBuf);

            if(len==0 )
                continue;
            if(len>TARIFF_PREFIX_LEN)
                len = TARIFF_PREFIX_LEN;
            pps_add_charge_prefix(iroot/COS_GROUP_NUM,
                    tmpBuf,
                    //ramshm_ptr->tariff_data.bill_tariff[iroot][index].bill_id,
                    len,
                    iroot*BILLING_BASE_NUM+index);
        }
    }
    DebugMsg(debugmib.display.asciiOut,"prefix ptr=%d",pps_prefix_node_ptr);
    return 1;
}

int pps_search_charge_prefix(u_short iroot, char *digits, u_char len)
{
    short   prefix_index=-1;
    unsigned short iNode=0;
    u_char  digit=digits[0]-0x30;


    if(digits[0]>=0x41 && digits[0]<=0x45)
    {
        digit = digits[0]-0x41+0x0a;
    }

    iNode = pps_prefix_table[iroot].sub[digit];

    if(len == 0 || digit>13 || iNode>= PPS_PREFIX_NODE_NUM)
        return prefix_index;

    if(len == 1)
    {
        if((pps_prefix_table[iNode].flag & 0x01) != 0x00)
            prefix_index = pps_prefix_table[iNode].prefix_index;
        return prefix_index;
    }
    else
    {
        if((pps_prefix_table[iNode].flag & 0x02) == 0x00)
        {
            if((pps_prefix_table[iNode].flag & 0x01) != 0x00)
            {
                prefix_index = pps_prefix_table[iNode].prefix_index;
            }
            return prefix_index;
        }
        if(iNode < iroot)
            return prefix_index;

        prefix_index = pps_search_charge_prefix(iNode, &digits[1],len - 1);
        if(prefix_index<0 && (pps_prefix_table[iNode].flag & 0x01) == 0x01)
        {
            prefix_index = pps_prefix_table[iNode].prefix_index;
        }
        return prefix_index;
    }
    return prefix_index;
}

int  pps_get_charge_prefix(u_char cosID,char *dialing_number)
{
    int len;

    len = strlen(dialing_number);
    if(len==0)
        return -1;
    /*
    if(len>TARIFF_PREFIX_LEN)
        len = TARIFF_PREFIX_LEN;
    */
    return pps_search_charge_prefix(cosID, dialing_number, len);
}                                                                                             

#ifdef OCS_WITH_CRM
int ocs_create_crm_base_tariff_tree()
{
	int iroot=0,len=0;
	int index=0;
	char tmpBuf[128];


	pps_prefix_node_ptr =  1;
	memset(&pps_prefix_table,0,sizeof(_pps_prefix_node)*PPS_PREFIX_NODE_NUM);

	for(index=0;index<BILLING_BASE_NUM;index++)
	{

		strcpy(tmpBuf, ramshm_ptr->tariff_data.base_tariff[index].bill_id);

		len = strlen(tmpBuf);
		if(len==0 )
			continue;
		if(len>TARIFF_PREFIX_LEN)
			len = TARIFF_PREFIX_LEN;

		pps_add_charge_prefix(iroot, tmpBuf, len, index);
    }

	DebugMsg(debugmib.display.asciiOut,"prefix ptr=%d",pps_prefix_node_ptr);

	return 1;
	
}


int  ocs_get_charge_prefix(char *dialed_number, char *prefix, char *area_name, int *call_type)
{
	int len;
	int index = -1;

	len = strlen(dialed_number);
	if(len==0)
		return 0;
   
	index = pps_search_charge_prefix(0, dialed_number, len);
	if(index == -1)
		return 0;

	strcpy(prefix, ramshm_ptr->tariff_data.base_tariff[index].bill_id);
	strcpy(area_name, ramshm_ptr->tariff_data.base_tariff[index].area);
	*call_type = ramshm_ptr->tariff_data.base_tariff[index].call_type;
	
	return 1;
	
}                                                                                             

#endif
