@@ -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 ([
0 commit comments