Skip to content

Commit

Permalink
Changelog:
Browse files Browse the repository at this point in the history
- Fixed: Serialization issues #2138
- Added: preload procurements products
  • Loading branch information
Blair2004 committed Jan 7, 2025
1 parent f1e3e49 commit 51c5e6d
Show file tree
Hide file tree
Showing 15 changed files with 216 additions and 98 deletions.
11 changes: 11 additions & 0 deletions app/Http/Controllers/Dashboard/ProcurementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Str;

class ProcurementController extends DashboardController
{
Expand Down Expand Up @@ -324,4 +325,14 @@ public function editProcurementProduct( ProcurementProduct $product )
{
return ProcurementProductCrud::form( $product );
}

public function preload( $uuid )
{
return $this->procurementService->preload( $uuid );
}

public function storePreload( Request $request )
{
return $this->procurementService->storePreload( Str::uuid(), $request->only([ 'products' ]) );
}
}
39 changes: 39 additions & 0 deletions app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,5 +328,44 @@ protected function loadConfiguration()
OrderProductRefund::CONDITION_UNSPOILED => __( 'Good Condition' ),
],
] );

/**
* We cannot set callback function on the configuration file
* as using optimize will throw an exception.
*/
config([
'accounting.accounts' => [
'assets' => [
'increase' => 'debit',
'decrease' => 'credit',
'label' => __( 'Assets' ),
'account' => 1000,
],
'liabilities' => [
'increase' => 'credit',
'decrease' => 'debit',
'label' => __( 'Liabilities' ),
'account' => 2000,
],
'equity' => [
'increase' => 'credit',
'decrease' => 'debit',
'label' => __( 'Equity' ),
'account' => 3000,
],
'revenues' => [
'increase' => 'credit',
'decrease' => 'debit',
'label' => __( 'Revenues' ),
'account' => 4000,
],
'expenses' => [
'increase' => 'debit',
'decrease' => 'credit',
'label' => __( 'Expenses' ),
'account' => 5000,
],
]
]);
}
}
126 changes: 94 additions & 32 deletions app/Services/ProcurementService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use App\Events\ProcurementBeforeHandledEvent;
use App\Events\ProcurementBeforeUpdateEvent;
use App\Exceptions\NotAllowedException;
use App\Exceptions\NotFoundException;
use App\Models\Procurement;
use App\Models\ProcurementProduct;
use App\Models\Product;
Expand All @@ -25,6 +26,8 @@
use Illuminate\Database\Eloquent\Collection as EloquentCollection;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Cache;
use stdClass;

class ProcurementService
{
Expand Down Expand Up @@ -1004,52 +1007,62 @@ public function getPaymentLabel( $label )
}
}

public function searchProduct( $argument, $limit = 10 )
public function searchQuery()
{
return Product::query()
->whereIn( 'type', Hook::filter( 'ns-procurement-searchable-product-type', [
Product::TYPE_DEMATERIALIZED,
Product::TYPE_MATERIALIZED,
]) )
->notGrouped()
->withStockEnabled()
->with( 'unit_quantities.unit' );
}

public function searchProduct( $argument, $limit = 10 )
{
return $this->searchQuery()
->limit( $limit )
->where( function ( $query ) use ( $argument ) {
$query->orWhere( 'name', 'LIKE', "%{$argument}%" )
->orWhere( 'sku', 'LIKE', "%{$argument}%" )
->orWhere( 'barcode', 'LIKE', "%{$argument}%" );
} )
->withStockEnabled()
->with( 'unit_quantities.unit' )
->limit( $limit )
->get()
->map( function ( $product ) {
$units = json_decode( $product->purchase_unit_ids );
return $this->populateLoadedProduct( $product );
} );
}

if ( $units ) {
$product->purchase_units = collect();
collect( $units )->each( function ( $unitID ) use ( &$product ) {
$product->purchase_units->push( Unit::find( $unitID ) );
} );
}
public function populateLoadedProduct( $product )
{
$units = json_decode( $product->purchase_unit_ids );

/**
* We'll pull the last purchase
* price for the item retreived
*/
$product->unit_quantities->each( function ( $unitQuantity ) use ( $product ) {
$unitQuantity->load( 'unit' );
if ( $units ) {
$product->purchase_units = collect();
collect( $units )->each( function ( $unitID ) use ( &$product ) {
$product->purchase_units->push( Unit::find( $unitID ) );
} );
}

/**
* just in case it's not a valid instance
* we'll provide a default value "0"
*/
$unitQuantity->last_purchase_price = $this->productService->getLastPurchasePrice(
product: $product,
unit: $unitQuantity->unit
);
} );
/**
* We'll pull the last purchase
* price for the item retreived
*/
$product->unit_quantities->each( function ( $unitQuantity ) use ( $product ) {
$unitQuantity->load( 'unit' );

return $product;
} );
/**
* just in case it's not a valid instance
* we'll provide a default value "0"
*/
$unitQuantity->last_purchase_price = $this->productService->getLastPurchasePrice(
product: $product,
unit: $unitQuantity->unit
);
} );

return $product;
}

public function searchProcurementProduct( $argument )
Expand All @@ -1068,10 +1081,59 @@ public function searchProcurementProduct( $argument )
return $procurementProduct;
}

public function handlePaymentStatusChanging( Procurement $procurement, string $previous, string $new )
public function preload( string $hash )
{
// if ( $previous === Procurement::PAYMENT_UNPAID && $new === Procurement::PAYMENT_PAID ) {
// $this->transn
// }
if ( Cache::has( 'procurements-' . $hash ) ) {
$data = Cache::get( 'procurements-' . $hash );

return [
'items' => collect( $data[ 'items' ] )->map( function( $item ) use ( $data ) {
$query = $this->searchQuery()
->where( 'id', $item[ 'product_id' ] )
->whereHas( 'unit_quantities', function( $query ) use( $item ) {
$query->where( 'unit_id', $item[ 'unit_id' ] );
} );

$product = $query->first();

if ( $product instanceof Product ) {

/**
* This will be helpful to set the desired unit
* and quantity provided on the preload configuration.
*/
$product->procurement = new stdClass;
$product->procurement->unit_id = $item[ 'unit_id' ];
$product->procurement->quantity = ns()->currency
->define( $item[ 'quantity' ] )
->multipliedBy( $data[ 'multiplier' ] )->toFloat();

return $this->populateLoadedProduct( $product );
}

return false;
})->filter()
];
}

throw new NotFoundException( __( 'Unable to preload products. The hash might have expired or is invalid.' ) );
}

public function storePreload( string $hash, Collection | array $items, $expiration = 86400, $multiplier = 1 )
{
if ( ! empty( $items ) ) {
$data = [];
$data[ 'multiplier' ] = $multiplier;
$data[ 'items' ] = $items;

Cache::put( 'procurements-' . $hash, $data, $expiration );

return [
'status' => 'success',
'message' => __( 'The procurement has been saved for later use.' ),
];
}

throw new Exception( __( 'Unable to save the procurement for later use.' ) );
}
}
36 changes: 0 additions & 36 deletions config/accounting.php

This file was deleted.

Loading

0 comments on commit 51c5e6d

Please sign in to comment.