mirror of
https://github.com/crater-invoice/crater.git
synced 2025-10-27 11:41:09 -04:00
v5.0.0 update
This commit is contained in:
@ -6,11 +6,11 @@ use Barryvdh\DomPDF\Facade as PDF;
|
||||
use Carbon\Carbon;
|
||||
use Crater\Jobs\GeneratePaymentPdfJob;
|
||||
use Crater\Mail\SendPaymentMail;
|
||||
use Crater\Services\SerialNumberFormatter;
|
||||
use Crater\Traits\GeneratesPdfTrait;
|
||||
use Crater\Traits\HasCustomFieldsTrait;
|
||||
use Illuminate\Database\Eloquent\Factories\HasFactory;
|
||||
use Illuminate\Database\Eloquent\Model;
|
||||
use Illuminate\Support\Facades\Auth;
|
||||
use Spatie\MediaLibrary\HasMedia;
|
||||
use Spatie\MediaLibrary\InteractsWithMedia;
|
||||
use Vinkla\Hashids\Facades\Hashids;
|
||||
@ -38,6 +38,11 @@ class Payment extends Model implements HasMedia
|
||||
'paymentPdfUrl',
|
||||
];
|
||||
|
||||
protected $casts = [
|
||||
'notes' => 'string',
|
||||
'exchange_rate' => 'float'
|
||||
];
|
||||
|
||||
protected static function booted()
|
||||
{
|
||||
static::created(function ($payment) {
|
||||
@ -56,13 +61,6 @@ class Payment extends Model implements HasMedia
|
||||
}
|
||||
}
|
||||
|
||||
public function getPaymentPrefixAttribute()
|
||||
{
|
||||
$prefix = explode("-", $this->payment_number)[0];
|
||||
|
||||
return $prefix;
|
||||
}
|
||||
|
||||
public function getFormattedCreatedAtAttribute($value)
|
||||
{
|
||||
$dateFormat = CompanySetting::getSetting('carbon_date_format', $this->company_id);
|
||||
@ -82,18 +80,16 @@ class Payment extends Model implements HasMedia
|
||||
return url('/payments/pdf/'.$this->unique_hash);
|
||||
}
|
||||
|
||||
public function getPaymentNumAttribute()
|
||||
{
|
||||
$position = $this->strposX($this->payment_number, "-", 1) + 1;
|
||||
|
||||
return substr($this->payment_number, $position);
|
||||
}
|
||||
|
||||
public function emailLogs()
|
||||
{
|
||||
return $this->morphMany('App\Models\EmailLog', 'mailable');
|
||||
}
|
||||
|
||||
public function customer()
|
||||
{
|
||||
return $this->belongsTo(Customer::class, 'customer_id');
|
||||
}
|
||||
|
||||
public function company()
|
||||
{
|
||||
return $this->belongsTo(Company::class);
|
||||
@ -104,29 +100,36 @@ class Payment extends Model implements HasMedia
|
||||
return $this->belongsTo(Invoice::class);
|
||||
}
|
||||
|
||||
public function user()
|
||||
{
|
||||
return $this->belongsTo(User::class, 'user_id');
|
||||
}
|
||||
|
||||
public function creator()
|
||||
{
|
||||
return $this->belongsTo('Crater\Models\User', 'creator_id');
|
||||
}
|
||||
|
||||
public function currency()
|
||||
{
|
||||
return $this->belongsTo(Currency::class);
|
||||
}
|
||||
|
||||
public function paymentMethod()
|
||||
{
|
||||
return $this->belongsTo(PaymentMethod::class);
|
||||
}
|
||||
|
||||
public function send($data)
|
||||
public function sendPaymentData($data)
|
||||
{
|
||||
$data['payment'] = $this->toArray();
|
||||
$data['user'] = $this->user->toArray();
|
||||
$data['user'] = $this->customer->toArray();
|
||||
$data['company'] = Company::find($this->company_id);
|
||||
$data['body'] = $this->getEmailBody($data['body']);
|
||||
$data['attach']['data'] = ($this->getEmailAttachmentSetting()) ? $this->getPDFData() : null;
|
||||
|
||||
return $data;
|
||||
}
|
||||
|
||||
public function send($data)
|
||||
{
|
||||
$data = $this->sendPaymentData($data);
|
||||
|
||||
\Mail::to($data['to'])->send(new SendPaymentMail($data));
|
||||
|
||||
return [
|
||||
@ -136,36 +139,32 @@ class Payment extends Model implements HasMedia
|
||||
|
||||
public static function createPayment($request)
|
||||
{
|
||||
$data = $request->validated();
|
||||
$data = $request->getPaymentPayload();
|
||||
|
||||
$data['company_id'] = $request->header('company');
|
||||
$data['creator_id'] = Auth::id();
|
||||
|
||||
if ($request->has('invoice_id') && $request->invoice_id != null) {
|
||||
if ($request->invoice_id) {
|
||||
$invoice = Invoice::find($request->invoice_id);
|
||||
if ($invoice && $invoice->due_amount == $request->amount) {
|
||||
$invoice->status = Invoice::STATUS_COMPLETED;
|
||||
$invoice->paid_status = Invoice::STATUS_PAID;
|
||||
$invoice->due_amount = 0;
|
||||
} elseif ($invoice && $invoice->due_amount != $request->amount) {
|
||||
$invoice->due_amount = (int)$invoice->due_amount - (int)$request->amount;
|
||||
if ($invoice->due_amount < 0) {
|
||||
return [
|
||||
'error' => 'invalid_amount',
|
||||
];
|
||||
}
|
||||
$invoice->paid_status = Invoice::STATUS_PARTIALLY_PAID;
|
||||
}
|
||||
$invoice->save();
|
||||
$invoice->subtractInvoicePayment($request->amount);
|
||||
}
|
||||
|
||||
|
||||
$payment = Payment::create($data);
|
||||
|
||||
$payment->unique_hash = Hashids::connection(Payment::class)->encode($payment->id);
|
||||
|
||||
$serial = (new SerialNumberFormatter())
|
||||
->setModel($payment)
|
||||
->setCompany($payment->company_id)
|
||||
->setCustomer($payment->customer_id)
|
||||
->setNextNumbers();
|
||||
|
||||
$payment->sequence_number = $serial->nextSequenceNumber;
|
||||
$payment->customer_sequence_number = $serial->nextCustomerSequenceNumber;
|
||||
$payment->save();
|
||||
|
||||
$company_currency = CompanySetting::getSetting('currency', $request->header('company'));
|
||||
|
||||
if ((string)$payment['currency_id'] !== $company_currency) {
|
||||
ExchangeRateLog::addExchangeRateLog($payment);
|
||||
}
|
||||
|
||||
$customFields = $request->customFields;
|
||||
|
||||
if ($customFields) {
|
||||
@ -173,9 +172,10 @@ class Payment extends Model implements HasMedia
|
||||
}
|
||||
|
||||
$payment = Payment::with([
|
||||
'user',
|
||||
'customer',
|
||||
'invoice',
|
||||
'paymentMethod',
|
||||
'fields'
|
||||
])->find($payment->id);
|
||||
|
||||
return $payment;
|
||||
@ -183,34 +183,40 @@ class Payment extends Model implements HasMedia
|
||||
|
||||
public function updatePayment($request)
|
||||
{
|
||||
$oldAmount = $this->amount;
|
||||
|
||||
if ($request->has('invoice_id') && $request->invoice_id && ($oldAmount != $request->amount)) {
|
||||
$amount = (int)$request->amount - (int)$oldAmount;
|
||||
$invoice = Invoice::find($request->invoice_id);
|
||||
$invoice->due_amount = (int)$invoice->due_amount - (int)$amount;
|
||||
|
||||
if ($invoice->due_amount < 0) {
|
||||
return [
|
||||
'error' => 'invalid_amount',
|
||||
];
|
||||
}
|
||||
|
||||
if ($invoice->due_amount == 0) {
|
||||
$invoice->status = Invoice::STATUS_COMPLETED;
|
||||
$invoice->paid_status = Invoice::STATUS_PAID;
|
||||
} else {
|
||||
$invoice->status = $invoice->getPreviousStatus();
|
||||
$invoice->paid_status = Invoice::STATUS_PARTIALLY_PAID;
|
||||
}
|
||||
|
||||
$invoice->save();
|
||||
}
|
||||
|
||||
$data = $request->all();
|
||||
|
||||
if ($request->invoice_id && (! $this->invoice_id || $this->invoice_id !== $request->invoice_id)) {
|
||||
$invoice = Invoice::find($request->invoice_id);
|
||||
$invoice->subtractInvoicePayment($request->amount);
|
||||
}
|
||||
|
||||
if ($this->invoice_id && (! $request->invoice_id || $this->invoice_id !== $request->invoice_id)) {
|
||||
$invoice = Invoice::find($this->invoice_id);
|
||||
$invoice->addInvoicePayment($this->amount);
|
||||
}
|
||||
|
||||
if ($this->invoice_id === $request->invoice_id && $request->amount !== $this->amount) {
|
||||
$invoice = Invoice::find($this->invoice_id);
|
||||
$invoice->addInvoicePayment($this->amount);
|
||||
$invoice->subtractInvoicePayment($request->amount);
|
||||
}
|
||||
|
||||
$serial = (new SerialNumberFormatter())
|
||||
->setModel($this)
|
||||
->setCompany($this->company_id)
|
||||
->setCustomer($request->customer_id)
|
||||
->setModelObject($this->id)
|
||||
->setNextNumbers();
|
||||
|
||||
$data['customer_sequence_number'] = $serial->nextCustomerSequenceNumber;
|
||||
$this->update($data);
|
||||
|
||||
$company_currency = CompanySetting::getSetting('currency', $request->header('company'));
|
||||
|
||||
if ((string)$data['currency_id'] !== $company_currency) {
|
||||
ExchangeRateLog::addExchangeRateLog($this);
|
||||
}
|
||||
|
||||
$customFields = $request->customFields;
|
||||
|
||||
if ($customFields) {
|
||||
@ -218,7 +224,7 @@ class Payment extends Model implements HasMedia
|
||||
}
|
||||
|
||||
$payment = Payment::with([
|
||||
'user',
|
||||
'customer',
|
||||
'invoice',
|
||||
'paymentMethod',
|
||||
])
|
||||
@ -252,54 +258,10 @@ class Payment extends Model implements HasMedia
|
||||
return true;
|
||||
}
|
||||
|
||||
private function strposX($haystack, $needle, $number)
|
||||
{
|
||||
if ($number == '1') {
|
||||
return strpos($haystack, $needle);
|
||||
} elseif ($number > '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 static function getNextPaymentNumber($value)
|
||||
{
|
||||
// Get the last created order
|
||||
$payment = Payment::where('payment_number', 'LIKE', $value.'-%')
|
||||
->orderBy('payment_number', 'desc')
|
||||
->first();
|
||||
|
||||
// Get number length config
|
||||
$numberLength = CompanySetting::getSetting('payment_number_length', request()->header('company'));
|
||||
$numberLengthText = "%0{$numberLength}d";
|
||||
|
||||
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($numberLengthText, intval($number) + 1);
|
||||
}
|
||||
|
||||
public function scopeWhereSearch($query, $search)
|
||||
{
|
||||
foreach (explode(' ', $search) as $term) {
|
||||
$query->whereHas('user', function ($query) use ($term) {
|
||||
$query->whereHas('customer', function ($query) use ($term) {
|
||||
$query->where('name', 'LIKE', '%'.$term.'%')
|
||||
->orWhere('contact_name', 'LIKE', '%'.$term.'%')
|
||||
->orWhere('company_name', 'LIKE', '%'.$term.'%');
|
||||
@ -320,7 +282,7 @@ class Payment extends Model implements HasMedia
|
||||
public function scopePaginateData($query, $limit)
|
||||
{
|
||||
if ($limit == 'all') {
|
||||
return collect(['data' => $query->get()]);
|
||||
return $query->get();
|
||||
}
|
||||
|
||||
return $query->paginate($limit);
|
||||
@ -351,8 +313,8 @@ class Payment extends Model implements HasMedia
|
||||
}
|
||||
|
||||
if ($filters->get('orderByField') || $filters->get('orderBy')) {
|
||||
$field = $filters->get('orderByField') ? $filters->get('orderByField') : 'payment_number';
|
||||
$orderBy = $filters->get('orderBy') ? $filters->get('orderBy') : 'asc';
|
||||
$field = $filters->get('orderByField') ? $filters->get('orderByField') : 'sequence_number';
|
||||
$orderBy = $filters->get('orderBy') ? $filters->get('orderBy') : 'desc';
|
||||
$query->whereOrder($field, $orderBy);
|
||||
}
|
||||
}
|
||||
@ -367,14 +329,14 @@ class Payment extends Model implements HasMedia
|
||||
$query->orWhere('id', $payment_id);
|
||||
}
|
||||
|
||||
public function scopeWhereCompany($query, $company_id)
|
||||
public function scopeWhereCompany($query)
|
||||
{
|
||||
$query->where('payments.company_id', $company_id);
|
||||
$query->where('payments.company_id', request()->header('company'));
|
||||
}
|
||||
|
||||
public function scopeWhereCustomer($query, $customer_id)
|
||||
{
|
||||
$query->where('payments.user_id', $customer_id);
|
||||
$query->where('payments.customer_id', $customer_id);
|
||||
}
|
||||
|
||||
public function getPDFData()
|
||||
@ -410,7 +372,7 @@ class Payment extends Model implements HasMedia
|
||||
|
||||
public function getCustomerBillingAddress()
|
||||
{
|
||||
if ($this->user && (! $this->user->billingAddress()->exists())) {
|
||||
if ($this->customer && (! $this->customer->billingAddress()->exists())) {
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user