<?php

require APPPATH . '/libraries/REST_Controller.php';

use Restserver\Libraries\REST_Controller;

class Transferitem extends REST_Controller
{

    /**
     * Get All Data from this method.
     *
     * @return Response
     */
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
        $this->load->model('counter_model');
        $this->load->model('transfer_model');
    }

    /**
     * Get All Data from this method.
     *
     * @return Response
     */
    public function index_get($id = 0)
    {
        if (!empty($id)) {

            $values = $this->transfer_model->get_trasfer_product_list($id);
            $data          = array();
            foreach ($values as $row) {
    
                $nestad_data = array();
                $nestad_data['row_id']  = $row['sti_id'];
                $nestad_data['product_code']  = $row['product_code'];
                $nestad_data['product_name']  = $row['product_name'];
                $nestad_data['unit_code']  = $row['unit_code'];
                //$nestad_data[]  = $row['uom_cost'];
                $nestad_data['product_retail_pirce']  = $row['product_retail_pirce'];
                $nestad_data['quantity']  = $row['quantity'];
                $nestad_data['product_cost']  = $row['product_cost'];
                $data[] = $nestad_data;
            }
        } else {
            $data = $this->db->get("stock_transfer_itemm")->result();
        }
        $response = array(
            'result' => $data,
            'status' => 1,
        );
        $this->response($data, REST_Controller::HTTP_OK);
    }

    public function productsearch_get()
    {
        $search_key_val = $this->get('search_string');
        $this->db->select("o.*,p.product_name,p.product_code");
        $this->db->from('batch_product o');
        $this->db->join("product p", "p.product_id = o.product_id", "inner");
        $this->db->where("o.batch_status", 1);
        $this->db->like('p.product_name', $search_key_val, 'after');
        $this->db->or_like('p.product_code', $search_key_val, 'after');
        //$this->db->or_where("o.batch_status = 1 AND  o.batch_ref_number LIKE '$search_key_val%'");
        $this->db->group_by('o.batch_id');
        $this->db->limit(5);
        $query = $this->db->get();
        $datar = $query->result_array();
        $data = array();
        foreach ($query->result_array() as $r) {
            $stock = $this->counter_model->get_stock_balance_batch('', $r['product_id'], $r['batch_id']);
            $nested_data = array(
                'id' => $r['batch_id'],
                'text' => $r['product_name'] . '(' . $r['product_code'] . ')' . ", BATCH  " . $r['batch_ref_number'] . ", COST RS." . $r['product_cost'] . ", STOCK " . $stock,
            );
            $data[] = $nested_data;
        }

        //return $query->result_array();

        $this->response($data, REST_Controller::HTTP_OK);
    }
    
    

    /**
     * Get All Data from this method.
     *
     * @return Response
     */
    public function index_post()
    {

        $batch_id = $this->post('product_id');
        $odr_id = $this->post('odr_id');
        $req_qty = $this->post('req_qty');
        $sale_cost = $this->post('sale_cost');
        $price_cost = $this->transfer_model->get_product_price_cost($batch_id);

        $stock = $this->counter_model->get_stock_ledger_balance('', $price_cost['product_id'], $batch_id);
        if ($stock < $req_qty) {
            $response = array(
                'msg' => 'OUT OF STOCK',
                'status' => 0,
            );
        } else {
            $data = array(
                'stm_id' => $odr_id,
                'product_id' => $price_cost['product_id'],
                'quantity' => $req_qty,
                'product_cost' => ($sale_cost)?$sale_cost:$price_cost['product_cost'],
                'product_retail_pirce' => 0,
                'product_wholsale_pirce' => 0,
                'product_credit_pirce' => 0,
                'product_code' => $price_cost['product_code'],
                'batch_id' => $batch_id,
                'batch_code' => $price_cost['batch_ref_number'],
            );
            $result = $this->transfer_model->save_trasfer_item($data);
            if ($result === FALSE) {
                $response = array(
                    'result' => $data,
                    'status' => 0,
                );
            } else {
                $response = array(
                    'result' => $data,
                    'status' => 1,
                    'msg' => 'Item successfully added.',
                );
            }
        }
        $this->response($response, REST_Controller::HTTP_OK);
    }

    /**
     * Get All Data from this method.
     *
     * @return Response
     */
    public function index_put($id)
    {
        $input = $this->put();
        $this->db->update('products', $input, array('id' => $id));

        $this->response(['Product updated successfully.'], REST_Controller::HTTP_OK);
    }

    /**
     * Get All Data from this method.
     *
     * @return Response
     */
    public function index_delete($id)
    {

        $result = $this->transfer_model->delete_trasfer_item($id);
        $st = array('status' => 1, 'msg' => 'Transfer item deleted');

        //$this->db->delete('products', array('id' => $id));

        $this->response($st, REST_Controller::HTTP_OK);
    }
}
