<?php



if (!defined('BASEPATH'))

  exit('No direct script access allowed');



class Kisans_model extends MY_Model {



  public function get_kisans($limit = '10', $offset = '0', $param = array()) {



    $status = @$param['status'];

    $customer_id = @$param['customer_id'];

    $state = @$param['state'];

    $city = @$param['city'];

    $keyword = trim($this->input->get_post('keyword', TRUE));

    $keyword = $this->db->escape_str($keyword); 

    $district_head_id = @$param['district_head_id']; 
    $kisan_mitra_id = @$param['kisan_mitra_id']; 
    $kisan_sahayak_id = @$param['kisan_sahayak_id']; 
    $customer_type = @$param['customer_type']; 
    

    if ($district_head_id != '') {
      $this->db->where("district_head_id", "$district_head_id");
    }
    if ($kisan_mitra_id != '') {
      $this->db->where("kisan_mitra_id", "$kisan_mitra_id");
    }
    if ($kisan_sahayak_id != '') {
      $this->db->where("kisan_sahayak_id", "$kisan_sahayak_id");
    }

    if ($customer_id != '') {

      $this->db->where("customers_id", "$customer_id");

    }

    if ($customer_type != '') {
      $this->db->where("customer_type", "$customer_type");
    }
    if ($status != '') {
      $this->db->where("status", "$status");
    }
    

    if ($state != '') {

      $this->db->where("state", "$state");

    }

    if ($city != '') {

      $this->db->where("city", "$city");

    }



    if ($keyword != '') {



      $this->db->where("(user_name LIKE '%" . $keyword . "%' OR CONCAT_WS(' ',first_name,last_name) LIKE '%" . $keyword . "%' OR gender LIKE '%" . $keyword . "%'  OR company_name LIKE '%" . $keyword . "%')");

    }



    $this->db->order_by('customers_id', 'desc');
    if($limit=='Unlimited'){
      $this->db->limit(100, $offset);
    }else{
      $this->db->limit($limit, $offset);
    }

    $this->db->select("SQL_CALC_FOUND_ROWS *,CONCAT_WS(' ',first_name,last_name) AS name ", FALSE);

    $this->db->from('wps_customers');

    $this->db->where('status !=', '2');

    $q = $this->db->get();

    //echo_sql();

    $result = $q->result_array();

    $result = ($limit == '1') ? $result[0] : $result;

    return $result;

  }



  public function get_kisan_row($id, $condtion = '') {

    $id = (int) $id;

    if ($id != '' && is_numeric($id)) {

      $condtion = "status !='2' AND customers_id=$id $condtion ";

      $fetch_config = array(

          'condition' => $condtion,

          'debug' => FALSE,

          'return_type' => "array"

      );

      $result = $this->find('wps_customers', $fetch_config);

      return $result;

    }

  }

  

  public function get_kisan_crops($id, $condtion = '') {

    $id = (int) $id;

    if ($id != '' && is_numeric($id)) {

      $condtion = "customers_id = '" . $id . "' $condtion ";

      $fetch_config = array(

          'condition' => $condtion,

          'debug' => FALSE,

          'return_type' => "array"

      );

      $result = $this->find('wps_kisan_crops', $fetch_config);

      return $result;

    }

  }
  

  public function get_kisan_factsheet($id, $condtion = '') {

    $id = (int) $id;

    if ($id != '' && is_numeric($id)) {

      $condtion = "customers_id = '" . $id . "' $condtion ";

      $fetch_config = array(

          'condition' => $condtion,

          'debug' => FALSE,

          'return_type' => "array"

      );

      $result = $this->find('wps_customers_factsheet', $fetch_config);

      return $result;

    }

  }



  public function add_newsletter_kisan($email, $name = NULL) {



    $query = $this->db->query("SELECT * FROM wps_newsletters  WHERE subscriber_email='" . $email . "' ");

    if ($query->num_rows() > 0) {



      $row = $query->row_array();

      if ($row['status'] == 1) {

        $error_type = "error";

        $error_msg = $this->config->item('newsletter_already_subscribed');

      } else {



        $where = "subscriber_email = '" . $row['subscriber_email'] . "'";

        $this->safe_update('wps_newsletters', array('status' => '1'), $where, FALSE);



        $error_type = "success";

        $error_msg = $this->config->item('newsletter_subscribed');

      }

    } else {

      $data = array('status' => '1',

          'subscriber_name' => $name,

          'subscriber_email' => $email

      );

      $this->safe_insert('wps_newsletters', $data);

    }

  }



