Pay.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php namespace Porder\Lib\Source;
  2. use Dever;
  3. use Porder\Lib\Pay as Core;
  4. class Pay extends Core
  5. {
  6. protected $type = 'source';
  7. # 获取明细
  8. protected function getDetail($info)
  9. {
  10. $source = Dever::db('psource/info')->find(['id' => $info['id']], ['col' => 'id,name,pic,cate_id,channel_id,cate_parent_id,type,have_vip']);
  11. if (!$source) {
  12. Dever::error('资源不存在');
  13. }
  14. if ($this->set['id'] != $source['cate_id']) {
  15. Dever::error('资源不存在');
  16. }
  17. //$detail = Dever::load(\Api\Lib\Sku::class)->getList(['info_id' => $source['id']], $info['sku_id'], 'psource');
  18. $detail = Dever::load(\Api\Lib\Sku::class)->getInfo(['id' => $info['sku_id'], 'info_id' => $source['id']], 'psource');
  19. if (!$detail) {
  20. Dever::error('规格不存在');
  21. }
  22. $detail = Dever::load(\Psource\Lib\Price::class)->get($detail, $this->set, $info['num']);
  23. $have = Dever::load(\Psource\Lib\Price::class)->getHave($source);
  24. if ($have['status'] != 1) {
  25. Dever::error('专享资源无法购买');
  26. }
  27. if (!$detail['pic']) {
  28. $source['pic'] = explode(',', $source['pic']);
  29. $detail['pic'] = $source['pic'][0];
  30. }
  31. $this->detail[] = [
  32. 'uid' => $this->place->uid,
  33. 'source_id' => $source['id'],
  34. 'source_type' => $source['type'],
  35. 'sku_id' => $detail['id'],
  36. 'scope' => $detail['scope'],
  37. 'name' => $source['name'],
  38. 'sku_name' => $detail['name'],
  39. 'pic' => $detail['pic'],
  40. 'cash' => Dever::number($detail['price']),
  41. 'cash_text' => $detail['price_text'],
  42. 'total_cash' => $detail['total_price'],
  43. 'pay_cash' => $detail['total_price'],
  44. 'num' => intval($detail['buy_num']),
  45. 'unum' => $detail['unum'],
  46. 'stock' => $detail['stock'],
  47. 'promotion' => $detail['promotion'],
  48. 'promotion_text' => $detail['promotion_text'],
  49. ];
  50. if ($source['type'] == 1) {
  51. # 实物,如果有快递发货
  52. $this->order['deliver_type'] = 1;
  53. # 0是销售渠道配送,1是平台配送
  54. $this->order['method'] = 1;
  55. # 1是需要填写地址,2是不需要
  56. $this->order['is_address'] = 1;
  57. if ($detail['stock'] && $detail['stock']['list'] && empty($this->order['method_list'])) {
  58. # 如果有店铺配送
  59. $this->order['method_list'] = $detail['stock']['list'];
  60. $this->order['method'] = $detail['stock']['value'];
  61. $this->order['is_address'] = $detail['stock']['address'];
  62. if ($this->order['method'] == 0 && $this->order['is_address'] == 2) {
  63. # 自提,获取店铺地址
  64. $this->order['sales_address'] = Dever::load(\Psales\Lib\Info::class)->getInfo($this->order['sales_type'], $this->order['sales_id']);
  65. }
  66. }
  67. }
  68. if (count($this->detail) < 3) {
  69. $this->order['name'][$source['id']] = $source['name'];
  70. }
  71. $this->order['cash'] += $detail['price'] * $detail['buy_num'];
  72. $this->order['num'] += $detail['buy_num'];
  73. }
  74. # 验证库存
  75. protected function check()
  76. {
  77. if (isset($this->order['deliver_type']) && $this->order['deliver_type'] == 1 && $this->order['is_address'] == 1 && !$this->order['address_id']) {
  78. Dever::error('您还未填写收货地址');
  79. }
  80. foreach ($this->detail as $v) {
  81. $stock = Dever::load(\Pstock\Lib\Info::class)->yue($v['source_id'], $v['sku_id']);
  82. if ($stock['num'] >= 0) {
  83. if ($stock['num'] < $v['num']) {
  84. Dever::error($v['name'] . '[' . $v['sku_name'] . ']库存不足');
  85. } else {
  86. $state = Dever::load(\Pstock\Lib\Info::class)->sell($this->order, $v['source_id'], $v['sku_id'], $v['num'], '订单号:' . $this->order['order_num']);
  87. if (!$state) {
  88. Dever::error('库存不足');
  89. }
  90. }
  91. }
  92. }
  93. if ($this->order['method'] == 0) {
  94. $this->order['method'] = 2;
  95. if ($this->order['is_address'] == 2) {
  96. $this->order['method'] = 3;
  97. }
  98. }
  99. }
  100. }