Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support transfer company and vat number in CRM custom fields #224

Merged
merged 6 commits into from
Jul 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
## v3.6.7
* Добавлена передача полей "Компания" и "Номер НДС" из заказа CMS в пользовательские поля заказа CRM

## v3.6.6
* Добавлена передача поля link при выгрузке брошенных корзин

Expand Down
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
3.6.6
3.6.7
29 changes: 25 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 Expand Up @@ -1525,10 +1525,13 @@ private static function updateAddressInvoice($addressInvoice, $customer, $order)

return;
}

if (RetailcrmTools::validateEntity($addressInvoice, null, true)) {
$addressInvoice->id_customer = $customer->id;
RetailcrmTools::assignAddressIdsByFields($customer, $addressInvoice);

self::setCompanyAndVatNumberForInvoiceAddress($order, $addressInvoice);

if (empty($addressInvoice->id)) {
self::loadInPrestashop($addressInvoice, 'save');

Expand Down Expand Up @@ -1954,6 +1957,24 @@ private static function createOrderDetail($item, $product, $parsedExtId, $presta
return $orderDetail;
}

private static function setCompanyAndVatNumberForInvoiceAddress($crmOrder, $addressInvoice)
{
if (!RetailcrmTools::isCorporateEnabled()
&& RetailcrmTools::isCampanyAndVatNumberSendEnabled()
) {
$company = $crmOrder['customFields']['ps_company'] ?? '';
$vatNumber = $crmOrder['customFields']['ps_vat_number'] ?? '';

if ('' !== $company) {
$addressInvoice->company = $company;
}

if ('' !== $vatNumber) {
$addressInvoice->vat_number = $vatNumber;
}
}
}

private static function handleError($order, $e)
{
RetailcrmLogger::writeCaller(
Expand Down
29 changes: 15 additions & 14 deletions retailcrm/lib/RetailcrmOrderBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -979,15 +979,25 @@ function ($v) use ($customer) {
;
$crmOrder = array_merge($crmOrder, $addressBuilder->getDataArray());

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

if (!empty($addressInvoice->vat_number)) {
$crmOrder['contragent']['INN'] = $addressInvoice->vat_number;
if ($isCorporateEnabled && RetailcrmTools::isOrderCorporate($order)) {
$crmOrder['contragent']['contragentType'] = 'legal-entity';
$crmOrder['contragent']['legalName'] = $addressInvoice->company ?? '';
$crmOrder['contragent']['INN'] = $addressInvoice->vat_number ?? '';
} else {
$crmOrder['contragent']['contragentType'] = 'individual';

if (
RetailcrmTools::isCampanyAndVatNumberSendEnabled()
&& Configuration::get(RetailCRM::COMPANY_AND_VAT_NUMBER_CREATED)
) {
$crmOrder['customFields']['ps_company'] = $addressInvoice->company ?? '';
$crmOrder['customFields']['ps_vat_number'] = $addressInvoice->vat_number ?? '';
}
}

if (isset($payment[$paymentType]) && !empty($payment[$paymentType])) {
if (!empty($payment[$paymentType])) {
$order_payment = [
'externalId' => $order->id . '#' . $order->reference,
'type' => $payment[$paymentType],
Expand Down Expand Up @@ -1124,9 +1134,6 @@ function ($v) use ($customer) {
'productName' => $product['product_name'],
'quantity' => $product['product_quantity'],
'initialPrice' => round($product['product_price'], 2),
/*'initialPrice' => !empty($item['rate'])
? $item['price'] + ($item['price'] * $item['rate'] / 100)
: $item['price'],*/
'purchasePrice' => round($product['purchase_supplier_price'], 2),
];

Expand Down Expand Up @@ -1161,12 +1168,6 @@ function ($v) use ($customer) {
if (!empty($site)) {
$crmOrder['customer']['site'] = $site;
}

if (RetailcrmTools::isCorporateEnabled() && RetailcrmTools::isOrderCorporate($order)) {
$crmOrder['contragent']['contragentType'] = 'legal-entity';
} else {
$crmOrder['contragent']['contragentType'] = 'individual';
}
}

return RetailcrmTools::filter(
Expand Down
48 changes: 32 additions & 16 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 All @@ -78,6 +100,16 @@ public static function isIcmlServicesEnabled()
return (bool) Configuration::get(RetailCRM::ENABLE_ICML_SERVICES);
}

/**
* Returns true if the transfer of company and VAT number is enabled in the settings
*
* @return bool
*/
public static function isCampanyAndVatNumberSendEnabled()
{
return (bool) Configuration::get(RetailCRM::ENABLE_COMPANY_AND_VAT_NUMBER_SEND);
}

/**
* Returns true if customer is corporate
*
Expand Down Expand Up @@ -969,22 +1001,6 @@ public static function getConfigurationByName($name)
}
}

/**
* @param $name
*
* @return DateTime|false
*/
public static function getConfigurationCreatedAtByName($name)
{
$config = self::getConfigurationByName($name);

if (empty($config)) {
return false;
}

return DateTimeImmutable::createFromFormat('Y-m-d H:i:s', $config['date_add']);
}

/**
* @return string
*/
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'] ?? 'Empresa';
$vatNumber = $translate[$locale]['vat_number'] ?? 'Número de IVA';

$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);
}
}
}
1 change: 1 addition & 0 deletions retailcrm/lib/settings/RetailcrmSettingsItems.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ public function __construct()
'enableCorporate' => new RetailcrmSettingsItemBool('enableCorporate', RetailCRM::ENABLE_CORPORATE_CLIENTS),
'enableOrderNumberSending' => new RetailcrmSettingsItemBool('enableOrderNumberSending', RetailCRM::ENABLE_ORDER_NUMBER_SENDING),
'enableOrderNumberReceiving' => new RetailcrmSettingsItemBool('enableOrderNumberReceiving', RetailCRM::ENABLE_ORDER_NUMBER_RECEIVING),
'enableCompanyAndVatNumberSend' => new RetailcrmSettingsItemBool('enableCompanyAndVatNumberSend', RetailCRM::ENABLE_COMPANY_AND_VAT_NUMBER_SEND),
'webJobs' => new RetailcrmSettingsItemBool('webJobs', RetailCRM::ENABLE_WEB_JOBS, '1'),
'debugMode' => new RetailcrmSettingsItemBool('debugMode', RetailCRM::ENABLE_DEBUG_MODE),

Expand Down
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
7 changes: 4 additions & 3 deletions retailcrm/retailcrm.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@

class RetailCRM extends Module
{
const VERSION = '3.6.6';
const VERSION = '3.6.7';

const API_URL = 'RETAILCRM_ADDRESS';
const API_KEY = 'RETAILCRM_API_TOKEN';
Expand All @@ -72,12 +72,12 @@ class RetailCRM extends Module
const ENABLE_BALANCES_RECEIVING = 'RETAILCRM_ENABLE_BALANCES_RECEIVING';
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';
const ENABLE_WEB_JOBS = 'RETAILCRM_ENABLE_WEB_JOBS';

const REQUIRED_CRM_SITE_ACCESS = 'access_selective';
const REQUIRED_CRM_SITE_COUNT = 1;
const REQUIRED_CRM_SCOPES = [
Expand Down Expand Up @@ -343,6 +343,7 @@ public function uninstall()
&& Configuration::deleteByName(static::ENABLE_BALANCES_RECEIVING)
&& Configuration::deleteByName(static::ENABLE_ORDER_NUMBER_SENDING)
&& Configuration::deleteByName(static::ENABLE_ORDER_NUMBER_RECEIVING)
&& Configuration::deleteByName(static::ENABLE_COMPANY_AND_VAT_NUMBER_SEND)
&& Configuration::deleteByName(static::ENABLE_DEBUG_MODE)
&& Configuration::deleteByName(static::ENABLE_WEB_JOBS)
&& Configuration::deleteByName('RETAILCRM_LAST_SYNC')
Expand Down
2 changes: 1 addition & 1 deletion retailcrm/views/js/app.js

Large diffs are not rendered by default.

Loading