Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

yonka's avatar

I want an easier way to make salesReturn

I want to make salesReturn. I have these models

  1. SoldInventory: sales information like customer and soldInventoryItems
  1. SoldInventoryItems: items sold
class SoldInventoryItems extends Model
{
    use HasFactory, CreatedUpdatedBy, softDeletes;

    protected $fillable = ['inventory_in_store_id','quantity','unit_price','sub_total'];

//    public function inventoryInSale()
//    {
//        return $this->belongsTo(InventoryInSale::class);
//    }

    public function soldInventory()
    {
        return $this->belongsTo(SoldInventory::class);
    }

    public function inventoryInStore() : BelongsTo
    {
        return $this->belongsTo(InventoryInStore::class);
    }

    public function user()
    {
        return $this->belongsTo(User::class);
    }
}

3.SalesInvoice: invoice made when customer buys items

class SalesInvoice extends Model
{
    use HasFactory, CreatedUpdatedBy, softDeletes;

    protected $fillable = ['sold_inventory_id','customer_id','invoice_no','reference_no','issue_date','sub_total','vat','discount','total_amount','balance','invoice_status'];

    public function soldInventory()
    {
        return $this->belongsTo(SoldInventory::class);
    }

    public function salesInvoiceReceipts()
    {
        return $this->hasMany(SalesInvoiceReceipt::class);
    }

    public function customer() :BelongsTo
    {
        return $this->belongsTo(Customer::class);
    }
}

4.SalesInvoiceReceipts

class SalesInvoiceReceipt extends Model
{
    use HasFactory, CreatedUpdatedBy, softDeletes;

    protected $fillable = ['sales_invoice_id','company_account_id','customer_id','receipt_no','reference_no','paid_amount','paid_date','balance','status','description'];

    public function companyAccount()
    {
        return $this->belongsTo(CompanyAccount::class);
    }

    public function salesInvoice()
    {
        return $this->belongsTo(SalesInvoice::class);
    }

    public function companyAccountTransaction(): HasOne
    {
        return $this->hasOne(CompanyAccountTransaction::class);
    }

    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }
}

If I make sales, SoldInventory model is created with soldInventoryItems and also salesInvoice, if customer makes payment, also salesInvoiceReceipts is created. so salesInvoice can have even many salesInvoiceReceipts. So If the customer return some items and said refund my money, I want to be able to make salesReturn and update soldInventoryItems which is also update inventoryInStore to track inventory in the store correctly and also be able to refund money that will update salesInvoice and saleInvoiceReceipts. if the salesReturn items updated wrong way, for instance user/saler made wrong by updating wrong item which is not the one returned, I also want to be able to update salesReturn to correct it.

0 likes
5 replies
LaryAI's avatar
Level 58

Certainly! Here’s a practical approach to implementing a salesReturn feature in your Laravel application, based on your models and requirements.


1. Create a SalesReturn Model

This model will track each return event, linking it to the original sale, customer, and the items being returned.

php artisan make:model SalesReturn -m
php artisan make:model SalesReturnItem -m

SalesReturn migration:

Schema::create('sales_returns', function (Blueprint $table) {
    $table->id();
    $table->foreignId('sold_inventory_id')->constrained()->onDelete('cascade');
    $table->foreignId('customer_id')->constrained()->onDelete('cascade');
    $table->date('return_date');
    $table->text('reason')->nullable();
    $table->timestamps();
});

SalesReturnItem migration:

Schema::create('sales_return_items', function (Blueprint $table) {
    $table->id();
    $table->foreignId('sales_return_id')->constrained()->onDelete('cascade');
    $table->foreignId('sold_inventory_item_id')->constrained()->onDelete('cascade');
    $table->integer('quantity');
    $table->decimal('refund_amount', 15, 2);
    $table->timestamps();
});

2. Define Relationships

SalesReturn.php

class SalesReturn extends Model
{
    protected $fillable = ['sold_inventory_id', 'customer_id', 'return_date', 'reason'];

    public function soldInventory()
    {
        return $this->belongsTo(SoldInventory::class);
    }

    public function customer()
    {
        return $this->belongsTo(Customer::class);
    }

    public function items()
    {
        return $this->hasMany(SalesReturnItem::class);
    }
}

SalesReturnItem.php

class SalesReturnItem extends Model
{
    protected $fillable = ['sales_return_id', 'sold_inventory_item_id', 'quantity', 'refund_amount'];

    public function salesReturn()
    {
        return $this->belongsTo(SalesReturn::class);
    }

    public function soldInventoryItem()
    {
        return $this->belongsTo(SoldInventoryItems::class);
    }
}

3. Sales Return Logic (Controller Example)

SalesReturnController.php


4. Editing/Correcting a Sales Return

Allow editing the SalesReturn and SalesReturnItem records. When editing, reverse the previous changes (restore quantities, etc.), then apply the new changes as above.


5. Summary of Steps

  • Track returns with new tables (sales_returns, sales_return_items).
  • Update sold item quantities and restore inventory.
  • Adjust invoices and receipts for refunds.
  • Allow corrections by reversing and re-applying changes.

This structure keeps your sales, inventory, and financial records accurate and auditable, and allows for easy corrections if mistakes are made.

If you need more detailed code for editing/correcting returns, let me know!

jlrdw's avatar

I suggest learning the correct business logic (discussed my me and others in previous post), then setup expense and income reports you need.

So If the customer return some items and said refund my money, I want to be able to make salesReturn and update soldInventoryItems which is also update inventoryInStore to track inventory in the store correctly and also be able to refund money that will update salesInvoice and saleInvoiceReceipts. if the salesReturn items updated wrong way, for instance user/saler made wrong by updating wrong item which is not the one returned, I also want to be able to update salesReturn to correct it.

It's looked up from the invoice table which would have child records of payments.

Snapey's avatar

you should not be altering history.

If the customer returns some items then that is a new transaction. It can reference the original sale, but you should not be looking to amend the sale or the invoice.

Please or to participate in this conversation.