Set.php 22 KB

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