Beispiel #1
0
 /**
  * Handle database operations to edit a bill product.
  *
  * @param array $data
  *      @option int billId
  *      @option int productId
  *      @option int billProductId
  *      @option string productCode
  *      @option string columnToUpdate
  *      @option string newValue
  * @return mixed
  */
 public static function handleBillProductEdit($data = [])
 {
     $response = new AjaxResponse();
     // Query for bill
     $bill = Auth::user()->bills()->where('id', $data['billId'])->first();
     // Now make sure exists in database
     if (!$bill) {
         $response->setFailMessage(trans('common.general_error'));
         return response($response->get(), $response->getDefaultErrorResponseCode())->header('Content-Type', 'application/json');
     }
     // Make sure bill product exists and belongs to current user
     if (!Product::where('id', $data['productId'])->count() && !ApplicationProduct::where('id', $data['productId'])->count()) {
         $response->setFailMessage(trans('bill.product_not_found'));
         return response($response->get(), 404)->header('Content-Type', 'application/json');
     }
     // Make sure bill product belongs to current user
     if (!BillProduct::where('id', $data['billProductId'])->count() && !BillApplicationProduct::where('id', $data['billProductId'])->count()) {
         $response->setFailMessage(trans('bill.bill_product_not_found'));
         return response($response->get(), 404)->header('Content-Type', 'application/json');
     }
     // We will use this variable to check if operation was successful
     $success = false;
     // Check if is a custom product
     if (Products::isCustomProduct($data['productId'], $data['productCode'])) {
         // Get product details and update with new data
         $product = BillProduct::where('id', $data['billProductId'])->first();
         BillProduct::where('id', $data['billProductId'])->update(Bills::getDataToUpdateOnEdit($data['columnToUpdate'], $data['newValue'], $product));
         $success = true;
     }
     // Check if is an application product
     if (Products::isApplicationProduct($data['productId'], $data['productCode'])) {
         // Get product details and update with new data
         $product = BillApplicationProduct::where('id', $data['billProductId'])->first();
         BillApplicationProduct::where('id', $data['billProductId'])->update(Bills::getDataToUpdateOnEdit($data['columnToUpdate'], $data['newValue'], $product));
         $success = true;
     }
     // Check if update was successful
     if ($success) {
         $response->setSuccessMessage(trans('bill.' . $data['columnToUpdate'] . '_updated'));
         return response($response->get())->header('Content-Type', 'application/json');
     }
     // If we arrive here something is wrong
     $response->setFailMessage(trans('common.general_error'));
     return response($response->get(), $response->getDefaultErrorResponseCode())->header('Content-Type', 'application/json');
 }