Skip to content

Commit

Permalink
review SendOrderToWarehouse (fix tests)
Browse files Browse the repository at this point in the history
  • Loading branch information
inikoo committed Sep 12, 2024
1 parent 6e04776 commit 6ae2f27
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 31 deletions.
27 changes: 13 additions & 14 deletions app/Actions/Ordering/Order/SendOrderToWarehouse.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,9 @@ public function handle(Order $order, array $modelData): DeliveryNote
}
}

$this->update($order, $modelData);
$this->orderHydrators($order);

UpdateOrder::make()->action($order, $modelData);


return $deliveryNote;
}
Expand All @@ -107,6 +108,13 @@ public function rules(): array
];
}

public function prepareForValidation(): void
{
if (!$this->has('warehouse_id')) {
$warehouse = $this->shop->organisation->warehouses()->first();
$this->set('warehouse_id', $warehouse->id);
}
}

public function afterValidator(Validator $validator): void
{
Expand All @@ -125,14 +133,14 @@ public function afterValidator(Validator $validator): void
}
}

public function action(Order $order, $modelData): DeliveryNote
public function action(Order $order, array $modelData): DeliveryNote
{
$this->asAction = true;
$this->scope = $order->shop;
$this->order = $order;
$this->initialisationFromShop($order->shop, []);
$this->initialisationFromShop($order->shop, $modelData);

return $this->handle($order, $modelData);
return $this->handle($order, $this->validatedData);
}

public function asController(Order $order, ActionRequest $request): DeliveryNote
Expand All @@ -144,13 +152,4 @@ public function asController(Order $order, ActionRequest $request): DeliveryNote
return $this->handle($order, $this->validatedData);
}

public function prepareForValidation(ActionRequest $request): void
{
if (!$this->has('warehouse_id')) {
$warehouse = $this->shop->organisation->warehouses()->first();
$this->set('warehouse_id', $warehouse->id);
}
}


}
33 changes: 25 additions & 8 deletions app/Actions/Ordering/Order/UpdateOrder.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?php
/*
* Author: Raul Perusquia <raul@inikoo.com>
* Created: Tue, 20 Jun 2023 20:33:12 Malaysia Time, Pantai Lembeng, Bali, Id
* Created: Tue, 20 Jun 2023 20:33:12 Malaysia Time, Pantai Lembeng, Bali, Indonesia
* Copyright (c) 2023, Raul A Perusquia Flores
*/

Expand All @@ -13,6 +13,7 @@
use App\Actions\Traits\WithActionUpdate;
use App\Actions\Traits\WithFixedAddressActions;
use App\Actions\Traits\WithModelAddressActions;
use App\Enums\Ordering\Order\OrderStateEnum;
use App\Models\Helpers\Address;
use App\Models\Helpers\Country;
use App\Models\Ordering\Order;
Expand All @@ -27,6 +28,7 @@ class UpdateOrder extends OrgAction
use WithActionUpdate;
use WithFixedAddressActions;
use WithModelAddressActions;
use HasOrderHydrators;

private Order $order;

Expand All @@ -40,7 +42,10 @@ public function handle(Order $order, array $modelData): Order
$deliveryAddressData = Arr::get($modelData, 'delivery_address');
data_forget($modelData, 'delivery_address');

$order = $this->update($order, $modelData, ['data']);


$order = $this->update($order, $modelData, ['data']);
$changedFields = $order->getChanges();

if ($billingAddressData) {
if ($order->billing_locked) {
Expand All @@ -62,14 +67,14 @@ public function handle(Order $order, array $modelData): Order
}
}
if ($deliveryAddressData) {
$groupId = $order->group_id;
$groupId = $order->group_id;

data_set($deliveryAddressData, 'group_id', $groupId);

if (Arr::exists($deliveryAddressData, 'id')) {
$countryCode = Country::find(Arr::get($deliveryAddressData, 'country_id'))->code;
data_set($deliveryAddressData, 'country_code', $countryCode);
$label = isset($deliveryAddressData['label']) ? $deliveryAddressData['label'] : null;
$label = $deliveryAddressData['label'] ?? null;
unset($deliveryAddressData['label']);
unset($deliveryAddressData['can_edit']);
unset($deliveryAddressData['can_delete']);
Expand All @@ -91,14 +96,21 @@ public function handle(Order $order, array $modelData): Order
}



OrderRecordSearch::dispatch($order);

if (array_key_exists('state', $changedFields)) {
$this->orderHydrators($order);
}


return $order;
}

