Set.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657
  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. '礼品卡抵扣金额',
  358. );
  359. if ($type == 'store') {
  360. $head[6] = '商品数量';
  361. }
  362. $head[] = '结算状态';
  363. if ($data) {
  364. $body = array();
  365. foreach ($data as $k => $v) {
  366. $cdate = date('Y-m-d H:i', $v['cdate']);
  367. $fdate = $v['fdate'] ? date('Y-m-d H:i', $v['fdate']) : '';
  368. $operdate = $v['operdate'] ? date('Y-m-d H:i', $v['operdate']) : '';
  369. if ($type == 'factory') {
  370. $cash = '¥' . round($v['p_cash'], 2);
  371. } else {
  372. $cash = '¥' . round($v['cash'], 2);
  373. }
  374. $card_code_card = '';
  375. $card_code_cash = '';
  376. if ($v['pay_type'] == 2) {
  377. $sell_order = Dever::db('shop/sell_order')->find($v['source_order_id']);
  378. if ($sell_order) {
  379. $card_code_card = $sell_order['card_code_card'];
  380. $card_code_cash = $sell_order['card_code_cash'];
  381. }
  382. }
  383. $jstype = Dever::db('cash/order')->config['config_jstype'][$v['jstype']];
  384. $body[$k][0] = $v['order_num'];
  385. $body[$k][1] = $v['source_order_num'];
  386. $body[$k][2] = $jstype;
  387. $body[$k][3] = $cdate;
  388. $body[$k][4] = $operdate;
  389. $body[$k][5] = $fdate;
  390. $body[$k][6] = $cash;
  391. if ($type == 'store') {
  392. $body[$k][6] = $v['num'];
  393. }
  394. $body[$k][7] = $card_code_card;
  395. $body[$k][8] = $card_code_cash;
  396. $body[$k][] = '已入账';
  397. }
  398. }
  399. Dever::excelExport($body, $header, $file);
  400. }
  401. # 打印订单单
  402. public function printer($user)
  403. {
  404. $id = Dever::input('id');
  405. if (!$id) {
  406. return false;
  407. }
  408. $type = Dever::input('type', 'shop');
  409. $table = 'cash/' . $type;
  410. $pay_type = 1;
  411. if ($type == 'shop') {
  412. $name = '门店';
  413. } elseif ($type == 'shop_sell') {
  414. $pay_type = '2,3';
  415. $name = '门店';
  416. $type = 'shop';
  417. } elseif ($type == 'store') {
  418. $name = '仓库';
  419. } elseif ($type == 'factory') {
  420. $name = '工厂';
  421. }
  422. $config = Dever::db($table)->config;
  423. $info = Dever::db($table)->one($id);
  424. $status = $config['config_status'][$info['status']];
  425. $other_status = $config['config_status'][$info[$type . '_status']];
  426. $other = Dever::db($type . '/info')->find($info[$type . '_id']);
  427. $member = Dever::db($type . '/member')->find($user['id']);
  428. $factory_config = Dever::db('main/factory_config')->find();
  429. $main_config = Dever::db('main/config')->find();
  430. $pdf = Dever::load('pdf/lib/base')->init();
  431. $pdf->hr('-', $other['name']);
  432. $pdf->br()->font(20)->center('对账周期:' . $this->statTime($info['start'], $info['end']));
  433. $pdf->font(10);
  434. $pdf->br();
  435. $pdf->br()->left('对账日期:' . $this->statDate($info['type'], $info['day']), 80)->left('制单人:' . $member['name'], 60)->left('制单时间:' . date('Y-m-d H:i'), 40);
  436. $pdf->hr();
  437. $data = $this->getOrderData($type, $info, 'NoPage', $pay_type);
  438. $body = array();
  439. $body_total = array();
  440. $body_total['cash'] = 0;
  441. $body_total['num'] = 0;
  442. if ($data) {
  443. foreach ($data as $k => $v) {
  444. $cdate = date('Y-m-d H:i', $v['cdate']);
  445. $fdate = $v['fdate'] ? date('Y-m-d H:i', $v['fdate']) : '';
  446. $operdate = $v['operdate'] ? date('Y-m-d H:i', $v['operdate']) : '';
  447. $prefix = '¥';
  448. if ($type == 'store') {
  449. $cash = $v['num'];
  450. $prefix = '';
  451. } elseif ($type == 'factory') {
  452. $cash = $v['p_cash'];
  453. } else {
  454. $cash = $v['cash'];
  455. }
  456. $cash = round($cash, 2);
  457. $body[] = array
  458. (
  459. $v['source_order_num'],
  460. $cdate,
  461. $fdate,
  462. $prefix . $cash,
  463. '已入账'
  464. );
  465. $body_total['cash'] += $cash;
  466. $body_total['num'] += 1;
  467. }
  468. }
  469. $head = array(array('订单号', 60), array('下单日期', 43), array('完成日期', 43), array('对账金额', 30), array('状态', 20));
  470. if ($type == 'store') {
  471. $head[3][0] = '对账数量';
  472. }
  473. if ($body) {
  474. $pdf->br();
  475. foreach ($head as $k => $v) {
  476. $pdf->left($v[0], $v[1]);
  477. }
  478. foreach ($body as $k => $v) {
  479. $pdf->br();
  480. foreach ($head as $k1 => $v1) {
  481. $pdf->left($v[$k1], $v1[1]);
  482. }
  483. }
  484. $pdf->br();
  485. if ($type == 'store') {
  486. $pdf->right('共'.$body_total['num'].'个订单,合计对账数量' . $body_total['cash'] . '个');
  487. } else {
  488. $pdf->right('共'.$body_total['num'].'个订单,合计对账金额¥' . $body_total['cash'] . '元');
  489. }
  490. $pdf->hr();
  491. }
  492. $pdf->br(1);
  493. //$pdf->SetY(-100);
  494. $pdf->right('如遇任何问题请致电客服');
  495. $pdf->br();
  496. $pdf->font(20);
  497. $pdf->left($main_config['name'], 160);
  498. $pdf->font(10);
  499. $pdf->right('电话:' . $factory_config['phone'], 30);
  500. $pdf->br();
  501. $pdf->left($main_config['site'], 160);
  502. $pdf->font(10);
  503. $pdf->right($main_config['worktime'], 30);
  504. $pdf->out('对账单');
  505. }
  506. # 获取对账单数据
  507. public function getOrderData($type, $info, $page = 'NoPage', $pay_type = 1)
  508. {
  509. $id = $info[$type . '_id'];;
  510. if ($type == 'shop' || $type == 'shop_sell') {
  511. $where['type'] = 1;
  512. $where['type_id'] = $id;
  513. $where['pay_type'] = $pay_type;
  514. $m = 'getAll';
  515. } elseif ($type == 'factory') {
  516. $where['source_type'] = 3;
  517. $where['source_id'] = $id;
  518. $m = 'getAll';
  519. } else {
  520. $where['type'] = 2;
  521. $where['type_id'] = $id;
  522. $where['source_type'] = 2;
  523. $where['source_id'] = $id;
  524. $m = 'getAllByStore';
  525. }
  526. $m .= $page;
  527. $where['status'] = 2;
  528. $where['start'] = $info['start'];
  529. $where['end'] = $info['end'];
  530. return Dever::db('cash/order')->$m($where);
  531. }
  532. }