add_filter( 'gform_validation', function ( $validation_result ) { // CSF. $forms_array[32] = array( 'child_amount_field_id' => 8, 'child_name_field_id' => 19, 'ledger_form_id' => 30, 'ledger_name_id' => 1, 'ledger_amount_id' => 4, ); // DP. $forms_array[62] = array( 'child_amount_field_id' => 8, 'child_name_field_id' => 19, 'ledger_form_id' => 60, 'ledger_name_id' => 1, 'ledger_amount_id' => 16, ); // EOF. $forms_array[26] = array( 'child_amount_field_id' => 8, 'child_name_field_id' => 19, 'ledger_form_id' => 28, 'ledger_name_id' => 1, 'ledger_amount_id' => 4, ); // get form. $form = $validation_result['form']; // if form isnt in array do nothing. if ( ! in_array( $form['id'], array_keys( $forms_array ), true ) ) { return $validation_result; } $nested_form = new GP_Nested_Forms( $form ); $child_amount_field_id = $forms_array[ $form['id'] ]['child_amount_field_id']; // check if field called amount. foreach ( $form['fields'] as $field ) { // if there is then validate. if ( $field->id === $child_amount_field_id ) { // get parent form then all child form entries. $parent_id = $nested_form->get_parent_form_id(); $parent_form = GFAPI::get_form( $parent_id ); $parent_form_nested = new GP_Nested_Forms( $parent_form ); $session = new GPNF_Session( $parent_id ); $entry_id_array = $session->get_valid_entry_ids( $session->get_cookie()['nested_entries'] ); $entries_array = array(); foreach ( $entry_id_array as $entryid ) { if ( is_array( $entryid ) ) { foreach ( $entryid as $id ) { $entries_array[] = $id; } } else { $entries_array[] = $entryid; } } $entries = $parent_form_nested->get_entries( $entries_array ); // add the total. $batch_value = 0; $value = str_replace( array( '$', ',' ), '', $field->gppa_hydrated_value ); foreach ( $entries as $entry ) { $child_form_id = $entry['form_id']; $child_form = GFAPI::get_form( $child_form_id ); foreach ( $child_form['fields'] as $child_field ) { if ( $child_field->id === $child_amount_field_id ) { $batch_value += intval( $entry[ $child_amount_field_id ] ); } } } $value += $batch_value; // check against balance // get ledger first. $ledger_form_id = $forms_array[ $form['id'] ]['ledger_form_id']; $ledger_name_field = $forms_array[ $form['id'] ]['ledger_name_id']; $ledger_amount_field = $forms_array[ $form['id'] ]['ledger_amount_id']; $name_field = null; $name_field_id = $forms_array[ $form['id'] ]['child_name_field_id']; for ( $j = 0; $j < count( $form['fields'] ); $j++ ) { if ( $form['fields'][ $j ]->id === $name_field_id ) { $name_field = $form['fields'][ $j ]->gppa_hydrated_value; break; } } $search_criteria = array(); $search_criteria['status'] = 'active'; $search_criteria['field_filters'] = array(); $paging_offset = 0; $paging = array( 'offset' => $paging_offset, 'page_size' => 25, ); $total_count = 0; $search_criteria['field_filters'][] = array( 'key' => $ledger_name_field, 'value' => $name_field, ); $ledger_entries = GFAPI::get_entries( $ledger_form_id, $search_criteria, null, $paging, $total_count ); // if more entries then returned then make more calls until get them all. while ( $total_count > count( $ledger_entries ) ) { $paging_offset += 25; $paging = array( 'offset' => $paging_offset, 'page_size' => 25, ); $new_entries = GFAPI::get_entries( $ledger_form_id, $search_criteria, null, $paging, $total_count ); foreach ( $new_entries as $new_entry ) { $ledger_entries[] = $new_entry; } } $ledger_amount = 0; foreach ( $ledger_entries as $entry ) { $ledger_amount += $entry[ $ledger_amount_field ]; } $paging_offset = 0; $paging = array( 'offset' => $paging_offset, 'page_size' => 25, ); $search_criteria['field_filters'][0] = array( 'key' => $name_field_id, 'value' => $name_field, ); $search_criteria['field_filters'][] = array( 'key' => 'workflow_final_status', 'value' => 'pending', ); $search_criteria['field_filters'][] = array( 'key' => 'workflow_step', 'operator' => 'isnot', 'value' => '0', ); // subtract pending amount. $pending_disbursals = 0; $child_entries = GFAPI::get_entries( $form['id'], $search_criteria, null, $paging, $total_count ); // if more entries than returned then make more calls until get them all. while ( $total_count > count( $child_entries ) ) { $paging_offset += 25; $paging = array( 'offset' => $paging_offset, 'page_size' => 25, ); $new_entries = GFAPI::get_entries( $form['id'], $search_criteria, null, $paging, $total_count ); foreach ( $new_entries as $new_entry ) { $child_entries[] = $new_entry; } } foreach ( $child_entries as $child_entry ) { // $new_parent = GFAPI::get_entry( $child_entry['gpnf_entry_parent'] ); $pending_disbursals += $child_entry[ $field['id'] ]; } $balance = ( $ledger_amount - $pending_disbursals ); if ( $value > $balance ) { $field->failed_validation = true; $money_balance = number_format( $balance, 2 ); $amount_over = $value - $balance; $amount_over = number_format( $amount_over, 2 ); $field->validation_message = "With this request, your total requested amount exceeds your available balance of $$money_balance by $$amount_over."; $validation_result['is_valid'] = false; } $validation_result['form'] = $form; return $validation_result; } } $validation_result['form'] = $form; return $validation_result; } );