<style>
        .table1{}
        .table1 td{
            border-top: none !important;
            padding:1px 8px 1px 8px !important;
        }

    .datepicker{top:550px !important;}

    .form-control .plain{
        border:none !important;
        background-color:transparent !important;
        height:25px !important;
    }
    
</style>  
<div class="wrapper">
            <div class="container-full">
                <!-- Page-Title -->
                <div class="row">
                    <div class="col-sm-12">
                        <h4 class="page-title">Payments <?php echo $program_enrollment[0]['customer_name']; ?> </h4>
                        <input type="hidden" id="advance_available" name="advance_available" value="<?php if(isset($remaining_advance)){ echo $remaining_advance->advance;}?>">
                        <input type="hidden" id="customername" name="customername" value="<?php echo $program_enrollment[0]['customer_name']; ?>">
                        <input type="hidden" id="enrollment_id" name="enrollment_id" value="<?php echo $program_enrollment[0]['id_program_enrollment']; ?>">
                    </div>
                </div>
                <?php if ($this->session->flashdata('Success') && $this->session->flashdata('Success') != "") { ?>
                    <div class="row hidden-print">
                        <div class="col-sm-12">
                            <div class="alert alert-success">
                                <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                                <strong>Success!</strong> <?php echo $this->session->flashdata('Success'); ?>
                            </div>
                        </div>
                    </div>
                <?php } ?>
                <?php if ($this->session->flashdata('Error') && $this->session->flashdata('Error') != "") { ?>
                    <div class="row hidden-print">
                        <div class="col-sm-12">
                            <div class="alert alert-danger">
                                <a href="#" class="close" data-dismiss="alert" aria-label="close">&times;</a>
                                <strong>Error!</strong> <?php echo $this->session->flashdata('Error'); ?>
                            </div>
                        </div>
                    </div>
                <?php } ?>
                <div class="row">
                    <div class="col-md-12">
                        <div class="panel panel-default">
                            <div class="panel-body">
                                <div class="row">
                                    <div class="pull-left">
                                        <h3 class="logo invoice-logo"><?php if(isset($business)){ if(isset($business[0]['business_logo1'])){echo "<img src='".base_url().'assets/images/business/'.$business[0]['business_logo1']."' alt='".$business[0]['business_name']."' class='img-responsive' />";}else{echo "<img src='".base_url().'assets/images/business/'.$business[0]['business_logo']."' alt='".$business[0]['business_name']."' class='img-responsive' />"; }} else {echo 'SalonPK';}?></h3>
                                    </div>
                                    <div class="pull-right">
                                        
                                        <table class="table table1" style="border:none !important;">
                                            <tr>
                                                <td style="border-right:1px solid #f7f7f7; color:#797979; text-align: right; vertical-align: bottom;">Enrollment Date</td>
                                                <td style="color: #000; font: normal normal 16px/16px arial, serif; vertical-align: middle;"><?php if(isset($program_enrollment)){ echo $program_enrollment[0]['enrollment_date'];} ?></td>
                                            </tr>
                                            <tr>
                                                <td style="border-right:1px solid #f7f7f7; color:#797979; text-align: right">Roll ID</td>
                                                <td style="color:#000;"><?php if(isset($program_enrollment)){ echo sprintf("%04s",$program_enrollment[0]['id_program_enrollment']);}else{echo '00001';} ?></td>
                                            </tr>
                                            <tr>
                                                <td style="border-right:1px solid #f7f7f7; color:#797979; text-align: right">Contact</td>
                                                <td style="color:#000;">Academy Manager</td>
                                            </tr>
                                            <tr>
                                                <td style="border-right:1px solid #f7f7f7; color:#797979; text-align: right"></td>
                                                <td style="color:#000;"><?php if(isset($business)){ echo $business[0]['business_email'];}?></td>
                                            </tr>
                                            <tr>
                                                <td style="border-right:1px solid #f7f7f7; color:#797979; text-align: right"></td>
                                                <td style="color:#000;"><?php if(isset($business)){ echo $business[0]['business_phone2'];}?></td>
                                            </tr>
                                        </table>
                                    </div>
                                </div>
                                
                                
                                <div class="row m-t-40">
                                   <div class="col-xs-6">
                                        <span style="color: #000; font: normal normal 16px/40px Roboto, serif;  text-transform: uppercase;"><?php echo $program_enrollment[0]['customer_name']; ?></span><br>
                                        <span style="color: #000; font: normal normal 16px/20px Roboto, serif;  "><?php echo $program_enrollment[0]['customer_address']; ?></span><br>
                                        <span style="color: #000; font: normal normal 12px/40px Roboto, serif;  text-transform: uppercase;"><?php echo $program_enrollment[0]['customer_cell']; ?></span><br>
                                        <br>
                                    </div>
                                    <div class="col-xs-6">
                                        <span style="color: #000; text-transform: uppercase;">Father Name: <?php echo $program_enrollment[0]['father_name']; ?></span><br>
                                        <span style="color: #000; "><?php echo $program_enrollment[0]['father_profession'].' -  ' .$program_enrollment[0]['father_cell']; ?></span><br>
                                        <span style="color: #000; text-transform: uppercase;">Mother Name: <?php echo $program_enrollment[0]['mother_name']; ?></span><br>
                                        <span style="color: #000; "><?php echo $program_enrollment[0]['mother_profession'].' - ' .$program_enrollment[0]['mother_cell']; ?></span><br>
                                    </div>
                                        
                                </div>
                                
                          
                                <div class="row">
                                    <div class="col-md-12">
                                       <span style="font: normal bold 22px/30px arial, serif; color:#000">PAYMENT HISTORY</span>
                                   </div>
                                </div>
                                <div class="row" style=" color:#000;">
                                    <div class="col-md-12">
                                        
                                        <p>We hereby confirm the payment as described below:</p>
                                        <div class="table">
                                            <table class="table m-t-10" id="tblservices" style="border-bottom:#f7f7f7 1px solid">
                                                <thead style="color:#000 !important">
                                                    <tr>
                                                        <th>#</th>                                                        
                                                        <th>Program</th>
                                                        <th>Agreed Sessions</th>
                                                        <th>Total Service Value</th>
                                                        <th>Scheduled Sessions</th>
                                                        <th colspan="2">Un-Invoiced Sessions</th>
                                                        <th style="border-left:#000 1px solid; text-align: center;">RECEIVABLE VOUCHERS</th>
                                                        <th></th>
                                                        <th></th>
                                                    </tr>
                                                </thead>
                                                <tbody style="color:#000 !important">
                                                    
                                                    <?php $customerreceivable=0; $customerpaid=0; $totalreceivable=0; $paid_amount_sum=0; if(null != $registration_fee_invoice){?>
                                                    <tr>
                                                        <td colspan="7"><?php echo $registration_fee_invoice[0]['program_invoice_remarks']; ?></td>
                                                        <td colspan="9" style="border-left:1px solid #606060;">
                                                            <table class="table">
                                                                    <thead>
                                                                        <tr>
                                                                            <td>Invoice ID</td>
                                                                            <td>Invoiced Sessions</td>                                                        
                                                                            <td>Invoice Date</td>
                                                                            <td>Status</td>
                                                                            <td>By</td>                                                                            
                                                                            <td class="hidden" style="text-align: right;">Paid Cash</td>
                                                                            <td class="hidden" style="text-align: right;">Paid Card</td>
                                                                            <td class="hidden" style="text-align: right;">Paid Check</td>
                                                                            <th style="text-align: right;">Invoiced Amount</th>
                                                                            <th style="text-align: right;">Discount</th>
                                                                            <th style="text-align: right;">Other Charge</th>
                                                                            <th style="text-align: right;">Taxes</th>
                                                                            <th style="text-align: right;">CC Fee</th>
                                                                            <th style="text-align: right;">Paid</th>
                                                                            <th class="text-danger" style="text-align: right;">Balance</th>
                                                                            <td ></td>
                                                                            <td></td>
                                                                            <td></td>
                                                                        </tr>
                                                                    </thead>
                                                                    <tbody>
                                                                        <?php foreach($registration_fee_invoice as $registration_fee){?>
                                                                        <td ><?php echo $registration_fee['id_program_invoices']; ?></td>
                                                                        <td ><?php echo $registration_fee['program_invoice_remarks']; ?></td>
                                                                        <td ><?php echo $registration_fee['invoice_date']; ?></td>
                                                                        <td ><?php echo $registration_fee['payment_mode']; ?></td>
                                                                        <td ><?php echo $registration_fee['created_by']; ?></td>                                                                        
                                                                        <td class="hidden" style="text-align: right;"><?php echo $registration_fee['paid_cash']; ?></td>
                                                                        <td class="hidden" style="text-align: right;"><?php echo $registration_fee['paid_card']; ?></td>
                                                                        <td class="hidden" style="text-align: right;"><?php echo $registration_fee['paid_check']; ?></td>
                                                                        <td style="text-align: right;"><?php echo $registration_fee['invoiced_amount']; $customerreceivable+=$registration_fee['invoiced_amount']; ?></td>
                                                                        <td style="text-align: right;"><?php echo $registration_fee['invoice_discount']; $customerreceivable-=$registration_fee['invoice_discount']; ?></td>
                                                                        <td style="text-align: right;">0</td>
                                                                        <td style="text-align: right;"><?php echo $registration_fee['salestax']; $customerreceivable+=$registration_fee['salestax'];?></td>
                                                                        <td style="text-align: right;"><?php echo $registration_fee['cc_charge']; $customerreceivable+=$registration_fee['cc_charge'];?></td>
                                                                        <td style="text-align: right;"><?php echo $registration_fee['paid_amount']; $customerreceivable-=$registration_fee['paid_amount'];?></td>
                                                                        <?php $balance = ($registration_fee['invoiced_amount']+$registration_fee['invoice_other_charges']+$registration_fee['cc_charge']+$registration_fee['salestax']) - ($registration_fee['paid_amount']+$registration_fee['invoice_discount']);?>
                                                                            <td class="text-danger" style="text-align: right;"><?php if($registration_fee['invoiced_amount']!==null){echo number_format($customerreceivable); }   ?></td>
                                                                            <td><a target="_blank" href="<?php echo base_url().'print_coaching_invoice/'.$registration_fee['id_program_invoices'];?>" class="btn btn-xs btn-custom waves-effect waves-light" id="btnprint"><i class="fa fa-print"></i></a></td>
                                                                            <td>                                                                                
                                                                                <?php if($registration_fee['payment_mode']=='Receivable' || $balance > 0){?>
                                                                                <button type="button" onClick="open_invoice(<?php $invoiced_amount=$registration_fee['invoiced_amount']+ $registration_fee['invoice_discount']; echo $registration_fee['id_program_invoices'].','.$invoiced_amount.','.$registration_fee['invoice_discount'].','.$registration_fee['program_enrollment_id'].','.$registration_fee['paid_amount'].','.$registration_fee['cc_charge'];?>);" class="hidden-xs btn btn-xs btn-success">Add Payment</button>
                                                                               <?php } ?>
                                                                            </td>
                                                                            
                                                                        <?php } ?>                                                                            
                                                                    </tbody>
                                                            </table>
                                                        </td>
                                                        <td></td>
                                                        <td></td>
                                                        
                                                    </tr>
                                                        <?php } ?>
                                                    
                                                    <?php $x=1;  $lastservice_name=''; $grand_total=0;  $totalservicevalue=0;
                                                    foreach($program_enrollment as $pe){ $booked=0; $invoiced=0;?>
                                                    <?php if($x!=1 && $pe['service_name'] != $lastservice_name){?>
                                                    <tr class="subtotals">
                                                        <td colspan="9"></td>
                                                        <td style="text-align:right;">Total Invoiced Amount 'To Date' for <strong><?php echo $lastservice_name;?></strong></td>
                                                        <td style="text-align: right; border-top:#606060 solid 1px;border-bottom:#606060 solid 1px; font-weight: bold;"><?php echo number_format($paid_amount_sum); $grand_total=$grand_total+$paid_amount_sum;?></td>
                                                        
                                                    </tr>
                                                    <?php } ?>
                                                    <tr id="idtr">
                                                        <?php if($pe['service_name'] != $lastservice_name){
                                                            $paid_amount_sum=0; $total_receivable;
                                                            ?>
                                                            
                                                            <td><?php echo $x;?></td>                                                                                                                      
                                                            <td><?php echo $pe['service_name']; ?></td>
                                                            <td><?php echo $pe['total_sessions'];?></td>
                                                            <td><?php echo $pe['total_sessions']*$pe['per_session_rate']; $totalreceivable = $pe['total_sessions']*$pe['per_session_rate']; $totalservicevalue=$totalservicevalue+ $totalreceivable;?></td>
                                                            <td><strong>
                                                                <?php foreach($pe['bookings'] as $bs){
                                                                    if($bs['program_enrolled_service_id']==$pe['id_program_enrolled_services']){ ?>                                                                
                                                                    <?php echo $bs['appointments']; $booked = $bs['appointments'];?>
                                                                <?php } else {$booked=0;}}?>
                                                                </strong>
                                                            </td><!-- booked Sessions -->
                                                            <td><?php if($booked>0){ echo $booked - $pe['invoiced']->invoiced_sessions;} else { echo 0;}?> </td>
                                                            <td>
                                                                <?php if($booked>0){$uninvoiced = $booked -  $pe['invoiced']->invoiced_sessions;}else{$uninvoiced=0;}
                                                                if ($uninvoiced>0){?>
                                                                    <input type="hidden" id="number_of_sessions_<?php echo $pe['id_program_enrolled_services'];?>" total_sessions="<?php echo $pe['total_sessions'];?>" booked_sessions="<?php echo $booked=0 ? 0 : $booked;?>" invoiced_sessions="<?php echo $invoiced = 0 ? 0 : $invoiced; ?>" value="<?php echo $booked - $invoiced;?>">
                                                                    <input type="hidden" id="uninvoiced_sessions_<?php echo $pe['id_program_enrolled_services'];?>" value="<?php echo $uninvoiced;?>">
                                                                    <input type="hidden" id="program_enrollment_id_<?php echo $pe['id_program_enrolled_services'];?>" value="<?php echo $pe['id_program_enrollment'];?>">
                                                                    <input type="hidden" id="program_enrolled_services_id_<?php echo $pe['id_program_enrolled_services'];?>" service_name="<?php echo $pe['service_name'];?>" value="<?php echo $pe['id_program_enrolled_services'];?>">
                                                                    <input type="hidden" id="program_enrolled_services_rate_<?php echo $pe['id_program_enrolled_services'];?>" value="<?php echo $pe['per_session_rate'];?>">
                                                                    <button type="button" onclick="open_add_invoice(<?php echo $pe['id_program_enrolled_services'];?>);" class="hidden-xs btn btn-xs btn-warning" >Add Receivable</button>                                                                    
                                                                <?php } ?>                                                            
                                                            </td> <!---un-invoiced-->
                                                            
                                                        <?php $x++; } else {?>
                                                            <td colspan="9"></td>
                                                        <?php } ?>
                                                            <?php $lastservice_name=$pe['service_name']; ?>
                                                            <td colspan="9" style="border-left:1px solid #606060;">
                                                                <table class="table">
                                                                    <thead>
                                                                        <tr>
                                                                            <td>Invoice ID</td>
                                                                            <td>Invoiced Sessions</td>                                                        
                                                                            <td>Invoice Date</td>
                                                                            <td>Status</td>
                                                                            <td>By</td>
                                                                            
                                                                            <td class="hidden" style="text-align: right;">Paid Cash</td>
                                                                            <td class="hidden" style="text-align: right;">Paid Card</td>
                                                                            <td class="hidden" style="text-align: right;">Paid Check</td>
                                                                            <th style="text-align: right;">Invoiced Amount</th>
                                                                            <th style="text-align: right;">Discount</th>
                                                                            <th style="text-align: right;">Other Charges</th>
                                                                            <th style="text-align: right;">Taxes</th>
                                                                            <th  style='text-align: right;'>CC Fee</th>
                                                                            <th style="text-align: right;">Paid</th>
                                                                            <th class="text-danger" style="text-align: right;">Balance</th>
                                                                            <td ></td>
                                                                            <td></td>
                                                                            <td></td>
                                                                        </tr>
                                                                    </thead>
                                                                    <tbody>
                                                                <?php foreach($pe['invoices'] as $invoice){?>
                                                                        <tr>
                                                                            <td ><?php echo $invoice['id_program_invoices'];?></td>  
                                                                            <td><?php echo $invoice['number_of_sessions'];  $invoice['number_of_sessions']=0 ? 0 : $invoiced=$invoice['number_of_sessions'];?></td>
                                                                            <td ><?php echo $invoice['invoice_date']; ?></td>
                                                                            <td ><?php echo strtoupper($invoice['payment_mode']); ?></td>
                                                                            <td ><?php echo $invoice['created_by']; ?></td>

                                                                            <td class="hidden" style="text-align: right;"><?php echo number_format($invoice['paid_cash']); ?></td>
                                                                            <td class="hidden" style="text-align: right;"><?php echo number_format($invoice['paid_card']); ?></td >
                                                                            <td class="hidden" style="text-align: right;"><?php echo number_format($invoice['paid_check']); ?></td>

                                                                            <td style="text-align: right; "><?php if($invoice['invoiced_amount']!==null){echo number_format($invoice['invoiced_amount']+ $invoice['invoice_discount']); $paid_amount_sum=$paid_amount_sum + $invoice['invoice_discount'] + $invoice['invoiced_amount']; $customerreceivable+=($invoice['invoiced_amount']+$invoice['invoice_discount']);} else {echo '0';} ?></td>
                                                                            <td style="text-align: right; ">(<?php if($invoice['invoice_discount']!==null){echo number_format($invoice['invoice_discount']); $customerreceivable-=$invoice['invoice_discount'];} else {echo '0';} ?>)</td>
                                                                            <td style="text-align: right; "><?php if($invoice['invoice_other_charges']!==null){echo number_format($invoice['invoice_other_charges']); $customerreceivable+=$invoice['invoice_other_charges'];} else {echo '0';} ?></td>
                                                                            <td style="text-align: right; "><?php if($invoice['salestax']!==null){echo number_format($invoice['salestax']); $customerreceivable+=$invoice['salestax'];} else {echo '0';} ?></td>
                                                                            <td style="text-align: right;"><?php echo $invoice['cc_charge']; $customerreceivable+=$invoice['cc_charge'];?></td>
                                                                            <td style="text-align: right; "><?php if($invoice['paid_amount']!==null){echo number_format($invoice['paid_amount']); $customerreceivable-=$invoice['paid_amount'];} else {echo '0';} ?></td>
                                                                            <?php $balance = ($invoice['invoiced_amount']+$invoice['invoice_other_charges']+$invoice['cc_charge']+$invoice['salestax']) - ($invoice['paid_amount']+$invoice['invoice_discount']);?>
                                                                            <td class="text-danger" style="text-align: right;"><?php if($invoice['invoiced_amount']!==null){echo number_format($customerreceivable); }  ?></td>
                                                                            <td><a target="_blank" href="<?php echo base_url().'print_coaching_invoice/'.$invoice['id_program_invoices'];?>" class="btn btn-xs btn-custom waves-effect waves-light" id="btnprint"><i class="fa fa-print"></i></a></td>
                                                                            <td>
                                                                                
                                                                                <?php if($invoice['payment_mode']=='Receivable' || $balance > 0){?>
                                                                                <button type="button" onClick="open_invoice(<?php $invoiced_amount=$invoice['invoiced_amount']+ $invoice['invoice_discount']; echo $invoice['id_program_invoices'].','.$invoiced_amount.','.$invoice['invoice_discount'].','.$invoice['program_enrollment_id'].','.$invoice['paid_amount'].','.$invoice['cc_charge'];?>);" class="hidden-xs btn btn-xs btn-success">Add Payment</button>
                                                                               <?php } ?>

                                                                            </td>
                                                                            <td><?php //if($invoice['payment_mode']=='Receivable'){?><button onclick="cancelinvoice(<?php echo $invoice['id_program_invoices'].','.$pe['id_program_enrollment'];?>);" abc="<?php //echo base_url().'coaching_controller/cancel_coaching_invoice/'.$invoice['id_program_invoices'].'/'.$pe['id_program_enrollment'] ;?>" class="hidden-xs btn btn-xs btn-danger">Cancel</button><?php //} ?></td>
                                                                        </tr>
                                                                        <?php } ?>
                                                                    
                                                                    </tbody>
                                                                </table>
                                                            </td>
                                                        </tr>
                                                        
                                                    <?php  } ?>
                                                        <tr class="subtotals">
                                                            <td colspan="9"></td>                                                            
                                                            <td style="text-align:right;">Total Invoiced Amount 'To Date' for <strong><?php echo $lastservice_name;?></strong></td>
                                                            <td style="text-align: right; border-top:#606060 solid 1px;border-bottom:#606060 solid 1px; font-weight: bold;"><?php echo number_format($paid_amount_sum);  $grand_total=$grand_total+$paid_amount_sum;?></td>
                                                            
                                                        </tr>
                                                </tbody>
                                                <tfoot>
                                                    <tr>
                                                        <td colspan="3">Total Service Value</td>
                                                        <td ><strong><?php echo number_format($totalservicevalue);?></strong></td>                                                        
                                                        <td colspan="5"></td>
                                                        <td  style="text-align: right; border-bottom:#606060 solid 2px; ">Total Invoiced Receivable: </td>
                                                        <td style="text-align: right; border-bottom:#606060 solid 2px; font-weight: bold;"><?php echo number_format($customerreceivable);?><td>                                                        
                                                    </tr>
                                                </tfoot>
                                            </table>
                                        </div>
                                    </div>
                                </div>
                                
                                <br>
                                <div class="hidden-print">
                                    <div class="pull-right">
                                        <button onclick="openadvancemodal(<?php echo $program_enrollment[0]['id_program_enrollment'] ;?>)" class="btn btn-primary waves-effect waves-light" id="btnpayment"><i class="fa fa-dollar"></i> Add Advance</button>
                                        <a type="submit" href="<?php echo base_url().'print_coaching_enrollment/'.$program_enrollment[0]['id_program_enrollment'] ;?>" class="btn btn-default waves-effect waves-light" id="btnenrollment"><i class="fa fa-file"></i> Form</a>
                                        <a type="submit" href="<?php echo base_url().'print_pending_invoices/'.$program_enrollment[0]['id_program_enrollment'] ;?>" target="_blank" class="btn btn-custom waves-effect waves-light" id="btnenrollment"><i class="fa fa-file"></i> Print Pending Invoices</a>
                                        
                                        <a href="javascript:window.print()" class="btn btn-inverse waves-effect waves-light" id="btnprint"><i class="fa fa-print"></i></a>
                                       
                                    </div>
                                </div>
                            </div>
                        </div>

                    </div>

                </div>
                <!-- end row -->
                
        <!--Add Invoice Modal-->
        <div id="addinvoicemodal" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="addpayment" aria-hidden="true" style="display: none;">
            <div class="modal-dialog">
                <div class="modal-content">
                    <form id="addInvoiceForm" action="<?php echo base_url("coaching_controller/add_coaching_invoice"); ?>" method="POST">
                        <input type="hidden" name="csrf_test_name" id="payment_csrf" value=""/>
                        <input type="hidden" name="number_of_sessions" id="number_of_sessions" value="">
                        <input type="hidden" name="program_enrolled_services_id" id="program_enrolled_services_id" value="">
                        <input type="hidden" name="total_sessions" id="total_sessions" value="">
                        <input type="hidden" name="booked_sessions" id="booked_sessions" value="">
                        <input type="hidden" name="invoiced_sessions" id="invoiced_sessions" value="">
                        <input type="hidden" name="customer_name" id="invoiced_sessions" value="<?php echo $program_enrollment[0]['customer_name']; ?>">
                        <input type="hidden" name="customer_id" id="invoiced_sessions" value="<?php echo $program_enrollment[0]['customer_id']; ?>">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            <h4 class="modal-title" id="custom-width-modalLabel">Add New Receivable Voucher</h4>
                        </div> 
                        <div class="modal-body">
                            <div class="row">
                                <div class="col-lg-12">
                                    <h2>Enrollment ID : <input style="border:none;" name="program_enrollment_id" id="program_enrollment_id" value=""></h2>
                                </div>
                                
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label for="txtInvoiceDate" class="control-label">Invoice Date:</label>
                                        <input id="txtInvoiceDate" name="txtInvoiceDate" class="form-control txtDate" required="required">
                                    </div> 
                                </div> 
                            </div>
                            
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label for="txtService" class="control-label">Service:</label>
                                        <input class="form-control" readonly="" name="program_enrolled_services_name" id="program_enrolled_services_name" value="">
                                    </div> 
                                </div> 
                            </div>
                            
                            <div class="row">
                                <div class="col-md-12">
                                    <table class="tbl">
                                        <thead>
                                            <tr>
                                                <th>
                                                    Un-Invoiced Sessions
                                                </th>
                                                <th>
                                                    Per Sessions Rate
                                                </th>
                                                <th>
                                                    SubTotal
                                                </th>
                                                <th>
                                                    Discount
                                                </th>
                                                <th>
                                                    Total
                                                </th>
                                                
                                            </tr>
                                        </thead>
                                        <tbody>
                                            <tr>
                                                <td><input onchange="CalTotalRecivable();" type="number" max="0" min="0" id="uninvoiced_sessions" name="uninvoiced_sessions" class="form-control" value="0"></td>
                                                <td><input type="" readonly="readonly" name="program_enrolled_services_rate" id="program_enrolled_services_rate" class="form-control numeric" value="0"></td>                                                
                                                <td><input readonly="readonly"  id="txtActualRate" name="txtActualRate" class="form-control numeric" value="0"></td>
                                                <td><input onchange="CalTotalRecivable();" id="txtDiscount" name="txtDiscount" class="form-control numeric" value="0">
                                                <td><input onchange="CalTotalRecivable();" id="txtTotalReceivable" name="txtTotalReceivable" class="form-control numeric" value="0">
                                            </tr>
                                        </tbody>
                                        <tfoot>
                                           
                                        </tfoot>
                                    </table>
                                </div>
                            </div>
                            
                            <div class="row">
                                <div class="col-md-12">
                                    <div class="form-group">
                                        <label for="txtremarks" class="control-label">Comments:</label>
                                        <textarea class="form-control" name="txt_invoice_remark" id="txt_invoice_remark" value=""></textarea>
                                    </div> 
                                </div> 
                            </div>
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
                            <button type="submit" onclick="$('#payment_csrf').val($('#cook').val());" class="btn btn-custom waves-effect waves-light">Save</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>        
                
        <!--Add Payment Modal-->
        <div id="addpaymentmodal" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="addpayment" aria-hidden="true" style="display: none;">
            <div class="modal-dialog">
                <div class="modal-content">
                    <form id="addPaymentForm" action="<?php echo base_url("coaching_controller/add_invoice_payment"); ?>" method="POST">
                        <input type="hidden" name="csrf_test_name" id="payment_csrf" value=""/>
                        <input type="hidden" name="txtinvoiceid" id="txtinvoiceid" value=""/>                        
                        <input type="hidden" name="txtpaymentenrollmentid" id="txtpaymentenrollmentid" value=""/>
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            <h4 class="modal-title" id="custom-width-modalLabel">Add A Payment</h4>
                        </div>
                        <div class="modal-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="txtinvoicecomments" style="border:none; text-align: right;" class="control-label date">Comments:</label>                                        
                                    </div> 
                                </div> 
                                <div class="col-md-6">
                                    <div class="form-group">                                        
                                        <input id="txtinvoicecomments" name="txtinvoicecomments" class="form-control">
                                    </div> 
                                </div>                                 
                            </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="txtPaymentDate" style="border:none; text-align: right;" class="control-label date">Payment Date:</label>                                        
                                    </div> 
                                </div> 
                                <div class="col-md-6">
                                    <div class="form-group">                                        
                                        <input id="txtPaymentDate" name="txtPaymentDate" style="text-align: right;" class="form-control txtDate1" required="required">
                                    </div> 
                                </div>                                 
                            </div>
                            <div class="row">
                                <div class="col-lg-6">
                                    <strong>Invoiced Amount: </strong>
                                </div>                                
                                <div class="col-lg-6">
                                    <input type="" readonly="readonly" class="form-control plain" style="border:none; text-align: right;" name="txtinvoiceamount" id="txtinvoiceamount" value=""/>
                                </div>                                
                            </div>
                            <div class="row">
                                <div class="col-lg-6">
                                    <strong>Invoiced Discount: </strong>
                                </div>                                
                                <div class="col-lg-6">
                                    <input type="" readonly="readonly" class="form-control plain" style="border:none; text-align: right;" name="txtinvoicediscount" id="txtinvoicediscount" value=""/>
                                </div>                                
                            </div>
                            <hr>
                            <div class="row">
                                <div class="col-lg-6">
                                    <strong>Receivable Amount: </strong>
                                </div>                                
                                <div class="col-lg-6">
                                    <input type="" readonly="readonly" class="form-control plain" style="border:none; text-align: right;" name="txtinvoicereceivable" id="txtinvoicereceivable" value=""/>
                                </div>                                
                            </div>
                             <div class="m-t-10 m-b-10 row" id="divcccharge" >
                                <div class="col-lg-6">
                                    <span >Other Charges</span>
                                </div>                                
                                <div class="col-lg-6">
                                    <input onchange="updatetotal();" onkeyup="if(this.value==''){this.value=0;}" class="cccharge form-control plain" id="txtothercharges" name="txtothercharges" style="text-align:right;" value="0" />
                                </div> 
                            </div>
                            <div class="m-t-10 m-b-10 row" id="divcccharge" >
                                <div class="col-lg-6">
                                    <span >CC Fee @ <input id="cc_charge" class="form-inline" style="width: 30px; border: none;" value="<?php echo $business[0]['cc_charge'];?>"> %</span> :  <?php echo CURRENCY_CODE;?>. 
                                </div>                                
                                <div class="col-lg-6">
                                    <input class="cccharge form-control plain" readonly="readonly" id="txtcc_charge" name="txtcc_charge" style="text-align:right;" value="0" />
                                </div> 
                            </div>
                            <hr>
                            <div class="m-t-10 m-b-10 row" >
                                <div class="col-lg-6">
                                    <label class="control-label">Gross Total</label>
                                </div>                                
                                <div class="col-lg-6">
                                    <input readonly="readonly" class="form-control plain" id="txtgross" name="txtgross" style="text-align: right;" value="0" />
                                </div> 
                            </div>
                            
                            <div class="m-t-10 m-b-10 row" id="divtaxes">
                            <?php if(isset($taxes)){$x=0;foreach ($taxes as $tax) {?>                                
                                <div class="col-lg-6">
                                    <input style="border:none;" readonly="readonly" id="txttaxname<?php echo $x; ?>" value="<?php echo $tax['tax_name'].' '; ?>" name="tax_name[]"/><input class="numeric" class="taxperc" style="width: 30px; border: none;" readonly="readonly" id="taxperc<?php echo $x;?>" value="<?php if(isset($tax['tax_percentage']) ||$tax['tax_percentage']!=""){ echo $tax['tax_percentage'];}?>">% :  <?php echo CURRENCY_CODE;?>. 
                                </div>                                
                                <div class="col-lg-6">
                                    <input name="sales_tax[]" class="tax form-control" mode="<?php echo $tax['tax_payment_mode']; ?>" readonly="readonly"  style="text-align: right;" value="0"/>
                                </div>   
                                
                            <?php $x++;}}?>                                            
                            </div>
                            
                            
                            <div class="m-t-10 m-b-10 row" >
                                <div class="col-lg-6">
                                    <label class="control-label">Rounding Adjustment</label>
                                </div>                                
                                <div class="col-lg-6">
                                    <input readonly="readonly" class="form-control" id="txtroundadj" name="txtroundadj" style="text-align: right;" value="0" />
                                </div> 
                            </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="txtreceivedpreviously" class="control-label">Received Previously:</label>                                        
                                    </div> 
                                </div> 
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <input readonly="readonly" style="text-align: right;" id="txtreceivedpreviously" name="txtreceivedpreviously" class="form-control" value="">
                                    </div> 
                                </div> 
                            </div>
                            <hr>
                            <div class="m-t-10 m-b-10 row" >
                                <div class="col-lg-6">
                                    <label class="control-label">Grand Total</label>
                                </div>                                
                                <div class="col-lg-6">
                                    <input readonly="readonly" class="form-control" id="grandtotal" name="grandtotal" style="text-align: right;" value="0" />
                                </div> 
                            </div>
                            <hr>                                
                            <hr>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="advance" class="control-label">Advance Available</label>                                        
                                    </div> 
                                </div> 
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <input readonly="readonly" style="text-align: right;" id="advance" name="advance" class="form-control" value="0">
                                    </div> 
                                </div> 
                            </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="cashpaid" class="control-label">Paid Cash</label>                                        
                                    </div> 
                                </div> 
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <input onchange="updatetotal();" onkeyup="if(this.value==''){this.value=0;}" style="text-align: right;" id="cashpaid" name="cashpaid" class="form-control" value="0.00">
                                    </div> 
                                </div> 
                            </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="cardpaid" class="control-label">Paid Card</label>                                        
                                    </div> 
                                </div> 
                                <div class="col-md-3">
                                    <div class="form-group">                                        
                                        <input style="text-align: right;"  id="card_instrument_number" name="card_instrument_number" placeholder="Card Number" class="form-control multinumeric" value="">
                                    </div> 
                                </div> 
                                <div class="col-md-3">
                                    <div class="form-group">                                        
                                        <input onchange="updatepayingcard();"  onkeyup="if(this.value==''){this.value=0;}"style="text-align: right;" id="cardpaid" name="cardpaid" class="form-control multinumeric" value="0.00">
                                    </div> 
                                </div> 
                            </div>
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="form-group">
                                        <label for="check_instrument_number" class="control-label">Paid Check</label>                                        
                                    </div> 
                                </div>
                                <div class="col-md-3">
                                    <div class="form-group">                                        
                                        <input style="text-align: right;"  id="check_instrument_number" name="check_instrument_number" placeholder="Check Number" class="form-control multinumeric" value="">
                                    </div> 
                                </div> 
                                <div class="col-md-3">
                                    <div class="form-group">
                                        <input onchange="updatetotal();" onkeyup="if(this.value==''){this.value=0;}" id="checkpaid" style="text-align: right;"  name="checkpaid" class="form-control" value="0.00">
                                    </div> 
                                </div> 
                            </div>
                             <div class="row">
                                <div class="col-md-6">
                                    <hr>
                                    <div class="form-group">
                                        <label for="txtTotalPayment" class="control-label">Total Payment</label>                                        
                                    </div> 
                                </div> 
                                 <div class="col-md-6">
                                     <hr>
                                    <div class="form-group">                                        
                                        <input id="txtTotalPayment" readonly="readonly" style="text-align: right;" name="txtTotalPayment" class="form-control" value="0.00">
                                    </div> 
                                </div> 
                            </div>
                            <div class="row">
                                <h4 class="text-right text-primary hidden-print">Return <?php echo CURRENCY_CODE;?>. 
                                <input class="numeric" style="text-align: right; width: 95px; border: none;" readonly="readonly" id="returnamount"  name="returnamount" value="0"></h4>                                
                            </div>
                                 
                        </div>
                        <div class="modal-footer">
                            <button type="button" class="btn btn-default waves-effect" data-dismiss="modal">Close</button>
                            <button id="btnsubmit" type="submit" onclick="$('#payment_csrf').val($('#cook').val());" class="btn btn-custom waves-effect waves-light">Save</button>
                        </div>
                    </form>
                </div>
            </div>
        </div>
        
