Untitled

mail@pastecode.io avatar
unknown
plain_text
2 years ago
3.4 kB
2
Indexable
Never
public function db_add_order_offline_payment($payment_method, $cart_ids, $notes = null,$user_address_id = null)
	{
		$order_status = "awaiting_approval";
		$payment_status = "awaiting_payment";
		if ($payment_method == 'Cash On Delivery') {
			$order_status = "order_processing";
		}

		//batch langsung bayar
		$new_order_status = 'waiting_approval';
		$cart_items = $this->cart_model->api_get_sess_cart_items();
		if (!empty($cart_items)) {
			foreach ($cart_items as $cart_item) {
				$product = get_available_product($cart_item->product_id);
				if (!empty($product->last_order)) {
						$new_order_status = 'waiting_payment';
				}
			}
		}

		$cart_total = $this->cart_model->db_calculate_cart_total($cart_ids);

		if (!empty($cart_total)) {
			$couponAmount = 0;
			//check coupon
			$coupon = $this->user_coupon_model->get_active_coupon();
			$coupon_amount = null;
			$user_coupon_id = null;
			if ($coupon) {
	            $couponConditions['coupons.id'] = $coupon->coupon_id;
	            $couponData = getCouponData($couponConditions);
	            if (isset($couponData[$coupon->coupon_id])) {
	                $coupon->coupon = $couponData[$coupon->coupon_id];
					$today = date("Y-m-d H:i:s");
					$expire = $coupon->expired_at; //from database
					$today_time = strtotime($today);
					$expire_time = strtotime($expire);

					if ($expire_time < $today_time) {
						return trans('coupon_expired');
					}
					if (!empty($coupon->coupon->minimum_order_amount)) {
						if ($cart_total->subtotal < (int)$coupon->coupon->minimum_order_amount) {
							return trans('minimum_order_not_achieve');
						}
					}
					$couponAmount = calculate_coupon($coupon->coupon, $cart_total->subtotal);
					$user_coupon_id = $coupon->id;
					$this->user_coupon_model->used($user_coupon_id);
	            }
	        }

			$data = array(
				'order_number' => uniqid(),
				'buyer_id' => 0,
				'buyer_type' => "guest",
				'price_subtotal' => $cart_total->subtotal,
				'price_shipping' => $cart_total->shipping_cost,
				'user_coupon_id' => $user_coupon_id,
				'coupon_amount' => $couponAmount,
				'price_total' => $cart_total->total - $couponAmount,
				'price_total' => $cart_total->total,
				'order_status' => $new_order_status,
				'notes' => $notes,
				'price_currency' => $cart_total->currency,
				'status' => 0,
				'payment_method' => $payment_method,
				'payment_status' => $payment_status, <=====
				'shop_approval_expired_at' => date('Y-m-d H:i:s', strtotime(" +1 hours")),
				'updated_at' => date('Y-m-d H:i:s'),
				'created_at' => date('Y-m-d H:i:s')
			);


			if (auth_check()) {
				$data["buyer_type"] = "registered";
				$data["buyer_id"] = user()->id;
			}
			if ($this->db->insert('orders', $data)) {
				$order_id = $this->db->insert_id();

				//update order number
				$this->update_order_number($order_id);

				//add order shipping
				$this->db_add_order_shipping($order_id, $cart_ids, $user_address_id);

				//add order products
				$this->db_add_order_products($order_id, $order_status, $cart_ids);

				//set bidding quotes as completed
				$this->load->model('bidding_model');
				$this->bidding_model->db_set_bidding_quotes_as_completed_after_purchase($cart_ids);

				//clear cart
				$this->cart_model->db_process_cart($cart_ids);

				return $order_id;
			}
			return false;
		}
		return false;
	}