Exemplo n.º 1
0
 /**
  * @param        $stockId
  * @param string $typeHistory
  * @param bool   $firstHistory
  *
  * @return int|mixed
  */
 public static function createHistoryStockSample($stockId, $typeHistory = 'in', $firstHistory = true)
 {
     $rollNumber = 'Stock-' . $stockId . rand(1, 1000);
     //Stockproducts::whereOnday($stockId);
     $recordStock = Stockproducts::findOrFail($stockId);
     $totalActive = $recordStock->total;
     $typeHistory = strtolower($typeHistory);
     $qtyIn = rand(1, 200);
     $qtyOut = rand(10, 100);
     if (!in_array($typeHistory, array('in', 'out'))) {
         $typeHistory = 'in';
     }
     /*Jika Qty Keluar Lebih besar dari Total Yang ada */
     if ($totalActive < $qtyOut) {
         $qtyOut = $totalActive;
     }
     /*Jika Total 0 Maka Tidak Bisa mengeluarkan Barang*/
     if ($totalActive == 0) {
         $qtyOut = 0;
     }
     if ($firstHistory) {
         $qtyOut = 0;
         /*Rumus => total =  QtyIn - QtyOut*/
         $total = $qtyIn - $qtyOut;
         $typeHistory = 'in';
     } else {
         /*Rumus => total = (totalsebelumny)+(qtyIn-QtyOut)  */
         $total = $totalActive + ($qtyIn - $qtyOut);
     }
     if ($typeHistory == 'in') {
         /*Jika History Input/Terima Barang*/
         $qtyOut = 0;
         $qtyBalance = $totalActive + $qtyIn;
     } else {
         /*Jika Keluar Barang */
         /* dan akan mengurang balance */
         $qtyIn = 0;
         $qtyBalance = $totalActive + ($qtyIn - $qtyOut);
     }
     $recordHistoryStock = static::create(static::getFake()->getProduct()->createHistoryStockProduct($stockId, $rollNumber, $qtyIn, $qtyOut, $qtyBalance, $total));
     /*Update Total Stock */
     $recordStock->total = $qtyBalance;
     $recordStock->save();
     return $recordHistoryStock->id;
 }