2016-01-13 18:43:41 +03:00
< ? php
2020-04-16 15:26:03 +03:00
/**
* MIT License
*
* Copyright ( c ) 2020 DIGITAL RETAIL TECHNOLOGIES SL
*
* 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 >
* @ copyright 2020 DIGITAL RETAIL TECHNOLOGIES SL
* @ 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 .
*/
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 ;
public $payment_modules = array ();
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 ()
{
$deliveryTypes = array ();
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 ) {
2016-01-13 18:43:41 +03:00
$deliveryTypes [] = array (
'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 ,
'options' => array (
2017-05-31 14:23:18 +03:00
'query' => $apiDeliveryTypes ,
2016-01-13 18:43:41 +03:00
'id' => 'id_option' ,
'name' => 'name'
)
);
}
}
return $deliveryTypes ;
}
public function getStatuses ()
{
$statusTypes = array ();
$states = OrderState :: getOrderStates ( $this -> default_lang , true );
2017-05-31 14:23:18 +03:00
$apiStatuses = $this -> getApiStatuses ();
2016-01-13 18:43:41 +03:00
if ( ! empty ( $states )) {
foreach ( $states as $state ) {
if ( $state [ 'name' ] != ' ' ) {
$key = $state [ 'id_order_state' ];
$statusTypes [] = array (
'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 ,
'options' => array (
2017-05-31 14:23:18 +03:00
'query' => $apiStatuses ,
2016-01-13 18:43:41 +03:00
'id' => 'id_option' ,
'name' => 'name'
)
);
}
}
}
return $statusTypes ;
}
public function getPaymentTypes ()
{
$payments = $this -> getSystemPaymentModules ();
$paymentTypes = array ();
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 ) {
$paymentTypes [] = array (
'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 ,
'options' => array (
2017-05-31 14:23:18 +03:00
'query' => $apiPaymentTypes ,
2016-01-13 18:43:41 +03:00
'id' => 'id_option' ,
'name' => 'name'
)
);
}
}
return $paymentTypes ;
}
2017-08-14 12:06:10 +02:00
public function getPaymentAndDeliveryForDefault ( $arParams )
2017-04-05 17:56:26 +03:00
{
$paymentTypes = array ();
$deliveryTypes = array ();
$paymentDeliveryTypes = array ();
if ( ! empty ( $this -> carriers )) {
$deliveryTypes [] = array (
'id_option' => '' ,
'name' => '' ,
);
foreach ( $this -> carriers as $valCarrier ) {
$deliveryTypes [] = array (
'id_option' => $valCarrier [ 'id_carrier' ],
'name' => $valCarrier [ 'name' ],
);
}
$paymentDeliveryTypes [] = array (
'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 ,
'options' => array (
'query' => $deliveryTypes ,
'id' => 'id_option' ,
'name' => 'name'
)
);
}
2017-05-22 21:13:18 +04:00
$paymentModules = $this -> getSystemPaymentModules ();
if ( ! empty ( $paymentModules )) {
2017-04-05 17:56:26 +03:00
$paymentTypes [] = array (
'id_option' => '' ,
'name' => '' ,
);
2017-05-31 14:23:18 +03:00
foreach ( $paymentModules as $valPayment ) {
2017-04-05 17:56:26 +03:00
$paymentTypes [ $valPayment [ 'id' ]] = array (
'id_option' => $valPayment [ 'code' ],
'name' => $valPayment [ 'name' ],
);
}
$paymentDeliveryTypes [] = array (
'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 ,
'options' => array (
'query' => $paymentTypes ,
'id' => 'id_option' ,
'name' => 'name'
)
);
}
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 ();
2016-01-13 18:43:41 +03:00
foreach ( $modules as $module ) {
2020-04-16 15:26:03 +03:00
if ( ! empty ( $module -> parent_class ) && $module -> parent_class == 'PaymentModule' ) {
2016-01-13 18:43:41 +03:00
if ( $module -> id ) {
2020-04-16 15:26:03 +03:00
$module_id = ( int ) $module -> id ;
2016-01-13 18:43:41 +03:00
if ( ! get_class ( $module ) == 'SimpleXMLElement' )
$module -> country = array ();
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 ));
2016-01-13 18:43:41 +03:00
foreach ( $countries as $country )
$module -> country [] = $country [ 'id_country' ];
if ( ! get_class ( $module ) == 'SimpleXMLElement' )
$module -> currency = array ();
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 ));
2016-01-13 18:43:41 +03:00
foreach ( $currencies as $currency )
$module -> currency [] = $currency [ 'id_currency' ];
if ( ! get_class ( $module ) == 'SimpleXMLElement' )
$module -> group = array ();
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 ));
2016-01-13 18:43:41 +03:00
foreach ( $groups as $group )
$module -> group [] = $group [ 'id_group' ];
} else {
$module -> country = null ;
$module -> currency = null ;
$module -> group = null ;
}
2018-05-28 17:09:31 +03:00
if ( $module -> active != 0 || $active === false ) {
2016-01-13 18:43:41 +03:00
$this -> payment_modules [] = array (
'id' => $module -> id ,
'code' => $module -> name ,
'name' => $module -> displayName
);
}
}
}
return $this -> payment_modules ;
}
2017-09-27 14:38:24 +02:00
public function getStatuseDefaultExport ()
{
return $this -> getApiStatuses ();
}
2016-01-13 18:43:41 +03:00
protected function getApiDeliveryTypes ()
{
$crmDeliveryTypes = array ();
$request = $this -> api -> deliveryTypesList ();
if ( $request ) {
$crmDeliveryTypes [] = array (
'id_option' => '' ,
'name' => '' ,
);
foreach ( $request -> deliveryTypes as $dType ) {
$crmDeliveryTypes [] = array (
'id_option' => $dType [ 'code' ],
'name' => $dType [ 'name' ],
);
}
}
return $crmDeliveryTypes ;
}
protected function getApiStatuses ()
{
$crmStatusTypes = array ();
$request = $this -> api -> statusesList ();
if ( $request ) {
$crmStatusTypes [] = array (
'id_option' => '' ,
'name' => '' ,
2021-05-31 12:09:15 +03:00
'ordering' => '' ,
2016-01-13 18:43:41 +03:00
);
foreach ( $request -> statuses as $sType ) {
$crmStatusTypes [] = array (
'id_option' => $sType [ 'code' ],
2021-05-31 12:09:15 +03:00
'name' => $sType [ 'name' ],
'ordering' => $sType [ 'ordering' ],
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 {
return ( $a [ 'ordering' ] < $b [ 'ordering' ] ? - 1 : 1 );
}
});
2016-01-13 18:43:41 +03:00
}
return $crmStatusTypes ;
}
protected function getApiPaymentTypes ()
{
$crmPaymentTypes = array ();
$request = $this -> api -> paymentTypesList ();
if ( $request ) {
$crmPaymentTypes [] = array (
'id_option' => '' ,
'name' => '' ,
);
foreach ( $request -> paymentTypes as $pType ) {
$crmPaymentTypes [] = array (
'id_option' => $pType [ 'code' ],
'name' => $pType [ 'name' ]
);
}
}
return $crmPaymentTypes ;
}
2018-05-28 17:09:31 +03:00
public function getStores ()
{
$storesShop = $this -> getShopStores ();
$retailcrmStores = $this -> getApiStores ();
foreach ( $storesShop as $key => $storeShop ) {
$stores [] = array (
'type' => 'select' ,
'name' => 'RETAILCRM_STORES[' . $key . ']' ,
'label' => $storeShop ,
'options' => array (
'query' => $retailcrmStores ,
'id' => 'id_option' ,
'name' => 'name'
)
);
}
return $stores ;
}
protected function getShopStores ()
{
$stores = array ();
$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 ()
{
$crmStores = array ();
$response = $this -> api -> storesList ();
if ( $response ) {
$crmStores [] = array (
'id_option' => '' ,
'name' => ''
);
foreach ( $response -> stores as $store ) {
$crmStores [] = array (
'id_option' => $store [ 'code' ],
'name' => $store [ 'name' ]
);
}
}
return $crmStores ;
}
2016-01-13 18:43:41 +03:00
}