Пример #1
0
 public function adjustDuringArrival($stock_id, $doses, $item_id, $lot_number, $original_amount)
 {
     $stock = Stock::find($stock_id);
     $amount = $stock->amount + ($original_amount - $doses);
     $storeStock = StoreStock::where('store_id', $stock->store_id)->where('vaccine_id', $item_id)->where('lot_number', $lot_number)->first();
     //        $stock->amount = $doses;
     //        $storeStock->amount = $doses;
     $volume = ($original_amount - $doses) * PackagingInformation::find($stock->packaging_id)->cm_per_dose * 0.001;
     //        $current_occupied_volume = $amount * PackagingInformation::find($stock->packaging_id)->cm_per_dose * 0.001;
     //        $volume_to_use = $current_occupied_volume - $volume;
     //        $stock->save();
     //        $storeStock->save();
     //        if($stock->amount == 0){
     //            $stock->delete();
     //        }
     //        if($storeStock->amount == 0){
     //            $storeStock->delete();
     //        }
     //updating volumes
     //        $store = Store::find($stock->store_id);
     //        $store->used_volume = $store->used_volume - $volume_to_use ;
     //        $store->save();
     $reason = AdjustmentReason::where('code', 'AR')->first();
     if ($reason) {
         $adjustment_reason = $reason->id;
     } else {
         $adjustment_reason = 1;
     }
     $adjustment = new Adjustment();
     $nextNumber = $this->getNextAdjustmentNumber();
     $str = "";
     for ($sj = 6; $sj > strlen($nextNumber); $sj--) {
         $str .= "0";
     }
     $adjustment->reference = date('Y') . "3" . $str + "" . $nextNumber;
     $adjustment->adjustment_reason = $adjustment_reason;
     $adjustment->adjustment_type = 'stock';
     $adjustment->resource_id = $stock->id;
     $adjustment->year = date('Y');
     $adjustment->order_no = $nextNumber;
     $adjustment->vaccine_id = $stock->vaccine_id;
     $adjustment->store_id = $stock->store_id;
     $adjustment->lot_number = $stock->lot_number;
     $adjustment->packaging_id = $stock->packaging_id;
     $adjustment->expiry_date = $stock->expiry_date;
     $adjustment->source_id = $stock->source_id;
     $adjustment->activity_id = $stock->activity_id;
     $adjustment->recipient_id = $stock->recipient_id;
     $adjustment->previous_amount = $amount;
     $adjustment->current_amount = $stock->amount;
     $adjustment->adjusted_volume = abs($volume);
     $adjustment->save();
     Log::create(array("user_id" => Auth::user()->id, "action" => "Stock Adjustment with reference Number " . $adjustment->reference));
     return $adjustment->reference;
 }
