$versions{'paypal_dispute.pl'} = '06.6.00.0000';
#######################################################################
#
# AgoraCart and all associated files, except where noted, are
# Copyright 2001 to Present jointly by K-Factor Technologies, Inc.
# and by C E Mayo (aka Mister Ed) at AgoraCart.com & K-Factor.net
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# This copyright notice may not be removed or altered in any way.
#
#######################################################################

#
#
# Contains some of the subroutines/functions for PayPal Dispute logging:
#
#

########################################################################
# not opened enough in most cases, so skipping file locks
sub log_pp_dispute {

    # disabled
    return;

    local ( $pp_case_found ) = q{};
    my ( $file, $newFile) = q{};
    my ( $pp_case_id, $cart_invoiceNumber, $pp_cart_id, $pp_txn_id, $pp_case_type ) = q{};
    my ( $pp_case_reason_code, $pp_case_creation_date, $pp_case_status, $pp_buyer_additional_information ) = q{};

    # NEEDS TO BE VERIFIED. IPN TESTER DOES NOT SUPPLY A DISPUTE TEST

    get_file_lock("$case_logfile.lockfile");
    # <:encoding(utf8)
    open(CASELOG,  '<', $case_logfile) or file_open_error("$case_logfile", "Opening $case_logfile for reading", __FILE__, __LINE__);
    local $/ = undef;  # turn on "slurp mode"
    $file = <CASELOG>;
    close(CASELOG);
    release_file_lock("$case_logfile.lockfile");

    if ( $form_data{'txn_type'} eq 'new_case') {
        $form_data{'buyer_additional_information'} =~ s/[\n\r]/ /g;
        my $str = "$form_data{'case_id'}\t$form_data{'invoice'}\t$form_data{'custom'}\t$form_data{'txn_id'}\t$form_data{'case_type'}\t$form_data{'reason_code'}\t$form_data{'case_creation_date'}\t$form_data{'txn_type'}\t$form_data{'buyer_additional_information'}\n";
        # write at beginning of file, saves a little time on future updates.
        $newFile = $str . $file;
    }
    else {
        while ($file) {
            (
                $pp_case_id, # $form_data{'case_id'} - pp dispute case id
                $cart_invoiceNumber, # $form_data{'invoice'} - cart invoice
                $pp_cart_id, # $form_data{'custom'} -  cart id
                $pp_txn_id, # $form_data{'txn_id'} or $form_data{'parent_txn_id'} if cancel, reversal or cancel reversal
                $pp_case_type, # $form_data{'case_type'} -
                $pp_case_reason_code, # $form_data{'reason_code'} - set if $form_data{'case_type'} is complaint or chargeback
                $pp_case_creation_date, # $form_data{'case_creation_date'} - HH:MM:SS Mmm DD, YYYY PDT
                $pp_case_status, # $form_data{'txn_type'} - if adjustment, it is closed
                $pp_buyer_additional_information # $form_data{'case_id'} - Notes the buyer entered into the Resolution Center.
            ) = split( /\t/, $_ );

            $pp_buyer_additional_information =~ s/[\n]//g;

            if ( $pp_case_id eq $form_data{'case_id'} ) {
                # update any notes from customer in regards to the case id
                if ( $form_data{'buyer_additional_information'} ) {
                    $form_data{'buyer_additional_information'} =~ s/[\n\r]/ /g;
                    $pp_buyer_additional_information .= '---- Additional Message ----' . $form_data{'buyer_additional_information'};
                }
                #update line
                $pp_case_found = 'yes';
                $orderLoggingHash{'pp_case_id'} = $pp_case_id;
                $orderLoggingHash{'pp_case_status'} = $form_data{'txn_type'};
                $newFile .= "$pp_case_id\t$cart_invoiceNumber\t$pp_cart_id\t$pp_txn_id\t$pp_case_type\t$pp_case_reason_code\t$pp_case_creation_date\t$form_data{'txn_type'}\t$buyer_additional_information\n";
            }
            else {
                # save existing line
                $newFile .= $_;
            }
        }    # End of while $file
    }

    get_file_lock("$case_logfile.lockfile");
    # >:encoding(utf8)
    open(LOGCASE,  '>', $case_logfile) or file_open_error("$case_logfile", "Opening $case_logfile for writing updates", __FILE__, __LINE__);
    print LOGCASE $newFile;
    close(LOGCASE);
    release_file_lock("$case_logfile.lockfile");

}

########################################################################
1;
