CouponRequest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. <?php
  2. namespace App\Http\Requests;
  3. use Illuminate\Foundation\Http\FormRequest;
  4. use Illuminate\Validation\Rule;
  5. class CouponRequest extends FormRequest
  6. {
  7. /**
  8. * Determine if the user is authorized to make this request.
  9. *
  10. * @return bool
  11. */
  12. public function authorize()
  13. {
  14. return true;
  15. }
  16. /**
  17. * Get the validation rules that apply to the request.
  18. *
  19. * @return array
  20. */
  21. public function rules()
  22. {
  23. $productsRule = 'sometimes';
  24. $minBuyRule = 'sometimes';
  25. $maxDiscountRule = 'sometimes';
  26. if ($this->request->get('type') == 'product_base') {
  27. $productsRule = 'required';
  28. }
  29. if ($this->request->get('type') == 'cart_base') {
  30. $minBuyRule = ['required','numeric','min:1'];
  31. $maxDiscountRule = ['required','numeric','min:1'];
  32. }
  33. return [
  34. 'type' => ['required'],
  35. 'code' => ['required', Rule::unique('coupons')->ignore($this->coupon), 'max:255',],
  36. 'discount' => ['required','numeric','min:1'],
  37. 'discount_type' => ['required'],
  38. 'product_ids' => $productsRule,
  39. 'min_buy' => $minBuyRule,
  40. 'max_discount' => $maxDiscountRule,
  41. 'date_range' => ['required'],
  42. 'start_date' => ['required'],
  43. 'end_date' => ['required'],
  44. 'details' => ['required'],
  45. ];
  46. }
  47. /**
  48. * Get the error messages for the defined validation rules.
  49. *
  50. * @return array
  51. */
  52. public function messages()
  53. {
  54. return [
  55. 'type.required' => translate('Coupon type is required'),
  56. 'code.required' => translate('Coupon code is required'),
  57. 'code.unique' => translate('Coupon already exist for this coupon code'),
  58. 'code.max' => translate('Max 255 characters'),
  59. 'product_ids.required' => translate('Product is required'),
  60. 'discount.required' => translate('Discount is required'),
  61. 'discount.numeric' => translate('Discount should be numeric type'),
  62. 'discount.min' => translate('Discount should be l or greater'),
  63. 'discount_type.required'=> translate('Discount type is required'),
  64. 'min_buy.required' => translate('Minimum shopping amount is required'),
  65. 'min_buy.numeric' => translate('Minimum shopping amount should be numeric type'),
  66. 'min_buy.min' => translate('Minimum shopping amount should be l or greater'),
  67. 'max_discount.required' => translate('Max discount amount is required'),
  68. 'max_discount.numeric' => translate('Max discount amount should be numeric type'),
  69. 'max_discount.min' => translate('Max discount amount should be l or greater'),
  70. 'date_range.required' => translate('Date Range is required'),
  71. ];
  72. }
  73. protected function prepareForValidation()
  74. {
  75. $date_range = explode(" - ", $this->date_range);
  76. $coupon_details = null;
  77. if ($this->type == "product_base") {
  78. $coupon_details = array();
  79. foreach($this->product_ids as $product_id) {
  80. $data['product_id'] = $product_id;
  81. array_push($coupon_details, $data);
  82. }
  83. $coupon_details = json_encode($coupon_details);
  84. }
  85. elseif ($this->type == "cart_base") {
  86. $data = array();
  87. $data['min_buy'] = $this->min_buy;
  88. $data['max_discount'] = $this->max_discount;
  89. $coupon_details = json_encode($data);
  90. }
  91. $this->merge([
  92. 'start_date' => strtotime($date_range[0]),
  93. 'end_date' => strtotime($date_range[1]),
  94. 'details' => $coupon_details
  95. ]);
  96. }
  97. }