<?php
    $today_date                         = date('Y-m-d');    
    $monthly_start_date                 = date('Y-m-01');    
    $monthly_end_date                   = date('Y-m-t');
    $yearly_start_date                  = date('Y-01-01');    
    $yearly_end_date                    = date('Y-12-t');       ?>
<div id="lead" class="panel">
   <div class="heading"><span>test</span></div>
<br>
       
<span><b>On Value</b></span>
<br><br>
    <div class="row">
        
          <div class="col-md-4">
            <section class="panel">
                <header class="panel-heading">
                    Order- Country
                </header>
                <div class="panel-body">
                    <figure class="demo-xchart" id="ytd_order_country_val_chart"></figure>
                </div>
            </section>
        </div>
<!--        <div class="col-md-4">
            <section class="panel">
                <header class="panel-heading">
                    Quotation- Country
                </header>
                <div class="panel-body">
                    <figure class="demo-xchart" id="ytd_quotation_country_val_chart"></figure>
                </div>
            </section>
        </div>-->
        
<!--         <div class="col-md-4">
            <section class="panel">
                <header class="panel-heading">
                    Lead- Country
                </header>
                <div class="panel-body">
                    <figure class="demo-xchart" id="ytd_lead_country_val_chart"></figure>
                </div>
            </section>
        </div>-->
    </div>


</div>

 <?php 
    //------------------------------------------------  MTD ----------------------------------------------------------------
  
      
      
      // lead - country by value 
      
//    $ytd_order_country_val_graph_data = array();
//    $ytd_order_country_val_target_axis="";
//    $model = MasCountry::model()->findAll(array('condition'=>'isActive="Y"'));
//    foreach ($model as $i=>$val) {   
//             $model_project_type = MasCountry::model()->findAll(array('condition'=>'isActive="Y"'));
//             foreach ($model_project_type as $j=>$value) {           
//                //$type = Yii::app()->db->createCommand("SELECT COUNT(*) FROM crm_lead WHERE crm_user_id=" . $value->id . "")->queryScalar();                    
//                $type =  Order::model()->with('currencyname','customer')->find(array('select'=>'SUM(CASE WHEN currencyname.IsBaseCurrency="Y" THEN grand_total ELSE 0 END ) as value1,SUM(CASE WHEN currencyname.IsBaseCurrency="N" THEN grand_total*currencyname.exchange_rate ELSE 0 END ) as value2','condition'=>'order_date>="'.$yearly_start_date.'" AND order_date<="'.$today_date.'" AND customer.customer_country ='.$value->id));//AND customer.customer_country ='.$value->id
//                //$type_base =  Order::model()->with('currencyname','customer')->find(array('select'=>'SUM(grand_total*currencyname.exchange_rate) as value','condition'=>'order_date>="'.$yearly_start_date.'" AND order_date<="'.$today_date.'" AND currencyname.IsBaseCurrency="N" AND customer.customer_country ='.$value->id));
//                $total = $type->value1+$type->value;
//               // print_r($type);die();
////                 $type = Lead::model()->with('customer')->find(array('select'=>'COUNT(*) as value','condition'=>' customer.customer_country ='.$value->id));
//                 if(!empty($total)){ 
//
//                       if($ytd_order_country_val_target_axis=="")
//                              $ytd_order_country_val_target_axis= '{"y":'.$total.',"x":"'.$value->country_name.'"}';
//                      else
//                              $ytd_order_country_val_target_axis.= ',{"y":'.$total.',"x":"'.$value->country_name.'"}';         
//                 }
//                  else
//                 {
//                      //$ytd_order_country_val_target_axis= '{"y":0,"x":"'.$value->country_name.'"}';
//                      continue;
//                 }
//           
//           
//           }
//           $ytd_order_country_val_graph_data[$i] = $ytd_order_country_val_target_axis;
////           print_r($ytd_order_country_val_graph_data);die();
//      }
      
//       $ytd_quotation_country_val_graph_data = array();
//    $ytd_quotation_country_val_target_axis="";
//    $model = MasCountry::model()->findAll(array('condition'=>'isActive="Y"'));
//    foreach ($model as $i=>$val) {   
//             $model_project_type = MasCountry::model()->findAll(array('condition'=>'isActive="Y"'));
//             foreach ($model_project_type as $j=>$value) {           
//                //$type = Yii::app()->db->createCommand("SELECT COUNT(*) FROM crm_lead WHERE crm_user_id=" . $value->id . "")->queryScalar();                    
//                $type =  Quotes::model()->with('currencyname','customer')->find(array('select'=>'SUM(grand_total) as value','condition'=>'qtn_date>="'.$yearly_start_date.'" AND qtn_date<="'.$today_date.'" AND currencyname.IsBaseCurrency="Y" AND customer.customer_country ='.$value->id));//
//                $type_base =  Quotes::model()->with('currencyname','customer')->find(array('select'=>'SUM(grand_total*currencyname.exchange_rate) as value','condition'=>'qtn_date>="'.$yearly_start_date.'" AND qtn_date<="'.$today_date.'" AND currencyname.IsBaseCurrency="N" AND customer.customer_country ='.$value->id));
////                if(empty($type->value)){$type->value=0;}  if(empty($type_base->value)){$type_base->value=0;}
//                $total= $type->value+$type_base->value;
//       // print_r($total);die();
//                // $type = Lead::model()->with('customer')->find(array('select'=>'COUNT(*) as value','condition'=>' customer.customer_country ='.$value->id));
//                
//                if(!empty($total)){ 
//
//                       if($ytd_quotation_country_val_target_axis=="")
//                              $ytd_quotation_country_val_target_axis= '{"y":'.$total.',"x":"'.$value->country_name.'"}';
//                      else
//                              $ytd_quotation_country_val_target_axis.= ',{"y":'.$total.',"x":"'.$value->country_name.'"}';         
//                 }
//                 else
//                 {
//                    // $ytd_quotation_country_val_target_axis= '{"y":0,"x":"'.$value->country_name.'"}';
//                    continue;
//                 }
//           
//           
//           }
//           $ytd_quotation_country_val_graph_data[$i] = $ytd_quotation_country_val_target_axis;
//          // print_r($ytd_quotation_country_val_graph_data);die();
//      }  
     // echo Yii::getVersion();
      
