Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Update] Import Update #2712

Draft
wants to merge 10 commits into
base: development
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ public function handle()
$productData['productType'] = $this->mapProductType($product);
}
$productsToExport[] = $productData;
$this->newLine();
$this->info('Processing Product with slug ' . $product->slug);
$this->newLine();
}

$chunks = array_chunk($productsToExport, 10);
Expand Down Expand Up @@ -161,9 +164,9 @@ public function mapWarehouses(Variants $variant): array
foreach ($variant->warehouses as $warehouse) {
$warehouses[] = [
'id' => $this->warehouseId,
'quantity' => $warehouse->quantity,
'quantity' => $variant->getQuantity($warehouse),
'status' => $warehouse->status !== null ? $this->mapStatus($warehouse->status) : null,
'price' => $warehouse->price ?? 0,
'price' => $variant->getPrice($warehouse),
'sku' => $warehouse->sku,
'position' => $warehouse->position,
'serial_number' => $warehouse->serial_number,
Expand Down
10 changes: 10 additions & 0 deletions app/Console/Commands/Setup/KanvasInventoryDefaultUpdate.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,16 @@ public function handle()
}
}
}

$variants = Variants::whereDoesntHave('status')
->where('companies_id', $companyData->getId())
->get();

foreach ($variants as $variant) {
$this->info("Working variant {$variant->getId()} status assignment \n");
$variant->status_id = $defaultStatus->getId();
$variant->saveQuietly();
}
}

return;
Expand Down
4 changes: 4 additions & 0 deletions app/GraphQL/Inventory/Mutations/Variants/Variants.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use Kanvas\Inventory\Attributes\Repositories\AttributesRepository;
use Kanvas\Inventory\Channels\Repositories\ChannelRepository;
use Kanvas\Inventory\Channels\Services\ChannelService;
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 Down Expand Up @@ -38,6 +39,9 @@ public function create(mixed $root, array $req): VariantModel
(int) $req['input']['status']['id'],
auth()->user()->getCurrentCompany()
)->getId();
} else {
$status = Status::getDefault(auth()->user()->getCurrentCompany());
$req['input']['status_id'] = $status->getId();
}

$variantDto = VariantDto::viaRequest($req['input'], auth()->user());
Expand Down
6 changes: 1 addition & 5 deletions graphql/schemas/Inventory/importer.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,18 @@
input ImporterProductInput {
name: String!
description: String
regionId: Int
slug: String!
price: Float!
discountPrice: Float
sku: String!
variants: [VariantsInput!]!
quantity: Int!
short_description: String
html_description: String
warranty_terms: String
files: [FilesystemInputUrl!]
upc: String
isPublished: Boolean
warehouses: [Int!]
source: String
sourceId: Mixed
status: StatusInput
productType: ProductTypeInput
categories: [CategoryInput!]
attributes: [ProductAttributesInput!]
Expand Down
235 changes: 9 additions & 226 deletions src/Domains/Inventory/Importer/Actions/ProductImporterAction.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Kanvas\Inventory\Importer\Actions;

use Baka\Contracts\AppInterface;
use Baka\Support\Str;
use Baka\Users\Contracts\UserInterface;
use Illuminate\Support\Facades\DB;
use Kanvas\Apps\Models\Apps;
Expand All @@ -29,18 +30,6 @@
use Kanvas\Inventory\Status\Actions\CreateStatusAction;
use Kanvas\Inventory\Status\DataTransferObject\Status;
use Kanvas\Inventory\Status\Models\Status as ModelsStatus;
use Kanvas\Inventory\Variants\Actions\AddAttributeAction as ActionsAddAttributeAction;
use Kanvas\Inventory\Variants\Actions\AddToWarehouseAction;
use Kanvas\Inventory\Variants\Actions\AddVariantToChannelAction;
use Kanvas\Inventory\Variants\Actions\CreateVariantsAction;
use Kanvas\Inventory\Variants\DataTransferObject\VariantChannel;
use Kanvas\Inventory\Variants\DataTransferObject\Variants as VariantsDto;
use Kanvas\Inventory\Variants\DataTransferObject\VariantsWarehouses;
use Kanvas\Inventory\Variants\Models\Variants as VariantsModel;
use Kanvas\Inventory\Variants\Models\VariantsWarehouses as ModelsVariantsWarehouses;
use Kanvas\Inventory\Variants\Services\VariantService;
use Kanvas\Inventory\Warehouses\Actions\CreateWarehouseAction;
use Kanvas\Inventory\Warehouses\DataTransferObject\Warehouses;
use Kanvas\Workflow\Enums\WorkflowEnum;
use Throwable;

Expand Down Expand Up @@ -101,12 +90,6 @@ public function execute(): ProductsModel

$this->categories();

$this->productWarehouse();

//$this->variants();
// @todo to be removed
//$this->variantsLocation($this->product);

if (! empty($this->importedProduct->productType)) {
$this->productType();
}
Expand All @@ -121,23 +104,23 @@ public function execute(): ProductsModel
return $this->product;
}

