Order.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510
  1. <?php
  2. namespace addons\TinyShop\common\models\order;
  3. use Yii;
  4. use common\enums\StatusEnum;
  5. use common\helpers\BcHelper;
  6. use common\traits\HasOneMerchant;
  7. use common\models\extend\PayLog;
  8. use common\models\common\Provinces;
  9. use common\models\member\Member;
  10. use addons\TinyShop\common\enums\OrderStatusEnum;
  11. use addons\TinyShop\common\models\common\ExpressCompany;
  12. use addons\TinyShop\common\models\marketing\Coupon;
  13. use addons\TinyShop\common\models\marketing\WholesaleRecord;
  14. /**
  15. * This is the model class for table "{{%addon_tiny_shop_order}}".
  16. *
  17. * @property int $id 订单id
  18. * @property int $merchant_id 商户id
  19. * @property string|null $merchant_title 商户店铺名称
  20. * @property string|null $order_sn 订单编号
  21. * @property string|null $unite_no 订单关联编号(批量支付)
  22. * @property string|null $order_from 订单来源
  23. * @property string|null $out_trade_no 外部交易号
  24. * @property int|null $order_type 订单类型
  25. * @property int|null $pay_type 支付类型
  26. * @property int|null $shipping_type 订单配送方式
  27. * @property int|null $buyer_id 买家id
  28. * @property string|null $buyer_nickname 买家会员名称
  29. * @property string|null $buyer_ip 买家ip
  30. * @property string|null $buyer_message 买家附言
  31. * @property int|null $receiver_id 收货地址ID
  32. * @property string|null $receiver_mobile 收货人的手机号码
  33. * @property int|null $receiver_province_id 收货人所在省
  34. * @property int|null $receiver_city_id 收货人所在城市
  35. * @property int|null $receiver_area_id 收货人所在街道
  36. * @property string|null $receiver_name 收货人详细地址
  37. * @property string|null $receiver_details 收货人详细地址
  38. * @property string|null $receiver_zip 收货人邮编
  39. * @property string|null $receiver_realname 收货人姓名
  40. * @property string|null $receiver_longitude 收货人经度
  41. * @property string|null $receiver_latitude 收货人纬度
  42. * @property int|null $seller_star 卖家对订单的标注星标
  43. * @property string|null $seller_memo 卖家对订单的备注
  44. * @property int|null $consign_time_adjust 卖家延迟发货时间
  45. * @property float|null $shipping_money 订单运费
  46. * @property float|null $product_money 商品优惠后总价
  47. * @property float|null $product_original_money 商品原本总价
  48. * @property float|null $product_profit_price 商品利润
  49. * @property int|null $product_type 商品类型
  50. * @property int|null $product_count 订单数量
  51. * @property float|null $order_money 订单总价
  52. * @property float|null $pay_money 订单实付金额
  53. * @property float|null $final_money 预售尾款
  54. * @property int|null $point 订单消耗积分
  55. * @property int $marketing_id 营销活动id
  56. * @property string $marketing_type 营销活动类型
  57. * @property int $marketing_product_id 营销活动产品id
  58. * @property int $wholesale_record_id 拼团记录ID
  59. * @property float|null $marketing_money 订单优惠活动金额
  60. * @property int|null $give_point 订单赠送积分
  61. * @property int|null $give_growth 赠送成长值
  62. * @property float|null $give_coin 订单成功之后返购物币
  63. * @property int|null $order_status 订单状态
  64. * @property int|null $pay_status 订单付款状态
  65. * @property int|null $shipping_status 订单配送状态
  66. * @property int|null $feedback_status 订单维权状态
  67. * @property int|null $is_evaluate 是否评价 0为未评价 1为已评价 2为已追评
  68. * @property float|null $tax_money 税费
  69. * @property int|null $store_id 门店id
  70. * @property int|null $invoice_id 发票id
  71. * @property int|null $express_company_id 物流公司
  72. * @property int|null $give_point_type 积分返还类型 1 订单完成 2 订单收货 3 支付订单
  73. * @property int|null $give_growth_type 成长值返还类型 1 订单完成 2 订单收货 3 支付订单
  74. * @property int|null $caballero_member_id 骑手用户id
  75. * @property int|null $pay_time 订单付款时间
  76. * @property int|null $receiving_time 骑手接单时间
  77. * @property int|null $consign_time 卖家发货时间
  78. * @property int|null $sign_time 买家签收时间
  79. * @property int|null $finish_time 订单完成时间
  80. * @property int|null $close_time 关闭的时间
  81. * @property int|null $auto_sign_time 自动签收时间
  82. * @property int|null $auto_finish_time 自动完成时间
  83. * @property int|null $auto_evaluate_time 自动评价时间
  84. * @property string|null $fixed_telephone 固定电话
  85. * @property string|null $distribution_time_out 配送时间段
  86. * @property int|null $subscribe_shipping_start_time 预约配送开始时间
  87. * @property int|null $subscribe_shipping_end_time 预约配送结束时间
  88. * @property int|null $is_new_member 是否新顾客
  89. * @property int $is_print 已打印 0未打印1已打印
  90. * @property int|null $is_oversold 是否超卖
  91. * @property float|null $refund_money 退款金额
  92. * @property int|null $refund_num 退款数量
  93. * @property int|null $is_after_sale 售后状态
  94. * @property string|null $promoter_code 推广码
  95. * @property int|null $promoter_id 推广人ID
  96. * @property string|null $promoter_nickname 推广人昵称
  97. * @property int|null $status 状态[-1:删除;0:禁用;1启用]
  98. * @property int|null $created_at
  99. * @property int|null $updated_at
  100. */
  101. class Order extends \common\models\base\BaseModel
  102. {
  103. use HasOneMerchant;
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public static function tableName()
  108. {
  109. return '{{%addon_tiny_shop_order}}';
  110. }
  111. /**
  112. * {@inheritdoc}
  113. */
  114. public function rules()
  115. {
  116. return [
  117. [
  118. [
  119. 'receiver_id',
  120. 'receiver_realname',
  121. 'receiver_name',
  122. 'receiver_mobile',
  123. 'receiver_details',
  124. 'receiver_province_id',
  125. 'receiver_city_id',
  126. 'receiver_area_id'
  127. ],
  128. 'required',
  129. 'on' => 'address'
  130. ],
  131. [
  132. [
  133. 'merchant_id',
  134. 'order_type',
  135. 'pay_type',
  136. 'shipping_type',
  137. 'buyer_id',
  138. 'receiver_province_id',
  139. 'receiver_city_id',
  140. 'receiver_area_id',
  141. 'seller_star',
  142. 'consign_time_adjust',
  143. 'product_type',
  144. 'product_count',
  145. 'point',
  146. 'marketing_id',
  147. 'marketing_product_id',
  148. 'wholesale_record_id',
  149. 'give_point',
  150. 'give_growth',
  151. 'order_status',
  152. 'pay_status',
  153. 'shipping_status',
  154. 'feedback_status',
  155. 'is_evaluate',
  156. 'store_id',
  157. 'invoice_id',
  158. 'express_company_id',
  159. 'give_point_type',
  160. 'give_growth_type',
  161. 'caballero_member_id',
  162. 'pay_time',
  163. 'receiving_time',
  164. 'consign_time',
  165. 'sign_time',
  166. 'finish_time',
  167. 'close_time',
  168. 'auto_sign_time',
  169. 'auto_finish_time',
  170. 'auto_evaluate_time',
  171. 'subscribe_shipping_start_time',
  172. 'subscribe_shipping_end_time',
  173. 'is_new_member',
  174. 'is_print',
  175. 'is_oversold',
  176. 'refund_num',
  177. 'is_after_sale',
  178. 'promoter_id',
  179. 'status',
  180. 'created_at',
  181. 'updated_at'
  182. ],
  183. 'integer'
  184. ],
  185. [
  186. [
  187. 'shipping_money',
  188. 'product_money',
  189. 'product_original_money',
  190. 'product_profit_price',
  191. 'order_money',
  192. 'pay_money',
  193. 'final_money',
  194. 'marketing_money',
  195. 'give_coin',
  196. 'tax_money',
  197. 'refund_money'
  198. ],
  199. 'number'
  200. ],
  201. [['merchant_title', 'receiver_longitude', 'receiver_latitude', 'promoter_nickname'], 'string', 'max' => 100],
  202. [
  203. [
  204. 'order_sn',
  205. 'unite_no',
  206. 'order_from',
  207. 'out_trade_no',
  208. 'buyer_nickname',
  209. 'receiver_realname',
  210. 'marketing_type',
  211. 'fixed_telephone',
  212. 'distribution_time_out',
  213. 'promoter_code'
  214. ],
  215. 'string',
  216. 'max' => 50
  217. ],
  218. [['buyer_ip', 'receiver_zip'], 'string', 'max' => 20],
  219. [['buyer_message', 'receiver_name', 'receiver_details'], 'string', 'max' => 200],
  220. [['receiver_mobile'], 'string', 'max' => 11],
  221. [['seller_memo'], 'string', 'max' => 255],
  222. ];
  223. }
  224. /**
  225. * {@inheritdoc}
  226. */
  227. public function attributeLabels()
  228. {
  229. return [
  230. 'id' => '订单id',
  231. 'merchant_id' => '商户id',
  232. 'merchant_title' => '商户店铺名称',
  233. 'order_sn' => '订单编号',
  234. 'unite_no' => '订单关联编号(多订单)',
  235. 'order_from' => '订单来源',
  236. 'out_trade_no' => '外部交易号',
  237. 'order_type' => '订单类型',
  238. 'pay_type' => '支付类型',
  239. 'shipping_type' => '订单配送方式',
  240. 'buyer_id' => '买家id',
  241. 'buyer_nickname' => '买家会员名称',
  242. 'buyer_ip' => '买家ip',
  243. 'buyer_message' => '买家附言',
  244. 'receiver_id' => '收货地址ID',
  245. 'receiver_mobile' => '收货人手机号码',
  246. 'receiver_province_id' => '收货人所在省',
  247. 'receiver_city_id' => '收货人所在城市',
  248. 'receiver_area_id' => '收货人所在街道',
  249. 'receiver_name' => '收货人地址',
  250. 'receiver_details' => '收货人详细地址',
  251. 'receiver_zip' => '收货人邮编',
  252. 'receiver_realname' => '收货人姓名',
  253. 'receiver_longitude' => '收货人经度',
  254. 'receiver_latitude' => '收货人纬度',
  255. 'seller_star' => '卖家对订单的标注星标',
  256. 'seller_memo' => '卖家对订单的备注',
  257. 'consign_time_adjust' => '卖家延迟发货时间',
  258. 'shipping_money' => '订单运费',
  259. 'product_money' => '商品优惠后总价',
  260. 'product_original_money' => '商品原本总价',
  261. 'product_profit_price' => '商品利润',
  262. 'product_type' => '商品类型',
  263. 'product_count' => '订单数量',
  264. 'order_money' => '订单总价',
  265. 'pay_money' => '订单实付金额',
  266. 'final_money' => '预售尾款',
  267. 'point' => '订单消耗积分',
  268. 'marketing_id' => '营销活动id',
  269. 'marketing_product_id' => '营销活动产品id',
  270. 'marketing_type' => '营销活动类型',
  271. 'marketing_money' => '订单优惠活动金额',
  272. 'wholesale_record_id' => '拼团记录ID',
  273. 'give_point' => '订单赠送积分',
  274. 'give_growth' => '赠送成长值',
  275. 'give_coin' => '订单成功之后返购物币',
  276. 'order_status' => '订单状态',
  277. 'pay_status' => '订单付款状态',
  278. 'shipping_status' => '订单配送状态',
  279. 'feedback_status' => '订单维权状态',
  280. 'is_evaluate' => '是否评价 0为未评价 1为已评价 2为已追评',
  281. 'tax_money' => '税费',
  282. 'store_id' => '门店id',
  283. 'invoice_id' => '发票id',
  284. 'express_company_id' => '物流公司',
  285. 'give_point_type' => '积分返还类型 1 订单完成 2 订单收货 3 支付订单',
  286. 'give_growth_type' => '成长值返还类型 1 订单完成 2 订单收货 3 支付订单',
  287. 'caballero_member_id' => '骑手用户id',
  288. 'pay_time' => '订单付款时间',
  289. 'receiving_time' => '骑手接单时间',
  290. 'consign_time' => '卖家发货时间',
  291. 'sign_time' => '买家签收时间',
  292. 'finish_time' => '订单完成时间',
  293. 'close_time' => '关闭的时间',
  294. 'auto_sign_time' => '自动签收时间',
  295. 'auto_finish_time' => '自动完成时间',
  296. 'auto_evaluate_time' => '自动评价时间',
  297. 'fixed_telephone' => '固定电话',
  298. 'distribution_time_out' => '配送时间段',
  299. 'subscribe_shipping_start_time' => '预约配送开始时间',
  300. 'subscribe_shipping_end_time' => '预约配送结束时间',
  301. 'is_new_member' => '是否新顾客',
  302. 'is_print' => '已打印 0未打印1已打印',
  303. 'is_oversold' => '是否超卖',
  304. 'refund_money' => '退款金额',
  305. 'refund_num' => '退款数量',
  306. 'is_after_sale' => '售后状态',
  307. 'promoter_code' => '推广码',
  308. 'promoter_id' => '推广人ID',
  309. 'promoter_nickname' => '推广人昵称',
  310. 'status' => '状态[-1:删除;0:禁用;1启用]',
  311. 'created_at' => 'Created At',
  312. 'updated_at' => 'Updated At',
  313. ];
  314. }
  315. /**
  316. * @return array
  317. */
  318. public function scenarios()
  319. {
  320. $scenarios = parent::scenarios();
  321. $scenarios['address'] = array_keys($this->attributeLabels());
  322. return $scenarios;
  323. }
  324. /**
  325. * @return \yii\db\ActiveQuery
  326. */
  327. public function getOrder()
  328. {
  329. return $this->hasOne(Order::class, ['id' => 'id']);
  330. }
  331. /**
  332. * 用户信息
  333. *
  334. * @return \yii\db\ActiveQuery
  335. */
  336. public function getMember()
  337. {
  338. return $this->hasOne(Member::class, ['id' => 'buyer_id']);
  339. }
  340. /**
  341. * 用户信息
  342. *
  343. * @return \yii\db\ActiveQuery
  344. */
  345. public function getBaseMember()
  346. {
  347. return $this->hasOne(Member::class, ['id' => 'buyer_id'])->select(['id', 'nickname', 'head_portrait']);
  348. }
  349. /**
  350. * 物流公司
  351. *
  352. * @return \yii\db\ActiveQuery
  353. */
  354. public function getExpressCompany()
  355. {
  356. return $this->hasOne(ExpressCompany::class, ['id' => 'express_company_id']);
  357. }
  358. /**
  359. * 订单发票
  360. *
  361. * @return \yii\db\ActiveQuery
  362. */
  363. public function getInvoice()
  364. {
  365. return $this->hasOne(Invoice::class, ['order_id' => 'id']);
  366. }
  367. /**
  368. * 订单优惠券
  369. *
  370. * @return \yii\db\ActiveQuery
  371. */
  372. public function getCoupon()
  373. {
  374. return $this->hasOne(Coupon::class, ['use_order_id' => 'id'])->select(['id', 'use_order_id', 'title']);
  375. }
  376. /**
  377. * 用户信息
  378. *
  379. * @return \yii\db\ActiveQuery
  380. */
  381. public function getCity()
  382. {
  383. return $this->hasOne(Provinces::class, ['id' => 'receiver_city']);
  384. }
  385. /**
  386. * 订单商品
  387. *
  388. * @return \yii\db\ActiveQuery
  389. */
  390. public function getProduct()
  391. {
  392. return $this->hasMany(OrderProduct::class, ['order_id' => 'id']);
  393. }
  394. /**
  395. * 营销
  396. *
  397. * @return \yii\db\ActiveQuery
  398. */
  399. public function getMarketingDetail()
  400. {
  401. return $this->hasMany(MarketingDetail::class, ['order_id' => 'id']);
  402. }
  403. /**
  404. * 订单虚拟商品
  405. *
  406. * @return \yii\db\ActiveQuery
  407. */
  408. public function getProductVirtual()
  409. {
  410. return $this->hasMany(ProductVirtual::class, ['order_sn' => 'order_sn']);
  411. }
  412. /**
  413. * 订单商品物流配送
  414. *
  415. * @return \yii\db\ActiveQuery
  416. */
  417. public function getProductExpress()
  418. {
  419. return $this->hasMany(ProductExpress::class, ['order_id' => 'id']);
  420. }
  421. /**
  422. * 拼团记录
  423. *
  424. * @return \yii\db\ActiveQuery
  425. */
  426. public function getWholesaleRecord()
  427. {
  428. return $this->hasOne(WholesaleRecord::class, ['id' => 'wholesale_record_id']);
  429. }
  430. /**
  431. * @return \yii\db\ActiveQuery
  432. */
  433. public function getPayLog()
  434. {
  435. return $this->hasMany(PayLog::class, ['order_sn' => 'order_sn'])
  436. ->andWhere(['pay_status' => StatusEnum::ENABLED, 'addon_name' => 'TinyShop']);
  437. }
  438. /**
  439. * @return \yii\db\ActiveQuery
  440. */
  441. public function getStore()
  442. {
  443. return $this->hasOne(Store::class, ['order_id' => 'id']);
  444. }
  445. /**
  446. * @param bool $insert
  447. * @return bool
  448. */
  449. public function beforeSave($insert)
  450. {
  451. // 未支付/未付款
  452. if ($this->isNewRecord || $this->order_status == OrderStatusEnum::NOT_PAY) {
  453. // 支付金额为商品金额
  454. $this->pay_money = $this->product_money;
  455. // 发票(税收)
  456. if (!empty($this->invoice_id)) {
  457. $config = Yii::$app->tinyShopService->config->setting();
  458. $order_invoice_tax = $config['order_invoice_tax'] ?? 0;
  459. $order_invoice_tax = $order_invoice_tax > 0 ? BcHelper::div($order_invoice_tax, 100, 4) : 0;
  460. $this->tax_money = BcHelper::mul($this->pay_money, $order_invoice_tax);
  461. $this->pay_money += $this->tax_money;
  462. }
  463. // 实付增加运费
  464. $this->pay_money += $this->shipping_money;
  465. // 订单总金额
  466. $this->order_money = $this->product_money + $this->shipping_money;
  467. !$this->isNewRecord && Invoice::updateAll(['tax_money' => $this->tax_money], ['order_id' => $this->id]);
  468. }
  469. // 修改订单商品状态
  470. if (!$this->isNewRecord && $this->order_status != $this->oldAttributes['order_status']) {
  471. OrderProduct::updateAll(
  472. ['order_status' => $this->order_status],
  473. [
  474. 'order_status' => $this->oldAttributes['order_status'],
  475. 'order_id' => $this->id,
  476. ]
  477. );
  478. // 骑手端订单配送状态同步
  479. $this->caballero_member_id !== 0 && Yii::$app->tinyErrandService->order->updateStatusByMapId($this->id, $this->order_status);
  480. }
  481. return parent::beforeSave($insert);
  482. }
  483. }
粤ICP备19079148号