<?php

namespace App\Models\Firc;

use CodeIgniter\Model;
use CodeIgniter\I18n\Time;

class Md_login extends Model {
    
    public $db;

    public function __construct() {
        parent::__construct();
        $this->firc = \Config\Database::connect('firc');
    }
    
    
 public function loginUserVerify($username,$password,$module) {

        $notifications = $this->services
                ->table('users')
                ->select('pk_user_id,usr_fullname,usr_email,usr_mobileno,module')
                ->where('usr_username', $username)
                ->where('usr_password', $password)
                ->where('module', $module)
                ->where('usr_status','1')
                ->limit(1)
                ->get()
                ->getRow();
                
                if(!empty($notifications))
                {
                    $roles= $this->services
                    ->table('user_privilege')
                    ->select('privilage')
                    ->where('fk_uid', $notifications->pk_user_id)
                    ->limit(1)
                    ->get()
                    ->getRow();
                    
                    $notifications->roles=(!empty($roles)?$roles->privilage:null);
                }
           
        return $notifications;
    }
    
  public function OTPVerifyCheck($Mobile) {

        $notifications = $this->db
                ->table('otp_verify')
                ->where('mobile', $Mobile)
                ->where('verify', 1)
                ->limit(1)
                ->get()
                ->getRow();
        return $notifications;
    }

   public function UpdateData($table, $pk_id, $data = array()) {
        $this->services
                ->table($table)
                ->update($data, $pk_id);
        return true;
    }
    
      public function loginCheck($Mobile,$EncryPassword) {

        $notifications = $this->firc
                ->table('users')
                ->where('usr_username', $Mobile)
                ->where('usr_password', $EncryPassword)
                ->where('usr_status','1')
                ->limit(1)
                ->get()
                ->getRow();
        return $notifications;
    }
    
    public function insertData($table, $data) {
        $this->firc->table($table)->insert($data);
        $result=$this->firc->insertID();
        return $result;
    }
    
    public function getBillingData() {
       $notifications = $this->firc
                ->table('billing')
                ->select('*,DATE_FORMAT(invoice_date, "%d-%b-%Y") as invoice_date')
                ->where('status',1)
                ->orderby('pk_id', 'DESC')
                ->get()
                ->getResult('array');
        return $notifications;
    }
    
    public function getCompany() {
      $notifications = $this->firc
                    ->table('billing')
                    ->select('company_divison')
                    ->distinct()
                    ->where('status', 1)
                    ->orderBy('company_divison', 'ASC')
                    ->get()
                    ->getResultArray();

        return $notifications;
    }
    
    public function getCustomerCompanyWise($comp=null) {
      $notifications = $this->firc
                    ->table('billing')
                    ->select('customer_name')
                    ->distinct()
                    ->where('company_divison',$comp)
                    ->where('status', 1)
                    ->orderBy('customer_name', 'ASC')
                    ->get()
                    ->getResultArray();

        return $notifications;
    }
    
    public function getBillingResult($comp=null,$customer=null) 
    {
        
      $notifications = $this->firc
                    ->table('billing')
                    ->select('*')
                    ->where('company_divison',$comp)
                    ->where('customer_name',$customer)
                    ->where('status', 1)
                    ->orderBy('invoice_date', 'ASC')
                    ->get()
                    ->getResultArray();

        return $notifications;
    }

}