['name' => '彩色-单面', 'amount' => 0, 'quantity' => 0,'discount' => 0], '1_2_1' => ['name' => '彩色-双面', 'amount' => 0, 'quantity' => 0,'discount' => 0], '2_1_1' => ['name' => '黑白-单面', 'amount' => 0, 'quantity' => 0,'discount' => 0], '2_2_1' => ['name' => '黑白-双面', 'amount' => 0, 'quantity' => 0,'discount' => 0], ]; /** * @param Request $request * @return Response */ #[Route(path: "create",methods: "post")] public function createOrder(Request $request): Response { try { $param = $this->_valid([ "shop.require" => trans("empty.require"), "print.require" => trans("empty.require"), "pay.require" => trans("empty.require"), "printName.default" => "", "express.require" => trans("empty.require"), "card.default" => [], "gift.default" => 0 ],$request->method()); if (!is_array($param)) return error($param); $cart = (new SaasCart)->where("shop_id",$param['shop'])->order("create_at desc")->select(); if ($cart->isEmpty()) return success('ok',['cart' => []]); $totalAmount = $totalDiscount = 0; foreach ($cart as $k=>$v){ $key = $v['color'] . '_' . $v['duplex'] . '_' . $v['source']; if (isset($this->types[$key])) { $this->types[$key]['quantity'] += $v['page']; $this->types[$key]['amount'] += $v['money']; } $cart[$k] = $v; $cart[$k]['money'] = format_money($v['money'] / 100,2); $cart[$k]['name'] = msubstr($v['name'],0,12); } $printData = (new SaasPrintClient)->where("shop_id",$param['shop'])->where("code",$param['print'])->select(); if ($printData->isEmpty()) return error('无可用打印机'); // 计算折扣 foreach ($this->types as $k=>$v) { $discount = (new SaasDiscount)->where("shop_id",$param['shop'])->where("keys",$k)->where("number",'<',$v['quantity'])->findOrEmpty(); if (!$discount->isEmpty()) { $v['discount'] = round($v['amount'] * $discount['rate']); $this->types[$k]['discount'] = $v['discount']; } $totalAmount += $v['amount']; // 实际金额 $totalDiscount += $v['discount']; // 折扣后金额 } $orderSn = CodeExtend::uniqidDate(16).date("is").rand(1,9); $totalDay = (new SaasOrder)->where("shop_id",$param['shop'])->whereDay("create_at")->count(); $orderData = [ "shop_id" => $param['shop'], // 所属店铺 "parent_id" => $param['shop'], // 消费店铺 "order_sn" => $orderSn, "money" => $totalAmount, "discount" => $totalDiscount, "uuid" => $request->user['id'], "print_name" => $param['printName'], "package" => $param['express'], "package_sn" => date('md')."-".sprintf("%02d",($totalDay+1)), "extra_money" => "", "remark" => $param['remark']??'' ]; if ($param['pay'] == 2) { // 会员卡支付或开通会员卡并充值 $card = (new SaasUser)->where("openid",$request->user['openid'])->where("shop_id",$param['shop'])->field("balance")->findOrEmpty(); if ($card->isEmpty()) // 创建会员并发起支付 { } if ($totalDiscount > 0 && $totalDiscount > $card['balance']) { return error("卡内余额不足~"); } if ($totalDiscount == 0 && $totalAmount > $card['balance']) { return error("卡内余额不足"); } } $orderDetail = []; foreach ($cart as $key=>$val) { unset($val['id']); $orderDetail[$key] = $val; $orderDetail[$key]['money'] = $val['money']*100; $orderDetail[$key]['order_sn'] = $orderSn; $orderDetail[$key]['print_name'] = $param['printName']; } print_r($orderDetail); } catch (\Throwable $throwable) { echo $throwable->getLine()."\n"; return error($throwable->getMessage()); } } }