Order.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\plugins\scanCodePay\models;
  8. use app\models\AccountLog;
  9. use app\models\Option;
  10. use Yii;
  11. use Codeception\PHPUnit\ResultPrinter\HTML;
  12. /**
  13. * This is the model class for table "{{%scan_code_pay_order}}".
  14. *
  15. * @property integer $id
  16. * @property integer $store_id
  17. * @property integer $user_id
  18. * @property string $order_no
  19. * @property string $total_price
  20. * @property string $pay_price
  21. * @property string $express_price
  22. * @property string $send_price
  23. * @property string $take_price
  24. * @property string $first_share_profit
  25. * @property string $second_share_profit
  26. * @property string $third_share_profit
  27. * @property string $name
  28. * @property string $mobile
  29. * @property string $address
  30. * @property string $remark
  31. * @property integer $is_pay
  32. * @property integer $pay_type
  33. * @property integer $pay_time
  34. * @property integer $send_time
  35. * @property string $express
  36. * @property string $express_no
  37. * @property integer $confirm_time
  38. * @property integer $is_comment
  39. * @property integer $apply_delete
  40. * @property integer $created_at
  41. * @property integer $is_delete
  42. * @property integer $is_price
  43. * @property integer $parent_id
  44. * @property string $first_price
  45. * @property string $second_price
  46. * @property string $third_price
  47. * @property string $coupon_sub_price
  48. * @property string $content
  49. * @property integer $is_offline
  50. * @property integer $clerk_id
  51. * @property string $address_data
  52. * @property string $offline_qrcode
  53. * @property string $before_update_price
  54. * @property integer $shop_id
  55. * @property string $discount
  56. * @property integer $user_coupon_id
  57. * @property string $integral
  58. * @property integer $give_integral
  59. * @property integer $parent_id_1
  60. * @property integer $parent_id_2
  61. * @property integer $is_sale
  62. * @property string $words
  63. * @property string $version
  64. * @property string $express_price_1
  65. * @property integer $mch_id
  66. * @property integer $is_recycle
  67. * @property string $seller_comments
  68. * @property integer $order_union_id
  69. * @property string $rebate
  70. * @property string $before_update_express
  71. * @property integer $is_transfer
  72. * @property integer $type
  73. * @property integer $share_user_first_id
  74. * @property integer $share_user_second_id
  75. * @property integer $share_user_third_id
  76. * @property integer $is_share
  77. * @property string $share_price
  78. * @property string $first_share_price
  79. * @property string $second_share_price
  80. * @property string $third_share_price
  81. * @property integer $is_show
  82. * @property integer $order_origin
  83. * @property integer $is_open_offline
  84. * @property integer $trade_status
  85. * @property string $transaction_id
  86. * @property integer $alipay_trade_no
  87. * @property integer $is_platform
  88. * @property integer $combine_money
  89. * @property integer $is_combine_pay
  90. * @property integer $share_send_type
  91. * @property integer $share_order_profit
  92. * @property integer $is_use_platform_mch
  93. * @property string $adapay_payment_id
  94. * @property integer $is_yunst_sharing
  95. * @property integer $md_id
  96. * @property integer $mch_is_price
  97. * @property integer $mch_is_price_time
  98. */
  99. class Order extends \yii\db\ActiveRecord
  100. {
  101. /**
  102. * 订单流转状态:默认
  103. */
  104. const ORDER_FLOW_DEFAULT = -1;
  105. /**
  106. * 订单流转状态:待发货
  107. */
  108. const ORDER_FLOW_NO_SEND = 0;
  109. /**
  110. * 订单流转状态:已取消
  111. */
  112. const ORDER_FLOW_CANCEL = 1;
  113. /**
  114. * 订单流转状态:已发货
  115. */
  116. const ORDER_FLOW_SEND = 2;
  117. /**
  118. * 订单流转状态:已完成
  119. */
  120. const ORDER_FLOW_CONFIRM = 3;
  121. public $valid_order_flow = [
  122. self::ORDER_FLOW_DEFAULT,
  123. self::ORDER_FLOW_NO_SEND,
  124. self::ORDER_FLOW_SEND,
  125. self::ORDER_FLOW_CANCEL,
  126. self::ORDER_FLOW_CONFIRM
  127. ];
  128. /**
  129. * 申请取消订单: 默认
  130. */
  131. const ORDER_APPLY_DELETE_DEFAULT = 0;
  132. /**
  133. * 申请取消订单: 申请中
  134. */
  135. const ORDER_APPLY_DELETE = 1;
  136. /**
  137. * 订单来源 公众号或网站
  138. */
  139. const ORDER_SOURCE_WEB = 1;
  140. /**
  141. * 订单来源 app
  142. */
  143. const ORDER_SOURCE_APP = 2;
  144. /**
  145. * 订单来源 小程序
  146. */
  147. const ORDER_SOURCE_MINI = 3;
  148. /**
  149. * 用户取消类型
  150. */
  151. const CANCEL_USER_TYPE = 0;
  152. /**
  153. * 后台取消类型
  154. */
  155. const CANCEL_BACK_TYPE = 1;
  156. /**
  157. * 是否取消(手动):已取消
  158. */
  159. const IS_DELETE_TRUE = 1;
  160. /**
  161. * 是否取消(手动):未取消
  162. */
  163. const IS_DELETE_FALSE = 0;
  164. /**
  165. * 是否支付:已支付
  166. */
  167. const IS_PAY_TRUE = 1;
  168. /**
  169. * 是否支付:未支付
  170. */
  171. const IS_PAY_FALSE = 0;
  172. /**
  173. * 支付方式:未支付
  174. */
  175. const PAY_TYPE_UNPAID = 0;
  176. /**
  177. * 支付方式:微信支付
  178. */
  179. const PAY_TYPE_WECHAT = 1;
  180. /**
  181. * 支付方式:货到付款
  182. */
  183. const PAY_TYPE_COD = 2;
  184. /**
  185. * 支付方式:余额支付
  186. */
  187. const PAY_TYPE_BALANCE_PAID = 3;
  188. /**
  189. * 订单类型:商城订单
  190. */
  191. const ORDER_TYPE_STORE = 0;
  192. /**
  193. * 订单类型:卡券兑换订单
  194. */
  195. const ORDER_TYPE_POND = 1;
  196. /**
  197. * 订单是否显示:显示
  198. */
  199. const IS_SHOW_TRUE = 1;
  200. /**
  201. * 订单是否显示:不显示
  202. */
  203. const IS_SHOW_FALSE = 0;
  204. /**
  205. * 是否过售后时间:是
  206. */
  207. const IS_SALE_TRUE = 1;
  208. /**
  209. * 是否过售后时间:否
  210. */
  211. const IS_SALE_FALSE = 0;
  212. /**
  213. * 自提订单
  214. */
  215. const IS_OFFLINE_TRUE = 1;
  216. const IS_OFFLINE_FALSE = 0;
  217. /**
  218. * 类型
  219. */
  220. // const SEND_TYPE_DADA = 1;
  221. // const SEND_TYPE_UU = 2;
  222. // const SEND_TYPE_PEISONG = 3;
  223. /**
  224. * 同城配送
  225. */
  226. const IS_SAME_CITY_NO = 0; //否
  227. const IS_SAME_CITY_YES = 1; //是
  228. /**
  229. * @inheritdoc
  230. */
  231. public static function tableName()
  232. {
  233. return '{{%scan_code_pay_order}}';
  234. }
  235. /**
  236. * @inheritdoc
  237. */
  238. public function rules()
  239. {
  240. return [
  241. [['store_id', 'user_id', 'order_no', 'first_price', 'second_price', 'third_price'], 'required'],
  242. [['store_id', 'user_id', 'is_pay', 'pay_type', 'pay_time', 'send_time',
  243. 'confirm_time', 'is_comment', 'apply_delete', 'created_at', 'is_delete', 'is_price', 'parent_id',
  244. 'is_offline', 'clerk_id', 'shop_id', 'user_coupon_id', 'give_integral', 'parent_id_1',
  245. 'parent_id_2', 'is_sale', 'mch_id', 'order_union_id', 'is_transfer', 'type', 'is_show',
  246. 'is_open_offline', 'order_origin', 'trade_status', 'is_platform', 'is_combine_pay', 'share_send_type', 'is_use_platform_mch', 'md_id'], 'integer'],
  247. [['total_price', 'pay_price', 'express_price', 'first_price', 'second_price', 'third_price',
  248. 'coupon_sub_price', 'before_update_price', 'discount', 'express_price_1', 'rebate',
  249. 'before_update_express', 'share_price', 'combine_money', 'share_order_profit', 'mch_is_price', 'mch_is_price_time'], 'number'],
  250. [['address_data', 'content', 'offline_qrcode', 'integral', 'words', 'seller_comments', 'transaction_id'], 'string'],
  251. [['order_no', 'name', 'mobile', 'express', 'express_no', 'version', 'alipay_trade_no'], 'string', 'max' => 255],
  252. [['address', 'remark'], 'string', 'max' => 1000],
  253. [['adapay_payment_id'], 'safe'],
  254. ];
  255. }
  256. /**
  257. * @inheritdoc
  258. */
  259. public function attributeLabels()
  260. {
  261. return [
  262. 'id' => 'ID',
  263. 'share_user_first_id' => 'share_user_first_id',
  264. 'share_user_second_id' => 'share_user_second_id',
  265. 'share_user_third_id' => 'share_user_third_id',
  266. 'is_share' => 'ID',
  267. 'store_id' => 'Store ID',
  268. 'user_id' => '用户id',
  269. 'order_no' => '订单号',
  270. 'total_price' => '订单总费用(包含运费)',
  271. 'pay_price' => '实际支付总费用(含运费)',
  272. 'express_price' => '运费',
  273. 'send_price' => '赠送用户金额',
  274. 'take_price' => '扣除用户联盟券抵扣金额',
  275. 'first_share_price' => '一级分销',
  276. 'second_share_price' => '二级分销',
  277. 'third_share_price' => '三级分销',
  278. 'name' => '收货人姓名',
  279. 'mobile' => '收货人手机',
  280. 'address' => '收货地址',
  281. 'remark' => '订单备注',
  282. 'is_pay' => '支付状态:0=未支付,1=已支付',
  283. 'pay_type' => '支付方式:1=微信支付',
  284. 'pay_time' => '支付时间',
  285. 'send_time' => '发货时间',
  286. 'express' => '物流公司',
  287. 'express_no' => 'Express No',
  288. 'confirm_time' => '确认收货时间',
  289. 'is_comment' => '是否已评价:0=未评价,1=已评价',
  290. 'apply_delete' => '是否申请取消订单:0=否,1=申请取消订单',
  291. 'created_at' => 'Addtime',
  292. 'is_delete' => 'Is Delete',
  293. 'is_price' => '是否发放佣金',
  294. 'parent_id' => '用户上级ID',
  295. 'first_price' => '一级佣金',
  296. 'second_price' => '二级佣金',
  297. 'third_price' => '三级佣金',
  298. 'coupon_sub_price' => '优惠券抵消金额',
  299. 'content' => 'Content',
  300. 'is_offline' => '是否到店自提 0--否 1--是',
  301. 'clerk_id' => '核销员user_id',
  302. 'address_data' => '收货地址信息,json格式',
  303. 'offline_qrcode' => '核销码',
  304. 'before_update_price' => '修改前的价格',
  305. 'shop_id' => '自提自提点ID',
  306. 'discount' => '会员折扣',
  307. 'user_coupon_id' => '使用的优惠券ID',
  308. 'integral' => '积分使用',
  309. 'give_integral' => '是否发放积分【1=> 已发放 , 0=> 未发放】',
  310. 'parent_id_1' => '用户上二级ID',
  311. 'parent_id_2' => '用户上三级ID',
  312. 'is_sale' => '是否超过售后时间',
  313. 'words' => '商家留言',
  314. 'version' => '版本',
  315. 'express_price_1' => '减免的运费',
  316. 'mch_id' => '入驻商户id',
  317. 'seller_comments' => '商家备注',
  318. 'order_union_id' => '合并订单的id',
  319. 'rebate' => '自购返利',
  320. 'before_update_express' => '价格修改前的运费',
  321. 'is_transfer' => '是否已转入商户账户:0=否,1=是',
  322. 'type' => '0普通订单1卡券兑换订单',
  323. 'share_price' => '发放佣金的金额',
  324. 'is_show' => '是否显示 0--不显示 1--显示(软删除用)',
  325. 'order_origin' => '订单来源 1:公众号或者网站 2:app 3:小程序',
  326. 'is_open_offline' => '是否是线下购物车下的订单',
  327. 'trade_status' => '订单状态,-1:默认,0:待发货,1:已取消,2:已发货,3:已确认',
  328. 'is_use_platform_mch' => '供应链系统下单时是否使用平台商户号,1使用,0未使用',
  329. 'transaction_id' => '微信商户号'
  330. ];
  331. }
  332. public function afterSave($insert, $changedAttributes)
  333. {
  334. parent::afterSave($insert, $changedAttributes); // TODO: Change the autogenerated stub
  335. if (intval($this->attributes['is_pay'])) {
  336. self::send_user_integral($this);
  337. }
  338. }
  339. public function getOrderDetail()
  340. {
  341. return $this->hasMany(OrderDetail::className(), ['order_id' => 'id'])->alias('od')
  342. ->leftJoin(['g' => Goods::tableName()], 'g.id=od.goods_id')->select(['od.*', 'g.name', 'g.attr goods_attr', 'g.cost_price']);
  343. }
  344. public function getDetail()
  345. {
  346. return $this->hasMany(OrderDetail::className(), ['order_id' => 'id']);
  347. }
  348. public function getGoods()
  349. {
  350. return $this->hasMany(Goods::className(), ['id' => 'goods_id'])->alias('g')
  351. ->viaTable(OrderDetail::tableName() . ' od', ['order_id' => 'id']);
  352. }
  353. public function getShop()
  354. {
  355. return $this->hasOne(Shop::className(), ['id' => 'shop_id']);
  356. }
  357. public function getClerk()
  358. {
  359. return $this->hasOne(User::className(), ['id' => 'clerk_id']);
  360. }
  361. public function getOrderForm()
  362. {
  363. return $this->hasMany(OrderForm::className(), ['order_id' => 'id'])->where(['is_delete' => 0]);
  364. }
  365. public function getUser()
  366. {
  367. return $this->hasOne(User::className(), ['id' => 'user_id']);
  368. }
  369. public function getRefund()
  370. {
  371. return $this->hasMany(OrderRefund::className(), ['order_id' => 'id']);
  372. }
  373. public function beforeSave($insert)
  374. {
  375. $this->content = \yii\helpers\Html::encode($this->content);
  376. return parent::beforeSave($insert);
  377. }
  378. public function getPondDetail()
  379. {
  380. return $this->hasOne(OrderDetail::className(), ['order_id' => 'id']);
  381. }
  382. public function getPondGoods()
  383. {
  384. return $this->hasOne(Goods::className(), ['id' => 'goods_id'])->alias('g')
  385. ->viaTable(OrderDetail::tableName() . ' od', ['order_id' => 'id']);
  386. }
  387. //发放积分
  388. public static function send_user_integral($order)
  389. {
  390. debug_log(['发放积分'], 'send_user_integral.log');
  391. try {
  392. $face_send_integral_profit = Option::get('face_send_integral_profit', $order->store_id, 'store', '0')['value'];
  393. debug_log(['face_send_integral_profit' => $face_send_integral_profit, 'store_id' => $order->store_id], 'send_user_integral.log');
  394. if ($face_send_integral_profit <= 0) {
  395. throw new \Exception('');
  396. }
  397. $user_id = $order->user_id;
  398. debug_log(['user_id' => $user_id], 'send_user_integral.log');
  399. if ($user_id) {
  400. $total_price = $order->total_price;
  401. debug_log(['total_price' => $total_price], 'send_user_integral.log');
  402. $integral = bcdiv(bcmul($total_price, $face_send_integral_profit, 2), 100, 2);
  403. AccountLog::saveLog($user_id, $integral, AccountLog::TYPE_INTEGRAL,
  404. AccountLog::LOG_TYPE_INCOME, AccountLog::ORDER_TYPE_FACE_PAY,
  405. $order->id, "商城当面付订单营销赠送积分, 订单号:{$order->order_no}");
  406. return [
  407. 'code' => 0,
  408. 'msg' => '发送成功'
  409. ];
  410. }
  411. return [
  412. 'code' => 1,
  413. 'msg' => '用户未找到'
  414. ];
  415. } catch (\Exception $e) {
  416. debug_log([
  417. 'msg' => $e->getMessage(),
  418. 'line' => $e->getLine(),
  419. 'file' => $e->getFile()
  420. ], 'send_user_integral.log');
  421. return [
  422. 'code' => 1,
  423. 'msg' => $e->getMessage()
  424. ];
  425. }
  426. }
  427. /**
  428. * 统计一个时间段 入住商或者当面付订单的数量以及订单的金额
  429. * @param $mchId
  430. * @param $startTime
  431. * @param $endTime
  432. * @return array|\yii\db\ActiveRecord|null
  433. * @author: hankaige
  434. * @Time: 2025/7/15 10:12
  435. */
  436. public static function getMchOrder($mchId = 0, $startTime = 0, $endTime = 0){
  437. $query = self::find()->where(['mch_id' => $mchId,'is_pay' => self::IS_PAY_TRUE,'is_delete' => self::IS_DELETE_FALSE]);
  438. if($startTime > 0){
  439. $query->andWhere(['>=','created_at',$startTime]);
  440. }
  441. if($endTime){
  442. $query->andWhere(['<=','created_at',$endTime]);
  443. }
  444. return $query->select('count(id) AS order_num,SUM(total_price) as order_price')->asArray()->one();
  445. }
  446. }