Skip to content

Commit d2d4ef0

Browse files
committed
Update
- Added : new crud component to edit a product expiration. - Fixed : date time picker description - Added : ensuring product with accurate tracking can be adjusted
1 parent 92506a3 commit d2d4ef0

File tree

13 files changed

+617
-50
lines changed

13 files changed

+617
-50
lines changed

app/Crud/ProcurementProductCrud.php

Lines changed: 457 additions & 0 deletions
Large diffs are not rendered by default.

app/Http/Controllers/Dashboard/ProcurementController.php

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace App\Http\Controllers\Dashboard;
99

1010
use App\Crud\ProcurementCrud;
11+
use App\Crud\ProcurementProductCrud;
1112
use App\Exceptions\NotAllowedException;
1213
use Illuminate\Http\Request;
1314
use Illuminate\Support\Facades\View;
@@ -20,7 +21,9 @@
2021
use App\Services\Options;
2122
use App\Http\Requests\ProcurementRequest;
2223
use App\Models\Procurement;
24+
use App\Models\ProcurementProduct;
2325
use App\Models\Product;
26+
use App\Models\Unit;
2427
use App\Services\ProductService;
2528
use Tendoo\Core\Exceptions\AccessDeniedException;
2629

@@ -236,7 +239,28 @@ public function procurementInvoice( Procurement $procurement )
236239

237240
public function searchProduct( Request $request )
238241
{
239-
$products = $this->productService->searchProduct( $request->input( 'search' ) );
242+
$products = Product::query()->orWhere( 'name', 'LIKE', "%{$request->input( 'argument' )}%" )
243+
->searchable()
244+
->trackingDisabled()
245+
->with( 'unit_quantities.unit' )
246+
->where( function( $query ) use ( $request ) {
247+
$query->where( 'sku', 'LIKE', "%{$request->input( 'argument' )}%" )
248+
->orWhere( 'barcode', 'LIKE', "%{$request->input( 'argument' )}%" );
249+
})
250+
->limit( 5 )
251+
->get()
252+
->map( function( $product ) {
253+
$units = json_decode( $product->purchase_unit_ids );
254+
255+
if ( $units ) {
256+
$product->purchase_units = collect();
257+
collect( $units )->each( function( $unitID ) use ( &$product ) {
258+
$product->purchase_units->push( Unit::find( $unitID ) );
259+
});
260+
}
261+
262+
return $product;
263+
});
240264

241265
if ( ! $products->isEmpty() ) {
242266
return [
@@ -250,5 +274,15 @@ public function searchProduct( Request $request )
250274
'product' => $this->procurementService->searchProduct( $request->input( 'search' ) )
251275
];
252276
}
277+
278+
public function getProcurementProducts()
279+
{
280+
return ProcurementProductCrud::table();
281+
}
282+
283+
public function editProcurementProduct( ProcurementProduct $product )
284+
{
285+
return ProcurementProductCrud::form( $product );
286+
}
253287
}
254288

app/Http/Controllers/Dashboard/ProductsController.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -500,11 +500,12 @@ public function createAdjustment( Request $request )
500500
*/
501501
foreach( $request->input( 'products' ) as $product ) {
502502
$results[] = $this->productService->stockAdjustment( $product[ 'adjust_action' ], [
503-
'unit_price' => $product[ 'adjust_unit' ][ 'sale_price' ],
504-
'unit_id' => $product[ 'adjust_unit' ][ 'unit_id' ],
505-
'product_id' => $product[ 'id' ],
506-
'quantity' => $product[ 'adjust_quantity' ],
507-
'description' => $product[ 'adjust_reason' ] ?? '',
503+
'unit_price' => $product[ 'adjust_unit' ][ 'sale_price' ],
504+
'unit_id' => $product[ 'adjust_unit' ][ 'unit_id' ],
505+
'procurement_product_id' => $product[ 'procurement_product_id' ] ?? null,
506+
'product_id' => $product[ 'id' ],
507+
'quantity' => $product[ 'adjust_quantity' ],
508+
'description' => $product[ 'adjust_reason' ] ?? '',
508509
]);
509510
}
510511

