<?php 
    $this->load->view('templates/header');
    $role = $this->session->userdata('user_role_id');
    // echo $role;
    if($voucher_type == 'PAYMENT'){
        $vm_voucher_type = 1;
        $vm_trans_type = 'DR';
    }
    else{
        $vm_voucher_type = 2;
        $vm_trans_type = 'CR';
    }
?>
<script>
     var link = "voucher";
        
    <?php
        if($voucher_type == 'PAYMENT'):
    ?>
            var sub_link = "voucher_payment";
    <?php
        elseif($voucher_type == 'RECEIPT'):
    ?>
            var sub_link = "voucher_receipt";
    <?php
        endif;
    ?>
</script>

<!-- Content Header (Page header) -->
<section class="content-header">
    <div class="row">
        <div class="col-md-4">
            <p style="font-size: 20px;font-weight: bold;">
                <?php
                    if(empty($master_data))
                    {
                        if($voucher_type == 'PAYMENT')
                            echo "Add New Payment ";
                        elseif($voucher_type == 'RECEIPT')
                            echo "Add New Receipt ";
                        else
                            echo "Add New Opening ";
                    }
                    else
                    {
                        if($voucher_type == 'PAYMENT')
                            echo "Edit Payment ";
                        elseif($voucher_type == 'RECEIPT')
                            echo "Edit Receipt ";
                        else
                            echo "Edit Opening ";
                    }
                ?>
                <small>Voucher</small>
            </p>
        </div>
        <div class="col-md-2 col-md-offset-6 col-xs-12">
        </div>
    </div>    
</section>


