#!/usr/bin/perl

use CGI;
use HTTP::Cookies;
use SOAP::Lite;
# use LWP::Debug; LWP::Debug::level('+'); SOAP::Lite->import(+trace => 'all');
use Data::Dumper;
use Data::UUID;
use XML::Parser;
use POSIX qw(strftime);

my $cookie_jar = HTTP::Cookies->new();

my $soap = SOAP::Lite->new(uri => "http://127.0.0.1:34012");
$soap->proxy("http://127.0.0.1:34012/?wsdl",cookie_jar => $cookie_jar);
$soap->default_ns('urn:api3');
#$soap->outputxml("true");

# Авторизация
my $login = "admin";
my $password = "";

my @params = (
    SOAP::Data->name("login" => $login),
    SOAP::Data->name("pass" => $password)
);
my $response = $soap->Login(@params);
print "\nLogin response:\n", Dumper($response->result);

# добавить агента
@params = (
    SOAP::Data->name("val" => \SOAP::Data->value(
	SOAP::Data->name("agent" => \SOAP::Data->value(
	        SOAP::Data->name("descr" => "agent example"),
    	        SOAP::Data->name("type" => "4"),
    	        SOAP::Data->name("id" => "0"))
        ))
    ),
    SOAP::Data->name("isInsert" => "1")
);
$response = $soap->insupdAgent(@params);
$agentid = $response->valueof('//insupdAgentResponse/ret');
print "\nInsert agent:\n", Dumper($response->result);

# добавить пользователя
$ug = Data::UUID->new;
$uuid = $ug->to_string($ug->create());
$accname = "acc".$uuid;
@params = (
	SOAP::Data->name("val" => \SOAP::Data->value(
	SOAP::Data->name("account" => \SOAP::Data->value(
		SOAP::Data->name("uid" => "0"),
		SOAP::Data->name("type" => "2"),
		SOAP::Data->name("login" => $accname),
		SOAP::Data->name("pass" => "acc"),
	        SOAP::Data->name("name" => "acc example"))
        ))
    ),
    SOAP::Data->name("isInsert" => "1")
);
$response = $soap->insupdAccount(@params);
$accid = $response->valueof('//insupdAccountResponse/ret');
print "\nInsert account:\n", Dumper($response->result);

# добавить договор
@params = (
    SOAP::Data->name("val" => \SOAP::Data->value(
	SOAP::Data->name("uid" => $accid),
	SOAP::Data->name("number" => "agrm".$uuid),
	SOAP::Data->name("agrmid" => "0"),
        SOAP::Data->name("operid" => "0"),
	SOAP::Data->name("curid" => "0")
    )),
    SOAP::Data->name("isInsert" => "1")
);
$response = $soap->insupdAgreement(@params);
$agrmid = $response->valueof('//insupdAgreementResponse/ret');
print "\nInsert agreement:\n", Dumper($response->result);

# добавить учётную запись
@params = (
	SOAP::Data->name("val" => \SOAP::Data->value(
	SOAP::Data->name("vgroup" => \SOAP::Data->value(
		SOAP::Data->name("agrmid" => $agrmid),
		SOAP::Data->name("id" => $agentid),
		SOAP::Data->name("uid" => $accid),
		SOAP::Data->name("vgid" => "0"))
        ))
    ),
    SOAP::Data->name("isInsert" => "1")
);
$response = $soap->insupdVgroup(@params);
$vgid = $response->valueof('//insupdVgroupResponse/ret');
print "\nInsert vgroup:\n", Dumper($response->result);

# получить договор
@params = (
    SOAP::Data->name("flt" => \SOAP::Data->value(
        SOAP::Data->name("agrmid" => $agrmid))
    )
);
$response = $soap->getAgreements(@params);
print "\ngetAgreements response:\n", Dumper($response->result);

# получить учётные записи по договору
$response = $soap->getVgroups(@params);
print "\ngetVgroups response:\n", Dumper($response->result);

# обновить данные учётной записи (описание)
@params = (
	SOAP::Data->name("val" => \SOAP::Data->value(
	SOAP::Data->name("vgroup" => \SOAP::Data->value(
		SOAP::Data->name("agrmid" => $agrmid),
		SOAP::Data->name("id" => $agentid),
		SOAP::Data->name("uid" => $accid),
		SOAP::Data->name("vgid" => $vgid),
		SOAP::Data->name("descr" => "updated vgroup data"))
        ))
    ),
    SOAP::Data->name("isInsert" => "0")
);
$response = $soap->insupdVgroup(@params);
print "\nUpdate vgroup:\n", Dumper($response->result);

# получить учётную запись по идентификатору
@params = (
    SOAP::Data->name("flt" => \SOAP::Data->value(
        SOAP::Data->name("vgid" => $vgid))
    )
);
$response = $soap->getVgroups(@params);
print "\ngetVgroups response:\n", Dumper($response->result);

# задать курс
$now = strftime "%F", localtime;
@params = (
    SOAP::Data->name("val" => \SOAP::Data->value(
        SOAP::Data->name("rate" => "20.0"),
        SOAP::Data->name("date" => $now))
    )
);
$response = $soap->updCurrencyRate(@params);
$curid = $response->valueof('//updCurrencyRateResponse/ret');
print "\nupdCurrencyRate response:\n", Dumper($response->result);

# платёж
@params = (
    SOAP::Data->name("val" => \SOAP::Data->value(
        SOAP::Data->name("agrmid" => $agrmid),
        SOAP::Data->name("amount" => "200.0"),
        SOAP::Data->name("receipt" => "rec".$uuid),
        SOAP::Data->name("comment" => "test payment"),
        SOAP::Data->name("curid" => $curid))
    )
);
$response = $soap->Payment(@params);
$paymentid = $response->valueof('//PaymentResponse/ret');
print "\nPayment response:\n", Dumper($response->result);

# получить информацию о платеже
@params = (
    SOAP::Data->name("flt" => \SOAP::Data->value(
        SOAP::Data->name("recordid" => $paymentid))
    )
);
$response = $soap->getPayments(@params);
print "\ngetPayments response:\n", Dumper($response->result);

# удалить учётную запись
$response = $soap->delVgroup(SOAP::Data->name("id" => $vgid));
print "\nDelete vgroup:\n", Dumper($response->result);

# закрыть договор
@params = (
	SOAP::Data->name("agrmid" => $agrmid),
	SOAP::Data->name("closedate" => $now)
);
$response = $soap->closeAgreement(@params);
print "\nClose agreement:\n", Dumper($response->result);

# удалить договор
$response = $soap->delAgreement(SOAP::Data->name("agrmid" => $agrmid));
print "\nDelete agreement:\n", Dumper($response->result);

# удалить пользователя
$response = $soap->delAccount(SOAP::Data->name("id" => $accid));
print "\nDelete account:\n", Dumper($response->result);

# удалить агента
$response = $soap->delAgent(SOAP::Data->name("id" => $agentid));
print "\nDelete agent:\n", Dumper($response->result);

$response = $soap->Logout();
print "\nLogout response:\n", Dumper($response->result);

