<?php
    $CI =& get_instance();
    //Earning graph
    $CI->load->model('Retainedearning');
    $earning[$this->lang->line('lastYear')] = $CI->Retainedearning->incomeNexpenses($lastYear,$lastPeriod,1);
    for($i = $start; $i <= $end; $i++){
      $earning[$this->lang->line('monthSrt'.$i).$year] = $CI->Retainedearning->incomeNexpenses($year,$i,2);
    }

    //Customer due invoiced amount
      $CI->lang->load("saleInvoice",$this->session->userdata('language'));
      $CI->load->model('salesInvoice_model');
      $salesDue = $CI->salesInvoice_model->salesInvoieDue();
?>
<div class="row-fluid scRow">
    <!-- Company Profit and Loss -->
    <div class="span6 scCol">
        <div class="block">
            <div class="head">                                        
              <h2><?php echo $this->lang->line('salesDueInvoices'); ?></h2>
              <ul class="buttons">
                <li><a href="#" class="block_loading" onclick="drawChart()"><span class="i-cycle"></span></a></li>
                <li><a href="#" class="block_toggle"><span class="i-arrow-down-3"></span></a></li>
              </ul>
            </div>
            <div class="content">
                <table cellpadding="0" cellspacing="0" width="100%" class="list" id="invoicesAr">
                    <thead>
                        <tr>
                            <th class="span1"><?php echo $this->lang->line('date')?></th>
                            <th class="span3"><?php echo $this->lang->line('custInvoiceNo')?></th>
                            <th class="span3"><?php echo $this->lang->line('custName')?></th>
                            <th class="span2"><?php echo $this->lang->line('contact')?></th>
                            <th class="span2 tar"><?php echo $this->lang->line('outstandingAmount')?></th>
                        </tr>
                    </thead>
                    <tbody>
                            <?php foreach($salesDue[1] as $v): ?>
                        <tr>
                            <td class="span1"><?php echo date("d-m-Y",strtotime($v->salesInvoiceDate)); ?></td>  
                            <td class="span3"><a href="<?php echo base_url(); ?>artransaction/newReceivePayment/<?php echo $v->custID.'/'.$v->ID; ?>" target="_blank"><?php echo $v->formNo; ?></a></td>  
                            <td class="span3"><?php echo $v->customerName; ?></td>  
                            <td class="span2"><?php echo $v->phoneNumber1; ?></td>  
                            <td class="span2 tar"><?php echo $v->totalReceivable; ?></td>  
                        </tr>
                            <?php endforeach; ?>
                    </tbody>
                    <tfoot>
                        <tr>
                            <td colspan="5" align="right">
                                <a class="btn-form btn-info-form" href="<?php echo base_url(); ?>artransaction/newReceivePayment" target="_blank"><?php echo $this->lang->line('receivePayment')?></a>
                            </td>
                        </tr>
                    </tfoot>
                </table>
            </div>
        </div>
    </div>
    <div class="span6 scCol">
        <div class="block">
            <div class="head">                                        
              <h2><?php echo $this->lang->line('salesDue'); ?></h2>
              <ul class="buttons">
                <li><a href="#" class="block_loading" onclick="drawChart()"><span class="i-cycle"></span></a></li>
                <li><a href="#" class="block_toggle"><span class="i-arrow-down-3"></span></a></li>
              </ul>
            </div>
            <div class="content">
              <div id="toolbar_div"></div>
              <div id="salesDue" style=" margin: 0px auto;"></div>
            </div>
        </div>
    </div>
</div>

<script>
  
     google.load("visualization", "1", {packages:["corechart"]});
    google.setOnLoadCallback(barChart1);
    function salesDue() {
      <?php $totalAmount = $salesDue[0][0]->totalSale+$salesDue[0][0]->amountPaid+$salesDue[0][0]->unPaid; ?>
      console.log(<?php echo number_format($salesDue[0][0]->unPaid/$totalAmount * 100,2,'.',''); ?>);
      var data = google.visualization.arrayToDataTable([
          ['Year', 'Sales'],
          ["Total Sale",<?php echo number_format(($salesDue[0][0]->totalSale/$totalAmount * 100),2,'.',''); ?>],
          ["Total Paid",<?php echo number_format(($salesDue[0][0]->amountPaid/$totalAmount * 100),2,'.',''); ?>],
          ["Total Unpaid",<?php echo number_format(($salesDue[0][0]->unPaid/$totalAmount * 100),2,'.',''); ?>],
        ]);

        var options = {
          title: '<?php 
              if(!empty($salesDue[0][0])){
                    echo $this->lang->line("salesStatus");
                  }
              ?>',
          is3D: true,
        };

        var chart = new google.visualization.PieChart(document.getElementById('salesDue'));
        chart.draw(data, options);
    }

    function drawToolbar() {
      var components = [
          {type: 'igoogle', datasource: 'https://spreadsheets.google.com/tq?key=pCQbetd-CptHnwJEfo8tALA',
           gadget: 'https://www.google.com/ig/modules/pie-chart.xml',
           userprefs: {'3d': 1}},
          {type: 'html', datasource: 'https://spreadsheets.google.com/tq?key=pCQbetd-CptHnwJEfo8tALA'},
          {type: 'csv', datasource: 'https://spreadsheets.google.com/tq?key=pCQbetd-CptHnwJEfo8tALA'},
          {type: 'htmlcode', datasource: 'https://spreadsheets.google.com/tq?key=pCQbetd-CptHnwJEfo8tALA',
           gadget: 'https://www.google.com/ig/modules/pie-chart.xml',
           userprefs: {'3d': 1},
           style: 'width: 800px; height: 700px; border: 3px solid purple;'}
      ];

      var container = document.getElementById('toolbar_div');
      google.visualization.drawToolbar(container, components);
    };

    $(window).resize(function(){
        salesDue();
    });
</script>