Set.php 20 KB

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