Buy.php 59 KB

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