<?php
$query_rsodest_claim_no = "select * from process_claim_no where process_claim_no='$process_claim_no'";
$rsodest_claim_no = mysql_query($query_rsodest_claim_no, $courier) or die(mysql_error());
$row_rsodest_claim_no = mysql_fetch_assoc($rsodest_claim_no);
$totalRows_rsodest_claim_no = mysql_num_rows($rsodest_claim_no);

// $count_procurement = mysql_num_rows(mysql_query("SELECT id FROM job_activities_status WHERE process_claim_no='$process_claim_no'"));
 
$rsclaim=mysql_query("select * from claims_details where process_claim_no='$process_claim_no' order by id desc;", $courier) or die(mysql_error());
$row_rsclaim = mysql_fetch_assoc($rsclaim);
$totalRows_rsclaim = mysql_num_rows($rsclaim);

$rsclaim_total=mysql_query("select sum(qty*unit_price) as totalprice from claims_details where process_claim_no='$process_claim_no' and mode_of_payment='cash'", $courier) or die(mysql_error());
$row_rsclaim_total = mysql_fetch_assoc($rsclaim_total);
$total_cash=$row_rsclaim_total['totalprice'];

$rsclaim_total=mysql_query("select sum(qty*unit_price) as totalprice from claims_details where process_claim_no='$process_claim_no' and mode_of_payment='gratis'", $courier) or die(mysql_error());
$row_rsclaim_total = mysql_fetch_assoc($rsclaim_total);
$total_gratis=$row_rsclaim_total['totalprice'];

$rsclaim_total=mysql_query("select sum(qty*unit_price) as totalprice,mode_of_payment from claims_details where process_claim_no='$process_claim_no' and (mode_of_payment<>'cash' AND mode_of_payment<>'gratis')", $courier) or die(mysql_error());
$row_rsclaim_total = mysql_fetch_assoc($rsclaim_total);
$total_other=$row_rsclaim_total['totalprice'];
$payment_mode_other=$row_rsclaim_total['mode_of_payment'];
$currency = '';

$discount_amount_info = mysql_fetch_assoc(mysql_query("SELECT sum(discount_amount) AS total_discount FROM patient_visit_discounts WHERE process_claim_no = '$process_claim_no' AND discount_status='Approved'"));
$discount_amount = $discount_amount_info['total_discount'];

$query_total_discount = mysql_num_rows(mysql_query("select id from claims_details where process_claim_no='$process_claim_no' and mode_of_payment <> 'gratis' and (payment_status='Owing' OR payment_status='Paid' OR payment_status='Approved') and (item_service_type='Optical Lens' OR item_service_type='Optical Frame') and discount_applied='not applied'"));
$receipt_avail = mysql_num_rows(mysql_query("SELECT id FROM cashier_payments WHERE process_claim_no='$process_claim_no'"));

$lens_added = mysql_fetch_assoc(mysql_query("SELECT sum(qty*unit_price) AS total_lens FROM claims_details WHERE item_service_type='Optical Lens' AND process_claim_no='$process_claim_no'"));
$lens_amount_expected = $lens_added['total_lens'];

$sum_lens_amount = mysql_fetch_assoc(mysql_query("SELECT sum(qty*unit_price) AS total_lens FROM claims_details WHERE item_service_type='Optical Lens' AND (payment_status='Paid' or payment_status='Approved') AND process_claim_no='$process_claim_no'"));
$lens_amount_paid = $sum_lens_amount['total_lens'];
$lens_amount_diff = $lens_amount_expected - $lens_amount_paid;

$job_numbers = mysql_query("SELECT * FROM job_numbers WHERE process_claim_no='$process_claim_no' AND audit_status = 'none'");

