noblemfd
1 week ago

How to ensure that order price is not less than product price

Posted 1 week ago by noblemfd

I am working on dynamic input field using Laravel-5.8. The project is based on Sales Order.

Model

class Product extends Model
{
  protected $table = 'products';
  protected $fillable = [
          'name',
          'rate',
          'vat',
      ];
}

class Order extends Model
{
  protected $table = 'orders';
  protected $fillable = [
          'date',
          'customer_id',
          'workOrder_id',
          'details',
          'product_id',
          'rate',
          'vat',
          'quantity',
          'remaining',
      'amount',
      ];

  public function customer(){
    return $this->belongsTo(Customer::class, 'customer_id');
  }

  public function product(){
      return $this->belongsTo(Product::class, 'product_id');
  }
}

product_id in products is a foreign key to orders

Controller

public function create()
{
    $order = null;
    $products = Product::all('id','name');
    $customers = Customer::all('id','name');
    return view('pages.order.create', compact('order','products','customers'));
}

public function store(Request $request)
{
    $request->validate([
        'date'      => 'required',
        'customer_id'  => ['required', Rule::notIn(['','0'])],
        'workOrder_id'     => 'required|unique:orders',
    ]);

    for ($i=0; $i < count($request->product_id) ; $i++) { 

        $order =  new Order;

        $order->date = $request->date;
        $order->customer_id = $request->customer_id;
        $order->workOrder_id = $request->workOrder_id;
        $order->details = $request->details;
        $order->product_id = $request->product_id[$i];
        $order->rate = $request->rate[$i];
        $order->vat = $request->vat[$i];
        $order->quantity = $request->quantity[$i];
        $order->remaining = $request->quantity[$i];
        $order->amount = $request->quantity[$i] * $request->rate[$i];

        $order->save();
    }

    return redirect()
                ->route('order.index')
                ->with('success', 'Added Successfully');
}

In an order, a customer can have several products

view

<section class="content">
    <div class="container-fluid">
        <div class="card card-warning">
            <div class="card-body">
                <form method="POST" action="{{ route('order.store') }}">
                    @csrf
                    {{-- Date --}}
                    <div class="form-group">
                        <label for="date">
                            Date
                        </label>

                        <input type="text" class="form-control{{ $errors->has('date') ? ' is-invalid' : '' }}" name="date" id="date" value="{{ old('date', optional($order)->date) }}">

                        @if ($errors->has('date'))
                        <span class="invalid-feedback">
                            <strong>{{ $errors->first('date') }}</strong>
                        </span>
                        @endif
                    </div>

                    {{-- customer --}}
                    <div class="form-group">
                        <label for="customer_id">Customer</label>
                        <select name="customer_id" class="form-control{{ $errors->has('customer_id') ? ' is-invalid' : '' }}" id="customer_id">
                            <option value="">Select</option>
                            @foreach($customers as $customer)
                            <option value="{{ $customer->id }}" {{old('customer_id', optional($order)->customer_id) == $customer->id ? 'selected':''}}>{{ $customer->name }}</option>
                            @endforeach
                        </select>
                        @if ($errors->has('customer_id'))
                        <span class="invalid-feedback">
                            <strong>{{ $errors->first('customer_id') }}</strong>
                        </span>
                        @endif
                    </div>

                    <div class="row">
                        <div class="col-md-1">
                            <button id="add_more" class="btn btn-info mt-4"><i class="fa fa-plus" title="Add More Product"></i></button>
                        </div>
                        <div class="col-md-11">
                            <div id="more_product">
                                <div class="row">
                                    <div class="col-md-3">
                                        {{-- Product --}}
                                        <div class="form-group">
                                            <label for="product_id">Select Product</label>
                                            <select name="product_id[]" class="form-control" id="product_id" required="required">
                                                <option value="">Select Product</option>
                                                @forelse($products as $product)
                                                <option value="{{ $product->id }}" >
                                                    {{ $product->name }}
                                                </option>
                                                @empty
                                                <option value="">No Product Found</option>
                                                @endforelse
                                            </select>
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        {{-- Rate --}}
                                        <div class="form-group">
                                            <label for="rate">
                                                Rate
                                            </label>
                                            <input type="number" min="0" step="any" class="form-control" name="rate[]" id="rate" required="required">
                                        </div>
                                    </div>

                                    <div class="col-md-3">
                                        {{-- Quantity --}}
                                        <div class="form-group">
                                            <label for="vat">
                                                Vat
                                            </label>

                                            <input type="number" min="0" step="any" class="form-control" name="vat[]" id="vat" required="required">
                                        </div>
                                    </div>
                                    <div class="col-md-3">
                                        {{-- Quantity --}}
                                        <div class="form-group">
                                            <label for="quantity">
                                                Quantity
                                            </label>

                                            <input type="number" min="0" class="form-control" name="quantity[]" id="quantity" required="required">
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>
                    </div>

                    {{-- workOrder_id --}}
                    <div class="form-group">
                        <label for="workOrder_id">Work Order No</label>
                        <input type="text" class="form-control{{ $errors->has('workOrder_id') ? ' is-invalid' : '' }}" name="workOrder_id" id="workOrder_id" value="{{ old('workOrder_id', optional($order)->workOrder_id) }}">

                        @if ($errors->has('workOrder_id'))
                        <span class="invalid-feedback">
                            <strong>{{ $errors->first('workOrder_id') }}</strong>
                        </span>
                        @endif
                    </div>

                    {{-- Payment Type --}}

            {{-- <div class="form-group">
                <label for="payment">Payment</label>
                <select name="payment" class="form-control{{ $errors->has('payment') ? ' is-invalid' : '' }}" id="payment">
                    <option value="">Select Payment</option>
                    <option value="1">Cash</option>
                    <option value="2">Due</option>
                </select>
                @if ($errors->has('payment'))
                <span class="invalid-feedback">
                    <strong>{{ $errors->first('payment') }}</strong>
                </span>
                @endif
            </div> --}}


            {{-- Details --}}
            <div class="form-group">
                <label for="details">
                    Remarks
                </label>

                <textarea name="details" class="form-control {{ $errors->has('details') ? ' is-invalid' : '' }}" id="details" cols="30" rows="5">{{ old('details', optional($order)->details) }}</textarea>

                @if( $errors->has('details'))
                <span class="invalid-feedback">
                    <strong>{{ $errors->first('details') }}</strong>
                </span>
                @endif
            </div>

            {{-- Save --}}
            <div class="form-group row mb-0">
                <div class="col-md-12">
                    <button type="submit" class="btn btn-primary">
                        {{ __('Save') }}
                    </button>
                </div>
            </div>
        </form>
    </div>
