Set.php 22 KB

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