Order.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. namespace Cash\Lib;
  3. use Dever;
  4. class Order
  5. {
  6. # 更新结算单
  7. public function up($order, $jstype = 1, $audit = 1, $refund = false, $pay_type = 1)
  8. {
  9. Dever::config('base')->hook = true;
  10. if (!$order) {
  11. return false;
  12. }
  13. if ($audit == 2) {
  14. $status = 2;
  15. } else {
  16. $status = 1;
  17. }
  18. if ($jstype == 2) {
  19. $audit_type = 2;
  20. } else {
  21. $audit_type = 1;
  22. }
  23. $where['source_order_id'] = $order['id'];
  24. $where['jstype'] = $jstype;
  25. if (!$refund) {
  26. $where['refund_id'] = -1;
  27. } else {
  28. $where['refund_id'] = $refund['id'];
  29. }
  30. $where['clear'] = true;
  31. $info = Dever::db('cash/order')->find($where);
  32. $update = $where;
  33. $update['pay_type'] = $pay_type;
  34. $update['status'] = $status;
  35. $update['audit'] = $audit;
  36. $update['audit_type'] = $audit_type;
  37. if ($info && $jstype == 3) {
  38. } else {
  39. $update['type'] = $order['type'];
  40. $update['type_id'] = $order['type_id'];
  41. if ($order['source_type']) {
  42. $update['source_type'] = $order['source_type'];
  43. $update['source_id'] = $order['source_id'];
  44. }
  45. if ($update['status'] == 2) {
  46. $update['operdate'] = time();
  47. $update['fdate'] = $update['operdate'];
  48. if (isset($order['fdate']) && $order['fdate'] && ($order['status'] == 5 || $order['status'] == 6)) {
  49. $update['fdate'] = $order['fdate'];
  50. }
  51. }
  52. $update['source_order_num'] = $order['order_num'];
  53. $update['num'] = $order['num'];
  54. if ($refund) {
  55. $update['cash'] = $refund['cash'];
  56. $update['p_cash'] = isset($refund['p_cash']) ? $refund['p_cash'] : 0;
  57. } else {
  58. $update['cash'] = $order['price'];
  59. $update['p_cash'] = $order['p_price'];
  60. }
  61. if ($jstype == 2) {
  62. $update['cash'] = -1*$update['cash'];
  63. $update['p_cash'] = -1*$update['p_cash'];
  64. $update['num'] = -1*$update['num'];
  65. }
  66. }
  67. if ($info) {
  68. $update['where_id'] = $info['id'];
  69. $state = Dever::db('cash/order')->update($update);
  70. } else {
  71. $update['order_num'] = $this->getOrderId();
  72. $state = Dever::db('cash/order')->insert($update);
  73. }
  74. if ($state && $jstype == 2 && $status == 2 && isset($order['status']) && $order['status'] == 8) {
  75. # 如果是退款结算,同时要把主订单也结算
  76. $where = array();
  77. $where['source_order_id'] = $order['id'];
  78. if ($pay_type > 1) {
  79. $where['jstype'] = 3;
  80. } else {
  81. $where['jstype'] = 1;
  82. }
  83. $where['audit'] = 1;
  84. $where['clear'] = true;
  85. $info = Dever::db('cash/order')->find($where);
  86. if ($info) {
  87. $set['status'] = 2;
  88. $set['audit'] = 2;
  89. $set['where_id'] = $info['id'];
  90. $set['operdate'] = time();
  91. $set['fdate'] = time();
  92. Dever::db('cash/order')->update($set);
  93. }
  94. }
  95. }
  96. # 生成订单号
  97. public function getOrderId()
  98. {
  99. $where['order_num'] = Dever::order('JS');
  100. $state = Dever::db('cash/order')->one($where);
  101. if (!$state) {
  102. return $where['order_num'];
  103. } else {
  104. return $this->getOrderId();
  105. }
  106. }
  107. }