updateOrInsert( [ 'run_type' => $runType, 'period_start' => $periodStart->toDateString(), 'period_end' => $periodEnd->toDateString(), ], [ 'status' => 'running', 'before_total' => 0, 'before_quantity' => 0, 'before_amount' => 0, 'before_estimated_amount' => 0, 'after_total' => 0, 'after_quantity' => 0, 'after_amount' => 0, 'after_estimated_amount' => 0, 'diff_total' => 0, 'diff_quantity' => 0, 'diff_amount' => 0, 'diff_estimated_amount' => 0, 'details' => null, 'error_message' => null, 'started_at' => $now, 'finished_at' => null, 'created_at' => $now, 'updated_at' => $now, ], ); return (int) DB::table(self::TABLE) ->where('run_type', $runType) ->where('period_start', $periodStart->toDateString()) ->where('period_end', $periodEnd->toDateString()) ->value('id'); } /** * @param array{total:int,quantity:float,amount:float,estimated_amount_after_discount:float} $before * @param array{total:int,quantity:float,amount:float,estimated_amount_after_discount:float} $after * @param array $details */ public function complete(int $id, array $before, array $after, array $details = []): void { DB::table(self::TABLE)->where('id', $id)->update([ 'status' => 'succeeded', 'before_total' => $before['total'], 'before_quantity' => $before['quantity'], 'before_amount' => $before['amount'], 'before_estimated_amount' => $before['estimated_amount_after_discount'], 'after_total' => $after['total'], 'after_quantity' => $after['quantity'], 'after_amount' => $after['amount'], 'after_estimated_amount' => $after['estimated_amount_after_discount'], 'diff_total' => $after['total'] - $before['total'], 'diff_quantity' => round($after['quantity'] - $before['quantity'], 2), 'diff_amount' => round($after['amount'] - $before['amount'], 2), 'diff_estimated_amount' => round( $after['estimated_amount_after_discount'] - $before['estimated_amount_after_discount'], 2, ), 'details' => $details === [] ? null : json_encode($details, JSON_UNESCAPED_UNICODE | JSON_PRESERVE_ZERO_FRACTION), 'error_message' => null, 'finished_at' => now(), 'updated_at' => now(), ]); } public function fail(int $id, string $message): void { DB::table(self::TABLE)->where('id', $id)->update([ 'status' => 'failed', 'error_message' => mb_substr($message, 0, 2000), 'finished_at' => now(), 'updated_at' => now(), ]); } /** @return array{daily:?array,monthly:?array} */ public function latestSummary(): array { if (! Schema::hasTable(self::TABLE)) { return ['daily' => null, 'monthly' => null]; } return [ 'daily' => $this->latest('daily'), 'monthly' => $this->latest('monthly'), ]; } /** @return array|null */ private function latest(string $runType): ?array { $row = DB::table(self::TABLE) ->where('run_type', $runType) ->where('status', 'succeeded') ->orderByDesc('finished_at') ->orderByDesc('id') ->first(); if ($row === null) { return null; } $result = (array) $row; foreach ([ 'before_quantity', 'before_amount', 'before_estimated_amount', 'after_quantity', 'after_amount', 'after_estimated_amount', 'diff_quantity', 'diff_amount', 'diff_estimated_amount', ] as $key) { $result[$key] = (float) $result[$key]; } foreach (['before_total', 'after_total', 'diff_total'] as $key) { $result[$key] = (int) $result[$key]; } return $result; } }