  var table 

  var query_y    = getQueryVariable("y");
  var query_m    = getQueryVariable("m");
  var query_d    = getQueryVariable("d");


  function get_total_purchase(yy='not',mm='not',dd='not'){

    $.ajax({
                    url : 'models/crul_dashboard.php',
                    type : 'POST',
                    data : {
                              yy:yy,
                              mm:mm,
                              dd:dd,
                              type:'get_total_purchase'
                            },
                    dataType : 'json',
                    success : function(response) {
                        console.log(response)
                        
                          
                          $("#total_purchase").html(currency_format_display(response[0]['total_price']));                          

                    },
                    complete: function() {
                           //  me.data('requestRunning', false);
                    }

              }); // submit ajax

  }


  function get_total_expenses(yy='not',mm='not',dd='not'){

    $.ajax({
                    url : 'models/crul_dashboard.php',
                    type : 'POST',
                    data : {
                              yy:yy,
                              mm:mm,
                              dd:dd,
                              type:'get_total_expenses'
                            },
                    dataType : 'json',
                    success : function(response) {
                        console.log(response)
                        
                          
                          $("#total_expenses").html(currency_format_display(response[0]['total_price']));                          

                    },
                    complete: function() {
                        
                    }

              }); // submit ajax

  }


  function get_total_shop_capital(yy='not',mm='not',dd='not'){

    $.ajax({
                    url : 'models/crul_dashboard.php',
                    type : 'POST',
                    data : {
                              yy:yy,
                              mm:mm,
                              dd:dd,
                              type:'get_total_shop_capital'
                            },
                    dataType : 'json',
                    success : function(response) {
                        console.log(response)
                        
                          
                          $("#total_shop_capital").html(currency_format_display(response[0]['total_price']));                          

                    },
                    complete: function() {
                        
                    }

              }); // submit ajax

  }


  function get_total_factory_capital(yy='not',mm='not',dd='not'){

    $.ajax({
                    url : 'models/crul_dashboard.php',
                    type : 'POST',
                    data : {
                              yy:yy,
                              mm:mm,
                              dd:dd,
                              type:'get_total_factory_capital'
                            },
                    dataType : 'json',
                    success : function(response) {
                        console.log(response)
                        
                          
                          $("#total_factory_capital").html(currency_format_display(response[0]['total_price']));                          

                    },
                    complete: function() {
                        
                    }

              }); // submit ajax

  }


  function get_total_sells(yy='not',mm='not',dd='not'){

    $.ajax({
                    url : 'models/crul_dashboard.php',
                    type : 'POST',
                    data : {
                              yy:yy,
                              mm:mm,
                              dd:dd,
                              type:'get_total_sells'
                            },
                    dataType : 'json',
                    success : function(response) {
                        console.log(response)
                        
                          
                          $("#total_sells").html(currency_format_display(response[0]['total_price']));                          

                    },
                    complete: function() {
                        
                    }

              }); // submit ajax

  }

  function get_total_sells_raw(yy='not',mm='not',dd='not'){

              $.ajax({
                    url : 'models/crul_dashboard.php',
                    type : 'POST',
                    data : {
                              yy:yy,
                              mm:mm,
                              dd:dd,
                              type:'get_total_sells_raw'
                            },
                    dataType : 'json',
                    success : function(response) {
                        console.log(response)
                        
                          
                          $("#total_sells_raw").html(currency_format_display(response[0]['total_price']));                          

                    },
                    complete: function() {
                        
                    }

              }); // submit ajax

  }

  function get_total_sells_single(yy='not',mm='not',dd='not'){

              $.ajax({
                    url : 'models/crul_dashboard.php',
                    type : 'POST',
                    data : {
                              yy:yy,
                              mm:mm,
                              dd:dd,
                              type:'get_total_sells_single'
                            },
                    dataType : 'json',
                    success : function(response) {
                        console.log(response)
                        
                          
                          $("#total_sells_single").html(currency_format_display(response[0]['total_price']));                          

                    },
                    complete: function() {
                        
                    }

              }); // submit ajax

  }

   function get_total_sells_plural(yy='not',mm='not',dd='not'){

              $.ajax({
                    url : 'models/crul_dashboard.php',
                    type : 'POST',
                    data : {
                              yy:yy,
                              mm:mm,
                              dd:dd,
                              type:'get_total_sells_plural'
                            },
                    dataType : 'json',
                    success : function(response) {
                        console.log(response)
                        
                          
                          $("#total_sells_plural").html(currency_format_display(response[0]['total_price']));                          

                    },
                    complete: function() {
                        
                    }

              }); // submit ajax

  }


  function get_total_payments(yy='not',mm='not',dd='not'){

    $.ajax({
                    url : 'models/crul_dashboard.php',
                    type : 'POST',
                    data : {
                              yy:yy,
                              mm:mm,
                              dd:dd,
                              type:'get_total_payments'
                            },
                    dataType : 'json',
                    success : function(response) {
                        console.log(response)
                        
                          
                          $("#total_payments").html(currency_format_display(response[0]['total_price']));                          

                    },
                    complete: function() {
                        
                    }

              }); // submit ajax

  }


  function get_total_discount(yy='not',mm='not',dd='not'){

    $.ajax({
                    url : 'models/crul_dashboard.php',
                    type : 'POST',
                    data : {
                              yy:yy,
                              mm:mm,
                              dd:dd,
                              type:'get_total_discount'
                            },
                    dataType : 'json',
                    success : function(response) {
                        console.log(response)
                        
                          
                          $("#total_discount").html(currency_format_display(response[0]['total_price']));                          

                    },
                    complete: function() {
                        
                    }

              }); // submit ajax

  }

  function get_total_return(yy='not',mm='not',dd='not'){

    $.ajax({
                    url : 'models/crul_dashboard.php',
                    type : 'POST',
                    data : {
                              yy:yy,
                              mm:mm,
                              dd:dd,
                              type:'get_total_return'
                            },
                    dataType : 'json',
                    success : function(response) {
                        console.log(response)
                        
                          
                          $("#total_return").html(currency_format_display(response[0]['total_price']));                          

                    },
                    complete: function() {
                        
                    }

              }); // submit ajax

  }


  function get_total_remained(yy='not',mm='not',dd='not'){

    $.ajax({
                    url : 'models/crul_dashboard.php',
                    type : 'POST',
                    data : {
                              yy:yy,
                              mm:mm,
                              dd:dd,
                              type:'get_total_remained'
                            },
                    dataType : 'json',
                    success : function(response) {
                        console.log(response)
                        
                          
                          $("#total_remained").html(currency_format_display(response[0]['total_price']));                          

                    },
                    complete: function() {
                        
                    }

              }); // submit ajax

  }



  function get_total_product(yy='not',mm='not',dd='not'){

    $.ajax({
                    url : 'models/crul_dashboard.php',
                    type : 'POST',
                    data : {
                              yy:yy,
                              mm:mm,
                              dd:dd,
                              type:'get_total_product'
                            },
                    dataType : 'json',
                    success : function(response) {
                        console.log(response)
                        
                          
                          $("#total_product").html((response[0]['total_price']));                          

                    },
                    complete: function() {
                        
                    }

              }); // submit ajax

  }



  function get_total_supplier(yy='not',mm='not',dd='not'){

    $.ajax({
                    url : 'models/crul_dashboard.php',
                    type : 'POST',
                    data : {
                              yy:yy,
                              mm:mm,
                              dd:dd,
                              type:'get_total_supplier'
                            },
                    dataType : 'json',
                    success : function(response) {
                        console.log(response)
                        
                          
                          $("#total_supplier").html(currency_format_display(response[0]['total_price']));                          

                    },
                    complete: function() {
                        
                    }

              }); // submit ajax

  }


  function get_total_reward(yy='not',mm='not',dd='not'){

    $.ajax({
                    url : 'models/crul_dashboard.php',
                    type : 'POST',
                    data : {
                              yy:yy,
                              mm:mm,
                              dd:dd,
                              type:'get_total_reward'
                            },
                    dataType : 'json',
                    success : function(response) {
                        console.log(response)
                        
                          
                          $("#total_reward").html(currency_format_display(response[0]['total_price']));                          

                    },
                    complete: function() {
                        
                    }

              }); // submit ajax

  }


  function get_total_penalty(yy='not',mm='not',dd='not'){

    $.ajax({
                    url : 'models/crul_dashboard.php',
                    type : 'POST',
                    data : {
                              yy:yy,
                              mm:mm,
                              dd:dd,
                              type:'get_total_penalty'
                            },
                    dataType : 'json',
                    success : function(response) {
                        console.log(response)
                        
                          
                          $("#total_penalty").html(currency_format_display(response[0]['total_price']));                          

                    },
                    complete: function() {
                        
                    }

              }); // submit ajax

  }


  function get_total_sallary(yy='not',mm='not',dd='not'){

    $.ajax({
                    url : 'models/crul_dashboard.php',
                    type : 'POST',
                    data : {
                              yy:yy,
                              mm:mm,
                              dd:dd,
                              type:'get_total_sallary'
                            },
                    dataType : 'json',
                    success : function(response) {
                        console.log(response)
                        
                          
                          $("#total_sallary").html(currency_format_display(response[0]['total_price']));                          

                    },
                    complete: function() {
                        
                    }

              }); // submit ajax

  }


  function get_daily_invoice(){

      $.ajax({
                      url : 'models/crul_dashboard.php',
                      type : 'POST',
                      data : {type:'get_daily_invoice'},
                      dataType : 'json',
                      success : function(response) {
                          console.log(response)
                          var Balance =0;
                          var pri = 0;
                          var tt ='';
                          for (var i = 0; i < response.length; i++) {

                            if(response[i].invoice_transaction == "Invoice"){
                              pri = response[i].final_price ;
                              Balance = +Balance + +pri;
                            }
                            
                            
                            tt = tt + `
                                <tr>

                                  <td>
                                    
                                      
                                        `+(+i + +1)+`
                                      
                                  
                                    
                                  </td>

                                  <td>
                                    <div class="media-body">
                                      <div class="media-heading">
                                        <a href="#" class="letter-icon-title">`+response[i].invoice_number+`</a>
                                      </div>
                                  
                                    </div>
                                  </td>

                                  <td>
                                    <div class="media-body">
                                      <div class="media-heading">
                                        <a href="index.php?p=invoice&id=`+response[i].customer_id_fk+`" class="letter-icon-title">`+response[i].name+`</a>
                                      </div>
                                      <div class="text-muted text-size-small"><i class="icon-phone text-size-mini position-left"></i> `+response[i].phone+`</div>
                                    </div>
                                  </td>
                                  
                                  <td>
                                    <span class="text-muted text-size-small">`+response[i].time+`</span>
                                  </td>
                                  <td>
                                    <h6 class="text-semibold no-margin">`+currency_format_display(pri)+`</h6>
                                  </td>
                                  <td>
                                    <h6 class="text-semibold text-success no-margin">`+currency_format_display(response[i].actual_discount)+`</h6>
                                  </td>
                                </tr>
                            `;

                          }
                            $("#Balance_invoice_id").html(currency_format_display(Balance));
                            $("#Table_invoice").html(tt);

                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

  }



  function get_daily_expenses(){

      $.ajax({
                      url : 'models/crul_dashboard.php',
                      type : 'POST',
                      data : {type:'get_daily_expenses'},
                      dataType : 'json',
                      success : function(response) {
                          console.log(response)
                          var Balance =0;
                          var pri = 0;
                          var tt ='';
                          for (var i = 0; i < response.length; i++) {
                            


                            if(response[i].attach_bill != ''){
                              var attach = `<a href="`+response[i].attach_bill+`" data-popup="lightbox">
                                              <img src="`+response[i].attach_bill+`" alt="" class="img-rounded img-preview">
                                            </a>`;
                            }else{
                              var attach = `<a href="assets/images/placeholder.jpg" data-popup="lightbox">
                                              <img src="assets/images/placeholder.jpg" alt="" class="img-rounded img-preview">
                                            </a>`;
                            }

                            pri = response[i].total_expenses ;
                            Balance = +Balance + +pri;
                            
                            tt = tt + `
                                <tr>

                                  <td>
                                    
                                      
                                        `+(+i + +1)+`
                                      
                                  
                                    
                                  </td>

                                  <td>
                                    
                                      <div class="media-heading">
                                        <a href="#" class="letter-icon-title">`+response[i].expenses_name+`</a>
                                      </div>
                                  
                                    
                                  </td>

                                  <td>
                                    
                                      <div class="media-heading">
                                        <span  class="letter-icon-title">`+response[i].bill_number+`</span>
                                      </div>
                                      
                                   
                                  </td>
                                  <td>
                                    <span class="text-muted text-size-small">`+attach+`</span>
                                  </td>
                                  <td>
                                    <span class="text-muted text-size-small">`+response[i].time+`</span>
                                  </td>
                                  <td>
                                    <h6 class="text-semibold no-margin">`+currency_format_display(pri)+`</h6>
                                  </td>
                                  
                                </tr>
                            `;

                          }
                            $("#Balance_expenses_id").html(currency_format_display(Balance));
                            $("#Table_expenses").html(tt);

                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

  }


  function get_purchase(yy='not',mm='not',dd='not'){

      $.ajax({
                      url : 'models/crul_dashboard.php',
                      type : 'POST',
                      data : {
                                yy:yy,
                                mm:mm,
                                dd:dd,
                                type:'get_purchase'
                              },
                      dataType : 'json',
                      success : function(response) {
                         
                          var Balance =0;
                          var pri = 0;
                          var tt ='';
                          for (var i = 0; i < response.length; i++) {
                            
                            if(response[i].purchase_transaction =="Purchase" ){
                              Balance = +Balance + +response[i].total_price;

                            }
                            
                            tt = tt + `
                                <tr>
                                  <td>
                                    
                                      <div class="media-heading">
                                        `+(+i + +1)+`
                                      </div>
                                  
                                    
                                  </td>
                                  <td>
                                    
                                      <div class="media-heading">
                                        <a href="#" class="letter-icon-title">`+response[i].purchase_auto_number+`</a>
                                      </div>
                                  
                                    
                                  </td>

                                  <td>
                                    
                                      <div class="media-heading">
                                        <span href="#" class="letter-icon-title">`+response[i].supplier_full_name+`</span>
                                      </div>
                                   
                                  </td>
                                  <td>
                                    <span class="text-muted text-size-small">`+response[i].purchase_transaction+`</span>
                                  </td>
                                  <td>
                                    <span class="text-muted text-size-small">`+response[i].time+`</span>
                                  </td>
                                  <td>
                                    <h6 class="text-semibold no-margin">`+currency_format_display(response[i].total_price)+`</h6>
                                  </td>
                                  
                                </tr>
                            `;

                          }
                            $("#Balance_purchase_id").html(currency_format_display(Balance));
                            $("#Table_purchase").html(tt);

                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

  }


  function get_order(yy='not',mm='not',dd='not'){

      $.ajax({
                      url : 'models/crul_dashboard.php',
                      type : 'POST',
                      data : {
                                yy:yy,
                                mm:mm,
                                dd:dd,
                                type:'get_order'
                              },
                      dataType : 'json',
                      success : function(response) {
                         
                          var Balance = response.length;
                          var pri = 0;
                          var tt ='';
                          for (var i = 0; i < response.length; i++) {
                            
                            data = response[i].delivery_status;
                            if(data ==11 ){
                               status=  `<span class="label bg-cyan btn-block ">`+Translation('Delivery In Shop')+`</span>`;
                             }else if(data ==10 ){
                               status=  `<span class="label bg-violet btn-block ">`+Translation('Delivery In Shop')+`</span>`;
                             }else if(data ==9 ){
                               status=  `<span class="label label-success btn-block ">`+Translation('Delivery In Factory')+`</span>`;
                             }else if(data ==8){
                               status = '<span class="label bg-teal  btn-block ">'+Translation('Packaging Stage')+'</span>';
                             }else if(data ==7){
                               status = '<span class="label bg-pink  btn-block ">'+Translation('Control Stage')+'</span>';
                             }else if(data ==6){
                               status = '<span class="label bg-purple  btn-block ">'+Translation('PVC Stage')+'</span>';
                             }else if(data ==5){
                               status = '<span class="label bg-orange  btn-block ">'+Translation('Check Point Stage')+'</span>';
                             }else if(data ==4){
                               status = '<span class="label bg-warning  btn-block ">'+Translation('Glue Stage')+'</span>';
                             }else if(data ==3){
                               status = '<span class="label bg-grey  btn-block ">'+Translation('Cleaning Wood Stage')+'</span>';
                             }else if(data ==2){
                               status = '<span class="label bg-info  btn-block ">'+Translation('CNC Stage')+'</span>';
                             }else if(data ==1){
                               status = '<span class="label bg-primary  btn-block ">'+Translation('Reiewed & Editing Stage')+'</span>';
                             }else if(data ==0){
                               status = '<span class="label bg-danger  btn-block ">'+Translation('لم يتم البدء بالانتاج')+'</span>';
                             }

                             // to show if it has original invoice
                             var Typs ='';
                             if(response[i].invoice_transaction == 'Return'){
                               Typs = `<a   data-popup='tooltip' data-original-title='`+Translation('عرض الوصل')+`'>`+'<span class="label bg-purple-400  btn-block">'+Translation('وصل معاد وتابع للوصل ذو العدد :')+' '+response[i].original_invoice_id_fk+'  </span></a>';

                             }
                             

                            
                            tt = tt + `
                                <tr>
                                  <td>
                                    
                                      <div class="media-heading">
                                        `+(+i + +1)+`
                                      </div>
                                  
                                    
                                  </td>
                                  <td>
                                    
                                      <div class="media-heading">
                                        <a href="#" class="letter-icon-title">`+response[i].invoice_number+`</a>
                                      </div>
                                  
                                    
                                  </td>

                                  <td>
                                    
                                      <div class="media-heading">
                                        <a href="#" class="letter-icon-title">`+response[i].name+`</a>
                                      </div>
                                      <div class="text-muted text-size-small"><i class="icon-phone text-size-mini position-left"></i> `+response[i].phone+`</div>
                                    
                                  </td>

                                  <td>
                                    <span class="text-muted text-size-small">`+response[i].author+`</span>
                                  </td>
                                  <td>
                                    <span class="text-muted text-size-small">`+response[i].time+`</span>
                                  </td>

                                  <td>
                                    <span class="text-muted text-size-small">`+response[i].delivery_time+`</span>
                                  </td>

                                  <td>
                                      <div class="media-heading">
                                        <span class="text-muted text-size-small">`+status +`</span>
                                      </div>
                                      <div class="text-muted text-size-small">
                                        <span class="text-muted text-size-small">`+ Typs+`</span>

                                      </div>
                                    
                                    
                                    
                                  </td>
                                  
                                </tr>
                            `;

                          }
                            $("#Balance_order_id").html((Balance));
                            $("#Table_order").html(tt);

                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

  }


  function get_pie_order(yy='not',mm='not',dd='not'){

      $.ajax({
                      url : 'models/crul_dashboard.php',
                      type : 'POST',
                      data : {
                                yy:yy,
                                mm:mm,
                                dd:dd,
                                type:'get_pie_order'
                              },
                      dataType : 'json',
                      success : function(response) {
                         
                         draw_pie_orders(response);
                          
                          
                      },
                      complete: function() {
                             
                      }

                }); // submit ajax

  }


  function get_pie_sells(yy='not',mm='not',dd='not'){

      $.ajax({
                      url : 'models/crul_dashboard.php',
                      type : 'POST',
                      data : {
                                yy:yy,
                                mm:mm,
                                dd:dd,
                                type:'get_pie_sells'
                              },
                      dataType : 'json',
                      success : function(response) {
                         
                         draw_pie_sells(response);
                          
                          
                      },
                      complete: function() {
                             
                      }

                }); // submit ajax

  }


  function get_pie_expenses(yy='not',mm='not',dd='not'){

      $.ajax({
                      url : 'models/crul_dashboard.php',
                      type : 'POST',
                      data : {
                                yy:yy,
                                mm:mm,
                                dd:dd,
                                type:'get_pie_expenses'
                              },
                      dataType : 'json',
                      success : function(response) {
                         
                         draw_pie_expenses(response);
                          
                          
                      },
                      complete: function() {
                             
                      }

                }); // submit ajax

  }


  function get_pie_purchase(yy='not',mm='not',dd='not'){

      $.ajax({
                      url : 'models/crul_dashboard.php',
                      type : 'POST',
                      data : {
                                yy:yy,
                                mm:mm,
                                dd:dd,
                                type:'get_pie_purchase'
                              },
                      dataType : 'json',
                      success : function(response) {
                         
                         draw_pie_purchase(response);
                          
                          
                      },
                      complete: function() {
                             
                      }

                }); // submit ajax

  }





    

   


    


    function get_user_sells(){

      $.ajax({
                      url : 'models/crul_dashboard.php',
                      type : 'POST',
                      data : {type:'get_user_sells'},
                      dataType : 'json',
                      success : function(response) {
                          console.log(response)
                          var Balance_today     =0;
                          var Balance_yesterday =0;
                          var pri               = 0;
                          var tt =` <tr class="active border-double">
                                      <td colspan="5">Today</td>
                                      <td class="text-right">
                                        <span class="progress-meter" id="today-progress" data-progress="30"></span>
                                      </td>
                                    </tr>`;

                          var yy =` <tr class="active border-double">
                                      <td colspan="5">Yesterday</td>
                                      <td class="text-right">
                                        <span class="progress-meter" id="yesterday-progress" data-progress="65"></span>
                                      </td>
                                    </tr>`;
                          // just to calculate Total Sell price and compute the ratio

                          for (var i = 0; i < response.length; i++) {
                            pri = response[i][1] ;

                            if(response[i][5]==0){
                              Balance_today     = +Balance_today + +pri;
                            }else{
                              Balance_yesterday = +Balance_yesterday + +pri;
                            }
                            
                          }

                          for (var i = 0; i < response.length; i++) {
                            pri = response[i][1] ;
                            var img = "image/profile/noimg.png";
                            if(response[i][4] !=null && response[i][4] !=''){
                                img = "image/profile/"+response[i][4] ;
                            }
                            if(response[i][5]==0){
                              var ratio = pri * (100 / Balance_today);
                              ratio     = parseFloat(ratio).toFixed(2);
                               tt = tt + `

                                <tr>
                                  <td>
                                    <div class="media-left media-middle">
                                      <a href="#"><img src="`+img+`" class="img-circle img-xs" alt=""></a>
                                    </div>
                                    <div class="media-left">
                                      <div class=""><a href="#" class="text-default text-semibold">`+response[i][2]+`</a></div>
                                      <div class="text-muted text-size-small">
                                        <span class="status-mark border-blue position-left"></span>
                                        `+response[i][3]+`
                                      </div>
                                    </div>
                                  </td>
                                  <td><span class="text-muted">`+response[i][0]+`</span></td>
                                  <td><span class="text-success-600"><i class="icon-stats-growth2 position-left"></i>`+ratio+`%</span></td>
                                  <td><h6 class="text-semibold">`+currency_format_display(pri)+`</h6></td>
                                  <td><span class="label bg-blue">Active</span></td>
                                </tr>


                               
                            `;
                            }else{
                              var ratio = pri * (100 / Balance_yesterday);
                              ratio     = parseFloat(ratio).toFixed(2);
                              yy = yy + `

                                <tr>
                                  <td>
                                    <div class="media-left media-middle">
                                      <a href="#"><img src="`+img+`" class="img-circle img-xs" alt=""></a>
                                    </div>
                                    <div class="media-left">
                                      <div class=""><a href="#" class="text-default text-semibold">`+response[i][2]+`</a></div>
                                      <div class="text-muted text-size-small">
                                        <span class="status-mark border-blue position-left"></span>
                                        `+response[i][3]+`
                                      </div>
                                    </div>
                                  </td>
                                  <td><span class="text-muted">`+response[i][0]+`</span></td>
                                  <td><span class="text-success-600"><i class="icon-stats-growth2 position-left"></i>`+ratio+`%</span></td>
                                  <td><h6 class="text-semibold">`+currency_format_display(pri)+`</h6></td>
                                  <td><span class="label bg-blue">Active</span></td>
                                </tr>


                               
                              `;
                            }


                           

                          }
                            $("#user_active").html(response.length + " Activity");
                            $("#today").html(tt+yy);

                      },
                      complete: function() {
                             //  me.data('requestRunning', false);
                      }

                }); // submit ajax

    }

    


$(document).ready(function() {
    
    // for top widget
    get_total_purchase(query_y,query_m,query_d);
    get_total_expenses(query_y,query_m,query_d);
    get_total_shop_capital(query_y,query_m,query_d);
    get_total_factory_capital(query_y,query_m,query_d);
    get_total_sells(query_y,query_m,query_d);
    get_total_sells_raw(query_y,query_m,query_d);
    get_total_sells_single(query_y,query_m,query_d);
    get_total_sells_plural(query_y,query_m,query_d);
    get_total_payments(query_y,query_m,query_d);
    get_total_discount(query_y,query_m,query_d);
    get_total_return(query_y,query_m,query_d);
    get_total_remained(query_y,query_m,query_d);

    get_total_product(query_y,query_m,query_d);
    get_total_supplier(query_y,query_m,query_d);
    // get_total_reward(query_y,query_m,query_d);
    // get_total_penalty(query_y,query_m,query_d);
    // get_total_sallary(query_y,query_m,query_d);
    // end to widget

    // for  daily tables
    get_daily_invoice();
    get_daily_expenses();
    get_purchase(query_y,query_m,query_d);
    get_order(query_y,query_m,query_d);
    
    // End Tables Section

    //  FOr Pie Chart
    get_pie_order(query_y,query_m,query_d);
    get_pie_sells(query_y,query_m,query_d);
    get_pie_expenses(query_y,query_m,query_d);
    get_pie_purchase(query_y,query_m,query_d);




    
    // get_first_sell_date();
    
    // get_daily_sells();
    
    // get_user_sells();

    $("#select_date").html()

    $('#filter_btn').on('click',function(){
      
      if($("#year_filter").val()!='' || $("#month_filter").val()!='' ){
        var yy = $("#year_filter").val();
        var mm = $("#month_filter").val();
        
        var _href ="index.php?p=dashboard";

        if(yy !=''){
         
          _href = _href + "&y="+yy;
        }

        if(mm !=''){
         
          _href = _href + "&m="+mm;
        }


        if($("#day_filter").val()!=''){
          var dd = $("#day_filter").val();
          _href = _href + "&d="+dd;
        }

      
        window.location = _href;
      }else{
            swal({
                    title:"Error",
                    text: Translation('الرجاء قم بتحديد السنة وا الشهر'),
                    confirmButtonColor: "#66BB6A",
                    type: "warning"
                });
      }


    });

  // $("#year_filter").selectpicker('refresh');
  // $("#month_filter").selectpicker('refresh');
  // $("#day_filter").selectpicker('refresh');

});
  

function draw_pie_orders(res){

    var months_val = ['0','0','0','0','0','0','0','0','0','0','0','0','0'];

    for (var i = 0; i < res.length; i++) {
                            
        months_val[res[i].montn_no] = res[i].no_of_orders

    }


    if(res.length ==0){
      if(query_y =='not'){
        var yyy = (new Date()).getFullYear();
      }else{
        var yyy = query_y;
      }
      
    }else{
      var yyy = res[0].year;
    }
  
    require.config({
          paths: {
              echarts: 'assets/js/plugins/visualization/echarts'
          }
      });


     
     

      // Configuration
      // ------------------------------

      require(
          [
              'echarts',
              'echarts/theme/limitless',
              'echarts/chart/bar',
              'echarts/chart/line',
              'echarts/chart/pie',
              'echarts/chart/funnel'
          ],


          // Charts setup
          function (ec, limitless) {


              // Initialize charts
              // ------------------------------

               var rose_diagram_visible = ec.init(document.getElementById('rose_diagram_visible'), limitless);
             
              // Charts setup
              // ------------------------------                    

  
              //
              // Nightingale roses with visible labels options
              //

              rose_diagram_visible_options = {

                  // Add title
                  title: {
                      text: 'Order\'s review',
                      subtext: yyy,
                      x: 'center'
                  },

                  // Add tooltip
                  tooltip: {
                      trigger: 'item',
                      formatter: "{a} <br/>{b}: +{c} ({d}%)"
                  },

                  // Add legend
                  legend: {
                      x: 'left',
                      y: 'top',
                      orient: 'vertical',
                      data: ['January','February','March','April','May','June','July','August','September','October','November','December']
                  },

                  // Display toolbox
                  toolbox: {
                      show: true,
                      orient: 'vertical',
                      feature: {
                          mark: {
                              show: true,
                              title: {
                                  mark: 'Markline switch',
                                  markUndo: 'Undo markline',
                                  markClear: 'Clear markline'
                              }
                          },
                          dataView: {
                              show: true,
                              readOnly: false,
                              title: 'View data',
                              lang: ['View chart data', 'Close', 'Update']
                          },
                          magicType: {
                              show: true,
                              title: {
                                  pie: 'Switch to pies',
                                  funnel: 'Switch to funnel',
                              },
                              type: ['pie', 'funnel']
                          },
                          restore: {
                              show: true,
                              title: 'Restore'
                          },
                          saveAsImage: {
                              show: true,
                              title: 'Same as image',
                              lang: ['Save']
                          }
                      }
                  },

                  // Enable drag recalculate
                  calculable: false,

                  // Add series
                  series: [
                      {
                          name: 'Number of Orders',
                          type: 'pie',
                          radius: ['15%', '73%'],
                          center: ['50%', '57%'],
                          roseType: 'area',

                          // Funnel
                          width: '40%',
                          height: '78%',
                          x: '30%',
                          y: '17.5%',
                          max: 450,
                          sort: 'ascending',

                          data: [
                              {value: months_val[1], name: 'January'},
                              {value: months_val[2], name: 'February'},
                              {value: months_val[3], name: 'March'},
                              {value: months_val[4], name: 'April'},
                              {value: months_val[5], name: 'May'},
                              {value: months_val[6], name: 'June'},
                              {value: months_val[7], name: 'July'},
                              {value: months_val[8], name: 'August'},
                              {value: months_val[9], name: 'September'},
                              {value: months_val[10], name: 'October'},
                              {value: months_val[11], name: 'November'},
                              {value: months_val[12], name: 'December'}
                          ]
                      }
                  ]
              };


             



              // Apply options
              // ------------------------------
              rose_diagram_visible.setOption(rose_diagram_visible_options);
              


              // Resize charts
              // ------------------------------

              window.onresize = function () {
                  setTimeout(function (){

                      rose_diagram_visible.resize();
                   }, 200);
              }
          }


         
      );
}




function draw_pie_sells(res){

    var months_val = ['0','0','0','0','0','0','0','0','0','0','0','0','0'];

    for (var i = 0; i < res.length; i++) {
                            
        months_val[res[i].montn_no] = res[i].final_price

    }


    if(res.length ==0){
      if(query_y =='not'){
        var yyy = (new Date()).getFullYear();
      }else{
        var yyy = query_y;
      }
      
    }else{
      var yyy = res[0].year;
    }
  
    require.config({
          paths: {
              echarts: 'assets/js/plugins/visualization/echarts'
          }
      });


     
     

      // Configuration
      // ------------------------------

      require(
          [
              'echarts',
              'echarts/theme/limitless',
              'echarts/chart/bar',
              'echarts/chart/line',
              'echarts/chart/pie',
              'echarts/chart/funnel'
          ],


          // Charts setup
          function (ec, limitless) {


              // Initialize charts
              // ------------------------------

               var rose_diagram_visible = ec.init(document.getElementById('rose_diagram_visible_sells'), limitless);
             
              // Charts setup
              // ------------------------------                    

  
              //
              // Nightingale roses with visible labels options
              //

              rose_diagram_visible_options = {

                  // Add title
                  title: {
                      text: 'Sells\'s review',
                      subtext: yyy,
                      x: 'center'
                  },

                  // Add tooltip
                  tooltip: {
                      trigger: 'item',
                      formatter: "{a} <br/>{b}: {c} $ ({d}%)"
                  },

                  // Add legend
                  legend: {
                      x: 'left',
                      y: 'top',
                      orient: 'vertical',
                      data: ['January','February','March','April','May','June','July','August','September','October','November','December']
                  },

                  // Display toolbox
                  toolbox: {
                      show: true,
                      orient: 'vertical',
                      feature: {
                          mark: {
                              show: true,
                              title: {
                                  mark: 'Markline switch',
                                  markUndo: 'Undo markline',
                                  markClear: 'Clear markline'
                              }
                          },
                          dataView: {
                              show: true,
                              readOnly: false,
                              title: 'View data',
                              lang: ['View chart data', 'Close', 'Update']
                          },
                          magicType: {
                              show: true,
                              title: {
                                  pie: 'Switch to pies',
                                  funnel: 'Switch to funnel',
                              },
                              type: ['pie', 'funnel']
                          },
                          restore: {
                              show: true,
                              title: 'Restore'
                          },
                          saveAsImage: {
                              show: true,
                              title: 'Same as image',
                              lang: ['Save']
                          }
                      }
                  },

                  // Enable drag recalculate
                  calculable: false,

                  // Add series
                  series: [
                      {
                          name: 'Total Sells Price',
                          type: 'pie',
                          radius: ['15%', '73%'],
                          center: ['50%', '57%'],
                          roseType: 'area',

                          // Funnel
                          width: '40%',
                          height: '78%',
                          x: '30%',
                          y: '17.5%',
                          max: 450,
                          sort: 'ascending',

                          data: [
                              {value: months_val[1], name: 'January'},
                              {value: months_val[2], name: 'February'},
                              {value: months_val[3], name: 'March'},
                              {value: months_val[4], name: 'April'},
                              {value: months_val[5], name: 'May'},
                              {value: months_val[6], name: 'June'},
                              {value: months_val[7], name: 'July'},
                              {value: months_val[8], name: 'August'},
                              {value: months_val[9], name: 'September'},
                              {value: months_val[10], name: 'October'},
                              {value: months_val[11], name: 'November'},
                              {value: months_val[12], name: 'December'}
                          ]
                      }
                  ]
              };


             



              // Apply options
              // ------------------------------
              rose_diagram_visible.setOption(rose_diagram_visible_options);
              


              // Resize charts
              // ------------------------------

              window.onresize = function () {
                  setTimeout(function (){

                      rose_diagram_visible.resize();
                   }, 200);
              }
          }


         
      );
}



function draw_pie_expenses(res){

    var months_val = ['0','0','0','0','0','0','0','0','0','0','0','0','0'];

    for (var i = 0; i < res.length; i++) {
                            
        months_val[res[i].montn_no] = res[i].final_price

    }


    if(res.length ==0){
      if(query_y =='not'){
        var yyy = (new Date()).getFullYear();
      }else{
        var yyy = query_y;
      }
      
    }else{
      var yyy = res[0].year;
    }
  
    require.config({
          paths: {
              echarts: 'assets/js/plugins/visualization/echarts'
          }
      });


     
     

      // Configuration
      // ------------------------------

      require(
          [
              'echarts',
              'echarts/theme/limitless',
              'echarts/chart/bar',
              'echarts/chart/line',
              'echarts/chart/pie',
              'echarts/chart/funnel'
          ],


          // Charts setup
          function (ec, limitless) {


              // Initialize charts
              // ------------------------------

               var rose_diagram_visible = ec.init(document.getElementById('rose_diagram_visible_expenses'), limitless);
             
              // Charts setup
              // ------------------------------                    

  
              //
              // Nightingale roses with visible labels options
              //

              rose_diagram_visible_options = {

                  // Add title
                  title: {
                      text: 'Expense\'s review',
                      subtext: yyy,
                      x: 'center'
                  },

                  // Add tooltip
                  tooltip: {
                      trigger: 'item',
                      formatter: "{a} <br/>{b}: {c} $ ({d}%)"
                  },

                  // Add legend
                  legend: {
                      x: 'left',
                      y: 'top',
                      orient: 'vertical',
                      data: ['January','February','March','April','May','June','July','August','September','October','November','December']
                  },

                  // Display toolbox
                  toolbox: {
                      show: true,
                      orient: 'vertical',
                      feature: {
                          mark: {
                              show: true,
                              title: {
                                  mark: 'Markline switch',
                                  markUndo: 'Undo markline',
                                  markClear: 'Clear markline'
                              }
                          },
                          dataView: {
                              show: true,
                              readOnly: false,
                              title: 'View data',
                              lang: ['View chart data', 'Close', 'Update']
                          },
                          magicType: {
                              show: true,
                              title: {
                                  pie: 'Switch to pies',
                                  funnel: 'Switch to funnel',
                              },
                              type: ['pie', 'funnel']
                          },
                          restore: {
                              show: true,
                              title: 'Restore'
                          },
                          saveAsImage: {
                              show: true,
                              title: 'Same as image',
                              lang: ['Save']
                          }
                      }
                  },

                  // Enable drag recalculate
                  calculable: false,

                  // Add series
                  series: [
                      {
                          name: 'Total Expense Price',
                          type: 'pie',
                          radius: ['15%', '73%'],
                          center: ['50%', '57%'],
                          roseType: 'area',

                          // Funnel
                          width: '40%',
                          height: '78%',
                          x: '30%',
                          y: '17.5%',
                          max: 450,
                          sort: 'ascending',

                          data: [
                              {value: months_val[1], name: 'January'},
                              {value: months_val[2], name: 'February'},
                              {value: months_val[3], name: 'March'},
                              {value: months_val[4], name: 'April'},
                              {value: months_val[5], name: 'May'},
                              {value: months_val[6], name: 'June'},
                              {value: months_val[7], name: 'July'},
                              {value: months_val[8], name: 'August'},
                              {value: months_val[9], name: 'September'},
                              {value: months_val[10], name: 'October'},
                              {value: months_val[11], name: 'November'},
                              {value: months_val[12], name: 'December'}
                          ]
                      }
                  ]
              };


             



              // Apply options
              // ------------------------------
              rose_diagram_visible.setOption(rose_diagram_visible_options);
              


              // Resize charts
              // ------------------------------

              window.onresize = function () {
                  setTimeout(function (){

                      rose_diagram_visible.resize();
                   }, 200);
              }
          }


         
      );
}


function draw_pie_purchase(res){

    var months_val = ['0','0','0','0','0','0','0','0','0','0','0','0','0'];

    for (var i = 0; i < res.length; i++) {
                            
        months_val[res[i].montn_no] = res[i].final_price

    }


    if(res.length ==0){
      if(query_y =='not'){
        var yyy = (new Date()).getFullYear();
      }else{
        var yyy = query_y;
      }
      
    }else{
      var yyy = res[0].year;
    }
  
    require.config({
          paths: {
              echarts: 'assets/js/plugins/visualization/echarts'
          }
      });


     
     

      // Configuration
      // ------------------------------

      require(
          [
              'echarts',
              'echarts/theme/limitless',
              'echarts/chart/bar',
              'echarts/chart/line',
              'echarts/chart/pie',
              'echarts/chart/funnel'
          ],


          // Charts setup
          function (ec, limitless) {


              // Initialize charts
              // ------------------------------

               var rose_diagram_visible = ec.init(document.getElementById('rose_diagram_visible_purchase'), limitless);
             
              // Charts setup
              // ------------------------------                    

  
              //
              // Nightingale roses with visible labels options
              //

              rose_diagram_visible_options = {

                  // Add title
                  title: {
                      text: 'Purchase\'s Review',
                      subtext: yyy,
                      x: 'center'
                  },

                  // Add tooltip
                  tooltip: {
                      trigger: 'item',
                      formatter: "{a} <br/>{b}: {c} $ ({d}%)"
                  },

                  // Add legend
                  legend: {
                      x: 'left',
                      y: 'top',
                      orient: 'vertical',
                      data: ['January','February','March','April','May','June','July','August','September','October','November','December']
                  },

                  // Display toolbox
                  toolbox: {
                      show: true,
                      orient: 'vertical',
                      feature: {
                          mark: {
                              show: true,
                              title: {
                                  mark: 'Markline switch',
                                  markUndo: 'Undo markline',
                                  markClear: 'Clear markline'
                              }
                          },
                          dataView: {
                              show: true,
                              readOnly: false,
                              title: 'View data',
                              lang: ['View chart data', 'Close', 'Update']
                          },
                          magicType: {
                              show: true,
                              title: {
                                  pie: 'Switch to pies',
                                  funnel: 'Switch to funnel',
                              },
                              type: ['pie', 'funnel']
                          },
                          restore: {
                              show: true,
                              title: 'Restore'
                          },
                          saveAsImage: {
                              show: true,
                              title: 'Same as image',
                              lang: ['Save']
                          }
                      }
                  },

                  // Enable drag recalculate
                  calculable: false,

                  // Add series
                  series: [
                      {
                          name: 'Total Purchase Price',
                          type: 'pie',
                          radius: ['15%', '73%'],
                          center: ['50%', '57%'],
                          roseType: 'area',

                          // Funnel
                          width: '40%',
                          height: '78%',
                          x: '30%',
                          y: '17.5%',
                          max: 450,
                          sort: 'ascending',

                          data: [
                              {value: months_val[1], name: 'January'},
                              {value: months_val[2], name: 'February'},
                              {value: months_val[3], name: 'March'},
                              {value: months_val[4], name: 'April'},
                              {value: months_val[5], name: 'May'},
                              {value: months_val[6], name: 'June'},
                              {value: months_val[7], name: 'July'},
                              {value: months_val[8], name: 'August'},
                              {value: months_val[9], name: 'September'},
                              {value: months_val[10], name: 'October'},
                              {value: months_val[11], name: 'November'},
                              {value: months_val[12], name: 'December'}
                          ]
                      }
                  ]
              };


             



              // Apply options
              // ------------------------------
              rose_diagram_visible.setOption(rose_diagram_visible_options);
              


              // Resize charts
              // ------------------------------

              window.onresize = function () {
                  setTimeout(function (){

                      rose_diagram_visible.resize();
                   }, 200);
              }
          }


         
      );
}

  


$(function() {

  


  if(query_y != 'not' ){
    $("#year_filter").val(query_y);
  }

  if( query_m != 'not'){
    $("#month_filter").val(query_m);
  }

  
  if(query_d != 'not'){
    $("#day_filter").val(query_d);
  }

    // Set paths
    // ------------------------------

    





});



