DemoController.php 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Order;
  4. use App\Models\ProductStock;
  5. use Illuminate\Http\Request;
  6. use DB;
  7. use Schema;
  8. use ZipArchive;
  9. use File;
  10. use Artisan;
  11. use App\Models\Upload;
  12. use App\Models\Banner;
  13. use App\Models\Brand;
  14. use App\Models\User;
  15. use App\Models\Category;
  16. use App\Models\CategoryTranslation;
  17. use App\Models\SubCategory;
  18. use App\Models\SubCategoryTranslation;
  19. use App\Models\SubSubCategory;
  20. use App\Models\SubSubCategoryTranslation;
  21. use App\Models\CustomerPackage;
  22. use App\Models\CustomerProduct;
  23. use App\Models\FlashDeal;
  24. use App\Models\Product;
  25. use App\Models\ProductTax;
  26. use App\Models\Tax;
  27. use App\Models\Shop;
  28. use App\Models\Slider;
  29. use App\HomeCategory;
  30. use App\Models\BusinessSetting;
  31. use App\Models\Translation;
  32. use App\Models\Attribute;
  33. use App\Models\AttributeValue;
  34. class DemoController extends Controller
  35. {
  36. public function __construct()
  37. {
  38. ini_set('memory_limit', '2048M');
  39. ini_set('max_execution_time', 600);
  40. }
  41. public function cron_1()
  42. {
  43. if (env('DEMO_MODE') != 'On') {
  44. return back();
  45. }
  46. $this->drop_all_tables();
  47. $this->import_demo_sql();
  48. }
  49. public function cron_2()
  50. {
  51. if (env('DEMO_MODE') != 'On') {
  52. return back();
  53. }
  54. $this->remove_folder();
  55. $this->extract_uploads();
  56. }
  57. public function drop_all_tables()
  58. {
  59. Schema::disableForeignKeyConstraints();
  60. foreach (DB::select('SHOW TABLES') as $table) {
  61. $table_array = get_object_vars($table);
  62. Schema::drop($table_array[key($table_array)]);
  63. }
  64. }
  65. public function import_demo_sql()
  66. {
  67. Artisan::call('cache:clear');
  68. $sql_path = base_path('demo.sql');
  69. DB::unprepared(file_get_contents($sql_path));
  70. }
  71. public function extract_uploads()
  72. {
  73. $zip = new ZipArchive;
  74. $zip->open(base_path('public/uploads.zip'));
  75. $zip->extractTo('public/uploads');
  76. }
  77. public function remove_folder()
  78. {
  79. File::deleteDirectory(base_path('public/uploads'));
  80. }
  81. public function migrate_attribute_values(Request $request){
  82. foreach (Product::all() as $product) {
  83. if ($product->variant_product) {
  84. try {
  85. $choice_options = json_decode($product->choice_options);
  86. foreach ($choice_options as $choice_option) {
  87. foreach ($choice_option->values as $value) {
  88. $attribute_value = AttributeValue::where('value', $value)->first();
  89. if ($attribute_value == null) {
  90. $attribute_value = new AttributeValue;
  91. $attribute_value->attribute_id = $choice_option->attribute_id;
  92. $attribute_value->value = $value;
  93. $attribute_value->save();
  94. }
  95. }
  96. }
  97. } catch (\Exception $e) {
  98. }
  99. }
  100. }
  101. }
  102. public function convertTaxes()
  103. {
  104. $tax = Tax::first();
  105. foreach (Product::all() as $product) {
  106. $product_tax = new ProductTax;
  107. $product_tax->product_id = $product->id;
  108. $product_tax->tax_id = $tax->id;
  109. $product_tax->tax = $product->tax;
  110. $product_tax->tax_type = $product->tax_type;
  111. $product_tax->save();
  112. }
  113. }
  114. public function convert_assets(Request $request)
  115. {
  116. $type = array(
  117. "jpg" => "image",
  118. "jpeg" => "image",
  119. "png" => "image",
  120. "svg" => "image",
  121. "webp" => "image",
  122. "gif" => "image",
  123. "mp4" => "video",
  124. "mpg" => "video",
  125. "mpeg" => "video",
  126. "webm" => "video",
  127. "ogg" => "video",
  128. "avi" => "video",
  129. "mov" => "video",
  130. "flv" => "video",
  131. "swf" => "video",
  132. "mkv" => "video",
  133. "wmv" => "video",
  134. "wma" => "audio",
  135. "aac" => "audio",
  136. "wav" => "audio",
  137. "mp3" => "audio",
  138. "zip" => "archive",
  139. "rar" => "archive",
  140. "7z" => "archive",
  141. "doc" => "document",
  142. "txt" => "document",
  143. "docx" => "document",
  144. "pdf" => "document",
  145. "csv" => "document",
  146. "xml" => "document",
  147. "ods" => "document",
  148. "xlr" => "document",
  149. "xls" => "document",
  150. "xlsx" => "document"
  151. );
  152. foreach (Banner::all() as $key => $banner) {
  153. if ($banner->photo != null) {
  154. $arr = explode('.', $banner->photo);
  155. $upload = Upload::create([
  156. 'file_original_name' => null, 'file_name' => $banner->photo, 'user_id' => User::where('user_type', 'admin')->first()->id, 'extension' => $arr[1],
  157. 'type' => isset($type[$arr[1]]) ? $type[$arr[1]] : "others", 'file_size' => 0
  158. ]);
  159. $banner->photo = $upload->id;
  160. $banner->save();
  161. }
  162. }
  163. foreach (Brand::all() as $key => $brand) {
  164. if ($brand->logo != null) {
  165. $arr = explode('.', $brand->logo);
  166. $upload = Upload::create([
  167. 'file_original_name' => null, 'file_name' => $brand->logo, 'user_id' => User::where('user_type', 'admin')->first()->id, 'extension' => $arr[1],
  168. 'type' => isset($type[$arr[1]]) ? $type[$arr[1]] : "others", 'file_size' => 0
  169. ]);
  170. $brand->logo = $upload->id;
  171. $brand->save();
  172. }
  173. }
  174. foreach (Category::all() as $key => $category) {
  175. if ($category->banner != null) {
  176. $arr = explode('.', $category->banner);
  177. $upload = Upload::create([
  178. 'file_original_name' => null, 'file_name' => $category->banner, 'user_id' => User::where('user_type', 'admin')->first()->id, 'extension' => $arr[1],
  179. 'type' => isset($type[$arr[1]]) ? $type[$arr[1]] : "others", 'file_size' => 0
  180. ]);
  181. $category->banner = $upload->id;
  182. $category->save();
  183. }
  184. if ($category->icon != null) {
  185. $arr = explode('.', $category->icon);
  186. $upload = Upload::create([
  187. 'file_original_name' => null, 'file_name' => $category->icon, 'user_id' => User::where('user_type', 'admin')->first()->id, 'extension' => $arr[1],
  188. 'type' => isset($type[$arr[1]]) ? $type[$arr[1]] : "others", 'file_size' => 0
  189. ]);
  190. $category->icon = $upload->id;
  191. $category->save();
  192. }
  193. }
  194. foreach (CustomerPackage::all() as $key => $package) {
  195. if ($package->logo != null) {
  196. $arr = explode('.', $package->logo);
  197. $upload = Upload::create([
  198. 'file_original_name' => null, 'file_name' => $package->logo, 'user_id' => User::where('user_type', 'admin')->first()->id, 'extension' => $arr[1],
  199. 'type' => isset($type[$arr[1]]) ? $type[$arr[1]] : "others", 'file_size' => 0
  200. ]);
  201. $package->logo = $upload->id;
  202. $package->save();
  203. }
  204. }
  205. foreach (CustomerProduct::all() as $key => $product) {
  206. if ($product->photos != null) {
  207. $files = array();
  208. foreach (json_decode($product->photos) as $key => $photo) {
  209. $arr = explode('.', $photo);
  210. $upload = Upload::create([
  211. 'file_original_name' => null, 'file_name' => $photo, 'user_id' => $product->user_id, 'extension' => $arr[1],
  212. 'type' => isset($type[$arr[1]]) ? $type[$arr[1]] : "others", 'file_size' => 0
  213. ]);
  214. array_push($files, $upload->id);
  215. }
  216. $product->photos = implode(',', $files);
  217. $product->save();
  218. }
  219. if ($product->thumbnail_img != null) {
  220. $arr = explode('.', $product->thumbnail_img);
  221. $upload = Upload::create([
  222. 'file_original_name' => null, 'file_name' => $product->thumbnail_img, 'user_id' => $product->user_id, 'extension' => $arr[1],
  223. 'type' => isset($type[$arr[1]]) ? $type[$arr[1]] : "others", 'file_size' => 0
  224. ]);
  225. $product->thumbnail_img = $upload->id;
  226. $product->save();
  227. }
  228. if ($product->meta_img != null) {
  229. $arr = explode('.', $product->meta_img);
  230. $upload = Upload::create([
  231. 'file_original_name' => null, 'file_name' => $product->meta_img, 'user_id' => $product->user_id, 'extension' => $arr[1],
  232. 'type' => isset($type[$arr[1]]) ? $type[$arr[1]] : "others", 'file_size' => 0
  233. ]);
  234. $product->meta_img = $upload->id;
  235. $product->save();
  236. }
  237. }
  238. foreach (FlashDeal::all() as $key => $flash_deal) {
  239. if ($flash_deal->banner != null) {
  240. $arr = explode('.', $flash_deal->banner);
  241. $upload = Upload::create([
  242. 'file_original_name' => null, 'file_name' => $flash_deal->banner, 'user_id' => User::where('user_type', 'admin')->first()->id, 'extension' => $arr[1],
  243. 'type' => isset($type[$arr[1]]) ? $type[$arr[1]] : "others", 'file_size' => 0
  244. ]);
  245. $flash_deal->banner = $upload->id;
  246. $flash_deal->save();
  247. }
  248. }
  249. foreach (Product::all() as $key => $product) {
  250. if ($product->photos != null) {
  251. $files = array();
  252. foreach (json_decode($product->photos) as $key => $photo) {
  253. $arr = explode('.', $photo);
  254. $upload = Upload::create([
  255. 'file_original_name' => null, 'file_name' => $photo, 'user_id' => $product->user_id, 'extension' => $arr[1],
  256. 'type' => isset($type[$arr[1]]) ? $type[$arr[1]] : "others", 'file_size' => 0
  257. ]);
  258. array_push($files, $upload->id);
  259. }
  260. $product->photos = implode(',', $files);
  261. $product->save();
  262. }
  263. if ($product->thumbnail_img != null) {
  264. $arr = explode('.', $product->thumbnail_img);
  265. $upload = Upload::create([
  266. 'file_original_name' => null, 'file_name' => $product->thumbnail_img, 'user_id' => $product->user_id, 'extension' => $arr[1],
  267. 'type' => isset($type[$arr[1]]) ? $type[$arr[1]] : "others", 'file_size' => 0
  268. ]);
  269. $product->thumbnail_img = $upload->id;
  270. $product->save();
  271. }
  272. if ($product->featured_img != null) {
  273. $arr = explode('.', $product->featured_img);
  274. $upload = Upload::create([
  275. 'file_original_name' => null, 'file_name' => $product->featured_img, 'user_id' => $product->user_id, 'extension' => $arr[1],
  276. 'type' => isset($type[$arr[1]]) ? $type[$arr[1]] : "others", 'file_size' => 0
  277. ]);
  278. $product->featured_img = $upload->id;
  279. $product->save();
  280. }
  281. if ($product->flash_deal_img != null) {
  282. $arr = explode('.', $product->flash_deal_img);
  283. $upload = Upload::create([
  284. 'file_original_name' => null, 'file_name' => $product->flash_deal_img, 'user_id' => $product->user_id, 'extension' => $arr[1],
  285. 'type' => isset($type[$arr[1]]) ? $type[$arr[1]] : "others", 'file_size' => 0
  286. ]);
  287. $product->flash_deal_img = $upload->id;
  288. $product->save();
  289. }
  290. if ($product->meta_img != null) {
  291. $arr = explode('.', $product->meta_img);
  292. $upload = Upload::create([
  293. 'file_original_name' => null, 'file_name' => $product->meta_img, 'user_id' => $product->user_id, 'extension' => $arr[1],
  294. 'type' => isset($type[$arr[1]]) ? $type[$arr[1]] : "others", 'file_size' => 0
  295. ]);
  296. $product->meta_img = $upload->id;
  297. $product->save();
  298. }
  299. }
  300. foreach (Shop::all() as $key => $shop) {
  301. if ($shop->sliders != null) {
  302. $files = array();
  303. foreach (json_decode($shop->sliders) as $key => $photo) {
  304. $arr = explode('.', $photo);
  305. $upload = Upload::create([
  306. 'file_original_name' => null, 'file_name' => $photo, 'user_id' => $shop->user_id, 'extension' => $arr[1],
  307. 'type' => isset($type[$arr[1]]) ? $type[$arr[1]] : "others", 'file_size' => 0
  308. ]);
  309. array_push($files, $upload->id);
  310. }
  311. $shop->sliders = implode(',', $files);
  312. $shop->save();
  313. }
  314. if ($shop->logo != null) {
  315. $arr = explode('.', $shop->logo);
  316. $upload = Upload::create([
  317. 'file_original_name' => null, 'file_name' => $shop->logo, 'user_id' => $shop->user_id, 'extension' => $arr[1],
  318. 'type' => isset($type[$arr[1]]) ? $type[$arr[1]] : "others", 'file_size' => 0
  319. ]);
  320. $shop->logo = $upload->id;
  321. $shop->save();
  322. }
  323. }
  324. foreach (Slider::all() as $key => $slider) {
  325. if ($slider->photo != null) {
  326. $arr = explode('.', $slider->photo);
  327. $upload = Upload::create([
  328. 'file_original_name' => null, 'file_name' => $slider->photo, 'user_id' => User::where('user_type', 'admin')->first()->id, 'extension' => $arr[1],
  329. 'type' => isset($type[$arr[1]]) ? $type[$arr[1]] : "others", 'file_size' => 0
  330. ]);
  331. $slider->photo = $upload->id;
  332. $slider->save();
  333. }
  334. }
  335. foreach (User::all() as $key => $user) {
  336. if ($user->avatar_original != null) {
  337. $arr = explode('.', $user->avatar_original);
  338. $upload = Upload::create([
  339. 'file_original_name' => null, 'file_name' => $user->avatar_original, 'user_id' => $user->id, 'extension' => $arr[1],
  340. 'type' => isset($type[$arr[1]]) ? $type[$arr[1]] : "others", 'file_size' => 0
  341. ]);
  342. $user->avatar_original = $upload->id;
  343. $user->save();
  344. }
  345. }
  346. $business_setting = BusinessSetting::where('type', 'home_slider_images')->first();
  347. $business_setting->value = json_encode(Slider::pluck('photo')->toArray());
  348. $business_setting->save();
  349. $business_setting = BusinessSetting::where('type', 'home_slider_links')->first();
  350. $business_setting->value = json_encode(Slider::pluck('link')->toArray());
  351. $business_setting->save();
  352. $business_setting = BusinessSetting::where('type', 'home_banner1_images')->first();
  353. $business_setting->value = json_encode(Banner::where('position', 1)->pluck('photo')->toArray());
  354. $business_setting->save();
  355. $business_setting = BusinessSetting::where('type', 'home_banner1_links')->first();
  356. $business_setting->value = json_encode(Banner::where('position', 1)->pluck('url')->toArray());
  357. $business_setting->save();
  358. $business_setting = BusinessSetting::where('type', 'home_banner2_images')->first();
  359. $business_setting->value = json_encode(Banner::where('position', 2)->pluck('photo')->toArray());
  360. $business_setting->save();
  361. $business_setting = BusinessSetting::where('type', 'home_banner2_links')->first();
  362. $business_setting->value = json_encode(Banner::where('position', 2)->pluck('url')->toArray());
  363. $business_setting->save();
  364. $business_setting = BusinessSetting::where('type', 'home_categories')->first();
  365. $business_setting->value = json_encode(HomeCategory::pluck('category_id')->toArray());
  366. $business_setting->save();
  367. $business_setting = BusinessSetting::where('type', 'top10_categories')->first();
  368. $business_setting->value = json_encode(Category::where('top', 1)->pluck('id')->toArray());
  369. $business_setting->save();
  370. $business_setting = BusinessSetting::where('type', 'top10_brands')->first();
  371. $business_setting->value = json_encode(Brand::where('top', 1)->pluck('id')->toArray());
  372. $business_setting->save();
  373. $code = 'en';
  374. $jsonString = [];
  375. if(File::exists(base_path('resources/lang/'.$code.'.json'))){
  376. $jsonString = file_get_contents(base_path('resources/lang/'.$code.'.json'));
  377. $jsonString = json_decode($jsonString, true);
  378. }
  379. foreach($jsonString as $key => $string){
  380. $translation_def = new Translation;
  381. $translation_def->lang = $code;
  382. $translation_def->lang_key = $key;
  383. $translation_def->lang_value = $string;
  384. $translation_def->save();
  385. }
  386. }
  387. public function convert_category()
  388. {
  389. foreach (SubCategory::all() as $key => $value) {
  390. $category = new Category;
  391. $parent = Category::find($value->category_id);
  392. $category->name = $value->name;
  393. $category->digital = $parent->digital;
  394. $category->banner = null;
  395. $category->icon = null;
  396. $category->meta_title = $value->meta_title;
  397. $category->meta_description = $value->meta_description;
  398. $category->parent_id = $parent->id;
  399. $category->level = $parent->level + 1;
  400. $category->slug = $value->slug;
  401. $category->commision_rate = $parent->commision_rate;
  402. $category->save();
  403. foreach (SubCategoryTranslation::where('sub_category_id', $value->id)->get() as $translation) {
  404. $category_translation = new CategoryTranslation;
  405. $category_translation->category_id = $category->id;
  406. $category_translation->lang = $translation->lang;
  407. $category_translation->name = $translation->name;
  408. $category_translation->save();
  409. }
  410. }
  411. foreach (SubSubCategory::all() as $key => $value) {
  412. $category = new Category;
  413. $parent = Category::find(Category::where('name', SubCategory::find($value->sub_category_id)->name)->first()->id);
  414. $category->name = $value->name;
  415. $category->digital = $parent->digital;
  416. $category->banner = null;
  417. $category->icon = null;
  418. $category->meta_title = $value->meta_title;
  419. $category->meta_description = $value->meta_description;
  420. $category->parent_id = $parent->id;
  421. $category->level = $parent->level + 1;
  422. $category->slug = $value->slug;
  423. $category->commision_rate = $parent->commision_rate;
  424. $category->save();
  425. foreach (SubSubCategoryTranslation::where('sub_sub_category_id', $value->id)->get() as $translation) {
  426. $category_translation = new CategoryTranslation;
  427. $category_translation->category_id = $category->id;
  428. $category_translation->lang = $translation->lang;
  429. $category_translation->name = $translation->name;
  430. $category_translation->save();
  431. }
  432. }
  433. foreach (Product::all() as $key => $value) {
  434. try {
  435. if ($value->subsubcategory_id == null) {
  436. $value->category_id = Category::where('name', SubCategory::find($value->subcategory_id)->name)->first()->id;
  437. $value->save();
  438. } else {
  439. $value->category_id = Category::where('name', SubSubCategory::find($value->subsubcategory_id)->name)->first()->id;
  440. $value->save();
  441. }
  442. } catch (\Exception $e) {
  443. }
  444. }
  445. foreach (CustomerProduct::all() as $key => $value) {
  446. try {
  447. if ($value->subsubcategory_id == null) {
  448. $value->category_id = Category::where('name', SubCategory::find($value->subcategory_id)->name)->first()->id;
  449. $value->save();
  450. } else {
  451. $value->category_id = Category::where('name', SubSubCategory::find($value->subsubcategory_id)->name)->first()->id;
  452. $value->save();
  453. }
  454. } catch (\Exception $e) {
  455. }
  456. }
  457. // foreach (Product::all() as $key => $product) {
  458. // if (is_array(json_decode($product->tags))) {
  459. // $tags = array();
  460. // foreach (json_decode($product->tags) as $tag) {
  461. // array_push($tags, $tag->value);
  462. // }
  463. // $product->tags = implode(',', $tags);
  464. // $product->save();
  465. // }
  466. // }
  467. }
  468. public function insert_product_variant_forcefully(Request $request)
  469. {
  470. foreach (Product::all() as $product) {
  471. if ($product->stocks->isEmpty()) {
  472. $product_stock = new ProductStock;
  473. $product_stock->product_id = $product->id;
  474. $product_stock->variant = '';
  475. $product_stock->price = $product->unit_price;
  476. $product_stock->sku = $product->sku;
  477. $product_stock->qty = $product->current_stock;
  478. $product_stock->save();
  479. }
  480. }
  481. }
  482. public function update_seller_id_in_orders($id_min, $id_max)
  483. {
  484. $orders = Order::where('id', '>=', $id_min)->where('id', '<=', $id_max)->get();
  485. foreach ($orders as $order) {
  486. $this->update_seller_id_in_order($order);
  487. }
  488. }
  489. public function update_seller_id_in_order($order)
  490. {
  491. if($order->seller_id == 0){
  492. //dd($order->orderDetails[0]->seller_id);
  493. $order->seller_id = $order->orderDetails[0]->seller_id;
  494. $order->save();
  495. }
  496. }
  497. }