VendorContactModel.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. <?php
  2. namespace App\Models;
  3. use CodeIgniter\Model;
  4. class VendorContactModel extends Model
  5. {
  6. protected $table = 'vendor_contacts';
  7. protected $primaryKey = 'id';
  8. protected $useAutoIncrement = true;
  9. protected $returnType = 'array';
  10. protected $useSoftDeletes = false;
  11. protected $allowedFields = [
  12. 'vendor_id', 'contact_type', 'name', 'position',
  13. 'phone', 'email', 'is_primary'
  14. ];
  15. protected $useTimestamps = true;
  16. protected $createdField = 'created_at';
  17. protected $updatedField = 'updated_at';
  18. protected $validationRules = [
  19. 'vendor_id' => 'required|integer|is_not_unique[vendors.id]',
  20. 'contact_type' => 'required|in_list[PRIMARY,SECONDARY,BILLING,TECHNICAL]',
  21. 'name' => 'required|max_length[100]',
  22. 'position' => 'permit_empty|max_length[100]',
  23. 'phone' => 'permit_empty|max_length[20]',
  24. 'email' => 'permit_empty|max_length[255]|valid_email',
  25. 'is_primary' => 'permit_empty|in_list[0,1]'
  26. ];
  27. protected $validationMessages = [
  28. 'vendor_id' => [
  29. 'required' => '벤더사 id는 필수입니다.',
  30. 'is_not_unique' => '존재하지 않는 벤더사입니다.'
  31. ],
  32. 'name' => [
  33. 'required' => '담당자명은 필수입니다.'
  34. ],
  35. 'email' => [
  36. 'valid_email' => '유효하지 않은 이메일 형식입니다.'
  37. ]
  38. ];
  39. // 기본 담당자로 설정
  40. public function setPrimaryContact($vendorId, $contactId)
  41. {
  42. $this->db->transStart();
  43. // 기존 기본 담당자 해제
  44. $this->where('vendor_id', $vendorId)
  45. ->set('is_primary', 0)
  46. ->update();
  47. // 새로운 기본 담당자 설정
  48. $this->update($contactId, ['is_primary' => 1]);
  49. $this->db->transComplete();
  50. return $this->db->transStatus();
  51. }
  52. }