Order.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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, $operdate = false)
  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 ($update['status'] == 2) {
  38. $update['operdate'] = $operdate ? $operdate : time();
  39. $update['fdate'] = $update['operdate'];
  40. if (isset($order['fdate']) && $order['fdate'] && ($order['status'] == 5 || $order['status'] == 6)) {
  41. $update['fdate'] = $order['fdate'];
  42. }
  43. }
  44. if (isset($order['record_cash']) && $order['record_cash']) {
  45. $order['price'] += $order['record_cash'];
  46. }
  47. if ($info && $jstype == 3) {
  48. } else {
  49. $update['type'] = $order['type'];
  50. $update['type_id'] = $order['type_id'];
  51. if ($order['source_type']) {
  52. $update['source_type'] = $order['source_type'];
  53. $update['source_id'] = $order['source_id'];
  54. }
  55. $update['source_order_num'] = $order['order_num'];
  56. $update['num'] = $order['num'];
  57. if ($refund) {
  58. $update['cash'] = $refund['cash'];
  59. $update['p_cash'] = isset($refund['p_cash']) ? $refund['p_cash'] : 0;
  60. } else {
  61. $update['cash'] = $order['price'];
  62. $update['p_cash'] = $order['p_price'];
  63. }
  64. if ($jstype == 2) {
  65. $update['cash'] = -1*$update['cash'];
  66. $update['p_cash'] = -1*$update['p_cash'];
  67. $update['num'] = -1*$update['num'];
  68. }
  69. }
  70. if (isset($update['cash']) && $update['cash'] <= 0) {
  71. return;
  72. }
  73. if ($info) {
  74. $update['where_id'] = $info['id'];
  75. $state = Dever::db('cash/order')->update($update);
  76. } else {
  77. $update['order_num'] = $this->getOrderId();
  78. $state = Dever::db('cash/order')->insert($update);
  79. }
  80. if ($state && $jstype == 2 && $status == 2 && isset($order['status']) && $order['status'] == 8) {
  81. # 如果是退款结算,同时要把主订单也结算
  82. $where = array();
  83. $where['source_order_id'] = $order['id'];
  84. if ($pay_type > 1) {
  85. $where['jstype'] = 3;
  86. } else {
  87. $where['jstype'] = 1;
  88. }
  89. $where['audit'] = 1;
  90. $where['clear'] = true;
  91. $info = Dever::db('cash/order')->find($where);
  92. if ($info) {
  93. $set['status'] = 2;
  94. $set['audit'] = 2;
  95. $set['where_id'] = $info['id'];
  96. $set['operdate'] = $operdate ? $operdate : time();
  97. $set['fdate'] = $operdate ? $operdate : time();
  98. Dever::db('cash/order')->update($set);
  99. }
  100. }
  101. }
  102. # 生成订单号
  103. public function getOrderId()
  104. {
  105. $where['order_num'] = Dever::order('JS');
  106. $state = Dever::db('cash/order')->one($where);
  107. if (!$state) {
  108. return $where['order_num'];
  109. } else {
  110. return $this->getOrderId();
  111. }
  112. }
  113. #导出Excel
  114. public function out_order_api($data){
  115. $header = array('ID', '结算单号', '结算门店', '订货单编号', '数量', '结算金额', '结算类型', '入账状态', '审核状态', '支付类型', '结算时间','审核备注','生成时间', '礼品卡卡号');
  116. $body = array();
  117. foreach($data as $k => $v){
  118. $type_info = Dever::db('shop/info')->find(array('id'=>$v['type_id']));
  119. $config_jstype = Dever::db('cash/order')->config['config_jstype'];
  120. if(isset($v['jstype']) && $v['jstype']){
  121. $jstype = Dever::status($config_jstype,$v['jstype']);
  122. }
  123. $config_status = Dever::db('cash/order')->config['config_status'];
  124. if(isset($v['status']) && $v['status']){
  125. $status = Dever::status($config_status,$v['status']);
  126. }
  127. $config_audit = Dever::db('cash/order')->config['config_audit'];
  128. if(isset($v['audit']) && $v['audit']){
  129. $audit = Dever::status($config_audit,$v['audit']);
  130. }
  131. if(isset($v['pay_type']) && $v['pay_type']){
  132. if($v['pay_type'] == 1){
  133. $pay_type_name = '在线支付';
  134. }elseif($v['pay_type'] == 2){
  135. $pay_type_name = '礼品卡支付';
  136. }else{
  137. $pay_type_name = '钱包支付';
  138. }
  139. }
  140. $operdate = '-';
  141. if(isset($v['operdate']) && $v['operdate']){
  142. $operdate = date('Y-m-d H:i',$v['operdate']);
  143. }
  144. $cdate = '-';
  145. if(isset($v['cdate']) && $v['cdate']){
  146. $cdate = date('Y-m-d H:i',$v['cdate']);
  147. }
  148. $card_code_card = '';
  149. if ($v['pay_type'] == 2) {
  150. $sell_order = Dever::db('shop/sell_order')->find($v['source_order_id']);
  151. if ($sell_order) {
  152. $card_code_card = $sell_order['card_code_card']."\n";
  153. }
  154. }
  155. $d = array
  156. (
  157. $v['id'],
  158. $v['order_num'],
  159. $type_info['name'],
  160. $v['source_order_num'],
  161. $v['num'],
  162. $v['cash'],
  163. $jstype,
  164. $status,
  165. $audit,
  166. $pay_type_name,
  167. $operdate,
  168. $v['desc'],
  169. $cdate,
  170. $card_code_card,
  171. );
  172. $body[] = $d;
  173. }
  174. $file = Dever::input('excel_name');
  175. return Dever::excelExport($body, $header, $file);
  176. }
  177. }