DEV Community

Spsoi
Spsoi

Posted on • Edited on

AMOCRM TEST

<?php

namespace Classes\AmoCrm;

class AmoCrmClassCompany
{
    public $amo;

    public $phoneF = 173279;
    public $emailF = 173281;

    public function __construct() 
    {
        $this->amo = app('client')->crm();
        $this->l = logger('crm/AMOCRM');
    }


    public function companySearch($array)
    {
        $this->amoCRM = $array;
        $amo = $this->amo;
        $contact = null;
        if (!empty($this->amoCRM['phone'])) {
            $contacts = $amo->companies()->searchByPhone($this->amoCRM['phone']);
            if ($contact = $contacts->first()) {
                $contact->cf()->byId($this->emailF)->setValue($this->amoCRM['email'],'WORK');
                $contact->save();
                return $contact;
            }
        }

        if (!$contact && !empty($this->amoCRM['email'])) {
            if (filter_var($this->amoCRM['email'], FILTER_VALIDATE_EMAIL)) {
                $contacts = $amo->companies()->searchByEmail($this->amoCRM['email']);
                if ($contact = $contacts->first()) {
                    $contact->cf()->byId($this->phoneF)->setValue($this->amoCRM['phone'],'WORK');
                    $contact->save();
                    return $contact;
                }
            }
        }

        return false;
    }

    public function companyCreate()
    {
        $amo = $this->amo;
        if (!empty($this->amoCRM['companyName']) && !empty($this->amoCRM['phone'])) {
            $contact = $amo->companies()->create();
            $contact->responsible_user_id = $this->amoCRM['responsible_user_id'];
            $contact->name = $this->amoCRM['companyName'];
            if (isset($this->amoCRM['phone'])) {
                $contact->cf()->byId($this->phoneF)->setValue($this->amoCRM['phone'], 'WORK');
            }
            if (isset($this->amoCRM['email'])) {
                $contact->cf()->byId($this->emailF)->setValue($this->amoCRM['email'], 'WORK');
            }
            // Тип организации
            if (isset($this->amoCRM['type-object'])) {
                $contact->cf()->byId(177033)->setValue($this->amoCRM['type-object']);
            }


            $contact->save();
            return $contact;
        }  
    }

    public function contactCreate($entity, $data)
    {
        $this->amoCRM = $data;
        $amo = $this->amo;
            $contact = $entity->createContact();
            $contact->responsible_user_id = $this->amoCRM['responsible_user_id'];
            $contact->name = $this->amoCRM['contactName'];
            $contact->save();
            return $contact;
    }
}
Enter fullscreen mode Exit fullscreen mode

Image of Docusign

Bring your solution into Docusign. Reach over 1.6M customers.

Docusign is now extensible. Overcome challenges with disconnected products and inaccessible data by bringing your solutions into Docusign and publishing to 1.6M customers in the App Center.

Learn more

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay