Set.php 22 KB

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