Skip to content

Commit

Permalink
Merge pull request #1358 from bakaphp/development
Browse files Browse the repository at this point in the history
Release - Version 1.33
  • Loading branch information
kaioken committed May 26, 2024
2 parents a3f1dc4 + 6f2ccc5 commit 037003f
Show file tree
Hide file tree
Showing 34 changed files with 672 additions and 379 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Console\Commands\Shopify;
namespace App\Console\Commands\Connectors\Shopify;

use Illuminate\Console\Command;
use Kanvas\Apps\Models\Apps;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,16 @@

declare(strict_types=1);

namespace App\Console\Commands\Shopify;
namespace App\Console\Commands\Connectors\Shopify;

use Illuminate\Console\Command;
use Kanvas\Apps\Models\Apps;
use Kanvas\Companies\Models\Companies;
use Kanvas\Connectors\Shopify\Enums\StatusEnum;
use Kanvas\Connectors\Shopify\Services\ShopifyInventoryService;
use Kanvas\Inventory\Channels\Models\Channels;
use Kanvas\Inventory\Products\Models\Products;
use Kanvas\Inventory\Warehouses\Models\Warehouses;
use Kanvas\Users\Models\UserCompanyApps;

class ShopifyInventorySyncCommand extends Command
Expand All @@ -19,7 +21,7 @@ class ShopifyInventorySyncCommand extends Command
*
* @var string
*/
protected $signature = 'kanvas:inventory-shopify-sync {app_id} {company_id}';
protected $signature = 'kanvas:inventory-shopify-sync {app_id} {company_id} {warehouse_id} {channel_id}';