Пример #2
0
 /**
  * get adjustedItems.
  *@param $recipient
  * @param $level
  * @return Response
  */
 public function adjustedItems1($recipient, $level)
 {
     if ($level == '1') {
         return Adjustment::where('recipient_id', $recipient)->get();
     } elseif ($level == '2') {
         $orgunit = Recipient::find($recipient);
         $arr = [0, $recipient];
         array_push($arr, $recipient);
         foreach ($orgunit->childrens as $val) {
             array_push($arr, $val->id);
         }
         $dispatch = Adjustment::whereIn('recipient_id', $arr)->get();
     } elseif ($level == '3') {
         $orgunit = Recipient::find($recipient);
         $arr = [0];
         array_push($arr, $recipient);
         foreach ($orgunit->childrens as $val) {
             array_push($arr, $val->id);
             $orgunit1 = Recipient::find($val->id);
             foreach ($orgunit1->childrens as $val) {
                 array_push($arr, $val->id);
             }
         }
         $dispatch = Adjustment::whereIn('recipient_id', $arr)->get();
     } else {
         return Adjustment::where('recipient_id', $recipient)->get();
     }
     return $dispatch;
 }
 public function store(Request $request)
 {
     $denied = false;
     if (!$request->session()->has('data_adjustment')) {
         $denied = true;
     } else {
         // Reduction ( Pengurangan )
         $data_adjustment_reduction = $request->session()->get('data_adjustment.reduction');
         $data_adjustment_reduction_bahan = isset($data_adjustment_reduction['bahan']) ? $data_adjustment_reduction['bahan'] : [];
         $data_adjustment_reduction_produk = isset($data_adjustment_reduction['produk']) ? $data_adjustment_reduction['produk'] : [];
         // Increase ( Penambahan )
         $data_adjustment_increase = $request->session()->get('data_adjustment.increase');
         $data_adjustment_increase_bahan = isset($data_adjustment_increase['bahan']) ? $data_adjustment_increase['bahan'] : [];
         $data_adjustment_increase_produk = isset($data_adjustment_increase['produk']) ? $data_adjustment_increase['produk'] : [];
         if (empty($data_adjustment_reduction_bahan) && empty($data_adjustment_reduction_produk) && empty($data_adjustment_increase_bahan) && empty($data_adjustment_increase_produk)) {
             $denied = true;
         }
     }
     if ($denied) {
         return redirect('adjustment/add')->withInput()->withErrors(['no_details' => 'Tidak ada barang yang di adjustment.']);
     }
     // Adjustment
     $karyawan_id = Auth::check() ? Auth::user()->karyawan->id : '1';
     $input = $request->only(['keterangan', 'tanggal']) + ['karyawan_id' => $karyawan_id];
     $adjustment = Adjustment::create($input);
     # Update Data [Bahan => Harga, Produk => HPP]
     // Bahan
     if (!empty($data_adjustment_increase_bahan)) {
         $keys = array_keys($data_adjustment_increase_bahan);
         $bahans = \App\StokBahan::with(['bahan'])->whereIn('bahan_id', $keys)->orderBy('bahan_id')->get();
         foreach ($bahans as $bahan) {
             $bId = $bahan->bahan_id;
             $inStok = $data_adjustment_increase_bahan[$bId]['qty'];
             $inHarga = $data_adjustment_increase_bahan[$bId]['harga'];
             if ($bahan->bahan->harga != $inHarga) {
                 $oldTtl = $bahan->stok > 0 ? $bahan->bahan->harga * $bahan->stok : 0;
                 $inTtl = $inStok > 0 ? $inHarga * $inStok : 0;
                 $sumInOld = $oldTtl + $inTtl;
                 $qtyTotal = $bahan->stok + $inStok;
                 $harga = $sumInOld > 0 && $qtyTotal > 0 ? $sumInOld / $qtyTotal : 0;
                 if ($harga != $bahan->bahan->harga) {
                     \App\Bahan::find($bId)->update(['harga' => $harga]);
                     \App\AveragePriceAction::create(['type' => 'bahan', 'relation_id' => $bId, 'old_price' => $bahan->bahan->harga, 'old_stok' => $bahan->stok, 'input_price' => $inHarga, 'input_stok' => $inStok, 'average_with_round' => $harga, 'action' => "Adjustment Increase #" . $adjustment->id]);
                 }
             }
         }
     }
     // Produk, Harga => HPP
     if (!empty($data_adjustment_increase_produk)) {
         $keys = array_keys($data_adjustment_increase_produk);
         $produks = \App\StokProduk::with(['produk'])->whereIn('produk_id', $keys)->orderBy('produk_id')->get();
         foreach ($produks as $produk) {
             $pId = $produk->produk_id;
             $inStok = $data_adjustment_increase_produk[$pId]['qty'];
             $inHarga = $data_adjustment_increase_produk[$pId]['harga'];
             if ($produk->produk->hpp != $inHarga) {
                 $oldTtl = $produk->stok > 0 ? $produk->produk->hpp * $produk->stok : 0;
                 $inTtl = $inStok > 0 ? $inHarga * $inStok : 0;
                 $sumInOld = $oldTtl + $inTtl;
                 $qtyTotal = $produk->stok + $inStok;
                 $harga = $sumInOld > 0 && $qtyTotal > 0 ? $sumInOld / $qtyTotal : 0;
                 // HPP
                 if ($harga != $produk->produk->hpp) {
                     \App\Produk::find($pId)->update(['hpp' => $harga]);
                     \App\AveragePriceAction::create(['type' => 'produk', 'relation_id' => $pId, 'old_price' => $produk->produk->hpp, 'old_stok' => $produk->stok, 'input_price' => $inHarga, 'input_stok' => $inStok, 'average_with_round' => $harga, 'action' => "Adjustment Increase #" . $adjustment->id]);
                 }
             }
         }
     }
     // Adjustment Detail
     $data_adjustment = array_merge($data_adjustment_reduction_bahan, $data_adjustment_reduction_produk);
     $data_adjustment = array_merge($data_adjustment_increase_bahan, $data_adjustment);
     $data_adjustment = array_merge($data_adjustment_increase_produk, $data_adjustment);
     $details = [];
     foreach ($data_adjustment as $da) {
         $temp = $da;
         $temp['adjustment_id'] = $adjustment->id;
         array_push($details, $temp);
     }
     AdjustmentDetail::insert($details);
     Artisan::call('bahan:count');
     Artisan::call('produk:count');
     $request->session()->forget('data_adjustment');
     $request->session()->forget('info_adjustment');
     return redirect('/adjustment')->with('succcess', 'Sukses simpan adjustment bahan / produk.');
 }