Example #1
0
 public static function createFromInput(User $user, OrderPostRequest $request, ProductManagerInterface $productManager, $customer = null)
 {
     $fileName = null;
     $newFileName = null;
     if ($request->file('image')) {
         $originalFileName = $request->file('image')->getClientOriginalName();
         $fileName = 'upload_' . $user->id . mt_rand() . '.' . pathinfo($originalFileName, PATHINFO_EXTENSION);
         $newFileName = $request->file('image')->move(public_path('img/user'), $fileName);
         Log::info('Move ' . $newFileName);
     }
     $hasOrganizationMembership = $productManager->hasOrganizationMembership($request->getProductPricing());
     return self::proofOfTransferFromRequestWithoutImages($user, $request, "img/user/{$fileName}", $request->totalAmount($customer), $request->isHq(), $hasOrganizationMembership);
 }
 /**
  * @param User $user
  * @param $proofOfTransfer
  * @param array $products
  * @param array $quantityHash
  * @param $status
  * @param $customer
  * @param $isHq
  * @param bool $isPickup
  * @param $ipAddress
  * @return Order
  */
 private function createOrder(User $user, $proofOfTransfer, array $products, array $quantityHash, $status, $customer, $isHq, $isPickup = false, $ipAddress)
 {
     assert($user);
     assert($user->id);
     if ($customer) {
         assert($customer->referral_id == $user->id);
     }
     if (empty($products)) {
         \App::abort(500, 'invalid');
     }
     if (empty($quantityHash)) {
         \App::abort(500, 'invalid');
     }
     $allowedProducts = $this->productManager->getAvailableProductList($user, (bool) $customer)->pluck('id')->all();
     assert(!empty($allowedProducts));
     $organizationId = null;
     $hq = Organization::HQ();
     $organizationId = $isHq ? $hq->id : $user->organization_id;
     $orderModel = config('order.order_model');
     $order = new $orderModel();
     $order->fill(['order_status_id' => $status, 'proof_of_transfer_id' => $proofOfTransfer->id, 'customer_id' => $customer ? $customer->id : null, 'organization_id' => $organizationId, 'user_id' => $user->id, 'is_hq' => $isHq, 'is_pickup' => $isPickup, 'created_by_id' => $this->userManager->getRealUserId(), 'ip_address' => $ipAddress]);
     if ($this->date) {
         $order->created_at = $this->date;
     }
     $order->save();
     $index = 0;
     foreach ($products as $key => $product) {
         if (!config('order.allow_quantity') && $quantityHash[$key] != 1) {
             \App::abort(500, 'invalid');
         }
         if (env('APP_ENV') != 'production') {
             if (!in_array($product->id, $allowedProducts)) {
                 ddd([$user, (bool) $customer, $product, $allowedProducts]);
             }
         }
         assert(in_array($product->id, $allowedProducts));
         $product->getPriceAndDelivery($user, $customer, $price, $delivery);
         $organizationId = $product->is_hq ? $hq->id : $user->organization_id;
         assert($organizationId == $order->organization_id, 'organization_id');
         assert($isHq == $product->is_hq, 'is_hq');
         $quantity = $quantityHash[$key];
         assert($product->max_quantity >= $quantity);
         // by default awarded_user_id is auth user
         $awardedUserId = $user->id;
         // if product->awarded_parent is true the set referral id as awarded
         // Only applies to new system
         if ($product->award_parent) {
             assert($user->new_referral_id);
             $awardedUserId = $user->new_referral_id;
         }
         $orderItem = new OrderItem();
         $orderItem->fill(['product_id' => $product->id, 'order_id' => $order->id, 'quantity' => $quantity, 'product_price' => $product->isOtherProduct() ? $proofOfTransfer->amount : $price, 'delivery' => $delivery, 'index' => $index++, 'organization_id' => $organizationId, 'awarded_user_id' => $awardedUserId]);
         if ($this->date) {
             $orderItem->created_at = $this->date;
         }
         $orderItem->save();
     }
     $this->orderCreated($order);
     return $order;
 }