128 lines
4.8 KiB
PHP
128 lines
4.8 KiB
PHP
<?php
|
|
|
|
namespace Database\Seeders;
|
|
|
|
use Illuminate\Database\Seeder;
|
|
use App\Models\SalesInvoice;
|
|
use App\Models\SalesInvoiceItem;
|
|
use App\Models\SalesInvoiceReturn;
|
|
use App\Models\SalesInvoiceReturnItem;
|
|
use Carbon\Carbon;
|
|
|
|
class DemoSalesReturnSeeder extends Seeder
|
|
{
|
|
public function run(DemoContext $ctx): void
|
|
{
|
|
if (SalesInvoiceReturn::where('created_by', $ctx->userId)->exists()) {
|
|
return;
|
|
}
|
|
|
|
if (empty($ctx->salesInvoiceIds)) {
|
|
return;
|
|
}
|
|
|
|
$returns = [
|
|
[
|
|
'invoice_offset' => 0,
|
|
'days_after' => 10,
|
|
'status' => 'approved',
|
|
'reason' => 'defective',
|
|
'return_pct' => 0.3,
|
|
],
|
|
[
|
|
'invoice_offset' => 1,
|
|
'days_after' => 7,
|
|
'status' => 'approved',
|
|
'reason' => 'wrong_item',
|
|
'return_pct' => 1.0,
|
|
],
|
|
[
|
|
'invoice_offset' => 2,
|
|
'days_after' => 14,
|
|
'status' => 'approved',
|
|
'reason' => 'excess_quantity',
|
|
'return_pct' => 0.2,
|
|
],
|
|
[
|
|
'invoice_offset' => 3,
|
|
'days_after' => 5,
|
|
'status' => 'draft',
|
|
'reason' => 'damaged',
|
|
'return_pct' => 0.5,
|
|
],
|
|
[
|
|
'invoice_offset' => 4,
|
|
'days_after' => 3,
|
|
'status' => 'draft',
|
|
'reason' => 'other',
|
|
'return_pct' => 0.4,
|
|
],
|
|
];
|
|
|
|
foreach ($returns as $index => $ret) {
|
|
if (!isset($ctx->salesInvoiceIds[$ret['invoice_offset']])) {
|
|
continue;
|
|
}
|
|
|
|
$invoiceId = $ctx->salesInvoiceIds[$ret['invoice_offset']];
|
|
$invoice = SalesInvoice::with('items')->find($invoiceId);
|
|
if (!$invoice || $invoice->items->isEmpty()) continue;
|
|
|
|
$returnDate = Carbon::parse($invoice->invoice_date)->addDays($ret['days_after']);
|
|
$returnNumber = 'SR-' . $returnDate->format('Y') . '-' . $returnDate->format('m') . '-' . str_pad($index + 1, 3, '0', STR_PAD_LEFT);
|
|
|
|
$return = SalesInvoiceReturn::create([
|
|
'return_number' => $returnNumber,
|
|
'return_date' => $returnDate->toDateString(),
|
|
'customer_id' => $invoice->customer_id,
|
|
'warehouse_id' => $invoice->warehouse_id,
|
|
'original_invoice_id' => $invoice->id,
|
|
'reason' => $ret['reason'],
|
|
'status' => $ret['status'],
|
|
'notes' => 'Auto-generated demo return for ' . $invoice->invoice_number,
|
|
'creator_id' => $ctx->userId,
|
|
'created_by' => $ctx->userId,
|
|
'created_at' => $returnDate,
|
|
'updated_at' => $returnDate,
|
|
]);
|
|
|
|
$subtotal = 0;
|
|
$taxTotal = 0;
|
|
|
|
// Return a portion of items from the original invoice
|
|
$itemsToReturn = $ret['return_pct'] >= 1.0
|
|
? $invoice->items
|
|
: $invoice->items->take(max(1, (int) ceil($invoice->items->count() * $ret['return_pct'])));
|
|
|
|
foreach ($itemsToReturn as $originalItem) {
|
|
$returnQty = $ret['return_pct'] >= 1.0
|
|
? $originalItem->quantity
|
|
: max(1, (int) ceil($originalItem->quantity * $ret['return_pct']));
|
|
|
|
$returnItem = SalesInvoiceReturnItem::create([
|
|
'return_id' => $return->id,
|
|
'product_id' => $originalItem->product_id,
|
|
'original_invoice_item_id' => $originalItem->id,
|
|
'original_quantity' => $originalItem->quantity,
|
|
'return_quantity' => $returnQty,
|
|
'unit_price' => $originalItem->unit_price,
|
|
'discount_percentage' => $originalItem->discount_percentage,
|
|
'tax_percentage' => $originalItem->tax_percentage,
|
|
'reason' => $ret['reason'],
|
|
]);
|
|
|
|
$subtotal += $returnItem->total_amount;
|
|
$taxTotal += $returnItem->tax_amount;
|
|
}
|
|
|
|
$return->update([
|
|
'subtotal' => $subtotal - $taxTotal,
|
|
'tax_amount' => $taxTotal,
|
|
'total_amount' => $subtotal,
|
|
]);
|
|
|
|
$ctx->salesReturnIds[] = $return->id;
|
|
}
|
|
}
|
|
}
|