'1') { return strpos( $haystack, $needle, $this->strposX($haystack, $needle, $number - 1) + strlen($needle) ); } else { return error_log('Error: Value for parameter $number is out of range'); } } public function getPaymentNumAttribute() { $position = $this->strposX($this->payment_number, "-", 1) + 1; return substr($this->payment_number, $position); } public static function getNextPaymentNumber($value) { // Get the last created order $payment = Payment::where('payment_number', 'LIKE', $value . '-%') ->orderBy('created_at', 'desc') ->first(); if (!$payment) { // We get here if there is no order at all // If there is no number set it to 0, which will be 1 at the end. $number = 0; } else { $number = explode("-",$payment->payment_number); $number = $number[1]; } // If we have ORD000001 in the database then we only want the number // So the substr returns this 000001 // Add the string in front and higher up the number. // the %05d part makes sure that there are always 6 numbers in the string. // so it adds the missing zero's when needed. return sprintf('%06d', intval($number) + 1); } public function getPaymentPrefixAttribute () { $prefix= explode("-",$this->payment_number)[0]; return $prefix; } public function invoice() { return $this->belongsTo(Invoice::class); } public function user() { return $this->belongsTo(User::class); } public function paymentMethod() { return $this->belongsTo(PaymentMethod::class); } public function getFormattedCreatedAtAttribute($value) { $dateFormat = CompanySetting::getSetting('carbon_date_format', $this->company_id); return Carbon::parse($this->created_at)->format($dateFormat); } public function getFormattedPaymentDateAttribute($value) { $dateFormat = CompanySetting::getSetting('carbon_date_format', $this->company_id); return Carbon::parse($this->payment_date)->format($dateFormat); } public function scopeWhereSearch($query, $search) { foreach (explode(' ', $search) as $term) { $query->whereHas('user', function ($query) use ($term) { $query->where('name', 'LIKE', '%'.$term.'%') ->orWhere('contact_name', 'LIKE', '%'.$term.'%') ->orWhere('company_name', 'LIKE', '%'.$term.'%'); }); } } public function scopePaymentNumber($query, $paymentNumber) { return $query->where('payments.payment_number', 'LIKE', '%'.$paymentNumber.'%'); } public function scopePaymentMethod($query, $paymentMethodId) { return $query->where('payments.payment_method_id', $paymentMethodId); } public function scopeApplyFilters($query, array $filters) { $filters = collect($filters); if ($filters->get('search')) { $query->whereSearch($filters->get('search')); } if ($filters->get('payment_number')) { $query->paymentNumber($filters->get('payment_number')); } if ($filters->get('payment_method_id')) { $query->paymentMethod($filters->get('payment_method_id')); } if ($filters->get('customer_id')) { $query->whereCustomer($filters->get('customer_id')); } if ($filters->get('orderByField') || $filters->get('orderBy')) { $field = $filters->get('orderByField') ? $filters->get('orderByField') : 'payment_number'; $orderBy = $filters->get('orderBy') ? $filters->get('orderBy') : 'asc'; $query->whereOrder($field, $orderBy); } } public function scopeWhereOrder($query, $orderByField, $orderBy) { $query->orderBy($orderByField, $orderBy); } public function scopeWhereCompany($query, $company_id) { $query->where('payments.company_id', $company_id); } public function scopeWhereCustomer($query, $customer_id) { $query->where('payments.user_id', $customer_id); } }