<?php $currency_symbol = $this->customlib->getSchoolCurrencyFormat(); ?>
<!-- Content Wrapper. Contains page content -->
<div class="content-wrapper">

    <section class="content-header">
        <h1>
            <i class="fa fa-mortar-board"></i>Uni Point of Sale</h1>
						
	    </section>

    <!-- Main content -->
    <section class="content">
        <div class="row">
            <?php
            if ($this->rbac->hasPrivilege('class', 'can_add')) {
                ?>  
                <div class="col-md-4">
                    <!-- Horizontal Form -->
                    <div class="box box-primary">
                        <div class="box-header with-border">
                            <h3 class="box-title"><?php echo $title;       ?></h3>
                        </div><!-- /.box-header -->
                        <form id="form1" action="<?php echo base_url() ?>uniselling"  method="POST" accept-charset="utf-8">
                            <div class="box-body">
                                <?php if ($this->session->flashdata('msg')) { ?>
                                    <?php echo $this->session->flashdata('msg') ?>
                                <?php } ?>
                                <?php
                                if (isset($error_message)) {
                                    echo "<div class='alert alert-danger'>" . $error_message . "</div>";
                                }    
                                ?>
                                <?php echo $this->customlib->getCSRF(); ?> 
                                <th>Inv</th>
                                
                                <tr>
                                            <td class="mailbox-name"><?php echo $row->created_date;?></td>
                                            
                                                <td><?php 
                                                															
															  ///////////////////////////////////////////////////////////////
            				//CODE FOR GENERATING RECEIPT NUMBERS AND INSERTING THEM TO THE DATABASE
				$value2='';
				//Query to fetch last inserted invoice number
				$this->db->select('inv_number');
				$this->db->from('supplier_invoice');
				$query1 = $this->db->get();
				$result1 = $query1->num_rows();    // CODE FOR COUNTING NUMBER OF ROWS IN TOTAL
				//$get_result = $query1->row();		//CODE FOR PULLING THE RECEIPT NUMBERS
				
				if($result1 > 0) {
					
						$value2 =$result1;
						//$value2 = substr($value2, 10, 13);//separating numeric part
						$value2 = $value2 + 1;//Incrementing numeric part
						$value2 = "INV" . sprintf('%04s', $value2);//concatenating incremented value
						$value = $value2; 						
				} 
				else {
					$value2 = "INV0001";
					$value = $value2;
									
				}
							
				$inv_number = $value;	


                //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 = "UNIF INV" . sprintf('%04s', $value22);//concatenating incremented value
						$value1 = $value22; 						
				} 
				else {
					$value22 = "UNIF INV0001";
					$value1 = $value22;
									
				}
							
				$inv_number_st = $value1;			


                                                echo "<div style='color:purple;fontsize:14px;'> ".$inv_number_st."</div>";?></td>
                                       </tr>       
                                <!-- <div class="form-group">
                                    <label for="colour">Invoice Number</label>
                                    <input style='color:purple;fontsize:14px;' autofocus="" id="" name="" placeholder="UNIF INV00001" type="text" class="form-control"  value="<?php echo set_value('invnumber'); ?>" />
                                    <span class="text-danger"><?php echo form_error('quantity'); ?></span>
                                </div> -->
                                <div class="form-group">
                                    <label for="exampleInputEmail1">Date<?php echo $this->lang->line(''); ?></label><small class="req"> *</small>
                                    <input autofocus="" id="inv_date" name="inv_date" placeholder="" type="date"
                                    class="form-control" value="<?php echo date("Y-m-d")?>" />
                                    <span class="text-danger"><?php echo form_error('created_date'); ?></span>
                                </div>

                                <div class="form-group">
                                <label for="student_id">Student Id</label><small class="req"> *</small> <!--stud-->
                                <select id="student_id" name="student_id" placeholder="" type="text"
                                    class="form-control">
                                    <option value="select"><?php echo $this->lang->line('select') ?></option>
                                    <?php 
												foreach ($stud_id->result() as $student_id) {
												?>
                                    <option value="<?php echo $student_id->admission_no; ?>">
                                        <?php echo $student_id->admission_no; ?></option>
                                    <?php }
											?>
                                </select>
                                <span class="text-danger"><?php echo form_error('student_id'); ?></span>
                            </div>
                                
