/**
  * adding a sequential order number.
  */
 function onBeforeWrite()
 {
     parent::onBeforeWrite();
     if ($order = $this->Order()) {
         if (!$this->Total) {
             $this->Total = $order->Total();
             $this->SubTotal = $order->SubTotal();
         }
     }
     if (!intval($this->SequentialOrderNumber)) {
         $this->SequentialOrderNumber = 1;
         $min = intval(EcommerceConfig::get("Order", "order_id_start_number")) - 0;
         if (isset($this->ID)) {
             $id = intval($this->ID);
         } else {
             $id = 0;
         }
         $lastOne = OrderStatusLog_Submitted::get()->Exclude(array("ID" => $id))->Sort("SequentialOrderNumber", "DESC")->First();
         if ($lastOne) {
             $this->SequentialOrderNumber = intval($lastOne->SequentialOrderNumber) + 1;
         }
         if (intval($min) && $this->SequentialOrderNumber < $min) {
             $this->SequentialOrderNumber = $min;
         }
     }
 }
 function updateOrderStatusLogSequentialOrderNumber_190()
 {
     $explanation = "\r\n\t\t\t<h1>190. Set sequential order numbers</h1>\r\n\t\t\t<p>Prepopulates old orders for OrderStatusLog_Submitted.SequentialOrderNumber.</p>\r\n\t\t";
     if ($this->retrieveInfoOnly) {
         return $explanation;
     } else {
         echo $explanation;
     }
     $submittedOrdersLog = OrderStatusLog_Submitted::get()->sort("Created", "ASC")->limit($this->limit, $this->start);
     $changes = 0;
     if ($submittedOrdersLog->count()) {
         foreach ($submittedOrdersLog as $submittedOrderLog) {
             $old = $submittedOrderLog->SequentialOrderNumber;
             $submittedOrderLog->write();
             $new = $submittedOrderLog->SequentialOrderNumber;
             if ($old != $new) {
                 $changes++;
                 $this->DBAlterationMessageNow("Changed the SequentialOrderNumber for order #" . $submittedOrderLog->OrderID . " from {$old} to {$new} ");
             }
         }
         if (!$changes) {
             $this->DBAlterationMessageNow("There were no changes in any of the OrderStatusLog_Submitted.SequentialOrderNumber fields.");
         }
         return $this->start + $this->limit;
     } else {
         $this->DBAlterationMessageNow("There are no logs to update.");
     }
     return 0;
 }