12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211 |
- <?php
- /**
- * Niushop商城系统 - 团队十年电商经验汇集巨献!
- * =========================================================
- * Copy right 2019-2029 山西牛酷信息科技有限公司, 保留所有权利。
- * ----------------------------------------------
- * 官方网址: https://www.niushop.com.cn
- * 这不是一个自由软件!您只能在不用于商业目的的前提下对程序代码进行修改和使用。
- * 任何企业和个人不允许对程序代码以任何形式任何目的再发布。
- * =========================================================
- */
- namespace app\model\order;
- use addon\coupon\model\Coupon;
- use app\api\controller\Goodssku;
- use app\model\express\ExpressTemplate;
- use app\model\goods\Goods;
- use app\model\goods\GoodsStock;
- use app\model\member\FranchiseeExamine;
- use app\model\member\Member;
- use app\model\member\Member as MemberModel;
- use app\model\member\MemberAccount;
- use app\model\member\MemberVip;
- use app\model\store\Store;
- use app\model\system\Cron;
- use app\model\web\Platform as PlatformModel;
- use think\facade\Cache;
- use app\model\express\Express;
- use app\model\system\Pay;
- use app\model\goods\Cart;
- use app\model\member\MemberAddress;
- use app\model\express\Config as ExpressConfig;
- use app\model\BaseModel;
- use app\model\message\Message;
- /**
- * 订单创建(普通订单)
- *
- * @author Administrator
- *
- */
-
- class OrderCreate extends BaseModel
- {
- private $goods_money = 0;//商品金额
- private $delivery_money = 0;//配送费用
- private $coupon_money = 0;//优惠券金额
- private $adjust_money = 0;//调整金额
- private $invoice_money = 0;//发票费用
- private $promotion_money = 0;//优惠金额
- private $order_money = 0;//订单金额
- private $pay_money = 0;//支付总价
- private $is_virtual = 0; //是否是虚拟类订单
- private $order_name = ''; //订单详情
- private $goods_num = 0; //商品种数
- private $error = 0; //是否有错误
- private $error_msg = ''; //错误描述
- private $pay_type = 'ONLINE_PAY';
- private $balance_money = 0;
- private $is_exist_not_free = false;
- private $is_exist_free = false;
- private $member_balance_money = 0;//会员账户余额(计算过程中会逐次减少)
- public function ordercreate($data){
- $member_arrdess = $this->member_address($data);
- // $member_level = $this->member_discount($data['member_id']);
- // if(empty($member_level['data'])){
- // $member_level['data']['discount'] = 1;
- // $member_level['data']['name'] = '无折扣';
- // }
- $platform_model = new PlatformModel();
- $platform_division = $platform_model->info('platform_division');
- $pre =1-$platform_division['data']['value']/100;
- $goods_id = model('goods_sku')->getInfo(['sku_id'=>$data['sku'][0]['sku_id']],'goods_id');
- model("order")->startTrans();
- $dev=['goods_id'=>$goods_id['goods_id'],
- 'member_id'=>$data['member_id'],
- 'address_id'=>$data['address_id'],
- 'num'=>$data['number'],
- ];
- $delivery_money = $this->freight($dev);
- //循环生成多个订单
- $pay = new Pay();
- $out_trade_no = $pay->createOutTradeNo();
- try {
- $order_no = $this->createOrderNo($data['site_id']);
- $site_id = $data['site_id'];
- $site_name = $data['site_name'];
- $price = $data['price'];
- $number = $data['number'];
- $unit_price = $data['unit_price'];
- $add_data = [
- 'order_no' => $order_no,
- 'site_id' => $site_id,
- 'site_name' => $site_name,
- 'price' => $price,
- 'number' => $number,
- 'order_type' => 1,
- 'order_type_name' => '普通订单',
- 'order_status_name' => '待支付',
- 'member_id'=>$data['member_id'],
- 'order_status_action' =>'{"status":0,"name":"待支付","is_allow_refund":0,"action":[{"action":"orderClose","title":"关闭订单","color":""},{"action":"orderAddressUpdate","title":"修改地址","color":""},{"action":"orderAdjustMoney","title":"调整价格","color":""}],"member_action":[{"action":"orderClose","title":"关闭订单","color":""},{"action":"orderPay","title":"支付","color":""}],"color":""}',
- 'name' => $member_arrdess['name'],
- 'out_trade_no' => $out_trade_no,
- 'mobile' => $member_arrdess['mobile'],
- 'telephone' => $member_arrdess['telephone'],
- 'province_id' => $member_arrdess['province_id'],
- 'city_id' => $member_arrdess['city_id'],
- 'district_id' => $member_arrdess['district_id'],
- 'community_id' => $member_arrdess['community_id'] ,
- 'address' => $member_arrdess['address'] ,
- 'full_address' => $member_arrdess['full_address'] ,
- 'longitude' => $member_arrdess['longitude'] ,
- 'latitude' => $member_arrdess['latitude'] ,
- 'buyer_ip' => request()->ip(),
- 'create_time' => time(),
- 'delivery_money'=>$delivery_money['data'],
- 'pay_money' => $price,
- 'goods_num'=>$number,
- 'order_money'=>$price,
- 'goods_money'=>$price,
- ];
- $add_data['shop_money'] = $pre*$add_data['pay_money'];
- $add_data['pay_money']=$add_data['pay_money']+$delivery_money['data'];
- $order_id = model("order")->add($add_data);
- model("order")->commit();
- $goods_stock_model = new GoodsStock();
- foreach ($data['sku'] as &$v){
- $order_goods = $this->goodssku($v['sku_id']);
- $data_order_goods =[
- 'order_id' => $order_id,
- 'site_id' =>$order_goods['goodssku']['site_id'],
- 'site_name' =>$order_goods['goodssku']['site_name'],
- 'order_no' => $order_no,
- 'member_id' => $data['member_id'],
- 'sku_id' => $order_goods['goodssku']['sku_id'],
- 'sku_name' => $order_goods['goodssku']['sku_name'],
- 'sku_image' => $order_goods['goodssku']['sku_image'],
- 'sku_no' => $order_goods['goodssku']['sku_no'],
- 'is_virtual' => $order_goods['goodssku']['is_virtual'],
- 'goods_class' => $order_goods['goodssku']['goods_class'],
- 'goods_class_name' => $order_goods['goodssku']['goods_class_name'],
- 'price' => $unit_price,
- 'cost_price' => $price,
- 'num' => $v['num'],
- 'goods_money' => $unit_price*$v['num'],
- 'cost_money' => '',
- 'real_goods_money' =>$unit_price*$v['num'],
- 'commission_rate' => $order_goods['goodssku']['commission_rate'],
- 'goods_id' => $order_goods['goodssku']['goods_id'],
- 'delivery_status' => 0,
- 'delivery_status_name' => "未发货",
- ];
- $data_order_goods['real_goods_money']=$data_order_goods['real_goods_money'];
- model("order_goods")->add($data_order_goods);
- $stock_result = $goods_stock_model->decStock(["sku_id" => $data_order_goods['sku_id'], "num" => $data_order_goods['num']]);
- if($stock_result["code"] != 0){
- model("order")->rollback();
- return $stock_result;
- }
- }
- $pay->addPay(0, $out_trade_no, $this->pay_type, '商品购买', '商品购买', $add_data['pay_money'], '', 'OrderPayNotify', '');
- $this->addOrderCronClose($order_id);//增加关闭订单自动事件
- }
- catch(\Exception $e)
- {
- model("order")->rollback();
- return $this->error('', $e->getMessage().$e->getFile().$e->getLine());
- }
- return $this->success(['out_trade_no'=>$out_trade_no,'order_id'=>$order_id,'pay_money'=>$add_data['pay_money']]);
- }
- /**
- * @param $data
- * @return mixed
- * 用户地址
- */
- public function member_address($data){
- $member_address = new MemberAddress();
- $address = $member_address->getMemberAddressInfo([['member_id','=',$data['member_id']],['id', '=', $data['address_id']]]);
- $member = $address['data'];
- return $member;
- }
- /**
- * 直接下单运费
- */
- public function freight($data){
- $delivery_money = 0;
- $goods_model = new Goods();
- $template = new ExpressTemplate();
- $member_address = $this->member_address($data);
- $goods = $goods_model->getGoodsInfo([['goods_id', '=', $data['goods_id']]],'goods_name,site_id,is_free_shipping,shipping_template,sku_id,weight');
- if($goods['data']['is_free_shipping']==0){
- $temolate_item_info = $template->getExpressTemplateInfo($goods['data']['shipping_template'],$goods['data']['site_id']);
- foreach ($temolate_item_info['data']['template_item'] as $k_item => $v_item)
- {
- if(strpos($v_item['area_ids'] , '"'.$member_address['district_id'].'"') !== false)
- {
- switch($v_item['fee_type'])
- {
- case 1:
- $tag = $data['num']*$goods['data']['weight'];
- break;
- case 3:
- $tag =$data['num'];
- break;
- default:
- break;
- }
- //开始计算
- if ($tag <= $v_item['snum']) {
- $delivery_money += $v_item['sprice'];
- } else {
- $ext_tag = $tag - $v_item['snum'];
- if ($v_item['xnum'] == 0) {
- $v_item['xnum'] = 1;
- }
- if (($ext_tag * 100) % ($v_item['xnum'] * 100) == 0) {
- $ext_data = $ext_tag / $v_item['xnum'];
- } else {
- $ext_data = floor($ext_tag / $v_item['xnum']) + 1;
- }
- $delivery_money += $v_item['sprice'] + $ext_data * $v_item['xprice'];
- }
- break;
- }
- }
- }
- return $this->success($delivery_money);
- }
- /**
- * @param $data
- * @return mixed
- * 商品信息
- */
- public function goodsInfo($data){
- $goods_model = new Goods();
- $goods = $goods_model->getGoodsInfo([['goods_id', '=', $data]],'goods_name,is_free_shipping,shipping_template');
- $data['goods'] = $goods['data'];
- return $data;
- }
- public function goodssku($data){
- $goods_model =new Goods();
- $goods_sku =$goods_model->getGoodsSkuInfo([['sku_id','=',$data]]);
- $goodssku['goodssku'] = $goods_sku['data'];
- return $goodssku;
- }
- /**
- * 订单创建
- * @param unknown $data
- */
- public function create($data)
- {
- //查询出会员相关信息
- $platform_model = new PlatformModel();
- $platform_division = $platform_model->info('platform_division');
- $pre =1-$platform_division['data']['value']/100;
- $calculate_data = $this->calculate($data);
- if($this->error > 0){
- return $this->error("", $this->error_msg);
- }
- $pay = new Pay();
- if($this->is_exist_not_free){
- $out_trade_no = $pay->createOutTradeNo();
- $order_name = [];
- }
- if($this->is_exist_free){
- $free_out_trade_no = $pay->createOutTradeNo();
- $free_order_name = [];
- }
- // $member_level = $this->member_discount($data['member_id']);
- // if(empty($member_level['data'])){
- // $member_level['data']['discount'] = 1;
- // $member_level['data']['name'] = '无折扣';
- // }
- model("order")->startTrans();
- //循环生成多个订单
- try{
- $pay_money = 0;
- $goods_stock_model = new GoodsStock();
- foreach ($calculate_data['shop_goods_list'] as $k => $v)
- {
- $item_delivery = $v['delivery'] ?? [];
- $delivery_type = $item_delivery['delivery_type'] ?? '';
- $delivery_type_name = Express::express_type[$delivery_type]["title"] ?? '';
- //判断交易流水号
- if($v["pay_money"] == 0){
- $temp_out_trade_no = $free_out_trade_no;
- $free_order_name[] = $v["order_name"];
- }else{
- $temp_out_trade_no = $out_trade_no;
- $order_name[] = $v["order_name"];
- }
- //订单主表
- $order_type = $this->orderType($v, $calculate_data);
- $order_no = $this->createOrderNo($v['site_id']);
- $data_order = [
- 'order_no' => $order_no,
- 'site_id' => $v['site_id'],
- 'site_name' => $v['site_name'],
- 'order_from' => $data['order_from'],
- 'order_from_name' => $data['order_from_name'],
- 'order_type' => $order_type['order_type_id'],
- 'order_type_name' => $order_type['order_type_name'],
- 'order_status_name' => $order_type['order_status']['name'],
- 'order_status_action' => json_encode($order_type['order_status'], JSON_UNESCAPED_UNICODE),
- 'out_trade_no' => $temp_out_trade_no,
- 'member_id' => $data['member_id'],
- 'name' => $calculate_data['member_address']['name'] ?? '',
- 'mobile' => $calculate_data['member_address']['mobile'] ?? '',
- 'telephone' => $calculate_data['member_address']['telephone'] ?? '',
- 'province_id' => $calculate_data['member_address']['province_id'] ?? '',
- 'city_id' => $calculate_data['member_address']['city_id'] ?? '',
- 'district_id' => $calculate_data['member_address']['district_id'] ?? '',
- 'community_id' => $calculate_data['member_address']['community_id'] ?? '',
- 'address' => $calculate_data['member_address']['address'] ?? '',
- 'full_address' => $calculate_data['member_address']['full_address'] ?? '',
- 'longitude' => $calculate_data['member_address']['longitude'] ?? '',
- 'latitude' => $calculate_data['member_address']['latitude'] ?? '',
- 'buyer_ip' => request()->ip(),
- 'goods_money' => $v['goods_money'],
- 'delivery_money' => $v['delivery_money'],
- 'coupon_id' => isset($v['coupon_id']) ? $v['coupon_id'] : 0,
- 'coupon_money' => $v['coupon_money'],
- 'adjust_money' => $v['adjust_money'],
- 'invoice_money' => $v['invoice_money'],
- 'promotion_money' => $v['promotion_money'],
- 'order_money' => $v['order_money'],
- 'balance_money' => $v['balance_money'],
- 'pay_money' => $v['pay_money'],
- 'create_time' => time(),
- 'is_enable_refund' => 0,
- 'order_name' => $v["order_name"],
- 'goods_num' => $v['goods_num'],
- 'delivery_type' => $delivery_type,
- 'delivery_type_name' => $delivery_type_name,
- 'delivery_store_id' => $v["delivery_store_id"] ?? 0,
- "delivery_store_name" => $v["delivery_store_name"] ?? '',
- "delivery_store_info" => $v["delivery_store_info"] ?? '',
- "buyer_message" => $data["buyer_message"],
- "website_id" => $v["website_id"],
- ];
- $data_order['shop_money'] = $pre*$data_order['pay_money'];
- // print_r($data_order);die;
- $order_id = model("order")->add($data_order);
- $this->addOrderCronClose($order_id);//增加关闭订单自动事件
- $order_id_arr[] = $order_id;
- $pay_money += $v['pay_money'];
- //订单项目表
- foreach ($v['goods_list'] as $k_order_goods => $order_goods)
- {
- $data_order_goods = array(
- 'order_id' => $order_id,
- 'site_id' => $v['site_id'],
- 'site_name' => $v['site_name'],
- 'order_no' => $order_no,
- 'member_id' => $data['member_id'],
- 'sku_id' => $order_goods['sku_id'],
- 'sku_name' => $order_goods['sku_name'],
- 'sku_image' => $order_goods['sku_image'],
- 'sku_no' => $order_goods['sku_no'],
- 'is_virtual' => $order_goods['is_virtual'],
- 'goods_class' => $order_goods['goods_class'],
- 'goods_class_name' => $order_goods['goods_class_name'],
- 'price' => $order_goods['discount_price'],
- 'cost_price' => $order_goods['cost_price'],
- 'num' => $order_goods['num'],
- 'goods_money' => $order_goods['discount_price']*$order_goods['num'],
- 'cost_money' => $order_goods['cost_price']*$order_goods['num'],
- 'commission_rate' => $order_goods['commission_rate'],
- 'goods_id' => $order_goods['goods_id'],
- 'delivery_status' => 0,
- 'delivery_status_name' => "未发货",
- );
- $data_order_goods['real_goods_money'] = $data_order_goods['goods_money'];
- model("order_goods")->add($data_order_goods);
- //库存变化
- $stock_result = $goods_stock_model->decStock(["sku_id" => $order_goods['sku_id'], "num" => $order_goods['num']]);
- if($stock_result["code"] != 0){
- model("order")->rollback();
- return $stock_result;
- }
- // model("goods_sku")->setDec([['sku_id', '=', $order_goods['sku_id']]], 'stock', $order_goods['num']);
- // $sku_stock = model("goods_sku")->getInfo([['sku_id', '=', $order_goods['sku_id']]], 'stock');
- // if($sku_stock['stock'] < 0)
- // {
- // model("order")->rollback();
- // return $this->error('', "库存不足");
- // }
- }
- //订单满减优惠
- $manjian_rule = isset($v['promotion']['manjian']) ? $v['promotion']['manjian'] : '';
- if(!empty($manjian_rule))
- {
- $order_promotion_detail = [
- 'order_id' => $order_id,
- 'site_id' => $v['site_id'],
- 'promotion_text' => '满'.$manjian_rule['money'].'元减'.$manjian_rule['discount_money'],
- 'money' => $manjian_rule['discount_money'],
- 'create_time' => time()
- ];
- model('order_promotion_detail')->add($order_promotion_detail);
- }
- //优惠券
- if($data_order['coupon_id'] > 0 && $data_order['coupon_money'] > 0)
- {
- //优惠券处理方案
- $member_coupon_model = new Coupon();
- $coupon_use_result = $member_coupon_model->useCoupon($data_order['coupon_id'], $data['member_id'], $order_id);//使用优惠券
- if($coupon_use_result['code'] < 0)
- {
- model("order")->rollback();
- return $this->error('', "COUPON_ERROR");
- }
- }
- }
- //扣除余额(统一扣除)
- if($calculate_data["balance_money"] > 0){
- $balance_result = $this->useBalance($calculate_data);
- if($balance_result["code"] < 0){
- model("order")->rollback();
- return $balance_result;
- }
- }
- //生成整体付费支付单据
- if($this->is_exist_not_free) {
- $order_name_title = implode(",", $order_name);
- $pay->addPay(0, $out_trade_no, $this->pay_type, $order_name_title, $order_name_title, $this->pay_money, '', 'OrderPayNotify', '');
- }
- //免费订单支付单据
- if($this->is_exist_free){
- $free_order_name_title = implode(",", $free_order_name);
- $pay->addPay(0, $free_out_trade_no, $this->pay_type, $free_order_name_title, $free_order_name_title, 0, '', 'OrderPayNotify', '');
- }
- $cart_ids = isset($data['cart_ids']) ? $data['cart_ids'] : '';
- if(!empty($cart_ids))
- {
- $cart = new Cart();
- $data_cart = [
- 'cart_id' => $cart_ids,
- 'member_id' => $data['member_id']
- ];
- $cart->deleteCart($data_cart);
- }
- Cache::tag("order_create_member_".$data['member_id'])->clear();
- //循环执行订单完成事件
- foreach($order_id_arr as $k => $v){
- $result_list = event("OrderCreate", ['order_id' => $v]);
- if(!empty($result_list)){
- foreach($result_list as $k => $v){
- if(!empty($v) && $v["code"] < 0){
- model("order")->rollback();
- return $v;
- }
- }
- }
- }
- // $this->checkFree($data_order);//如果订单金额为0, 直接调用支付成功
- model("order")->commit();
- //订单生成的消息
- $message_model = new Message();
- $message_model->sendMessage(['keywords' => "ORDER_CREATE", 'order_id'=>$order_id]);
- return $this->success(['out_trade_no'=>$out_trade_no ?? $free_out_trade_no,'order_id'=>$order_id_arr,'pay_money'=>$this->pay_money]);
-
- }catch(\Exception $e)
- {
- model("order")->rollback();
- return $this->error('', $e->getMessage().$e->getFile().$e->getLine());
- }
-
- }
- public function member_discount($member_id){
- $member = new MemberModel();
- $vip_level = $member->getMemberInfo([['member_id','=',$member_id]],'member_level,member_type');
- if($vip_level['data']['member_type']==1) {
- $vip_discount = new MemberVip();
- $member_discount = $vip_discount->getMemberVipInfo([['id', '=', $vip_level['data']['member_level']]], 'discount,name');
- }
- if($vip_level['data']['member_type']==2){
- $franchisee_discount = new FranchiseeExamine();
- $member_discount = $franchisee_discount->getFranchiseeExamineInfo([['member_id', '=', $member_id]], 'discount');
- if($member_discount['data']) {
- $member_discount['data']['name'] = '加盟商';
- }
- }
- return $member_discount;
- }
-
- /**
- * 生成订单编号
- *
- * @param array $site_id
- */
- public function createOrderNo($site_id)
- {
- $time_str = date('YmdHi');
- $num = 0;
- $max_no = Cache::get($site_id . "_" . $time_str);
- if (! isset($max_no) || empty($max_no)) {
- $max_no = 1;
- } else {
- $max_no = $max_no + 1;
- }
- $order_no = $time_str . sprintf("%04d", $max_no);
- Cache::set($site_id . "_" . $time_str, $max_no);
- return $order_no;
- }
-
- /**
- * 订单类型判断
- * @param unknown $shop_goods
- */
- public function orderType($shop_goods, $data)
- {
- if($data["is_virtual"] == 1)
- {
- $order = new VirtualOrder();
- return [
- 'order_type_id' => 4,
- 'order_type_name' => '虚拟订单',
- 'order_status' => $order->order_status[0]
- ];
- }else{
- if($shop_goods['delivery']['delivery_type'] == 'express')
- {
- $order = new Order();
- return [
- 'order_type_id' => 1,
- 'order_type_name' => '普通订单',
- 'order_status' => $order->order_status[0]
- ];
- }elseif($shop_goods['delivery']['delivery_type'] == 'store'){
- $order = new StoreOrder();
- return [
- 'order_type_id' => 2,
- 'order_type_name' => '自提订单',
- 'order_status' => $order->order_status[0]
- ];
- }elseif($shop_goods['delivery']['delivery_type'] == 'local'){
- $order = new LocalOrder();
- return [
- 'order_type_id' => 3,
- 'order_type_name' => '外卖订单',
- 'order_status' => $order->order_status[0]
- ];
- }
- }
- }
-
- /**
- * 订单计算
- * @param unknown $data
- */
- public function calculate($data)
- {
- $data = $this->initMemberAddress($data);//初始化地址
- $data = $this->initMemberAccount($data);//初始化会员账户
- //余额付款
- if($data['is_balance'] > 0)
- {
- $this->member_balance_money = $data["member_account"]["balance_total"] ?? 0;
- }
- //传输购物车id组合','隔开要进行拆单
- if(!empty($data['cart_ids']))
- {
- //商品列表信息
- $shop_goods_list = $this->getOrderGoodsCalculate($data);
- foreach ($shop_goods_list as $k => $v)
- {
- $data['shop_goods_list'][$k] = $this->shopOrderCalculate($v, $data);
- }
- }
- else{
- //商品列表信息
- $shop_goods_list = $this->getOrderGoodsCalculate($data);
- //判断是否是虚拟订单
- if($shop_goods_list[0]['goods_list'][0]['is_virtual'])
- {
- $this->is_virtual = 1;
- }else{
- $this->is_virtual = 0;
- }
- $data['shop_goods_list'][$shop_goods_list[0]['goods_list'][0]['site_id']] = $this->shopOrderCalculate($shop_goods_list[0], $data);
- }
- //总结计算
- $data['delivery_money'] = $this->delivery_money;
- $data['coupon_money'] = $this->coupon_money;
- $data['adjust_money'] = $this->adjust_money;
- $data['invoice_money'] = $this->invoice_money;
- $data['promotion_money'] = $this->promotion_money;
- $data['order_money'] = $this->order_money;
- $data['balance_money'] = $this->balance_money;
- $data['pay_money'] = $this->pay_money;
- $data['goods_money'] = $this->goods_money;
- $data['goods_num'] = $this->goods_num;
- $data['is_virtual'] = $this->is_virtual;
- return $data;
- }
-
- /**
- * 待付款订单
- * @param unknown $data
- */
- public function orderPayment($data)
- {
- $calculate_data = $this->calculate($data);
- foreach ($calculate_data['shop_goods_list'] as $k => $v)
- {
- //1、查询会员优惠券
- $member_coupon_model = new Coupon();
- $member_coupon_list_result = $member_coupon_model->getMemberCouponList($data["member_id"], 1, $v["site_id"], $v["goods_money"], "money desc");
- $coupon_list = $member_coupon_list_result["data"];
- $calculate_data['shop_goods_list'][$k]["coupon_list"] = $coupon_list;
- $express_type = [];
- if($this->is_virtual == 0){
- if(!empty($calculate_data['member_address'])){
- //2. 查询店铺配送方式(1. 物流 2. 自提 3. 外卖)
- if($v["express_config"]["is_use"] == 1){
- $express_type[] = ["title" => Express::express_type["express"]["title"], "name" => "express"];
- }
- //查询店铺是否开启门店自提
- if($v["store_config"]["is_use"] == 1){
- $store_model = new Store();
- $member_address = $calculate_data["member_address"] ?? [];
- $province_id = $member_address["province_id"] ?? 0;
- $city_id = $member_address["city_id"] ?? 0;
- $store_condition = array(
- ["site_id", "=", $v["site_id"]],
- ["province_id", "=", $province_id],
- ["city_id", "=", $city_id],
- ["status", "=", 1],
- ["is_pickup", "=", 1],
- );
- $store_list_result = $store_model->getStoreList($store_condition);
- $store_list = $store_list_result["data"];
- $express_type[] = ["title" => Express::express_type["store"]["title"], "name" => "store", "store_list" => $store_list];
- }
- //查询店铺是否开启外卖配送
- if($v["local_config"]["is_use"] == 1){
- //查询本店的通讯地址
- $express_type[] = ["title" => "外卖配送", "name" => "local"];
- }
- }
- }
- $calculate_data['shop_goods_list'][$k]["express_type"] = $express_type;
- }
- return $calculate_data;
-
- }
-
- /**
- * 初始化收货地址
- * @param unknown $data
- */
- public function initMemberAddress($data)
- {
- //收货人地址管理
- if(empty($data['member_address']))
- {
- $member_address = new MemberAddress();
- $address = $member_address->getMemberAddressInfo([['member_id', '=', $data['member_id']], ['is_default', '=', 1]]);
- $data['member_address'] = $address['data'];
- }
- return $data;
- }
-
- /**
- * 获取商品的计算信息
- * @param unknown $data
- */
- public function getOrderGoodsCalculate($data)
- {
- $shop_goods_list = [];
- //传输购物车id组合','隔开要进行拆单
- if(!empty($data['cart_ids']))
- {
- // $cache = Cache::get("order_create_cart_".$data['cart_ids'].'_'.$data['member_id']);
- // if(!empty($cache))
- // {
- // return $cache;
- // }
- $goods_list = $this->getCartGoodsList($data['cart_ids'], $data['member_id']);
- foreach ($goods_list as $k => $shop_goods)
- {
- $shop_goods['promotion_money'] = 0;
- //查询店铺是否存在满减活动
- $manjian_info = model("promotion_manjian")->getInfo(['site_id' => $shop_goods['site_id'], "status" => 1], 'rule_json');
- $manjian_rule = [];
- if(!empty($manjian_info))
- {
- $manjian_rules = json_decode($manjian_info['rule_json'], true);
- $manjian_rules = array_reverse($manjian_rules);
- foreach ($manjian_rules as $k => $v)
- {
- if($shop_goods['goods_money'] >= $v['money'])
- {
- $shop_goods['promotion_money'] += $v['discount_money'];//优惠金额
- $promotion_item = $v;
- $promotion_item["content"] = "满".$v['money']."减".$v['discount_money'];
- $shop_goods['promotion']['manjian'] = $promotion_item;//前台显示满减规则
- break;
- }
- }
- }
- $shop_goods_list[$shop_goods['site_id']] = $shop_goods;
- }
- // Cache::tag("order_create_member_".$data['member_id'])->set("order_create_cart_".$data['cart_ids'].'_'.$data['member_id'], $shop_goods_list, 600);
-
- }else{
- // $cache = Cache::get("order_create_".$data['sku_id'].'_'.$data['num'].'_'.$data['member_id']);
- // if(!empty($cache))
- // {
- // return $cache;
- // }
- $shop_goods = $this->getShopGoodsList($data);
- //查询店铺是否存在满减活动
- $manjian_info = model("promotion_manjian")->getInfo(['site_id' => $shop_goods['site_id'], "status" => 1], 'rule_json');
- $manjian_rule = [];
- $shop_goods['promotion_money'] = 0;
- if(!empty($manjian_info))
- {
- $manjian_rules = json_decode($manjian_info['rule_json'], true);
- $manjian_rules = array_reverse($manjian_rules);
- foreach ($manjian_rules as $k => $v)
- {
- if($shop_goods['goods_money'] >= $v['money'])
- {
- $shop_goods['promotion_money'] = $v['discount_money'];//优惠金额
- $promotion_item = $v;
- $promotion_item["content"] = "满".$v['money']."减".$v['discount_money'];
- $shop_goods['promotion']['manjian'] = $promotion_item;//前台显示满减规则
- break;
- }
- }
- }
- $shop_goods_list[0] = $shop_goods;
- // Cache::tag("order_create_member_".$data['member_id'])->set("order_create_".$data['sku_id'].'_'.$data['num'].'_'.$data['member_id'], $shop_goods_list, 600);
- }
- return $shop_goods_list;
- }
-
- /**
- * 获取购物车商品列表信息
- * @param unknown $cart_ids
- */
- public function getCartGoodsList($cart_ids, $member_id)
- {
- //组装商品列表
- $field = ' ngc.member_id, ngc.sku_id, ngc.num, ngs.sku_name, ngs.sku_no,
- ngs.price, ngs.discount_price, ngs.cost_price, ngs.stock, ngs.weight, ngs.volume, ngs.sku_image,
- ngs.site_id, ngs.site_name, ngs.website_id, ngs.is_own, ngs.goods_state, ngs.is_virtual,
- ngs.is_free_shipping, ngs.shipping_template, ngs.goods_class, ngs.goods_class_name, ngs.commission_rate,ngs.goods_id';
- $alias = 'ngc';
- $join = [
- [
- 'goods_sku ngs',
- 'ngc.sku_id = ngs.sku_id',
- 'inner'
- ],
- ];
- $goods_list = model("goods_cart")->getList([['cart_id', 'in', $cart_ids], ['member_id', '=', $member_id]], $field, '', $alias, $join);
- $shop_goods_list = [];
- if(!empty($goods_list))
- {
- foreach ($goods_list as $k => $v)
- {
- $goods_model = new \app\model\goods\Goods();
- $gods_price =$goods_model->getGoodsInfo([['goods_id','=',$v['goods_id']]],'pay_num1,pay_num2,pay_num3,price1,price2,price3,vip_price');
- $user_model = new Member();
- $user_info = $user_model->getMemberInfo([['member_id','=',$member_id]],'member_type,member_level');
- if($user_info['data']['member_type']==2||$user_info['data']['member_level']==1) {
- $money=$gods_price['data']['vip_price'];
- }
- else {
- if ($v['num'] >= $gods_price['data']['pay_num1']) {
- $money = $gods_price['data']['price1'];
- }
- if ($v['num'] >= $gods_price['data']['pay_num2']) {
- $money = $gods_price['data']['price2'];
- }
- if ($v['num'] >= $gods_price['data']['pay_num3']) {
- $money = $gods_price['data']['price3'];
- }
- }
- $v['discount_price'] =$money;
- $site_id = $v['site_id'];
- if(isset($shop_goods_list[$site_id]))
- {
- $shop_goods_list[$site_id]['goods_list'][] = $v;
- $shop_goods_list[$site_id]['order_name'] = string_split($shop_goods_list[$site_id]['order_name'], ",", $v['sku_name']);
- $shop_goods_list[$site_id]['goods_num'] += $v['num'];
- $shop_goods_list[$site_id]['goods_money'] += $v['discount_price'] * $v['num'];
- $shop_goods_list[$site_id]['goods_list_str'] = $shop_goods_list[$site_id]['goods_list_str'].';'.$v['sku_id'].':'.$v['num'];
- }else{
- $shop_goods_list[$site_id]['site_id'] = $site_id;
- $shop_goods_list[$site_id]['site_name'] = $v['site_name'];
- $shop_goods_list[$site_id]['website_id'] = $v['website_id'];
- $shop_goods_list[$site_id]['goods_money'] = $v['discount_price'] * $v['num'];
- $shop_goods_list[$site_id]['goods_list_str'] = $v['sku_id'].':'.$v['num'];
- $shop_goods_list[$site_id]['order_name'] = string_split("", ",", $v['sku_name']);
- $shop_goods_list[$site_id]['goods_num'] = $v['num'];
- $shop_goods_list[$site_id]['goods_list'][] = $v;
- }
- }
- }
- return $shop_goods_list;
- }
-
- /**
- * 获取立即购买商品信息
- * @param unknown $data
- * @return multitype:string number unknown mixed
- */
- public function getShopGoodsList($data)
- {
- $sku_info = model("goods_sku")->getInfo([['sku_id', '=', $data['sku_id']]], 'sku_id, sku_name, sku_no, price, discount_price,
- cost_price, stock, volume, weight, sku_image, site_id, site_name,
- website_id, is_own, goods_state, is_virtual, is_free_shipping, shipping_template,goods_class, goods_class_name, commission_rate, goods_id');
- $sku_info['num'] = $data['num'];
- $goods_money = $sku_info['discount_price'] * $data['num'];
- $goods_list[] = $sku_info;
- $shop_goods = [
- 'goods_money' => $goods_money,
- 'site_id' => $sku_info['site_id'],
- 'site_name' => $sku_info['site_name'],
- 'website_id' => $sku_info['website_id'],
- 'goods_list_str' => $sku_info['sku_id'].':'.$sku_info['num'],
- 'goods_list' => $goods_list,
- 'order_name' => $sku_info["sku_name"],
- 'goods_num' => $sku_info['num'],
- ];
- return $shop_goods;
- }
-
- /**
- * 获取店铺订单计算
- * @param unknown $site_id 店铺id
- * @param unknown $site_name 店铺名称
- * @param unknown $goods_money 商品总价
- * @param unknown $goods_list 店铺商品列表
- * @param unknown $data 传输生成订单数据
- */
- public function shopOrderCalculate($shop_goods, $data)
- {
- $site_id = $shop_goods['site_id'];
- //定义计算金额
- $goods_money = $shop_goods['goods_money']; //商品金额
- $delivery_money = 0; //配送费用
- $promotion_money = $shop_goods['promotion_money']; //优惠费用(满减)
- $coupon_money = 0; //优惠券费用
- $adjust_money = 0; //调整金额
- $invoice_money = 0; //发票金额
- $order_money = 0; //订单金额
- $balance_money = 0; //会员余额
- $pay_money = 0; //应付金额
-
- //计算邮费
- if($this->is_virtual == 1)
- {
- //虚拟订单 运费为0
- $delivery_money = 0;
- $shop_goods['delivery']['delivery_type'] = '';
- }else{
- if(empty($data['member_address']))
- {
- $delivery_money = 0;
- $shop_goods['delivery']['delivery_type'] = 'express';
- $this->error = 1;
- $this->error_msg = "未配置默认收货地址!";
- }else{
- //查询店铺是否开启快递配送
- $express_config_model = new ExpressConfig();
- $express_config_result = $express_config_model->getExpressConfig($site_id);
- $express_config = $express_config_result["data"];
- $shop_goods["express_config"] = $express_config;
- //查询店铺是否开启门店自提
- $store_config_result = $express_config_model->getStoreConfig($site_id);
- $store_config = $store_config_result["data"];
- $shop_goods["store_config"] = $store_config;
- //查询店铺是否开启外卖配送
- $local_config_result = $express_config_model->getLocalDeliveryConfig($site_id);
- $local_config = $local_config_result["data"];
- $shop_goods["local_config"] = $local_config;
- if(!isset($data['delivery'][$site_id]["delivery_type"]) || $data['delivery'][$site_id]["delivery_type"] == "express"){
- if($shop_goods["express_config"]["is_use"] == 1){
- //物流配送
- $express = new Express();
- $express_fee_result = $express->calculate($shop_goods, $data);
- if($express_fee_result["code"] < 0){
- $this->error = 1;
- $this->error_msg = $express_fee_result["message"];
- $delivery_fee = 0;
- }else{
- $delivery_fee = $express_fee_result['data']['delivery_fee'];
- }
- }else{
- $this->error = 1;
- $this->error_msg = "物流配送方式未开启!";
- $delivery_fee = 0;
- }
- $delivery_money = $delivery_fee;
- $shop_goods['delivery']['delivery_type'] = 'express';
- }else if($data['delivery'][$site_id]["delivery_type"] == "store"){
- //门店自提
- $delivery_money = 0;
- $shop_goods['delivery']['delivery_type'] = 'store';
- if($shop_goods["store_config"]["is_use"] == 0){
- $this->error = 1;
- $this->error_msg = "门店自提方式未开启!";
- }
- if(empty($data['delivery'][$site_id]["store_id"])){
- $this->error = 1;
- $this->error_msg = "门店未选择!";
- }
- $shop_goods['delivery']['store_id'] = $data['delivery'][$site_id]["store_id"];
- $shop_goods = array_merge($shop_goods,$this->storeOrderData($shop_goods, $data));
- }else if($data['delivery'][$site_id]["delivery_type"] == "local"){
- //外卖配送
- $delivery_money = 0;
- $shop_goods['delivery']['delivery_type'] = 'local';
- if($shop_goods["local_config"]["is_use"] == 0){
- $this->error = 1;
- $this->error_msg = "外卖配送方式未开启!";
- }
- }
- }
- }
- $order_money = $goods_money + $delivery_money - $promotion_money + $invoice_money;
- //优惠券活动(采用站点id:coupon_id)
- if(!empty($data['coupon'][$site_id]) && $data['coupon'][$site_id]["coupon_id"] > 0)
- {
- $coupon_info = $data['coupon'][$site_id];
- $shop_goods['coupon_id'] = $coupon_info["coupon_id"];
- //查询优惠券信息,计算优惠券费用
- $coupon = new Coupon();
- $coupon_info_result = $coupon->getCouponInfo([['coupon_id', '=', $shop_goods['coupon_id']]], 'member_id,at_least,money,state');
- $coupon_info = $coupon_info_result["data"];
- if(($coupon_info['at_least'] <= $goods_money-$promotion_money) && ($coupon_info['state'] == 1) && $coupon_info['member_id'] == $data['member_id'])
- {
- $coupon_money = $coupon_info['money'];
- }else{
- $this->error = 1;
- $this->error_msg = "优惠券不可用!";
- }
- }
- if($coupon_money > $order_money){
- $coupon_money = $order_money;
- }
- $order_money -= $coupon_money;
- //买家留言
- if(isset($data['buyer_message']) && isset($data['buyer_message'][$site_id])){
- $item_buyer_message = $data['buyer_message'][$site_id];
- $shop_goods["buyer_message"] = $item_buyer_message;
- }else{
- $shop_goods["buyer_message"] = '';
- }
- if($order_money < 0)
- {
- $order_money = 0;
- }
- //余额抵扣(判断是否使用余额)
- if($this->member_balance_money > 0)
- {
- if($order_money <= $this->member_balance_money)
- {
- $balance_money = $order_money;
- }else{
- $balance_money = $this->member_balance_money;
- }
- }else{
- $balance_money = 0;
- }
- $pay_money = $order_money - $balance_money;//计算出实际支付金额
- //判断是否存在支付金额为0的订单
- if($pay_money > 0){
- $this->is_exist_not_free = true;
- }else{
- $this->is_exist_free = true;
- }
- $this->member_balance_money -= $balance_money;//预减少账户余额
- $this->balance_money += $balance_money;//累计余额
- //总结计算
- $shop_goods['goods_money'] = $goods_money;
- $shop_goods['delivery_money'] = $delivery_money;
- $shop_goods['coupon_money'] = $coupon_money;
- $shop_goods['adjust_money'] = $adjust_money;
- $shop_goods['invoice_money'] = $invoice_money;
- $shop_goods['promotion_money'] = $promotion_money;
- $shop_goods['order_money'] = $order_money;
- $shop_goods['balance_money'] = $balance_money;
- $shop_goods['pay_money'] = $pay_money;
- $this->goods_money += $goods_money;
- $this->delivery_money += $delivery_money;
- $this->coupon_money += $coupon_money;
- $this->adjust_money += $adjust_money;
- $this->invoice_money += $invoice_money;
- $this->promotion_money += $promotion_money;
- $this->order_money += $order_money;
- $this->pay_money += $pay_money;
- $this->goods_num += $shop_goods["goods_num"];
- $this->order_name = string_split($this->order_name, ",", $shop_goods["order_name"]);
- return $shop_goods;
- }
- /**
- * 增加订单自动关闭事件
- * @param $order_id
- */
- public function addOrderCronClose($order_id){
- //计算订单自动关闭时间
- $config_model = new Config();
- $order_config_result = $config_model->getOrderEventTimeConfig();
- $order_config = $order_config_result["data"];
- $now_time = time();
- if(!empty($order_config)){
- $execute_time = $now_time + $order_config["value"]["auto_close"]*60;//自动关闭时间
- }else{
- $execute_time = $now_time + 3600;//尚未配置 默认一天
- }
- $cron_model = new Cron();
- $cron_model->addCron(1, 0, "订单自动关闭", "CronOrderClose", $execute_time, $order_id);
- }
- /**
- * 验证订单支付金额知否为0 如果为0 立即支付完成
- * @param $order_data
- */
- public function checkFree($order_data){
- if($order_data["pay_money"] == 0){
- // $pay_model = new Pay();
- // $pay_model->onlinePay($order_data["out_trade_no"], "ONLINE_PAY", '', '');
- }
- }
- /**
- * 补齐门店数据
- * @param $data
- */
- public function storeOrderData($shop_goods, $data){
- $temp_data = [];
- $delivery_store_id = $shop_goods['delivery']['store_id'] ?? 0;//门店id
- if($delivery_store_id > 0){
- $member_address = $data["member_address"] ?? [];
- $province_id = $member_address["province_id"] ?? 0;
- $city_id = $member_address["city_id"] ?? 0;
- $store_model = new Store();
- $condition = array(
- ["store_id", "=", $delivery_store_id],
- ["site_id", "=", $shop_goods['site_id']],
- ["province_id", "=", $province_id],
- ["city_id", "=", $city_id],
- ["status", "=", 1],
- ["is_pickup", "=", 1],
- );
- $store_info_result = $store_model->getStoreInfo($condition);
- $store_info = $store_info_result["data"] ?? [];
- if(empty($store_info)) {
- $this->error = 1;
- $this->error_msg = "当前门店不存在或未开启!";
- }else {
- $temp_data["delivery_store_id"] = $delivery_store_id;
- $delivery_store_name = $store_info_result["data"]["store_name"];
- $temp_data["delivery_store_name"] = $delivery_store_name;
- $delivery_store_info = array(
- "open_date" => $store_info["open_date"],
- "full_address" => $store_info["full_address"],
- "longitude" => $store_info["longitude"],
- "latitude" => $store_info["latitude"],
- "telphone" => $store_info["telphone"],
- );
- $temp_data["delivery_store_info"] = json_encode($delivery_store_info, JSON_UNESCAPED_UNICODE);
- }
- }else{
- $this->error = 1;
- $this->error_msg = "配送门店不可为空!";
- }
- return $temp_data;
- }
- /**
- * 使用余额
- * @param $order_data
- * @return array
- */
- public function useBalance($data){
- $this->pay_type = "BALANCE";
- $member_model = new Member();
- $result = $member_model->checkPayPassword($data["member_id"], $data["pay_password"]);
- if($result["code"] >= 0){
- $balance_money = $data["member_account"]["balance_money"];//不可提现余额
- $balance = $data["member_account"]["balance"];//可提现余额
- $member_account_model = new MemberAccount();
- $surplus_banance = $data["balance_money"];
- //优先扣除不可提现余额
- if($balance > 0){
- if($balance >= $surplus_banance){
- $real_balance = $surplus_banance;
- }else{
- $real_balance = $balance;
- }
- $result = $member_account_model->addMemberAccount($data["member_id"], "balance", -$real_balance, "order", "余额抵扣","订单余额抵扣,扣除不可提现余额:".$real_balance);
- $surplus_banance -= $real_balance;
- }
- // if($balance_money > 0){
- // if($balance_money > $surplus_banance){
- // $real_balance_money = $surplus_banance;
- // }else{
- // $real_balance_money = $balance_money;
- // }
- // $result = $member_account_model->addMemberAccount($data["member_id"], "balance", -$real_balance, "order", "余额抵扣","订单余额抵扣,扣除不可提现余额:".$real_balance);
- // }
- if($surplus_banance > 0){
- $result = $member_account_model->addMemberAccount($data["member_id"], "balance_money", -$surplus_banance, "order", "余额抵扣","订单余额抵扣,扣除可提现余额:".$surplus_banance);
- }
- return $result;
- }else{
- return $result;
- }
- }
- /**
- * 初始化会员账户
- * @param $data
- * @return mixed
- */
- public function initMemberAccount($data){
- $member_model = new Member();
- $member_info_result = $member_model->getMemberDetail($data["member_id"]);
- $member_info = $member_info_result["data"];
- if(!empty($member_info)){
- if(!empty($member_info["pay_password"])){
- $is_pay_password = 1;
- }else{
- $is_pay_password = 0;
- }
- unset($member_info["pay_password"]);
- $member_info["is_pay_password"] = $is_pay_password;
- $data['member_account'] = $member_info;
- }
- return $data;
- }
- }
|