<?php
defined('BASEPATH') OR exit('No direct script access allowed');

class Data extends CI_Controller {
	public function __construct() {
        parent::__construct();
    }

	public function get_product_unit(){
		$limit = 20;
		$q = $this->input->get("term");
		$page = $this->input->get("page");

		$offset = ($page - 1) * $limit;

		$this->db->order_by("name");
		if (strlen($q) > 0) {
			$this->db->like("name", $q);
		}
		$this->db->select("idproduct_unit as id, name as text");
		$this->db->where('status', '1');

		$data = $this->db->get("product_unit", $limit, $offset);

		if (strlen($q) > 0) {
			$this->db->like("name", $q);
		}
		$this->db->where('status', '1');
		$cdata = $this->db->get("product_unit");
		$count = $cdata->num_rows();

		$endCount = $offset + $limit;
		$morePages = $endCount < $count;

		$results = array(
		  "results" => $data->result_array(),
		  "pagination" => array(
		  	"more" => $morePages
		  )
		);
		echo json_encode($results);
	}

	public function get_product_category(){
		$limit = 20;
		$q = $this->input->get("term");
		$page = $this->input->get("page");

		$offset = ($page - 1) * $limit;

		$this->db->order_by("name");
		if (strlen($q) > 0) {
			$this->db->like("name", $q);
		}
		$this->db->select("idproduct_category as id, name as text");
		$this->db->where('status', '1');

		$data = $this->db->get("product_category", $limit, $offset);

		if (strlen($q) > 0) {
			$this->db->like("name", $q);
		}
		$this->db->where('status', '1');
		$cdata = $this->db->get("product_category");
		$count = $cdata->num_rows();

		$endCount = $offset + $limit;
		$morePages = $endCount < $count;

		$results = array(
		  "results" => $data->result_array(),
		  "pagination" => array(
		  	"more" => $morePages
		  )
		);
		echo json_encode($results);
	}

	public function get_outlet(){
		$limit = 20;
		$q = $this->input->get("term");
		$page = $this->input->get("page");

		$offset = ($page - 1) * $limit;

		$this->db->order_by("outlet_name");
		if (strlen($q) > 0) {
			$this->db->like("outlet_name", $q);
		}
		$this->db->select("idoutlet as id, outlet_name as text");
		$this->db->where('status', '1');

		$data = $this->db->get("outlet", $limit, $offset);

		if (strlen($q) > 0) {
			$this->db->like("outlet_name", $q);
		}
		$this->db->where('status', '1');
		$cdata = $this->db->get("outlet");
		$count = $cdata->num_rows();

		$endCount = $offset + $limit;
		$morePages = $endCount < $count;

		$results = array(
		  "results" => $data->result_array(),
		  "pagination" => array(
		  	"more" => $morePages
		  )
		);
		echo json_encode($results);
	}

	public function get_product(){
		$limit = 20;
		$q = $this->input->get("term");
		$page = $this->input->get("page");
		$parent = $this->input->get('parent');

		$offset = ($page - 1) * $limit;

		$this->db->where('idoutlet', $parent);
		$this->db->order_by("product_code");
		if (strlen($q) > 0) {
			$this->db->like("product_code", $q);
			$this->db->or_like('product_code', $q);
		}
		$this->db->select("idproduct as id, concat(product_code, ' - ', product_name) as text");
		$this->db->where('status', '1');

		$data = $this->db->get("product", $limit, $offset);

		if (strlen($q) > 0) {
			$this->db->like("product_code", $q);
			$this->db->or_like('product_code', $q);
		}
		$this->db->where('idoutlet', $parent);
		$this->db->where('status', '1');
		$cdata = $this->db->get("product");
		$count = $cdata->num_rows();

		$endCount = $offset + $limit;
		$morePages = $endCount < $count;

		$results = array(
		  "results" => $data->result_array(),
		  "pagination" => array(
		  	"more" => $morePages
		  )
		);
		echo json_encode($results);
	}

	public function get_supplier(){
		$limit = 20;
		$q = $this->input->get("term");
		$page = $this->input->get("page");

		$offset = ($page - 1) * $limit;

		$this->db->order_by("supplier_name");
		if (strlen($q) > 0) {
			$this->db->like("supplier_name", $q);
		}
		$this->db->select("idsupplier as id, supplier_name as text");
		$this->db->where('status', '1');

		$data = $this->db->get("supplier", $limit, $offset);

		if (strlen($q) > 0) {
			$this->db->like("supplier_name", $q);
		}
		$this->db->where('status', '1');
		$cdata = $this->db->get("supplier");
		$count = $cdata->num_rows();

		$endCount = $offset + $limit;
		$morePages = $endCount < $count;

		$results = array(
		  "results" => $data->result_array(),
		  "pagination" => array(
		  	"more" => $morePages
		  )
		);
		echo json_encode($results);
	}

