<?php

namespace App\Modules\Stock;

use Exception;

class StockPurchaseOrderItemSerialObserver
{
    public function created($model)
    {
        $this->updatePurchaseReceivedItems($model);
    }

    /**
     * ------------------------------------------------
     * PROTECTED FUNCTIONS BELOW
     * ------------------------------------------------.
     */

    /**
     *	Update the number of items received everytime a new serial number to that item is created.
     *
     * @return void
     */
    protected function updatePurchaseReceivedItems($model)
    {
        $item = StockPurchaseOrderItem::findOrFail($model->order_item_id);
        $item->received_quantity++;
        if (! $item->save()) {
            throw new Exception(json_encode($item->erros->all()), 1);
        }
    }
}