<!-- Main content -->
<section class="content">
	<div class="row">
		<div class="col-md-12 col-sm-12">
            <div class="alert_msg"></div>
			<form class="form-horizontal" id="voucher_form">
                <div class="panel panel-default">
                    <div class="panel-heading" style="font-size:12px;">
                        Voucher Details

                        <!-- CANCEL BUTTON -->                            
                        <a href="<?php echo base_url('voucher/voucherctrl/voucher_master?action=view');?>" type="button" class="btn btn-danger pull-right" tabindex="12">CANCEL</a>
                        <!-- CANCEL BUTTON -->
                        <?php if(empty($master_data)): ?>
                            <button type="button" class="btn btn-default pull-right" id="sbt_btn" onClick="add_payment_voucher(0)" style="margin-right: 1%;" disabled="disabled" tabindex="11">SUMBIT</button>
                        <?php else: ?>
                            <button type="button" class="btn btn-success pull-right" id="sbt_btn" onClick="add_payment_voucher(<?php echo $master_data[0]['vm_id'] ?>)" style="margin-right: 1%;" tabindex="11">UPDATE</button>
                        <?php endif; ?>
                        
                        <br/>
                        <br/>
                    </div>
                    <div class="panel-body remove-bottom">
                        <!-- <div class="box-body "> -->
                            <div class="form-group floating-form">
                                <!-- Voucher No -->
                                <div class="col-md-2 col-sm-2 floating-label">
                                    <label>Voucher No.</label>
                                    <input type="text" class="form-control floating-input" id="vm_entry_no" name="vm_entry_no" value="<?php echo empty($master_data) ? $entry_no : $master_data[0]['vm_entry_no']; ?>" placeholder=" " autocomplete="off" readonly />
                                </div>
                                <!-- Voucher No -->

                                <!-- Voucher Date -->
                                <div class="col-md-2 col-sm-2 floating-label">
                                    <label>Voucher Date</label>                                        
                                    <input type="text" class="form-control floating-input" id="vm_entry_date" name="vm_entry_date" value="<?php echo empty($master_data) ? date("d-m-Y") : date('d-m-Y', strtotime($master_data[0]['vm_entry_date'])); ?>" placeholder=" " autocomplete="off" readonly />
                                </div>
                                <!-- Voucher Date -->

                                <!-- Account -->
                                <div class="col-md-2 col-sm-2 floating-label">
                                    <label>Payment Mode</label>                                        
                                    <?php if(empty($master_data)): ?>
                                    <?php echo form_dropdown('', $account_data, '','id="vm_cash_acc_id" name="vm_cash_acc_id" class="form-control floating-select" tabindex="1" autofocus="on" onchange="set_chq_field()"');?> 
                                    <?php else: ?>
                                    <input type="text" class="form-control floating-input" id="vm_cash_acc_id" name="vm_cash_acc_id" value="<?php echo $master_data[0]['vm_cash_acc_id']; ?>" placeholder=" " autocomplete="off" readonly />
                                    <?php endif; ?>
                                    <input type="hidden" name="vm_voucher_type" id="vm_voucher_type" value="<?php echo $vm_voucher_type; ?>" />
                                </div>
                                <!-- Account -->

                                <!-- Group -->
                                <div class="col-md-2 col-sm-2 floating-label">
                                    <label>Group</label>                                        
                                    <?php if(empty($master_data)): ?>
                                    <?php echo form_dropdown('', $group_data, 0,'id="vm_grp_id" name="vm_grp_id" class="form-control floating-select" onChange="get_party_list();" tabindex="2" ');?>
                                    <?php else: ?>
                                    <input type="text" class="form-control floating-input" id="grp_name" name="grp_name" value="<?php echo $master_data[0]['grp_name']; ?>" placeholder=" " autocomplete="off" readonly />
                                    <input type="hidden" id="vm_grp_id" name="vm_grp_id" value="<?php echo $master_data[0]['vm_grp_id']; ?>" />
                                    <?php endif; ?>
                                </div>
                                <!-- Group -->

                                <!-- Client / Karigar Name -->
                                <div class="col-md-4 col-sm-4 floating-label">
                                    <label>Name</label>              
                                    <?php if(empty($master_data)): ?>                          
                                    <?php echo form_dropdown('', $party_data, 0,'id="vm_party_id" name="vm_party_id" class="form-control floating-select flexselect" onChange="get_party_payment_status();" tabindex="3" ');?> 
                                    <?php else: ?>
                                    <input type="text" class="form-control floating-input" id="party_name" name="party_name" value="<?php echo $master_data[0]['vm_grp_id'] == 5 ? $master_data[0]['client_name'].' - '.$master_data[0]['client_mob1'] : $master_data[0]['km_code'].' - '.$master_data[0]['km_mobile'] ; ?>" placeholder=" " autocomplete="off" readonly />
                                    <input type="hidden" id="vm_party_id" name="vm_party_id" value="<?php echo $master_data[0]['vm_party_id']; ?>" />
                                    <?php endif; ?>
                                </div>                        
                                <!-- Client / Karigar Name -->        

                            </div>
                            <div class="form-group floating-form">

                                <!-- Client / Karigar Balance -->
                                <div class="col-md-2 col-sm-2 floating-label">
                                    <label>Balance</label>                                        
                                    <input type="text" name="" class="form-control floating-input" id="vm_payment_status"  placeholder=" " value="" readonly />
                                    <label id="payment_status"></label>
                                </div>
                                <!-- Client / Karigar Balance -->

                                <!-- Amount To Pay / Receive -->
                                <div class="col-md-2 col-sm-2 floating-label">
                                    <label><?php echo $voucher_type == 'PAYMENT' ? 'Paid' : 'Receipt' ?> Amount</label>
                                    <input type="number" name="vm_amount" class="form-control floating-input" id="vm_amount" value="<?php echo empty($master_data) ? 0 : $master_data[0]['vm_amount']; ?>" placeholder=" " oninput="this.value = Math.abs(this.value)" tabindex="4" onkeyup="check_round_off_amt()" <?php echo empty($master_data) ? '' : 'readonly' ?> />
                                    <input type="hidden" name="vm_trans_type" class="form-control" id="vm_trans_type" value="<?php echo $vm_trans_type ?>" />
                                </div>
                                <!-- Amount To Pay / Receive -->

                                <!-- Round Off -->
                                <div class="col-md-2 col-sm-2 floating-label">
                                    <label>Round off</label>                                        
                                    <input type="number" name="vm_round_off" class="form-control floating-input" id="vm_round_off" value="<?php echo empty($master_data) ? 0 : $master_data[0]['vm_round_off']; ?>" onkeyup="check_round_off_amt();" placeholder=" " oninput="this.value = Math.abs(this.value)"  tabindex="5"
                                    <?php echo empty($master_data) ? '' : 'readonly' ?> />
                                </div>
                                <!-- Round Off -->

                                <!-- Cheque No -->
                                <div class="col-md-2 col-sm-2 floating-label">
                                    <label id="vm_cheque_no_label" >Cheque No</label>
                                    <input type="text" name="vm_cheque_no" class="form-control floating-input" id="vm_cheque_no"  value="<?php echo empty($master_data) ? '' : $master_data[0]['vm_cheque_no']; ?>" placeholder=" " autocomplete="off" value="" readonly="readonly" tabindex="6" />
                                </div>
                                <!-- Cheque No -->

                                <!-- Cheque Date -->
                                <div class="col-md-2 col-sm-2 floating-label">
                                    <label id="vm_cheque_date_label" >Cheque Date</label>
                                    <input type="text" name="vm_cheque_date" class="form-control floating-input" id="vm_cheque_date"  placeholder=" " autocomplete="off" value="<?php echo empty($master_data) ? '' : $master_data[0]['vm_cheque_date']; ?>" readonly="readonly" tabindex="7" />
                                </div>
                                <!-- Cheque Date -->

                                <!-- Notes -->
                                <div class="col-md-2 col-sm-2 floating-label">                                            
                                    <label>Notes</label>
                                    <textarea name="vm_notes" class="form-control floating-input" id="vm_notes"  placeholder=" " tabindex="8"><?php echo (empty($master_data)?"":$master_data[0]['vm_notes'])?></textarea>
                                </div>
                                <!-- Notes -->

                            </div>

                            <div class="form-group floating-form">

                                <!-- PREPARED BY -->
                            <div class="col-md-2 col-sm-12 floating-label">
                                <label for="inputEmail3">PREPARED BY</label>
                                <?php echo form_dropdown('', $user_data, empty($master_data) ? $_SESSION['user_id'] : $master_data[0]['vm_prepared_id'],'id="vm_prepared_id" name="vm_prepared_id" class="form-control floating-select" tabindex="9" ');?>
                            </div>
                            <!-- PREPARED BY -->
                            <!-- CHECKED BY -->
                            <div class="col-md-2 col-sm-12 floating-label">
                                <label for="inputEmail3">CHECKED BY</label>
                                <?php echo form_dropdown('', $user_data, empty($master_data) ? 0 : $master_data[0]['vm_checked_id'],'id="vm_checked_id" name="vm_checked_id" class="form-control floating-select" tabindex="10" ');?>
                            </div>
                            <!-- CHECKED BY -->

                            </div>
                        <!-- </div> -->
                    </div>
                </div>
                <div id="bal_purchase_details">
                    <?php if(!empty($trans_data)): ?>
                        <div class="panel panel-default">
                            <div class="panel-heading">
                                <h3 class="panel-title">Voucher Details</h3>
                            </div>
                            <div class="panel-body">
                                <table class="table">
                                    <tr>
                                        <?php if($master_data[0]['vm_grp_id'] == 5): ?>
                                            <th width="8%">Order No.</th>
                                            <th width="10%">Order Date</th>
                                            <th width="8%">Total Qty</th>
                                        <?php elseif($master_data[0]['vm_grp_id'] == 23): ?>
                                            <th width="8%">Hisab No.</th>
                                            <th width="10%">Hisab Date</th>
                                            <th width="8%">Total Job</th>
                                        <?php endif; ?>
                                        <th width="10%">Final Total</th>
                                        <th width="10%">Allocated Amt</th>
                                        <th >Allocated Round off Amt</th>
                                        <th width="10%">Adjust Amt</th>
                                        <th width="10%">Round off Amt</th>
                                        <th width="10%">Current Bal Amt</th>
                                        <th width="5%">NOD</th>
                                    </tr>
                                    <?php 
                                        foreach ($trans_data as $key => $value): 
                                        $current_bal = $value['vt_total_amt'] - ($value['om_allocated_amt'] + $value['hm_allocated_amt']) - $value['vt_adjust_round_off'];
                                        $diff = strtotime($value['vt_entry_date']) - strtotime($master_data[0]['vm_entry_date']);
                                        $nod = abs(round($diff / 86400));
                                        $voucher_cnt = 1;
                                    ?>

                                        <tr id="bill_cnt_<?php echo $voucher_cnt ?>">
                                            <!-- Entry No -->
                                            <td>
                                                <input type="hidden" name="vt_id[]" id="vt_id_<?php echo $voucher_cnt ?>" value="<?php echo $value['vt_id'] ?>"/>
                                                <input type="hidden" name="_id[]" id="_id_<?php echo $voucher_cnt ?>" value="<?php echo $master_data[0]['vm_grp_id'] == 5 ? $value['vt_om_id'] : $value['vt_hm_id'] ; ?>"/>
                                                <input type="text" class="form-control" name="entry_no[]" id="entry_no_<?php echo $voucher_cnt ?>" value="<?php echo $value['vt_entry_no'] ?>" readonly/></td>
                                            </td>
                                            <!-- Entry No -->


                                            <!-- Entry Date -->
                                            <td>
                                                <input type="text" class="form-control" name="entry_date[]" id="entry_date_<?php echo $voucher_cnt ?>" value="<?php echo date('d-m-Y', strtotime($value['vt_entry_date'])); ?>" readonly/>
                                            </td>
                                            <!-- Entry Date -->

                                            <!-- Total Qty -->
                                            <td>
                                                <input type="number" class="form-control" name="total_qty[]" id="total_qty_<?php echo $voucher_cnt ?>" value="<?php echo $value['vt_total_qty'] ?>" readonly/>
                                            </td>
                                            <!-- Total Qty -->

                                            <!-- Total Amt -->
                                            <td>
                                                <input type="number" class="form-control" name="total_amt[]" id="total_amt_<?php echo $voucher_cnt ?>" value="<?php echo $value['vt_total_amt']; ?>" readonly/>
                                            </td>
                                            <!-- Total Amt -->

                                            <!-- Allocated Amt -->
                                            <td>
                                                <input type="text" class="form-control" name="allocated_amt[]" id="allocated_amt_<?php echo $voucher_cnt ?>" value="<?php echo ($value['om_allocated_amt'] + $value['hm_allocated_amt']); ?>" readonly/>
                                            </td>
                                            <!-- Allocated Amt -->

                                            <!-- Allocated Round Off Amt -->
                                            <td>
                                                <input type="text" class="form-control" name="adjust_round_off_old[]" id="adjust_round_off_old_<?php echo $voucher_cnt ?>" value="<?php echo $value['vt_adjust_round_off'] ?>" readonly/>
                                            </td>
                                            <!-- Allocated Round Off Amt -->

                                            
                                            <!-- Adjust Amt -->
                                            <td>
                                                <input type="number" class="form-control" name="adjust_amt[]" id="adjust_amt_<?php echo $voucher_cnt ?>" value="0" oninput="this.value = Math.abs(this.value)" <?php echo $current_bal > 0 ? '' : 'style="display:none;"' ?>  />
                                            </td>
                                            <!-- Adjust Amt -->

                                            <!-- Adjust Round off amt -->
                                            <td>
                                                <input type="number" class="form-control" name="adjust_round_off[]" id="adjust_round_off_<?php echo $voucher_cnt ?>" value="0" oninput="this.value = Math.abs(this.value)" <?php echo $current_bal > 0 ? '' : 'style="display:none;"' ?> />
                                            </td>
                                            <!-- Adjust Round off amt -->
                                            
                                            <!-- Current Bal -->
                                            <td>
                                                <input type="text" class="form-control" id="current_bal_amt_<?php echo $voucher_cnt ?>" value="<?php echo $current_bal ?>" readonly/>
                                            </td>
                                            <!-- Current Bal -->

                                            <!-- NOD -->
                                            <td>
                                                <input type="text" class="form-control" id="current_bal_amt_<?php echo $voucher_cnt ?>" value="<?php echo $nod; ?>" readonly/>
                                            </td>
                                            <!-- NOD -->
                                        </tr>
                                    <?php endforeach; ?>
                                </table>
                            </div>
                        </div>
                    <?php endif; ?>
                </div>
            </form>
		</div>
	</div>
</section>
<!-- Main content -->


<?php 
    $this->load->view('templates/footer');
?>
<script src="<?php echo assets('js/voucher/voucher.js')?>"></script>

</body>
</html>