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

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

class Uniformreceipting extends Admin_Controller {

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

    function index() {
        if (!$this->rbac->hasPrivilege('class', 'can_view')) {
            access_denied();
        }
        $this->session->set_userdata('top_menu', 'Uniform POS');
        $this->session->set_userdata('sub_menu', 'uniformreceipting/index');
        $data['title'] = 'Make Transactions ';
        $data['title_list'] = 'Uniform Receipting';
        
        // Set validation rules for the form fields
        $this->form_validation->set_rules('student_id', 'Student Id', 'required');
        $this->form_validation->set_rules('description', 'Description', 'required');
        $this->form_validation->set_rules('gl_account', 'Account Name', 'required');
        $this->form_validation->set_rules('payment', 'Payment Method', 'required');
        $this->form_validation->set_rules('inv_number', 'Reference', 'required');
        $this->form_validation->set_rules('amount', 'Amount', 'required');
        $this->form_validation->set_rules('name', 'Name', 'required');

        if ($this->form_validation->run() == FALSE) {
            // Form validation failed, continue to show the form
        } else {   
            // Get the form input data
            $student_id = $this->input->post('student_id');
            $name = $this->input->post('name');
            $gl_account = $this->input->post('gl_account');
            $amount = $this->input->post('amount');
            $discount = $this->input->post('discount');
            $total_amount = $amount - $discount;  // Calculate the total amount after discount
            
            // Calculate the customer change (the difference between amount and total_amount)
            $customer_change = $amount - $total_amount;  // This reflects the balance or change
            
            // Prepare the data to insert into the database
            $uniformposrec_array = array(
                'student_id' => $student_id,
                'name' => $this->input->post('name'),
                'gl_account' => $gl_account,
                'description' => $this->input->post('description'),
                'amount' => $amount,
                'discount' => $discount,
                'reference' => $this->input->post('inv_number'),
                'total_amount' => $total_amount,  // Store the discounted total amount
                'payment' => 0,  // Assuming no payment method selected yet
                'created_date' => date('Y-m-d'),
                'balance' => $customer_change  // Store the calculated customer change
            );

            // Add the data to the database
            $this->uniformpos_model->add($uniformposrec_array);
            
            // Set a success message and redirect
            $this->session->set_flashdata('msg', '<div class="alert alert-success text-left">Transaction made successfully</div>');
            redirect('uniformreceipting');
        }

        // Load required data for the view
        $data['payment_methods'] = $this->payment_methods_model->select();
        $data['code'] = $this->uniformitem_model->select();
        $data['uniform'] = $this->uniformpos_model->selectrec2();
        $data['itemlist'] = $this->uniformitem_model->select();
        $data['studentID'] = $this->group_model->selectSIDrec();
        $data['studenID'] = $this->student_model->selectstuden();
        $data['glledger_accounts'] = $this->glledger_accounts_model->select();
        $data['references'] = $this->uniformpos_model->get_references(); // Fetch references
        $data['settinglist'] = $this->setting_model->get();

        // Generate the receipt number
        if ($this->group_model->validateselectReceiptNo() != "null") {
            $data['rec'] = 'REC000' . ($this->group_model->validateselectReceiptNo() + 1);
        } else {
            $this->group_model->addReceiptNo();
            $data['rec'] = 'REC000' . ($this->group_model->validateselectReceiptNo() + 1);
        }

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

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

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

        $data['id'] = $id;
        $data['groups'] = $this->group_model->update($id);
        $data['typ'] = $this->types_model->select();  // Populate types model
        
        $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');
        $total_amount = $amount - $discount; // Calculate the discounted amount
    
        $data = [
            'student_id' => $this->input->post('student_id'),
            'name' => $this->input->post('name'),
            'gl_account' => $this->input->post('gl_account'),
            'description' => $this->input->post('description'),
            'narration' => $this->input->post('narration'),
            'amount' => $this->input->post('amount'),
            'discount' => $this->input->post('discount'),
            'reference' => $this->input->post('inv_number'),
            'total_amount' => $total_amount,  // Store the discounted total amount
            'payment' => $this->input->post('payment'),
            'created_date' => $current_date,
            'batch_num' => $this->input->post('rec'),
        ];
    
        $this->group_model->addIntoUniformpostemprec($data);
        $this->session->set_flashdata('msg', '<div class="alert alert-success text-left">Uniform saved successfully</div>');
        redirect('uniformreceipting');
    }

    function updatenow() {
        $updated = $this->group_model->proccessDatarec();
        echo 'success';
    }
}
?>
