/** * @param Purchase $purchase * @return Log[] */ public function findByPurchase(Purchase $purchase) { $dql = "select l, u from ent:Log l left join l.user u where l.resource_id = :id and l.resource_type = :type"; $query = $this->em()->createQuery($dql); $query->setParameters(['id' => $purchase->getId(), 'type' => Purchase::class]); return $query->getResult(); }
/** * @param $optional */ protected function assignCancelDate($optional) { if ($canceledAt = $this->getCanceledAt()) { if (!$canceledAt instanceof \DateTime) { $canceledAt = \DateTime::createFromFormat($this->getInputDateFormat(), $canceledAt); if (!$canceledAt instanceof \DateTime) { $canceledAt = new \DateTime(); } } $this->purchase->setCanceledAt($canceledAt); } else { if (!$this->purchase->getCanceledAt() instanceof \DateTime && !$optional) { $this->purchase->setCanceledAt(new \DateTime()); } } }
/** * @return int|null */ public function getRecordIdOrNull() { return $this->existingRecord ? $this->existingRecord->getId() : null; }
/** * Download purchase detail */ public function downloadExcelAction() { try { $purchase = $this->getPurchaseService()->getPurchaseWithItems($this->getRequestQuery('id')); $account = $this->account(); // set excel working directory $config = $this->getServiceManager()->get('ApplicationConfig'); if (isset($config['cache_dir'])) { $this->setExcelDocsWorkingDir($config['cache_dir']); } else { $this->setExcelDocsWorkingDir(sys_get_temp_dir()); } $excel = $this->createExcelDoc(function (\PHPExcel $excel) use($purchase, $account) { $ws = $excel->getActiveSheet(); $ws->mergeCells('A2:F2'); $ws->setCellValue('A2', $purchase->getTitle()); $ws->getRowDimension(2)->setRowHeight(30); $ws->getStyle('A2')->getFont()->setBold(true); $ws->getStyle('A2')->getFont()->setSize(18); $ws->mergeCells('A3:F3'); $ws->setCellValue('A3', sprintf('Status: %s', Purchase::statusToLocaleID($purchase->getStatus()))); $ws->getStyle('A3')->getFont()->setSize(13); $ws->getRowDimension(3)->setRowHeight(22); $thRowNum = 5; if ($purchase->getCreator() instanceof User) { $ws->mergeCells('A5:F5'); $ws->setCellValue('A5', sprintf('Dibuat Oleh: %s', $purchase->getCreator()->getName())); $ws->getStyle('A5')->getFont()->setSize(13); $ws->getRowDimension(5)->setRowHeight(22); $thRowNum += 1; } if ($purchase->getCreatedAt() instanceof \DateTime) { $ws->mergeCells('A6:F6'); $ws->setCellValue('A6', sprintf('Tgl. Dibuat: %s', $purchase->getCreatedAt()->format('d M Y'))); $ws->getStyle('A6')->getFont()->setSize(13); $ws->getRowDimension(6)->setRowHeight(22); $thRowNum += 1; } if ($purchase->getOrderedAt() instanceof \DateTime) { $ws->mergeCells('A7:F7'); $ws->setCellValue('A7', sprintf('Tgl. Order: %s', $purchase->getOrderedAt()->format('d M Y'))); $ws->getStyle('A7')->getFont()->setSize(13); $ws->getRowDimension(7)->setRowHeight(22); $thRowNum += 2; } if ($purchase->getDeliveredAt() instanceof \DateTime) { $ws->mergeCells('A8:F8'); $ws->setCellValue('A8', sprintf('Tgl. Diterima: %s', $purchase->getDeliveredAt()->format('d M Y'))); $ws->getStyle('A8')->getFont()->setSize(13); $ws->getRowDimension(8)->setRowHeight(22); $thRowNum += 1; } $ws->getColumnDimension('A')->setWidth(5); $ws->getColumnDimension('B')->setWidth(32); $ws->getColumnDimension('C')->setWidth(14); $ws->getColumnDimension('D')->setWidth(20); $ws->getColumnDimension('E')->setWidth(20); $ws->getColumnDimension('F')->setWidth(20); $ws->setCellValue('A' . $thRowNum, 'No'); $ws->setCellValue('B' . $thRowNum, 'Item'); $ws->setCellValue('C' . $thRowNum, 'Qty'); $ws->setCellValue('D' . $thRowNum, 'Harga/Unit'); $ws->setCellValue('E' . $thRowNum, 'Subtotal'); $ws->setCellValue('F' . $thRowNum, 'Total'); $ws->getRowDimension($thRowNum)->setRowHeight(22); $ws->getStyle(sprintf('A%d:F%d', $thRowNum, $thRowNum))->getFont()->setBold(true); $itemStartRow = $thRowNum + 1; $itemEndRow = $itemStartRow; foreach ($purchase->getItems() as $index => $item) { $ws->setCellValue('A' . $itemEndRow, $index + 1); $ws->setCellValue('B' . $itemEndRow, $item->getItemName()); $ws->setCellValue('C' . $itemEndRow, sprintf('%s %s', number_format($item->getQuantity(), 0), $item->getUnit())); $ws->setCellValue('D' . $itemEndRow, $item->getUnitPrice()); $ws->setCellValue('E' . $itemEndRow, $item->getSubtotal()); $ws->setCellValue('F' . $itemEndRow, $item->getTotal()); $ws->getRowDimension($itemEndRow)->setRowHeight(22); $itemEndRow++; } $rollUpRowNum = $itemEndRow; $ws->getRowDimension($rollUpRowNum)->setRowHeight(22); $ws->mergeCells(sprintf('A%d:D%d', $rollUpRowNum, $rollUpRowNum)); $ws->setCellValue('A' . $rollUpRowNum, 'Total'); $ws->setCellValue('E' . $rollUpRowNum, sprintf('=SUM(E%d:E%d)', $itemStartRow, $itemEndRow - 1)); $ws->setCellValue('F' . $rollUpRowNum, sprintf('=SUM(F%d:F%d)', $itemStartRow, $itemEndRow - 1)); $noteRowNum = $rollUpRowNum + 2; if ($purchase->getNote()) { $ws->mergeCells(sprintf('A%d:F%d', $noteRowNum, $noteRowNum)); $ws->getStyle('A' . $noteRowNum)->getFont()->setSize(13); $ws->setCellValue('A' . $noteRowNum, 'Catatan:'); $ws->mergeCells(sprintf('A%d:F%d', $noteRowNum + 1, $noteRowNum + 1)); $ws->getStyle('A' . ($noteRowNum + 1))->getFont()->setSize(13); $ws->setCellValue('A' . ($noteRowNum + 1), $purchase->getNote()); } $ws->getStyle(sprintf('A%d:F%d', $thRowNum, $rollUpRowNum))->getBorders()->getAllBorders()->setBorderStyle(\PHPExcel_Style_Border::BORDER_THIN); $ws->getStyle(sprintf('D%d:D%d', $itemStartRow, $rollUpRowNum - 1))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); $ws->getStyle(sprintf('E%d:E%d', $itemStartRow, $rollUpRowNum))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); $ws->getStyle(sprintf('F%d:F%d', $itemStartRow, $rollUpRowNum))->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); $ws->getStyle(sprintf('D%d:D%d', $itemStartRow, $rollUpRowNum - 1))->getNumberFormat()->setFormatCode(sprintf('"%s" #,##0.00_-', $account->getDefaultCurrency())); $ws->getStyle(sprintf('E%d:E%d', $itemStartRow, $rollUpRowNum))->getNumberFormat()->setFormatCode(sprintf('"%s" #,##0.00_-', $account->getDefaultCurrency())); $ws->getStyle(sprintf('F%d:F%d', $itemStartRow, $rollUpRowNum))->getNumberFormat()->setFormatCode(sprintf('"%s" #,##0.00_-', $account->getDefaultCurrency())); $ws->getStyle(sprintf('A%d:F%d', $thRowNum, $rollUpRowNum))->getFont()->setSize(13); }); $this->downloadExcelDoc($excel, $purchase->getTitle()); return null; } catch (NoResultException $e) { return $this->notFoundAction(); } }
/** * @param Purchase $purchase * @param User $corrector * @param array $rawItems */ public function increaseStock(Purchase $purchase, User $corrector, array $rawItems) { $getStockId = function ($stock) { if ($stock instanceof Stock) { return $stock->getId(); } else { if (is_numeric($stock)) { return $stock; } else { throw new \InvalidArgumentException(); } } }; $stocks = []; $unitPrices = []; foreach ($rawItems as $item) { // todo: change $items array to object? to prevent missing keys in array. if (isset($item['stock']) && $item['stock'] && isset($item['quantity']) && $item['quantity']) { try { $stocks[$getStockId($item['stock'])] = $item['quantity']; if (isset($item['total']) && $item['total'] && $item['quantity'] > 0) { $unitPrices[$getStockId($item['stock'])] = $item['total'] / $item['quantity']; } } catch (\Exception $e) { continue; } } } /** @var Stock[] $stockEntities */ $stockEntities = $this->findStockInIds(array_keys($stocks)); foreach ($stockEntities as $stockEntity) { if (isset($stocks[$stockEntity->getId()])) { $quantity = $stocks[$stockEntity->getId()]; $ratio = $stockEntity->getStockItem()->getStorageUnit()->getRatio(); $unitQuantity = $quantity * $ratio; if ($unitQuantity) { // create level change $levelChange = new LevelChange(); $levelChange->setStock($stockEntity); $levelChange->setCorrector($corrector); $levelChange->setCurrentLevel($stockEntity->getCurrentLevel()); $levelChange->setType(LevelChange::TYPE_PURCHASE); $levelChange->setDelta($unitQuantity); $levelChange->setAuto(true); $levelChange->setNote(sprintf("#%d (+%s %s)", $purchase->getId(), $quantity, $stockEntity->getStockItem()->getStorageUnit()->getName())); // update stock $stockEntity->setCurrentLevel($stockEntity->getCurrentLevel() + $unitQuantity); $stockEntity->setLastChange(new \DateTime()); if (isset($unitPrices[$stockEntity->getId()])) { $stockEntity->setCurrentUnitPrice($unitPrices[$stockEntity->getId()]); } if ($purchase->getOrderedAt() instanceof \DateTime) { $stockEntity->setLastPurchase($purchase->getOrderedAt()); } $this->em()->persist($levelChange); $this->em()->persist($stockEntity); } } } $this->em()->flush(); }