Order.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. <?php
  2. namespace Factory\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. $factory_id = Dever::input('factory_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. $goods_sku = Dever::db('factory/goods_sku')->find(array('factory_id' => $factory_id, 'goods_id' => $v['id'], 'sku_id' => $s));
  30. $this->data['list'][$k] = Dever::load('goods/lib/info')->getPayInfo($v['id'], $s);
  31. if (isset($this->data['list'][$k]['min']) && $this->data['list'][$k]['min'] > 0) {
  32. if ($n < $this->data['list'][$k]['min']) {
  33. //$n = $this->data['list'][$k]['min'];
  34. }
  35. }
  36. $this->data['list'][$k]['num'] = $n;
  37. $this->data['list'][$k]['buy_num'] = $n;
  38. $this->data['num'] += $n;
  39. if ($goods_sku) {
  40. $p_price = $goods_sku['p_price'];
  41. } else {
  42. $p_price = 0;
  43. }
  44. $this->data['list'][$k]['p_price'] = $p_price;
  45. $price = $this->data['list'][$k]['price'];
  46. $this->data['price'] += $price * $n;
  47. $this->data['p_price'] += $p_price * $n;
  48. $this->data['name'][] = $this->data['list'][$k]['name'];
  49. }
  50. $this->data['name'] = implode(',', $this->data['name']);
  51. }
  52. # 下单
  53. public function action_commit_api()
  54. {
  55. $info = Dever::input('info');
  56. $this->goods();
  57. $order_data['type'] = 2;
  58. $order_data['type_id'] = Dever::input('store_id');
  59. $order_data['source_id'] = Dever::input('factory_id');
  60. $order_data['source_type'] = 3;
  61. $order_data['name'] = $this->data['name'];
  62. $order_data['num'] = $this->data['num'];
  63. $order_data['info'] = $info;
  64. $order_data['price'] = $this->data['price'];
  65. $order_data['p_price'] = $this->data['p_price'];
  66. $order_data['operdate'] = time();
  67. $order_data['status'] = 3;
  68. $order_data['order_num'] = $this->getOrderId();
  69. $order_data['refund_cash'] = 0;
  70. $order_data['refund_p_cash'] = 0;
  71. $id = Dever::db('shop/buy_order')->insert($order_data);
  72. if (!$id) {
  73. Dever::alert('下单失败');
  74. }
  75. $order_data['id'] = $id;
  76. foreach($this->data['list'] as $k => $v) {
  77. $data['order_id'] = $id;
  78. $data['goods_id'] = $v['id'];
  79. $data['sku_id'] = $v['sku_id'];
  80. $data['price'] = $v['price'];
  81. $data['p_price'] = $v['p_price'];
  82. $data['num'] = $v['buy_num'];
  83. $state = Dever::db('shop/buy_order_goods')->insert($data);
  84. }
  85. Dever::load('cash/lib/order')->up($order_data, 1, 1);
  86. 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=3', 'manage');
  87. }
  88. # 生成订单号
  89. public function getOrderId()
  90. {
  91. $where['order_num'] = Dever::order('F');
  92. $state = Dever::db('shop/buy_order')->one($where);
  93. if (!$state) {
  94. return $where['order_num'];
  95. } else {
  96. return $this->getOrderId();
  97. }
  98. }
  99. }