126 lines
4.1 KiB
PHP
126 lines
4.1 KiB
PHP
<?php
|
|
|
|
namespace Modules\Maintenance\Services;
|
|
|
|
use App\User;
|
|
use Modules\Maintenance\Models\Equipment;
|
|
use Modules\Maintenance\Models\WorkOrder;
|
|
use Modules\Maintenance\Utils\MaintenanceUtil;
|
|
use Modules\Project\Entities\Project;
|
|
|
|
class MaintenanceCrmSyncService
|
|
{
|
|
public function __construct(
|
|
protected MaintenanceUtil $maintenanceUtil
|
|
) {}
|
|
|
|
/**
|
|
* همگامسازی مشتری از پروژه (CRM → CMMS)
|
|
*/
|
|
public function syncCustomerFromProject(Equipment $equipment): Equipment
|
|
{
|
|
if (! $equipment->project_id || $equipment->customer_id) {
|
|
return $equipment;
|
|
}
|
|
|
|
if (! class_exists(Project::class)) {
|
|
return $equipment;
|
|
}
|
|
|
|
$project = Project::find($equipment->project_id);
|
|
if ($project && $project->contact_id) {
|
|
$equipment->customer_id = $project->contact_id;
|
|
if ($equipment->client_type === 'project') {
|
|
$equipment->client_type = 'factory';
|
|
}
|
|
}
|
|
|
|
return $equipment;
|
|
}
|
|
|
|
/**
|
|
* ثبت پیگیری CRM هنگام ایجاد تجهیز (CMMS → CRM)
|
|
*/
|
|
public function notifyCrmOnEquipmentCreated(Equipment $equipment, User $user): void
|
|
{
|
|
if (! $equipment->customer_id) {
|
|
return;
|
|
}
|
|
|
|
if (! $this->maintenanceUtil->isCrmEnabled($user->business_id)) {
|
|
return;
|
|
}
|
|
|
|
$settings = $this->maintenanceUtil->getMaintenanceSettings($user->business_id);
|
|
if (empty($settings['crm_sync_followup_on_equipment'])) {
|
|
return;
|
|
}
|
|
|
|
if (! class_exists(\Modules\Crm\Entities\Schedule::class)) {
|
|
return;
|
|
}
|
|
|
|
$now = now();
|
|
$schedule = \Modules\Crm\Entities\Schedule::create([
|
|
'business_id' => $user->business_id,
|
|
'contact_id' => $equipment->customer_id,
|
|
'title' => 'ثبت تجهیز: '.$equipment->name,
|
|
'status' => 'completed',
|
|
'start_datetime' => $now,
|
|
'end_datetime' => $now->copy()->addHour(),
|
|
'description' => 'تجهیز «'.$equipment->name.'» (کد: '.$equipment->code.') در سیستم نگهداری ثبت شد.'
|
|
.($equipment->production_line_id ? ' خط تولید مرتبط دارد.' : ''),
|
|
'schedule_type' => 'meeting',
|
|
'allow_notification' => 0,
|
|
'notify_via' => ['sms' => 0, 'mail' => 0],
|
|
'notify_before' => null,
|
|
'notify_type' => 'hour',
|
|
'created_by' => $user->id,
|
|
]);
|
|
|
|
$schedule->users()->sync([$user->id]);
|
|
}
|
|
|
|
/**
|
|
* ثبت پیگیری CRM هنگام تکمیل دستور کار (CMMS → CRM)
|
|
*/
|
|
public function notifyCrmOnWorkOrderCompleted(WorkOrder $workOrder, User $user): void
|
|
{
|
|
$equipment = $workOrder->equipment;
|
|
if (! $equipment || ! $equipment->customer_id) {
|
|
return;
|
|
}
|
|
|
|
if (! $this->maintenanceUtil->isCrmEnabled($user->business_id)) {
|
|
return;
|
|
}
|
|
|
|
$settings = $this->maintenanceUtil->getMaintenanceSettings($user->business_id);
|
|
if (empty($settings['crm_sync_followup_on_work_order'])) {
|
|
return;
|
|
}
|
|
|
|
if (! class_exists(\Modules\Crm\Entities\Schedule::class)) {
|
|
return;
|
|
}
|
|
|
|
$now = now();
|
|
$schedule = \Modules\Crm\Entities\Schedule::create([
|
|
'business_id' => $user->business_id,
|
|
'contact_id' => $equipment->customer_id,
|
|
'title' => 'تکمیل دستور کار: '.$workOrder->work_order_number,
|
|
'status' => 'completed',
|
|
'start_datetime' => $now,
|
|
'end_datetime' => $now->copy()->addHour(),
|
|
'description' => 'دستور کار «'.$workOrder->work_order_number.'» برای تجهیز «'.$equipment->name.'» تکمیل شد.'
|
|
.($workOrder->cost > 0 ? ' هزینه: '.$workOrder->cost : ''),
|
|
'schedule_type' => 'call',
|
|
'allow_notification' => 0,
|
|
'notify_via' => ['sms' => 0, 'mail' => 0],
|
|
'created_by' => $user->id,
|
|
]);
|
|
|
|
$schedule->users()->sync([$user->id]);
|
|
}
|
|
}
|