/** * @param Note[] $notes * @return \nanodesu88\amocrm\models\Note[] */ public function getNotesElement(array $notes) { Note::fixArray($notes); $leadsId = []; $contactsId = []; foreach ($notes as $note) { if ($note->isContact()) { $contactsId[] = $note->element_id; } else { $leadsId[] = $note->element_id; } } $contacts = $this->getApi()->getContacts()->get(['id' => $contactsId]); $leads = $this->getApi()->getLeads()->get(['id' => $leadsId]); foreach ($notes as $note) { if ($note->isContact()) { $note->placeElement($contacts[$note->element_id]); } else { $note->placeElement($leads[$note->element_id]); } } Contact::fixArray($contacts); Lead::fixArray($leads); foreach ($contacts as $key => $contact) { $leads[$key] = $contact; } return $leads; }
/** * @uses testAccount */ public function testSave() { $newLead = new Lead(); $newLead->name = 'Lead.testBasic'; $newLead->status_id = $this->getApi()->getAccounts()->current()->leads_statuses[0]->id; $leadSaveResult = $this->getApi()->getLeads()->save($newLead); $this->assertTrue($leadSaveResult); $this->assertTrue(is_numeric($newLead->id)); $this->assertFalse($newLead->isNew()); $this->assertInstanceOf(User::class, $newLead->getUser()); $newContact = new Contact(); $newContact->name = 'Contact.testBasic'; $contactSaveResult = $this->getApi()->getContacts()->save($newContact); $this->assertTrue($contactSaveResult); $this->assertTrue(is_numeric($newContact->id)); $this->assertFalse($newContact->isNew()); $this->assertInstanceOf(User::class, $newContact->getUser()); $newCompany = new Company(); $newCompany->name = 'Company.testBasic'; $companySaveResult = $this->getApi()->getCompany()->save($newCompany); $this->assertTrue($companySaveResult); $this->assertTrue(is_numeric($newCompany->id)); $this->assertFalse($newCompany->isNew()); $this->assertInstanceOf(User::class, $newCompany->getUser()); $this->assertEquals($newLead->getUser(), $newContact->getUser()); $this->assertEquals($newLead->getUser(), $newCompany->getUser()); $this->assertCount(0, $newLead->getContacts()); $this->assertCount(0, $newContact->getLeads()); $newContact->addLead($newLead); $newCompany->addLead($newLead); $this->getApi()->getContacts()->save($newContact); $this->getApi()->getCompany()->save($newCompany); $this->assertCount(2, $newLead->getContacts()); // Компания = контакт $this->assertCount(1, $newContact->getLeads()); $this->assertCount(1, $newLead->getNotes()); $this->assertCount(1, $newContact->getNotes()); $newLeadNote = new Note(); $newContactNote = new Note(); $newLeadNote->note_type = Note::TYPE_SIMPLE; $newLeadNote->text = 'Note.Simple.Lead'; $newLeadNote->setElement($newLead); $newContactNote->note_type = Note::TYPE_SIMPLE; $newContactNote->text = 'Note.Simple.Contact'; $newContactNote->setElement($newContact); $noteSaveResult = $this->getApi()->getNotes()->save($newLeadNote) && $this->getApi()->getNotes()->save($newContactNote); $this->assertCount(2, $newLead->getNotes()); $this->assertCount(2, $newContact->getNotes()); $this->assertTrue($noteSaveResult); $this->assertTrue(is_numeric($newLeadNote->id)); $this->assertFalse($newLeadNote->isNew()); $newTask = new Task(); $newTask->setElement($newLead); $newTask->text = 'Task.Lead'; $newTask->task_type = Task::TYPE_MEETING; $newTask->complete_till = time() + 3600; $taskSaveResult = $this->getApi()->getTasks()->save($newTask); $this->assertTrue($taskSaveResult); $this->assertTrue(is_numeric($newTask->id)); $this->assertFalse($newTask->isNew()); }