2021-12-06 11:52:58 +00:00
< ? php
namespace Sarga\API\Http\Controllers ;
2021-12-22 15:53:36 +00:00
2022-06-27 09:05:20 +00:00
use Illuminate\Support\Arr ;
use Illuminate\Support\Facades\DB ;
use Illuminate\Support\Facades\Event ;
2022-01-20 07:14:24 +00:00
use Illuminate\Support\Facades\Log ;
2021-12-06 11:52:58 +00:00
use Illuminate\Support\Facades\Storage ;
2021-12-18 20:20:59 +00:00
use Illuminate\Support\Facades\Validator ;
2021-12-06 11:52:58 +00:00
use Webkul\API\Http\Controllers\Shop\Controller ;
use Webkul\Core\Contracts\Validations\Slug ;
2022-02-01 07:33:34 +00:00
use Sarga\Shop\Repositories\ProductRepository ;
2022-01-19 08:32:04 +00:00
use Webkul\Marketplace\Repositories\SellerRepository ;
2022-06-27 09:05:20 +00:00
use Webkul\Product\Repositories\ProductAttributeValueRepository ;
2021-12-06 11:52:58 +00:00
2021-12-10 14:30:26 +00:00
class IntegrationController extends Controller
2021-12-06 11:52:58 +00:00
{
2021-12-18 20:20:59 +00:00
2022-04-12 06:58:43 +00:00
public function __construct ( protected ProductRepository $productRepository ,
2022-06-27 09:05:20 +00:00
protected ProductAttributeValueRepository $attributeValueRepository ,
2022-04-12 08:50:55 +00:00
protected SellerRepository $sellerRepository ){}
2021-12-18 20:20:59 +00:00
2021-12-06 11:52:58 +00:00
public function store (){
if ( ! request () -> has ( 'product' )){
return response () -> json ([ 'status' => false , 'message' => 'bad request' ], 405 );
}
$product = json_decode ( request ( 'product' ), true );
$this -> validate ( $product , [
'sku' => [ 'required' , 'unique:products,sku' , new Slug ],
]);
// $product = $this->productRepository->create(request()-
return $product ;
}
2021-12-18 20:20:59 +00:00
public function create (){
2021-12-22 15:53:36 +00:00
try {
$data = json_decode ( request () -> getContent (), true );
2022-04-14 06:21:40 +00:00
if ( ! $data ){
return response () -> json ([ 'message' => 'data not found' ], 405 );
}
2021-12-22 15:53:36 +00:00
}
catch ( \Exception $e ){
2022-04-07 09:53:59 +00:00
Log :: error ( $e -> getMessage ());
2021-12-22 15:53:36 +00:00
return response () -> json ([ 'errors' => $e -> getMessage ()], 400 );
}
2021-12-18 20:20:59 +00:00
$validation = Validator :: make ( $data , [
2022-01-20 12:25:29 +00:00
'categories' => 'required' ,
2022-01-20 13:36:41 +00:00
// 'sku' => ['required', 'unique:products,sku', new Slug],
2021-12-18 20:20:59 +00:00
'images' => 'required' ,
'name' => 'required' ,
'url_key' => 'required' ,
'price' => 'required' ,
2022-04-07 10:36:31 +00:00
'vendor' => 'required' ,
'weight' => 'required'
2021-12-18 20:20:59 +00:00
]);
if ( $validation -> fails ()) {
2022-06-03 07:14:12 +00:00
// Log::info($data);
2022-01-25 07:25:49 +00:00
2021-12-22 15:53:36 +00:00
return response () -> json ([ 'errors' => $validation -> getMessageBag () -> all ()], 422 );
2021-12-18 20:20:59 +00:00
}
2022-04-12 06:58:43 +00:00
if ( $product = $this -> productRepository -> findOneByField ( 'sku' , $data [ 'product_group_id' ]))
{ //product_group_id
2022-06-28 08:47:46 +00:00
Log :: info ( $data );
2022-06-27 09:05:20 +00:00
$this -> updateVariants ( $product , $data );
return response () -> json ([ 'success' => true , 'product_id' => $product -> id ]);
2022-01-20 13:36:41 +00:00
}
2022-04-12 08:50:55 +00:00
elseif ( $product = $this -> productRepository -> createProduct ( $data )){
2022-01-26 15:28:48 +00:00
2022-01-19 08:32:04 +00:00
return response () -> json ([ 'success' => true , 'product_id' => $product -> id ]);
2022-01-12 07:35:04 +00:00
} else {
2022-04-05 11:46:12 +00:00
2022-01-20 12:13:30 +00:00
return response () -> json ([ 'success' => false ], 400 );
2022-01-12 07:35:04 +00:00
}
2021-12-18 20:20:59 +00:00
}
2022-04-10 12:00:58 +00:00
public function update (){
try {
$data = json_decode ( request () -> getContent (), true );
}
catch ( \Exception $e ){
2022-05-20 10:01:34 +00:00
Log :: info ( $e -> getMessage ());
2022-04-10 12:00:58 +00:00
return response () -> json ([ 'errors' => $e -> getMessage ()], 400 );
}
2022-04-14 06:29:09 +00:00
if ( ! $product = $this -> productRepository -> findOneByField ( 'sku' , $data [ 'product_group_id' ])){
2022-05-20 10:34:02 +00:00
return response () -> json ([ 'success' => false , 'message' => 'product not found' ], 404 );
2022-04-10 12:00:58 +00:00
}
2022-04-12 08:50:55 +00:00
if ( $this -> productRepository -> updateProduct ( $product , $data )){
return response () -> json ([ 'success' => true , 'product_id' => $product -> id ]);
}
2022-04-10 12:00:58 +00:00
}
2022-06-27 09:05:20 +00:00
private function updateVariants ( $product , $data ) {
try {
DB :: beginTransaction ();
if ( $product -> type == 'configurable' ){
if ( ! empty ( $data [ 'color_variants' ])) {
foreach ( $data [ 'color_variants' ] as $colorVariant ) {
$description = implode ( array_map ( fn ( $value ) : string => '<p>' . $value [ 'description' ] . '</p>' , $colorVariant [ 'descriptions' ]));
if ( ! empty ( $colorVariant [ 'size_variants' ]))
foreach ( $colorVariant [ 'size_variants' ] as $sizeVariant ) {
$sku = " { $data [ 'product_group_id' ] } - { $colorVariant [ 'product_number' ] } - { $sizeVariant [ 'itemNumber' ] } " ;
if ( $variant = $this -> productRepository -> findOneByField ( 'sku' , $sku ))
$this -> updateAttribute ( $variant , $sizeVariant );
else {
$variant = $this -> productRepository -> createVariant ( $product , $sku );
$this -> productRepository -> assignImages ( $variant , $colorVariant [ 'images' ]);
$attributes = [
'sku' => $variant -> sku ,
'product_number' => " { $colorVariant [ 'product_number' ] } - { $sizeVariant [ 'itemNumber' ] } " ,
'color' => $this -> productRepository -> getAttributeOptionId ( 'color' , $colorVariant [ 'color' ]),
'name' => $colorVariant [ 'name' ],
'size' => $this -> productRepository -> getAttributeOptionId ( 'size' , $sizeVariant [ 'attributeValue' ]),
// 'price' => $sizeVariant['price'],
'weight' => $colorVariant [ 'weight' ] ? ? 0.45 ,
'status' => 1 ,
'visible_individually' => 1 ,
'url_key' => $variant -> sku ,
'source' => $colorVariant [ 'url_key' ],
'description' => $description ,
'short_description' => $description ,
'favoritesCount' => $colorVariant [ 'favorite_count' ]
];
$this -> productRepository -> assignAttributes ( $variant , array_merge ( $attributes , $this -> productRepository -> calculatePrice ( $sizeVariant [ 'price' ])));
}
}
elseif ( $variant = $this -> productRepository -> findOneByField ( 'sku' , " { $data [ 'product_group_id' ] } - { $colorVariant [ 'product_number' ] } " ))
{
$this -> updateAttribute ( $variant , $colorVariant );
}
else {
$variant = $this -> productRepository -> createVariant ( $product , " { $data [ 'product_group_id' ] } - { $colorVariant [ 'product_number' ] } " );
$this -> productRepository -> assignImages ( $variant , $colorVariant [ 'images' ]);
$desc = implode ( array_map ( fn ( $value ) : string => '<p>' . $value [ 'description' ] . '</p>' , $data [ 'descriptions' ]));
$attributes = [
'sku' => $variant -> sku ,
'product_number' => $colorVariant [ 'product_number' ],
'color' => $this -> productRepository -> getAttributeOptionId ( 'color' , $colorVariant [ 'color' ]),
'name' => $colorVariant [ 'name' ],
// 'price' => Arr::get($colorVariant, 'price.discountedPrice.value'),
'weight' => $colorVariant [ 'weight' ] ? ? 0.45 ,
'status' => 1 ,
'visible_individually' => 1 ,
'url_key' => $variant -> sku ,
'source' => $colorVariant [ 'url_key' ],
'description' => $description ,
'short_description' => $description ,
'favoritesCount' => $colorVariant [ 'favorite_count' ]
];
$this -> assignAttributes ( $variant , array_merge ( $attributes , $this -> productRepository -> calculatePrice ( $colorVariant [ 'price' ])));
}
}
}
elseif ( ! empty ( $data [ 'size_variants' ])){
foreach ( $data [ 'size_variants' ] as $sizeVariant ) {
$sku = " { $data [ 'product_group_id' ] } - { $data [ 'product_number' ] } - { $sizeVariant [ 'itemNumber' ] } " ;
if ( $variant = $this -> productRepository -> findOneByField ( 'sku' , $sku )){
$this -> updateAttribute ( $variant , $sizeVariant );
} else {
$variant = $this -> productRepository -> createVariant ( $product , $sku );
$this -> productRepository -> assignImages ( $variant , $data [ 'images' ]);
$desc = implode ( array_map ( fn ( $value ) : string => '<p>' . $value [ 'description' ] . '</p>' , $data [ 'descriptions' ]));
$attributes = [
'sku' => $variant -> sku ,
'size' => $this -> productRepository -> getAttributeOptionId ( 'size' , $sizeVariant [ 'attributeValue' ]),
'product_number' => " { $data [ 'product_number' ] } - { $sizeVariant [ 'itemNumber' ] } " ,
'name' => $data [ 'name' ],
// 'price' => $sizeVariant['price'],
'weight' => $data [ 'weight' ] ? ? 0.45 ,
'status' => 1 ,
'featured' => 0 ,
'new' => 0 ,
'visible_individually' => 1 ,
'url_key' => $variant -> sku ,
'source' => $data [ 'url_key' ],
'description' => $desc ,
'short_description' => $desc ,
'favoritesCount' => $data [ 'favorite_count' ]
];
if ( ! empty ( $data [ 'color' ])) {
$attributes [ 'color' ] = $this -> productRepository -> getAttributeOptionId ( 'color' , $data [ 'color' ]);
}
$this -> assignAttributes ( $variant , array_merge ( $attributes , $this -> productRepository -> calculatePrice ( $sizeVariant [ 'price' ])));
}
}
}
} else if ( $product -> type == 'simple' ){
$this -> updateAttribute ( $product , $data );
}
Event :: dispatch ( 'catalog.product.update.after' , $product );
DB :: commit ();
}
catch ( \Exception $ex ){
DB :: rollBack ();
Log :: error ( $ex );
// Log::info($data);
return false ;
}
}
private function updateAttribute ( $product , $data ){
2022-06-28 08:44:40 +00:00
2022-06-27 09:05:20 +00:00
if ( isset ( $data [ 'is_sellable' ]) && ! $data [ 'is_sellable' ]){
2022-06-28 08:44:40 +00:00
Log :: info ( $data );
2022-06-27 09:05:20 +00:00
//$attribute = $this->attributeRepository->findOneByField('code', 'status'); status id = 8
$this -> attributeValueRepository -> updateOrCreate ([ 'product_id' => $product -> id , 'attribute_id' => 8 ],[ 'boolean_value' => 0 ]);
2022-06-22 12:08:21 +00:00
2022-06-27 09:05:20 +00:00
} else {
$originalPrice = Arr :: get ( $data , 'price.originalPrice.value' );
$discountedPrice = Arr :: get ( $data , 'price.discountedPrice.value' );
$this -> attributeValueRepository -> updateOrCreate ([ 'product_id' => $product -> id , 'attribute_id' => 8 ],[ 'boolean_value' => 1 ]);
if ( $discountedPrice >= $originalPrice ){
$this -> attributeValueRepository -> updateOrCreate ([ 'product_id' => $product -> id , 'attribute_id' => 11 ],[ 'float_value' => $discountedPrice ]); // price id 11
$this -> attributeValueRepository -> updateOrCreate ([ 'product_id' => $product -> id , 'attribute_id' => 13 ],[ 'float_value' => null ]); //special price id 13
} else {
$this -> attributeValueRepository -> updateOrCreate ([ 'product_id' => $product -> id , 'attribute_id' => 11 ],[ 'float_value' => $originalPrice ]); // price id 11
$this -> attributeValueRepository -> updateOrCreate ([ 'product_id' => $product -> id , 'attribute_id' => 13 ],[ 'float_value' => $discountedPrice ]); //special price id 13
}
}
2022-06-22 12:08:21 +00:00
}
2022-06-27 09:05:20 +00:00
private function createVariant ( $variant ){
}
2022-05-26 10:31:31 +00:00
public function updateOrderStatus (){
Log :: info ( request () -> input ());
2022-05-30 08:52:08 +00:00
//todo update order status,
2022-05-26 10:31:31 +00:00
}
2021-12-06 11:52:58 +00:00
}