I have a simple application which sum amount of my orders and payments only per same day and submit them in table. I have a drawer which starting every day and when finish the day I can submit total sum of day (orders,pay in/out) and at the next day I'm starting from begining with 0 cash again.
My question: Is that a possible to end my drawer and start it again in the same day with 0 cash multiple times per day?
Now if I start again I'm seeing all orders whose are already maked today.
My controller:
public function show($id)
{
$locations = Location::find($id)->orders()->whereDate('created_at', '=', Carbon::today()->toDateString()); //show orders
$wallets = Location::find($id)->locsales()->whereDate('created_at', '=', Carbon::today()->toDateString()); //show payments
$loc = Location::find($id); //to show location name
return view('location')
->with('locations', $locations)
->with('wallets', $wallets)
->with('loc', $loc);
}
Part of my view:
<div class="card-body">
<div class="form-group">
<div class="list-group-item">
<label for="location">Location: {{$loc->location_id}}</label>
<form method="post" action="/post_drawer">
#csrf
<input type="number" class="form-control" name="start_cash" id="start_cash" value="{{$wallets->sum('start_cash')}}" readonly>
<label for="info">Amount of orders(today):</label>
<input type="number" class="form-control" name="amount" id="amount" value="{{$locations->sum('total')}}" readonly>
Update 1: I added to orders table and to my view option to submit added_to_drawer field with boolean and now my controller is like that:
$locations = Location::find($id)->orders()->whereDate('created_at', '=', Carbon::today()->toDateString())->whereNull('added_to_drawer');
May be I'm doing something wrong but it doesn't work.
Update 2:I maked my controller to show only records with value 1 and now it's working correct.
$locations = Location::find($id)->orders()
->where('added_to_drawer', 1)
$affected = DB::table('orders')->whereid('location', $id)->update(array('added_to_drawer' => 0));
$affected = DB::table('wallets')->where('location_id', $id)->update(array('added_to_drawer' => 0));
But I have another problem. When I close my drawer he update(reset) all locations
drawer to 0.
For example - I want to update only this locations where name is New York but to be dynamically. Not like that: where('location','=','New York')
I tried it like this: where('location','=' $id) but it's doesn't work.
Related
How to take the amount from an input and add(+) to the amount table in db? (Laravel)
I have this view :
enter image description here
and i have a table named Collaborators (which has an column named istoric_proiecte_sum_suma(made with withSum() function from laravel, in relationship with another table ProjectHistory that contains details about what collaborator works in any project and how much the owner paid him. and if the collab work on multiple projects the owners has to pay him multiple times, and if that, we have multiple inserts in ProjectHistory table with sums and withSum() function makes the relation between those and add the sum and display it to me) which contains the expenses, payments from the owner of the company to a collaborator) like you see in "suma" column in first image (suma = amount). So if i have a - amount the owner of the company needs to pay the collaborator and he pay him from completing that input and press "realizeaza plata" (translated -> make the payment).
So when he press "make payment" -> realizeaza plata, i need to add (+ sign operator) the amount that i typed in "suma" input to the "suma" (amount) column in first image(which is column istoric_proiecte_sum_suma).
My question is how do i resolve this problem because i have no idea. Look what i've tried:
CollaboratorController.php
public function realizeazaPlata(Request $request, Colaboratori $colaborator, $id)
{
$colaboratori1 = Colaboratori::findOrFail($id);
$suma = $request->input('introduSuma');
dd($suma);
DB::update('UPDATE Colaboratori SET istoric_proiecte_sum_suma=? WHERE id=?', [($colaborator->istoric_proiecte_sum_suma + $suma), $colaboratori1->id]);
return back();
}
ColabsView.blade
<form action="{{ url('colaboratori') }}" method="POST">
#csrf
#method('GET')
<td>
<input type="text" name='introduSuma' placeholder="Suma">
<button type="submit" class="btn btn-primary">Realizeaza plata</button>
</td>
</form>
web.php
Route::get('colaboratori', [App\Http\Controllers\ColaboratoriController::class, 'realizeazaPlata'])->name('realizeazaPlata');
Can someone help me please. Thanks
Do you mean you need to convert negative number to positive and then SUM it?
You could use abs() function:
$suma = abs($request->input('introduSuma'));
You should use type=number, to prevent users from entering characters:
<input type="number" name='introduSuma' placeholder="Suma">
You can do it like the following:
$colaboratori1 = Colaboratori::findOrFail($id);
$suma = $request->input('introduSuma');
$colaboratori1->istoric_proiecte_sum_suma = $colaboratori1->istoric_proiecte_sum_suma + $suma;
$colaboratori1->save();
Help logically understand, I do not quite understand how you can implement this moment.
There is a form:
#foreach($weeks as $key => $value)
<li>
<label>
<input type="checkbox" value="{{$key}}" name="days[]"> {{$value}}
<div class="checkbox"></div>
</label>
<div class="time" data-day="mon">
<input type="time" name="start_time[]" class="start">
<span>-</span>
<input type="time" name="end_time[]" class="finish">
</div>
</li>
#endforeach
Images are attached to this form. My relationship is many to many.
In the request I get the following data:
images go to one table, days and times to another, intermediate between them. The problem is, I can't catch up with how to add data to the table with days. Since I get 3 arrays - days, start time and end.
I parsed the request in the controller. I add data to a table with an image.
$data = $request->validated();
$days[] = $data['days'];
$days[] = $data['start_time'];
$days[] = $data['end_time'];
unset($data['days'], $data['start_time'], $data['end_time']);
$image = Storage::disk('public')->put('/images', $data['image']);
$cover = DynamicCover::create([
'image' => $image,
'group_link' => $data['group_link'],
]);
The following data remains in the days array:
And here I am at a dead end, I don’t understand how to process them correctly, add them to the database and also fill in the link table
I will be grateful for advice. Many-to-many relationships, because there can be many images for one day, so this image will have many days
I need your help with my situation in storing data.
I got a cart with several products, and now I want to store all these products in Order table at the same time by clicking submit button
My view blade.php
This view shows all products in the cart and I need to store these information in Ordertable
<form action="{{route('order.store')}}" method="post">
#csrf
#foreach ($cart as $ct)
<input type="text" name="product_id" value="{{$ct->id}}" hidden>
<input type="text" value="{{$ct->color}}" name="color" >
<input type="text" value="{{$ct->size}}" name="size" >
<input type="text" value="{{$ct->price}}" name="price" >
#endforeach
<button type="submit">submit</button>
</form>
In my Order table, I got these 4 columns need to fill in: product_id, color, size and price.
My foreach loop gets data from Cart table and all data were shown without errors.
My question is that How I can store these data to my Order table just by click on submit button once? What I should write in my store function in OrderController?
If my cart has 3 products then my expected value is the Order table will look like this:
id----product_id----size----color---price---+
---------------------------------------------
1 1 abc xyz 123
---------------------------------------------
2 2 abc2 xyz2 456
---------------------------------------------
3 3 aaa bbb 789
Thank for your help!
DB:
Order:
user_id
created_at
...
orderProducts:
price
color
size
order_id (relation)
product_id (relation)
...
View
<form action="{{route('order.store')}}" method="post">
#csrf
#foreach ($cart as $ct)
<input type="text" name="products[]" value="{{$ct->product_id}}" hidden>
<input type="text" value="{{$ct->color}}" name="color[]" >
<input type="text" value="{{$ct->size}}" name="size[]" >
<input type="text" value="{{$ct->price}}" name="price[]" >
#endforeach
<button type="submit">submit</button>
</form>
controller function store
$order= new Order;// change the model here
// .. complete your information's like user id or created_at
$order->save();
foreach($request->products as $key=>$product_id)
{
$product = Product::findOrFail($product_id); // validations the product id
// get your information's from db (color,size,price) don't trust get information's from user .
// $product->price , $product->color .... or get from cart
//from cart direct $color[$key] or $price[$key] or $size[$key] "I don't recomend to use this point"
// must be create new table
$orderProducts=new orderProducts; // create new table ordersProducts
$orderProducts->price = $product->price;
// .. complete your information's
$orderProducts->order_id = $order->id; // primary key
$orderProducts->product_id= $product->id;
$orderProducts->save();
}
Notice:
- You need to use try-catch to record info in order if fail in the step "findOrFail" or change to find and then if product not found in the table then record order not complete and show an error to the user
My Answer is quite similar to above answer but i would just like to put a different approach here with Database Transaction, Try Catch & Bulk Insert(to prevent placing queries in loop)
try {
DB::begintransaction();
$order = Order::create($orderData); // change the model here
$orderProductsData = [];
foreach($request->products as $product_id) {
$product = Product::find($product_id); // validations the product id
$orderProductsData[] = $tmpData = [
'price' => $product->price,
'order_id' => $order->id,
'product_id' => $product->id;
];
// must be create new table
// You can also store this way, but its still in loop so putting under comment.
// $orderProducts = orderProducts::create($tmpData);
}
orderProducts::insert($orderProductsData);
DB::commit();
} catch (Exception $e) {
DB::rollback();
thow $e; // modify with, How you handle your error response.
}
Again, Above answer is totally correct, However just putting a different approach, which i believe little more optimised way.
I am working on a Laravel 4 project and I am trying to add a bunch of services to the services column in a simple service table. the columns are formatted as:
id userID services price
What I would like it to do is when a user selects the services all the services that user selects get's put into the services column to look like:
id userID services price
1 5 clean $4.95
2 5 unload $7.95
3 5 dance $1.95
4 5 vacuum $12.95
5 5 clean $4.95
basically all the services are in different input fields. So they aren't related.
I tried adding an array to the model but it gives an error:
Array to String conversion
Part of my Controller
$service = new Service();
$service->userID = $user->id;
$service->services = array(
Input::get('rooms'),
Input::get('pr_deodorizer'),
Input::get('pr_protectant') .
Input::get('pr_sanitzer'),
Input::get('fr_couch'),
Input::get('fr_chair'),
Input::get('fr_sectional'),
Input::get('fr_ottoman')
);
var_dump($service->services);die;
$service->save();
Laravel won't understand that you want to create various new instances of the model from just the array, so for each of the services, you'll have to create a separate instance of the Service model for each, eg.
$service = new Service();
$service->userID = $user->id;
$service->services = Input::get('rooms');
$service->save();
One idea.. perhaps you could group your services into a PHP array on your form using the square bracket notation:
<div>
<label for="room">Room</label>
<input type="text" id="room" name="services[]">
</div>
<div>
<label for="pr_deodorizer">Deodorizer</label>
<input type="text" id="pr_deodorizer" name="services[]">
</div>
<div>
<label for="pr_protectant">Protectant</label>
<input type="text" id="pr_protectant" name="services[]">
</div>
and then loop through the selected services in your Controller:
foreach(Input::get('services') as $service)
{
$service = new Service();
$service->userID = $user->id;
$service->services = $service;
$service->save();
}
... That's not tested, and I don't know what the format of your data is, but should give you a start on the problem..
I am making a kind of checkout form. At the top I have two radio buttons, one that should show prices without tax when clicked, and another to show prices when clicked.
<form id="f-p" method="post" action="#####">
<label for="exkl">Exkl. moms</label><input name="moms" id="exkl" type="radio"value="exkl" checked="checked"/>
<label for="inkl">Inkl. moms</label><input name="moms" id="inkl" type="radio" value="inkl"/>
</form>
Then I have prices below in tables. Those prices all come from a MySQL database. Now I can, separately, query the database for product IDs:
if($_POST['moms'] == "inkl") {
$inkl_query ="SELECT `product_id` FROM `######products_fieldvals` WHERE `fielddef_id`=4";
$iresult = mysql_query($inkl_query);
but I want to cycle through and compare product IDs between two tables, and if they match, to isert the price for that item from the first table into the second one, and to toggle back for the other radio. How can I do this? I want to use:
if($list1ID = $list2ID) {
INSERT INTO products(price)
SELECT value FROM products_fieldvals WHERE fieldval_id="4",
}
but that isn't working. Can someone please help?
if($list1ID == $list2ID) {
mysql_query("INSERT INTO products(price) SELECT value FROM products_fieldvals WHERE fieldval_id='4'");
}