2016-01-13 18:43:41 +03:00
< ? php
2020-04-16 15:26:03 +03:00
/**
* MIT License
*
2021-12-06 14:37:43 +03:00
* Copyright ( c ) 2021 DIGITAL RETAIL TECHNOLOGIES SL
2020-04-16 15:26:03 +03:00
*
* Permission is hereby granted , free of charge , to any person obtaining a copy
* of this software and associated documentation files ( the " Software " ), to deal
* in the Software without restriction , including without limitation the rights
* to use , copy , modify , merge , publish , distribute , sublicense , and / or sell
* copies of the Software , and to permit persons to whom the Software is
* furnished to do so , subject to the following conditions :
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software .
*
* THE SOFTWARE IS PROVIDED " AS IS " , WITHOUT WARRANTY OF ANY KIND , EXPRESS OR
* IMPLIED , INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY ,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT . IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM , DAMAGES OR OTHER
* LIABILITY , WHETHER IN AN ACTION OF CONTRACT , TORT OR OTHERWISE , ARISING FROM ,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE .
*
* DISCLAIMER
*
* Do not edit or add to this file if you wish to upgrade PrestaShop to newer
* versions in the future . If you wish to customize PrestaShop for your
* needs please refer to http :// www . prestashop . com for more information .
*
* @ author DIGITAL RETAIL TECHNOLOGIES SL < mail @ simlachat . com >
2021-12-06 14:37:43 +03:00
* @ copyright 2021 DIGITAL RETAIL TECHNOLOGIES SL
2020-04-16 15:26:03 +03:00
* @ license https :// opensource . org / licenses / MIT The MIT License
*
* Don ' t forget to prefix your containers with your own identifier
* to avoid any conflicts with others containers .
*/
2021-12-06 14:37:43 +03:00
2016-01-13 18:43:41 +03:00
class RetailcrmReferences
{
2020-05-07 13:04:44 +03:00
const GIFT_WRAPPING_ITEM_EXTERNAL_ID = 'giftWrappingCost' ;
2018-05-28 17:09:31 +03:00
public $default_lang ;
public $carriers ;
2021-11-03 16:19:39 +07:00
public $payment_modules = [];
2021-10-19 17:44:32 +03:00
public $apiStatuses ;
2018-05-28 17:09:31 +03:00
2021-07-27 13:25:44 +03:00
/**
2021-11-03 16:19:39 +07:00
* @ var bool | RetailcrmApiClientV5 | RetailcrmProxy
2021-07-27 13:25:44 +03:00
*/
2018-05-28 17:09:31 +03:00
private $api ;
2016-01-13 18:43:41 +03:00
public function __construct ( $client )
{
$this -> api = $client ;
$this -> default_lang = ( int ) Configuration :: get ( 'PS_LANG_DEFAULT' );
2017-04-05 17:56:26 +03:00
$this -> carriers = Carrier :: getCarriers (
2016-01-13 18:43:41 +03:00
$this -> default_lang ,
true ,
false ,
false ,
null ,
PS_CARRIERS_AND_CARRIER_MODULES_NEED_RANGE
);
2017-04-05 17:56:26 +03:00
}
public function getDeliveryTypes ()
{
2021-11-03 16:19:39 +07:00
$deliveryTypes = [];
2017-05-31 14:23:18 +03:00
$apiDeliveryTypes = $this -> getApiDeliveryTypes ();
2016-01-13 18:43:41 +03:00
2017-04-05 17:56:26 +03:00
if ( ! empty ( $this -> carriers )) {
foreach ( $this -> carriers as $carrier ) {
2021-11-03 16:19:39 +07:00
$deliveryTypes [] = [
2016-01-13 18:43:41 +03:00
'type' => 'select' ,
'label' => $carrier [ 'name' ],
'name' => 'RETAILCRM_API_DELIVERY[' . $carrier [ 'id_carrier' ] . ']' ,
2020-04-16 15:26:03 +03:00
'subname' => $carrier [ 'id_carrier' ],
2016-01-13 18:43:41 +03:00
'required' => false ,
2021-11-03 16:19:39 +07:00
'options' => [
2017-05-31 14:23:18 +03:00
'query' => $apiDeliveryTypes ,
2016-01-13 18:43:41 +03:00
'id' => 'id_option' ,
2021-11-03 16:19:39 +07:00
'name' => 'name' ,
],
];
2016-01-13 18:43:41 +03:00
}
}
return $deliveryTypes ;
}
public function getStatuses ()
{
2021-11-03 16:19:39 +07:00
$statusTypes = [];
2016-01-13 18:43:41 +03:00
$states = OrderState :: getOrderStates ( $this -> default_lang , true );
2021-10-19 17:44:32 +03:00
$this -> apiStatuses = $this -> apiStatuses ? : $this -> getApiStatuses ();
2016-01-13 18:43:41 +03:00
if ( ! empty ( $states )) {
foreach ( $states as $state ) {
2021-11-03 16:19:39 +07:00
if ( ' ' != $state [ 'name' ]) {
2016-01-13 18:43:41 +03:00
$key = $state [ 'id_order_state' ];
2021-11-03 16:19:39 +07:00
$statusTypes [] = [
2016-01-13 18:43:41 +03:00
'type' => 'select' ,
'label' => $state [ 'name' ],
'name' => " RETAILCRM_API_STATUS[ $key ] " ,
2020-04-16 15:26:03 +03:00
'subname' => $key ,
2016-01-13 18:43:41 +03:00
'required' => false ,
2021-11-03 16:19:39 +07:00
'options' => [
2021-10-19 17:44:32 +03:00
'query' => $this -> apiStatuses ,
2016-01-13 18:43:41 +03:00
'id' => 'id_option' ,
2021-11-03 16:19:39 +07:00
'name' => 'name' ,
],
];
2016-01-13 18:43:41 +03:00
}
}
}
return $statusTypes ;
}
2021-10-19 17:44:32 +03:00
public function getOutOfStockStatuses ( $arParams )
{
2021-11-03 16:19:39 +07:00
$statusTypes = [];
2021-10-19 17:44:32 +03:00
$this -> apiStatuses = $this -> apiStatuses ? : $this -> getApiStatuses ();
foreach ( $arParams as $key => $state ) {
2021-11-03 16:19:39 +07:00
$statusTypes [] = [
2021-10-19 17:44:32 +03:00
'type' => 'select' ,
'label' => $state ,
'name' => " RETAILCRM_API_OUT_OF_STOCK_STATUS[ $key ] " ,
'subname' => $key ,
'required' => false ,
2021-11-03 16:19:39 +07:00
'options' => [
2021-10-19 17:44:32 +03:00
'query' => $this -> apiStatuses ,
'id' => 'id_option' ,
2021-11-03 16:19:39 +07:00
'name' => 'name' ,
],
];
2021-10-19 17:44:32 +03:00
}
return $statusTypes ;
}
2016-01-13 18:43:41 +03:00
public function getPaymentTypes ()
{
$payments = $this -> getSystemPaymentModules ();
2021-11-03 16:19:39 +07:00
$paymentTypes = [];
2017-05-31 14:23:18 +03:00
$apiPaymentTypes = $this -> getApiPaymentTypes ();
2016-01-13 18:43:41 +03:00
if ( ! empty ( $payments )) {
foreach ( $payments as $payment ) {
2021-11-03 16:19:39 +07:00
$paymentTypes [] = [
2016-01-13 18:43:41 +03:00
'type' => 'select' ,
'label' => $payment [ 'name' ],
'name' => 'RETAILCRM_API_PAYMENT[' . $payment [ 'code' ] . ']' ,
2020-04-16 15:26:03 +03:00
'subname' => $payment [ 'code' ],
2016-01-13 18:43:41 +03:00
'required' => false ,
2021-11-03 16:19:39 +07:00
'options' => [
2017-05-31 14:23:18 +03:00
'query' => $apiPaymentTypes ,
2016-01-13 18:43:41 +03:00
'id' => 'id_option' ,
2021-11-03 16:19:39 +07:00
'name' => 'name' ,
],
];
2016-01-13 18:43:41 +03:00
}
}
return $paymentTypes ;
}
2017-08-14 12:06:10 +02:00
public function getPaymentAndDeliveryForDefault ( $arParams )
2017-04-05 17:56:26 +03:00
{
2021-11-03 16:19:39 +07:00
$paymentTypes = [];
$deliveryTypes = [];
2017-04-05 17:56:26 +03:00
2021-11-03 16:19:39 +07:00
$paymentDeliveryTypes = [];
2017-04-05 17:56:26 +03:00
if ( ! empty ( $this -> carriers )) {
2021-11-03 16:19:39 +07:00
$deliveryTypes [] = [
2017-04-05 17:56:26 +03:00
'id_option' => '' ,
'name' => '' ,
2021-11-03 16:19:39 +07:00
];
2017-04-05 17:56:26 +03:00
foreach ( $this -> carriers as $valCarrier ) {
2021-11-03 16:19:39 +07:00
$deliveryTypes [] = [
2017-04-05 17:56:26 +03:00
'id_option' => $valCarrier [ 'id_carrier' ],
'name' => $valCarrier [ 'name' ],
2021-11-03 16:19:39 +07:00
];
2017-04-05 17:56:26 +03:00
}
2021-11-03 16:19:39 +07:00
$paymentDeliveryTypes [] = [
2017-04-05 17:56:26 +03:00
'type' => 'select' ,
2017-06-16 16:13:25 +03:00
'label' => $arParams [ 0 ],
2017-04-05 17:56:26 +03:00
'name' => 'RETAILCRM_API_DELIVERY_DEFAULT' ,
'required' => false ,
2021-11-03 16:19:39 +07:00
'options' => [
'query' => $deliveryTypes ,
2017-04-05 17:56:26 +03:00
'id' => 'id_option' ,
2021-11-03 16:19:39 +07:00
'name' => 'name' ,
],
];
2017-04-05 17:56:26 +03:00
}
2017-05-22 21:13:18 +04:00
$paymentModules = $this -> getSystemPaymentModules ();
if ( ! empty ( $paymentModules )) {
2021-11-03 16:19:39 +07:00
$paymentTypes [] = [
2017-04-05 17:56:26 +03:00
'id_option' => '' ,
'name' => '' ,
2021-11-03 16:19:39 +07:00
];
2017-04-05 17:56:26 +03:00
2017-05-31 14:23:18 +03:00
foreach ( $paymentModules as $valPayment ) {
2021-11-03 16:19:39 +07:00
$paymentTypes [ $valPayment [ 'id' ]] = [
2017-04-05 17:56:26 +03:00
'id_option' => $valPayment [ 'code' ],
'name' => $valPayment [ 'name' ],
2021-11-03 16:19:39 +07:00
];
2017-04-05 17:56:26 +03:00
}
2021-11-03 16:19:39 +07:00
$paymentDeliveryTypes [] = [
2017-04-05 17:56:26 +03:00
'type' => 'select' ,
2017-06-16 16:13:25 +03:00
'label' => $arParams [ 1 ],
2017-04-05 17:56:26 +03:00
'name' => 'RETAILCRM_API_PAYMENT_DEFAULT' ,
'required' => false ,
2021-11-03 16:19:39 +07:00
'options' => [
2017-04-05 17:56:26 +03:00
'query' => $paymentTypes ,
'id' => 'id_option' ,
2021-11-03 16:19:39 +07:00
'name' => 'name' ,
],
];
2017-04-05 17:56:26 +03:00
}
return $paymentDeliveryTypes ;
}
2018-05-28 17:09:31 +03:00
public function getSystemPaymentModules ( $active = true )
2016-01-13 18:43:41 +03:00
{
2020-04-16 15:26:03 +03:00
$shop_id = ( int ) Context :: getContext () -> shop -> id ;
2016-01-13 18:43:41 +03:00
2020-04-16 15:26:03 +03:00
/**
* Get all modules then select only payment ones
*/
$modules = RetailCRM :: getCachedCmsModulesList ();
2021-08-02 11:58:01 +03:00
$allPaymentModules = PaymentModule :: getInstalledPaymentModules ();
2021-11-03 16:19:39 +07:00
$paymentModulesIds = [];
2021-08-02 11:58:01 +03:00
foreach ( $allPaymentModules as $module ) {
2021-11-03 16:19:39 +07:00
$paymentModulesIds [] = $module [ 'id_module' ];
2021-08-02 11:58:01 +03:00
}
2016-01-13 18:43:41 +03:00
foreach ( $modules as $module ) {
2021-11-03 16:19:39 +07:00
if (( ! empty ( $module -> parent_class ) && 'PaymentModule' == $module -> parent_class )
2021-08-02 11:58:01 +03:00
|| in_array ( $module -> id , $paymentModulesIds )
) {
2016-01-13 18:43:41 +03:00
if ( $module -> id ) {
2020-04-16 15:26:03 +03:00
$module_id = ( int ) $module -> id ;
2021-11-03 16:19:39 +07:00
if ( 'SimpleXMLElement' == ! get_class ( $module )) {
$module -> country = [];
}
2020-04-16 15:26:03 +03:00
$countries = DB :: getInstance () -> executeS ( 'SELECT id_country FROM ' . _DB_PREFIX_ . 'module_country WHERE id_module = ' . pSQL ( $module_id ) . ' AND `id_shop`=' . pSQL ( $shop_id ));
2021-11-03 16:19:39 +07:00
foreach ( $countries as $country ) {
2016-01-13 18:43:41 +03:00
$module -> country [] = $country [ 'id_country' ];
2021-11-03 16:19:39 +07:00
}
if ( 'SimpleXMLElement' == ! get_class ( $module )) {
$module -> currency = [];
}
2020-04-16 15:26:03 +03:00
$currencies = DB :: getInstance () -> executeS ( 'SELECT id_currency FROM ' . _DB_PREFIX_ . 'module_currency WHERE id_module = ' . pSQL ( $module_id ) . ' AND `id_shop`=' . pSQL ( $shop_id ));
2021-11-03 16:19:39 +07:00
foreach ( $currencies as $currency ) {
2016-01-13 18:43:41 +03:00
$module -> currency [] = $currency [ 'id_currency' ];
2021-11-03 16:19:39 +07:00
}
if ( 'SimpleXMLElement' == ! get_class ( $module )) {
$module -> group = [];
}
2020-04-16 15:26:03 +03:00
$groups = DB :: getInstance () -> executeS ( 'SELECT id_group FROM ' . _DB_PREFIX_ . 'module_group WHERE id_module = ' . pSQL ( $module_id ) . ' AND `id_shop`=' . pSQL ( $shop_id ));
2021-11-03 16:19:39 +07:00
foreach ( $groups as $group ) {
2016-01-13 18:43:41 +03:00
$module -> group [] = $group [ 'id_group' ];
2021-11-03 16:19:39 +07:00
}
2016-01-13 18:43:41 +03:00
} else {
$module -> country = null ;
$module -> currency = null ;
$module -> group = null ;
}
2021-11-03 16:19:39 +07:00
if ( 0 != $module -> active || false === $active ) {
$this -> payment_modules [] = [
2016-01-13 18:43:41 +03:00
'id' => $module -> id ,
'code' => $module -> name ,
2021-11-03 16:19:39 +07:00
'name' => $module -> displayName ,
];
2016-01-13 18:43:41 +03:00
}
}
}
return $this -> payment_modules ;
}
2017-09-27 14:38:24 +02:00
public function getStatuseDefaultExport ()
{
return $this -> getApiStatuses ();
}
2021-06-25 18:44:52 +03:00
public function getApiDeliveryTypes ()
2016-01-13 18:43:41 +03:00
{
2021-11-03 16:19:39 +07:00
$crmDeliveryTypes = [];
2016-01-13 18:43:41 +03:00
$request = $this -> api -> deliveryTypesList ();
if ( $request ) {
2021-11-03 16:19:39 +07:00
$crmDeliveryTypes [] = [
2016-01-13 18:43:41 +03:00
'id_option' => '' ,
'name' => '' ,
2021-11-03 16:19:39 +07:00
];
2016-01-13 18:43:41 +03:00
foreach ( $request -> deliveryTypes as $dType ) {
2021-06-08 16:04:39 +03:00
if ( ! $dType [ 'active' ]) {
continue ;
}
2021-11-03 16:19:39 +07:00
$crmDeliveryTypes [] = [
2016-01-13 18:43:41 +03:00
'id_option' => $dType [ 'code' ],
'name' => $dType [ 'name' ],
2021-11-03 16:19:39 +07:00
];
2016-01-13 18:43:41 +03:00
}
}
return $crmDeliveryTypes ;
}
2021-06-25 18:44:52 +03:00
public function getApiStatuses ()
2016-01-13 18:43:41 +03:00
{
2021-11-03 16:19:39 +07:00
$crmStatusTypes = [];
2016-01-13 18:43:41 +03:00
$request = $this -> api -> statusesList ();
if ( $request ) {
2021-11-03 16:19:39 +07:00
$crmStatusTypes [] = [
2016-01-13 18:43:41 +03:00
'id_option' => '' ,
'name' => '' ,
2021-05-31 12:09:15 +03:00
'ordering' => '' ,
2021-11-03 16:19:39 +07:00
];
2016-01-13 18:43:41 +03:00
foreach ( $request -> statuses as $sType ) {
2021-06-08 16:04:39 +03:00
if ( ! $sType [ 'active' ]) {
continue ;
}
2021-11-03 16:19:39 +07:00
$crmStatusTypes [] = [
2016-01-13 18:43:41 +03:00
'id_option' => $sType [ 'code' ],
2021-05-31 12:09:15 +03:00
'name' => $sType [ 'name' ],
'ordering' => $sType [ 'ordering' ],
2021-11-03 16:19:39 +07:00
];
2016-01-13 18:43:41 +03:00
}
2021-05-31 12:09:15 +03:00
usort ( $crmStatusTypes , function ( $a , $b ) {
if ( $a [ 'ordering' ] == $b [ 'ordering' ]) {
return 0 ;
} else {
2021-11-03 16:19:39 +07:00
return $a [ 'ordering' ] < $b [ 'ordering' ] ? - 1 : 1 ;
2021-05-31 12:09:15 +03:00
}
});
2016-01-13 18:43:41 +03:00
}
return $crmStatusTypes ;
}
2021-06-25 18:44:52 +03:00
public function getApiPaymentTypes ()
2016-01-13 18:43:41 +03:00
{
2021-11-03 16:19:39 +07:00
$crmPaymentTypes = [];
2016-01-13 18:43:41 +03:00
$request = $this -> api -> paymentTypesList ();
if ( $request ) {
2021-11-03 16:19:39 +07:00
$crmPaymentTypes [] = [
2016-01-13 18:43:41 +03:00
'id_option' => '' ,
'name' => '' ,
2021-11-03 16:19:39 +07:00
];
2016-01-13 18:43:41 +03:00
foreach ( $request -> paymentTypes as $pType ) {
2021-06-08 16:04:39 +03:00
if ( ! $pType [ 'active' ]) {
continue ;
}
2021-11-03 16:19:39 +07:00
$crmPaymentTypes [] = [
2016-01-13 18:43:41 +03:00
'id_option' => $pType [ 'code' ],
2021-11-03 16:19:39 +07:00
'name' => $pType [ 'name' ],
];
2016-01-13 18:43:41 +03:00
}
}
return $crmPaymentTypes ;
}
2021-07-27 13:25:44 +03:00
public function getSite ()
{
try {
$response = $this -> api -> credentials ();
if ( ! ( $response instanceof RetailcrmApiResponse ) || ! $response -> isSuccessful ()
2021-11-03 16:19:39 +07:00
|| 'access_selective' !== $response [ 'siteAccess' ]
|| 1 !== count ( $response [ 'sitesAvailable' ])
2021-07-27 13:25:44 +03:00
|| ! in_array ( '/api/reference/sites' , $response [ 'credentials' ])
|| ! in_array ( '/api/reference/sites/{code}/edit' , $response [ 'credentials' ])
) {
RetailcrmLogger :: writeCaller (
__METHOD__ ,
sprintf (
'ShopID=%s: Error with CRM credentials: need an valid apiKey assigned to one certain site' ,
Shop :: getContextShopID ()
)
);
return null ;
}
$response = $this -> api -> sitesList ();
if ( $response instanceof RetailcrmApiResponse && $response -> isSuccessful ()
&& $response -> offsetExists ( 'sites' ) && $response [ 'sites' ]) {
return current ( $response [ 'sites' ]);
}
} catch ( Exception $e ) {
2022-02-07 14:19:39 +03:00
RetailcrmLogger :: writeException ( __METHOD__ , $e -> getMessage (), null , false );
} catch ( Error $e ) {
RetailcrmLogger :: writeException ( __METHOD__ , $e -> getMessage (), null , false );
2021-07-27 13:25:44 +03:00
}
return null ;
}
2018-05-28 17:09:31 +03:00
public function getStores ()
{
$storesShop = $this -> getShopStores ();
$retailcrmStores = $this -> getApiStores ();
foreach ( $storesShop as $key => $storeShop ) {
2021-11-03 16:19:39 +07:00
$stores [] = [
2018-05-28 17:09:31 +03:00
'type' => 'select' ,
2021-11-03 16:19:39 +07:00
'name' => 'RETAILCRM_STORES[' . $key . ']' ,
2018-05-28 17:09:31 +03:00
'label' => $storeShop ,
2021-11-03 16:19:39 +07:00
'options' => [
2018-05-28 17:09:31 +03:00
'query' => $retailcrmStores ,
'id' => 'id_option' ,
2021-11-03 16:19:39 +07:00
'name' => 'name' ,
],
];
2018-05-28 17:09:31 +03:00
}
return $stores ;
}
protected function getShopStores ()
{
2021-11-03 16:19:39 +07:00
$stores = [];
2018-05-28 17:09:31 +03:00
$warehouses = Warehouse :: getWarehouses ();
foreach ( $warehouses as $warehouse ) {
$arrayName = explode ( '-' , $warehouse [ 'name' ]);
$warehouseName = trim ( $arrayName [ 1 ]);
$stores [ $warehouse [ 'id_warehouse' ]] = $warehouseName ;
}
return $stores ;
}
protected function getApiStores ()
{
2021-11-03 16:19:39 +07:00
$crmStores = [];
2018-05-28 17:09:31 +03:00
$response = $this -> api -> storesList ();
if ( $response ) {
2021-11-03 16:19:39 +07:00
$crmStores [] = [
2018-05-28 17:09:31 +03:00
'id_option' => '' ,
2021-11-03 16:19:39 +07:00
'name' => '' ,
];
2018-05-28 17:09:31 +03:00
foreach ( $response -> stores as $store ) {
2021-11-03 16:19:39 +07:00
$crmStores [] = [
2018-05-28 17:09:31 +03:00
'id_option' => $store [ 'code' ],
2021-11-03 16:19:39 +07:00
'name' => $store [ 'name' ],
];
2018-05-28 17:09:31 +03:00
}
}
return $crmStores ;
}
2016-01-13 18:43:41 +03:00
}