Skip to content

Commit aaee914

Browse files
author
realshade
committed
додано міграцію для видалення nullable з полів weight_initial та weight_used у таблиці filament_spools; оновлено логіку обробки даних у контролерах та запитах
1 parent 2541670 commit aaee914

File tree

9 files changed

+354
-240
lines changed

9 files changed

+354
-240
lines changed

app/Http/Controllers/Api/TaskController.php

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -49,10 +49,11 @@ public function afterPrint(AfterPrintRequest $request) : JsonResponse
4949

5050
$dataTasks = $this->parseFilename($request->filename);
5151
if ($dataTasks['success']) {
52-
DB::transaction(function() use ($request, $dataTasks, $printer) {
52+
$dataTasks['data']['new'] = $dataTasks['data']['old'];
53+
DB::transaction(function() use ($request, &$dataTasks, $printer) {
5354
// Обновление значений count_printed в PartTask
54-
foreach ($dataTasks['data']['tasks'] ?? [] as $taskData) {
55-
foreach ($taskData['parts'] ?? [] as $partData) {
55+
foreach ($dataTasks['data']['old']['tasks'] ?? [] as $taskID => $taskData) {
56+
foreach ($taskData['parts'] ?? [] as $partID => $partData) {
5657
if (!$partData['count_printing'] || !$partData['is_printing']) {
5758
continue;
5859
}
@@ -64,16 +65,20 @@ public function afterPrint(AfterPrintRequest $request) : JsonResponse
6465

6566
$partTask->count_printed += $partData['count_printing'];
6667
$partTask->save();
68+
$dataTasks['data']['new']['tasks'][ $taskID ]['parts'][ $partID ]['count_printed'] = $partTask->count_printed;
69+
$dataTasks['data']['new']['tasks'][ $taskID ]['parts'][ $partID ]['count_printing'] = 0;
70+
unset($dataTasks['data']['new']['tasks'][ $taskID ]['parts'][ $partID ]['is_printing']);
6771

6872
PrintingTaskLog::create([
69-
'part_task_id' => $partData['part_task_id'],
73+
'part_task_id' => $partID,
7074
'printer_id' => $printer->id,
7175
'count' => $partData['count_printing'],
7276
'event_source' => PrintTaskEventSource::API,
7377
]);
74-
75-
7678
}
79+
$task = Task::find($taskID);
80+
$dataTasks['data']['new']['tasks'][ $taskID ]['count_set_printing'] = 0;
81+
$dataTasks['data']['new']['tasks'][ $taskID ]['count_set_printed'] = $task->getCompletedSetsCount();
7782
}
7883

7984
$printer->printingTasks()->delete();
@@ -83,9 +88,8 @@ public function afterPrint(AfterPrintRequest $request) : JsonResponse
8388

8489
$dataSlots = $this->parseFilament($request, $printer);
8590
if ($dataSlots['success']) {
86-
8791
DB::transaction(function() use ($printer, &$dataSlots) {
88-
foreach ($dataSlots['data']['slots'] ?? [] as $slotName => $usedWeight) {
92+
foreach ($dataSlots['data']['input'] ?? [] as $slotName => $usedWeight) {
8993
/** @var PrinterFilamentSlot $slot */
9094
$slot = $printer->filamentSlots()->where('attribute', $slotName)->first();
9195
if (!$slot) {
@@ -94,14 +98,12 @@ public function afterPrint(AfterPrintRequest $request) : JsonResponse
9498
if ($slot->filamentSpool) {
9599
$filamentSpool = $slot->filamentSpool;
96100

97-
$dataSlots['data']['change'][ $slotName ] = [
101+
$dataSlots['data']['old'][ $slotName ] = [
98102
'filament_spool_id' => $filamentSpool->id,
99103
'filament_spool' => sprintf('#%d %s %s (%s)', $filamentSpool->id, $filamentSpool->filament->name, $filamentSpool->filament->type->name, $filamentSpool->filament->vendor->name),
100104
'weight_initial' => $filamentSpool->weight_initial,
101105
'weight_used' => $filamentSpool->weight_used,
102-
'weight_remaining' => $filamentSpool->weight_initial - $filamentSpool->weight_used,
103-
'subtracted' => $usedWeight,
104-
'weight_future' => $filamentSpool->weight_initial - $filamentSpool->weight_used - $usedWeight,
106+
'weight_remaining' => $filamentSpool->weight_remaining,
105107
'date_last_used' => $filamentSpool->date_last_used?->format('Y-m-d H:i:s'),
106108
];
107109

@@ -111,8 +113,16 @@ public function afterPrint(AfterPrintRequest $request) : JsonResponse
111113
$filamentSpool->date_first_used = now();
112114
}
113115
$filamentSpool->save();
116+
$dataSlots['data']['new'][ $slotName ] = [
117+
'filament_spool_id' => $filamentSpool->id,
118+
'filament_spool' => sprintf('#%d %s %s (%s)', $filamentSpool->id, $filamentSpool->filament->name, $filamentSpool->filament->type->name, $filamentSpool->filament->vendor->name),
119+
'weight_initial' => $filamentSpool->weight_initial,
120+
'weight_used' => $filamentSpool->weight_used,
121+
'weight_remaining' => $filamentSpool->weight_remaining,
122+
'date_last_used' => $filamentSpool->date_last_used?->format('Y-m-d H:i:s'),
123+
];
114124
} else {
115-
$dataSlots['data']['change'][ $slotName ] = [
125+
$dataSlots['data']['old'][ $slotName ] = [
116126
'filament_spool_id' => null,
117127
'filament_spool' => __('printer.filament_slot.empty'),
118128
'weight_initial' => null,
@@ -153,7 +163,7 @@ public function beforePrint(BeforePrintRequest $request) : JsonResponse
153163
$result['tasks'] = $dataFilename;
154164

155165
if ($dataFilename['success']) {
156-
foreach ($dataFilename['data']['tasks'] as $taskData) {
166+
foreach ($dataFilename['data']['old']['tasks'] as $taskData) {
157167
foreach ($taskData['parts'] ?? [] as $partData) {
158168
if ($partData['count_printing']) {
159169
PrintingTask::create([

app/Http/Controllers/Filament/FilamentSpoolController.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ public function store(FilamentSpoolRequest $request) : JsonResponse
6262
for ($i = 0; $i < $quantity; $i++) {
6363
$spool = new FilamentSpool($validatedData);
6464
$spool->user_id = auth()->id();
65-
$spool->weight_initial = $packaging->weight;
6665
$spool->cost = $validatedData['cost'] ?? null;
6766
$spool->save();
6867
}

app/Http/Requests/Filament/FilamentSpoolRequest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public function rules() : array
4545
if ($this->spool) {
4646
$attr = array_merge($attr, [
4747
'weight_initial' => 'required|numeric|min:0',
48-
'weight_used' => 'nullable|numeric|min:0|lte:weight_initial',
48+
'weight_used' => 'nullable|numeric|min:0',
4949
'date_first_used' => 'nullable|date',
5050
'date_last_used' => 'nullable|date|after_or_equal:date_first_used',
5151
]);

app/Models/FilamentSpool.php

Lines changed: 34 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,19 @@
1313
/**
1414
* FilamentSpool
1515
*
16-
* @property int $id
17-
* @property int $filament_id
18-
* @property int $filament_packaging_id
19-
* @property float $weight_initial
20-
* @property float $weight_used
21-
* @property Carbon|null $date_first_used
22-
* @property Carbon|null $date_last_used
23-
* @property float $cost
24-
* @property int $user_id
25-
* @property Filament $filament
16+
* @property int $id
17+
* @property int $filament_id
18+
* @property int $filament_packaging_id
19+
* @property float $weight_initial
20+
* @property float $weight_used
21+
* @property Carbon|null $date_first_used
22+
* @property Carbon|null $date_last_used
23+
* @property float $cost
24+
* @property int $user_id
25+
* @property Filament $filament
2626
* @property FilamentUsedLog[]|null $filamentUsed
27-
* @property FilamentPackaging $packaging
27+
* @property FilamentPackaging $packaging
28+
* @property-read float $weight_remaining
2829
*/
2930
class FilamentSpool extends Model
3031
{
@@ -55,7 +56,7 @@ public function filament() : BelongsTo
5556
return $this->belongsTo(Filament::class);
5657
}
5758

58-
public function filamentUsed() : HasMany
59+
public function filamentUsedLog() : HasMany
5960
{
6061
return $this->hasMany(FilamentUsedLog::class)->latest('id');
6162
}
@@ -66,11 +67,6 @@ public function packaging() : BelongsTo
6667
}
6768

6869
/* **************************************** Getters **************************************** */
69-
public function getRemainingWeightAttribute() : float
70-
{
71-
return round($this->weight_initial - ($this->weight_used ?? 0), 4);
72-
}
73-
7470
public function getUsedPercentageAttribute() : float
7571
{
7672
if (!$this->weight_initial) {
@@ -79,4 +75,25 @@ public function getUsedPercentageAttribute() : float
7975

8076
return ($this->weight_used ?? 0) / $this->weight_initial * 100;
8177
}
78+
79+
public function getWeightRemainingAttribute() : float
80+
{
81+
return round(max(0, $this->weight_initial - $this->weight_used), 4);
82+
}
83+
84+
/* **************************************** Protected **************************************** */
85+
protected static function boot() : void
86+
{
87+
parent::boot();
88+
89+
static::creating(function($model) {
90+
if ($model->filament_packaging_id) {
91+
$packaging = FilamentPackaging::find($model->filament_packaging_id);
92+
if ($packaging) {
93+
$model->weight_initial = $packaging->weight;
94+
}
95+
}
96+
});
97+
}
98+
8299
}

app/Traits/ParseFilament.php

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
trait ParseFilament
99
{
1010

11-
/* **************************************** Public **************************************** */
11+
/* **************************************** Public **************************************** */
1212
public function parseFilament(AfterPrintRequest $request, Printer $printer) : array
1313
{
1414
$result = [
@@ -35,27 +35,21 @@ public function parseFilament(AfterPrintRequest $request, Printer $printer) : ar
3535

3636
foreach ($slots as $slotName => $weight) {
3737
if (!is_numeric($weight)) {
38-
return [
39-
'success' => false,
40-
'errors' => [
41-
'slots' => __('printer.validation.slots.float'),
42-
],
43-
];
38+
$result['success'] = false;
39+
$result['errors'][ $slotName ] = __('printer.validation.slots.not_found', ['slot' => $slotName]);
40+
continue;
4441
}
4542

4643
$slot = $printer->filamentSlots()
4744
->where('attribute', $slotName)
4845
->first();
4946
if (!$slot) {
50-
return [
51-
'success' => false,
52-
'errors' => [
53-
'slots' => __('printer.validation.slots.not_found', ['slot' => $slotName]),
54-
],
55-
];
47+
$result['success'] = false;
48+
$result['errors'][ $slotName ] = __('printer.validation.slots.not_found', ['slot' => $slotName]);
49+
continue;
5650
}
5751

58-
$result['data']['slots'][ $slotName ] = $weight;
52+
$result['data']['input'][ $slotName ] = $weight;
5953
}
6054

6155
return $result;

app/Traits/ParseFilename.php

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ private function validateParsedData(array $parsedData) : array
9494
return (int)($part->pivot->count_printed / $part->pivot->count_per_set);
9595
})->min() ?? 0,
9696
'count_set_printing' => 0,
97-
'count_set_future' => 0,
9897
];
9998
// ... також заповнюємо дані частин
10099
foreach ($parts as $part) {
@@ -109,7 +108,6 @@ private function validateParsedData(array $parsedData) : array
109108
'count_required' => $part->pivot->count_per_set * $task->count_set_planned,
110109
'count_printed' => $part->pivot->count_printed,
111110
'count_printing' => 0,
112-
'count_future' => $part->pivot->count_printed,
113111
];
114112
}
115113
}
@@ -125,14 +123,12 @@ private function validateParsedData(array $parsedData) : array
125123
// якщо частина є, то додаємо до неї дані
126124
$part = &$taskData[ $item['task_id'] ]['parts'][ $item['part_id'] ];
127125
$part['count_printing'] += $item['count'];
128-
$part['count_future'] += $item['count'];
129126
$part['is_printing'] = true; // если часть указана, то считаем, что она печатается
130127
} else {
131128
if (!empty($taskData[ $item['task_id'] ]['parts'])) {
132129
foreach ($taskData[ $item['task_id'] ]['parts'] as &$part) {
133130
// якщо частина не вказана, то вважаємо, що це друкується вся партія
134131
$part['count_printing'] += $item['count'] * $part['count_per_set'];
135-
$part['count_future'] += $item['count'] * $part['count_per_set'];
136132
$part['is_printing'] = true;
137133
}
138134
}
@@ -152,16 +148,12 @@ private function validateParsedData(array $parsedData) : array
152148
$item['count_set_printing'] = min(array_map(function($part) {
153149
return (int)($part['count_printing'] / $part['count_per_set']);
154150
}, $item['parts']));
155-
156-
$item['count_set_future'] = min(array_map(function($part) {
157-
return (int)($part['count_future'] / $part['count_per_set']);
158-
}, $item['parts']));
159151
}
160152
}
161153
unset($item);
162154

163155
$result['success'] = true;
164-
$result['data']['tasks'] = $taskData;
156+
$result['data']['old']['tasks'] = $taskData;
165157

166158
return $result;
167159
}

database/factories/PrinterFilamentSlotFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ class PrinterFilamentSlotFactory extends Factory
1515
public function definition() : array
1616
{
1717
return [
18-
'name' => $this->faker->name(),
18+
'name' => $this->faker->unique()->name(),
1919
'attribute' => $this->faker->word(),
2020
'description' => $this->faker->text(),
2121
'created_at' => Carbon::now(),
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
return new class extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*/
12+
public function up(): void
13+
{
14+
Schema::table('filament_spools', function (Blueprint $table) {
15+
DB::table('filament_spools')->whereNull('weight_initial')->update(['weight_initial' => 0]);
16+
DB::table('filament_spools')->whereNull('weight_used')->update(['weight_used' => 0]);
17+
$table->decimal('weight_initial', 10, 4)->default(0)->change();
18+
$table->decimal('weight_used', 10, 4)->default(0)->change();
19+
});
20+
}
21+
22+
/**
23+
* Reverse the migrations.
24+
*/
25+
public function down(): void
26+
{
27+
Schema::table('filament_spools', function (Blueprint $table) {
28+
$table->decimal('weight_initial', 10, 4)->nullable()->change();
29+
$table->decimal('weight_used', 10, 4)->nullable()->change();
30+
DB::table('filament_spools')->where('weight_initial', 0)->update(['weight_initial' => null]);
31+
DB::table('filament_spools')->where('weight_used', 0)->update(['weight_used' => null]);
32+
});
33+
}
34+
};

0 commit comments

Comments
 (0)