protected function createStatus(): ?ModelsStatus
protected function createStatus(): ModelsStatus
{
if ($this->importedProduct->status) {
$createStatus = new CreateStatusAction(
new Status(
$this->app,
$this->company,
$this->user,
$this->importedProduct->status,
$this->importedProduct->status['name'],
),
$this->user
);

return $createStatus->execute();
} else {
return ModelsStatus::getDefault($this->company);
}

return null;
}

/**
Expand All @@ -147,13 +130,14 @@ protected function productType(): void
{
$productType = null;

if (isset($this->importedProduct->productType['source_id'])) {
if (isset($this->importedProduct->productType)) {
$productType = ProductsTypesModel::getByCustomField(
$this->importedProduct->getSourceKey(),
$this->importedProduct->productType['source_id'],
'slug',
Str::slug($this->importedProduct->productType['name']),
$this->company
);
}

if ($productType) {
$this->product->update(['products_types_id' => $productType->id]);
} else {
Expand All @@ -167,13 +151,6 @@ protected function productType(): void

$productType = (new CreateProductTypeAction($productTypeDto, $this->user))->execute();

if (isset($this->importedProduct->productType['source_id']) && $this->importedProduct->isFromThirdParty()) {
$productType->setLinkedSource(
$this->importedProduct->source,
$this->importedProduct->productType['source_id']
);
}

$this->product->update(['products_types_id' => $productType->id]);
}
}
Expand Down Expand Up @@ -250,198 +227,4 @@ public function attributes(): void
}
}
}

public function productWarehouse(): void
{
foreach ($this->importedProduct->warehouses as $warehouseLocation) {
$warehouseData = Warehouses::from([
'company' => $this->company,
'user' => $this->user,
'app' => $this->app,
'region' => $this->region,
'regions_id' => $this->region->getId(),
'name' => $warehouseLocation['warehouse'],
]);

$warehouse = (new CreateWarehouseAction($warehouseData, $this->user))->execute();

$this->product->warehouses()->syncWithoutDetaching([$warehouse->getId()]);
}
}

/**
* variants.
*/
public function variants(): void
{
foreach ($this->importedProduct->variants as $variant) {
$variantModel = null;

if (isset($variant['source_id'])) {
$variantModel = VariantsModel::getByCustomField(
$this->importedProduct->getSourceKey(),
$variant['source_id'],
$this->company
);
}

if ($variantModel) {
$this->product->variants()->save($variantModel);
} else {
$variantDto = VariantsDto::from([
'product' => $this->product,
'products_id' => $this->product->getId(),
'warehouse_id' => (int) $variant['warehouse']['id'],
...$variant,
]);

$variantModel = (new CreateVariantsAction($variantDto, $this->user))->execute();
if (isset($variant['source_id']) && $this->importedProduct->isFromThirdParty()) {
$variantModel->setLinkedSource($this->importedProduct->source, $variant['source_id']);
}
}

/* if (! empty($variant['files'])) {
foreach ($variant['files'] as $file) {
$variantModel->addFileFromUrl($file['url'], $file['name']);
}
}

$this->variantsAttributes($variantModel, $variant); */

$this->addVariantsToLocation($variantModel);
}
}

