public function updateLeasingReturnQty($data)
 {
     $update = SalesOrderItem::where($this->key_detail, $data->id)->where('type', 2)->get();
     if (!empty($update)) {
         foreach ($update as $key => $value) {
             $params = ['leasing_return_quantity' => $value->quantity];
             if (!$value->update($params)) {
                 DB::rollback();
                 throw $this->response->error('failed_update_leasing_return_quantity', 500);
             }
         }
     }
 }
 public function updateDataBySOItem($data)
 {
     $idSalesOrderItem = $data->id_sales_order_item;
     $salesOrderItem = SalesOrderItem::find($idSalesOrderItem);
     if (count($salesOrderItem) == 0) {
         DB::rollback();
         throw $this->response->error(trans('response.no_data_found'), 404);
     }
     $update = LeasingReturnItem::find($data->id);
     $update = $update->update(['rent_date' => $salesOrderItem->rent_date, 'return_date' => $salesOrderItem->return_date, 'id_item' => $salesOrderItem->id_item, 'id_unit' => $salesOrderItem->id_unit, 'quantity' => $salesOrderItem->leasing_return_quantity]);
     if (!$update) {
         DB::rollback();
         throw $this->response->error(trans('response.save_data_failed'), 404);
     }
 }
 public function UpdateQtyLeasingSo($data)
 {
     //kurangi data di leasing return
     $dataLeasingItem = LeasingReturnItem::where('id_leasing_return', $data->id)->get();
     if (!empty($dataLeasingItem)) {
         foreach ($dataLeasingItem as $key => $value) {
             $updateSO = SalesOrderItem::find($value->id_sales_order_item);
             $updateParam = ['leasing_return_quantity' => DB::raw('leasing_return_quantity-' . ($value->quantity_return + $value->quantity_demaged + $value->quantity_lost))];
             if (!$updateSO->update($updateParam)) {
                 DB::rollback();
                 throw $this->response->error('update_so_item_failed', 500);
             }
         }
     }
 }
 public function validationLeasingReturn()
 {
     Validator::extend('unique_leasing_item', function ($attribute, $value, $parameters, $validator) {
         $idSalesOrderItem = Request::get('id_sales_order_item');
         //cek list dengan status harus 0  kalo sudah 0 berarti sudah di confirm bisa masuk lagi
         $data = LeasingReturnItem::where('id_sales_order_item', $idSalesOrderItem)->join('leasing_returns', 'leasing_return_items.id_leasing_return', '=', 'leasing_returns.id')->where('leasing_returns.status', 0)->where('leasing_return_items.deleted_at');
         // ignore when edit detail
         if (isset(Request::get('detail')['id_detail'])) {
             $data->where('leasing_return_items.id', '!=', Request::get('detail')['id_detail']);
         }
         if ($data->count() > 0) {
             return false;
         }
         return true;
     });
     Validator::extend('exist_in_so', function ($attribute, $value, $parameters, $validator) {
         $idSalesOrder = Request::get('id_sales_order');
         $idSalesOrderItem = Request::get('id_sales_order_item');
         //cek list detail merupakan anak dari so yang dipilih
         $data = SalesOrder::where('sales_orders.id', $idSalesOrder)->join('sales_order_items', 'sales_order_items.id_sales_order', '=', 'sales_orders.id')->where('sales_order_items.id', $idSalesOrderItem)->where('sales_order_items.deleted_at')->count();
         if ($data <= 0) {
             return false;
         }
         return true;
     });
     Validator::extend('so_confirmed', function ($attribute, $value, $parameters, $validator) {
         $idSalesOrder = Request::get('id_sales_order');
         //cek so harus punya status confirm
         $data = SalesOrder::where('sales_orders.id', $idSalesOrder)->join('sales_order_items', 'sales_order_items.id_sales_order', '=', 'sales_orders.id')->where('sales_orders.status', 1)->where('sales_order_items.deleted_at')->count();
         if ($data <= 0) {
             return false;
         }
         return true;
     });
     Validator::extend('lr_before_confirm_destroy_update', function ($attribute, $value, $parameters, $validator) {
         $idLeasingReturn = Request::get('id');
         //cek lr harus punya status open
         $data = LeasingReturn::where('leasing_returns.id', $idLeasingReturn)->where('leasing_returns.status', 0)->count();
         if ($data <= 0) {
             return false;
         }
         return true;
     });
     Validator::extend('lr_before_void', function ($attribute, $value, $parameters, $validator) {
         $idLeasingReturn = Request::get('id');
         //cek lr harus punya status confirm
         $data = LeasingReturn::where('leasing_returns.id', $idLeasingReturn)->where('leasing_returns.status', 1)->count();
         if ($data <= 0) {
             return false;
         }
         return true;
     });
     Validator::extend('max_leasing_item', function ($attribute, $value, $parameters, $validator) {
         $idSoItem = Request::get('id_sales_order_item');
         //cek lr harus punya status confirm
         $data = SalesOrderItem::where('sales_order_items.id', $idSoItem)->first();
         $totalQty = Request::get('quantity_return') + Request::get('quantity_demaged') + Request::get('quantity_lost');
         if ($totalQty <= $data->leasing_return_quantity) {
             return true;
         }
         return false;
     });
 }