Skip to content

Commit

Permalink
tolta opzione per eliminare prodotti in fase di importazione; potenzi…
Browse files Browse the repository at this point in the history
…ato unit test importazione prodotti csv
  • Loading branch information
madbob committed Nov 12, 2023
1 parent 90b2001 commit 6bcfc02
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 6 deletions.
6 changes: 1 addition & 5 deletions code/app/Importers/CSV/Products.php
Original file line number Diff line number Diff line change
Expand Up @@ -266,15 +266,11 @@ public function run($request)
}
}

$reset_mode = $request->input('reset_list');
$reset_mode = $request->input('reset_list', 'no');
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
13 changes: 13 additions & 0 deletions code/app/Order.php
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,19 @@ public function involvedModifiers($include_shipping_places = false)
public function applyModifiers($aggregate_data = null, $enforce_status = false)
{
$modifiers = $this->involvedModifiers(true);

/*
TODO: se ci sono prenotazioni consegnate, non posso fidarmi
dell'elenco teorico dei modificatori in quanto potrebbero essercene
altri assegnati (e.g. quelli usati per le consegne manuali) dunque
devo andarli a leggere direttamente dalle prenotazioni stesse.
D'altro canto, per le prenotazioni consegnate non serve ottenere una
Redux dell'ordine in quanto appunto vado a leggermi i dati dai
modificatori già calcolati e salvati sul DB, dunque compio questa
(lenta) operazione inutilmente.
Questa funzione potrebbe essere un po' più complessa, e generare una
Redux complessiva solo in presenza di prenotazioni non consegnate
*/
$has_shipped_bookings = $this->bookings->where('status', '!=', 'pending')->count() != 0;

if ($modifiers->isEmpty() == false || $has_shipped_bookings) {
Expand Down
2 changes: 1 addition & 1 deletion code/resources/views/import/csvproductsselect.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

<div class="row">
<div class="col-md-6">
<x-larastrap::radios name="reset_list" :label="_i('Prodotti Esistenti')" :options="['no' => _i('Ignora'), 'disable' => _i('Disabilita'), 'remove' => _i('Elimina')]" value="no" />
<x-larastrap::radios name="reset_list" :label="_i('Prodotti Esistenti')" :options="['no' => _i('Ignora'), 'disable' => _i('Disabilita')]" value="no" />
</div>
</div>

Expand Down
53 changes: 53 additions & 0 deletions code/tests/ImportersTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
use App\Notifications\ManualWelcomeMessage;
use App\User;
use App\Supplier;
use App\Category;
use App\Measure;

class ImportersTest extends TestCase
{
Expand Down Expand Up @@ -95,6 +97,57 @@ public function testProductsCsv()
$this->assertEquals('Frutta secca', $data['products'][9]->temp_category_name);
$this->assertEquals('Sacchetti', $data['products'][9]->temp_measure_name);
$this->assertEquals(0, $data['products'][9]->vat_rate_id);

$final_block = [
'supplier_id' => $supplier->id,
'reset_list' => 'disable',
'import' => [],
'weight' => [],
'package_size' => [],
'min_quantity' => [],
'multiple' => [],
'portion_quantity' => [],
'name' => [],
'description' => [],
'price' => [],
'category_id' => [],
'measure_id' => [],
'vat_rate_id' => [],
'supplier_code' => [],
'want_replace' => [],
];

foreach($data['products'] as $index => $prod) {
$final_block['import'][] = $index;
$final_block['weight'][] = $prod->weight;
$final_block['package_size'][] = $prod->package_size;
$final_block['min_quantity'][] = $prod->min_quantity;
$final_block['multiple'][] = $prod->multiple;
$final_block['portion_quantity'][] = $prod->portion_quantity;
$final_block['name'][] = $prod->name;
$final_block['description'][] = $prod->description;
$final_block['price'][] = $prod->price;
$final_block['category_id'][] = $prod->temp_category_name ? sprintf('new:%s', $prod->temp_category_name) : $prod->category_id;
$final_block['measure_id'][] = $prod->temp_measure_name ? sprintf('new:%s', $prod->temp_measure_name) : $prod->measure_id;
$final_block['vat_rate_id'][] = $prod->vat_rate_id;
$final_block['supplier_code'][] = $prod->supplier_code;
$final_block['want_replace'][] = $prod->want_replace ? $prod->want_replace->id : 0;
}

$request = new \Illuminate\Http\Request();
$request->merge($final_block);

$data = $importer->run($request);

$this->nextRound();

$supplier = $supplier->fresh();
$this->assertEquals(10, $supplier->products()->count());

$this->assertNotNull(Category::where('name', 'Biscotti e dolci')->first());
$this->assertNotNull(Category::where('name', 'Frutta secca')->first());
$this->assertNotNull(Measure::where('name', 'Barattoli')->first());
$this->assertNotNull(Measure::where('name', 'Sacchetti')->first());
}

public function testUsersCsv()
Expand Down

0 comments on commit 6bcfc02

Please sign in to comment.