| Current Path : /var/www/homesaver/www/bitrix/classes/mysql/ |
| Current File : /var/www/homesaver/www/bitrix/classes/mysql/COfdFermaReceipt.php |
<?php
class COfdFermaReceipt extends CAllOfdFermaReceipt
{
function Add($arFields)
{
global $DB;
$arFields1 = array();
foreach ($arFields as $key => $value) {
if (substr($key, 0, 1) === '=') {
$arFields1[substr($key, 1)] = $value;
unset($arFields[$key]);
}
}
$events = GetModuleEvents(CMainOfdFerma::$MODULE_ID, 'OnBeforeReceiptAdd', true);
foreach ($events as $arEvent) {
if (ExecuteModuleEventEx($arEvent, Array(&$arFields)) === false) {
return false;
}
}
$arInsert = $DB->PrepareInsert('b_ofdferma_receipt', $arFields);
foreach ($arFields1 as $key => $value) {
if (strlen($arInsert[0]) > 0) {
$arInsert[0] .= ', ';
}
$arInsert[0] .= $key;
if (strlen($arInsert[1]) > 0) {
$arInsert[1] .= ', ';
}
$arInsert[1] .= $value;
}
$ID = false;
if (strlen($arInsert[0]) > 0) {
$strSql =
'INSERT INTO b_ofdferma_receipt(' . $arInsert[0] . ') ' .
'VALUES(' . $arInsert[1] . ')';
$DB->Query($strSql, false, 'File: ' . __FILE__ . '<br>Line: ' . __LINE__);
$ID = (int)$DB->LastID();
}
if ($ID) {
$events = GetModuleEvents(CMainOfdFerma::$MODULE_ID, 'OnReceiptAdd', true);
foreach ($events as $arEvent) {
ExecuteModuleEventEx($arEvent, Array($ID, &$arFields));
}
}
return $ID;
}
public static function Update($ID, $arFields)
{
global $DB;
$ID = (int)$ID;
$arFields1 = array();
foreach ($arFields as $key => $value) {
if (substr($key, 0, 1) == '=') {
$arFields1[substr($key, 1)] = $value;
unset($arFields[$key]);
}
}
$events = GetModuleEvents(CMainOfdFerma::$MODULE_ID, 'OnBeforeReceiptUpdate', true);
foreach ($events as $arEvent) {
if (ExecuteModuleEventEx($arEvent, Array($ID, &$arFields)) === false) {
return false;
}
}
$strUpdate = $DB->PrepareUpdate('b_ofdferma_receipt', $arFields);
foreach ($arFields1 as $key => $value) {
if (strlen($strUpdate) > 0) {
$strUpdate .= ', ';
}
$strUpdate .= $key . '=' . $value . ' ';
}
if (strlen($strUpdate) > 0) {
$strSql =
'UPDATE b_ofdferma_receipt SET ' .
' ' . $strUpdate . ' ' .
'WHERE ID = ' . $ID . ' ';
$DB->Query($strSql, false, 'File: ' . __FILE__ . '<br>Line: ' . __LINE__);
unset(static::$arReceiptCache[$ID]);
} else {
$ID = false;
}
if ($ID) {
$events = GetModuleEvents(CMainOfdFerma::$MODULE_ID, 'OnReceiptUpdate', true);
foreach ($events as $arEvent) {
ExecuteModuleEventEx($arEvent, Array($ID, &$arFields));
}
}
return $ID;
}
private static $arReceiptCache;
public function GetByID($ID)
{
global $DB;
$ID = (int)$ID;
if (
!empty(static::$arReceiptCache[$ID])
&& is_set(static::$arReceiptCache[$ID], 'ID')
) {
return static::$arReceiptCache[$ID];
} else {
static $strSql;
if (!isset($strSql)) {
$strSql =
'SELECT *, ' .
' ' . $DB->DateToCharFunction('DATE_CREATE', 'FULL') . ' as DATE_CREATE, ' .
' ' . $DB->DateToCharFunction('DATE_UPDATE', 'FULL') . ' as DATE_UPDATE ' .
'FROM b_ofdferma_receipt ' .
'WHERE ID = ';
}
$dbResult = $DB->Query($strSql . $ID, false, 'File: ' . __FILE__ . '<br>Line: ' . __LINE__);
if ($arResult = $dbResult->Fetch()) {
static::$arReceiptCache[$ID] = $arResult;
return $arResult;
}
}
return false;
}
public function GetByReceiptID($ReceiptID)
{
global $DB;
if (
!empty(static::$arReceiptCache[$ReceiptID])
&& is_set(static::$arReceiptCache[$ReceiptID], 'ID')
) {
return static::$arReceiptCache[$ReceiptID];
} else {
static $strSql;
if (!isset($strSql)) {
$strSql =
'SELECT *, ' .
' ' . $DB->DateToCharFunction('DATE_CREATE', 'FULL') . ' as DATE_CREATE, ' .
' ' . $DB->DateToCharFunction('DATE_UPDATE', 'FULL') . ' as DATE_UPDATE ' .
'FROM b_ofdferma_receipt ' .
'WHERE RECEIPT_ID = ';
}
$dbResult = $DB->Query($strSql . json_encode($ReceiptID), false, 'File: ' . __FILE__ . '<br>Line: ' . __LINE__);
if ($arResult = $dbResult->Fetch()) {
static::$arReceiptCache[$ReceiptID] = $arResult;
return $arResult;
}
}
return false;
}
public function GetByOrderID($OrderID)
{
global $DB;
if (
!empty(static::$arReceiptCache[$OrderID])
&& is_set(static::$arReceiptCache[$OrderID], 'ID')
) {
return static::$arReceiptCache[$OrderID];
} else {
static $strSql;
if (!isset($strSql)) {
$strSql =
'SELECT *, ' .
' ' . $DB->DateToCharFunction('DATE_CREATE', 'FULL') . ' as DATE_CREATE, ' .
' ' . $DB->DateToCharFunction('DATE_UPDATE', 'FULL') . ' as DATE_UPDATE ' .
'FROM b_ofdferma_receipt ' .
'WHERE ORDER_ID = ';
}
$dbResult = $DB->Query($strSql . json_encode($OrderID), false, 'File: ' . __FILE__ . '<br>Line: ' . __LINE__);
if ($arResult = $dbResult->Fetch()) {
static::$arReceiptCache[$OrderID] = $arResult;
return $arResult;
}
}
return false;
}
public function GetList(
$arOrder = Array('ID' => 'DESC'),
$arFilter = Array(),
$arGroupBy = false,
$arNavStartParams = false,
$arSelectFields = array()
) {
global $DB, $USER_FIELD_MANAGER;
static $blogPostEventIdList = null;
$obUserFieldsSql = new CUserTypeSQL;
$obUserFieldsSql->SetEntity('OFDFERMA_RECEIPT', 'P.ID');
$obUserFieldsSql->SetSelect($arSelectFields);
$obUserFieldsSql->SetFilter($arFilter);
$obUserFieldsSql->SetOrder($arOrder);
if (count($arSelectFields) <= 0) {
$arSelectFields = array(
'ID', 'ORDER_ID', 'RECEIPT_ID', 'DATE_CREATE', 'DATE_UPDATE', 'TYPE', 'CONTENT', 'TOTAL_PRICE',
'STATUS_ID', 'STATUS_NAME', 'STATUS_MESSAGE', 'DEVICE_ID', 'RNM', 'ZN', 'FN', 'FDN', 'FPD',
);
}
if (in_array('*', $arSelectFields)) {
$arSelectFields = array(
'ID', 'ORDER_ID', 'RECEIPT_ID', 'DATE_CREATE', 'DATE_UPDATE', 'TYPE', 'CONTENT', 'TOTAL_PRICE',
'STATUS_ID', 'STATUS_NAME', 'STATUS_MESSAGE', 'DEVICE_ID', 'RNM', 'ZN', 'FN', 'FDN', 'FPD',
);
}
$arFields = array(
'ID' => array('FIELD' => 'P.ID', 'TYPE' => 'int'),
'ORDER_ID' => array('FIELD' => 'P.ORDER_ID', 'TYPE' => 'int'),
'RECEIPT_ID' => array('FIELD' => 'P.RECEIPT_ID', 'TYPE' => 'string'),
'DATE_CREATE' => array('FIELD' => 'P.DATE_CREATE', 'TYPE' => 'datetime'),
'DATE_UPDATE' => array('FIELD' => 'P.DATE_UPDATE', 'TYPE' => 'datetime'),
'TYPE' => array('FIELD' => 'P.TYPE', 'TYPE' => 'string'),
'CONTENT' => array('FIELD' => 'P.CONTENT', 'TYPE' => 'string'),
'TOTAL_PRICE' => array('FIELD' => 'P.TOTAL_PRICE', 'TYPE' => 'decimal'),
'STATUS_ID' => array('FIELD' => 'P.STATUS_ID', 'TYPE' => 'int'),
'STATUS_NAME' => array('FIELD' => 'P.STATUS_NAME', 'TYPE' => 'string'),
'STATUS_MESSAGE' => array('FIELD' => 'P.STATUS_MESSAGE', 'TYPE' => 'string'),
'DEVICE_ID' => array('FIELD' => 'P.DEVICE_ID', 'TYPE' => 'int'),
'RNM' => array('FIELD' => 'P.RNM', 'TYPE' => 'string'),
'ZN' => array('FIELD' => 'P.ZN', 'TYPE' => 'string'),
'FN' => array('FIELD' => 'P.FN', 'TYPE' => 'string'),
'FDN' => array('FIELD' => 'P.FDN', 'TYPE' => 'string'),
'FPD' => array('FIELD' => 'P.FPD', 'TYPE' => 'string'),
);
foreach ($arFilter as $key => $val) {
$key_res = COfdFermaReceipt::GetFilterOperation($key);
$k = $key_res['FIELD'];
if (
$k == 'POST_PARAM_OFDFERMA_RECEIPT_IMPRTNT'
&& $key_res['NEGATIVE'] == 'Y'
&& $key_res['OR_NULL'] == 'N'
) {
unset($arFilter[$key]);
}
}
// <-- FIELDS
$bNeedDistinct = false;
$arSqls = COfdFermaReceipt::PrepareSql($arFields, $arOrder, $arFilter, $arGroupBy, $arSelectFields, $obUserFieldsSql);
if ($bNeedDistinct) {
$arSqls['SELECT'] = str_replace('%%_DISTINCT_%%', 'DISTINCT', $arSqls['SELECT']);
} else {
$arSqls['SELECT'] = str_replace('%%_DISTINCT_%%', '', $arSqls['SELECT']);
}
$r = $obUserFieldsSql->GetFilter();
if (strlen($r) > 0) {
$strSqlUFFilter = ' (' . $r . ') ';
}
if (is_array($arGroupBy) && count($arGroupBy) == 0) {
$strSql =
'SELECT ' . $arSqls['SELECT'] . ' ' .
$obUserFieldsSql->GetSelect() . ' ' .
'FROM b_ofdferma_receipt P ' .
' ' . $arSqls['FROM'] . ' ' .
$obUserFieldsSql->GetJoin('P.ID') . ' ';
if (strlen($arSqls['WHERE']) > 0) {
$strSql .= 'WHERE ' . $arSqls['WHERE'] . ' ';
}
if (strlen($arSqls['WHERE']) > 0 && strlen($strSqlUFFilter) > 0) {
$strSql .= ' AND ' . $strSqlUFFilter . ' ';
} elseif (strlen($arSqls['WHERE']) <= 0 && strlen($strSqlUFFilter) > 0) {
$strSql .= ' WHERE ' . $strSqlUFFilter . ' ';
}
if (strlen($arSqls['GROUPBY']) > 0) {
$strSql .= 'GROUP BY ' . $arSqls['GROUPBY'] . ' ';
}
//echo "!1!=".htmlspecialcharsbx($strSql)."<br>";
$dbRes = $DB->Query($strSql, false, 'File: ' . __FILE__ . '<br>Line: ' . __LINE__);
if ($arRes = $dbRes->Fetch()) {
return $arRes['CNT'];
} else {
return false;
}
}
$strSql =
'SELECT ' . $arSqls['SELECT'] . ' ' .
$obUserFieldsSql->GetSelect() . ' ' .
'FROM b_ofdferma_receipt P ' .
' ' . $arSqls['FROM'] . ' ' .
$obUserFieldsSql->GetJoin('P.ID') . ' ';
if (strlen($arSqls['WHERE']) > 0) {
$strSql .= 'WHERE ' . $arSqls['WHERE'] . ' ';
}
if (strlen($arSqls['WHERE']) > 0 && strlen($strSqlUFFilter) > 0) {
$strSql .= ' AND ' . $strSqlUFFilter . ' ';
} elseif (strlen($arSqls['WHERE']) <= 0 && strlen($strSqlUFFilter) > 0) {
$strSql .= ' WHERE ' . $strSqlUFFilter . ' ';
}
if (strlen($arSqls['GROUPBY']) > 0) {
$strSql .= 'GROUP BY ' . $arSqls['GROUPBY'] . ' ';
}
if (strlen($arSqls['ORDERBY']) > 0) {
$strSql .= 'ORDER BY ' . $arSqls['ORDERBY'] . ' ';
}
if (is_array($arNavStartParams) && IntVal($arNavStartParams['nTopCount']) <= 0) {
$strSql_tmp =
'SELECT COUNT(DISTINCT P.ID) as CNT ' .
'FROM b_ofdferma_receipt P ' .
' ' . $arSqls['FROM'] . ' ' .
$obUserFieldsSql->GetJoin('P.ID') . ' ';
if (strlen($arSqls['WHERE']) > 0) {
$strSql_tmp .= 'WHERE ' . $arSqls['WHERE'] . ' ';
}
if (strlen($arSqls['WHERE']) > 0 && strlen($strSqlUFFilter) > 0) {
$strSql_tmp .= ' AND ' . $strSqlUFFilter . ' ';
} elseif (strlen($arSqls['WHERE']) <= 0 && strlen($strSqlUFFilter) > 0) {
$strSql_tmp .= ' WHERE ' . $strSqlUFFilter . ' ';
}
if (strlen($arSqls['GROUPBY']) > 0) {
$strSql_tmp .= 'GROUP BY ' . $arSqls['GROUPBY'] . ' ';
}
//echo "!2.1!=".htmlspecialcharsbx($strSql_tmp)."<br>";
$dbRes = $DB->Query($strSql_tmp, false, 'File: ' . __FILE__ . '<br>Line: ' . __LINE__);
$cnt = 0;
if (strlen($arSqls['GROUPBY']) <= 0) {
if ($arRes = $dbRes->Fetch()) {
$cnt = $arRes['CNT'];
}
} else {
$cnt = $dbRes->SelectedRowsCount();
}
$dbRes = new CDBResult();
//echo "!2.2!=".htmlspecialcharsbx($strSql)."<br>";
$dbRes->SetUserFields($USER_FIELD_MANAGER->GetUserFields('OFDFERMA_RECEIPT'));
$dbRes->NavQuery($strSql, $cnt, $arNavStartParams);
} else {
if (is_array($arNavStartParams) && IntVal($arNavStartParams['nTopCount']) > 0) {
$strSql .= 'LIMIT ' . IntVal($arNavStartParams['nTopCount']);
}
//echo "!3!=".htmlspecialcharsbx($strSql)."<br>";
$dbRes = $DB->Query($strSql, false, 'File: ' . __FILE__ . '<br>Line: ' . __LINE__);
$dbRes->SetUserFields($USER_FIELD_MANAGER->GetUserFields('OFDFERMA_RECEIPT'));
}
//echo "!4!=".htmlspecialcharsbx($strSql)."<br>";
return $dbRes;
}
}