  public function get_kisan_address_book($customer_id, $offset = '', $limit = '', $address_type = '', $default_status = 'Y') {

    $customer_id = (int) $customer_id;

    $offset = $offset;

    $limit = $limit;



    if ($customer_id != '') {

      $condtion = "customer_id =$customer_id AND default_status='$default_status'  ";



      if ($address_type != '') {



        $condtion .= "AND address_type ='$address_type'";

      }



      $fetch_config = array(

          'condition' => $condtion,

          'debug' => FALSE,

          'return_type' => "array"

      );

      if ($offset != -1) {

        $fetch_config['start'] = $offset;

      }

      if ($limit > 0) {

        $fetch_config['limit'] = $limit;

      }



      //trace($fetch_config);



      $result = $this->findAll('wps_customers_address_book', $fetch_config);

      return $result;

    }

  }



  public function get_wislists($offset = FALSE, $per_page = FALSE, $param = array()) {

    $keyword = trim($this->db->escape_str($this->input->post('keyword')));

    $condition = "wp.status ='1'";

    if ($this->session->userdata('user_id') != '') {

      $condition .= "AND wis.customer_id = " . $this->session->userdata('user_id');

    }

    $opts = array(

        'condition' => $condition,

        'limit' => $per_page,

        'offset' => $offset,

        'debug' => FALSE,

        'fromcond' => 'wps_products AS wp',

        'selectcond' => 'wp.*, wis.customer_id, wis.id,pm.media',

        'joins' => array(array('tblname' => 'wps_wishlists AS wis', 'jclause' => 'wis.products_Id=wp.products_id'), array('tblname' => 'wps_products_media AS pm', 'jclause' => "wp.products_id=pm.products_id AND pm.is_default = 'Y'")),

    );

    return $this->myCustomJoin($opts);

  }


  public function add_bulk_upload_kisan($worksheet) {
    for ($i = 2; $i <= count($worksheet); $i++) {

      // $title = (!isset($worksheet[$i][1])) ? '' : addslashes(trim($worksheet[$i][1]));
      // $first_name = (!isset($worksheet[$i][2])) ? '' : addslashes(trim($worksheet[$i][2]));
      // $last_name = (!isset($worksheet[$i][3])) ? '' : addslashes(trim($worksheet[$i][3]));
      // $mobile_no = (!isset($worksheet[$i][4])) ? '' : addslashes(trim($worksheet[$i][4]));
      // $alt_mobile_no = (!isset($worksheet[$i][5])) ? '' : addslashes(trim($worksheet[$i][5]));

      // $email = (!isset($worksheet[$i][6])) ? '' : addslashes(trim($worksheet[$i][6]));
      // $alt_email_address = (!isset($worksheet[$i][7])) ? '' : addslashes(trim($worksheet[$i][7]));
      // $password = (!isset($worksheet[$i][8])) ? '' : addslashes(trim($worksheet[$i][8]));
      // $company_name = (!isset($worksheet[$i][9])) ? '' : addslashes(trim($worksheet[$i][9]));
      // $address = (!isset($worksheet[$i][10])) ? '' : addslashes(trim($worksheet[$i][10]));
      // $country = (!isset($worksheet[$i][11])) ? '' : addslashes(trim($worksheet[$i][11]));
     
      // $state_name = (!isset($worksheet[$i][12])) ? '' : addslashes(trim($worksheet[$i][12]));
      // $city_name = (!isset($worksheet[$i][13])) ? '' : addslashes(trim($worksheet[$i][13]));
      // $pincode = (!isset($worksheet[$i][14])) ? '' : addslashes(trim($worksheet[$i][14]));

      // $locality = (!isset($worksheet[$i][15])) ? '' : addslashes(trim($worksheet[$i][15]));
      // $landmark = (!isset($worksheet[$i][16])) ? '' : addslashes(trim($worksheet[$i][16]));
     
      // $nature_of_business = (!isset($worksheet[$i][17])) ? '' : addslashes(trim($worksheet[$i][17]));
      // $additional_nature_of_business = (!isset($worksheet[$i][18])) ? '' : addslashes(trim($worksheet[$i][18]));
      // $ceo_name = (!isset($worksheet[$i][19])) ? '' : addslashes(trim($worksheet[$i][19]));
      // $registered_address = (!isset($worksheet[$i][20])) ? '' : addslashes(trim($worksheet[$i][20]));
      // $website = (!isset($worksheet[$i][21])) ? '' : addslashes(trim($worksheet[$i][21]));
      // $designation = (!isset($worksheet[$i][22])) ? '' : addslashes(trim($worksheet[$i][22]));

      // $about_company = (!isset($worksheet[$i][23])) ? '' : addslashes(trim($worksheet[$i][23]));
       $website = (!isset($worksheet[$i][1])) ? '' : addslashes(trim($worksheet[$i][1]));
      $company_name = (!isset($worksheet[$i][2])) ? '' : addslashes(trim($worksheet[$i][2]));
      $title = (!isset($worksheet[$i][3])) ? '' : addslashes(trim($worksheet[$i][3]));
      $first_name = (!isset($worksheet[$i][4])) ? '' : addslashes(trim($worksheet[$i][4]));
      $last_name = (!isset($worksheet[$i][5])) ? '' : addslashes(trim($worksheet[$i][5]));

      $email = (!isset($worksheet[$i][6])) ? '' : addslashes(trim($worksheet[$i][6]));
      $alt_email_address = (!isset($worksheet[$i][7])) ? '' : addslashes(trim($worksheet[$i][7]));
      $mobile_no = (!isset($worksheet[$i][8])) ? '' : addslashes(trim($worksheet[$i][8]));
      $alt_mobile_no = (!isset($worksheet[$i][9])) ? '' : addslashes(trim($worksheet[$i][9]));
      $registered_address = (!isset($worksheet[$i][10])) ? '' : addslashes(trim($worksheet[$i][10]));
      $city_name = (!isset($worksheet[$i][11])) ? '' : addslashes(trim($worksheet[$i][11]));
     
      $state_name = (!isset($worksheet[$i][12])) ? '' : addslashes(trim($worksheet[$i][12]));
      $pincode = (!isset($worksheet[$i][13])) ? '' : addslashes(trim($worksheet[$i][13]));
      $country = (!isset($worksheet[$i][14])) ? '' : addslashes(trim($worksheet[$i][14]));

      $locality = (!isset($worksheet[$i][15])) ? '' : addslashes(trim($worksheet[$i][15]));
      $landmark = (!isset($worksheet[$i][16])) ? '' : addslashes(trim($worksheet[$i][16]));
      $address = (!isset($worksheet[$i][17])) ? '' : addslashes(trim($worksheet[$i][17]));
      $nature_of_business = (!isset($worksheet[$i][18])) ? '' : addslashes(trim($worksheet[$i][18]));
      $additional_nature_of_business = (!isset($worksheet[$i][19])) ? '' : addslashes(trim($worksheet[$i][19]));
      $ceo_name = (!isset($worksheet[$i][20])) ? '' : addslashes(trim($worksheet[$i][20]));
      $designation = (!isset($worksheet[$i][21])) ? '' : addslashes(trim($worksheet[$i][21]));

      $about_company = (!isset($worksheet[$i][22])) ? '' : addslashes(trim($worksheet[$i][22]));
    
      // $state_id = get_db_field_value("wps_states_list", "id", "WHERE name like '%".trim($state_name)."%'");
      $state_id = get_db_field_value("wps_states_list", "id", "WHERE name = '".$state_name."'");
      //$city_id = get_db_field_value("wps_cities_list", "id", "WHERE city like '%".trim($city_name)."%'");
      $city_id = get_db_field_value("wps_cities_list", "id", "WHERE city = '".$city_name."'");
      // if($password==''){
        $password = str_pad(mt_rand(0, 999999), 6, '0', STR_PAD_LEFT);
      // }
      $sss = $this->db->query('select * from wps_customers where user_name = "'.$mobile_no.'"')->result_array();
     
      if(count($sss) > 0){

      }else{
  
      $posted_data_customers = array(
            'title' => $title,
            'user_name' => $mobile_no,
            'password' => $this->safe_encrypt->encode($password),
            'mobile_number' => $mobile_no,
            'first_name' => $first_name,
            'last_name' => $last_name,
            'company_name' => $company_name,
            'profile_picture' => '',
            'designation' => $designation,
            'address' => $address,
            'state' => $state_id,
            'city' => $city_id,
            'country' => $country,
            'landmark' => $landmark,
            'locality' => $locality,
            'zipcode' => $pincode,
            'alt_mobile' => $alt_mobile_no,
            'user_email' => $email,
            'alt_email' => $alt_email_address,
            'website' => $website,
            'status' => '1',
            'is_verified' => '1',
            'is_self_verified' => '1',
            'actkey' => md5($email),
            'account_created_date' => $this->config->item('config.date.time'),
            'current_login' => $this->config->item('config.date.time'),
            'ip_address' => $this->input->ip_address()
      );
      $businessNatureAll = $this->config->item('businessNatureAll');
      $business_nature = array_search ($nature_of_business, $businessNatureAll);

      $s = explode(',', $additional_nature_of_business);
      $businessNature = $this->config->item('businessNature');

      $total = count($s);
      $i=0;
      foreach ($s as $key => $value) {
         $i++;
         $business_nature_alt .= array_search ($value, $businessNature);
         if ($i != $total) $business_nature_alt .= ', ';
      }
     
       $customerID = $this->kisans_model->safe_insert('wps_customers', $posted_data_customers, FALSE);
        $posted_customer_factsheet = array(
            'customers_id' => $customerID,
            'business_nature' => $business_nature,
            'business_nature_alt' => $business_nature_alt,
            'ceo_name' => $ceo_name,
            'registered_address' => $registered_address,
            'about_company' => $about_company,
            'youtube_id' => '',
            'employee_count' => '',
            'establishment_year' => '',
            'legal_firm_status' => '',
            'promoter_name' => '',
            'turnover' => '',
            'building_infrastructure' => '',
            "infra_img" => '',
            "infra_img2" => '',
            "infra_img3" => '',
            "infra_img4" => '',
            'premises_size' => '',
            'premises_unit' => '',
            'competitive_advantage' => '',
            'contract_manufacturing' => '',
            'testing_facilities' => '',
            'ie_code' => '',
            'banker' => '',
            'gst_no' => '',
            'customized_packaging' => '',
            'payment_mode' => '',
            'shipment_mode' => '',
        );
      
      $this->kisans_model->safe_insert('wps_customers_factsheet', $posted_customer_factsheet, FALSE);
    }
    }
    //exit;
    return true;

  }

