PageRenderTime 69ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/modules/soap1c/classes/model/soap1c.php

https://bitbucket.org/seyar/ari100krat.local
PHP | 674 lines | 626 code | 15 blank | 33 comment | 11 complexity | 83e78502c2da61bde55af4e6aa761cbd MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1
  1. <?php defined('SYSPATH') OR die('No direct access allowed.');
  2. /* @version $Id: v 0.1 04.06.2010 - 15:38:00 Exp $
  3. *
  4. * Project: microline.shade
  5. * File: transport.php *
  6. *
  7. * This library is commercial distributed software; you can't
  8. * redistribute it and/or modify it without owner (or author) approval.
  9. *
  10. * @link http://bestartdesign.com
  11. * @Best IT Solutions (C) 2010
  12. *
  13. * @author Seyar Chapuh <seyarchapuh@gmail.com>, <sc@bestitsolutions.biz>
  14. */
  15. class Model_Soap1c extends Model
  16. {
  17. function deleteCategory($id)
  18. {
  19. if (is_numeric($id))
  20. {
  21. if( Model_Categories::categories_delete($id) )
  22. return 1;
  23. }
  24. return 0;
  25. }
  26. function saveCategory($data)
  27. {
  28. if (!$post = json_decode($data, TRUE))
  29. {
  30. return new SoapFault("error", "Ошибка разбора soap");
  31. }
  32. $id = (int) $post['id'];
  33. if (!$id)
  34. {
  35. return new SoapFault("error", "на задан ай-да");
  36. }
  37. $lang = 1;
  38. if (!$post['parent_id'])
  39. {
  40. return new SoapFault("error", "на задан парент ай-да");
  41. }
  42. Kohana_Log::instance()->add('soap save category', print_r($post,1));
  43. $res = Model_Categories::categories_load($id);
  44. $parent = Model_Categories::categories_load($post['parent_id']);
  45. if( !isset($parent) || count($parent) == 0 )
  46. {
  47. return new SoapFault("error", "Парент ай-да не существует");
  48. }
  49. $post['description'] = isset($post['description']) ? $post['description'] : DB::expr('description');
  50. $post['status'] = isset($post['status']) ? $post['status'] : DB::expr('status');
  51. $post['name'] = isset($post['name']) ? $post['name'] : DB::expr('name');
  52. $post['seo_url'] = isset($post['seo_url']) ? $post['seo_url'] : ( isset($post['name']) ? Controller_Admin::to_url($post['name']) : DB::expr('seo_url') );
  53. if ((int) @$res['id'])
  54. { // update
  55. $id = (int) $id;
  56. // Load current page info
  57. // $result = DB::select()->from('shop_categories')
  58. // ->limit(1)
  59. // ->where('id', '=', $id)
  60. // ->execute()
  61. // ->current()
  62. // ;
  63. DB::update('shop_categories')
  64. ->set(
  65. array(
  66. // 'name' => $post['name'],
  67. 'seo_url' => $post['seo_url'],
  68. // 'seo_keywords' => $post['seo_keywords'],
  69. // 'seo_description' => $post['seo_description'],
  70. 'description' => $post['description'],
  71. 'parent_id' => $post['parent_id'],
  72. // 'order_id' => $post['order_id'],
  73. 'status' => $post['status'],
  74. )
  75. )
  76. ->where('id', '=', $id)
  77. ->execute()
  78. ;
  79. }
  80. else
  81. { // create
  82. $id = 0;
  83. list($id, $total_rows) = DB::insert('shop_categories',
  84. array(
  85. 'id',
  86. 'languages_id',
  87. 'name',
  88. 'seo_url',
  89. // 'seo_keywords',
  90. // 'seo_description',
  91. 'description',
  92. 'parent_id',
  93. // 'order_id',
  94. 'status'
  95. )
  96. )
  97. ->values(
  98. array(
  99. $post['id'],
  100. $lang,
  101. $post['name'],
  102. $post['seo_url'],
  103. // $post['seo_keywords'],
  104. // $post['seo_description'],
  105. $post['description'],
  106. $post['parent_id'],
  107. // $post['order_id'],
  108. $post['status'],
  109. )
  110. )
  111. ->execute();
  112. }
  113. // new parents_path from new id
  114. $q1 = DB::select('parents_path')->from('shop_categories')
  115. ->where('id', '=', $post['parent_id'])
  116. ->execute();
  117. $q1 = $q1->as_array();
  118. $new_parent = $q1[0]['parents_path'].$id.',';
  119. DB::update('shop_categories')
  120. ->set(array('parents_path' => $new_parent))
  121. ->where('id', '=', $id)
  122. ->execute();
  123. // Model_Categories::save_add_filelds($lang, $id, $post);
  124. // return $id;
  125. return 1;
  126. }
  127. function saveGood($data)
  128. {
  129. if( substr( trim($data), 0,1 ) != '[' ) $data = "[$data]";
  130. $search = array('\\', '""""');
  131. $replace = array('_', '""');
  132. $data = str_replace($search, $replace, $data);
  133. $data = preg_replace('|\t|', '', $data);
  134. $data = preg_replace('|\r|', '', $data);
  135. $data = preg_replace('|\n|', '', $data);
  136. Kohana_log::instance()->add('soap save good json', print_r($data,1));
  137. $post = array();
  138. if( !$post = json_decode($data, TRUE) )
  139. {
  140. Kohana_log::instance()->add('$post good errored', print_r($data,1));
  141. return new SoapFault("error", "Ошибка разбора soap");
  142. }
  143. if( count($post) > 0 )
  144. {
  145. $res = array();
  146. foreach( $post as $item )
  147. {
  148. $to = array(Kohana::config('shop.send_to'),Kohana::config('shop.EmailDeveloper'));
  149. $from = array(Kohana::config('shop.send_from'), $_SERVER['HTTP_HOST']);
  150. $subject = array(Kohana::config('shop.subject'));
  151. if( self::saveGoodSingle($item) != 1)
  152. {
  153. Email::send($to,$from, $subject, 'не прошло сохранение товара с 1с '.$item['id'] );
  154. Kohana_log::instance()->add('soap error save good', print_r($item,1) );
  155. }
  156. }
  157. }
  158. return 1;
  159. }
  160. static function saveGoodSingle($post)
  161. {
  162. Kohana_log::instance()->add('soap save good', print_r($post,1));
  163. /*
  164. array(
  165. id
  166. categories_id
  167. brand_id
  168. alter_name
  169. scu
  170. name
  171. short_description
  172. full_description
  173. is_bestseller
  174. is_new
  175. rating
  176. show_on_startpage
  177. default_photo
  178. start_date
  179. edit_date
  180. cost
  181. weight
  182. quantity
  183. guaranties_period
  184. store_approximate_date
  185. seo_keywords
  186. seo_description
  187. seo_url
  188. status
  189. );
  190. */
  191. // if (!$post = json_decode($data, TRUE))
  192. // {
  193. // return new SoapFault("error", "Ошибка разбора soap");
  194. // }
  195. //
  196. $post['currencies_id'] = @$post['currencies_id'] ? $post['currencies_id'] : 1;
  197. $id = (int) $post['id'];
  198. if (!$id)
  199. {
  200. return new SoapFault("error", "на задан ай-ди");
  201. }
  202. /* проверяем есть ли такой товар. */
  203. $res = Model_Goods::load($id);
  204. $cat = Model_Categories::categories_load($post['categories_id']);
  205. if(count($cat) == 0)
  206. {
  207. return new SoapFault("error", "Категории не существует");
  208. }
  209. $post['status'] = Model_Content::arrGet($post, 'status', 'from1c');
  210. $goodstatuses = array('active','archive','out_of_store','unverified', 'allow_to_order');
  211. $post['status'] = in_array(trim($post['status']),$goodstatuses) ? $post['status'] : 'from1c';
  212. $post['seo_url'] = isset($post['seo_url']) ? str_replace( Model_Frontend_Categories::$url_search, Model_Frontend_Categories::$url_replace, urlencode($post['seo_url']) ) : $res['seo_url'];
  213. $post['seo_url'] = str_replace( '__', '_', $post['seo_url'] );
  214. $post['seo_url'] = strtolower( str_replace( '__', '_', $post['seo_url'] ) );
  215. $post['scu'] = isset($post['scu']) ? $post['scu'] : DB::expr('scu');
  216. $post['full_description'] = isset($post['full_description']) ? $post['full_description'] : DB::expr('full_description');
  217. $post['cost'] = isset($post['cost']) ? $post['cost'] : DB::expr('cost');
  218. $post['quantity'] = isset($post['quantity']) ? $post['quantity'] : DB::expr('quantity');
  219. $post['guaranties_period'] = isset($post['guaranties_period']) ? $post['guaranties_period'] : DB::expr('guaranties_period');
  220. $post['alter_name'] = isset($post['alter_name']) ? $post['alter_name'] : ( isset($post['name']) ? $post['name'] : DB::expr('alter_name') );
  221. $post['store_approximate_date'] = (isset($post['store_approximate_date']) && strtotime($post['store_approximate_date']) ? $post['store_approximate_date'] : DB::expr('store_approximate_date') );
  222. $post['name'] = isset($post['name']) ? $post['name'] : Model_Content::arrGet($post, 'alter_name', 'new good from1c');
  223. if( isset($res) && count($res) > 0 )
  224. { // update
  225. DB::update('shop_goods')
  226. ->set(
  227. array(
  228. 'categories_id' => $post['categories_id'],
  229. 'scu' => $post['scu'],
  230. 'name' => (isset($res) && !empty($res['name']) && $res['name'] != 0 ) ? $res['name'] : $post['name'],
  231. // 'brand_id' => $post['brand_id'],
  232. 'alter_name' => $post['alter_name'],
  233. 'short_description' => $post['full_description'],
  234. // 'full_description' => @$post['full_description'],
  235. // 'is_bestseller' => $post['is_bestseller'],
  236. // 'is_new' => $post['is_new'],
  237. // 'rating' => $post['rating'],
  238. // 'show_on_startpage' => $post['show_on_startpage'],
  239. 'cost' => $post['cost'],
  240. // 'weight' => $post['weight'],
  241. 'quantity' => $post['quantity'],
  242. 'guaranties_period' => $post['guaranties_period'],
  243. 'store_approximate_date' => $post['store_approximate_date'],
  244. // 'seo_keywords' => $post['seo_keywords'],
  245. // 'seo_description' => $post['seo_description'],
  246. 'seo_url' => $post['seo_url'],
  247. 'status' => $post['status'],
  248. )
  249. )
  250. ->where('id', '=', $id)
  251. ->execute()
  252. ;
  253. }
  254. else
  255. { // create
  256. $id = $total_rows = 0;
  257. $post['status'] = $post['status'] == 'archive' ? 'archive' : 'from1c';
  258. list($id, $total_rows) = DB::insert('shop_goods',
  259. array(
  260. 'id',
  261. 'categories_id',
  262. 'scu',
  263. 'name',
  264. // 'brand_id',
  265. 'alter_name',
  266. // 'short_description',
  267. 'full_description',
  268. // 'is_bestseller',
  269. // 'is_new',
  270. // 'rating',
  271. // 'show_on_startpage',
  272. 'cost',
  273. // 'weight',
  274. 'quantity',
  275. 'guaranties_period',
  276. 'store_approximate_date',
  277. // 'seo_keywords',
  278. // 'seo_description',
  279. 'seo_url',
  280. 'status',
  281. )
  282. )
  283. ->values(
  284. array(
  285. $post['id'],
  286. $post['categories_id'],
  287. $post['scu'],
  288. $post['name'],
  289. // $post['brand_id'],
  290. $post['alter_name'],
  291. // $post['short_description'],
  292. $post['full_description'],
  293. // $post['is_bestseller'],
  294. // $post['is_new'],
  295. // $post['rating'],
  296. // $post['show_on_startpage'],
  297. $post['cost'],
  298. // $post['weight'],
  299. $post['quantity'],
  300. $post['guaranties_period'],
  301. $post['store_approximate_date'],
  302. // $post['seo_keywords'],
  303. // $post['seo_description'],
  304. $post['seo_url'],
  305. $post['status'],
  306. )
  307. )
  308. ->execute();
  309. }
  310. // Prices update
  311. $post['currencies_id'] = Model_Content::arrGet($post, 'currencies_id', 1);
  312. $post['price'] = Arr::get($post, 'price', DB::expr('price') );
  313. $post['price_uah'] = Arr::get($post, 'price_uah', DB::expr('price') );
  314. self::pricesUpdate( $post['id'],$post['currencies_id'],$post['price'],$post['price_uah'] );
  315. return 1;
  316. }
  317. function pricesUpdate( $id = NULL, $currencies_id = NULL, $price = NULL, $price_uah = NULL )
  318. {
  319. $priceCats = Db::select()
  320. ->from('shop_price_categories')
  321. ->execute()
  322. ;
  323. if( $priceCats->count() > 0 )
  324. {
  325. foreach( $priceCats->as_array() as $item )
  326. {
  327. $priceRow = Db::select()
  328. ->from('shop_prices')
  329. ->where( 'currencies_id', '=', $currencies_id )
  330. ->and_where('good_id', '=', $id)
  331. ->and_where('price_categories_id', '=', $item['id'])
  332. ->execute()
  333. ;
  334. if( $priceRow->count() == 0 )
  335. {
  336. // insert new price
  337. $q = Db::insert('shop_prices', array('price', 'price_categories_id', 'currencies_id', 'good_id'));
  338. $q->values(array(
  339. $price,
  340. $item['id'],
  341. $currencies_id,
  342. $id
  343. ));
  344. if( Kohana::config('shop.rateMode') == 'manual' )
  345. {
  346. $q->values(array(
  347. $price_uah,
  348. Kohana::config('shop.rate_uah_id'),
  349. $currencies_id,
  350. $id
  351. ));
  352. }
  353. $q->execute();
  354. }
  355. else
  356. {
  357. // update current price
  358. $query = Db::update('shop_prices')
  359. ->set( array('price' => $price) )
  360. ->where('currencies_id', '=', $currencies_id )
  361. ->where('price_categories_id', '=', $item['id'] )
  362. ->and_where('good_id', '=', $id)
  363. ->execute()
  364. ;
  365. }
  366. }
  367. }
  368. }
  369. function deleteGood($id)
  370. {
  371. $query = DB::delete('shop_goods');
  372. if (is_numeric($id))
  373. {
  374. $query->where('id', '=', $id); // ->limit(1)
  375. $total_rows = $query->execute();
  376. return 1;
  377. }else
  378. return 0;
  379. }
  380. function deleteCurrency($id = NULL)
  381. {
  382. $query = DB::delete('shop_currencies');
  383. if (is_numeric($id))
  384. {
  385. $query->where('id', '=', $id); // ->limit(1)
  386. $total_rows = $query->execute();
  387. return 1;
  388. }else
  389. return 0;
  390. }
  391. function saveCurrency($data)
  392. {
  393. Kohana_Log::instance()->add('soap save currency', Kohana::dump($data));
  394. if (!$post = json_decode($data, TRUE))
  395. {
  396. return new SoapFault("error", "Ошибка разбора soap");
  397. }
  398. if (!$id = $post['id'])
  399. {
  400. return new SoapFault("error", "не задан ай-ди");
  401. }
  402. /* может есть уже? */
  403. $res = Model_Currencies::load($post['id']);
  404. if ((int) @$res['id'])
  405. { // update
  406. DB::update('shop_currencies')
  407. ->set(
  408. array(
  409. 'name' => $post['name'],
  410. 'code' => $post['code'],
  411. 'iso_code' => $post['iso_code'],
  412. 'rate' => str_replace(',', '.', $post['rate']),
  413. 'rate_w_vat' => str_replace(',', '.', $post['rate_w_vat']),
  414. )
  415. )
  416. ->where('id', '=', $post['id'])
  417. ->execute();
  418. }
  419. else
  420. { // create
  421. DB::insert('shop_currencies',
  422. array(
  423. 'name', 'code', 'iso_code', 'rate', 'rate_w_vat',
  424. )
  425. )
  426. ->values(
  427. array(
  428. $post['name'],
  429. $post['code'],
  430. $post['iso_code'],
  431. str_replace(',', '.', $post['rate']),
  432. str_replace(',', '.', $post['rate_w_vat']),
  433. )
  434. )
  435. ->execute();
  436. }
  437. return 1;
  438. }
  439. function editOrder($data)
  440. {
  441. /*
  442. array(
  443. 'id',
  444. 'status',
  445. 'goods_list',
  446. );
  447. */
  448. Kohana_Log::instance()->add('editOrder', Kohana::dump($data));
  449. if (!$post = json_decode($data, TRUE))
  450. return new SoapFault("error", "Ошибка разбора soap");
  451. if (!@$post['id'])
  452. return new SoapFault("error", "Ошибка разбора id");
  453. /* get cur order */
  454. $order = Model_Orders::load($post['id']);
  455. /**/
  456. $total_sum = 0;
  457. //Kohana_Log::instance()->add('d', Kohana::debug($post));
  458. foreach ($post['goods_list'] as $key => $val)
  459. {
  460. $info = Model_Goods::load($key);
  461. //$info['scu'] = isset($info['scu']) ? $info['scu'] : '0';
  462. $goods_info[] = array(
  463. 'id' => $info['id'],
  464. 'scu' => $info['scu'],
  465. 'name' => $info['name'] ? $info['name'] : $info['alter_name'],
  466. 'count' => $val,
  467. 'price' => $info['prices'][Kohana::config('shop')->default_category][Kohana::config('shop')->frontend_currency],
  468. 'discount' => 0,
  469. 'sum' => $val * $info['prices'][Kohana::config('shop')->default_category][Kohana::config('shop')->frontend_currency],
  470. );
  471. $total_sum += $val * $info['prices'][Kohana::config('shop')->default_category][Kohana::config('shop')->frontend_currency];
  472. }
  473. $post['exported1c'] = isset($post['exported1c']) ? $post['exported1c'] : 1;
  474. $update = array(
  475. 'status' => $post['status'],
  476. 'exported1c' => $post['exported1c'],
  477. 'subtotal' => $total_sum,
  478. 'sum' => ($total_sum + $order['shipment_cost']),
  479. // 'discount' => ,
  480. // 'tax' => ,
  481. // 'total_cost' => ,
  482. );
  483. if ($goods_info)
  484. $update['ser_details'] = serialize($goods_info);
  485. DB::update('shop_orders')
  486. ->set($update)
  487. ->where('id', '=', $post['id'])
  488. ->execute()
  489. ;
  490. // return $post['id'];
  491. return 1;
  492. }
  493. function deleteOrder($id)
  494. {
  495. /*
  496. 'id',
  497. */
  498. $query = DB::delete('shop_orders');
  499. if (is_numeric($id))
  500. {
  501. $query->where('id', '=', $id); // ->limit(1)
  502. $total_rows = $query->execute();
  503. return 1;
  504. }else
  505. return 0;
  506. return 1;
  507. }
  508. function getOrders()
  509. {
  510. Kohana_Log::instance()->add('soap запрос заказов', '1');
  511. $query = DB::select('*')
  512. ->from(array('shop_orders', 'orders'))
  513. ->where('exported1c', '=', 0)
  514. ;
  515. $result = $query
  516. ->order_by('id', 'DESC')
  517. ->execute();
  518. // if ($result->count() == 0) return new SoapFault("error", "а заказов то новых нет");
  519. if ($result->count() == 0)
  520. return 0;
  521. else
  522. $res = $result->as_array();
  523. $shipment_methods = Model_Shop::shipment_methods();
  524. // DB::update('shop_orders')
  525. // ->set(array('exported1c' => 1))
  526. // ->execute()
  527. // ;
  528. $return = '';
  529. // Kohana_Log::instance()->add('ret', print_r($res,1));
  530. foreach ($res as $key => $item)
  531. {
  532. if (isset($shipment_methods[$item['shipment_method_id']]['name']))
  533. {
  534. $item = array_merge(
  535. array('shipment_method_name' => $shipment_methods[$item['shipment_method_id']]['name']),
  536. $item
  537. );
  538. }
  539. $return .= "{\"";
  540. foreach ($item as $kkey => $iitem)
  541. {
  542. if ($kkey == 'ser_details' || $kkey == 'ser_status_history')
  543. {
  544. $str = '';
  545. $ser = unserialize($iitem);
  546. if ($ser):
  547. $str .= "{";
  548. foreach ($ser as $keygood => $itemGood)
  549. {
  550. foreach ($itemGood as $kkeygood => $iitemGood)
  551. {
  552. $str .= "\"$kkeygood\":\"$iitemGood\", ";
  553. }
  554. }
  555. $str = rtrim($str,', ') . "}, ";
  556. endif;
  557. $return .= $kkey.'":'.str_replace('&quot;','',$str).' "';
  558. }
  559. else
  560. {
  561. $return .= ''.$kkey.'":"'.str_replace('&quot;','',$iitem).'", "';
  562. }
  563. }
  564. $return = rtrim($return,', "') . "}\r\n";
  565. }
  566. //$e = new SoapFault("error", "error message");
  567. // file_put_contents(DOCROOT . '/file.txt', $return . "\r\n");
  568. //Kohana_Log::instance()->add('orders', Kohana::dump(json_encode($res)));
  569. //return $e;
  570. // $res = json_encode($res);
  571. // $res = mb_convert_encoding($res, 'cp1251', 'utf-8');
  572. return $return;
  573. }
  574. public function getCount()
  575. {
  576. Kohana_Log::instance()->add('soap запрос количества товаров', '1');
  577. $priceCategoryId = 1;
  578. $prices = DB::select( array('sp.good_id', 'good_id'), array('sp.price', 'price') )
  579. ->from( array('shop_prices', 'sp') )
  580. ->cached(240)
  581. ->execute()
  582. ->as_array('good_id', 'price')
  583. ;
  584. $result = DB::select( array('sg.id','id'), array('sg.quantity', 'quantity') )
  585. ->from( array('shop_goods', 'sg') )
  586. ->where('status','!=','unverified')
  587. ->where('status','!=','out_of_store')
  588. ->cached(240)
  589. ->execute()
  590. ;
  591. if( $result->count() == 0 ) return array();
  592. $return = array();
  593. foreach( $result->as_array() as $item )
  594. {
  595. $return[$item['id']] = array('quantity'=>$item['quantity'],'price'=> Arr::get($prices, $item['id'], "-1") );
  596. }
  597. return json_encode($return);
  598. }
  599. }
  600. ?>