| 1 | #!/usr/bin/perl | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | use WWW::Shopify; | ||
| 7 | |||
| 8 | package WWW::Shopify::Model::Order; | ||
| 9 | use parent "WWW::Shopify::Model::Item"; | ||
| 10 | |||
| 11 | my $fields; sub fields { return $fields; } | ||
| 12 | BEGIN { $fields = { | ||
| 13 | "buyer_accepts_marketing" => new WWW::Shopify::Field::Boolean(), | ||
| 14 | "cancel_reason" => new WWW::Shopify::Field::String(), | ||
| 15 | "cancelled_at" => new WWW::Shopify::Field::Date(), | ||
| 16 | "cart_token" => new WWW::Shopify::Field::String::Hex32(), | ||
| 17 | # These two also not in docs, but exist, and are necessary to sometimes calculate the payment reference field. | ||
| 18 | "checkout_token" => new WWW::Shopify::Field::String::Hex32(), | ||
| 19 | "checkout_id" => new WWW::Shopify::Field::BigInt(), | ||
| 20 | "device_id" => new WWW::Shopify::Field::Int(), | ||
| 21 | "user_id" => new WWW::Shopify::Field::BigInt(), | ||
| 22 | "closed_at" => new WWW::Shopify::Field::Date(), | ||
| 23 | "processed_at" => new WWW::Shopify::Field::Date(), | ||
| 24 | "gateway" => new WWW::Shopify::Field::String(), | ||
| 25 | "created_at" => new WWW::Shopify::Field::Date(), | ||
| 26 | "currency" => new WWW::Shopify::Field::Currency(), | ||
| 27 | "email" => new WWW::Shopify::Field::String::Email(), | ||
| 28 | "contact_email" => new WWW::Shopify::Field::String::Email(), | ||
| 29 | "financial_status" => new WWW::Shopify::Field::String::Enum(["authorized", "pending", "paid", "abandoned", "refunded", "voided", "partially_refunded", "partially_paid"]), | ||
| 30 | # Rolling eyes here; obviously "restocked" not in the documentation. | ||
| 31 | "fulfillment_status" => new WWW::Shopify::Field::String::Enum(["fulfilled", undef, "partial", "restocked"]), | ||
| 32 | "reference" => new WWW::Shopify::Field::String(), | ||
| 33 | "id" => new WWW::Shopify::Field::Identifier(), | ||
| 34 | "landing_site" => new WWW::Shopify::Field::String::URL::Long(), | ||
| 35 | "name" => new WWW::Shopify::Field::String::Regex("#[1-9][0-9]{0,3}"), | ||
| 36 | "note" => new WWW::Shopify::Field::Text(), | ||
| 37 | "number" => new WWW::Shopify::Field::Int(), | ||
| 38 | "phone" => new WWW::Shopify::Field::String::Phone(), | ||
| 39 | "referring_site" => new WWW::Shopify::Field::String::URL::Long(), | ||
| 40 | "subtotal_price" => new WWW::Shopify::Field::Money(), | ||
| 41 | "taxes_included" => new WWW::Shopify::Field::Boolean(), | ||
| 42 | "test" => new WWW::Shopify::Field::Boolean(), | ||
| 43 | "confirmed" => new WWW::Shopify::Field::Boolean(), | ||
| 44 | "token" => new WWW::Shopify::Field::String::Hex32(), | ||
| 45 | # Does not actually represent all discounts; misses out on certain shipping discounts. | ||
| 46 | "total_discounts" => new WWW::Shopify::Field::Money(), | ||
| 47 | "total_line_items_price" => new WWW::Shopify::Field::Money(), | ||
| 48 | "source_name" => new WWW::Shopify::Field::String::Enum(["web", "pos"]), | ||
| 49 | "source_identifier" => new WWW::Shopify::Field::String(), | ||
| 50 | "source_url" => new WWW::Shopify::Field::String::URL(), | ||
| 51 | "total_price" => new WWW::Shopify::Field::Money(), | ||
| 52 | "total_price_usd" => new WWW::Shopify::Field::Money::USD(), | ||
| 53 | "total_tax" => new WWW::Shopify::Field::Money(), | ||
| 54 | "total_weight" => new WWW::Shopify::Field::Float(), | ||
| 55 | "updated_at" => new WWW::Shopify::Field::Date(), | ||
| 56 | "browser_ip" => new WWW::Shopify::Field::String::IPAddress(), | ||
| 57 | "landing_site_ref" => new WWW::Shopify::Field::String(), | ||
| 58 | "tags" => new WWW::Shopify::Field::Text(), | ||
| 59 | "order_number" => new WWW::Shopify::Field::Int(), | ||
| 60 | "app_id" => new WWW::Shopify::Field::BigInt(), | ||
| 61 | "customer_locale" => new WWW::Shopify::Field::String(), | ||
| 62 | "location_id" => new WWW::Shopify::Field::Relation::ReferenceOne('WWW::Shopify::Model::Location'), | ||
| 63 | "discount_applications" => new WWW::Shopify::Field::Relation::Many("WWW::Shopify::Model::Order::DiscountApplication"), | ||
| 64 | "discount_codes" => new WWW::Shopify::Field::Relation::Many("WWW::Shopify::Model::Order::DiscountCode", 0, 1), | ||
| 65 | "note_attributes" => new WWW::Shopify::Field::Relation::Many("WWW::Shopify::Model::Order::NoteAttribute"), | ||
| 66 | "processing_method" => new WWW::Shopify::Field::String::Enum(["direct", "indirect"]), | ||
| 67 | "line_items" => new WWW::Shopify::Field::Relation::Many("WWW::Shopify::Model::Order::LineItem", 1), | ||
| 68 | "shipping_lines" => new WWW::Shopify::Field::Relation::Many("WWW::Shopify::Model::Order::ShippingLine"), | ||
| 69 | "tax_lines" => new WWW::Shopify::Field::Relation::Many("WWW::Shopify::Model::Order::TaxLine"), | ||
| 70 | "billing_address" => new WWW::Shopify::Field::Relation::OwnOne("WWW::Shopify::Model::Order::BillingAddress", 1), | ||
| 71 | "shipping_address" => new WWW::Shopify::Field::Relation::OwnOne("WWW::Shopify::Model::Order::ShippingAddress", 1), | ||
| 72 | "fulfillments" => new WWW::Shopify::Field::Relation::Many("WWW::Shopify::Model::Order::Fulfillment"), | ||
| 73 | "client_details" => new WWW::Shopify::Field::Relation::OwnOne("WWW::Shopify::Model::Order::ClientDetails"), | ||
| 74 | "payment_details" => new WWW::Shopify::Field::Relation::OwnOne("WWW::Shopify::Model::Order::PaymentDetails"), | ||
| 75 | "customer" => new WWW::Shopify::Field::Relation::OwnOne("WWW::Shopify::Model::Customer"), | ||
| 76 | "metafields" => new WWW::Shopify::Field::Relation::Many("WWW::Shopify::Model::Metafield"), | ||
| 77 | "transactions" => new WWW::Shopify::Field::Relation::Many("WWW::Shopify::Model::Transaction"), | ||
| 78 | "refunds" => new WWW::Shopify::Field::Relation::Many("WWW::Shopify::Model::Refund"), | ||
| 79 | "risks" => new WWW::Shopify::Field::Relation::Many("WWW::Shopify::Model::Order::Risk"), | ||
| 80 | "presentment_currency" => new WWW::Shopify::Field::Currency(), | ||
| 81 | |||
| 82 | "total_line_items_price_set" => new WWW::Shopify::Field::Relation::OwnOne("WWW::Shopify::Model::Order::PriceSet"), | ||
| 83 | "total_discounts_price_set" => new WWW::Shopify::Field::Relation::OwnOne("WWW::Shopify::Model::Order::PriceSet"), | ||
| 84 | "subtotal_price_set" => new WWW::Shopify::Field::Relation::OwnOne("WWW::Shopify::Model::Order::PriceSet"), | ||
| 85 | "total_price_set" => new WWW::Shopify::Field::Relation::OwnOne("WWW::Shopify::Model::Order::PriceSet"), | ||
| 86 | "total_tax_set" => new WWW::Shopify::Field::Relation::OwnOne("WWW::Shopify::Model::Order::PriceSet"), | ||
| 87 | "total_discounts_set" => new WWW::Shopify::Field::Relation::OwnOne("WWW::Shopify::Model::Order::PriceSet"), | ||
| 88 | # I love how all of the above are representative of actual fields, but "total_shipping" doesn't actually exist. Sigh. | ||
| 89 | "total_shipping_price_set" => new WWW::Shopify::Field::Relation::OwnOne("WWW::Shopify::Model::Order::PriceSet"), | ||
| 90 | "total_tip_received" => new WWW::Shopify::Field::Money(), | ||
| 91 | |||
| 92 | "payment_gateway_names" => new WWW::Shopify::Field::Freeform::Array(), | ||
| 93 | |||
| 94 | "send_webhooks" => new WWW::Shopify::Field::Boolean(), | ||
| 95 | "send_receipt" => new WWW::Shopify::Field::Boolean(), | ||
| 96 | "send_fulfillment_receipt" => new WWW::Shopify::Field::Boolean(), | ||
| 97 | "inventory_behaviour" => new WWW::Shopify::Field::String::Enum(["bypass", "decrement_ignoring_policy", "decrement_obeying_policy"]), | ||
| 98 | "order_status_url" => new WWW::Shopify::Field::String::URL(), | ||
| 99 | "current_total_price" => new WWW::Shopify::Field::Money(), | ||
| 100 | "current_total_discounts" => new WWW::Shopify::Field::Money(), | ||
| 101 | "current_subtotal_price" => new WWW::Shopify::Field::Money(), | ||
| 102 | "current_total_tax" => new WWW::Shopify::Field::Money(), | ||
| 103 | "current_total_tax_set" => new WWW::Shopify::Field::Relation::OwnOne("WWW::Shopify::Model::Order::PriceSet"),, | ||
| 104 | "current_subtotal_price_set" => new WWW::Shopify::Field::Relation::OwnOne("WWW::Shopify::Model::Order::PriceSet"), | ||
| 105 | "current_total_price_set" => new WWW::Shopify::Field::Relation::OwnOne("WWW::Shopify::Model::Order::PriceSet"), | ||
| 106 | "current_total_duties_set" => new WWW::Shopify::Field::Relation::OwnOne("WWW::Shopify::Model::Order::PriceSet"), | ||
| 107 | "current_total_discounts_set" => new WWW::Shopify::Field::Relation::OwnOne("WWW::Shopify::Model::Order::PriceSet"), | ||
| 108 | "original_total_duties_set" => new WWW::Shopify::Field::Relation::OwnOne("WWW::Shopify::Model::Order::PriceSet"), | ||
| 109 | "total_outstanding" => new WWW::Shopify::Field::Money() | ||
| 110 | }; } | ||
| 111 | my $queries; sub queries { return $queries; } | ||
| 112 | BEGIN { $queries = { | ||
| 113 | created_at_min => new WWW::Shopify::Query::LowerBound('created_at'), | ||
| 114 | created_at_max => new WWW::Shopify::Query::UpperBound('created_at'), | ||
| 115 | updated_at_min => new WWW::Shopify::Query::LowerBound('updated_at'), | ||
| 116 | updated_at_max => new WWW::Shopify::Query::UpperBound('updated_at'), | ||
| 117 | ids => new WWW::Shopify::Query::MultiMatch('id'), | ||
| 118 | name => new WWW::Shopify::Query::Match('name'), | ||
| 119 | status => new WWW::Shopify::Query::Enum('status', ['open', 'closed', 'cancelled', 'any']), | ||
| 120 | financial_status => new WWW::Shopify::Query::Enum('financial_status', ['authorized', 'pending', 'paid', 'partially_paid', 'abandoned', 'partially_refunded', 'refunded', 'voided', 'any', 'unpaid']), | ||
| 121 | fulfillment_status => new WWW::Shopify::Query::Enum('fulfillment_status', ['shipped', 'partial', 'unshipped', 'any']), | ||
| 122 | since_id => new WWW::Shopify::Query::LowerBound('id'), | ||
| 123 | attribution_app_id => new WWW::Shopify::Query::Match('app_id'), | ||
| 124 | customer_id => new WWW::Shopify::Query::Custom('customer_id', sub { | ||
| 125 | my ($rs, $value) = @_; | ||
| 126 | return $rs->search({ 'customer.id' => $value }, | ||
| 127 | { 'join' => 'customer', '+select' => ['collects.collection_id'], '+as' => ['collection_id'], } | ||
| 128 | ) | ||
| 129 | }) | ||
| 130 | }; } | ||
| 131 | |||
| 132 | sub actions { return qw(open close cancel); } | ||
| 133 | |||
| 134 | sub get_fields { return grep { $_ ne "send_webhooks" && $_ ne "send_receipt" && $_ ne "send_fulfillment_receipt" && $_ ne "inventory_behaviour" } keys(%$fields); } | ||
| 135 | sub creation_minimal { return qw(line_items); } | ||
| 136 | sub update_filled { return qw(updated_at); } | ||
| 137 | sub update_fields { return qw(note note_attributes email buyer_accepts_marketing customer tags name shipping_address metafields); }; | ||
| 138 | sub throws_webhooks { 1; } | ||
| 139 | sub webhook_update_name { 'updated' } | ||
| 140 | sub get_order { ({'desc' => 'created_at' }, { 'desc' => 'id' }) } | ||
| 141 | |||
| 142 | sub status { | ||
| 143 | return "cancelled" if $_[0]->cancelled_at; | ||
| 144 | return "closed" if $_[0]->closed_at; | ||
| 145 | return "open"; | ||
| 146 | } | ||
| 147 | |||
| 148 | # A very large chunk of me has been stupid. | ||
| 149 | sub refund { return $_[0]->refunds; } | ||
| 150 | |||
| 151 | sub read_scope { return "read_orders"; } | ||
| 152 | sub write_scope { return "write_orders"; } | ||
| 153 | |||
| 154 | eval(__PACKAGE__->generate_accessors); die $@ if $@; | ||
| 155 | |||
| 156 | 1 |