Ejemplo n.º 1
0
 /**
  *
  * Создает новый трансфер с указанным списком товаров, но не проводит его
  * Если указанно, то создает резерв товара
  *
  * @param ProductQtyCollection $collection
  * @param Warehouse $source
  * @param Warehouse $target
  * @param boolean $reserve //зарезервировать товары?
  * @return StockTransfer
  */
 public function createTransfer(ProductQtyCollection $collection, Warehouse $source, Warehouse $target, $reserve = false)
 {
     if ($source->organization()->id != $target->organization()->id) {
         throw new StockException('Для перемещения между организациями, используйте метод StockRepository::createOrganisationsTransfer');
     }
     $transfer = new StockTransfer();
     $organization = $source->organization();
     $transfer->sourceOrganization()->associate($organization);
     $transfer->targetOrganization()->associate($organization);
     $transfer->sourceWarehouse()->associate($source);
     $transfer->targetWarehouse()->associate($target);
     $transfer->save();
     foreach ($collection as $row) {
         $productId = $row['id'];
         $qty = $row['qty'];
         $product = Product::findOrFail($productId);
         $stock = $this->stockRepository->findStockByProductAndWarehouse($product, $source);
         if (empty($stock)) {
             throw new StockNotFound();
         }
         $item = new StockTransferItem();
         $item->product()->associate($product);
         $item->stock()->associate($stock);
         $item->setQty($qty);
         $item->save();
     }
     if ($reserve === true) {
         $transfer->is_reserved = true;
         $rep = new ReserveRepository();
         $rep->createByDocument($transfer);
     }
     return $transfer;
 }
Ejemplo n.º 2
0
 /**
  * Провести документ
  *
  * @return $this
  * @throws StockException
  */
 public function activate()
 {
     if ($this->is_activated) {
         throw new StockException('Документ уже проведен');
     }
     if (!$this->is_reserved) {
         $rep = new ReserveRepository();
         $rep->createByDocument($this);
     }
     $items = $this->items();
     foreach ($items as $item) {
         //@todo проверить существование стоков
         $source = $item->sourceStock();
         $target = $item->targetStock();
         $product = $item->product();
         $qty = $item->qty;
         if (!$source->checkAvailable($qty)) {
             throw new StockException('Недостаточно кол-ва для трансфера');
         }
         $source->decreaseQty($qty);
         $target->increaseQty($qty);
         $source->save();
         $target->save();
     }
     $this->status = self::STATUS_ACTIVATED;
     $this->calcTotals();
     $this->save();
     return $this;
 }