示例#1
0
 /**
  * Store a newly created resource in storage.
  *
  * @param bool $id
  * @param bool $method
  *
  * @return \Illuminate\Http\Response
  */
 public function postStore($id = FALSE, $method = FALSE)
 {
     $response['status'] = 'error';
     $response['message'] = trans('orders.not_saved');
     if (!empty($_POST)) {
         if ($method == FALSE) {
             $error = FALSE;
             if (empty(trim(Input::get('city')))) {
                 $response['message'] = trans('orders.city_required');
                 $error = TRUE;
             }
             if (empty(trim(Input::get('address')))) {
                 $response['message'] = trans('orders.address_required');
                 $error = TRUE;
             }
             if (empty(trim(Input::get('phone')))) {
                 $response['message'] = trans('orders.phone_required');
                 $error = TRUE;
             }
             if (empty(trim(Input::get('last_name')))) {
                 $response['message'] = trans('orders.last_name_required');
                 $error = TRUE;
             }
             if (empty(trim(Input::get('name')))) {
                 $response['message'] = trans('orders.name_required');
                 $error = TRUE;
             }
             if (empty(trim(Input::get('delivery_type')))) {
                 $response['message'] = trans('orders.delivery_type_required');
                 $error = TRUE;
             }
             if (empty(trim(Input::get('status')))) {
                 $response['message'] = trans('orders.status_required');
                 $error = TRUE;
             }
             if ($error === FALSE) {
                 $data = ['user_id' => Input::get('user_id'), 'name' => trim(Input::get('name')), 'last_name' => trim(Input::get('last_name')), 'email' => trim(Input::get('email')), 'phone' => trim(Input::get('phone')), 'address' => trim(Input::get('address')), 'city' => trim(Input::get('city')), 'state' => trim(Input::get('state')), 'post_code' => trim(Input::get('post_code')), 'comment' => trim(Input::get('comment')), 'status' => Input::get('status'), 'delivery_type' => Input::get('delivery_type'), 'created_at' => Input::get('created_at')];
                 if ($id == FALSE) {
                     if (($id = Model_Orders::insertOrder($data)) > 0) {
                         $response['status'] = 'success';
                         $response['message'] = trans('orders.saved');
                         $response['id'] = $id;
                     }
                 } elseif (is_numeric($id)) {
                     if (Model_Orders::updateOrder($id, $data)) {
                         //If updated check status and if it's canceled, return product quantities
                         if (Input::get('status') == 'canceled') {
                             $records = Model_Orders::getOrderProducts($id);
                             if (!empty($records) && is_array($records)) {
                                 foreach ($records as $record) {
                                     if (!empty($record['id'])) {
                                         Model_Orders::returnProduct($record['id']);
                                     }
                                 }
                             }
                         }
                         $response['status'] = 'success';
                         $response['message'] = trans('orders.saved');
                     }
                 }
             }
         } elseif ($method == 'product') {
             $response['message'] = trans('orders.product_not_saved');
             $error = FALSE;
             if (empty($order_id = intval(trim(Input::get('order_id'))))) {
                 $error = TRUE;
             }
             if (empty($size = trim(Input::get('size')))) {
                 $error = TRUE;
             }
             if (empty($quantity = intval(trim(Input::get('quantity'))))) {
                 $error = TRUE;
             }
             if (empty($product_id = intval(trim(Input::get('product_id'))))) {
                 $error = TRUE;
             }
             if ($error === FALSE) {
                 $product = Model_Products::getProducts($product_id, ['sizes']);
                 if (!empty($product[$product_id]) && is_array($product[$product_id])) {
                     $product = $product[$product_id];
                 }
                 $data = ['order_id' => $order_id, 'product_id' => $product_id, 'size' => $size, 'quantity' => $quantity, 'original_price' => $product['original_price']];
                 //If size doesn't have price, get products one
                 if (empty($price = trim(Input::get('price')))) {
                     $data['price'] = floatval($product['price']);
                 } else {
                     $data['price'] = floatval($price);
                 }
                 $data['total'] = $data['price'];
                 //Discount
                 $discount_start = strtotime($product['discount_start']);
                 $discount_end = strtotime($product['discount_end']);
                 $now = time();
                 //If currently the product have discount
                 if ($now > $discount_start && $now < $discount_end) {
                     //If size doesn't have discount, get products one
                     if (empty($discount = trim(Input::get('discount')))) {
                         $data['discount'] = floatval($product['discount_price']);
                     } else {
                         $data['discount'] = floatval($discount);
                     }
                     $data['total'] = $data['discount'];
                 }
                 //If quantity more then one
                 if (intval($quantity) > 1) {
                     $data['total'] = intval($quantity) * $data['total'];
                 }
                 if (Model_Orders::insertProduct($data) === TRUE) {
                     //Discard product sizes
                     if (!empty($product['sizes'])) {
                         $product['sizes'] = json_decode($product['sizes'], TRUE);
                     }
                     foreach ($product['sizes'] as $size_name => $product_size) {
                         if ($product_size['name'] == $size && intval($quantity) > 0) {
                             $product['sizes'][$size_name]['quantity'] = $product_size['quantity'] - $quantity;
                         }
                     }
                     $sizes = json_encode($product['sizes']);
                     if (intval($quantity) > 0) {
                         $product['quantity'] = intval($product['quantity']) - intval($quantity);
                     } else {
                         $product['quantity'] = 0;
                     }
                     Model_Orders::manageQuantities($product['id'], $sizes, $product['quantity']);
                     Model_Products::saveProductToSize($product['id'], $sizes);
                     $response['status'] = 'success';
                     $response['message'] = trans('orders.product_saved');
                 }
             }
         }
     }
     return response()->json($response);
 }
