month.php 20 KB

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