Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
- Added : new crud component to edit a product expiration.
- Fixed : date time picker description
- Added : ensuring product with accurate tracking can be adjusted
  • Loading branch information
Blair2004 committed May 7, 2021
1 parent 92506a3 commit d2d4ef0
Show file tree
Hide file tree
Showing 13 changed files with 617 additions and 50 deletions.
457 changes: 457 additions & 0 deletions app/Crud/ProcurementProductCrud.php

Large diffs are not rendered by default.

36 changes: 35 additions & 1 deletion app/Http/Controllers/Dashboard/ProcurementController.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
namespace App\Http\Controllers\Dashboard;

use App\Crud\ProcurementCrud;
use App\Crud\ProcurementProductCrud;
use App\Exceptions\NotAllowedException;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\View;
Expand All @@ -20,7 +21,9 @@
use App\Services\Options;
use App\Http\Requests\ProcurementRequest;
use App\Models\Procurement;
use App\Models\ProcurementProduct;
use App\Models\Product;
use App\Models\Unit;
use App\Services\ProductService;
use Tendoo\Core\Exceptions\AccessDeniedException;

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

public function searchProduct( Request $request )
{
$products = $this->productService->searchProduct( $request->input( 'search' ) );
$products = Product::query()->orWhere( 'name', 'LIKE', "%{$request->input( 'argument' )}%" )
->searchable()
->trackingDisabled()
->with( 'unit_quantities.unit' )
->where( function( $query ) use ( $request ) {
$query->where( 'sku', 'LIKE', "%{$request->input( 'argument' )}%" )
->orWhere( 'barcode', 'LIKE', "%{$request->input( 'argument' )}%" );
})
->limit( 5 )
->get()
->map( function( $product ) {
$units = json_decode( $product->purchase_unit_ids );

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

return $product;
});

if ( ! $products->isEmpty() ) {
return [
Expand All @@ -250,5 +274,15 @@ public function searchProduct( Request $request )
'product' => $this->procurementService->searchProduct( $request->input( 'search' ) )
];
}

public function getProcurementProducts()
{
return ProcurementProductCrud::table();
}

public function editProcurementProduct( ProcurementProduct $product )
{
return ProcurementProductCrud::form( $product );
}
}

11 changes: 6 additions & 5 deletions app/Http/Controllers/Dashboard/ProductsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -500,11 +500,12 @@ public function createAdjustment( Request $request )
*/
foreach( $request->input( 'products' ) as $product ) {
$results[] = $this->productService->stockAdjustment( $product[ 'adjust_action' ], [
'unit_price' => $product[ 'adjust_unit' ][ 'sale_price' ],
'unit_id' => $product[ 'adjust_unit' ][ 'unit_id' ],
'product_id' => $product[ 'id' ],
'quantity' => $product[ 'adjust_quantity' ],
'description' => $product[ 'adjust_reason' ] ?? '',
'unit_price' => $product[ 'adjust_unit' ][ 'sale_price' ],
'unit_id' => $product[ 'adjust_unit' ][ 'unit_id' ],
'procurement_product_id' => $product[ 'procurement_product_id' ] ?? null,
'product_id' => $product[ 'id' ],
'quantity' => $product[ 'adjust_quantity' ],
'description' => $product[ 'adjust_reason' ] ?? '',
]);
}

Expand Down
3 changes: 3 additions & 0 deletions app/Models/ProcurementProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class ProcurementProduct extends NsModel

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

const STOCK_INCREASE = 'increase';
const STOCK_REDUCE = 'reduce';

