Skip to content

Commit

Permalink
Merge branch 'master' into social
Browse files Browse the repository at this point in the history
  • Loading branch information
madbob committed Oct 30, 2023
2 parents 07b6719 + d46194f commit 915a835
Show file tree
Hide file tree
Showing 32 changed files with 270 additions and 144 deletions.
16 changes: 10 additions & 6 deletions code/app/Attachment.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ class Attachment extends Model

public function attached(): MorphTo
{
if ($this->target_type && in_array('Illuminate\Database\Eloquent\SoftDeletes', class_uses($this->target_type))) {
$uses = class_uses($this->target_type);

if ($this->target_type && $uses && in_array('Illuminate\Database\Eloquent\SoftDeletes', $uses)) {
// @phpstan-ignore-next-line
return $this->morphTo('target')->withoutGlobalScopes()->withTrashed();
}
Expand Down Expand Up @@ -55,8 +57,9 @@ public function isImage()

if (file_exists($file)) {
$mime = mime_content_type($file);
if (strncmp($mime, 'image/', 6) == 0)
if ($mime && strncmp($mime, 'image/', 6) == 0) {
return true;
}
}

return false;
Expand All @@ -70,11 +73,12 @@ public function getSize()
if ($this->isImage()) {
$file = $this->path;
$size = getimagesize($file);
return array_slice($size, 0, 2);
}
else {
Log::error('Richiesta dimensione per allegato non immagine');
if ($size) {
return array_slice($size, 0, 2);
}
}

Log::error('Richiesta dimensione per allegato non immagine');
}
catch(\Exception $e) {
Log::error('Impossibile recuperare dimensione allegato ' . $this->id);
Expand Down
79 changes: 46 additions & 33 deletions code/app/Console/Commands/OpenOrders.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,8 @@ public function __construct()
parent::__construct();
}

public function handle()
private function getDates()
{
/*
Qui vengono aperti gli ordini che erano stati impostati con una data
futura
*/

$pending = Order::where('status', 'suspended')->where('start', Carbon::today()->format('Y-m-d'))->get();
foreach($pending as $p) {
$p->status = 'open';
$p->save();
}

/*
Da qui vengono gestiti gli ordini schedulati con le date
*/

$dates = Date::where('type', 'order')->get();
$today = date('Y-m-d');
$aggregable = [];
Expand Down Expand Up @@ -90,34 +75,62 @@ public function handle()
}
}

return $aggregable;
}

private function openByDates($aggregable)
{
$today = date('Y-m-d');

foreach($aggregable as $aggr) {
$aggregate = new Aggregate();
$aggregate->save();

foreach($aggr as $date) {
$supplier = $date->target;

$order = new Order();
$order->aggregate_id = $aggregate->id;
$order->supplier_id = $supplier->id;
$order->comment = $date->comment;
$order->status = 'suspended';
$order->keep_open_packages = 'no';
$order->start = $today;
$order->end = date('Y-m-d', strtotime($today . ' +' . $date->end . ' days'));

if (!empty($date->shipping)) {
$order->shipping = date('Y-m-d', strtotime($today . ' +' . $date->shipping . ' days'));
}
$supplier = $date->target;

$order = new Order();
$order->aggregate_id = $aggregate->id;
$order->supplier_id = $supplier->id;
$order->comment = $date->comment;
$order->status = 'suspended';
$order->keep_open_packages = 'no';
$order->start = $today;
$order->end = date('Y-m-d', strtotime($today . ' +' . $date->end . ' days'));

if (!empty($date->shipping)) {
$order->shipping = date('Y-m-d', strtotime($today . ' +' . $date->shipping . ' days'));
}

Log::debug('Apro ordine automatico per ' . $supplier->name);
$order->save();
Log::debug('Apro ordine automatico per ' . $supplier->name);
$order->save();

$order->products()->sync($supplier->products()->where('active', '=', true)->get());
$order->products()->sync($supplier->products()->where('active', '=', true)->get());

$order->status = 'open';
$order->save();
}
}
}

public function handle()
{
/*
Qui vengono aperti gli ordini che erano stati impostati con una data
futura
*/

$pending = Order::where('status', 'suspended')->where('start', Carbon::today()->format('Y-m-d'))->get();
foreach($pending as $p) {
$p->status = 'open';
$p->save();
}

/*
Da qui vengono gestiti gli ordini schedulati con le date
*/

$aggregable = $this->getDates();
$this->openByDates($aggregable);
}
}
2 changes: 1 addition & 1 deletion code/app/Helpers/Components.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ function formatObjectsToComponentRec($options)
$ret = [];

