<?php

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

class Users_model extends MY_Model {

  /**
   * Get account by id
   *
   * @access public
   * @param string $account_id
   * @return object account object
   */
  public function create_user() {

    $uploaded_file = "";
    /*
    if (!empty($_FILES) && $_FILES['image1']['name'] != '') {
      $config['max_size'] = '2048';
      $this->load->library('upload');
      $uploaded_data = $this->upload->my_upload('image1', 'user');
      if (is_array($uploaded_data) && !empty($uploaded_data)) {
        $uploaded_file = $uploaded_data['upload_data']['file_name'];
      }
    }*/

    $aadhar_card = "";
    if (!empty($_FILES) && $_FILES['aadhar_card']['name'] != '') {
      $config['max_size'] = '2048';
      $this->load->library('upload');
      $uploaded_data = $this->upload->my_upload('aadhar_card', 'user');
      if (is_array($uploaded_data) && !empty($uploaded_data)) {
        $aadhar_card = $uploaded_data['upload_data']['file_name'];
      }
    }
    $pan_card = "";
    if (!empty($_FILES) && $_FILES['pan_card']['name'] != '') {
      $config['max_size'] = '2048';
      $this->load->library('upload');
      $uploaded_data = $this->upload->my_upload('pan_card', 'user');
      if (is_array($uploaded_data) && !empty($uploaded_data)) {
        $pan_card = $uploaded_data['upload_data']['file_name'];
      }
    }
    $cancelled_Cheque = "";
    if (!empty($_FILES) && $_FILES['cancelled_Cheque']['name'] != '') {
      $config['max_size'] = '2048';
      $this->load->library('upload');
      $uploaded_data = $this->upload->my_upload('cancelled_Cheque', 'user');
      if (is_array($uploaded_data) && !empty($uploaded_data)) {
        $cancelled_Cheque = $uploaded_data['upload_data']['file_name'];
      }
    }
    $address_proof = "";
    if (!empty($_FILES) && $_FILES['address_proof']['name'] != '') {
      $config['max_size'] = '2048';
      $this->load->library('upload');
      $uploaded_data = $this->upload->my_upload('address_proof', 'user');
      if (is_array($uploaded_data) && !empty($uploaded_data)) {
        $address_proof = $uploaded_data['upload_data']['file_name'];
      }
    }

    $password = $this->safe_encrypt->encode($this->input->post('password', TRUE));

    $infra_uploaded_file1='';
    $infra_uploaded_file2='';
    $infra_uploaded_file3='';
    $infra_uploaded_file4='';

    $register_array = array(
        //'district_head_id' => $this->input->post('district_head_id'),
        //'kisan_mitra_id' => $this->input->post('kisan_mitra_id'),
        //'kisan_sahayak_id' => $this->input->post('kisan_sahayak_id'),
        'user_name' => $this->input->post('mobile_number', TRUE),
        'password' => $password,
        'mobile_number' => $this->input->post('mobile_number', TRUE),
        'first_name' => $this->input->post('first_name', TRUE),
        'fathers_name' => $this->input->post('fathers_name', TRUE),
        'birth_date' => $this->input->post('birth_date', TRUE),
        'profile_picture' => $uploaded_file,
        'alt_name' => $this->input->post('alt_name', TRUE),
        'alt_mobile' => $this->input->post('alt_mobile', TRUE),
        'relationship' => $this->input->post('relationship', TRUE),
        'land_size_owned' => $this->input->post('land_size_owned', TRUE),
        'land_size_owned_unit' => $this->input->post('land_size_owned_unit', TRUE),
        'land_size_leased' => $this->input->post('land_size_leased', TRUE),
        'land_size_leased_unit' => $this->input->post('land_size_leased_unit', TRUE),
        'total_land_size' => $this->input->post('total_land_size', TRUE),
        'current_annual_income' => $this->input->post('current_annual_income', TRUE),
        'major_issues' => $this->input->post('major_issues', TRUE),
        'aadhar_card' => $aadhar_card,
        'pan_card' => $pan_card,
        'cancelled_Cheque' => $cancelled_Cheque,
        'address_proof' => $address_proof,
        'country_id' => $this->input->post('country_id'),
        'state_id' => $this->input->post('state_id'),
        'city_id' => $this->input->post('city_id'),
        'block_id' => $this->input->post('block_id'),
        'panchayat_id' => $this->input->post('panchayat_id'),
        'village_id' => $this->input->post('village_id'),
        'status' => '1',
        'is_verified' => '1',
        'add_admin_type' => '1',// admin or self
        'actkey' => md5($this->input->post('mobile_number')),
        'account_created_date' => $this->config->item('config.date.time'),
        'current_login' => $this->config->item('config.date.time'),
        'ip_address' => $this->input->ip_address()

    );
    $insId = $this->safe_insert('wps_customers', $register_array, FALSE);

    
    
    if ($insId > 0) {

      //insert crops data
      $postCategory = $this->input->post('category_id');
      $category_links = "";
      $catlink = $ctl = "";
      foreach ($postCategory as $ctv) {
        $catlink = get_parent_categories($ctv, "AND status='1'", "category_id,parent_id");
        $ctl = array_keys($catlink);
        $category_links .= implode(",", $ctl) . ',';
      }
      $category_links = substr($category_links, 0, -1);

      $posted_kisan_crops = array(
        'customers_id' => $insId,
        'category_id' => implode(',', $this->input->post('category_id')),
        'category_links' => $category_links,
        'qty_per_year' => implode(',', $this->input->post('qty_per_year')),
        'selling_price_msp' => implode(',', $this->input->post('selling_price_msp')),
        'selling_month' => implode(',', $this->input->post('selling_month')),
    );
    //trace($posted_customer_factsheet);
    $this->safe_insert('wps_kisan_crops', $posted_kisan_crops, FALSE);



      $posted_customer_factsheet = array(
        'customers_id' => $insId,
        'business_nature' => $this->input->post('business_nature'),
        'business_nature_alt' => implode(',', $this->input->post('business_nature_alt')),
        'ceo_name' => $this->input->post('ceo_name'),
        'registered_address' => $this->input->post('registered_address'),
        'about_company' => $this->input->post('about_company'),
        'youtube_id' => $this->input->post('youtube_id'),
        'employee_count' => $this->input->post('employee_count'),
        'establishment_year' => $this->input->post('establishment_year'),
        'legal_firm_status' => $this->input->post('legal_firm_status'),
        'promoter_name' => $this->input->post('promoter_name'),
        'turnover' => $this->input->post('turnover'),
        'building_infrastructure' => $this->input->post('building_infrastructure'),
        "infra_img" => $infra_uploaded_file1,
        "infra_img2" => $infra_uploaded_file2,
        "infra_img3" => $infra_uploaded_file3,
        "infra_img4" => $infra_uploaded_file4,
        'premises_size' => $this->input->post('premises_size'),
        'premises_unit' => $this->input->post('premises_unit'),
        'competitive_advantage' => $this->input->post('competitive_advantage'),
        'contract_manufacturing' => $this->input->post('contract_manufacturing'),
        'testing_facilities' => $this->input->post('testing_facilities'),
        'ie_code' => $this->input->post('ie_code'),
        'banker' => $this->input->post('banker'),
        'gst_no' => $this->input->post('gst_no'),
        'customized_packaging' => $this->input->post('customized_packaging'),
        'payment_mode' => @implode(',', $this->input->post('payment_mode')),
        'shipment_mode' => $this->input->post('shipment_mode'),
      );
      //trace($posted_customer_factsheet);
      $this->safe_insert('wps_customers_factsheet', $posted_customer_factsheet, FALSE);
        /*
        $add_array = array(
            'customer_id' => $insId,
            'reciv_date' => $this->config->item('config.date.time'),
            'address_type' => 'Bill',
            'default_status' => 'Y'
        );
        $this->safe_insert('wps_customers_address_book', $add_array, FALSE);
        */
    }
    return $insId;
  }

