예제 #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;
 }