#!/bin/bash

url=http://127.0.0.1:34012
header="Content-Type:text/xml;charset=UTF-8"

# Авторизация
echo -e "Login:"
curl -c cookie-jar.txt --header $header -d "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:api3\"><soap:Header/><soap:Body><urn:Login><login>admin</login><pass></pass></urn:Login></soap:Body></soap:Envelope>" $url

# добавить агента
response=$(curl -s -b cookie-jar.txt --header $header -d "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:api3\"><soap:Body><insupdAgent xmlns=\"urn:api3\"><val><agent><descr xsi:type=\"xsd:string\">agent example</descr><type xsi:type=\"xsd:int\">4</type><id xsi:type=\"xsd:int\">0</id></agent></val><isInsert xsi:type=\"xsd:int\">1</isInsert></insupdAgent></soap:Body></soap:Envelope>" $url | tr '\n' ' ')
echo -e "Insert agent:"
agentid=$(sed -e 's/.*<ret>\(.*\)<\/ret>.*/\1/' <<< "$response")
echo -e $agentid

# добавить пользователя
uuid=$(uuidgen)
response=$(curl -s -b cookie-jar.txt --header $header -d "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:api3\"><soap:Body><insupdAccount xmlns=\"urn:api3\"><val><account><uid xsi:type=\"xsd:int\">0</uid><type xsi:type=\"xsd:int\">2</type><login xsi:type=\"xsd:string\">acc${uuid}</login><pass xsi:type=\"xsd:string\">acc</pass><name xsi:type=\"xsd:string\">acc example</name></account></val><isInsert xsi:type=\"xsd:int\">1</isInsert></insupdAccount></soap:Body></soap:Envelope>" $url | tr '\n' ' ')
echo -e "Insert account:"
accid=$(sed -e 's/.*<ret>\(.*\)<\/ret>.*/\1/' <<< "$response")
echo -e $accid

# добавить договор
response=$(curl -s -b cookie-jar.txt --header $header -d "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:api3\"><soap:Body><insupdAgreement xmlns=\"urn:api3\"><val><uid xsi:type=\"xsd:int\">${accid}</uid><number xsi:type=\"xsd:string\">agrm${uuid}</number><agrmid xsi:type=\"xsd:int\">0</agrmid><operid xsi:type=\"xsd:int\">0</operid><curid xsi:type=\"xsd:int\">0</curid></val><isInsert xsi:type=\"xsd:int\">1</isInsert></insupdAgreement></soap:Body></soap:Envelope>" $url | tr '\n' ' ')
echo -e "Insert agreement:"
agrmid=$(sed -e 's/.*<ret>\(.*\)<\/ret>.*/\1/' <<< "$response")
echo -e $agrmid

# добавить учётную запись
response=$(curl -s -b cookie-jar.txt --header $header -d "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:api3\"><soap:Body><insupdVgroup xmlns=\"urn:api3\"><val><vgroup><agrmid xsi:type=\"xsd:int\">${agrmid}</agrmid><id xsi:type=\"xsd:int\">${agentid}</id><uid xsi:type=\"xsd:int\">${accid}</uid><vgid xsi:type=\"xsd:int\">0</vgid></vgroup></val><isInsert xsi:type=\"xsd:int\">1</isInsert></insupdVgroup></soap:Body></soap:Envelope>" $url | tr '\n' ' ')
echo -e "Insert vgroup:"
vgid=$(sed -e 's/.*<ret>\(.*\)<\/ret>.*/\1/' <<< "$response")
echo -e $vgid

# получить договор
echo -e "getAgreements response:"
curl -b cookie-jar.txt --header $header -d "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:api3\"><soap:Body><getAgreements xmlns=\"urn:api3\"><flt><agrmid xsi:type=\"xsd:int\">${agrmid}</agrmid></flt></getAgreements></soap:Body></soap:Envelope>" $url

# получить учётные записи по договору
echo -e "getVgroups response:"
curl -b cookie-jar.txt --header $header -d "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:api3\"><soap:Body><getVgroups xmlns=\"urn:api3\"><flt><agrmid xsi:type=\"xsd:int\">${agrmid}</agrmid></flt></getVgroups></soap:Body></soap:Envelope>" $url

