ElixirVault/www/system-brs-core/cms/cmsOrders/inc/cmsOrdersModule.php

478 lines
15 KiB
PHP

<?php
namespace BRSystems\Core\Cms\cmsOrders\inc;
use BRSystems\Core\Library\cDb\cDb;
use BRSystems\Core\Library\cJSON\cJSON;
class cmsOrdersModule
{
public cDb $db;
/**
* Class constructor
*/
public function __construct()
{
$this->db = cDb::getInstance();
}
/**
* Function to get module records
*
* @param string $where - sql query
* @param string $sort - sql query
* @param int $offset - db table offset
* @param int $limitPerPage - limit per page
* @return array of data
* @category cmsOrders Module
* @author Mareks Keišs <keiss.mareks@gmail.com>
* @version 1.1
*/
public function getModuleRecords($where, $sort, $offset = 0, $limitPerPage = 20): array
{
$sSql = "
SELECT SQL_CALC_FOUND_ROWS
mo.id,
mo.o_nr,
mo.o_date,
CASE WHEN LENGTH(mo.o_mail) > 0 THEN mo.o_mail ELSE ee.a_ben_email END as o_mail,
mo.o_edited,
mo.o_locked
FROM
mod_orders mo
LEFT JOIN mod_orders_edited ee ON
mo.id = ee.o_id
WHERE
1 {$where}
ORDER BY
{$sort}
LIMIT
{$offset}, {$limitPerPage}
";
$this->db->query($sSql);
return $this->db->getArray() ?? [];
}
/**
* Function to add or save record data
*
* @return int - record id
* @category cmsOrders Module
* @author Mareks Keišs <keiss.mareks@gmail.com>
* @version 1.1
*/
public function insertDummyOrder($data)
{
return $this->db->_queryInsert('mod_orders', $data);
}
/**
* Function to get next deleted order nr and reserve it
*
* @return bool
* @category cmsOrders Module
* @author Aivars Zukovskis <mareks.zukovskis@gmail.com>
* @version 1.1
*/
public function getNextDeletedOrderNr()
{
$nr = $this->db->_querySelectOne('mod_orders_deleted_nr', ['order_nr'], ['reserved' => 0], ['id' => 'ASC']);
if (strlen($nr)) {
$this->db->_queryUpdate('mod_orders_deleted_nr', ['reserved' => 1], ['order_nr' => $nr]);
}
return $nr;
}
/**
* Function to delete the reserved number
*
* @param string $sOrderNr - order number
* @return bool
* @author Aivars Zukovskis <mareks.zukovskis@gmail.com>
* @category cmsOrders Module
* @version 1.1
*/
public function deleteReservedOrderNr($sOrderNr)
{
return $this->db->_queryDelete('mod_orders_deleted_nr', ['reserved' => 1, 'order_nr' => $sOrderNr]);
}
/**
* Function to add or save record data
*
* @param bool $bEdited - indicates whether its first edit or not
* @param int $id - record id for update
* @param array $data - record data
* @return bool
* @author Mareks Keišs <keiss.mareks@gmail.com>
* @category cmsOrders Module
* @version 1.1
*/
public function saveRecordDataById($bEdited, $id, $data)
{
if ($id) {
$this->db->_queryDelete('mod_orders_edited', ['o_id' => $id]);
$this->db->_queryInsert('mod_orders_edited', $data, false, true);
$this->db->_queryUpdate('mod_orders', ['o_edited' => 1], ['id' => $id]);
return true;
}
return false;
}
public function saveTableData($id, $data)
{
if ($id) {
$this->db->_queryDelete('mod_orders_data_edited', ['o_id' => $id]);
if (!empty($data) && count($data)) {
$this->db->_queryInsertArray('mod_orders_data_edited', $data, false, true);
}
}
}
public function updateToCount($vData)
{
if (!!$vData && is_array($vData)) {
$this->db->_queryInsertArray('mod_products_to_count', $vData);
}
}
public function chkIfFormHasBeenEdited($id)
{
return $this->db->_querySelectOne('mod_orders', ['o_edited'], ['id' => $id]);
}
public function chkIfFormIsLocked($id)
{
return $this->db->_querySelectOne('mod_orders', ['o_locked'], ['id' => $id]);
}
/**
* Function to get record data by id
*
* @param int $id - record id
* @param string $subWhere - sub where
* @return array - record data
* @category cmsOrders Module
* @author Mareks Keišs <keiss.mareks@gmail.com>
* @version 1.1
*/
public function getRecordDataById($id)
{
return $this->db->_querySelectRow(
'mod_orders',
['id', 'o_nr', 'o_date', 'o_input_data', 'o_modified'],
['id' => $id],
);
}
public function getRecordDataById2($id)
{
return $this->db->_querySelectRow('mod_orders_edited', false, ['o_id' => $id]);
}
public function getTableData($id)
{
$id = $this->db->prepData($id);
$sSql = "
SELECT
dd.p_id,
dd.p_row_id as rid,
dd.p_type1 as cl1,
dd.p_type2 as cl2,
dd.p_price,
dd.p_count,
tt.t_title as cl1n,
tt2.t_title as cl2n,
ii.prod_name as p_name,
att.p_type as cl1t,
att2.p_type as cl2t
FROM
mod_orders_data dd
LEFT JOIN mod_products_info ii ON
ii.prod_id = dd.p_id
LEFT JOIN site_languages sl ON
sl.id = ii.lang_id
LEFT JOIN mod_items_attr_translations tt ON
tt.t_id = dd.p_type1
AND tt.t_lang_id = sl.id
LEFT JOIN mod_items_attr att ON
att.id = tt.t_id
LEFT JOIN mod_items_attr_translations tt2 ON
tt2.t_id = dd.p_type2
AND tt2.t_lang_id = sl.id
LEFT JOIN mod_items_attr att2 ON
att2.id = tt2.t_id
WHERE
dd.o_id = {$id}
AND sl.`default` = '1'
AND sl.type = '0'
";
$this->db->query($sSql);
return $this->db->getArray();
}
/**
* Function to get attribute types
*
* @param int $langId - default lang id
* @return array
* @category APP pubBuy
* @author Aivars Žukovskis <keiss.mareks@gmail.com>
*/
public function getAttTypes($langId)
{
$sSql = "
SELECT
att.id,
tt.t_title
FROM
mod_items_attr att
LEFT JOIN mod_items_attr_translations tt ON
tt.t_id = att.id
AND tt.t_lang_id = {$langId}
WHERE
att.p_type = 1
AND p_status = '0'
";
$this->db->query($sSql);
return $this->db->getFieldBasedArrayFieldsAdv('id', 't_title');
}
public function getTableData2($id)
{
return $this->db->_querySelect(
'mod_orders_data_edited',
['p_id', 'p_row_id as rid', 'p_cl1 as cl1', 'p_cl2 as cl2', 'row_nr', 'p_price', 'p_count', 'p_name'],
['o_id' => $id],
['row_nr' => 'ASC'],
);
}
public function deleteRecords($ids, $userId): bool
{
if (empty($ids) || !is_array($ids)) {
return false;
}
// Check if it's not older-then 30 days before delete, can delete only till 30 days
$where = ['id' => $ids, 'o_date' => ['o' => '>', 'v' => strtotime("-30 days")]];
$this->db->_querySelect('mod_orders', ['id'], $where, false, false, false);
$vIdsToDelete = $this->db->getKeyValBasedArray('id', 'id');
if (empty($vIdsToDelete)) {
cJSON::senderror("No id's to delete"); // Send error if no ids to delete
}
// Check if any of these orders are sent
$this->db->_querySelect('mod_orders', ['id'], ['id' => $vIdsToDelete, 'o_locked' => 1], false, false, false);
$vSentOrders = $this->db->getFieldBasedArrayFieldsAdv('id', 'id');
if (!!$vSentOrders && is_array($vSentOrders)) {
// Get all products and their counts
$fields = ['p_id', 'p_row_id', 'p_count', 'p_cl1', 'p_cl2'];
$where = ['o_id' => $vSentOrders, 'p_id' => ['o' => '>', 'v' => 0]];
$this->db->_querySelect('mod_orders_data_edited', $fields, $where, false, false, false);
$vProdData = $this->db->getArray();
// Create array for prod to count && insert
if (!!$vProdData && is_array($vProdData)) {
$insertToCount = [];
$iTime = time();
foreach ($vProdData as $v) {
$insertToCount[] = [
'prod_id' => $v['p_id'],
'row_id' => $v['p_row_id'],
'cl1_id' => $v['p_cl1'],
'cl2_id' => $v['p_cl2'],
'count' => $v['p_count'],
'op_type' => cmsOrdersController::PRODUCT_OPERATION_ORDER_DELETED,
'user_id' => $userId,
'op_date' => $iTime,
'date' => $iTime,
'modified' => $iTime,
'modified_by' => $userId,
'created' => $iTime,
'created_by' => $userId,
];
}
if (count($insertToCount) > 0) {
$this->db->_queryInsertArray('mod_products_to_count', $insertToCount);
}
}
}
// Gather all orders numbers && insert them in deleted order number table
$this->db->_querySelect('mod_orders', ['o_nr'], ['id' => $vIdsToDelete], false, false, false);
$vOrderNumbers = $this->db->getFieldBasedArrayFieldsAdv('o_nr', 'o_nr');
if (!!$vOrderNumbers && is_array($vOrderNumbers)) {
$vOrderNrInsert = [];
foreach ($vOrderNumbers as $v) {
$vOrderNrInsert[] = [
'order_nr' => $v,
'reserved' => 0,
'time' => time(),
];
}
if (!!$vOrderNrInsert) {
$this->db->_queryInsertArray('mod_orders_deleted_nr', $vOrderNrInsert);
}
}
// Delete order and its data
$this->db->_queryDelete('mod_orders_data_edited', ['o_id' => $vIdsToDelete]);
$this->db->_queryDelete('mod_orders_addresses', ['o_id' => $vIdsToDelete]);
$this->db->_queryDelete('mod_orders_edited', ['o_id' => $vIdsToDelete]);
$this->db->_queryDelete('mod_orders_data', ['o_id' => $vIdsToDelete]);
return $this->db->_queryDelete('mod_orders', ['id' => $vIdsToDelete]);
}
/**
* Function to change record status in db table
*
* @param array $ids - record ids to affect
* @param string $field - field to update for status ENUM
* @return bool true / false
* @category cmsOrders Module
* @author Mareks Keišs <keiss.mareks@gmail.com>
* @version 1.1
*/
public function changeRecordsStatus($ids, $field): bool
{
$table = 'mod_classificators';
$ids = implode(",", $ids);
$time = time();
$active = '1';
$inactive = '0';
$query = "UPDATE $table mc SET $field = '$active', cl_modified = '$time' WHERE $field = '0' AND id IN ($ids)";
if (!$this->db->query($query)) {
return false;
}
$where = "{$field} = '1' AND id IN ({$ids}) AND cl_modified <> '{$time}'";
$query = "UPDATE $table mc SET {$field} = '$inactive', cl_modified = '{$time}' WHERE $where";
return $this->db->query($query);
}
/**
* Function to get cl data by cl id
*
* @param int|array $id - cl id
* @return array
* @category APP pubBuy
* @author Mareks Keišs <keiss.mareks@gmail.com>
*/
public function getClDataByClId($id)
{
$id = (!!$id && is_array($id) ? implode(',', $id) : $id);
$sSql = "
SELECT
id,
cl_name as n,
cl_data as d
FROM
mod_classificators
WHERE
id IN ({$id})
AND cl_blocked = '0'
ORDER BY
cl_order ASC, cl_name ASC
";
$this->db->query($sSql);
return $this->db->getFieldBasedArrayFieldsAdv(['id'], ['n', 'd']);
}
/**
* Function to get cl data by cl type id
*
* @param int $id - cl id
* @return array
* @category APP pubBuy
* @author Mareks Keišs <keiss.mareks@gmail.com>
*/
public function getClDataByClTypeId($id)
{
$sSql = "
SELECT
id,
cl_name as n,
cl_data as d
FROM
mod_classificators
WHERE
cl_type = {$id}
AND cl_blocked = '0'
ORDER BY
cl_order ASC, cl_name ASC
";
$this->db->query($sSql);
return $this->db->getFieldBasedArrayFieldsAdv(['id'], ['n', 'd']);
}
/**
* Function to get cl data by cl type id
*
* @param string $sWhere - where clause
* @return array
* @category APP pubBuy
* @author Aivars Žukovskis <info@brsystems.lv>
*/
public function getProductAvailableCount($sWhere)
{
$vReturn = [];
if (strlen($sWhere)) {
$sSql = "
SELECT *
FROM view_products_count
WHERE {$sWhere}
";
$this->db->query($sSql);
$vReturn = $this->db->getFieldBasedArrayFieldsAdv(['prod_id', 'row_id'], ['cl1_id', 'cl2_id', 'count']);
}
return $vReturn;
}
/**
* Function to get cl data by cl type id
*
* @param string $sWhere - where clause
* @return array
* @category APP pubBuy
* @author Aivars Žukovskis <info@brsystems.lv>
*/
public function getProductAvailableCountAdv($sWhere)
{
$vReturn = [];
if (strlen($sWhere)) {
$sSql = "
SELECT
pc.prod_id as p_id,
pc.row_id as rid,
pc.cl1_id as cl1,
pc.cl2_id as cl2,
pc.count as p_count,
pc.price as p_price
FROM
view_products_count pc
LEFT JOIN mod_products_to_count_and_classificators cc ON
pc.row_id = cc.id
AND pc.prod_id = cc.prod_id
WHERE
cc.prod_disabled = '0'
AND {$sWhere}
HAVING
p_count > 0
";
$this->db->query($sSql);
$vReturn = $this->db->getFieldBasedArrayFieldsAdv(['p_id', 'rid'], ['cl1', 'cl2', 'p_count', 'p_price']);
}
return $vReturn;
}
}