Set.php 20 KB

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