<div id="advancemodal" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="advancemodal" aria-hidden="true" style="display: none;">
    <div class="modal-dialog modal-md"  >
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                <h4 class="modal-title" id="custom-width-modalLabel">Add Advance Payment</h4>
            </div>
            <div class="modal-body">
                <div class='row'>
                    <div class="col-md-12" id="advance_table">
                        <div>
                            <table id="adv_table" class="table table-stripped">
                                <tbody>
                                    <?php if(isset($enrollment_advance)){ ?>
                                    <thead>
                                        <tr>
                                            <td >ID</td>
                                            <td >Date</td>
                                            <td >Amount</td>
                                        </tr>
                                    </thead>
                                    <?php foreach($enrollment_advance as $advance){?>
                                    <tr>
                                        <td><?php echo $advance['id_program_advance_note'];?></td>
                                        <td><?php echo $advance['advance_date'];?></td>
                                        <td><?php echo $advance['advance_amount'];?></td>
                                    </tr>
                                    <?php }}?>
                                </tbody>
                                <tfoot>
                                </tfoot>
                            </table>
                            <hr>
                        </div>
                    </div>
                </div>
                <div class='row'>
                    <div class="col-md-6 " id="advance_block">
                        <div class="row">
                            <div class="form-group">
                                <div class="col-md-4">
                                    Advance Amount:
                                </div>
                                <div class="col-md-8">
                                    <div class="input-group">
                                        <span class="input-group-addon"><?php echo CURRENCY_CODE;?>. </span>
                                        <input class="form-control numeric" onchange="advanceamountchanged();" name="advance_amount" id="advance_amount" style="text-align: right;" />
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div id="advance_inst_div" style="display:none;" >
                            <div class="row">    
                                <div class="form-group">
                                    <div class="col-md-4">
                                        Inst. #:
                                    </div>
                                    <div class="col-md-8">
                                        <input class="form-control numeric" name="advance_inst" id="advance_inst" style="text-align: right; " />
                                    </div>
                                </div>
                            </div>
                        </div>
                        <div id="advance_card_div" style="display:none;" >
                            <div class="row">
                                <div class="form-group">
                                    <div class="col-md-4">
                                        CC Fee %:
                                    </div>
                                    <div class="col-md-8">
                                        <input readonly="readonly" class="form-control numeric" name="cc_charge_setting" id="cc_charge_setting" style="text-align: right; " value="<?php echo $business[0]['cc_charge']; ?>" />
                                    </div>
                                </div>
                            </div>
                            <div class="row">    
                                <div class="form-group">
                                    <div class="col-md-4">
                                        Extra :
                                    </div>
                                    <div class="col-md-8">
                                        <input readonly="readonly" class="form-control" name="advance_cccharge" id="advance_cccharge" style="text-align: right; " value="0" />
                                    </div>
                                </div>    
                            
                            </div>
                        </div>
                        <div class="form-group">
                            <select class="form-control" onchange="showhideinst();" id="advance_mode" name = "advance_mode">
                                <option value="cash">Cash</option>
                                <option value="card">Credit Card</option>
                                <option value="card">Debit Card</option>
                                <option value="check">Bank</option>
                            </select>
                        </div>
                        <div class="form-group">
                            <div class="col-md-4">
                                Advance Date:
                            </div>
                            <div class="col-md-8">
                                <input class="form-control date-picker" name="advance_date" id="advance_date" />
                            </div>
                        </div>
                    
                    </div>
                    <div class='col-md-6'>
                        <div>
                            Enter Advance Comment:
                        </div>
                        <div class="form-group">
                            <textarea class="form-control" name="advance_comment" id="advance_comment" ></textarea>
                        </div>
                    </div>
                </div>
                    
                    
            </div>
        
            <div class='modal-footer'>
                <button type="button" onclick="printadvance();"  class="btn btn-default waves-effect waves-light pull-right m-l-5"><i class="fa fa-print"></i></button>
                <button type="button" onclick="saveadvance();"  class="btn btn-custom waves-effect waves-light pull-right">Save</button>

            </div>
        </div>
    </div>