protected $dispatchesEvents = [
'creating' => ProcurementProductBeforeCreateEvent::class,
'created' => ProcurementProductAfterCreateEvent::class,
Expand Down
14 changes: 14 additions & 0 deletions app/Models/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ class Product extends NsModel
const EXPIRES_ALLOW_SALES = 'allow_sales';

protected $table = 'nexopos_' . 'products';
protected $cats = [
'accurate_tracking' => 'boolean'
];

public function category()
{
Expand Down Expand Up @@ -165,6 +168,17 @@ public function scopeWithStockDisabled( $query )
return $query->where( 'stock_management', Product::STOCK_MANAGEMENT_DISABLED );
}

/**
* Filter query by getitng product with
* accurate stock enabled or not.
* @param QueryBuilder $query
* @return QueryBuilder
*/
public function scopeAccurateTracking( $query, $argument = true )
{
return $query->where( 'accurate_tracking', $argument );
}

/**
* Filter product that are searchable
* @param QueryBuilder
Expand Down
3 changes: 2 additions & 1 deletion app/Providers/CrudServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
use App\Crud\TaxesGroupCrud;
use App\Crud\UserCrud;
use App\Crud\ProcurementCrud;
use App\Crud\ProcurementProductCrud;
use App\Crud\ProductHistoryCrud;
use App\Crud\ProductUnitQuantitiesCrud;
use App\Crud\RegisterCrud;
use App\Crud\RegisterHistoryCrud;
use App\Crud\RolesCrud;
use App\Crud\UnpaidOrderCrud;
use App\Models\ExpenseHistory;
use Illuminate\Support\ServiceProvider;
use TorMorten\Eventy\Facades\Events as Hook;

Expand Down Expand Up @@ -88,6 +88,7 @@ public function boot()
case 'ns.registers': return RegisterCrud::class;
case 'ns.registers-hitory': return RegisterHistoryCrud::class;
case 'ns.procurements': return ProcurementCrud::class;
case 'ns.procurements-products': return ProcurementProductCrud::class;
case 'ns.roles': return RolesCrud::class;
}
return $namespace;
Expand Down
9 changes: 7 additions & 2 deletions app/Services/MenuService.php
Original file line number Diff line number Diff line change
Expand Up @@ -319,9 +319,14 @@ public function buildMenus()
'href' => ns()->url( '/dashboard/procurements' )
],
'procurements-create' => [
'label' => __( 'New Procurement' ),
'label' => __( 'New Procurement' ),
'permissions' => [ 'nexopos.create.procurements' ],
'href' => ns()->url( '/dashboard/procurements/create' )
'href' => ns()->url( '/dashboard/procurements/create' )
],
'procurements-products' => [
'label' => __( 'Products' ),
'permissions' => [ 'nexopos.update.procurements' ],
'href' => ns()->url( '/dashboard/procurements/products' )
],
]
],
Expand Down
9 changes: 1 addition & 8 deletions app/Services/OrdersService.php
Original file line number Diff line number Diff line change
Expand Up @@ -961,14 +961,7 @@ private function __saveOrderProducts($order, $products)
$gross = 0;

$products->each(function ($product) use (&$subTotal, &$taxes, &$order, &$gross) {

/**
* this should run only if the product looped doesn't include an identifier.
* Usually if it's the case, the product is supposed to have been already handled before.
*/
// if ( empty( $product[ 'id' ] ) ) {
// }


/**
* storing the product
* history as a sale
Expand Down
10 changes: 6 additions & 4 deletions app/Services/ProcurementService.php
Original file line number Diff line number Diff line change
Expand Up @@ -926,10 +926,12 @@ public function searchProduct( $argument )
->with([ 'unit', 'procurement' ])
->first();

$procurementProduct->unit_quantity = $this->productService->getUnitQuantity(
$procurementProduct->product_id,
$procurementProduct->unit_id
);
if ( $procurementProduct instanceof ProcurementProduct ) {
$procurementProduct->unit_quantity = $this->productService->getUnitQuantity(
$procurementProduct->product_id,
$procurementProduct->unit_id
);
}

return $procurementProduct;
}
Expand Down
43 changes: 40 additions & 3 deletions app/Services/ProductService.php
Original file line number Diff line number Diff line change
Expand Up @@ -935,14 +935,16 @@ public function stockAdjustment( $action, $data )
* @param id unit_id
* @param float $unit_price
* @param float total_price
* @param int procurement_product_id
* @param string $description
* @param float quantity
* @param string sku
* @param string $unit_identifier
*/
$product = isset( $product_id ) ? Product::findOrFail( $product_id ) : Product::usingSKU( $sku )->first();
$product_id = $product->id;
$unit_id = isset( $unit_id ) ? $unit_id : Unit::identifier( $unit_identifier )->firstOrFail()->id;
$product = isset( $product_id ) ? Product::findOrFail( $product_id ) : Product::usingSKU( $sku )->first();
$product_id = $product->id;
$unit_id = isset( $unit_id ) ? $unit_id : Unit::identifier( $unit_identifier )->firstOrFail()->id;
$procurementProduct = isset( $procurement_product_id ) ? ProcurementProduct::find( $procurement_product_id ) : false;

/**
* let's check the different
Expand Down Expand Up @@ -1011,6 +1013,15 @@ public function stockAdjustment( $action, $data )
* @var array [ 'oldQuantity', 'newQuantity' ]
*/
$result = $this->reduceUnitQuantities( $product_id, $unit_id, abs( $quantity ), $oldQuantity );

/**
* We should reduce the quantity if
* we're dealing with a product that has
* accurate stock tracking
*/
if ( $procurementProduct instanceof ProcurementProduct ) {
$this->updateProcurementProductQuantity( $procurementProduct, $quantity, ProcurementProduct::STOCK_REDUCE );
}
} else {

/**
Expand All @@ -1019,6 +1030,15 @@ public function stockAdjustment( $action, $data )
* @var array [ 'oldQuantity', 'newQuantity' ]
*/
$result = $this->increaseUnitQuantities( $product_id, $unit_id, abs( $quantity ), $oldQuantity );

/**
* We should reduce the quantity if
* we're dealing with a product that has
* accurate stock tracking
*/
if ( $procurementProduct instanceof ProcurementProduct ) {
$this->updateProcurementProductQuantity( $procurementProduct, $quantity, ProcurementProduct::STOCK_INCREASE );
}
}
}

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

/**
* Update procurement product quantity
* @param ProcurementProduct $procurementProduct
* @param int $quantity
* @param string $action
*/
public function updateProcurementProductQuantity( $procurementProduct, $quantity, $action )
{
if ( $action === ProcurementProduct::STOCK_INCREASE ) {
$procurementProduct->available_quantity += $quantity;
} else if ( $action === ProcurementProduct::STOCK_REDUCE ) {
$procurementProduct->available_quantity -= $quantity;
}

$procurementProduct->save();
}

/**
* reduce Product unit quantities and update
* the available quantity for the unit provided
Expand Down
2 changes: 1 addition & 1 deletion resources/ts/components/ns-date-time-picker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const nsDateTimePicker = Vue.component( 'ns-date-time-picker', {
<span v-if="currentDay === null">N/A</span>
</span>
</div>
<p class="text-sm text-gray-500">{{ field.descrition }}</p>
<p class="text-sm text-gray-500 py-1">{{ field.description }}</p>
<div class="relative z-10 h-0 w-0 -mb-2" v-if="visible">
<div class="w-72 mt-2 shadow rounded bg-white anim-duration-300 zoom-in-entrance flex flex-col">
<div class="flex-auto">
Expand Down
Loading

0 comments on commit d2d4ef0

Please sign in to comment.