| 1 | # | ||
| 2 | # OrderManager.pm - Object that manages order checkout. | ||
| 3 | # | ||
| 4 | # This file is part of Apache::iNcom. | ||
| 5 | # | ||
| 6 | # Author: Francis J. Lacoste <francis.lacoste@iNsu.COM> | ||
| 7 | # | ||
| 8 | # Copyright (C) 1999 Francis J. Lacoste, iNsu Innovations | ||
| 9 | # | ||
| 10 | # This program is free software; you can redistribute it and/or modify | ||
| 11 | # it under the terms of the GNU General Public License as published by | ||
| 12 | # the Free Software Foundation; either version 2 of the License, or | ||
| 13 | # (at your option) any later version. | ||
| 14 | # | ||
| 15 | # This program is distributed in the hope that it will be useful, | ||
| 16 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 17 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 18 | # GNU General Public License for more details. | ||
| 19 | # | ||
| 20 | # You should have received a copy of the GNU General Public License | ||
| 21 | # along with this program; if not, write to the Free Software | ||
| 22 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA | ||
| 23 | # | ||
| 24 | package Apache::iNcom::OrderManager; | ||
| 25 | |||
| 26 | use strict; | ||
| 27 | |||
| 28 | use DBIx::SearchProfiles; | ||
| 29 | |||
| 30 | use Apache::Constants qw( OK ); | ||
| 31 | use HTML::Embperl; | ||
| 32 | |||
| 33 | use vars qw( $VERSION ); | ||
| 34 | |||
| 35 | BEGIN { | ||
| 36 | ($VERSION) = '$Revision: 1.5 $' =~ /Revision: ([\d.]+)/; | ||
| 37 | } | ||
| 38 | |||
| 39 | =pod | ||
| 40 | |||
| 41 | =head1 NAME | ||
| 42 | |||
| 43 | Apache::iNcom::OrderManager - Module responsible for order management. | ||
| 44 | |||
| 45 | =head1 SYNOPSIS | ||
| 46 | |||
| 47 | my $order = $Order->checkout( "order", $Cart, %fdat ); | ||
| 48 | my $report $Order->order_report( $order ); | ||
| 49 | |||
| 50 | =head1 DESCRIPTION | ||
| 51 | |||
| 52 | This is the part of the Apache::iNcom framework that is responsible | ||
| 53 | for managing the order process. Once the user is ready to check out, | ||
| 54 | the OrderManager rides in. This module enters the user's order in the | ||
| 55 | database according to an order profile. It can also generate order | ||
| 56 | reports and such. | ||
| 57 | |||
| 58 | It is in that module that future development will place links with | ||
| 59 | CyberCash (tm) and other online payment systems. | ||
| 60 | |||
| 61 | =head1 DATABASE | ||
| 62 | |||
| 63 | The order should be entered in two tables. One table table contains | ||
| 64 | the global order informations : order no, client's information, | ||
| 65 | status, total, taxes, etc., and another table contains the ordered | ||
| 66 | items. | ||
| 67 | |||
| 68 | To make order customizable, the OrderManager uses DBIx::SearchProfiles | ||
| 69 | to insert the information. That design is similar to DBIx::UserDB in | ||
| 70 | that only a few fields are required by the framework and the schema | ||
| 71 | can easily be adapted for application specific needs. | ||
| 72 | |||
| 73 | The mandatory fields in the main table are the C<order_no> field, | ||
| 74 | which should be a primary key on the table, and the C<status> field. | ||
| 75 | The status field is used to track the order process. | ||
| 76 | |||
| 77 | You may also want to add monetary fields for C<total>, C<subtotal> and | ||
| 78 | for the taxes, shipping and discount informations since that will | ||
| 79 | always be available in the order. | ||
| 80 | |||
| 81 | The mandatory fields in the items table is C<order_no> which links the | ||
| 82 | items to the order table. What will be inserted in that table are the | ||
| 83 | cart items, so you may want to use the following fields : C<price>, | ||
| 84 | C<subtotal>, C<quantity> and fields for the discount information. | ||
| 85 | |||
| 86 | =head1 ORDER PROFILES | ||
| 87 | |||
| 88 | The order profiles is a file which is C{eval}-ed at runtime. (It is | ||
| 89 | also reloaded whenever it changes on disk. It should return an hash | ||
| 90 | reference which contains the name of a profile associated with an hash | ||
| 91 | reference which may contains the following items : | ||
| 92 | |||
| 93 | =over | ||
| 94 | |||
| 95 | =item order_template | ||
| 96 | |||
| 97 | The record template that will be used to insert the order | ||
| 98 | informations. | ||
| 99 | |||
| 100 | =item order_item_template | ||
| 101 | |||
| 102 | The record template that will be used to insert order items. | ||
| 103 | |||
| 104 | =item order_no | ||
| 105 | |||
| 106 | The template query that will be used to generate order number. | ||
| 107 | |||
| 108 | =item taxes_fields | ||
| 109 | |||
| 110 | The field names that should be given to the order's taxes. This is an | ||
| 111 | array reference which defaults to [taxes0, taxes1, taxes2...] if there | ||
| 112 | is more than one taxes or [ taxes ] if there is only one taxes. | ||
| 113 | |||
| 114 | =item shipping_fields | ||
| 115 | |||
| 116 | The field names that should be given to the order's shipping charges. | ||
| 117 | This is an array reference which defaults to [ shipping0, shipping1, | ||
| 118 | shipping2...] if there more than one shipping charges or [ shipping ] | ||
| 119 | if only one is present. | ||
| 120 | |||
| 121 | =item discount_fields | ||
| 122 | |||
| 123 | The field names that should be given to the order's discounts. | ||
| 124 | This is an array reference which defaults to [discount0, discount1, | ||
| 125 | discount2...] if there more than one discount or [ discount ] if only | ||
| 126 | one is present. | ||
| 127 | |||
| 128 | |||
| 129 | =item item_discount_fields | ||
| 130 | |||
| 131 | The field names that should be given to the items' discounts. | ||
| 132 | This is an array reference which defaults to [discount0, discount1, | ||
| 133 | discount2...] if there more than one discount or [ discount ] if only | ||
| 134 | one is present. | ||
| 135 | |||
| 136 | =back | ||
| 137 | |||
| 138 | Example of an order profile : | ||
| 139 | |||
| 140 | { | ||
| 141 | order => | ||
| 142 | { | ||
| 143 | order_template => "order", | ||
| 144 | order_item_template => "order_items", | ||
| 145 | order_no => "order_no", | ||
| 146 | taxes_fields => [qw( gst gsp ) ], | ||
| 147 | }, | ||
| 148 | } | ||
| 149 | |||
| 150 | =head1 INITIALIZATION | ||
| 151 | |||
| 152 | An object is automatically initialized on each request by the | ||
| 153 | Apache::iNcom framework. It is accessible through the $Order global | ||
| 154 | variable in Apache::iNcom pages. | ||
| 155 | |||
| 156 | |||
| 157 | =head1 METHODS | ||
| 158 | |||
| 159 | =cut | ||
| 160 | |||
| 161 | sub new { | ||
| 162 | my $proto = shift; | ||
| 163 | my $class = ref $proto || $proto; | ||
| 164 | |||
| 165 | my ($database,$profile_file,$request) = @_; | ||
| 166 | |||
| 167 | my $self = { DB => $database, | ||
| 168 | profile_file => $profile_file, | ||
| 169 | request => $request, | ||
| 170 | }; | ||
| 171 | |||
| 172 | bless $self, $class; | ||
| 173 | } | ||
| 174 | |||
| 175 | sub load_profiles { | ||
| 176 | my $self = shift; | ||
| 177 | |||
| 178 | my $file = $self->{profile_file}; | ||
| 179 | |||
| 180 | die "No such file: $file\n" unless -f $file; | ||
| 181 | die "Can't read $file\n" unless -r _; | ||
| 182 | |||
| 183 | my $mtime = (stat _)[9]; | ||
| 184 | return if $self->{profiles} and $self->{profiles_mtime} <= $mtime; | ||
| 185 | |||
| 186 | $self->{profiles} = do $file; | ||
| 187 | die "Error in order profiles: $@\n" if $@; | ||
| 188 | die "Order profiles didn't return an hash ref\n" | ||
| 189 | unless ref $self->{profiles} eq "HASH"; | ||
| 190 | |||
| 191 | $self->{profiles_mtime} = $mtime; | ||
| 192 | } | ||
| 193 | |||
| 194 | sub build_multi_fields { | ||
| 195 | my ( $order, $name, $fields, $field_names ) = @_; | ||
| 196 | |||
| 197 | if ( ref $fields ) { | ||
| 198 | my $i = 0; | ||
| 199 | my @multi_fields = (); | ||
| 200 | if ( $field_names ) { | ||
| 201 | foreach my $f ( @$field_names ) { | ||
| 202 | $order->{$f} = $fields->[$i++]; | ||
| 203 | } | ||
| 204 | } else { | ||
| 205 | foreach my $f ( @$fields ) { | ||
| 206 | $order->{$name . "_" . $i++} = $f; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | } else { | ||
| 210 | if ( $field_names ) { | ||
| 211 | my $f = $field_names->[0]; | ||
| 212 | $order->{$f} = $fields; | ||
| 213 | } else { | ||
| 214 | $order->{$name} = $fields; | ||
| 215 | } | ||
| 216 | } | ||
| 217 | |||
| 218 | } | ||
| 219 | |||
| 220 | sub build_order_fields { | ||
| 221 | my ( $profile, $order_no, $order_data, $cart ) = @_; | ||
| 222 | |||
| 223 | my $order_fields = { %{$order_data} }; | ||
| 224 | $order_fields->{subtotal} = $cart->subtotal; | ||
| 225 | $order_fields->{total} = $cart->total; | ||
| 226 | $order_fields->{order_no} = $order_no; | ||
| 227 | $order_fields->{status} = "created"; | ||
| 228 | |||
| 229 | build_multi_fields( $order_fields, "taxes", | ||
| 230 | scalar($cart->taxes), $profile->{taxes_fields} | ||
| 231 | ); | ||
| 232 | build_multi_fields( $order_fields, "discount", | ||
| 233 | scalar($cart->discount), $profile->{discount_fields} | ||
| 234 | ); | ||
| 235 | build_multi_fields( $order_fields, "shipping", | ||
| 236 | scalar($cart->shipping), $profile->{shipping_fields} | ||
| 237 | ); | ||
| 238 | |||
| 239 | return $order_fields; | ||
| 240 | } | ||
| 241 | |||
| 242 | =pod | ||
| 243 | |||
| 244 | =head2 checkout ( $name, $cart, $order_data ) | ||
| 245 | |||
| 246 | This method enter the user order in the database. It takes the following | ||
| 247 | parameters : | ||
| 248 | |||
| 249 | =over | ||
| 250 | |||
| 251 | =item $name | ||
| 252 | |||
| 253 | The name of the order profile to use. | ||
| 254 | |||
| 255 | =item $cart | ||
| 256 | |||
| 257 | The cart that contains the user's order. | ||
| 258 | |||
| 259 | =item $order_data | ||
| 260 | |||
| 261 | An hash reference which contains other informations that should be stored | ||
| 262 | with the order. (Like customer's name and address for example.) | ||
| 263 | |||
| 264 | =back | ||
| 265 | |||
| 266 | The cart is emptied if the checkout is successful. NOTE: This is the | ||
| 267 | only method in the framework that will do an explicit commit to make | ||
| 268 | sure that the order is correctly entered in the database. | ||
| 269 | |||
| 270 | The method returns an hash references which contains the order | ||
| 271 | informations. | ||
| 272 | |||
| 273 | =cut | ||
| 274 | |||
| 275 | sub checkout { | ||
| 276 | my ( $self, $name, $cart, $order_data ) = @_; | ||
| 277 | |||
| 278 | $self->load_profiles; | ||
| 279 | |||
| 280 | my $order = $self->{profiles}{$name}; | ||
| 281 | die "No such profile $name\n" unless $order; | ||
| 282 | |||
| 283 | my $DB = $self->{DB}; | ||
| 284 | my $order_no; | ||
| 285 | my $order_fields; | ||
| 286 | |||
| 287 | # Insert order in database. | ||
| 288 | eval { | ||
| 289 | $order_no = $DB->template_get( $order->{order_no} )->{order_no}; | ||
| 290 | die "No order_no returned by query\n" unless defined $order_no; | ||
| 291 | |||
| 292 | # Insert order | ||
| 293 | $order_fields = build_order_fields( $order,$order_no, | ||
| 294 | $order_data, $cart ); | ||
| 295 | $DB->record_insert( $order->{order_template}, $order_fields ); | ||
| 296 | $order_fields->{items} = []; | ||
| 297 | |||
| 298 | # Insert order items | ||
| 299 | foreach my $item ( $cart->items ) { | ||
| 300 | my $item_data = { order_no => $order_no, | ||
| 301 | %$item, | ||
| 302 | }; | ||
| 303 | build_multi_fields( $item_data, "discount", | ||
| 304 | $item->{discount}, | ||
| 305 | $order->{item_discount_fields} | ||
| 306 | ); | ||
| 307 | $DB->record_insert( $order->{order_item_template}, | ||
| 308 | $item_data ); | ||
| 309 | push @{$order_fields->{items}}, $item_data; | ||
| 310 | } | ||
| 311 | # Commit | ||
| 312 | $DB->commit; | ||
| 313 | }; | ||
| 314 | if ($@) { | ||
| 315 | $DB->rollback; | ||
| 316 | die $@; | ||
| 317 | } | ||
| 318 | |||
| 319 | # Here we may handle online payment and | ||
| 320 | # other stuff | ||
| 321 | |||
| 322 | # Update status to processeed | ||
| 323 | eval { | ||
| 324 | $DB->record_update( $order->{order_template}, | ||
| 325 | { order_no => $order_no, | ||
| 326 | status => "processed", | ||
| 327 | } | ||
| 328 | ); | ||
| 329 | # Commit | ||
| 330 | $DB->commit; | ||
| 331 | }; | ||
| 332 | if ($@) { | ||
| 333 | $DB->rollback; | ||
| 334 | die $@; | ||
| 335 | } | ||
| 336 | |||
| 337 | # Empty the cart | ||
| 338 | $cart->empty; | ||
| 339 | |||
| 340 | return $order_fields; | ||
| 341 | } | ||
| 342 | |||
| 343 | =pod | ||
| 344 | |||
| 345 | =head2 order_report ( $order, $template ) | ||
| 346 | |||
| 347 | This method will generate an order report for a particular order. The | ||
| 348 | first parameter is the order as returned by C<checkout>, the second is | ||
| 349 | the template file that should be used. This is a standard | ||
| 350 | Apache::iNcom pages. The report will be generated in the namespace of | ||
| 351 | the calling page, so standard Apache::iNcom globals will be | ||
| 352 | accessible. DONT ABUSE IT. Also the order for which the report is generated | ||
| 353 | will be accessible through the %order global hash. | ||
| 354 | |||
| 355 | =cut | ||
| 356 | use vars qw( %order ); | ||
| 357 | |||
| 358 | sub order_report { | ||
| 359 | my ( $self, $order, $report_tmpl ) = @_; | ||
| 360 | |||
| 361 | # Generate report | ||
| 362 | my $report; | ||
| 363 | my $request = $self->{request}; | ||
| 364 | |||
| 365 | no strict 'refs'; | ||
| 366 | *order = $order; | ||
| 367 | local *{"$request->{package}\:\:order"} = \%order; | ||
| 368 | |||
| 369 | my $params = { output => \$report, | ||
| 370 | inputfile => $report_tmpl, | ||
| 371 | # optDisableHtmlScan + optRawInput + | ||
| 372 | # optKeepSpaces + optReturnError | ||
| 373 | options => 512|16|1048576||262144 }; | ||
| 374 | my $result = Apache::iNcom::Request::Include( $params ); | ||
| 375 | die "Error in report generation: $result\n" if ($result != OK ); | ||
| 376 | |||
| 377 | return $report; | ||
| 378 | } | ||
| 379 | |||
| 380 | |||
| 381 | 1; | ||
| 382 | |||
| 383 | __END__ |