Viewing File: /home/ubuntu/vedadeals-backend-base/app/Jobs/PriceConvertionJob.php

<?php

namespace App\Jobs;

use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Exception;
use App\Models\{Product};

class PriceConvertionJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;

    protected $to;

    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($to)
    {
        $this->to = $to;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {
        try {

            Product::orderBy('id', 'DESC')
            ->chunkById(200, function ($products) {
                foreach($products as $product) {
                    if($this->to == "usd") {

                        $original_price = $product->original_price * 0.01131;

                        $discount_price = $product->discount_price * 0.01131;

                        $price = $product->price * 0.01131;

                    } elseif($this->to == "pscn") {

                        $original_price = $product->original_price / 0.01131;

                        $discount_price = $product->discount_price / 0.01131;

                        $price = $product->price / 0.01131;

                    } else {

                        throw new Exception("Please add to parameter (usd or pscn).");
                    }

                    $product->update([
                        'original_price' => number_format((float)$original_price, 2, '.', ''),
                        'discount_price' => number_format((float)$discount_price, 2, '.', ''),
                        'price' => number_format((float)$price, 2, '.', ''),
                    ]);

                    info("Product Updated : ".$product->id);
                }
            }, $column = 'id');

            info("PriceConvertionJob Success");

            return true;

        } catch(Exception $e) {

            info("PriceConvertionJob Error : ".$e->getMessage());
        }
    }
}
Back to Directory File Manager