Buy.php 62 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763
  1. <?php
  2. # 支付
  3. namespace Mshop\Lib;
  4. use Dever;
  5. class Buy
  6. {
  7. # 1是门店,2是仓库,3是工厂,1,2,3是查看采购人的订单,11,12,13是查看供货商的订单
  8. public $type = 1;
  9. # 1是列表,2是详情
  10. public $view = 1;
  11. # 获取配置
  12. public $config = array();
  13. # table
  14. public $table = 'shop/buy_order';
  15. public function __construct()
  16. {
  17. $this->config = Dever::db($this->table)->config;
  18. }
  19. # 设置订单的类型
  20. public function set($type, $view)
  21. {
  22. $this->type = $type;
  23. $this->view = $view;
  24. return $this;
  25. }
  26. # 获取公共的where
  27. public function where($id)
  28. {
  29. $where = array();
  30. if ($this->type > 10) {
  31. $type = $this->type - 10;
  32. $where['source_type'] = $type;
  33. $where['source_id'] = $id;
  34. } else {
  35. $where['type'] = $this->type;
  36. $where['type_id'] = $id;
  37. }
  38. if (!$where) {
  39. Dever::alert('参数错误');
  40. }
  41. return $where;
  42. }
  43. # 获取订单列表
  44. public function getList($id)
  45. {
  46. $result = array();
  47. $where = $this->where($id);
  48. $status = Dever::input('status');
  49. if ($status) {
  50. if ($status == 'refund') {
  51. $where['refund_status'] = 2;
  52. } else {
  53. $where['status'] = $status;
  54. }
  55. }
  56. $order_num = Dever::input('order_num');
  57. if ($order_num) {
  58. $where['order_num'] = $order_num;
  59. }
  60. $refund_status = Dever::input('refund_status');
  61. if ($refund_status) {
  62. $where['refund_status'] = $refund_status;
  63. }
  64. $result['search_value'] = $where;
  65. $result['search_value']['day'] = $day = Dever::input('day');
  66. if ($day) {
  67. if ($this->type > 10) {
  68. $where['start_o'] = Dever::maketime($day . ' 00:00:00');
  69. $where['end_o'] = Dever::maketime($day . ' 23:59:59');
  70. } elseif ($this->type == 2) {
  71. $where['start_f'] = Dever::maketime($day . ' 00:00:00');
  72. $where['end_f'] = Dever::maketime($day . ' 23:59:59');
  73. } else {
  74. $where['start'] = Dever::maketime($day . ' 00:00:00');
  75. $where['end'] = Dever::maketime($day . ' 23:59:59');
  76. }
  77. }
  78. $result['search_value']['start'] = $start = Dever::input('start');
  79. $result['search_value']['end'] = $end = Dever::input('end');
  80. if ($start && $end) {
  81. if ($this->type > 10) {
  82. $where['start_o'] = Dever::maketime($start);
  83. $where['end_o'] = Dever::maketime($end);
  84. } elseif ($this->type == 2) {
  85. $where['start_f'] = Dever::maketime($start);
  86. $where['end_f'] = Dever::maketime($end);
  87. } else {
  88. $where['start'] = Dever::maketime($start);
  89. $where['end'] = Dever::maketime($end);
  90. }
  91. }
  92. $excel = Dever::input('excel', 1);
  93. if ($excel == 2) {
  94. $result['order'] = Dever::db($this->table)->getData($where);
  95. } else {
  96. $result['order'] = Dever::db($this->table)->getAll($where);
  97. }
  98. if ($result['order']) {
  99. foreach ($result['order'] as $k => $v) {
  100. $result['order'][$k] = $this->getInfo($v);
  101. }
  102. }
  103. $result['search'] = array();
  104. $result['search']['status'] = array
  105. (
  106. array('value' => 1, 'name' => '待支付'),
  107. array('value' => 2, 'name' => '待审核'),
  108. array('value' => 3, 'name' => '待发货'),
  109. array('value' => 4, 'name' => '待收货确认'),
  110. array('value' => 5, 'name' => '已完成'),
  111. array('value' => 6, 'name' => '已完成(有退款)'),
  112. array('value' => '7,8,11', 'name' => '已取消'),
  113. );
  114. $result['config'] = Dever::db('main/manage_config')->find();
  115. return $result;
  116. }
  117. # 查看详情
  118. public function getView($id, $order_id, $show = true)
  119. {
  120. $where = $this->where($id);
  121. $where['id'] = $order_id;
  122. $result = Dever::db($this->table)->find($where);
  123. if (!$result) {
  124. Dever::alert('订单不存在');
  125. }
  126. if ($show) {
  127. $result = $this->getInfo($result, true);
  128. }
  129. $result['config'] = Dever::db('main/manage_config')->find();
  130. return $result;
  131. }
  132. # 查看退款详情
  133. public function getRefund($id, $order_id)
  134. {
  135. $data = $this->getView($id, $order_id, false);
  136. $data['refund'] = Dever::db('shop/buy_order_refund')->select(array('order_id' => $info['id']));
  137. return $data;
  138. }
  139. # 获取订单详细信息
  140. public function getInfo($info, $view = false)
  141. {
  142. if ($info['status'] == 1) {
  143. # 15分钟内支付,900秒
  144. $m = 900;
  145. # 支付倒计时
  146. $info['time'] = time() - $info['cdate'];
  147. if ($info['time'] >= $m) {
  148. # 已过期,自动取消
  149. $info['time'] = -1;
  150. Dever::db($this->table)->update(array('where_id' => $info['id'], 'status' => 11));
  151. $info['status'] = 11;
  152. } else {
  153. $info['time'] = $m - $info['time'];
  154. }
  155. }
  156. $info['status_name'] = $this->config['status'][$info['status']];
  157. $info['goods'] = Dever::db('shop/buy_order_goods')->select(array('order_id' => $info['id']));
  158. if ($info['type'] == 1) {
  159. $info['type_info'] = Dever::db('shop/info')->getOne($info['type_id']);
  160. } elseif ($info['type'] == 2) {
  161. $info['type_info'] = Dever::db('store/info')->find($info['type_id']);
  162. } elseif ($info['type'] == 3) {
  163. $info['type_info'] = Dever::db('factory/info')->find($info['type_id']);
  164. }
  165. if ($info['source_type'] == 1) {
  166. $info['source_info'] = Dever::db('shop/info')->getOne($info['source_id']);
  167. } elseif ($info['source_type'] == 2) {
  168. $info['source_info'] = Dever::db('store/info')->find($info['source_id']);
  169. } elseif ($info['source_type'] == 3) {
  170. $info['source_info'] = Dever::db('factory/info')->find($info['source_id']);
  171. }
  172. $info['cdate'] = date('Y-m-d H:i', $info['cdate']);
  173. if ($info['operdate']) {
  174. $info['operdate'] = date('Y-m-d H:i', $info['operdate']);
  175. } else {
  176. $info['operdate'] = $info['cdate'];
  177. }
  178. $goods_status = Dever::db('shop/buy_order_goods')->config['status'];
  179. $goods_process = Dever::db('shop/buy_order_refund')->config['process'];
  180. if ($view || $this->view == 2) {
  181. foreach ($info['goods'] as $k => $v) {
  182. $info['goods'][$k]['info'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
  183. $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
  184. }
  185. } else {
  186. foreach ($info['goods'] as $k => $v) {
  187. $goods = Dever::db('goods/info')->one($v['goods_id']);
  188. $info['goods'][$k]['name'] = $goods['name'];
  189. $info['goods'][$k]['cover'] = $goods['cover'];
  190. $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
  191. }
  192. }
  193. if (!$info['refund_cash']) {
  194. $info['refund_cash'] = 0;
  195. }
  196. # 配送信息
  197. $info['ps_info'] = Dever::db('shop/buy_order_ps')->find(array('order_id' => $info['id']));
  198. if ($info['ps_info']) {
  199. $info['ps_info']['service_name'] = '商家自送';
  200. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  201. if ($info['ps_info']['ydate']) {
  202. $info['ps_info']['ydate'] = date('Y-m-d H:i', $info['ps_info']['ydate']);
  203. }
  204. $status = Dever::db('shop/buy_order_ps')->config['status'];
  205. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  206. if ($info['ps_info']['service_id'] > 0) {
  207. $service = Dever::db('shop/service')->find($info['ps_info']['service_id']);
  208. $info['ps_info']['service_name'] = $service['name'];
  209. }
  210. }
  211. return $info;
  212. }
  213. # 发起支付
  214. public function action($type, $type_id, $name, $num, $goods, $price, $info, $refer = '')
  215. {
  216. $order_data['type'] = $type;
  217. $order_data['type_id'] = $type_id;
  218. $order_data['name'] = $name;
  219. $order_data['num'] = $num;
  220. $order_data['info'] = $info;
  221. $order_data['price'] = $price;
  222. $order_data['order_num'] = $this->getOrderId();
  223. $order_data['refund_cash'] = 0;
  224. $order_data['refund_p_cash'] = 0;
  225. $id = Dever::db('shop/buy_order')->insert($order_data);
  226. if (!$id) {
  227. Dever::alert('支付失败');
  228. }
  229. foreach($goods as $k => $v) {
  230. $data['order_id'] = $id;
  231. $data['goods_id'] = $v['id'];
  232. $data['sku_id'] = $v['sku_id'];
  233. $data['price'] = $v['price'];
  234. $data['num'] = $v['buy_num'];
  235. Dever::db('shop/buy_order_goods')->insert($data);
  236. }
  237. if ($price <= 0) {
  238. $param['pay_product_id'] = $id;
  239. $param['pay_order_id'] = $order_data['order_num'];
  240. $param['pay_status'] = 2;
  241. $param['pay_cash'] = $price;
  242. $param['pay_msg'] = '';
  243. $this->success($param);
  244. return array
  245. (
  246. 'order' => '',
  247. 'order_num' => $order_data['order_num'],
  248. 'order_id' => $id,
  249. 'type' => 'pay',
  250. );
  251. }
  252. $param = array
  253. (
  254. 'project_id' => 2,
  255. 'channel_id' => 1,
  256. 'system_source' => 5,
  257. 'uid' => -1,
  258. 'name' => $order_data['name'],
  259. 'cash' => $price,
  260. 'product_id' => $id,
  261. 'order_id' => $order_data['order_num'],
  262. 'refer' => $refer,
  263. );
  264. $receipt = Dever::input('receipt');
  265. if ($receipt) {
  266. $param['receipt'] = $receipt;
  267. }
  268. //$result = Dever::load('pay/api.pay', $param);
  269. $result['type'] = 'test';
  270. $result['order'] = '';
  271. $result['order_num'] = $order_data['order_num'];
  272. $result['order_id'] = $id;
  273. return $result;
  274. }
  275. # 再次发起支付
  276. public function raction($id, $refer = '')
  277. {
  278. $order = Dever::db('shop/buy_order')->find($id);
  279. if (!$order) {
  280. Dever::alert('订单信息错误');
  281. }
  282. if ($order['status'] > 1) {
  283. Dever::alert('订单已支付或已取消');
  284. }
  285. $param = array
  286. (
  287. 'project_id' => 2,
  288. 'channel_id' => 1,
  289. 'system_source' => 5,
  290. 'uid' => -1,
  291. 'name' => $order['name'],
  292. 'cash' => $order['price'],
  293. 'product_id' => $id,
  294. 'order_id' => $order['order_num'],
  295. 'refer' => $refer,
  296. );
  297. $receipt = Dever::input('receipt');
  298. if ($receipt) {
  299. $param['receipt'] = $receipt;
  300. }
  301. $result = Dever::load('pay/api.pay', $param);
  302. $result['order_num'] = $order['order_num'];
  303. $result['order_id'] = $id;
  304. return $result;
  305. }
  306. # 支付成功回调 安全加密 设置token
  307. public function success_secure_api_token()
  308. {
  309. $project_id = Dever::input('pay_project_id');
  310. $info = Dever::db('pay/project')->one($project_id);
  311. if ($info) {
  312. return $info['key'];
  313. }
  314. return 'shop_buy_dever_2020';
  315. }
  316. # 支付成功回调 安全加密
  317. public function success_secure_api($param = array())
  318. {
  319. $this->success($param);
  320. }
  321. # 支付成功回调
  322. public function success($param = array())
  323. {
  324. $send = $param ? $param : Dever::preInput('pay_');
  325. $product_id = $send['pay_product_id'];
  326. $order_id = $send['pay_order_id'];
  327. $status = $send['pay_status'];
  328. $cash = $send['pay_cash'];
  329. $msg = $send['pay_msg'];
  330. $order = Dever::db('shop/buy_order')->one(array('id' => $product_id, 'time' => time()));
  331. if ($order && $order['status'] == 1 && $status == 2) {
  332. $update['status'] = 2;
  333. $update['where_id'] = $order['id'];
  334. Dever::db('shop/buy_order')->update($update);
  335. # 生成结算单
  336. Dever::load('cash/lib/order')->up($order, 1, 1);
  337. }
  338. return 'ok';
  339. }
  340. # 生成订单号
  341. public function getOrderId()
  342. {
  343. $where['order_num'] = Dever::order('C');
  344. $state = Dever::db('shop/buy_order')->one($where);
  345. if (!$state) {
  346. return $where['order_num'];
  347. } else {
  348. return $this->getOrderId();
  349. }
  350. }
  351. # 添加配送信息
  352. public function updatePs($order, $status = 1)
  353. {
  354. $where['order_id'] = $order['id'];
  355. $data = Dever::db('shop/buy_order_ps')->find($where);
  356. if ($data) {
  357. $update['where_id'] = $data['id'];
  358. $update['status'] = $status;
  359. $update['ydate'] = time();
  360. Dever::db('shop/buy_order_ps')->update($update);
  361. }
  362. }
  363. # 确认收货
  364. public function finish($id, $order_id)
  365. {
  366. $data = $this->getView($id, $order_id, false);
  367. if ($data['status'] <= 4) {
  368. if ($data['refund_cash'] > 0) {
  369. $status = 6;
  370. } else {
  371. $status = 5;
  372. }
  373. $state = Dever::db('shop/buy_order')->update(array('where_id' => $data['id'], 'status' => $status, 'fdate' => time()));
  374. if ($state) {
  375. $this->updatePs($data, 3);
  376. # 增加库存
  377. $where['order_id'] = $data['id'];
  378. $where['status'] = 1;
  379. $goods = Dever::db('shop/buy_order_goods')->select($where);
  380. if ($goods) {
  381. Dever::load('shop/lib/goods')->oper($data, 1, 1, $goods);
  382. }
  383. # 生成结算单
  384. Dever::load('cash/lib/order')->up($data, 1, 2);
  385. }
  386. return 'ok';
  387. } else {
  388. Dever::alert('您没有权限');
  389. }
  390. }
  391. # 取消订单
  392. public function cancel($id, $order_id)
  393. {
  394. $data = $this->getView($id, $order_id, false);
  395. if ($data['status'] == 1) {
  396. $state = Dever::db('shop/buy_order')->update(array('where_id' => $data['id'], 'status' => 7, 'operdate' => time()));
  397. # 生成结算单
  398. if ($state) {
  399. //Dever::load('cash/lib/order')->up($data, 1, 3);
  400. }
  401. return 'ok';
  402. } else {
  403. Dever::alert('当前订单状态不允许取消');
  404. }
  405. }
  406. # 查看订单状态
  407. public function orderStatus($id)
  408. {
  409. $config = Dever::db('shop/buy_order')->config;
  410. $info = Dever::db('shop/buy_order')->one($id);
  411. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'process' => 1));
  412. $status = $config['status'][$info['status']];
  413. if ($tk) {
  414. $status = '申请退款中';
  415. }
  416. return $status;
  417. }
  418. # 后台审核退款
  419. public function refund_commit_api()
  420. {
  421. $id = Dever::input('id');
  422. $process = Dever::input('process');
  423. return Dever::load('shop/lib/refund')->set('buy')->action($id, $process, false);
  424. }
  425. # 货物未发出,仅退款
  426. public function refund_apply_commit_api()
  427. {
  428. $order_id = Dever::input('order_id');
  429. $type = Dever::input('type');
  430. $type_id = Dever::input('type_id');
  431. $desc = Dever::input('desc');
  432. $pic = Dever::input('pic');
  433. return Dever::load('shop/lib/refund')->set('buy')->apply($type, $type_id, $order_id, false, 3, 0, $desc, $pic, 2);
  434. }
  435. # 货物已收到 全部退款 报损 需要审核
  436. public function refund_apply_info_commit_api()
  437. {
  438. $order_id = Dever::input('order_id');
  439. $status = Dever::input('status');
  440. $desc = Dever::input('desc');
  441. $pic = Dever::input('pic');
  442. $type = Dever::input('type');
  443. $type_id = Dever::input('type_id');
  444. return Dever::load('shop/lib/refund')->set('buy')->apply($type, $type_id, $order_id, false, $status, 0, $desc, $pic, 1);
  445. }
  446. # 部分退款信息 报损
  447. public function refund_apply_info_one_info()
  448. {
  449. $order_id = Dever::input('order_id');
  450. $order_goods_id = Dever::input('order_goods_id');
  451. $type = Dever::input('type');
  452. $type_id = Dever::input('type_id');
  453. return Dever::load('shop/lib/refund')->set('buy')->getInfo($type, $type_id, $order_id, $order_goods_id);
  454. }
  455. # 部分退款 报损
  456. public function refund_apply_info_one_commit_api()
  457. {
  458. $order_id = Dever::input('order_id');
  459. $order_goods_id = Dever::input('order_goods_id');
  460. $num = Dever::input('num', 0);
  461. $status = Dever::input('status');
  462. $desc = Dever::input('desc');
  463. $pic = Dever::input('pic');
  464. $type = Dever::input('type');
  465. $type_id = Dever::input('type_id');
  466. return Dever::load('shop/lib/refund')->set('buy')->apply($type, $type_id, $order_id, $order_goods_id, $status, $num, $desc, $pic, 1);
  467. }
  468. # 展示订单详情
  469. public function show()
  470. {
  471. $id = Dever::input('order_id');
  472. $type = 1;
  473. $type = Dever::input('type', $type);
  474. $noprice = Dever::input('noprice', 2);
  475. if ($type == 3) {
  476. $noprice = 1;
  477. }
  478. $cash_type = Dever::input('cash', 1);
  479. $config = Dever::db('shop/buy_order')->config;
  480. $info = Dever::db('shop/buy_order')->one($id);
  481. $tk_status = Dever::db('shop/buy_order_refund')->config['status'];
  482. $tk_process = Dever::db('shop/buy_order_refund')->config['process'];
  483. $status = $config['status'][$info['status']];
  484. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'type' => 1));
  485. $cdate = date('Y-m-d H:i', $info['cdate']);
  486. if ($info['operdate']) {
  487. $operdate = date('Y-m-d H:i', $info['operdate']);
  488. } else {
  489. $operdate = '';
  490. }
  491. if ($info['fdate']) {
  492. $fdate = date('Y-m-d H:i', $info['fdate']);
  493. } else {
  494. $fdate = '';
  495. }
  496. if ($type == 2 || $info['type'] == 2) {
  497. $info['price'] = $info['p_price'];
  498. }
  499. if ($type == 1 && $tk && $tk['process'] == 1) {
  500. $status = '申请' . $tk_status[$tk['status']];
  501. }
  502. $time = array();
  503. if ($type == 1) {
  504. $time[] = array('下单时间', $cdate);
  505. $time[] = array('订货时间', $operdate);
  506. } else {
  507. $time[] = array('订货时间', $operdate);
  508. }
  509. if ($fdate) {
  510. $time[] = array('完成时间', $fdate);
  511. }
  512. if ($info['type'] == 1) {
  513. $type_info = Dever::db('shop/info')->find($info['type_id']);
  514. } elseif ($info['type'] == 2) {
  515. $type_info = Dever::db('store/info')->find($info['type_id']);
  516. } elseif ($info['type'] == 3) {
  517. $type_info = Dever::db('store/info')->find($info['type_id']);
  518. }
  519. $result = array();
  520. $result[$info['order_num']] = array
  521. (
  522. 'type' => 'info',
  523. 'content' => array
  524. (
  525. array
  526. (
  527. array('订单状态', $status),
  528. ),
  529. $time,
  530. array
  531. (
  532. array('配送至', $type_info['name']),
  533. array('配送地址', $type_info['address']),
  534. ),
  535. array
  536. (
  537. array('联系人', $type_info['truename']),
  538. array('联系电话', $type_info['mobile']),
  539. array('订单备注', $info['info']),
  540. ),
  541. )
  542. );
  543. if ($type == 1 && $info['source_id'] > 0) {
  544. if ($info['source_type'] == 2) {
  545. # 仓库
  546. $source_info = Dever::db('store/info')->find($info['source_id']);
  547. } elseif ($info['source_type'] == 3) {
  548. # 工厂
  549. $source_info = Dever::db('factory/info')->find($info['source_id']);
  550. }
  551. $result[$info['order_num']]['content'][] = array
  552. (
  553. array('供应商', $source_info['name']),
  554. array('联系人', $source_info['truename']),
  555. array('联系电话', $source_info['mobile']),
  556. );
  557. }
  558. $button = array();
  559. if ($type == 2) {
  560. $config = Dever::load('factory/admin/auth.config');
  561. if ($info['status'] < 5) {
  562. $button[] = array
  563. (
  564. 'type' => 'link',
  565. 'link' => Dever::url('admin/order.print?id=' . $info['id'] . '&type=1', 'factory'),
  566. 'name' => '打印货单',
  567. );
  568. }
  569. if ($info['status'] == 3) {
  570. $button[] = array
  571. (
  572. 'type' => 'edit',
  573. 'link' => Dever::url('order_send?id=' . $info['id'], 'factory'),
  574. 'name' => '配送发货',
  575. );
  576. $button[] = array
  577. (
  578. 'type' => 'cancel',
  579. 'link' => Dever::url('lib/buy.refund_apply_commit?json=1&order_id=' . $info['id'] . '&type_id=' . $info['source_id'], 'mshop'),
  580. 'name' => '取消订单',
  581. );
  582. }
  583. $config['phone'] = '您的专属客服:' . $config['kf_name'] . ',联系电话:' . $config['phone'];
  584. $button[] = array
  585. (
  586. 'type' => 'alert',
  587. 'content' => $config['phone'],
  588. 'name' => '联系平台',
  589. );
  590. } elseif ($noprice == 1) {
  591. $config = Dever::load('factory/admin/auth.config');
  592. $button[] = array
  593. (
  594. 'type' => 'link',
  595. 'link' => Dever::url('admin/order.print?id=' . $info['id'] . '&type=2', 'store'),
  596. 'name' => '打印货单',
  597. );
  598. if ($info['type'] == 2) {
  599. $button[] = array
  600. (
  601. 'type' => 'link',
  602. 'link' => Dever::url('lib/buy.excel_one?type='.$info['type'].'&type_id='.$info['type_id'].'&id='.$info['id'], 'mshop'),
  603. 'name' => '导出货单',
  604. );
  605. }
  606. if ($info['status'] == 3 && $info['type'] == 1) {
  607. $button[] = array
  608. (
  609. 'type' => 'edit',
  610. 'link' => Dever::url('order_send?id=' . $info['id'] . '&type=2', 'store'),
  611. 'name' => '配送发货',
  612. );
  613. }
  614. if ($info['status'] == 4 && $info['type'] == 2) {
  615. $button[] = array
  616. (
  617. 'type' => 'action',
  618. 'link' => Dever::url('admin/factory_order.action_commit?order_id='.$info['id'], 'store'),
  619. 'name' => '确认收货',
  620. );
  621. }
  622. $config['phone'] = '您的专属客服:' . $config['kf_name'] . ',联系电话:' . $config['phone'];
  623. $button[] = array
  624. (
  625. 'type' => 'alert',
  626. 'content' => $config['phone'],
  627. 'name' => '联系平台',
  628. );
  629. }
  630. $url = Dever::url('lib/buy.refund_commit?json=1', 'mshop');
  631. if (!$info['refund_cash']) {
  632. $info['refund_cash'] = 0;
  633. }
  634. if ($info['refund_cash'] > 0 && $tk) {
  635. if ($type == 2) {
  636. $info['refund_cash'] = $info['refund_p_cash'];
  637. }
  638. if ($tk['process'] == 1) {
  639. $process = '<a href="javascript:;" onclick="audit('.$tk['id'].', 2, \''.$url.'\')" class="layui-btn layui-btn-primary">通过</a><a href="javascript:;" onclick="audit('.$tk['id'].', 3, \''.$url.'\')" class="layui-btn layui-btn-danger">驳回</a>';
  640. } else {
  641. $process = $tk_process[$tk['process']];
  642. }
  643. $desc = array();
  644. $desc['退款原因'] = $tk['desc'];
  645. $desc['退款图片'] = '暂无';
  646. if ($tk['pic']) {
  647. $pic = explode(',', $tk['pic']);
  648. $tk['pic'] = '';
  649. foreach ($pic as $k => $v) {
  650. $tk['pic'] .= '<a href="'.$v.'" target="_blank"><img src="'.$v.'" width="100"/></a>';
  651. }
  652. }
  653. ;
  654. $tk['cdate'] = date('Y-m-d H:i', $tk['cdate']);
  655. $result['退款信息'] = array
  656. (
  657. 'type' => 'info',
  658. 'content' => array
  659. (
  660. array
  661. (
  662. array('退款金额', $info['refund_cash']),
  663. array('数量', $info['num']),
  664. array('退款进度', $process),
  665. ),
  666. array
  667. (
  668. array('申请时间', $tk['cdate']),
  669. array('申请原因', $tk['desc']),
  670. array('图片', $tk['pic']),
  671. ),
  672. ),
  673. );
  674. }
  675. $info['ps_info'] = Dever::db('shop/buy_order_ps')->find(array('order_id' => $info['id']));
  676. if ($info['ps_info']) {
  677. $info['ps_info']['service_name'] = '供应商自送';
  678. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  679. if ($info['ps_info']['ydate']) {
  680. $info['ps_info']['ydate'] = date('Y-m-d H:i', $info['ps_info']['ydate']);
  681. } else {
  682. $info['ps_info']['ydate'] = '无';
  683. }
  684. $status = Dever::db('shop/buy_order_ps')->config['status'];
  685. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  686. if ($info['ps_info']['service_id'] > 0) {
  687. //暂时还没有
  688. $service = Dever::db('shop/service')->find($info['ps_info']['service_id']);
  689. $info['ps_info']['service_name'] = $service['name'];
  690. }
  691. $info['ps_info']['order_num'] = str_replace("\r\n", ',', $info['ps_info']['order_num']);
  692. if ($noprice == 1) {
  693. $result['物流信息'] = array
  694. (
  695. 'type' => 'info',
  696. 'content' => array
  697. (
  698. array
  699. (
  700. array('名称', $info['ps_info']['service_name']),
  701. array('单号', $info['ps_info']['order_num']),
  702. ),
  703. array
  704. (
  705. array('数量', $info['ps_info']['num']),
  706. array('重量', $info['ps_info']['zl']),
  707. array('体积', $info['ps_info']['tj']),
  708. ),
  709. array
  710. (
  711. array('发货时间', $info['ps_info']['cdate']),
  712. array('收货时间', $info['ps_info']['ydate']),
  713. ),
  714. ),
  715. );
  716. } else {
  717. $result['物流信息'] = array
  718. (
  719. 'type' => 'info',
  720. 'content' => array
  721. (
  722. array
  723. (
  724. array('名称', $info['ps_info']['service_name']),
  725. array('单号', $info['ps_info']['order_num']),
  726. array('费用', $info['ps_info']['price']),
  727. ),
  728. array
  729. (
  730. array('配货员', $info['ps_info']['phy']),
  731. array('打包员', $info['ps_info']['dby']),
  732. ),
  733. array
  734. (
  735. array('数量', $info['ps_info']['num']),
  736. array('重量', $info['ps_info']['zl']),
  737. array('体积', $info['ps_info']['tj']),
  738. ),
  739. array
  740. (
  741. array('发货时间', $info['ps_info']['cdate']),
  742. array('收货时间', $info['ps_info']['ydate']),
  743. ),
  744. ),
  745. );
  746. }
  747. }
  748. $body = array();
  749. $body_total = array();
  750. $body_total['price'] = 0;
  751. $body_total['num'] = 0;
  752. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $info['id']));
  753. $goods_status = Dever::db('shop/buy_order_goods')->config['status'];
  754. $refund_body = array();
  755. $refund_body_total = array();
  756. $refund_body_total['price'] = 0;
  757. $refund_body_total['num'] = 0;
  758. foreach ($goods as $k => $v) {
  759. if ($type == 2) {
  760. $v['price'] = $v['p_price'];
  761. }
  762. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  763. $status = $goods_status[$v['status']];
  764. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
  765. if ($tk && $tk['process'] == 1) {
  766. if ($type == 1) {
  767. $status = '申请' . $tk_status[$tk['status']];
  768. } else {
  769. $status = '已上报';
  770. }
  771. if ($type == 1) {
  772. $desc = array();
  773. $desc['退款原因'] = $tk['desc'];
  774. $desc['退款图片'] = '暂无';
  775. if ($tk['pic']) {
  776. $desc['退款图片'] = '';
  777. $pic = explode(',', $tk['pic']);
  778. foreach ($pic as $k1 => $v1) {
  779. $desc['退款图片'] .= '<a href="'.$v1.'" target="_blank"><img src="'.$v1.'" width="100"/></a>';
  780. }
  781. }
  782. $status .= '<div style="width:300px"><table><tr><td><a href="javascript:showAlert($(\'#desc_content\').html());">点此查看原因</a><span id="desc_content" style="display:none">'.Dever::table($desc).'</span><td>';
  783. $status .= '<td><button href="javascript:;" style="width:80px" onclick="audit('.$tk['id'].', 2, \''.$url.'\')" class="layui-btn layui-btn-primary">通过</button></td><td><button href="javascript:;" onclick="audit('.$tk['id'].', 3, \''.$url.'\')" style="width:80px" class="layui-btn layui-btn-danger">驳回</button></td></tr></table></div>';
  784. }
  785. } elseif ($type > 1 && (!$tk || ($tk && $tk['process'] == 3))) {
  786. //$tui = Dever::url('lib/buy.refund_apply_info_one_commit?json=1', 'mshop');
  787. if ($type == 2) {
  788. $tui = Dever::url('order_tui?order_goods_id=' . $v['id'] . '&order_id=' . $info['id'] . '&type=13&type_id=' . $info['source_id'], 'factory');
  789. } else {
  790. if ($info['source_type'] == 3) {
  791. $tui = Dever::url('order_tui?order_goods_id=' . $v['id'] . '&order_id=' . $info['id'] . '&type=13&type_id=' . $info['source_id'], 'store');
  792. } else {
  793. $tui = Dever::url('order_tui?order_goods_id=' . $v['id'] . '&order_id=' . $info['id'] . '&type=12&type_id=' . $info['source_id'], 'store');
  794. }
  795. }
  796. if ($info['status'] == 3) {
  797. $status = '<a href="javascript:;" onclick="fastEdit($(this), \''.$tui.'\', \'缺货退款\', \'\', 1)" class="layui-btn">缺货退款</a>';
  798. } elseif ($info['status'] == 4) {
  799. $status = '<a href="javascript:;" onclick="fastEdit($(this), \''.$tui.'\', \'报损退款\', \'\', 1)" class="layui-btn">报损退款</a>';
  800. }
  801. }
  802. if (isset($goods_info['sku'])) {
  803. $sku = '[' . $goods_info['sku']['string'] . ']';
  804. } else {
  805. $sku = '';
  806. }
  807. $d = array
  808. (
  809. 'pic' => $goods_info['cover'],
  810. 'name' => $goods_info['name'],
  811. 'sku' => $sku,
  812. 'price' => $v['price'],
  813. 'num' => $v['num'],
  814. 'status' => $status,
  815. );
  816. if ($tk && $v['status'] == 3 && $tk['status'] != 3) {
  817. $d['status'] = $tk_status[$tk['status']];
  818. if ($noprice == 1) {
  819. unset($d['price']);
  820. }
  821. $refund_body[] = $d;
  822. $price = $v['price']*$v['num'];
  823. $refund_body_total['price'] += $price;
  824. $refund_body_total['num'] += $v['num'];
  825. } else {
  826. if ($noprice == 1) {
  827. unset($d['price']);
  828. }
  829. $body[] = $d;
  830. $price = $v['price']*$v['num'];
  831. $body_total['price'] += $price;
  832. $body_total['num'] += $v['num'];
  833. }
  834. }
  835. if ($body) {
  836. if ($refund_body_total['price']) {
  837. $body_total['set_price'] = $body_total['price'] - $refund_body_total['price'];
  838. }
  839. if ($refund_body_total['num']) {
  840. $body_total['set_num'] = $body_total['num'] - $refund_body_total['num'];
  841. }
  842. if ($noprice == 1) {
  843. unset($body_total['price']);
  844. unset($body_total['set_price']);
  845. }
  846. $result['商品清单'] = array
  847. (
  848. 'type' => 'list',
  849. 'content' => $body,
  850. 'total' => $body_total,
  851. );
  852. }
  853. if ($refund_body) {
  854. if ($noprice == 1) {
  855. unset($refund_body_total['price']);
  856. }
  857. $result['缺货与报损商品'] = array
  858. (
  859. 'type' => 'list',
  860. 'content' => $refund_body,
  861. 'total' => $refund_body_total,
  862. );
  863. }
  864. $search_option_type = Dever::input('search_option_type');
  865. if ($cash_type == 1) {
  866. if ($search_option_type == 1) {
  867. # 门店结算单
  868. $cash_order = Dever::db('cash/order')->select(array('type' => $info['type'], 'type_id' => $info['type_id'], 'source_order_id' => $info['id']));
  869. $col = 'cash';
  870. } elseif ($search_option_type == 2) {
  871. # 工厂结算单
  872. $cash_order = Dever::db('cash/order')->select(array('source_type' => $info['source_type'], 'source_id' => $info['source_id'], 'source_order_id' => $info['id']));
  873. $col = 'p_cash';
  874. } elseif ($search_option_type == 3) {
  875. # 仓库结算单
  876. $cash_order = Dever::db('cash/order')->select(array('source_type' => $info['source_type'], 'source_id' => $info['source_id'], 'source_order_id' => $info['id']));
  877. $col = 'num';
  878. }
  879. }
  880. if (isset($cash_order) && $cash_order) {
  881. $head = array('序号', '结算单号', '数量', '金额', '结算类型', '入账状态', '审核状态', '结算时间', '审核未通过原因', '管理');
  882. if ($search_option_type == 3) {
  883. unset($head[3]);
  884. }
  885. $body = array();
  886. $config = Dever::db('cash/order')->config;
  887. foreach ($cash_order as $k => $v) {
  888. $cdate = date('Y-m-d H:i', $v['cdate']);
  889. $fdate = $v['fdate'] ? date('Y-m-d H:i', $v['fdate']) : '';
  890. $operdate = $v['operdate'] ? date('Y-m-d H:i', $v['operdate']) : '';
  891. $cash = $v[$col];
  892. $url = Dever::url('project/database/list?project=shop&table=buy_order_goods&order_id='.$v['source_order_id'].'&page_type=1', 'manage');
  893. $v['source_order_num'] = '<a href="'.$url.'" style="color:blue">'.$v['source_order_num'].'</a>';
  894. $m = '';
  895. if ($v['audit'] == 1 && $v['audit_type'] == 2) {
  896. $url = Dever::url('project/database/update?project=cash&table=order&where_id='.$v['id'].'&col=audit,desc', 'manage');
  897. $m = '<a href="javascript:;" onclick="fastEdit($(this), \''.$url.'\', \'审核\', \'\', 1)" class="layui-btn">审核</a>';
  898. }
  899. $d = array
  900. (
  901. $k+1,
  902. $v['order_num'],
  903. $v['num'],
  904. $cash,
  905. $config['config_jstype'][$v['jstype']],
  906. $config['config_status'][$v['status']],
  907. $config['config_audit'][$v['audit']],
  908. $operdate,
  909. $v['desc'],
  910. $m,
  911. );
  912. if ($search_option_type == 3) {
  913. unset($d[3]);
  914. }
  915. $body[] = $d;
  916. }
  917. $result['对账清单'] = array
  918. (
  919. 'type' => 'table',
  920. 'content' => array
  921. (
  922. 'head' => $head,
  923. 'body' => $body,
  924. )
  925. );
  926. }
  927. $head = array
  928. (
  929. 'name' => '基本信息',
  930. 'btn' => $button,
  931. );
  932. $html = Dever::show($head, $result);
  933. return $html;
  934. }
  935. # 发货
  936. /*
  937. public function send_commit()
  938. {
  939. $order_id = Dever::input('order_id');
  940. $order = Dever::db('shop/buy_order')->find($order_id);
  941. if (!$order || ($order && $order['status'] != 3)) {
  942. Dever::alert('当前订单状态无法发货');
  943. }
  944. $state = Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'status' => 4));
  945. if ($state) {
  946. $this->updatePs($order, 1);
  947. }
  948. return 'reload';
  949. }*/
  950. # 采购订单审核 拆单
  951. public function audit_commit()
  952. {
  953. $order_id = Dever::input('order_id');
  954. $order = Dever::db('shop/buy_order')->find($order_id);
  955. if (!$order || ($order && $order['status'] != 2)) {
  956. Dever::alert('当前订单状态无法拆单');
  957. }
  958. if ($order['type'] != 1) {
  959. Dever::alert('这不是门店的订单');
  960. }
  961. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $order_id));
  962. $shop = Dever::db('shop/info')->find($order['type_id']);
  963. # 获取门店分配的仓库和工厂
  964. $shop_store = Dever::db('shop/store')->select(array('shop_id' => $shop['id']));
  965. $store = array();
  966. $distance = array();
  967. # 按照距离排序
  968. foreach ($shop_store as $k => $v) {
  969. $info = Dever::db('store/info')->find($v['store_id']);
  970. if ($info && $info['status'] == 1) {
  971. $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
  972. $store[] = $info;
  973. }
  974. }
  975. if (!$store) {
  976. Dever::alert('未分配仓库');
  977. }
  978. array_multisort($distance, SORT_ASC, SORT_NUMERIC, $store);
  979. $shop_factory = Dever::db('shop/factory')->select(array('shop_id' => $shop['id']));
  980. $factory = array();
  981. $distance = array();
  982. # 按照距离排序
  983. foreach ($shop_factory as $k => $v) {
  984. $info = Dever::db('factory/info')->find($v['factory_id']);
  985. if ($info && $info['status'] == 1) {
  986. $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
  987. $factory[] = $info;
  988. }
  989. }
  990. if (!$factory) {
  991. Dever::alert('未分配工厂');
  992. }
  993. array_multisort($distance, SORT_ASC, SORT_NUMERIC, $factory);
  994. $this->order_num = 0;
  995. $this->order_id = array();
  996. $delete = array();
  997. $yes = array();
  998. foreach ($goods as $k => $v) {
  999. # 开始找仓库
  1000. $find = $this->store($order['id'], $order['order_num'] . '_' . $this->order_num, $shop, $store, $v);
  1001. if (!$find[1]) {
  1002. # 仓库没有,找厂家
  1003. if ($factory) {
  1004. $find = $this->factory($order['id'], $order['order_num'] . '_' . $this->order_num, $shop, $factory, $v);
  1005. }
  1006. }
  1007. if ($find[1]) {
  1008. $delete[$v['id']] = $v;
  1009. } else {
  1010. $yes[$v['id']] = $v;
  1011. }
  1012. }
  1013. $num = count($this->order_id);
  1014. if ($num > 0) {
  1015. if ($num == 1) {
  1016. $key = array_keys($this->order_id);
  1017. $key = $key[0];
  1018. Dever::db('shop/buy_order')->delete($key);
  1019. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $key));
  1020. $p_price = 0;
  1021. foreach ($goods as $k => $v) {
  1022. $goods_info = Dever::db('shop/buy_order_goods')->find(array('order_id' => $order['id'], 'goods_id' => $v['goods_id']));
  1023. if ($goods_info) {
  1024. Dever::db('shop/buy_order_goods')->update(array('where_id' => $goods_info['id'], 'set_p_price' => $v['p_price']));
  1025. $p_price += $v['p_price'];
  1026. }
  1027. }
  1028. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'status' => 3, 'source_type' => $this->order_id[$key][0], 'source_id' => $this->order_id[$key][1], 'p_price' => $p_price, 'operdate' => time()));
  1029. Dever::db('shop/buy_order_goods')->delete(array('order_id' => $key));
  1030. $this->order_id[$order['id']] = array($this->order_id[$key][0], $this->order_id[$key][1]);
  1031. unset($this->order_id[$key]);
  1032. } else {
  1033. if (!$yes) {
  1034. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'state' => 2));
  1035. Dever::load('cash/lib/order')->up($order, 1, 3);
  1036. } else {
  1037. $price = 0;
  1038. $num = 0;
  1039. foreach ($delete as $k => $v) {
  1040. Dever::db('shop/buy_order_goods')->delete(array('where_id' => $v['id']));
  1041. $price += $v['price'];
  1042. $num += $v['num'];
  1043. }
  1044. $order['price'] = $order['price'] - $price;
  1045. $order['num'] = $order['num'] - $num;
  1046. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'price' => $order['price'], 'num' => $order['num']));
  1047. Dever::load('shop/lib/refund')->set('buy')->apply(1, $shop['id'], $order_id, false, 3, 0, '供货商库存不足');
  1048. Dever::load('cash/lib/order')->up($order, 1, 3);
  1049. }
  1050. }
  1051. } else {
  1052. # 没有找到,退款
  1053. Dever::load('shop/lib/refund')->set('buy')->apply(1, $shop['id'], $order_id, false, 3, 0, '供货商库存不足');
  1054. }
  1055. if ($this->order_id) {
  1056. foreach ($this->order_id as $k => $v) {
  1057. $this->sendMsg($v[0], $v[1], $k);
  1058. if ($k != $order['id']) {
  1059. $order_info = Dever::db('shop/buy_order')->find($k);
  1060. Dever::load('cash/lib/order')->up($order_info, 1, 1);
  1061. }
  1062. }
  1063. }
  1064. return 'ok';
  1065. }
  1066. private function store($order_id, $order_num, $shop, $store, $goods, $state = 2)
  1067. {
  1068. $find = array();
  1069. $type_id = array();
  1070. $id = array();
  1071. if (!$store) {
  1072. return array(2, $type_id, $id);
  1073. }
  1074. foreach ($store as $k => $v) {
  1075. $where['store_id'] = $v['id'];
  1076. $where['goods_id'] = $goods['goods_id'];
  1077. $where['sku_id'] = ($goods['sku_id'] && $goods['sku_id'] > 0) ? $goods['sku_id'] : -1;
  1078. $sku = Dever::db('store/goods_sku')->getOne($where);
  1079. if ($sku) {
  1080. if ($goods['num'] <= $sku['total']) {
  1081. # 如果库存符合,直接下单即可,生成仓库订单
  1082. $state = $this->createOrder($order_id, $order_num, 1, $shop['id'], 2, $where['store_id'], $goods['price'], $goods['price'], $goods['num'], $where['goods_id'], $where['sku_id']);
  1083. if ($state) {
  1084. $type_id[] = $where['store_id'];
  1085. $id[] = $state;
  1086. $this->order_id[$state] = array(2, $where['store_id']);
  1087. break;
  1088. }
  1089. } elseif ($state == 1 && $sku['total'] > 0) {
  1090. # 如果库存不够,从这个仓库扣下一部分,剩余的从另外仓库处理
  1091. $goods['num'] = $goods['num'] - $sku['total'];
  1092. # 生成仓库订单
  1093. $state = $this->createOrder($order_id, $order_num, 1, $shop['id'], 2, $where['store_id'], $goods['price'], $goods['price'], $sku['total'], $where['goods_id'], $where['sku_id']);
  1094. if (!$state) {
  1095. $goods['num'] = $goods['num'] + $sku['total'];
  1096. } else {
  1097. $type_id[] = $where['store_id'];
  1098. $id[] = $state;
  1099. $this->order_id[$state] = array(2, $where['store_id']);
  1100. }
  1101. continue;
  1102. } else {
  1103. # 直接切换到下一个仓库处理
  1104. continue;
  1105. }
  1106. }
  1107. }
  1108. return array(2, $type_id, $id);
  1109. }
  1110. private function factory($order_id, $order_num, $shop, $factory, $goods)
  1111. {
  1112. $find = array();
  1113. $type_id = array();
  1114. $id = array();
  1115. if (!$factory) {
  1116. return array(3, $type_id, $id);
  1117. }
  1118. foreach ($factory as $k => $v) {
  1119. $where['factory_id'] = $v['id'];
  1120. $where['goods_id'] = $goods['goods_id'];
  1121. $where['sku_id'] = ($goods['sku_id'] && $goods['sku_id'] > 0) ? $goods['sku_id'] : -1;
  1122. $sku = Dever::db('factory/goods_sku')->getOne($where);
  1123. if ($sku && $sku['p_price'] > 0) {
  1124. $state = $this->createOrder($order_id, $order_num, 1, $shop['id'], 3, $where['factory_id'], $goods['price'], $sku['p_price'], $goods['num'], $where['goods_id'], $where['sku_id']);
  1125. if ($state) {
  1126. $type_id[] = $where['factory_id'];
  1127. $id[] = $state;
  1128. $this->order_id[$state] = array(3, $where['factory_id']);
  1129. break;
  1130. }
  1131. } else {
  1132. continue;
  1133. }
  1134. }
  1135. return array(3, $type_id, $id);
  1136. }
  1137. # 发送消息
  1138. private function sendMsg($source_type, $source_id, $id)
  1139. {
  1140. if (Dever::project('message')) {
  1141. $msg_param['type'] = 1;//消息类型1是订单消息
  1142. $msg_param['id'] = $id;
  1143. $msg_param['source_id'] = $source_id;
  1144. $msg_param['source_type'] = $source_type;
  1145. $msg_param = Dever::json_encode($msg_param);
  1146. $project = $source_type + 1;
  1147. $msg = '您有一个新的订货单,请及时处理。';
  1148. Dever::load('message/lib/data')->push(-1, $source_id, '订货通知', $msg, 2, $project, false, $msg_param);
  1149. }
  1150. }
  1151. # 生成拆单的订单
  1152. private function createOrder($order_id, $order_num, $type, $type_id, $source_type, $source_id, $price, $p_price, $num, $goods_id, $sku_id)
  1153. {
  1154. $where['parent_order_id'] = $order_id;
  1155. $where['type'] = $type;
  1156. $where['type_id'] = $type_id;
  1157. $where['source_type'] = $source_type;
  1158. $where['source_id'] = $source_id;
  1159. $where['r'] = $goods_id . '_' . microtime() . '_' . mt_rand(1, 10000);
  1160. $info = Dever::db('shop/buy_order')->find($where);
  1161. if (!$info) {
  1162. $this->order_num++;
  1163. $data = $where;
  1164. $data['name'] = $order_id;
  1165. $data['order_num'] = $order_num;
  1166. $data['operdate'] = time();
  1167. $data['status'] = 3;
  1168. $data['price'] = $price * $num;
  1169. $data['p_price'] = $p_price * $num;
  1170. $data['num'] = $num;
  1171. $data['refund_cash'] = 0;
  1172. $data['refund_p_cash'] = 0;
  1173. $id = $state = Dever::db('shop/buy_order')->insert($data);
  1174. } else {
  1175. $id = $info['id'];
  1176. $data['where_id'] = $id;
  1177. $data['price'] = $info['price'] + ($price * $num);
  1178. $data['p_price'] = $info['p_price'] + ($p_price * $num);
  1179. $data['num'] = $info['num'] + $num;
  1180. $state = Dever::db('shop/buy_order')->update($data);
  1181. }
  1182. if ($state && $id > 0) {
  1183. $goods_info = Dever::db('shop/buy_order_goods')->find(array('order_id' => $id));
  1184. if (!$goods_info) {
  1185. $goods['order_id'] = $id;
  1186. $goods['goods_id'] = $goods_id;
  1187. $goods['sku_id'] = $sku_id;
  1188. $goods['price'] = $price;
  1189. $goods['p_price'] = $p_price;
  1190. $goods['num'] = $num;
  1191. Dever::db('shop/buy_order_goods')->insert($goods);
  1192. }
  1193. $state = $this->updateSell($source_type, $source_id, $goods_id, $sku_id, $num);
  1194. return $id;
  1195. }
  1196. return false;
  1197. }
  1198. # 减少库存,增加销量
  1199. public function updateSell($source_type, $source_id, $goods_id, $sku_id, $num)
  1200. {
  1201. if ($source_type == 2) {
  1202. $data['store_id'] = $source_id;
  1203. $data['goods_id'] = $goods_id;
  1204. $data['sku_id'] = $sku_id;
  1205. $data['num'] = $num;
  1206. return Dever::load('store/lib/goods')->oper($source_id, 1, 2, array($data));
  1207. } else {
  1208. $up = array();
  1209. $up['where_factory_id'] = $source_id;
  1210. $up['where_goods_id'] = $goods_id;
  1211. $up['sell_num'] = $num;
  1212. $state = Dever::db('factory/goods')->incSell($up);
  1213. if ($state) {
  1214. if ($sku_id > 0) {
  1215. } else {
  1216. $sku_id = -1;
  1217. }
  1218. $upSku = $up;
  1219. $upSku['where_sku_id'] = $sku_id;
  1220. $state = Dever::db('factory/goods_sku')->incSell($upSku);
  1221. }
  1222. return $state;
  1223. }
  1224. }
  1225. # 打印订单单
  1226. public function printer($user)
  1227. {
  1228. $id = Dever::input('id');
  1229. $type = Dever::input('type', 1);
  1230. $factory_config = Dever::db('main/factory_config')->find();
  1231. $main_config = Dever::db('main/config')->find();
  1232. $config = Dever::db('shop/buy_order')->config;
  1233. $info = Dever::db('shop/buy_order')->one($id);
  1234. $source_info = Dever::db('factory/info')->find($info['source_id']);
  1235. if ($type == 2) {
  1236. # 仓库
  1237. $member = Dever::db('store/member')->find($user['id']);
  1238. $name = '仓库名称';
  1239. } else {
  1240. # 工厂
  1241. $member = Dever::db('factory/member')->find($user['id']);
  1242. $name = '工厂名称';
  1243. }
  1244. if ($info['type'] == 1) {
  1245. $type_info = Dever::db('shop/info')->find($info['type_id']);
  1246. } elseif ($info['type'] == 2) {
  1247. $type_info = Dever::db('store/info')->find($info['type_id']);
  1248. } elseif ($info['type'] == 3) {
  1249. $type_info = Dever::db('store/info')->find($info['type_id']);
  1250. }
  1251. $cdate = date('Y-m-d H:i', $info['cdate']);
  1252. if ($info['operdate']) {
  1253. $operdate = date('Y-m-d H:i', $info['operdate']);
  1254. } else {
  1255. $operdate = '';
  1256. }
  1257. if ($info['fdate']) {
  1258. $fdate = date('Y-m-d H:i', $info['fdate']);
  1259. } else {
  1260. $fdate = '';
  1261. }
  1262. $status = $config['status'][$info['status']];
  1263. $info['ps_info'] = Dever::db('shop/buy_order_ps')->find(array('order_id' => $info['id']));
  1264. if ($info['ps_info']) {
  1265. $info['ps_info']['service_name'] = '供应商自送';
  1266. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  1267. if ($info['ps_info']['ydate']) {
  1268. $info['ps_info']['ydate'] = date('Y-m-d H:i', $info['ps_info']['ydate']);
  1269. } else {
  1270. $info['ps_info']['ydate'] = '无';
  1271. }
  1272. $ps_status = Dever::db('shop/buy_order_ps')->config['status'];
  1273. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  1274. if ($info['ps_info']['service_id'] > 0) {
  1275. //暂时还没有
  1276. $service = Dever::db('shop/service')->find($info['ps_info']['service_id']);
  1277. $info['ps_info']['service_name'] = $service['name'];
  1278. }
  1279. $info['ps_info']['order_num'] = str_replace("\r\n", ',', $info['ps_info']['order_num']);
  1280. }
  1281. $pdf = Dever::load('pdf/lib/base')->init();
  1282. $pdf->hr('-', $main_config['name']);
  1283. $pdf->br()->font(20)->center('订货单号:' . $info['order_num']);
  1284. $pdf->font(10);
  1285. $pdf->br();
  1286. $pdf->br()->left($name . ':' . $type_info['name'], 80)->left('制单人:' . $member['name'], 60)->left('制单时间:' . date('Y-m-d H:i'), 40);
  1287. $pdf->hr();
  1288. if ($type == 2 && $info['type'] == 2) {
  1289. $pdf->br()->left('订单状态:' . $status, 140)->left('订货时间:' . $operdate, 40);
  1290. $pdf->hr();
  1291. if (!$info['ps_info']) {
  1292. $pdf->left('发货供应商:' . $source_info['name']);
  1293. } else {
  1294. $pdf->br()->left('发货供应商:' . $source_info['name'], 140)->left('发货时间:' . $info['ps_info']['cdate'], 40);
  1295. }
  1296. if ($info['ps_info']) {
  1297. $pdf->br()->left('物流信息:' . $info['ps_info']['service_name'] . ' 物流单号:' . $info['ps_info']['order_num'], 140)->left('确认收货时间:' . $info['ps_info']['ydate'], 40);
  1298. }
  1299. $pdf->hr();
  1300. } else {
  1301. $pdf->br()->left('订单状态:' . $status, 80)->left('订货人:' . $type_info['truename'], 60)->left('订货时间:' . $operdate, 40);
  1302. $pdf->hr();
  1303. $pdf->br()->left('配送至:' . $type_info['name'], 150)->right('收件人联系方式:' . $type_info['truename'] . ',' . $type_info['mobile'], 40);
  1304. $pdf->left('配送地址:' . $type_info['address']);
  1305. $pdf->hr();
  1306. if ($info['ps_info']) {
  1307. $pdf->br()->left('发货时间:' . $info['ps_info']['cdate'], 80)->left('配货员:' . $info['ps_info']['phy'], 70)->left('打包员:' . $info['ps_info']['dby'], 40);
  1308. if ($info['ps_info']['zl'] && $info['ps_info']['tj']) {
  1309. $pdf->br()->left('包裹重量:' . $info['ps_info']['zl'] . '克', 80)->left('包裹体积:' . $info['ps_info']['tj'] . '立方厘米', 70)->left('包裹数量:' . $info['ps_info']['num'], 40);
  1310. } elseif (!$info['ps_info']['zl'] && $info['ps_info']['tj']) {
  1311. $pdf->br()->left('包裹体积:' . $info['ps_info']['tj'] . '立方厘米', 150)->left('包裹数量:' . $info['ps_info']['num'], 40);
  1312. } elseif ($info['ps_info']['zl'] && !$info['ps_info']['tj']) {
  1313. $pdf->br()->left('包裹重量:' . $info['ps_info']['zl'] . '克', 150)->left('包裹数量:' . $info['ps_info']['num'], 40);
  1314. } else {
  1315. $pdf->left('包裹数量:' . $info['ps_info']['num']);
  1316. }
  1317. $pdf->br()->left('物流信息:' . $info['ps_info']['service_name'], 40);
  1318. if ($info['ps_info']['order_num']) {
  1319. $pdf->left('物流单号:' . $info['ps_info']['order_num'], 70);
  1320. }
  1321. if ($info['ps_info']['ydate']) {
  1322. $pdf->left('到货确认时间:' . $info['ps_info']['ydate']);
  1323. }
  1324. $pdf->hr();
  1325. }
  1326. }
  1327. $body = array();
  1328. $body_total = array();
  1329. $body_total['price'] = 0;
  1330. $body_total['num'] = 0;
  1331. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $info['id']));
  1332. $goods_status = Dever::db('shop/buy_order_goods')->config['status'];
  1333. $refund_body = array();
  1334. $refund_body_total = array();
  1335. $refund_body_total['price'] = 0;
  1336. $refund_body_total['num'] = 0;
  1337. foreach ($goods as $k => $v) {
  1338. if ($type == 1) {
  1339. $v['price'] = $v['p_price'];
  1340. }
  1341. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  1342. $status = $goods_status[$v['status']];
  1343. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
  1344. if (isset($goods_info['sku'])) {
  1345. $sku = $goods_info['sku']['string'];
  1346. } else {
  1347. $sku = '无';
  1348. }
  1349. $d = array
  1350. (
  1351. $goods_info['id'],
  1352. $goods_info['name'],
  1353. $sku,
  1354. $v['price'],
  1355. 'x ' . $v['num'],
  1356. );
  1357. if ($type == 2) {
  1358. unset($d[3]);
  1359. }
  1360. if ($tk && $v['status'] == 3 && $tk['status'] != 3) {
  1361. $refund_body[] = $d;
  1362. $price = $v['price']*$v['num'];
  1363. $refund_body_total['price'] += $price;
  1364. $refund_body_total['num'] += $v['num'];
  1365. } else {
  1366. $body[] = $d;
  1367. $price = $v['price']*$v['num'];
  1368. $body_total['price'] += $price;
  1369. $body_total['num'] += $v['num'];
  1370. }
  1371. }
  1372. $head = array(array('商品编号', 40), array('商品名称', 60), array('商品属性', 40), array('单价', 40), array('数量', 20));
  1373. if ($type == 2) {
  1374. unset($head[3]);
  1375. $head[1][1] = 70;
  1376. $head[2][1] = 60;
  1377. }
  1378. if ($body) {
  1379. $pdf->center('订单商品');
  1380. $pdf->br();
  1381. foreach ($head as $k => $v) {
  1382. $pdf->left($v[0], $v[1]);
  1383. }
  1384. foreach ($body as $k => $v) {
  1385. $pdf->br();
  1386. foreach ($head as $k1 => $v1) {
  1387. $pdf->left($v[$k1], $v1[1]);
  1388. }
  1389. }
  1390. $pdf->br();
  1391. if ($type == 2) {
  1392. $pdf->right('共'.$body_total['num'].'件商品');
  1393. } else {
  1394. $pdf->right('共'.$body_total['num'].'件商品,合计金额¥' . $body_total['price'] . '元');
  1395. }
  1396. $pdf->hr();
  1397. }
  1398. if ($refund_body) {
  1399. $pdf->center('缺货与报损商品');
  1400. $pdf->br();
  1401. foreach ($head as $k => $v) {
  1402. $pdf->left($v[0], $v[1]);
  1403. }
  1404. foreach ($refund_body as $k => $v) {
  1405. $pdf->br();
  1406. foreach ($head as $k1 => $v1) {
  1407. $pdf->left($v[$k1], $v1[1]);
  1408. }
  1409. }
  1410. $pdf->br();
  1411. if ($type == 2) {
  1412. $pdf->right('共'.$refund_body_total['num'].'件商品');
  1413. } else {
  1414. $pdf->right('共'.$refund_body_total['num'].'件商品,合计金额¥' . $refund_body_total['price'] . '元');
  1415. }
  1416. $pdf->hr();
  1417. }
  1418. $pdf->br(1);
  1419. $pdf->right('如遇任何问题请致电客服');
  1420. $pdf->br();
  1421. $pdf->font(20);
  1422. $pdf->left($main_config['name'], 160);
  1423. $pdf->font(10);
  1424. $pdf->right('电话:' . $factory_config['phone'], 30);
  1425. $pdf->br();
  1426. $pdf->left($main_config['site'], 160);
  1427. $pdf->font(10);
  1428. $pdf->right($main_config['worktime'], 30);
  1429. $pdf->out('订货单');
  1430. }
  1431. # 导出单个订单
  1432. public function excel_one_api()
  1433. {
  1434. $this->type = Dever::input('type');
  1435. $id = Dever::input('id');
  1436. $type_id = Dever::input('type_id');
  1437. $data = $this->getView($type_id, $id);
  1438. $this->excel(array($data));
  1439. }
  1440. # 导出订单
  1441. public function excel($data)
  1442. {
  1443. if (!$data) {
  1444. Dever::alert('无导出数据');
  1445. }
  1446. $file = '入库订单';
  1447. $type = Dever::input('search_option_type', $this->type);
  1448. $header = array('所属仓库', '订单号', '商品来源', '发货日期', '商品名称', '商品规格属性', '商品数量', '报损退款状态', '订单状态');
  1449. $table = 'store/info';
  1450. if ($type == 1) {
  1451. $header[0] = '所属门店';
  1452. $table = 'shop/info';
  1453. }
  1454. $body = array();
  1455. foreach ($data as $k => $v) {
  1456. if (!is_array($v)) {
  1457. continue;
  1458. }
  1459. foreach ($v['goods'] as $k1 => $v1) {
  1460. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v1['goods_id'], $v1['sku_id']);
  1461. if (isset($goods_info['sku'])) {
  1462. $sku = $goods_info['sku']['string'];
  1463. } else {
  1464. $sku = '';
  1465. }
  1466. $d = array
  1467. (
  1468. $v['type_info']['name'],
  1469. $v['order_num'],
  1470. $v['source_info']['name'],
  1471. $v['cdate'],
  1472. $goods_info['name'],
  1473. $sku,
  1474. $v1['num'],
  1475. $v1['status_name'],
  1476. $v['status_name'],
  1477. );
  1478. $body[] = $d;
  1479. }
  1480. }
  1481. Dever::excelExport($body, $header, $file);
  1482. }
  1483. }