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

class Stock extends Api_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function getData()
    {
        $data = $this->getStockModel()->get();
        if ($data) {
            $this->SuccessResponse($data);
        } else {
            $this->FailedResponse("There is no item yet");
        }
    }

    public function addData()
    {
        //ItemCode~Location~Supplier~Quantity~UOM~RunningNumber
        $item_code = $this->input->post('item_code');
        $location = $this->input->post('location');
        $warehouse = $this->input->post('warehouse');
        $quantity = $this->input->post('quantity');
        $uom = $this->input->post('uom');
        $running_no = $this->input->post('running_no');


        if (!isset($item_code, $location, $warehouse, $quantity, $uom, $running_no)) {
            $this->FailedResponse('Invalid Param');
        }

        $stock_data = $this->getStockModel()->get_by(array(
            Serial_Number => $running_no
        ), true);

        if ($stock_data) $this->FailedResponse("Already added the running number");

        $data = array(
            Serial_Number => $running_no,
            Item_Code => $item_code,
            Quantity=> $quantity,
            UOM => $uom,
            "Warehouse" =>$warehouse,
            "Location_Pole" => $location
        );

        $id = $this->getStockModel()->save($data);
        if ($id) {
            $this->TextResponse("Added item successfully");
        } else {
            $this->FailedResponse('Something went wrong.');
        }
    }

    public function deleteData()
    {
        $id = $this->input->post('id');

        if (!isset($id)) {
            $this->FailedResponse('Invalid Param');
        }

        $data = $this->getStockModel()->get_by(array(
            ID => $id,
        ), true);

        if (!$data) {
            $this->FailedResponse("System has no item for your request");
        }

        $this->getStockModel()->delete($id);
        $this->TextResponse("Removed data successfully");
    }


}