app/Models/ProcurementProduct.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class ProcurementProduct extends NsModel
1717

1818
protected $table = 'nexopos_' . 'procurements_products';
1919

20+
const STOCK_INCREASE = 'increase';
21+
const STOCK_REDUCE = 'reduce';
22+
2023
protected $dispatchesEvents = [
2124
'creating' => ProcurementProductBeforeCreateEvent::class,
2225
'created' => ProcurementProductAfterCreateEvent::class,

app/Models/Product.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ class Product extends NsModel
1717
const EXPIRES_ALLOW_SALES = 'allow_sales';
1818

1919
protected $table = 'nexopos_' . 'products';
20+
protected $cats = [
21+
'accurate_tracking' => 'boolean'
22+
];
2023

2124
public function category()
2225
{
@@ -165,6 +168,17 @@ public function scopeWithStockDisabled( $query )
165168
return $query->where( 'stock_management', Product::STOCK_MANAGEMENT_DISABLED );
166169
}
167170

171+
/**
172+
* Filter query by getitng product with
173+
* accurate stock enabled or not.
174+
* @param QueryBuilder $query
175+
* @return QueryBuilder
176+
*/
177+
public function scopeAccurateTracking( $query, $argument = true )
178+
{
179+
return $query->where( 'accurate_tracking', $argument );
180+
}
181+
168182
/**
169183
* Filter product that are searchable
170184
* @param QueryBuilder

app/Providers/CrudServiceProvider.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,13 @@
2525
use App\Crud\TaxesGroupCrud;
2626
use App\Crud\UserCrud;
2727
use App\Crud\ProcurementCrud;
28+
use App\Crud\ProcurementProductCrud;
2829
use App\Crud\ProductHistoryCrud;
2930
use App\Crud\ProductUnitQuantitiesCrud;
3031
use App\Crud\RegisterCrud;
3132
use App\Crud\RegisterHistoryCrud;
3233
use App\Crud\RolesCrud;
3334
use App\Crud\UnpaidOrderCrud;
34-
use App\Models\ExpenseHistory;
3535
use Illuminate\Support\ServiceProvider;
3636
use TorMorten\Eventy\Facades\Events as Hook;
3737

@@ -88,6 +88,7 @@ public function boot()
8888
case 'ns.registers': return RegisterCrud::class;
8989
case 'ns.registers-hitory': return RegisterHistoryCrud::class;
9090
case 'ns.procurements': return ProcurementCrud::class;
91+
case 'ns.procurements-products': return ProcurementProductCrud::class;
9192
case 'ns.roles': return RolesCrud::class;
9293
}
9394
return $namespace;

app/Services/MenuService.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,14 @@ public function buildMenus()
319319
'href' => ns()->url( '/dashboard/procurements' )
320320
],
321321
'procurements-create' => [
322-
'label' => __( 'New Procurement' ),
322+
'label' => __( 'New Procurement' ),
323323
'permissions' => [ 'nexopos.create.procurements' ],
324-
'href' => ns()->url( '/dashboard/procurements/create' )
324+
'href' => ns()->url( '/dashboard/procurements/create' )
325+
],
326+
'procurements-products' => [
327+
'label' => __( 'Products' ),
328+
'permissions' => [ 'nexopos.update.procurements' ],
329+
'href' => ns()->url( '/dashboard/procurements/products' )
325330
],
326331
]
327332
],

app/Services/OrdersService.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -961,14 +961,7 @@ private function __saveOrderProducts($order, $products)
961961
$gross = 0;
962962

963963
$products->each(function ($product) use (&$subTotal, &$taxes, &$order, &$gross) {
964-
965-
/**
966-
* this should run only if the product looped doesn't include an identifier.
967-
* Usually if it's the case, the product is supposed to have been already handled before.
968-
*/
969-
// if ( empty( $product[ 'id' ] ) ) {
970-
// }
971-
964+
972965
/**
973966
* storing the product
974967
* history as a sale

app/Services/ProcurementService.php

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -926,10 +926,12 @@ public function searchProduct( $argument )
926926
->with([ 'unit', 'procurement' ])
927927
->first();
928928

929-
$procurementProduct->unit_quantity = $this->productService->getUnitQuantity(
930-
$procurementProduct->product_id,
931-
$procurementProduct->unit_id
932-
);
929+
if ( $procurementProduct instanceof ProcurementProduct ) {
930+
$procurementProduct->unit_quantity = $this->productService->getUnitQuantity(
931+
$procurementProduct->product_id,
932+
$procurementProduct->unit_id
933+
);
934+
}
933935

934936
return $procurementProduct;
935937
}

app/Services/ProductService.php

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -935,14 +935,16 @@ public function stockAdjustment( $action, $data )
935935
* @param id unit_id
936936
* @param float $unit_price
937937
* @param float total_price
938+
* @param int procurement_product_id
938939
* @param string $description
939940
* @param float quantity
940941
* @param string sku
941942
* @param string $unit_identifier
942943
*/
943-
$product = isset( $product_id ) ? Product::findOrFail( $product_id ) : Product::usingSKU( $sku )->first();
944-
$product_id = $product->id;
945-
$unit_id = isset( $unit_id ) ? $unit_id : Unit::identifier( $unit_identifier )->firstOrFail()->id;
944+
$product = isset( $product_id ) ? Product::findOrFail( $product_id ) : Product::usingSKU( $sku )->first();
945+
$product_id = $product->id;
946+
$unit_id = isset( $unit_id ) ? $unit_id : Unit::identifier( $unit_identifier )->firstOrFail()->id;
947+
$procurementProduct = isset( $procurement_product_id ) ? ProcurementProduct::find( $procurement_product_id ) : false;
946948

947949
/**
948950
* let's check the different
@@ -1011,6 +1013,15 @@ public function stockAdjustment( $action, $data )
10111013
* @var array [ 'oldQuantity', 'newQuantity' ]
10121014
*/
10131015
$result = $this->reduceUnitQuantities( $product_id, $unit_id, abs( $quantity ), $oldQuantity );
1016+
1017+
/**
1018+
* We should reduce the quantity if
1019+
* we're dealing with a product that has
1020+
* accurate stock tracking
1021+
*/
1022+
if ( $procurementProduct instanceof ProcurementProduct ) {
1023+
$this->updateProcurementProductQuantity( $procurementProduct, $quantity, ProcurementProduct::STOCK_REDUCE );
1024+
}
10141025
} else {
10151026

10161027
/**
@@ -1019,6 +1030,15 @@ public function stockAdjustment( $action, $data )
10191030
* @var array [ 'oldQuantity', 'newQuantity' ]
10201031
*/
10211032
$result = $this->increaseUnitQuantities( $product_id, $unit_id, abs( $quantity ), $oldQuantity );
1033+
1034+
/**
1035+
* We should reduce the quantity if
1036+
* we're dealing with a product that has
1037+
* accurate stock tracking
1038+
*/
1039+
if ( $procurementProduct instanceof ProcurementProduct ) {
1040+
$this->updateProcurementProductQuantity( $procurementProduct, $quantity, ProcurementProduct::STOCK_INCREASE );
1041+
}
10221042
}
10231043
}
10241044

@@ -1043,6 +1063,23 @@ public function stockAdjustment( $action, $data )
10431063
return $history;
10441064
}
10451065

1066+
/**
1067+
* Update procurement product quantity
1068+
* @param ProcurementProduct $procurementProduct
1069+
* @param int $quantity
1070+
* @param string $action
1071+
*/
1072+
public function updateProcurementProductQuantity( $procurementProduct, $quantity, $action )
1073+
{
1074+
if ( $action === ProcurementProduct::STOCK_INCREASE ) {
1075+
$procurementProduct->available_quantity += $quantity;
1076+
} else if ( $action === ProcurementProduct::STOCK_REDUCE ) {
1077+
$procurementProduct->available_quantity -= $quantity;
1078+
}
1079+
1080+
$procurementProduct->save();
1081+
}
1082+
10461083
/**
10471084
* reduce Product unit quantities and update
10481085
* the available quantity for the unit provided

0 commit comments

Comments
 (0)