	public function get_customer(){
		$limit = 20;
		$q = $this->input->get("term");
		$page = $this->input->get("page");
		$parent = $this->input->get("parent");

		$sess_idoutlet = $this->session->userdata('idoutlet');
		if (strlen($sess_idoutlet) > 0) {
			$parent = $sess_idoutlet;
		}

		$offset = ($page - 1) * $limit;

		$this->db->order_by("customer_name");
		if (strlen($q) > 0) {
			$this->db->group_start();
			$this->db->like("customer_name", $q);
			$this->db->or_like("customer.phone", $q);
			$this->db->or_like("customer.customer_code", $q);
			$this->db->group_end();
		}
		if (strlen($parent) > 0) {
			$this->db->where('idoutlet', $parent);
		}
		$this->db->select("idcustomer as id, concat(customer_code, ' - ', customer_name) as text");
		$this->db->where('status', '1');

		$data = $this->db->get("customer", $limit, $offset);

		if (strlen($q) > 0) {
			$this->db->group_start();
			$this->db->like("customer_name", $q);
			$this->db->or_like("customer.phone", $q);
			$this->db->or_like("customer.customer_code", $q);
			$this->db->group_end();
		}
		if (strlen($parent) > 0) {
			$this->db->where('idoutlet', $parent);		
		}
		$this->db->where('status', '1');
		$cdata = $this->db->get("customer");
		$count = $cdata->num_rows();

		$endCount = $offset + $limit;
		$morePages = $endCount < $count;

		$results = array(
		  "results" => $data->result_array(),
		  "pagination" => array(
		  	"more" => $morePages
		  )
		);
		echo json_encode($results);
	}

	public function get_data_user(){
		$limit = 20;
		$q = $this->input->get("term");
		$page = $this->input->get("page");
		$parent = $this->input->get("parent");

		$sess_idoutlet = $this->session->userdata('idoutlet');
		if (strlen($sess_idoutlet) > 0) {
			$parent = $sess_idoutlet;
		}

		$offset = ($page - 1) * $limit;

		$this->db->order_by("user.name");
		$this->db->where('idoutlet', $parent);		
		if (strlen($q) > 0) {
			$this->db->like("user.name", $q);
		}
		$this->db->select("iduser as id, user.name as text");
		$this->db->where('user.status', '1');
		$this->db->join('role', 'user.idrole = role.idrole');
		$this->db->where('role.name', 'cashier');

		$data = $this->db->get("user", $limit, $offset);
		$this->db->where('idoutlet', $parent);
		if (strlen($q) > 0) {
			$this->db->like("user.name", $q);
		}
		$this->db->join('role', 'user.idrole = role.idrole');
		$this->db->where('role.name', 'cashier');
		$this->db->where('user.status', '1');
		$cdata = $this->db->get("user");
		$count = $cdata->num_rows();

		$endCount = $offset + $limit;
		$morePages = $endCount < $count;

		$results = array(
		  "results" => $data->result_array(),
		  "pagination" => array(
		  	"more" => $morePages
		  )
		);
		echo json_encode($results);
	}
	
	function get_data_product () {
		$this->load->library('AZApp');
		$crud = $this->azapp->add_crud();

		$idoutlet = $this->input->get('idoutlet');
		$idcustomer = $this->input->get('idcustomer');
		$idtable = $this->input->get('idtable');

		$this->db->where('transaction_group.status', 1);
		$this->db->where('idcustomer', $idcustomer);
		$this->db->where('transaction_group_status !=', 'Selesai');
		$this->db->join('transaction_group', 'transaction_group.idtransaction_group = transaction.idtransaction_group');
		$this->db->group_by('transaction.iditem');
		$check = $this->db->get('transaction');
		foreach ($check->result() as $c_key => $c_value) {
			$comma = '';
			// if ($idtable != ',') {
			// 	$comma = ',';
			// }

			if (substr($idtable, -1) != ',') {
				$comma = ',';
			}

			$idtable .= $comma.$c_value->iditem;
		}

		if (strlen($idtable) > 1) {
			$crud->add_where("item.iditem not in (".substr($idtable, 1).")");
		}

		$crud->add_where('idcustomer = "'.$idcustomer.'"');
		$crud->add_where("idoutlet = '".$idoutlet."'");
		$b_l = $this->input->get('b_l');
        if (strlen($b_l) == 0) { $b_l = $this->input->post('b_l'); }
        $b_l = strtoupper(trim((string)$b_l));
        if (in_array($b_l, array('B', 'L'))) {
        $crud->add_where("product.b_l = '" . $b_l . "'");
        }
		$crud->set_select('iditem, item_code, product_name, sell_price, "" as photo, photo1, photo2, photo3, photo4, photo5, photo6, "" as warna, "" as corak, idcolour, idhue');
		$crud->set_select_table('iditem, item_code, product_name, sell_price, photo, warna, corak');
		$crud->set_filter('item_code, product_name, sell_price');
		$crud->set_sorting('item_code, product_name, sell_price');
		$crud->set_id('item');
		$crud->add_join('product');
		$crud->add_where("item.status > 0");
		$crud->set_table('item');
		$crud->set_order_by('item.item_code');
		$crud->set_custom_style('custom_style_product');
		$crud->set_edit(false);
		$crud->set_delete(false);
		$crud->set_select_decimal('2');
		$crud->set_select_align(',,right');
		echo $crud->get_table();
	}

