<?php
namespace SCart\Core\Admin\Models;
use SCart\Core\Front\Models\ShopStone;
use SCart\Core\Front\Models\ShopProductStore;
use SCart\Core\Front\Models\ShopProductDescription;
use SCart\Core\Front\Models\ShopAttributeGroup;
use SCart\Core\Front\Models\ShopProductCategory;
class AdminStone extends ShopStone
{
/**
* Get product detail in admin
*
* @param [type] $id [$id description]
*
* @return [type] [return description]
*/
public static function getStoneAdmin($id)
{
$tableStone = (new ShopStone)->getTable();
$data = self::where('id', $id);
$data = $data->first();
return $data;
}
/**
* Get list product in admin
*
* @param [array] $dataSearch [$dataSearch description]
*
* @return [type] [return description]
*/
public static function getStoneListAdmin(array $dataSearch, $storeId = null)
{
$keyword = $dataSearch['keyword'] ?? '';
$category_id = $dataSearch['category_id'] ?? '';
$sort_order = $dataSearch['sort_order'] ?? '';
$arrSort = $dataSearch['arrSort'] ?? '';
$tableStone = (new ShopStone)->getTable();
//Select field
$stoneList = (new ShopStone);
$stoneList->groupBy($tableStone.'.id');
if ($sort_order && array_key_exists($sort_order, $arrSort)) {
$field = explode('__', $sort_order)[0];
$sort_field = explode('__', $sort_order)[1];
$stoneList = $stoneList->sort($field, $sort_field);
} else {
$stoneList = $stoneList->sort($tableStone.'.created_at', 'desc');
}
$stoneList = $stoneList->paginate(20);
return $stoneList;
}
/**
* Get list product select in admin
*
* @param array $dataFilter [$dataFilter description]
*
* @return [] [return description]
*/
public function getStoneSelectAdmin(array $dataFilter = [], $storeId = null)
{
$limit = $dataFilter['limit'] ?? '';
$kind = $dataFilter['kind'] ?? [];
$tableStone = $this->getTable();
$colSelect = [
'id',
'sku',
];
$stoneList = (new ShopStone)->select($colSelect);
if (is_array($kind) && $kind) {
$stoneList = $stoneList->whereIn('kind', $kind);
}
if ($limit) {
$stoneList = $stoneList->limit($limit);
}
$stoneList->groupBy($tableStone.'.id');
$dataTmp = $stoneList->get()->keyBy('id');
$data = [];
foreach ($dataTmp as $key => $row) {
$data[$key] = [
'id' => $row['id'],
'sku' => $row['sku'],
'name' => addslashes($row['name']),
];
}
return $data;
}
/**
* Create a new product
*
* @param array $dataInsert [$dataInsert description]
*
* @return [type] [return description]
*/
public static function createStoneAdmin(array $dataInsert)
{
return self::create($dataInsert);
}
/**
* Insert data description
*
* @param array $dataInsert [$dataInsert description]
*
* @return [type] [return description]
*/
public static function insertDescriptionAdmin(array $dataInsert)
{
return ShopProductDescription::create($dataInsert);
}
/**
* Validate product
*
* @param [type]$type [$type description]
* @param null $fieldValue [$field description]
* @param null $pId [$pId description]
* @param null $storeId [$storeId description]
* @param null [ description]
*
* @return [type] [return description]
*/
public function checkProductValidationAdmin($type = null, $fieldValue = null, $pId = null, $storeId = null)
{
$tableProductStore = (new ShopProductStore)->getTable();
$storeId = $storeId ? sc_clean($storeId) : session('adminStoreId');
$type = $type ? sc_clean($type) : 'sku';
$fieldValue = sc_clean($fieldValue);
$pId = sc_clean($pId);
$check = $this
->leftJoin($tableProductStore, $tableProductStore . '.product_id', $this->getTable() . '.id')
->where($type, $fieldValue);
$check = $check->where($tableProductStore . '.store_id', $storeId);
if ($pId) {
$check = $check->where($this->getTable().'.id', '<>', $pId);
}
$check = $check->first();
if ($check) {
return false;
} else {
return true;
}
}
/**
* Get total product of system
*
* @return [type] [return description]
*/
public static function getTotalProduct()
{
return self::count();
}
/**
* Render html option price in admin
*
* @param [type]$currency [$currency description]
* @param nul $rate [$rate description]
* @param null [ description]
*
* @return [type] [return description]
*/
public function renderAttributeDetailsAdmin($currency = null, $rate = null)
{
$html = '';
$details = $this->attributes()->get()->groupBy('attribute_group_id');
$groups = ShopAttributeGroup::getListAll();
foreach ($details as $groupId => $detailsGroup) {
$html .= '<br><b><label>' . $groups[$groupId] . '</label></b>: ';
foreach ($detailsGroup as $k => $detail) {
$valueOption = $detail->name.'__'.$detail->add_price;
$html .= '<label class="radio-inline"><input ' . (($k == 0) ? "checked" : "") . ' type="radio" name="add_att[' . $this->id . '][' . $groupId . ']" value="' . $valueOption . '">' . sc_render_option_price($valueOption, $currency, $rate) . '</label> ';
}
}
return $html;
}
/**
* Get list category id from product id
*
* @param [array] $arrProductId
* @return collection
*/
public function getListCategoryIdFromProductId($arrProductId)
{
return (new ShopProductCategory)->whereIn('product_id', $arrProductId)->get()->groupBy('product_id');
}
}
|