lifesound's avatar

Code improvement advices

try {
            DB::transaction(function () use ($file, $file_name, &$ship) {
                $ship = Shipment::create([
                    'filename' => $file_name
                ]);

                $all_stored_product_serials = Product::distinct()->pluck('serial')->toArray();
                $all_stored_product_mfg_serials = Product::distinct()->pluck('mfg_serial')->toArray(); 
                Excel::toCollection(new ProductsImport($ship->id), $file)->first()->map(function ($import) use ($ship, $all_stored_product_serials, $all_stored_product_mfg_serials) {
                    $import = $import->toArray();
                    unset($import['id']);
                    $import['serial'] = trim(strtoupper($import['serial']));
                    $import['mfg_serial'] = trim(strtoupper($import['mfg_serial']));
                    // $import['category'] = $import['category']  ?? '';
                    $import['shipment_id'] = $ship->id;
                    $import['created_at'] = now();
                    $import['updated_at'] = now();

                    $import['sku'] = $import['serial'] ? trim(strtoupper($import['sku'])) : ''; // temporary sku until generated after loop to decrease querires
                    if (
                        in_array(strtoupper($import['serial']), $all_stored_product_serials) ||
                        in_array(strtoupper($import['mfg_serial']), $all_stored_product_mfg_serials)
                    ) {
                        Duplicate::insert($import);
                    } else {
                        Product::insert($import);
                    }
                });
                $this->generateSku($ship); // <===== Not called why ?
            });

            $message = 'File uploaded successfully';
            return redirect('/shipments/' . $ship->id)->with('success', $message);
        } catch (\Exception $e) {
            $message = 'Unexpected error happened';
            return back()->withErrors('', $message);
        }
    }

    public function generateSku($ship)
    {
        $sku_start_number = 100001;
        $all_stored_product_names_skus = Product::distinct()->pluck('product', 'sku')->toArray();

        $shipment_products = Shipment::find($ship)->products;

        foreach ($shipment_products as $product) {
            if ($product->sku == '' || $product->sku == null) {
                // if the name already exists in the db - get the same sku
                if (array_key_exists(trim($product->product), $all_stored_product_names_skus)) {
                    $product->sku = $all_stored_product_names_skus['product']; //sku from arr by product name
                } else {
                    $type_2letters = $product->type ? substr(trim($product->type), 0, 2) : 'PR';
                    $brand_2letters = $product->brand ? substr(trim($product->brand), 0, 2) : 'BR';
                    $sku_start_number += 1;
                    $sku = $type_2letters . $brand_2letters . $sku_start_number;
                    $product->sku = strtoupper($sku);
                    $product->save();
                }
            }
        }

        $ship->ended_at = now();
        $ship->save();
    }

Without using Quoues: I tried to decrease the queries to the fewer amount but the upload time is still the same , any improvement Is appreciated

also, i do not know why the generateSku function was not called or executed. + the page not redirect ...

should I upload via ajax ?

0 likes
2 replies
lifesound's avatar

why the function is not redirect as a frustrating issue ?

jlrdw's avatar
jlrdw
Best Answer
Level 75

@lifesound I suggest a combination of dd at various places to see what is happening. And check your network tab response and request to see what is happening.

These two techniques usually helps me figure things out.

1 like

Please or to participate in this conversation.