/** * @param : int $id * @param : array $data * @return boolean */ public static function update($id, $data) { if (!is_array($data) || !count($data)) { return; } $shipFromData = $data['shipFrom']; $shipToData = $data['shipTo']; $itemData = $data['item']; $companyData = $data['company']; $query = "SELECT * FROM `order` WHERE id = :id"; $param = array('id' => $id); $result = DB::select($query, $param); if (count($result) > 0) { $order = $result[0]; $shipToId = $order->ship_to ? $order->ship_to : 0; $shipFromId = $order->ship_from ? $order->ship_from : 0; $itemId = $order->item_id ? $order->item_id : 0; $companyId = $order->company_id ? $order->company_id : 0; ShipFrom::update($shipFromId, $shipFromData); ShipTo::update($shipToId, $shipToData); Item::update($itemId, $itemData); Company::update($companyId, $companyData); return true; } }
public function saveOrder() { $inputShipFrom = Input::get('shipFrom'); $inputShipFrom = $this->jsonToInputArray($inputShipFrom); $inputshipTo = Input::get('shipTo'); $inputshipTo = $this->jsonToInputArray($inputshipTo); $inputItem = Input::get('itemDetail'); $inputItem = $this->jsonToInputArray($inputItem); $inputCompany = Input::get('companyInfo'); $inputCompany = $this->jsonToInputArray($inputCompany); $result = array('errors' => '', 'result' => ''); $foundErrors = false; //validate ship from info. $shipFrom = new ShipFrom(); if ($shipFrom->validate($inputShipFrom) === false) { $foundErrors = true; $messages = $shipFrom->messages(); if (count($messages)) { foreach ($messages as $message) { $result['errors'] .= "<li>{$message}</li>"; } } } //validate ship to information $shipTo = new ShipTo(); if ($shipTo->validate($inputshipTo) === false) { $foundErrors = true; $messages = $shipTo->messages(); if (count($messages)) { foreach ($messages as $message) { $result['errors'] .= "<li>{$message}</li>"; } } } //validate item information $item = new Item(); if ($item->validate($inputItem) === false) { $foundErrors = true; $messages = $item->messages(); if (count($messages)) { foreach ($messages as $message) { $result['errors'] .= "<li>{$message}</li>"; } } } //validate company information $company = new Company(); if ($company->validate($inputCompany) === false) { $foundErrors = true; $messages = $company->messages(); if (count($messages)) { foreach ($messages as $message) { $result['errors'] .= "<li>{$message}</li>"; } } } if ($foundErrors == true) { echo json_encode($result); return; } $data = array('shipFrom' => $inputShipFrom, 'shipTo' => $inputshipTo, 'item' => $inputItem, 'company' => $inputCompany); $action = Input::get('action'); if ($action == 'update') { $orderId = Input::get('order_id'); if (Order::update($orderId, $data) == true) { $result['result'] = 'success'; } else { $result['errors'] = "<li>There was a problem with record update.Please try later.</li>"; } } else { //create new order $newOrderId = Order::insert($data); if (is_numeric($newOrderId) && $newOrderId > 0) { $result['result'] = 'success'; } } echo json_encode($result); }