hasValue($code)) return null; $customer = Customer::where('customer_code', $code)->first(); return $customer; } /** * データを新規登録する * * @param array $input 登録データ * @return Customer 得意先 */ public function store(array $input) { $customer = Customer::create($input); return $customer; } /** * データを更新する * * @param int $id 得意先ID * @param array $input 更新データ * @return Customer 得意先 */ public function update(int $id, array $input) { $customer = $this->findOrFail($id); $customer = $customer->fill($input); $customer->save(); return $customer; } /** * データを削除する * * @param int $id 得意先ID * @return Customer 得意先 */ public function destroy(int $id) { $customer = $this->findOrFail($id); $customer->delete(); return $customer; } /** * 条件よりデータリストを取得する * * @param object $input 検索条件 * @return Collection 取得データ */ public function retrieve(object $input) { $query = $this->getListQuery($input); $query->orderBy('id', 'asc'); $data = $query->get(); return $data; } /** * 条件よりページ単位でデータリストを取得する * * @param object $input 検索条件 * @return LengthAwarePaginator 取得データ */ public function paginate(object $input) { $query = $this->getListQuery($input); $query->addSelect('denominations.denomination_name'); $query->addSelect('business_types.business_type_name'); $query->leftJoin('denominations', 'denominations.id', 'customers.denomination_id'); $query->leftJoin('business_types', 'business_types.id', 'customers.business_type_id'); $query = $this->getSortQuery($query, $input); $data = $query->paginate($input->perpage, ['*'], 'page', $input->page); return $data; } /** * 条件より候補リストを取得する * * @param object $input 検索条件 * @return Collection 取得データ */ public function getCandidates(object $input) { $query = $this->getListQuery($input); $query->active(); $query->select('customers.id'); $query->addSelect('customers.customer_code AS code'); $query->addSelect('customers.customer_name AS name'); $query->orderBy('customers.customer_code', 'asc'); $data = $query->limit(50)->get(); // 初回50件制限 return $data; } /** * 条件より候補リストを取得する(ページネーション付き) * * @param object $input 検索条件 * @param int $page ページ番号 * @param int $perPage 1ページあたりの件数 * @return Collection 取得データ */ public function getCandidatesPaginated(object $input, $page = 1, $perPage = 50) { $query = Customer::query(); $query->active(); $query->select('customers.id'); $query->addSelect('customers.customer_code AS code'); $query->addSelect('customers.customer_name AS name'); $query->orderBy('customers.customer_code', 'asc'); $offset = ($page - 1) * $perPage; $data = $query->offset($offset)->limit($perPage)->get(); return $data->toArray(); } /** * キーワードで候補リストを検索する * * @param string $keyword 検索キーワード * @return Collection 候補リスト */ public function findByKeyword(string $keyword) { if (!$this->hasValue($keyword)) return collect([]); $query = Customer::query(); $query->select('id', 'customer_code', 'customer_name'); $query->where(function($q) use ($keyword) { $q->where('customer_code', 'LIKE', $keyword . '%') ->orWhere('customer_name', 'LIKE', '%' . $keyword . '%'); }); $query->orderBy('customer_code', 'asc'); $query->limit(100); // キーワード検索は100件まで return $query->get(); } /** * |条件よりデータリストを取得するクエリを生成する * * @param object $input 検索条件 * @return QueryBuilder クエリ */ private function getListQuery(object $input) { $query = Customer::query(); $query->select('customers.*'); // 得意先コード|customer_code_from, customer_code_to if ($this->has($input, 'customer_code_from')) { $query->where('customer_code', '>=', $input->customer_code_from); } if ($this->has($input, 'customer_code_to')) { $query->where('customer_code', '<=', $input->customer_code_to); } // 名前|customer_name if ($this->has($input, 'customer_name')) { //$query->where('customer_name', $input->customer_name); $where = $this->whereName($input->customer_name, 'customer_name'); $query->whereRaw($where); } if ($this->has($input, 'name')) { $where = $this->whereName($input->name, 'customer_name'); $query->whereRaw($where); } // 月の締め日|closing_day if ($this->has($input, 'closing_day')) { $query->where('closing_day', $input->closing_day); } // 金種名|denomination_id if ($this->has($input, 'denomination_id')) { $query->where('denomination_id', $input->denomination_id); } // 入金日|payment_day if ($this->has($input, 'payment_day')) { $query->where('payment_day', $input->payment_day); } // 入金サイクル|payment_cycle if ($this->has($input, 'payment_cycle')) { $query->where('payment_cycle', $input->payment_cycle); } // 業態|business_type_id if ($this->has($input, 'business_type_id')) { $query->where('business_type_id',$input->business_type_id); } // 営業担当者|sales_rep_id if ($this->has($input, 'sales_rep_id')) { $query->addSelect('customer_category_sales_reps.sales_rep_id'); $query->join('customer_category_sales_reps','customer_id', 'customers.id'); $query->where('customer_category_sales_reps.sales_rep_id',$input->sales_rep_id); } // 得意先グループID |customer_group_id if ($this->has($input, 'customer_group_id')) { $query->where('customer_group_id',$input->customer_group_id); } <<<<<<< HEAD // typeパラメータによるフィルタリング追加 if (isset($input->type)) { switch ($input->type) { case 'payment-receipt-parent': // 入金先用:親得意先のみ $query->where(function($q) { $q->whereNull('customers.payment_receipt_customer_id') ->orWhereColumn('customers.payment_receipt_customer_id', 'customers.id'); }); break; case 'payment-receipt-child': // 得意先用:入金先自身または入金先に紐づく子得意先 if (isset($input->parent_payment_receipt_customer_id) && $input->parent_payment_receipt_customer_id) { $query->where(function($q) use ($input) { $q->where('customers.id', $input->parent_payment_receipt_customer_id) // 入金先自身 ->orWhere('customers.payment_receipt_customer_id', $input->parent_payment_receipt_customer_id); // 子得意先 }); } else { $query->whereRaw('1 = 0'); // 空の結果 } break; default: // 既存機能:制限なし(何もしない) break; } } ======= // 無効|invalid_yes_no if ($this->has($input, 'invalid_yes_no') && $input->invalid_yes_no != 0) { $where = $this->whereName( $input->invalid_yes_no == 1 ? 9 : 1, 'customers.is_active'); $query->whereRaw($where); } >>>>>>> master return $query; } /** * |クエリにソート条件を追加する * * @param QueryBuilder クエリ * @param object $input 検索条件 * @return QueryBuilder クエリ */ private function getSortQuery($query, object $input = null) { list($key, $type) = $this->getSortKey($input); switch ($key) { //|無効 case 'active': $query->orderBy('is_active', $type); break; //|得意先コード case 'customer_code': $query->orderBy('customer_code', $type); break; //|得意先名 case 'customer_name': $query->orderBy('customer_name', $type); break; //|電話番号 case 'tel': $query->orderBy('tel', $type); break; //FAX番号 case 'fax': $query->orderBy('fax', $type); break; //郵便番号 case 'delivery_name': $query->orderBy('postal_code', $type); break; //都道府県 case 'prefecture': $query->orderBy('prefecture', $type); break; //住所1 case 'address1': $query->orderBy('address1', $type); break; //住所2 case 'address2': $query->orderBy('address2', $type); break; //代表者名 case 'president_name': $query->orderBy('president_name', $type); break; //締日 case 'closing_day': $query->orderBy('closing_day', $type); break; //金種名 case 'denomination_name': $query->orderBy('denomination_name', $type); break; //入金日 case 'payment_day': $query->orderBy('payment_day', $type); break; //入金サイクル case 'payment_cycle': $query->orderBy('payment_cycle', $type); break; //業態 case 'business_type_name': $query->orderBy('business_type_name', $type); break; //更新日時 case 'updated_at': $query->orderBy('updated_at', $type); break; default: } return $query; } /** <<<<<<< HEAD * 入金入力で選択可能な得意先を取得 * (payment_receipt_customer_idがNULLまたは自分自身のid) * * @return Collection 得意先リスト */ public function getAvailableCustomersForPaymentReceipt() { return Customer::where(function($query) { $query->whereNull('payment_receipt_customer_id') ->orWhereColumn('payment_receipt_customer_id', 'id'); }) ->active() ->select('id', 'customer_code', 'customer_name') ->orderBy('customer_code') ->get(); } /** * 得意先とその子会社のIDを取得 * * @param int $customerId 得意先ID * @return array 得意先ID配列 */ public function getCustomerIdsWithChildren($customerId) { $customerIds = [$customerId]; // 子会社を取得 $children = Customer::where('payment_receipt_customer_id', $customerId) ->pluck('id') ->toArray(); return array_merge($customerIds, $children); } /** * 指定したID配列の得意先データを取得 * * @param array $customerIds 得意先ID配列 * @return Collection 得意先データ */ public function getCustomersByIds($customerIds) { return Customer::whereIn('id', $customerIds) ->select('id', 'customer_code', 'customer_name', 'payment_receipt_customer_id') ->orderBy('customer_code') ->get(); } ======= * 条件よりデータリストを取得する(Excel出力用) * * @param object $input 検索条件 * @return Collection 取得データ */ public function getExcelList(object $input) { $query = $this->getListQuery($input); $query = $this->getSortQuery($query, $input); $data = $query->get(); return $data; } >>>>>>> master }