838 lines
25 KiB
PHP
Raw Normal View History

2017-09-14 22:21:00 +03:00
<?php
namespace App\Http\Controllers\Incomes;
use App\Events\InvoiceCreated;
use App\Events\InvoicePrinting;
use App\Events\InvoiceUpdated;
use App\Http\Controllers\Controller;
use App\Http\Requests\Income\Invoice as Request;
use App\Http\Requests\Income\InvoicePayment as PaymentRequest;
2018-11-05 01:25:43 +03:00
use App\Jobs\Income\CreateInvoice;
use App\Jobs\Income\CreateInvoicePayment;
2017-09-14 22:21:00 +03:00
use App\Models\Banking\Account;
2018-11-05 01:25:43 +03:00
use App\Models\Common\Item;
2018-08-31 19:55:27 +03:00
use App\Models\Common\Media;
2017-09-14 22:21:00 +03:00
use App\Models\Income\Customer;
use App\Models\Income\Invoice;
use App\Models\Income\InvoiceHistory;
use App\Models\Income\InvoiceItem;
use App\Models\Income\InvoiceItemTax;
use App\Models\Income\InvoiceTotal;
2017-09-14 22:21:00 +03:00
use App\Models\Income\InvoicePayment;
use App\Models\Income\InvoiceStatus;
use App\Models\Setting\Category;
use App\Models\Setting\Currency;
use App\Models\Setting\Tax;
2017-11-16 20:27:30 +03:00
use App\Notifications\Income\Invoice as Notification;
2018-06-10 02:48:51 +03:00
use App\Notifications\Common\Item as ItemNotification;
2018-09-29 17:37:55 +03:00
use App\Notifications\Common\ItemReminder as ItemReminderNotification;
2017-09-14 22:21:00 +03:00
use App\Traits\Currencies;
use App\Traits\DateTime;
2017-11-26 15:20:17 +03:00
use App\Traits\Incomes;
2017-09-14 22:21:00 +03:00
use App\Traits\Uploads;
2018-06-23 15:59:13 +03:00
use App\Utilities\Import;
2017-11-30 11:47:56 +03:00
use App\Utilities\ImportFile;
2017-09-14 22:21:00 +03:00
use App\Utilities\Modules;
use Date;
2017-11-16 20:27:30 +03:00
use File;
2018-08-31 19:55:27 +03:00
use Illuminate\Http\Request as ItemRequest;
2017-12-15 17:09:06 +03:00
use Image;
use Storage;
2018-10-15 16:10:49 +03:00
use SignedUrl;
2017-09-14 22:21:00 +03:00
class Invoices extends Controller
{
2017-11-26 15:20:17 +03:00
use DateTime, Currencies, Incomes, Uploads;
2017-09-14 22:21:00 +03:00
/**
* Display a listing of the resource.
*
* @return Response
*/
public function index()
{
2017-12-11 22:37:05 +03:00
$invoices = Invoice::with(['customer', 'status', 'items', 'payments', 'histories'])->collect(['invoice_number'=> 'desc']);
2017-09-14 22:21:00 +03:00
2018-11-02 11:32:42 +03:00
$customers = collect(Customer::enabled()->orderBy('name')->pluck('name', 'id'));
2017-09-27 21:40:25 +03:00
2018-11-02 11:32:42 +03:00
$categories = collect(Category::enabled()->type('income')->orderBy('name')->pluck('name', 'id'));
2017-09-14 22:21:00 +03:00
2018-11-02 11:32:42 +03:00
$statuses = collect(InvoiceStatus::all()->pluck('name', 'code'));
return view('incomes.invoices.index', compact('invoices', 'customers', 'categories', 'statuses'));
2017-09-14 22:21:00 +03:00
}
/**
* Show the form for viewing the specified resource.
*
* @param Invoice $invoice
*
* @return Response
*/
public function show(Invoice $invoice)
{
2018-06-29 14:42:15 +03:00
$accounts = Account::enabled()->orderBy('name')->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
2018-06-29 14:42:15 +03:00
$currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code')->toArray();
2017-09-14 22:21:00 +03:00
$account_currency_code = Account::where('id', setting('general.default_account'))->pluck('currency_code')->first();
2018-06-29 14:42:15 +03:00
$customers = Customer::enabled()->orderBy('name')->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
2018-06-29 14:42:15 +03:00
$categories = Category::enabled()->type('income')->orderBy('name')->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
$payment_methods = Modules::getPaymentMethods();
2018-10-15 16:10:49 +03:00
$customer_share = SignedUrl::sign(url('links/invoices/' . $invoice->id));
return view('incomes.invoices.show', compact('invoice', 'accounts', 'currencies', 'account_currency_code', 'customers', 'categories', 'payment_methods', 'customer_share'));
2017-09-14 22:21:00 +03:00
}
/**
* Show the form for creating a new resource.
*
* @return Response
*/
public function create()
{
2018-06-29 14:42:15 +03:00
$customers = Customer::enabled()->orderBy('name')->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
2018-06-29 14:42:15 +03:00
$currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code');
2017-09-14 22:21:00 +03:00
$currency = Currency::where('code', '=', setting('general.default_currency'))->first();
2018-06-29 14:42:15 +03:00
$items = Item::enabled()->orderBy('name')->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
2018-06-29 14:42:15 +03:00
$taxes = Tax::enabled()->orderBy('rate')->get()->pluck('title', 'id');
2017-09-14 22:21:00 +03:00
2018-06-29 14:42:15 +03:00
$categories = Category::enabled()->type('income')->orderBy('name')->pluck('name', 'id');
2018-04-23 22:17:20 +03:00
2017-11-26 15:20:17 +03:00
$number = $this->getNextInvoiceNumber();
2017-10-31 22:41:06 +03:00
return view('incomes.invoices.create', compact('customers', 'currencies', 'currency', 'items', 'taxes', 'categories', 'number'));
2017-09-14 22:21:00 +03:00
}
/**
* Store a newly created resource in storage.
*
* @param Request $request
*
* @return Response
*/
public function store(Request $request)
{
2018-11-05 01:25:43 +03:00
$invoice = dispatch(new CreateInvoice($request));
2017-09-14 22:21:00 +03:00
$message = trans('messages.success.added', ['type' => trans_choice('general.invoices', 1)]);
flash($message)->success();
return redirect('incomes/invoices/' . $invoice->id);
}
2017-11-26 15:20:17 +03:00
/**
* Duplicate the specified resource.
*
* @param Invoice $invoice
*
* @return Response
*/
public function duplicate(Invoice $invoice)
{
$clone = $invoice->duplicate();
// Add invoice history
InvoiceHistory::create([
'company_id' => session('company_id'),
'invoice_id' => $clone->id,
'status_code' => 'draft',
'notify' => 0,
'description' => trans('messages.success.added', ['type' => $clone->invoice_number]),
]);
2017-11-26 15:20:17 +03:00
// Update next invoice number
$this->increaseNextInvoiceNumber();
$message = trans('messages.success.duplicated', ['type' => trans_choice('general.invoices', 1)]);
flash($message)->success();
return redirect('incomes/invoices/' . $clone->id . '/edit');
}
2017-11-30 11:47:56 +03:00
/**
* Import the specified resource.
*
* @param ImportFile $import
*
* @return Response
*/
public function import(ImportFile $import)
{
2018-06-23 15:59:13 +03:00
$success = true;
2017-11-30 11:47:56 +03:00
2018-06-27 17:01:26 +03:00
$allowed_sheets = ['invoices', 'invoice_items', 'invoice_histories', 'invoice_payments', 'invoice_totals'];
2018-06-23 15:59:13 +03:00
// Loop through all sheets
2018-06-27 17:01:26 +03:00
$import->each(function ($sheet) use (&$success, $allowed_sheets) {
$sheet_title = $sheet->getTitle();
if (!in_array($sheet_title, $allowed_sheets)) {
$message = trans('messages.error.import_sheet');
flash($message)->error()->important();
return false;
}
$slug = 'Income\\' . str_singular(studly_case($sheet_title));
2018-06-11 03:47:32 +03:00
2018-06-23 16:45:03 +03:00
if (!$success = Import::createFromSheet($sheet, $slug)) {
2018-06-23 15:59:13 +03:00
return false;
}
2018-06-11 03:47:32 +03:00
});
2017-11-30 11:47:56 +03:00
2018-06-23 15:59:13 +03:00
if (!$success) {
return redirect('common/import/incomes/invoices');
}
2017-11-30 11:47:56 +03:00
$message = trans('messages.success.imported', ['type' => trans_choice('general.invoices', 2)]);
flash($message)->success();
return redirect('incomes/invoices');
}
2017-09-14 22:21:00 +03:00
/**
* Show the form for editing the specified resource.
*
* @param Invoice $invoice
*
* @return Response
*/
public function edit(Invoice $invoice)
{
2018-06-29 14:42:15 +03:00
$customers = Customer::enabled()->orderBy('name')->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
2018-06-29 14:42:15 +03:00
$currencies = Currency::enabled()->orderBy('name')->pluck('name', 'code');
2017-09-14 22:21:00 +03:00
$currency = Currency::where('code', '=', $invoice->currency_code)->first();
2018-08-17 17:17:56 +03:00
2018-06-29 14:42:15 +03:00
$items = Item::enabled()->orderBy('name')->pluck('name', 'id');
2017-09-14 22:21:00 +03:00
2018-06-29 14:42:15 +03:00
$taxes = Tax::enabled()->orderBy('rate')->get()->pluck('title', 'id');
2017-09-14 22:21:00 +03:00
2018-06-29 14:42:15 +03:00
$categories = Category::enabled()->type('income')->orderBy('name')->pluck('name', 'id');
2018-04-23 22:17:20 +03:00
2018-08-17 17:17:56 +03:00
return view('incomes.invoices.edit', compact('invoice', 'customers', 'currencies', 'currency', 'items', 'taxes', 'categories'));
2017-09-14 22:21:00 +03:00
}
/**
* Update the specified resource in storage.
*
* @param Invoice $invoice
* @param Request $request
*
* @return Response
*/
public function update(Invoice $invoice, Request $request)
{
$taxes = [];
2018-10-19 19:43:50 +03:00
$tax = 0;
$tax_total = 0;
$sub_total = 0;
2018-04-17 16:40:52 +03:00
$discount_total = 0;
$discount = $request['discount'];
$invoice_item = [];
2017-09-14 22:21:00 +03:00
$invoice_item['company_id'] = $request['company_id'];
$invoice_item['invoice_id'] = $invoice->id;
if ($request['item']) {
2018-08-31 19:55:27 +03:00
$this->deleteRelationships($invoice, 'items');
2017-09-14 22:21:00 +03:00
foreach ($request['item'] as $item) {
unset($tax_object);
2017-09-14 22:21:00 +03:00
$item_sku = '';
if (!empty($item['item_id'])) {
2017-10-07 19:37:58 +03:00
$item_object = Item::find($item['item_id']);
2017-09-14 22:21:00 +03:00
2018-04-10 17:36:48 +03:00
$item['name'] = $item_object->name;
2017-10-07 19:37:58 +03:00
$item_sku = $item_object->sku;
2017-09-14 22:21:00 +03:00
}
$item_tax = 0;
$item_taxes = [];
$invoice_item_taxes = [];
2017-10-07 19:37:58 +03:00
if (!empty($item['tax_id'])) {
foreach ($item['tax_id'] as $tax_id) {
$tax_object = Tax::find($tax_id);
2017-09-14 22:21:00 +03:00
$item_taxes[] = $tax_id;
2017-09-14 22:21:00 +03:00
$tax = (((double) $item['price'] * (double) $item['quantity']) / 100) * $tax_object->rate;
2018-04-17 16:40:52 +03:00
// Apply discount to tax
if ($discount) {
$tax = $tax - ($tax * ($discount / 100));
}
$invoice_item_taxes[] = [
'company_id' => $request['company_id'],
'invoice_id' => $invoice->id,
'tax_id' => $tax_id,
'name' => $tax_object->name,
'amount' => $tax,
];
$item_tax += $tax;
2018-04-17 16:40:52 +03:00
}
2017-09-14 22:21:00 +03:00
}
$invoice_item['item_id'] = $item['item_id'];
2017-12-24 09:25:48 +03:00
$invoice_item['name'] = str_limit($item['name'], 180, '');
2017-09-14 22:21:00 +03:00
$invoice_item['sku'] = $item_sku;
2018-04-30 11:49:47 +03:00
$invoice_item['quantity'] = (double) $item['quantity'];
2018-04-30 10:32:52 +03:00
$invoice_item['price'] = (double) $item['price'];
2017-10-07 19:37:58 +03:00
$invoice_item['tax'] = $tax;
$invoice_item['tax_id'] = 0;//$tax_id;
2018-04-30 11:49:47 +03:00
$invoice_item['total'] = (double) $item['price'] * (double) $item['quantity'];
$invoice_item_created = InvoiceItem::create($invoice_item);
if ($invoice_item_taxes) {
foreach ($invoice_item_taxes as $invoice_item_tax) {
$invoice_item_tax['invoice_item_id'] = $invoice_item_created->id;
InvoiceItemTax::create($invoice_item_tax);
// Set taxes
if (isset($taxes) && array_key_exists($invoice_item_tax['tax_id'], $taxes)) {
$taxes[$invoice_item_tax['tax_id']]['amount'] += $invoice_item_tax['amount'];
} else {
$taxes[$invoice_item_tax['tax_id']] = [
'name' => $invoice_item_tax['name'],
'amount' => $invoice_item_tax['amount']
];
}
}
}
2017-09-14 22:21:00 +03:00
$tax_total += $item_tax;
$sub_total += $invoice_item['total'];
2017-09-14 22:21:00 +03:00
}
}
2018-04-17 16:40:52 +03:00
$s_total = $sub_total;
// Apply discount to total
if ($discount) {
$s_discount = $s_total * ($discount / 100);
$discount_total += $s_discount;
$s_total = $s_total - $s_discount;
}
2018-08-07 16:39:54 +03:00
$amount = $s_total + $tax_total;
$request['amount'] = money($amount, $request['currency_code'])->getAmount();
2017-09-14 22:21:00 +03:00
$invoice->update($request->input());
2017-12-28 17:20:16 +03:00
// Upload attachment
if ($request->file('attachment')) {
$media = $this->getMedia($request->file('attachment'), 'invoices');
2018-01-02 16:22:30 +03:00
$invoice->attachMedia($media, 'attachment');
2017-12-28 17:20:16 +03:00
}
2017-10-12 18:06:40 +03:00
// Delete previous invoice totals
2018-08-31 19:55:27 +03:00
$this->deleteRelationships($invoice, 'totals');
2017-10-12 18:06:40 +03:00
// Add invoice totals
2018-04-17 16:40:52 +03:00
$this->addTotals($invoice, $request, $taxes, $sub_total, $discount_total, $tax_total);
2018-04-26 18:40:04 +03:00
// Recurring
2018-04-27 16:01:31 +03:00
$invoice->updateRecurring();
2018-04-26 18:40:04 +03:00
2017-09-14 22:21:00 +03:00
// Fire the event to make it extendible
event(new InvoiceUpdated($invoice));
$message = trans('messages.success.updated', ['type' => trans_choice('general.invoices', 1)]);
flash($message)->success();
return redirect('incomes/invoices/' . $invoice->id);
}
/**
* Remove the specified resource from storage.
*
* @param Invoice $invoice
*
* @return Response
*/
public function destroy(Invoice $invoice)
{
2018-08-31 19:55:27 +03:00
$this->deleteRelationships($invoice, ['items', 'histories', 'payments', 'recurring', 'totals']);
2017-09-14 22:21:00 +03:00
$invoice->delete();
$message = trans('messages.success.deleted', ['type' => trans_choice('general.invoices', 1)]);
flash($message)->success();
return redirect('incomes/invoices');
}
2018-06-11 03:47:32 +03:00
/**
* Export the specified resource.
*
* @return Response
*/
public function export()
{
\Excel::create('invoices', function ($excel) {
2018-06-21 16:24:40 +03:00
$invoices = Invoice::with(['items', 'histories', 'payments', 'totals'])->filter(request()->input())->get();
2018-06-11 03:47:32 +03:00
$excel->sheet('invoices', function ($sheet) use ($invoices) {
2018-06-11 03:47:32 +03:00
$sheet->fromModel($invoices->makeHidden([
2018-11-05 21:31:40 +03:00
'company_id', 'parent_id', 'created_at', 'updated_at', 'deleted_at', 'attachment', 'discount', 'items', 'histories', 'payments', 'totals', 'media', 'paid'
2018-06-11 03:47:32 +03:00
]));
});
$tables = ['items', 'histories', 'payments', 'totals'];
foreach ($tables as $table) {
$excel->sheet('invoice_' . $table, function ($sheet) use ($invoices, $table) {
2018-06-25 10:46:37 +03:00
$hidden_fields = ['id', 'company_id', 'created_at', 'updated_at', 'deleted_at', 'title'];
2018-06-21 16:22:37 +03:00
$i = 1;
2018-08-07 16:39:54 +03:00
2018-06-21 16:22:37 +03:00
foreach ($invoices as $invoice) {
$model = $invoice->$table->makeHidden($hidden_fields);
if ($i == 1) {
$sheet->fromModel($model, null, 'A1', false);
} else {
// Don't put multiple heading columns
$sheet->fromModel($model, null, 'A1', false, false);
}
$i++;
}
2018-06-11 03:47:32 +03:00
});
}
})->download('xlsx');
}
2017-11-07 05:11:03 +03:00
/**
* Mark the invoice as sent.
*
2017-11-08 17:22:04 +03:00
* @param Invoice $invoice
2017-11-07 05:11:03 +03:00
*
* @return Response
*/
2017-11-08 17:22:04 +03:00
public function markSent(Invoice $invoice)
2017-11-07 05:11:03 +03:00
{
2018-03-13 15:24:39 +03:00
$invoice->invoice_status_code = 'sent';
2018-03-13 15:24:39 +03:00
$invoice->save();
// Add invoice history
InvoiceHistory::create([
'company_id' => $invoice->company_id,
'invoice_id' => $invoice->id,
'status_code' => 'sent',
'notify' => 0,
'description' => trans('invoices.mark_sent'),
]);
2017-11-07 05:11:03 +03:00
flash(trans('invoices.messages.marked_sent'))->success();
return redirect()->back();
}
/**
2017-11-16 20:27:30 +03:00
* Download the PDF file of invoice.
2017-11-07 05:11:03 +03:00
*
2017-11-08 17:22:04 +03:00
* @param Invoice $invoice
2017-11-07 05:11:03 +03:00
*
* @return Response
*/
2017-11-16 20:27:30 +03:00
public function emailInvoice(Invoice $invoice)
2017-11-07 05:11:03 +03:00
{
if (empty($invoice->customer_email)) {
return redirect()->back();
}
2017-11-16 20:27:30 +03:00
$invoice = $this->prepareInvoice($invoice);
2017-11-08 17:22:04 +03:00
2018-04-26 16:04:27 +03:00
$html = view($invoice->template_path, compact('invoice'))->render();
2017-11-07 05:11:03 +03:00
2017-11-16 20:27:30 +03:00
$pdf = \App::make('dompdf.wrapper');
$pdf->loadHTML($html);
2017-11-07 05:11:03 +03:00
2017-11-16 20:27:30 +03:00
$file = storage_path('app/temp/invoice_'.time().'.pdf');
2017-11-07 05:11:03 +03:00
2017-11-16 20:27:30 +03:00
$invoice->pdf_path = $file;
2017-11-07 05:11:03 +03:00
2017-11-16 20:27:30 +03:00
// Save the PDF file into temp folder
$pdf->save($file);
// Notify the customer
$invoice->customer->notify(new Notification($invoice));
// Delete temp file
File::delete($file);
unset($invoice->paid);
unset($invoice->template_path);
unset($invoice->pdf_path);
unset($invoice->reconciled);
2017-11-16 20:27:30 +03:00
// Mark invoice as sent
if ($invoice->invoice_status_code != 'partial') {
$invoice->invoice_status_code = 'sent';
$invoice->save();
}
// Add invoice history
InvoiceHistory::create([
'company_id' => $invoice->company_id,
'invoice_id' => $invoice->id,
'status_code' => 'sent',
'notify' => 1,
'description' => trans('invoices.send_mail'),
]);
2017-11-16 20:27:30 +03:00
flash(trans('invoices.messages.email_sent'))->success();
return redirect()->back();
}
/**
* Print the invoice.
*
* @param Invoice $invoice
*
* @return Response
*/
public function printInvoice(Invoice $invoice)
{
$invoice = $this->prepareInvoice($invoice);
2017-11-07 05:11:03 +03:00
2018-04-26 16:04:27 +03:00
return view($invoice->template_path, compact('invoice'));
2017-11-07 05:11:03 +03:00
}
/**
2017-11-15 21:53:40 +03:00
* Download the PDF file of invoice.
2017-11-07 05:11:03 +03:00
*
2017-11-08 17:22:04 +03:00
* @param Invoice $invoice
2017-11-07 05:11:03 +03:00
*
* @return Response
*/
2017-11-15 21:53:40 +03:00
public function pdfInvoice(Invoice $invoice)
2017-11-07 05:11:03 +03:00
{
2017-11-16 20:27:30 +03:00
$invoice = $this->prepareInvoice($invoice);
2017-11-07 05:11:03 +03:00
2018-04-26 16:04:27 +03:00
$html = view($invoice->template_path, compact('invoice'))->render();
2017-11-15 21:53:40 +03:00
2018-06-10 15:38:07 +03:00
$pdf = app('dompdf.wrapper');
2017-11-15 21:53:40 +03:00
$pdf->loadHTML($html);
2017-11-16 20:27:30 +03:00
//$pdf->setPaper('A4', 'portrait');
2017-11-15 21:53:40 +03:00
$file_name = 'invoice_'.time().'.pdf';
return $pdf->download($file_name);
2017-11-07 05:11:03 +03:00
}
/**
2017-11-15 21:53:40 +03:00
* Mark the invoice as paid.
2017-11-07 05:11:03 +03:00
*
2017-11-08 17:22:04 +03:00
* @param Invoice $invoice
2017-11-07 05:11:03 +03:00
*
* @return Response
*/
2017-11-15 21:53:40 +03:00
public function markPaid(Invoice $invoice)
2017-11-07 05:11:03 +03:00
{
$paid = 0;
foreach ($invoice->payments as $item) {
$amount = $item->amount;
if ($invoice->currency_code != $item->currency_code) {
$item->default_currency_code = $invoice->currency_code;
2017-11-07 05:11:03 +03:00
$amount = $item->getDynamicConvertedAmount();
}
$paid += $amount;
2017-11-07 05:11:03 +03:00
}
2017-11-15 21:53:40 +03:00
$amount = $invoice->amount - $paid;
2017-11-07 05:11:03 +03:00
if (!empty($amount)) {
$request = new PaymentRequest();
$request['company_id'] = $invoice->company_id;
$request['invoice_id'] = $invoice->id;
$request['account_id'] = setting('general.default_account');
$request['payment_method'] = setting('general.default_payment_method', 'offlinepayment.cash.1');
$request['currency_code'] = $invoice->currency_code;
$request['amount'] = $amount;
$request['paid_at'] = Date::now()->format('Y-m-d');
$request['_token'] = csrf_token();
$this->payment($request);
} else {
$invoice->invoice_status_code = 'paid';
$invoice->save();
}
2017-11-07 05:11:03 +03:00
2017-11-15 21:53:40 +03:00
return redirect()->back();
2017-11-07 05:11:03 +03:00
}
/**
* Add payment to the invoice.
*
* @param PaymentRequest $request
*
* @return Response
*/
public function payment(PaymentRequest $request)
{
// Get currency object
$currency = Currency::where('code', $request['currency_code'])->first();
$request['currency_code'] = $currency->code;
$request['currency_rate'] = $currency->rate;
$invoice = Invoice::find($request['invoice_id']);
$total_amount = $invoice->amount;
$amount = (double) $request['amount'];
if ($request['currency_code'] != $invoice->currency_code) {
2017-11-07 05:11:03 +03:00
$request_invoice = new Invoice();
$request_invoice->amount = (float) $request['amount'];
$request_invoice->currency_code = $currency->code;
$request_invoice->currency_rate = $currency->rate;
$amount = $request_invoice->getConvertedAmount();
}
2017-11-07 05:11:03 +03:00
if ($invoice->payments()->count()) {
$total_amount -= $invoice->payments()->paid();
}
2017-11-07 05:11:03 +03:00
2018-07-11 15:16:24 +03:00
// For amount cover integer
$multiplier = 1;
for ($i = 0; $i < $currency->precision; $i++) {
$multiplier *= 10;
}
2018-08-07 16:39:54 +03:00
2018-07-11 15:16:24 +03:00
$amount *= $multiplier;
$total_amount *= $multiplier;
if ($amount > $total_amount) {
2018-03-21 16:21:51 +03:00
$message = trans('messages.error.over_payment');
return response()->json([
'success' => false,
'error' => true,
'message' => $message,
]);
} elseif ($amount == $total_amount) {
$invoice->invoice_status_code = 'paid';
} else {
$invoice->invoice_status_code = 'partial';
2017-11-07 05:11:03 +03:00
}
$invoice->save();
2018-11-05 01:25:43 +03:00
$invoice_payment = dispatch(new CreateInvoicePayment($request, $invoice));
2017-11-07 05:11:03 +03:00
2017-12-28 17:20:16 +03:00
// Upload attachment
if ($request->file('attachment')) {
$media = $this->getMedia($request->file('attachment'), 'invoices');
$invoice_payment->attachMedia($media, 'attachment');
}
2018-03-31 11:13:49 +03:00
$message = trans('messages.success.added', ['type' => trans_choice('general.payments', 1)]);
2017-11-07 05:11:03 +03:00
return response()->json([
'success' => true,
'error' => false,
'message' => $message,
]);
2017-11-07 05:11:03 +03:00
}
2017-09-14 22:21:00 +03:00
/**
* Remove the specified resource from storage.
*
* @param InvoicePayment $payment
*
* @return Response
*/
public function paymentDestroy(InvoicePayment $payment)
{
$invoice = Invoice::find($payment->invoice_id);
2018-03-13 15:44:59 +03:00
if ($invoice->payments()->count() > 1) {
2018-03-23 18:02:37 +03:00
$invoice->invoice_status_code = 'partial';
} else {
$invoice->invoice_status_code = 'sent';
}
$invoice->save();
2018-03-13 15:44:59 +03:00
$desc_amount = money((float) $payment->amount, (string) $payment->currency_code, true)->format();
2018-03-13 18:53:18 +03:00
$description = $desc_amount . ' ' . trans_choice('general.payments', 1);
2018-03-13 15:44:59 +03:00
// Add invoice history
InvoiceHistory::create([
'company_id' => $invoice->company_id,
'invoice_id' => $invoice->id,
2018-03-23 18:02:37 +03:00
'status_code' => $invoice->invoice_status_code,
'notify' => 0,
2018-03-13 18:53:18 +03:00
'description' => trans('messages.success.deleted', ['type' => $description]),
]);
2018-03-13 15:20:09 +03:00
$payment->delete();
2017-09-14 22:21:00 +03:00
$message = trans('messages.success.deleted', ['type' => trans_choice('general.invoices', 1)]);
flash($message)->success();
return redirect()->back();
2017-09-14 22:21:00 +03:00
}
2017-10-12 18:06:40 +03:00
public function addItem(ItemRequest $request)
{
if ($request['item_row']) {
$item_row = $request['item_row'];
$taxes = Tax::enabled()->orderBy('rate')->get()->pluck('title', 'id');
$currency = Currency::where('code', '=', $request['currency_code'])->first();
// it should be integer for amount mask
$currency->precision = (int) $currency->precision;
$html = view('incomes.invoices.item', compact('item_row', 'taxes', 'currency'))->render();
return response()->json([
'success' => true,
'error' => false,
'data' => [
'currency' => $currency
],
'message' => 'null',
'html' => $html,
]);
}
return response()->json([
'success' => false,
'error' => true,
'data' => 'null',
'message' => trans('issue'),
'html' => 'null',
]);
}
2017-11-16 20:27:30 +03:00
protected function prepareInvoice(Invoice $invoice)
{
$paid = 0;
foreach ($invoice->payments as $item) {
2018-07-10 17:21:40 +03:00
$amount = $item->amount;
2017-11-16 20:27:30 +03:00
2018-07-10 17:21:40 +03:00
if ($invoice->currency_code != $item->currency_code) {
$item->default_currency_code = $invoice->currency_code;
$amount = $item->getDynamicConvertedAmount();
}
$paid += $amount;
2017-11-16 20:27:30 +03:00
}
$invoice->paid = $paid;
$invoice->template_path = 'incomes.invoices.invoice';
event(new InvoicePrinting($invoice));
return $invoice;
}
2018-04-17 16:40:52 +03:00
protected function addTotals($invoice, $request, $taxes, $sub_total, $discount_total, $tax_total)
2017-10-12 18:06:40 +03:00
{
$sort_order = 1;
2018-04-17 16:40:52 +03:00
// Added invoice sub total
InvoiceTotal::create([
2017-10-12 18:06:40 +03:00
'company_id' => $request['company_id'],
'invoice_id' => $invoice->id,
'code' => 'sub_total',
'name' => 'invoices.sub_total',
'amount' => $sub_total,
'sort_order' => $sort_order,
]);
2017-10-12 18:06:40 +03:00
$sort_order++;
2018-04-17 16:40:52 +03:00
// Added invoice discount
if ($discount_total) {
InvoiceTotal::create([
'company_id' => $request['company_id'],
'invoice_id' => $invoice->id,
'code' => 'discount',
'name' => 'invoices.discount',
'amount' => $discount_total,
'sort_order' => $sort_order,
]);
// This is for total
$sub_total = $sub_total - $discount_total;
2018-10-09 19:41:05 +03:00
$sort_order++;
2018-04-17 16:40:52 +03:00
}
// Added invoice taxes
2018-10-19 19:43:50 +03:00
if (isset($taxes)) {
foreach ($taxes as $tax) {
InvoiceTotal::create([
2017-10-12 18:06:40 +03:00
'company_id' => $request['company_id'],
'invoice_id' => $invoice->id,
'code' => 'tax',
'name' => $tax['name'],
'amount' => $tax['amount'],
'sort_order' => $sort_order,
]);
2017-10-12 18:06:40 +03:00
$sort_order++;
}
}
2018-04-17 16:40:52 +03:00
// Added invoice total
InvoiceTotal::create([
2017-10-12 18:06:40 +03:00
'company_id' => $request['company_id'],
'invoice_id' => $invoice->id,
'code' => 'total',
'name' => 'invoices.total',
'amount' => $sub_total + $tax_total,
'sort_order' => $sort_order,
]);
2017-10-12 18:06:40 +03:00
}
2017-09-14 22:21:00 +03:00
}