Class: SpeedeeManifest

Inherits:
ApplicationRecord show all
Defined in:
app/models/speedee_manifest.rb

Overview

== Schema Information

Table name: speedee_manifests
Database name: primary

id :integer not null, primary key
filename :string not null
superseded_at :datetime
transmitted_at :datetime not null
created_at :datetime not null
updated_at :datetime not null

Indexes

index_speedee_manifests_on_filename (filename)
index_speedee_manifests_on_transmitted_at (transmitted_at)

Constant Summary

Constants included from Models::Schedulable

Models::Schedulable::SIMPLE_FORM_OPTIONS

Has many collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationRecord

ransackable_associations, ransackable_attributes, ransackable_scopes, ransortable_attributes, #to_relation

Methods included from Models::Schedulable

config

Methods included from Models::AfterCommittable

#after_commit

Methods included from Models::EventPublishable

#publish_event

Class Method Details

.drop_voided_shipments!(shipments) ⇒ void

This method returns an undefined value.

Drops the given shipments off their manifests and flags those manifests for
reprint.

Nothing is transmitted to Spee-Dee at close of day — the manifest exists so
the paper in the driver's hand matches Spee-Dee's dashboard and the parcels
actually on the truck. A void breaks all three at once: the Dispatch Science
order is cancelled, the parcel stays on the dock, but the manifest row (and
therefore the zone tables, charge rollups, totals and the scannable QR, all
of which Pdf::Document::SpeedeeSummary derives from these rows) still lists
it. Deleting the row is what makes a reprint truthful; superseded_at is only
the flag that tells the warehouse a reprint is owed.

Idempotent — a second void on the same manifest finds no rows to drop and
leaves the flag where it is.

Parameters:

  • shipments (ActiveRecord::Relation<Shipment>)


44
45
46
47
48
49
50
51
# File 'app/models/speedee_manifest.rb', line 44

def self.drop_voided_shipments!(shipments)
  rows = SpeedeeManifestShipment.where(shipment_id: shipments.select(:id))
  manifest_ids = rows.distinct.pluck(:speedee_manifest_id).compact
  return if manifest_ids.empty?

  rows.delete_all
  where(id: manifest_ids).update_all(superseded_at: Time.current, updated_at: Time.current)
end

.supersededActiveRecord::Relation<SpeedeeManifest>

A relation of SpeedeeManifests that are superseded. Active Record Scope

Returns:

See Also:



25
# File 'app/models/speedee_manifest.rb', line 25

scope :superseded, -> { where.not(superseded_at: nil) }

Instance Method Details

#cancel_manifestObject

Reverses a manifest run. Safe now that nothing is transmitted at close time
(the legacy CSV/FTP made this irreversible) — the manifest is a local record
plus a printed sheet, so it can be dropped and regenerated.

Only order-backed Deliveries carry the manifest_completed state to revert;
StandaloneDeliveries stay ship_labeled throughout and just lose their
manifest rows (compact avoids a nil crash on those).



94
95
96
97
# File 'app/models/speedee_manifest.rb', line 94

def cancel_manifest
  speedee_manifest_shipments.filter_map { |sms| sms.shipment&.delivery }.uniq.each(&:revert_to_pending_manifest_completion!)
  destroy!
end

#emptied?Boolean

True when every shipment has since been voided off the manifest — the
printed sheet is now entirely wrong and there is nothing left to hand over.

Returns:

  • (Boolean)


63
# File 'app/models/speedee_manifest.rb', line 63

def emptied? = superseded? && speedee_manifest_shipments.empty?

#speedee_manifest_shipmentsActiveRecord::Relation<SpeedeeManifestShipment>

Returns:



21
# File 'app/models/speedee_manifest.rb', line 21

has_many :speedee_manifest_shipments, dependent: :destroy

#speedee_manifest_shipments_with_dasActiveRecord::Relation<SpeedeeManifestShipment>

Manifest lines that carried a Delivery Area Surcharge, for the Additional
Charges block on the driver summary. das is frozen into each row at
build time from the Dispatch Science quote's DAS charge line (the 2019
SpeedeeDasZip list is gone; historical rows were backfilled).

(An explicit method rather than delegate …, prefix: — YARD's DSL
handler crashes on that delegate form during the docs build.)

Returns:



74
# File 'app/models/speedee_manifest.rb', line 74

def speedee_manifest_shipments_with_das = speedee_manifest_shipments.with_das # rubocop:disable Rails/Delegate -- YARD's DSL handler crashes on this delegate form

#superseded?Boolean

True once a shipment was voided off this manifest after it was printed, so
the sheet the driver is holding overstates what's leaving the dock.

Returns:

  • (Boolean)


57
# File 'app/models/speedee_manifest.rb', line 57

def superseded? = superseded_at.present?

#upload_summary_pdf(summary_pdf_path) ⇒ Boolean

Parameters:

  • summary_pdf_path (String)

Returns:

  • (Boolean)


78
79
80
81
82
83
84
85
# File 'app/models/speedee_manifest.rb', line 78

def upload_summary_pdf(summary_pdf_path)
  upload = Upload.uploadify(summary_pdf_path, 'summary_pdf')
  if uploads << upload
    true
  else
    false
  end
end

#uploadsActiveRecord::Relation<Upload>

Returns:

  • (ActiveRecord::Relation<Upload>)


23
# File 'app/models/speedee_manifest.rb', line 23

has_many :uploads, -> { order(position: :asc, created_at: :desc) }, as: :resource, dependent: :destroy