<?php

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

class Uniselling extends Admin_Controller {

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

    function index() {
        if (!$this->rbac->hasPrivilege('class', 'can_view')) {
            access_denied();
        }
        $this->session->set_userdata('top_menu', 'Account Type');
        $this->session->set_userdata('sub_menu', 'uniselling/index');
        $data['title'] = 'Add Item';
        $data['title_list'] = 'Processed Transactions';

      //  $this->form_validation->set_rules('student_id', 'Student Id', 'required');
		//$this->form_validation->set_rules('name', 'Account Type', 'required');
		//$this->form_validation->set_rules('description', 'Description', 'required');
		$this->form_validation->set_rules('quantity', 'Quantity', 'required');
       $this->form_validation->set_rules('selling_price', 'Selling Price', 'required');
      //  $this->form_validation->set_rules('method', 'Payment Method', 'required');
      

        if ($this->form_validation->run() == FALSE) {
            
        } else {	
            $student_id = $this->input->post('student_id'); //stud id 1
            $itemcode = $this->input->post('itemcode');	//1	
           // $itemcode2=$itemcode;
           $price = $this->input->post('selling_price');
            $quantity = $this->input->post('quantity');
          $payment_methods = $this->input->post('payment_methods');//p1
            $total = $price*$quantity;
            $active = 1;

            ////////////////////////// junk
         // $itemcode2= $row->itemcode;


            $this->db->select('*');
            $this->db->from('uniformitem');//1
            $this->db->where('itemcode', $itemcode);
            $query = $this->db->get();
            $result = $query->row();
           echo $result->sellingprice;
            //echo $sellingprice = 6;
            
                                            
                                        
            //////////////////////////
            $this->db->select('*');//2
            $this->db->from('uniformitem');
            $this->db->where('itemcode', $itemcode);
            ///////////////////////////////////////////////////////////////
            		


                  //CODE FOR GENERATING RECEIPT NUMBERS AND INSERTING THEM TO THE DATABASE
                    $value22='';
                    //Query to fetch last inserted invoice number
                    $this->db->select('used');
                    $this->db->from('pos');
                    $query12 = $this->db->get();
                    $result12 = $query12->num_rows();    // CODE FOR COUNTING NUMBER OF ROWS IN TOTAL
                                    //$get_result = $query1->row();		//CODE FOR PULLING THE RECEIPT NUMBERS
                                    
				if($result12 > 0) {
					
						$value22 =$result12;
						//$value2 = substr($value2, 10, 13);//separating numeric part
						$value22 = $value22 + 1;//Incrementing numeric part
						$value22 = "UNI INV" . sprintf('%04s', $value22);//concatenating incremented value
						$value1 = $value22; 						
				} 
				else {
					$value22 = "UNIF INV0001";
					$value1 = $value22;
									
				}
							
				$inv_number_st = $value1;			

            ///////////////////////////////////////////////////////////////////
            $deprt_array = array(
               'student_id' => $this->input->post('student_id'),
                //'name' => $this->input->post('name'),	
                                'itemcode' => $this->input->post('itemcode'),						
                                //'description' => $this->input->post('description'),
                                'quantity' => $this->input->post('quantity'),
                                'selling_price' => $this->input->post('selling_price'),
                                'total' => $total,
                                'invoice' => $inv_number_st,
                                'payment_methods' => $this->input->post('payment_methods'),//payment method p2
                                'created_date' => date('Y-m-d'),				
                                'active' => 1				
                            );
                            $dprt_array = array(
                                'student_id' => $this->input->post('student_id'),
                //'name' => $this->input->post('name'),	
                                'itemcode' => $this->input->post('itemcode'),						
                                //'description' => $this->input->post('description'),
                                'quantity' => $this->input->post('quantity'),
                                'selling_price' => $this->input->post('selling_price'),
                                'total' => $total,
                                'invoice' => $inv_number_st,
                                'payment_methods' => $this->input->post('payment_methods'),//payment method p2
                                'created_date' => date('Y-m-d'),				
                                'active' => 1				
             );
             $inv_num = 1;
             $inv_num = 1;
             $d_array = array(
                'student_id' => $this->input->post('student_id'),						
                 'invoice_number' => $inv_num,
                 'date_created' => date('Y-m-d'),				
                 'used' => 1
                 				
             );
			$this->gl_accounts_types_model->add($deprt_array);
            $this->gl_accounts_types_model->updateU($dprt_array);
            $this->gl_accounts_types_model->geninvo($d_array);
            $this->session->set_flashdata('msg', '<div class="alert alert-success text-left">Item added successfully</div>');
            redirect('uniselling');
        }  
        $data['stud_id']=$this->student_model->select(); //stud id	2
         $data['uniselling']=$this->gl_accounts_types_model->select(); // change the model 
       //  $data['payment_methods']=$this->payment_methods_model->select(); //payment methods
         $data['stud_id']=$this->student_model->select(); 	 
         $data['code']=$this->uniformitem_model->select();//3
         $data['sellingprice']=$this->uniformitem_model->select();//s3
         $data['itemlist']=$this->uniformitem_model->select();//4
         $data['payment_methods']=$this->payment_methods_model->select();//payment method 2
         $data['gtotal']=$this->gl_accounts_types_model->getTotal($gtotal);  

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

    function delete($id) {
        if (!$this->rbac->hasPrivilege('class', 'can_delete')) {
            access_denied();
        }
        $data['title'] = 'Cashbook_details';
        $this->gl_accounts_types_model->remove($id);
        redirect('uniselling');
    }

    function sub($id) {
       
        $inv_num = 1;
        $d_array = array(
           'student_id' => $this->input->post('student_id'),						
            'invoice_number' => $inv_num,
            'date_created' => date('Y-m-d'),				
            'used' => 1
                            
        );
       $this->gl_accounts_types_model->geninvo($d_array);
    }

    function edit($id) {
        $this->session->set_userdata('top_menu', 'Account Type');
        $this->session->set_userdata('sub_menu', 'uniselling/index');
		
        $data['uniselling']=$this->uniselling_model->select(); 
        $data['title'] = 'Edit Account Type';
        $data['id'] = $id;

        $data['uniselling'] = $this->uniselling_model->update($id);
				
		$this->form_validation->set_rules('name', 'Account Type', 'required');
		$this->form_validation->set_rules('description', 'Description', 'required');
		
        if ($this->form_validation->run() == FALSE) {
			
            $this->load->view('layout/header', $data);
            $this->load->view('uniselling/gl_accounts_typesEdit', $data);
            $this->load->view('layout/footer', $data);
			
        } else {
            $data = array(
                'id' => $id,
                'name' => $this->input->post('name'),
                'description' => $this->input->post('description'),
            );
            $this->uniselling_model->add($data);
            $this->session->set_flashdata('msg', '<div class="alert alert-success">Account Type updated  successfully</div>');
            redirect('uniselling/index');
        }
    }
    function printPos() { //add incre code here
        if (!$this->rbac->hasPrivilege('class', 'can_view')) {
            access_denied();
        }
		        $setting_result = $this->setting_model->get();
                $data['settinglist'] = $setting_result;
		 

                $inv_num = 1;
                $inv_num = 1;
                $d_array = array(
                   'student_id' => $this->input->post('student_id'),						
                    'invoice_number' => $inv_num,
                    'date_created' => date('Y-m-d'),				
                    'used' => 1
                                    
                );
               
                
        $data['gtotal']=$this->gl_accounts_types_model->getTotal($gtotal);  
		$data['uniform']=$this->gl_accounts_types_model->select(); //where the problem is 
        $this->gl_accounts_types_model->geninvo($d_array);
        $this->load->view('print/printPos', $data);
     }
    //  function printFeesByGroupArray() { //code to retrieve name 
    //     $setting_result = $this->setting_model->get();
    //     $data['settinglist'] = $setting_result;
    //     $record = $this->input->post('data');
    //     $record_array = json_decode($record);
    //     $fees_array = array();
    //     foreach ($record_array as $key => $value) {
    //         $fee_groups_feetype_id = $value->fee_groups_feetype_id;
    //         $fee_master_id = $value->fee_master_id;
    //         $fee_session_group_id = $value->fee_session_group_id;
    //         $feeList = $this->studentfeemaster_model->getDueFeeByFeeSessionGroupFeetype($fee_session_group_id, $fee_master_id, $fee_groups_feetype_id);
    //         $fees_array[] = $feeList;
    //     }
    //     $data['feearray'] = $fees_array;
    //     $this->load->view('print/printPos', $data);
    // }

     function getByModule() {//tues
        $module = $this->input->get('class_id');
		
		if($module == 'Learner'){
			$data = $this->gl_accounts_types_model->getPrice();
			echo json_encode($data);			
		}		
		
			
          
         $data['journal_accounts']=$this->journal_accounts_model->select();  
         $data['jornal']=$this->gl_accounts_types_model->select();  

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

?>