1, 'is_opurchase' => 0, 'is_mer' => 1]; $arrs = Opurchaseclass::whereDay('create_time')->where($where1)->column('cart_id,id'); $cart_arr = []; $order_id = []; foreach ($arrs as $k => $v) { if (empty($v['cart_id'])) { self::setError('没有购物车信息'); return false; } $arr = explode(',', $v['cart_id']); foreach ($arr as $kk => $vv) { $cart_arr[] = $vv; } $order_id[] = $v['id']; } $where = ['is_pay' => 1, 'is_fail' => 0]; $cart_select = Cart::whereIn('cart_id', $cart_arr)->where($where)->field('goods_id as goods,cart_num')->select()->toArray(); if (empty($cart_select)) { self::setError('今日采购订单的购物车没有商品'); return false; } /** 计算价格 */ foreach ($cart_select as $k => $v) { $sell = Goods::where(['id' => $v['goods']])->value('sell'); $cart_select[$k]['total'] = bcmul($v['cart_num'], $sell, 2); $cart_select[$k]['price'] = $sell; $cart_select[$k]['is_mer'] = 2; } $order = [ 'time' => time(), 'number' => static::getNewOrderId('CG'), 'total' => array_sum(array_column($cart_select, 'total')), 'pay_type' => $params['pay_type'] ?? 0, 'cart_id' => implode(',', $cart_arr), 'order_arr' => implode(',', $order_id) ]; return ['order' => $order, 'cart_list' => $cart_select]; } /** * 创建购货订单 * @return Object|bool */ static public function createOpurchaseOrder($user = null, $params = []) { $orderInfo = self::cartIdByPurchaseOrderInfo($user, $params); if (!$orderInfo) { return false; } $_order = $orderInfo['order']; $_order['merchant'] = getenv('OPERATED'); $_order['money'] = $_order['total']; $_order['actual'] = $_order['total']; $_order['paid'] = 1; $_order['is_mer'] = 2; Db::startTrans(); try { $order = Opurchaseclass::create($_order); $goods_list = $orderInfo['cart_list']; foreach ($goods_list as $k => $v) { $goods_list[$k]['nums'] = $v['cart_num']; $goods_list[$k]['pid'] = $order->id; } (new Opurchaseinfo())->saveAll($goods_list); $order_arr = explode(',', $_order['order_arr']); Opurchaseclass::where('id', 'in', $order_arr)->update(['is_opurchase' => 1]); Db::commit(); return $order; } catch (\Exception $e) { Db::rollback(); self::setError($e->getMessage()); return false; } } /** * @notes 获取采购订单详情 * @param $params * @return array * @author likeadmin * @date 2024/04/27 11:26 */ public static function detail($params): array { $id = $params['id']; $data = Opurchaseclass::findOrEmpty($params['id'])->toArray(); if ($data) { $data['goods_info'] = Opurchaseinfo::where('pid', $params['id'])->limit(25)->select()->each(function ($item) use ($id) { $find = Goods::where('id', $item['goods'])->with('unitName')->find(); if ($find) { $item['goods_name'] = $find['name']; $item['unit_name'] = $find['unit_name']; } $where = [ 'goods_id' => $item['goods'], 'order_id' => $id, 'is_adopt' => 1 ]; $item['nums_count'] = OpurchaseGoodsOffer::where($where)->sum('nums'); }); $data['merchant_name'] = Merchant::where('mer_id', $data['merchant'])->value('mer_name'); } return $data; } /** * @notes 采购订单子订单详情 * @param $id * @return array * @author likeadmin * @date 2024/04/27 11:26 */ public static function sub_detail($id, $is_mer, $page_no): array { if ($is_mer == 1) { $order_arr = Opurchaseclass::where('id', $id)->value('order_arr'); $data = []; if ($order_arr) { $order_arr = explode(',', $order_arr); $order_arr_count = count($order_arr); $offset = ($page_no - 1) * $order_arr_count; $paged_items = array_slice($order_arr, $offset, $order_arr_count); if (!$paged_items) { return []; } $list = Cashierclass::whereIn('id', $order_arr)->select()?->toArray(); $data['count'] = $order_arr_count; $data['lists'] = $list; $data['page_no'] = $page_no; $data['page_siz'] = 15; } } else { $order_arr = Opurchaseclass::where('id', $id)->where('is_mer', $is_mer)->value('order_arr'); $data = []; if ($order_arr) { $order_arr = explode(',', $order_arr); $order_arr_count = count($order_arr); $offset = ($page_no - 1) * $order_arr_count; $paged_items = array_slice($order_arr, $offset, $order_arr_count); if (!$paged_items) { return []; } $list = Opurchaseclass::whereIn('id', $order_arr)->select()?->toArray(); $data['count'] = $order_arr_count; $data['lists'] = $list; $data['page_no'] = $page_no; $data['page_siz'] = 15; } } return $data; } /** * @notes 推送供应商商品 * @param $params * @return bool * @date 2024/04/27 11:26 */ public static function createSupplierGoods($goods) { try { $supplier_arr = SupplierBindGoods::where('goods_id',$goods['goods'])->column('supplier_id'); if ($supplier_arr) { $supplier_arr = array_unique($supplier_arr); $time=strtotime(date('Y-m-d',time())); foreach ($supplier_arr as $k => $v) { $goods_offer[] = [ 'supplier_id' => $v, 'goods_id' => $goods['goods'], 'order_id' => $goods['pid'], 'price' => 0, 'need_num' => $goods['nums'], 'create_time' => time() ]; $find=Db::name('opurchase_goods_offer_date')->where('supplier_id',$v)->where('create_time',$time)->find(); if($find){ Db::name('opurchase_goods_offer_date')->where('id',$find['id'])->inc('nums')->update(); }else{ $data=['supplier_id'=>$v,'nums'=>1,'create_time'=>$time]; Db::name('opurchase_goods_offer_date')->insert($data); } } if (count($goods_offer) >= 1) { $res = OpurchaseGoodsOffer::insertAll($goods_offer); if ($res) { Opurchaseinfo::where('id', $goods['id'])->update(['is_push' => 1]); return $goods_offer; } } } } catch (\Exception $e) { self::setError($e->getMessage()); Log::error('添加采购订单报价失败:' . $e->getMessage()); return false; } } /** * @notes 设置采纳商品 * @param $params * @return bool * @date 2024/04/27 11:26 */ public static function goodsOfferUpdate($id, $type = 0, $notes = '', $nums = 0) { try { $find = OpurchaseGoodsOffer::where('id', $id)->find(); if ($type == 0) { if ($find && $find['price'] == 0) { self::setError('报价未设置'); return false; } $find->is_adopt = 2; } else { $find->is_storage = 1; $find->notes = $notes; $find->before_nums = $find->nums; $find->nums = $nums; } $res = $find->save(); if ($res) { return true; } return false; } catch (\Exception $e) { self::setError($e->getMessage()); return false; } } /** * @notes 获取订单号 * @param $type * @return string * @author likeadmin * @date 2021/7/28 17:05 */ static public function getNewOrderId($type) { list($msec, $sec) = explode(' ', microtime()); $msectime = number_format((floatval($msec) + floatval($sec)) * 1000, 0, '', ''); $orderId = $type . $msectime . mt_rand(10000, max(intval($msec * 10000) + 10000, 98369)); return $orderId; } }