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/indolok.id/application/controllers/admin/

Upload File :
current_dir [ Writeable ] document_root [ Writeable ]

 

Current File : /var/www/indolok.id/application/controllers/admin/Orders.php
<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Orders extends Admin_Controller {
		
	function __construct() {
		parent::__construct();	
		$this->load->model('order_m');  
		$this->load->model('order_detail_m'); 
		$this->load->model('configuration_m');  
		$this->load->model('customer_m');  
		$this->load->helper('shipping');

		if (!in_array('orders', $this->data['allowed_module'])) {
			$this->data['allowed'] = false;
		} else {
			$this->data['allowed'] = true;
		}

		if (!in_array('customers', $this->data['allowed_module'])) {
			$this->data['allowedCust'] = false;
		} else {
			$this->data['allowedCust'] = true;
		}						 
	}
		
	//this is to list all orders
	public function index() { 
		
		//pagination in action. 100 results per page
		$this->load->library('pagination');
		$config['base_url'] = base_url() . 'admin/orders/index';
		$config['per_page'] = 100;
		$config["uri_segment"] = 4; 

		//fetch all orders
		$config['total_rows'] = $this->order_m->record_count(); 
		$this->pagination->initialize($config);
   		$this->data['orders'] = $this->order_m->get_all_orders($config["per_page"], 
		$this->uri->segment(4));
	
		//load view
		$this->data['subview'] = 'admin/orders/index';
		$this->load->view('admin/templates/header', $this->data_header); 
		$this->load->view('admin/_layout_main', $this->data);
		$this->load->view('admin/templates/footer');	 
		
    }

	//get all successful orders for specific customer only
	public function customer($customer_id) { 
		
		//pagination in action. 20 results per page
		$this->load->library('pagination');
		$config['base_url'] = base_url() . 'admin/orders/customer';
		$config['per_page'] = 100;
		$config['uri_segment'] = 4; 
 
		//check number of orders
		$this->db->select('id_orders');
		$this->db->from('orders');
		$this->db->where('customer_id', $customer_id);
		$this->db->where('(payment_status=3 or payment_status=4 or payment_status=5)');
		$config['total_rows'] = $this->db->get()->num_rows();
		$this->pagination->initialize($config);

		//get all orders
		$this->db->select('*');
		$this->db->from('orders');
		$this->db->where('customer_id', $customer_id);
        $this->db->where('(payment_status=3 or payment_status=4 or payment_status=5)');
		$this->db->order_by('order_date', 'DESC');
		//$this->db->limit($config['per_page'], $this->uri->segment($config['uri_segment']));
   		$this->data['orders'] = $this->db->get()->result();
	
		//get customer data
		$this->db->select('*')->from('customers')->where('id_customers', $customer_id);  
		$this->data['customer'] = $this->db->get()->row();

		//load view
		$this->data['subview'] = 'admin/orders/customer_orders';
		$this->load->view('admin/templates/header', $this->data_header); 
		$this->load->view('admin/_layout_main', $this->data);
		$this->load->view('admin/templates/footer');	 
    }

    /*filter order by*/
    public function filter_order_by() {
    	$filter_key = $this->security->xss_clean($this->input->post('filter_key'));
    	//pagination in action. 100 results per page
		$this->load->library('pagination');
		$config['base_url'] = base_url() . 'admin/orders/filter_order_by';
		$config['per_page'] = 100;
		$config["uri_segment"] = 4; 

    	if($filter_key == 'order_id'){
    		//fetch all orders
    		$this->db->select('*');
			$this->db->from('orders');
			$this->db->where('id_orders',$this->security->xss_clean($this->input->post('order_id')));
			$this->db->order_by('id_orders', 'desc');
			$this->db->limit($config['per_page'], $this->uri->segment(4));
			$config['total_rows'] = $this->db->get()->num_rows(); 

			$this->pagination->initialize($config);

			$this->db->select('*');
			$this->db->from('orders');
			$this->db->where('id_orders',$this->security->xss_clean($this->input->post('order_id')));
			$this->db->order_by('id_orders', 'desc');
			$this->db->limit($config['per_page'], $this->uri->segment(4));
	   		$this->data['orders'] = $this->db->get()->result(); 
    	}
    	elseif($filter_key == 'voucher'){
    		$this->db->select('*');
			$this->db->from('orders');
			$this->db->where('redeemed_voucher_code !=',null);
			$this->db->order_by('id_orders', 'desc');
			$this->db->limit($config['per_page'], $this->uri->segment(4));
			$config['total_rows'] = $this->db->get()->num_rows(); 

			$this->pagination->initialize($config);

			$this->db->select('*');
			$this->db->from('orders');
			$this->db->where('redeemed_voucher_code !=',null);
			$this->db->order_by('id_orders', 'desc');
			$this->db->limit($config['per_page'], $this->uri->segment(4));
	   		$this->data['orders'] = $this->db->get()->result(); 
    	}
    	elseif($filter_key == 'payment_status'){
    		$this->db->select('*');
			$this->db->from('orders');
			$this->db->where('payment_status',$this->security->xss_clean($this->input->post('payment_status')));
			$this->db->order_by('id_orders', 'desc');
			$this->db->limit($config['per_page'], $this->uri->segment(4));
			$config['total_rows'] = $this->db->get()->num_rows(); 

			$this->pagination->initialize($config);

			$this->db->select('*');
			$this->db->from('orders');
			$this->db->where('payment_status',$this->security->xss_clean($this->input->post('payment_status')));
			$this->db->order_by('id_orders', 'desc');
			$this->db->limit($config['per_page'], $this->uri->segment(4));
	   		$this->data['orders'] = $this->db->get()->result();
    	}
    	elseif($filter_key == 'payment_type'){
    		$this->db->select('*');
			$this->db->from('orders');
			$this->db->where('payment_type',$this->security->xss_clean($this->input->post('payment_type')));
			$this->db->order_by('id_orders', 'desc');
			$this->db->limit($config['per_page'], $this->uri->segment(4));
			$config['total_rows'] = $this->db->get()->num_rows(); 

			$this->pagination->initialize($config);

			$this->db->select('*');
			$this->db->from('orders');
			$this->db->where('payment_type',$this->security->xss_clean($this->input->post('payment_type')));
			$this->db->order_by('id_orders', 'desc');
			$this->db->limit($config['per_page'], $this->uri->segment(4));
	   		$this->data['orders'] = $this->db->get()->result();

    	}
    	elseif($filter_key == 'flashsale'){
    		$this->db->select('
    			orders.id_orders,
    			orders.order_date,
    			orders.recipient_name,
    			orders.total_amount,
    			orders.redeemed_voucher_amount,
    			orders.minus_reward_amount,
    			orders.shipping_fee,
    			orders.free_shipping_fee,
    			orders.payment_status,
    			orders.payment_type,
    			orders.payment_confirm,
    			orders.payment_status_message
    		');
			$this->db->from('orders');
			$this->db->join('orders_detail','orders_detail.orders_id = orders.id_orders');
			$this->db->where('orders_detail.is_flashsale','yes');
			$this->db->order_by('orders.id_orders', 'desc');
			$this->db->limit($config['per_page'], $this->uri->segment(4));
			$config['total_rows'] = $this->db->get()->num_rows(); 

			$this->pagination->initialize($config);

			$this->db->select('
    			orders.id_orders,
    			orders.order_date,
    			orders.recipient_name,
    			orders.total_amount,
    			orders.redeemed_voucher_amount,
    			orders.minus_reward_amount,
    			orders.shipping_fee,
    			orders.free_shipping_fee,
    			orders.payment_status,
    			orders.payment_type,
    			orders.payment_confirm,
    			orders.payment_status_message
    		');
			$this->db->from('orders');
			$this->db->join('orders_detail','orders_detail.orders_id = orders.id_orders');
			$this->db->where('orders_detail.is_flashsale','yes');
			$this->db->order_by('orders.id_orders', 'desc');
			$this->db->limit($config['per_page'], $this->uri->segment(4));
	   		$this->data['orders'] = $this->db->get()->result(); 
    	}
    	elseif($filter_key == 'sale'){
    		$this->db->select('
    			orders.id_orders,
    			orders.order_date,
    			orders.recipient_name,
    			orders.total_amount,
    			orders.redeemed_voucher_amount,
    			orders.minus_reward_amount,
    			orders.shipping_fee,
    			orders.free_shipping_fee,
    			orders.payment_status,
    			orders.payment_type,
    			orders.payment_confirm,
    			orders.payment_status_message
    		');
			$this->db->from('orders');
			$this->db->join('orders_detail','orders_detail.orders_id = orders.id_orders');
			$this->db->where('orders_detail.is_sale','yes');
			$this->db->order_by('orders.id_orders', 'desc');
			$this->db->limit($config['per_page'], $this->uri->segment(4));
			$config['total_rows'] = $this->db->get()->num_rows(); 

			$this->pagination->initialize($config);

			$this->db->select('
    			orders.id_orders,
    			orders.order_date,
    			orders.recipient_name,
    			orders.total_amount,
    			orders.redeemed_voucher_amount,
    			orders.minus_reward_amount,
    			orders.shipping_fee,
    			orders.free_shipping_fee,
    			orders.payment_status,
    			orders.payment_type,
    			orders.payment_confirm,
    			orders.payment_status_message
    		');
			$this->db->from('orders');
			$this->db->join('orders_detail','orders_detail.orders_id = orders.id_orders');
			$this->db->where('orders_detail.is_sale','yes');
			$this->db->order_by('orders.id_orders', 'desc');
			$this->db->limit($config['per_page'], $this->uri->segment(4));
	   		$this->data['orders'] = $this->db->get()->result(); 
    	}elseif($filter_key == "date"){
				$this->db->select('*');
				$this->db->from('orders');
				$this->db->where('order_date >=', date('Y-m-d 00:00:00', strtotime($this->security->xss_clean($this->input->post('start_date')))));
				$this->db->where('order_date <=', date('Y-m-d 23:59:59', strtotime($this->security->xss_clean($this->input->post('end_date')))));
				$this->db->order_by('id_orders', 'desc');
				$this->db->limit($config['per_page'], $this->uri->segment(4));
				$config['total_rows'] = $this->db->get()->num_rows();

				$this->pagination->initialize($config);

				$this->db->select('*');
				$this->db->from('orders');
				$this->db->where('order_date >=', date('Y-m-d 00:00:00', strtotime($this->security->xss_clean($this->input->post('start_date')))));
				$this->db->where('order_date <=', date('Y-m-d 23:59:59', strtotime($this->security->xss_clean($this->input->post('end_date')))));
				$this->db->order_by('id_orders', 'desc');
				$this->db->limit($config['per_page'], $this->uri->segment(4));
				$this->data['orders'] = $this->db->get()->result();
		}elseif($filter_key == "totalorder"){
				$this->db->select('*');
				$this->db->from('orders');

				if($this->security->xss_clean($this->input->post('start_amt')) <> ""){
					$this->db->where('((`total_amount`+IFNULL(ABS(`sisa_kembali`),0))-IFNULL(`redeemed_voucher_value`,0))+(IFNULL(`shipping_fee`,0)-IFNULL(`free_shipping_fee`,0)) >=', $this->security->xss_clean($this->input->post('start_amt')));
				}
				if($this->security->xss_clean($this->input->post('end_amt')) <> ""){
						$this->db->where('((`total_amount`+IFNULL(ABS(`sisa_kembali`),0))-IFNULL(`redeemed_voucher_value`,0))+(IFNULL(`shipping_fee`,0)-IFNULL(`free_shipping_fee`,0)) <=', $this->security->xss_clean($this->input->post('end_amt')));
				}

				$this->db->order_by('id_orders', 'desc');
				$this->db->limit($config['per_page'], $this->uri->segment(4));
				$config['total_rows'] = $this->db->get()->num_rows();

				$this->pagination->initialize($config);

				$this->db->select('*');
				$this->db->from('orders');
				if($this->security->xss_clean($this->input->post('start_amt')) <> ""){
					$this->db->where('((`total_amount`+IFNULL(ABS(`sisa_kembali`),0))-IFNULL(`redeemed_voucher_value`,0))+(IFNULL(`shipping_fee`,0)-IFNULL(`free_shipping_fee`,0)) >=', $this->security->xss_clean($this->input->post('start_amt')));
				}
				if($this->security->xss_clean($this->input->post('end_amt')) <> ""){
						$this->db->where('((`total_amount`+IFNULL(ABS(`sisa_kembali`),0))-IFNULL(`redeemed_voucher_value`,0))+(IFNULL(`shipping_fee`,0)-IFNULL(`free_shipping_fee`,0)) <=', $this->security->xss_clean($this->input->post('end_amt')));
				}

				$this->db->order_by('id_orders', 'desc');
				$this->db->limit($config['per_page'], $this->uri->segment(4));
				$this->data['orders'] = $this->db->get()->result();
		}
    	else{
    		redirect('admin/orders');
    	}

		
	
		//load view
		$this->data['subview'] = 'admin/orders/index';
		$this->load->view('admin/templates/header', $this->data_header); 
		$this->load->view('admin/_layout_main', $this->data);
		$this->load->view('admin/templates/footer');	
    }

    //add order form marketplace
    public function add_order(){


		if($this->data['role'] == 'apoteker') {
			//get warehouse id for this apoteker
	        $this->db->select('warehouse_id')->from('users')->where('id', $this->session->userdata('admin')['id']);
	        $apoteker_warehouse_id = $this->db->get()->row()->warehouse_id;  
		}

        
   
        if(($this->data['role'] == 'apoteker') || $this->data['role'] == 'super admin' || $this->data['role'] == 'admin' || $this->data['role'] == 'kepala apoteker') {
       	/*---allowed access----*/
       } else {
       	show_404();
       }

    	/*$this->data['customer_marketplace'] = $this->db->select('*')->from('customers')->where('(type="marketplace" or type="regular")')->order_by('id_customers','ASC')->get()->result();*/
        $this->data['customer_marketplace'] = $this->db->select('*')->from('customers')->order_by('id_customers','ASC')->get()->result();
    	$this->data['marketplace']			= $this->db->select('*')->from('marketplace')->order_by('id','ASC')->get()->result();
        $this->data['warehouses']           = $this->db->select('*')->from('warehouse')->order_by('id','ASC')->get()->result();

    	$config = $this->order_m->add_order_rules;

        $this->load->library('form_validation');
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>'); //above is to add class to form validation error, to be styled

        $this->form_validation->set_rules($config); 
        $this->form_validation->set_error_delimiters('<div class="error">', '</div>');

        if($this->form_validation->run($this) == TRUE) {
        	/*get customer info detail*/
        	$customer_id 	= $this->security->xss_clean($this->input->post('customer_id'));
        	$customer 		= $this->db->select('*')->from('customers')->where('id_customers',$customer_id)->get()->row();
        	/*get customer info detail*/

        	$product 			= $this->security->xss_clean($this->input->post('id_product'));
        	$count_product		= count($product);
        	$marketplace_price 	= $this->security->xss_clean($this->input->post('marketplace_price'));
        	$quantitas_beli 	= $this->security->xss_clean($this->input->post('quantitas_beli'));

            $warehouse_id       = $this->security->xss_clean($this->input->post('warehouse_id'));

        	/*cek product dan qty kosong*/
        	if($product != 0){
        		for($a=0;$a<$count_product;$a++){
        			if($quantitas_beli[$a] == ""){
        				$this->session->set_flashdata('success', '<br><p style="background:red; color:white; padding:5px; font-weight:bold;">Not Set Product Quantity</p>');
						redirect('admin/orders/add_order');
        			}
        		}
        	}
        	else{
        		$this->session->set_flashdata('success', '<br><p style="background:red; color:white; padding:5px; font-weight:bold;">Not Product Found</p>');
				redirect('admin/orders/add_order');
        	}
        	/*cek product dan qty kosong*/

        	/*cek stock warehouse*/
        	for($cek=0;$cek<$count_product;$cek++){
        		$product_name	= $this->db->select('title')->from('products')->where('id_products',$product[$cek])->get()->row()->title;
        		$stock 			= $this->db->select('stock')->from('stock')->where('id_product',$product[$cek])->where('warehouse_id',$warehouse_id)->get()->row()->stock;
        		if($quantitas_beli[$cek] > $stock){
        			$this->session->set_flashdata('success', '<br><p style="background:red; color:white; padding:5px; font-weight:bold;">Not Enough Stock for "'.$product_name.'"</p>');
					redirect('admin/orders/add_order');
        		}
        	}
        	/*cek stock warehouse*/

        	/*hitung total amount*/
        	$this->load->helper('shipping');
        	$this->load->helper('rajaongkir');

        	$total_amount	= 0;
        	$subtotal		= 0;

        	for($sf=0;$sf<$count_product;$sf++){
        		/*$shipping_fee_info =  calculate_shipping_fee(3, 1, $product[$sf], $quantitas_beli[$sf], $customer->shipping_id_subdistrict);*/
        		/*$subtotal 		= ($marketplace_price[$sf] * $quantitas_beli[$sf]) + $shipping_fee_info['total_shipping_fee'];*/
                $subtotal       = ($marketplace_price[$sf] * $quantitas_beli[$sf]);
        		$total_amount	= $total_amount + $subtotal;
        	}

            //get customer district
           

        	/*hitung total amount*/
        	
        	$order = array(
        		'customer_id' 				=> $customer_id, 
        		'payment_status' 			=> 3, 
        		'payment_status_message'	=> NULL, 
        		'payment_confirm' 			=> 0, 
        		'payment_confirm_details' 	=> NULL, 
        		'total_amount' 				=> $total_amount, 
        		'payment_type' 				=> 'TOP', 
        		'payment_method' 			=> NULL, 
        		'payment_date' 				=> NULL, 
        		'cancel_date' 				=> NULL, 
        		'sent_date' 				=> NULL, 
        		'airway_bill' 				=> NULL, 
        		'recipient_name' 			=> $customer->recipient_name, 
        		'address' 					=> $customer->shipping_address, 
        		'district' 					=> $customer->shipping_district, 
        		'subdistrict'				=> $customer->shipping_subdistrict, 
        		'province'					=> $customer->shipping_province, 
        		'postcode'					=> $customer->shipping_postcode, 
        		'phone'						=> $customer->shipping_phone, 
                'email'                     => $customer->email, 
        		'country'					=> $customer->shipping_country, 
        		'redeemed_voucher_code'		=> NULL, 
        		'redeemed_voucher_amount'	=> NULL, 
        		'shipping_type'				=> "", 
        		'shipping_fee'				=> $this->input->post('shipping_fee'), 
        		'ppn'						=> 0, 
        		'plus_reward'				=> 0, 
        		'minus_reward'				=> 0, 
        		'minus_reward_amount'		=> NULL, 
        		'no_resi'					=> NULL, 
        		'customer_note'				=> "", 
        	);

        	$order_id = (int) $this->order_m->add_order($order);

        	/*insert into order detail*/
        	for($c=0;$c<$count_product;$c++){
        		$product_item = $this->db->select('title,is_backorder')->from('products')->where('id_products',$product[$c])->get()->row();
        		/*$shipping_fee_info =  calculate_shipping_fee(3, 1, $product[$c], $quantitas_beli[$c], $customer->shipping_id_subdistrict);*/

        		$order_detail = array(
        			'orders_id'			=> $order_id, 
        			'item_id' 			=> $product[$c], 
        			'item_name'			=> $product_item->title,
        			'item_price'		=> $marketplace_price[$c], 
        			'quantity'			=> $quantitas_beli[$c], 
        			/*'subtotal'			=> ($marketplace_price[$c] * $quantitas_beli[$c]) + $shipping_fee_info['total_shipping_fee'],*/
                    'subtotal'          => ($marketplace_price[$c] * $quantitas_beli[$c]),
        			'sku'				=> "",
        			'attributes'		=> "",
        			'warehouse_id'		=> $warehouse_id,
        			'chosen_shipping_id'=> 3,
        			/* 'shipping_fee'		=> $shipping_fee_info['total_shipping_fee'], */
                    'shipping_fee'      => $this->input->post('shipping_fee'),
        			'is_backorder'		=> 'no',
        			'status'			=> 0,
        			'no_resi'			=> "",
        		);

        		$this->db->insert('orders_detail',$order_detail);
        	}
        	/*insert into order detail*/

        	/*kurangi stock pada warehouse*/
        	for($d=0;$d<$count_product;$d++){

        		$curent_stock 	= $this->db->select('stock')->from('stock')->where('id_product',$product[$d])->where('warehouse_id',$warehouse_id)->get()->row()->stock;
        		$final_stock = array(
        			'stock' => $curent_stock - $quantitas_beli[$d], 
        		);
        		$this->db->where('id_product',$product[$d]);
        		$this->db->where('warehouse_id',$warehouse_id);
        		$this->db->update('stock',$final_stock);
        	}
        	/*kurangi stock pada warehouse*/

        	$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Add Order Successful</p>');
			redirect('admin/orders');

        }

     	//load view
		$this->data['subview'] = 'admin/orders/add_order';
		$this->load->view('admin/templates/header', $this->data_header); 
		$this->load->view('admin/_layout_main', $this->data);
		$this->load->view('admin/templates/footer');
    }
    function ajax_get_product_marketplace(){
    	$id 			= (int) $this->input->post('id_marketplace');
    	$data['product']= $this->db->select('*')->from('products')->join('marketplace_product_price', 'marketplace_product_price.product_id = products.id_products')->where('marketplace_product_price.marketplace_id',$id)->get()->result();
    	$this->load->view('admin/orders/ajax_get_product_marketplace', $data);
    }

	function ajax_get_address_customer_marketplace(){
    	$id 			= (int) $this->input->post('id_customers');
    	$data['customers'] = $this->db->select('*')->from('customers')->where('customers.id_customers',$id)->get()->result();
    	$this->load->view('admin/orders/ajax_get_address_customer_marketplace', $data);
    }
    function ajax_get_product_detail(){
    	$id 			= (int) $this->input->post('id_product');
    	$id_marketplace	= (int) $this->input->post('id_marketplace');
    	$data['product']= $this->db->select('*')->from('products')->join('marketplace_product_price', 'marketplace_product_price.product_id = products.id_products')->where('products.id_products',$id)->where('marketplace_product_price.marketplace_id',$id_marketplace)->get()->row();
    	echo json_encode($data);
    }
	
	//to VIEW and EDIT order in admin
	public function view($id) {

		if($id == null){
			redirect('admin/orders');
		}

		//get orders
		$this->db->select('*')->from('orders')->where('id_orders', $id);
		$current_order = $this->db->get()->row();

		if(count($current_order) < 1) {show_404();}

		if (isset($_POST['payment_status'])) {

			if ($this->input->post('payment_status') == 2) {

				//Status CANCEL
				$data = array( 
					'payment_status' => 2,
					'cancel_date' => date('Y-m-d')
				);
				$this->db->where('id_orders', $id);
				$this->db->update('orders', $data);

				//return the quantity back to stock
				//get order details
				$order_details =  $this->order_detail_m->get_orders_detail($id);

                //get customer_id
                $this->db->select('customer_id')->from('orders')->where('id_orders', $id);
                $customer_id = (int) $this->db->get()->row()->customer_id;


				foreach ($order_details as $item) {

					//get current stock
					$this->db->select('stock')->from('stock')->where('id_product', $item->item_id)->where('warehouse_id', $item->warehouse_id);
					$current_stock = $this->db->get()->row()->stock;
					$qty = 0;
					if($item->is_backorder == 'yes'){
						$qty = $qty + 0;
					}
					else{
						$qty = $qty + $item->quantity;
					}
					
					$data = array(
						'stock' => $current_stock + $qty,
					);
					$this->db->where('id_product', $item->item_id);
					$this->db->where('warehouse_id', $item->warehouse_id);
					$this->db->update('stock', $data);

                    if($item->is_flashsale != 0){
                        //return flashsale counter & customer purchase
                        /*get purchase qty from flashsale_customer*/
                        $flashsale_purchase = $this->db->select('purchase_qty')->from('flashsale_customer')->where('customer_id',$customer_id)->where('flashsale_id',$item->is_flashsale)->where('flashsale_product_id',$item->item_id)->get()->row()->purchase_qty;
                        /*return counter and terjual from flashsale product*/
                        $current_flashsale_product = $this->db->select('counter,terjual')->from('flashsale_products')->where('flashsale_id',$item->is_flashsale)->where('product_id',$item->item_id)->get()->row();
                        $return_counter_terjual = array(
                            'counter' => $current_flashsale_product->counter + $flashsale_purchase, 
                            'terjual' => $current_flashsale_product->terjual - $flashsale_purchase,
                        );
                        $this->db->where('flashsale_id',$item->is_flashsale);
                        $this->db->where('product_id',$item->item_id);
                        $this->db->update('flashsale_products',$return_counter_terjual);
                        /*delete flashsale customer record*/
                        $this->db->where('customer_id',$customer_id);
                        $this->db->where('flashsale_id',$item->is_flashsale);
                        $this->db->delete('flashsale_customer');
                    }
				}

				//return customer point reward back to customers table because he cancel
				//get customer current point
				$this->db->select('current_pointreward')->from('customers')->where('id_customers', $customer_id);
				$current_point = (int) $this->db->get()->row()->current_pointreward;

				//get minus point from order
				$this->db->select('minus_reward')->from('orders')->where('id_orders', $id);
				$rewards = $this->db->get()->row();
				$minus_point = (int) $rewards->minus_reward;

				$updated_point = $current_point + $minus_point;

				//update point reward
				$data = array(
					'current_pointreward' => $updated_point
				);
				$this->db->where('id_customers', $customer_id);
				$this->db->update('customers', $data);

				//----SEND EMAIL TO CUSTOMER 
				//get order detail and customer detail
				$data['order'] = $this->order_m->get_order($id);  
				$data['customer'] = $this->customer_m->get_customer($data['order']->customer_id);
				$data['minus_point'] = $minus_point;

				//get website data
				$this->db->select('logo, from_email, website_name, email_smtp_host, email_smtp_port, email_smtp_password, email_smtp')->from('configuration')->where('id_configuration', 1);
				$website_data = $this->db->get()->row();
				$data['logo'] = $website_data->logo;
				$data['website_name'] = $website_data->website_name;

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

				$this->load->library('email');
				//get email setting 
				$config['protocol'] = 'smtp';
				$config['smtp_crypto'] = 'tls'; 
				$config['smtp_host'] = $website_data->email_smtp_host; 
				$config['smtp_port'] = $website_data->email_smtp_port;
				$config['smtp_user'] = $website_data->email_smtp; 
				$config['smtp_pass'] = $website_data->email_smtp_password;
				$config['mailtype'] = 'html';
				$config['charset'] = 'utf-8';
				$config['wordwrap'] = TRUE;
				$config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard 
				$this->email->initialize($config);
				$this->email->from($data['emails']->from_email, $data['emails']->website_name);
				$this->email->to($data['customer']->email); 
				$this->email->subject('Order Cancel'); 
				$email = $this->load->view('email/indonesian/order_cancel', $data, TRUE);
				$this->email->message($email);	    
				$this->email->send();  
				//----end send email 

				$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Payment status updated Successful</p>');
				redirect('admin/orders/view/' . $id);
			}

			if ($this->input->post('payment_status') == 1) {

				//Status NOT PAID
				$data = array( 
					'payment_status' => 1,
				);
				$this->db->where('id_orders', $id);
				$this->db->update('orders', $data);
				 
				$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Payment status updated Successful</p>');
				redirect('admin/orders/view/' . $id);
			}

			if ($this->input->post('payment_status') == 3) {

				//Status PAID
				$data = array( 
					'payment_status' => 3,
					'payment_date' => date('Y-m-d')
				);
				$this->db->where('id_orders', $id);
				$this->db->update('orders', $data);

				//update current point rewards
				//get customer_id
				$this->db->select('customer_id')->from('orders')->where('id_orders', $id);
				$customer_id = (int) $this->db->get()->row()->customer_id;

				//get customer current point
				$this->db->select('current_pointreward')->from('customers')->where('id_customers', $customer_id);
				$current_point = (int) $this->db->get()->row()->current_pointreward;

				//get add and minus point from order
				$this->db->select('plus_reward')->from('orders')->where('id_orders', $id);
				$rewards = $this->db->get()->row();
				$plus_point = (int) $rewards->plus_reward;

				$updated_point = $current_point + $plus_point;

				//update point reward
				$data = array(
					'current_pointreward' => $updated_point
				);
				$this->db->where('id_customers', $customer_id);
				$this->db->update('customers', $data);


				//SEND EMAIL TO CUSTOMER
				$data['title'] = 'Payment Confirmation'; 
				//get order detail and customer detail
				$data['order'] = $this->order_m->get_order($id);
				$data['customer'] = $this->customer_m->get_customer($data['order']->customer_id);

				//get website data
				$this->db->select('logo, from_email, website_name, email_smtp_host, email_smtp_port, email_smtp_password, email_smtp')->from('configuration')->where('id_configuration', 1);
				$website_data = $this->db->get()->row();
				$data['logo'] = $website_data->logo;
				$data['website_name'] = $website_data->website_name;
				$data['emails'] = $this->configuration_m->get_emails(); 
				$data['plus_point'] = $plus_point;

				$this->load->library('email');
				//get email setting 
				$config['protocol'] = 'smtp';
				$config['smtp_crypto'] = 'tls'; 
				$config['smtp_host'] = $website_data->email_smtp_host; 
				$config['smtp_port'] = $website_data->email_smtp_port;
				$config['smtp_user'] = $website_data->email_smtp; 
				$config['smtp_pass'] = $website_data->email_smtp_password;
				$config['mailtype'] = 'html';
				$config['charset'] = 'utf-8';
				$config['wordwrap'] = TRUE;
				$config['newline'] = "\r\n"; //use double quotes to comply with RFC 822 standard 
				$this->email->initialize($config);
				$this->email->from($data['emails']->from_email, $data['emails']->website_name);
				$this->email->to($data['customer']->email); 
				$this->email->subject('Payment Confirmation'); 
				$email = $this->load->view('email/indonesian/payment_confirmation', $data, TRUE);   
				$this->email->message($email);	
				$this->email->send();    
				//----end send email   


 
				$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Payment status updated Successful</p>');
				redirect('admin/orders/view/' . $id);
			}

			if ($this->input->post('payment_status') == 4) {

				//Status PROCESS
				$data = array( 
					'payment_status' => 4,
				);
				$this->db->where('id_orders', $id);
				$this->db->update('orders', $data);

				$email_data['order'] = $current_order;

				//get order detail
				$order_details = $this->db->select('*')->from('orders_detail')->where('orders_id', $id)->get()->result();

				foreach ($order_details as $detail) {

					//get warehouse email
					$email_data['warehouse'] = $this->db->select('email, name')->from('warehouse')->where('id', $detail->warehouse_id)->get()->row();

					$email_data['detail'] = $detail;

					//get product detail
					$email_data['product'] = $this->db->select('product_code, sku, title')->from('products')->where('id_products', $detail->item_id)->get()->row();

					//SEND EMAIL TO WAREHOUSE
					//get website data
					$this->db->select('logo, from_email, website_name, email_smtp_host, email_smtp_port, email_smtp_password, email_smtp')->from('configuration')->where('id_configuration', 1);
					$website_data 			= $this->db->get()->row();
					$email_data['logo'] 			= $website_data->logo;
					$email_data['website_name'] 	= $website_data->website_name;
					$email_data['emails'] 		= $this->configuration_m->get_emails(); 

					$this->load->library('email');
					//get email setting 
					$config['protocol'] 	= 'smtp';
					$config['smtp_crypto'] = 'tls'; 
					$config['smtp_host'] 	= $website_data->email_smtp_host; 
					$config['smtp_port'] 	= $website_data->email_smtp_port;
					$config['smtp_user'] 	= $website_data->email_smtp; 
					$config['smtp_pass'] 	= $website_data->email_smtp_password;
					$config['mailtype'] 	= 'html';
					$config['charset'] 		= 'utf-8';
					$config['wordwrap'] 	= TRUE;
					$config['newline'] 		= "\r\n"; //use double quotes to comply with RFC 822 standard 
					$this->email->initialize($config);
					$this->email->from($email_data['emails']->from_email, $email_data['emails']->website_name);
					$this->email->to('herman.canvasweb@gmail.com'); 
					$this->email->subject("Persiapan produk Order No: {$id}"); 
					$email = $this->load->view('email/indonesian/order_warehouse_prepare.php', $email_data, TRUE);
					$this->email->message($email);	
					$this->email->send();    
					//----end send email
				}
 
				$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Order status updated Successful</p>');
				redirect('admin/orders/view/' . $id);
			}

			if ($this->input->post('payment_status') == 5) {

				//Status DELIVERED
				$data = array( 
					'payment_status' => 5,
				);
				$this->db->where('id_orders', $id);
				$this->db->update('orders', $data);
 
				$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Payment status updated Successful</p>');
				redirect('admin/orders/view/' . $id);
			}
		}

		//get order detail and customer detail
		$this->data['order'] = $this->order_m->get_order($id);  
		$this->data['customer'] = $this->customer_m->get_customer($this->data['order']->customer_id);
		$this->data['order_details'] = $this->order_detail_m->get_orders_detail($id); 
		
		$this->data['subview'] = 'admin/orders/view';
		$this->load->view('admin/templates/header', $this->data_header); 
		$this->load->view('admin/_layout_main', $this->data);
		$this->load->view('admin/templates/footer');	
	}

	/*update order detail*/
	public function update_status() {

		$id 		= $this->security->xss_clean($this->input->post('id_product_detail')); //order detail
		$id_order 	= $this->security->xss_clean($this->input->post('id_order')); //order
		$status		= $this->input->post('status');
		
		if($status == 0) {
			$order_details = array(
				'status' => $status, 
			);
		}

		//generate airaway bill
		elseif($status == 1) {
			
			if($this->security->xss_clean($this->input->post('backed_order')) == 'yes') {
				$cek_stock = 
				$this->db
				->select('stock')
				->from('stock')
				->where('id_product',$this->security->xss_clean($this->input->post('item_order')))
				->where('warehouse_id',$this->security->xss_clean($this->input->post('warehouse_order')))
				->get()
				->row()
				->stock;
				
				if($this->security->xss_clean($this->input->post('qty_order')) <= $cek_stock){
					
					$order_details = array(
						'status' => $status, 
					);

				}
				else{
					$this->session->set_flashdata('success'.$this->security->xss_clean($this->input->post('id')), '<p style="color:red; font-weight:bold;">Stock Tidak Cukup. Silahkan tambahkan stok</p>');
					redirect('admin/orders/view/' . $id_order);	
				}
			}
		

			$order_data = $this->db->select('*')->from('orders')->where('id_orders', $id_order)->get()->row();
			$order_detail_data = $this->db->select('*')->from('orders_detail')->where('id_orders_detail', $id)->get()->row();

			//get shipper warehouse postcode
			$shipper_postcode = $this->db->select('postcode')->from('warehouse')->where('id', $order_detail_data->warehouse_id)->get()->row()->postcode;

			//get service code
			switch ($order_data->kurir) {
				case 'REGULAR':
					$service_code = 'REG';
					break;
				case 'EXPRESS':
					$service_code = 'EXP';
					break;	
				case 'REX-1':
					$service_code = 'REX-1';
					break;	
				default:
				$service_code = 'REG';
					break;
			}

			//calculate item weight
			//get product weight
			$this->db->select('dimension_weight, dimension_length, dimension_width, dimension_height')->from('products')->where('id_products', $order_detail_data->item_id);
			$product_dimension = $this->db->get()->row();

			$product_weight = $product_dimension->dimension_weight; //gram
			$product_length = $product_dimension->dimension_length; //cm
			$product_width = $product_dimension->dimension_width; //cm
			$product_height = $product_dimension->dimension_height; //cm

			//check if volume is bigger than weight
			$volume_weight = $product_length * $product_width * $product_height / 4000; //kg

			if(($volume_weight * 1000) >= $product_weight) {
				$weight = $volume_weight * 1000; //gram
			} else {
				$weight = $product_weight; //gram
			}
			$total_weight = ceil($weight * $order_detail_data->quantity); 

			//generate airwaybill
			$airwaybill_data = array(
				'shipper_postcode' => $shipper_postcode,
				'receiver_postcode' => $order_data->postcode,
				'shipper_name' => 'Indolok',
				'shipper_address' => 'Jl. Salemba Raya No.32 Jakarta',
				'shipper_hp' => '0 804 133 8383',
				'shipper_email' => 'ecommerce@indolok.id',
				'receiver_name' => $order_data->recipient_name,
				'receiver_address' => $order_data->address,
				'receiver_postcode' => $order_data->postcode,
				'receiver_hp'  => $order_data->phone,
				'receiver_email'  => $order_data->email,
				'service_code' => $service_code,
				'item_weight' => $total_weight,
				'item_desc'  => $order_detail_data->quantity . ' pcs',
				'item_name'  => $order_detail_data->item_name,
				'item_price'  => $order_detail_data->item_price,
				'item_category'  => 'security product',
				'id_order' => $id_order
			);

			$this->load->helper('shipping');

			$airwaybill = rex_generate_awb($airwaybill_data);

			$order_details = array(
				'status' => $status, 
				'no_resi' => $airwaybill['tracking_ref_no'],
				'rex_tracking_ref_no' => $airwaybill['booking_id'],
			);
		}

		elseif($status == 2) {
			$order_details = array(
				'status' => $status,
			);

			//SEND EMAIL TO CUSTOMER
			$data['title'] 			= 'Product Picked Up'; 
			//get order detail and customer detail
			$data['product_detail'] = $this->db->select('*')->from('orders_detail')->where('id_orders_detail',$id)->get()->row();
			$data['warehouse']		= $this->db->select('name')->from('warehouse')->where('id',$data['product_detail']->warehouse_id)->get()->row()->name;
			$data['shipping_method']= $this->db->select('name')->from('shipment_method')->where('id',$data['product_detail']->chosen_shipping_id)->get()->row()->name;
			$data['no_resi']		= $data['product_detail']->no_resi;
			$customer_id 			= $this->db->select('customer_id')->from('orders')->where('id_orders',$id_order)->get()->row()->customer_id;
			$data['customer']		= $this->customer_m->get_customer($customer_id);

			//get website data
			$this->db->select('logo, from_email, website_name, email_smtp_host, email_smtp_port, email_smtp_password, email_smtp')->from('configuration')->where('id_configuration', 1);
			$website_data 			= $this->db->get()->row();
			$data['logo'] 			= $website_data->logo;
			$data['website_name'] 	= $website_data->website_name;
			$data['emails'] 		= $this->configuration_m->get_emails(); 

			$this->load->library('email');
			//get email setting 
			$config['protocol'] 	= 'smtp';
			$config['smtp_crypto'] = 'tls'; 
			$config['smtp_host'] 	= $website_data->email_smtp_host; 
			$config['smtp_port'] 	= $website_data->email_smtp_port;
			$config['smtp_user'] 	= $website_data->email_smtp; 
			$config['smtp_pass'] 	= $website_data->email_smtp_password;
			$config['mailtype'] 	= 'html';
			$config['charset'] 		= 'utf-8';
			$config['wordwrap'] 	= TRUE;
			$config['newline'] 		= "\r\n"; //use double quotes to comply with RFC 822 standard 
			$this->email->initialize($config);
			$this->email->from($data['emails']->from_email, $data['emails']->website_name);
			$this->email->to($data['customer']->email); 
			$this->email->subject('Payment Confirmation'); 
			$email = $this->load->view('email/indonesian/pickedup_confirmation', $data, TRUE);
			$this->email->message($email);	
			$this->email->send();    
			//----end send email
		}
		
		$this->db->where('id_orders_detail',$id);
		$this->db->update('orders_detail',$order_details);

		/*cek detail status to update order status*/
		$cek_status = 0;
		$all_order_detail_status = $this->db->select('status')->from('orders_detail')->where('orders_id',$id_order)->get()->result();
		foreach ($all_order_detail_status as $item) {
			if($item->status != 2){
				$cek_status = $cek_status + 1;
			}
			else{
				$cek_status = $cek_status;
			}
		}
		if($cek_status == 0){
			$data = array( 
				'payment_status' => 5,
			);
			$this->db->where('id_orders', $id_order);
			$this->db->update('orders', $data);
		}
		/*cek detail status to update order status*/
		
		$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Update Product Status Success</p>');
		redirect('admin/orders/view/' . $id_order);
	}


	public function generate_invoice_pdf($id) {

		//add PDF attachment DOMPDF 
			  
		$data['title'] = 'Invoice'; 

		
		//get order
		$this->db->select('*')->from('orders')->where('id_orders', $id);
		$data['order'] = $this->db->get()->row();
		$data['customer'] = $this->customer_m->get_customer($data['order']->customer_id);
		$data['emails'] = $this->configuration_m->get_emails();  

		$data['bank'] = $this->configuration_m->get_bank();   

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

		//get order detail and customer detail
		$this->db->select('*')->from('orders_detail')->where('orders_id', $id);
		$data['order_details'] = $this->db->get()->result();

		$this->load->library('dompdf_gen');
		$html= $this->load->view('invoice_pdf', $data ,true);
		$this->dompdf->load_html($html);
		$this->dompdf->render(); 
		$this->dompdf->stream('invoice.pdf', array("Attachment" => 0));
		$output = $this->dompdf->output(); 
		$file_to_save = 'uploads/pdf/invoice.pdf'; 
		file_put_contents($file_to_save, $output);  
	}

	public function generate_multiple_invoices() {

		if(!$this->input->post('generate_invoices')) { redirect('admin/orders');}

		$start_date_array = explode('-', $this->security->xss_clean($this->input->post('start_date')));
		$end_date_array = explode('-', $this->security->xss_clean($this->input->post('end_date')));
		//change to YYYY-MM-DD format for database insertion, and use php DatePeriod class
		$begin = new DateTime($start_date_array[2] . '-' . $start_date_array[1] . '-' . $start_date_array[0]);
		$end = new DateTime($end_date_array[2] . '-' . $end_date_array[1] . '-' . $end_date_array[0]);
		$end = $end->modify('+1 day'); 
		$interval = new DateInterval('P1D');
		$daterange = new DatePeriod($begin, $interval ,$end);

		$this->load->library('dompdf_gen');

		//create a new directory inside pdf folder
		if (!is_dir('./uploads/' . 'pdf/' . 'invoice-' . $this->input->post('start_date') . '-' .  $this->input->post('end_date'))) { //check if directory already exist
			mkdir('./uploads/' . 'pdf/' . 'invoice-' . $this->input->post('start_date') . '-' .  $this->input->post('end_date'));
			chmod('./uploads/' . 'pdf/' . 'invoice-' . $this->input->post('start_date') . '-' .  $this->input->post('end_date'), 0777);  //change permission to writable 777, so can be deleted
		}

		$total_orders_found = 0;
		
		foreach($daterange as $date){

			//get orders from each particular date
			//DATE(order_date) is a mysql function, to get onlye date from timestamp, exclude h-m-s
			$this->db->select('*')->from('orders');
			$this->db->where('DATE(order_date)', $date->format('Y-m-d'));
			if($this->input->post('include_unpaid_orders') == 'no') {
				$this->db->group_start();
				$this->db->where('payment_status', 1);
				$this->db->or_where('payment_status', 3);
				$this->db->group_end();
			}
			$orders = $this->db->get()->result();

			if(count($orders) > 0) {
				
				//orders are available..then proceed 
				$total_orders_found = $total_orders_found + count($orders);

				foreach($orders as $data['order']) {

					//generate pdf for each order in this particular date
					//add PDF attachment DOMPDF 
					$data['title'] = 'Invoice ' . $data['order']->id_orders;
					$data['customer'] = $this->customer_m->get_customer($data['order']->customer_id);
					$data['emails'] = $this->configuration_m->get_emails();  
					$data['bank'] = $this->configuration_m->get_bank();   
					$this->db->select('logo')->from('configuration')->where('id_configuration', 1);
					$data['logo'] = $this->db->get()->row()->logo;
					//get order detail and customer detail
					$this->db->select('*')->from('orders_detail')->where('orders_id', $data['order']->id_orders);
					$data['order_details'] = $this->db->get()->result();
			
					$html= $this->load->view('invoice_pdf', $data ,true);
					//create a new dompdf instance (this is the crucial step)
					$this->dompdf = new DOMPDF();
					$this->dompdf->load_html($html);
					$this->dompdf->render();  //render html as pdf
					/* $this->dompdf->stream('invoice.pdf', array("Attachment" => 0)); //display to browser*/
					$output = $this->dompdf->output();  
					
					//add pdf file into directory
					$file_to_save = 'uploads/pdf/invoice-' . $this->input->post('start_date') . '-' .  $this->input->post('end_date') . '/invoice-orderid-' . $data['order']->id_orders . '.pdf'; 
					file_put_contents($file_to_save, $output);  //$output function to add files to folder

				}

			}
	
		}

		if($total_orders_found == 0) {
			//dont download files, but go back to orders page and give error notice..
			$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">No orders are found</p>');
				redirect('admin/orders');
		}

		//zip the generated pds and downlod the zip file
		$this->load->library('zip');
		$path = './uploads/' . 'pdf/' . 'invoice-' . $this->input->post('start_date') . '-' .  $this->input->post('end_date');
		$this->zip->read_dir($path, FALSE); //FALSE is to exclude entir path
		// Download the files to desktop. Name it "my_backup.zip"
		$this->zip->download('invoices-' . $this->input->post('start_date') . '-' .  $this->input->post('end_date') . '.zip');

		//finally, delete the directory
		//THIS NOT WORKING YET......
		//rmdir('./uploads/' . 'pdf/' . 'invoice-' . $this->input->post('start_date') . '-' .  $this->input->post('end_date'));

	}

	public function generate_deliveryslip_pdf($id) {

		//add PDF attachment DOMPDF 
			  
		$data['title'] = 'Delivery Slip'; 
		
		//get order
		$this->db->select('*')->from('orders')->where('id_orders', $id);
		$data['order'] = $this->db->get()->row();

		if($data['order']->dropship == 'yes') {
			//use dropshipper 
			$this->db->select('id_customers, name, email, phone, reseller_id, dropship, dropship_shop_name, dropship_shop_address, dropship_shop_phone, dropship_shop_email, dropship_shop_logo');
			$this->db->from('customers');
			$this->db->where('id_customers', $data['order']->customer_id);
			$data['customer'] = $this->db->get()->row();
			$data['logo'] = $data['customer']->dropship_shop_logo;
			$data['dropship_status'] = 'yes';
		
		} else {
			//non dropshipper
			$this->db->select('id_customers, name, email, phone, reseller_id');
			$this->db->from('customers');
			$this->db->where('id_customers', $data['order']->customer_id);
			$data['customer'] = $this->db->get()->row();
			$data['dropship_status'] = 'no';
			$this->db->select('logo')->from('configuration')->where('id_configuration', 1);
			$data['logo'] = $this->db->get()->row()->logo;
		} 

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

		//$data['bank'] = $this->configuration_m->get_bank();   

		//get order detail and customer detail
		$this->db->select('*')->from('orders_detail')->where('orders_id', $id);
		$data['order_details'] = $this->db->get()->result();

		$this->load->library('dompdf_gen');
		$html= $this->load->view('deliveryslip_pdf', $data ,true);
		$this->dompdf->load_html($html);
		$this->dompdf->render(); 
		$this->dompdf->stream('deliveryslip.pdf', array("Attachment" => 0));
		$output = $this->dompdf->output(); 
		$file_to_save = 'uploads/pdf/deliveryslip.pdf'; 
		file_put_contents($file_to_save, $output);  
	}

	public function generate_multiple_deliveryslips() {

		if(!$this->input->post('generate_deliveryslips')) { redirect('admin/orders');}

		$start_date_array = explode('-', $this->security->xss_clean($this->input->post('start_date')));
		$end_date_array = explode('-', $this->security->xss_clean($this->input->post('end_date')));
		//change to YYYY-MM-DD format for database insertion, and use php DatePeriod class
		$begin = new DateTime($start_date_array[2] . '-' . $start_date_array[1] . '-' . $start_date_array[0]);
		$end = new DateTime($end_date_array[2] . '-' . $end_date_array[1] . '-' . $end_date_array[0]);
		$end = $end->modify('+1 day'); 
		$interval = new DateInterval('P1D');
		$daterange = new DatePeriod($begin, $interval ,$end);

		$this->load->library('dompdf_gen');

		//create a new directory inside pdf folder
		if (!is_dir('./uploads/' . 'pdf/' . 'deliveryslip-' . $this->input->post('start_date') . '-' .  $this->input->post('end_date'))) { //check if directory already exist
			mkdir('./uploads/' . 'pdf/' . 'deliveryslip-' . $this->input->post('start_date') . '-' .  $this->input->post('end_date'));
			chmod('./uploads/' . 'pdf/' . 'deliveryslip-' . $this->input->post('start_date') . '-' .  $this->input->post('end_date'), 0777);  //change permission to writable 777, so can be deleted
		}

		$total_orders_found = 0;
		
		foreach($daterange as $date) {

			//get orders from each particular date
			//DATE(order_date) is a mysql function, to get onlye date from timestamp, exclude h-m-s
			$this->db->select('*');
			$this->db->from('orders');
			$this->db->where('DATE(order_date)', $date->format('Y-m-d'));
			if($this->input->post('include_unpaid_orders') == 'no') {
				$this->db->group_start();
				$this->db->where('payment_status', 1);
				$this->db->or_where('payment_status', 3);
				$this->db->group_end();
			}
			$orders = $this->db->get()->result();

			if(count($orders) > 0) {
				
				$total_orders_found = $total_orders_found + count($orders);

				foreach($orders as $data['order']) {

					//add PDF attachment DOMPDF 
					$data['title'] = 'Delivery Slip'; 
					
					//get order
					$this->db->select('*')->from('orders')->where('id_orders', $data['order']->id_orders);
					$data['order'] = $this->db->get()->row();

					if($data['order']->dropship == 'yes') {
						//use dropshipper 
						$this->db->select('id_customers, name, email, phone, reseller_id, dropship, dropship_shop_name, dropship_shop_address, dropship_shop_phone, dropship_shop_email, dropship_shop_logo');
						$this->db->from('customers');
						$this->db->where('id_customers', $data['order']->customer_id);
						$data['customer'] = $this->db->get()->row();
						$data['logo'] = $data['customer']->dropship_shop_logo;
						$data['dropship_status'] = 'yes';
					
					} else {
						//non dropshipper
						$this->db->select('id_customers, name, email, phone, reseller_id');
						$this->db->from('customers');
						$this->db->where('id_customers', $data['order']->customer_id);
						$data['customer'] = $this->db->get()->row();
						$data['dropship_status'] = 'no';
						$this->db->select('logo')->from('configuration')->where('id_configuration', 1);
						$data['logo'] = $this->db->get()->row()->logo;
					} 

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

					//$data['bank'] = $this->configuration_m->get_bank();   

					//get order detail and customer detail
					$this->db->select('*')->from('orders_detail')->where('orders_id', $data['order']->id_orders);
					$data['order_details'] = $this->db->get()->result();
					
					$html= $this->load->view('deliveryslip_pdf', $data ,true);
					//create a new dompdf instance (this is the crucial step)
					$this->dompdf = new DOMPDF();
					$this->dompdf->load_html($html);
					$this->dompdf->render(); 
					//$this->dompdf->stream('deliveryslip.pdf', array("Attachment" => 0));
					$output = $this->dompdf->output(); 

					//add pdf file into directory
					$file_to_save = 'uploads/pdf/deliveryslip-' . $this->input->post('start_date') . '-' .  $this->input->post('end_date') . '/deliveryslip-orderid-' . $data['order']->id_orders . '.pdf'; 
					file_put_contents($file_to_save, $output);  //$output function to add files to folder

				}

			}
	
		}

		if($total_orders_found == 0) {
			//dont download files, but go back to orders page and give error notice..
			$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">No orders are found</p>');
				redirect('admin/orders');
		}

		//zip the generated pds and downlod the zip file
		$this->load->library('zip');
		$path = './uploads/' . 'pdf/' . 'deliveryslip-' . $this->input->post('start_date') . '-' .  $this->input->post('end_date');
		$this->zip->read_dir($path, FALSE); //FALSE is to exclude entir path
		// Download the files to desktop. 
		$this->zip->download('deliveryslips-' . $this->input->post('start_date') . '-' .  $this->input->post('end_date') . '.zip');

		//finally, delete the directory
		//THIS NOT WORKING YET......
		//rmdir('./uploads/' . 'pdf/' . 'invoice-' . $this->input->post('start_date') . '-' .  $this->input->post('end_date'));

	}

	public function generate_shipping_mark($id){
		/*get order data*/
		$data['orders_detail'] 	= $this->db->select('*')->from('orders_detail')->where('id_orders_detail',$id)->get()->row();
		$data['orders']			= $this->db->select('*')->from('orders')->where('id_orders',$data['orders_detail']->orders_id)->get()->row();

		/*get customer id with order id*/
		$customer_id			= $this->db->select('customer_id')->from('orders')->where('id_orders',$data['orders_detail']->orders_id)->get()->row()->customer_id;

		/*get customer detail*/
		$data['customer']		= $this->db->select('*')->from('customers')->where('id_customers',$customer_id)->get()->row();

		/*get warehouse detail*/
		$data['warehouse']		= $this->db->select('*')->from('warehouse')->where('id',$data['orders_detail']->warehouse_id)->get()->row();
		$data['district']		= $this->db->select('district')->from('indonesia_districts')->where('rajaongkir_id_district',$data['warehouse']->id_district)->get()->row()->district;
		$data['subdistrict']	= $this->db->select('subdistrict')->from('indonesia_subdistricts')->where('rajaongkir_id_subdistrict',$data['warehouse']->id_subdistrict)->get()->row()->subdistrict;
		$data['province']		= $this->db->select('province')->from('indonesia_provinces')->where('rajaongkir_province_id',$data['warehouse']->id_province)->get()->row()->province;

		//add PDF attachment DOMPDF 
		$data['title'] 		= 'Shipping Mark';

		$this->load->library('dompdf_gen');
		$html = $this->load->view('shipping_mark_pdf', $data ,true);
		echo $html;die();
		$this->dompdf->load_html($html);
		$this->dompdf->set_paper('A4','landscape');
		$this->dompdf->render(); 
		$this->dompdf->stream('shipping_mark.pdf', array("Attachment" => 0));
		$output = $this->dompdf->output(); 
		$file_to_save = 'uploads/pdf/shipping_mark.pdf'; 
		file_put_contents($file_to_save, $output);

	}

	public function generate_invoice($id){
		/*get order data*/
		$data['orders']			= $this->db->select('*')->from('orders')->where('id_orders',$id)->get()->row();
		$data['orders_detail'] 	= $this->db->select('*')->from('orders_detail')->where('orders_id',$id)->get()->result();

		/*get customer id with order id*/
		$customer_id			= $this->db->select('customer_id')->from('orders')->where('id_orders',$id)->get()->row()->customer_id;

		/*get customer detail*/
		$data['customer']		= $this->db->select('*')->from('customers')->where('id_customers',$data['orders']->customer_id)->get()->row();

		//add PDF attachment DOMPDF 
		$data['title'] 		= 'Shipping Invoice';

		$this->load->library('dompdf_gen');
		$html = $this->load->view('shipping_invoice_pdf', $data ,true);
		echo $html;die();
		$this->dompdf->load_html($html);
		$this->dompdf->set_paper('A4','landscape');
		$this->dompdf->render(); 
		$this->dompdf->stream('shipping_mark.pdf', array("Attachment" => 0));
		$output = $this->dompdf->output(); 
		$file_to_save = 'uploads/pdf/shipping_mark.pdf'; 
		file_put_contents($file_to_save, $output);

	}

	public function new_generate_invoice($id) {
		if($id == NULL) {
			redirect('admin/penawaran');
		}

		//add PDF attachment DOMPDF 	  
		$pdf_data['title'] = 'INVOICE'; 

		//get all penawaran details
		$this->db->select('*')->from('orders')->where('id_orders', $id);
		$pdf_data['order'] = $this->db->get()->row();

		if(count($pdf_data['order']) > 0) {

			//get customer detail
			$this->db->select('*')->from('customers')->where('id_customers', $pdf_data['order']->customer_id);
			$pdf_data['customer'] = $this->db->get()->row();

			// get order detail
			$pdf_data['orders_detail'] 	= $this->db->select('*')->from('orders_detail')->where('orders_id',$id)->get()->result();

			//get current month ij roman
			$pdf_data['current_month'] = integerToRoman(date('n'));
			//get current year 2 digits
			$pdf_data['current_year'] = date('y');

			$this->load->library('dompdf_gen');
			$html = $this->load->view('new_shipping_invoice_pdf', $pdf_data ,true); 
			/*tambahan*/ 
			$html = preg_replace('/>\s+</', '><', $html);
			/*tambahan*/
			$this->dompdf->load_html($html);
			$this->dompdf->set_paper('A4', 'portrait');
			$this->dompdf->render(); 
			$this->dompdf->stream('new_shipping_mark.pdf', array("Attachment" => 0));
			$output = $this->dompdf->output(); 
			$file_to_save = 'uploads/pdf/new_shipping_mark.pdf'; 
			file_put_contents($file_to_save, $output); 

		} else {
			redirect('admin/orders');
		}
	}

    public function update_noresi(){
        $order_detail_id = $this->input->post('id_orders_detail');
        /*get order id*/
        $id_order = $this->db->select('orders_id')->from('orders_detail')->where('id_orders_detail',$order_detail_id)->get()->row()->orders_id;
        /*get order id*/
        $new_resi = array(
            'no_resi' => $this->input->post('no_resi'), 
        );

        $this->db->where('id_orders_detail',$order_detail_id);
        $this->db->update('orders_detail',$new_resi);
        $this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">No. Resi Updated</p>');
        redirect('admin/orders/view/' . $id_order); 
		}
		
		public function cancel_pickup($order_id, $order_detail_id) {

			//get order data
			$order = $this->db->select('*')->from('orders')->where('id_orders', $order_id)->get()->row();
			$order_detail = $this->db->select('*')->from('orders_detail')->where('id_orders_detail',  $order_detail_id)->get()->row();

			//cancel pickup
			$this->load->helper('shipping');
			$airwaybill = rex_cancel_awb($order_detail->no_resi);

			if($airwaybill->status == 'ok') {
				//update order and order detail table..
				$data = array(
					'payment_status' => 4
				);
				$this->db->where('id_orders', $order_id);
				$this->db->update('orders', $data);

				$data = array(
					'no_resi' => '',
					'rex_tracking_ref_no' => '',
					'status' => 0
				);
				$this->db->where('id_orders_detail', $order_detail_id);
				$this->db->update('orders_detail', $data);
			}

			$this->session->set_flashdata('success', '<br><p style="background:green; color:white; padding:5px; font-weight:bold;">Pickup has been Cancel.</p>');
			redirect('admin/orders/view/' . $order_id); 
		}

		public function excel_export() {
		
			$this->load->library('excel'); 
			header("Content-type: application/vnd-ms-excel");
			header("Content-Disposition: attachment; filename=indolok_transaction_report.xls");

			if(!empty($this->input->post('sales_start'))) {
				$date_start = explode('-', $this->input->post('sales_start'));
				$new_date_start = $date_start[2] . '-' . $date_start[1] . '-' . $date_start[0];
			} else {
				$new_date_start = '';
			}

			if(!empty($this->input->post('sales_end'))) {
				$date_end = explode('-', $this->input->post('sales_end'));
				$new_date_end = $date_end[2] . '-' . $date_end[1] . '-' . $date_end[0] . ' 23:59:59';
			} else {
				$new_date_end = '';
			}

			$data['order_details'] = $this->order_m->find_orderdetails_filter_date($new_date_start, $new_date_end); 
	
			$this->load->view('admin/orders/salesorder_excel_report', $data);
		}		
			
}

https://t.me/RX1948 - 2025