<!-- <div class="form-group">
                                    <label for="colour">Student Name</label><small class="req"> *</small>
                                    <input autofocus="" id="name" name="name" placeholder="" type="text" class="form-control"  value="<?php echo set_value('name'); ?>" />
                                    <span class="text-danger"><?php echo form_error('name'); ?></span>
                                </div>-->
                                <div class="form-group">
                                <label for="itemcode">Item Code</label><small class="req"> *</small> <!--item code-->
                                <select id="itemcode" name="itemcode" placeholder="" type="text" class="form-control">
                                    <option value="select"><?php echo $this->lang->line('select') ?></option>
                                    <?php 
												foreach ($code->result() as $item) {
												?>
                                    <option value="<?php echo $item->itemcode; ?>"> <?php echo $item->itemcode; ?>
                                    </option>
                                    <?php }
											?>
                                </select>
                                <span class="text-danger"><?php echo form_error('itemcode'); ?></span>
                            </div>	
                            
                           							
								<!--<div class="form-group">
                                    <label for="colour">Description</label><small class="req"> *</small>
                                    <input autofocus="" id="description" name="description" placeholder="" type="text" class="form-control"  value="<?php echo set_value('description'); ?>" />
                                    <span class="text-danger"><?php echo form_error('description'); ?></span>
                                </div>-->
                              
                                <div class="form-group">
                                    <label for="colour">Quantity</label><small class="req"> *</small>
                                    <input autofocus="" id="quantity" name="quantity" placeholder="" type="text" class="form-control"  value="<?php echo set_value('quantity'); ?>" />
                                    <span class="text-danger"><?php echo form_error('quantity'); ?></span>
                                </div>
                               <div 
                                 class="form-group">
                                    <label for="colour">Selling Price</label><small class="req"> *</small>
                                  
                                    <input autofocus="" id="selling_price" name="selling_price" placeholder="" type="text" class="form-control"  value="<?php echo set_value('selling_price'); ?>" />
                                    <span class="text-danger"><?php echo form_error('selling_price'); ?></span>
                                </div>
                                <div class="form-group">
                                <label for="payment">Payment Method</label><small class="req"> *</small>
                                <select id="payment_methods" name="payment_methods" placeholder="" type="text" class="form-control"> 
                                    <option value="select"><?php echo $this->lang->line('select') ?></option>
                                    <?php 
												foreach ($payment_methods->result() as $method) {
												?>
                                    <option value="<?php echo $method->payment_methods; ?>"> <?php echo $method->payment_methods; ?></option>
                                    <?php }
											?>
                                </select>
                                <span class="text-danger"><?php echo form_error('payment'); ?></span>
                            </div>	
                                