示例#2
0
 /**
  * Update the specified resource in storage.
  *
  * @param  \Illuminate\Http\Request $request
  * @param  int $id
  * @param  int $action
  *
  * @return \Illuminate\Http\Response
  */
 public function postUpdate(Request $request, $id)
 {
     $response['status'] = 'error';
     $response['message'] = trans('products.not_updated');
     if (!empty($_POST)) {
         if (!empty(Input::get('image_sync'))) {
             if (empty($images = json_decode(Input::get('images'), TRUE))) {
                 $images = [];
             }
             $response['message'] = trans('products.images_not_synced');
             //Get current local images
             $images_array = [];
             if (!empty($id) && is_dir($this->images_path . $id) && is_dir($this->images_path . $id . DIRECTORY_SEPARATOR . Config::get('images.full_size'))) {
                 $local_images = array_diff(scandir($this->images_path . $id . DIRECTORY_SEPARATOR . Config::get('images.full_size')), array('..', '.'));
                 natcasesort($local_images);
                 $local_images = array_values(array_filter($local_images));
                 if (!empty($local_images) && is_array($local_images)) {
                     foreach ($local_images as $key => $data) {
                         if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
                             $data = iconv("WINDOWS-1251", "UTF-8", $data);
                         }
                         if (empty($images_array[$data])) {
                             $images_array[$data] = $key;
                         }
                     }
                 }
             }
             //Remove image
             if (!empty($remove_image = Input::get('remove_image'))) {
                 if (isset($images[$remove_image])) {
                     //Remove from array
                     unset($images[$remove_image]);
                     //If WIN reconvert to utf8
                     if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
                         $remove_image = iconv("UTF-8", "WINDOWS-1251", $remove_image);
                     }
                     //Remove local files
                     $images_to_remove = [$this->images_path . $id . DIRECTORY_SEPARATOR . Config::get('images.full_size') . DIRECTORY_SEPARATOR . $remove_image, $this->images_path . $id . DIRECTORY_SEPARATOR . Config::get('images.lg_icon_size') . DIRECTORY_SEPARATOR . $remove_image, $this->images_path . $id . DIRECTORY_SEPARATOR . Config::get('images.md_icon_size') . DIRECTORY_SEPARATOR . $remove_image, $this->images_path . $id . DIRECTORY_SEPARATOR . Config::get('images.sm_icon_size') . DIRECTORY_SEPARATOR . $remove_image];
                     foreach ($images_to_remove as $image) {
                         if (file_exists($image)) {
                             unlink($image);
                         }
                     }
                     //Remove message
                     $message_success = trans('products.image_removed');
                 }
             } else {
                 //Images save message
                 $message_success = trans('products.images_synced');
             }
             if (is_array($images)) {
                 $images = array_merge($images_array, $images);
             } else {
                 $images = $images_array;
             }
             if (!empty($images)) {
                 Model_Products::deleteAllImages($id);
                 if (Model_Products::setProductObjects(['images' => $images], $id) === TRUE) {
                     $response['status'] = 'success';
                     $response['message'] = $message_success;
                 }
             } else {
                 if (Model_Products::deleteAllImages($id) === TRUE) {
                     $response['status'] = 'success';
                     $response['message'] = $message_success;
                 }
             }
         } else {
             $error = FALSE;
             if (empty(trim(Input::get('title')))) {
                 $response['message'] = trans('products.title_required');
                 $error = TRUE;
             }
             if (empty(trim(Input::get('friendly_url')))) {
                 $response['message'] = trans('products.url_required');
                 $error = TRUE;
             }
             $available_sizes = [];
             if (!empty(Input::get('sizes')) && is_array(Input::get('sizes'))) {
                 foreach (Input::get('sizes') as $size_name => $size) {
                     if ($size['quantity'] > 0) {
                         $available_sizes[] = $size_name;
                     }
                 }
             }
             if (!empty(Input::get('quantity'))) {
                 $available = 1;
             } else {
                 $available = 0;
             }
             if ($error === FALSE) {
                 $data = ['title' => trim(Input::get('title')), 'description' => Input::get('description'), 'main_category' => Input::get('main_category'), 'quantity' => Input::get('quantity'), 'available' => $available, 'position' => Input::get('position'), 'active' => Input::get('active'), 'original_price' => Input::get('original_price'), 'price' => Input::get('price'), 'discount_price' => Input::get('discount_price'), 'discount_start' => Input::get('discount_start'), 'discount_end' => Input::get('discount_end'), 'created_at' => Input::get('created_at'), 'sizes' => Input::get('sizes'), 'page_title' => Input::get('page_title'), 'meta_description' => Input::get('meta_description'), 'meta_keywords' => Input::get('meta_keywords'), 'related_products' => Input::get('related_products'), 'dimensions_table' => Input::get('dimensions_table')];
                 if (Model_Products::updateProduct($id, $data) === TRUE) {
                     try {
                         //Manage sizes relations
                         if (!empty($available_sizes) && is_array($available_sizes)) {
                             Model_Products::saveProductToSize($id, $available_sizes);
                         }
                         //Manage categories relations
                         if (!empty(Input::get('categories')) && is_array(Input::get('categories'))) {
                             Model_Products::setProductToCategory($id, Input::get('categories'));
                         }
                         //Manage Friendly URL
                         Model_Products::setURL($id, Input::get('friendly_url'));
                         //Manage tags
                         $tags = Input::get('tags');
                         if (!empty($tags)) {
                             $tags = explode(',', $tags);
                             Model_Products::saveTags($id, $tags);
                         } else {
                             Model_Products::removeAllTags($id);
                         }
                         //Manage manufacturer
                         $manufacturer = Input::get('manufacturer');
                         if (!empty($manufacturer)) {
                             Model_Products::setManufacturer($id, $manufacturer);
                         }
                         //Manage material
                         $material = Input::get('material');
                         if (!empty($material)) {
                             Model_Products::setMaterial($id, $material);
                         }
                         //Manage colors
                         if (!empty(Input::get('colors')) && is_array(Input::get('colors'))) {
                             Model_Products::setColors($id, Input::get('colors'));
                         }
                         $response['status'] = 'success';
                         $response['message'] = trans('products.updated');
                     } catch (Exception $e) {
                         $response['message'] = $e;
                     }
                 } else {
                     $response['message'] = trans('products.not_updated');
                 }
             }
         }
     }
     return response()->json($response);
 }