File "AddOrderItemIdToPOItems.php"

Full Path: /home/clickysoft/public_html/jmapi5.clickysoft.net/app/Console/Commands/AddOrderItemIdToPOItems.php
File size: 1.2 KB
MIME-type: text/x-php
Charset: utf-8

<?php

namespace App\Console\Commands;

use App\Models\OrderItems;
use App\Models\PurchaseOrderToCreate;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;

class AddOrderItemIdToPOItems extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'email:add-item-ids-to-po-orders';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Add order item id to items that are not created yet.';

    public function handle(): void
    {
        $order_items = OrderItems::where('po_created', 0)->get();
        foreach ($order_items as $item) {
            $po_items = PurchaseOrderToCreate::where([
                'order_id' => $item->order_id,
                'product_id' => $item->product_id,
                'price_id' => $item->price_id,
                'quantity' => $item->quantity,
            ])->get();

            foreach ($po_items as $po_item) {
                $po_item->order_item_id = $item->id;
                $po_item->save();
            }
        }

        Log::channel('info_errors')
            ->info("Record ids updated for PO To Create.");
    }
}