# обновить данные учётной записи (описание)
response=$(curl -s -b cookie-jar.txt --header $header -d "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:api3\"><soap:Body><insupdVgroup xmlns=\"urn:api3\"><val><vgroup><agrmid xsi:type=\"xsd:int\">${agrmid}</agrmid><id xsi:type=\"xsd:int\">${agentid}</id><uid xsi:type=\"xsd:int\">${accid}</uid><vgid xsi:type=\"xsd:int\">${vgid}</vgid><descr xsi:type=\"xsd:string\">updated vgroup data</descr></vgroup></val><isInsert xsi:type=\"xsd:int\">0</isInsert></insupdVgroup></soap:Body></soap:Envelope>" $url | tr '\n' ' ')
echo -e "Update vgroup:"
vgid=$(sed -e 's/.*<ret>\(.*\)<\/ret>.*/\1/' <<< "$response")
echo -e $vgid

# получить учётную запись по идентификатору
echo -e "getVgroups response:"
curl -b cookie-jar.txt --header $header -d "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:api3\"><soap:Body><getVgroups xmlns=\"urn:api3\"><flt><vgid xsi:type=\"xsd:int\">${vgid}</vgid></flt></getVgroups></soap:Body></soap:Envelope>" $url 

# задать курс
now=$(date +"%Y-%m-%d")
response=$(curl -b cookie-jar.txt --header $header -d "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:api3\"><soap:Body><updCurrencyRate xmlns=\"urn:api3\"><val><rate xsi:type=\"xsd:float\">20.0</rate><date xsi:type=\"xsd:date\">${now}</date></val></updCurrencyRate></soap:Body></soap:Envelope>" $url | tr '\n' ' ')
echo -e "updCurrencyRate response:"
curid=$(sed -e 's/.*<ret>\(.*\)<\/ret>.*/\1/' <<< "$response")
echo -e $curid

# платёж
response=$(curl -b cookie-jar.txt --header $header -d "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:api3\"><soap:Body><Payment xmlns=\"urn:api3\"><val><agrmid xsi:type=\"xsd:int\">${agrmid}</agrmid><amount xsi:type=\"xsd:float\">200.0</amount><receipt xsi:type=\"xsd:string\">rec${uuid}</receipt><comment xsi:type=\"xsd:string\">test payment</comment><curid xsi:type=\"xsd:int\">${curid}</curid></val></Payment></soap:Body></soap:Envelope>" $url | tr '\n' ' ')
echo -e "Payment response:"
paymentid=$(sed -e 's/.*<ret>\(.*\)<\/ret>.*/\1/' <<< "$response")
echo -e $paymentid

# получить информацию о платеже
echo -e "getPayments response:"
curl -b cookie-jar.txt --header $header -d "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:api3\"><soap:Body><getPayments xmlns=\"urn:api3\"><flt><recordid xsi:type=\"xsd:int\">${paymentid}</recordid></flt></getPayments></soap:Body></soap:Envelope>" $url

# удалить учётную запись
echo -e "Delete vgroup:"
curl -b cookie-jar.txt --header $header -d "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:api3\"><soap:Body><delVgroup xmlns=\"urn:api3\"><id xsi:type=\"xsd:int\">${vgid}</id></delVgroup></soap:Body></soap:Envelope>" $url

# закрыть договор
echo -e "Close agreement:"
curl -b cookie-jar.txt --header $header -d "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:api3\"><soap:Body><closeAgreement xmlns=\"urn:api3\"><agrmid xsi:type=\"xsd:int\">${agrmid}</agrmid><closedate xsi:type=\"xsd:date\">${now}</closedate></closeAgreement></soap:Body></soap:Envelope>" $url

# удалить договор
echo -e "Delete agreement:"
curl -b cookie-jar.txt --header $header -d "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:api3\"><soap:Body><delAgreement xmlns=\"urn:api3\"><agrmid xsi:type=\"xsd:int\">${agrmid}</agrmid></delAgreement></soap:Body></soap:Envelope>" $url

# удалить пользователя
echo -e "Delete account:"
curl -b cookie-jar.txt --header $header -d "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:api3\"><soap:Body><delAccount xmlns=\"urn:api3\"><id xsi:type=\"xsd:int\">${accid}</id></delAccount></soap:Body></soap:Envelope>" $url

# удалить агента
echo -e "Delete agent:"
curl -b cookie-jar.txt --header $header -d "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:api3\"><soap:Body><delAgent xmlns=\"urn:api3\"><id xsi:type=\"xsd:int\">${agentid}</id></delAgent></soap:Body></soap:Envelope>" $url

echo -e "Logout:"
curl -b cookie-jar.txt --header $header -d "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:urn=\"urn:api3\"><soap:Body><Logout xmlns=\"urn:api3\" xsi:nil=\"true\" /></soap:Body></soap:Envelope>" $url

rm cookie-jar.txt
