Observer that auto-generates sale transaction codes ('SELL0001' format), sets default date, associates user, and links to cash drawer.
<?php
namespace App\Observers;
use App\Models\Tenants\CashDrawer;
use App\Models\Tenants\Selling;
use App\Models\Tenants\Setting;
use Illuminate\Contracts\Validation\DataAwareRule;
use Illuminate\Support\Str;
class SellingObserver extends AbstractObserver implements DataAwareRule
{
protected $data = [];
public function setData($data)
{
$this->data = $data;
return $this;
}
public function creating(Selling $selling)
{
if (! $selling->date) {
$selling->date = now()->format('Y-m-d H:i:s');
}
$sellings = Selling::all();
$lastCount = $sellings->count();
/* TODO: fixing the iteration code <10-08-22, sheenazien8> */
$selling->code = 'SELL'.Str::of($lastCount + 1)->padLeft(4, 0)->value();
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search 40K more like it.
Get a free API key