Set.php 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598
  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 />对账金额¥' . $info['cash'] . '元';
  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($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'], 'cache' => false));
  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. $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');
  208. $v['source_order_num'] = '<a href="'.$url.'" style="color:blue">'.$v['source_order_num'].'</a>';
  209. $head = array('结算单号', '订货单号', '结算类型', '下单日期', '完成日期', '结算日期', '对账金额');
  210. if ($type == 'store') {
  211. $head[6] = '商品数量';
  212. }
  213. $head[] = '结算状态';
  214. $d = array
  215. (
  216. $v['order_num'],
  217. $v['source_order_num'],
  218. $config['config_jstype'][$v['jstype']],
  219. $cdate,
  220. $fdate,
  221. $operdate,
  222. $cash,
  223. );
  224. if ($type == 'store') {
  225. $d[6] = $v['num'];
  226. }
  227. $d[] = '已入账';
  228. $body[] = $d;
  229. } else {
  230. $head = array('订货单号', '下单日期', '完成日期', '对账金额');
  231. if ($type == 'store') {
  232. $head[3] = '商品数量';
  233. }
  234. $head[] = '状态';
  235. $d = array
  236. (
  237. $v['source_order_num'],
  238. $cdate,
  239. $fdate,
  240. $cash,
  241. );
  242. if ($type == 'store') {
  243. $d[3] = $v['num'];
  244. }
  245. $d[] = '已入账';
  246. $body[] = $d;
  247. }
  248. }
  249. $page = Dever::page("current");
  250. $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>';
  251. }
  252. $html .= '</div>';
  253. return '<div class="layui-card-body">' . $html . '</div>';
  254. }
  255. private function table($head, $data)
  256. {
  257. $html = '';
  258. if ($head) {
  259. $html = '<table class="layui-table">';
  260. $html .= '<thead><tr>';
  261. foreach ($head as $k => $v) {
  262. $html .= '<th>'.$v.'</th>';
  263. }
  264. $html .= '</tr></thead>';
  265. $html .= '<tbody>';
  266. foreach ($data as $k => $v) {
  267. $html .= '<tr>';
  268. foreach ($v as $k1 => $v1) {
  269. $html .= '<td>'.$v1.'</td>';
  270. }
  271. $html .= '</tr>';
  272. }
  273. $html .= '</tbody>';
  274. $html .= '</table>';
  275. } else {
  276. foreach ($data as $k => $v) {
  277. $html .= '';
  278. foreach ($v as $k1 => $v1) {
  279. $html .= $v1 . '&nbsp;&nbsp;&nbsp;&nbsp;';
  280. }
  281. $html .= '';
  282. }
  283. }
  284. return $html;
  285. }
  286. # 导出对账单
  287. public function excel_api()
  288. {
  289. $type = Dever::input('type');
  290. $id = Dever::input('id');
  291. $table = 'cash/' . $type;
  292. $config = Dever::db($table)->config;
  293. $info = Dever::db($table)->one($id);
  294. $data = $this->getOrderData($type, $info);
  295. $stat_type = Dever::db('cash/shop')->config['config_type'];
  296. $other = Dever::db($type . '/info')->find($id);
  297. $status = $config['config_status'][$info['status']];
  298. $other_status = $config['config_status'][$info[$type . '_status']];
  299. $info['name'] = Dever::load('cash/lib/set')->statDate($stat_type, $info['day']);
  300. $info['status_name'] = $status;
  301. $file = $other['name'] . '的' . $info['name'] . '对账单';
  302. $header = $body = array();
  303. $header = array
  304. (
  305. 'top' => array
  306. (
  307. $file . ' ' . $info['status_name'] . ' 对账金额¥' . $info['cash']. ' 商品数量¥' . $info['num'],
  308. ),
  309. '结算单号',
  310. '订货单号',
  311. '结算类型',
  312. '下单日期',
  313. '完成日期',
  314. '结算日期',
  315. '对账金额',
  316. );
  317. if ($type == 'store') {
  318. $head[6] = '商品数量';
  319. }
  320. $head[] = '结算状态';
  321. if ($data) {
  322. $body = array();
  323. foreach ($data as $k => $v) {
  324. $cdate = date('Y-m-d H:i', $v['cdate']);
  325. $fdate = $v['fdate'] ? date('Y-m-d H:i', $v['fdate']) : '';
  326. $operdate = $v['operdate'] ? date('Y-m-d H:i', $v['operdate']) : '';
  327. if ($type == 'factory') {
  328. $cash = '¥' . round($v['p_cash'], 2);
  329. } else {
  330. $cash = '¥' . round($v['cash'], 2);
  331. }
  332. $jstype = Dever::db('cash/order')->config['config_jstype'][$v['jstype']];
  333. $body[$k][0] = $v['order_num'];
  334. $body[$k][1] = $v['source_order_num'];
  335. $body[$k][2] = $jstype;
  336. $body[$k][3] = $cdate;
  337. $body[$k][4] = $operdate;
  338. $body[$k][5] = $fdate;
  339. $body[$k][6] = $cash;
  340. if ($type == 'store') {
  341. $body[$k][6] = $v['num'];
  342. }
  343. $body[$k][] = '已入账';
  344. }
  345. }
  346. Dever::excelExport($body, $header, $file);
  347. }
  348. # 打印订单单
  349. public function printer($user)
  350. {
  351. $id = Dever::input('id');
  352. if (!$id) {
  353. return false;
  354. }
  355. $type = Dever::input('type', 'shop');
  356. if ($type == 'shop') {
  357. $name = '门店';
  358. } elseif ($type == 'store') {
  359. $name = '仓库';
  360. } elseif ($type == 'factory') {
  361. $name = '工厂';
  362. }
  363. $table = 'cash/' . $type;
  364. $config = Dever::db($table)->config;
  365. $info = Dever::db($table)->one($id);
  366. $status = $config['config_status'][$info['status']];
  367. $other_status = $config['config_status'][$info[$type . '_status']];
  368. $other = Dever::db($type . '/info')->find($info[$type . '_id']);
  369. $member = Dever::db($type . '/member')->find($user['id']);
  370. $factory_config = Dever::db('main/factory_config')->find();
  371. $main_config = Dever::db('main/config')->find();
  372. $pdf = Dever::load('pdf/lib/base')->init();
  373. $pdf->hr('-', $other['name']);
  374. $pdf->br()->font(20)->center('对账周期:' . $this->statTime($info['start'], $info['end']));
  375. $pdf->font(10);
  376. $pdf->br();
  377. $pdf->br()->left('对账日期:' . $this->statDate($info['type'], $info['day']), 80)->left('制单人:' . $member['name'], 60)->left('制单时间:' . date('Y-m-d H:i'), 40);
  378. $pdf->hr();
  379. $data = $this->getOrderData($type, $info);
  380. $body = array();
  381. $body_total = array();
  382. $body_total['cash'] = 0;
  383. $body_total['num'] = 0;
  384. if ($data) {
  385. foreach ($data as $k => $v) {
  386. $cdate = date('Y-m-d H:i', $v['cdate']);
  387. $fdate = $v['fdate'] ? date('Y-m-d H:i', $v['fdate']) : '';
  388. $operdate = $v['operdate'] ? date('Y-m-d H:i', $v['operdate']) : '';
  389. $prefix = '¥';
  390. if ($type == 'store') {
  391. $cash = $v['num'];
  392. $prefix = '';
  393. } elseif ($type == 'factory') {
  394. $cash = $v['p_cash'];
  395. } else {
  396. $cash = $v['cash'];
  397. }
  398. $cash = round($cash, 2);
  399. $body[] = array
  400. (
  401. $v['source_order_num'],
  402. $cdate,
  403. $fdate,
  404. $prefix . $cash,
  405. '已入账'
  406. );
  407. $body_total['cash'] += $cash;
  408. $body_total['num'] += 1;
  409. }
  410. }
  411. $head = array(array('订单号', 60), array('下单日期', 43), array('完成日期', 43), array('对账金额', 30), array('状态', 20));
  412. if ($type == 'store') {
  413. $head[3][0] = '对账数量';
  414. }
  415. if ($body) {
  416. $pdf->br();
  417. foreach ($head as $k => $v) {
  418. $pdf->left($v[0], $v[1]);
  419. }
  420. foreach ($body as $k => $v) {
  421. $pdf->br();
  422. foreach ($head as $k1 => $v1) {
  423. $pdf->left($v[$k1], $v1[1]);
  424. }
  425. }
  426. $pdf->br();
  427. if ($type == 'store') {
  428. $pdf->right('共'.$body_total['num'].'个订单,合计对账数量' . $body_total['cash'] . '个');
  429. } else {
  430. $pdf->right('共'.$body_total['num'].'个订单,合计对账金额¥' . $body_total['cash'] . '元');
  431. }
  432. $pdf->hr();
  433. }
  434. $pdf->br(1);
  435. //$pdf->SetY(-100);
  436. $pdf->right('如遇任何问题请致电客服');
  437. $pdf->br();
  438. $pdf->font(20);
  439. $pdf->left($main_config['name'], 160);
  440. $pdf->font(10);
  441. $pdf->right('电话:' . $factory_config['phone'], 30);
  442. $pdf->br();
  443. $pdf->left($main_config['site'], 160);
  444. $pdf->font(10);
  445. $pdf->right($main_config['worktime'], 30);
  446. $pdf->out('对账单');
  447. }
  448. # 获取对账单数据
  449. public function getOrderData($type, $info, $page = 'NoPage')
  450. {
  451. $id = $info[$type . '_id'];;
  452. if ($type == 'shop') {
  453. $where['type'] = 1;
  454. $where['type_id'] = $id;
  455. $m = 'getAll';
  456. } elseif ($type == 'factory') {
  457. $where['source_type'] = 3;
  458. $where['source_id'] = $id;
  459. $m = 'getAll';
  460. } else {
  461. $where['type'] = 2;
  462. $where['type_id'] = $id;
  463. $where['source_type'] = 2;
  464. $where['source_id'] = $id;
  465. $m = 'getAllByStore';
  466. }
  467. $m .= $page;
  468. $where['status'] = 2;
  469. $where['start'] = $info['start'];
  470. $where['end'] = $info['end'];
  471. return Dever::db('cash/order')->$m($where);
  472. }
  473. }