Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V4.8.x #1108

Merged
merged 6 commits into from
Oct 19, 2022
Merged

V4.8.x #1108

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Console/Commands/CrudGeneratorCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public function askNamespace()
} elseif ( $name == 'Q' ) {
return;
}
$this->error( __( 'Please provide a valid value' ) );
$this->error( __( 'Please provide a valid value.' ) );

return $this->askNamespace();
}
Expand Down
9 changes: 8 additions & 1 deletion app/Crud/CustomerCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -659,9 +659,16 @@ public function bulkAction( Request $request )
'failed' => 0,
];

foreach ( $request->input( 'entries_id' ) as $id ) {
foreach ( $request->input( 'entries' ) as $id ) {
$entity = $this->model::find( $id );
if ( $entity instanceof Customer ) {

/**
* We want to check if we're allowed to delete
* the selected customer by checking his dependencies.
*/
$this->handleDependencyForDeletion( $entity );

$entity->delete();
$status[ 'success' ]++;
} else {
Expand Down
3 changes: 2 additions & 1 deletion app/Crud/ProductCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,8 @@ public function setActions( CrudEntry $entry, $namespace )
$entry->type = match ( $entry->type ) {
'materialized' => __( 'Materialized' ),
'dematerialized' => __( 'Dematerialized' ),
'grouped' => __( 'Grouped' )
'grouped' => __( 'Grouped' ),
default => sprintf( __( 'Unknown Type: %s' ), $entry->type ),
};

$entry->type = '<strong class="' . $class . ' ">' . $entry->type . '</strong>';
Expand Down
3 changes: 3 additions & 0 deletions app/Crud/RegisterHistoryCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,9 @@ public function setActions( $entry, $namespace )
case RegisterHistory::ACTION_CASHOUT:
$entry->{ '$cssClass' } = 'warning border';
break;
case RegisterHistory::ACTION_CHANGE:
$entry->{ '$cssClass' } = 'warning border';
break;
case RegisterHistory::ACTION_CLOSING:
$entry->{ '$cssClass' } = 'warning border';
break;
Expand Down
2 changes: 1 addition & 1 deletion app/Crud/UserCrud.php
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ public function canAccess( $fields )
*
* @return void
*/
public function beforeDelete( $namespace, $id, $model )
public function beforeDelete( $namespace, int $id, $model )
{
if ( $namespace == 'ns.users' ) {
$this->allowedTo( 'delete' );
Expand Down
11 changes: 4 additions & 7 deletions app/Http/Controllers/Dashboard/CashRegistersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,10 @@

class CashRegistersController extends DashboardController
{
/**
* @var CashRegistersService
*/
protected $registersService;

public function __construct(
CashRegistersService $registersService
protected CashRegistersService $registersService
) {
parent::__construct();
$this->registersService = $registersService;
}

public function listRegisters()
Expand Down Expand Up @@ -150,6 +144,9 @@ public function getSessionHistory( Register $register )
case RegisterHistory::ACTION_CASHOUT:
$session->label = __( 'Cash Out' );
break;
case RegisterHistory::ACTION_CHANGE:
$session->label = __( 'Give Change' );
break;
case RegisterHistory::ACTION_CLOSING:
$session->label = __( 'Closing' );
break;
Expand Down
70 changes: 1 addition & 69 deletions app/Http/Controllers/Dashboard/CrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,75 +52,7 @@ public function crudDelete( $namespace, $id )
throw new NotFoundException( __( 'Unable to delete an entry that no longer exists.' ) );
}

if ( method_exists( $model, 'getDeclaredDependencies' ) ) {
/**
* Let's verify if the current model
* is a dependency for other models.
*/
$declaredDependencies = $model->getDeclaredDependencies();

foreach ( $declaredDependencies as $class => $indexes ) {
$localIndex = $indexes[ 'local_index' ] ?? 'id';
$request = $class::where( $indexes[ 'foreign_index' ], $model->$localIndex );
$dependencyFound = $request->first();
$countDependency = $request->count() - 1;

if ( $dependencyFound instanceof $class ) {
if ( isset( $model->{ $indexes[ 'local_name' ] } ) && ! empty( $indexes[ 'foreign_name' ] ) ) {
/**
* if the foreign name is an array
* we'll pull the first model set as linked
* to the item being deleted.
*/
if ( is_array( $indexes[ 'foreign_name' ] ) ) {
$relatedSubModel = $indexes[ 'foreign_name' ][0]; // model name
$localIndex = $indexes[ 'foreign_name' ][1]; // local index on the dependency table $dependencyFound
$foreignIndex = $indexes[ 'foreign_name' ][2] ?? 'id'; // foreign index on the related table $model
$labelColumn = $indexes[ 'foreign_name' ][3] ?? 'name'; // foreign index on the related table $model

/**
* we'll find if we find the model
* for the provided details.
*/
$result = $relatedSubModel::where( $foreignIndex, $dependencyFound->$localIndex )->first();

/**
* the model might exists. If that doesn't exists
* then probably it's not existing. There might be a misconfiguration
* on the relation.
*/
if ( $result instanceof $relatedSubModel ) {
$foreignName = $result->$labelColumn ?? __( 'Unidentified Item' );
} else {
$foreignName = $result->$labelColumn ?? __( 'Unexisting Item' );
}
} else {
$foreignName = $dependencyFound->{ $indexes[ 'foreign_name' ] } ?? __( 'Unidentified Item' );
}

/**
* The local name will always pull from
* the related model table.
*/
$localName = $model->{ $indexes[ 'local_name' ] };

throw new NotAllowedException( sprintf(
__( 'Unable to delete "%s" as it\'s a dependency for "%s"%s' ),
$localName,
$foreignName,
$countDependency >= 1 ? ' ' . trans_choice( '{1} and :count more item.|[2,*] and :count more items.', $countDependency, [ 'count' => $countDependency ] ) : '.'
) );
} else {
throw new NotAllowedException( sprintf(
$countDependency === 1 ?
__( 'Unable to delete this resource as it has %s dependency with %s item.' ) :
__( 'Unable to delete this resource as it has %s dependency with %s items.' ),
$class
) );
}
}
}
}
$resource->handleDependencyForDeletion( $model );

/**
* Run the filter before deleting
Expand Down
11 changes: 11 additions & 0 deletions app/Http/Controllers/Dashboard/CustomersController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use App\Services\CustomerService;
use App\Services\OrdersService;
use Exception;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Facades\View;
Expand Down Expand Up @@ -66,6 +67,16 @@ public function listCustomers()
return CustomerCrud::table();
}

/**
* Retreive few customers ordered by
* their recent activity
* @return Collection
*/
public function getRecentlyActive()
{
return $this->customerService->getRecentlyActive(10);
}

/**
* get list of avialable customers
*
Expand Down
8 changes: 4 additions & 4 deletions app/Jobs/ProcessCashRegisterHistoryFromPaymentJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;

/**
* @deprecated
*/
class ProcessCashRegisterHistoryFromPaymentJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, NsSerialize;
Expand All @@ -32,9 +35,6 @@ public function __construct( public Order $order, public OrderPayment $orderPaym
*/
public function handle( CashRegistersService $cashRegistersService )
{
$cashRegistersService->increaseFromOrderPayment(
order: $this->order,
orderPayment: $this->orderPayment
);
// ...
}
}
18 changes: 5 additions & 13 deletions app/Jobs/ProcessCashRegisterHistoryJob.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
namespace App\Jobs;

use App\Models\Order;
use App\Models\Register;
use App\Models\RegisterHistory;
use App\Services\CashRegistersService;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
Expand All @@ -30,23 +29,16 @@ public function __construct( public Order $order )
*
* @return void
*/
public function handle()
public function handle( CashRegistersService $cashRegistersService )
{
/**
* If the payment status changed from
* supported payment status to a "Paid" status.
*/
if ( $this->order->register_id !== null && $this->order->payment_status === Order::PAYMENT_PAID ) {
$register = Register::find( $this->order->register_id );

$registerHistory = new RegisterHistory;
$registerHistory->balance_before = $register->balance;
$registerHistory->value = $this->order->total;
$registerHistory->balance_after = $register->balance + $this->order->total;
$registerHistory->register_id = $this->order->register_id;
$registerHistory->action = RegisterHistory::ACTION_SALE;
$registerHistory->author = $this->order->author;
$registerHistory->save();
$cashRegistersService->recordCashRegisterHistorySale(
order: $this->order
);
}
}
}
5 changes: 1 addition & 4 deletions app/Listeners/OrderPaymentAfterCreatedEventListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace App\Listeners;

use App\Events\OrderAfterPaymentCreatedEvent;
use App\Jobs\ComputeDayReportJob;
use App\Jobs\ProcessCashRegisterHistoryFromPaymentJob;

class OrderPaymentAfterCreatedEventListener
{
Expand All @@ -23,7 +21,6 @@ public function __construct()
*/
public function handle( OrderAfterPaymentCreatedEvent $event )
{
ComputeDayReportJob::dispatch();
ProcessCashRegisterHistoryFromPaymentJob::dispatch( $event->order, $event->orderPayment );
// ...
}
}
8 changes: 2 additions & 6 deletions app/Models/NsModel.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@
namespace App\Models;

use App\Classes\Hook;
use App\Traits\NsDependable;
use Illuminate\Notifications\Notifiable;

abstract class NsModel extends NsRootModel
{
use Notifiable;
use Notifiable, NsDependable;

/**
* if defined, the Crud component
Expand All @@ -18,11 +19,6 @@ abstract class NsModel extends NsRootModel
// ...
];

public function getDeclaredDependencies()
{
return $this->isDependencyFor;
}

public function __construct( $attributes = [] )
{
parent::__construct( $attributes );
Expand Down
6 changes: 6 additions & 0 deletions app/Models/RegisterHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
* @property string action
* @property int author
* @property float value
* @property integer payment_id
* @property integer payment_type_id
* @property integer order_id
* @property string description
* @property string uuid
*/
Expand All @@ -30,6 +33,8 @@ class RegisterHistory extends NsModel

const ACTION_SALE = 'register-sale';

const ACTION_CHANGE = 'register-change';

const ACTION_DELETE = 'register-cash-delete';

const ACTION_REFUND = 'register-refund';
Expand All @@ -45,6 +50,7 @@ class RegisterHistory extends NsModel
self::ACTION_CLOSING,
self::ACTION_CASHOUT,
self::ACTION_DELETE,
self::ACTION_CHANGE,
];

protected $dispatchesEvents = [
Expand Down
13 changes: 12 additions & 1 deletion app/Models/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Models;

use App\Services\UserOptions;
use App\Traits\NsDependable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Relations\HasManyThrough;
use Illuminate\Foundation\Auth\User as Authenticatable;
Expand All @@ -29,14 +30,24 @@ class User extends Authenticatable
{
use Notifiable,
HasFactory,
HasApiTokens;
HasApiTokens,
NsDependable;

protected $table = 'nexopos_users';

protected $casts = [
'active' => 'boolean',
];

protected $isDependencyFor = [
Product::class => [
'local_name' => 'username',
'local_index' => 'id',
'foreign_name' => 'name',
'foreign_index' => 'author',
],
];

/**
* While saving model, this will
* use the timezone defined on the settings
Expand Down
Loading