/**
* The console command description.
Expand All @@ -37,26 +39,24 @@ public function handle()
{
$app = Apps::getById((int) $this->argument('app_id'));
$company = Companies::getById((int) $this->argument('company_id'));
$channel = Channels::getByIdFromCompany((int) $this->argument('channel_id'), $company);
$warehouses = Warehouses::getByIdFromCompany((int) $this->argument('warehouse_id'), $company);

$associatedApps = UserCompanyApps::where('apps_id', $app->getId())
->where('companies_id', $company->getId())->first();
->where('companies_id', $company->getId())->first();

$companyData = $associatedApps->company;
$this->info("Checking company {$companyData->getId()} \n");

$products = Products::where('companies_id', $companyData->getId())
->where('apps_id', $app->getId())
->get();
->where('apps_id', $app->getId())
->get();

foreach ($products as $product) {
$this->info("Checking product {$product->getId()} {$product->name} \n");

foreach ($product->variants as $variant) {
$variant->warehouses->map(function ($warehouses) use ($variant) {
$shopifyService = new ShopifyInventoryService($variant->app, $variant->company, $warehouses);
$shopifyService->saveProduct($variant->product, StatusEnum::ACTIVE);
});
}
$shopifyService = new ShopifyInventoryService($product->app, $product->company, $warehouses);
$shopifyService->saveProduct($product, StatusEnum::ACTIVE, $channel);
}
return;
}
Expand Down
2 changes: 2 additions & 0 deletions app/Console/Commands/KanvasInventoryDefaultUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ public function handle()
foreach ($variants as $variant) {
$this->info("Working variant {$variant->getId()} warehouse assignment \n");
$variantWarehouseDto = DataTransferObjectVariantsWarehouses::viaRequest(
$variant,
$defaultWarehouses,
[
'status_id' => $defaultStatus->getId()
]
Expand Down
47 changes: 25 additions & 22 deletions app/GraphQL/Inventory/Mutations/Variants/Variants.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
use Kanvas\Inventory\Attributes\Repositories\AttributesRepository;
use Kanvas\Inventory\Channels\Models\Channels;
use Kanvas\Inventory\Channels\Repositories\ChannelRepository;
use Kanvas\Inventory\Status\Models\Status;
use Kanvas\Inventory\Status\Repositories\StatusRepository;
use Kanvas\Inventory\Variants\Actions\AddAttributeAction;
use Kanvas\Inventory\Variants\Actions\AddToWarehouseAction as AddToWarehouse;
Expand All @@ -23,6 +22,7 @@
use Kanvas\Inventory\Variants\Services\VariantService;
use Kanvas\Inventory\Warehouses\Models\Warehouses;
use Kanvas\Inventory\Warehouses\Repositories\WarehouseRepository;
use Kanvas\Inventory\Warehouses\Services\WarehouseService;

class Variants
{
Expand All @@ -46,29 +46,34 @@ public function create(mixed $root, array $req): VariantModel
if (isset($req['input']['attributes'])) {
$variantModel->addAttributes(auth()->user(), $req['input']['attributes']);
}
if (! $variantDto->warehouse_id) {
$variantDto->warehouse_id = Warehouses::getDefault($company)->getId();
}
$warehouse = WarehouseRepository::getById($variantDto->warehouse_id, $company);

if (isset($req['input']['warehouse']['status'])) {
$status = StatusRepository::getById(
(int) $req['input']['warehouse']['status']['id'],
$company
)->getId();
if (isset($req['input']['warehouses'])) {
foreach ($req['input']['warehouses'] as $warehouseData) {
$warehouse = WarehouseRepository::getById((int) $warehouseData['id'], $company);

WarehouseService::addToWarehouses(
$variantModel,
$warehouse,
$company,
$warehouseData
);
}
} else {
$status = Status::getDefault($company);
$warehouse = Warehouses::getDefault($company);

WarehouseService::addToWarehouses(
$variantModel,
$warehouse,
$company,
[]
);
}
$req['input']['warehouse']['status_id'] = $status ? $status->getId() : null;

if (! empty($variantDto->files)) {
foreach ($variantDto->files as $file) {
$variantModel->addFileFromUrl($file['url'], $file['name']);
}
}
$variantWarehouses = VariantsWarehouses::viaRequest($req['input']['warehouse'] ?? []);

(new AddToWarehouse($variantModel, $warehouse, $variantWarehouses))->execute();

if (isset($req['input']['channels'])) {
foreach ($req['input']['channels'] as $variantChannel) {
Expand Down Expand Up @@ -104,9 +109,8 @@ public function update(mixed $root, array $req): VariantModel
$variant->addAttributes(auth()->user(), $req['input']['attributes']);
}

if (isset($req['input']['warehouse'])) {
$warehouse = WarehouseRepository::getById((int) $req['input']['warehouse']['warehouse_id'], $company);
VariantService::updateWarehouseVariant($variant, $warehouse, $req['input']['warehouse']);
if (isset($req['input']['warehouses'])) {
WarehouseService::updateWarehouseVariant($variant, auth()->user(), $req['input']['warehouses']);
}

return $variant;
Expand Down Expand Up @@ -134,7 +138,7 @@ public function addToWarehouse(mixed $root, array $req): VariantModel
if (isset($req['input']['status'])) {
$req['input']['status_id'] = StatusRepository::getById((int) $req['input']['status']['id'], $company)->getId();
}
$variantWarehouses = VariantsWarehouses::viaRequest($req['input']);
$variantWarehouses = VariantsWarehouses::viaRequest($variant, $warehouse, $req['input']);

(new AddToWarehouse($variant, $warehouse, $variantWarehouses))->execute();

Expand All @@ -155,17 +159,16 @@ public function updateVariantInWarehouse(mixed $root, array $req): VariantModel
}

/**
* @todo Remove and use softdelete.
* removeToWarehouse.
*/
public function removeToWarehouse(mixed $root, array $req): VariantModel
{
$company = auth()->user()->getCurrentCompany();

$variant = VariantsRepository::getById((int) $req['id'], $company);

$warehouse = WarehouseRepository::getById($req['warehouse_id'], $company);
$variant->warehouses()->detach($warehouse);

WarehouseService::removeVariantWarehouses($variant, $warehouse, auth()->user());

return $variant;
}
Expand Down
Loading

0 comments on commit 037003f

Please sign in to comment.