Order.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. <?php
  2. namespace Store\Lib;
  3. use Dever;
  4. class Order
  5. {
  6. # 得到商品和总价
  7. private function goods()
  8. {
  9. $goods = Dever::input('goods');
  10. if (!$goods) {
  11. Dever::alert('请传入商品');
  12. }
  13. $goods = Dever::json_decode($goods);
  14. $this->data['price'] = 0;
  15. $this->data['p_price'] = 0;
  16. $this->data['num'] = 0;
  17. $this->data['name'] = array();
  18. $source_id = Dever::input('source_id');
  19. # 计算总价格
  20. foreach ($goods as $k => $v) {
  21. if (strstr($v['id'], '-')) {
  22. $t = explode('-', $v['id']);
  23. $v['id'] = $t[0];
  24. $s = $t[1];
  25. } else {
  26. $s = -1;
  27. }
  28. $n = $v['num'];
  29. $where['store_id'] = $source_id;
  30. $where['goods_id'] = $v['id'];
  31. $where['sku_id'] = $s;
  32. $goods_sku = Dever::db('store/goods_sku')->getOne($where);
  33. $this->data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v['id'], $s, 2);
  34. if (isset($this->data['list'][$k]['min']) && $this->data['list'][$k]['min'] > 0) {
  35. if ($n < $this->data['list'][$k]['min']) {
  36. //$n = $this->data['list'][$k]['min'];
  37. }
  38. }
  39. if ($goods_sku && $goods_sku['total'] < $n) {
  40. $n = $goods_sku['total'];
  41. }
  42. $this->data['list'][$k]['num'] = $n;
  43. $this->data['list'][$k]['buy_num'] = $n;
  44. $this->data['num'] += $n;
  45. $price = $this->data['list'][$k]['price'];
  46. $this->data['price'] += $price * $n;
  47. $this->data['name'][] = $this->data['list'][$k]['name'];
  48. }
  49. $this->data['name'] = implode(',', $this->data['name']);
  50. }
  51. # 下单
  52. public function action_commit_api()
  53. {
  54. $info = Dever::input('info');
  55. $this->goods();
  56. $order_data['type'] = 2;
  57. $order_data['type_id'] = Dever::input('store_id');
  58. $order_data['source_id'] = Dever::input('source_id');
  59. if ($order_data['type_id'] == $order_data['source_id']) {
  60. Dever::alert('发货仓库和收货仓库不能相同');
  61. }
  62. $order_data['source_type'] = 2;
  63. $order_data['name'] = $this->data['name'];
  64. $order_data['num'] = $this->data['num'];
  65. $order_data['info'] = $info;
  66. $order_data['price'] = $this->data['price'];
  67. $order_data['p_price'] = 0;
  68. $order_data['operdate'] = time();
  69. $order_data['status'] = 3;
  70. $order_data['order_num'] = $this->getOrderId();
  71. $order_data['refund_cash'] = 0;
  72. $order_data['refund_p_cash'] = 0;
  73. $id = Dever::db('shop/buy_order')->insert($order_data);
  74. if (!$id) {
  75. Dever::alert('下单失败');
  76. }
  77. $order_data['id'] = $id;
  78. $buy_order_goods = array();
  79. foreach($this->data['list'] as $k => $v) {
  80. $data['order_id'] = $id;
  81. $data['goods_id'] = $v['id'];
  82. $data['sku_id'] = $v['sku_id'];
  83. $data['price'] = $v['price'];
  84. $data['p_price'] = 0;
  85. $data['num'] = $v['buy_num'];
  86. $state = Dever::db('shop/buy_order_goods')->insert($data);
  87. if ($state) {
  88. $buy_order_goods[$k] = $data;
  89. $buy_order_goods[$k]['id'] = $state;
  90. }
  91. }
  92. Dever::load('cash/lib/order')->up($order_data, 1, 1);
  93. # 减少发货库存
  94. if ($buy_order_goods) {
  95. Dever::load('store/lib/goods')->oper($order_data['source_id'], 1, 2, $buy_order_goods);
  96. }
  97. return Dever::url('project/database/list&project=shop&table=buy_order&menu=shop&menu_id=77&search_option_state=1&search_option_type=2&search_option_source_type=2', 'manage');
  98. }
  99. # 生成订单号
  100. public function getOrderId()
  101. {
  102. $where['order_num'] = Dever::order('C');
  103. $state = Dever::db('shop/buy_order')->one($where);
  104. if (!$state) {
  105. return $where['order_num'];
  106. } else {
  107. return $this->getOrderId();
  108. }
  109. }
  110. }