<?php
namespace App\AI\Contexts\Entity; class ContactPerson extends \App\AI\Contexts\Entity\EntityAbstract { public function getDeveloperMessages(): array { $developerMessages = []; $developerMessages[] = "Never show internal IDs such as draft_id, tool call IDs or storage paths to the user unless explicitly requested.
            Use natural references like \"this draft\" or \"the contact person draft\"."; $developerMessages[] = "Adding contact persons instructions:
            - The available types are: corporate, supplier.
            - For corporate contact persons, idCorporate is required.
            - For supplier contact persons, idSupplier is required.
            - firstName and lastName are required fields.
            - If you only have the e-mail available, try to extract the firstName and lastName from it.
            - Users usually refer to the company (corporation) or the supplier by **name** or **alias**. You must **not** guess `idCorporate` or idSupplier`.
            - For companies/corporations: before creating or updating a contact person draft, resolve the company by calling **`run_rd_corporates_analytics`**: filter on `corp.alias` and/or `corp.companyNameStd` (and related name fields) until you identify the correct row, then use its `corp.id` as `idCorporate`.
            - For suppliers: before creating or updating a contact person draft, resolve the supplier by calling **`run_rd_suppliers_analytics`**: filter on `supp.alias` and/or `supp.companyName` (and related name fields) until you identify the correct row, then use its `supp.id` as `idSupplier`.
        "; return $developerMessages; } protected $entityFields = [ 'type', 'idCorporate', 'idSupplier', 'firstName', 'lastName', 'email', 'telephone', ]; protected $mandatoryFields = ['type', 'firstName', 'lastName', ]; protected function checkPermission_create_contact_person_draft() : bool { return \App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToSection('Modules/Partners/Corporates/ContactPersons') && \App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToSection('Modules/Partners/Suppliers/ContactPersons'); } protected function checkPermission_update_contact_person_draft() : bool { return \App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToSection('Modules/Partners/Corporates/ContactPersons') && \App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToSection('Modules/Partners/Suppliers/ContactPersons'); } protected function checkPermission_preview_contact_person() : bool { return \App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToSection('Modules/Partners/Corporates/ContactPersons') && \App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToSection('Modules/Partners/Suppliers/ContactPersons'); } protected function checkPermission_submit_contact_person() : bool { return \App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToSection('Modules/Partners/Corporates/ContactPersons') && \App\UserAccess\CurrentUserAccessFast::getInstance()->hasAccessToSection('Modules/Partners/Suppliers/ContactPersons'); } protected function generateFunctionObject_create_contact_person_draft(): \App\AI\Api\ChatGpt\Responses\ToolFunction { $funcObj = new \App\AI\Api\ChatGpt\Responses\ToolFunction( "create_contact_person_draft", "Create a new contact person draft. This does NOT post/commit the contact person. It only stores a draft and returns what is missing.", false ); $paramsObj = new \App\AI\Api\ChatGpt\Responses\Parameters(); $this->populateFunctionInsertUpdateParameters($paramsObj); $funcObj->setParameters($paramsObj); return $funcObj; } protected function generateFunctionObject_update_contact_person_draft(): \App\AI\Api\ChatGpt\Responses\ToolFunction { $funcObj = new \App\AI\Api\ChatGpt\Responses\ToolFunction( "update_contact_person_draft", "Update an existing contact person draft with new or corrected fields. Does NOT commit the contact person.", false ); $paramsObj = new \App\AI\Api\ChatGpt\Responses\Parameters(); $funcObj->setParameters($paramsObj); $draftIdObj = new \App\AI\Api\ChatGpt\Responses\Property(); $draftIdObj->type = "integer"; $draftIdObj->description = "The draft ID returned when the draft was created."; $paramsObj->addProperty("draft_id", $draftIdObj); $patchObj = new \App\AI\Api\ChatGpt\Responses\Property(); $patchObj->type = "object"; $patchObj->description = "An object with the fields to update in the draft."; $this->populateFunctionInsertUpdateParameters($patchObj); $paramsObj->addProperty("patch", $patchObj); $paramsObj->setRequired(['draft_id', 'patch']); return $funcObj; } protected function generateFunctionObject_preview_contact_person(): \App\AI\Api\ChatGpt\Responses\ToolFunction { $funcObj = new \App\AI\Api\ChatGpt\Responses\ToolFunction( "preview_contact_person", "Return a human-friendly preview of the contact person draft (all the fields) for user review. Does NOT commit.", false ); $paramsObj = new \App\AI\Api\ChatGpt\Responses\Parameters(); $funcObj->setParameters($paramsObj); $draftIdObj = new \App\AI\Api\ChatGpt\Responses\Property(); $draftIdObj->type = "integer"; $paramsObj->addProperty("draft_id", $draftIdObj); $paramsObj->setRequired(['draft_id']); return $funcObj; } protected function generateFunctionObject_submit_contact_person(): \App\AI\Api\ChatGpt\Responses\ToolFunction { $funcObj = new \App\AI\Api\ChatGpt\Responses\ToolFunction( "submit_contact_person", "Commit/post the contact person draft into the system. Only call this after user explicitly confirms.", false ); $paramsObj = new \App\AI\Api\ChatGpt\Responses\Parameters(); $funcObj->setParameters($paramsObj); $draftIdObj = new \App\AI\Api\ChatGpt\Responses\Property(); $draftIdObj->type = "integer"; $paramsObj->addProperty("draft_id", $draftIdObj); $confirmObj = new \App\AI\Api\ChatGpt\Responses\Property(); $confirmObj->type = "string"; $confirmObj->description = "Must be the exact user confirmation phrase: 'CONFIRM'."; $paramsObj->addProperty("user_confirmation_text", $confirmObj); $paramsObj->setRequired(['draft_id', 'user_confirmation_text']); return $funcObj; } protected function populateFunctionInsertUpdateParameters( \App\AI\Api\ChatGpt\Responses\Parameters|\App\AI\Api\ChatGpt\Responses\Property $paramsObj ) : \App\AI\Api\ChatGpt\Responses\Parameters|\App\AI\Api\ChatGpt\Responses\Property { $typeObj = new \App\AI\Api\ChatGpt\Responses\Property(); $typeObj->type = "string"; $typeObj->description = "Contact person type: corporate or supplier."; $typeObj->enum = ['corporate', 'supplier']; $paramsObj->addProperty("type", $typeObj); $idCorporateObj = new \App\AI\Api\ChatGpt\Responses\Property(); $idCorporateObj->type = "integer"; $idCorporateObj->description = "The ID of the corporate (company) this contact person belongs to. Required when type is 'corporate'."; $paramsObj->addProperty("idCorporate", $idCorporateObj); $idSupplierObj = new \App\AI\Api\ChatGpt\Responses\Property(); $idSupplierObj->type = "integer"; $idSupplierObj->description = "The ID of the supplier this contact person belongs to. Required when type is 'supplier'."; $paramsObj->addProperty("idSupplier", $idSupplierObj); $firstNameObj = new \App\AI\Api\ChatGpt\Responses\Property(); $firstNameObj->type = "string"; $firstNameObj->description = "Contact person's first name."; $paramsObj->addProperty("firstName", $firstNameObj); $lastNameObj = new \App\AI\Api\ChatGpt\Responses\Property(); $lastNameObj->type = "string"; $lastNameObj->description = "Contact person's last name."; $paramsObj->addProperty("lastName", $lastNameObj); $emailObj = new \App\AI\Api\ChatGpt\Responses\Property(); $emailObj->type = "string"; $emailObj->format = "email"; $emailObj->description = "Contact person's email address."; $paramsObj->addProperty("email", $emailObj); $telephoneObj = new \App\AI\Api\ChatGpt\Responses\Property(); $telephoneObj->type = "string"; $telephoneObj->description = "Contact person's telephone number."; $paramsObj->addProperty("telephone", $telephoneObj); return $paramsObj; } protected function executeFunction_create_contact_person_draft(string $arguments) : array { $args = json_decode($arguments, 1); return $this->getCreateEntityDraft($args); } protected function executeFunction_update_contact_person_draft(string $arguments) : array { $args = json_decode($arguments, 1); return $this->getUpdateEntityDraft($args); } protected function executeFunction_preview_contact_person(string $arguments) : array { $args = json_decode($arguments, 1); return $this->getPreviewEntityDraft($args); } protected function executeFunction_submit_contact_person(string $arguments) : array { $args = json_decode($arguments, 1); $draft = $this->getPreviewEntityDraft($args); $result = []; try { \DB::startTransaction(); $contactEnt = $this->createContactPersonEntityFromDraft($draft['fields']); \DB::commitTransaction(); $draft['fields']['id'] = $contactEnt->getId(); $result['draft_id'] = $args['draft_id']; $result['idContactPerson'] = $contactEnt->getId(); $result['message'] = 'Contact person committed successfully.'; } catch (\App\Entity\Exception $ex) { \DB::rollbackTransaction(); $result['draft_id'] = $args['draft_id']; $result['error'] = $ex->getMessageBulk(); } $this->executeCronjob('ReportsDataPopulate'); return $result; } protected function createContactPersonEntityFromDraft(array $fields): \Entity\ContactPerson { $contactEnt = \Entity\ContactPerson::insert(); if ($fields['type'] == 'supplier') { $contactEnt->type = 'supplier'; $contactEnt->idCompany = $fields['idSupplier'] ?? 0; } else { $contactEnt->type = 'corporate'; $contactEnt->idCompany = $fields['idCorporate'] ?? 0; } if (!empty($fields['firstName'])) { $contactEnt->firstName = $fields['firstName']; } if (!empty($fields['lastName'])) { $contactEnt->lastName = $fields['lastName']; } if (!empty($fields['email'])) { $contactEnt->email = $fields['email']; } if (!empty($fields['telephone'])) { $contactEnt->telephone = $fields['telephone']; } $contactEnt->save(); return $contactEnt; } protected function developerMessage_submit_contact_person(string $arguments, array $response) : string { $args = json_decode($arguments, 1); if ($response['idContactPerson']) { $draft = $this->getPreviewEntityDraft($args); if ($draft['fields']['type']=='supplier') { return "Contact person created with ID: ".$response['idContactPerson'].". Allow the user to see the `Suppliers contact persons` section by giving him a clickable link to the section: " . \Engine\Mvc\Dispatcher::getInstance()->getAppUrl('Modules/Partners/Suppliers/ContactPersons', 'index', ['filters[supplier][id]'=>$draft['fields']['idSupplier']]); } else { return "Contact person created with ID: ".$response['idContactPerson'].". Allow the user to see the `Companies contact persons` section by giving him a clickable link to the section: " . \Engine\Mvc\Dispatcher::getInstance()->getAppUrl('Modules/Partners/Corporates/ContactPersons', 'index', ['filters[corporate][id]'=>$draft['fields']['idCorporate']]); } } return ''; } } 