//       $ytd_lead_country_val_graph_data = array();
//    $ytd_lead_country_val_target_axis="";
//    $model = MasCountry::model()->findAll(array('condition'=>'isActive="Y"'));
//    foreach ($model as $i=>$val) {   
//             $model_project_type = MasCountry::model()->findAll(array('condition'=>'isActive="Y"'));
//             foreach ($model_project_type as $j=>$value) {           
//                //$type = Yii::app()->db->createCommand("SELECT COUNT(*) FROM crm_lead WHERE crm_user_id=" . $value->id . "")->queryScalar();                    
//                $type =  Lead::model()->with('currency','customer')->find(array('select'=>'SUM(value) as value','condition'=>'enq_date>="'.$yearly_start_date.'" AND enq_date<="'.$today_date.'" AND currency.IsBaseCurrency="Y" AND customer.customer_country ='.$value->id));
//                $type_base =  Lead::model()->with('currency','customer')->find(array('select'=>'SUM(value*currency.exchange_rate) as value','condition'=>'enq_date>="'.$yearly_start_date.'" AND enq_date<="'.$today_date.'" AND currency.IsBaseCurrency="N" AND customer.customer_country ='.$value->id));
//                $total= $type->value+$type_base->value;
//               
//                // $type = Lead::model()->with('customer')->find(array('select'=>'COUNT(*) as value','condition'=>' customer.customer_country ='.$value->id));
//                 if(!empty($total)){ 
//
//                       if($ytd_lead_country_val_target_axis=="")
//                              $ytd_lead_country_val_target_axis= '{"y":'.$total.',"x":"'.$value->country_name.'"}';
//                      else
//                              $ytd_lead_country_val_target_axis.= ',{"y":'.$total.',"x":"'.$value->country_name.'"}';         
//                 }
//                  else{
//                      continue;
//                 }
//           
//           
//           }
//           $ytd_lead_country_val_graph_data[$i] = $ytd_lead_country_val_target_axis;
//           //print_r($lead_sg_target_axis);die();
//      }

 ?>     
  <script type="text/javascript">
(function() {
        
 //-------------------------------------MTD-----------------------------------------
 //---------Qty-----------
  
     
 var ytd_order_country_val_data = [{
                "xScale": "ordinal",
                "comp": [],
                "main": [{
                        "className": ".main.l2",
                        "data": [
                        
                             <?php if(isset($ytd_order_country_val_graph_data[0])) {echo $ytd_order_country_val_graph_data[0];}?>
                           
                                ]}
                ],
                "type": "bar",
                "yScale": "linear"}, ]; 
               
var ytd_quotation_country_val_data = [{
                "xScale": "ordinal",
                "comp": [],
                "main": [{
                        "className": ".main.l2",
                        "data": [ <?php if(isset($ytd_quotation_country_val_graph_data[0])) {echo $ytd_quotation_country_val_graph_data[0];}?> ]}
                ],
                "type": "bar",
                "yScale": "linear"}, ]; 
                
 
   ytd_order_country_val_chart = new xChart('bar', ytd_order_country_val_data[0], '#ytd_order_country_val_chart', {
            axisPaddingTop: 5,
            dataFormatX: function (x) {
                return x;
            },
            dataFormatY: function (y) {
                return y;
            },
        });   
        
  ytd_quotation_country_val_chart = new xChart('bar', ytd_quotation_country_val_data[0], '#ytd_quotation_country_val_chart', {
            axisPaddingTop: 5,
            dataFormatX: function (x) {
                return x;
            },
            dataFormatY: function (y) {
                return y;
            },
        });         

//var ytd_lead_country_val_data = [{
//                "xScale": "ordinal",
//                "comp": [],
//                "main": [{
//                        "className": ".main.l2",
//                        "data": [ <?php if(isset($ytd_lead_country_val_graph_data[0])) {echo $ytd_lead_country_val_graph_data[0];}?> ]}
//                ],
//                "type": "bar",
//                "yScale": "linear"}, ]; 
//  
// ytd_lead_country_val_chart = new xChart('bar', ytd_lead_country_val_data[0], '#ytd_lead_country_val_chart', {
//            axisPaddingTop: 5,
//            dataFormatX: function (x) {
//                return x;
//            },
//            dataFormatY: function (y) {
//                return y;
//            },
//        });  

 }());               
</script>          