month.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422
  1. <?php
  2. /**
  3. * 任务计划 - 月执行的任务
  4. *
  5. *
  6. *
  7. *
  8. */
  9. defined('InShopNC') or exit('Access Invalid!');
  10. class monthControl extends BaseCronControl {
  11. /**
  12. * 默认方法
  13. */
  14. public function indexOp(){
  15. $this->_create_bill();
  16. }
  17. private function _create_bill() {
  18. //更新订单商品佣金值
  19. $this->_order_commis_rate_update();
  20. $model = Model('bill');
  21. //实物订单结算
  22. try {
  23. $model->beginTransaction();
  24. $this->_real_order();
  25. $model->commit();
  26. } catch (Exception $e) {
  27. $this->log('实物账单:'.$e->getMessage());
  28. }
  29. //虚拟订单结算
  30. try {
  31. $model->beginTransaction();
  32. $this->_vr_order();
  33. $model->commit();
  34. } catch (Exception $e) {
  35. $this->log('虚拟账单:'.$e->getMessage());
  36. }
  37. }
  38. /**
  39. * 生成上月账单[实物订单]
  40. */
  41. private function _real_order() {
  42. $model_order = Model('order');
  43. $model_bill = Model('bill');
  44. $order_statis_max_info = $model_bill->getOrderStatisInfo(array(),'os_end_date','os_month desc');
  45. //计算起始时间点,自动生成以月份为单位的空结算记录
  46. if (!$order_statis_max_info){
  47. $order_min_info = $model_order->getOrderInfo(array(),array(),'min(add_time) as add_time');
  48. $start_unixtime = is_numeric($order_min_info['add_time']) ? $order_min_info['add_time'] : time();
  49. } else {
  50. $start_unixtime = $order_statis_max_info['os_end_date'];
  51. }
  52. $data = array();
  53. $i = 1;
  54. $start_unixtime = strtotime(date('Y-m-01 00:00:00', $start_unixtime));
  55. $current_time = strtotime(date('Y-m-01 00:00:01',time()));
  56. while (($time = strtotime('-'.$i.' month',$current_time)) >= $start_unixtime) {
  57. if (date('Ym',$start_unixtime) == date('Ym',$time)) {
  58. //如果两个月份相等检查库是里否存在
  59. $order_statis = $model_bill->getOrderStatisInfo(array('os_month'=>date('Ym',$start_unixtime)));
  60. if ($order_statis) {
  61. break;
  62. }
  63. }
  64. $first_day_unixtime = strtotime(date('Y-m-01 00:00:00', $time)); //该月第一天0时unix时间戳
  65. $last_day_unixtime = strtotime(date('Y-m-01 23:59:59', $time)." +1 month -1 day"); //该月最后一天最后一秒时unix时间戳
  66. $key = count($data);
  67. $os_month = date('Ym',$first_day_unixtime);
  68. $data[$key]['os_month'] = $os_month;
  69. $data[$key]['os_year'] = date('Y',$first_day_unixtime);
  70. $data[$key]['os_start_date'] = $first_day_unixtime;
  71. $data[$key]['os_end_date'] = $last_day_unixtime;
  72. //生成所有店铺月订单出账单
  73. $this->_create_real_order_bill($data[$key]);
  74. $fileds = 'sum(ob_order_totals) as ob_order_totals,sum(ob_shipping_totals) as ob_shipping_totals,
  75. sum(ob_order_return_totals) as ob_order_return_totals,
  76. sum(ob_commis_totals) as ob_commis_totals,sum(ob_commis_return_totals) as ob_commis_return_totals,
  77. sum(ob_store_cost_totals) as ob_store_cost_totals,sum(ob_result_totals) as ob_result_totals';
  78. $order_bill_info = $model_bill->getOrderBillInfo(array('os_month'=>$os_month),$fileds);
  79. $data[$key]['os_order_totals'] = floatval($order_bill_info['ob_order_totals']);
  80. $data[$key]['os_shipping_totals'] = floatval($order_bill_info['ob_shipping_totals']);
  81. $data[$key]['os_order_return_totals'] = floatval($order_bill_info['ob_order_return_totals']);
  82. $data[$key]['os_commis_totals'] = floatval($order_bill_info['ob_commis_totals']);
  83. $data[$key]['os_commis_return_totals'] = floatval($order_bill_info['ob_commis_return_totals']);
  84. $data[$key]['os_store_cost_totals'] = floatval($order_bill_info['ob_store_cost_totals']);
  85. $data[$key]['os_result_totals'] = floatval($order_bill_info['ob_result_totals']);
  86. $i++;
  87. }
  88. krsort($data);
  89. foreach ($data as $v) {
  90. $insert = $model_bill->addOrderStatis($v);
  91. if (!$insert) {
  92. throw new Exception('生成平台月出账单['.$v['os_month'].']失败');
  93. }
  94. }
  95. }
  96. /**
  97. * 生成所有店铺月订单出账单[实物订单]
  98. *
  99. * @param int $data
  100. */
  101. private function _create_real_order_bill($data){
  102. $model_order = Model('order');
  103. $model_bill = Model('bill');
  104. $model_store = Model('store');
  105. //批量插件order_bill表
  106. // $condition = array();
  107. // $condition['order_state'] = ORDER_STATE_SUCCESS;
  108. // $condition['finnshed_time'] = array(array('egt',$data['os_start_date']),array('elt',$data['os_end_date']),'and');
  109. // 取出有最终成交订单的店铺ID数量(ID不重复)
  110. // $store_count = $model_order->getOrderInfo($condition,array(),'count(DISTINCT store_id) as c');
  111. // $store_count = $store_count['c'];
  112. //取店铺表数量(因为可能存在无订单,但有店铺活动费用,所以不再从订单表取店铺数量)
  113. $store_count = $model_store->getStoreCount(array());
  114. //分批生成该月份的店铺空结算表,每批生成300个店铺
  115. $insert = false;
  116. for ($i=0;$i<=$store_count;$i=$i+300){
  117. // $store_list = $model_order->getOrderList($condition,'','DISTINCT store_id','',"{$i},300");
  118. $store_list = $model_store->getStoreList(array(),null,'','store_id',"{$i},300");
  119. if ($store_list){
  120. //自动生成以月份为单位的空结算记录
  121. $data_bill = array();
  122. foreach($store_list as $store_info){
  123. $data_bill['ob_no'] = $data['os_month'].$store_info['store_id'];
  124. $data_bill['ob_start_date'] = $data['os_start_date'];
  125. $data_bill['ob_end_date'] = $data['os_end_date'];
  126. $data_bill['os_month'] = $data['os_month'];
  127. $data_bill['ob_state'] = 0;
  128. $data_bill['ob_store_id'] = $store_info['store_id'];
  129. if (!$model_bill->getOrderBillInfo(array('ob_no'=>$data_bill['ob_no']))) {
  130. $insert = $model_bill->addOrderBill($data_bill);
  131. if (!$insert) {
  132. throw new Exception('生成账单['.$data_bill['ob_no'].']失败');
  133. }
  134. //对已生成空账单进行销量、退单、佣金统计
  135. $update = $this->_calc_real_order_bill($data_bill);
  136. if (!$update){
  137. throw new Exception('更新账单['.$data_bill['ob_no'].']失败');
  138. }
  139. // 发送店铺消息
  140. $param = array();
  141. $param['code'] = 'store_bill_affirm';
  142. $param['store_id'] = $store_info['store_id'];
  143. $param['param'] = array(
  144. 'state_time' => date('Y-m-d H:i:s', $data_bill['ob_start_date']),
  145. 'end_time' => date('Y-m-d H:i:s', $data_bill['ob_end_date']),
  146. 'bill_no' => $data_bill['ob_no']
  147. );
  148. QueueClient::push('sendStoreMsg', $param);
  149. }
  150. }
  151. }
  152. }
  153. }
  154. /**
  155. * 计算某月内,某店铺的销量,退单量,佣金[实物订单]
  156. *
  157. * @param array $data_bill
  158. */
  159. private function _calc_real_order_bill($data_bill){
  160. $model_order = Model('order');
  161. $model_bill = Model('bill');
  162. $model_store = Model('store');
  163. $order_condition = array();
  164. $order_condition['order_state'] = ORDER_STATE_SUCCESS;
  165. $order_condition['store_id'] = $data_bill['ob_store_id'];
  166. $order_condition['finnshed_time'] = array('between',"{$data_bill['ob_start_date']},{$data_bill['ob_end_date']}");
  167. $update = array();
  168. //订单金额
  169. $fields = 'sum(order_amount) as order_amount,sum(shipping_fee) as shipping_amount,store_name';
  170. $order_info = $model_order->getOrderInfo($order_condition,array(),$fields);
  171. $update['ob_order_totals'] = floatval($order_info['order_amount']);
  172. //运费
  173. $update['ob_shipping_totals'] = floatval($order_info['shipping_amount']);
  174. //店铺名字
  175. $store_info = $model_store->getStoreInfoByID($data_bill['ob_store_id']);
  176. $update['ob_store_name'] = $store_info['store_name'];
  177. //佣金金额
  178. $order_info = $model_order->getOrderInfo($order_condition,array(),'count(DISTINCT order_id) as count');
  179. $order_count = $order_info['count'];
  180. $commis_rate_totals_array = array();
  181. //分批计算佣金,最后取总和
  182. for ($i = 0; $i <= $order_count; $i = $i + 300){
  183. $order_list = $model_order->getOrderList($order_condition,'','order_id','',"{$i},300");
  184. $order_id_array = array();
  185. foreach ($order_list as $order_info) {
  186. $order_id_array[] = $order_info['order_id'];
  187. }
  188. if (!empty($order_id_array)){
  189. $order_goods_condition = array();
  190. $order_goods_condition['order_id'] = array('in',$order_id_array);
  191. $field = 'SUM(ROUND(goods_pay_price*commis_rate/100,2)) as commis_amount';
  192. $order_goods_info = $model_order->getOrderGoodsInfo($order_goods_condition,$field);
  193. $commis_rate_totals_array[] = $order_goods_info['commis_amount'];
  194. }else{
  195. $commis_rate_totals_array[] = 0;
  196. }
  197. }
  198. $update['ob_commis_totals'] = floatval(array_sum($commis_rate_totals_array));
  199. //退款总额
  200. $model_refund = Model('refund_return');
  201. $refund_condition = array();
  202. $refund_condition['seller_state'] = 2;
  203. $refund_condition['store_id'] = $data_bill['ob_store_id'];
  204. $refund_condition['goods_id'] = array('gt',0);
  205. $refund_condition['admin_time'] = array(array('egt',$data_bill['ob_start_date']),array('elt',$data_bill['ob_end_date']),'and');
  206. $refund_info = $model_refund->getRefundReturnInfo($refund_condition,'sum(refund_amount) as amount');
  207. $update['ob_order_return_totals'] = floatval($refund_info['amount']);
  208. //退款佣金
  209. $refund = $model_refund->getRefundReturnInfo($refund_condition,'sum(ROUND(refund_amount*commis_rate/100,2)) as amount');
  210. if ($refund) {
  211. $update['ob_commis_return_totals'] = floatval($refund['amount']);
  212. } else {
  213. $update['ob_commis_return_totals'] = 0;
  214. }
  215. //店铺活动费用
  216. $model_store_cost = Model('store_cost');
  217. $cost_condition = array();
  218. $cost_condition['cost_store_id'] = $data_bill['ob_store_id'];
  219. $cost_condition['cost_state'] = 0;
  220. $cost_condition['cost_time'] = array(array('egt',$data_bill['ob_start_date']),array('elt',$data_bill['ob_end_date']),'and');
  221. $cost_info = $model_store_cost->getStoreCostInfo($cost_condition,'sum(cost_price) as cost_amount');
  222. $update['ob_store_cost_totals'] = floatval($cost_info['cost_amount']);
  223. //本期应结
  224. $update['ob_result_totals'] = $update['ob_order_totals'] - $update['ob_order_return_totals'] -
  225. $update['ob_commis_totals'] + $update['ob_commis_return_totals']-
  226. $update['ob_store_cost_totals'];
  227. $update['ob_create_date'] = time();
  228. $update['ob_state'] = 1;
  229. return $model_bill->editOrderBill($update,array('ob_no'=>$data_bill['ob_no']));
  230. }
  231. /**
  232. * 生成上月账单[虚拟订单]
  233. */
  234. private function _vr_order() {
  235. $model_order = Model('vr_order');
  236. $model_bill = Model('vr_bill');
  237. $order_statis_max_info = $model_bill->getOrderStatisInfo(array(),'os_end_date','os_month desc');
  238. //计算起始时间点,自动生成以月份为单位的空结算记录
  239. if (!$order_statis_max_info){
  240. $order_min_info = $model_order->getOrderInfo(array(),'min(add_time) as add_time');
  241. $start_unixtime = is_numeric($order_min_info['add_time']) ? $order_min_info['add_time'] : time();
  242. } else {
  243. $start_unixtime = $order_statis_max_info['os_end_date'];
  244. }
  245. $data = array();
  246. $i = 1;
  247. $start_unixtime = strtotime(date('Y-m-01 00:00:00', $start_unixtime));
  248. $current_time = strtotime(date('Y-m-01 00:00:01',time()));
  249. while (($time = strtotime('-'.$i.' month',$current_time)) >= $start_unixtime) {
  250. if (date('Ym',$start_unixtime) == date('Ym',$time)) {
  251. //如果两个月份相等检查库是里否存在
  252. $order_statis = $model_bill->getOrderStatisInfo(array('os_month'=>date('Ym',$start_unixtime)));
  253. if ($order_statis) {
  254. break;
  255. }
  256. }
  257. $first_day_unixtime = strtotime(date('Y-m-01 00:00:00', $time)); //该月第一天0时unix时间戳
  258. $last_day_unixtime = strtotime(date('Y-m-01 23:59:59', $time)." +1 month -1 day"); //该月最后一天最后一秒时unix时间戳
  259. $key = count($data);
  260. $os_month = date('Ym',$first_day_unixtime);
  261. $data[$key]['os_month'] = $os_month;
  262. $data[$key]['os_year'] = date('Y',$first_day_unixtime);
  263. $data[$key]['os_start_date'] = $first_day_unixtime;
  264. $data[$key]['os_end_date'] = $last_day_unixtime;
  265. //生成所有店铺月订单出账单
  266. $this->_create_vr_order_bill($data[$key]);
  267. $fileds = 'sum(ob_order_totals) as ob_order_totals,
  268. sum(ob_commis_totals) as ob_commis_totals,sum(ob_result_totals) as ob_result_totals';
  269. $order_bill_info = $model_bill->getOrderBillInfo(array('os_month'=>$os_month),$fileds);
  270. $data[$key]['os_order_totals'] = floatval($order_bill_info['ob_order_totals']);
  271. $data[$key]['os_commis_totals'] = floatval($order_bill_info['ob_commis_totals']);
  272. $data[$key]['os_result_totals'] = floatval($order_bill_info['ob_result_totals']);
  273. $i++;
  274. }
  275. krsort($data);
  276. foreach ($data as $v) {
  277. $insert = $model_bill->addOrderStatis($v);
  278. if (!$insert) {
  279. throw new Exception('生成平台月出账单['.$v['os_month'].']失败');
  280. }
  281. }
  282. }
  283. /**
  284. * 生成所有店铺月订单出账单[虚拟订单]
  285. *
  286. * @param int $data
  287. */
  288. private function _create_vr_order_bill($data){
  289. $model_order = Model('vr_order');
  290. $model_bill = Model('vr_bill');
  291. $model_store = Model('store');
  292. //批量插入order_bill表
  293. $condition = array();
  294. $condition['order_state'] = array('egt',ORDER_STATE_PAY);
  295. $condition['payment_time'] = array(array('egt',$data['os_start_date']),array('elt',$data['os_end_date']),'and');
  296. //取出有最终成交订单的店铺ID数量(ID不重复)
  297. $order_info = $model_order->getOrderInfo($condition,'count(DISTINCT store_id) as store_count');
  298. $store_count = $order_info['store_count'];
  299. //分批生成该月份的店铺空结算表,每批生成300个店铺
  300. $insert = false;
  301. for ($i=0;$i<=$store_count;$i=$i+300){
  302. $store_list = $model_order->getOrderList($condition,'','DISTINCT store_id','',"{$i},300");
  303. if ($store_list){
  304. //自动生成以月份为单位的空结算记录
  305. $data_bill = array();
  306. foreach($store_list as $store_info){
  307. $data_bill['ob_no'] = $data['os_month'].$store_info['store_id'];
  308. $data_bill['ob_start_date'] = $data['os_start_date'];
  309. $data_bill['ob_end_date'] = $data['os_end_date'];
  310. $data_bill['os_month'] = $data['os_month'];
  311. $data_bill['ob_state'] = 0;
  312. $data_bill['ob_store_id'] = $store_info['store_id'];
  313. if (!$model_bill->getOrderBillInfo(array('ob_no'=>$data_bill['ob_no']))) {
  314. $insert = $model_bill->addOrderBill($data_bill);
  315. if (!$insert) {
  316. throw new Exception('生成账单['.$data_bill['ob_no'].']失败');
  317. }
  318. //对已生成空账单进行销量、佣金统计
  319. $update = $this->_calc_vr_order_bill($data_bill);
  320. if (!$update){
  321. throw new Exception('更新账单['.$data_bill['ob_no'].']失败');
  322. }
  323. // 发送店铺消息
  324. $param = array();
  325. $param['code'] = 'store_bill_affirm';
  326. $param['store_id'] = $store_info['store_id'];
  327. $param['param'] = array(
  328. 'state_time' => date('Y-m-d H:i:s', $data_bill['ob_start_date']),
  329. 'end_time' => date('Y-m-d H:i:s', $data_bill['ob_end_date']),
  330. 'bill_no' => $data_bill['ob_no']
  331. );
  332. QueueClient::push('sendStoreMsg', $param);
  333. }
  334. }
  335. }
  336. }
  337. }
  338. /**
  339. * 计算某月内,某店铺的销量,佣金
  340. *
  341. * @param array $data_bill
  342. */
  343. private function _calc_vr_order_bill($data_bill){
  344. $model_order = Model('vr_order');
  345. $model_bill = Model('vr_bill');
  346. $model_store = Model('store');
  347. //计算已使用兑换码
  348. $order_condition = array();
  349. $order_condition['vr_state'] = 1;
  350. $order_condition['store_id'] = $data_bill['ob_store_id'];
  351. $order_condition['vr_usetime'] = array('between',"{$data_bill['ob_start_date']},{$data_bill['ob_end_date']}");
  352. $update = array();
  353. //订单金额
  354. $fields = 'sum(pay_price) as order_amount,SUM(ROUND(pay_price*commis_rate/100,2)) as commis_amount';
  355. $order_info = $model_order->getOrderCodeInfo($order_condition, $fields);
  356. $update['ob_order_totals'] = floatval($order_info['order_amount']);
  357. //佣金金额
  358. $update['ob_commis_totals'] = $order_info['commis_amount'];
  359. //计算已过期不退款兑换码
  360. $order_condition = array();
  361. $order_condition['vr_state'] = 0;
  362. $order_condition['store_id'] = $data_bill['ob_store_id'];
  363. $order_condition['vr_invalid_refund'] = 0;
  364. $order_condition['vr_indate'] = array('between',"{$data_bill['ob_start_date']},{$data_bill['ob_end_date']}");
  365. //订单金额
  366. $fields = 'sum(pay_price) as order_amount,SUM(ROUND(pay_price*commis_rate/100,2)) as commis_amount';
  367. $order_info = $model_order->getOrderCodeInfo($order_condition, $fields);
  368. $update['ob_order_totals'] += floatval($order_info['order_amount']);
  369. //佣金金额
  370. $update['ob_commis_totals'] += $order_info['commis_amount'];
  371. //店铺名
  372. $store_info = $model_store->getStoreInfoByID($data_bill['ob_store_id']);
  373. $update['ob_store_name'] = $store_info['store_name'];
  374. //本期应结
  375. $update['ob_result_totals'] = $update['ob_order_totals'] - $update['ob_commis_totals'];
  376. $update['ob_create_date'] = time();
  377. $update['ob_state'] = 1;
  378. return $model_bill->editOrderBill($update,array('ob_no'=>$data_bill['ob_no']));
  379. }
  380. }