Esempio n. 1
0
 public function process()
 {
     $stop = 0;
     if ($this->start == 0) {
         $this->modx->exec("TRUNCATE TABLE {$this->modx->getTableName('mSkladProductExchange')}");
     }
     $request = new RestRequest($this->config['restUrl'] . 'Good/list?start=' . $this->start . '&count=500', 'GET');
     $request->setUsername($this->config['Username']);
     $request->setPassword($this->config['Password']);
     $request->execute();
     $responseInfo = $request->getResponseInfo();
     $http_code = $responseInfo['http_code'];
     if ($this->config['debug']) {
         $this->modx->msklad->restLog('loadProduct', $request);
     }
     if ($http_code == '200') {
         $xml = simplexml_load_string($request->getResponseBody());
         $this->total = $xml->attributes()->total;
         foreach ($xml->good as $val) {
             $code = isset($val->code) && preg_match("/^[\\d\\+]+\$/", $val->code) ? intval($val->code) : 0;
             $uuid = isset($val->uuid) ? (string) $val->uuid : '';
             $uuid1c = isset($val->externalcode) ? (string) $val->externalcode : '';
             $priceUuid = '';
             foreach ($val->salePrices->price as $price) {
                 if ($price->attributes()->priceTypeUuid == $this->config['SalePriceType']) {
                     $priceUuid = $price->uuid;
                 }
             }
             $newProduct = $this->modx->newObject('mSkladProductExchange');
             $newProduct->fromArray(array('product_id' => $code, 'uuid_1c' => $uuid1c, 'uuid' => $uuid, 'price_uuid' => $priceUuid));
             if (!$newProduct->save()) {
                 return $this->failure($this->modx->lexicon('msklad_api_err_db'));
             }
         }
         unset($xml);
     } else {
         return $this->failure($this->modx->lexicon('msklad_api_err_connect'));
     }
     if ($this->start + 500 > $this->total) {
         $stop = 1;
     }
     return $this->success(array('total' => $this->total, 'stop' => $stop));
 }
 public function setUomUuid()
 {
     $request = new RestRequest($this->config['restUrl'] . 'Uom/list', 'GET');
     $request->setUsername($this->config['Username']);
     $request->setPassword($this->config['Password']);
     $request->execute();
     $responseInfo = $request->getResponseInfo();
     $http_code = $responseInfo['http_code'];
     if ($http_code == '200') {
         $xml = simplexml_load_string($request->getResponseBody());
         $cc = 0;
         foreach ($xml->uom as $val) {
             $name = (string) $val->attributes()->name;
             if ($name == 'шт') {
                 $uomType = $this->modx->getObject('modSystemSetting', 'msklad_uom_type_uuid');
                 $uomType->set('value', $val->uuid);
                 $uomType->save();
             }
             ++$cc;
         }
         unset($xml);
     }
 }
 public function process()
 {
     $stop = 0;
     $c = $this->modx->newQuery('mSkladProductData');
     $c->innerJoin('msProduct', 'msProduct', 'msProduct.id=mSkladProductData.product_id');
     $c->innerJoin('msProductData', 'msProductData', 'msProductData.id=mSkladProductData.product_id');
     $c->sortby('mSkladProductData.product_id', 'ASC');
     $c->select('mSkladProductData.product_id,mSkladProductData.uuid,mSkladProductData.uuid_1c,mSkladProductData.price_uuid,mSkladProductData.active,msProduct.parent,msProduct.pagetitle,msProduct.longtitle,msProduct.description,msProductData.*');
     $c->where(array('active:=' => '1'));
     $this->total = $this->modx->getCount('mSkladProductData', $c);
     $c->limit(100, $this->start);
     $c->prepare();
     if ($c->stmt->execute()) {
         //$c->prepare() &&
         $productsData = $c->stmt->fetchAll(PDO::FETCH_ASSOC);
         $collection = array();
         if (is_array($productsData) && count($productsData) > 0) {
             foreach ($productsData as $productData) {
                 $name = trim(htmlspecialchars(html_entity_decode($productData['pagetitle'], ENT_COMPAT, 'UTF-8'), ENT_QUOTES));
                 $description = trim(htmlspecialchars(html_entity_decode($productData['description'], ENT_COMPAT, 'UTF-8'), ENT_QUOTES));
                 $price = str_replace(".", "", $productData['price']) . '.0';
                 $weight = $productData['weight'];
                 $code = $productData['product_id'];
                 $productCode = trim(htmlspecialchars(html_entity_decode($productData['article'], ENT_COMPAT, 'UTF-8'), ENT_QUOTES));
                 $uuid = $productData['uuid'];
                 $priceUuid = $productData['price_uuid'];
                 $priceTypeUuid = $this->config['SalePriceType'];
                 $uomUuid = $this->config['Uom'];
                 $externalCode = $uuid_1c = $productData['uuid_1c'];
                 $parentUuid = '';
                 if ($productData['parent']) {
                     $parent_q = $this->modx->newQuery('msCategory', array('id' => $productData['parent']));
                     $parent_q->leftJoin('mSkladCategoryData', 'Data', 'Data.category_id = msCategory.id');
                     $parent_q->select('msCategory.id, Data.uuid');
                     $parent_q->limit(1);
                     if ($parent_q->prepare() && $parent_q->stmt->execute()) {
                         $parentRes = $parent_q->stmt->fetch(PDO::FETCH_ASSOC);
                         $parentUuid = !empty($parentRes['uuid']) ? 'parentUuid="' . $parentRes['uuid'] . '"' : '';
                     }
                 }
                 $collection[] = '
                         <good isSerialTrackable="false" uomUuid="' . $uomUuid . '" productCode="' . $productCode . '" name="' . $name . '" weight="' . $weight . '" ' . $parentUuid . '>
                             <uuid>' . $uuid . '</uuid>
                             <code>' . $code . '</code>
                             <externalcode>' . $externalCode . '</externalcode>
                             <description>' . $description . '</description>
                             <salePrices>
                                 <price value="' . $price . '" priceTypeUuid="' . $priceTypeUuid . '">
                                     <uuid>' . $priceUuid . '</uuid>
                                 </price>
                             </salePrices>
                         </good>
                     ';
             }
             $xml = '<?xml version="1.0" encoding="UTF-8"?>
                     <collection>
                     ' . implode('', $collection) . '
                     </collection>';
             $request = new RestRequest($this->config['restUrl'] . 'Good/list/update', 'PUT', $xml);
             $request->setUsername($this->config['Username']);
             $request->setPassword($this->config['Password']);
             $request->execute();
             $responseInfo = $request->getResponseInfo();
             $http_code = $responseInfo['http_code'];
             if ($this->config['debug']) {
                 $this->modx->msklad->restLog('updateProductSklad_' . $this->start, $request);
             }
             if ($http_code == '200') {
                 $xml = simplexml_load_string($request->getResponseBody());
                 $cc = 0;
                 foreach ($xml->id as $val) {
                     if ($productData = $this->modx->getObject('mSkladProductData', array('product_id' => $productsData[$cc]['product_id']))) {
                         $productData->set('uuid', $val);
                         $productData->save();
                     }
                     ++$cc;
                 }
                 unset($xml);
             }
             unset($productsData);
         }
     }
     if ($this->start + 100 > $this->total) {
         $stop = 1;
     }
     return $this->success(array('total' => $this->total, 'stop' => $stop));
 }
 public function process()
 {
     $stop = 0;
     $c = $this->modx->newQuery('mSkladCategoryData');
     $c->select('category_id,uuid,level,active');
     $c->where(array('active:=' => '1', 'level:=' => $this->level));
     $this->total = $this->modx->getCount('mSkladCategoryData', $c);
     $c->limit(100, $this->start);
     if ($c->prepare() && $c->stmt->execute()) {
         $categoriesData = $c->stmt->fetchAll(PDO::FETCH_ASSOC);
         $collection = array();
         if (is_array($categoriesData) && count($categoriesData) > 0) {
             foreach ($categoriesData as $categoryData) {
                 if ($category = $this->modx->getObject('msCategory', array('id' => $categoryData['category_id']))) {
                     $name = trim(htmlspecialchars(html_entity_decode($category->get('pagetitle'), ENT_COMPAT, 'UTF-8'), ENT_QUOTES));
                     $description = trim(htmlspecialchars(html_entity_decode($category->get('description'), ENT_COMPAT, 'UTF-8'), ENT_QUOTES));
                     $parentUuid = '';
                     if ($categoryData['level'] > 1) {
                         $parent_q = $this->modx->newQuery('msCategory', array('id' => $category->get('parent')));
                         $parent_q->leftJoin('mSkladCategoryData', 'Data', 'Data.category_id = msCategory.id');
                         $parent_q->select('msCategory.id, Data.uuid, Data.uuid_1c');
                         $parent_q->limit(1);
                         //                        $parent_q->prepare();
                         //                        $this->modx->msklad->restLog('toSQL',print_r($parent_q->toSQL(),true));
                         if ($parent_q->prepare() && $parent_q->stmt->execute()) {
                             $parentRes = $parent_q->stmt->fetch(PDO::FETCH_ASSOC);
                             $parentUuid = !empty($parentRes['uuid']) ? 'parentUuid="' . $parentRes['uuid'] . '"' : '';
                         }
                     }
                     $collection[] = '
                         <goodFolder ' . $parentUuid . ' productCode="" name="' . $name . '">
                             <uuid>' . $categoryData['uuid'] . '</uuid>
                             <description>' . $description . '</description>
                             <code>' . $categoryData['category_id'] . '</code>
                             <externalcode>' . $categoryData['uuid_1c'] . '</externalcode>
                         </goodFolder>
                     ';
                 }
             }
             $xml = '<?xml version="1.0" encoding="UTF-8"?>
                     <collection>
                     ' . implode('', $collection) . '
                     </collection>';
             $request = new RestRequest($this->config['restUrl'] . 'GoodFolder/list/update', 'PUT', $xml);
             $request->setUsername($this->config['Username']);
             $request->setPassword($this->config['Password']);
             $request->execute();
             $responseInfo = $request->getResponseInfo();
             $http_code = $responseInfo['http_code'];
             if ($this->config['debug']) {
                 $this->modx->msklad->restLog('updateCategorySklad_' . $this->start . '_' . $this->level, $request);
             }
             if ($http_code == '200') {
                 $xml = simplexml_load_string($request->getResponseBody());
                 $cc = 0;
                 foreach ($xml->id as $val) {
                     if ($categoryData = $this->modx->getObject('mSkladCategoryData', array('category_id' => $categoriesData[$cc]['category_id']))) {
                         $categoryData->set('uuid', $val);
                         $categoryData->save();
                     }
                     ++$cc;
                 }
                 unset($xml);
             }
             unset($categoriesData);
         } else {
             $this->level += 1;
         }
     }
     if ($this->start == 0 && !$this->total) {
         $stop = 1;
     }
     return $this->success(array('total' => $this->total, 'level' => $this->level, 'stop' => $stop));
 }
Esempio n. 5
0
<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    $data = str_replace('\\n', '', $_POST['body']);
    $data = str_replace(' ', '', $data);
    $data = stripslashes($data);
    $request = new RestRequest($_POST['url'], $_POST['method'], $data);
    $request->setUsername('qwe');
    $request->setPassword('qwe');
    $request->execute();
}
?>
<!DOCTYPE html>
<html>
<head>
	<title>REST API demo</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

<h1>REST API demo</h1>
<form method="post">
	<p>
		<labe>Method</label><br>
		<select name="method">
			<option value="GET" <?php 
if (isset($_POST['method']) && $_POST['method'] == 'GET') {
    print "selected";
}
?>
>GET</option>