Esempio n. 1
0
 public function add($data, $pm, $status = null)
 {
     $taxBased = Config::get('vividstore.taxBased');
     $taxlabel = Config::get('vividstore.taxName');
     $this->set('taxlabel', $taxlabel);
     $taxCalc = Config::get('vividstore.calculation');
     $db = Database::get();
     //get who ordered it
     $customer = new Customer();
     //what time is it?
     $dt = Core::make('helper/date');
     $now = $dt->getLocalDateTime();
     //get the price details
     $shipping = VividCart::getShippingTotal();
     $shipping = Price::formatFloat($shipping);
     $taxvalue = VividCart::getTaxTotal();
     $taxName = Config::get('vividstore.taxName');
     $total = VividCart::getTotal();
     $total = Price::formatFloat($total);
     $tax = 0;
     $taxIncluded = 0;
     if ($taxCalc == 'extract') {
         $taxIncluded = $taxvalue;
     } else {
         $tax = $taxvalue;
     }
     $tax = Price::formatFloat($tax);
     //get payment method
     $pmID = $pm->getPaymentMethodID();
     //add the order
     $vals = array($customer->getUserID(), $now, $pmID, $shipping, $tax, $taxIncluded, $taxName, $total);
     $db->Execute("INSERT INTO VividStoreOrders(cID,oDate,pmID,oShippingTotal,oTax,oTaxIncluded,oTaxName,oTotal) VALUES (?,?,?,?,?,?,?,?)", $vals);
     $oID = $db->lastInsertId();
     $order = Order::getByID($oID);
     if ($status) {
         $order->updateStatus($status);
     } else {
         $order->updateStatus(OrderStatus::getStartingStatus()->getHandle());
     }
     $order->setAttribute("email", $customer->getEmail());
     $order->setAttribute("billing_first_name", $customer->getValue("billing_first_name"));
     $order->setAttribute("billing_last_name", $customer->getValue("billing_last_name"));
     $order->setAttribute("billing_address", $customer->getValueArray("billing_address"));
     $order->setAttribute("billing_phone", $customer->getValue("billing_phone"));
     $order->setAttribute("shipping_first_name", $customer->getValue("shipping_first_name"));
     $order->setAttribute("shipping_last_name", $customer->getValue("shipping_last_name"));
     $order->setAttribute("shipping_address", $customer->getValueArray("shipping_address"));
     $customer->setLastOrderID($oID);
     //add the order items
     $cart = VividCart::getCart();
     foreach ($cart as $cartItem) {
         $taxvalue = VividCart::getTaxProduct($cartItem['product']['pID']);
         $tax = 0;
         $taxIncluded = 0;
         if ($taxCalc == 'extract') {
             $taxIncluded = $taxvalue;
         } else {
             $tax = $taxvalue;
         }
         $productTaxName = $taxName;
         if ($taxvalue == 0) {
             $productTaxName = '';
         }
         OrderItem::add($cartItem, $oID, $tax, $taxIncluded, $productTaxName);
         $product = VividProduct::getByID($cartItem['product']['pID']);
         if ($product && $product->hasUserGroups()) {
             $usergroupstoadd = $product->getProductUserGroups();
             foreach ($usergroupstoadd as $id) {
                 $g = Group::getByID($id);
                 if ($g) {
                     $customer->getUserInfo()->enterGroup($g);
                 }
             }
         }
     }
     if (!$customer->isGuest()) {
         //add user to Store Customers group
         $group = \Group::getByName('Store Customer');
         if (is_object($group) || $group->getGroupID() < 1) {
             $customer->getUserInfo()->enterGroup($group);
         }
     }
     // create order event and dispatch
     $event = new OrderEvent($order);
     Events::dispatch('on_vividstore_order', $event);
     //send out the alerts
     $mh = new MailService();
     $pkg = Package::getByHandle('vivid_store');
     $fromEmail = Config::get('vividstore.emailalerts');
     if (!$fromEmail) {
         $fromEmail = "store@" . $_SERVER['SERVER_NAME'];
     }
     $alertEmails = explode(",", Config::get('vividstore.notificationemails'));
     $alertEmails = array_map('trim', $alertEmails);
     //receipt
     $mh->from($fromEmail);
     $mh->to($customer->getEmail());
     $mh->addParameter("order", $order);
     $mh->addParameter("taxbased", $taxBased);
     $mh->addParameter("taxlabel", $taxlabel);
     $mh->load("order_receipt", "vivid_store");
     $mh->sendMail();
     //order notification
     $mh->from($fromEmail);
     foreach ($alertEmails as $alertEmail) {
         $mh->to($alertEmail);
     }
     $mh->addParameter("order", $order);
     $mh->addParameter("taxbased", $taxBased);
     $mh->addParameter("taxlabel", $taxlabel);
     $mh->load("new_order_notification", "vivid_store");
     $mh->sendMail();
     VividCart::clear();
     return $order;
 }