public function adminOrderSave(Customer $customer, Order $order = NULL) { // Set flag indicating if we are creating a new order or // updating an existing order. $isCreateAction = FALSE; if (is_null($order)) { $isCreateAction = TRUE; } // First, save the order header. if ($isCreateAction) { $order = new Order(); } $order->delivery_terms = Input::get('shipping_option'); $order->order_notes = Input::get('order_notes'); if ($order->order_notes == 'Order Notes') { $order->order_notes = NULL; } $order->order_status = Input::get('order_status'); $order->customer_id = $customer->id; if ($isCreateAction) { $order->order_date = date('Y-m-d'); $order->online_order_ind = FALSE; $order->save(); } $this->order_id = $order->id; $this->customer_id = $order->customer->id; $formIdList = Input::get('form_id'); $qtyList = Input::get('qty'); //$now = Carbon::now('utc')->toDateTimeString(); $orderItemList = array(); for ($i = 0; $i < count($formIdList); $i++) { if (empty($formIdList[$i])) { continue; } $formIdList[$i] = strtoupper($formIdList[$i]); if (!in_array(substr($formIdList[$i], 0, 1), array('C', 'D')) && !strstr($formIdList[$i], 'SET')) { $formIdList[$i] = 'CD' . str_pad($formIdList[$i], 2, '0', STR_PAD_LEFT); } $query = Product::where('form_id', '=', $formIdList[$i]); $query->where('workshop_year', '=', Config::get('workshop.current_workshop_year')); $product = $query->get()->first(); // Do bulk database insert from array for better performance. if ($product && $product->id > 0) { $orderItemList[] = array('product_id' => $product->id, 'order_id' => $order->id, 'qty' => $qtyList[$i], 'mp3_ind' => FALSE); } } // Delete existing order items, if any, // if this is an 'update' action. if (!$isCreateAction) { $items = array(); foreach ($order->orderItems as $item) { $items[] = $item->id; } OrderItem::destroy($items); } // Bulk insert! OrderItem::insert($orderItemList); // We must get charges AFTER adding/inserting the order items. $order = OrdersController::getOrderCharges($order); Log::debug('Admin Order Create - Override value: ' . print_r(Input::get('override_amounts'), TRUE)); if (Input::get('override_amounts', FALSE)) { $override_values = array('subtotal_amt' => Input::get('subtotal_amt'), 'shipping_charge' => Input::get('shipping_charge'), 'discounts' => Input::get('discounts'), 'order_total' => Input::get('order_total')); if (strlen($override_values['subtotal_amt'])) { $order->subtotal_amt = $override_values['subtotal_amt']; } if (strlen($override_values['shipping_charge'])) { $order->shipping_charge = $override_values['shipping_charge']; } if (strlen($override_values['discounts'])) { $order->discounts = $override_values['discounts']; } if (strlen($override_values['order_total'])) { $order->order_total = $override_values['order_total']; } else { $order->order_total = $order->subtotal_amt + $order->shipping_charge - $order->discounts; } } if ($order->updateUniques()) { // Re-direct to display the order details. return Redirect::route('orders.show', $order->id); } }