<?php
    if (!defined('BASEPATH')) exit('No direct script access allowed');
    class Productcountreport extends MY_Controller {
        
        function __construct() {
            parent::__construct();
            $this->load->model('settings/reports_model');
            $this->load->model('partner/customers_model');
            $this->load->library('Formatslist');
        }
        
        function index() {
            if($_POST){
                // Start date
                $data["date"] =  $_POST["datefrom"];
                // End date
                $data["end_date"] =  $_POST["dateto"];
                if($_POST["customerid"])
                    $customerid =   $_POST["customerid"];
                else
                    $customerid =   '';
                $data['productstotallist'] = $this->reports_model->productstotallist($data["date"],$data["end_date"],$customerid);
                $data['productcount'] = $this->reports_model->productcount($data["date"],$data["end_date"],$customerid);
                $data['producttotalcount'] = $this->reports_model->producttotalcount($data["date"],$data["end_date"],$customerid);
            //   echo "<pre>";print_r($data['producttotalcount']);exit;
            }
//            $data["customerslist"] = $this->common->getAll('CUSTOMERS');
            $data["customerslist"] = $this->db->select('ID,NAME')->order_by("NAME", "asc")->get('CUSTOMERS')->result();

            $data['template'] = 'reports/reports_productcount_view';
            $this->load->view('template', $data);
        }
        
        public function exportreportexcelandpdf() {
            $file = "Productlist001.xls";
            header("Content-type: application/vnd.ms-excel");
            header("Content-Disposition: attachment; filename=$file");
            $date       =   $_REQUEST["datefrom"];
            $end_date   =   $_REQUEST["dateto"];
            $customerid =   $_REQUEST["customerid"];
            if($date || $end_date || $customerid) { 
                $date       =   date("Y-m-d",strtotime($_REQUEST["datefrom"]));
                $end_date   =   date("Y-m-d",strtotime($_REQUEST["dateto"]));
                $customerdet    =   $this->customers_model->getCustomerById($customerid);
                $productstotallist = $this->reports_model->productstotallist($date,$end_date,$customerid);
                $productcount = $this->reports_model->productcount($date,$end_date,$customerid);
                $producttotalcount = $this->reports_model->producttotalcount($date,$end_date,$customerid);
?>
            <table class="table table-striped table-hover table-bordered editable">
                <tr>
                    <td colspan="<?php echo $_REQUEST["j"]; ?>" style="font-size: 22px;font-weight: bold;">
                        <?php echo $customerdet[0]->NAME; ?>
                    </td>
                </tr>
                <tr>
                    <td colspan="<?php echo $_REQUEST["j"]; ?>" style="font-size: 16px;font-weight: bold;">
                        <?php
                        if($customerdet[0]->ADDRESS!='' || $customerdet[0]->CITY!='' || $customerdet[0]->COUNTRY!='')
                            echo $customerdet[0]->ADDRESS."<br/>".$customerdet[0]->CITY."<br/>".$customerdet[0]->COUNTRY;
                        ?>
                    </td>
                </tr>
                <tr style="font-weight: bold;">
                    <td>Date</td>
                    <?php
                    if($productstotallist){
                        $i=0;
                        foreach($productstotallist as $productstotallistrec){
                    ?>
                            <td><?php echo $productstotallistrec->NAME; ?></td>
                    <?php
                        $prarray[$i]    =   $productstotallistrec->ID;
                        $prpricearray[$i]    =   $productstotallistrec->PRICE;
                        $i++;
                        }
                    }
                    ?>
                </tr>
                    <?php
                    while (strtotime($date) <= strtotime($end_date)) {
                    ?>
                        <tr><td><div style="width: 100px;"><?php echo $date; ?></div></td>
                            <?php                          
                            if(isset($prarray) && $prarray){
                                foreach($prarray as $prarrayData){
                                    $count=0;
                                    //  Check date and product id to get the product count of that day
                                    if($productcount){
                                        foreach($productcount as $productcountrec){
                                            if($productcountrec->ID==$prarrayData && $productcountrec->RECIEPTDATE==$date)
                                                $count=$productcountrec->CNT;
                                        }
                                    }
                            ?>
                                <td>
                                <?php
                                    echo $count;
                                ?>
                                </td>
                            <?php
                                }
                            }
                            ?>
                        </tr>
                    <?php
                        $date = date ("Y-m-d", strtotime("+1 day", strtotime($date)));
                    }
                    ?>
                        <tr style="font-weight: bold;">
                            <td>Quantity</td>
                            <?php                          
                            if(isset($prarray) && $prarray){
                                $j=0;
                                foreach($prarray as $prarrayData){
                                    $totcount=0;
                                    if($producttotalcount){
                                        foreach($producttotalcount as $producttotalcountrec){
                                            if($producttotalcountrec->ID==$prarrayData) {
                                                $totcount=$totcount+$producttotalcountrec->CNT;
                                            }
                                        }
                                    }
                            ?>
                            <td><div><?php echo $totcount; ?></div></td>
                            <?php
                                $j++;
                                }
                            }
                            ?>
                        </tr>
                        <tr style="font-weight: bold;">
                            <td>Price</td>
                            <?php                          
                            if(isset($prarray) && $prarray){
                                $j=0;
                                $grandtotal=0;
                                foreach($prarray as $prarrayData){
                                    $totprice=0;
                                    if($producttotalcount){
                                        foreach($producttotalcount as $producttotalcountrec){
                                            if($producttotalcountrec->ID==$prarrayData) {
                                                $totprice=$totprice+$producttotalcountrec->TOTALPRICE;
                                            }
                                        }
                                    }
                                    $grandtotal=$grandtotal+$totprice;
                            ?>
                            <td><div><?php echo $this->formatslist->formatCurrencywithHash($totprice); ?></div></td>
                            <?php
                                $j++;
                                }
                            }
                            ?>
                        </tr>
                        <tr style="font-weight: bold;">
                            <td>Sub Total</td>
                            <td colspan="<?php echo $j; ?>" style="font-weight: bold;">
                                <div>
                                <?php
                                $taxamount  =   $grandtotal*.05;
                                $subtotal   =   $grandtotal-$taxamount;
                                echo $this->formatslist->formatCurrencywithHash($subtotal);
                                ?>
                                </div>
                            </td>
                        </tr>
                        <tr style="font-weight: bold;">
                            <td>Tax Amount</td>
                            <td colspan="<?php echo $j; ?>" style="font-weight: bold;">
                                <div><?php echo $this->formatslist->formatCurrencywithHash($taxamount); ?></div>
                            </td>
                        </tr>
                        <tr style="font-weight: bold;">
                            <td>Total Amount</td>
                            <td colspan="<?php echo $j; ?>" style="font-weight: bold;">
                                <div><?php echo $this->formatslist->formatCurrencywithHash($grandtotal); ?></div>
                            </td>
                        </tr>
            </table>
              <?php
              }
        }
    }