PageRenderTime 1337ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/api/app/controllers/spree/api/v1/shipments_controller.rb

http://github.com/spree/spree
Ruby | 186 lines | 159 code | 27 blank | 0 comment | 11 complexity | 199368084ed73eb7f3aae7e1edd3485e MD5 | raw file
Possible License(s): BSD-3-Clause
  1. module Spree
  2. module Api
  3. module V1
  4. class ShipmentsController < Spree::Api::BaseController
  5. before_action :find_and_update_shipment, only: [:ship, :ready, :add, :remove]
  6. before_action :load_transfer_params, only: [:transfer_to_location, :transfer_to_shipment]
  7. def mine
  8. if current_api_user.persisted?
  9. @shipments = Spree::Shipment.
  10. reverse_chronological.
  11. joins(:order).
  12. where(spree_orders: { user_id: current_api_user.id }).
  13. includes(mine_includes).
  14. ransack(params[:q]).result.page(params[:page]).per(params[:per_page])
  15. else
  16. render 'spree/api/errors/unauthorized', status: :unauthorized
  17. end
  18. end
  19. def create
  20. @order = Spree::Order.find_by!(number: params.fetch(:shipment).fetch(:order_id))
  21. authorize! :show, @order
  22. authorize! :create, Shipment
  23. quantity = params[:quantity].to_i
  24. @shipment = @order.shipments.create(stock_location_id: params.fetch(:stock_location_id))
  25. @line_item = Spree::Dependencies.cart_add_item_service.constantize.call(order: @order,
  26. variant: variant,
  27. quantity: quantity,
  28. options: { shipment: @shipment }).value
  29. respond_with(@shipment.reload, default_template: :show)
  30. end
  31. def update
  32. @shipment = Spree::Shipment.accessible_by(current_ability, :update).readonly(false).find_by!(number: params[:id])
  33. @shipment.update_attributes_and_order(shipment_params)
  34. respond_with(@shipment.reload, default_template: :show)
  35. end
  36. def ready
  37. unless @shipment.ready?
  38. if @shipment.can_ready?
  39. @shipment.ready!
  40. else
  41. render 'spree/api/v1/shipments/cannot_ready_shipment', status: 422 and return
  42. end
  43. end
  44. respond_with(@shipment, default_template: :show)
  45. end
  46. def ship
  47. @shipment.ship! unless @shipment.shipped?
  48. respond_with(@shipment, default_template: :show)
  49. end
  50. def add
  51. quantity = params[:quantity].to_i
  52. Spree::Dependencies.cart_add_item_service.constantize.call(order: @shipment.order,
  53. variant: variant,
  54. quantity: quantity,
  55. options: { shipment: @shipment })
  56. respond_with(@shipment, default_template: :show)
  57. end
  58. def remove
  59. quantity = if params.key?(:quantity)
  60. params[:quantity].to_i
  61. else
  62. @shipment.inventory_units_for(variant).sum(:quantity)
  63. end
  64. Spree::Dependencies.cart_remove_item_service.constantize.call(order: @shipment.order,
  65. variant: variant,
  66. quantity: quantity,
  67. options: { shipment: @shipment })
  68. if @shipment.inventory_units.any?
  69. @shipment.reload
  70. else
  71. @shipment.destroy!
  72. end
  73. respond_with(@shipment, default_template: :show)
  74. end
  75. def transfer_to_location
  76. @stock_location = Spree::StockLocation.find(params[:stock_location_id])
  77. unless @quantity > 0
  78. unprocessable_entity("#{Spree.t(:shipment_transfer_errors_occured, scope: 'api')} \n #{Spree.t(:negative_quantity, scope: 'api')}")
  79. return
  80. end
  81. @original_shipment.transfer_to_location(@variant, @quantity, @stock_location)
  82. render json: { success: true, message: Spree.t(:shipment_transfer_success) }, status: 201
  83. end
  84. def transfer_to_shipment
  85. @target_shipment = Spree::Shipment.find_by!(number: params[:target_shipment_number])
  86. error =
  87. if @quantity < 0 && @target_shipment == @original_shipment
  88. "#{Spree.t(:negative_quantity, scope: 'api')}, \n#{Spree.t('wrong_shipment_target', scope: 'api')}"
  89. elsif @target_shipment == @original_shipment
  90. Spree.t(:wrong_shipment_target, scope: 'api')
  91. elsif @quantity < 0
  92. Spree.t(:negative_quantity, scope: 'api')
  93. end
  94. if error
  95. unprocessable_entity("#{Spree.t(:shipment_transfer_errors_occured, scope: 'api')} \n#{error}")
  96. else
  97. @original_shipment.transfer_to_shipment(@variant, @quantity, @target_shipment)
  98. render json: { success: true, message: Spree.t(:shipment_transfer_success) }, status: 201
  99. end
  100. end
  101. private
  102. def load_transfer_params
  103. @original_shipment = Spree::Shipment.find_by!(number: params[:original_shipment_number])
  104. @variant = Spree::Variant.find(params[:variant_id])
  105. @quantity = params[:quantity].to_i
  106. authorize! :show, @original_shipment
  107. authorize! :create, Shipment
  108. end
  109. def find_and_update_shipment
  110. @shipment = Spree::Shipment.accessible_by(current_ability, :update).readonly(false).find_by!(number: params[:id])
  111. @shipment.update(shipment_params)
  112. @shipment.reload
  113. end
  114. def shipment_params
  115. if params[:shipment] && !params[:shipment].empty?
  116. params.require(:shipment).permit(permitted_shipment_attributes)
  117. else
  118. {}
  119. end
  120. end
  121. def variant
  122. @variant ||= Spree::Variant.unscoped.find(params.fetch(:variant_id))
  123. end
  124. def mine_includes
  125. {
  126. order: {
  127. bill_address: {
  128. state: {},
  129. country: {}
  130. },
  131. ship_address: {
  132. state: {},
  133. country: {}
  134. },
  135. adjustments: {},
  136. payments: {
  137. order: {},
  138. payment_method: {}
  139. }
  140. },
  141. inventory_units: {
  142. line_item: {
  143. product: {},
  144. variant: {}
  145. },
  146. variant: {
  147. product: {},
  148. default_price: {},
  149. option_values: {
  150. option_type: {}
  151. }
  152. }
  153. }
  154. }
  155. end
  156. end
  157. end
  158. end
  159. end