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

class captainwisesale_model extends CI_Model {

public function __construct() {
parent::__construct();
}
//waiter or  tariff report
 function getcaptainwisesale($datefrom,$dateto, $property)
    {
 //$property="Waiter";
      $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,CATEGORIES.NAME AS CATEGORY,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->join('CATEGORIES', 'CATEGORIES.ID = PRODUCTS.CATEGORY', '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();
 //var_dump($datafull);
$this->db->select("RECEIPTS.ID,PRODUCTS.CODE,RECEIPTS.DATENEW, PRODUCTS.NAME AS PRODUCT,TICKETLINES.PRICE AS PRICE, SUM(TICKETLINES.UNITS) AS UNITS,(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');
$rows = $this->db->get()->result_array();
  // var_dump($rows);
$property="Captian";
$x=0;
$y=0;
$z=0;
$p=0;
$q=0;
$r=0;
$arr=null;
$arryval=null;
$arryval1=null;
$valpax=0;
$amount=0;
$paxamount=1;
$perpax=0;
//$arryval[0]['NAME']='';
//$arryval[0]['AMOUNT']='';
//get suplier 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)
                                {
// For Tariff & Waiter starts
if ( $property!='ff')
{
   //  var_dump('jjjjjjjjj');
                             //var_dump($attributeskey0);
                                   if ($attributesvalue1 == "$property")
                                   {

                                   $val=(string) $value;
                                   $count=count($arryval);
                                   $found=0;
                                   for($i=0;$i<$count;$i++)
                                    {
                                       if($arryval[$i]['NAME']==$val)
                                       {
                                            //  $arryval[$i]['AMOUNT']=(float)$arryval[$i]['AMOUNT']+(float)$row->TOTAL;
                                           // $found=1;
                                            break;
                                       }
                                  }
                                  if($found==0)
                                  {
                                  $arryval[$x]['NAME']=$val;
                                  $arryval[$y]['PRODUCT']=$row->PRODUCT;
                                  $arryval[$p]['TOTAL']=$row->TOTAL;
                                  $arryval[$q]['CATEGORY']=$row->CATEGORY;
                                  $arryval[$r]['UNITS']=$row->UNITS;
                                   $x++;
                                   $y++;
                                   $p++;
                                   $q++;
                                   $r++;
                                  }


                                 }
}
 // For Pax reports  starts                        //a.s
 elseif ($property=='aptian')
 {


    if (($attributesvalue1 == "$property")||($attributesvalue1 == "Pax"))
                                  {
 if( $attributesvalue1 == "Pax")
 {


 $valpax= (string)$value;
 //var_dump($valpax);
 }
  //var_dump($valpax);
  if ($attributesvalue1 == "$property")
  {
      $bb=$valpax;
                                  $val=(string) $value;
                                   $count=count($arryval);
                                   $found=0;
                                  for($i=0;$i<$count;$i++)
                                    {
                                       if($arryval[$i]['NAME']==$val)
                                       {
                                           $arryval[$i]['PAX']=$arryval[$i]['PAX']+$bb;
                                           $arryval[$i]['AMOUNT']=(float)$arryval[$i]['AMOUNT']+(float)$row->TOTAL;
                                           $amount=$arryval[$i]['AMOUNT'];
                                           $paxamount=$arryval[$i]['PAX'];
                                           if($paxamount==0){$perpax=0;}
                                            else
                                             {$perpax=$amount/$paxamount;}
                                           $arryval[$i]['PERPAX']=$perpax;

                                            $found=1;
                                            break;
                                       }
                                  }
                                  if(($found==0)&&($val!=''))
                                  {
                                   $arryval[$x]['NAME']=$val;
                                   $arryval[$y]['AMOUNT']=(float)$row->TOTAL;
                                   $arryval[$z]['PAX']=$bb;
                                   $amount=$arryval[$x]['AMOUNT'];
                                   $paxamount=$arryval[$y]['PAX'];
                                   if($paxamount==0){$perpax=0;}
                                   else
                                   {$perpax=$amount/$paxamount;}
                                   $arryval[$p]['PERPAX']=$perpax;
                                   $x++;
                                   $y++;
                                   $z++;
                                   $p++;
                                  }
  }

                                 }
 }

 // For Pax reports  ends
 //a.e
                             }
         }
            }


     }

    }
//  function compare($x, $y)
//{
// if ( $x['NAME'] == $y['NAME'] )
//  return 0;
// else if ( $x['NAME'] < $y['NAME'] )
//  return -1;
// else
//  return 1;
//}
//
//usort($arryval, 'compare');
//var_dump($arryval);
    $data['records'] = "" . count($arryval) . "";
// uksort($arryval, "isort");
    //$arryval=ksort($arryval);
   $i=0;
   //  var_dump($data);
//        foreach ($arryval as $row) {
//            $data['rows'][$i]['id'] = $i + 1;
//            $data['rows'][$i]['cell'] = array($row['NAME'],$row['PAX'],$row['AMOUNT'],$row['PERPAX']);
//            $i++;
//
//        }
//
//        return json_encode($data);

   //var_dump($data);
        foreach ($arryval as $row) {
            $data['rows'][$i]['id'] = $i + 1;
            $data['rows'][$i]['cell'] = array($row['NAME'],$row['PRODUCT'],$row['UNITS'],$row['TOTAL'],$row['CATEGORY']);
            $i++;
        }

        return json_encode($data);
    }

function subval_sort($a,$subkey) {
	foreach($a as $k=>$v) {
		$b[$k] = strtolower($v[$subkey]);
	}
	asort($b);
	foreach($b as $key=>$val) {
		$c[] = $a[$key];
	}
	return $c;
}
function getappsettings($name) {
        $this->db->select('CONTENT');
                $this->db->where('NAME', 'AppSettings');
        $data=$this->db->get('RESOURCES')->row();
        $xml = simplexml_load_string(htmlspecialchars_decode($data->CONTENT));
        foreach ($xml as $key0 => $value) {
                        foreach ($value->attributes() as $attributeskey0 => $attributesvalue1) {
                            if ($key0 == "Waiter") {
                                 return $attributesvalue1;
                            }
                        }
        }
        return null;

    }


//A.E


}




//SELECT `RECEIPTS`.`ID`, `RECEIPTS`.`ATTRIBUTES`,`RECEIPTS`.`DATENEW`,`TICKETLINES`.`PRICE` AS PRICE,`PRODUCTS`.`CODE`,`PRODUCTS`.`NAME`,`TICKETLINES`.`PRICE` AS PRICE,SUM(TICKETLINES.UNITS) AS UNITS
//FROM (`RECEIPTS`)
//LEFT JOIN `TICKETS` ON `TICKETS`.`ID` = `RECEIPTS`.`ID`
//LEFT JOIN `TICKETLINES` ON `TICKETLINES`.`TICKET` = `TICKETS`.`ID`
//LEFT JOIN `PRODUCTS` ON `PRODUCTS`.`ID` = `TICKETLINES`.`PRODUCT`
//GROUP BY `RECEIPTS`.`DATENEW`;




