// регистрируем обработчик
AddEventHandler("sale", "OnSaleDeliveryOrder", Array("MyClassSaleDeliveryOrder", "MyOnSaleDeliveryOrder"));
// обработчик события
class MyClassSaleDeliveryOrder
{
function MyOnSaleDeliveryOrder($ID, $val)
{
global $DB;
$arOrder = CSaleOrder::GetByID($ID);
if (!$arOrder)
{
$GLOBALS["APPLICATION"]->ThrowException(str_replace("#ID#", $ID, GetMessage("SKGO_NO_ORDER")), "NO_ORDER");
return False;
}
if ($arOrder["PAYED"] == $val)
{
$GLOBALS["APPLICATION"]->ThrowException(str_replace("#ID#", $ID, GetMessage("SKGO_DUB_PAY")), "ALREADY_FLAG");
return False;
}
$errorCode = "";
$userID = $arOrder["USER_ID"];
$userID = IntVal($userID);
if ($userID <= 0)
{
$GLOBALS["APPLICATION"]->ThrowException(GetMessage("SKGU_EMPTY_USER_ID"), "EMPTY_USER_ID");
return False;
}
$paySum = $arOrder["PRICE"];
$paySum = str_replace(",", ".", $paySum);
$paySum = DoubleVal($paySum);
if ($paySum <= 0)
{
$GLOBALS["APPLICATION"]->ThrowException(GetMessage("SKGU_EMPTY_SUM"), "EMPTY_SUM");
return False;
}
$payCurrency = $arOrder["CURRENCY"];
$payCurrency = Trim($payCurrency);
if (strlen($payCurrency) <= 0)
{
$GLOBALS["APPLICATION"]->ThrowException(GetMessage("SKGU_EMPTY_CURRENCY"), "EMPTY_CURRENCY");
return False;
}
$orderID = IntVal($ID);
$currentBudget = 0.0;
// Check current user account budget
$dbUserAccount = CSaleUserAccount::GetList(
array(),
array("USER_ID" => $userID, "CURRENCY" => $payCurrency)
);
if ($arUserAccount = $dbUserAccount->Fetch())
$currentBudget = roundEx(DoubleVal($arUserAccount["CURRENT_BUDGET"]), SALE_VALUE_PRECISION);
if ($arUserAccount)
{
$arFields = array(
"CURRENT_BUDGET" => ($currentBudget - $paySum)
);
CSaleUserAccount::Update($arUserAccount["ID"], $arFields);
}
else
{
$arFields = array(
"USER_ID" => $userID,
"CURRENT_BUDGET" => ($currentBudget - $paySum),
"CURRENCY" => $payCurrency
);
CSaleUserAccount::Add($arFields);
}
$arFields = array(
"USER_ID" => $userID,
"TRANSACT_DATE" => date($DB->DateFormatToPHP(CSite::GetDateFormat("FULL", SITE_ID))),
"AMOUNT" => $paySum,
"CURRENCY" => $payCurrency,
"DEBIT" => "N",
"ORDER_ID" => (($orderID > 0) ? $orderID : False),
"DESCRIPTION" => "ORDER_PAY",
"EMPLOYEE_ID" => ($GLOBALS["USER"]->IsAuthorized() ? $GLOBALS["USER"]->GetID() : False)
);
CSaleUserTransact::Add($arFields);
//CSaleUserAccount::UnLock($userID, $payCurrency);
return True |