public function rules(): array
{
$rules = [
'reference' => [
'reference' => [
'sometimes',
'string',
'max:64',
Expand All @@ -115,16 +127,18 @@ public function rules(): array
'delivery_address' => ['sometimes', 'required', new ValidAddress()],
'billing_locked' => ['sometimes', 'boolean'],
'delivery_locked' => ['sometimes', 'boolean'],
'last_fetched_at' => ['sometimes', 'date'],
'in_warehouse_at' => ['sometimes', 'date'],
'payment_amount' => ['sometimes'],
'delivery_address_id' => ['sometimes', Rule::exists('addresses', 'id')],
'public_notes' => ['sometimes', 'nullable', 'string', 'max:4000'],
'internal_notes' => ['sometimes', 'nullable', 'string', 'max:4000'],
'state' => ['sometimes', Rule::enum(OrderStateEnum::class)],
// 'customer_notes' => ['sometimes', 'nullable', 'string', 'max:4000'],
];

if (!$this->strict) {
$rules['reference'] = ['sometimes', 'string', 'max:64'];
$rules['reference'] = ['sometimes', 'string', 'max:64'];
$rules['last_fetched_at'] = ['sometimes', 'date'];
}

return $rules;
Expand All @@ -140,15 +154,18 @@ public function action(Order $order, array $modelData, bool $strict = true, int
$this->hydratorsDelay = $hydratorsDelay;
$this->order = $order;



$this->initialisationFromShop($order->shop, $modelData);

return $this->handle($order, $this->validatedData);
}

public function asController(Order $order, ActionRequest $request)
public function asController(Order $order, ActionRequest $request): Order
{
$this->order = $order;
$this->initialisationFromShop($order->shop, $request);

return $this->handle($order, $this->validatedData);
}
}
3 changes: 3 additions & 0 deletions app/Actions/Traits/WithModelAddressActions.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,9 @@ protected function addAddressToModel($model, $addressData, $scope = 'default', $

unset($addressData['label']);


data_forget($addressData, 'id');

$address = Address::create($addressData);


Expand Down
22 changes: 13 additions & 9 deletions tests/Feature/OrderingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Actions\CRM\CustomerClient\StoreCustomerClient;
use App\Actions\CRM\CustomerClient\UpdateCustomerClient;
use App\Actions\Ordering\Order\DeleteOrder;
use App\Actions\Ordering\Order\SendOrderToWarehouse;
use App\Actions\Ordering\Order\StoreOrder;
use App\Actions\Ordering\Order\UpdateOrder;
use App\Actions\Ordering\Order\UpdateOrderStateToInWarehouse;
Expand All @@ -33,6 +34,7 @@
use App\Models\Catalogue\HistoricAsset;
use App\Models\Catalogue\Shop;
use App\Models\CRM\Customer;
use App\Models\Dispatching\DeliveryNote;
use App\Models\Dropshipping\CustomerClient;
use App\Models\Helpers\Address;
use App\Models\Ordering\Order;
Expand All @@ -50,16 +52,18 @@
$this->organisation,
$this->user,
$this->shop
) = createShop();
) = createShop();

$this->group = $this->organisation->group;

list(
$this->tradeUnit,
$this->product
) = createProduct($this->shop);
) = createProduct($this->shop);

$this->customer = createCustomer($this->shop);

createWarehouse();
});


Expand Down Expand Up @@ -122,17 +126,16 @@


test('create transaction', function ($order) {
$transactionData = Transaction::factory()->definition();
$historicAsset = $this->product->historicAsset;
$transactionData = Transaction::factory()->definition();
$historicAsset = $this->product->historicAsset;
expect($historicAsset)->toBeInstanceOf(HistoricAsset::class);
$transaction = StoreTransaction::make()->action($order, $historicAsset, $transactionData);

$order->refresh();

expect($transaction)->toBeInstanceOf(Transaction::class)
->and($transaction->order->stats->number_transactions_at_creation)->toBe(1)
->and($order->stats->number_transactions)->toBe(1)
;
->and($order->stats->number_transactions)->toBe(1);

return $transaction;
})->depends('create order');
Expand Down Expand Up @@ -163,9 +166,10 @@


test('update order state to in warehouse', function (Order $order) {
$order = UpdateOrderStateToInWarehouse::make()->action($order);

expect($order->state)->toEqual(OrderStateEnum::IN_WAREHOUSE);
$deliveryNote = SendOrderToWarehouse::make()->action($order, []);
$order->refresh();
expect($deliveryNote)->toBeInstanceOf(DeliveryNote::class)
->and($order->state)->toEqual(OrderStateEnum::IN_WAREHOUSE);
})->depends('update order state to submitted');

test('update state to packed from handling', function ($order) {
Expand Down

0 comments on commit 6ae2f27

Please sign in to comment.