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

class PurchasePricing extends CI_Controller{
	public function __construct(){
		parent::__construct();
		
		$this->load->library('layouts');
		$this->load->helper('form');
		$this->load->helper('menu');
		$this->layouts->set_title('iAccount::Purchase Pricing');
		
		$this->load->model('master/masterCode_model');
		$this->load->model('Menu');
		$this->load->model('Purchase_pricing');		
		$this->lang->load("purchasepricing",$this->session->userdata('language'));	
	}
	
	public function index(){
		//Retrive from predata
		$data = $this->Purchase_pricing->getFormData();
				
		$this->layouts->add_includes('js/datatables/jquery.dataTables.min.js');
		$this->layouts->view('purchase/pricing/allpurchaseprice', array('latest' => 'sidebar/latest'),$data);
	}
	
	//Add price to database
	public function add(){
		if($this->input->post('supplierID') && $this->input->post('itemID')){
			//Insert Location Transfer informations
			$itemPrice = array( 
				'ID'  				=> $this->input->post('itemID'),
				'supplierPrice'		=> $this->input->post('itemPrice'),
				'supplierID'		=> $this->input->post('supplierID'),
				'updatedTS'	   		=> date('Y-m-d G:i:s',time()),
			
			);
			
		  	$itemPriceID = $this->Purchase_pricing->insert($this->input->post('itemID'),$itemPrice);	
			if($itemPriceID){
				$this->session->set_flashdata('msg', 'Updated Successfully');
				echo 'Updated';
			}else{
				$this->session->set_flashdata('msg', 'Unsuccessful');
				echo 'unsuccessfull';
			}
		}
		
		exit();	
	}
	
	//Get individual item price
	public function get(){
		$data = $this->Purchase_pricing->getPrice($this->input->post('ID'));
		
		echo json_encode((array)$data[0]);
		exit();
	}
	
	//Delete item price
	public function delete(){
		$data = explode(',',$this->input->post('data'));
		$error = false;
		foreach($data as &$v){
			$v = $this->Sales_pricing->delete($v);
		}
		echo "Successfully Deleted";	
		exit();
	}
}