?>    
 <div class="portlet-title">
                            <div class="caption">
                                <span class="caption-subject font-blue-madison bold uppercase" style="font-size: 14px;"> Default Payment : <?php echo strtoupper($row_rsodest_claim_no['mode_of_payment']); ?> </span>
                            </div>
                            <div class="actions">
                                <div class="btn-group btn-group-devided" data-toggle="buttons">
                                    <?php if ($query_total_discount > 0) { ?>  
                                        <label id="hide_discount_btn" onclick="$('#discount_wrapper').toggle();" class="btn red btn-outline btn-circle btn-sm active">
                                            <input type="radio" name="options" class="toggle" id="option2"> Apply Discount <i class="fa fa-percent"></i>
                                        </label>
                                    <?php } ?>
                                   
                                     <?php if($total_cash>0){?>
                                    <label onclick="issueCashierPayments('forward_to_cashier_administrative', '#view_items_response_wrapper_2', '<?php echo $process_claim_no;?>', 'Are You Sure?', 'You want to Issue Payments to the Client?','view_patient_payments','<?php echo $row_rsodest_claim_no['patient_no'];?>','<?php echo $row_rsodest_claim_no['patient_id'];?>','<?php echo $row_rsodest_claim_no['typeofvisit']?>');" class="btn green btn-outline btn-circle btn-sm active">
                                        <input type="radio" name="options" class="toggle" id="option2"> Forward to Cashier <i class="fa fa-forward"></i>
                                    </label>
                                     <?php } else{ ?>
                                      <label onclick="getForwardPatient('forward_to_claims_administrative', '#view_items_response_wrapper_2', '<?php echo $process_claim_no;?>', 'Are You Sure?', 'You want to Forward this Client to Accounts?');" class="btn green btn-outline btn-circle btn-sm active">
                                        <input type="radio" name="options" class="toggle" id="option2">Forward to Accounts <i class="fa fa-forward"></i>
                                    </label>   
                                     <?php } ?>
                                     <?php if ($receipt_avail > 0) { ?>  
                                    <label onclick="getData('fetch_receiptnos', '<?php echo $process_claim_no; ?>', '#receipt_wrapper');" id="hide_discount_btn" class="btn red btn-outline btn-circle btn-sm active" data-toggle="modal" href="#large">
                                            <input type="radio" name="options" class="toggle" id="option2"> Print Receipt <i class="fa fa-print"></i>
                                        </label>
                                    <?php } ?>
                                    <label style="display:none;" id="generate_job_no_item_wrapper" onclick="getForwardPatient('generate_job_number', '#view_items_response_wrapper_2', '<?php echo $process_claim_no.'[===]'.$row_rsodest_claim_no['patient_no'];?>', 'Are You Sure?', 'You want to Generate Job Number for the Selected Items?','');" class="btn red btn-outline btn-circle btn-sm active">
                                        <input type="radio" name="options" class="toggle" id="option2"> Generate Job Number <i class="fa fa-forward"></i>
                                    </label>
                                </div>
                            </div>
     
     <div class="row" id="discount_wrapper" style="display: none;">
         <br>
         <div class="col-md-12">
             <div class="col-md-3">
                 <select onchange="$('.discount_rate_fixed').hide();if(this.value=='fixed'){$('#amount').show();}else{$('#discount_rate').show();}" class="form-control" name="discount_type" id="discount_type">
                     <!--<option value="fixed">Fixed Amount</option>-->
                     <option selected="" value="rate">Rate (%)</option>
                 </select>
             </div>
             <div class="col-md-2">
                 <select class="form-control discount_rate_fixed" name="discount_rate" id="discount_rate" onchange="getValue(this.value, '#discount_btn');">
                     <?php
                     $i=0.5;
                     do{
                      $total_percentage = $total_percentage + $i;?>
                      <option value="<?php echo $total_percentage; ?>"><?php echo number_format($total_percentage,2); ?></option>
                     <?php 
                     }while($total_percentage<=100) ?>
                 </select>
                 <input style="display: none;" class="form-control discount_rate_fixed" name="amount" id="amount">
             </div>
             <div class="col-md-3">
                 <?php
                 $reasons = mysql_query("SELECT * FROM discount_reasons");
                 ?>
                 <select class="form-control" name="discount_reason" id="discount_reason">
                     <option value="">Reason for Discount</option>
                     <?php while ($row = mysql_fetch_assoc($reasons)) { ?>
                         <option value="<?php echo $row['discount_reason']; ?>"><?php echo $row['discount_reason']; ?></option>
                     <?php } ?>
                 </select>
             </div>
             <div class="col-md-4">
                 <a class="btn green" id="discount_btn" onclick="approveGratis('forward_discount', '<?php echo $process_claim_no; ?>', 'Forward Discount for Approval?', 'Are you sure you want to Forward this Discount for Approval.','Yes, Forward Discount!');$('#discount_wrapper').hide();"> Apply Discount Now </a>
             </div>
         </div>
     </div>
     <br>
     
     <table class="table" style="background-color: #EEEEEE">
         <tbody>
             <tr>
                 <td> <div style="padding: 2px;"> <b> TOTAL </b> : <?php echo $currency." ".number_format(($total_cash+$total_other+$total_gratis),2); ?> </div> </td>
                 <td> <div style="padding: 2px;"> <b> <?php echo strtoupper($payment_mode_other);?> </b> : <?php echo $currency." ".number_format($total_other,2); ?> </div> </td>
                 <td> <div style="padding: 2px;"> <b> GRATIS </b> : <?php echo $currency." ".number_format($total_gratis,2); ?> </div> </td>
                 <td> <div style="padding: 2px;"> <b> CASH </b> : <?php echo $currency." ".number_format($total_cash,2); ?> </div> </td>
                 <td> <div style="padding: 2px;"> <b> DISCOUNT </b> : <?php echo $currency." ".number_format($discount_amount,2); ?> </div> </td>
             </tr>
         </tbody>
     </table>

     <div class="btn-group btn-group-devided" data-toggle="buttons" style="float:right">
     <?php if(mysql_num_rows($job_numbers) > 0){ 
         while($row_job_nos = mysql_fetch_assoc($job_numbers)){ ?>
     <label id="" onclick="getForwardPatient('forward_to_procurement', '#view_items_response_wrapper_2', '<?php echo $process_claim_no.'[===]'.$row_job_nos['job_number'];?>', 'Forward Job Number <?php echo $row_job_nos['job_number']; ?>?', 'You want to Forward this Job to Procurement?');" class="btn red btn-outline btn-circle btn-sm active">
            <input type="radio" name="options" class="toggle" id="option2"> Forward Job Number <?php echo $row_job_nos['job_number']; ?> <i class="fa fa-forward"></i>
     </label>
     <?php } } ?>
     </div>

     <div class="btn-group btn-group-devided" data-toggle="buttons" style="float:right;display: none;" id="swap_cancel_item_wrapper">
         <label onclick="getData('fetch_cancellation_items', '<?php echo $process_claim_no; ?>', '#cancellation_item_wrapper');" data-toggle="modal" href="#cancellation" class="btn red btn-outline btn-circle btn-sm active" data-toggle="modal" href="#large">
         <!--<label onclick="SwapCancelItem('swap_items','','<?php echo $process_claim_no; ?>','Swap Selected Items', 'You are about to Swap the selected Items. Please note, Approval is needed before swapping can be done.', 'Swap Items');" id="hide_discount_btn" class="btn red btn-outline btn-circle btn-sm active" data-toggle="modal" href="#large">-->
             <input type="radio" name="options" class="toggle"> Cancel Item(s) <i class="fa fa-close"></i>
         </label>
     </div>
                                     </div>
                        <div class="portlet-body">
                            
                            <div id="claimsno_diagnostic" class="slimScrollDiv" style="height: 305px;overflow-y: scroll;overflow-x: hidden;">
                                <div id="claim_response">
                                   
                                     <table class="table table-bordered">
                                         <thead>
                                             <tr>
                                                 <th style="">Item</th>
                                                 <!-- <th>Eye</th> -->
                                                 <th>Job No.</th>
                                                 <th style="text-align:right">Qty</th>
                                                 <th style="text-align:right">Price</th>
                                                 <th style="text-align:right"></th>
                                                 <th style="">Mode</th>
                                                 <th style="">Status</th>
                                                 <th style="">Action</th>
                                             </tr>
                                         </thead>
                                                <tbody>
                                                <?php if ($totalRows_rsclaim> 0) { 
                                                    do {$i = $i + 1; ?>
                                                        <tr>
                                                          <td>
                                                          <?php // if(($row_rsclaim['payment_status'] == 'Owing' || $row_rsclaim['payment_status'] == 'Paid' || $row_rsclaim['payment_status'] == 'Approved') && $row_rsclaim['mode_of_payment'] != 'gratis' && ($row_rsclaim['item_service_type']=='Optical Lens' || $row_rsclaim['item_service_type'] == 'Optical Frame') && $row_rsclaim['discount_applied']=='not applied'){ ?>
                                                              <?php if($row_rsclaim['discount_applied']=='not applied' || $row_rsclaim['job_number']==''){ ?>
                                                              <input type="hidden" multiple="" name="item_trans_id[]" value="<?php echo $row_rsclaim['id'];?>">
                                                              <label class="mt-checkbox item_check_box_job_no">
                                                                  <input onchange="BoxesforJobNumber();" type="checkbox" class="item_check_box" multiple="" name="item_trans_amount[]" value="<?php echo ($row_rsclaim['qty']*$row_rsclaim['unit_price']).'[===]'.$row_rsclaim['id'];?>">
                                                              <span></span>
                                                               </label>
                                                            <?php } ?>

                                                          <?php 
                                                          if($row_rsclaim['item'] == 'Client Own Frame'){
                                                                echo strtoupper($row_rsclaim['item']);?>
                                                                <input onChange="searchItemsName('update_claim_details_administrative','#view_items_response_wrapper_2','<?php echo $row_rsclaim['id']; ?>','rx_prescription_name',this.value);" placeholder="Description" type="text" name="description" id="description" value="<?php echo $row_rsclaim['rx_prescription_name']; ?>">
                                                         <?php }else{
                                                                echo strtoupper($row_rsclaim['item']); 
                                                          } ?> </td>
                                                          <!-- <td> -->
                                                        <?php // if($row_rsclaim['item_service_type'] == 'Optical Lens'){ ?>
                                                          <!-- <select onChange="searchItemsName('update_claim_details_administrative','#view_items_response_wrapper_2','<?php echo $row_rsclaim['id']; ?>','eye_optical',this.value);">
                                                             <option <?php if($row_rsclaim['eye_optical'] == 'R'){echo 'selected=""';} ?> value="R">R</option>
                                                             <option <?php if($row_rsclaim['eye_optical'] == 'L'){echo 'selected=""';} ?> value="L">L</option>
                                                             <option <?php if($row_rsclaim['eye_optical'] == 'B'){echo 'selected=""';} ?> value="B">B</option>
                                                          </select> -->
                                                        <?php // } ?>
                                                          <!-- </td> -->
                                                        <td><?php echo $row_rsclaim['job_number'];?></td>
                                                        <td style="text-align:right">
                                                            <?php if($row_rsclaim['payment_status']=='Approved'||$row_rsclaim['payment_status']=='Approved-Paid'||$row_rsclaim['payment_status']=='Paid-Cancelled'|| ($row_rsclaim['payment_status']=='Paid' && $row_rsclaim['mode_of_payment']=='cash') || $row_rsclaim['submitted_status']=='submitted'){echo $row_rsclaim['qty'];}else{ ?>
                                                            <input type="number" id="itemqty<?php echo $row_rsclaim['id']; ?>" name="itemqty<?php echo $row_rsclaim['id']; ?>" style="width:40px;" min="1" value="<?php echo $row_rsclaim['qty']; ?>" onChange="searchItemsName('update_claim_details_administrative','#view_items_response_wrapper_2','<?php echo $row_rsclaim['id']; ?>','qty',this.value);">
                                                           <?php } ?>
                                                        </td>
                                                        <td style="text-align:right"> 
                                                            <?php 
                                                            if($row_rsclaim['item_service_type'] == 'Optical Lens'){ ?>
                                                            <input type="text" id="itemqty<?php echo $row_rsclaim['id']; ?>" name="itemqty<?php echo $row_rsclaim['id']; ?>" style="width:40px;" min="1" value="<?php echo $row_rsclaim['unit_price']; ?>" onChange="searchItemsName('update_claim_details_administrative','#view_items_response_wrapper_2','<?php echo $row_rsclaim['id']; ?>','unit_price',this.value);">
                                                           <?php }else{
                                                            echo number_format($row_rsclaim['unit_price'],2); 
                                                            }
                                                            ?>
                                                        </td>
                                                        <td style="text-align:right"><?php echo number_format($row_rsclaim['qty']*$row_rsclaim['unit_price'],2);?></td>
                                                        <td><?php echo strtoupper($row_rsclaim['mode_of_payment']); ?></td>
                                                        <td><?php if($row_rsclaim['payment_status']=='Owing' || $row_rsclaim['payment_status']=='Unapproved'){ 
														  echo "<span style='color:red;'>".$row_rsclaim['payment_status']."</span>";
														 }else{
														  echo "<span style='color:#090;'>".$row_rsclaim['payment_status']."</span>";
														 }?>
                                                        </td>
                                                        <td style="text-align:center;">
                                                          <?php if($row_rsclaim['action_status']!='Sent to Claims'){?>
                                                          <span style="font-weight:bold;"> 
                     <?php if($row_rsclaim['payment_status']=='Approved'||$row_rsclaim['payment_status']=='Approved-Paid'|| ($row_rsclaim['payment_status']=='Paid' && $row_rsclaim['mode_of_payment']=='cash') || $row_rsclaim['submitted_status']=='submitted'){ ?>
                         <label class="mt-checkbox mt-checkbox-outline swap_cancel_items">
                             <input onchange="BoxesforSwapCancel();" value="<?php echo $row_rsclaim['id']; ?>" name="item_ids_[]" multiple="" class="item_check_box" type="checkbox">
                             <span></span>
                         </label> 
                           <!--<i style="color:red;" class="fa fa-refresh"></i>-->
                     <?php }else{ ?>
                                                              <a style="color:red;" href="#" onclick="delInformtion('del_claim_details_administrative_administrative', '#view_items_response_wrapper_2', '<?php echo $row_rsclaim['id']; ?>', '<?php echo strtoupper($row_rsclaim['item']); ?> ?', 'You are about to Delete <?php echo strtoupper($row_rsclaim['item']); ?> from this Patient.', '<?php echo strtoupper($row_rsclaim['item']); ?> Deleted Successfully !');"> <i id="trash_<?php echo $row_rsclaim['id']; ?>" class="icon-trash"></i> <i style="display: none;" id="spin_refresh_<?php echo $row_rsclaim['id']; ?>" class="fa fa-refresh fa-spin fa-fw"></i>  </a>
                    <?php } ?>
                    
                    </span>
                    <?php }?>
                    </td>
                    </tr>
                  <?php }  while ($row_rsclaim = mysql_fetch_assoc($rsclaim)); ?>
                  <?php }else{echo '<tr><td colspan="9"><div class="none">No Billed Items Found. Search Items to Add to Bill.</div></td></tr>';}?>
                                                </tbody>
                </table> 

                                </div>  
                            </div>
                        </div>


<div class="modal fade bs-modal-lg" id="large" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                <h4 class="modal-title">Print Receipt</h4>
            </div>
            <div class="modal-body" id="receipt_wrapper">

            </div>
            <div class="modal-footer">
                <button type="button" class="btn red btn-outline" data-dismiss="modal">Close</button>
                <!--<button type="button" class="btn red" onclick="">Print Receipts</button>-->
            </div>
        </div>
    </div>
</div>
             
<div class="modal fade bs-modal-lg" id="cancellation" tabindex="-1" role="dialog" aria-hidden="true">
    <div class="modal-dialog modal-lg">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true"></button>
                <h4 class="modal-title">Cancellation of Items</h4>
            </div>
            <div class="modal-body" id="cancellation_item_wrapper">

            </div>
            <div class="modal-footer">
                <button id="close_modal_id" type="button" class="btn red btn-outline" data-dismiss="modal">Close</button>
                <button type="button" class="btn red" onclick="processInfo('forward_for_approval_cancel_item','<?php echo $row_rsodest_claim_no['patient_no'] ?>');">Forward for Approval</button>
            </div>
        </div>
    </div>
</div>