mirror of
https://github.com/InvoiceShelf/InvoiceShelf.git
synced 2026-09-08 08:04:10 +00:00
feat(sales): fresh sales implementation
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Sales\Http\Controllers\Company;
|
||||
|
||||
use App\Domains\Sales\Models\Estimate;
|
||||
use App\Platform\Http\Controller;
|
||||
use App\Platform\Pdf\Rendering\PdfTemplateUtils;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class EstimateTemplatesController extends Controller
|
||||
{
|
||||
/**
|
||||
* List the PDF templates an estimate can be rendered with, each already
|
||||
* paired with its preview image.
|
||||
*/
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
$this->authorize('viewAny', Estimate::class);
|
||||
|
||||
return response()->json([
|
||||
'estimateTemplates' => PdfTemplateUtils::getFormattedTemplates('estimate'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,179 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Sales\Http\Controllers\Company;
|
||||
|
||||
use App\Domains\Sales\Application\EstimateService;
|
||||
use App\Domains\Sales\Http\Requests\DeleteEstimatesRequest;
|
||||
use App\Domains\Sales\Http\Requests\EstimatesRequest;
|
||||
use App\Domains\Sales\Http\Requests\SendEstimatesRequest;
|
||||
use App\Domains\Sales\Http\Resources\EstimateResource;
|
||||
use App\Domains\Sales\Http\Resources\InvoiceResource;
|
||||
use App\Domains\Sales\Jobs\GenerateEstimatePdfJob;
|
||||
use App\Domains\Sales\Models\Estimate;
|
||||
use App\Domains\Sales\Models\Invoice;
|
||||
use App\Platform\Http\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Mail\Markdown;
|
||||
|
||||
/**
|
||||
* Company-scoped estimate endpoints: listing, the write surface, bulk removal,
|
||||
* mailing, and the two document conversions.
|
||||
*/
|
||||
class EstimatesController extends Controller
|
||||
{
|
||||
public function __construct(private readonly EstimateService $estimateService) {}
|
||||
|
||||
/**
|
||||
* Paginated estimates of the active company, joined to their customer so the
|
||||
* list can be filtered and sorted by customer name.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$this->authorize('viewAny', Estimate::class);
|
||||
|
||||
$filters = $request->all();
|
||||
$perPage = $request->has('limit') ? $request->input('limit') : 10;
|
||||
|
||||
$page = Estimate::query()
|
||||
->whereCompany()
|
||||
->join('customers', fn ($join) => $join->on('customers.id', '=', 'estimates.customer_id'))
|
||||
->applyFilters($filters)
|
||||
->select(['estimates.*', 'customers.name'])
|
||||
->orderByDesc('created_at')
|
||||
->paginateData($perPage);
|
||||
|
||||
return EstimateResource::collection($page)->additional([
|
||||
'meta' => [
|
||||
'estimate_total_count' => Estimate::query()->whereCompany()->count(),
|
||||
],
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Persist a new estimate, optionally mailing it straight away, and queue the
|
||||
* PDF render.
|
||||
*/
|
||||
public function store(EstimatesRequest $request)
|
||||
{
|
||||
$this->authorize('create', Estimate::class);
|
||||
|
||||
$estimate = $this->estimateService->create(...$this->writeArguments($request));
|
||||
|
||||
if ($request->has('estimateSend')) {
|
||||
$this->estimateService->send($estimate, $request->only(['title', 'body']));
|
||||
}
|
||||
|
||||
GenerateEstimatePdfJob::dispatch($estimate);
|
||||
|
||||
return EstimateResource::make($estimate);
|
||||
}
|
||||
|
||||
public function show(Request $request, Estimate $estimate)
|
||||
{
|
||||
$this->authorize('view', $estimate);
|
||||
|
||||
return EstimateResource::make($estimate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Overwrite an estimate — lines and taxes are replaced wholesale — and
|
||||
* re-render its PDF.
|
||||
*/
|
||||
public function update(EstimatesRequest $request, Estimate $estimate)
|
||||
{
|
||||
$this->authorize('update', $estimate);
|
||||
|
||||
$estimate = $this->estimateService->update($estimate, ...$this->writeArguments($request));
|
||||
|
||||
GenerateEstimatePdfJob::dispatch($estimate, true);
|
||||
|
||||
return EstimateResource::make($estimate);
|
||||
}
|
||||
|
||||
/**
|
||||
* Bulk removal. Ids outside the active company are silently skipped.
|
||||
*/
|
||||
public function delete(DeleteEstimatesRequest $request)
|
||||
{
|
||||
$this->authorize('delete multiple estimates');
|
||||
|
||||
$ids = Estimate::query()
|
||||
->whereCompany()
|
||||
->whereIn('id', $request->input('ids'))
|
||||
->pluck('id');
|
||||
|
||||
Estimate::destroy($ids);
|
||||
|
||||
return response()->json(['success' => true]);
|
||||
}
|
||||
|
||||
public function send(SendEstimatesRequest $request, Estimate $estimate)
|
||||
{
|
||||
$this->authorize('send estimate', $estimate);
|
||||
|
||||
return response()->json(
|
||||
$this->estimateService->send($estimate, $request->all())
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Render the mail body the customer would receive, without sending it.
|
||||
*/
|
||||
public function sendPreview(SendEstimatesRequest $request, Estimate $estimate)
|
||||
{
|
||||
$this->authorize('send estimate', $estimate);
|
||||
|
||||
$data = $this->estimateService->sendEstimateData($estimate, $request->all());
|
||||
$data['url'] = $estimate->estimatePdfUrl;
|
||||
|
||||
$renderer = new Markdown(view(), config('mail.markdown'));
|
||||
|
||||
return $renderer->render('emails.send.estimate', ['data' => $data]);
|
||||
}
|
||||
|
||||
public function clone(Request $request, Estimate $estimate)
|
||||
{
|
||||
$this->authorize('view', $estimate);
|
||||
$this->authorize('create', Estimate::class);
|
||||
|
||||
return EstimateResource::make($this->estimateService->clone($estimate));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reading the source estimate is checked on top of the invoice-create
|
||||
* ability so the conversion cannot reach across companies.
|
||||
*/
|
||||
public function convertToInvoice(Request $request, Estimate $estimate)
|
||||
{
|
||||
$this->authorize('view', $estimate);
|
||||
$this->authorize('create', Invoice::class);
|
||||
|
||||
return InvoiceResource::make($this->estimateService->convertToInvoice($estimate));
|
||||
}
|
||||
|
||||
public function changeStatus(Request $request, Estimate $estimate)
|
||||
{
|
||||
$this->authorize('send estimate', $estimate);
|
||||
|
||||
$this->estimateService->changeStatus($estimate, $request->input('status'));
|
||||
|
||||
return response()->json(['success' => true]);
|
||||
}
|
||||
|
||||
/**
|
||||
* The arguments create() and update() share, keyed by parameter name.
|
||||
*
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
private function writeArguments(EstimatesRequest $request): array
|
||||
{
|
||||
$fields = $request->input('customFields');
|
||||
|
||||
return [
|
||||
'attributes' => $request->getEstimatePayload(),
|
||||
'items' => $request->input('items'),
|
||||
'taxes' => $request->has('taxes') ? $request->input('taxes') : null,
|
||||
'customFields' => is_iterable($fields) ? $fields : null,
|
||||
];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,27 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Sales\Http\Controllers\Company;
|
||||
|
||||
use App\Domains\Sales\Models\Invoice;
|
||||
use App\Platform\Http\Controller;
|
||||
use App\Platform\Pdf\Rendering\PdfTemplateUtils;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
class InvoiceTemplatesController extends Controller
|
||||
{
|
||||
/**
|
||||
* List the PDF templates an invoice can be rendered with, each already
|
||||
* paired with its preview image.
|
||||
*
|
||||
* @return JsonResponse
|
||||
*/
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
$this->authorize('viewAny', Invoice::class);
|
||||
|
||||
return response()->json([
|
||||
'invoiceTemplates' => PdfTemplateUtils::getFormattedTemplates('invoice'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,132 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Sales\Http\Controllers\Company;
|
||||
|
||||
use App\Domains\Sales\Application\RecurringInvoiceService;
|
||||
use App\Domains\Sales\Http\Requests\RecurringInvoiceRequest;
|
||||
use App\Domains\Sales\Http\Resources\RecurringInvoiceResource;
|
||||
use App\Domains\Sales\Models\RecurringInvoice;
|
||||
use App\Platform\Http\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* The admin surface for standing orders — the schedules that mint invoices on
|
||||
* a cron expression.
|
||||
*
|
||||
* Each row is an invoice held in template form, so the write endpoints hand
|
||||
* the service the same three parcels a document does: the row's own columns,
|
||||
* the line items, and the document-level taxes.
|
||||
*/
|
||||
class RecurringInvoiceController extends Controller
|
||||
{
|
||||
public function __construct(
|
||||
private readonly RecurringInvoiceService $recurringInvoiceService,
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Page through the company's schedules.
|
||||
*
|
||||
* Alongside the page itself the payload carries how many schedules the
|
||||
* company holds in total, which the listing screen shows even when a
|
||||
* filter has narrowed the rows down to a handful.
|
||||
*/
|
||||
public function index(Request $request)
|
||||
{
|
||||
$this->authorize('viewAny', RecurringInvoice::class);
|
||||
|
||||
$perPage = $request->has('limit') ? $request->input('limit') : 10;
|
||||
|
||||
$schedules = RecurringInvoice::whereCompany()
|
||||
->applyFilters($request->all())
|
||||
->paginateData($perPage);
|
||||
|
||||
$companyTotal = RecurringInvoice::whereCompany()->count();
|
||||
|
||||
return RecurringInvoiceResource::collection($schedules)
|
||||
->additional(['meta' => [
|
||||
'recurring_invoice_total_count' => $companyTotal,
|
||||
]]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set up a new schedule from the submitted template.
|
||||
*/
|
||||
public function store(RecurringInvoiceRequest $request)
|
||||
{
|
||||
$this->authorize('create', RecurringInvoice::class);
|
||||
|
||||
$schedule = $this->recurringInvoiceService->create(
|
||||
attributes: $request->getRecurringInvoicePayload(),
|
||||
items: $request->input('items'),
|
||||
taxes: $request->has('taxes') ? $request->input('taxes') : null,
|
||||
customFields: $this->customFields($request),
|
||||
);
|
||||
|
||||
return new RecurringInvoiceResource($schedule);
|
||||
}
|
||||
|
||||
/**
|
||||
* Show one schedule.
|
||||
*/
|
||||
public function show(RecurringInvoice $recurringInvoice)
|
||||
{
|
||||
$this->authorize('view', $recurringInvoice);
|
||||
|
||||
return new RecurringInvoiceResource($recurringInvoice);
|
||||
}
|
||||
|
||||
/**
|
||||
* Restate a schedule, template and all.
|
||||
*
|
||||
* Items and taxes are replaced wholesale rather than reconciled, so the
|
||||
* submission is the schedule's new contents in full.
|
||||
*/
|
||||
public function update(RecurringInvoiceRequest $request, RecurringInvoice $recurringInvoice)
|
||||
{
|
||||
$this->authorize('update', $recurringInvoice);
|
||||
|
||||
$this->recurringInvoiceService->update(
|
||||
recurringInvoice: $recurringInvoice,
|
||||
attributes: $request->getRecurringInvoicePayload(),
|
||||
items: $request->input('items'),
|
||||
taxes: $request->has('taxes') ? $request->input('taxes') : null,
|
||||
customFields: $this->customFields($request),
|
||||
);
|
||||
|
||||
return new RecurringInvoiceResource($recurringInvoice);
|
||||
}
|
||||
|
||||
/**
|
||||
* Drop several schedules at once.
|
||||
*
|
||||
* The submitted ids are narrowed to the acting company before anything is
|
||||
* removed, so ids belonging elsewhere are quietly passed over. Invoices
|
||||
* already minted by a dropped schedule survive it — they are merely cut
|
||||
* loose from the parent.
|
||||
*/
|
||||
public function delete(Request $request)
|
||||
{
|
||||
$this->authorize('delete multiple recurring invoices');
|
||||
|
||||
$ids = RecurringInvoice::whereCompany()
|
||||
->whereIn('id', $request->input('ids'))
|
||||
->pluck('id');
|
||||
|
||||
$this->recurringInvoiceService->delete($ids);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* The submitted custom-field values, or nothing when the payload carries
|
||||
* something the service cannot walk.
|
||||
*/
|
||||
private function customFields(RecurringInvoiceRequest $request): ?iterable
|
||||
{
|
||||
$values = $request->input('customFields');
|
||||
|
||||
return is_iterable($values) ? $values : null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Sales\Http\Controllers\Company;
|
||||
|
||||
use App\Domains\Sales\Models\RecurringInvoice;
|
||||
use App\Platform\Http\Controller;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* Previews the moment a schedule would fire for the first time.
|
||||
*/
|
||||
class RecurringInvoiceFrequencyController extends Controller
|
||||
{
|
||||
/**
|
||||
* Read a cron expression and a start date, and answer with the first
|
||||
* firing they produce.
|
||||
*
|
||||
* The schedule form asks for this while it is still being filled in, so
|
||||
* nothing here is gated or written down — the date is worked out, handed
|
||||
* back and forgotten. A start date the parser cannot read, or an
|
||||
* expression it cannot parse, comes back as a server error rather than a
|
||||
* validation message.
|
||||
*/
|
||||
public function __invoke(Request $request)
|
||||
{
|
||||
$nextRun = RecurringInvoice::getNextInvoiceDate(
|
||||
$request->input('frequency'),
|
||||
$request->input('starts_at'),
|
||||
);
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'next_invoice_at' => $nextRun,
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Sales\Http\Controllers\Company;
|
||||
|
||||
use App\Domains\Receivables\Models\Payment;
|
||||
use App\Domains\Sales\Application\SerialNumberService;
|
||||
use App\Domains\Sales\Models\Estimate;
|
||||
use App\Domains\Sales\Models\Invoice;
|
||||
use App\Platform\Http\Controller;
|
||||
use Illuminate\Http\JsonResponse;
|
||||
use Illuminate\Http\Request;
|
||||
|
||||
/**
|
||||
* Serves the number-format previews the settings and document screens use.
|
||||
*/
|
||||
class SerialNumberController extends Controller
|
||||
{
|
||||
/**
|
||||
* Render the number the next document of the requested kind would carry.
|
||||
*
|
||||
* The `key` query parameter picks the document kind; an unknown one is
|
||||
* reported as a plain failure rather than an error. `format` overrides the
|
||||
* company's stored format so the settings screen can preview edits, and
|
||||
* `model_id` lets an existing document keep the numbers it already has.
|
||||
*/
|
||||
public function nextNumber(Request $request, Invoice $invoice, Estimate $estimate, Payment $payment): JsonResponse
|
||||
{
|
||||
$serial = (new SerialNumberService)
|
||||
->setCompany($request->header('company'))
|
||||
->setCustomer($request->userId);
|
||||
|
||||
// Invoices and credit notes live in one table, so each is pinned to its
|
||||
// own row type: the preview must never count the other kind's rows.
|
||||
switch ($request->key) {
|
||||
case 'invoice':
|
||||
$serial->setModel($invoice)
|
||||
->setSequenceScope(['type' => Invoice::TYPE_INVOICE]);
|
||||
|
||||
break;
|
||||
|
||||
case 'credit_note':
|
||||
$serial->setModel($invoice)
|
||||
->setSettingKey('credit_note_number_format')
|
||||
->setSequenceScope(['type' => Invoice::TYPE_CREDIT_NOTE]);
|
||||
|
||||
break;
|
||||
|
||||
case 'estimate':
|
||||
$serial->setModel($estimate);
|
||||
|
||||
break;
|
||||
|
||||
case 'payment':
|
||||
$serial->setModel($payment);
|
||||
|
||||
break;
|
||||
|
||||
default:
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
]);
|
||||
}
|
||||
|
||||
try {
|
||||
$nextNumber = $serial->setModelObject($request->model_id)
|
||||
->getNextNumber($request->input('format'));
|
||||
} catch (\Exception $exception) {
|
||||
return response()->json([
|
||||
'success' => false,
|
||||
'message' => $exception->getMessage(),
|
||||
]);
|
||||
}
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'nextNumber' => $nextNumber,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* List the tokens a submitted format string is made of.
|
||||
*/
|
||||
public function placeholders(Request $request): JsonResponse
|
||||
{
|
||||
$format = $request->input('format');
|
||||
|
||||
return response()->json([
|
||||
'success' => true,
|
||||
'placeholders' => $format ? SerialNumberService::getPlaceholders($format) : [],
|
||||
]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user