  public function create_mobile_user($mobile, $email, $password) {
    $register_array = array(
        'user_name' => ($mobile)?$mobile:$email,
        'password' => $this->safe_encrypt->encode($password),
        'first_name' => $this->input->post('firstname', TRUE),
        'last_name' => $this->input->post('lastname', TRUE),
        'user_email' => $email,
        'mobile_number' => $mobile,
        'user_location' => '',
        'country'=> $this->input->post('country', TRUE),
        'actkey' => md5($this->input->post('mobile', TRUE)),
        'account_created_date' => $this->config->item('config.date.time'),
        'current_login' => $this->config->item('config.date.time'),
        'status' => '1',
        'is_verified' => ($mobile)?'1':'0',
        'ip_address' => $this->input->ip_address()
    );
    $insId = $this->safe_insert('wps_customers', $register_array, FALSE);
    return $insId;
  }

  public function is_email_exits($data) {
    $this->db->select('customers_id');
    $this->db->from('wps_customers');
    $this->db->where($data);
    $this->db->where('status !=', '2');

    $query = $this->db->get();
    if ($query->num_rows() == 1) {
      return TRUE;
    } else {
      return FALSE;
    }
  }

  public function logout() {
    $data = array(
        'user_id' => 0,
        'email' => 0,
        'name' => 0,
        'user_photo' => 0,
        'logged_in' => FALSE
    );
    $this->session->sess_destroy();
    $this->session->unset_userdata($data);
  }

}

/* End of file users_model.php */
/* Location: ./application/modules/users/models/users_model.php */