public function calculator(Request $request, $id) { $logistics = Logistics::findOrFail($id); $package = new Package(); $items = $request->json()->get('items', []); foreach ($items as $index => $item) { $package->add($item); } $package->to($request->json()->get('to')); return ['price' => $logistics->deliverer->calc($package)]; }
/** * 根据当前的订单信息及给定的商品计算订单价格 * @param $order_products * @return array */ public function calc($order_products = null) { $total_amount = $subtotal_product = $subtotal_tax = $subtotal_logistics = $subtotal_discount = 0; $products = $order_products === null ? $this->products : $order_products; // total_amount = subtotal_product + subtotal_tax + subtotal_logistics - subtotal_discount $package = new Package(); $package->to($this->logistics_region); foreach ($products as $product) { $subtotal_product += $product->price * $product->quantity; $subtotal_discount += $product->discount * $product->quantity; $subtotal_tax += $product->tax * $product->quantity; $package->add($product->quantity); } $subtotal_discount += $this->order_discount; $subtotal_logistics = $this->logistics->deliverer->calc($package); $this->subtotal_product = $subtotal_product; $this->subtotal_tax = $subtotal_tax; $this->subtotal_discount = $subtotal_discount; $this->subtotal_logistics = $subtotal_logistics; $this->total_amount = $subtotal_product + $subtotal_tax + $subtotal_logistics - $subtotal_discount; return $this; }