	function custom_style_product($key, $value, $data) {
		$idproduct = azarr($data, 'iditem');
		$product_type = azarr($data, 'product_type');
		$price = azarr($data, 'sell_price');
		$product_name = azarr($data, 'product_name');
		$product_type = azlang($product_type);
		$code = azarr($data, 'item_code');

		if ($key == 'product_type') {
			return azlang($value);
		}
		else if ($key == 'photo') {
			$photo1 = azarr($data, 'photo1');
			$photo2 = azarr($data, 'photo2');
			$photo3 = azarr($data, 'photo3');
			$photo4 = azarr($data, 'photo4');
			$photo5 = azarr($data, 'photo5');
			$photo6 = azarr($data, 'photo6');

			$img = '';

			if (strlen($photo1) > 0) {
				$img .= "<img class='img-item-list' src='".base_url().AZAPP.'assets/item/'.$photo1."'>";
			}
			if (strlen($photo2) > 0) {
				$img .= "<img class='img-item-list' src='".base_url().AZAPP.'assets/item/'.$photo2."'>";
			}
			if (strlen($photo3) > 0) {
				$img .= "<img class='img-item-list' src='".base_url().AZAPP.'assets/item/'.$photo3."'>";
			}
			if (strlen($photo4) > 0) {
				$img .= "<img class='img-item-list' src='".base_url().AZAPP.'assets/item/'.$photo4."'>";
			}
			if (strlen($photo5) > 0) {
				$img .= "<img class='img-item-list' src='".base_url().AZAPP.'assets/item/'.$photo5."'>";
			}
			if (strlen($photo6) > 0) {
				$img .= "<img class='img-item-list' src='".base_url().AZAPP.'assets/item/'.$photo6."'>";
			}

			return $img;
		}
		else if ($key == 'action') {
			return "<button data-code='".$code."' class='btn btn-info btn-xs btn-choose-product' type='button'><i class='fa fa-arrow-down'></i> ".azlang('Choose')."</button>";
		}
		else if ($key == 'warna') {
			$idcolour = azarr($data, 'idcolour');
			$colour = az_convert_colour($idcolour);
			return $colour;
		}
		else if ($key == 'corak') {
			$idhue = azarr($data, 'idhue');
			$hue = az_convert_hue($idhue);
			return $hue;
		}
		return $value;
	}

	function edit_data_product() {}
	function delete_data_product() {}

	public function get_data_outlay_type(){
		$limit = 20;
		$q = $this->input->get("term");
		$page = $this->input->get("page");

		$offset = ($page - 1) * $limit;

		$this->db->order_by("outlay_type_name");
		if (strlen($q) > 0) {
			$this->db->like("outlay_type_name", $q);
		}
		$this->db->select("idoutlay_type as id, outlay_type_name as text");
		$this->db->where('status', '1');

		$data = $this->db->get("outlay_type", $limit, $offset);

		if (strlen($q) > 0) {
			$this->db->like("outlay_type_name", $q);
		}
		$this->db->where('status', '1');
		$cdata = $this->db->get("outlay_type");
		$count = $cdata->num_rows();

		$endCount = $offset + $limit;
		$morePages = $endCount < $count;

		$results = array(
		  "results" => $data->result_array(),
		  "pagination" => array(
		  	"more" => $morePages
		  )
		);
		echo json_encode($results);
	}

