Skip to content

Commit

Permalink
add crm-item with batch support
Browse files Browse the repository at this point in the history
Signed-off-by: mesilov <mesilov.maxim@gmail.com>
  • Loading branch information
mesilov committed Aug 13, 2023
1 parent 324b587 commit 8243632
Show file tree
Hide file tree
Showing 8 changed files with 372 additions and 62 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
* `Bitrix24\SDK\Services\User\Service\User::get` - get user
* `Bitrix24\SDK\Services\User\Service\User::update` - update user
* `Bitrix24\SDK\Services\User\Service\User::search` - search users
* add [crm item support](https://github.com/mesilov/bitrix24-php-sdk/issues/330)

### Changed

Expand Down
62 changes: 13 additions & 49 deletions src/Services/CRM/CRMServiceBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,22 +5,9 @@
namespace Bitrix24\SDK\Services\CRM;

use Bitrix24\SDK\Services\AbstractServiceBuilder;
use Bitrix24\SDK\Services\CRM\Contact;
use Bitrix24\SDK\Services\CRM\Deal;
use Bitrix24\SDK\Services\CRM\Product;
use Bitrix24\SDK\Services\CRM\Settings;


/**
* Class CRMServiceBuilder
*
* @package Bitrix24\SDK\Services\CRM
*/
class CRMServiceBuilder extends AbstractServiceBuilder
{
/**
* @return Settings\Service\Settings
*/
public function settings(): Settings\Service\Settings
{
if (!isset($this->serviceCache[__METHOD__])) {
Expand All @@ -30,9 +17,6 @@ public function settings(): Settings\Service\Settings
return $this->serviceCache[__METHOD__];
}

/**
* @return Deal\Service\DealContact
*/
public function dealContact(): Deal\Service\DealContact
{
if (!isset($this->serviceCache[__METHOD__])) {
Expand All @@ -42,9 +26,6 @@ public function dealContact(): Deal\Service\DealContact
return $this->serviceCache[__METHOD__];
}

/**
* @return Deal\Service\DealCategory
*/
public function dealCategory(): Deal\Service\DealCategory
{
if (!isset($this->serviceCache[__METHOD__])) {
Expand All @@ -54,9 +35,6 @@ public function dealCategory(): Deal\Service\DealCategory
return $this->serviceCache[__METHOD__];
}

/**
* @return Deal\Service\Deal
*/
public function deal(): Deal\Service\Deal
{
if (!isset($this->serviceCache[__METHOD__])) {
Expand All @@ -70,9 +48,6 @@ public function deal(): Deal\Service\Deal
return $this->serviceCache[__METHOD__];
}

/**
* @return \Bitrix24\SDK\Services\CRM\Deal\Service\DealUserfield
*/
public function dealUserfield(): Deal\Service\DealUserfield
{
if (!isset($this->serviceCache[__METHOD__])) {
Expand All @@ -85,9 +60,6 @@ public function dealUserfield(): Deal\Service\DealUserfield
return $this->serviceCache[__METHOD__];
}

/**
* @return Contact\Service\Contact
*/
public function contact(): Contact\Service\Contact
{
if (!isset($this->serviceCache[__METHOD__])) {
Expand All @@ -101,9 +73,6 @@ public function contact(): Contact\Service\Contact
return $this->serviceCache[__METHOD__];
}

/**
* @return \Bitrix24\SDK\Services\CRM\Contact\Service\ContactUserfield
*/
public function contactUserfield(): Contact\Service\ContactUserfield
{
if (!isset($this->serviceCache[__METHOD__])) {
Expand All @@ -128,9 +97,6 @@ public function dealProductRows(): Deal\Service\DealProductRows
return $this->serviceCache[__METHOD__];
}

/**
* @return Deal\Service\DealCategoryStage
*/
public function dealCategoryStage(): Deal\Service\DealCategoryStage
{
if (!isset($this->serviceCache[__METHOD__])) {
Expand All @@ -140,9 +106,6 @@ public function dealCategoryStage(): Deal\Service\DealCategoryStage
return $this->serviceCache[__METHOD__];
}

/**
* @return Product\Service\Product
*/
public function product(): Product\Service\Product
{
if (!isset($this->serviceCache[__METHOD__])) {
Expand All @@ -156,9 +119,6 @@ public function product(): Product\Service\Product
return $this->serviceCache[__METHOD__];
}

/**
* @return Userfield\Service\Userfield
*/
public function userfield(): Userfield\Service\Userfield
{
if (!isset($this->serviceCache[__METHOD__])) {
Expand All @@ -171,9 +131,6 @@ public function userfield(): Userfield\Service\Userfield
return $this->serviceCache[__METHOD__];
}

/**
* @return Lead\Service\Lead
*/
public function lead(): Lead\Service\Lead
{
if (!isset($this->serviceCache[__METHOD__])) {
Expand All @@ -187,9 +144,6 @@ public function lead(): Lead\Service\Lead
return $this->serviceCache[__METHOD__];
}

/**
* @return Activity\Service\Activity
*/
public function activity(): Activity\Service\Activity
{
if (!isset($this->serviceCache[__METHOD__])) {
Expand All @@ -203,9 +157,6 @@ public function activity(): Activity\Service\Activity
return $this->serviceCache[__METHOD__];
}

/**
* @return Activity\ActivityFetcherBuilder
*/
public function activityFetcher(): Activity\ActivityFetcherBuilder
{
if (!isset($this->serviceCache[__METHOD__])) {
Expand All @@ -219,4 +170,17 @@ public function activityFetcher(): Activity\ActivityFetcherBuilder

return $this->serviceCache[__METHOD__];
}

public function item(): Item\Service\Item
{
if (!isset($this->serviceCache[__METHOD__])) {
$this->serviceCache[__METHOD__] = new Item\Service\Item(
new Item\Service\Batch($this->batch, $this->log),
$this->core,
$this->log
);
}

return $this->serviceCache[__METHOD__];
}
}
51 changes: 38 additions & 13 deletions src/Services/CRM/Common/Result/AbstractCrmItem.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class AbstractCrmItem extends AbstractItem
private const CRM_USERFIELD_PREFIX = 'UF_CRM_';

/**
* @var \Money\Currency
* @var Currency
*/
private Currency $currency;

Expand All @@ -25,7 +25,6 @@ public function __construct(array $data, Currency $currency = null)
if ($currency !== null) {
$this->currency = $currency;
}

}

/**
Expand All @@ -49,6 +48,15 @@ public function __get($offset)
case 'ASSIGNED_BY_ID':
case 'CREATED_BY_ID':
case 'MODIFY_BY_ID':
case 'createdBy':
case 'updatedBy':
case 'movedBy':
case 'begindate':
case 'closedate':
case 'opportunity':
case 'opportunityAccount':
case 'taxValueAccount':
case 'taxValue':
// deal
case 'LEAD_ID':
case 'CONTACT_ID':
Expand All @@ -57,12 +65,20 @@ public function __get($offset)
case 'OWNER_ID':
// DealCategoryItem
case 'SORT':
case 'id':
case 'categoryId':
case 'webformId':
case 'assignedById':
case 'contactId':
case 'lastActivityBy':
if ($this->data[$offset] !== '' && $this->data[$offset] !== null) {
return (int)$this->data[$offset];
}

return null;
case 'COMPANY_ID':
case 'companyId':
case 'mycompanyId':
if ($this->data[$offset] !== '' && $this->data[$offset] !== null && $this->data[$offset] !== '0') {
return (int)$this->data[$offset];
}
Expand All @@ -73,17 +89,9 @@ public function __get($offset)
case 'HAS_EMAIL':
case 'HAS_IMOL':
case 'OPENED':
// deal
case 'PRICE_EXCLUSIVE':
case 'PRICE_NETTO':
case 'PRICE_BRUTTO':
case 'PRICE':
if ($this->data[$offset] !== '' && $this->data[$offset] !== null) {
$var = $this->data[$offset] * 100;
return new Money((string)$var, new Currency($this->currency->getCode()));
}
return null;
case 'opened':
case 'IS_MANUAL_OPPORTUNITY':
case 'isManualOpportunity':
case 'CLOSED':
case 'IS_NEW':
case 'IS_LOCKED':
Expand All @@ -97,11 +105,28 @@ public function __get($offset)
case 'BIRTHDATE':
case 'BEGINDATE':
case 'CLOSEDATE':
case 'createdTime':
case 'updatedTime':
case 'movedTime':
case 'lastActivityTime':
if ($this->data[$offset] !== '') {
return DateTimeImmutable::createFromFormat(DATE_ATOM, $this->data[$offset]);
}

return null;
// deal
case 'PRICE_EXCLUSIVE':
case 'PRICE_NETTO':
case 'PRICE_BRUTTO':
case 'PRICE':
if ($this->data[$offset] !== '' && $this->data[$offset] !== null) {
$var = $this->data[$offset] * 100;
return new Money((string)$var, new Currency($this->currency->getCode()));
}
return null;
case 'currencyId':
case 'accountCurrencyId':
return new Currency($this->data[$offset]);
default:
return $this->data[$offset] ?? null;
}
Expand All @@ -113,7 +138,7 @@ public function __get($offset)
* @param string $fieldName
*
* @return mixed|null
* @throws \Bitrix24\SDK\Services\CRM\Userfield\Exceptions\UserfieldNotFoundException
* @throws UserfieldNotFoundException
*/
protected function getKeyWithUserfieldByFieldName(string $fieldName)
{
Expand Down
53 changes: 53 additions & 0 deletions src/Services/CRM/Item/Result/ItemItemResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php

declare(strict_types=1);

namespace Bitrix24\SDK\Services\CRM\Item\Result;

use Bitrix24\SDK\Services\CRM\Common\Result\AbstractCrmItem;
use DateTimeImmutable;
use Money\Currency;

/**
* @property-read int $id
* @property-read string $xmlId
* @property-read string $title
* @property-read int $createdBy
* @property-read int $updatedBy
* @property-read int $movedBy
* @property-read DateTimeImmutable $createdTime
* @property-read DateTimeImmutable $updatedTime
* @property-read DateTimeImmutable $movedTime
* @property-read int $categoryId
* @property-read bool $opened
* @property-read string $previousStageId
* @property-read DateTimeImmutable $begindate
* @property-read DateTimeImmutable $closedate
* @property-read int $companyId
* @property-read int $contactId
* @property-read int $opportunity
* @property-read bool $isManualOpportunity
* @property-read int $taxValue
* @property-read Currency $currencyId
* @property-read int $opportunityAccount
* @property-read int $taxValueAccount
* @property-read Currency $accountCurrencyId
* @property-read int $mycompanyId
* @property-read string $sourceId
* @property-read string $sourceDescription
* @property-read int $webformId
* @property-read int $assignedById
* @property-read int $lastActivityBy
* @property-read DateTimeImmutable $lastActivityTime
* @property-read string $utmSource
* @property-read string $utmMedium
* @property-read string $utmCampaign
* @property-read string $utmContent
* @property-read string $utmTerm
* @property-read array $observers
* @property-read array $contactIds
* @property-read int $entityTypeId
*/
class ItemItemResult extends AbstractCrmItem
{
}
16 changes: 16 additions & 0 deletions src/Services/CRM/Item/Result/ItemResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php


declare(strict_types=1);

namespace Bitrix24\SDK\Services\CRM\Item\Result;

use Bitrix24\SDK\Core\Result\AbstractResult;

class ItemResult extends AbstractResult
{
public function item(): ItemItemResult
{
return new ItemItemResult($this->getCoreResponse()->getResponseData()->getResult()['item']);
}
}
25 changes: 25 additions & 0 deletions src/Services/CRM/Item/Result/ItemsResult.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

declare(strict_types=1);

namespace Bitrix24\SDK\Services\CRM\Item\Result;

use Bitrix24\SDK\Core\Exceptions\BaseException;
use Bitrix24\SDK\Core\Result\AbstractResult;

class ItemsResult extends AbstractResult
{
/**
* @return ItemItemResult[]
* @throws BaseException
*/
public function getItems(): array
{
$items = [];
foreach ($this->getCoreResponse()->getResponseData()->getResult() as $item) {
$items[] = new ItemItemResult($item);
}

return $items;
}
}
Loading

0 comments on commit 8243632

Please sign in to comment.