foreach($options as $option) {
if (is_a($option, \App\Models\Concerns\HasChildren::class) && $option->children()->count() != 0) {
if (is_a($option, \App\Models\Concerns\HasChildren::class) && $option->children->count() != 0) {
$ret[$option->id] = (object) [
// @phpstan-ignore-next-line
'label' => $option->printableName(),
Expand Down
6 changes: 0 additions & 6 deletions code/app/Http/Controllers/MeasuresController.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,6 @@ public function update(Request $request, $id)
return $this->successResponse();
}

public function listProducts(Request $request, $id)
{
$measure = Measure::findOrFail($id);
return view('measures.products-list', ['products' => $measure->products]);
}

public function discretes()
{
$measures = Measure::all();
Expand Down
10 changes: 10 additions & 0 deletions code/app/Http/Controllers/ProductsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -188,4 +188,14 @@ public function updatePrices(Request $request, $id)

return $this->successResponse();
}

public function search(Request $request)
{
return $this->easyExecute(function() use ($request) {
$supplier = $request->input('supplier');
$term = $request->input('term');
$products = $this->service->search($supplier, $term);
return response()->json($products);
});
}
}
39 changes: 24 additions & 15 deletions code/app/Importers/CSV/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,9 @@ public function guess($request)
]);
}

private function mapSelection($class, $param, $value, $field, &$product)
private function mapSelection($objects, $param, $value, $field, &$product)
{
$test = $class::where($param, $value)->first();
$test = $objects->firstWhere($param, $value);
if (is_null($test)) {
return 'temp_' . $field . '_name';
}
Expand All @@ -106,6 +106,10 @@ public function select($request)
$s = $this->getSupplier($request);

$products = $errors = [];
$all_products = $s->products;
$all_categories = Category::all();
$all_measures = Measure::all();
$all_vatrates = VatRate::all();

foreach($reader->getRecords() as $line) {
if (empty($line) || (count($line) == 1 && empty($line[0]))) {
Expand All @@ -116,19 +120,18 @@ public function select($request)
$test = null;

if ($supplier_code_index != -1 && filled($line[$supplier_code_index])) {
$test = $s->products()->withTrashed()->where('supplier_code', $line[$supplier_code_index])->orderBy('id', 'desc')->first();
$test = $all_products->firstWhereAbout('supplier_code', $line[$supplier_code_index]);
}

if (is_null($test)) {
if ($name_index != -1 && filled($line[$name_index])) {
$test = $s->products()->withTrashed()->where('name', $line[$name_index])->orderBy('id', 'desc')->first();
$test = $all_products->firstWhereAbout('name', $line[$name_index]);
}
}

$want_replace = is_null($test) ? 0 : $test->id;

if ($want_replace) {
if (is_null($test) == false) {
$p = $test;
$p->want_replace = $test;
}
else {
$p = new Product();
Expand All @@ -139,21 +142,20 @@ public function select($request)
$p->multiple = 0;
$p->package_size = 0;
$p->portion_quantity = 0;
$p->want_replace = null;
$price_without_vat = null;
$vat_rate = null;
$package_price = null;
}

$p->want_replace = $want_replace;

foreach ($columns as $index => $field) {
$value = trim($line[$index]);

if ($field == 'category') {
$field = $this->mapSelection(Category::class, 'name', $value, 'category', $p);
$field = $this->mapSelection($all_categories, 'name', $value, 'category', $p);
}
elseif ($field == 'measure') {
$field = $this->mapSelection(Measure::class, 'name', $value, 'measure', $p);
$field = $this->mapSelection($all_measures, 'name', $value, 'measure', $p);
}
elseif ($field == 'price') {
$value = guessDecimal($value);
Expand All @@ -165,7 +167,7 @@ public function select($request)
continue;
}
else {
$field = $this->mapSelection(VatRate::class, 'percentage', $value, 'vat_rate', $p);
$field = $this->mapSelection($all_vatrates, 'percentage', $value, 'vat_rate', $p);
$vat_rate = $value;
}
}
Expand Down Expand Up @@ -222,7 +224,7 @@ public function run($request)

foreach($data['import'] as $index) {
try {
if ($data['want_replace'][$index] != '0') {
if (isset($data['want_replace'][$index]) && $data['want_replace'][$index] != '0') {
$p = Product::find($data['want_replace'][$index]);
}
else {
Expand Down Expand Up @@ -264,8 +266,15 @@ public function run($request)
}
}

if ($request->has('reset_list')) {
$s->products()->whereNotIn('id', $products_ids)->update(['active' => false]);
$reset_mode = $request->input('reset_list');
switch($reset_mode) {
case 'disable':
$s->products()->whereNotIn('id', $products_ids)->update(['active' => false]);
break;

case 'remove':
$s->products()->whereNotIn('id', $products_ids)->delete();
break;
}

DB::commit();
Expand Down
3 changes: 2 additions & 1 deletion code/app/Modifier.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Support\Collection;
use GeneaLabs\LaravelModelCaching\Traits\Cachable;

use Log;
Expand All @@ -31,7 +32,7 @@ public function target(): MorphTo
public function getDefinitionsAttribute()
{
$ret = json_decode($this->definition);
return collect($ret ?: []);
return new Collection($ret ?: []);
}

public function getModelTypeAttribute()
Expand Down
3 changes: 2 additions & 1 deletion code/app/Notifications/SupplierOrderShipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Collection;

use Log;
use Auth;
Expand Down Expand Up @@ -38,7 +39,7 @@ public function toMail($notifiable)
assegnato a molteplici GAS, che possono avere configurazioni diverse
per la formattazione delle mail
*/
$notifiable->setRelation('gas', collect($this->gas));
$notifiable->setRelation('gas', new Collection([$this->gas]));

$message = $this->formatMail($message, $notifiable, 'supplier_summary', [
'supplier_name' => $this->order->supplier->name,
Expand Down
39 changes: 21 additions & 18 deletions code/app/Observers/MovementObserver.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,25 @@ public function __construct()
$this->movements_hub = App::make('MovementsHub');
}

private function testPeer(&$movement, $metadata, $peer)
{
$type = sprintf('%s_type', $peer);
$id = sprintf('%s_id', $peer);

if (is_null($metadata->$type)) {
$movement->$type = null;
$movement->$id = null;
}
else {
if ($metadata->$type != $movement->$type) {
Log::error('Movimento ' . $movement->id . ': ' . $type . ' non coerente');
return false;
}
}

return true;
}

private function verifyConsistency($movement)
{
$metadata = $movement->type_metadata;
Expand All @@ -32,24 +51,8 @@ private function verifyConsistency($movement)
return false;
}

if (is_null($metadata->sender_type)) {
$movement->sender_type = null;
$movement->sender_id = null;
}
else {
if ($metadata->sender_type != $movement->sender_type) {
Log::error(_i('Movimento %d: sender_type non coerente (%s != %s)', [$movement->id, $metadata->sender_type, $movement->sender_type]));
return false;
}
}

if (is_null($metadata->target_type)) {
$movement->target_type = null;
$movement->target_id = null;
}
else {
if ($metadata->target_type != $movement->target_type) {
Log::error(_i('Movimento %d: target_type non coerente (%s != %s)', [$movement->id, $metadata->target_type, $movement->target_type]));
foreach(['sender', 'target'] as $peer) {
if ($this->testPeer($movement, $metadata, $peer) == false) {
return false;
}
}
Expand Down
14 changes: 14 additions & 0 deletions code/app/Providers/AppServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,20 @@ public function boot()
/** @var Collection $this */
return $this->sortBy(fn($b) => $b->user->printableName());
});

/*
Estrapola da una Collection il primo elemento il cui attributo $attr
ha un valore "simile" a $value
*/
Collection::macro('firstWhereAbout', function ($attr, $value) {
$value = preg_replace('/[^a-zA-Z0-9]*/', '', mb_strtolower(trim($value)));

/** @var Collection $this */
return $this->first(function($o, $k) use ($attr, $value) {
$test = preg_replace('/[^a-zA-Z0-9]*/', '', mb_strtolower(trim($o->$attr)));
return $test == $value;
});
});
}

public function register()
Expand Down
Loading

0 comments on commit 915a835

Please sign in to comment.