https://t.me/RX1948
Server : Apache/2.4.18 (Ubuntu)
System : Linux canvaswebdesign 3.13.0-71-generic #114-Ubuntu SMP Tue Dec 1 02:34:22 UTC 2015 x86_64
User : oppastar ( 1041)
PHP Version : 7.0.33-0ubuntu0.16.04.15
Disable Function : pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,
Directory :  /var/www/kanvakanva.com/public_html/application/controllers/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : //var/www/kanvakanva.com/public_html/application/controllers/Payment.php
<?php if (!defined('BASEPATH')) {
  exit('No direct script access allowed');
}

class Payment extends Customer_Controller
{
  function __construct()
  {
    parent::__construct();
    $this->load->model('order_m');
    $this->load->model('order_detail_m');
    $this->load->model('product_m');
    $this->load->model('configuration_m');
    $this->load->model('customer_m');
    $this->load->helper('rajaongkir');
  }

  public function process_payment()
  {
    $data['carrier'] = 'jne';
		$data['code_carrier'] = 'jne_reguler';
		$data['carrier_name'] = 'JNE REG';

    if (!isset($_POST['process_payment'])) {
      show_404();
    }

    //manual bank transfer
    if ($this->input->post('payment_type') == 'bank_transfer') {
      $payment_type = 'bank transfer';

      $this->insert_new_order($payment_type);

      //----SEND EMAIL TO CUSTOMER (BANK TRANSFER)
      $data['bank'] = $this->configuration_m->get_bank();
      $data['title'] = 'Bank Transfer';

      //get website logo
      $this->db
        ->select('logo')
        ->from('configuration')
        ->where('id_configuration', 1);
      $data['logo'] = $this->db->get()->row()->logo;

      //get order detail and customer detail
      $data['order'] = $this->order_m->get_order(
        $this->session->userdata('order_id')
      );
      $data['order_details'] = $this->order_detail_m->get_orders_detail(
        $this->session->userdata('order_id')
      );

      //get vouchers detail
      if ($this->session->userdata('chosen_voucher_code')) {
        $data['chosen_voucher_code'] = $this->session->userdata(
          'chosen_voucher_code'
        );
        $data['chosen_voucher_type'] = $this->session->userdata(
          'chosen_voucher_type'
        );
        $data['chosen_voucher_discount'] = $this->session->userdata(
          'chosen_voucher_discount'
        );
      }

      //get shippng fee total
      $data['carrier_name'] = $this->session->userdata('carrier_name');
      $data['total_shipping_fee'] = $this->session->userdata(
        'total_shipping_fee'
      );

      $config['protocol'] = 'smtp';
      $config['smtp_host'] = 'ssl://smtp.gmail.com'; //change this
      $config['smtp_port'] = '465';
      $config['smtp_user'] = 'info@kanvakanva.com'; //change this
      $config['smtp_pass'] = 'Akunk4nva'; //change this
      $config['mailtype'] = 'html';
      $config['charset'] = 'iso-8859-1';
      $config['wordwrap'] = true;
      $config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard

      $this->load->library('email');
      $this->email->initialize($config);

      $data['emails'] = $this->configuration_m->get_emails();

      //$this->email->from($data['emails']->from_email, $data['emails']->website_name);
      $this->email->from('info@kanvakanva.com', $data['emails']->website_name);

      $this->email->to($this->session->userdata('customer')['customer_email']);
      $this->email->cc($data['emails']->from_email);
      $this->email->subject('Order Notification');

      $email = $this->load->view('email/bank_transfer', $data, true);
      $this->email->message($email);
      $email_sent_status = $this->email->send();

      //if(!$email_sent_status) {
       	//echo $this->email->print_debugger();
      //}
      //----end send email

      //LOAD PAYMENT RETURN PAGE
      $data['bank'] = $this->configuration_m->get_bank();
      $data['email'] = $this->session->userdata('customer')['customer_email'];
      $data['order'] = $this->order_m->get_order(
        $this->session->userdata('order_id')
      );

      $order_details = $this->db
        ->select('*')
        ->from('orders_detail')
        ->where('orders_id', (int) $this->session->userdata('order_id'))
        ->get()
        ->result();

      //get order item/product
      $data['products_text'] = '';
      $data['product_amount'] = 0;

      foreach ($order_details as $detail) {
        //get product alias
        $product_alias = $this->db
          ->select('alias')
          ->from('products')
          ->where('id_products', $detail->item_id)
          ->get()
          ->row()->alias;

        if ($data['products_text'] == '') {
          $data['products_text'] = "'" . $product_alias . "'";
        } else {
          $data['products_text'] =
            $data['products_text'] . ', ' . "'" . $product_alias . "'";
        }

        $data['product_amount'] =
          $data['product_amount'] + $detail->item_price * $detail->quantity;
      }

      //get SEO
      $this->db
        ->select('website_name, meta_keywords')
        ->from('configuration')
        ->where('id_configuration', 1);
      $website_name = $this->db->get()->row();
      $this->data_header['browser_title'] =
        ucwords($website_name->website_name) . ' - Payment';
      $this->data_header['meta_description'] =
        ucwords($website_name->website_name) . ' - Payment';
      $this->data_header['meta_keywords'] = $website_name->meta_keywords;

      $this->cart->destroy();

      $this->load->view('template/header', $this->data_header);
      $this->load->view('banktransfer', $data);
      $this->load->view('template/footer', $this->data_footer);

      $this->destroy_session_data();
    }

    //MITRANS CREDIT CARD & VIRTUAL ACCOUNT BANK TRANSFER
    if (
      $this->input->post('payment_type') == 'veritrans' ||
      $this->input->post('payment_type') == 'virtualaccount'
    ) {
      $payment_type = $this->input->post('payment_type');

      $this->insert_new_order($payment_type);

      //----SEND EMAIL TO CUSTOMER (BANK TRANSFER)
      $data['bank'] = $this->configuration_m->get_bank();
      $data['title'] = 'Veritrans';

      //get website logo
      $this->db
        ->select('logo')
        ->from('configuration')
        ->where('id_configuration', 1);
      $data['logo'] = $this->db->get()->row()->logo;

      //get order detail and customer detail
      $data['order'] = $this->order_m->get_order(
        $this->session->userdata('order_id')
      );
      $data['order_details'] = $this->order_detail_m->get_orders_detail(
        $this->session->userdata('order_id')
      );

      //get vouchers detail
      if ($this->session->userdata('chosen_voucher_code')) {
        $data['chosen_voucher_code'] = $this->session->userdata(
          'chosen_voucher_code'
        );
        $data['chosen_voucher_type'] = $this->session->userdata(
          'chosen_voucher_type'
        );
        $data['chosen_voucher_discount'] = $this->session->userdata(
          'chosen_voucher_discount'
        );
      }

      //get shippng fee total
      $data['carrier_name'] = $this->session->userdata('carrier_name');
      $data['total_shipping_fee'] = $this->session->userdata(
        'total_shipping_fee'
      );

      //send email to customer link for confiramtion retrieve new password
      $config['protocol'] = 'smtp';
      $config['smtp_host'] = 'ssl://smtp.gmail.com'; //change this
      $config['smtp_port'] = '465';
      $config['smtp_user'] = 'info@kanvakanva.com'; //change this
      $config['smtp_pass'] = 'Akunk4nva'; //change this
      $config['mailtype'] = 'html';
      $config['charset'] = 'iso-8859-1';
      $config['wordwrap'] = true;
      $config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard

      $this->load->library('email');
      $this->email->initialize($config);

      $data['emails'] = $this->configuration_m->get_emails();

      //$this->email->from($data['emails']->from_email, $data['emails']->website_name);
      $this->email->from('info@kanvakanva.com', $data['emails']->website_name);

      $this->email->to($this->session->userdata('customer')['customer_email']);
      $this->email->cc($data['emails']->from_email);
      $this->email->subject('Order Notification');
      $email = $this->load->view('email/creditcard', $data, true);
      $this->email->message($email);
      $email_sent_status = $this->email->send();

      // if(!$email_sent_status) {
      // 	echo $this->email->print_debugger();
      // }

      //VERITRANS IN ACTION
      require_once APPPATH . 'third_party/Veritrans.php';

      //get key
      $this->db
        ->select(
          'veritrans_server_key, veritrans_client_key, veritrans_production_mode'
        )
        ->from('configuration')
        ->where('id_configuration', 1);
      $veritrans = $this->db->get()->row();
      $production_mode = $veritrans->veritrans_production_mode;

      //Set Your server key
      Veritrans_Config::$serverKey = $veritrans->veritrans_server_key;

      if ($production_mode == 'true') {
        Veritrans_Config::$isProduction = true;
      } else {
        Veritrans_Config::$isProduction = false;
      }

      // Enable sanitization
      Veritrans_Config::$isSanitized = true;

      // Enable 3D-Secure
      Veritrans_Config::$is3ds = true;

      $data_order = $this->order_m->get_order(
        $this->session->userdata('order_id')
      );

      $transaction_details = [
        'order_id' => (int) $this->session->userdata('order_id'),
        'gross_amount' => (int) $data_order->total_amount,
      ];

      // Populate items
      // define a two-dimensional array
      $cart = $this->cart->contents();

      foreach ($cart as $cart_item) {
        $items[] = [
          'id' => $cart_item['id'],
          'price' => $cart_item['price'],
          'quantity' => $cart_item['qty'],
          'name' => strip_tags(substr($cart_item['name'], 0, 48)) . '..',
        ];
      }

      //add voucher fee into the item
      if ($this->session->userdata('chosen_voucher_code')) {
        if ($this->session->userdata('chosen_voucher_type') == 'amount') {
          //by amount
          //deduct voucher into item, so gross == total items
          $items[] = [
            'id' => 'voucher',
            'price' => -$this->session->userdata('chosen_voucher_discount'),
            'quantity' => 1,
            'name' =>
              'Voucher: ' . $this->session->userdata('chosen_voucher_code'),
          ];
        } else {
          //by percentage
          //deduct voucher into item, so gross == total items
          $items[] = [
            'id' => 'voucher',
            'price' => -(
              ($this->session->userdata('chosen_voucher_discount') *
                $this->session->userdata('product_grand_total')) /
              100
            ),
            'quantity' => 1,
            'name' =>
              'Voucher: ' . $this->session->userdata('chosen_voucher_code'),
          ];
        }
      }

      if($data_order->discount_redeem > 0){
        $items[] = [
          'id' => 'disc_redeem_point',
          'price' => '-'.$data_order->discount_redeem,
          'quantity' => 1,
          'name' => 'Discount Redeem Point',
        ];
      }

      //add shipping fee into the item, so gross == total items
      $items[] = [
        'id' => 'shipping',
        'price' => $this->session->userdata('total_shipping_fee'),
        'quantity' => 1,
        'name' => 'Shipping ' . $this->session->userdata('carrier_name'),
      ];

      //get customer district / city name
      $this->db
        ->select(
          'name, email, address, district, province, postcode, phone, recipient_name'
        )
        ->from('customers')
        ->where(
          'id_customers',
          (int) $this->session->userdata('customer')['customer_id']
        );
      $customer_data = $this->db->get()->row();

      // Populate customer's billing address
      $billing_address = [
        'first_name' => $customer_data->name,
        'last_name' => '',
        'address' => $customer_data->address,
        'city' => $customer_data->district . '. ' . $customer_data->province,
        'postal_code' => $customer_data->postcode,
        'phone' => $customer_data->phone,
        'country_code' => 'IDN',
      ];

      // Populate customer's shipping address
      $shipping_address = [
        'first_name' => $customer_data->name,
        'last_name' => '',
        'address' => $customer_data->address,
        'city' => $customer_data->district . '. ' . $customer_data->province,
        'postal_code' => $customer_data->postcode,
        'phone' => $customer_data->phone,
        'country_code' => 'IDN',
      ];

      // Populate customer's Info
      $customer_details = [
        'first_name' => $customer_data->name,
        'last_name' => '',
        'email' => $customer_data->email,
        'phone' => $customer_data->phone,
        'billing_address' => $billing_address,
        'shipping_address' => $shipping_address,
      ];

      // Optional, remove this to display all available payment methods
      //$enable_payments = array('credit_card','cimb_clicks','mandiri_clickpay','echannel');
      //$enable_payments = array('credit_card','bank_transfer');

      // Fill transaction details
      $transaction = [
        /* 'enabled_payments' => $enable_payments, */
        'transaction_details' => $transaction_details,
        'customer_details' => $customer_details,
        'item_details' => $items,
      ];

      $data['snapToken'] = Veritrans_Snap::getSnapToken($transaction);
      $data['client_key'] = $veritrans->veritrans_client_key;

      //LOAD PAYMENT RETURN PAGE
      $data['bank'] = $this->configuration_m->get_bank();
      $data['email'] = $this->session->userdata('customer')['customer_email'];
      $data['order'] = $this->order_m->get_order(
        $this->session->userdata('order_id')
      );

      //get SEO
      $this->db
        ->select('website_name, meta_keywords')
        ->from('configuration')
        ->where('id_configuration', 1);
      $website_name = $this->db->get()->row();
      $this->data_header['browser_title'] =
        ucwords($website_name->website_name) . ' - Veritrans Payment';
      $this->data_header['meta_description'] =
        ucwords($website_name->website_name) . ' - Veritans Payment';
      $this->data_header['meta_keywords'] = $website_name->meta_keywords;

      $this->cart->destroy();

      $this->load->view('template/header', $this->data_header);
      $this->load->view('veritrans', $data);
      $this->load->view('template/footer', $this->data_footer);

      $this->destroy_session_data();
    }

    //4x cicilan tetap
    if ($this->input->post('payment_type') == '4kali') {
      $payment_type = '4kali';

      $this->insert_new_order($payment_type);

      //----SEND EMAIL TO CUSTOMER
      $data['bank'] = $this->configuration_m->get_bank();
      $data['title'] = 'EMPATKALI';

      //get website logo
      $this->db
        ->select('logo')
        ->from('configuration')
        ->where('id_configuration', 1);
      $data['logo'] = $this->db->get()->row()->logo;

      //get order detail and customer detail
      $data['order'] = $this->order_m->get_order(
        $this->session->userdata('order_id')
      );
      $data['order_details'] = $this->order_detail_m->get_orders_detail(
        $this->session->userdata('order_id')
      );

      //get vouchers detail
      if ($this->session->userdata('chosen_voucher_code')) {
        $data['chosen_voucher_code'] = $this->session->userdata(
          'chosen_voucher_code'
        );
        $data['chosen_voucher_type'] = $this->session->userdata(
          'chosen_voucher_type'
        );
        $data['chosen_voucher_discount'] = $this->session->userdata(
          'chosen_voucher_discount'
        );
      }

      //get shippng fee total
      $data['carrier_name'] = $this->session->userdata('carrier_name');
      $data['total_shipping_fee'] = $this->session->userdata(
        'total_shipping_fee'
      );

      $config['protocol'] = 'smtp';
      $config['smtp_host'] = 'ssl://smtp.gmail.com'; //change this
      $config['smtp_port'] = '465';
      $config['smtp_user'] = 'info@kanvakanva.com'; //change this
      $config['smtp_pass'] = 'Akunk4nva'; //change this
      $config['mailtype'] = 'html';
      $config['charset'] = 'iso-8859-1';
      $config['wordwrap'] = true;
      $config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard

      $this->load->library('email');
      $this->email->initialize($config);

      $data['emails'] = $this->configuration_m->get_emails();

      //$this->email->from($data['emails']->from_email, $data['emails']->website_name);
      $this->email->from('info@kanvakanva.com', $data['emails']->website_name);

      $this->email->to($this->session->userdata('customer')['customer_email']);
      $this->email->cc($data['emails']->from_email);
      $this->email->subject('Order Notification');
      $email = $this->load->view('email/empatkali', $data, true);
      $this->email->message($email);
      $email_sent_status = $this->email->send();

      // if(!$email_sent_status) {
      // 	echo $this->email->print_debugger();
      // }
      //----end send email

      //$md5_encode = md5($this->session->userdata('grand_total') . $this->session->userdata('order_id') . '5c889ca432e02f0e9dbd1a2f' . 'uHzwa37pOxVE2FAVqxs9PA==');

      $data = [
        'transactionNumber' => $this->session->userdata('order_id'), //invoice number
        'amount' => $this->session->userdata('grand_total'), // total jumlah pembelian
        //'user_mobileNumber' => '081278993551', // nomer handphone user EMPATKALI
        'store' => '5c889ca432e02f0e9dbd1a2f', // Storeid yg didapat dari email
        //'md5' => $md5_encode, // contoh -> $md5 = md5($_POST['amount'] . $_POST['invoive_number'] . $_POST[id_merchant] . secretKey');
        //'token' => '7802', // token yg didapat dari aplikasi EMPATKALI
      ];

      /* $ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, "http://sb-api.empatkali.co.id/merchant/api/approvedtransactions");
		
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
			curl_setopt($ch, CURLOPT_HEADER, FALSE);
			$payload = json_encode($data);

			curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
			
			curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
			
			$response = curl_exec($ch);
			curl_close($ch);
		
			echo $response; exit; */

      //get SEO
      $this->db
        ->select('website_name, meta_keywords')
        ->from('configuration')
        ->where('id_configuration', 1);
      $website_name = $this->db->get()->row();
      $this->data_header['browser_title'] =
        ucwords($website_name->website_name) . ' - 4X Payment';
      $this->data_header['meta_description'] =
        ucwords($website_name->website_name) . ' - 4X Payment';
      $this->data_header['meta_keywords'] = $website_name->meta_keywords;

      $this->load->view('template/header', $this->data_header);
      $this->load->view('empatkali', $data);
      $this->load->view('template/footer', $this->data_footer);
    }
  }

