Completed
Push — master ( 3b65eb...dc665d )
by Nicolaas
11:00 queued 02:51
created

tasks/EcommerceTaskArchiveAllSubmittedOrders.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
/**
4
 * After a bug in the saving of orders in the CMS
5
 * This "fixer"  was introduced to fix older orders
6
 * without a submission record.
7
 *
8
 * @authors: Nicolaas [at] Sunny Side Up .co.nz
9
 * @package: ecommerce
10
 * @sub-package: tasks
11
 * @inspiration: Silverstripe Ltd, Jeremy
12
 **/
13
class EcommerceTaskArchiveAllSubmittedOrders extends BuildTask
14
{
15
    protected $title = 'Archive all submitted orders';
16
17
    protected $description = "
18
    This task moves all orders to the 'Archived' (last) Order Step without running any of the tasks in between.";
19
20
    public function run($request)
21
    {
22
        //IMPORTANT!
23
        Config::inst()->update('Email', 'send_all_emails_to', 'no-one@localhost');
24
        Email::set_mailer(new Ecommerce_Dummy_Mailer());
0 ignored issues
show
Deprecated Code introduced by
The method Email::set_mailer() has been deprecated with message: since version 4.0

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
25
        $orderStatusLogClassName = 'OrderStatusLog';
26
        $submittedOrderStatusLogClassName = EcommerceConfig::get('OrderStatusLog', 'order_status_log_class_used_for_submitting_order');
27
        if ($submittedOrderStatusLogClassName) {
28
            $sampleSubmittedStatusLog = $submittedOrderStatusLogClassName::get()
29
                ->First();
30
            if ($sampleSubmittedStatusLog) {
31
                $lastOrderStep = OrderStep::get()->sort('Sort', 'DESC')->First();
32
                if ($lastOrderStep) {
33
                    $joinSQL = "INNER JOIN \"$orderStatusLogClassName\" ON \"$orderStatusLogClassName\".\"OrderID\" = \"Order\".\"ID\"";
34
                    $whereSQL = 'WHERE "StatusID" <> '.$lastOrderStep->ID." AND \"$orderStatusLogClassName\".ClassName = '$submittedOrderStatusLogClassName'";
35
                    $count = DB::query("
36
                        SELECT COUNT (\"Order\".\"ID\")
37
                        FROM \"Order\"
38
                        $joinSQL
39
                        $whereSQL
40
                    ")->value();
41
                    $do = DB::query("
42
                        UPDATE \"Order\"
43
                        $joinSQL
44
                        SET \"Order\".\"StatusID\" = ".$lastOrderStep->ID."
45
                        $whereSQL
46
                    ");
47
                    if ($count) {
48
                        DB::alteration_message("NOTE: $count records were updated.", 'created');
49
                    } else {
50
                        DB::alteration_message('No records were updated.');
51
                    }
52
                } else {
53
                    DB::alteration_message('Could not find the last order step.', 'deleted');
54
                }
55
            } else {
56
                DB::alteration_message('Could not find any submitted order logs.', 'deleted');
57
            }
58
        } else {
59
            DB::alteration_message('Could not find a class name for submitted orders.', 'deleted');
60
        }
61
    }
62
}
63