| 1 | package XTaTIK::Model::Cart; | ||
| 2 | |||
| 3 | our $VERSION = '0.005003'; # VERSION | ||
| 4 | |||
| 5 | 2 | ▶ | use Mojo::Base -base; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 6 | 0 | ▶ | use XTaTIK::Model::Products; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 7 | use XTaTIK::Model::Quote; | ||
| 8 | use Mojo::Pg; | ||
| 9 | use JSON::Meth; | ||
| 10 | |||
| 11 | use experimental 'postderef'; | ||
| 12 | |||
| 13 | my $Blank_Cart_Data = { | ||
| 14 | contents => [], | ||
| 15 | total => '0.00', | ||
| 16 | }; | ||
| 17 | |||
| 18 | has [qw/ | ||
| 19 | pg products id contents total dollars cents | ||
| 20 | _is_modified | ||
| 21 | /]; | ||
| 22 | |||
| 23 | sub new_cart { | ||
| 24 | my $self = shift; | ||
| 25 | |||
| 26 | $self->id( | ||
| 27 | $self->pg->db->query( | ||
| 28 | 'INSERT INTO carts (created_on, data) | ||
| 29 | VALUES (?, ?) | ||
| 30 | RETURNING id', | ||
| 31 | time(), | ||
| 32 | $Blank_Cart_Data->$j, | ||
| 33 | )->hash->{id} | ||
| 34 | ); | ||
| 35 | |||
| 36 | # This is needed, for when we swap carts | ||
| 37 | $self->contents([]); | ||
| 38 | $self->_recalculate_total; | ||
| 39 | |||
| 40 | return $self->id; | ||
| 41 | } | ||
| 42 | |||
| 43 | sub all_items { | ||
| 44 | my $self = shift; | ||
| 45 | |||
| 46 | my @contents = @{ $self->contents }; | ||
| 47 | my @products = $self->products->get_by_number(map $_->{n}, @contents); | ||
| 48 | |||
| 49 | my %quantities; | ||
| 50 | $quantities{ $_->{n} } = $_->{q} for @contents; | ||
| 51 | $_->{quantity} = $quantities{ $_->{number} } for @products; | ||
| 52 | |||
| 53 | return \@products; | ||
| 54 | } | ||
| 55 | |||
| 56 | sub all_items_cart_quote { | ||
| 57 | my $self = shift; | ||
| 58 | my ( @cart, @quote ); | ||
| 59 | $_->{price} == -1 ? push @quote, $_ : push @cart, $_ | ||
| 60 | for $self->all_items->@*; | ||
| 61 | |||
| 62 | return \@cart, \@quote; | ||
| 63 | } | ||
| 64 | |||
| 65 | sub all_items_cart_quote_kv { | ||
| 66 | my $self = shift; | ||
| 67 | my ( $cart, $quote ) = $self->all_items_cart_quote; | ||
| 68 | return ( cart => $cart, quote => $quote ); | ||
| 69 | } | ||
| 70 | |||
| 71 | sub alter_quantity { | ||
| 72 | my ( $self, $num, $qty ) = @_; | ||
| 73 | |||
| 74 | return $self->remove($num) | ||
| 75 | if $qty <= 0; | ||
| 76 | |||
| 77 | for ( @{ $self->contents } ) { | ||
| 78 | next unless $_->{n} eq $num; | ||
| 79 | $_->{q} = $qty; | ||
| 80 | $self->_recalculate_total; | ||
| 81 | return; | ||
| 82 | } | ||
| 83 | } | ||
| 84 | |||
| 85 | sub remove { | ||
| 86 | my ( $self, $num ) = @_; | ||
| 87 | @{ $self->contents } = grep $_->{n} ne $num, @{ $self->contents }; | ||
| 88 | $self->_recalculate_total; | ||
| 89 | return; | ||
| 90 | } | ||
| 91 | |||
| 92 | sub add { | ||
| 93 | my ( $self, $quantity, $number ) = @_; | ||
| 94 | |||
| 95 | my $product = $self->products->get_by_number( $number ) | ||
| 96 | or return; | ||
| 97 | |||
| 98 | my $added = 0; | ||
| 99 | for ( @{ $self->contents } ) { | ||
| 100 | if ( $_->{n} eq $product->{number} ) { | ||
| 101 | $added = 1; | ||
| 102 | $_->{q} += $quantity; | ||
| 103 | last; | ||
| 104 | } | ||
| 105 | } | ||
| 106 | |||
| 107 | $added or push @{ $self->contents }, +{ | ||
| 108 | n => $number, | ||
| 109 | q => $quantity, | ||
| 110 | p => $product->{'price'}, | ||
| 111 | }; | ||
| 112 | |||
| 113 | $self->_recalculate_total; | ||
| 114 | return $product; | ||
| 115 | } | ||
| 116 | |||
| 117 | sub load { | ||
| 118 | my $self = shift; | ||
| 119 | |||
| 120 | my $id = $self->id | ||
| 121 | or die "MISSING CART ID on load"; | ||
| 122 | |||
| 123 | my $cart_row = $self->pg->db->query( | ||
| 124 | 'SELECT * FROM "carts" WHERE "id" = ?', | ||
| 125 | $id, | ||
| 126 | )->hash; | ||
| 127 | |||
| 128 | my $cart = eval { $cart_row->{data}->$j } // $Blank_Cart_Data->$j->$j; | ||
| 129 | |||
| 130 | $self->contents( $cart->{contents} ); | ||
| 131 | $self->_recalculate_total; | ||
| 132 | |||
| 133 | return 1; | ||
| 134 | } | ||
| 135 | |||
| 136 | sub submit { | ||
| 137 | my $self = shift; | ||
| 138 | my %customer_data = @_; | ||
| 139 | |||
| 140 | my $id = $self->id or die "MISSING CART ID on submit"; | ||
| 141 | |||
| 142 | my @quote_products = map +{ q => $_->{q}, n => $_->{n}, }, | ||
| 143 | grep $_->{price}//-1 == -1, $self->contents->@*; | ||
| 144 | return unless @quote_products; | ||
| 145 | |||
| 146 | $quote_products[$_]{o} = $_ for 0 .. $#quote_products; | ||
| 147 | |||
| 148 | my $quote = XTaTIK::Model::Quote->new( pg => $self->pg ); | ||
| 149 | $quote->new_quote( $id )->contents(\@quote_products); | ||
| 150 | $quote->$_( $customer_data{$_} ) for keys %customer_data; | ||
| 151 | $quote->save; | ||
| 152 | return 1; | ||
| 153 | } | ||
| 154 | |||
| 155 | sub save { | ||
| 156 | my $self = shift; | ||
| 157 | |||
| 158 | return unless $self->_is_modified; | ||
| 159 | |||
| 160 | my $id = $self->id or die "MISSING CART ID on save"; | ||
| 161 | |||
| 162 | $self->pg->db->query( | ||
| 163 | 'UPDATE "carts" SET "data" = ? WHERE "id" = ?', | ||
| 164 | { | ||
| 165 | contents => $self->contents, | ||
| 166 | total => $self->total, | ||
| 167 | }->$j, | ||
| 168 | $id, | ||
| 169 | ); | ||
| 170 | |||
| 171 | return 1; | ||
| 172 | } | ||
| 173 | |||
| 174 | |||
| 175 | sub _recalculate_total { | ||
| 176 | my $self = shift; | ||
| 177 | |||
| 178 | my $total = 0; | ||
| 179 | $total += $_->{p} * $_->{q} | ||
| 180 | for grep $_->{p} > 0, @{ $self->contents }; | ||
| 181 | |||
| 182 | $self->_is_modified( 1 ); | ||
| 183 | |||
| 184 | $total = sprintf '%.2f', $total; | ||
| 185 | $self->total( $total ); | ||
| 186 | $self->dollars( (split /\./, $total)[0] ); | ||
| 187 | $self->cents( (split /\./, $total)[1] ); | ||
| 188 | return $self->total; | ||
| 189 | } | ||
| 190 | |||
| 191 | |||
| 192 | 1; | ||
| 193 | |||
| 194 | __END__ | ||