db = cDb::getInstance(); } /** * Function to check if product by id exists in db * * @author Mareks Keišs * @category pubBuy * @param int $prodId * @return array of data * @version 1.1 */ public function chkProdExists($prodId){ return $this->db->_querySelectOne('mod_products', array('id'), array('id' => $prodId, 'prod_deleted' => 0, 'prod_disabled' => 0)); } /** * Function to get delivery options for order * * @author Mareks Keišs * @category pubBuy * @param int $pId - product id * @param string $langId - language id * @param int $d1 - first selected attribute id * @param int $d2 - second selected attribute id * @param bool $defLangCode - indicates if we need default lang code too. * @return array of data * @version 1.1 */ public function getAddedProdData($pId, $langId, $d1 = 0, $d2 = 0, $defLangCode = false){ $pId = $this->db->prepData($pId); $d1 = (int)$this->db->prepData($d1); $d2 = (int)$this->db->prepData($d2); $variantWhere = ''; if ($d1 > 0 && $d2 > 0) { $variantWhere = " AND ( (cc.prod_cl_id = {$d1} AND cc.prod_cl_id_2 = {$d2}) OR (cc.prod_cl_id = {$d2} AND cc.prod_cl_id_2 = {$d1}) ) "; } elseif ($d1 > 0) { $variantWhere = "AND ({$d1} IN (cc.prod_cl_id, cc.prod_cl_id_2))"; } elseif ($d2 > 0) { $variantWhere = "AND ({$d2} IN (cc.prod_cl_id, cc.prod_cl_id_2))"; } $sSql = " SELECT ii.prod_name as t, CONCAT(uu.url_fname, CASE WHEN sl.default = '1' THEN " . ($defLangCode?"sl.lang_code":"''") . " ELSE sl.lang_code END) as u, cc.prod_price as price, pp.prod_discount, pp.prod_discount_value, att.t_title as c1, att2.t_title as c2, img.img_path as img, cc.id as r_id, cc.prod_cl_id as cl1, cc.prod_cl_id_2 as cl2 FROM mod_products pp LEFT JOIN mod_products_to_count_and_classificators cc ON pp.id = cc.prod_id LEFT JOIN mod_products_info ii ON pp.id = ii.prod_id AND ii.lang_id = {$langId} LEFT JOIN mod_items_attr_translations att ON att.t_id = cc.prod_cl_id AND att.t_lang_id = ii.lang_id LEFT JOIN mod_items_attr_translations att2 ON att2.t_id = cc.prod_cl_id_2 AND att2.t_lang_id = ii.lang_id LEFT JOIN site_map_urls uu ON uu.url_item_id = pp.id AND uu.url_lang_id = ii.lang_id LEFT JOIN site_languages sl ON uu.url_lang_id = sl.id LEFT JOIN mod_products_images img ON pp.id = img.prod_id AND img.img_blocked = '0' AND img.img_order = 1 WHERE pp.id = {$pId} AND pp.prod_disabled = '0' AND cc.prod_disabled = '0' {$variantWhere} AND uu.url_type = 0 ORDER BY uu.id DESC, cc.id DESC LIMIT 25 "; $this->db->query($sSql); return $this->db->getArray(); } /** * Function to get delivery options for order * * @author Mareks Keišs * @category pubBuy * @param array $ids * @return array of data * @version 1.1 */ public function getCartDeliveryOptions($ids){ if(isset($ids['cat']) && count($ids['cat']) == 0) $ids['cat'] = 0; if(isset($ids['act']) && count($ids['act']) == 0) $ids['act'] = 0; $ids['cat'] = (is_array($ids['cat']) ? implode(',', $ids['cat']) : $ids['cat']); $ids['act'] = (is_array($ids['act']) ? implode(',', $ids['act']) : $ids['act']); $sSql = " SELECT shops.* FROM ( SELECT 'cat' as t, dd.prod_id, dd.shops_id as id FROM mod_cat_products_to_shops dd WHERE dd.prod_id IN ({$ids['cat']}) /*UNION ALL SELECT 'act' as t, dd.prod_id, dd.shops_id as id FROM mod_products_to_shops dd WHERE dd.prod_id IN ({$ids['act']})*/ ) shops LEFT JOIN mod_shops ss ON ss.id = shops.id WHERE ss.shop_dummy = '0' AND ss.shop_disabled = '0' "; $this->db->query($sSql); return $this->db->getFieldBasedArray('t', 'prod_id', false, 'id'); } /** * Function to get payment & delivery options for order * * @author Mareks Keišs * @category pubBuy * @param array $ids - product ids * @return array of data * @version 1.1 */ public function getCartPaymentAndDeliveryOptions($ids){ $ids = (!!$ids && count($ids) ? implode(',', $ids) : '0'); $sSql = " SELECT mp.id as prod_id, cc.id, cc.cl_type as `type`, cc.cl_name as n FROM mod_products mp LEFT JOIN ( SELECT pp.prod_id, pp.paym_id as cl_id FROM mod_products_to_payments pp UNION ALL SELECT pd.prod_id, pd.dev_id as cl_id FROM mod_products_to_delivery pd ) prod ON mp.id = prod.prod_id LEFT JOIN mod_classificators cc ON cc.id = prod.cl_id WHERE mp.id IN ({$ids}) AND (cc.cl_blocked = '0' OR ISNULL(cc.id)) order by cc.id "; $this->db->query($sSql); return $this->db->getFieldBasedArrayFieldsAdv(array('prod_id', 'type', 'id'), 'n'); } /** * Function to get payment translation data for order * * @author Mareks Keišs * @category pubBuy * @param array $ids - shop ids * @param int $langId - language id * @return array of data * @version 1.1 */ public function getPaymentData($ids, $langId){ $ids = (is_array($ids) ? implode(',', $ids) : $ids); if(!!$ids){ $sSql = " SELECT cc.id, cc.cl_name as `key`, ccl.cl_value as title FROM mod_classificators cc LEFT JOIN mod_classificators_translations ccl ON ccl.cl_id = cc.id AND ccl.cl_lang_id = {$langId} WHERE cc.cl_blocked = '0' AND cc.id IN ({$ids}) ORDER BY cc.cl_order "; $this->db->query($sSql); return $this->db->getFieldBasedArray('id'); } } /** * Function to check all product availability * * @author Mareks Keišs * @category APP pubBuy * @param array $ids - array of cart ids or specific id * @return array of data * @version 1.1 */ public function getProdCount($ids){ if(!isset($ids['cat']) || isset($ids['cat']) && count($ids['cat']) == 0) $ids['cat'] = 0; if(!isset($ids['act']) || isset($ids['act']) && count($ids['act']) == 0) $ids['act'] = 0; $ids['cat'] = (is_array($ids['cat']) ? implode(',', $ids['cat']) : $ids['cat']); $ids['act'] = (is_array($ids['act']) ? implode(',', $ids['act']) : $ids['act']); $sSql = " /*SELECT 'act' as t, pp.prod_id, '0,0' as division, pp.prod_count, pp.prod_available FROM view_product_bought pp WHERE pp.prod_id IN ({$ids['act']}) UNION ALL*/ SELECT 'cat' as t, pp.prod_id, CONCAT(pp.prod_cl_id,',',pp.prod_cl_id_2) as division, pp.prod_sold, pp.prod_available FROM view_product_count pp WHERE pp.prod_id IN ({$ids['cat']}) "; $this->db->query($sSql); return $this->db->getFieldBasedArray('t', 'prod_id', false); } /** * Function to insert different data into db tables by array * * @author Mareks Keišs * @category pubBuy * @param string $sTableName - table name * @param array $oData - data to insert * @return array of data * @version 1.1 */ public function insertDataIntoDbTable($sTableName, $oData = array()){ if(strlen($sTableName) && !!$oData) return $this->db->_queryInsertArray($sTableName, $oData, false, true); return false; } /** * Function to update data * * @author Mareks Keišs * @category pubBuy * @param int $order_id - order id * @param array $vData - data to update * @return array of data * @version 1.1 */ public function updateOrdersData($order_id, $vData = array()){ $this->db->_queryUpdate(self::TABLE_ORDERS, $vData, array('id' => $order_id)); return false; } public function getUserOrderHistory(int $userId, int $limit = 50): array { $limit = max(1, min(200, $limit)); if ($userId <= 0) { return []; } $sSql = " SELECT mo.id, mo.o_nr, mo.o_date, mo.o_mail, mo.o_payment_met, mo.o_delivery_met, mo.o_input_data, mo.o_modified, COALESCE(SUM(od.p_count), 0) as items_qty, COALESCE(SUM(od.p_price * od.p_count), 0) as subtotal, oa.o_addr_name, oa.o_addr_lastname, oa.o_addr_address, oa.o_addr_city, oa.o_addr_country, oa.o_addr_zip, oa.o_addr_phone FROM mod_orders mo LEFT JOIN mod_orders_data od ON od.o_id = mo.id LEFT JOIN mod_orders_addresses oa ON oa.o_id = mo.id AND oa.o_type = 0 WHERE mo.o_user = {$userId} AND mo.o_deleted = '0' GROUP BY mo.id, mo.o_nr, mo.o_date, mo.o_mail, mo.o_payment_met, mo.o_delivery_met, mo.o_input_data, mo.o_modified, oa.o_addr_name, oa.o_addr_lastname, oa.o_addr_address, oa.o_addr_city, oa.o_addr_country, oa.o_addr_zip, oa.o_addr_phone ORDER BY mo.o_date DESC, mo.id DESC LIMIT {$limit} "; $this->db->query($sSql); return $this->db->getArray() ?? []; } public function assignOrdersToUserByEmail(int $userId, string $email): bool { $email = trim($this->db->prepData($email)); if ($userId <= 0 || !strlen($email)) { return false; } return (bool)$this->db->query(" UPDATE mod_orders SET o_user = {$userId} WHERE (o_user = 0 OR o_user IS NULL) AND o_mail = '{$email}' AND o_deleted = 0 "); } public function getOrdersInputDataByEmail(string $email): array { $email = trim($this->db->prepData($email)); if (!strlen($email)) { return []; } $sSql = " SELECT id, o_input_data FROM mod_orders WHERE o_mail = '{$email}' "; $this->db->query($sSql); return $this->db->getArray() ?? []; } /** * Function to reserve product by id * * @author Mareks Keišs * @category pubBuy * @param int|bool $order_id - order id to * @param array $order - base order data * @param array $delivery - order delivery data * @param array $receipt - order receipt data * @param array $products - order details * @return array of data * @version 1.1 */ public function reserveProduct(&$order_id = false, $order = array(), $delivery = array(), $receipt = array(), $products = array()){ if(!!$order && !!$delivery){ if($order_id){ // Existing order - update data $this->db->_queryUpdate(self::TABLE_ORDERS, $order, array('id' => $order_id)); $this->db->_queryUpdate(self::TABLE_ORDERS_DELIVERY, $delivery, array('order_id' => $order_id), true); if(!!($receipt)){ $this->db->_queryUpdate(self::TABLE_ORDERS_RECEIPTS, array('rec_status' => '2'), array('order_id' => $order_id, 'rec_status' => '0'), true); $this->db->_queryInsert(self::TABLE_ORDERS_RECEIPTS, $receipt); } if(!!($products)){ $this->db->_queryDelete(self::TABLE_ORDERS_DATA, array('order_id' => $order_id)); $this->db->_queryInsertArray(self::TABLE_ORDERS_DATA, $products); } return $order_id; }else{ // New order if($order_id = $this->db->_queryInsert(self::TABLE_ORDERS, $order)){ $this->db->_queryInsert(self::TABLE_ORDERS_DELIVERY, $delivery, false, true); if(!!($receipt)) $this->db->_queryInsert(self::TABLE_ORDERS_RECEIPTS, $receipt); if(!!($products)) $this->db->_queryInsertArray(self::TABLE_ORDERS_DATA, $products); return $order_id; } } } return false; } /** * Function to get row from custom table * * @author Mareks Keišs * @category pubBuy * @param string $table - table from which to select data * @param array $where - data for query construction * @return array of row data * @version 1.1 */ public function getRowDataByTable($table, $where) { return $this->db->_querySelectRow($table, false, $where); } /** * Function to get receipt data for bill pdf generation * * @author Mareks Keišs * @category pubBuy * @param int $receipt_Id * @param int $lang_id * @return array of data * @version 1.1 */ public function getOrderDataForReceipt($receipt_Id, $lang_id){ if($receipt_Id){ $order = array(); if($order['receipt'] = $this->getRowDataByTable(self::TABLE_ORDERS_RECEIPTS, array('rec_nr' => $receipt_Id))){ $order['order'] = $this->getRowDataByTable(self::TABLE_ORDERS, array('id' => $order['receipt']['order_id'])); $order['delivery'] = $this->getRowDataByTable(self::TABLE_ORDERS_DELIVERY, array('order_id' => $order['receipt']['order_id'])); $sSql = " SELECT pp.*, COALESCE(ct.cl_value) as cl_1, COALESCE(ct2.cl_value, '') as cl_2, ci.prod_name as prod_name, cp.prod_nr as prod_nr /*CASE WHEN pp.prod_type = '0' THEN ai.prod_name ELSE ci.prod_name END as prod_name, CASE WHEN pp.prod_type = '0' THEN ap.prod_nr ELSE cp.prod_nr END as prod_nr*/ FROM mod_orders_data pp LEFT JOIN mod_cat_products_info ci ON ci.prod_id = pp.prod_id AND ci.lang_id = {$lang_id} AND pp.prod_type = '1' LEFT JOIN mod_cat_products cp ON cp.id = ci.prod_id /*LEFT JOIN mod_products_info ai ON ai.prod_id = pp.prod_id AND ai.lang_id = {$lang_id} AND pp.prod_type = '0' LEFT JOIN mod_products ap ON ap.id = ai.prod_id*/ LEFT JOIN view_classificator_translations ct ON ct.id = pp.prod_cl_id AND ct.lang_id = {$lang_id} LEFT JOIN view_classificator_translations ct2 ON ct2.id = pp.prod_cl_id_2 AND ct2.lang_id = {$lang_id} WHERE pp.order_id = {$order['receipt']['order_id']} "; //$order['products'] = $this->db->_querySelect(self::TABLE_ORDERS_DATA, false, array('order_id' => $order['receipt']['order_id'])); $this->db->query($sSql); $order['products'] = $this->db->getArray(); } return $order; } return false; } /** * Function to update order reservation status by receipt nr * * @author Mareks Keišs * @category pubBuy * @param string $recNr * @return array of data * @version 1.1 */ public function updOrderReservationById($recNr){ if($recNr){ $sSql = " UPDATE mod_orders oo, mod_orders_receipts rr SET oo.order_res = '0', oo.order_res_till = 0 where rr.rec_nr = '{$recNr}' AND oo.id = rr.order_id "; return $this->db->query($sSql); } } /** * Function to get receipt row data * * @author Mareks Keišs * @category pubBuy * @param string $recNr * @return array of data * @version 1.1 */ public function getReceiptByNr($recNr) { return $this->db->_querySelectRow('mod_orders_receipts', false, ['rec_nr' => $recNr]); } public function updReceiptByReceiptNr($recNr, $data): bool { return $this->db->_queryUpdate('mod_orders_receipts', $data, ['rec_nr' => $recNr]); } /** * Function to update order data by id * * @author Mareks Keišs * @category pubBuy * @param int $id - order id * @param array $data - data to store * @return bool of data * @version 1.1 */ public function updOrderById($id, $data): bool { return $this->db->_queryUpdate('mod_orders', $data, ['id' => $id]); } /** * Function to get all cart data for new payment attempt * * @author Mareks Keišs * @category pubBuy * @param int $orderId - order id * @return array of data * @version 1.1 */ public function reloadCartContentByRecId($orderId){ if($orderId){ $d = array(); $sSql = " SELECT CASE WHEN dd.prod_type = '0' THEN 'act' ELSE 'cat' END as typeK, dd.prod_count as `count`, dd.prod_id, CONCAT(dd.prod_cl_id, ',', dd.prod_cl_id_2) as t FROM mod_orders_data dd WHERE dd.order_id = {$orderId} "; $this->db->query($sSql); $d['cart'] = $this->db->getFieldBasedArray('typeK', 'prod_id', false); $sSql = " SELECT dd.prod_delivery_json FROM mod_orders_delivery dd WHERE dd.order_id = {$orderId} "; $this->db->query($sSql); $d['inp'] = json_decode($this->db->getOne(), true); return $d; } return false; } /** * Function to check if order payment has failed * * @author Mareks Keišs * @category pubBuy * @param string $recNr - receipt nr * @return array of data * @version 1.1 */ public function chkOrderFailed($recNr){ if($recNr){ $sSql = " SELECT rr.order_id FROM mod_orders_receipts rr WHERE rr.rec_nr = '{$recNr}' AND rr.rec_status = '0' AND rr.rec_type <> " . pubBuyController::PMET_OTHER_E . " "; $this->db->query($sSql); return $this->db->getOne(); } return false; } /** * Function to get order receipt info for * * @author Mareks Keišs * @category pubBuy * @param string $recNr - receipt nr * @return array of data * @version 1.1 */ public function getOrderDataForReceiptMail($recNr){ if($recNr){ $sSql = " SELECT dd.prod_delivery_json FROM mod_orders_receipts rr LEFT JOIN mod_orders_delivery dd ON dd.order_id = rr.order_id WHERE rr.rec_nr = '{$recNr}' "; $this->db->query($sSql); return cJSON::decode($this->db->getOne(), true); } return false; } /** * Function to get order receipt info for * * @author Mareks Keišs * @category pubBuy * @param string $recNr - receipt nr * @return false|array of data * @version 1.1 */ public function getPageBackPaymentData(string $recNr): array { if ($recNr) { $sSql = " SELECT rr.rec_nr as order_nr, rr.rec_value as order_sum, rec_type FROM mod_orders_receipts rr WHERE rr.rec_nr = '{$recNr}' "; $this->db->query($sSql); return $this->db->getRow(); } return false; } public function getClassificatorDataById(int $id): array { $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 next deleted order nr and reserve it * * @author Aivars Zukovskis * @category cmsOrders Module * @return bool|string * @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; } public function deleteReservedOrderNr(string $sOrderNr): bool { return $this->db->_queryDelete('mod_orders_deleted_nr', ['reserved' => 1, 'order_nr' => $sOrderNr]); } }