appends, "created_by_name"); } public function scopeWithoutSyncBoot($q) { $q->withoutGlobalScope('syncActiveServiceCategory') ->withoutGlobalScope('syncActiveSiteProject') ->withoutGlobalScope('syncActiveCityBranch') ->withoutGlobalScope('roleLevel1'); } protected static function writeCustomerChatLog() { return false; } protected static function orderIdForLog($model) { return $model->order_id ?? null; } protected static function projectIdForLog($model) { return $model->project_id ?? null; } protected static function customerIdForLog($model) { return $model->customer_id ?? null; } /** * samakan dengan translate "menu" * * @param $model setelah create/update * @return string */ protected static function customerLogAs($model) { return self::class; } /** * @return array [ * 0 => [ * id, * name * ] * ] */ protected static function appendOrderLogRelatedUsers($model) { return []; } protected static function withServiceCategory() { return true; } protected static function withCityBranchId() { return true; } protected static function withSiteProjectId() { return true; } protected static function withRoleLevel() { return true; } protected static function acceptNullCreatedBy() { return true; } protected static function createCustomerChatLog($model, $act) { $customerId = self::customerIdForLog($model); if ($customerId) { /** @var CustomerChatFacade */ $customerChatFacade = app(CustomerChatFacade::class); $customerChatFacade->createLog( (new CustomerChatAttr) ->setIsLog(true) ->setLogId($model->id) ->setlogName(self::customerLogAs($model)) ->setLogAct($act) ->setLogRelatedUsers(self::appendOrderLogRelatedUsers($model)) ->setCustomerId($customerId) ); } } protected static function boot() { parent::boot(); $tableName = (new self)->getTable(); if ($user = Auth::user()) { if (self::writeCustomerChatLog()) { self::created(function ($model) { self::createCustomerChatLog($model, 'created'); }); self::updated(function ($model) { self::createCustomerChatLog($model, 'updated'); }); self::deleted(function ($model) { self::createCustomerChatLog($model, 'deleted'); }); } // menambahkan created_by saat membuat data // dengan memanggil method "create" di model yg berkaitan static::creating(function (Model $item) use ($user) { $item->created_by = $user->id; //assigning value if (self::withServiceCategory() && $serviceCategoryId = getActiveServiceCategoryId()) { $item->service_category_id = $serviceCategoryId; if (self::withCityBranchId()) { if (getActiveCityBranchId() && !isset($item->city_branch_id)) { $item->city_branch_id = getActiveCityBranchId(); } } if (self::withSiteProjectId()) { if (getActiveSiteProjectId() && !isset($item->site_project_id)) { $item->site_project_id = getActiveSiteProjectId(); } } } }); // menambahkan kondisi site project harus sama dengan // user yang sedang login static::addGlobalScope('syncActiveServiceCategory', function (Builder $builder) use ($tableName) { if (self::withServiceCategory() and getActiveServiceCategoryId()) { $builder->where( $tableName . '.service_category_id', getActiveServiceCategoryId() ); } }); static::addGlobalScope('syncActiveCityBranch', function (Builder $builder) use ($tableName) { if (self::withCityBranchId() and getActiveCityBranchId()) { $builder->where( $tableName . '.city_branch_id', getActiveCityBranchId() ); } }); static::addGlobalScope('syncActiveSiteProject', function (Builder $builder) use ($tableName) { if (self::withSiteProjectId() and getActiveSiteProjectId()) { $builder->where( $tableName . '.site_project_id', getActiveSiteProjectId() ); } }); } else { static::creating(function (Model $item) { $item->service_category_id = getActiveServiceCategoryId(); $item->city_branch_id = getActiveCityBranchId(); $item->site_project_id = getActiveSiteProjectId(); }); if (self::withServiceCategory() && getActiveServiceCategoryId()) { // menambahkan kondisi site project harus sama dengan // user yang sedang login static::addGlobalScope('syncActiveServiceCategory', function (Builder $builder) use ($tableName) { return $builder->where( $tableName . '.service_category_id', getActiveServiceCategoryId() ); }); } if (getActiveCityBranchId()) { static::addGlobalScope('syncActiveCityBranch', function (Builder $builder) use ($tableName) { if (self::withCityBranchId() and getActiveCityBranchId()) { $builder->where( $tableName . '.city_branch_id', getActiveCityBranchId() ); } }); } if (getActiveSiteProjectId()) { static::addGlobalScope('syncActiveSiteProject', function (Builder $builder) use ($tableName) { if (self::withSiteProjectId() and getActiveSiteProjectId()) { $builder->where( $tableName . '.site_project_id', getActiveSiteProjectId() ); } }); } } } public function createdBy() { return $this->belongsTo(User::class, 'created_by')->select(["id", "name"]); } public function getCreatedByNameAttribute() { return $this->createdBy->name ?? ""; } }