Skip to content

Commit

Permalink
created resources of Warehouse dependecies and generated createRecord…
Browse files Browse the repository at this point in the history
… and updateRecord function
  • Loading branch information
TemuulenBM committed Nov 30, 2023
1 parent 97614d2 commit 017da90
Show file tree
Hide file tree
Showing 21 changed files with 378 additions and 72 deletions.
4 changes: 2 additions & 2 deletions addon/components/cell/product-info.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ import Component from '@glimmer/component';
import { action, computed, get } from '@ember/object';

export default class CellProductInfoComponent extends Component {
@computed('args.{column.modelPath,row}') get product() {
@computed('args.row', 'args.column.{modelPath}') get product() {
const { column, row } = this.args;

if (typeof column?.modelPath === 'string') {
return get(row, column.modelPath);
}

return row;
return row
}

@action onClick(event) {
Expand Down
17 changes: 1 addition & 16 deletions addon/components/inventory-form-panel.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -46,21 +46,6 @@
{{model.name}}
</ModelSelect>
</InputGroup>
<InputGroup @name="Supplier">
<ModelSelect
@modelName="vendor"
@selectedModel={{this.inventory.supplier}}
@placeholder="Select Supplier"
@triggerClass="form-select form-input"
@infiniteScroll={{false}}
@renderInPlace={{true}}
@onChange={{fn (mut this.inventory.supplier)}}
@onChangeId={{fn (mut this.inventory.supplier_uuid)}}
as |model|
>
{{model.name}}
</ModelSelect>
</InputGroup>
<InputGroup @name="Warehouse">
<ModelSelect
@modelName="warehouse"
Expand Down Expand Up @@ -112,7 +97,7 @@
<InputGroup @name="Batch Quantity" @type="number" @value={{this.inventory.batch.quantity}} />
<InputGroup @name="Batch Number" @type="number" @value={{this.inventory.batch.batch_number}} />
<InputGroup @name="Batch Reason">
<Textarea @value={{this.batch.reason}} aria-label="Reason" class="w-full form-input" placeholder="Reason" rows={{5}} />
<Textarea @value={{this.inventory.batch.reason}} aria-label="Reason" class="w-full form-input" placeholder="Reason" rows={{5}} />
</InputGroup>
</ContentPanel>
</div>
Expand Down
74 changes: 31 additions & 43 deletions addon/components/warehouse-editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,66 +23,54 @@ export default class WarehouseEditorComponent extends Component {
this.warehouse = this.args.warehouse;
}

@action removeSection(section) {
section.destroyRecord();
}

@action removeAisle(aisle) {
aisle.destroyRecord();
}

@action removeRack(rack) {
rack.destroyRecord();
}

@action removeBin(bin) {
bin.destroyRecord();
}

@action addSection() {
const section = this.store.createRecord('warehouse-section', { warehouse_uuid: this.warehouse.id });
this.warehouse.sections.pushObject(section);
}

@action addAisle(section) {
if (section && section.uuid) {
const aisle = this.store.createRecord('warehouse-aisle', {
section: section,
});

if (!section.aisles) {
section.set('aisles', []);
}
const aisle = this.store.createRecord('warehouse-aisle', { section: section });

section.aisles.pushObject(aisle);
if (!section.aisles) {
section.set('aisles', []);
}

section.aisles.pushObject(aisle);
}

@action addRacks(aisle) {
if (aisle && aisle.uuid) {
const rack = this.store.createRecord('warehouse-rack', {
aisle: aisle,
});

if (!aisle.racks) {
aisle.set('racks', []);
}
const rack = this.store.createRecord('warehouse-rack', { aisle: aisle });

aisle.racks.pushObject(rack);
if (!aisle.racks) {
aisle.set('racks', []);
}

aisle.racks.pushObject(rack);
}

@action addBins(rack) {
if (rack && rack.uuid) {
const bin = this.store.createRecord('warehouse-bin', {
rack: rack,
});

if (!rack.bins) {
rack.set('bins', []);
}
const bin = this.store.createRecord('warehouse-bin', { rack: rack });

rack.bins.pushObject(bin);
if (!rack.bins) {
rack.set('bins', []);
}

rack.bins.pushObject(bin);
}

@action removeSection(section) {
section.destroyRecord();
}

@action removeAisle(aisle) {
aisle.destroyRecord();
}

@action removeRack(rack) {
rack.destroyRecord();
}

@action removeBin(bin) {
bin.destroyRecord();
}
}
4 changes: 0 additions & 4 deletions addon/components/warehouse-form-panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,6 @@ export default class WarehouseFormPanelComponent extends Component {
@action save() {
const { warehouse } = this;

// if (warehouse.sections) {
// all(warehouse.sections.map((section) => section.save()));
// }

this.loader.showLoader('.next-content-overlay-panel-container', { loadingMessage: 'Saving place...', preserveTargetPosition: true });
this.isLoading = true;

Expand Down
12 changes: 10 additions & 2 deletions addon/controllers/inventory/index/new.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ export default class InventoryIndexNewController extends Controller {
*
* @var {InventoryModel}
*/
@tracked inventory = this.store.createRecord('inventory');
@tracked inventory = this.store.createRecord('inventory', {
type: 'pallet-inventory',
meta: {},
batches: [this.store.createRecord('batch')],
});

/**
* Set the overlay component context object.
Expand Down Expand Up @@ -83,6 +87,10 @@ export default class InventoryIndexNewController extends Controller {
* @memberof InventoryIndexNewController
*/
resetForm() {
this.inventory = this.store.createRecord('inventory');
this.inventory = this.store.createRecord('inventory', {
type: 'pallet-inventory',
meta: {},
batches: [this.store.createRecord('batch')],
});
}
}
2 changes: 2 additions & 0 deletions addon/controllers/warehouses/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ export default class WarehousesIndexController extends Controller {
* @var {String}
*/
@tracked neighborhood;

@tracked warehouse;

/**
* All columns applicable for orders
Expand Down
2 changes: 1 addition & 1 deletion addon/routes/inventory/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,6 @@ export default class InventoryIndexRoute extends Route {
};

model(params) {
return this.store.query('inventory', { ...params, with: ['product'] });
return this.store.query('inventory', { ...params, with: ['product', 'warehouse', 'batch'] });
}
}
1 change: 1 addition & 0 deletions addon/serializers/inventory.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export default class InventorySerializer extends ApplicationSerializer.extend(Em
return {
product: { embedded: 'always' },
warehouse: { embedded: 'always' },
batch: { embedded: 'always' },
};
}
}
3 changes: 0 additions & 3 deletions addon/serializers/warehouse.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ export default class WarehouseSerializer extends ApplicationSerializer.extend(Em
get attrs() {
return {
sections: { embedded: 'always' },
aisles: { embedded: 'always' },
racks: { embedded: 'always' },
bins: { embedded: 'always' },
};
}
}
27 changes: 27 additions & 0 deletions server/src/Http/Controllers/InventoryController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@

namespace Fleetbase\Pallet\Http\Controllers;

use Fleetbase\Exceptions\FleetbaseRequestValidationException;
use Fleetbase\Pallet\Http\Resources\IndexInventory;
use Fleetbase\Pallet\Models\Batch;
use Fleetbase\Pallet\Models\Inventory;
use Illuminate\Http\Request;
use Fleetbase\Support\Http;
use Illuminate\Database\QueryException;

class InventoryController extends PalletResourceController
{
Expand All @@ -24,4 +28,27 @@ public function queryRecord(Request $request)

return IndexInventory::collection($data);
}

public function createRecord(Request $request)
{
try {
$this->validateRequest($request);
$record = $this->model->createRecordFromRequest($request, null, function ($request, $inventory) {
$batch = $request->array('inventory.batch', []);
Batch::create(['uuid' => data_get($batch, 'uuid')], array_merge($batch, ['batch_number' =>('batch_number') ,'company_uuid' => session('company'), 'created_by_uuid' => session('user')]));
});
if (Http::isInternalRequest($request)) {
$this->resource::wrap($this->resourceSingularlName);

return new $this->resource($record);
}
return new $this->resource($record);
} catch (\Exception $e) {
return response()->error($e->getMessage());
} catch (QueryException $e) {
return response()->error($e->getMessage());
} catch (FleetbaseRequestValidationException $e) {
return response()->error($e->getErrors());
}
}
}
60 changes: 59 additions & 1 deletion server/src/Http/Controllers/WarehouseController.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,64 @@ class WarehouseController extends PalletResourceController
*/
public $resource = 'warehouse';

public function createRecord(Request $request)
{
try {
$this->validateRequest($request);
$record = $this->model->createRecordFromRequest($request, null, function ($request, $warehouse) {
$sections = $request->array('warehouse.sections', []);

foreach ($sections as $section) {
$createdSection = WarehouseSection::create(array_merge($section, [
'warehouse_uuid' => data_get($warehouse, 'uuid'),
'company_uuid' => session('company'),
'created_by_uuid' => session('user')
]));

$aisles = data_get($section, 'aisles', []);
foreach ($aisles as $aisle) {
$createdAisle = WarehouseAisle::create(array_merge($aisle, [
'section_uuid' => $createdSection->uuid,
'company_uuid' => session('company'),
'created_by_uuid' => session('user')
]));

$racks = data_get($aisle, 'racks', []);
foreach ($racks as $rack) {
$createdRack = WarehouseRack::create(array_merge($rack, [
'aisle_uuid' => $createdAisle->uuid,
'company_uuid' => session('company'),
'created_by_uuid' => session('user')
]));

$bins = data_get($rack, 'bins', []);
foreach ($bins as $bin) {
WarehouseBin::create(array_merge($bin, [
'rack_uuid' => $createdRack->uuid,
'company_uuid' => session('company'),
'created_by_uuid' => session('user')
]));
}
}
}
}
});

if (Http::isInternalRequest($request)) {
$this->resource::wrap($this->resourceSingularlName);
return new $this->resource($record);
}

return new $this->resource($record);
} catch (\Exception $e) {
return response()->error($e->getMessage());
} catch (QueryException $e) {
return response()->error($e->getMessage());
} catch (FleetbaseRequestValidationException $e) {
return response()->error($e->getErrors());
}
}

public function updateRecord(Request $request, string $id)
{
try {
Expand All @@ -33,7 +91,7 @@ public function updateRecord(Request $request, string $id)
$aisles = data_get($section, 'aisles', []);
foreach ($aisles as $aisle) {
WarehouseAisle::updateOrCreate(['uuid' => data_get($aisle, 'uuid')], array_merge($aisle, ['section_uuid' => data_get($section, 'uuid'), 'company_uuid' => session('company'), 'created_by_uuid' => session('user')]));

$racks = data_get($aisle, 'racks', []);
foreach ($racks as $rack) {
WarehouseRack::updateOrCreate(['uuid' => data_get($rack, 'uuid')], array_merge($rack, ['aisle_uuid' => data_get($aisle, 'uuid'), 'company_uuid' => session('company'), 'created_by_uuid' => session('user')]));
Expand Down
31 changes: 31 additions & 0 deletions server/src/Http/Resources/Batch.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Fleetbase\Pallet\Http\Resources;

use Fleetbase\Http\Resources\FleetbaseResource;
use Fleetbase\Pallet\Models\WarehouseRack;
use Fleetbase\Support\Http;

class Batch extends FleetbaseResource
{
/**
* Transform the resource into an array.
*
* @param \Illuminate\Http\Request $request
*
* @return array|\Illuminate\Contracts\Support\Arrayable|\JsonSerializable
*/
public function toArray($request)
{
return [
'id' => $this->when(Http::isInternalRequest(), $this->id, $this->public_id),
'uuid' => $this->when(Http::isInternalRequest(), $this->uuid),
'public_id' => $this->when(Http::isInternalRequest(), $this->public_id),
'batch_number' => $this->batch_number,
'batch_quantity' => $this->batch_quantity,
'racks' => WarehouseRack::collection($this->racks),
'updated_at' => $this->updated_at,
'created_at' => $this->created_at,
];
}
}
1 change: 1 addition & 0 deletions server/src/Http/Resources/IndexInventory.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ public function toArray($request)
'public_id' => $this->latest_public_id,
'product_uuid' => $this->product_uuid,
'product' => $this->whenLoaded('product', $this->product),
'batch' => $this->whenLoaded('batch', $this->batch),
'quantity' => (int) $this->total_quantity,
'comments' => $this->latest_comments,
'updated_at' => $this->latest_updated_at,
Expand Down
Loading

0 comments on commit 017da90

Please sign in to comment.