</div>
        
<script>
    
 
    $('document').ready(function(){
        
        $('#advance_date').datepicker({
                default: today,
                autoclose: true,
                todayHighlight: true,
                format: 'yyyy-mm-dd'
            }).datepicker('setDate', new Date());
        
        $('.txtDate').datepicker({
            default: today,
            autoclose: true,
            todayHighlight: true,
            format: 'yyyy-mm-dd',
            orientation: 'bottom'
        }).datepicker('setDate', new Date());
        
        $('.txtDate1').datepicker({
            default: today,
            autoclose: true,
            todayHighlight: true,
            format: 'yyyy-mm-dd',
            orientation: 'bottom'
        }).datepicker('setDate', new Date());
        
        $(".numeric").keypress(function (e) {
            //if the letter is not digit then display error and don't type anything
            if (e.which != 13 && e.which != 8 && e.which != 0  && e.which != 46 && (e.which < 48 || e.which > 57)) {
               return false;
           }
        });
        $(".multinumeric").keypress(function (e) {
            //if the letter is not digit then display error and don't type anything
            if (e.which != 13 && e.which != 8 && e.which != 0  && e.which != 46 && e.which != 44 && (e.which < 48 || e.which > 57)) {
               return false;
           }
        });

    }); 
    
    
    function openadvancemodal(enrollmentid){
        
        $("#advancemodal").modal('show');
        
    }
    function calcTotal(){
       
    }
   
   function open_add_invoice(val){
      // console.log(val);
       var num_sessions= "#number_of_sessions_"+val;
       var peid= "#program_enrollment_id_"+val;       
       var pesid= "#program_enrolled_services_id_"+val;
       var pesrate= "#program_enrolled_services_rate_"+val;
       var ui = "#uninvoiced_sessions_"+val;

      
      $("#number_of_sessions").val($(num_sessions).val());
      $("#total_sessions").val($(num_sessions).attr('total_sessions'));
      $("#booked_sessions").val($(num_sessions).attr('booked_sessions'));
      $("#invoiced_sessions").val($(num_sessions).attr('invoiced_sessions'));
      
     var uninvoiced = $(ui).val();
     
      var actualrate = uninvoiced * parseInt($(pesrate).val());

      $("#uninvoiced_sessions").val(uninvoiced);
      $("#uninvoiced_sessions").attr("max",uninvoiced);
      $("#program_enrolled_services_rate").val($(pesrate).val());
      
      $("#txtActualRate").val(actualrate);
      $("#txtTotalReceivable").val(actualrate);
      
      
      
      $("#program_enrollment_id").val($(peid).val());
      $("#program_enrolled_services_id").val($(pesid).val());
      $("#program_enrolled_services_name").val($(pesid).attr('service_name'));
      
      $("#addinvoicemodal").modal('show');
       
   }

    function CalTotalRecivable(){
        
        var uninvoiced = parseInt($("#uninvoiced_sessions").val());
        var actualrate = uninvoiced * parseInt($("#program_enrolled_services_rate").val());
        $("#txtActualRate").val(actualrate);
        
        var discount = parseInt($("#txtDiscount").val());
        
        var finalrate = actualrate - discount;
        
        $("#txtTotalReceivable").val(finalrate);
        
    }

    function open_invoice(invoiceid, invoiceamount, invoicediscount, enrollmentid, received, cc_charge){
        
        $("#txtinvoiceid").val(invoiceid);
        
        $.ajax({
                url: '<?php echo base_url() . 'coaching_controller/get_coaching_invoice_detail'; ?>',
                type: 'POST',
                data: {invoice_id: invoiceid},
                success: function (data) {
                    data = $.parseJSON(data);
                    //console.log(data);
                    var invoiced_amount = parseInt(data.invoiced_amount)+parseInt(data.invoice_discount);
                    $("#txtinvoiceamount").val(invoiced_amount);
                    $("#txtinvoicediscount").val(data.invoice_discount);
                    
                    var receivable = invoiced_amount-parseInt(data.invoice_discount);
                    $("#txtinvoicereceivable").val(receivable);
                    
                    $("#txtpaymentenrollmentid").val(data.program_enrollment_id);
                    if(received > 0){
                        $("#txtothercharges").attr("readonly","readonly");               
                    } else {
                        $("#txtothercharges").removeAttr("readonly","readonly");
                    } 
                    $("#txtcc_charge").val(data.cc_charge);
                    $("#txtinvoicecomments").val(data.invoice_remarks);

                    updatetotal();

                    $("#addpaymentmodal").modal('show');
                }
        });
        
        //console.log(invoicediscount);
        
        
//        if(invoicediscount==='' || typeof invoicediscount == 'undefined'){invoicediscount=0;}
//        $("#txtinvoicediscount").val(invoicediscount);
//
//        var receivable = parseInt(invoiceamount)-parseInt(invoicediscount);
//        $("#txtinvoicereceivable").val(receivable);
//        
//        $("#txtreceivedpreviously").val(received);
//        
//        $("#txtpaymentenrollmentid").val(enrollmentid);
//        
//        if(received > 0){
//            $("#txtothercharges").attr("readonly","readonly");               
//        } else {
//            $("#txtothercharges").removeAttr("readonly","readonly");
//        } 
//        
//        $("#txtcc_charge").val(cc_charge);
//        
//        $("#txtinvoicecomments").val(remarks);
//                
//        updatetotal();
//
//        $("#addpaymentmodal").modal('show');

    }    


    function updatepayingcard(){
       
        if(parseFloat($("#cc_charge").val()) > 0){
            $("#divcccharge").show();
            //$("#txtcc_charge").val("0");
            //console.log('grandtotal : '+ parseFloat($("#grandtotal").val()) + " cc_charge : " + $("#cc_charge").val());
            var ccc= (parseFloat($("#cardpaid").val()) * parseFloat($("#cc_charge").val()))/100;
           
            var totalccc=Math.ceil(ccc + parseFloat($("#txtcc_charge").val()));
           
            $("#txtcc_charge").val(totalccc);
            $("#cardpaid").val(Math.ceil(parseFloat($("#cardpaid").val()) + ccc));
        }
        
        var card = parseFloat($("#cashpaid").val()) + parseFloat($("#cardpaid").val());
        $("#paid").val(Math.round(Math.ceil(card)));
        
        updatetotal();
    }
    
    
    
    
    async function updatetotal(){
        
        $('#btnsubmit').addClass('disabled');
        $('#btnsubmit').html('<i class="fa fa-spin fa-spinner"></i> Please wait');
        
        if($('#txtinvoicediscount').val()===""){$('#txtinvoicediscount').val('0');}
        if($('#txtothercharges').val()===""){$('#txtothercharges').val('0');}
       
        var grosstotal=(parseFloat($("#txtinvoicereceivable").val()) +  parseFloat($("#txtothercharges").val()) + parseFloat($("#txtcc_charge").val()));
        var taxabletotal=(parseFloat($("#txtinvoicereceivable").val()) +  parseFloat($("#txtothercharges").val()));
        var orggrosstotal = parseFloat($("#txtinvoicereceivable").val()) +  parseFloat($("#txtothercharges").val()); //was txt_org_subtotal
        //console.log(grosstotal);
        
        grosstotal = Math.round(grosstotal*100)/100;
        taxabletotal= Math.round(taxabletotal*100)/100;
        orggrosstotal=Math.round(orggrosstotal*100)/100;
        
        $("#txtgross").val(grosstotal);
        
        var nettax= 0;
        var mpaymentmode = 'all';
        
        if(parseFloat($("#cashpaid").val())>0){
            mpaymentmode="CASH";
        } else if(parseFloat($("#cardpaid").val())>0){
            mpaymentmode="CARD";
        } else if(parseFloat($("#heckpaid").val())>0){
            mpaymentmode="CHECK";
        } else if(parseFloat($("#cashpaid").val())>0 && parseFloat($("#cardpaid").val())>0){
            mpaymentmode="Mixed";
        }
        
        
        var taxarray = [];
        
            $( ".tax" ).each(function(index) {
                $(this).val('0');
                console.log($(this).attr("mode").toUpperCase());
                if($("#txtreceivedpreviously").val() == 0){
                    if($(this).attr("mode")=="all" || $(this).attr("mode").toUpperCase()==mpaymentmode){
                      <?php if(isset($business[0]['tax_mode'])){if($business[0]['tax_mode'] == 'before'){?>
                          var thistax=orggrosstotal*parseFloat($("#taxperc"+index).val())/100;
                      <?php } else{?>
                          var thistax=taxabletotal*parseFloat($("#taxperc"+index).val())/100;
                      <?php }?>
                      <?php } else {?>
                          var thistax=orggrosstotal*parseFloat($("#taxperc"+index).val())/100;
                      <?php }?>    

                      $(this).val(Math.round(thistax*100)/100);
                      taxarray.push($(this).val());
                    }
                }
            });
            for(x=0; x<taxarray.length;x++){
                nettax=nettax + parseFloat(taxarray[x]);
            }
        
            
        
        var totalpayable=grosstotal + nettax;
        $("#txttotalpayable").val(Math.round(totalpayable*100)/100);
        
        var unroundedgrandtotal = (grosstotal + nettax);
        var grandtotal = Math.round(unroundedgrandtotal);
        var roundadjustment = grandtotal - unroundedgrandtotal;
        
        $("#txtroundadj").val(Math.round(roundadjustment*100)/100); 
        
        var receivedpreviously = $("#txtreceivedpreviously").val();
        
        
        if(grandtotal>=0){
            $("#grandtotal").val(grandtotal-receivedpreviously);
        }else{
            $("#grandtotal").val('0');
        }
        var advance = parseFloat($("#advance_available").val());
        if(advance > 0 && advance > grandtotal){            
            $("#advance").val(grandtotal-receivedpreviously);
        }else if(advance > 0 && advance < grandtotal){            
            $("#advance").val(advance);
        }
        
        var totalpaid = parseFloat($("#advance").val()) + parseFloat($("#cashpaid").val()) + parseFloat($("#cardpaid").val()) + parseFloat($("#checkpaid").val());
        $('#txtTotalPayment').val(totalpaid);
        
        
//        if(totalpaid === "0" || totalpaid === ""){
//            $("#balanceamount").val(Math.round((grosstotal + nettax)*100)/100);
//            $("#paid").val(0);
//        } else {
//            $("#balanceamount").val(Math.round((grosstotal - parseFloat($("#paid").val()))*100)/100);
//        }
        
        if(totalpaid > parseInt(grandtotal)){
            var return_amount = Math.round((totalpaid - grandtotal)*100)/100;
            $('#returnamount').val(return_amount);
           // $("#balanceamount").val(0);
        } else{
            $("#returnamount").val(0);
          //  $('#balanceamount').val(Math.round((parseFloat(grandtotal) - totalpaid)*100)/100);
        }
        
        
        
       
        $('#btnsubmit').html('Submit');        
        $('#btnsubmit').removeClass('disabled');
    }
    
    function cancelinvoice(invoiceid,enrollmentid){
        swal({
                title: "Are you sure?",
                text: "This will also remove all payments against this invoice!",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: '#DD6B55',
                confirmButtonText: 'Yes, I am sure!',
                cancelButtonText: "No, Dont cancel it!",
                closeOnConfirm: true,
                closeOnCancel: true
            },
            function(isConfirm) {
                if(isConfirm){
                $.ajax({
                    type: "POST",
                    url: "<?php echo base_url(); ?>coaching_controller/cancel_coaching_invoice/"+invoiceid+"/"+enrollmentid,
                    success: function(data) {
                        location.reload();
                    }
                });
                }
        });
    }
    
    function saveadvance() {

        if ($("#advance_amount").val() === '' || $("#advance_inst_div").is(":visible") && $("#advance_inst").val() == "") {
            if ($("#advance_amount").val() === '') {
                swal({
                    title: 'Please Enter Advance Amount!',
                    text: '',
                    type: 'error',
                    confirmButtonText: 'OK!'
                });
                $("#advance_amount").focus();
            } else if ($("#advance_inst_div").is(":visible") && $("#advance_inst").val() == "") {
                swal({
                    title: 'Please Enter Instrument Number!',
                    text: '',
                    type: 'error',
                    confirmButtonText: 'OK!'
                });
                $("#advance_inst").focus();
            }
            return false;
        } else {

            $.ajax({
                type: 'POST',
                //url: 'Scheduler_controller/active_staff_list',
                url: "<?php echo base_url() . 'coaching_controller/add_enrollment_advance'; ?>",
                data: {enrollment_id: $('#enrollment_id').val(),
                    advance_amount: $("#advance_amount").val(),
                    advance_mode: $("#advance_mode option:selected").val(),
                    advance_inst: $("#advance_inst").val(),
                    advance_cc_charge: $("#advance_cccharge").val(),
                    advance_comment: $("#advance_comment").val(),
                    advance_date: $("#advance_date").val()
                },                
                cache: false,
                async: true,
                success: function (data) {
                    toastr.success('Advance of <?php echo CURRENCY_CODE;?>' + $("#advance_amount").val() + ' Added for ' + $('#customername').val(), 'Done!');
                    console.log(data);
                    $("#advancemodal").modal('hide');
                   location.reload();
                }
            });
        }
    }
    
</script>
