1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-18 09:04:35 +01:00
invoiceninja/app/ninja/repositories/PaymentRepository.php

125 lines
3.7 KiB
PHP
Raw Normal View History

2014-01-06 19:03:00 +01:00
<?php namespace ninja\repositories;
use Payment;
2014-01-16 22:12:46 +01:00
use Credit;
2014-01-06 19:03:00 +01:00
use Invoice;
use Client;
use Utils;
class PaymentRepository
{
public function find($clientPublicId = null, $filter = null)
{
$query = \DB::table('payments')
->join('clients', 'clients.id', '=','payments.client_id')
2014-01-13 20:22:43 +01:00
->join('invoices', 'invoices.id', '=','payments.invoice_id')
2014-01-06 19:03:00 +01:00
->join('contacts', 'contacts.client_id', '=', 'clients.id')
2014-01-15 15:01:24 +01:00
->leftJoin('payment_types', 'payment_types.id', '=', 'payments.payment_type_id')
2014-01-06 19:03:00 +01:00
->where('payments.account_id', '=', \Auth::user()->account_id)
->where('payments.deleted_at', '=', null)
->where('clients.deleted_at', '=', null)
->where('contacts.is_primary', '=', true)
2014-01-15 15:01:24 +01:00
->select('payments.public_id', 'payments.transaction_reference', 'clients.name as client_name', 'clients.public_id as client_public_id', 'payments.amount', 'payments.payment_date', 'invoices.public_id as invoice_public_id', 'invoices.invoice_number', 'clients.currency_id', 'contacts.first_name', 'contacts.last_name', 'contacts.email', 'payment_types.name as payment_type');
2014-01-06 19:03:00 +01:00
if ($clientPublicId)
{
$query->where('clients.public_id', '=', $clientPublicId);
}
if ($filter)
{
$query->where(function($query) use ($filter)
{
$query->where('clients.name', 'like', '%'.$filter.'%');
});
}
return $query;
}
2014-01-16 22:12:46 +01:00
public function getErrors($input)
{
$rules = array(
'client' => 'required',
'invoice' => 'required',
'amount' => 'required|positive'
);
if ($input['payment_type_id'] == PAYMENT_TYPE_CREDIT)
{
$rules['payment_type_id'] = 'has_credit:' . $input['client'] . ',' . $input['amount'];
}
$validator = \Validator::make($input, $rules);
if ($validator->fails())
{
return $validator;
}
return false;
}
2014-01-06 19:03:00 +01:00
public function save($publicId = null, $input)
{
if ($publicId)
{
$payment = Payment::scope($publicId)->firstOrFail();
}
else
{
$payment = Payment::createNew();
}
2014-01-16 22:12:46 +01:00
$paymentTypeId = $input['payment_type_id'] ? $input['payment_type_id'] : null;
$amount = Utils::parseFloat($input['amount']);
if ($paymentTypeId == PAYMENT_TYPE_CREDIT)
{
$credits = Credit::scope()->where('balance', '>', 0)->orderBy('created_at')->get();
$applied = 0;
foreach ($credits as $credit)
{
$applied += $credit->apply($amount);
if ($applied >= $amount)
{
break;
}
}
}
2014-01-06 19:03:00 +01:00
$payment->client_id = Client::getPrivateId($input['client']);
$payment->invoice_id = isset($input['invoice']) && $input['invoice'] != "-1" ? Invoice::getPrivateId($input['invoice']) : null;
2014-01-16 22:12:46 +01:00
$payment->payment_type_id = $paymentTypeId;
2014-01-06 19:03:00 +01:00
$payment->payment_date = Utils::toSqlDate($input['payment_date']);
2014-01-16 22:12:46 +01:00
$payment->amount = $amount;
2014-01-06 19:03:00 +01:00
$payment->save();
return $payment;
}
public function bulk($ids, $action)
{
2014-01-12 19:55:33 +01:00
if (!$ids)
{
return 0;
}
2014-01-06 19:03:00 +01:00
$payments = Payment::scope($ids)->get();
foreach ($payments as $payment)
{
if ($action == 'delete')
{
$payment->is_deleted = true;
$payment->save();
}
$payment->delete();
}
return count($payments);
}
}