Set.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658
  1. <?php
  2. namespace Cash\Lib;
  3. use Dever;
  4. class Set
  5. {
  6. public function getCash($cash)
  7. {
  8. if (!$cash || $cash <= 0) {
  9. return 0;
  10. }
  11. return round($cash, 2);
  12. }
  13. public function info($audit_type, $audit)
  14. {
  15. $config = Dever::db('cash/order')->config;
  16. return $config['config_audit_type'][$audit_type] . $config['config_audit'][$audit];
  17. }
  18. public function statDate($type, $day)
  19. {
  20. if ($type == 1) {
  21. $string = 'Y年m月';
  22. } elseif ($type == 2) {
  23. $string = 'Y年W周';
  24. } else {
  25. $string = 'Y年m月d日';
  26. }
  27. return date($string, $day);
  28. }
  29. public function statYes($info, $type, $source = false)
  30. {
  31. $table = 'cash/' . $type;
  32. if ($type == 'shop_sell') {
  33. $type = 'shop';
  34. }
  35. $info = is_array($info) ? $info : Dever::db($table)->one($info);
  36. $other = Dever::db($type . '/info')->find($info[$type . '_id']);
  37. if (!$other) {
  38. return '';
  39. }
  40. if ($type == 'store') {
  41. return $this->statDate($info['type'], $info['day']) . '与'.$other['name'].'对账单数量是否正确<br />对账数量' . $info['num'] . '个';
  42. }
  43. return $this->statDate($info['type'], $info['day']) . '与'.$other['name'].'对账单款项是否正确<br />对账金额¥' . round($info['cash'],2) . '元';
  44. }
  45. public function statTime($start, $end)
  46. {
  47. return date('Y-m-d', $start) . ' ~ ' . date('Y-m-d', $end);
  48. }
  49. public function orderUpdate_commit($id, $name, $data)
  50. {
  51. Dever::config('base')->hook = true;
  52. $update = array();
  53. $audit = Dever::param('audit', $data);
  54. $info = Dever::db('cash/order')->one($id);
  55. if ($audit > 1 && $info['refund_id'] && $info['refund_id'] > 0) {
  56. Dever::load('shop/lib/refund')->set('buy')->action($info['refund_id'], $audit, false, false);
  57. if ($audit == 2) {
  58. $state = Dever::db('cash/order')->update(array('where_id' => $id, 'status' => 2, 'operdate' => time(), 'fdate' => time()));
  59. $order = Dever::db('shop/buy_order')->find(array('id' => $info['source_order_id'], 'clear' => true));
  60. if ($state && $info['jstype'] == 2 && isset($order['status']) && $order['status'] == 8) {
  61. # 如果是退款结算,同时要把主订单也结算
  62. $where = array();
  63. $where['source_order_id'] = $order['id'];
  64. $where['jstype'] = 1;
  65. $where['audit'] = 1;
  66. $info = Dever::db('cash/order')->find($where);
  67. if ($info) {
  68. $set['status'] = 2;
  69. $set['audit'] = 2;
  70. $set['where_id'] = $info['id'];
  71. $set['operdate'] = time();
  72. $set['fdate'] = time();
  73. Dever::db('cash/order')->update($set);
  74. }
  75. }
  76. }
  77. }
  78. }
  79. # 审核对账
  80. public function audit_api()
  81. {
  82. $id = Dever::input('id');
  83. $type = Dever::input('type', 'shop');
  84. $table = 'cash/' . $type;
  85. $data = Dever::db($table)->one($id);
  86. if ($data) {
  87. Dever::db($table)->update(array('where_id' => $id, 'status' => 2));
  88. if ($type == 'shop_sell' && $data['cash'] > 0) {
  89. return 'reload';
  90. $card = Dever::db('cash/order')->getCardAll(array('start'=>$data['start'],'end'=>$data['end'],'pay_type'=>2,'status'=>2,'type'=>1,'type_id'=>$data['shop_id'],'state'=>1));
  91. $shop = Dever::db('shop/info')->find($data['shop_id']);
  92. if ($card && $card['cash'] > 0) {
  93. $cash = sprintf("%01.2f", $card['cash']);
  94. $yue = $shop['price'] + $cash;
  95. $explan = date('Y年m月d日',$data['end']).'礼品卡对账金额';
  96. Dever::load('shop/lib/record')->pingtai($data['shop_id'],$shop['sid'],8,$data['shop_id'],$cash,$yue,$explan);
  97. }
  98. $bao = Dever::db('cash/order')->getCardAll(array('start'=>$data['start'],'end'=>$data['end'],'pay_type'=>3,'status'=>2,'type'=>1,'type_id'=>$data['shop_id'],'state'=>1));
  99. if($bao && $bao['cash']>0){
  100. $shop = Dever::db('shop/info')->find(array('id'=>$shop['id'],'s'=>1));
  101. $cash = sprintf("%01.2f", $bao['cash']);
  102. $yue = $shop['price'] + $cash;
  103. $explan = date('Y年m月d日',$data['end']).'钱包对账金额';
  104. Dever::load('shop/lib/record')->pingtai($data['shop_id'],$shop['sid'],8,$data['shop_id'],$cash,$yue,$explan);
  105. }
  106. }
  107. }
  108. return 'reload';
  109. }
  110. # 审核对账
  111. public function audit_other_api()
  112. {
  113. $id = Dever::input('id');
  114. $type = Dever::input('type', 'shop');
  115. $table = 'cash/' . $type;
  116. $info = Dever::db($table)->one($id);
  117. if ($info) {
  118. Dever::db($table)->update(array('where_id' => $id, $type . '_status' => 2));
  119. }
  120. return 'reload';
  121. }
  122. # 查看对账单详情
  123. public function view_api()
  124. {
  125. $id = Dever::input('id');
  126. if (!$id) {
  127. return false;
  128. }
  129. $show = Dever::input('show', 1);
  130. $type = Dever::input('type', 'shop');
  131. $pay_type = 1;
  132. $table = 'cash/' . $type;
  133. $source_type = $type;
  134. if ($type == 'shop') {
  135. $search_option_type = 1;
  136. $name = '门店';
  137. } if ($type == 'shop_sell') {
  138. $search_option_type = 1;
  139. $pay_type = '2,3';
  140. $name = '门店零售';
  141. $type = 'shop';
  142. } elseif ($type == 'store') {
  143. $search_option_type = 3;
  144. $name = '仓库';
  145. } elseif ($type == 'factory') {
  146. $search_option_type = 2;
  147. $name = '工厂';
  148. }
  149. $config = Dever::db($table)->config;
  150. $info = Dever::db($table)->one($id);
  151. $status = $config['config_status'][$info['status']];
  152. $other_status = $config['config_status'][$info[$type . '_status']];
  153. $other = Dever::db($type . '/info')->find($info[$type . '_id']);
  154. $html = '<div class="layui-col-md12"><div class="layui-card"><div class="layui-card-header">对账单详情</div><div class="layui-card-body">';
  155. $html .= '<table class="layui-table"><tbody>';
  156. $html .= '<tr>
  157. <td width="100">对账'.$name.'</td>
  158. <td>'.$this->table(false, array(array($other['name']))).'</td>
  159. </tr>';
  160. $html .= '<tr>
  161. <td width="80">对账时间</td>
  162. <td>'.$this->table(false, array(array($this->statDate($info['type'], $info['day'])))).'</td>
  163. </tr>';
  164. $html .= '<tr>
  165. <td width="80">对账周期</td>
  166. <td>'.$this->table(false, array(array($this->statTime($info['start'], $info['end'])))).'</td>
  167. </tr>';
  168. if ($type == 'store') {
  169. $html .= '<tr>
  170. <td width="80">商品数量</td>
  171. <td>'.$this->table(false, array(array($info['num']))).'</td>
  172. </tr>';
  173. } else {
  174. $html .= '<tr>
  175. <td width="80">对账金额</td>
  176. <td>'.$this->table(false, array(array('¥' . round($info['cash'], 2)))).'</td>
  177. </tr>';
  178. }
  179. if ($show == 1) {
  180. $html .= '<tr>
  181. <td width="80">'.$name.'对账状态</td>
  182. <td>'.$this->table(false, array(array($other_status))).'</td>
  183. </tr>';
  184. $html .= '<tr>
  185. <td width="100">平台对账状态</td>
  186. <td>'.$this->table(false, array(array($status))).'</td>
  187. </tr>';
  188. } else {
  189. $html .= '<tr>
  190. <td width="80">对账状态</td>
  191. <td>'.$this->table(false, array(array($other_status))).'</td>
  192. </tr>';
  193. }
  194. $button = array();
  195. if ($show == 1) {
  196. if ($info['status'] == 1) {
  197. $url = Dever::url('lib/set.audit&id='.$id.'&type=' . $source_type, 'cash');
  198. $button[] = '<button class="layui-btn layui-btn-primary" onclick="load(\''.$url.'\', \''.$this->statYes($info, $source_type).'\', \'请确认\')">立即确认</button>';
  199. }
  200. $config['phone'] = '联系人:' . $other['truename'] . ',联系电话:' . $other['mobile'];
  201. $button[] = '<button class="layui-btn layui-btn-primary" onclick="showAlert(\''.$config['phone'].'\')">联系'.$name.'</button>';
  202. $start = date('Y-m-d H:i:s', $info['start']);
  203. $end = date('Y-m-d H:i:s', $info['end']);
  204. $out = Dever::url('lib/set.excel?id=' . $info['id'] . '&type=' . $source_type, 'cash');
  205. $button[] = '<a class="layui-btn layui-btn-primary" href="'.$out.'">导出对账单</a>';
  206. } elseif ($show == 2) {
  207. if ($info[$type . '_status'] == 1 && $show != 1) {
  208. $url = Dever::url('lib/set.audit_other&id='.$id.'&type=' . $source_type, 'cash');
  209. $button[] = '<button class="layui-btn layui-btn-primary" onclick="load(\''.$url.'\', \''.$this->statYes($info, $source_type).'\', \'请确认\')">立即确认</button>';
  210. }
  211. $config = Dever::load('factory/admin/auth.config');
  212. $config['phone'] = '联系电话:' . $config['dz_phone'];
  213. $button[] = '<button class="layui-btn layui-btn-primary" onclick="showAlert(\''.$config['phone'].'\')">联系对账专员</button>';
  214. $print = Dever::url('admin/stat.print?id=' . $info['id'] . '&type=' . $source_type, $type);
  215. $button[] = '<a class="layui-btn layui-btn-primary" href="'.$print.'" target="_blank">打印对账单</a>';
  216. }
  217. $html .= '<tr>
  218. <td>功能按钮</td>
  219. <td>'.$this->table(false, array($button)).'</td>
  220. </tr>';
  221. $html .= '</tbody></table></div></div>';
  222. $data = $this->getOrderData($type, $info, '', $pay_type);
  223. if ($data) {
  224. $body = array();
  225. $config = Dever::db('cash/order')->config;
  226. foreach ($data as $k => $v) {
  227. $cdate = date('Y-m-d H:i', $v['cdate']);
  228. $fdate = $v['fdate'] ? date('Y-m-d H:i', $v['fdate']) : '';
  229. $operdate = $v['operdate'] ? date('Y-m-d H:i', $v['operdate']) : '';
  230. $noprice = 2;
  231. if ($type == 'factory') {
  232. $set_type = 2;
  233. $cash = '¥' . round($v['p_cash'], 2);
  234. } else {
  235. $cash = '¥' . round($v['cash'], 2);
  236. $set_type = 1;
  237. }
  238. if ($show == 1) {
  239. if ($v['source_type'] == 4) {
  240. $url = Dever::url('project/database/list?project=shop&table=sell_order_goods&order_id='.$v['source_order_id'].'&page_type=1&noprice=' . $noprice . '&type=' . $set_type, 'manage');
  241. } else {
  242. $url = Dever::url('project/database/list?project=shop&table=buy_order_goods&order_id='.$v['source_order_id'].'&page_type=1&noprice=' . $noprice . '&type=' . $set_type, 'manage');
  243. }
  244. $v['source_order_num'] = '<a href="'.$url.'" style="color:blue">'.$v['source_order_num'].'</a>';
  245. $head = array('结算单号', '订货单号', '结算类型', '下单日期', '完成日期', '结算日期', '对账金额');
  246. if ($type == 'store') {
  247. $head[6] = '商品数量';
  248. }
  249. $head[] = '结算状态';
  250. $d = array
  251. (
  252. $v['order_num'],
  253. $v['source_order_num'],
  254. $config['config_jstype'][$v['jstype']],
  255. $cdate,
  256. $fdate,
  257. $operdate,
  258. $cash,
  259. );
  260. if ($type == 'store') {
  261. $d[6] = $v['num'];
  262. }
  263. $d[] = '已入账';
  264. $body[] = $d;
  265. } else {
  266. $head = array('订货单号', '下单日期', '完成日期', '对账金额');
  267. if ($type == 'store') {
  268. $head[3] = '商品数量';
  269. }
  270. $head[] = '状态';
  271. $d = array
  272. (
  273. $v['source_order_num'],
  274. $cdate,
  275. $fdate,
  276. $cash,
  277. );
  278. if ($type == 'store') {
  279. $d[3] = $v['num'];
  280. }
  281. $d[] = '已入账';
  282. $body[] = $d;
  283. }
  284. }
  285. $page = Dever::page("current");
  286. $html .= '<div class="layui-card"><div class="layui-card-header">对账清单</div><div class="layui-card-body" style="max-heights: 500px;overflow: auto;">' . $this->table($head, $body) . $page . '</div></div>';
  287. }
  288. $html .= '</div>';
  289. return '<div class="layui-card-body">' . $html . '</div>';
  290. }
  291. private function table($head, $data)
  292. {
  293. $html = '';
  294. if ($head) {
  295. $html = '<table class="layui-table">';
  296. $html .= '<thead><tr>';
  297. foreach ($head as $k => $v) {
  298. $html .= '<th>'.$v.'</th>';
  299. }
  300. $html .= '</tr></thead>';
  301. $html .= '<tbody>';
  302. foreach ($data as $k => $v) {
  303. $html .= '<tr>';
  304. foreach ($v as $k1 => $v1) {
  305. $html .= '<td>'.$v1.'</td>';
  306. }
  307. $html .= '</tr>';
  308. }
  309. $html .= '</tbody>';
  310. $html .= '</table>';
  311. } else {
  312. foreach ($data as $k => $v) {
  313. $html .= '';
  314. foreach ($v as $k1 => $v1) {
  315. $html .= $v1 . '&nbsp;&nbsp;&nbsp;&nbsp;';
  316. }
  317. $html .= '';
  318. }
  319. }
  320. return $html;
  321. }
  322. # 导出对账单
  323. public function excel_api()
  324. {
  325. $type = Dever::input('type');
  326. $id = Dever::input('id');
  327. $table = 'cash/' . $type;
  328. $pay_type = 1;
  329. if ($type == 'shop_sell') {
  330. $pay_type = '2,3';
  331. $type = 'shop';
  332. }
  333. $config = Dever::db($table)->config;
  334. $info = Dever::db($table)->one($id);
  335. $data = $this->getOrderData($type, $info, 'NoPage', $pay_type);
  336. $stat_type = Dever::db('cash/shop')->config['config_type'];
  337. $other = Dever::db($type . '/info')->find($info[$type . '_id']);
  338. $status = $config['config_status'][$info['status']];
  339. $other_status = $config['config_status'][$info[$type . '_status']];
  340. $info['name'] = Dever::load('cash/lib/set')->statDate($stat_type, $info['day']);
  341. $info['status_name'] = $status;
  342. $file = $other['name'] . '的' . $info['name'] . '对账单';
  343. $header = $body = array();
  344. $header = array
  345. (
  346. 'top' => array
  347. (
  348. $file . ' ' . $info['status_name'] . ' 对账金额¥' . $info['cash']. ' 商品数量¥' . $info['num'],
  349. ),
  350. '结算单号',
  351. '订货单号',
  352. '结算类型',
  353. '下单日期',
  354. '完成日期',
  355. '结算日期',
  356. '对账金额',
  357. '礼品卡卡号',
  358. '礼品卡抵扣金额',
  359. );
  360. if ($type == 'store') {
  361. $head[6] = '商品数量';
  362. }
  363. $head[] = '结算状态';
  364. if ($data) {
  365. $body = array();
  366. foreach ($data as $k => $v) {
  367. $cdate = date('Y-m-d H:i', $v['cdate']);
  368. $fdate = $v['fdate'] ? date('Y-m-d H:i', $v['fdate']) : '';
  369. $operdate = $v['operdate'] ? date('Y-m-d H:i', $v['operdate']) : '';
  370. if ($type == 'factory') {
  371. $cash = '¥' . round($v['p_cash'], 2);
  372. } else {
  373. $cash = '¥' . round($v['cash'], 2);
  374. }
  375. $card_code_card = '';
  376. $card_code_cash = '';
  377. if ($v['pay_type'] == 2) {
  378. $sell_order = Dever::db('shop/sell_order')->find($v['source_order_id']);
  379. if ($sell_order) {
  380. $card_code_card = $sell_order['card_code_card'];
  381. $card_code_cash = $sell_order['card_code_cash'];
  382. }
  383. }
  384. $jstype = Dever::db('cash/order')->config['config_jstype'][$v['jstype']];
  385. $body[$k][0] = $v['order_num'];
  386. $body[$k][1] = $v['source_order_num'];
  387. $body[$k][2] = $jstype;
  388. $body[$k][3] = $cdate;
  389. $body[$k][4] = $operdate;
  390. $body[$k][5] = $fdate;
  391. $body[$k][6] = $cash;
  392. if ($type == 'store') {
  393. $body[$k][6] = $v['num'];
  394. }
  395. $body[$k][7] = $card_code_card;
  396. $body[$k][8] = $card_code_cash;
  397. $body[$k][] = '已入账';
  398. }
  399. }
  400. Dever::excelExport($body, $header, $file);
  401. }
  402. # 打印订单单
  403. public function printer($user)
  404. {
  405. $id = Dever::input('id');
  406. if (!$id) {
  407. return false;
  408. }
  409. $type = Dever::input('type', 'shop');
  410. $table = 'cash/' . $type;
  411. $pay_type = 1;
  412. if ($type == 'shop') {
  413. $name = '门店';
  414. } elseif ($type == 'shop_sell') {
  415. $pay_type = '2,3';
  416. $name = '门店';
  417. $type = 'shop';
  418. } elseif ($type == 'store') {
  419. $name = '仓库';
  420. } elseif ($type == 'factory') {
  421. $name = '工厂';
  422. }
  423. $config = Dever::db($table)->config;
  424. $info = Dever::db($table)->one($id);
  425. $status = $config['config_status'][$info['status']];
  426. $other_status = $config['config_status'][$info[$type . '_status']];
  427. $other = Dever::db($type . '/info')->find($info[$type . '_id']);
  428. $member = Dever::db($type . '/member')->find($user['id']);
  429. $factory_config = Dever::db('main/factory_config')->find();
  430. $main_config = Dever::db('main/config')->find();
  431. $pdf = Dever::load('pdf/lib/base')->init();
  432. $pdf->hr('-', $other['name']);
  433. $pdf->br()->font(20)->center('对账周期:' . $this->statTime($info['start'], $info['end']));
  434. $pdf->font(10);
  435. $pdf->br();
  436. $pdf->br()->left('对账日期:' . $this->statDate($info['type'], $info['day']), 80)->left('制单人:' . $member['name'], 60)->left('制单时间:' . date('Y-m-d H:i'), 40);
  437. $pdf->hr();
  438. $data = $this->getOrderData($type, $info, 'NoPage', $pay_type);
  439. $body = array();
  440. $body_total = array();
  441. $body_total['cash'] = 0;
  442. $body_total['num'] = 0;
  443. if ($data) {
  444. foreach ($data as $k => $v) {
  445. $cdate = date('Y-m-d H:i', $v['cdate']);
  446. $fdate = $v['fdate'] ? date('Y-m-d H:i', $v['fdate']) : '';
  447. $operdate = $v['operdate'] ? date('Y-m-d H:i', $v['operdate']) : '';
  448. $prefix = '¥';
  449. if ($type == 'store') {
  450. $cash = $v['num'];
  451. $prefix = '';
  452. } elseif ($type == 'factory') {
  453. $cash = $v['p_cash'];
  454. } else {
  455. $cash = $v['cash'];
  456. }
  457. $cash = round($cash, 2);
  458. $body[] = array
  459. (
  460. $v['source_order_num'],
  461. $cdate,
  462. $fdate,
  463. $prefix . $cash,
  464. '已入账'
  465. );
  466. $body_total['cash'] += $cash;
  467. $body_total['num'] += 1;
  468. }
  469. }
  470. $head = array(array('订单号', 60), array('下单日期', 43), array('完成日期', 43), array('对账金额', 30), array('状态', 20));
  471. if ($type == 'store') {
  472. $head[3][0] = '对账数量';
  473. }
  474. if ($body) {
  475. $pdf->br();
  476. foreach ($head as $k => $v) {
  477. $pdf->left($v[0], $v[1]);
  478. }
  479. foreach ($body as $k => $v) {
  480. $pdf->br();
  481. foreach ($head as $k1 => $v1) {
  482. $pdf->left($v[$k1], $v1[1]);
  483. }
  484. }
  485. $pdf->br();
  486. if ($type == 'store') {
  487. $pdf->right('共'.$body_total['num'].'个订单,合计对账数量' . $body_total['cash'] . '个');
  488. } else {
  489. $pdf->right('共'.$body_total['num'].'个订单,合计对账金额¥' . $body_total['cash'] . '元');
  490. }
  491. $pdf->hr();
  492. }
  493. $pdf->br(1);
  494. //$pdf->SetY(-100);
  495. $pdf->right('如遇任何问题请致电客服');
  496. $pdf->br();
  497. $pdf->font(20);
  498. $pdf->left($main_config['name'], 160);
  499. $pdf->font(10);
  500. $pdf->right('电话:' . $factory_config['phone'], 30);
  501. $pdf->br();
  502. $pdf->left($main_config['site'], 160);
  503. $pdf->font(10);
  504. $pdf->right($main_config['worktime'], 30);
  505. $pdf->out('对账单');
  506. }
  507. # 获取对账单数据
  508. public function getOrderData($type, $info, $page = 'NoPage', $pay_type = 1)
  509. {
  510. $id = $info[$type . '_id'];;
  511. if ($type == 'shop' || $type == 'shop_sell') {
  512. $where['type'] = 1;
  513. $where['type_id'] = $id;
  514. $where['pay_type'] = $pay_type;
  515. $m = 'getAll';
  516. } elseif ($type == 'factory') {
  517. $where['source_type'] = 3;
  518. $where['source_id'] = $id;
  519. $m = 'getAll';
  520. } else {
  521. $where['type'] = 2;
  522. $where['type_id'] = $id;
  523. $where['source_type'] = 2;
  524. $where['source_id'] = $id;
  525. $m = 'getAllByStore';
  526. }
  527. $m .= $page;
  528. $where['status'] = 2;
  529. $where['start'] = $info['start'];
  530. $where['end'] = $info['end'];
  531. return Dever::db('cash/order')->$m($where);
  532. }
  533. }