Advertisement
Guest User

Untitled

a guest
Nov 21st, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.65 KB | None | 0 0
  1. <?php
  2.  
  3. namespace App\Jobs;
  4.  
  5. use App\Entities\ProductImages;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Queue\SerializesModels;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Contracts\Queue\ShouldQueue;
  10. use Illuminate\Foundation\Bus\Dispatchable;
  11. use App\Entities\Product;
  12.  
  13. class SendProductsToShopJob implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16.  
  17. protected $username;
  18.  
  19. protected $password;
  20.  
  21. protected $url;
  22.  
  23. /**
  24. * Create a new job instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. $this->username = config('integration.shop.username');
  31. $this->password = config('integration.shop.password');
  32. $this->url = config('integration.shop.url');
  33.  
  34. }
  35.  
  36. /**
  37. * Execute the job.
  38. *
  39. * @return void
  40. */
  41. public function handle()
  42. {
  43. try {
  44. $client = \DreamCommerce\ShopAppstoreLib\Client::factory(
  45. \DreamCommerce\ShopAppstoreLib\Client::ADAPTER_BASIC_AUTH,
  46. array(
  47. 'entrypoint' => $this->url,
  48. 'username' => $this->username,
  49. 'password' => $this->password
  50. )
  51. );
  52.  
  53. $resource = new \DreamCommerce\ShopAppstoreLib\Resource\Product($client);
  54. $products = $this->getProducts();
  55. $i = 1;
  56. foreach ($products as $product) {
  57. if ($product->shop_product_id !== null) {
  58. $item = $resource->get($product->shop_product_id);
  59. $i++;
  60. var_dump($i . ' :' . $product->shop_product_id);
  61. continue;
  62. }
  63. if (isset($item)) {
  64. if (isset($item->special_offer)) {
  65. var_dump($i . ' :' . $product->shop_product_id);
  66. continue;
  67. }
  68. if (isset($item->stock)) {
  69. if ($item->stock->price < $product->price_standard) {
  70. $data = array(
  71. 'category_id' => 350,
  72. 'producer_id' => 35,
  73. 'translations' => array(
  74. 'pl_PL' => array(
  75. 'name' => $product->name,
  76. 'description' => $product->description,
  77. 'short_description' => $product->description,
  78. 'active' => true
  79. ),
  80. ),
  81. 'stock' => array(
  82. 'price' => $product->price_standard,
  83. 'price_wholesale' => $product->price_wholesale,
  84. 'price_special' => $product->price_wholesale2,
  85. 'warn_level' => '1',
  86. 'delivery_id' => $product->quantity > 0 ? '1' : '',
  87. 'active' => 0,
  88. 'stock' => $product->quantity
  89. ),
  90. );
  91.  
  92. $result = $resource->put($product->shop_product_id, $data);
  93. var_dump($product->shop_product_id);
  94. } else {
  95. var_dump($i . ' :' . $product->shop_product_id);
  96. continue;
  97. }
  98. } else {
  99. var_dump($i . ' :' . $product->shop_product_id);
  100. continue;
  101. }
  102. } else {
  103. if (isset($product->ean)) {
  104. $ean = substr($product->ean, 0, 13);
  105. if (strlen($ean) == 12) {
  106. $ean = 5907589890983;
  107. }
  108.  
  109. $data = array(
  110. 'category_id' => 350,
  111. 'producer_id' => 35,
  112. 'translations' => array(
  113. 'pl_PL' => array(
  114. 'name' => $product->name,
  115. 'description' => $product->description,
  116. 'short_description' => $product->description,
  117. 'active' => true
  118. )
  119. ),
  120. 'stock' => array(
  121. 'price' => $product->price_standard,
  122. 'price_wholesale' => $product->price_wholesale,
  123. 'price_special' => $product->price_wholesale2,
  124. 'warn_level' => '1',
  125. 'delivery_id' => $product->quantity > 0 ? '1' : '',
  126. 'active' => 0,
  127. 'stock' => $product->quantity
  128. ),
  129. 'tax_id' => 1,
  130. 'code' => $product->sku,
  131. 'additional_producer' => $product->sku,
  132. 'ean' => $ean != null ? $ean : '5907589890983',
  133. 'unit_id' => 1,
  134.  
  135. );
  136. var_dump($ean);
  137. $result = $resource->post($data);
  138.  
  139. Product::where('sku', $product->sku)->update([
  140. 'shop_product_id' => $result,
  141. 'ean' => $ean
  142. ]);
  143.  
  144. // $resources = new \DreamCommerce\ShopAppstoreLib\Resource\ProductImage($client);
  145. // $productImages = ProductImages::where('product_id', $product->id)->get();
  146. // $data = array(
  147. // 'product_id' => $result,
  148. // 'file' => $productImages->first->id->filename,
  149. // 'url' => $productImages->first->id->url,
  150. // );
  151. // $status = $resources->post($data);
  152. }
  153. }
  154. }
  155.  
  156.  
  157. } catch (\DreamCommerce\ShopAppstoreLib\Exception\Exception $ex) {
  158. die($ex->getMessage());
  159. }
  160. }
  161.  
  162. public
  163. function getProducts()
  164. {
  165. $products = Product::all()->where('wholesaler_id', '=', '1');
  166.  
  167. return $products;
  168. }
  169. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement