public static function fetch($warehouse_id, $product_id) { $stock = Doctrine_Query::create()->from('Stock s')->where('s.warehouse_id =' . $warehouse_id)->andWhere('s.product_id =' . $product_id)->fetchOne(); if (!$stock) { $stock = new Stock(); $stock->setWarehouseId($warehouse_id); $stock->setProductId($product_id); $stock->setCurrentqty(0); $stock->setDate("2011-01-01"); $stock->save(); } return $stock; }
public function create($data) { $currentUser = parent::authenticateUser(); $stock = new Stock(); if (isset($data->total) && isset($data->date)) { $stock->setDate($data->date); $stock->setTotal($data->total); $stock->setOwner($currentUser->getLogin()); try { $idStock = $this->stockDAO->save($stock); header($this->server->getServerProtocol() . ' 201 Created'); header('Location: ' . $this->server->getRequestUri() . "/" . $idStock); header('Content-Type: application/json'); } catch (ValidationException $e) { header($this->server->getServerProtocol() . ' 400 Bad request'); echo json_encode($e->getErrors()); } } }
/** * Metodo que contiene el algoritmo para la generacion de posiciones. * Ver diagrama de actividades del Manual Tecnico, pag 67. * */ public function getPositions($owner) { $currentUser = parent::authenticateUser(); $startDate = $this->request->getStartDate(); $endDate = $this->request->getEndDate(); $stockRef = $this->stockDAO->findByOwnerAndDate($owner, $startDate); $stocks = $this->stockDAO->findByOwnerAndFilter($owner, $startDate, $endDate); $spendings = $this->spendingDAO->findByOwnerAndFilter($owner, $startDate, $endDate); $revenues = $this->revenueDAO->findByOwnerAndFilter($owner, $startDate, $endDate); if ($stocks == NULL && $spendings == NULL && $revenues == NULL) { header($this->server->getServerProtocol() . ' 400 Bad request'); echo "The defined interval time not contains spendings"; return; } $stocksChart = []; $stocks_array = []; $begin = new DateTime($startDate); $end = new DateTime($endDate); $interval = DateInterval::createFromDateString('1 month'); $period = new DatePeriod($begin, $interval, $end); foreach ($period as $dt) { $aux = new DateTime($dt->format("Y-m-d")); $initMonth = $dt; $topMonth = $aux->add($interval); foreach ($stocks as $stock) { if ($stock->getDate() >= $initMonth->format("Y-m-d") && $stock->getDate() < $topMonth->format("Y-m-d")) { $stockRef = $stock; } } $quantitySpendings = 0; foreach ($spendings as $spending) { if ($stockRef != NULL) { if ($spending->getDate() >= $stockRef->getDate() && $spending->getDate() <= $topMonth->format("Y-m-d")) { $quantitySpendings += $spending->getQuantity(); } } else { if ($spending->getDate() <= $topMonth->format("Y-m-d")) { $quantitySpendings += $spending->getQuantity(); } } } $quantityRevenues = 0; foreach ($revenues as $revenue) { if ($stockRef != NULL) { if ($revenue->getDate() >= $stockRef->getDate() && $revenue->getDate() < $topMonth->format("Y-m-d")) { $quantityRevenues += $revenue->getQuantity(); } } else { if ($revenue->getDate() <= $topMonth->format("Y-m-d")) { $quantityRevenues += $revenue->getQuantity(); } } } if ($stockRef != NULL) { $total = $stockRef->getTotal() + $quantityRevenues - $quantitySpendings; } else { $total = $quantityRevenues - $quantitySpendings; } $stockChart = new Stock(); $stockChart->setTotal($total); $stockChart->setDate($dt->format("Y-m-d")); array_push($stocksChart, $stockChart); $quantitySpendings = 0; $quantityRevenues = 0; } foreach ($stocksChart as $stock) { array_push($stocks_array, ["date" => $stock->getDate(), "total" => $stock->getTotal()]); } header($this->server->getServerProtocol() . ' 200 Ok'); header('Content-Type: application/json'); echo json_encode($stocks_array); }