	public function get_data_invoice(){
		$limit = 20;
		$q = $this->input->get("term");
		$page = $this->input->get("page");
		$parent = $this->input->get('parent');

		$offset = ($page - 1) * $limit;

		$this->db->order_by("transaction_group.date", "desc");
		$this->db->join('customer', 'transaction_group.idcustomer = customer.idcustomer', 'left');
		if (strlen($q) > 0) {
			$this->db->group_start();
			$this->db->like("code", $q);
			$this->db->or_like('customer_name', $q);
			$this->db->group_end();
		}
		$this->db->where('transaction_group_status', 'BELUM LUNAS');
		$this->db->where('transaction_group.idoutlet', $parent);
		$this->db->select("idtransaction_group as id, concat(code, ' - ', ifnull(customer_name, '')) as text");
		$this->db->where('transaction_group.status', '1');

		$data = $this->db->get("transaction_group", $limit, $offset);

		$this->db->join('customer', 'transaction_group.idcustomer = customer.idcustomer', 'left');
		if (strlen($q) > 0) {
			$this->db->group_start();
			$this->db->like("code", $q);
			$this->db->or_like('customer_name', $q);
			$this->db->group_end();
		}
		$this->db->where('transaction_group_status', 'BELUM LUNAS');
		$this->db->where('transaction_group.idoutlet', $parent);
		$this->db->where('transaction_group.status', '1');
		$cdata = $this->db->get("transaction_group");
		$count = $cdata->num_rows();

		$endCount = $offset + $limit;
		$morePages = $endCount < $count;

		$results = array(
		  "results" => $data->result_array(),
		  "pagination" => array(
		  	"more" => $morePages
		  )
		);
		echo json_encode($results);
	}

	public function get_data_employee(){
		$limit = 20;
		$q = $this->input->get("term");
		$page = $this->input->get("page");
		$parent = $this->input->get("parent");

		$offset = ($page - 1) * $limit;

		$this->db->order_by("employee_name");
		if (strlen($q) > 0) {
			$this->db->like("employee_name", $q);
		}
		if (isset($parent) > 0) {
			$this->db->where('idoutlet', $parent);
		}
		$this->db->select("idemployee as id, employee_name as text");
		$this->db->where('status', '1');

		$data = $this->db->get("employee", $limit, $offset);

		if (strlen($q) > 0) {
			$this->db->like("employee_name", $q);
		}
		if (isset($parent) > 0) {
			$this->db->where('idoutlet', $parent);
		}
		$this->db->where('status', '1');
		$cdata = $this->db->get("employee");
		$count = $cdata->num_rows();

		$endCount = $offset + $limit;
		$morePages = $endCount < $count;

		$results = array(
		  "results" => $data->result_array(),
		  "pagination" => array(
		  	"more" => $morePages
		  )
		);
		echo json_encode($results);
	}

	public function get_nota(){
		$limit = 20;
		$q = $this->input->get("term");
		$page = $this->input->get("page");

		$offset = ($page - 1) * $limit;

		$this->db->order_by("code");
		if (strlen($q) > 0) {
			$this->db->like("code", $q);
		}
		$this->db->select("idtransaction_group as id, code as text");
		$this->db->where('status', '1');
		$this->db->where_in('pay', array('DP PAID', 'NOT PAID YET'));
		$data = $this->db->get("transaction_group", $limit, $offset);

		if (strlen($q) > 0) {
			$this->db->like("code", $q);
		}
		$this->db->where('status', '1');
		$this->db->where_in('pay', array('DP PAID', 'NOT PAID YET'));
		$cdata = $this->db->get("transaction_group");
		$count = $cdata->num_rows();

		$endCount = $offset + $limit;
		$morePages = $endCount < $count;

		$results = array(
		  "results" => $data->result_array(),
		  "pagination" => array(
		  	"more" => $morePages
		  )
		);
		echo json_encode($results);
	}

	public function get_data_agent(){
		$limit = 20;
		$q = $this->input->get("term");
		$page = $this->input->get("page");
		$parent = $this->input->get("parent");

		$offset = ($page - 1) * $limit;

		$this->db->where('idoutlet', $parent);
		$this->db->order_by("agent_name");
		if (strlen($q) > 0) {
			$this->db->like("agent_name", $q);
		}
		$this->db->select("idagent as id, agent_name as text");
		$this->db->where('status', '1');

		$data = $this->db->get("agent", $limit, $offset);

		$this->db->where('idoutlet', $parent);
		if (strlen($q) > 0) {
			$this->db->like("agent_name", $q);
		}
		$this->db->where('status', '1');
		$cdata = $this->db->get("agent");
		$count = $cdata->num_rows();

		$endCount = $offset + $limit;
		$morePages = $endCount < $count;

		$results = array(
		  "results" => $data->result_array(),
		  "pagination" => array(
		  	"more" => $morePages
		  )
		);
		echo json_encode($results);
	}

	
}