Skip to content

Commit

Permalink
Create custom fields in CRM
Browse files Browse the repository at this point in the history
  • Loading branch information
uryvskiy-dima committed Jul 31, 2024
1 parent d3f5a8c commit 999544f
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 29 deletions.
8 changes: 4 additions & 4 deletions retailcrm/lib/RetailcrmHistory.php
Original file line number Diff line number Diff line change
Expand Up @@ -1235,10 +1235,10 @@ private static function createAddress($order, $customer)
$addressBuilder = new RetailcrmCustomerAddressBuilder();
$address = $addressBuilder
->setIdCustomer($customer->id)
->setDataCrm(isset($order['delivery']['address']) ? $order['delivery']['address'] : [])
->setFirstName(isset($order['firstName']) ? $order['firstName'] : null)
->setLastName(isset($order['lastName']) ? $order['lastName'] : null)
->setPhone(isset($order['phone']) ? $order['phone'] : null)
->setDataCrm($order['delivery']['address'] ?? [])
->setFirstName($order['firstName'] ?? null)
->setLastName($order['lastName'] ?? null)
->setPhone($order['phone'] ?? null)
->build()
->getData()
;
Expand Down
14 changes: 13 additions & 1 deletion retailcrm/lib/RetailcrmOrderBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -985,7 +985,19 @@ function ($v) use ($customer) {
? 'legal-entity'
: 'individual';

if (!$isCorporateEnabled && RetailcrmTools::isCampanyAndVatNumberSendEnabled()) {
if ($addressInvoice instanceof Address && !empty($addressInvoice->company)) {
$crmOrder['contragent']['legalName'] = $addressInvoice->company;

if (!empty($addressInvoice->vat_number)) {
$crmOrder['contragent']['INN'] = $addressInvoice->vat_number;
}
}

if (
!$isCorporateEnabled
&& RetailcrmTools::isCampanyAndVatNumberSendEnabled()
&& Configuration::get(RetailCRM::COMPANY_AND_VAT_NUMBER_CREATED)
) {
$company = $addressDelivery->company;
$vatNumber = $addressDelivery->vat_number;

Expand Down
22 changes: 22 additions & 0 deletions retailcrm/lib/RetailcrmTools.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,28 @@ public static function defaultLang()
return self::$default_lang;
}

/**
* Returns ISO code of current employee language or default language.
*
* @return string
*/
public static function getCurrentLanguageISO()
{
global $cookie;

$context = Context::getContext();

if (!empty($context) && !empty($context->employee)) {
$langId = (int) $context->employee->id_lang;
} elseif ($cookie instanceof Cookie) {
$langId = (int) $cookie->id_lang;
} else {
$langId = (int) Configuration::get('PS_LANG_DEFAULT');
}

return (string) Language::getIsoById($langId);
}

/**
* Returns true if corporate customers are enabled in settings
*
Expand Down
2 changes: 1 addition & 1 deletion retailcrm/lib/api/RetailcrmApiClientV5.php
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ public function customFieldsCreate($entity, $customField)
);
}

if (empty($entity) || 'customer' != $entity || 'order' != $entity) {
if (empty($entity)) {
throw new \InvalidArgumentException(
'Parameter `entity` must contain a data & value must be `order` or `customer`'
);
Expand Down
33 changes: 33 additions & 0 deletions retailcrm/lib/settings/RetailcrmSettings.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,13 @@ public function save()
$changed['consultantScript'] = $this->consultantScript->getValueStored();
}

if (
!empty($changed['enableCompanyAndVatNumberSend'])
&& !Configuration::get(RetailCRM::COMPANY_AND_VAT_NUMBER_CREATED)
) {
$this->createCompanyAndVatNumberFields();
}

return [
'success' => $this->validator->getSuccess(),
'errors' => $this->validator->getErrors(),
Expand Down Expand Up @@ -127,4 +134,30 @@ private function updateConsultantCode()
}
}
}

private function createCompanyAndVatNumberFields()
{
$api = RetailcrmTools::getApiClient();
$locale = RetailcrmTools::getCurrentLanguageISO();
$translate = [
'ru' => ['company' => 'Компания', 'vat_number' => 'Номер НДС'],
'en' => ['company' => 'Company', 'vat_number' => 'VAT number'],
];

$company = $translate[$locale]['company'] ?? 'Firma';
$vatNumber = $translate[$locale]['vat_number'] ?? 'CVR-nummer';

$customFields = [
['code' => 'ps_company', 'name' => $company, 'type' => 'string', 'displayArea' => 'customer'],
['code' => 'ps_vat_number', 'name' => $vatNumber, 'type' => 'string', 'displayArea' => 'customer']
];

if (null !== $api) {
foreach ($customFields as $field) {
$api->customFieldsCreate('order', $field);
}

Configuration::updateValue(RetailCRM::COMPANY_AND_VAT_NUMBER_CREATED, true);
}
}
}
22 changes: 0 additions & 22 deletions retailcrm/lib/templates/RetailcrmAbstractTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,28 +65,6 @@ public function __construct(Module $module, $smarty, $assets)
$this->assets = $assets;
}

/**
* Returns ISO code of current employee language or default language.
*
* @return string
*/
protected function getCurrentLanguageISO()
{
$langId = 0;

global $cookie;

if (!empty($this->context) && !empty($this->context->employee)) {
$langId = (int) $this->context->employee->id_lang;
} elseif ($cookie instanceof Cookie) {
$langId = (int) $cookie->id_lang;
} else {
$langId = (int) Configuration::get('PS_LANG_DEFAULT');
}

return (string) Language::getIsoById($langId);
}

/**
* @param $file
*
Expand Down
2 changes: 1 addition & 1 deletion retailcrm/lib/templates/RetailcrmSettingsTemplate.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ protected function buildParams()
'chunk-vendors' => filemtime(_PS_MODULE_DIR_ . '/retailcrm/views/js/chunk-vendors.js'),
],
'appData' => [
'locale' => $this->getCurrentLanguageISO(),
'locale' => RetailcrmTools::getCurrentLanguageISO(),
'debug' => RetailcrmTools::isDebug(),
'routes' => [
'settings' => RetailcrmTools::getAdminControllerUrl(RetailcrmSettingsController::class),
Expand Down
1 change: 1 addition & 0 deletions retailcrm/retailcrm.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class RetailCRM extends Module
const ENABLE_ORDER_NUMBER_SENDING = 'RETAILCRM_ENABLE_ORDER_NUMBER_SENDING';
const ENABLE_ORDER_NUMBER_RECEIVING = 'RETAILCRM_ENABLE_ORDER_NUMBER_RECEIVING';
const ENABLE_COMPANY_AND_VAT_NUMBER_SEND = 'RETAILCRM_ENABLE_COMPANY_AND_VAT_NUMBER_SEND';
const COMPANY_AND_VAT_NUMBER_CREATED = 'RETAILCRM_COMPANY_AND_VAT_NUMBER_CREATED';
const ENABLE_DEBUG_MODE = 'RETAILCRM_ENABLE_DEBUG_MODE';
const CONSULTANT_SCRIPT = 'RETAILCRM_CONSULTANT_SCRIPT';
const CONSULTANT_RCCT = 'RETAILCRM_CONSULTANT_RCCT';
Expand Down

0 comments on commit 999544f

Please sign in to comment.