#!perl

use Test::More;

# Added to support the entity ID test.
our %GLOBAL;
#

our $test_request_data = [
	# AR/AP Transaction Screen Tests
	{
		'_test_id'  => 'AR Transaction Screen',
		'_codebase' => 'old',
		'module'    => 'ar.pl',
		'action'    => 'add',
	    '_api_test' => sub {
			my $request = shift;
			ok($request->{batch_number}, 'Batch Number is Defined');
	     },
	},
	{
		'_test_id'  => 'AP Transaction Screen',
		'_codebase' => 'old',
		'module'    => 'ap.pl',
		'action'    => 'add'
	},
	
	# Tests adding a new customer/vendor.
	
	#https://127.0.0.1/ledgersmb/vendor.pl?login=aurynn_lsmb&module=vendor.pl&action=add&db=vendor&
	{
	    "_test_id"      => "AP Add Vendor",
	    "_codebase"     => "new",
	    "action"        => "save",
	    "module"        => "vendor.pl",
	    "control_code"  => "TestAddVendorObj",
	    "name"          => "TestObj",
	    "country_id_t"  => 232,
	    "account_class" => 1,
	    "created_as_class" => 1,
	    "db"            => "vendor",
	    "_script_handle" => "LedgerSMB::Scripts::vendor",
	},
	
	{
	    "_test_id"          => "AP Save Vendor",
	    "_codebase"         => "new",
	    "module"            => "vendor.pl",
	    "action"            => "save",
	    "pay_to_name"       => "TestEntity",
	    "entity_id"         => '_$GLOBAL$:entity_id',
	    "control_code"      => "TestAddVendorObj",
	    "ar_ap_account_id"  => 10,
	    "cash_account_id"   => 1,
	    "country_id_t"  => 232,
	    "account_class" => 1,
	    "created_as_class" => 1,
	    "db"            => "vendor",
	    "name"          => "TestObj",
	    "_script_handle" => "LedgerSMB::Scripts::vendor",
	},
	{
	    "_test_id"          => "AP Add Transaction",
	    "_codebase"         => "old",
	    "module"            => "ap.pl",
	    "action"            => "post",
	    "vendor"            => "TestObj",
	    "currency"          => "USD",
	    "amount_1"          => "5",
	    "AP_amount_1"       => "5010--Purchases",
	    "AP"                => "2100--Accounts Payable",
	    "transdate"         => "2009-12-08",
	    "AP_paid_1"         => "1060--Checking Account"
	},
	
	# Create Batch Screens
	{
		'_test_id'   => 'AR Transaction Voucher Screen',
		'_codebase'  => 'new',
		'action'     => 'create_batch',
		'batch_type' => 'recievable',
		'module'     => 'vouchers.pl',
	},
	{
		'_test_id'   => 'AP Transaction Voucher Screen',
		'_codebase'  => 'new',
		'action'     => 'create_batch',
		'batch_type' => 'payable',
		'module'     => 'vouchers.pl',
		'_api_test'  => sub {
			my $request = shift;
			ok($request->{batch_number}, 'Batch Number is Defined');
		}
	},
	{
		'_test_id'   => 'Payment Transaction Voucher Screen',
		'_codebase'  => 'new',
		'action'     => 'create_batch',
		'batch_type' => 'payment',
		'module'     => 'vouchers.pl',
		'_api_test'  => sub {
			my $request = shift;
			ok($request->{batch_number}, 'Batch Number is Defined');
         },
	},
	{
		'_test_id'   => 'Payment Reversal Transaction Voucher Screen',
		'_codebase'  => 'new',
		'action'     => 'create_batch',
		'batch_type' => 'payment_reversal',
		'module'     => 'vouchers.pl',
		'_api_test'  => sub { 
			my $request = shift;
			ok($request->{batch_number}, 'Batch Number is Defined');
		},
	},
	{
		'_test_id'   => 'Income Statement Search Screen',
		'_codebase'  => 'old',
		'action'     => 'report',
		'report'     => 'income_statement',
		'module'     => 'rp.pl',
	},
	{
		'_test_id'   => 'Balance sheet search Screen',
		'_codebase'  => 'old',
		'action'     => 'report',
		'report'     => 'balance_sheet',
		'module'     => 'rp.pl',
	},
	{
		'_test_id'   => 'GL Transaction Voucher Screen',
		'_codebase'  => 'new',
		'action'     => 'create_batch',
		'batch_type' => 'gl',
		'module'     => 'vouchers.pl',
		'_api_test'  => sub {
			my $request = shift;
			ok($request->{batch_number}, 'Batch Number is Defined');
		},
	},
	{
		'_test_id'      => 'Payment source start error',
		'_codebase'     => 'new',
		'_error_test'   => 1,
		'account_class' => 1,
		'module'	=> 'payment.pl',
		'action'        => 'display_payments',
		'approved'      => 0,
		'currency'      => 'USD',				
		'datepaid'      => '2008-07-22',
         '_api_test'     => sub {
			my $self = shift;
			cmp_ok($self->{_error}, 'eq', 'No source start defined!', "Payment source start error: error correct");
		}
	},
	{
		'_test_id'      => 'Payment source numbering',
		'_codebase'     => 'new',
		'_error_test'   => 1,
		'account_class' => 1,
		'start_source'  => 1,
		'module'	=> 'payment.pl',
		'action'        => 'update_payments',
		'approved'      => 0,
		'currency'      => 'USD',				
		'datepaid'      => '2008-07-22',
		'id_1'		=> 1,
		'id_3'		=> 3,
		'_exec_override' => {
			'payment_get_all_contact_invoices' => sub {
				my $self = shift;
				$retval = [ { contact_id => 1,
					total_due => Math::BigFloat->new(1000),
					econtrol_code => 'TEST1',
					eca_description => 'test',
					contact_name => 'test',
					account_number => 'test',
					invoices => '{{"100","101","2009-01-01",1000,1000}}',
					has_vouchers => 0, 
				} ,
				{
					contact_id => 2,
					total_due => Math::BigFloat->new(1000),
					econtrol_code => 'TEST1',
					eca_description => 'test',
					contact_name => 'test',
					account_number => 'test',
					invoices => '{{101,101,2009-01-01,1000,1000}}',
					has_vouchers => 0, 
				} ,
				{
					contact_id => 3,
					total_due => Math::BigFloat->new(1000),
					econtrol_code => 'TEST1',
					eca_description => 'test',
					contact_name => 'test',
					account_number => 'test',
					invoices => '{{102,101,2009-01-01,1000,1000}}',
					has_vouchers => 0, 
				} ];
				$self->{contact_invoices} = $retval;
				$self->debug({'file' => '/tmp/test2'});
				return @$retval;
			}
		},
        '_user'         =>  { numberformat => '1,000.00', dateformat => 'yyyy-mm-dd' },
		'_api_test'     => sub {
			my $self = shift;
			$self->debug({file => '/tmp/test'});
			ok(defined $self->{source_1}, "$self->{_test_id} source 1 defined");
	        cmp_ok($self->{source_2}, 'eq', '',"$self->{_test_id} source 2 not found");
	        ok(defined $self->{source_3}, "$self->{_test_id} source 3 defined");
	        cmp_ok($self->{grand_total}, 'eq', '2,000.00',"Grand total formatting works");
	     },
	},
	{
		'_test_id'     => 'Empty Recon Submission',
		'_codebase'    => 'new',
		'action'       => 'submit_recon_set',
		'module'       => 'recon.pl',
		'report_id'    => '-100',
		'their_total'  => '0',
		'_exec_override' => {
			'reconciliation__submit_set' => sub {
		 		my ($self) = @_;
		 		$self->_pre_save;
		 		is($self->{line_ids}, '{}', 'Empty recon set line ids ok');
				is($self->{report_id}, -100, 'Recon set report id correct');
		 	},		
		 },
	},
	{
		'_test_id'     => 'Payment search (OLD)',
		'_codebase'    => 'old',
		'account'      => '1060--Checking Account',
		'module'       => 'rp.pl',
		'action'       => 'list_payments',
	},
	{
		'_test_id'	=> 'Payment Search (NEW)',
		'_codebase'	=> 'new',
		module		=> 'payment.pl',
		account_class	=> 1,				
		meta_number     => 'TEST_VENDOR',	
		cash_accno	=> '-111',
		date_reversed	=> '2009-12-01',
		action 		=> 'get_search_results',
	},
	{
		'_test_id'  => 'Recon Search Screen',
		'_codebase' => 'new',
		'module'    => 'recon.pl',
		'report'    => '1',
		'action'    => 'search',
	},	
	{
		'_test_id'  => 'Menu round trip',
		'_codebase' => 'new',
		'module'    => 'menu.pl',
		'menu'      => '1',
		'id'        => '4',
		'open'      => ':1:4:11',
	},	
	{
		'_test_id'      => 'Payment print total',
		'_codebase'     => 'new',
		'action'        => 'print',
		'approved'      => 0,
		'currency'      => 'USD',				
		'datepaid'      => '2008-07-22',	
		'contact_1'	=> '25',				
		'id_25'         => '25',				
		'paid_25'       => 'all',		
        	'payment_209'   => '1000.00',	
		'invoice_25_1'  => 209,
		'net_209'	=> '1000.00',				
		'payment_210'	=> '-100.00',
		'invoice_25_2'	=> '210',
		'net_210'	=> '-100.00',
		'payment_344'	=> '100000.00',
		'invoice_25_3'	=> '344',
		'net_344'	=> '100000.00',
		'payment_352'	=> '1000.00',
		'invoice_25_4'	=> '352',
		'net_352'	=> '1000.00',
		'payment_353'	=> '1000.00',
		'invoice_25_5'	=> '353',
		'net_353'	=> '1000.00',
		'payment_354'	=> '1000.00',
		'invoice_25_6'	=> '354',
		'net_354'	=> '1000.00',
		'payment_355'	=> '1000.00',
		'invoice_25_7'	=> '355',
		'net_355'	=> '1000.00',
		'payment_356'	=> '1000.00',
		'invoice_25_8'	=> '356',
		'net_356'	=> '1000.00',
		'invoice_count_25' => 8,				
		'contact_count'	=> 1,				
		'media'		=> 'screen',	
		'format'	=> 'PDF',			
		'module'        => 'payment.pl',
		'account_class' => 1,
		'currency'      => 'USD',
		'multiple'      => 1,
		'_pre_test_sub' => sub 
		{
			$LedgerSMB::Sysconfig::check_max_invoices = 1;
		},
		'_api_test'     => sub { 
			use strict;
		    my $request = shift;
		    cmp_ok($request->{invoice_count_25}, '>', $LedgerSMB::Sysconfig::check_max_invoices, 'Test is valid under current config');
			cmp_ok(scalar @{$request->{checks}}, '==', $request->{contact_count}, 'Correct number of checks printed');
			my $check = shift @{$request->{checks}};
			cmp_ok( $request->format_amount({
				                   amount => $check->{amount}, 
				                   format => '1000.00',
				                   precision => 2
			                 }), 
					'eq', '105900.00', 'Printed amount is correct');
	        },
	},	
	# the following 4 tests check that it is possible to remove an empty batch
	{
		# access the batch creation screen
		"_skip_lwp"             => 1,
		"_test_id"		=> "AP Voucher Screen",
		"_codebase"		=> "new",
		"action"		=> "create_batch",
		"module"		=> "vouchers.pl",
		"batch_type"	=> "ap",
		"company"		=> "test",
	},
	{
		# create an empty batch
		"_test_id"		=> "Create Empty Batch",
		"_skip_lwp"             => 1,
		"_codebase"		=> "new",
		"action"		=> "create_vouchers",
		"module"		=> "vouchers.pl",
		"batch_type"	=> "ap",
		"batch_date"	=> '2010-12-30',
		"batch_number"	=> '_$GLOBAL$:batch_no',
		"description"	=> "foo",
		"form_id"	=> '_$GLOBAL$:form_id',
	},
	{
		# get the list of batches and save the first row of the list in GLOBAL
		"_test_id"		=> "List Empty Batches",
		"_skip_lwp"             => 1,
		"_codebase"		=> "new",
		"action"		=> "list_batches",
		"empty"			=> 1,
		"class_id"		=> 1,
		"created_by_eid"	=> 0,
		"module"		=> "vouchers.pl",
	},
	{
		# remove first row of the list of empty batches (obtained from GLOBAL)
		"_test_id"		=> "Remove Empty Batches",
		"_skip_lwp"             => 1,
		"_codebase"		=> "new",
		"module"		=> "vouchers.pl",
		"form_id"		=> '_$GLOBAL$:form_id',
		"rowcount"		=> 1,
		"empty"			=> 1,
		"class_id"		=> 1,
		"row_1"			=> '_$GLOBAL$:row_1',
		"action"		=> "list_batches_batch_delete",
		'_$GLOBAL$:batch_id' => 1,
	},
];

our $api_test_cases = {
	'AP Transaction Voucher Screen' => sub {
	},
};

# Overridden methods in classes for tests.

package LedgerSMB::DBObject::Payment;
sub call_dbmethod{
    my $self = shift;
    my %args = @_;
    my $funcname = $args{funcname};
    if (ref $self->{_exec_override} eq 'HASH'){
       if ($self->{_exec_override}->{"$funcname"}){
           return $self->{_exec_override}->{"$args{funcname}"}($self);
        }
    }
    LedgerSMB::PGObject::call_dbmethod($self, @_);
}

package LedgerSMB::DBObject::Reconciliation;

sub call_dbmethod{
    LedgerSMB::DBObject::Payment::call_dbmethod(@_);
}