</div>
</div>
</section>

Javascript

<script>
{{-- jquery datepicker --}}
$( function() {
    $( "#date" ).datepicker({
        dateFormat: 'yy-mm-dd',
        changeMonth: true,
        changeYear: true,
    });
});


$('#customer_id').select2({
    placeholder: 'Select Customer',

    ajax: {
        url: '{!!URL::route('customer-autocomplete-search')!!}',
        dataType: 'json',
        delay: 250,
        processResults: function (data) {
            return {
                results: data
            };
        },
        cache: true
    },
    theme: "bootstrap"
});

$('#product_id').select2({
    placeholder: 'Select Product',

    ajax: {
        url: '{!!URL::route('product-autocomplete-search')!!}',
        dataType: 'json',
        delay: 250,
        processResults: function (data) {
            return {
                results: data
            };
        },
        cache: true
    },
    theme: "bootstrap"
});

$(document).ready(function() {
    var max_fields      = 150;
    var wrapper         = $("#more_product");
    var add_button      = $("#add_more");

    var x = 1;
    $(add_button).click(function(e){
        e.preventDefault();
        if(x < max_fields){
            x++;
            $(wrapper).append('<div class="row"><div class="col-md-3">{{-- Product --}}<div class="form-group"><label for="product_id">Select Product</label><select name="product_id[]" class="form-control product_id" id="product_id" required="required"><option value="">Select Product</option>@forelse($products as $product)<option value="{{ $product->id }}" >{{ $product->name }}</option>@empty<option value="">No Product Found</option>@endforelse </select></div></div><div class="col-md-3">{{-- Rate --}}<div class="form-group"><label for="rate">Rate</label><input type="number" min="0" step="any" class="form-control" name="rate[]" id="rate" required="required"></div></div><div class="col-md-2">{{-- vat --}}<div class="form-group"><label for="vat">Vat</label><input type="number" min="0" step="any" class="form-control" name="vat[]" id="vat" required="required"></div></div><div class="col-md-3">{{-- Quantity --}}<div class="form-group"><label for="quantity">Quantity</label><input type="number" min="0" class="form-control" name="quantity[]" id="quantity" required="required"></div></div><div class="col-sm-1"><a href="#" class="remove_field"><button style="margin-top: 30px;" class="btn btn-info"><i class="fa fa-minus" title="Remove Item"></i></button></a></div></div>');

            $('.product_id').select2({
                placeholder: 'Select Product',

                ajax: {
                    url: '{!!URL::route('product-autocomplete-search')!!}',
                    dataType: 'json',
                    delay: 250,
                    processResults: function (data) {
                        return {
                            results: data
                        };
                    },
                    cache: true
                },
                theme: "bootstrap"
            });
        }
    });

    $(wrapper).on("click",".remove_field", function(e){
        e.preventDefault(); 
        $(this).parent().parent('div').remove(); 
        x--;
    })
});
</script>

I have been on this for days. What I want to achieve is that as the User enter the rate on order on a particular product, the system should compare it with the rate in products.

If the rate on order is less than that in product, the system should display an error message to the user explaining the problem.

How do I achieve this?

Thank you

Please sign in or create an account to participate in this conversation.