ProductService.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. <?php
  2. namespace App\Services;
  3. use App\Models\Color;
  4. use App\Models\Product;
  5. use App\Models\User;
  6. use App\Utility\ProductUtility;
  7. use Combinations;
  8. use Illuminate\Support\Str;
  9. class ProductService
  10. {
  11. public function store(array $data)
  12. {
  13. $collection = collect($data);
  14. $approved = 1;
  15. if (auth()->user()->user_type == 'seller') {
  16. $user_id = auth()->user()->id;
  17. if (get_setting('product_approve_by_admin') == 1) {
  18. $approved = 0;
  19. }
  20. } else {
  21. $user_id = User::where('user_type', 'admin')->first()->id;
  22. }
  23. $tags = array();
  24. if ($collection['tags'][0] != null) {
  25. foreach (json_decode($collection['tags'][0]) as $key => $tag) {
  26. array_push($tags, $tag->value);
  27. }
  28. }
  29. $collection['tags'] = implode(',', $tags);
  30. $discount_start_date = null;
  31. $discount_end_date = null;
  32. if ($collection['date_range'] != null) {
  33. $date_var = explode(" to ", $collection['date_range']);
  34. $discount_start_date = strtotime($date_var[0]);
  35. $discount_end_date = strtotime($date_var[1]);
  36. }
  37. unset($collection['date_range']);
  38. if ($collection['meta_title'] == null) {
  39. $collection['meta_title'] = $collection['name'];
  40. }
  41. if ($collection['meta_description'] == null) {
  42. $collection['meta_description'] = strip_tags($collection['description']);
  43. }
  44. if ($collection['meta_img'] == null) {
  45. $collection['meta_img'] = $collection['thumbnail_img'];
  46. }
  47. $shipping_cost = 0;
  48. if (isset($collection['shipping_type'])) {
  49. if ($collection['shipping_type'] == 'free') {
  50. $shipping_cost = 0;
  51. } elseif ($collection['shipping_type'] == 'flat_rate') {
  52. $shipping_cost = $collection['flat_shipping_cost'];
  53. }
  54. }
  55. unset($collection['flat_shipping_cost']);
  56. $slug = Str::slug($collection['name']);
  57. $same_slug_count = Product::where('slug', 'LIKE', $slug . '%')->count();
  58. $slug_suffix = $same_slug_count ? '-' . $same_slug_count + 1 : '';
  59. $slug .= $slug_suffix;
  60. $colors = json_encode(array());
  61. if (
  62. isset($collection['colors_active']) &&
  63. $collection['colors_active'] &&
  64. $collection['colors'] &&
  65. count($collection['colors']) > 0
  66. ) {
  67. $colors = json_encode($collection['colors']);
  68. }
  69. $options = ProductUtility::get_attribute_options($collection);
  70. $combinations = Combinations::makeCombinations($options);
  71. if (count($combinations[0]) > 0) {
  72. foreach ($combinations as $key => $combination) {
  73. $str = ProductUtility::get_combination_string($combination, $collection);
  74. unset($collection['price_' . str_replace('.', '_', $str)]);
  75. unset($collection['sku_' . str_replace('.', '_', $str)]);
  76. unset($collection['qty_' . str_replace('.', '_', $str)]);
  77. unset($collection['img_' . str_replace('.', '_', $str)]);
  78. }
  79. }
  80. unset($collection['colors_active']);
  81. $choice_options = array();
  82. if (isset($collection['choice_no']) && $collection['choice_no']) {
  83. $str = '';
  84. $item = array();
  85. foreach ($collection['choice_no'] as $key => $no) {
  86. $str = 'choice_options_' . $no;
  87. $item['attribute_id'] = $no;
  88. $attribute_data = array();
  89. // foreach (json_decode($request[$str][0]) as $key => $eachValue) {
  90. foreach ($collection[$str] as $key => $eachValue) {
  91. // array_push($data, $eachValue->value);
  92. array_push($attribute_data, $eachValue);
  93. }
  94. unset($collection[$str]);
  95. $item['values'] = $attribute_data;
  96. array_push($choice_options, $item);
  97. }
  98. }
  99. $choice_options = json_encode($choice_options, JSON_UNESCAPED_UNICODE);
  100. if (isset($collection['choice_no']) && $collection['choice_no']) {
  101. $attributes = json_encode($collection['choice_no']);
  102. unset($collection['choice_no']);
  103. } else {
  104. $attributes = json_encode(array());
  105. }
  106. $published = 1;
  107. if ($collection['button'] == 'unpublish' || $collection['button'] == 'draft') {
  108. $published = 0;
  109. }
  110. unset($collection['button']);
  111. $data = $collection->merge(compact(
  112. 'user_id',
  113. 'approved',
  114. 'discount_start_date',
  115. 'discount_end_date',
  116. 'shipping_cost',
  117. 'slug',
  118. 'colors',
  119. 'choice_options',
  120. 'attributes',
  121. 'published'
  122. ))->toArray();
  123. return Product::create($data);
  124. }
  125. public function update(array $data, Product $product)
  126. {
  127. $collection = collect($data);
  128. $slug = Str::slug($collection['name']);
  129. $slug = $collection['slug'] ? Str::slug($collection['slug']) : Str::slug($collection['name']);
  130. $same_slug_count = Product::where('slug', 'LIKE', $slug . '%')->count();
  131. $slug_suffix = $same_slug_count > 1 ? '-' . $same_slug_count + 1 : '';
  132. $slug .= $slug_suffix;
  133. if(addon_is_activated('refund_request') && !isset($collection['refundable'])){
  134. $collection['refundable'] = 0;
  135. }
  136. if(!isset($collection['is_quantity_multiplied'])){
  137. $collection['is_quantity_multiplied'] = 0;
  138. }
  139. if(!isset($collection['cash_on_delivery'])){
  140. $collection['cash_on_delivery'] = 0;
  141. }
  142. if(!isset($collection['featured'])){
  143. $collection['featured'] = 0;
  144. }
  145. if(!isset($collection['todays_deal'])){
  146. $collection['todays_deal'] = 0;
  147. }
  148. if ($collection['lang'] != env("DEFAULT_LANGUAGE")) {
  149. unset($collection['name']);
  150. unset($collection['unit']);
  151. unset($collection['description']);
  152. }
  153. unset($collection['lang']);
  154. $tags = array();
  155. if ($collection['tags'][0] != null) {
  156. foreach (json_decode($collection['tags'][0]) as $key => $tag) {
  157. array_push($tags, $tag->value);
  158. }
  159. }
  160. $collection['tags'] = implode(',', $tags);
  161. $discount_start_date = null;
  162. $discount_end_date = null;
  163. if ($collection['date_range'] != null) {
  164. $date_var = explode(" to ", $collection['date_range']);
  165. $discount_start_date = strtotime($date_var[0]);
  166. $discount_end_date = strtotime($date_var[1]);
  167. }
  168. unset($collection['date_range']);
  169. if ($collection['meta_title'] == null) {
  170. $collection['meta_title'] = $collection['name'];
  171. }
  172. if ($collection['meta_description'] == null) {
  173. $collection['meta_description'] = strip_tags($collection['description']??'');
  174. }
  175. if ($collection['meta_img'] == null) {
  176. $collection['meta_img'] = $collection['thumbnail_img'];
  177. }
  178. $shipping_cost = 0;
  179. if (isset($collection['shipping_type'])) {
  180. if ($collection['shipping_type'] == 'free') {
  181. $shipping_cost = 0;
  182. } elseif ($collection['shipping_type'] == 'flat_rate') {
  183. $shipping_cost = $collection['flat_shipping_cost'];
  184. }
  185. }
  186. unset($collection['flat_shipping_cost']);
  187. $colors = json_encode(array());
  188. if (
  189. isset($collection['colors_active']) &&
  190. $collection['colors_active'] &&
  191. $collection['colors'] &&
  192. count($collection['colors']) > 0
  193. ) {
  194. $colors = json_encode($collection['colors']);
  195. }
  196. $options = ProductUtility::get_attribute_options($collection);
  197. $combinations = Combinations::makeCombinations($options);
  198. if (count($combinations[0]) > 0) {
  199. foreach ($combinations as $key => $combination) {
  200. $str = ProductUtility::get_combination_string($combination, $collection);
  201. unset($collection['price_' . str_replace('.', '_', $str)]);
  202. unset($collection['sku_' . str_replace('.', '_', $str)]);
  203. unset($collection['qty_' . str_replace('.', '_', $str)]);
  204. unset($collection['img_' . str_replace('.', '_', $str)]);
  205. }
  206. }
  207. unset($collection['colors_active']);
  208. $choice_options = array();
  209. if (isset($collection['choice_no']) && $collection['choice_no']) {
  210. $str = '';
  211. $item = array();
  212. foreach ($collection['choice_no'] as $key => $no) {
  213. $str = 'choice_options_' . $no;
  214. $item['attribute_id'] = $no;
  215. $attribute_data = array();
  216. // foreach (json_decode($request[$str][0]) as $key => $eachValue) {
  217. foreach ($collection[$str] as $key => $eachValue) {
  218. // array_push($data, $eachValue->value);
  219. array_push($attribute_data, $eachValue);
  220. }
  221. unset($collection[$str]);
  222. $item['values'] = $attribute_data;
  223. array_push($choice_options, $item);
  224. }
  225. }
  226. $choice_options = json_encode($choice_options, JSON_UNESCAPED_UNICODE);
  227. if (isset($collection['choice_no']) && $collection['choice_no']) {
  228. $attributes = json_encode($collection['choice_no']);
  229. unset($collection['choice_no']);
  230. } else {
  231. $attributes = json_encode(array());
  232. }
  233. unset($collection['button']);
  234. $data = $collection->merge(compact(
  235. 'discount_start_date',
  236. 'discount_end_date',
  237. 'shipping_cost',
  238. 'slug',
  239. 'colors',
  240. 'choice_options',
  241. 'attributes',
  242. ))->toArray();
  243. $product->update($data);
  244. return $product;
  245. }
  246. }