/**********************************************************************
 * fm_pymt_pol_spec_collect_get_ach()
 * Get ACH information
 **********************************************************************/
static void
fm_pymt_pol_spec_collect_get_ach(
	pcm_context_t	*ctxp,
	int32		flags,
	pin_flist_t	*i_flistp,
	pin_flist_t	*o_flistp,
	pin_errbuf_t	*ebufp)
{

	void		*vp		= NULL;
	pin_flist_t	*flistp		= NULL;
	pin_flist_t	*p_flistp	= NULL;
	pin_flist_t	*rp_flistp	= NULL;
	pin_flist_t	*a_flistp	= NULL;
	pin_flist_t	*ra_flistp	= NULL;
	int32		rec_id		= 0;
	poid_t		*p_pdp		= NULL;
	poid_t		*a_pdp		= NULL;

	if (PIN_ERR_IS_ERR(ebufp)) {
		return;
	}

	PIN_ERR_CLEAR_ERR(ebufp);

	p_pdp = PIN_FLIST_FLD_GET(i_flistp, PIN_FLD_PAYINFO_OBJ, 0, ebufp);
        a_pdp = PIN_FLIST_FLD_GET(i_flistp, PIN_FLD_POID, 0, ebufp);
	
	/** read the payinfo to get the ach pointer. **/
	p_flistp = PIN_FLIST_CREATE(ebufp);
	PIN_FLIST_FLD_SET(p_flistp, PIN_FLD_POID, p_pdp, ebufp);
	PIN_FLIST_FLD_SET(p_flistp, PIN_FLD_ACH, &rec_id, ebufp);
	
	PCM_OP(ctxp, PCM_OP_READ_FLDS, PCM_OPFLG_READ_UNCOMMITTED, 
					p_flistp, &rp_flistp, ebufp);	

	if (rp_flistp != (pin_flist_t *)NULL) {
		vp = PIN_FLIST_FLD_GET(rp_flistp, PIN_FLD_ACH, 0, ebufp);
		
		if (vp) {
			rec_id = *(int32 *)vp;
		}
                a_flistp = PIN_FLIST_CREATE(ebufp);
                PIN_FLIST_FLD_SET(a_flistp, PIN_FLD_POID, a_pdp, ebufp);
                PIN_FLIST_FLD_SET(a_flistp, PIN_FLD_ACH,
                                                (void *) &rec_id, ebufp);

                PCM_OP(ctxp, PCM_OP_PYMT_GET_ACH_INFO, 0, a_flistp,
                                                &ra_flistp, ebufp);

                flistp = PIN_FLIST_ELEM_GET(ra_flistp, PIN_FLD_RESULTS, 0,
                                                PIN_ELEMID_ANY, ebufp);

                vp = PIN_FLIST_FLD_GET(flistp, PIN_FLD_MERCHANT, 0, ebufp);
                PIN_FLIST_FLD_SET(o_flistp, PIN_FLD_MERCHANT, vp, ebufp);

                PIN_FLIST_FLD_SET(o_flistp, PIN_FLD_ACH, (void *) &rec_id, ebufp);

                vp = PIN_FLIST_FLD_TAKE(flistp, PIN_FLD_POID_VAL, 0, ebufp);

                flistp = PIN_FLIST_ELEM_ADD(o_flistp, PIN_FLD_BATCH_INFO, 0,
                                                                ebufp);
                PIN_FLIST_FLD_PUT(flistp, PIN_FLD_POID_VAL, vp, ebufp);
	}

	if (PIN_ERR_IS_ERR(ebufp)) {
		PIN_ERR_LOG_EBUF(PIN_ERR_LEVEL_ERROR,
				"fm_pymt_pol_spec_collect_get_ach error", ebufp);
	}

	PIN_FLIST_DESTROY_EX(&p_flistp, NULL);
	PIN_FLIST_DESTROY_EX(&rp_flistp, NULL);
	PIN_FLIST_DESTROY_EX(&a_flistp, NULL);
	PIN_FLIST_DESTROY_EX(&ra_flistp, NULL);

	return;
}
/*******************************************************************
 * fm_pymt_pol_collect_writeoff_account():
 * This function checks for the event type .
 * If event type is /event/billing/writeoff_reversal
 * then it writesoff the account 
 *******************************************************************/
static void
fm_pymt_pol_collect_writeoff_account(
	pcm_context_t		*ctxp,
	pin_flist_t		*i_flistp,
	int			flags,
	pin_errbuf_t		*ebufp)
{
	pin_flist_t		*flistp = NULL;
	pin_flist_t		*wa_flistp = NULL;
	pin_flist_t		*r_flistp = NULL;
	char			*writeoff_event_type = { "" };
	int			result_flag = 0;
	void			*vp = NULL;
	int32			rec_id;
	pin_cookie_t		cookie = NULL;
	int			temp = 0;
	u_int			count = 0;

	if (PIN_ERR_IS_ERR(ebufp))
		return;
	PIN_ERR_CLEAR_ERR(ebufp);

	/***********************************************************
	 * Check for the event type /event/billing/writeoff_reversal
	 ***********************************************************/
	cookie = NULL;
	while ((flistp = PIN_FLIST_ELEM_GET_NEXT(i_flistp, PIN_FLD_EVENTS,
		&rec_id, 1, &cookie, ebufp)) != (pin_flist_t *)NULL){
		
			vp = PIN_FLIST_FLD_GET( flistp, PIN_FLD_POID, 0, ebufp);

			if (vp != NULL)
				writeoff_event_type = 
					(char *)PIN_POID_GET_TYPE((poid_t *) vp );

			if( writeoff_event_type && strcmp( writeoff_event_type, 
					PIN_OBJ_TYPE_EVENT_WRITEOFF_REVERSAL) == 0 ){
				count++;
				break;		 			
			}
	}

	/*****************************************
	 * Bail out if no WOR performed
	 *****************************************/
	if( !count ) 
		return;
	
	vp = PIN_FLIST_FLD_GET( flistp,
		PIN_FLD_RESULT,	0, ebufp);
	if ( vp != NULL ) {
		result_flag = *(int *)vp;
	}

	if( result_flag == PIN_REVERSE_RES_PASS ) {
		
		/***********************************************************
		 * Writeoff the Account by calling PCM_OP_AR_ACCOUNT_WRITEOFF
		 ***********************************************************/
		wa_flistp = PIN_FLIST_CREATE(ebufp);

		vp = PIN_FLIST_FLD_GET(i_flistp, PIN_FLD_POID, 0, ebufp);
		PIN_FLIST_FLD_SET(wa_flistp, PIN_FLD_POID, vp, ebufp);

		vp = PIN_FLIST_FLD_TAKE(i_flistp, PIN_FLD_PROGRAM_NAME, 0, ebufp);
		PIN_FLIST_FLD_PUT(wa_flistp, PIN_FLD_PROGRAM_NAME, vp, ebufp);
		/*************************************************************
		* Add the String ID and String version for the GL
		*************************************************************/
		temp = PIN_WRITEOFF_FOR_AUTO_WRITEOFF_REVERSAL;
		PIN_FLIST_FLD_SET(wa_flistp, PIN_FLD_STRING_ID, &temp, ebufp);
		temp = PIN_REASON_CODES_CREDIT_REASONS;
		PIN_FLIST_FLD_SET(wa_flistp, PIN_FLD_STR_VERSION, &temp, ebufp);
		
		PIN_ERR_LOG_FLIST(PIN_ERR_LEVEL_DEBUG,
			"fm_pymt_pol_collect_writeoff_account PCM_OP_AR_ACCOUNT_WRITEOFF input flist", 
			wa_flistp);
		PCM_OP(ctxp, PCM_OP_AR_ACCOUNT_WRITEOFF, flags, wa_flistp,
			&r_flistp, ebufp);
		PIN_ERR_LOG_FLIST(PIN_ERR_LEVEL_DEBUG,
			"fm_pymt_pol_collect_writeoff_account PCM_OP_AR_ACCOUNT_WRITEOFF output flist", 
			r_flistp);

		if (PIN_ERR_IS_ERR(ebufp)) {
			PIN_ERR_LOG_EBUF(PIN_ERR_LEVEL_ERROR,
				"fm_pymt_pol_collect_writeoff_account", ebufp);
		}
		
		/***********************************************************
		 * Clean up.
		 ***********************************************************/
		PIN_FLIST_DESTROY_EX(&wa_flistp, NULL);
		PIN_FLIST_DESTROY_EX(&r_flistp, NULL);

	}

	if (PIN_ERR_IS_ERR(ebufp)) {
		PIN_ERR_LOG_EBUF(PIN_ERR_LEVEL_ERROR,
			"fm_pymt_pol_collect_writeoff_account error", ebufp);
	}
	
	return;
}
/********************************************************************
 * fm_cust_pol_get_poplist()
 *
 *	Obtains a list of pops and adds to the given outflistp.
 *	If no PIN_FLD_ANI is found on the input flist, then the
 *	entire pop list is returned.  Otherwise, only the POP
 *	matching the ANI is returned. 
 *
 ********************************************************************/
