Example #1
0
 /**
  * Add new product to given bill.
  *
  * @param int $billId
  * @param array $inputs
  * @return mixed
  */
 public static function insertProduct($billId, $inputs)
 {
     // Create ajax response instance
     $response = new AjaxResponse();
     // Check if bill belongs to current user
     if (!Bills::belongsToAuthUser($billId)) {
         $response->setFailMessage('');
         return response($response->get(), $response->getDefaultErrorResponseCode())->header('Content-Type', 'application/json');
     }
     $applicationProduct = ApplicationProduct::where('code', $inputs['product_code'])->first();
     $customProduct = Product::where('code', $inputs['product_code'])->where('user_id', Auth::user()->id)->first();
     // Check if products exists and if is an application product or custom product
     if ($applicationProduct) {
         // Get application product and create db table instance
         $product = $applicationProduct;
         $query = DB::table('bill_application_products');
     } else {
         if ($customProduct) {
             // Query for custom product and create db table instance
             $product = $customProduct;
             $query = DB::table('bill_products');
         } else {
             // Product does not exists
             $response->setFailMessage(trans('bill.product_not_found'));
             $response->addExtraFields(['product_not_exists' => true]);
             return response($response->get(), $response->getDefaultErrorResponseCode())->header('Content-Type', 'application/json');
         }
     }
     $inputs['bill_id'] = $billId;
     self::handleProductInsert($product, $inputs, $query);
     $response->setSuccessMessage(trans('bill.product_added_successfully'));
     return response($response->get())->header('Content-Type', 'application/json');
 }
Example #2
0
 /**
  * Edit bill payment term.
  *
  * @param int $billId
  * @param EditPaymentTermRequest $request
  * @return mixed
  */
 public function editPaymentTerm($billId, EditPaymentTermRequest $request)
 {
     return Bills::updatePaymentTerm($billId, $request->get('payment_term'));
 }
Example #3
0
 /**
  * @param int $userId
  * @param GetUserPaidBillsRequest $request
  * @return mixed
  */
 public function getUserPaidBills($userId, GetUserPaidBillsRequest $request)
 {
     // Make sure user exists
     if (!User::where('id', $userId)->count()) {
         $response = new AjaxResponse();
         $response->setFailMessage(trans('users_manager.user_not_found'));
         return response($response->get())->header('Content-Type', 'application/json');
     }
     return Bills::get(true, $userId);
 }
Example #4
0
 public function get()
 {
     return Bills::get(true);
 }
Example #5
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');
 }
Example #6
0
 public function search(SearchBillsRequest $request)
 {
     $config = ['searchTerm' => $request->get('term'), 'page' => $request->get('page')];
     return Bills::get($config);
 }