<!DOCTYPE html>
<html>
<head>
<!-- TABLES CSS CODE -->
<?php include"comman/code_css.php"; ?>
<!-- </copy> -->  
</head>
<body class="hold-transition skin-blue sidebar-mini">

<div class="wrapper">

  <?php include"sidebar.php"; ?>

  <!-- Content Wrapper. Contains page content -->
  <div class="content-wrapper">
    <!-- Content Header (Page header) -->
    <section class="content-header">
      <h1>
        <?= $this->lang->line('invoice'); ?>
        <small>Add/Update Invoice</small>
      </h1>
      <ol class="breadcrumb">
        <li><a href="<?php echo $base_url; ?>dashboard"><i class="fa fa-dashboard"></i> Home</a></li>
        <li><a href="<?php echo $base_url; ?>purchase"><?= $this->lang->line('purchase_list'); ?></a></li>
        <li><a href="<?php echo $base_url; ?>purchase/add"><?= $this->lang->line('new_purchase'); ?></a></li>
        <li class="active"><?= $this->lang->line('invoice'); ?></li>
      </ol>
    </section>
    <div class="row">
      <div class="col-md-12">
      <!-- ********** ALERT MESSAGE START******* -->
      <?php include"comman/code_flashdata.php"; ?>
      <!-- ********** ALERT MESSAGE END******* -->
      </div>
    </div>
    <?php
    $CI =& get_instance();
    
    // Helper function to format currency with correct symbol
    function format_currency_with_symbol($amount, $symbol) {
        return $symbol . ' ' . number_format($amount, 2);
    }
    $q3=$this->db->query("SELECT b.store_id,a.supplier_name,a.mobile,a.phone,a.gstin,a.tax_number,a.email,
                           a.opening_balance,a.country_id,a.state_id,a.city,
                           a.postcode,a.address,b.purchase_date,b.reference_no,
                           b.purchase_code,b.purchase_status,b.purchase_note,
                           coalesce(b.grand_total,0) as grand_total,
                           coalesce(b.subtotal,0) as subtotal,
                           coalesce(b.paid_amount,0) as paid_amount,
                           coalesce(b.other_charges_input,0) as other_charges_input,
                           other_charges_tax_id,
                           coalesce(b.other_charges_amt,0) as other_charges_amt,
                           discount_to_all_input,
                           b.discount_to_all_type,
                           coalesce(b.tot_discount_to_all_amt,0) as tot_discount_to_all_amt,
                           coalesce(b.round_off,0) as round_off,
                           b.payment_status,
                           b.transaction_currency_id,
                           b.exchange_rate,
                           b.base_currency_total,
                           c.currency_name,
                           c.currency_code,
                           c.symbol as currency_symbol

                           FROM db_suppliers a,
                           db_purchase b 
                           LEFT JOIN db_currency c ON b.transaction_currency_id = c.id
                           WHERE 
                           a.`id`=b.`supplier_id` AND 
                           b.`id`='$purchase_id' AND b.store_id=".get_current_store_id());
                        
    
    $res3=$q3->row();
    if(!$res3){
      echo "Error: Purchase record not found.";
      exit();
    }
    if($res3->store_id!=get_current_store_id()){
      $CI->show_access_denied_page();exit();
    }
    
    // Determine currency symbol to use
    $currency_symbol = '$'; // Default to base currency
    if (!empty($res3->transaction_currency_id) && !empty($res3->currency_symbol)) {
        $currency_symbol = $res3->currency_symbol;
    } else {
        // Fallback to base currency
        if (function_exists('get_base_currency')) {
            $base_currency = get_base_currency();
            $currency_symbol = $base_currency->symbol;
        } else {
            $currency_symbol = '$'; // Ultimate fallback
        }
    }
    
    $supplier_name=$res3->supplier_name;
    $supplier_mobile=$res3->mobile;
    $supplier_phone=$res3->phone;
    $supplier_email=$res3->email;
    $supplier_state=$res3->state_id;
    $supplier_city=$res3->city;
    $supplier_address=$res3->address;
    $supplier_postcode=$res3->postcode;
    $supplier_gst_no=$res3->gstin;
    $supplier_tax_number=$res3->tax_number;
    $supplier_opening_balance=$res3->opening_balance;
    $purchase_date=$res3->purchase_date;
    $reference_no=$res3->reference_no;
    $purchase_code=$res3->purchase_code;
    $purchase_status=$res3->purchase_status;
    $purchase_note=$res3->purchase_note;
    $supplier_country=get_country($res3->country_id);
    $supplier_state=get_state($res3->state_id);

    
    $subtotal=$res3->subtotal;
    $grand_total=$res3->grand_total;
    $other_charges_input=$res3->other_charges_input;
    $other_charges_tax_id=$res3->other_charges_tax_id;
    $other_charges_amt=$res3->other_charges_amt;
    $paid_amount=$res3->paid_amount;
    $discount_to_all_input=$res3->discount_to_all_input;
    $discount_to_all_type=$res3->discount_to_all_type;
    $discount_to_all_type = ($discount_to_all_type=='in_percentage') ? '%' : 'Fixed';
    $tot_discount_to_all_amt=$res3->tot_discount_to_all_amt;
    $round_off=$res3->round_off;
    $payment_status=$res3->payment_status;
  

    $q1=$this->db->query("select * from db_store where id=".$res3->store_id." ");
    $res1=$q1->row();
    $store_name=$res1->store_name;
    $company_mobile=$res1->mobile;
    $company_phone=$res1->phone;
    $company_email=$res1->email;
    // $company_country=$res1->country;
    // $company_state=$res1->state;
    $company_city=$res1->city;
    $company_address=$res1->address;
    $company_gst_no=$res1->gst_no;
    $company_vat_no=$res1->vat_no;
    $company_pan_no=$res1->pan_no;

    ?>


    <!-- Main content -->
    <section class="invoice">
      <!-- title row -->
      <div class="printableArea">
      <div class="row">
        <div class="col-xs-12">
          <h2 class="page-header">
            <i class="fa fa-globe"></i> <?= $this->lang->line('purchase_invoice'); ?>
            <small class="pull-right">Date: <?= show_date($purchase_date); ?></small>
          </h2>
        </div>
        <!-- /.col -->
      </div>
      <!-- info row -->
      <div class="row invoice-info">
        <div class="col-sm-4 invoice-col">
          <i><?= $this->lang->line('from'); ?></i>
          <address>
            <strong><?php echo  $store_name; ?></strong><br>
            <?php echo  $company_address; ?>,
            <?= $this->lang->line('city'); ?>:<?php echo  $company_city; ?><br>
            <?= $this->lang->line('phone'); ?>: <?php echo  $company_phone; ?>,
            <?= $this->lang->line('mobile'); ?>: <?php echo  $company_mobile; ?><br>
            <?php echo (!empty(trim($company_email))) ? $this->lang->line('email').": ".$company_email."<br>" : '';?>
            <?php echo (!empty(trim($company_gst_no)) && gst_number()) ? $this->lang->line('gst_number').": ".$company_gst_no."<br>" : '';?>
            <?php echo (!empty(trim($company_vat_no)) && vat_number()) ? $this->lang->line('vat_number').": ".$company_vat_no."<br>" : '';?>
            <?php echo (!empty(trim($company_pan_no)) && pan_number()) ? $this->lang->line('pan_number').": ".$company_pan_no."<br>" : '';?>
           
          </address>
        </div>
        <!-- /.col -->
        <div class="col-sm-4 invoice-col">
          <i><?= $this->lang->line('supplier_details'); ?><br></i>
          <address>
            <strong><?php echo  $supplier_name; ?></strong><br>
            <?php 
              if(!empty($supplier_address)){
                echo $supplier_address;
              }
              if(!empty($supplier_country)){
                echo $supplier_country;
              }
              if(!empty($supplier_state)){
                echo ",".$supplier_state;
              }
              if(!empty($supplier_city)){
                echo ",".$supplier_city;
              }
              if(!empty($supplier_postcode)){
                echo "-".$supplier_postcode;
              }
            ?>
            <br>
            <?php echo (!empty(trim($supplier_mobile))) ? $this->lang->line('mobile').": ".$supplier_mobile."<br>" : '';?>
            <?php echo (!empty(trim($supplier_phone))) ? $this->lang->line('phone').": ".$supplier_phone."<br>" : '';?>
            <?php echo (!empty(trim($supplier_email))) ? $this->lang->line('email').": ".$supplier_email."<br>" : '';?>
            <?php echo (!empty(trim($supplier_gst_no)) && gst_number()) ? $this->lang->line('gst_number').": ".$supplier_gst_no."<br>" : '';?>
            <?php echo (!empty(trim($supplier_tax_number))) ? $this->lang->line('tax_number').": ".$supplier_tax_number."<br>" : '';?>

          </address>
        </div>
        <!-- /.col -->
        <div class="col-sm-4 invoice-col">
          <b><?= $this->lang->line('invoice'); ?> #<?php echo  $purchase_code; ?></b><br>
          <b><?= $this->lang->line('purchase_status'); ?> :<?php echo  $purchase_status; ?></b><br>
         
        </div>
        <!-- /.col -->
      </div>
      <!-- /.row -->

      <!-- Table row -->
      <div class="row">
        <div class="col-xs-12 table-responsive">
          <table class="table  records_table table-bordered">
            <thead class="bg-gray-active">
            <tr>
              <th>#</th>
              <th><?= $this->lang->line('item_name'); ?></th>
              <th><?= $this->lang->line('purchase_price'); ?></th>
              <th><?= $this->lang->line('quantity'); ?></th>
              <th><?= $this->lang->line('tax'); ?></th>
              <th><?= $this->lang->line('tax_amount'); ?></th>
              <th><?= $this->lang->line('discount'); ?></th>
              <th><?= $this->lang->line('discount_amount'); ?></th>
              <th><?= $this->lang->line('unit_cost'); ?></th>
              <th><?= $this->lang->line('total_amount'); ?></th>
            </tr>
            </thead>
            <tbody>

              <?php
              $i=0;
              $tot_qty=0;
              $tot_purchase_price=0;
              $tot_tax_amt=0;
              $tot_discount_amt=0;
              $tot_unit_total_cost=0;
              $tot_total_cost=0;
              $tax_summary = array(); // Array to hold tax breakdown
              $q2=$this->db->query("SELECT a.description,c.item_name, a.purchase_qty,a.tax_type,
                                  a.price_per_unit, b.tax,b.tax_name,a.tax_amt,
                                  a.discount_input,a.discount_amt, a.unit_total_cost,
                                  a.total_cost 
                                  FROM 
                                  db_purchaseitems AS a,db_tax AS b,db_items AS c 
                                  WHERE 
                                  c.id=a.item_id AND b.id=a.tax_id AND a.purchase_id='$purchase_id'");
              foreach ($q2->result() as $res2) {
                  // Build tax summary
                  $tax_rate = floatval($res2->tax);
                  $tax_key = $tax_rate . '%';
                  if ($tax_rate == 0) {
                      $tax_key = (stripos($res2->tax_name, 'exempt') !== false) ? 'exempt' : '0%';
                  }
                  
                  if (!isset($tax_summary[$tax_key])) {
                      $tax_summary[$tax_key] = array(
                          'rate' => $tax_rate,
                          'name' => $res2->tax_name,
                          'taxable' => 0,
                          'tax' => 0,
                          'total' => 0
                      );
                  }
                  
                  // Ensure we have valid numeric values
                  $item_total_cost = floatval($res2->total_cost);
                  $item_tax_amt = floatval($res2->tax_amt);
                  $item_tax_rate = floatval($tax_rate);
                  
                  // If values are invalid (likely old bad data), recalculate from price and tax
                  if ($item_total_cost <= 0 || $item_total_cost == 1) {
                      // Recalculate from price, qty, tax, and discount
                      $item_price = floatval($res2->price_per_unit);
                      $item_qty = floatval($res2->purchase_qty);
                      $item_discount = floatval($res2->discount_amt);
                      
                      if ($item_price > 0 && $item_qty > 0) {
                          $base_amount = ($item_price * $item_qty) - $item_discount;
                          
                          if ($res2->tax_type == 'Inclusive') {
                              // For inclusive: base_amount already includes tax
                              // Calculate: base_amount = taxable * (1 + tax_rate/100)
                              // So: taxable = base_amount / (1 + tax_rate/100)
                              if ($item_tax_rate > 0) {
                                  $item_taxable_amt = $base_amount / (1 + ($item_tax_rate / 100));
                                  $item_tax_amt = $base_amount - $item_taxable_amt;
                              } else {
                                  $item_taxable_amt = $base_amount;
                                  $item_tax_amt = 0;
                              }
                              $item_total_cost = $base_amount;
                          } else {
                              // For exclusive: base_amount is taxable, add tax
                              $item_taxable_amt = $base_amount;
                              if ($item_tax_rate > 0) {
                                  $item_tax_amt = $base_amount * ($item_tax_rate / 100);
                              } else {
                                  $item_tax_amt = 0;
                              }
                              $item_total_cost = $item_taxable_amt + $item_tax_amt;
                          }
                      } else {
                          // Can't recalculate, use existing values but ensure they're valid
                          $item_taxable_amt = max(0, $item_total_cost - $item_tax_amt);
                      }
                  } else {
                      // Calculate taxable amount correctly based on tax type
                      if ($res2->tax_type == 'Inclusive') {
                          // For Inclusive: total_cost includes tax
                          // taxable = total_cost / (1 + tax_rate/100) OR total_cost - tax_amt
                          if ($item_tax_rate > 0 && $item_total_cost > 0) {
                              $item_taxable_amt = $item_total_cost / (1 + ($item_tax_rate / 100));
                              // Recalculate tax_amt to ensure consistency
                              $item_tax_amt = $item_total_cost - $item_taxable_amt;
                          } else {
                              $item_taxable_amt = max(0, $item_total_cost - $item_tax_amt);
                          }
                      } else {
                          // For Exclusive: total_cost = taxable + tax_amt, so taxable = total_cost - tax_amt
                          $item_taxable_amt = $item_total_cost - $item_tax_amt;
                      }
                      
                      // Ensure taxable is not negative
                      if ($item_taxable_amt < 0) {
                          $item_taxable_amt = 0;
                      }
                  }
                  
                  $tax_summary[$tax_key]['taxable'] += $item_taxable_amt;
                  $tax_summary[$tax_key]['tax'] += $item_tax_amt;
                  $tax_summary[$tax_key]['total'] += $item_total_cost;
                  $str = ($res2->tax_type=='Inclusive')? 'Inc.' : 'Exc.';
                  $discount = (empty($res2->discount_input)||$res2->discount_input==0)? '0':store_number_format($res2->discount_input)."%";
                  $discount_amt = (empty($res2->discount_amt)||$res2->discount_input==0)? '0':$res2->discount_amt."";
                  echo "<tr>";  
                  echo "<td>".++$i."</td>";
                  echo "<td>";
                    echo $res2->item_name;
                    echo (!empty($res2->description)) ? "<br><i>[".nl2br($res2->description)."]</i>" : '';
                  echo "</td>";
                  echo "<td>".format_currency_with_symbol($res2->price_per_unit, $currency_symbol)."</td>";
                  echo "<td>".format_qty($res2->purchase_qty)."</td>";
                  echo "<td>".$res2->tax_name."[".$str."]</td>";
                  echo "<td class='text-right'>".format_currency_with_symbol($res2->tax_amt, $currency_symbol)."</td>";
                  echo "<td class='text-right'>".$discount."</td>";
                  echo "<td class='text-right'>".format_currency_with_symbol($discount_amt, $currency_symbol)."</td>";
                  // Check if total_cost is invalid (1 or 0) and recalculate if needed
                  $display_total_cost = floatval($res2->total_cost);
                  $display_unit_total_cost = floatval($res2->unit_total_cost);
                  
                  if ($display_total_cost <= 0 || $display_total_cost == 1) {
                      // Recalculate from price, qty, tax, and discount
                      $calc_price = floatval($res2->price_per_unit);
                      $calc_qty = floatval($res2->purchase_qty);
                      $calc_discount = floatval($res2->discount_amt);
                      $calc_tax = floatval($res2->tax_amt);
                      
                      if ($calc_price > 0 && $calc_qty > 0) {
                          $base_amt = ($calc_price * $calc_qty) - $calc_discount;
                          if ($res2->tax_type == 'Inclusive') {
                              $display_total_cost = $base_amt;
                          } else {
                              $display_total_cost = $base_amt + $calc_tax;
                          }
                          $display_unit_total_cost = ($calc_qty > 0) ? $display_total_cost / $calc_qty : 0;
                      }
                  }
                  
                  echo "<td class='text-right'>".format_currency_with_symbol($display_unit_total_cost, $currency_symbol)."</td>";
                  echo "<td class='text-right'>".format_currency_with_symbol($display_total_cost, $currency_symbol)."</td>";
                  echo "</tr>";  
                  $tot_qty +=$res2->purchase_qty;
                  $tot_purchase_price +=$res2->price_per_unit;
                  $tot_tax_amt +=$res2->tax_amt;
                  $tot_discount_amt +=$res2->discount_amt;
                  $tot_unit_total_cost +=$display_unit_total_cost;
                  $tot_total_cost +=$display_total_cost;
              }
              ?>
         
      
            </tbody>
            <tfoot class="text-right text-bold bg-gray">
              <tr>
                <td colspan="3" class="text-center"><?= $this->lang->line('total'); ?></td>
                <td class="text-left"><?=format_qty($tot_qty);?></td>
                <td>-</td>
                <td><?=format_currency_with_symbol($tot_tax_amt, $currency_symbol);?></td>
                <td>-</td>
                <td><?= format_currency_with_symbol($tot_discount_amt, $currency_symbol) ;?></td>
                <td><?= format_currency_with_symbol($tot_unit_total_cost, $currency_symbol) ;?></td>
                <td><?= format_currency_with_symbol($tot_total_cost, $currency_symbol) ;?></td>
              </tr>
            </tfoot>
          </table>
        </div>
        <!-- /.col -->
      </div>
      <!-- /.row -->
    
      <div class="row">
       <div class="col-md-6">
           <div class="row">
              <div class="col-md-12">
                 <div class="form-group">
                    <label for="discount_to_all_input" class="col-sm-4 control-label" style="font-size: 17px;"><?= $this->lang->line('discount_on_all'); ?></label>    
                    <div class="col-sm-8">
                       <label class="control-label  " style="font-size: 17px;">: <?=$discount_to_all_input; ?> (<?= $discount_to_all_type ?>)</label>
                    </div>
                 </div>
              </div>
           </div>
          <div class="row">
              <div class="col-md-12">
                 <div class="form-group">
                    <label for="purchase_note" class="col-sm-4 control-label" style="font-size: 17px;"><?= $this->lang->line('note'); ?></label>    
                    <div class="col-sm-8">
                       <label class="control-label  " style="font-size: 17px;">: <?=$purchase_note;?></label>
                    </div>
                 </div>
              </div>
           </div> 
           <div class="row">
              <div class="col-md-12">
                 <div class="form-group">
                    <table class="table table-hover table-bordered" style="width:100%" id=""><h4 class="box-title text-info"><?= $this->lang->line('payments_information'); ?> : </h4>
                       <thead>
                          <tr class="bg-purple " >
                             <th>#</th>
                             <th><?= $this->lang->line('date'); ?></th>
                             <th><?= $this->lang->line('payment_type'); ?></th>
                             <th><?= $this->lang->line('account'); ?></th>
                             <th><?= $this->lang->line('payment_note'); ?></th>
                             <th><?= $this->lang->line('payment'); ?></th>
                          </tr>
                       </thead>
                       <tbody>
                          <?php 
                            if(isset($purchase_id)){
                              $q3 = $this->db->query("select * from db_purchasepayments where purchase_id=$purchase_id");
                              if($q3->num_rows()>0){
                                $i=1;
                                $total_paid = 0;
                                foreach ($q3->result() as $res3) {
                                  echo "<tr class='text-center text-bold' id='payment_row_".$res3->id."'>";
                                  echo "<td>".$i++."</td>";
                                  echo "<td>".show_date($res3->payment_date)."</td>";
                                  echo "<td>".$res3->payment_type."</td>";
                                  echo "<td>".get_account_name($res3->account_id)."</td>";
                                  echo "<td>".$res3->payment_note."</td>";
                                  echo "<td class='text-right'>".format_currency_with_symbol($res3->payment, $currency_symbol)."</td>";
                                  echo "</tr>";
                                  $total_paid +=$res3->payment;
                                }
                                echo "<tr class='text-right text-bold'><td colspan='5' >Total</td><td>".format_currency_with_symbol($total_paid, $currency_symbol)."</td></tr>";
                              }
                              else{
                                echo "<tr><td colspan='6' class='text-center text-bold'>No Previous Payments Found!!</td></tr>";
                              }

                            }
                            else{
                              echo "<tr><td colspan='6' class='text-center text-bold'>Payments Pending!!</td></tr>";
                            }
                          ?>
                       </tbody>
                    </table>
                 </div>
              </div>
           </div>           
        </div>

        <div class="col-md-6">
           <div class="row">
              <div class="col-md-12">
                 <div class="form-group">
                    <?php if (!empty($tax_summary)) { ?>
                    <div class="box box-info">
                       <div class="box-header with-border">
                          <h3 class="box-title"><?= $this->lang->line('tax_summary'); ?></h3>
                       </div>
                       <div class="box-body table-responsive">
                          <table class="table table-bordered">
                             <thead>
                                <tr class="bg-gray">
                                   <th style="text-align:center;"><?= $this->lang->line('tax_rate'); ?></th>
                                   <th style="text-align:right;"><?= $this->lang->line('taxable_amount'); ?></th>
                                   <th style="text-align:right;"><?= $this->lang->line('tax_amount'); ?></th>
                                   <th style="text-align:right;"><?= $this->lang->line('total'); ?></th>
                                </tr>
                             </thead>
                             <tbody>
                                <?php 
                                $total_taxable = 0;
                                $total_tax = 0;
                                $total_amount = 0;
                                // Sort by tax rate descending
                                krsort($tax_summary);
                                foreach ($tax_summary as $key => $tax_item) {
                                    $rate_label = $tax_item['rate'] . '%';
                                    if ($key == 'exempt') {
                                        $rate_label = 'Ex.';
                                    } elseif ($tax_item['rate'] == 0) {
                                        $rate_label = '0%';
                                    }
                                    $total_taxable += $tax_item['taxable'];
                                    $total_tax += $tax_item['tax'];
                                    $total_amount += $tax_item['total'];
                                    echo '<tr>';
                                    echo '<td style="text-align:center;">' . $rate_label . '</td>';
                                    echo '<td style="text-align:right;">' . format_currency_with_symbol($tax_item['taxable'], $currency_symbol) . '</td>';
                                    echo '<td style="text-align:right;">' . format_currency_with_symbol($tax_item['tax'], $currency_symbol) . '</td>';
                                    echo '<td style="text-align:right;">' . format_currency_with_symbol($tax_item['total'], $currency_symbol) . '</td>';
                                    echo '</tr>';
                                }
                                ?>
                                <tr class="bg-gray text-bold">
                                   <td style="text-align:center;"><?= $this->lang->line('total'); ?></td>
                                   <td style="text-align:right;"><?= format_currency_with_symbol($total_taxable, $currency_symbol); ?></td>
                                   <td style="text-align:right;"><?= format_currency_with_symbol($total_tax, $currency_symbol); ?></td>
                                   <td style="text-align:right;"><?= format_currency_with_symbol($total_amount, $currency_symbol); ?></td>
                                </tr>
                             </tbody>
                          </table>
                       </div>
                    </div>
                    <?php } ?>
                 </div>
              </div>
           </div>
           <div class="row">
              <div class="col-md-12">
                 <div class="form-group">
                     
                    <table  class="col-md-11">
                       <tr>
                          <th class="text-right" style="font-size: 17px;"><?= $this->lang->line('subtotal'); ?></th>
                          <th class="text-right" style="padding-left:10%;font-size: 17px;">
                             <h4><b id="subtotal_amt" name="subtotal_amt"><?=format_currency_with_symbol($subtotal, $currency_symbol);?></b></h4>
                          </th>
                       </tr>
                       <tr>
                          <th class="text-right" style="font-size: 17px;"><?= $this->lang->line('other_charges'); ?></th>
                          <th class="text-right" style="padding-left:10%;font-size: 17px;">
                             <h4><b id="other_charges_amt" name="other_charges_amt"><?=format_currency_with_symbol($other_charges_amt, $currency_symbol);?></b></h4>
                          </th>
                       </tr>
                       <tr>
                          <th class="text-right" style="font-size: 17px;"><?= $this->lang->line('discount_on_all'); ?></th>
                          <th class="text-right" style="padding-left:10%;font-size: 17px;">
                             <h4><b id="discount_to_all_amt" name="discount_to_all_amt"><?=format_currency_with_symbol($tot_discount_to_all_amt, $currency_symbol);?></b></h4>
                          </th>
                       </tr>
                       <tr>
                          <th class="text-right" style="font-size: 17px;"><?= $this->lang->line('round_off'); ?></th>
                          <th class="text-right" style="padding-left:10%;font-size: 17px;">
                             <h4><b id="round_off_amt" name="tot_round_off_amt"><?=format_currency_with_symbol($round_off, $currency_symbol);?></b></h4>
                          </th>
                       </tr>
                       <tr>
                          <th class="text-right" style="font-size: 17px;"><?= $this->lang->line('grand_total'); ?></th>
                          <th class="text-right" style="padding-left:10%;font-size: 17px;">
                             <h4><b id="total_amt" name="total_amt"><?=format_currency_with_symbol($grand_total, $currency_symbol);?></b></h4>
                          </th>
                       </tr>
                    </table>
                 </div>
              </div>
           </div>
        </div>
        <!-- /.col -->
      </div>
      <!-- /.row -->

    </div><!-- printableArea -->
      <!-- this row will not appear when printing -->
      <div class="row no-print">
        <div class="col-xs-6">
          <?php if($CI->permissions('sales_edit')) { ?>
          <a href="<?php echo $base_url; ?>purchase/update/<?php echo  $purchase_id ?>" class="btn btn-success">
            <i class="fa  fa-edit"></i> Edit
          </a>
          <?php } ?>

          <a href='<?= base_url('items/labels/'.$purchase_id);?>' class="btn btn-info" title='Pop Up'><i class="fa fa-barcode"></i> Barcode</a>

          
          
          
          
          <?php if($CI->permissions('purchase_return_add')) { ?>
            <a href="<?php echo $base_url; ?>purchase_return/add/<?php echo  $purchase_id ?>" class="btn btn-danger">
            <i class="fa  fa-undo"></i> Purchase Return
          </a>
          <?php } ?>
       
          
          
        </div>
        <div class="col-xs-6 text-right">
          <a href="<?php echo $base_url; ?>purchase/print_invoice/<?php echo  $purchase_id ?>" target="_blank" class="btn btn-warning">
              <i class="fa fa-print"></i> 
            Print
          </a>

          <a href="<?php echo $base_url; ?>purchase/pdf/<?php echo  $purchase_id ?>" target="_blank" class="btn btn-primary">
              <i class="fa fa-file-pdf-o"></i> 
            PDF
          </a>
        </div>
      </div>

    </section>
    <!-- /.content -->
    <div class="clearfix"></div>
  </div>
  <!-- /.content-wrapper -->
  <?php include"footer.php"; ?>

 
  <!-- Add the sidebar's background. This div must be placed
       immediately after the control sidebar -->
  <div class="control-sidebar-bg"></div>
</div>
<!-- ./wrapper -->

<!-- SOUND CODE -->
<?php include"comman/code_js_sound.php"; ?>
<!-- TABLES CODE -->
<?php include"comman/code_js.php"; ?>

<!-- Make sidebar menu hughlighter/selector -->
<script>$(".purchase-list-active-li").addClass("active");</script>
</body>
</html>
