shipping_backup_client 1.0.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,606 @@
1
+ # == Schema Information
2
+ #
3
+ # Table name: shipments
4
+ #
5
+ # id :integer not null, primary key
6
+ # type :string(255)
7
+ # delivery_type :string(255)
8
+ # state :string(255)
9
+ # sla :integer
10
+ # merchant_facility_id :integer
11
+ # fkl_origin_facility_id :integer
12
+ # fkl_destination_facility_id :integer
13
+ # vendor_facility_id :integer
14
+ # payment_type :string(255)
15
+ # estimated_ship_cost :decimal(10, 2)
16
+ # shipment_value :decimal(10, 2)
17
+ # tax_paid :decimal(10, 2)
18
+ # merchant_code :string(255)
19
+ # customer_id :integer
20
+ # vendor_id :integer
21
+ # merchant_reference_id :string(255)
22
+ # vendor_tracking_id :string(255)
23
+ # service_instructions :string(4096)
24
+ # special_instructions :string(4096)
25
+ # postal_code :string(255)
26
+ # flags :integer default(0)
27
+ # created_at :datetime
28
+ # updated_at :datetime
29
+ # fkl_current_facility_id :integer
30
+ # dispatched_date :datetime
31
+ # vendor_receive_date :datetime
32
+ # amount_to_collect :decimal(10, 2) default(0.0)
33
+ # fkl_current_station_id :integer
34
+ # request_type :string(255)
35
+ #
36
+
37
+ class ShipmentDAO < ActiveRecord::Base
38
+ self.table_name = "shipments"
39
+
40
+ LZN_MAP = { "l" => "local", "z" => "zonal", "n" => "national" }
41
+
42
+ acts_as_tenant :merchant_code
43
+
44
+ has_many :shipment_items
45
+ has_many :shipment_status_histories, :class_name => 'ShipmentStatusHistoryDAO', :foreign_key => :shipment_id
46
+ has_many :shipment_attributes
47
+ has_many :shipment_segment_histories
48
+ has_many :shipment_delivery_details
49
+ has_many :shipment_scan_audits
50
+ has_many :shipment_notes
51
+ has_many :shipment_form_details
52
+ has_many :vendor_service_requests
53
+ has_one :shipment_consolidation_info
54
+ has_one :rto_detail
55
+ has_one :cs_ticket
56
+ has_one :shipment_dimension
57
+ has_one :shipment_charge
58
+ has_one :ekart_shipment_reference
59
+ has_one :reshipment_mapping, :class_name => "ReshipmentMapping", :foreign_key => :new_shipment_id
60
+ has_one :old_shipment, :through => :reshipment_mapping, :source => :old_shipment
61
+
62
+ has_one :original_shipment, :class_name => "ReshipmentMapping", :foreign_key => :old_shipment_id
63
+ has_one :new_shipment, :through => :original_shipment, :source => :new_shipment
64
+
65
+ has_one :mp_3pl_shipment_mappings, :class_name => "MarketPlace3plShipmentMapping", :foreign_key => :pickup_shipment_id
66
+ has_one :mp_3pl_delivery_shipment, :through => :mp_3pl_shipment_mappings, :source => :mp_3pl_delivery_shipment
67
+
68
+ has_one :mp_3pl_delivery_shipment_mapping, :class_name => "MarketPlace3plShipmentMapping", :foreign_key => :delivery_shipment_id
69
+ has_one :mp_3pl_pickup_shipment, :through => :mp_3pl_delivery_shipment_mapping, :source => :mp_3pl_pickup_shipment
70
+
71
+ has_one :latest_scan_audit, :class_name => "ShipmentScanAudit", :conditions => ["scanning_type = 'inscan' and status in ('received','received_with_error')"], :order => "id DESC"
72
+ has_one :first_scan_audit, :class_name => "ShipmentScanAudit", :conditions => ["scanning_type = 'inscan' and status in ('received','received_with_error')"], :order => "id"
73
+
74
+ has_one :shipment_seller_mappings, :class_name => 'ShipmentSellerMappingDAO', :foreign_key => :shipment_id
75
+ has_one :seller, :through => :shipment_seller_mappings, :source => :seller
76
+
77
+ has_one :shipment_return_seller_mappings, :class_name => 'ShipmentReturnSellerMappingsDAO', :foreign_key => :shipment_id
78
+ has_one :return_seller, :through => :shipment_return_seller_mappings, :source => :return_seller
79
+
80
+ has_one :shipment_origin_location_mapping
81
+ has_one :origin_location, :through => :shipment_origin_location_mapping
82
+
83
+ has_one :print_label
84
+
85
+ belongs_to :customer
86
+
87
+ has_one :seller_new_shipment_mapping, :class_name => "MarketplaceReturnShipmentMapping", :foreign_key => :new_shipment_id
88
+ has_one :seller_old_shipment, :through => :seller_new_shipment_mapping, :source => :seller_old_shipment
89
+
90
+ has_one :seller_old_shipment_mapping, :class_name => "MarketplaceReturnShipmentMapping", :foreign_key => :old_shipment_id
91
+ has_one :seller_new_shipment, :through => :seller_old_shipment_mapping, :source => :seller_new_shipment
92
+
93
+ validates_presence_of :shipment_value
94
+ validates_presence_of :estimated_ship_cost
95
+ validates_presence_of :tax_paid
96
+ validates_presence_of :postal_code
97
+ validates_presence_of :merchant_code
98
+ validates_presence_of :merchant_reference_id
99
+ validates_presence_of :fkl_destination_facility_id
100
+ validates_presence_of :fkl_origin_facility_id
101
+
102
+ scope :by_state, lambda { |state| where(:state => state) if state }
103
+ scope :by_vendor, lambda { |vendor_id| where(:vendor_id => vendor_id) if vendor_id }
104
+ scope :by_merchant, lambda { |merchant_code| where(:merchant_code => merchant_code) if merchant_code }
105
+ scope :by_fkl_facility, lambda { |facility_id| where(:fkl_current_facility_id => facility_id) if facility_id }
106
+ scope :by_fkl_origin_facility, lambda { |facility_id| where(:fkl_origin_facility_id => facility_id) if facility_id }
107
+ scope :by_fkl_station, lambda { |station_id| where(:fkl_current_station_id => station_id) if station_id }
108
+ scope :by_type, lambda { |type| where(:type => type) if type }
109
+ scope :by_request_type, lambda { |request_type| where(:request_type => request_type) if request_type }
110
+ attr_accessor :is_fsd_outscanned
111
+
112
+ NON_FA_SERVICE_TYPE = "non_fa"
113
+ FOUR_HOUR_DELIVERY_HUB = "blr_hsr_01"
114
+
115
+ def as_json(options = {})
116
+ super.merge!(:pending_since => pending_since, :shipment_type => type)
117
+ end
118
+
119
+ def self.created_before(end_time)
120
+ end_time = DateTime.parse(end_time.to_s).strftime("%Y-%m-%d %H:%M:%S") if end_time.present?
121
+ end_time.present? ? where("shipments.created_at < '#{end_time}'") : Shipment.scoped
122
+ end
123
+
124
+ def self.created_after(start_time)
125
+ start_time = DateTime.parse(start_time.to_s).strftime("%Y-%m-%d %H:%M:%S") if start_time.present?
126
+ start_time.present? ? where("shipments.created_at >= '#{start_time}'") : Shipment.scoped
127
+ end
128
+
129
+ def self.by_cs_status(status)
130
+ status ? joins(:cs_ticket).where('cs_tickets.status' => status) : Shipment.scoped
131
+ end
132
+
133
+ def self.dispatched_before(days)
134
+ days ? where("shipments.dispatched_date <= '#{(Time.now + 1.day).midnight - days.days}'") : Shipment.scoped
135
+ end
136
+
137
+ def self.dispatched_after(start_time)
138
+ start_time = DateTime.parse(start_time.to_s).strftime("%Y-%m-%d %H:%M:%S") if start_time.present?
139
+ start_time.present? ? where("shipments.dispatched_date >= '#{start_time}'") : Shipment.scoped
140
+ end
141
+
142
+ def self.dispatched_bef(end_time)
143
+ end_time = DateTime.parse(end_time.to_s).strftime("%Y-%m-%d %H:%M:%S") if end_time.present?
144
+ end_time.present? ? where("shipments.dispatched_date < '#{end_time}'") : Shipment.scoped
145
+ end
146
+
147
+ def self.dispatched_between(start_time, end_time)
148
+ start_time.present? && end_time.present? ?
149
+ where("shipments.dispatched_date BETWEEN ? and ?", start_time, end_time) : Shipment.scoped
150
+ end
151
+
152
+ def self.created_after(start_time)
153
+ start_time = DateTime.parse(start_time.to_s).strftime("%Y-%m-%d %H:%M:%S") if start_time.present?
154
+ start_time.present? ? where("shipments.created_at >= '#{start_time}'") : Shipment.scoped
155
+ end
156
+
157
+ def self.created_before(end_time)
158
+ end_time = DateTime.parse(end_time.to_s).strftime("%Y-%m-%d %H:%M:%S") if end_time.present?
159
+ end_time.present? ? where("shipments.created_at < '#{end_time}'") : Shipment.scoped
160
+ end
161
+
162
+ def self.dispatched_before_time(days)
163
+ days ? where("shipments.dispatched_date <= '#{Time.now - days.days}'") : Shipment.scoped
164
+ end
165
+
166
+ def self.by_dispatched_date(dispatched_date)
167
+ where("shipments.dispatched_date #{self.build_date_filter(dispatched_date)}")
168
+ end
169
+
170
+ def self.undelivered
171
+ where("shipments.state in ('undelivered_attempted','undelivered_unattempted')")
172
+ end
173
+
174
+ def self.received
175
+ where("(shipments.state in ('#{Shipment::States::RECEIVED}','#{Shipment::States::RECEIVED_WITH_ERROR}'))")
176
+ end
177
+
178
+ def self.pending_for(days)
179
+ days ? dispatched_before(days).undelivered : Shipment.scoped
180
+ end
181
+
182
+ def self.by_city(name)
183
+ name ? joins(:customer => :postal_addresses).where("postal_addresses.city = '#{name}'").group("shipments.id") : Shipment.scoped
184
+ end
185
+
186
+ def self.by_limit(per_page)
187
+ per_page ? limit(per_page) : Shipment.scoped
188
+ end
189
+
190
+ def self.by_offset(page, offset)
191
+ page ? offset(offset) : Shipment.scoped
192
+ end
193
+
194
+ def self.by_rto_type(type)
195
+ type ? (type == 'merchant_reship' ? where("(flags & 32) =32") : rto_detail.merge(RtoDetail.by_type(type))) : Shipment.scoped
196
+ end
197
+
198
+ def self.by_unreceived_rto()
199
+ joins("LEFT JOIN `rto_details` on rto_details.shipment_id = shipments.id").where(" rto_details.received_date is null OR rto_details.received_date < shipments.dispatched_date")
200
+ end
201
+
202
+ def self.reshipped_rto(from_date, to_date)
203
+ where_clause = rto_detail.join_shipment_status_history_with_history_status_date_index.merge(by_state(States::RESHIPPED)).where("rto_details.id is NOT NULL").merge(RtoDetail.by_status(RtoDetail::Statuses::COMPLETED)).merge(ShipmentStatusHistory.shipment_histories.where("new_status = '#{States::RESHIPPED}'")).group("shipments.id")
204
+ from_date || to_date ? where_clause.merge(ShipmentStatusHistory.from_date(from_date)).merge(ShipmentStatusHistory.to_date(to_date)) : where_clause.where("shipment_status_histories.status_date #{self.build_date_filter(Time.now.to_date.to_s)}")
205
+ end
206
+
207
+ def self.returned_rto(from_date, to_date)
208
+ where_clause = rto_detail.join_shipment_status_history_with_history_status_date_index.where("shipments.state IN ('#{States::DISPATCHED_TO_MERCHANT}','#{States::DISPATCHED_TO_FACILITY}','#{States::DISPATCHED_TO_SELLER}')").where("rto_details.id is NOT NULL").merge(RtoDetail.by_statuses([RtoDetail::Statuses::COMPLETED, RtoDetail::Statuses::RECEIVED])).merge(ShipmentStatusHistory.shipment_histories.where("new_status IN ('#{States::DISPATCHED_TO_MERCHANT}','#{States::DISPATCHED_TO_FACILITY}','#{States::DISPATCHED_TO_SELLER}')")).group("shipments.id")
209
+ from_date || to_date ? where_clause.merge(ShipmentStatusHistory.from_date(from_date)).merge(ShipmentStatusHistory.to_date(to_date)) : where_clause.where("shipment_status_histories.status_date #{self.build_date_filter(Time.now.to_date.to_s)}")
210
+ end
211
+
212
+ def self.pending_today_rto(from_date, to_date)
213
+ where_clause = rto_detail.where("shipments.state IN ('#{States::RECEIVED}','#{States::RECEIVED_WITH_ERROR}','#{States::MARKED_FOR_MERCHANT_DISPATCH}','#{States::MARKED_FOR_RESHIPMENT}','#{States::MARKED_FOR_SELLER_RETURN}')").where("rto_details.id is NOT NULL").merge(RtoDetail.by_status(RtoDetail::Statuses::RECEIVED))
214
+ from_date || to_date ? where_clause.merge(RtoDetail.by_rto_received_from_date(from_date)).merge(RtoDetail.by_rto_received_to_date(to_date)) : where_clause.merge(RtoDetail.by_rto_received_from_date(Time.now.to_date.to_s)).merge(RtoDetail.by_rto_received_to_date(Time.now.to_date.to_s))
215
+ end
216
+
217
+ def self.pending_till_yesterday_rto(from_date)
218
+ where_clause = rto_detail.where("shipments.state IN ('#{States::RECEIVED}','#{States::RECEIVED_WITH_ERROR}','#{States::MARKED_FOR_MERCHANT_DISPATCH}','#{States::MARKED_FOR_RESHIPMENT}','#{States::MARKED_FOR_SELLER_RETURN}')").where("rto_details.id is NOT NULL").merge(RtoDetail.by_status(RtoDetail::Statuses::RECEIVED))
219
+ from_date ? where_clause.merge(RtoDetail.before_date(from_date)) : where_clause.merge(RtoDetail.before_date(Time.now.to_date.to_s))
220
+ end
221
+
222
+ def self.received_by_merchant_rto(from_date, to_date)
223
+ where_clause = rto_detail.join_shipment_status_history_with_history_status_date_index.where("shipments.state IN ('#{States::RECEIVED_BY_MERCHANT}','#{States::RETURNED_TO_SELLER}') ").where("rto_details.id is NOT NULL").merge(RtoDetail.by_status(RtoDetail::Statuses::COMPLETED)).merge(ShipmentStatusHistory.shipment_histories.where("new_status IN ('#{States::RECEIVED_BY_MERCHANT}','#{States::RETURNED_TO_SELLER}')")).group("shipments.id")
224
+ from_date || to_date ? where_clause.merge(ShipmentStatusHistory.from_date(from_date)).merge(ShipmentStatusHistory.to_date(to_date)) : where_clause.where("shipment_status_histories.status_date #{self.build_date_filter(Time.now.to_date.to_s)}")
225
+ end
226
+
227
+ def self.rto_expected(from_date, to_date)
228
+ where_clause = rto_detail.where("shipments.state NOT IN ('#{States::RECEIVED}','#{States::LOST}','#{States::DELIVERED}')").merge(RtoDetail.by_status(RtoDetail::Statuses::CREATED))
229
+ from_date || to_date ? where_clause.merge(RtoDetail.from_date(from_date)).merge(RtoDetail.to_date(to_date)) : where_clause.merge(RtoDetail.to_date(Time.now.to_date.to_s))
230
+ end
231
+
232
+ def self.reshipment
233
+ by_state(States::RECEIVED).where("(flags & 8) = 8")
234
+ end
235
+
236
+ def self.by_vendor_id(id)
237
+ id ? where("shipments.vendor_id = ?", id) : Shipment.scoped
238
+ end
239
+
240
+ def self.by_merchant_ref_or_tracking_id(id)
241
+ id ? where("shipments.merchant_reference_id = ? or shipments.vendor_tracking_id = ?", id, id) : Shipment.scoped
242
+ end
243
+
244
+ def self.by_merchant_ref_and_tracking_id(merchant_ref_id, tracking_id)
245
+ (merchant_ref_id && tracking_id) ? where("shipments.merchant_reference_id = ? and shipments.vendor_tracking_id = ?", merchant_ref_id, tracking_id) : Shipment.scoped
246
+ end
247
+
248
+ def self.by_vendor_id_and_merchant_ref_or_tracking_id(vid, alt_vid, id)
249
+ id ? where("shipments.vendor_id in (?,?) and (shipments.merchant_reference_id = ? or shipments.vendor_tracking_id = ?)", vid, alt_vid, id, id) : Shipment.scoped
250
+ end
251
+
252
+ def self.by_merchant_ref_or_tracking_ids(ids)
253
+ ids ? where("shipments.merchant_reference_id IN (#{ids}) or shipments.vendor_tracking_id IN (#{ids})") : Shipment.scoped
254
+ end
255
+
256
+ def self.by_payment_type(type)
257
+ type ? where("shipments.payment_type = '#{type}'") : Shipment.scoped
258
+ end
259
+
260
+ def self.by_delivery_type(delivery_type)
261
+ delivery_type ? where("shipments.delivery_type = '#{delivery_type}'") : Shipment.scoped
262
+ end
263
+
264
+ def self.by_merchant(code)
265
+ code ? where("shipments.merchant_code = '#{code}'") : Shipment.scoped
266
+ end
267
+
268
+ def self.merchant_reship_pending_today(from_date, to_date)
269
+ where_clause = where("shipments.state IN ('#{States::MERCHANT_RESHIP_REQUESTED}','#{States::EXPECTED}') AND flags & 32 = 32")
270
+ from_date || to_date ? where_clause.merge(Shipment.from_date(from_date)).merge(Shipment.to_date(to_date)) : where_clause.where("shipments.updated_at #{self.build_date_filter(Time.now.to_date.to_s)}")
271
+ end
272
+
273
+ def self.merchant_reship_dispatched_today(from_date, to_date)
274
+ where_clause = where("shipments.state = ('#{States::DISPATCHED_TO_MERCHANT}') AND flags & 32 = 32")
275
+ from_date || to_date ? where_clause.merge(Shipment.from_date(from_date)).merge(Shipment.to_date(to_date)) : where_clause.where("shipments.updated_at #{self.build_date_filter(Time.now.to_date.to_s)}")
276
+ end
277
+
278
+ def self.merchant_reship_pending_till_yesterday(from_date)
279
+ from_date = DateTime.parse(from_date).strftime("%Y-%m-%d %H:%M:%S") if from_date
280
+ where_clause = where("shipments.state IN ('#{States::MERCHANT_RESHIP_REQUESTED}','#{States::DISPATCHED_TO_MERCHANT}','#{States::EXPECTED}') AND flags & 32 = 32")
281
+ from_date ? where_clause.where("shipments.updated_at < '#{from_date}'") : where_clause.where("shipments.updated_at < '#{Time.now.to_date.to_s} 00:00:00'")
282
+ end
283
+
284
+ def self.not_marked_for_reshipment
285
+ where("shipments.state NOT IN ('#{States::MARKED_FOR_RESHIPMENT}', '#{States::RESHIPPED}')")
286
+ end
287
+
288
+ def self.by_merchant_ref_id(merchant_ref_id)
289
+ merchant_ref_id ? where(:merchant_reference_id => merchant_ref_id) : Shipment.scoped
290
+ end
291
+
292
+ def self.by_vendor_tracking_vendor_id(vendor_tracking_id, vendor_id)
293
+ vendor_tracking_id && vendor_id ? where(:vendor_tracking_id => vendor_tracking_id, :vendor_id => vendor_id).first : Shipment.scoped
294
+ end
295
+
296
+ def self.by_vendor_tracking_id(vendor_tracking_id)
297
+ vendor_tracking_id ? where(:vendor_tracking_id => vendor_tracking_id) : Shipment.scoped
298
+ end
299
+
300
+
301
+ def self.filter(args = {})
302
+ offset = args[:per_page].to_i * (args[:page].to_i - 1) if (args[:per_page] && args[:page])
303
+ by_vendor(args[:by_vendor]).by_merchant(args[:by_merchant]).by_type(
304
+ args[:by_type]).by_cs_status(args[:by_cs_status]).by_delivery_type(
305
+ args[:by_delivery_type]).pending_for(args[:pending_for]).by_city(
306
+ args[:by_city]).by_state(args[:by_state]).by_rto_type(
307
+ args[:by_rto_type]).by_fkl_facility(args[:by_fkl_facility]).by_fkl_station(
308
+ args[:by_fkl_station]).from_date(
309
+ args[:by_from_date]).to_date(args[:by_to_date]).by_limit(
310
+ args[:per_page]).by_offset(args[:page], offset.to_i).merge(
311
+ (Shipment.get_multiple_shipments_by_id(args[:by_tracking_id]) if args[:by_tracking_id])
312
+ )
313
+ end
314
+
315
+ def self.unit_filter(args = {})
316
+ offset = args[:per_page].to_i * (args[:page].to_i - 1) if (args[:per_page] && args[:page])
317
+ by_vendor(args[:by_vendor]).by_merchant(args[:by_merchant]).by_type(
318
+ args[:by_type]).by_cs_status(args[:by_cs_status]).by_delivery_type(
319
+ args[:by_delivery_type]).pending_for(args[:pending_for]).by_city(
320
+ args[:by_city]).by_state(args[:by_state]).by_rto_type(
321
+ args[:by_rto_type]).by_fkl_facility(args[:by_fkl_facility]).by_fkl_station(
322
+ args[:by_fkl_station]).from_date(
323
+ args[:by_from_date]).to_date(args[:by_to_date]).by_limit(
324
+ args[:per_page]).by_offset(args[:page], offset.to_i).merge(
325
+ (Shipment.by_merchant_ref_or_tracking_ids(args[:by_tracking_ids]) if args[:by_tracking_ids])
326
+ )
327
+ end
328
+
329
+
330
+ def self.new_filter(args = {})
331
+ offset = args[:per_page].to_i * (args[:page].to_i - 1) if (args[:per_page] && args[:page])
332
+ by_vendor(args[:by_vendor]).by_merchant(args[:by_merchant]).by_type(
333
+ args[:by_type]).by_fkl_origin_facility(args[:by_fkl_facility]).by_unreceived_rto().dispatched_after(
334
+ args[:from_date]).dispatched_bef(args[:to_date]).by_fkl_station(
335
+ args[:by_fkl_station]).by_limit(
336
+ args[:per_page]).by_offset(args[:page], offset.to_i).merge(
337
+ (Shipment.get_multiple_shipments_by_id(args[:by_tracking_id]) if args[:by_tracking_id])
338
+ )
339
+ end
340
+
341
+ def self.filter_rto(args = {})
342
+ scope = Shipment.scoped
343
+ source_type = args[:by_source_type]
344
+ merchant_code = args[:by_merchant]
345
+ scope = scope.rto_expected(args[:by_rto_from_date], args[:by_rto_to_date]).by_source_type(source_type) if args[:rto_expected]
346
+ scope = scope.pending_till_yesterday_rto(args[:by_rto_from_date]).by_source_type(source_type) if args[:pending_till_yesterday_rto]
347
+ scope = scope.pending_today_rto(args[:by_rto_from_date], args[:by_rto_to_date]).by_source_type(source_type) if args[:pending_today_rto]
348
+ scope = scope.returned_rto(args[:by_rto_from_date], args[:by_rto_to_date]).by_source_type(source_type) if args[:returned_rto]
349
+ scope = scope.received_by_merchant_rto(args[:by_rto_from_date], args[:by_rto_to_date]).by_source_type(source_type) if args[:received_by_merchant_rto]
350
+ scope = scope.reshipped_rto(args[:by_rto_from_date], args[:by_rto_to_date]).by_source_type(source_type) if args[:reshipped_rto]
351
+ scope
352
+ end
353
+
354
+ def self.filter_merchant_reship(args = {})
355
+ scope = Shipment.scoped
356
+ scope = scope.merchant_reship_pending_till_yesterday(args[:by_rto_from_date]) if args[:pending_till_yesterday_rto]
357
+ scope = scope.merchant_reship_pending_today(args[:by_rto_from_date], args[:by_rto_to_date]) if args[:pending_today_rto]
358
+ scope = scope.merchant_reship_dispatched_today(args[:by_rto_from_date], args[:by_rto_to_date]) if args[:returned_rto]
359
+ scope
360
+ end
361
+
362
+ def self.filter_forward(args = {})
363
+ scope = Shipment.scoped
364
+ from_date, to_date = args[:from_date], args[:to_date]
365
+ source_type = args[:by_source_type]
366
+ priority = args[:by_priority]
367
+
368
+ scope = scope.forward_shipments_with_rto_created.pending_fwd_before(from_date).by_source_type(source_type).group("shipments.id") if args[:pending_till_yesterday_fwd]
369
+ scope = scope.forward_shipments_with_rto_created.pending_fwd_for(from_date, to_date).by_source_type(source_type).group("shipments.id") if args[:received_today_fwd]
370
+ scope = scope.forward_shipments_with_rto_created.dispatched_fwd(from_date, to_date).by_source_type(source_type).group("shipments.id") if args[:dispatched_to_vendor_fwd]
371
+ scope = scope.forward_shipments_with_rto_created.expected_fwd(from_date, to_date).by_source_type(source_type).group("shipments.id") if args[:expected_today_fwd]
372
+ scope = scope.forward_shipments_with_rto_created.expected_fwd_before(from_date).by_source_type(source_type).group("shipments.id") if args[:expected_till_yesterday_fwd]
373
+ scope = scope.new_filter(args).group("shipments.id") if args[:actual_dispatched_to_vendor_fwd_count]
374
+
375
+ scope
376
+ end
377
+
378
+ def self.by_kerala_postal_codes
379
+ where("shipments.postal_code BETWEEN '670001' AND '699999' ")
380
+ end
381
+
382
+
383
+ def self.forward_dashboard_filters(from_date, to_date)
384
+ from_date(from_date).to_date_time_non_inclusive(to_date).where("shipments.state IN ('#{States::RECEIVED}','#{States::RECEIVED_WITH_ERROR}', '#{States::EXPECTED}', '#{States::DISPATCHED_TO_VENDOR}','#{States::DISPATCHED_TO_FACILITY}') AND shipments.type = 'OutgoingShipment' AND flags & 32 != 32")
385
+ end
386
+
387
+ def self.forward_dashboard_filters_new(from_date, to_date)
388
+ from_date(from_date).from_status_date(from_date).to_status_date(to_date).where("shipments.state IN ('#{States::RECEIVED}','#{States::RECEIVED_WITH_ERROR}', '#{States::EXPECTED}', '#{States::DISPATCHED_TO_VENDOR}','#{States::DISPATCHED_TO_FACILITY}') AND shipments.type = 'OutgoingShipment' AND shipment_status_histories.new_status = shipments.state AND flags & 32 != 32")
389
+ end
390
+
391
+ def self.dispatched_fwd(from_date, to_date)
392
+ from_date(from_date).where("shipments.state IN ('#{States::DISPATCHED_TO_VENDOR}','#{States::DISPATCHED_TO_FACILITY}') AND shipments.type = 'OutgoingShipment' AND flags & 32 != 32").by_history_status("'#{States::DISPATCHED_TO_VENDOR}','#{States::DISPATCHED_TO_FACILITY}'").from_status_date(from_date).to_status_date(to_date)
393
+ end
394
+
395
+ def self.dispatched_fwd_with_non_terminal_status(from_date, days)
396
+ from_date(from_date).dispatched_before_time(days).where("shipments.state IN ('#{States::DISPATCHED_TO_VENDOR}','#{States::DISPATCHED_TO_FACILITY}','#{States::UNDELIVERED_ATTEMPTED}','#{States::UNDELIVERED_UNATTEMPTED}') AND shipments.type in ('OutgoingShipment') AND flags & 32 != 32")
397
+ end
398
+
399
+ def self.dispatched_rvp_with_non_terminal_status(from_date, days)
400
+ from_date(from_date).dispatched_before_time(days).where("shipments.state IN ('#{States::DISPATCHED_TO_VENDOR}','#{States::DISPATCHED_TO_FACILITY}','#{States::UNDELIVERED_ATTEMPTED}','#{States::UNDELIVERED_UNATTEMPTED}') AND shipments.type in ('IncomingShipment') AND flags & 32 != 32")
401
+ end
402
+
403
+
404
+ def self.rvp_scheduled_shipments(from_date, days)
405
+ from_date(from_date).joins( "INNER JOIN incoming_shipment_pickup_details on shipments.id = incoming_shipment_pickup_details.incoming_shipment_id ").where ("shipments.state IN ('#{States::EXPECTED}','#{States::SCHEDULED}','#{States::REVERSE_PICKUP_SCHEDULED}','#{States::REQUEST_FOR_RESCHEDULE}','#{States::REQUEST_FOR_CANCELLATION}','#{States::REVERSE_PICKUP_COMPLETED}','#{States::VENDOR_RECEIVED}') AND shipments.type in ('IncomingShipment') AND flags & 32 != 32 AND incoming_shipment_pickup_details.pickup_type IN ('#{States::PICKUP_TYPE_ONLY}')")
406
+ end
407
+
408
+ def self.rvp_scheduled_shipments_gati(from_date, days)
409
+ from_date(from_date).joins( "INNER JOIN incoming_shipment_pickup_details on shipments.id = incoming_shipment_pickup_details.incoming_shipment_id ").where ("shipments.state IN ('#{States::EXPECTED}','#{States::SCHEDULED}','#{States::REVERSE_PICKUP_SCHEDULED}','#{States::REQUEST_FOR_RESCHEDULE}','#{States::REQUEST_FOR_CANCELLATION}') AND shipments.type in ('IncomingShipment') AND flags & 32 != 32 AND incoming_shipment_pickup_details.pickup_type IN ('#{States::PICKUP_TYPE_ONLY}')")
410
+ end
411
+
412
+ def self.dispatched_fwd_with_dispatch_to_vendor_status(from_date, days)
413
+ from_date(from_date).dispatched_before_time(days).where("shipments.state IN ('#{States::DISPATCHED_TO_VENDOR}','#{States::DISPATCHED_TO_FACILITY}') AND shipments.type in ('OutgoingShipment') AND flags & 32 != 32")
414
+ end
415
+
416
+ def self.dispatched_rvp_with_dispatch_to_vendor_status(from_date, days)
417
+ from_date(from_date).dispatched_before_time(days).where("shipments.state IN ('#{States::DISPATCHED_TO_VENDOR}','#{States::DISPATCHED_TO_FACILITY}') AND shipments.type in ('IncomingShipment') AND flags & 32 != 32")
418
+ end
419
+
420
+ def self.dispatched_fwd_with_after_dispatch_to_vendor_status(from_date, days)
421
+ from_date(from_date).dispatched_before_time(days).where("shipments.state IN ('#{States::UNDELIVERED_ATTEMPTED}','#{States::UNDELIVERED_UNATTEMPTED}') AND shipments.type in ('OutgoingShipment') AND flags & 32 != 32")
422
+ end
423
+
424
+ def self.dispatched_rvp_with_after_dispatch_to_vendor_status(from_date, days)
425
+ from_date(from_date).dispatched_before_time(days).where("shipments.state IN ('#{States::UNDELIVERED_ATTEMPTED}','#{States::UNDELIVERED_UNATTEMPTED}') AND shipments.type in ('IncomingShipment') AND flags & 32 != 32")
426
+ end
427
+
428
+ def self.get_dispatched_outgoing_shipments
429
+ where("shipments.state IN ('#{States::DISPATCHED_TO_VENDOR}','#{States::DISPATCHED_TO_FACILITY}') AND shipments.type in ('OutgoingShipment') AND flags & 32 != 32")
430
+ end
431
+ def self.get_dispatched_outgoing_shipments_fedex
432
+ where("shipments.state IN ('#{States::DISPATCHED_TO_VENDOR}','#{States::DISPATCHED_TO_FACILITY}','#{States::UNDELIVERED_ATTEMPTED}','#{States::UNDELIVERED_UNATTEMPTED}','#{States::DELIVERED}') AND shipments.type in ('OutgoingShipment') AND flags & 32 != 32")
433
+ end
434
+ def self.without_vendor_service_request
435
+ joins("LEFT OUTER JOIN vendor_service_requests on shipments.id = vendor_service_requests.shipment_id").where("vendor_service_requests.id is null")
436
+ end
437
+ def self.without_lite_shipments(ids)
438
+ joins("LEFT OUTER JOIN shipping_lite_shipments on shipments.merchant_reference_id=shipping_lite_shipments.merchant_reference_id where (shipments.vendor_tracking_id IN (#{ids}) OR shipments.merchant_reference_id IN (#{ids})) and shipping_lite_shipments.merchant_reference_id IS NULL")
439
+ end
440
+ def self.include_shipment_details
441
+ includes(:shipment_items,:customer => :postal_addresses,:customer => :telecom_numbers,:seller => :shipment_seller_mapping)
442
+ end
443
+
444
+ def self.pickup_rvp_incoming_shipments
445
+ joins( "INNER JOIN incoming_shipment_pickup_details on shipments.id = incoming_shipment_pickup_details.incoming_shipment_id ").where ("shipments.state IN ('#{States::EXPECTED}') AND shipments.type in ('IncomingShipment') AND flags & 32 != 32 AND incoming_shipment_pickup_details.state IN ('#{States::PARTIALLY_SCHEDULED}') AND incoming_shipment_pickup_details.pickup_type IN ('#{States::PICKUP_TYPE_ONLY}')")
446
+ end
447
+
448
+ def self.pickup_rvp_incoming_shipments_scheduled_state
449
+ joins( "INNER JOIN incoming_shipment_pickup_details on shipments.id = incoming_shipment_pickup_details.incoming_shipment_id ").where ("shipments.state IN ('#{States::EXPECTED}') AND shipments.type in ('IncomingShipment') AND flags & 32 != 32 AND incoming_shipment_pickup_details.state IN ('#{States::SCHEDULED}') AND incoming_shipment_pickup_details.pickup_type IN ('#{States::PICKUP_TYPE_ONLY}')")
450
+ end
451
+
452
+ def self.with_vendor_service_request_and_attemps_count
453
+ joins("LEFT OUTER JOIN vendor_service_requests on shipments.id = vendor_service_requests.shipment_id").where("vendor_service_requests.no_of_attempts <= '#{States::THRESHOLD_NUMBER_OF_ATTEMPT}' AND vendor_service_requests.state in ('processing', 'failed')")
454
+ end
455
+
456
+ def self.failed_shipments_in_vendor_service_request(request_type)
457
+ request_type_condition = request_type.present? ? " and vendor_service_requests.request_type='#{request_type}'" : ""
458
+ joins("LEFT OUTER JOIN vendor_service_requests on shipments.id = vendor_service_requests.shipment_id").where("vendor_service_requests.no_of_attempts <= '#{States::THRESHOLD_NUMBER_OF_ATTEMPT}' AND vendor_service_requests.state in ('failed') #{request_type_condition}")
459
+ end
460
+
461
+ def self.processing_shipments_in_vendor_service_request(request_type)
462
+ request_type_condition = request_type.present? ? " and vendor_service_requests.request_type='#{request_type}'" : ""
463
+ joins("LEFT OUTER JOIN vendor_service_requests on shipments.id = vendor_service_requests.shipment_id").where("vendor_service_requests.updated_at <= '#{Time.now-(States::THRESHOLD_TIME).hours}' AND vendor_service_requests.state in ('processing') #{request_type_condition}")
464
+ end
465
+
466
+ #def self.filter_rvp_scoped(args = {})
467
+ # scope = Shipment.scoped
468
+ # scope = scope.incoming_shipment_requested if args[:rvp_requested]
469
+ # scope = scope.incoming_shipment_cancelled if args[:rvp_cancelled]
470
+ # scope = scope.incoming_shipment_scheduled_rescheduled if args[:rvp_scheduled_rescheduled]
471
+ # scope = scope.incoming_shipment_partially_scheduled if args[:rvp_partially_scheduled]
472
+ # scope = scope.incoming_shipment_received if args[:rvp_received]
473
+ # scope
474
+ #end
475
+
476
+ def self.dispatched_today
477
+ where("shipments.state IN ('#{States::DISPATCHED_TO_VENDOR}','#{States::DISPATCHED_TO_FACILITY}') AND shipments.type = 'OutgoingShipment' AND shipments.updated_at #{self.build_date_filter(Time.now.to_date.to_s)} AND flags & 1 != 1 and flags & 32 != 32")
478
+ end
479
+
480
+ def self.pending_fwd_for(from_date, to_date)
481
+ from_date(from_date).by_history_status("'#{States::RECEIVED}','#{States::RECEIVED_WITH_ERROR}'").from_status_date(from_date).to_status_date(to_date).where("shipments.state IN ('#{States::RECEIVED}','#{States::RECEIVED_WITH_ERROR}') AND shipments.type = 'OutgoingShipment' AND flags & 32 != 32")
482
+ end
483
+
484
+ def self.use_force_index_on_state
485
+ from("shipments FORCE INDEX (index_shipments_on_state)")
486
+ end
487
+
488
+ def self.use_force_index_on_updated_at
489
+ from("shipments FORCE INDEX (index_shipments_on_updated_at)")
490
+ end
491
+
492
+ def self.join_shipment_status_history_with_history_status_date_index
493
+ joins("INNER JOIN shipment_status_histories FORCE INDEX for join (`index_shipment_status_histories_on_status_date`) ON shipment_status_histories.shipment_id = shipments.id")
494
+ end
495
+
496
+ def self.join_shipment_status_history_with_history_status_date_index_with_shipment_type
497
+ joins("INNER JOIN shipment_status_histories FORCE INDEX for join (`index_shipment_status_histories_on_status_date`) ON shipment_status_histories.shipment_id = shipments.id").where("new_status IN ('received','received_with_error', 'expected','dispatched_to_vendor','dispatched_to_facility') and status_type = 'SHIPMENT'")
498
+ end
499
+
500
+ def self.pending_fwd_before(from_date)
501
+ by_history_status("'#{States::RECEIVED}','#{States::RECEIVED_WITH_ERROR}'").to_status_date_v1(from_date).where("shipments.state IN ('#{States::RECEIVED}','#{States::RECEIVED_WITH_ERROR}') AND shipments.type = 'OutgoingShipment' AND flags & 32 != 32 ")
502
+ end
503
+
504
+ def self.expected_fwd(from_date, to_date)
505
+ from_date(from_date).by_history_status("'#{States::EXPECTED}'").from_status_date(from_date).to_status_date(to_date).where("shipments.state = '#{States::EXPECTED}' AND shipments.type = 'OutgoingShipment' AND flags & 32 != 32")
506
+ end
507
+
508
+ def self.expected_fwd_before(from_date)
509
+ by_history_status("'#{States::EXPECTED}'").to_status_date_v1(from_date).where("shipments.state = '#{States::EXPECTED}' AND shipments.type = 'OutgoingShipment' AND flags & 32 != 32")
510
+ end
511
+
512
+ def self.from_date(from_date)
513
+ from_date = DateTime.parse(from_date).strftime("%Y-%m-%d %H:%M:%S") if from_date
514
+ from_date ? where("shipments.updated_at >= '#{from_date}'") : Shipment.scoped
515
+ end
516
+
517
+ def self.to_date_time(date)
518
+ date = DateTime.parse(date).strftime("%Y-%m-%d %H:%M:%S") if date
519
+ date ? where("shipments.updated_at <= '#{date}'") : Shipment.scoped
520
+ end
521
+
522
+ def self.to_date_time_non_inclusive(date)
523
+ date = DateTime.parse(date).strftime("%Y-%m-%d %H:%M:%S") if date
524
+ date ? where("shipments.updated_at < '#{date}'") : Shipment.scoped
525
+ end
526
+
527
+ def self.to_date(to_date)
528
+ to_date = DateTime.parse(to_date).strftime("%Y-%m-%d %H:%M:%S") if to_date
529
+ to_date ? where("shipments.updated_at < '#{to_date}' + interval 1 day") : Shipment.scoped
530
+ end
531
+
532
+ def self.from_status_date(from_date)
533
+ from_date = DateTime.parse(from_date).strftime("%Y-%m-%d %H:%M:%S") if from_date
534
+ from_date ? where("shipment_status_histories.status_date >= '#{from_date}'") : Shipment.scoped
535
+ end
536
+
537
+ def self.to_status_date(to_date)
538
+ to_date = DateTime.parse(to_date).strftime("%Y-%m-%d %H:%M:%S") if to_date
539
+ to_date ? where("shipment_status_histories.status_date <= '#{to_date}'") : Shipment.scoped
540
+ end
541
+
542
+ def self.to_status_date_v1(to_date)
543
+ to_date = DateTime.parse(to_date).strftime("%Y-%m-%d %H:%M:%S") if to_date
544
+ to_date ? where("shipment_status_histories.status_date < '#{to_date}'") : Shipment.scoped
545
+ end
546
+
547
+ def self.by_history_status(status_string)
548
+ joins("JOIN shipment_status_histories force index (index_shipment_status_histories_on_status_date) on shipments.id = shipment_status_histories.shipment_id").where("new_status IN (#{status_string}) and status_type = 'SHIPMENT'")
549
+ end
550
+
551
+ def self.pending_fwd
552
+ where("shipments.state in ('received') and flags & 1 != 1")
553
+ end
554
+
555
+ def self.not_rto
556
+ where("(flags & 1) != 1")
557
+ end
558
+
559
+ def self.rto_shipments
560
+ where("(flags & 1) = 1")
561
+ end
562
+
563
+ def self.by_flag_1
564
+ where("flags = 1")
565
+ end
566
+
567
+ def self.without_tracking_id
568
+ where("shipments.vendor_tracking_id IS NULL and shipments.vendor_id is NOT NULL")
569
+ end
570
+
571
+ def self.without_vendor_and_tracking_id
572
+ where("shipments.vendor_tracking_id IS NULL and shipments.vendor_id is NULL")
573
+ end
574
+
575
+ def self.forward_shipments_without_rto_details
576
+ joins("LEFT OUTER JOIN rto_details on shipments.id = rto_details.shipment_id").where("rto_details.id is null")
577
+ end
578
+
579
+ def self.forward_shipments_with_rto_created
580
+ joins("LEFT OUTER JOIN rto_details on shipments.id = rto_details.shipment_id").where("rto_details.status IS NULL or rto_details.status = '#{RtoDetail::Statuses::CREATED}' or rto_details.status = '#{RtoDetail::Statuses::IN_TRANSIT}' or rto_details.status = '#{RtoDetail::Statuses::CANCELLED}' ")
581
+ end
582
+
583
+ def self.by_source_type(source_type)
584
+ if (!source_type.present?)
585
+ scoped
586
+ elsif (source_type.try(:downcase) == "fbf")
587
+ where("shipments.source_type = ?", Shipment::SourceTypes::FBF)
588
+ else # nonfbf
589
+ where("shipments.source_type = ? OR shipments.source_type IS NULL", Shipment::SourceTypes::WSR)
590
+ end
591
+ end
592
+
593
+ def self.by_priority(priority)
594
+ if (priority.present? && priority.try(:downcase) == Shipment::CostOfBreach::SDD)
595
+ joins("INNER JOIN shipment_attributes ON shipment_attributes.shipment_id = shipments.id").where("shipment_attributes.name = 'tier_type' and shipment_attributes.value = '#{Shipment::CostOfBreachValue::SDD}'")
596
+ elsif (priority.present? && priority.try(:downcase) == Shipment::CostOfBreach::NDD)
597
+ joins("INNER JOIN shipment_attributes ON shipment_attributes.shipment_id = shipments.id").where("shipment_attributes.name = 'tier_type' and shipment_attributes.value = '#{Shipment::CostOfBreachValue::NDD}'")
598
+ else
599
+ scoped
600
+ end
601
+ end
602
+
603
+ def incoming_market_place_shipment?
604
+ rvp? && Shipment.market_place_merchant(self.merchant.name)
605
+ end
606
+ end
@@ -0,0 +1,50 @@
1
+ class ShippingLiteShipmentDAO < ActiveRecord::Base
2
+ self.table_name = "shipping_lite_shipments"
3
+ # To change this template use File | Settings | File Templates.
4
+
5
+ validates_presence_of :merchant_reference_id
6
+ validates_presence_of :vendor_tracking_id
7
+ validates_presence_of :service_request_id
8
+ validates_presence_of :request_type
9
+ validates_presence_of :client_id
10
+ validates_presence_of :vendor_id
11
+ validates_presence_of :active
12
+
13
+ def self.by_merchant_ref_or_tracking_id(id)
14
+ id ? where("shipping_lite_shipments.merchant_reference_id = ? or shipping_lite_shipments.vendor_tracking_id = ?", id, id) : []
15
+ end
16
+
17
+ def self.by_merchant_ref_and_tracking_id(merchant_reference_id, vendor_tracking_id)
18
+ (merchant_reference_id && vendor_tracking_id) ? where("shipping_lite_shipments.merchant_reference_id = ? and shipping_lite_shipments.vendor_tracking_id = ?", merchant_reference_id, vendor_tracking_id) : []
19
+ end
20
+
21
+ def self.by_merchant_ref_id(id,is_active=true)
22
+ id ? where("shipping_lite_shipments.merchant_reference_id = ? and shipping_lite_shipments.active = ?", id,is_active) : []
23
+ end
24
+
25
+ def self.by_vendor_track_id(id,is_active=true)
26
+ id ? where("shipping_lite_shipments.vendor_tracking_id = ? and shipping_lite_shipments.active = ?", id,is_active) : []
27
+ end
28
+
29
+ def self.is_idempotent(service_request_id,merchant_reference_id,vendor_tracking_id,vendor_id)
30
+ where("shipping_lite_shipments.service_request_id = ? and shipping_lite_shipments.merchant_reference_id = ? and shipping_lite_shipments.vendor_tracking_id = ? and shipping_lite_shipments.vendor_id = ?",service_request_id,merchant_reference_id,vendor_tracking_id,vendor_id)
31
+ end
32
+
33
+ def self.by_merchant_ref_id_or_tracking_id_active(id,is_active=true)
34
+ id ? where("(shipping_lite_shipments.merchant_reference_id = ? or shipping_lite_shipments.vendor_tracking_id = ? ) and shipping_lite_shipments.active = ?", id, id,is_active) : []
35
+ end
36
+
37
+ def self.by_merchant_ref_ids_or_tracking_ids_active(ids, is_active=true)
38
+ ids ? where("(shipping_lite_shipments.merchant_reference_id IN (#{ids}) or shipping_lite_shipments.vendor_tracking_id IN (#{ids}) ) and shipping_lite_shipments.active = ?", is_active) : []
39
+ end
40
+
41
+ def self.find_existing_lite_shipment(merchant_reference_id,vendor_tracking_id,vendor_id)
42
+ shipment = ShipmentDAO.by_vendor_tracking_id(vendor_tracking_id)
43
+
44
+ if(shipment.first.present? && shipment.first.is_mh_offload_shipment?)
45
+ !(shipment.first.flags == 64) # Enabling only for seller return DMH
46
+ else
47
+ where("shipping_lite_shipments.merchant_reference_id = ? and shipping_lite_shipments.vendor_tracking_id = ? and shipping_lite_shipments.vendor_id = ?",merchant_reference_id,vendor_tracking_id,vendor_id).first
48
+ end
49
+ end
50
+ end