public function addAction()
 {
     $validator = Validator::make(Input::all(), ["account" => "required|exists:account,id", "items" => "required"]);
     if ($validator->passes()) {
         $order = Order::create(["account_id" => Input::get("account")]);
         try {
             $items = json_decode(Input::get("items"));
         } catch (Exception $e) {
             return Response::json(["status" => "error", "errors" => ["items" => ["Invalid items format."]]]);
         }
         $total = 0;
         foreach ($items as $item) {
             $orderItem = OrderItem::create(["order_id" => $order->id, "product_id" => $item->product_id, "quantity" => $item->quantity]);
             $product = $orderItem->product;
             $orderItem->price = $product->price;
             $orderItem->save();
             $product->stock -= $item->quantity;
             $product->save();
             $total += $orderItem->quantity * $orderItem->price;
         }
         $result = $this->gateway->pay(Input::get("number"), Input::get("expiry"), $total, "usd");
         if (!$result) {
             return Response::json(["status" => "error", "errors" => ["gateway" => ["Payment error"]]]);
         }
         $account = $order->account;
         $document = $this->document->create($order);
         $this->messenger->send($order, $document);
         return Response::json(["status" => "ok", "order" => $order->toArray()]);
     }
     return Response::json(["status" => "error", "errors" => $validator->errors()->toArray()]);
 }
 public function run()
 {
     $faker = $this->getFaker();
     $orders = Order::all();
     $products = Product::all()->toArray();
     foreach ($orders as $order) {
         $used = [];
         for ($i = 0; $i < rand(1, 5); $i++) {
             $product = $faker->randomElement($products);
             if (!in_array($product["id"], $used)) {
                 $id = $product["id"];
                 $price = $product["price"];
                 $quantity = rand(1, 3);
                 OrderItem::create(["order_id" => $order->id, "product_id" => $id, "price" => $price, "quantity" => $quantity]);
                 $used[] = $product["id"];
             }
         }
     }
 }
Пример #3
0
 /**
  * Creating the order item for an order
  *
  * @param Order    $order
  * @param stdClass $itemObj
  *
  * @return Ambigous <Ambigous, OrderItem, BaseEntityAbstract>
  */
 private function _createItem(Order $order, $itemObj)
 {
     $productXml = CatelogConnector::getConnector(B2BConnector::CONNECTOR_TYPE_CATELOG, $this->_getWSDL(), $this->_getApiUser(), $this->_getApiKey())->getProductInfo(trim($itemObj->sku));
     $product = Product::create(trim($itemObj->sku), trim($itemObj->name), trim($itemObj->product_id));
     if (($updateOptions = trim($itemObj->product_options)) !== '' && is_array($updateOptions = unserialize($updateOptions))) {
         if (isset($updateOptions['options'])) {
             $stringArray = array();
             foreach ($updateOptions['options'] as $option) {
                 $stringArray[] = '<b>' . trim($option['label']) . '</b>';
                 $stringArray[] = trim($option['print_value']);
                 $stringArray[] = '';
             }
             $updateOptions = '<br />' . implode('<br />', $stringArray);
         } else {
             $updateOptions = '';
         }
     }
     return OrderItem::create($order, $product, trim($itemObj->price) * 1.1, trim($itemObj->qty_ordered), trim($itemObj->row_total) * 1.1, trim($itemObj->item_id), null, $product->getName() . $updateOptions);
 }
