public function post() { $post = Input::all(); $validator = Product::validate($post); $productId = $post['id']; if ($validator->fails()) { return Redirect::to('productos/' . $productId)->withErrors($validator)->withInput(); } else { $product = self::__checkExistence($productId); $isNew = false; if (!$productId) { $product = new Product(); $isNew = true; } $product->name = $post['name']; $product->description = $post['description']; $product->code = $post['code']; $product->minimum_stock = $post['minimum_stock']; $product->cost = str_replace(',', '.', $post['cost']); $product->save(); if ($isNew) { Globals::triggerAlerts(4, array('productId' => $product->id)); } if ($post['status'] == 'inactive') { $product->delete(); } else { if ($product->trashed()) { $product->restore(); } } Session::flash('success', 'Producto guardado correctamente.'); return Redirect::to('productos'); } }
public function post() { $post = Input::all(); $validator = Adjustment::validate($post); $adjustmentId = $post['id']; if ($validator->fails()) { return Redirect::to('ajustes/' . $adjustmentId)->withErrors($validator)->withInput(); } else { $adjustment = self::__checkExistence($adjustmentId); if (!$adjustmentId) { DB::beginTransaction(); $adjustment = new Adjustment(); $adjustment->products_id = $post['products_id']; $adjustment->users_id = Auth::user()->id; $adjustment->type = $post['type']; $adjustment->amount = $post['amount']; $adjustment->reason = $post['reason']; $adjustment->save(); $product = Product::find($post['products_id']); if ($post['type'] == 'credit') { $product->stock = $product->stock + $post['amount']; $product->save(); } else { if ($product->stock >= $post['amount']) { $product->stock = $product->stock - $post['amount']; $product->save(); Globals::triggerAlerts(1, array('productId' => $product->id)); Globals::triggerAlerts(2, array('productId' => $product->id)); } else { Session::flash('error', 'No se puede debitar más de lo que hay en stock.'); DB::rollback(); return Redirect::to('ajustes'); } } Session::flash('success', 'Ajuste realizado exitosamente.'); DB::commit(); } return Redirect::to('ajustes'); } }
public function post() { $post = Input::all(); if ($post['products'] == '[]') { $post['products'] = ''; } $validator = SupplierOrder::validate($post); if ($validator->fails()) { return Redirect::to('pedidos/' . $post['id'])->withErrors($validator)->withInput(); } else { try { DB::transaction(function () use($post) { $supplierOrder = self::__checkExistence($post['id']); if (!$supplierOrder) { //new order $supplierOrder = new SupplierOrder(); $supplierOrder->suppliers_id = $post['suppliers_id']; $supplierOrder->users_id = Auth::user()->id; $supplierOrder->code = $post['code']; $supplierOrder->save(); $products = json_decode($post['products']); $toSync = array(); foreach ($products as $current) { $product = Product::find($current->id); if (!is_null($product)) { $toSync[$current->id]['cost'] = $product->cost; $toSync[$current->id]['amount'] = $current->amount; } } $supplierOrder->products()->sync($toSync); Globals::triggerAlerts(5, array('supplierOrderId' => $supplierOrder->id)); } else { if (isset($post['received']) and $post['received'] == 1) { //received $supplierOrder->status = 'received'; $supplierOrder->save(); foreach ($supplierOrder->products as $pivotProduct) { $array = $pivotProduct->toArray(); $product = Product::find($array['id']); if (!is_null($product)) { $product->stock = $product->stock + $array['pivot']['amount']; $product->save(); } } Globals::triggerAlerts(6, array('supplierOrderId' => $supplierOrder->id)); } elseif (isset($post['received']) and $post['received'] == 2) { //canceled $supplierOrder->status = 'canceled'; $supplierOrder->save(); } else { Session::flash('error', 'Error en la recepción de su pedido, por favor vuelva a intentarlo.'); } } }); Session::flash('success', 'Pedido procesado exitosamente.'); } catch (Exception $e) { Session::flash('error', 'Ocurrió un error inesperado, por favor vuelva a intentarlo.'); } return Redirect::to('pedidos'); } }