I want to reduce my stock when bill is generated/saved - laravel-5

I am using this method to save customer data and sales as per ID. Now i want to reduce my stock when bill is saved or generated.
public function addBill(Request $request)
{
// $this->validate($request,[
// 'cus_id'=>'required'
// ]);
$customer = new Customer;
$customer->cus_id_no = $request->cus_id_no;
$customer->name = $request->name;
$customer->p_number = $request->p_number;
$customer->address = $request->address;
$customer->cus_type = $request->cus_type;
$customer->total = $request->total;
$customer->tax = $request->tax;
$customer->g_total = $request->g_total;
$customer->paid = $request->paid;
$customer->remaining = $request->g_total - $request->paid;
if($customer->save()){
$id=$customer->id;
foreach($request->productname as $key=>$v)
{
$data = array('cus_id'=>$id,
'pro_id'=>$v,
'actual_cost'=>$request->actual_cost[$key],
'qty'=>$request->qty[$key],
'price'=>$request->price[$key],
'dis'=>$request->dis[$key],
'amount'=>$request->amount[$key]
);
Sale::insert($data);
}
}
}
Stock is my Model name and stocks is my table name.

Related

How to connect controller eloquent's

I don't know if my question title is correct but I will try to form the question here correctly.
So I am creating order and customer management system in laravel and I am struggling with connecting two tables and controllers with each other.
I have two tables:
orders table with one test record
customers table with one test record
Also I have controller "OrdersController" with store function here is a source code:
public function store(Request $request)
{
$this->validate($request, [
'customer'=>'required',
'date'=>'required',
'products'=>'required',
'phone'=>'required',
'country'=>'required',
'state'=>'required',
'city'=>'required',
'address1'=>'required',
'address2'=>'required',
'zip'=>'required',
'sold_price'=>'required',
'gross_price'=>'required',
'paypal_fee'=>'required',
'track_number'=>'required',
'track_price'=>'required',
'track_link'=>'required',
'currency'=>'required'
]);
$order = new Order;
$order->customer = $request->input('customer');
$order->date = $request->input('date');
$order->products = $request->input('products');
$order->phone = $request->input('phone');
$order->country = $request->input('country');
$order->state = $request->input('state');
$order->city = $request->input('city');
$order->address1 = $request->input('address1');
$order->address2 = $request->input('address2');
$order->zip = $request->input('zip');
$order->sold_price = $request->input('sold_price');
$order->gross_price = $request->input('gross_price');
$order->paypal_fee = $request->input('paypal_fee');
$order->track_number = $request->input('track_number');
$order->track_price = $request->input('track_price');
$order->track_link = $request->input('track_link');
$order->currency = $request->input('currency');
$order->save();
return redirect('/orders')->with('success', 'შეკვეთა წარმატებით დაემატა');
}
So I want to check if record with $order->phone exists in customers table, if it exists don't create new record in customers table, and if it not exists I want to create new record in customers table with $order->customer (customer's name) and $order->phone (customer's phone).
This is my questions, I am a beginner and this is my first project, if here is some fatal mistakes please tell me. Thank you in advance!
First check in customer table with phone number record exist or not like this. I assume that your customer model is Customer
$customer = Customer::where('phone', '=', $request->input('phone'))->first();
if(!$customer){
create Customer here
}
$order = new Order;
$order->customer = $request->input('customer');
$order->date = $request->input('date');
$order->products = $request->input('products');
$order->phone = $request->input('phone');
$order->country = $request->input('country');
$order->state = $request->input('state');
$order->city = $request->input('city');
$order->address1 = $request->input('address1');
$order->address2 = $request->input('address2');
$order->zip = $request->input('zip');
$order->sold_price = $request->input('sold_price');
$order->gross_price = $request->input('gross_price');
$order->paypal_fee = $request->input('paypal_fee');
$order->track_number = $request->input('track_number');
$order->track_price = $request->input('track_price');
$order->track_link = $request->input('track_link');
$order->currency = $request->input('currency');
$order->save();
return redirect('/orders')->with('success', 'შეკვეთა წარმატებით დაემატა');

Access the cart session - Laravel 5.8

Hi i am using sopping cart in laravel 5.8. I want to access the array to be able to store the order in the database. I was able to access the data in the session cart. But to the part of options -> marca y medida. Also I do not know how to bring the total cart.
My controller is as follows
public function transferencia(Request $request)
{
$cart = Session::get('cart');
foreach ($cart as $key => $order) {
$data = json_decode($order, true);
foreach($data as $item){
$opt = new Order;
$opt->id_cliente = $request->input('idusuario');
$opt->fecha = date('j/n/Y');
$opt->cliente = $request->input('persona');
$opt->dni = $request->input('dni');
$opt->producto = $item['name'];
$opt->medida = 'medida'; //how to access this data
$opt->marca = $item['marca']; //how to access this data
$opt->precio = $item['price'];
$opt->cantidad = $item['qty'];
$opt->total = 'total';
$opt->factura = 'factura';
$opt->idpedido = 'idpedido';
$opt->save();
}
}
//dd($item);
//print_r($cont);
}

Laravel 5.5 Data is being inserted twice in database

This code enters multiple products in orders table, but what strange thing is happening is if i insert 2 products, data is being entered 4 times, if i insert 1 product it is being inerted 2 times. What is wrong with this code ?
foreach($request->product_id as $rpk => $rpv)
{
$data = new Order();
$data->user_id = $request->customer_id[$rpk];
$data->status = $request->status[$rpk];
$data->save();
Order::where('id', $data->id)->update(['order_number' => $data->id]);
// For OrderDetail
$detail = new OrderDetail();
$detail->customer_id = $request->customer_id[$rpk];
$detail->order_id = $data->id;
$detail->product_id = $request->product_id[$rpk];
$detail->price = $request->price[$rpk];
$detail->quantity = $request->quantity[$rpk];
$detail->total = $request->total[$rpk];
$detail->comment = $request->comment[0];
$detail->date = date('Y-m-d H:i:s');
$detail->bill_date = date('Y-m-d H:i:s');
//$detail->sales_tax = '13'; // Need to change this
$success = $detail->save();
}

CakePHP serializing objects

I'm stuck with the following problem:
I have a class CartItem. I want to store array of objects of CartItem in session (actually i'm implementing a shopping cart).
class CartItem extends AppModel{
var $name = "CartItem";
var $useTable = false;
}
I tried this:
function addToCart(){
$this->loadModel("Cart");
$this->layout = false;
$this->render(false);
$cart = array();
$tempcart = unserialize($this->Session->read("cart"));
if(isset($tempcart)){
$cart = $tempcart;
}
$productId = $this->request->data("id");
if(!$this->existsInCart($cart, $productId)){
$cartItem = new Cart();
$cartItem->productId = $productId;
$cartItem->createdAt = date();
$cart[] = $cartItem;
$this->Session->write("cart", serialize($cart));
echo "added";
}
else
echo "duplicate";
}
I think I'm writing these lines wrong:
$tempcart = unserialize($this->Session->read("cart"));
$this->Session->write("cart", serialize($cart));
as I'm not getting data from the session.
You are trying to add the whole Cart object to the session.
You should just add an array, like
$cart[] = array(
'productId' => $productId,
'createdAt' => date('Y-m-d H:i:s')
);
If you need to add an object to a session, you can use __sleep and __wakeup magic functions but I think in this case it's better to just add only the product id and date to the session.

Magento - How can I add rating information to a review

I'm manually creating reviews in Magento and I'm trying to find out how I add the rating information in? I can add the reviews no problem but I'm struggling with the rating values (star values).
I have an array that looks like this:
array("Price"=>80, "Value"=>60, "Quality"=>60);
How can I add that to the star system and the Summary Rating?
Thanks.
Ok, so this is what I have so far:
This adds a review:
$review->setEntityPkValue(23);//product id
$review->setStatusId(1);
$review->setTitle("title");
$review->setDetail("detail");
$review->setEntityId($review->getEntityIdByCode(Mage_Review_Model_Review::ENTITY_PRODUCT_CODE));
$review->setStoreId(Mage::app()->getStore()->getId());
$review->setStatusId(1); //approved
$review->setNickname("Me");
$review->setReviewId($review->getId());
$review->setStores(array(Mage::app()->getStore()->getId()));
$review->save();
$review->aggregate();
This adds a rating for a review <-I'm stuck here!
// this is some test code to add the rating review
$rating[0]['Price'] = 80;
$rating[0]['Value'] = 100;
$rating[0]['Quality'] = 80;
$product_id = 23;
$review_id = 631;
foreach ($rating as $ratingId => $optionId) {
// This is the bit where it all seems to go wrong!:
Mage::getModel('rating/rating')
->setRatingId(1)
->setReviewId($review_id)
->addOptionVote($val, $product_id);
}
Thanks!
This worked for me:
public function addReview($ratingarray)
{
$product_id = $ratingarray['product_id'];
$storeid = $ratingarray['store_id'];
$title = $ratingarray['title'];
$customerid = $ratingarray['customer_id'];
$nickname = $ratingarray['nickname'];
$detail = $ratingarray['detail'];
$review = Mage::getModel('review/review');
$review->setEntityPkValue($product_id);
$review->setStatusId(1);
$review->setTitle($title);
$review->setDetail($detail );
$review->setEntityId($review->getEntityIdByCode(Mage_Review_Model_Review::ENTITY_PRODUCT_CODE));
$review->setStoreId($storeid);
$review->setStatusId(1); //approved
$review->setCustomerId($customerid);
$review->setNickname($nickname);
$review->setReviewId($review->getId());
$review->setStores(array($storeid));
$review->save();
$review->aggregate();
//return "success";
$rating_options = $ratingarray['options'];
/*array(
array(1,2,3,4),
array(6,7,8),
array(11,12)
);*/
$row = count($rating_options);
$rating_id = 1;
foreach($rating_options as $key1=>$val1)
{
foreach($val1 as $key2=>$val2)
{
$_rating = Mage::getModel('rating/rating')
->setRatingId($key1)
->setReviewId($review->getId())
->addOptionVote($val2,$product_id );
}
}
return "Success";
}
I am calling this like =>
$options = array(1=>array(1,2,3,4),2=>array(6,7,8),3=>array(11,12));
$reviewarray = array('customer_id'=>'21','product_id'=>'176','store_id'=>'4','title'=>'Review','nickname'=>'XYZ','detail'=>'Nice Product with Life time warrenty', 'options'=>$options);

Resources