| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120 |
- <?php
- namespace app\controller\api;
- use app\extra\basic\Base;
- use app\extra\tools\CodeExtend;
- use app\middleware\WxMiddleware;
- use app\model\saas\SaasCart;
- use app\model\saas\SaasDiscount;
- use app\model\saas\SaasOrder;
- use app\model\saas\SaasPrintClient;
- use app\model\saas\SaasUser;
- use LinFly\Annotation\Route\Controller;
- use LinFly\Annotation\Route\Middleware;
- use LinFly\Annotation\Route\Route;
- use support\Request;
- use support\Response;
- #[Controller(prefix: "/wx_api/order"),Middleware(WxMiddleware::class)]
- class Order extends Base
- {
- protected array $noNeedLogin = [];
- protected array $types = [
- '1_1_1' => ['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());
- }
- }
- }
|