Пример #4
0
 /**
  * saveOrder
  *
  * @param unknown $sender
  * @param unknown $param
  *
  * @throws Exception
  *
  */
 public function saveOrder($sender, $param)
 {
     $results = $errors = array();
     try {
         Dao::beginTransaction();
         $customer = Customer::get(trim($param->CallbackParameter->customer->id));
         if (!$customer instanceof Customer) {
             throw new Exception('Invalid Customer passed in!');
         }
         if (!isset($param->CallbackParameter->type) || ($type = trim($param->CallbackParameter->type)) === '' || !in_array($type, Order::getAllTypes())) {
             throw new Exception('Invalid type passed in!');
         }
         $order = null;
         if (isset($param->CallbackParameter->orderId) && ($orderId = trim($param->CallbackParameter->orderId)) !== '') {
             if (!($order = Order::get($orderId)) instanceof Order) {
                 throw new Exception('Invalid Order to edit!');
             }
         }
         $orderCloneFrom = null;
         if (isset($param->CallbackParameter->orderCloneFromId) && ($orderCloneFromId = trim($param->CallbackParameter->orderCloneFromId)) !== '') {
             if (!($orderCloneFrom = Order::get($orderCloneFromId)) instanceof Order) {
                 throw new Exception('Invalid Order to clone from!');
             }
         }
         $shipped = isset($param->CallbackParameter->shipped) && intval($param->CallbackParameter->shipped) === 1;
         $poNo = isset($param->CallbackParameter->poNo) && trim($param->CallbackParameter->poNo) !== '' ? trim($param->CallbackParameter->poNo) : '';
         if (isset($param->CallbackParameter->shippingAddr)) {
             $shippAddress = $order instanceof Order ? $order->getShippingAddr() : null;
             $shippAddress = Address::create($param->CallbackParameter->shippingAddr->street, $param->CallbackParameter->shippingAddr->city, $param->CallbackParameter->shippingAddr->region, $param->CallbackParameter->shippingAddr->country, $param->CallbackParameter->shippingAddr->postCode, $param->CallbackParameter->shippingAddr->contactName, $param->CallbackParameter->shippingAddr->contactNo, $param->CallbackParameter->shippingAddr->companyName, $shippAddress);
         } else {
             $shippAddress = $customer->getShippingAddress();
         }
         $printItAfterSave = false;
         if (isset($param->CallbackParameter->printIt)) {
             $printItAfterSave = intval($param->CallbackParameter->printIt) === 1 ? true : false;
         }
         if (!$order instanceof Order) {
             $order = Order::create($customer, $type, null, '', OrderStatus::get(OrderStatus::ID_NEW), new UDate(), false, $shippAddress, $customer->getBillingAddress(), false, $poNo, $orderCloneFrom);
         } else {
             $order->setType($type)->setPONo($poNo)->save();
         }
         $totalPaymentDue = 0;
         if (trim($param->CallbackParameter->paymentMethodId)) {
             $paymentMethod = PaymentMethod::get(trim($param->CallbackParameter->paymentMethodId));
             if (!$paymentMethod instanceof PaymentMethod) {
                 throw new Exception('Invalid PaymentMethod passed in!');
             }
             $totalPaidAmount = trim($param->CallbackParameter->totalPaidAmount);
             $order->addPayment($paymentMethod, $totalPaidAmount);
             $order = Order::get($order->getId());
             $order->addInfo(OrderInfoType::ID_MAGE_ORDER_PAYMENT_METHOD, $paymentMethod->getName(), true);
             if ($shipped === true) {
                 $order->setType(Order::TYPE_INVOICE);
             }
         } else {
             $paymentMethod = '';
             $totalPaidAmount = 0;
         }
         foreach ($param->CallbackParameter->items as $item) {
             $product = Product::get(trim($item->product->id));
             if (!$product instanceof Product) {
                 throw new Exception('Invalid Product passed in!');
             }
             if (isset($item->active) && intval($item->active) === 1 && intval($product->getActive()) !== 1 && $type === Order::TYPE_INVOICE) {
                 throw new Exception('Product(SKU:' . $product->getSku() . ') is DEACTIVATED, please change it to be proper product before converting it to a ' . $type . '!');
             }
             $unitPrice = trim($item->unitPrice);
             $qtyOrdered = trim($item->qtyOrdered);
             $totalPrice = trim($item->totalPrice);
             $itemDescription = trim($item->itemDescription);
             if (intval($item->active) === 1) {
                 $totalPaymentDue += $totalPrice;
                 if ($orderCloneFrom instanceof Order || !($orderItem = OrderItem::get($item->id)) instanceof OrderItem) {
                     $orderItem = OrderItem::create($order, $product, $unitPrice, $qtyOrdered, $totalPrice, 0, null, $itemDescription);
                 } else {
                     $orderItem->setActive(intval($item->active))->setProduct($product)->setUnitPrice($unitPrice)->setQtyOrdered($qtyOrdered)->setTotalPrice($totalPrice)->setItemDescription($itemDescription)->save();
                     $existingSellingItems = SellingItem::getAllByCriteria('orderItemId = ?', array($orderItem->getId()));
                     foreach ($existingSellingItems as $sellingItem) {
                         //DELETING ALL SERIAL NUMBER BEFORE ADDING
                         $sellingItem->setActive(false)->save();
                     }
                 }
                 $orderItem = OrderItem::get($orderItem->getId())->reCalMarginFromProduct()->resetCostNMarginFromKits()->save();
             } else {
                 if ($orderCloneFrom instanceof Order) {
                     continue;
                 } elseif (($orderItem = OrderItem::get($item->id)) instanceof OrderItem) {
                     $orderItem->setActive(false)->save();
                 }
             }
             if (isset($item->serials) && count($item->serials) > 0) {
                 foreach ($item->serials as $serialNo) {
                     $orderItem->addSellingItem($serialNo)->save();
                 }
             }
             if ($shipped === true) {
                 $orderItem->setIsPicked(true)->save();
             }
         }
         if (isset($param->CallbackParameter->courierId)) {
             $totalShippingCost = 0;
             $courier = null;
             if (is_numeric($courierId = trim($param->CallbackParameter->courierId))) {
                 $courier = Courier::get($courierId);
                 if (!$courier instanceof Courier) {
                     throw new Exception('Invalid Courier passed in!');
                 }
                 $order->addInfo(OrderInfoType::ID_MAGE_ORDER_SHIPPING_METHOD, $courier->getName(), true);
             } else {
                 $order->addInfo(OrderInfoType::ID_MAGE_ORDER_SHIPPING_METHOD, $courierId, true);
             }
             if (isset($param->CallbackParameter->totalShippingCost)) {
                 $totalShippingCost = StringUtilsAbstract::getValueFromCurrency(trim($param->CallbackParameter->totalShippingCost));
                 $order->addInfo(OrderInfoType::ID_MAGE_ORDER_SHIPPING_COST, StringUtilsAbstract::getCurrency($totalShippingCost), true);
             }
             if ($shipped === true) {
                 if (!$courier instanceof Courier) {
                     $courier = Courier::get(Courier::ID_LOCAL_PICKUP);
                 }
                 Shippment::create($shippAddress, $courier, '', new UDate(), $order, '');
             }
         } else {
             $courier = '';
             $totalShippingCost = 0;
         }
         $totalPaymentDue += $totalShippingCost;
         $comments = isset($param->CallbackParameter->comments) ? trim($param->CallbackParameter->comments) : '';
         $order = $order->addComment($comments, Comments::TYPE_SALES)->setTotalPaid($totalPaidAmount);
         if ($shipped === true) {
             $order->setStatus(OrderStatus::get(OrderStatus::ID_SHIPPED));
         }
         $order->setTotalAmount($totalPaymentDue)->save();
         if (isset($param->CallbackParameter->newMemo) && ($newMemo = trim($param->CallbackParameter->newMemo)) !== '') {
             $order->addComment($newMemo, Comments::TYPE_MEMO);
         }
         $results['item'] = $order->getJson();
         if ($printItAfterSave === true) {
             $results['printURL'] = '/print/order/' . $order->getId() . '.html?pdf=1';
         }
         $results['redirectURL'] = '/order/' . $order->getId() . '.html' . (trim($_SERVER['QUERY_STRING']) === '' ? '' : '?' . $_SERVER['QUERY_STRING']);
         Dao::commitTransaction();
     } catch (Exception $ex) {
         Dao::rollbackTransaction();
         $errors[] = $ex->getMessage();
     }
     $param->ResponseData = StringUtilsAbstract::getJson($results, $errors);
 }
