| @@ -0,0 +1,78 @@ | |||
| <?php | |||
| namespace App\Http\Controllers\Web\Custom\HelloTechno; | |||
| use App\Codes\UserRole; | |||
| use App\Files\CsvFile; | |||
| use App\Http\Controllers\Web\IParam; | |||
| use App\Repositories\Custom\HelloTechno\UseSummaryRepository as Repository; | |||
| use Illuminate\Http\Request; | |||
| use Illuminate\Support\Facades\Auth; | |||
| use Symfony\Component\HttpFoundation\BinaryFileResponse; | |||
| class UseSummaryCSVController extends HelloTechnoController | |||
| { | |||
| public function name(): string | |||
| { | |||
| return "[HelloTechno専用]利用実績一覧CSV取得"; | |||
| } | |||
| public function description(): string | |||
| { | |||
| return "[HelloTechno専用]利用実績の一覧CSVを取得する"; | |||
| } | |||
| public function __construct( | |||
| protected UseSummaryCSVParam $param, | |||
| private Repository $repository | |||
| ) { | |||
| parent::__construct(); | |||
| $this->roleAllow(UserRole::NORMAL_ADMIN); | |||
| } | |||
| protected function getParam(): IParam | |||
| { | |||
| return $this->param; | |||
| } | |||
| protected function run(Request $request): BinaryFileResponse | |||
| { | |||
| $param = $this->param; | |||
| $condition = [ | |||
| ...$param->toArray(), | |||
| Repository::CONDITION_CONTRACT_ID => Auth::user()->contract_id, | |||
| Repository::CONDITION_SORT_TARGET => 'customer_code', | |||
| ]; | |||
| $list = $this->repository->forCsv()->get($condition); | |||
| // $csv = new CsvFile($this->getHeaderLabel(), CsvFile::ENCODE_SJIS); | |||
| $csv = new CsvFile($this->getHeaderLabel()); | |||
| foreach ($list as $row) { | |||
| $csv->addLine($row->toArray()); | |||
| } | |||
| return $csv->download($this->getDownloadFileName()); | |||
| } | |||
| private function getHeaderLabel(): array | |||
| { | |||
| $headers = [ | |||
| '集計ID', | |||
| '集計年月', | |||
| '顧客コード', | |||
| '顧客名', | |||
| '駐車場管理コード', | |||
| '領収証発行依頼回数', | |||
| 'SMS送信件数', | |||
| ]; | |||
| return $headers; | |||
| } | |||
| private function getDownloadFileName() | |||
| { | |||
| return sprintf("利用実績_%s.csv", $this->param->summaryYyyymm); | |||
| } | |||
| } | |||
| @@ -0,0 +1,23 @@ | |||
| <?php | |||
| namespace App\Http\Controllers\Web\Custom\HelloTechno; | |||
| use App\Http\Controllers\Web\BaseParam; | |||
| use App\Repositories\Custom\HelloTechno\UseSummaryRepository as Repository; | |||
| /** | |||
| * @property ?string $summaryYyyymm | |||
| */ | |||
| class UseSummaryCSVParam extends BaseParam | |||
| { | |||
| public function rules(): array | |||
| { | |||
| return | |||
| array_merge( | |||
| [ | |||
| Repository::CONDITION_SUMMARY_YYYYMM => $this->str(), | |||
| ], | |||
| ); | |||
| } | |||
| } | |||
| @@ -26,4 +26,9 @@ class BaseRepositoryData extends stdClass | |||
| } | |||
| return $ret; | |||
| } | |||
| public function toArray(): array | |||
| { | |||
| return json_decode(json_encode($this), true); | |||
| } | |||
| } | |||
| @@ -8,6 +8,7 @@ use App\Repositories\BaseRepository; | |||
| use Illuminate\Database\Query\JoinClause; | |||
| use Illuminate\Support\Collection; | |||
| use Illuminate\Support\Facades\DB; | |||
| use LogicException; | |||
| class UseSummaryRepository extends BaseRepository | |||
| { | |||
| @@ -21,6 +22,17 @@ class UseSummaryRepository extends BaseRepository | |||
| const TABLE_SUMMARY = "summary"; | |||
| const TABLE_NAME = "name"; | |||
| private const SELECT_LIST = 'SELECT_LIST'; | |||
| private const SELECT_CSV = 'SELECT_CSV'; | |||
| private string $select = self::SELECT_LIST; | |||
| public function forCsv(): static | |||
| { | |||
| $this->select = self::SELECT_CSV; | |||
| return $this; | |||
| } | |||
| /** | |||
| * コレクションを取得する | |||
| @@ -53,7 +65,13 @@ class UseSummaryRepository extends BaseRepository | |||
| $this->where($table, $condition, static::CONDITION_SUMMARY_YYYYMM, $this->makeColumnName([static::TABLE_SUMMARY, UseByKeySummary::COL_NAME_SUMMARY_YYYYMM])); | |||
| $table->select($this->columns()); | |||
| if ($this->select === self::SELECT_LIST) { | |||
| $table->select($this->columns()); | |||
| } else if ($this->select === self::SELECT_CSV) { | |||
| $table->select($this->csvColumns()); | |||
| } else { | |||
| throw new LogicException("SELECT不正"); | |||
| } | |||
| $main = DB::table($table, "main"); | |||
| @@ -86,4 +104,20 @@ class UseSummaryRepository extends BaseRepository | |||
| ]; | |||
| return $columns; | |||
| } | |||
| private function csvColumns() | |||
| { | |||
| $summary = static::TABLE_SUMMARY; | |||
| $name = static::TABLE_NAME; | |||
| $columns = [ | |||
| $this->makeColumnNameForSelect([$summary, UseByKeySummary::COL_NAME_ID]), | |||
| $this->makeColumnNameForSelect([$summary, UseByKeySummary::COL_NAME_SUMMARY_YYYYMM]), | |||
| $this->makeColumnNameForSelect([$summary, UseByKeySummary::COL_NAME_SUMMARY_KEY1]), | |||
| $this->makeColumnNameForSelect([$name, HtCustomParkingName::COL_NAME_CUSTOMER_NAME]), | |||
| $this->makeColumnNameForSelect([$summary, UseByKeySummary::COL_NAME_SUMMARY_KEY2]), | |||
| $this->makeColumnNameForSelect([$name, HtCustomParkingName::COL_NAME_PARKING_NAME]), | |||
| $this->makeColumnNameForSelect([$summary, UseByKeySummary::COL_NAME_RECEIPT_ORDER_COUNT]), | |||
| $this->makeColumnNameForSelect([$summary, UseByKeySummary::COL_NAME_SMS_SEND_COUNT]), | |||
| ]; | |||
| return $columns; | |||
| } | |||
| } | |||