HEX
Server: Apache/2.4.41 (Ubuntu)
System: Linux ip-172-31-42-149 5.15.0-1084-aws #91~20.04.1-Ubuntu SMP Fri May 2 07:00:04 UTC 2025 aarch64
User: ubuntu (1000)
PHP: 7.4.33
Disabled: pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,
Upload Files
File: /var/www/vhost/disk-apps/demo.sports-crowd.com/app/Http/Controllers/ProductAttributeController.php
<?php

namespace App\Http\Controllers;

use DataTables;
use App\Product;
use App\Parameter;
use App\Attribute;
use App\Core\CorporateIdentity\Application\CorporateIdentityService;
use App\ProductAttribute;
use Illuminate\Http\Request;
use App\Http\Controllers\ShopifyController;
use App\Http\Controllers\WoocommerceController;

class ProductAttributeController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index($product_id)
    {
        $parameter = Parameter::first();

        $corporateIdentity = CorporateIdentityService::get();
        return view('product_attributes.list', compact('product_id', 'corporateIdentity'))
            ->with('parameter', $parameter);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create($product_id)
    {
        $attributes = Attribute::where('active', true)->get();
        return view('product_attributes.create', compact('attributes', 'product_id'));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request, $product_id, $sync = true)
    {
        $attribute = Attribute::where('id', $request->input('attribute_id'))->first();
        $product = Product::select('reference_woocommerce_product_id', 'reference_shopify_id', 'price')->where('id', $product_id)->first();
        $finalPrice = $product->price + floatval($request->input('price_additional'));
        $reference_shopify_variation_id = null;
        $reference_shopify_id = null;
        $reference_id = null;
        $reference_varation_id = null;
        $array_data = [];

        if ($sync) {
            $woo = new WoocommerceController();
            $data = [
                'name' => $request->input('value'),
            ];
            $woo->createAttribute($data);
            $reference_id = $woo->createAttributeTerm($attribute->reference_woocommerce_attribute_id, $data);
            if ($reference_id) {
                $data_pro = [
                    'attributes' => [
                        [
                            'id'        => $attribute->reference_woocommerce_attribute_id,
                            'option'    => $request->input('value')
                        ]
                    ]
                ];
                $reference_varation_id = $woo->createProductVariation($product->reference_woocommerce_product_id, $data_pro);
            }

            $parameters = Parameter::select('sync_shopify', 'sucursal_products')->find(1);
            if ($parameters && $parameters->sync_shopify) {

                $sho = new ShopifyController();
                $data_sho = array("variant" => array(
                    "option1"               => $request->input('value'),
                    "price"                 => $finalPrice,
                    "sku"                   => $request->input('sku'),
                    "inventory_management"  => "shopify",
                ));
                $variant = $sho->createVariant($product->reference_shopify_id, $data_sho, 'COP');
                if ($variant) {
                    $reference_shopify_variation_id = $variant['inventory_item_id'];
                    $reference_shopify_id = $variant['id'];
                    $sho->updateInventoryLevel(
                        $reference_shopify_variation_id,
                        $request->input('available_units'),
                        'TI108',
                        'COP'
                    );
                }
                $array_data = array_merge($request->all(), [
                    'reference_woocommerce_id' => $reference_id,
                    'reference_woocommerce_variation_id' => $reference_varation_id,
                    'reference_shopify_variation_id'     => $reference_shopify_variation_id,
                    'reference_shopify_id'     => $reference_shopify_id,
                ]);
            }
        } else {
            $array_data = $request->all();
        }
        $array_data['pum'] = $array_data['weight'] != '0' ? ROUND($finalPrice / ($array_data['weight'] ?? 1), 2) : null;

        if (ProductAttribute::updateOrCreate(
            ['product_id' => $array_data['product_id'], 'value' => $array_data['value']],
            $array_data
        )) {
            return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.created_successfully'), "data" => null));
        } else {
            return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_creating'), "data" => null));
        }
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($product_id, $product_attribute_id)
    {
        $attributes = Attribute::where('active', true)->get();
        $product_attribute = ProductAttribute::where('id', $product_attribute_id)->first();
        if ($product_attribute) {
            return view('product_attributes.edit', compact('attributes', 'product_attribute', 'product_id'));
        } else {
            return redirect()->back();
        }
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $product_id, $product_attribute_id)
    {
        $attribute = Attribute::where('id', $request->input('attribute_id'))->first();
        $product_attribute = ProductAttribute::where('id', $product_attribute_id)->first();
        $product = Product::select('reference_woocommerce_product_id', 'price')->where('id', $product_id)->first();
        $finalPrice = $product->price + floatval($request->input('price_additional'));

        $woo = new WoocommerceController();
        $data = [
            'name' => $request->input('value'),
        ];
        $woo->updateAttribute($attribute->reference_woocommerce_attribute_id, $data);
        $data_pro = [
            'attributes' => [
                [
                    'id'        => $attribute->reference_woocommerce_attribute_id,
                    'option'    => $request->input('value')
                ]
            ]
        ];
        $woo->updateAttributeTerm($product_attribute->reference_woocommerce_id, $attribute->reference_woocommerce_attribute_id, $data);
        $woo->updateProductVariation($product->reference_woocommerce_product_id, $product_attribute->reference_woocommerce_variation_id, $data_pro);

        $parameters = Parameter::select('sync_shopify', 'sucursal_products')->find(1);
        if ($parameters && $parameters->sync_shopify) {
            $sho = new ShopifyController();
            $data_sho = array("variant" => array(
                "option1"               => $request->input('value'),
                "price"                 => $finalPrice,
                "sku"                   => $request->input('sku'),
            ));
            $sho->updateVariant($product_attribute->reference_shopify_id, $data_sho);
        }

        $array_data = $request->all();
        $array_data['pum'] = $array_data['weight'] != '0' ? ROUND($finalPrice / ($array_data['weight'] ?? 1), 2) : null;

        if (ProductAttribute::where('id', $product_attribute_id)->update(array_slice($array_data, 1))) {
            return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.updated_successfully'), "data" => $array_data));
        } else {
            return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_updating'), "data" => null));
        }
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($product_id, $product_attribute_id)
    {
        $product_attribute = ProductAttribute::where('id', $product_attribute_id)->first();
        $attribute = Attribute::where('id', $product_attribute->attribute_id)->first();
        $product = Product::select('reference_woocommerce_product_id', 'reference_shopify_id')->where('id', $product_id)->first();

        $woo = new WoocommerceController();
        $woo->deleteAttribute($attribute->reference_woocommerce_attribute_id);
        $woo->deleteAttributeTerm($product_attribute->reference_woocommerce_id, $attribute->reference_woocommerce_attribute_id);
        $woo->deleteProductVariation($product->reference_woocommerce_product_id, $product_attribute->reference_woocommerce_variation_id);

        $parameters = Parameter::select('sync_shopify', 'sucursal_products')->find(1);
        if ($parameters && $parameters->sync_shopify) {
            $sho = new ShopifyController();
            $sho->deleteVariant($product_attribute->reference_shopify_id, $product->reference_shopify_id, 'COP');
        }

        if (ProductAttribute::where('id', $product_attribute_id)->delete()) {
            return response(array("r" => true, "type" => "success", "title" => "", "m" => __('messages.updated_successfully'), "data" => null));
        } else {
            return response(array("r" => false, "type" => "error", "title" => "Oops...", "m" => __('messages.error_updating'), "data" => null));
        }
    }

    public function tableFilter($product_id)
    {
        $obj = ProductAttribute::where('product_id', $product_id)->with('product', 'attribute');
        return DataTables::of($obj)
            ->addColumn('actions', function ($obj) {
                return '<i class="fa fa-pencil iconMini" onClick="clickEdit(' . $obj->id . ',' . $obj->product_id . ')" title="Editar"></i>
                    <i class="fa fa-trash iconMini" onClick="clickDelete(' . $obj->id . ',' . $obj->product_id . ')" title="Eliminar"></i>';
            })
            ->rawColumns(['actions'])
            ->make(true);
    }
}