HEX
Server: Apache/2.4.6 (CentOS) mpm-itk/2.4.7-04 mod_fcgid/2.3.9 PHP/5.4.16
System: Linux dvm.vladweb.ru 3.10.0-1160.119.1.el7.x86_64 #1 SMP Tue Jun 4 14:43:51 UTC 2024 x86_64
User: region-gk.ru (1016)
PHP: 7.3.33
Disabled: NONE
Upload Files
File: //home/temp/yarusvl.ru/api/Variants.php
<?php

require_once('Mgc.php');

class Variants extends Mgc {

    /*Выборка вариантов*/
    public function get_variants($filter = array()) {
        $product_id_filter = '';
        $variant_id_filter = '';
        $instock_filter = '';

        if(!empty($filter['product_id'])) {
            $product_id_filter = $this->db->placehold('AND v.product_id in(?@)', (array)$filter['product_id']);
        }

        if(!empty($filter['id'])) {
            $variant_id_filter = $this->db->placehold('AND v.id in(?@)', (array)$filter['id']);
        }

        if(!empty($filter['in_stock']) && $filter['in_stock']) {
            $instock_filter = $this->db->placehold('AND (v.stock>0 OR v.stock IS NULL)');
        }

        if(!$product_id_filter && !$variant_id_filter) {
            return array();
        }
        $lang_sql = $this->languages->get_query(array('object'=>'variant'));
        $query = $this->db->placehold("SELECT 
                v.id, 
                v.product_id,
                v.weight,
                v.price, 
            v.price as real_price, 
        IF(v.opt_price>0,v.opt_price,0) as opt_price, 
                NULLIF(v.compare_price, 0) as compare_price, 
                v.sku, 
                IFNULL(v.stock, ?) as stock, 
                (v.stock IS NULL) as infinity, 
                v.attachment, 
                v.position, 
                v.currency_id, 
                v.feed,
                v.size,
                v.color_name,
                v.color_code,
                c.rate_from, 
                c.rate_to, 
                $lang_sql->fields
            FROM __variants AS v
            $lang_sql->join
            left join __currencies as c on(c.id=v.currency_id)
            WHERE
                1
                $product_id_filter
                $variant_id_filter
                $instock_filter
            GROUP BY v.id
            ORDER BY v.position, v.id
        ", $this->settings->max_order_amount);

        $this->db->query($query);
        $variants = $this->db->results();



        if (defined('IS_CLIENT') && !empty($variants)) {
            foreach($variants as $k=>  $row) {

                $variants[$k]->is_opt = $this->products->check_opt($row);
              //  var_dump($variants[$k]->is_opt);
              //  $variants[$k]->price = $this->products->get_opt($row);
                if ($row->rate_from != $row->rate_to && $row->currency_id) {
                    $row->price = $row->price*$row->rate_to/$row->rate_from;
                    $row->real_price =  $row->price ;
                    $row->compare_price = $row->compare_price*$row->rate_to/$row->rate_from;
                }
            }
        }else{
            foreach($variants as $k=> $row) {
                $variants[$k]->real_price =  $variants[$k]->price ;
          //      $variants[$k]->price = $this->products->get_opt($row);
                $variants[$k]->is_opt = $this->products->check_opt($row);


                   }
        }
        return $variants;
    }

    /*Выборка конкретного варианта*/
    public function get_variant($id) {
        if(empty($id)) {
            return false;
        }
        $variant_id_filter = $this->db->placehold('AND v.id=?', intval($id));

        $lang_sql = $this->languages->get_query(array('object'=>'variant'));
        $query = $this->db->placehold("SELECT 
                v.id, 
                v.product_id,
                v.weight,
                v.price, 
          v.price as real_price, 
         IF(v.opt_price>0,v.opt_price,0) as opt_price, 
                NULLIF(v.compare_price, 0) as compare_price, 
                v.sku, 
                IFNULL(v.stock, ?) as stock, 
                (v.stock IS NULL) as infinity, 
                v.attachment, 
                v.currency_id, 
                v.feed,
                c.rate_from, 
                c.rate_to, 
                $lang_sql->fields
            FROM __variants v
            $lang_sql->join
            left join __currencies as c on(c.id=v.currency_id) 
            WHERE 
                1 
                $variant_id_filter 
            LIMIT 1
        ", $this->settings->max_order_amount);

        $this->db->query($query);
        $variant = $this->db->result();
        $variant->real_price =  $variant->price ;

        $variant->is_opt = $this->products->check_opt($variant);
     //   $variant->price = $this->products->get_opt($variant);
        if (defined('IS_CLIENT') && $variant->id) {
            if ($variant->rate_from != $variant->rate_to && $variant->currency_id) {

                $variant->price = $variant->price*$variant->rate_to/$variant->rate_from;
                $variant->compare_price = $variant->compare_price*$variant->rate_to/$variant->rate_from;
            }
        }
        return $variant;
    }

    public function update_variant($id, $variant) {
        $variant = (object)$variant;
        $result = $this->languages->get_description($variant, 'variant');

        $v = (array)$variant;
        if (!empty($v)) {
            $query = $this->db->placehold("UPDATE __variants SET ?% WHERE id=? LIMIT 1", $variant, intval($id));
            $this->db->query($query);
        }

        if(!empty($result->description)) {
            $this->languages->action_description($id, $result->description, 'variant', $this->languages->lang_id());
        }

        return $id;
    }

    public function add_variant($variant) {
        $variant = (object)$variant;
        $result = $this->languages->get_description($variant, 'variant');

        $query = $this->db->placehold("INSERT INTO __variants SET ?%", $variant);
        $this->db->query($query);
        $variant_id = $this->db->insert_id();

        if(!empty($result->description)) {
            $this->languages->action_description($variant_id, $result->description, 'variant');
        }

        return $variant_id;
    }

    public function delete_variant($id) {
        if(!empty($id)) {
            $this->delete_attachment($id);
            $query = $this->db->placehold("DELETE FROM __variants WHERE id = ? LIMIT 1", intval($id));
            $this->db->query($query);
            $this->db->query('UPDATE __purchases SET variant_id=NULL WHERE variant_id=?', intval($id));
            $this->db->query("DELETE FROM __lang_variants WHERE variant_id = ?", intval($id));
        }
    }

    public function delete_attachment($id) {
        $query = $this->db->placehold("SELECT attachment FROM __variants WHERE id=?", $id);
        $this->db->query($query);
        $filename = $this->db->result('attachment');
        $query = $this->db->placehold("SELECT 1 FROM __variants WHERE attachment=? AND id!=?", $filename, $id);
        $this->db->query($query);
        $exists = $this->db->num_rows();
        if(!empty($filename) && $exists == 0) {
            @unlink($this->config->root_dir.'/'.$this->config->downloads_dir.$filename);
        }
        $this->update_variant($id, array('attachment'=>null));
    }

}