discount_amount = ProductPrecentageDiscount::first()['amount'] ?? 0; } public function collection(Collection $collection) { $productPrice = new SamaProducts; $stat = true; foreach ($collection as $row) { if ($row->filter()->isNotEmpty()) { $input = $row->toArray(); $price = (float) str_replace(',', '', $input['price']); // Remove unnecessary columns unset($input['id'], $input['created_at'], $input['updated_at']); // Check for empty or negative price if (empty($input['price']) || $price < 0) { continue; } // $product = InternalProducts::where('sama_sku', $input['product_sku']); // if ($product->exists()) { // $product = $product->first(); // $matchData = [ // "product_id" => $product['id'], // "sku" => $input['product_sku'], // "metalType" => $input['metaltype'], // "metalColor" => $input['metalcolor'], // "diamondQuality" => $input['diamondquality'], // "finishLevel" => $input['finishlevel'], // ]; // $tobeUpdate = [ // "product_id" => $product['id'], // "sku" => $input['product_sku'], // "metalType" => $input['metaltype'], // "metalColor" => $input['metalcolor'], // "diamondQuality" => $input['diamondquality'], // "finishLevel" => $input['finishlevel'], // "reference_price" => $price, // "discount_percentage" => 0, // "price" => $price, // ]; // $is_saved = SamaPrices::updateOrCreate($matchData, $tobeUpdate); // if (!$is_saved) { // $stat = false; // Log::warning("Failed to save SamaPrices", [ // 'product_id' => $product['id'], // 'sku' => $input['product_sku'], // ]); // }else{ // Log::info("Successfully saved SamaPrices", [ // 'product_id' => $product['id'], // 'sku' => $input['product_sku'], // ]); // } // } else { // Log::info("Skipped product because not found", [ // 'sku' => $input['product_sku'] ?? null, // 'metalType' => $input['metaltype'] ?? null, // 'metalColor' => $input['metalcolor'] ?? null, // 'diamondQuality' => $input['diamondquality'] ?? null, // 'finishLevel' => $input['finishlevel'] ?? null, // ]); // continue; // } $product = InternalProducts::where('sama_sku', $input['product_sku'])->first(); if ($product) { $price = (float) str_replace(',', '', $input['price'] ?? 0); $matchData = [ 'product_id' => $product->id, 'sku' => $input['product_sku'], 'metalType' => $input['metaltype'], 'metalColor' => $input['metalcolor'], 'diamondQuality' => $input['diamondquality'], 'finishLevel' => $input['finishlevel'], ]; try { $samaPrice = SamaPrices::firstOrNew($matchData); // Set/update the fields $samaPrice->reference_price = $price; $samaPrice->discount_percentage = 0; $samaPrice->price = $price; if ($samaPrice->save()) { $samaPrice->touch(); Log::channel('imported_products')->info('Successfully saved SamaPrices', [ 'product_id' => $product->id, 'sku' => $input['product_sku'], 'id' => $product->id, 'price' => $price, ]); } else { Log::channel('skipped_products')->warning('Failed to save SamaPrices', [ 'product_id' => $product->id, 'sku' => $input['product_sku'], ]); $stat = false; } } catch (\Exception $e) { Log::channel('skipped_products')->error('Exception while saving SamaPrices', [ 'product_id' => $product->id, 'sku' => $input['product_sku'], 'error' => $e->getMessage(), ]); $stat = false; } } else { Log::channel('skipped_products')->info('Skipped product - not found in database', [ 'sku' => $input['product_sku'] ?? null, 'metalType' => $input['metaltype'] ?? null, 'metalColor' => $input['metalcolor'] ?? null, 'diamondQuality' => $input['diamondquality'] ?? null, 'finishLevel' => $input['finishlevel'] ?? null, ]); continue; } } } $this->importStatus = ['is_updated' => $stat ? 'true' : 'false']; $this->importedData = $collection; } public function getImportedData() { return $this->importedData; } public function getImportStatus() { return $this->importStatus; } }