1
0
mirror of https://github.com/invoiceninja/invoiceninja.git synced 2024-11-16 08:02:40 +01:00
invoiceninja/app/ninja/repositories/PaymentRepository.php

155 lines
6.0 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)
{
2014-01-06 19:03:00 +01:00
$query = \DB::table('payments')
2015-01-11 13:30:08 +01:00
->join('clients', 'clients.id', '=', 'payments.client_id')
->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('clients.deleted_at', '=', null)
2015-01-11 13:30:08 +01:00
->where('contacts.is_primary', '=', true)
->where('contacts.deleted_at', '=', null)
2015-02-25 23:11:00 +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', 'payments.account_gateway_id', 'payments.deleted_at', 'payments.is_deleted', 'invoices.is_deleted as invoice_is_deleted');
2014-01-06 19:03:00 +01:00
2015-01-11 13:30:08 +01:00
if (!\Session::get('show_trash:payment')) {
2015-02-25 23:11:00 +01:00
$query->where('payments.deleted_at', '=', null)
->where('invoices.deleted_at', '=', null);
2014-02-18 22:56:18 +01:00
}
2015-01-11 13:30:08 +01:00
if ($clientPublicId) {
2014-01-06 19:03:00 +01:00
$query->where('clients.public_id', '=', $clientPublicId);
}
2015-01-11 13:30:08 +01:00
if ($filter) {
$query->where(function ($query) use ($filter) {
2014-01-06 19:03:00 +01:00
$query->where('clients.name', 'like', '%'.$filter.'%');
});
}
return $query;
}
public function findForContact($contactId = null, $filter = null)
{
$query = \DB::table('payments')
2015-01-11 13:30:08 +01:00
->join('clients', 'clients.id', '=', 'payments.client_id')
->join('invoices', 'invoices.id', '=', 'payments.invoice_id')
->join('contacts', 'contacts.client_id', '=', 'clients.id')
2015-01-11 13:30:08 +01:00
->leftJoin('invitations', function ($join) {
$join->on('invitations.invoice_id', '=', 'invoices.id')
2015-01-11 13:30:08 +01:00
->on('invitations.contact_id', '=', 'contacts.id');
})
->leftJoin('payment_types', 'payment_types.id', '=', 'payments.payment_type_id')
->where('clients.is_deleted', '=', false)
->where('payments.is_deleted', '=', false)
->where('invitations.deleted_at', '=', null)
2015-02-25 23:11:00 +01:00
->where('invoices.deleted_at', '=', null)
2014-12-03 23:05:38 +01:00
->where('invitations.contact_id', '=', $contactId)
2015-01-11 13:30:08 +01:00
->select('invitations.invitation_key', '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', 'payments.account_gateway_id');
2015-01-11 13:30:08 +01:00
if ($filter) {
$query->where(function ($query) use ($filter) {
$query->where('clients.name', 'like', '%'.$filter.'%');
});
}
return $query;
}
2014-01-06 19:03:00 +01:00
2014-01-16 22:12:46 +01:00
public function getErrors($input)
{
$rules = array(
'client' => 'required',
2015-01-11 13:30:08 +01:00
'invoice' => 'required',
'amount' => 'required',
2014-01-16 22:12:46 +01:00
);
2015-01-11 13:30:08 +01:00
if ($input['payment_type_id'] == PAYMENT_TYPE_CREDIT) {
$rules['payment_type_id'] = 'has_credit:'.$input['client'].','.$input['amount'];
2014-01-16 22:12:46 +01:00
}
$validator = \Validator::make($input, $rules);
2015-01-11 13:30:08 +01:00
if ($validator->fails()) {
2014-01-16 22:12:46 +01:00
return $validator;
}
return false;
}
2015-01-11 13:30:08 +01:00
public function save($publicId = null, $input)
{
if ($publicId) {
2014-01-06 19:03:00 +01:00
$payment = Payment::scope($publicId)->firstOrFail();
2015-01-11 13:30:08 +01:00
} else {
2014-01-06 19:03:00 +01:00
$payment = Payment::createNew();
}
2014-01-16 22:12:46 +01:00
$paymentTypeId = $input['payment_type_id'] ? $input['payment_type_id'] : null;
2015-02-10 12:29:13 +01:00
$payment->payment_type_id = $paymentTypeId;
$payment->payment_date = Utils::toSqlDate($input['payment_date']);
$payment->transaction_reference = trim($input['transaction_reference']);
if (!$publicId) {
$clientId = Client::getPrivateId($input['client']);
$amount = Utils::parseFloat($input['amount']);
2014-01-16 22:12:46 +01:00
2015-02-10 12:29:13 +01:00
if ($paymentTypeId == PAYMENT_TYPE_CREDIT) {
$credits = Credit::scope()->where('client_id', '=', $clientId)
->where('balance', '>', 0)->orderBy('created_at')->get();
$applied = 0;
2014-01-16 22:12:46 +01:00
2015-02-10 12:29:13 +01:00
foreach ($credits as $credit) {
$applied += $credit->apply($amount);
2014-01-16 22:12:46 +01:00
2015-02-10 12:29:13 +01:00
if ($applied >= $amount) {
break;
}
2014-01-16 22:12:46 +01:00
}
}
2015-02-10 12:29:13 +01:00
$payment->client_id = $clientId;
$payment->invoice_id = isset($input['invoice']) && $input['invoice'] != "-1" ? Invoice::getPrivateId($input['invoice']) : null;
$payment->amount = $amount;
2014-01-16 22:12:46 +01:00
}
2014-01-06 19:03:00 +01:00
$payment->save();
2015-01-11 13:30:08 +01:00
return $payment;
}
public function bulk($ids, $action)
{
if (!$ids) {
2014-01-12 19:55:33 +01:00
return 0;
}
2014-09-13 23:13:09 +02:00
$payments = Payment::withTrashed()->scope($ids)->get();
2014-01-06 19:03:00 +01:00
2015-01-11 13:30:08 +01:00
foreach ($payments as $payment) {
if ($action == 'restore') {
2014-11-23 22:47:10 +01:00
$payment->restore();
2015-01-11 13:30:08 +01:00
} else {
if ($action == 'delete') {
2014-11-23 22:47:10 +01:00
$payment->is_deleted = true;
$payment->save();
2015-01-11 13:30:08 +01:00
}
2014-01-06 19:03:00 +01:00
2014-11-23 22:47:10 +01:00
$payment->delete();
}
2014-01-06 19:03:00 +01:00
}
2015-01-11 13:30:08 +01:00
return count($payments);
}
}