예제 #1
0
 public function store(Request $request)
 {
     $validator = Validator::make($request->all(), ['firstname' => 'required', 'lastname' => 'required', 'street' => 'required', 'email' => 'required|email|max:255', 'city' => 'required', 'state' => 'required', 'city' => 'required', 'postcode' => 'required', 'phone' => 'required', 'shipping' => 'required']);
     if ($validator->fails()) {
         return Redirect::to('cart/billing')->withErrors($validator)->withInput();
     } else {
         $count = Transaction::all()->count();
         $formid = 'hr' . '-' . date('m') . date('Y') . '-' . ($count + 1);
         $cart_content = Cart::content();
         $get_subtotal = Cart::total();
         $get_shipping = Input::get('shipping');
         #save user
         $user = new User();
         $user->name = Input::get('firstname') . ' ' . Input::get('lastname');
         $user->email = Input::get('email');
         $user->save();
         $user_id = $user->id;
         #save group
         $group = array('id_user' => $user_id, 'id_group' => 3);
         User::set_group($group);
         #save profile
         $profile = new profile();
         $profile->id_user = $user_id;
         $profile->firstname = Input::get('firstname');
         $profile->lastname = Input::get('lastname');
         $profile->country = Input::get('country');
         $profile->street = Input::get('street');
         $profile->optionals = Input::get('optionals');
         $profile->email = Input::get('email');
         $profile->city = Input::get('city');
         $profile->state = Input::get('state');
         $profile->postcode = Input::get('postcode');
         $profile->phone = Input::get('phone');
         $profile->note = Input::get('note');
         $profile->save();
         $profile_id = $profile->id;
         #save transaction
         $transaction = new Transaction();
         $transaction->date = date('Y-m-d');
         $transaction->code = $formid;
         $transaction->subtotal = $get_subtotal;
         $transaction->shipping = $get_shipping;
         $transaction->total = $get_subtotal + $get_shipping;
         $transaction->subsribe = 0;
         $transaction->id_user = $user_id;
         $transaction->save();
         $transaction_id = $transaction->id;
         #save order
         #show all data in cart
         foreach ($cart_content as $cart) {
             $data = array('id_transaction' => $transaction_id, 'id_product' => $cart->id, 'qty' => $cart->qty, 'subtotal' => $cart->subtotal);
             Transaction::insert_order($data);
         }
         #save transaction subsribe
         #save order
         #show all data in cart
         // start count total and price
         foreach ($cart_content as $cart) {
             $qty = 0;
             $price_total = 0;
             if ($cart->options->subs > 0) {
                 $qty += $cart->qty;
                 $price_total += $cart->subtotal;
                 // shipping calculate
                 if ($qty == 1) {
                     $shipping = 5;
                 } else {
                     $shipping = 2 * ($qty - 1) + 5;
                 }
                 $subsribe = new Transaction();
                 $subsribe->date = date('Y-m-d');
                 $subsribe->code = $formid . '-' . $cart->options->subs;
                 $subsribe->subtotal = $price_total;
                 $subsribe->shipping = $shipping;
                 $subsribe->total = $price_total + $shipping;
                 $subsribe->subsribe = $cart->options->subs;
                 $subsribe->id_user = $user_id;
                 $subsribe->subsribe_status = 'active';
                 $subsribe->save();
                 $subsribe_id = $subsribe->id;
                 $data = array('id_transaction' => $subsribe_id, 'id_product' => $cart->id, 'qty' => $cart->qty, 'subtotal' => $cart->subtotal);
                 Transaction::insert_order($data);
             }
         }
         // $transac = new Transac();
         // $transac->send_order($transaction_id);
         Cart::destroy();
         return redirect('cart/finish/' . $transaction_id)->with('message', 'You have done successfully');
     }
 }