CustomerNumbers.php 2.7 KB
<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use App\Models\Customers;
use App\Models\Contacts;
use DB;

class CustomerNumbers extends Model
{
    protected $table = 'customer_numbers';

    public function getRecordOfAltNumber($campaignArr, $number)
    {
        $numAvailInCustNumbers = array();

        $numAvailInCustNumbers = Contacts::join('customers as cs', 'cs.id', '=', 'contacts.customer_id')
                                ->join('customer_numbers as cn', 'cn.customer_id' ,'=', 'cs.id')
                                ->whereIn('contacts.client', $campaignArr)
                                ->where('cn.number', '=', $number)
                                ->select('cs.peopledata')
                                ->get()
                                ->toArray();

        return $numAvailInCustNumbers;
    }

    public function searchPhone($mobile){
        $mobile10=substr($mobile,-10);
        $inb_cust_data = array();
        /*$inb_cust_data = Contacts::join('customerss as cs', 'cs.id', '=', 'contacts.customer_id')
                                ->join('customer_numbers as cn', 'cn.customer_id' ,'=', 'cs.id')
                                ->where('Right(cn.number,10)', '=', $mobile10)
                                ->select('cs.peopledata')
                                ->get()
                                ->toArray();*/

        $inb_cust_data = DB::select(DB::raw("select * from contacts as ct join customers as cs on cs.id = ct.customer_id join customer_numbers as cn on cn.customer_id=cs.id where RIGHT(cn.number,10)=$mobile10 limit 1;"));
        if(empty($inb_cust_data)){
            DB::select(DB::raw("select * from contacts as ct join customers as cs on cs.id = ct.customer_id where RIGHT(ct.mobile,10)=$mobile10 limit 1;"));
        }
        return $inb_cust_data;
    }

    public function addAltNumber($customerId, $ppldataSerialize, $altnumberArr)
    {
        $customers  = Customers::where('id', '=', $customerId)
                                ->update(['peopledata'=>$ppldataSerialize]);

        $customernumbers                = new CustomerNumbers();
        $customernumbers->customer_id   = $customerId;
        $customernumbers->number        = $altnumberArr[0];
        $customernumbers->type          = $altnumberArr[1];
        $customernumbers->save();
        
        return $customers;
    }

    public function delAltNumber($customerId, $ppldataSerialize, $altnumber)
    {
        $customers = Customers::where('id', '=', $customerId)
                                ->update(['peopledata'=>$ppldataSerialize]);
                    
        $customernumbers = CustomerNumbers::where('number', '=', $altnumber)->delete();

        return $customers;
    }
}