See also: OrderModifier
See also: OrderItem
Inheritance: extends DataObject
 /**
  * Populate some OrderItem object attributes before
  * writing them to the OrderItem DB record.
  *
  * PRECONDITION: The order item is not saved in the database yet.
  */
 function onBeforeWrite()
 {
     parent::onBeforeWrite();
     //always keep quantity above 0
     if ($this->Quantity < 1) {
         $this->Quantity = 1;
     }
     $this->CalculateTotal();
 }
 /**
  * Recalculate total before saving to database.
  */
 public function onBeforeWrite()
 {
     parent::onBeforeWrite();
     if ($this->OrderID && $this->Order() && $this->Order()->isCart()) {
         $this->calculatetotal();
     }
 }
 /**
  * standard SS method
  */
 function onBeforeWrite()
 {
     parent::onBeforeWrite();
 }
 /**
  * Get the amount saved on the given order with this discount.
  * 
  * @param  Order  $order order to match against
  * @return double  savings amount
  */
 public function getSavingsForOrder(Order $order)
 {
     $itemsavings = OrderAttribute::get()->innerJoin("Product_OrderItem_Discounts", "\"OrderAttribute\".\"ID\" = \"Product_OrderItem_Discounts\".\"Product_OrderItemID\"")->filter("Product_OrderItem_Discounts.DiscountID", $this->ID)->filter("OrderAttribute.OrderID", $order->ID)->sum("DiscountAmount");
     $modifiersavings = OrderAttribute::get()->innerJoin("OrderDiscountModifier_Discounts", "\"OrderAttribute\".\"ID\" = \"OrderDiscountModifier_Discounts\".\"OrderDiscountModifierID\"")->filter("OrderDiscountModifier_Discounts.DiscountID", $this->ID)->filter("OrderAttribute.OrderID", $order->ID)->sum("DiscountAmount");
     return $itemsavings + $modifiersavings;
 }
 /**
  * Before this OrderModifier is written to
  * the database, we set some of the fields
  * based on the way it was set up
  * {@link OrderModifier::is_chargable()}.
  * 
  * Precondition: The order item is not
  * saved in the database yet.
  */
 function onBeforeWrite()
 {
     parent::onBeforeWrite();
     $this->Amount = $this->Amount();
     $this->Type = $this->stat('is_chargable') ? 'Chargable' : 'Deductable';
 }
 /**
  * Standard SS method
  * the method below is very important...
  * We initialise the order once it has an OrderItem
  */
 function onAfterWrite()
 {
     parent::onAfterWrite();
     $order = $this->Order();
     if ($order) {
         if (!$order->StatusID) {
             //this adds the modifiers and automatically WRITES AGAIN - WATCH RACING CONDITIONS!
             $order->init(true);
         }
     }
 }
 function mergeUncompletedOrderForOneMember_170()
 {
     $explanation = "\r\n\t\t\t<h1>170. Merge uncompleted orders into one.</h1>\r\n\t\t\t<p>Merges uncompleted orders by the same user into one.</p>\r\n\t\t";
     if ($this->retrieveInfoOnly) {
         return $explanation;
     } else {
         echo $explanation;
     }
     $orders = Order::get()->filter(array("MemberID:GreaterThan" => 0))->sort(array("MemberID" => "ASC", "\"Order\".\"Created\"" => "DESC"))->innerJoin("Member", "\"Order\".\"MemberID\" = \"Member\".\"ID\"")->limit($this->limit, $this->start);
     $count = 0;
     $previousOrderMemberID = 0;
     $lastOrderFromMember = null;
     if ($orders->count()) {
         foreach ($orders as $order) {
             //crucial ONLY for non-submitted orders...
             if ($order->IsSubmitted()) {
                 //do nothing!
                 $count++;
             } else {
                 $memberID = $order->MemberID;
                 //recurring member
                 if ($previousOrderMemberID == $memberID && $lastOrderFromMember) {
                     $this->DBAlterationMessageNow("We have a duplicate order for a member: " . $order->Member()->Email, "created");
                     $orderAttributes = OrderAttribute::get()->filter(array("OrderID" => $order->ID));
                     if ($orderAttributes->count()) {
                         foreach ($orderAttributes as $orderAttribute) {
                             $this->DBAlterationMessageNow("Moving attribute #" . $orderAttribute->ID, "created");
                             DB::query("UPDATE \"OrderAttribute\" SET \"OrderID\" = " . $lastOrderFromMember->ID . " WHERE \"ID\" = " . $orderAttribute->ID);
                         }
                     } else {
                         $this->DBAlterationMessageNow("There are no attributes for this order");
                     }
                     $orderStatusLogs = OrderStatusLog::get()->filter(array("OrderID" => $order->ID));
                     if ($orderStatusLogs->count()) {
                         foreach ($orderStatusLogs as $orderStatusLog) {
                             $this->DBAlterationMessageNow("Moving order status log #" . $orderStatusLog->ID, "created");
                             DB::query("UPDATE \"OrderStatusLog\" SET \"OrderID\" = " . $lastOrderFromMember->ID . " WHERE \"ID\" = " . $orderStatusLog->ID);
                         }
                     } else {
                         $this->DBAlterationMessageNow("There are no order status logs for this order");
                     }
                     $orderEmailRecords = OrderEmailRecord::get()->filter(array("OrderID" => $order->ID));
                     if ($orderEmailRecords->count()) {
                         foreach ($orderEmailRecords as $orderEmailRecord) {
                             DB::query("UPDATE \"OrderEmailRecord\" SET \"OrderID\" = " . $lastOrderFromMember->ID . " WHERE \"ID\" = " . $orderEmailRecord->ID);
                             $this->DBAlterationMessageNow("Moving email #" . $orderEmailRecord->ID, "created");
                         }
                     } else {
                         $this->DBAlterationMessageNow("There are no emails for this order.");
                     }
                 } else {
                     $previousOrderMemberID = $order->MemberID;
                     $lastOrderFromMember = $order;
                     $this->DBAlterationMessageNow("Found last order from member.");
                 }
                 if ($order->BillingAddressID && !$lastOrderFromMember->BillingAddressID) {
                     $this->DBAlterationMessageNow("Moving Billing Address.");
                     DB::query("UPDATE \"Order\" SET \"BillingAddressID\" = " . $order->BillingAddressID . " WHERE \"ID\" = " . $lastOrderFromMember->ID);
                     DB::query("UPDATE \"BillingAddress\" SET \"OrderID\" = " . $lastOrderFromMember->ID . " WHERE \"ID\" = " . $order->BillingAddressID);
                 }
                 if ($order->ShippingAddressID && !$lastOrderFromMember->ShippingAddressID) {
                     $this->DBAlterationMessageNow("Moving Shipping Address.");
                     DB::query("UPDATE \"Order\" SET \"ShippingAddressID\" = " . $order->ShippingAddressID . " WHERE \"ID\" = " . $lastOrderFromMember->ID);
                     DB::query("UPDATE \"ShippingAddress\" SET \"OrderID\" = " . $lastOrderFromMember->ID . " WHERE \"ID\" = " . $order->ShippingAddressID);
                 }
                 $order->delete();
             }
         }
         $this->DBAlterationMessageNow("Ignored {$count} Orders that have already been submitted.");
         return $this->start + $this->limit;
     } else {
         $this->DBAlterationMessageNow("There were no orders at all to work through.");
     }
     return 0;
 }
 /**
  * Populate some OrderItem object attributes before
  * writing them to the OrderItem DB record.
  * 
  * PRECONDITION: The order item is not saved in the database yet.
  */
 function onBeforeWrite()
 {
     parent::onBeforeWrite();
     $this->Quantity = $this->_quantity;
 }