fixed #613
This commit is contained in:
parent
3b3379833d
commit
17be9fa205
@ -3,12 +3,9 @@
|
||||
namespace App\Listeners\Incomes\Invoice;
|
||||
|
||||
use App\Events\InvoicePaid;
|
||||
|
||||
use App\Models\Income\Invoice;
|
||||
use App\Models\Income\InvoicePayment;
|
||||
use App\Models\Income\InvoiceHistory;
|
||||
use App\Http\Requests\Income\InvoicePayment as PaymentRequest;
|
||||
use App\Jobs\Income\CreateInvoicePayment;
|
||||
use App\Notifications\Customer\Invoice as Notification;
|
||||
|
||||
use App\Traits\DateTime;
|
||||
use Date;
|
||||
|
||||
@ -20,40 +17,14 @@ class Paid
|
||||
* Handle the event.
|
||||
*
|
||||
* @param $event
|
||||
* @return void
|
||||
* @return array
|
||||
*/
|
||||
public function handle(InvoicePaid $event)
|
||||
{
|
||||
$invoice = $event->invoice;
|
||||
$request = $event->request;
|
||||
|
||||
$request['invoice_id'] = $invoice->id;
|
||||
|
||||
if (!isset($request['company_id'])) {
|
||||
$request['company_id'] = session('company_id');
|
||||
}
|
||||
|
||||
if (!isset($request['account_id'])) {
|
||||
$request['account_id'] = setting('general.default_account');
|
||||
}
|
||||
|
||||
if (!isset($request['amount'])) {
|
||||
$request['amount'] = $invoice->amount;
|
||||
}
|
||||
|
||||
if (!isset($request['currency_code'])) {
|
||||
$request['currency_code'] = $invoice->currency_code;
|
||||
}
|
||||
|
||||
if (!isset($request['currency_rate'])) {
|
||||
$request['currency_rate'] = $invoice->currency_rate;
|
||||
}
|
||||
|
||||
if (!isset($request['notify'])) {
|
||||
$request['notify'] = 0;
|
||||
}
|
||||
|
||||
$request['paid_at'] = Date::parse('now')->format('Y-m-d');
|
||||
$invoice_payment = $this->createPayment($invoice, $request);
|
||||
|
||||
if ($request['amount'] > $invoice->amount) {
|
||||
$message = trans('messages.error.added', ['type' => trans_choice('general.payment', 1)]);
|
||||
@ -70,26 +41,6 @@ class Paid
|
||||
|
||||
$invoice->save();
|
||||
|
||||
if (!is_array($request)) {
|
||||
$invoice_payment = InvoicePayment::create($request->input());
|
||||
} else {
|
||||
$invoice_payment = InvoicePayment::create($request);
|
||||
}
|
||||
|
||||
$request['status_code'] = $invoice->invoice_status_code;
|
||||
|
||||
$desc_date = Date::parse($request['paid_at'])->format($this->getCompanyDateFormat());
|
||||
|
||||
$desc_amount = money((float) $request['amount'], $request['currency_code'], true)->format();
|
||||
|
||||
$request['description'] = $desc_date . ' ' . $desc_amount;
|
||||
|
||||
if (!is_array($request)) {
|
||||
$invoice_history = InvoiceHistory::create($request->input());
|
||||
} else {
|
||||
$invoice_history = InvoiceHistory::create($request);
|
||||
}
|
||||
|
||||
// Customer add payment on invoice send user notification
|
||||
foreach ($invoice->company->users as $user) {
|
||||
if (!$user->can('read-notifications')) {
|
||||
@ -104,4 +55,27 @@ class Paid
|
||||
'error' => false,
|
||||
];
|
||||
}
|
||||
|
||||
protected function createPayment($invoice, $request)
|
||||
{
|
||||
if (!is_array($request)) {
|
||||
$request = $request->input();
|
||||
}
|
||||
|
||||
$request['invoice_id'] = $invoice->id;
|
||||
$request['paid_at'] = Date::parse('now')->format('Y-m-d');
|
||||
$request['company_id'] = isset($request['company_id']) ? $request['company_id'] : session('company_id');
|
||||
$request['account_id'] = isset($request['account_id']) ? $request['account_id'] : setting('general.default_account');
|
||||
$request['payment_method'] = isset($request['payment_method']) ? $request['payment_method'] : setting('general.default_payment_method');
|
||||
$request['currency_code'] = isset($request['currency_code']) ? $request['currency_code'] : $invoice->currency_code;
|
||||
$request['currency_rate'] = isset($request['currency_rate']) ? $request['currency_rate'] : $invoice->currency_rate;
|
||||
$request['notify'] = isset($request['notify']) ? $request['notify'] : 0;
|
||||
|
||||
$payment_request = new PaymentRequest();
|
||||
$payment_request->merge($request);
|
||||
|
||||
$invoice_payment = dispatch(new CreateInvoicePayment($payment_request, $invoice));
|
||||
|
||||
return $invoice_payment;
|
||||
}
|
||||
}
|
||||
|
@ -18,11 +18,6 @@ class Company implements Scope
|
||||
*/
|
||||
public function apply(Builder $builder, Model $model)
|
||||
{
|
||||
$company_id = session('company_id');
|
||||
if (empty($company_id)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$table = $model->getTable();
|
||||
|
||||
// Skip for specific tables
|
||||
@ -37,7 +32,7 @@ class Company implements Scope
|
||||
}
|
||||
|
||||
// Apply company scope
|
||||
$builder->where($table . '.company_id', '=', $company_id);
|
||||
$builder->where($table . '.company_id', '=', session('company_id'));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -48,8 +48,8 @@ class OfflinePayment extends Controller
|
||||
}
|
||||
/**
|
||||
* Show the form for editing the specified resource.
|
||||
* @param Invoice
|
||||
* @param PaymentRequest
|
||||
* @param $invoice
|
||||
* @param $request
|
||||
* @return Response
|
||||
*/
|
||||
public function link(Invoice $invoice, PaymentRequest $request)
|
||||
@ -85,14 +85,10 @@ class OfflinePayment extends Controller
|
||||
|
||||
flash($message)->success();
|
||||
|
||||
$request_invoice_paid = [
|
||||
$event_response = event(new InvoicePaid($invoice, [
|
||||
'amount' => $invoice->amount,
|
||||
'currency_code' => $invoice->currency_code,
|
||||
'currency_rate' => $invoice->currency_rate,
|
||||
'payment_method' => $request['payment_method'],
|
||||
];
|
||||
|
||||
event(new InvoicePaid($invoice, $request_invoice_paid));
|
||||
]));
|
||||
|
||||
return response()->json([
|
||||
'error' => false,
|
||||
|
Loading…
x
Reference in New Issue
Block a user