|
[if-mm advanced order]
[button form=transactions bold=1 text="Ship checked order lines, send email to customer if appropriate"]
mv_todo=back
order=[cgi code]
[tag flag write]orderline transactions[/tag]
[perl tables="orderline transactions userdb __UI_META_TABLE__"]
my $odb = $Db{orderline}
or die "No orderline database!\n";
my $tdb = $Db{transactions}
or die "No transactions database!\n";
my $udb = $Db{userdb}
or die "No userdb database!\n";
my $on = $Scratch->{ship_notice_order_number} = $CGI->{code};
my $user = $tdb->field($on, 'username');
my $wants_copy = $udb->field($user, 'email_copy');
#Log("Order number=$on username=$user wants=$wants_copy");
delete $Scratch->{ship_notice_username};
delete $Scratch->{ship_notice_email};
if($wants_copy) {
$Scratch->{ship_notice_username} = $user;
$Scratch->{ship_notice_email} = $udb->field($user, 'email')
or delete $Scratch->{ship_notice_username};
}
if($CGI->{status} =~ /\d\d\d\d/) {
$tdb->set_field($on, 'status', $CGI->{status});
}
else {
$tdb->set_field($on, 'status', 'shipped');
}
my @shiplines = grep /\S/, split /\0/, $CGI->{lines_shipped};
my $to_ship = scalar @shiplines;
my $count_q = "select * from orderline where order_number = '$on'";
my $lines_ary = $odb->query($count_q);
if(! $lines_ary) {
$Scratch->{ui_message} = "No order lines for order $on";
return;
}
my $total_lines = scalar @$lines_ary;
my $odb_keypos = $odb->config('KEY_INDEX');
# See if some items have already shipped
my %already;
for(@$lines_ary) {
my $code = $_->[$odb_keypos];
my $status = $odb->field($code, 'status');
if($status eq 'shipped') {
$already{$code} = $code;
}
}
my $ship_mesg;
my $g_status;
my %shipping;
@shipping = grep ! $already{$_}, @shipping;
@shipping{@shiplines} = @shiplines;
if($total_lines == $to_ship) {
$ship_mesg = "Order $on complete, $total_lines lines set shipped.";
$Scratch->{ship_notice_complete} = $ship_mesg;
$g_status = 'shipped';
}
else {
$ship_mesg = "Order $on partially shipped ($to_ship of $total_lines lines).";
delete $Scratch->{ship_notice_complete};
$g_status = 'partial';
}
my $minor_mesg = '';
my $email_mesg = $Scratch->{ship_notice_username}
? "Email copy sent to $Scratch->{ship_notice_email}."
: "No email copy sent as per user preference.";
# Actually update the orderline database
for(@$lines_ary) {
my $code = $_->[$odb_keypos];
next if $already{$code};
my $status = $shipping{$code} ? 'shipped' : 'backorder';
$odb->set_field($code, 'status', $status)
or do {
$Scratch->{ui_message} = "Orderline $code ship status update failed.";
return;
};
}
for(keys %already) {
$shipping{$_} = $_;
}
my $total_shipped_now = scalar keys %shipping;
delete $Scratch->{ship_now_complete};
if (
$total_lines != scalar @shipping
and
$total_shipped_now == $total_lines
)
{
$g_status = 'shipped';
$Scratch->{ship_now_complete} = 1
if $total_shipped_now == $total_lines;
$ship_mesg = "Order $on now complete (all $total_lines lines).";
}
$tdb->set_field($on, 'status', $g_status);
$Scratch->{ui_message} = "$ship_mesg $email_mesg";
delete $Scratch->{ship_notice_username};
delete $Scratch->{ship_notice_email};
if($wants_copy) {
$Scratch->{ship_notice_username} = $user;
$Scratch->{ship_notice_email} = $tdb->field($on, 'email')
or $Scratch->{ship_notice_email} = $tdb->field($on, 'email')
or delete $Scratch->{ship_notice_username};
}
return;
[/perl]
[if scratch ship_notice_username]
[email-raw][include etc/ship_notice][/email-raw]
[/if]
[/button]
[loop prefix=partial list="[scratch order_lines_tmp]"]
Line [partial-increment] ([partial-filter 16][partial-data orderline description][/partial-filter])
[/loop]
[button bold=1 text="Change global status"]
order=[cgi code]
[/button]
[button text="Cancel"]
mv_nextpage=__UI_BASE__/order_view
order=[cgi code]
mv_todo=back
[/button]
[else]
[button text="Back"]
mv_nextpage=__UI_BASE__/order_view
order=[cgi code]
mv_todo=back
[/button]
[/else]
[/if-mm]
|