<!-- <div class="form-group">
                                    <label for="colour">Price</label>
                                    <?php

                                    $itemcode2= $row->itemcode;
                                    
                                    
                                    $this->db->select('*');
                                    $this->db->from('uniformitem');//1
                                    $this->db->where('itemcode', $itemcode2);
                                    $query = $this->db->get();
                                    $result = $query->row();
                                    echo $result->sellingprice; 
                                    
                                    //$price = $this->input->post('selling_price');
                                      //    $quantity = $this->input->post('quantity');
                                            // $total = $price*$quantity; ?>
                                    <input autofocus="" id="" name="total" placeholder="" type="text" class="form-control"  value="<?php echo set_value($selling_price); ?>" />
                                    <span class="text-danger"><?php echo form_error('selling_price'); ?></span>
                                </div>
                              -->
                            

                                									
                            </div><!-- /.box-body -->
                            <div class="box-footer">
                                <button type="submit" class="btn btn-info pull-right"><?php echo $this->lang->line('save'); ?></button>
                                <button type="submit" class="btn btn-info pull-left"><?php echo $this->lang->line('submit'); ?></button>
                            </div>
                          
                        </form>
                    </div>
                </div><!--/.col (right) -->
                <!-- left column -->
            <?php } ?>
            <div class="col-md-<?php
            if ($this->rbac->hasPrivilege('class', 'can_add')) {
                echo "8";
            } else {
                echo "12";
            }
            ?>">
                <!-- general form elements -->
                <div class="box box-primary">
                    <div class="box-header ptbnull">
                        <h3 class="box-title titlefix"><?php echo $title_list; ?></h3>
                        <div class="box-tools pull-right">
                        </div><!-- /.box-tools -->
                    </div><!-- /.box-header -->
                    <tr> <!--print button-->
													 <td>           
															<div class="btn-group"> 														
																<button  class="btn btn-xs btn-info printInv"                  
																		 title="<?php echo $this->lang->line('print'); ?>"
																		 ><i class="fa fa-print"></i> Print Invoice</button>
															</div> 
														</td>
												</tr>
                    <div class="box-body">
                        <div class="table-responsive mailbox-messages">
                            <div class="download_label"><?php echo $title_list; ?></div>
                            <table class="table table-striped table-bordered table-hover example">
                                <thead>
                                    <tr>
                                        <th>Date</th>
                                        <!-- <th>Inv</th> -->
                                        <th>Student ID</th>
                                        <th>Student Name</th>
                                        <th>Item Code</th>
                                        <th>Description</th>
                                        <th>Quantity</th>
                                        <th>Selling Price</th>
                                        <th>Method</th>
                                        <th>Total</th>
                                        <th class="text-right"><?php echo $this->lang->line('action'); ?></th>
                                    </tr>
                                </thead>
                                <tbody>
                                    
                                <?php  
										 foreach ($uniselling->result() as $row)  
										 {   
                                        

                                        ?>
                                        <tr>
                                            <td class="mailbox-name"><?php echo $row->created_date;?></td>
                                            
                                                <!-- <td><?php 
                                                															
															$invoice_number = $row->id;		
															
															$invoice_number_new = "UNIF INV" . sprintf('%04s', $invoice_number);

                                                echo "<div style='color:purple;fontsize:14px;'> ".$invoice_number_new."</div>";?></td>
                                               -->
                                            <td class="mailbox-name"><?php echo $row->student_id;?></td>
                                            <td class="mailbox-name"><?php 

$admission_no= $row->student_id;


$this->db->select('*');
$this->db->from('students');//s1
$this->db->where('admission_no', $admission_no);
$query = $this->db->get();
$result = $query->row();
echo $result->firstname.' '. $result->lastname;

?></td> 
                                            <td class="mailbox-name"><?php echo $row->itemcode;?></td>
                                            <td class="mailbox-name"><?php 

$itemcode2= $row->itemcode;


$this->db->select('*');
$this->db->from('uniformitem');//1
$this->db->where('itemcode', $itemcode2);
$query = $this->db->get();
$result = $query->row();
echo $result->itemgroups.'/'.$result->groupsize.'-'.$result->valuesize.'/'.$result->groupstyle.'-'.$result->stylevalue.'/'.$result->colourgroup.'-'.$result->colourvalue.'/'.$result->gendergroup.'-'.$result->gendervalue;

?></td> 
                                            <td class="mailbox-name"><?php echo $row->quantity;?></td>
                                            <td class="mailbox-name"><?php 

$itemcode2= $row->itemcode;


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

?></td> 
                                            <!--<td class="mailbox-name"><?php echo $row->selling_price;?></td>-->
                                            <td class="mailbox-name"><?php echo $row->payment_methods;?></td>
                                            <td class="mailbox-name"><?php echo $row->total;?></td>
                                            <td class="mailbox-date pull-right">
                                                <?php
                                                if ($this->rbac->hasPrivilege('types', 'can_edit')) {
                                                    ?>  
                                                    <a href="<?php echo base_url(); ?>uniselling/sub/<?php echo $row->id; ?>" class="btn btn-default btn-xs"  data-toggle="tooltip" title="<?php echo $this->lang->line('sub'); ?>">
                                                        <i class="fa fa-pencil"></i>
                                                    </a>
                                                    <?php
                                                }
                                                if ($this->rbac->hasPrivilege('types', 'can_delete')) {
                                                    ?>  
                                                    <a href="<?php echo base_url(); ?>uniselling/delete/<?php echo $row->id; ?>"class="btn btn-default btn-xs"  data-toggle="tooltip" title="<?php echo $this->lang->line('delete'); ?>" onclick="return confirm('<?php echo $this->lang->line('delete_confirm') ?>');">
                                                        <i class="fa fa-remove"></i>
                                                    </a>
                                                <?php } ?>
                                            </td>
                                        </tr>
									<?php } 
                                 
                                    
                                    ?>

                                    

                                    <!--- ROW IRI NDIRO RINE MAGRAND TOTALS ACHONA-->
                                    

                                    <tr class="box box-solid total-bg">
                                        <td align="left" ></td>
                                        <td align="left" ></td>
										<td align="left" ></td>
                                        <td align="left" ></td>
                                        <td align="left" class="text text-left" ></td>
                                        <td align="left" class="text text-left" ></td>
                                        <td align="left" class="text text-left" ></td>
                                        <td align="left" class="text text-left" ></td>
                                        <td class="text text-right">Total</td>
                                        <td class="text text-right"><?php
                                            echo ($currency_symbol . number_format($gtotal, 2, '.', ''));
                                            ?></td>  
                                        
                                           
                                    </tr>
                                </tbody>
                            </table><!-- /.table -->



                        </div><!-- /.mail-box-messages -->
                    </div><!-- /.box-body -->
                </div>
            </div><!--/.col (left) -->
            <!-- right column -->

        </div>
        <div class="row">
            <!-- left column -->

            <!-- right column -->
            <div class="col-md-12">

            </div><!--/.col (right) -->
        </div>   <!-- /.row -->
    </section><!-- /.content -->