Пример #5
0
 public function checkoutStore()
 {
     // dd(Input::all());
     $user = User::find(Auth::user()->id);
     $cart = Cart::content();
     $rules = ['alamat' => 'required', 'pembayaran' => 'required', 'total' => 'required'];
     Validator::make($data = Input::all(), $rules);
     $data['user_id'] = $user->id;
     $data['telp'] = $user->telp;
     $data['email'] = $user->email;
     $order = Order::create($data);
     if (!$order) {
         return Redirect::back()->with('message', 'gagal menambahkan order ke database');
     }
     $dataItems = [];
     foreach ($cart as $c) {
         $dataItems['order_id'] = $order->id;
         $dataItems['barang_id'] = $c->id;
         $dataItems['qty'] = $c->qty;
         $dataItems['price'] = $c->price;
         $dataItems['total'] = $c->price * $c->qty;
         $dataItems['berat'] = $c->options->berat * $c->qty;
         $dataItems['keterangan'] = $c->options->aroma;
         $orderItems = OrderItem::create($dataItems);
         if (!$orderItems) {
             return Redirect::back()->with('message', 'gagal menambahkan order item ke database');
         }
     }
     Cart::destroy();
     return Redirect::to('store')->with('message', 'Terima kasih, order anda berhasil masuk, silahkan tunggu untuk konfirmasi dari kami');
 }
Пример #6
0
 // get more product information
 $stmt = $product->readByIds($ids);
 // initialize total price
 $total_price = 0;
 // loop through product in the cart
 while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
     extract($row);
     // get user-inputted product quantity from session
     $quantity = $_SESSION['cart'][$id]['quantity'];
     // set values to order item properties
     $order_item->transaction_id = $transaction_id;
     $order_item->product_id = $id;
     $order_item->price = $price;
     $order_item->quantity = $quantity;
     // create the order item
     $order_item->create();
     // compute subtotal
     $sub_total = $price * $quantity;
     // compute total price
     $total_price += $sub_total;
 }
 // save order information
 $order->user_id = $_SESSION['user_id'];
 $order->transaction_id = $transaction_id;
 $order->total_cost = $total_price;
 $order->created = date("Y-m-d H:i:s");
 // create the order
 $order->create();
 // remove cart items
 unset($_SESSION['cart']);
 // tell the user order has been placed
Пример #7
0
 /**
  * adding a item onto the order
  *
  * @param Product $product
  * @param number  $unitPrice
  * @param number  $qty
  * @param number  $totalPrice
  * @param number  $mageOrderItemId The order_item_id from Magento
  * @param string  $eta
  *
  * @return PurchaseOrder
  */
 public function addItem(Product $product, $unitPrice = '0.0000', $qty = 1, $totalPrice = null, $mageOrderItemId = null, $eta = null, $itemDescription = '')
 {
     OrderItem::create($this, $product, $unitPrice, $qty, $totalPrice, $mageOrderItemId, $eta, $itemDescription);
     return $this;
 }