<?php
use Symfony\Component\HttpFoundation\Request;

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

class Uniformpos extends Admin_Controller {

    function __construct() {
        parent::__construct();
    }

    function index() {
        if (!$this->rbac->hasPrivilege('uniformpos', 'can_view')) {
            access_denied();
        }
        $this->session->set_userdata('top_menu', 'Uniform POS');
        $this->session->set_userdata('sub_menu', 'uniformpos/index');
        $data['title'] = 'Make Transactions ';
        $data['title_list'] = 'Uniform Transactions List';

    //(`student_id`, `itemcode`, `description`,original quantity'' `quantity`, `amount`, `payment`, `created_date`)

        $this->form_validation->set_rules('student_id', 'Student Id', 'required');
		$this->form_validation->set_rules('itemcode', 'Item Code', 'required');
		$this->form_validation->set_rules('description', 'Description', 'required');
        $this->form_validation->set_rules('originalqnty', 'Available Qnty', 'required');
		$this->form_validation->set_rules('quantity', 'Quanity', 'required');
        $this->form_validation->set_rules('warehouse', 'Warehouse', 'required');
		$this->form_validation->set_rules('amount', 'Amount', 'required');
        $this->form_validation->set_rules('name', 'Name', 'required');
		$this->form_validation->set_rules('payment', 'Payment Method', 'required');
		
        if ($this->form_validation->run() == FALSE) {
            
        } else {			
			
            $student_id = $this->input->post('student_id');
            $name = $this->input->post('name');
            $itemcode = $this->input->post('itemcode');
            $originalqnty = $this->input->post('originalqnty');
            $quantityuser = $this->input->post('quantity');
            $warehouse = $this->input->post('warehouse');
            $amountpaid = $this->input->post('amount');
            $this->db->select('*');
            $this->db->from('uniformitem');
            $this->db->where('itemcode', $itemcode);
            $query1 = $this->db->get();
            $result1 = $query1->row();
            $quantity2 = $result1->quantity;
            $sellingprice = $result1->sellingprice;
			//CALC QNTY LEFT FOR AN ITEM PURCHASED
            $leftquantity = $quantity2 - $quantityuser;					
				//CALC CUSTOMER CHANGE
					$customer_change = $sellingprice - 0;				
			
    				$uniformpos_array = array(
						'student_id' => $this->input->post('student_id'),
                        'name' => $this->input->post('name'),
						'itemcode' => $this->input->post('itemcode'),
						'description' => $this->input->post('description'),
                        'originalqnty' => $this->input->post('originalqnty'),
                        'warehouse' => $this->input->post('warehouse'),
						'quantity' => $this->input->post('quantity'),
						'amount' => $amountpaid,
						'payment' => 0,
						'created_date' => date('Y-m-d'),
						'balance' => $customer_change
					);
					$uniformitem_array = array(
						'itemcode' => $this->input->post('itemcode'),
						'quantity' => $leftquantity
					);				
					$this->uniformpos_model->add($uniformpos_array); 
					$this->uniformitem_model->updateQnty($uniformitem_array);     
					$this->session->set_flashdata('msg', '<div class="alert alert-success text-left">Transaction made successfully</div>');
					redirect('uniformpos');					

        }
		  $data['payment_methods']=$this->payment_methods_model->select();
		  $data['stud_id']=$this->student_model->select(); 		 
          $data['code']=$this->uniformitem_model->select();
          $data['uniform']=$this->uniformpos_model->select();
          $data['itemlist']=$this->uniformitem_model->select();
          $data['studenID'] = $this->student_model->selectstuden();
          $data['settinglist'] = $this->setting_model->get();

          $data['studentID']=$this->group_model->selectSID();
   
         

         if( $this->group_model->validateselectInvoiceNo()!="null")
         {
            $data['inv']='INV000'.($this->group_model->validateselectInvoiceNo()+1);

         }
         else{
            $this->group_model->addInvoiceNo();
            $data['inv']='INV000'.($this->group_model->validateselectInvoiceNo()+1);
         }
          


         
        $this->load->view('layout/header', $data);
        $this->load->view('uniformpos/uniformposList', $data);
        $this->load->view('layout/footer', $data);
    }

    function delete($id) {
        if (!$this->rbac->hasPrivilege('uniformpos', 'can_delete')) {
            access_denied();
        }
        $data['title'] = 'Uniform Transactions';
        $this->group_model->remove($id);
        redirect('uniformpos');
    }

    function edit($id) {
        if (!$this->rbac->hasPrivilege('types', 'can_edit')) {
            access_denied();
        }

        $data['id'] = $id;
		$data['groups'] = $this->group_model->update($id);
		$data['typ']=$this->types_model->select(); 		
			
        $this->form_validation->set_rules('groups', 'Group Name', 'required');
		$this->form_validation->set_rules('group_type', 'Group Type', 'required');
	   
        if ($this->form_validation->run() == FALSE) {
			
		$data['group']=$this->group_model->select(); 
				
            $this->load->view('layout/header', $data);
            $this->load->view('group/groupEdit', $data);
            $this->load->view('layout/footer', $data);
        } else {
            $data = array(
                'id' => $id,
                'groups' => $this->input->post('groups'),
				'group_type' => $this->input->post('group_type')
            );
            $this->group_model->add($data);
            $this->session->set_flashdata('msg', '<div class="alert alert-success text-left">Uniform Group updated successfully</div>');
            redirect('group/index');
        }
    }
    function add() {


        $current_date = date('Y-m-d');


        $data = [
            'student_id'      => $this->input->post('student_id'),
            'name' => $this->input->post('name'),
            'itemcode'        => $this->input->post('itemcode'),
            'description'     => $this->input->post('description'),
            'narration'     => $this->input->post('narration'),
            'originalqnty'=> $this->input->post('originalquantity'),
            'warehouse'=> $this->input->post('warehouse'),
            'quantity'        => $this->input->post('quantity'),
            'amount'          => $this->input->post('amount'),
            'payment'         => $this->input->post('payment'),
            'created_date'    => $current_date,
            'cost'         => $this->input->post('cost'),
            'discount'=> $this->input->post('discount')?: '0',
            'batch_num'=> $this->input->post('inv'),
        ];

         $this->group_model->addIntoUniformpostemp($data);

            $this->session->set_flashdata('msg', '<div class="alert alert-success text-left">Uniform saved successfully</div>');
            redirect('uniformpos');
        }
           
        
  function updatenow() {

     $updated =  $this->group_model->proccessData();

    echo 'success';

        }
      
    }

?>