</div><!-- /.content-wrapper -->


<!---trial 2ss--->


<script type="text/javascript"> //check this block for errors
    $(document).ready(function () {
        $(document).on('click', '.printDoc', function () {
            var main_invoice = $(this).data('main_invoice');
            var sub_invoice = $(this).data('sub_invoice');
            var student_session_id = '<?php echo $student_session_id; ?>';
            $.ajax({
                url: '<?php echo site_url("studentfee/printFeesByName") ?>',
                type: 'post',
                data: {'student_session_id': student_session_id, 'main_invoice': main_invoice, 'sub_invoice': sub_invoice},
                success: function (response) {
                    Popup(response);
                }
            });
        });																			
        $(document).on('click', '.printInv', function () {
            $.ajax({
                url: '<?php echo site_url("uniselling/printPos/") ?>',  //links to the html with template
                type: 'post',
                success: function (response) {
                    Popup(response);
                }
            });
        });
    });
</script>

<!---trial 3-->

<script>
var base_url = '<?php echo base_url() ?>';
    function Popup(data)
    {

        var frame1 = $('<iframe />');
        frame1[0].name = "frame1";
        frame1.css({"position": "absolute", "top": "-1000000px"});
        $("body").append(frame1);
        var frameDoc = frame1[0].contentWindow ? frame1[0].contentWindow : frame1[0].contentDocument.document ? frame1[0].contentDocument.document : frame1[0].contentDocument;
        frameDoc.document.open();
        //Create a new HTML document.
        frameDoc.document.write('<html>');
        frameDoc.document.write('<head>');
        frameDoc.document.write('<title></title>');
        frameDoc.document.write('<link rel="stylesheet" href="' + base_url + 'backend/bootstrap/css/bootstrap.min.css">');
        frameDoc.document.write('<link rel="stylesheet" href="' + base_url + 'backend/dist/css/font-awesome.min.css">');
        frameDoc.document.write('<link rel="stylesheet" href="' + base_url + 'backend/dist/css/ionicons.min.css">');
        frameDoc.document.write('<link rel="stylesheet" href="' + base_url + 'backend/dist/css/AdminLTE.min.css">');
        frameDoc.document.write('<link rel="stylesheet" href="' + base_url + 'backend/dist/css/skins/_all-skins.min.css">');
        frameDoc.document.write('<link rel="stylesheet" href="' + base_url + 'backend/plugins/iCheck/flat/blue.css">');
        frameDoc.document.write('<link rel="stylesheet" href="' + base_url + 'backend/plugins/morris/morris.css">');
        frameDoc.document.write('<link rel="stylesheet" href="' + base_url + 'backend/plugins/jvectormap/jquery-jvectormap-1.2.2.css">');
        frameDoc.document.write('<link rel="stylesheet" href="' + base_url + 'backend/plugins/datepicker/datepicker3.css">');
        frameDoc.document.write('<link rel="stylesheet" href="' + base_url + 'backend/plugins/daterangepicker/daterangepicker-bs3.css">');
        frameDoc.document.write('</head>');
        frameDoc.document.write('<body>');
        frameDoc.document.write(data);// what is shown on the pop
        frameDoc.document.write('</body>');
        frameDoc.document.write('</html>');
        frameDoc.document.close();
        setTimeout(function () {
            window.frames["frame1"].focus();
            window.frames["frame1"].print();
            frame1.remove();
        }, 500);


        return true;
    }
</script>

<!----------------------------------------------print codes------------------------------------------------------------->