  private function insert_new_order($payment_type)
  {
    //insert new order to orders table
    $data = [
      'customer_id' => (int) $this->session->userdata('customer')[
        'customer_id'
      ],
      'total_amount' => (int) $this->session->userdata('grand_total'),
      'payment_type' => $payment_type,
      'recipient_name' => $this->session->userdata('recipient_name'),
      'address' => $this->session->userdata('address'),
      'country' => $this->session->userdata('country'),
      'postcode' => $this->session->userdata('postcode'),
      'phone' => $this->session->userdata('phone'),
      'order_date' => date('Y-m-d H:i:s'),
      'shipping_type' => $this->session->userdata('carrier_name'),
      'shipping_fee' => $this->session->userdata('total_shipping_fee'),
      'customer_note' => $this->security->xss_clean(
        $this->input->post('customer_note')
      ),
    ];

    if ($this->session->userdata('chosen_voucher_code')) {
      $data['redeemed_voucher_code'] = $this->session->userdata(
        'chosen_voucher_code'
      );
    }

    // Check redeem point to discount
    if ($this->session->userdata('id_reward')) {
      $id_reward = $this->session->userdata('id_reward');
      $reward = $this->db->query("SELECT 
          a.point, a.discount, a.min_buy
        FROM reward_customer a
        WHERE a.id_reward_customer='$id_reward' and a.discount > 0");
      
      if($reward->num_rows() > 0){
        $res_reward = $reward->row();
        if($res_reward->discount > 0){
          $data['discount_redeem'] = $res_reward->discount;
        }
      }
    }

    //get district & province
    $this->db
      ->select('province, district')
      ->from('customers')
      ->where(
        'id_customers',
        (int) $this->session->userdata('customer')['customer_id']
      );
    $district = $this->db->get()->row();
    $data['district'] = $district->district;
    $data['province'] = $district->province;

    $order_id = $this->order_m->save($data, $id = null);
    $data['order_id'] = $order_id;

    // Save history redeem point
    if ($this->session->userdata('id_reward')) {
      if($reward->num_rows() > 0){
        
        if($res_reward->discount > 0){

          $data_point = array(
            'orders_id' => $order_id,
            'id_customers' => (int) $this->session->userdata('customer')['customer_id'],
            'title_reward' => "Discount ".number_format($res_reward->discount,0,",","."),
            'point' => $res_reward->point,
            'min_buy' => $res_reward->min_buy,
            'discount' => $res_reward->discount,
            'created_at' => date("Y-m-d H:i:s")
          );
          $this->db->insert('redeem_point', $data_point);

          $point = $this->customer_m->get_point();
          $data_cust = array(
            'point' => $point - $res_reward->point
          );
          $this->db->where('id_customers', (int) $this->session->userdata('customer')['customer_id']);
		      $this->db->update('customers', $data_cust);

        }
      }
    }

    //generate random string for payment_confirm code, and update into order table
    $random_str = 'abcdefgh';
    $shuffled = str_shuffle($random_str);

    $update_data = [
      'payment_confirmation_string' => $shuffled . '-' . $data['order_id'],
    ];
    $this->db->where('id_orders', $data['order_id']);
    $this->db->update('orders', $update_data);

    //put order_id into session, to use for email or payment return page
    $this->session->set_userdata('order_id', $data['order_id']);

    //insert new order details to order details table
    $cart_items = $this->cart->contents();

    foreach ($cart_items as $cart_item) {
      $item_data = [
        'orders_id' => (int) $this->session->userdata('order_id'),
        'item_id' => (int) $cart_item['id'],
        'item_name' => $cart_item['name'],
        'item_price' => (int) $cart_item['price'],
        'quantity' => (int) $cart_item['qty'],
        'subtotal' => (int) $cart_item['price'] * (int) $cart_item['qty'],
        'size' => $cart_item['options']['size'],
        'voucher_email' => $cart_item['options']['voucher_email'],
        'voucher_message' => $cart_item['options']['voucher_message'],
      ];

      $this->order_detail_m->save($item_data, $id = null);

      //DEDUCT VOUCHERS FROM VOUCHER TABLE
      if ($this->session->userdata('chosen_voucher_code')) {
        $this->db
          ->select('qty_ready')
          ->from('vouchers')
          ->where(
            'voucher_code',
            $this->session->userdata('chosen_voucher_code')
          );
        $qty_ready = $this->db->get()->row()->qty_ready;

        if ($qty_ready != null) {
          $new_qty_ready = $qty_ready - 1;

          $data = [
            'qty_ready' => $new_qty_ready,
          ];

          $this->db->where(
            'voucher_code',
            $this->session->userdata('chosen_voucher_code')
          );
          $this->db->update('vouchers', $data);
        }
      }

      //UPDATE VOUCHER USER TABLE
      //get voucher id
      if ($this->session->userdata('chosen_voucher_code')) {
        $this->db
          ->select('id_vouchers')
          ->from('vouchers')
          ->where(
            'voucher_code',
            $this->session->userdata('chosen_voucher_code')
          );
        $voucher_id = (int) $this->db->get()->row()->id_vouchers;

        //check if this voucher already been used before in voucher user table
        $this->db
          ->select('id_voucher_users')
          ->from('voucher_users')
          ->where('voucher_id', $voucher_id)
          ->where(
            'customer_id',
            (int) $this->session->userdata('customer')['customer_id']
          );
        $count_voucher = $this->db->get()->num_rows();

        if ($count_voucher == 0) {
          //voucher not exist yet, insert new voucher

          $data = [
            'voucher_id' => $voucher_id,
            'customer_id' => (int) $this->session->userdata('customer')[
              'customer_id'
            ],
            'voucher_used' => 1,
          ];

          $this->db->insert('voucher_users', $data);
        } else {
          //voucher already exist

          //get current used voucher quantity, and add 1
          $this->db
            ->select('id_voucher_users, voucher_used')
            ->from('voucher_users')
            ->where('voucher_id', (int) $voucher_id)
            ->where(
              'customer_id',
              (int) $this->session->userdata('customer')['customer_id']
            );

          $voucher_user = $this->db->get()->row();

          $new_voucher_used = $voucher_user->voucher_used + 1;

          $data = [
            'voucher_used' => (int) $new_voucher_used,
          ];

          $this->db->where('id_voucher_users', $voucher_user->id_voucher_users);
          $this->db->update('voucher_users', $data);
        }
      }

      //DEDUCT CURRENT STOCK WITH PURCHASE QUANTITY
      //get current stock froms stocks table
      $this->db->select('stock');
      $this->db->from('stocks');
      $this->db->join(
        'product_size',
        'product_size.id_product_size = stocks.size_id'
      );
      $this->db->where('product_id', (int) $cart_item['id']);
      $this->db->where('product_size', $cart_item['options']['size']);
      $query = $this->db->get();
      $stock = $query->row();
      $current_stock = (int) $stock->stock;

      $new_item_stock = $current_stock - (int) $cart_item['qty'];

      //get size id
      $this->db->select('id_product_size');
      $this->db->from('product_size');
      $this->db->where('product_size', $cart_item['options']['size']);
      $query = $this->db->get();
      $product_size = $query->row();
      $id_product_size = (int) $product_size->id_product_size;

      $stock_data = [
        'stock' => $new_item_stock,
      ];

      //update the product item stock in database
      $this->db->where('product_id', (int) $cart_item['id']);
      $this->db->where('size_id', $id_product_size);
      $this->db->update('stocks', $stock_data);
    }

    // Check redeem point to free product
    if ($this->session->userdata('id_reward')) {
      $id_reward = $this->session->userdata('id_reward');
      $reward = $this->db->query("SELECT a.point,a.min_buy,
          a.id_products,b.title, b.image1, b.alias, a.size_id
        FROM reward_customer a
        LEFT JOIN products b on a.id_products=b.id_products
        WHERE b.product_status='1' and a.id_products is not null and a.id_reward_customer='$id_reward'");
      
      if($reward->num_rows() > 0){
        $res_reward = $reward->row();
          
          $item_data = [
            'orders_id' => (int) $this->session->userdata('order_id'),
            'item_id' => (int) $res_reward->id_products,
            'item_name' => $res_reward->title,
            'item_price' => 0,
            'quantity' => 1,
            'subtotal' => 0,
            'size' => $res_reward->size_id,
          ];
    
          $this->order_detail_m->save($item_data, $id = null);

          //DEDUCT CURRENT STOCK WITH PURCHASE QUANTITY
          //get current stock froms stocks table
          $this->db->select('stock');
          $this->db->from('stocks');
          $this->db->where('product_id', (int) $res_reward->id_products);
          $this->db->where('size_id', $res_reward->size_id);
          $query = $this->db->get();
          $stock = $query->row();
          $current_stock = (int) $stock->stock;

          $new_item_stock = $current_stock - 1;

          //get size id
          $this->db->select('id_product_size');
          $this->db->from('product_size');
          $this->db->where('id_product_size', $res_reward->size_id);
          $query = $this->db->get();
          $product_size = $query->row();
          $id_product_size = (int) $product_size->id_product_size;

          $stock_data = [
            'stock' => $new_item_stock,
          ];

          //update the product item stock in database
          $this->db->where('product_id', (int) $res_reward->id_products);
          $this->db->where('size_id', $id_product_size);
          $this->db->update('stocks', $stock_data);

          // Save history redeem point
          $data_point = array(
            'orders_id' => $order_id,
            'id_customers' => (int) $this->session->userdata('customer')['customer_id'],
            'title_reward' => "Free ".$res_reward->title,
            'point' => $res_reward->point,
            'min_buy' => $res_reward->min_buy,
            'created_at' => date("Y-m-d H:i:s")
          );
          $this->db->insert('redeem_point', $data_point);

          $point = $this->customer_m->get_point();
          $data_cust = array(
            'point' => $point - $res_reward->point
          );
          $this->db->where('id_customers', (int) $this->session->userdata('customer')['customer_id']);
		      $this->db->update('customers', $data_cust);

      }
    }
    $this->session->set_userdata('id_reward', '');

  }

  private function destroy_session_data()
  {
    //DESTROY CART AND UNSET SOME SESSION, BUT NOT CUSTOMER SESSION
    $this->cart->destroy();
    $this->session->unset_userdata('grand_total');
    $this->session->unset_userdata('recipient_name');
    $this->session->unset_userdata('address');
    $this->session->unset_userdata('id_district');
    $this->session->unset_userdata('id_province');
    $this->session->unset_userdata('country');
    $this->session->unset_userdata('postcode');
    $this->session->unset_userdata('phone');
    $this->session->unset_userdata('order_id');
    $this->session->unset_userdata('is_from_cart');

    $this->session->unset_userdata('chosen_voucher_type');
    $this->session->unset_userdata('chosen_voucher_discount');
    $this->session->unset_userdata('chosen_voucher_code');

    $this->session->unset_userdata('total_shipping_fee');
    $this->session->unset_userdata('carrier');
    $this->session->unset_userdata('carrier_name');
  }
}


https://t.me/RX1948 - 2025