public function variantsLocation(ProductsModel $product): void
{
if ($product->variants()->count() > 0) {
foreach ($product->variants as $variant) {
$this->addVariantsToLocation($variant);
}
}
}

public function variantsAttributes(VariantsModel $variantModel, array $variantData): void
{
if (isset($variantData['attributes']) && ! empty($variantData['attributes'])) {
foreach ($variantData['attributes'] as $attribute) {
$attributeModel = null;
if (isset($attribute['source_id'])) {
$attributeModel = Attributes::getByCustomField(
$this->importedProduct->getSourceKey(),
$attribute['source_id'],
$this->company
);
}

if (! $attributeModel && ! empty($attribute['name']) && ! empty($attribute['value'])) {
$attributesDto = AttributesDto::from([
'app' => $this->app,
'user' => $this->user,
'company' => $this->company,
'name' => $attribute['name'],
'value' => $attribute['value'],
]);
$attributeModel = (new CreateAttribute($attributesDto, $this->user))->execute();

if (isset($attribute['source_id']) && $this->importedProduct->isFromThirdParty()) {
$attributeModel->setLinkedSource($this->importedProduct->source, $attribute['source_id']);
}
}

if ($attributeModel instanceof Attributes && ! empty($attribute['value'])) {
(new ActionsAddAttributeAction($variantModel, $attributeModel, $attribute['value']))->execute();
}
}
}
}

/**
* Add variant to warehouse and channels.
*/
public function addVariantsToLocation(VariantsModel $variantModel): void
{
//add to warehouse
foreach ($this->importedProduct->warehouses as $warehouseLocation) {
$warehouseData = Warehouses::from([
'company' => $this->company,
'user' => $this->user,
'app' => $this->app,
'region' => $this->region,
'regions_id' => $this->region->getId(),
'name' => $warehouseLocation['warehouse'],
]);

$warehouse = (new CreateWarehouseAction($warehouseData, $this->user))->execute();

$channelData = Channels::from([
'app' => $this->app,
'user' => $this->user,
'company' => $this->company,
'name' => $warehouseLocation['channel'],
]);

$channel = (new CreateChannel($channelData, $this->user))->execute();

$matchingVariantInfo = array_filter($this->importedProduct->variants, function ($variant) use ($variantModel) {
return $variant['sku'] === $variantModel->sku;
});

if (! empty($matchingVariantInfo)) {
// Since array_filter preserves keys, use array_values to reset them
$variantData = current($matchingVariantInfo);

if (! empty($variantData['warehouse'])) {
$variantData = [
'quantity' => $variantData['warehouse']['quantity'] ?? ($variantData['quantity'] ?? 1),
'price' => $variantData['warehouse']['price'] ?? $variantData['price'],
'discountPrice' => $variantData['warehouse']['discountPrice'] ?? ($variantData['discountPrice'] ?? 0),
];
}
} else {
$variantData = [
'quantity' => $this->importedProduct->quantity,
'price' => $this->importedProduct->price,
'discountPrice' => $this->importedProduct->discountPrice,
];
}

$variantChannel = VariantChannel::from([
'price' => (float) $variantData['price'],
'discounted_price' => (float) $variantData['discountPrice'],
'is_published' => $this->importedProduct->isPublished,
]);

$variantWarehouses = ModelsVariantsWarehouses::where('products_variants_id', $variantModel->getId())
->where('warehouses_id', $warehouse->getId())
->first();

if (! $variantWarehouses) {
$variantWarehouses = (new AddToWarehouseAction(
$variantModel,
$warehouse,
VariantsWarehouses::from([
'variant' => $variantModel,
'warehouse' => $warehouse,
'quantity' => $variantData['quantity'] ?? 1,
'price' => $variantData['price'],
'sku' => $variantModel->sku,
]),
))->execute();
} else {
VariantService::updateWarehouseVariant($variantModel, $warehouse, [
'quantity' => $variantData['quantity'] ?? 1,
'price' => $variantData['price'],
'sku' => $variantModel->sku,
]);
}

(new AddVariantToChannelAction(
$variantWarehouses,
$channel,
$variantChannel
))->execute();
}
}
}
Loading
Loading