<script type="text/javascript">
    $(document).ready(function () {
        $(document).on('click', '.printSelected', function () {
            var array_to_print = [];
            $.each($("input[name='fee_checkbox']:checked"), function () {
                var section_id = $(this).data('section_id');
                var class_id = $(this).data('class_id');
                var student_id = $(this).data('student_id');
                item = {}
                item ["section_id"] = section_id;
                item ["class_id"] = class_id;
                item ["student_id"] = student_id;

                array_to_print.push(item);
            });
            if (array_to_print.length == 0) {
                alert("no record selected");
            } else {
                $.ajax({
                    url: '<?php echo site_url("studentfee/printFeesStatementByGroup") ?>',
                    type: 'post',
                    data: {'data': JSON.stringify(array_to_print)},
                    success: function (response) {
                        Popup(response);
                    }
                });
            }
        });
    });


    $(function () {
        $(document).on('change', "#discount_group", function () {
            var amount = $('option:selected', this).data('disamount');

            var balance_amount = (parseFloat(fee_amount) - parseFloat(amount)).toFixed(2);
            if (typeof amount !== typeof undefined && amount !== false) {
                $('div#myFeesModal').find('input#amount_discount').prop('readonly', true).val(amount);
                $('div#myFeesModal').find('input#amount').val(balance_amount);

            } else {
                $('div#myFeesModal').find('input#amount').val(fee_amount);
                $('div#myFeesModal').find('input#amount_discount').prop('readonly', false).val(0);
            }

        });
    });
</script>
<!------------------------------------------------------------------------------------------------------------------->
<!-- <script type="text/javascript">
function getSectionByClass(class_id, section_id) {
    if (class_id != "" && section_id != "") {
        $('#section_id').html("");
        var base_url = '<?php echo base_url() ?>';
        var div_data = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
        $.ajax({
            type: "GET",
            url: base_url + "cashbook/getByModule",
            data: {
                'class_id': class_id
            },
            dataType: "json",
            success: function(data) {
                $.each(data, function(i, obj) {
                    var sel = "";
                    if (section_id == obj.section_id) {
                        sel = "selected";
                    }
                    if (class_id == 'Learner') {
                        div_data += "<option value=" + obj.firstname + " " + obj.lastname + ">" +
                            obj.firstname + " " + obj.lastname + "</option>";
                    } else if (class_id == 'Supplier') {
                        div_data += "<option value=" + obj.name + ">" + obj.name + "</option>";
                    } else if (class_id == 'GL') {
                        if (class_id == 'GL') {
                            div_data += "<option value=" + obj.glname + ">" + obj.gl_code + " > " +
                                obj.glname + "</option>";
                        }
                        if (class_id == 'GL') {
                            div_data += "<option value=" + obj.subname + ">" + obj.gl_code + " > " +
                                obj.glsub_code + " > " + obj.subname + "</option>";
                        }

                    } else if (class_id == 'Customer') {
                        div_data += "<option value=" + obj.name + ">" + obj.name + "</option>";
                    } else {
                        div_data += "<option value=" + obj.name + " " + sel + ">" + obj.name +
                            "</option>";
                    }
                });
                $('#section_id').append(div_data);
            }
        });
    }
}

$(document).ready(function() {
    var class_id = $('#class_id').val();
    var section_id = '<?php echo set_value('section_id') ?>';
    getSectionByClass(class_id, section_id);
    $(document).on('change', '#class_id', function(e) {
        $('#section_id').html("");
        var class_id = $(this).val();
        var base_url = '<?php echo base_url() ?>';
        var div_data = '<option value=""><?php echo $this->lang->line('select'); ?></option>';
        $.ajax({
            type: "GET",
            url: base_url + "cashbook/getByModule",
            data: {
                'class_id': class_id
            },
            dataType: "json",
            success: function(data) {
                $.each(data, function(i, obj) {
                    if (class_id == 'Learner') {
                        div_data += "<option value=" + obj.firstname + " " + obj
                            .lastname + ">" + obj.firstname + " " + obj.lastname +
                            "</option>";
                    } else if (class_id == 'Supplier') {
                        div_data += "<option value=" + obj.name + ">" + obj.name +
                            "</option>";
                    } else if (class_id == 'GL') {
                        if (class_id == 'GL') {
                            div_data += "<option value=" + obj.glname + ">" + obj
                                .gl_code + " > " + obj.glname + "</option>";
                        }
                        if (class_id == 'GL') {
                            div_data += "<option value=" + obj.subname + ">" + obj
                                .gl_code + " > " + obj.glsub_code + " > " + obj
                                .subname + "</option>";
                        }

                    } else if (class_id == 'Customer') {
                        div_data += "<option value=" + obj.name + ">" + obj.name +
                            "</option>";
                    } else {
                        div_data += "<option value=" + obj.name + ">" + obj.name +
                            "</option>";
                    }

                });
                $('#section_id').append(div_data);
            }
        });
    });
});
</script> -->