static void
fm_cust_pol_get_poplist(
	pcm_context_t	*ctxp,			/* Connection context	*/
	pin_flist_t	*in_flistp,		/* Client's input flist */
	pin_flist_t	*out_flistp,		/* Search results flist */
	pin_errbuf_t	*ebufp)			/* Error buff		*/
{
	u_int64 	database;		/* Requested database 	*/
	poid_t		*poidp = NULL;		/* Input poid		*/
	poid_t		*objp = NULL;		/* New search poid	*/
	poid_t		*type_poidp;		/* Used for full poplist*/
	pin_cookie_t    cookie = NULL;  	/* For getting results	*/
	pin_cookie_t    cookie2 = NULL;  	/* For getting anis	*/
	pin_flist_t	*flistp = NULL;		/* Search flist created	*/
        pin_flist_t	*ani_flistp = NULL;	/* ANI array flist	*/
	pin_flist_t	*a_flistp = NULL;	/* New element pointer	*/
	pin_flist_t	*e_flistp = NULL;	/* Element ptr 4 pop arr*/
	pin_flist_t	*r_flistp = NULL;	/* Search return flist	*/
	pin_flist_t	*sort_flistp = NULL;	/* Sort flist		*/
	u_int64         id;			/* Search id 		*/
	u_int		type = POP_PRIMARY;	/* Only return prime pop*/ 
	int32           rec_id;         	/* PIN_FLD_RESULTS elem	*/
	int32           rec_id2;         	/* ANI results elem	*/
	void		*vp;			/* PIN_FLD_ANI value	*/
	void		*vp2;			/* Returned fields ptr	*/

	/***********************************************************
         * Check the error buffer.
         ***********************************************************/
        if (PIN_ERR_IS_ERR(ebufp))
                return;
        PIN_ERR_CLEAR_ERR(ebufp);

        /***********************************************************
         * Allocate the flist for searching.
         ***********************************************************/
        flistp = PIN_FLIST_CREATE(ebufp);

        /***********************************************************
         * Get the database number.
         ***********************************************************/
	poidp = (poid_t *)PIN_FLIST_FLD_GET(in_flistp, PIN_FLD_POID, 0, ebufp);
	database = PIN_POID_GET_DB(poidp);

        /***********************************************************
         * Allocate the search poid and give it to the flist.  Use
	 * the 2 arg search id if args are found, otherwise use
	 * the 1 arg search which returns all the pops ordered.
         ***********************************************************/
       	vp = PIN_FLIST_FLD_GET(in_flistp, PIN_FLD_ANI, 1, ebufp);
	if (vp == NULL) {
		id = (u_int64)300;
	} else {
		id = (u_int64)301;
	}
        objp = PIN_POID_CREATE(database, "/search/pin", id, ebufp);
        PIN_FLIST_FLD_PUT(flistp, PIN_FLD_POID, (void *)objp, ebufp);

        /***********************************************************
         * Add a search arguments array.
         ***********************************************************/
        a_flistp = PIN_FLIST_ELEM_ADD(flistp, PIN_FLD_ARGS, 1, ebufp);

        /***********************************************************
         * Determine whether the entire poplist should be returned
	 * or an ani to pop match should be done.  The rule is if
	 * there's a PIN_FLD_ANI field, return the entire list,
 	 * otherwise, do a ani to pop match but only return the
	 * primary one.
         ***********************************************************/
        type_poidp = PIN_POID_CREATE(database, "/pop", (int64)-1, ebufp);
	if (vp == NULL) {
		PIN_FLIST_FLD_PUT(a_flistp, PIN_FLD_POID,
			(void *)type_poidp, ebufp);
	} else {
        	/*
         	** 2 arg search, return pop that matches ani and is a
		** primary pop.
         	*/
        	ani_flistp = PIN_FLIST_CREATE(ebufp);
		/*
		** PIN_FLD_ANI for our first arg.
		*/
		PIN_FLIST_FLD_SET(ani_flistp, PIN_FLD_ANI, vp, ebufp );
		PIN_FLIST_ELEM_SET(a_flistp, ani_flistp, PIN_FLD_ANIS,
                	0, ebufp );
		/*
		** PIN_FLD_TYPE for our second arg.
		*/
		a_flistp = PIN_FLIST_ELEM_ADD(flistp, PIN_FLD_ARGS, 2, ebufp);
        	ani_flistp = PIN_FLIST_CREATE(ebufp);
		PIN_FLIST_FLD_SET(ani_flistp, PIN_FLD_TYPE, 
			(void *)&type, ebufp);
		PIN_FLIST_ELEM_SET(a_flistp, ani_flistp, PIN_FLD_ANIS, 1,
			ebufp);
	}

        /***********************************************************
         * Put on the PIN_FLD_RESULTS array for our results.
         ***********************************************************/
        a_flistp = PIN_FLIST_ELEM_ADD(flistp, PIN_FLD_RESULTS, 0, ebufp);
	/*
	** Return the pop, city, state and zip fields if the entire poplist 
	** was requested, otherwise return the same fields including
	** the flags if pop matching was requested.
	*/
	PIN_FLIST_FLD_SET(a_flistp, PIN_FLD_PHONE, (void *)NULL, ebufp); 
	PIN_FLIST_FLD_SET(a_flistp, PIN_FLD_CITY, (void *)NULL, ebufp); 
	PIN_FLIST_FLD_SET(a_flistp, PIN_FLD_STATE, (void *)NULL, ebufp); 
	PIN_FLIST_FLD_SET(a_flistp, PIN_FLD_ZIP, (void *)NULL, ebufp); 
	if (vp != NULL) {
		/*
		** Add the ANIS array if PIN_FLD_ANI arg was passed in.
		*/
        	ani_flistp = PIN_FLIST_CREATE(ebufp);
		PIN_FLIST_FLD_SET(ani_flistp, PIN_FLD_ANI, 
			(void *)NULL, ebufp);
		PIN_FLIST_FLD_SET(ani_flistp, PIN_FLD_TYPE, 
			(void *)NULL, ebufp);
		PIN_FLIST_FLD_SET(ani_flistp, PIN_FLD_FLAGS, 
			(void *)NULL, ebufp);
		PIN_FLIST_ELEM_SET(a_flistp, ani_flistp, PIN_FLD_ANIS, 0,
			ebufp);
	}
	
        /***********************************************************
         * Call the DM to do the search.
         ***********************************************************/
        PCM_OP(ctxp, PCM_OP_SEARCH, 0, flistp, &r_flistp, ebufp);

	/***********************************************************
         * Walk the search results array adding each to the
	 * PIN_FLD_POP's array flist to pass back to the caller. 
         ***********************************************************/
        a_flistp = PIN_FLIST_ELEM_GET_NEXT(r_flistp, PIN_FLD_RESULTS,
                &rec_id, 1, &cookie, ebufp);
	while (a_flistp != (pin_flist_t *)NULL) {

        	e_flistp = PIN_FLIST_ELEM_ADD(out_flistp, PIN_FLD_POP, 
			rec_id, ebufp);

		/*
		** Take the PIN_FLD_PHONE and put it on the PIN_FLD_POP array.
		*/
		vp2 = PIN_FLIST_FLD_TAKE(a_flistp, PIN_FLD_PHONE, 0, ebufp);
		PIN_FLIST_FLD_PUT(e_flistp, PIN_FLD_PHONE, vp2, ebufp);
		/*
		** Take the PIN_FLD_CITY and put it on the PIN_FLD_POP array.
		*/
		vp2 = PIN_FLIST_FLD_TAKE(a_flistp, PIN_FLD_CITY, 0, ebufp);
		PIN_FLIST_FLD_PUT(e_flistp, PIN_FLD_CITY, vp2, ebufp);
		/*
		** Take the PIN_FLD_STATE and put it on the PIN_FLD_POP array.
		*/
		vp2 = PIN_FLIST_FLD_TAKE(a_flistp, PIN_FLD_STATE, 0, ebufp);
		PIN_FLIST_FLD_PUT(e_flistp, PIN_FLD_STATE, vp2, ebufp);
		/*
		** Take the PIN_FLD_ZIP and put it on the PIN_FLD_POP array.
		*/
		vp2 = PIN_FLIST_FLD_TAKE(a_flistp, PIN_FLD_ZIP, 0, ebufp);
		PIN_FLIST_FLD_PUT(e_flistp, PIN_FLD_ZIP, vp2, ebufp);
		/*
		** Walk the ani array (if we're pop matching) to get the 
		** PIN_FLD_FLAGS field, should only be one.
		*/
		if (vp != NULL) {
			ani_flistp = (pin_flist_t *)NULL;
			ani_flistp = PIN_FLIST_ELEM_GET_NEXT(a_flistp, 
				PIN_FLD_ANIS, &rec_id2, 0, &cookie2, ebufp);
			while (ani_flistp != (pin_flist_t *)NULL) {
				vp2 = PIN_FLIST_FLD_TAKE(ani_flistp, 
					PIN_FLD_FLAGS, 0, ebufp);
				PIN_FLIST_FLD_PUT(e_flistp, PIN_FLD_FLAGS, 
					vp2, ebufp);
				ani_flistp = PIN_FLIST_ELEM_GET_NEXT(a_flistp,
					PIN_FLD_ANIS, &rec_id2, 1, &cookie2, 
					ebufp);
			}
		}

		/***************************************************
                 * Get the next result.
                 ***************************************************/
                a_flistp = PIN_FLIST_ELEM_GET_NEXT(r_flistp,
                        PIN_FLD_RESULTS, &rec_id, 1, &cookie, ebufp);
	}

        /***********************************************************
         * Clean up.
         ***********************************************************/
	PIN_FLIST_DESTROY(flistp, NULL);
	PIN_FLIST_DESTROY(r_flistp, NULL);

        if (PIN_ERR_IS_ERR(ebufp)) {
                PIN_ERR_LOG_EBUF(PIN_ERR_LEVEL_ERROR,
			"fm_cust_pol_read_poplist error", ebufp);
        }

        return;
}	/* fm_cust_pol_read_poplist */
//****************************************************
// adjust_CashBalance
// Pre:
// Post:
//
bool bs_AdjustCashBalance::adjust_CashBalance(
  pin_Session * pinp,
  poid_t * account_pdp,
  pin_decimal_t * amountp,
  string source,
  string description,
  string service_str,
  string reason_id,
  pin_flist_t   **return_flistpp,
  pin_errbuf_t  *ebufp )
{
  pin_flist_t   *input_flistp = NULL;
  poid_t *bal_pdp = NULL;
  int       dollar_currency = 840;
  int       iversion  = DEFAULT_CASH_ADJUSTMENT_VER;
  ostringstream os;
  bool rc = false;

  char 		*service_code = (char *)NULL;
  service_code = (char *)calloc(256, sizeof(char));
  strcpy(service_code,(const char *)GENERIC_SERVICE_ADJUSTMENT_CODE);
  strcat(service_code,reason_id.c_str());

  int	id = atoi(service_code);

  PIN_ERR_CLEAR_ERR(ebufp);

  //--- setup --------------------------
  /* source / desc name */
  const string this_program("adjust_CashBalance");
  if ( description.empty() )
  {
    ostringstream os;
    os << DEFAULT_CASH_ADJUSTMENT_DESCR << "." << getuser(ENV_USER) << "." << this_program;
    description.assign(os.str());
  }
  if ( source.empty() )
  {
    source.assign(this_program);
  }
  PIN_ERR_LOG_MSG(PIN_ERR_LEVEL_DEBUG, (char*)description.c_str() );

  /* retrieving the bal_grp poid associated with a service
   * if the service_str is NULL, then account level bal_grp will be used
   */
  pin_flist_t *r_flistp = NULL;
  r_flistp = bs_AccountInfo::getService_and_BalGrpPoids(pinp, account_pdp, service_str);
  bal_pdp = PIN_FLIST_FLD_TAKE(r_flistp,PIN_FLD_BAL_GRP_OBJ, 1, ebufp);
  if(r_flistp) PIN_FLIST_DESTROY(r_flistp,ebufp);

  if(PIN_POID_IS_NULL(bal_pdp)) {
     PIN_ERR_LOG_EBUF(PIN_ERR_LEVEL_ERROR,(char*)"/balance_group poid is NULL", ebufp );
     return false;
  }

  //--- adjust------------------------------
  input_flistp = PIN_FLIST_CREATE(ebufp);
  PIN_FLIST_FLD_SET(input_flistp, PIN_FLD_PROGRAM_NAME, (void*)source.c_str(), ebufp);
  PIN_FLIST_FLD_SET(input_flistp, PIN_FLD_POID,(void *) account_pdp, ebufp);
  PIN_FLIST_FLD_SET(input_flistp, PIN_FLD_DESCR,  (void *)description.c_str(), ebufp);
  PIN_FLIST_FLD_SET(input_flistp, PIN_FLD_CURRENCY, (void *) &dollar_currency,ebufp);
  PIN_FLIST_FLD_SET(input_flistp, PIN_FLD_AMOUNT, amountp, ebufp);
  PIN_FLIST_FLD_SET(input_flistp, PIN_FLD_STR_VERSION, (void *) &iversion, ebufp);
  PIN_FLIST_FLD_SET(input_flistp, PIN_FLD_STRING_ID, (void *) &id, ebufp);

  /*** Inserting BAL_GRP poid ***/
  PIN_FLIST_FLD_PUT(input_flistp, PIN_FLD_BAL_GRP_OBJ, (void *) bal_pdp, ebufp);

  /*
  * Call opcode
  */
  rc = pinp->call_opcode( PCM_OP_AR_ACCOUNT_ADJUSTMENT, 0, input_flistp,
        return_flistpp, ebufp);

  PIN_FLIST_DESTROY(input_flistp, ebufp);
  return rc;
}