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

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

    public function getData()
    {
        $data = $this->getLogModel()->get_by(array(
            Type =>"BIN TO BIN TRANSFER",
            Status => 'Pending'
        ), false);

        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');
        $running_no = $this->input->post('running_no');
        $location = $this->input->post('location');
        $warehouse = $this->input->post('warehouse');
        $quantity = $this->input->post('quantity');
        $quantity_scanned = $this->input->post('quantity_scanned');
        $uom = $this->input->post('uom');
        $owner = $this->input->post('owner');
        $scanned_date = $this->input->post('scanned_date');

        if (!isset($item_code, $location, $warehouse, $quantity, $uom, $running_no)) {
            $this->FailedResponse('Invalid Param');
        }
        $month = date('m') ;
        $year = date('y') ;
        $year_full = date('Y') ;
        $month_full = date('M') ;
        $count = $this->getLogModel()->getTransferCount($year, $month);
        $count = $count + 1;
        $reference = 'BIN_TRANSER_['.$year.']['.$month.']['.$count.']';

        $data = array(
            Serial_Number => $running_no,
            Reference_No => $reference,
            Item_Code => $item_code,
            Quantity=> $quantity,
            Quantity_Scanned=> $quantity,
            UOM => $uom,
            Type => "BIN TO BIN TRANSFER",
            Status => "Pending",
            Original_Loc => $location,
            Original_WH => $warehouse,
            Owner =>$owner,
            "Month" =>$month_full,
            "Year" =>$year_full,
            Date_Receiving => $scanned_date,
            Owner =>$owner
        );

        $id = $this->getLogModel()->save($data);
        if ($id) {
           // $this->callUrl($this->triggerUrl.'?id='.$id, false);
            $this->TextResponse("Added item successfully");
        } else {
            $this->FailedResponse('Something went wrong.');
        }
    }
    public function updateData()
    {
        //ItemCode~Location~Supplier~Quantity~UOM~RunningNumber
        $id = $this->input->post('id');
        $location = $this->input->post('location');
        $warehouse = $this->input->post('warehouse');
        $scanned_date = $this->input->post('scanned_date');

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

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

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

        $id = $data[ID];

		$dest_loc = $data[Destination_Loc];
		$dest_wh = $data[Destination_WH];
		$date_scanned = $data[Date_Scanned];
		$status = $data[Status];

        $data = array(
            Destination_Loc => $location,
            Destination_WH => $warehouse,
            Date_Scanned => $scanned_date,
            Status => "Closed",
        );
        $this->getLogModel()->update($data, $id);

        $triggerUrl = ASPX_SERVER."Inv_Bin_to_Bin.aspx?id=".$id;
		$res = $this->callUrl($triggerUrl, true);
		if($res!==FALSE){
			$this->TextResponse("Updated data successfully");
		} else {
			$data[Destination_Loc] = $dest_loc;
			$data[Destination_WH] = $dest_wh;
			$data[Date_Scanned] = $date_scanned;
			$data[Status] = $status;
			$this->getLogModel()->update($data, $id);
			$this->FailedResponse("Updated failed");
		}
    }
   

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

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

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

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

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


}