
<?php
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class homedelivery_model extends CI_Model {

public function __construct() {
parent::__construct();
}
//Home Delivery  Report
 function gethomedelivery($datefrom,$dateto, $property)
    {
//$datefrom='2012-07-26';
$property='TariffArea';
        $page = $this->input->post('page');
        $limit = $this->input->post('rows'); // get how many rows we want to have into the grid
        $sidx = $this->input->post('sidx'); // get index row - i.e. user click to sort
        $sord = $this->input->post('sord'); // get the direction
//Default values
        if (!$sidx)
            $sidx = 'ID';
        if (!$sord)
            $sord = 'asc';

        if (!$page)
            $page = 1;
        if (!$limit)
            $limit = 25;
        //Total Returned Rows Count
         $table_name = "TICKETLINES";
        if ($datefrom && $dateto) {
            $this->db->where("DATE(RECEIPTS.DATENEW) >=", $datefrom);
            $this->db->where("DATE(RECEIPTS.DATENEW) <=", $dateto);
        } else if ($datefrom) {
            $this->db->where("DATE(RECEIPTS.DATENEW)=", $datefrom);
        }

        $this->db->select("RECEIPTS.ID,RECEIPTS.ATTRIBUTES,PAYMENTS.TOTAL")->from($table_name);
        $this->db->join('TICKETS', 'TICKETLINES.TICKET = TICKETS.ID','LEFT');
        $this->db->join('RECEIPTS', 'TICKETS.ID = RECEIPTS.ID','LEFT');
        $this->db->join('PAYMENTS','RECEIPTS.ID=PAYMENTS.RECEIPT','LEFT');
        $count = $this->db->count_all_results();

//Total Pages
        if ($count > 0 && $limit > 0) {
            $total_pages = ceil($count / $limit);
        } else {
            $total_pages = 0;
        }

//Check requested page
        if ($page > $total_pages)
            $page = $total_pages;

//Set start record number
        $start = $limit * $page - $limit;

//Format data for jqGrid output
        $data['page'] = "" . $page . "";
        $data['total'] = "" . $total_pages . "";

      //  var_dump($data);
      $table_name = "RECEIPTS";
       if ($datefrom && $dateto) {
            $this->db->where("DATE(RECEIPTS.DATENEW) >=", $datefrom);
            $this->db->where("DATE(RECEIPTS.DATENEW) <=", $dateto);
        } else if ($datefrom) {
            $this->db->where("DATE(RECEIPTS.DATENEW)=", $datefrom);
        }


         $this->db->select("RECEIPTS.ID,RECEIPTS.ATTRIBUTES,PRODUCTS.CODE,RECEIPTS.DATENEW, PRODUCTS.NAME AS PRODUCT,TICKETLINES.PRICE AS PRICE, SUM(TICKETLINES.UNITS) AS UNITS,(SUM(TICKETLINES.UNITS*TICKETLINES.PRICE)) AS TOTAL")->from($table_name);

         $this->db->join('TICKETS', 'TICKETS.ID = RECEIPTS.ID','LEFT');
         $this->db->join('TICKETLINES', 'TICKETLINES.TICKET = TICKETS.ID','LEFT');
         $this->db->join('PRODUCTS', 'PRODUCTS.ID = TICKETLINES.PRODUCT','LEFT');

         $this->db->group_by('RECEIPTS.DATENEW');
         $this->db->group_by('TICKETLINES.PRODUCT');
      //  $this->db->order_by('RECEIPTS.DATENEW');
      //   $this->db->order_by($sidx, $sord);
         $datafull = $this->db->get()->result();
//$property="TariffArea";
$x=1;

$arr=null;
$arryval=null;
$arryval1=null;
$valcaptain=null;
$valPacked=null;
$valtarif=null;
$valdelvry=null;
$valWaiter=null;
$valaddress=null;
$valaddress1=null;
$valphno=null;
$valtocken=0;
$valpax=null;
$amount=0;
$paxamount=1;
$perpax=0;
$valpx=0;

//get  blob values name as array
foreach ($datafull as $row)
 {

        $xml = simplexml_load_string(htmlspecialchars_decode($row->ATTRIBUTES));
       // var_dump($xml);
   if($xml)
     {
     foreach ($xml as $key0 => $value)
       {
           // var_dump((string)$value);
        if($value)

         {

         foreach ($value->attributes() as $attributeskey0 => $attributesvalue1)
             {


 if (($attributesvalue1 == "$property")||($attributesvalue1 == "Captian")||($attributesvalue1 == "Delivery Time")||($attributesvalue1 == "Token")||($attributesvalue1 == "PackedBy")||($attributesvalue1 == "Waiter")||($attributesvalue1 == "address")||($attributesvalue1 == "ph:no"))
   {
             if( $attributesvalue1 == "Captian")
                  {
                      $valcaptain= (string)$value;
                  }
                   if( $attributesvalue1 == "$property")
                  {
                      $valtarif= (string)$value;
                    //  var_dump($valtarif);
                  }

             if( $attributesvalue1 == "Delivery Time")
                  {
                      $valdelvry= (string)$value;
                  }
             if( $attributesvalue1 == "Token")
                  {
                      $valtocken= (string)$value;
                  }
             if( $attributesvalue1 == "PackedBy")
                  {
                      $valPacked= (string)$value;
                  }
             if( $attributesvalue1 == "Waiter")
                  {
                      $valWaiter= (string)$value;
                  }
             if( $attributesvalue1 == "address")
                  {
                      $valaddress= (string)$value;
                     // var_dump($valaddress1);
                  }
             if( $attributesvalue1 == "ph:no")
                  {

                      $valphno= (string)$value;
                  }


$val=null;
$valaddress1=null;
   if (($attributesvalue1 == "address"))
    {
        if( $attributesvalue1 == "address")
                  {
                      $valaddress1= (string)$value;
                   //  var_dump($valaddress1);

          //$bb=$valcaptain;
//$valaddress=$valaddress1;
  //var_dump($valaddress1);
          $val=(string)$value;
           // var_dump($valtarif);


                              // var_dump($valaddress);
                                  if(($valaddress1!=''))
                                 {

                                   $arryval[$x]['CAPTAIN']=$valcaptain;
                                   $arryval[$x]['AMOUNT']=(float)$row->TOTAL;
                                   $arryval[$x]['TARIF']=$valtarif;
                                   $arryval[$x]['TOKEN']=$valtocken;

                                   $arryval[$x]['PACKEDBY']=$valPacked;
                                   $arryval[$x]['WAITER']=$valWaiter;
                                   $arryval[$x]['QUANTITY']=(float)$row->UNITS;
                                   $arryval[$x]['ADDRESS']=$valaddress1;
                                   $arryval[$x]['PHONE']=$valphno;

                                   $arryval[$x]['DELIVERYTIME']=$valdelvry;
                                   $arryval[$x]['PRODUCT']=$row->PRODUCT;
                                   $arryval[$x]['BILLDATE']=$row->DATENEW;
                                   $x++;

                                  }

  }
}
   }


      }
     }
    }
   }
  }

//var_dump($arryval);

  $arrhome=null;
  $c=count($arryval);
  $j=0;
  for($i=1;$i<=$c;$i++)
  {
  if($arryval[$i]['TARIF']=='HOME DELIVERY')
  {
   $arrhome[$j]['TAFIF'] =$arryval[$i]['TARIF'];
    $arrhome[$j]['CAPTAIN']=$arryval[$i]['CAPTAIN'];
                                   $arrhome[$j]['AMOUNT']=$arryval[$i]['AMOUNT'];
                                   $arrhome[$j]['TARIF']=$arryval[$i]['TARIF'];
                                   $arrhome[$j]['TOKEN']=$arryval[$i]['TOKEN'];

                                   $arrhome[$j]['PACKEDBY']=$arryval[$i]['PACKEDBY'];
                                   $arrhome[$j]['WAITER']=$arryval[$i]['WAITER'];
                                   $arrhome[$j]['QUANTITY']=$arryval[$i]['QUANTITY'];
                                   $arrhome[$j]['ADDRESS']=$arryval[$i]['ADDRESS'];
                                   $arrhome[$j]['PHONE']=$arryval[$i]['PHONE'];

                                   $arrhome[$j]['DELIVERYTIME']=$arryval[$i]['DELIVERYTIME'];
                                   $arrhome[$j]['PRODUCT']=$arryval[$i]['PRODUCT'];
                                   $arrhome[$j]['BILLDATE']=$arryval[$i]['BILLDATE'];

   $j++;
  }
  }
 //var_dump($arrhome);
 $data['records'] = "" . count($arryval) . "";

 $i=0;
    // var_dump($data);
   foreach ($arrhome as $row)
       {
            $data['rows'][$i]['id'] = $i + 1;
            $data['rows'][$i]['cell'] = array($row['PRODUCT'],$row['QUANTITY'],$row['AMOUNT'],$row['BILLDATE'],$row['DELIVERYTIME'],$row['TOKEN'],$row['CAPTAIN'],$row['WAITER'],$row['PACKEDBY'],$row['ADDRESS'],$row['PHONE']);
            $i++;

        }

        return json_encode($data);


    }

}





