my $twentyone;
my $success = 0;
do {
$twentyone = 21;
my $onecard = $c->model('dRevAppDB::CardOrders')->search({ checked_out_by => { '=' => "" }, package_id => 7 });
my $twocard = $c->model('dRevAppDB::CardOrders')->search({ checked_out_by => { '=' => "" }, package_id => 8 });
my $threecard = $c->model('dRevAppDB::CardOrders')->search({ checked_out_by => { '=' => "" }, package_id => 9 });
my $fourcard = $c->model('dRevAppDB::CardOrders')->search({ checked_out_by => { '=' => "" }, package_id => 10 });
my $fivecard = $c->model('dRevAppDB::CardOrders')->search({ checked_out_by => { '=' => "" }, package_id => 11 });
# loop to check if we can piece together 21 cards without splitting a small order
foreach (((\$fivecard, 5), (\$fourcard, 4), (\$threecard, 3), (\$twocard, 2), (\$onecard, 1))) {
last if not $twentyone; # if we've reached zero, break the loop
for (my $a = ${$_[0]}->count; $a >= 0; $a--) {
last if $twentyone < $_[1]; # if we've reached zero, or if the current size would put us below, break the loop
$twentyone -= $_[1];
}
}
# if checking loop was successful, process the same orders again, but actually mark them
if ($twentyone == 0) {
$twentyone = 21; $success = 1;
# we need to create our new order, package_id 12, which is multiple-singles
my $multiorder = $c->model('dRevAppDB::CardOrders')->create({
package_id => 12,
});
foreach (([$fivecard, 5], [$fourcard, 4], [$threecard, 3], [$twocard, 2], [$onecard, 1])) {
last if not $twentyone;
for (my $a = $_[0]->count; $a >= 0; $a--) {
last if $twentyone < $_[1];
$twentyone -= $_[1];
#start processing the single card orders
$_[0]->next; # grab the next record in the resultset
my $order = $c->model('dRevAppDB::CardOrders')->find({ id => $_[0]->get_column('id')}); # use find to hit that record
$order->checked_out_by('system');
$order->update; # check it out and update the record
$c->model('dRevAppDB::MultipleOrderBundle')->create({ # create the relation, so we know which
parent => $multiorder->id, # single card orders belong to which
child => $order->id, # multiple order
});
}
}
}
$c->log->info("(Cards.pm) Total pending single card orders at this point: ");
} while ($success);