<?php
switch ($modx->event->name) {
    
    
    /*
    * Добавляем отправку данных во внешнюю CRM, в данном случае - RetailCRM.
    * Воспользуемся отличным бесплатным компонентом - https://modstore.pro/packages/import-and-export/modretailcrm
    */
    case 'MvtFormsOnFormSubmit':

        $forms = array(
            'about' => array(
                'method' => 'o-kompanii'
                ,'product' => ''
            )
            ,'repairs' => array(
                'method' => 'zakaz-remont'
                ,'product' => 'xxxxxxxxxxxxx'
            )
            ,'verification' => array(
                'method' => 'zakaz-poverka'
                ,'product' => 'xxxxxxxxxxxxx'
            )
            ,'wholesalers' => array(
                'method' => 'forma-dla-opta'
                ,'product' => ''
            )
        );
        
        $form = $data['form'];
                    
        if(isset($forms[$form])) {
            if ($modx->getService('modretailcrm','modRetailCrm', MODX_CORE_PATH.'components/modretailcrm/model/modretailcrm/')) {  
                $site = $modx->getOption('modretailcrm_siteCode');
                $apiKey = $modx->getOption('modretailcrm_apiKey');
                $crmUrl = $modx->getOption('modretailcrm_url');
                
                $modRetailCrm = new modRetailCrm($modx, $apiKey, $crmUrl, $site);
                
                $orderData = array();
                $orderData['call'] = 1;
                $orderData['orderMethod'] = $forms[$form]['method'];

                
                if(isset($data[$form.'_name'])) {
                    $orderData['firstName'] = $data[$form.'_name'];
                }
                if(isset($data[$form.'_phone'])) {
                    $orderData['phone'] = $data[$form.'_phone'];
                }
                if(isset($data[$form.'_email'])) {
                    $orderData['email'] = $data[$form.'_email'];
                }
                
                $orderData['customerComment'] = '';
                if(isset($data[$form.'_message'])) {
                    $orderData['customerComment'] .= $data[$form.'_message'];
                }
                if(isset($data[$form.'_city'])) {
                    $orderData['customerComment'] .= ' (город: '.$data[$form.'_city'].')';
                }

                if(!empty($forms[$form]['product'])) {
                    $orderData['items'][0]['offer']['externalId'] = $forms[$form]['product'];
                    $orderData['items'][0]['offer']['xmlId'] = $forms[$form]['product'];
                }
                
                $response = $modRetailCrm->request->ordersCreate($orderData, $site); 
            }
            else {
                $modx->log(1, '[ModRetailCrm] - Not found class RetailCrm');
            }
        }
    break;
    
}