Set.php 20 KB

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