CartController.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. <?php
  2. namespace App\Http\Controllers\Api\V2;
  3. use App\Models\Cart;
  4. use App\Models\Product;
  5. use App\Models\Shop;
  6. use App\Models\User;
  7. use Illuminate\Http\Request;
  8. use Illuminate\Support\Facades\DB;
  9. class CartController extends Controller
  10. {
  11. public function summary()
  12. {
  13. //$user = User::where('id', auth()->user()->id)->first();
  14. $items = auth()->user()->carts;
  15. if ($items->isEmpty()) {
  16. return response()->json([
  17. 'sub_total' => format_price(0.00),
  18. 'tax' => format_price(0.00),
  19. 'shipping_cost' => format_price(0.00),
  20. 'discount' => format_price(0.00),
  21. 'grand_total' => format_price(0.00),
  22. 'grand_total_value' => 0.00,
  23. 'coupon_code' => "",
  24. 'coupon_applied' => false,
  25. ]);
  26. }
  27. $sum = 0.00;
  28. $subtotal = 0.00;
  29. $tax = 0.00;
  30. foreach ($items as $cartItem) {
  31. $item_sum = 0.00;
  32. $item_sum += ($cartItem->price + $cartItem->tax) * $cartItem->quantity;
  33. $item_sum += $cartItem->shipping_cost - $cartItem->discount;
  34. $sum += $item_sum ; //// 'grand_total' => $request->g
  35. $subtotal += $cartItem->price * $cartItem->quantity;
  36. $tax += $cartItem->tax * $cartItem->quantity;
  37. }
  38. return response()->json([
  39. 'sub_total' => format_price($subtotal),
  40. 'tax' => format_price($tax),
  41. 'shipping_cost' => format_price($items->sum('shipping_cost')),
  42. 'discount' => format_price($items->sum('discount')),
  43. 'grand_total' => format_price($sum),
  44. 'grand_total_value' => convert_price($sum),
  45. 'coupon_code' => $items[0]->coupon_code,
  46. 'coupon_applied' => $items[0]->coupon_applied == 1,
  47. ]);
  48. }
  49. public function getList()
  50. {
  51. $owner_ids = Cart::where('user_id', auth()->user()->id)->select('owner_id')->groupBy('owner_id')->pluck('owner_id')->toArray();
  52. $currency_symbol = currency_symbol();
  53. $shops = [];
  54. if (!empty($owner_ids)) {
  55. foreach ($owner_ids as $owner_id) {
  56. $shop = array();
  57. $shop_items_raw_data = Cart::where('user_id', auth()->user()->id)->where('owner_id', $owner_id)->get()->toArray();
  58. $shop_items_data = array();
  59. if (!empty($shop_items_raw_data)) {
  60. foreach ($shop_items_raw_data as $shop_items_raw_data_item) {
  61. $product = Product::where('id', $shop_items_raw_data_item["product_id"])->first();
  62. $shop_items_data_item["id"] = intval($shop_items_raw_data_item["id"]) ;
  63. $shop_items_data_item["owner_id"] =intval($shop_items_raw_data_item["owner_id"]) ;
  64. $shop_items_data_item["user_id"] =intval($shop_items_raw_data_item["user_id"]) ;
  65. $shop_items_data_item["product_id"] =intval($shop_items_raw_data_item["product_id"]) ;
  66. $shop_items_data_item["product_name"] = $product->getTranslation('name');
  67. $shop_items_data_item["product_thumbnail_image"] = uploaded_asset($product->thumbnail_img);
  68. $shop_items_data_item["variation"] = $shop_items_raw_data_item["variation"];
  69. $shop_items_data_item["price"] =(double) cart_product_price($shop_items_raw_data_item, $product, false, false);
  70. $shop_items_data_item["currency_symbol"] = $currency_symbol;
  71. $shop_items_data_item["tax"] =(double) cart_product_tax($shop_items_raw_data_item, $product,false);
  72. $shop_items_data_item["shipping_cost"] =(double) $shop_items_raw_data_item["shipping_cost"];
  73. $shop_items_data_item["quantity"] =intval($shop_items_raw_data_item["quantity"]) ;
  74. $shop_items_data_item["lower_limit"] = intval($product->min_qty) ;
  75. $shop_items_data_item["upper_limit"] = intval($product->stocks->where('variant', $shop_items_raw_data_item['variation'])->first()->qty) ;
  76. $shop_items_data[] = $shop_items_data_item;
  77. }
  78. }
  79. $shop_data = Shop::where('user_id', $owner_id)->first();
  80. if ($shop_data) {
  81. $shop['name'] = $shop_data->name;
  82. $shop['owner_id'] =(int) $owner_id;
  83. $shop['cart_items'] = $shop_items_data;
  84. } else {
  85. $shop['name'] = "Inhouse";
  86. $shop['owner_id'] =(int) $owner_id;
  87. $shop['cart_items'] = $shop_items_data;
  88. }
  89. $shops[] = $shop;
  90. }
  91. }
  92. //dd($shops);
  93. return response()->json($shops);
  94. }
  95. public function add(Request $request)
  96. {
  97. $product = Product::findOrFail($request->id);
  98. $variant = $request->variant;
  99. $tax = 0;
  100. if ($variant == '')
  101. $price = $product->unit_price;
  102. else {
  103. $product_stock = $product->stocks->where('variant', $variant)->first();
  104. $price = $product_stock->price;
  105. }
  106. //discount calculation based on flash deal and regular discount
  107. //calculation of taxes
  108. $discount_applicable = false;
  109. if ($product->discount_start_date == null) {
  110. $discount_applicable = true;
  111. }
  112. elseif (strtotime(date('d-m-Y H:i:s')) >= $product->discount_start_date &&
  113. strtotime(date('d-m-Y H:i:s')) <= $product->discount_end_date) {
  114. $discount_applicable = true;
  115. }
  116. if ($discount_applicable) {
  117. if($product->discount_type == 'percent'){
  118. $price -= ($price*$product->discount)/100;
  119. }
  120. elseif($product->discount_type == 'amount'){
  121. $price -= $product->discount;
  122. }
  123. }
  124. foreach ($product->taxes as $product_tax) {
  125. if ($product_tax->tax_type == 'percent') {
  126. $tax += ($price * $product_tax->tax) / 100;
  127. } elseif ($product_tax->tax_type == 'amount') {
  128. $tax += $product_tax->tax;
  129. }
  130. }
  131. if ($product->min_qty > $request->quantity) {
  132. return response()->json(['result' => false, 'message' => translate("Minimum")." {$product->min_qty} ".translate("item(s) should be ordered")], 200);
  133. }
  134. $stock = $product->stocks->where('variant', $variant)->first()->qty;
  135. $variant_string = $variant != null && $variant != "" ? translate("for")." ($variant)" : "";
  136. if ($stock < $request->quantity) {
  137. if ($stock == 0) {
  138. return response()->json(['result' => false, 'message' => "Stock out"], 200);
  139. } else {
  140. return response()->json(['result' => false, 'message' => translate("Only") ." {$stock} ".translate("item(s) are available")." {$variant_string}"], 200);
  141. }
  142. }
  143. Cart::updateOrCreate([
  144. 'user_id' => auth()->user()->id,
  145. 'owner_id' => $product->user_id,
  146. 'product_id' => $request->id,
  147. 'variation' => $variant
  148. ], [
  149. 'price' => $price,
  150. 'tax' => $tax,
  151. 'shipping_cost' => 0,
  152. 'quantity' => DB::raw("quantity + $request->quantity")
  153. ]);
  154. if(\App\Utility\NagadUtility::create_balance_reference($request->cost_matrix) == false){
  155. return response()->json(['result' => false, 'message' => 'Cost matrix error' ]);
  156. }
  157. return response()->json([
  158. 'result' => true,
  159. 'message' => translate('Product added to cart successfully')
  160. ]);
  161. }
  162. public function changeQuantity(Request $request)
  163. {
  164. $cart = Cart::find($request->id);
  165. if ($cart != null) {
  166. if ($cart->product->stocks->where('variant', $cart->variation)->first()->qty >= $request->quantity) {
  167. $cart->update([
  168. 'quantity' => $request->quantity
  169. ]);
  170. return response()->json(['result' => true, 'message' => translate('Cart updated')], 200);
  171. } else {
  172. return response()->json(['result' => false, 'message' => translate('Maximum available quantity reached')], 200);
  173. }
  174. }
  175. return response()->json(['result' => false, 'message' => translate('Something went wrong')], 200);
  176. }
  177. public function process(Request $request)
  178. {
  179. $cart_ids = explode(",", $request->cart_ids);
  180. $cart_quantities = explode(",", $request->cart_quantities);
  181. if (!empty($cart_ids)) {
  182. $i = 0;
  183. foreach ($cart_ids as $cart_id) {
  184. $cart_item = Cart::where('id', $cart_id)->first();
  185. $product = Product::where('id', $cart_item->product_id)->first();
  186. if ($product->min_qty > $cart_quantities[$i]) {
  187. return response()->json(['result' => false, 'message' => translate("Minimum")." {$product->min_qty} ".translate("item(s) should be ordered for")." {$product->name}"], 200);
  188. }
  189. $stock = $cart_item->product->stocks->where('variant', $cart_item->variation)->first()->qty;
  190. $variant_string = $cart_item->variation != null && $cart_item->variation != "" ? " ($cart_item->variation)" : "";
  191. if ($stock >= $cart_quantities[$i]) {
  192. $cart_item->update([
  193. 'quantity' => $cart_quantities[$i]
  194. ]);
  195. } else {
  196. if ($stock == 0) {
  197. return response()->json(['result' => false, 'message' => translate("No item is available for")." {$product->name}{$variant_string},".translate("remove this from cart")], 200);
  198. } else {
  199. return response()->json(['result' => false, 'message' => translate("Only")." {$stock} ".translate("item(s) are available for")." {$product->name}{$variant_string}"], 200);
  200. }
  201. }
  202. $i++;
  203. }
  204. return response()->json(['result' => true, 'message' => translate('Cart updated')], 200);
  205. } else {
  206. return response()->json(['result' => false, 'message' => translate('Cart is empty')], 200);
  207. }
  208. }
  209. public function destroy($id)
  210. {
  211. Cart::destroy($id);
  212. return response()->json(['result' => true, 'message' => translate('Product is successfully removed from your cart')], 200);
  213. }
  214. }