<?php
class ControllerToolMssqlConnect extends Controller{

    private $connection = null;
    private $statement = null;

    public function index()
    {

        $customer_id = 123455;
        $data['firstname'] = 'Bilal';
        $data['lastname'] = 'Çakır';
        $data['address_1'] = 'Maltepe';
        $data['address_2'] = 'Maltepe';
        $data['city'] = 'İstanbul';
        $data['country_id'] = '215';
        $data['email'] = 'bilal603@gmail.com';
        $data['customer_id'] = $customer_id;


        $ch = curl_init ('http://185.86.15.173:2616/user_save.php');
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $results = curl_exec($ch);

        curl_close($ch);

        var_dump($results); die();


    }

    public function stock_save()
    {

        $data['code'] = '152.01.' . rand(0,999999);
        $data['name'] = 'Demo Product 3';
        $data['specode'] = 'DP3';
        $data['b2ccode'] = '189'; // neon özellik ID
        $data['quantity'] = 5;
        $data['barcode'] = rand(0,99999999);



        $ch = curl_init ('http://185.86.15.173:2616/stock_save.php');
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $results = curl_exec($ch);

        curl_close($ch);

        var_dump($results); die();

    }

    public function order_save()
    {

        $data = array();
        $this->load->model('checkout/order');
        $order_info = $this->model_checkout_order->getOrder(36621);

        //var_dump($order_info['payment_iso_code_2']); die();
        //var_dump($order_info); die();

        $data['order_id'] = $order_info['order_id'];
        if($order_info['payment_method'] == ''){
            $data['payment_method'] = 'K.K';
        }else{
            $data['payment_method'] = 'KAPIDA';
        }
        $data['firstname'] = $order_info['firstname'];
        $data['lastname'] = $order_info['lastname'];
        $data['email'] = $order_info['email'];
        if($order_info['customer_id'] == 0){
            $data['customer_id'] = $order_info['order_id'];
        }else{
            $data['customer_id'] = $order_info['customer_id'];
        }

        $data['payment_address_1'] = $order_info['payment_address_1'];
        $data['shipping_address_1'] = $order_info['shipping_address_1'];
        $data['payment_city'] = $order_info['payment_city'];
        $data['payment_country'] = $order_info['payment_country'];
        $data['payment_iso_code_2'] = $order_info['payment_iso_code_2'];
        $data['total'] = $order_info['total'];

        $data['is_person'] = 1;

        $data['sub_total'] = 0;
        $data['tax'] = 0;
        $data['total'] = 0;
        $data['coupon'] = 0;
        $data['shipping'] = 0;
        $data['promotion_total'] = 0;

        // Sipariş Tolamları
        $order_totals = $this->db->query("SELECT * FROM ps_order_total WHERE order_id = '".$data['order_id']."' ")->rows;
        foreach ($order_totals as $total){
            if($total['code'] == 'sub_total'){
                $data['sub_total'] = $total['value'];
            }
            if($total['code'] == 'tax'){
                $data['tax'] = $total['value'];
            }
            if($total['code'] == 'total'){
                $data['total'] = $total['value'];
            }
            if($total['code'] == 'coupon'){
                $data['coupon'] = $total['value'] * 1;
            }
            if($total['code'] == 'shipping'){
                $data['shipping'] = $total['value'];
            }
            if($total['code'] == 'promotion_total'){
                $data['promotion_total'] = $total['value'] * 1;
            }
        }

        $order_products = $this->db->query("SELECT * FROM ps_order_product WHERE order_id = '".$data['order_id']."' ")->rows;
        $data['order_products'] = array();
        foreach ($order_products as $key => $product){
            $key++;
            $data['order_products'][] = array(
                'row' => $key,
                'logicalref' => '42',
                'linetype' => 0,
                'name' => $product['name'],
                'quantity' => $product['quantity'],
                'price' => $product['price'],
                'total' => $product['total'],
                'tax' => $product['total'],
                'tax_rate' => 18,
                'vat' => 1,
            );
        }



        $ch = curl_init ('http://185.86.15.173:2616/order_save.php');
        curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($ch, CURLOPT_POST, true);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
        $results = curl_exec($ch);

        curl_close($ch);

        var_dump($results); die();

    }


    
}