  public function update_password($old_pass, $id) {

    $cond = "admin_id ='".$id."' AND admin_password ='".$old_pass."'";
    $num_row = $this->findCount('wps_admin', $cond);
    if ($num_row > 0) {
      $data = array(
          'admin_password' => $this->input->post('new_pass'),
      );
      $where = "admin_id=" . $id . " ";
      $this->safe_update('wps_admin', $data, $where, FALSE);

      $this->session->set_userdata('msg_type', "success");
      $this->session->set_flashdata('success', lang('successupdate'));
    } else {
      $this->session->set_userdata(array('msg_type' => 'error'));
      $this->session->set_flashdata('error', lang('password_incorrect'));
    }
  }

  public function update_profile($id,$uploaded_file,$aadhar_card,$pan_card,$cancelled_Cheque,$address_proof,$which_vehicle) {
    $cond = "admin_id = '" . $id . "'";
    $num_row = $this->findCount('wps_admin', $cond);
    if ($num_row > 0) {
      $data = array(
        'admin_name' => $this->input->post('admin_name'),
        'admin_image' => $uploaded_file,
        'phone' => $this->input->post('phone'),
        'contact_phone' => $this->input->post('contact_phone'),
        'admin_email' => $this->input->post('admin_email'),
        'contact_email' => $this->input->post('contact_email'),
        'address' => $this->input->post('address'),
        'pincode' => $this->input->post('pincode'),
        //'admin_country' => $this->input->post('country_id'),
        //'admin_state' => $this->input->post('state_id'),
        //'admin_district' => $this->input->post('city_id'),
        //'admin_block' => $this->input->post('block_id'),
        //'admin_panchayat' => $this->input->post('panchayat_id'),
        //'admin_village' => $this->input->post('village_id'),
        'aadhar_card' => $aadhar_card,
        'pan_card' => $pan_card,
        'cancelled_Cheque' => $cancelled_Cheque,
        'address_proof' => $address_proof,
        'has_laptop' => $this->input->post('has_laptop'),
        'has_smartphone' => $this->input->post('has_smartphone'),
        'has_vehicle' => $this->input->post('has_vehicle'),
        'which_vehicle' => $which_vehicle,
          //'facebook' => $this->input->post('facebook'),
          //'twitter' => $this->input->post('twitter'),
          //'linkedin' => $this->input->post('linkedin'),
          //'youtube' => $this->input->post('youtube'),
          //'whatsapp' => $this->input->post('whatsapp'),
          
      );
      $where = "admin_id=" . $id . " ";
      $this->safe_update('wps_admin', $data, $where, FALSE);
      $this->session->set_userdata('msg_type', "success");
      $this->session->set_flashdata('success', lang('successupdate'));
    } else {
      $this->session->set_userdata(array('msg_type' => 'error'));
      $this->session->set_flashdata('error', "Something went wrong, please try again!");
    }
  }



}

