@herry_collins You need to break your requirements and functionality down into steps. You‘ve listed at least three different things:
- Estimations
- Revisions of estimates
- Exporting estimates as PDFs
Tackle each thing independently, otherwise you’re just going to get overwhelmed.
As for how to structure things, I’d certainly keep business logic extracted in a dedicated class rather than stuffing it all in a controller action somewhere. If estimates are essentially quotes with line items and quantities, then I’d model it as such: a parent Estimate model, that has many EstimateItem models. You could then have an EstimateService class that uses a builder pattern for creating estimates (similar to Cashier’s newSubscription method):
$estimate = $this->estimateService->newEstimate()
->addItem($product1, $quantity1)
->addItem($product2, $quantity2)
->create();
When it comes to adding revisions, your changes would then be contained in just your estimate service class and its method for updating estimates:
$estimate = $this->estimateService->update($estimate, $newItems);
Finally, for exporting estimates as PDFs, it shouldn’t take too long if all you‘re doing is fetching records from a database and rendering them in a template. But if it is, you can push it to a queue, and just send a notification with a download link to the user when it’s ready:
class ExportEstaimte
{
use Queueable;
public function __construct(protected Estimate $estimate) {}
public function handle()
{
// Generate PDF...
$this->estimate->customer->notify(new EstimateReadyNotification($this->estimate));
}
}