Buy.php 102 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702
  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 setOutStatus($id, $type = 1)
  45. {
  46. if ($type == 1) {
  47. $data['out_status'] = 2;
  48. $data['out_date'] = time();
  49. } else {
  50. $data['source_out_status'] = 2;
  51. $data['source_out_date'] = time();
  52. }
  53. $data['where_id'] = $id;
  54. Dever::db('shop/buy_order')->update($data);
  55. }
  56. # 获取订单列表
  57. public function getList($id, $status = false)
  58. {
  59. $result = array();
  60. $where = $this->where($id);
  61. $status = Dever::input('status', $status);
  62. if ($status) {
  63. if ($status == 'refund') {
  64. $where['refund_status'] = 2;
  65. } else {
  66. $where['status'] = $status;
  67. }
  68. }
  69. $order_num = Dever::input('order_num');
  70. if ($order_num) {
  71. $where['order_num'] = $order_num;
  72. }
  73. $refund_status = Dever::input('refund_status');
  74. if ($refund_status) {
  75. $where['refund_status'] = $refund_status;
  76. }
  77. $result['search_value'] = $where;
  78. $result['search_value']['day'] = $day = Dever::input('day');
  79. if ($day) {
  80. if ($this->type > 10) {
  81. $where['start_o'] = Dever::maketime($day . ' 00:00:00');
  82. $where['end_o'] = Dever::maketime($day . ' 23:59:59');
  83. } elseif ($this->type == 2) {
  84. $where['start_f'] = Dever::maketime($day . ' 00:00:00');
  85. $where['end_f'] = Dever::maketime($day . ' 23:59:59');
  86. } else {
  87. $where['start'] = Dever::maketime($day . ' 00:00:00');
  88. $where['end'] = Dever::maketime($day . ' 23:59:59');
  89. }
  90. }
  91. $result['search_value']['start'] = $start = Dever::input('start');
  92. $result['search_value']['end'] = $end = Dever::input('end');
  93. if ($start && $end) {
  94. if ($this->type > 10) {
  95. $where['start_o'] = Dever::maketime($start);
  96. $where['end_o'] = Dever::maketime($end);
  97. } elseif ($this->type == 2) {
  98. $where['start_f'] = Dever::maketime($start);
  99. $where['end_f'] = Dever::maketime($end);
  100. } else {
  101. $where['start'] = Dever::maketime($start);
  102. $where['end'] = Dever::maketime($end);
  103. }
  104. }
  105. $type = Dever::input('type');
  106. if ($type) {
  107. $where['type'] = $type;
  108. }
  109. $source_type = Dever::input('source_type');
  110. if ($source_type) {
  111. $where['source_type'] = $source_type;
  112. }
  113. $parent_type = Dever::input('parent_type');
  114. if ($parent_type) {
  115. $where['parent_type'] = $parent_type;
  116. }
  117. $out_status = Dever::input('out_status');
  118. if ($out_status) {
  119. $where['out_status'] = $out_status;
  120. }
  121. $source_out_status = Dever::input('source_out_status');
  122. if ($source_out_status) {
  123. $where['source_out_status'] = $source_out_status;
  124. }
  125. $excel = Dever::input('excel', 1);
  126. if ($excel == 2) {
  127. $result['order'] = Dever::db($this->table)->getData($where);
  128. } else {
  129. $result['order'] = Dever::db($this->table)->getAll($where);
  130. }
  131. if ($result['order']) {
  132. foreach ($result['order'] as $k => $v) {
  133. $result['order'][$k] = $this->getInfo($v);
  134. }
  135. }
  136. $result['search'] = array();
  137. $result['search']['status'] = array
  138. (
  139. array('value' => 1, 'name' => '待支付'),
  140. array('value' => 2, 'name' => '待审核'),
  141. array('value' => 3, 'name' => '待发货'),
  142. array('value' => 4, 'name' => '待收货确认'),
  143. array('value' => 5, 'name' => '已完成'),
  144. array('value' => 6, 'name' => '已完成(有退款)'),
  145. array('value' => '7,8,11', 'name' => '已取消'),
  146. );
  147. $result['config'] = Dever::db('main/manage_config')->find();
  148. return $result;
  149. }
  150. # 查看详情
  151. public function getView($id, $order_id, $show = true)
  152. {
  153. $where = $this->where($id);
  154. $where['id'] = $order_id;
  155. $result = Dever::db($this->table)->find($where);
  156. if (!$result) {
  157. Dever::alert('订单不存在');
  158. }
  159. if ($show) {
  160. $result = $this->getInfo($result, true);
  161. }
  162. $result['config'] = Dever::db('main/manage_config')->find();
  163. return $result;
  164. }
  165. # 查看退款详情
  166. public function getRefund($id, $order_id)
  167. {
  168. $data = $this->getView($id, $order_id, false);
  169. $data['refund'] = Dever::db('shop/buy_order_refund')->select(array('order_id' => $info['id']));
  170. return $data;
  171. }
  172. # 获取订单详细信息
  173. public function getInfo($info, $view = false)
  174. {
  175. if ($info['status'] == 1) {
  176. # 15分钟内支付,900秒
  177. $m = 900;
  178. # 支付倒计时
  179. $info['time'] = time() - $info['cdate'];
  180. if ($info['time'] >= $m) {
  181. # 已过期,自动取消
  182. $info['time'] = -1;
  183. Dever::db($this->table)->update(array('where_id' => $info['id'], 'status' => 11));
  184. $info['status'] = 11;
  185. } else {
  186. $info['time'] = $m - $info['time'];
  187. }
  188. }
  189. $info['status_name'] = $this->config['status'][$info['status']];
  190. $info['goods'] = Dever::db('shop/buy_order_goods')->select(array('order_id' => $info['id']));
  191. if ($info['type'] == 1) {
  192. $info['type_info'] = Dever::db('shop/info')->find($info['type_id']);
  193. } elseif ($info['type'] == 2) {
  194. $info['type_info'] = Dever::db('store/info')->find($info['type_id']);
  195. } elseif ($info['type'] == 3) {
  196. $info['type_info'] = Dever::db('factory/info')->find($info['type_id']);
  197. }
  198. if ($info['source_type'] == 1) {
  199. $info['source_info'] = Dever::db('shop/info')->find($info['source_id']);
  200. } elseif ($info['source_type'] == 2) {
  201. $info['source_info'] = Dever::db('store/info')->find($info['source_id']);
  202. } elseif ($info['source_type'] == 3) {
  203. $info['source_info'] = Dever::db('factory/info')->find($info['source_id']);
  204. }
  205. $info['cdate_day'] = date('Y-m-d', $info['cdate']);
  206. $info['cdate'] = date('Y-m-d H:i', $info['cdate']);
  207. if ($info['operdate']) {
  208. $info['operdate_day'] = date('Y-m-d', $info['operdate']);
  209. $info['operdate'] = date('Y-m-d H:i', $info['operdate']);
  210. } else {
  211. $info['operdate_day'] = $info['cdate_day'];
  212. $info['operdate'] = $info['cdate'];
  213. }
  214. $goods_status = Dever::db('shop/buy_order_goods')->config['status'];
  215. $goods_process = Dever::db('shop/buy_order_refund')->config['process'];
  216. if ($view || $this->view == 2) {
  217. foreach ($info['goods'] as $k => $v) {
  218. $info['goods'][$k]['info'] = Dever::load('goods/lib/info')->getPayInfo($v['goods_id'], $v['sku_id']);
  219. $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
  220. }
  221. } else {
  222. foreach ($info['goods'] as $k => $v) {
  223. $goods = Dever::db('goods/info')->one($v['goods_id']);
  224. $info['goods'][$k]['name'] = $goods['name'];
  225. $info['goods'][$k]['cover'] = $goods['cover'];
  226. $info['goods'][$k]['status_name'] = $goods_status[$v['status']];
  227. }
  228. }
  229. if (!$info['refund_cash']) {
  230. $info['refund_cash'] = 0;
  231. }
  232. # 配送信息
  233. $info['ps_info'] = Dever::db('shop/buy_order_ps')->find(array('order_id' => $info['id']));
  234. if ($info['ps_info']) {
  235. $info['ps_info']['service'] = Dever::array_decode($info['ps_info']['service']);
  236. $info['ps_info']['cdate_day'] = date('Y-m-d', $info['ps_info']['cdate']);
  237. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  238. $info['ps_info']['ydate_day'] = '-';
  239. if ($info['ps_info']['ydate']) {
  240. $info['ps_info']['ydate_day'] = date('Y-m-d', $info['ps_info']['ydate']);
  241. $info['ps_info']['ydate'] = date('Y-m-d H:i', $info['ps_info']['ydate']);
  242. }
  243. $status = Dever::db('shop/buy_order_ps')->config['status'];
  244. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  245. if ($info['ps_info']['service']) {
  246. if (isset($info['ps_info']['service']['serivce'])) {
  247. $info['ps_info']['service'] = $info['ps_info']['service']['service'];
  248. }
  249. foreach ($info['ps_info']['service'] as $k => $v) {
  250. if (isset($v['order_num']) && $v['order_num']) {
  251. $info['ps_info']['service'][$k]['service_name'] = '供应商自送';
  252. if (isset($v['service_id']) && $v['service_id'] > 0) {
  253. $service = Dever::db('shop/service')->find($v['service_id']);
  254. if ($service) {
  255. $info['ps_info']['service'][$k]['service_name'] = $service['name'];
  256. } else {
  257. $info['ps_info']['service'][$k]['service_name'] = '';
  258. }
  259. }
  260. } else {
  261. unset($info['ps_info']['service'][$k]);
  262. }
  263. }
  264. }
  265. }
  266. $info = $this->refund_button($info);
  267. return $info;
  268. }
  269. # 是否可以退款
  270. public function refund_button($info)
  271. {
  272. $info['refund_button'] = 1;
  273. # 是否显示退款按钮
  274. $manage_config = Dever::db('main/manage_config')->find();
  275. $refund_time = $manage_config['buy_refund_day']*86400;
  276. if ($refund_time > 0 && ($info['status'] == 5 || $info['status'] == 6)) {
  277. $cur = time();
  278. if (strstr($info['fdate'], '-')) {
  279. $info['fdate'] = Dever::maketime($info['fdate']);
  280. }
  281. if ($cur - $info['fdate'] < $refund_time) {
  282. # 可以退款
  283. $info['refund_button'] = 1;
  284. } else {
  285. # 不可以退款
  286. $info['refund_button'] = 2;
  287. }
  288. }
  289. return $info;
  290. }
  291. # 发起支付
  292. public function action($uid, $type, $type_id, $name, $num, $goods, $price, $info, $refer = '', $area = '',$method=false)
  293. {
  294. $openid = false;
  295. if ($uid > 0) {
  296. $wechat = Dever::db('passport/wechat')->one(array('uid' => $uid, 'type' => 1, 'system_id' => 2));
  297. if (!$wechat) {
  298. Dever::alert('错误的用户信息');
  299. }
  300. $openid = $wechat['openid'];
  301. }
  302. $order_data['type'] = $type;
  303. $order_data['type_id'] = $type_id;
  304. if ($area) {
  305. $order_data['area'] = $area . ',' . $type_id;
  306. }
  307. $order_data['name'] = $name;
  308. $order_data['num'] = $num;
  309. $order_data['info'] = $info;
  310. $order_data['price'] = $price;
  311. $time = time();
  312. $order_data_where = $order_data;
  313. $order_data_where['start'] = $time - 60;
  314. $order_data_where['end'] = $time;
  315. $order_data_where['option']['start'] = array('yes-cdate', '>=');
  316. $order_data_where['option']['end'] = array('yes-cdate', '<=');
  317. $info = Dever::db('shop/buy_order')->find($order_data_where);
  318. $order_data['order_num'] = $this->getOrderId();
  319. $order_data['refund_cash'] = 0;
  320. $order_data['refund_p_cash'] = 0;
  321. if (!$info) {
  322. $id = Dever::db('shop/buy_order')->insert($order_data);
  323. } else {
  324. return array
  325. (
  326. 'order' => '',
  327. 'order_num' => $info['order_num'],
  328. 'order_id' => $info['id'],
  329. 'type' => 'test',
  330. );
  331. }
  332. if($id && isset($method) && $method && isset($method['method']) && $method['method'] == 1){
  333. if($method && isset($method['price']) && $method['price'] && $method['yue']>=0){
  334. $shop = Dever::db('shop/info')->find($type_id);
  335. $record = Dever::load('shop/lib/record')->pingtai($type_id,$shop['sid'],4,$id,$method['price'],$method['yue']);
  336. if($method['type'] ==1){
  337. $record_cash = $method['price'];
  338. }elseif($method['type'] == 2){
  339. $record_cash = $shop['price'];
  340. }
  341. Dever::db('shop/buy_order')->update(array('where_id'=>$id,'set_record_cash'=>$record_cash));
  342. }
  343. }
  344. if (!$id) {
  345. Dever::alert('支付失败');
  346. }
  347. foreach($goods as $k => $v) {
  348. $data['order_id'] = $id;
  349. $data['goods_id'] = $v['id'];
  350. $data['sku_id'] = $v['sku_id'];
  351. $data['price'] = $v['price'];
  352. $data['num'] = $v['buy_num'];
  353. Dever::db('shop/buy_order_goods')->insert($data);
  354. }
  355. // return 'ok';
  356. if ($price <= 0) {
  357. $param['pay_product_id'] = $id;
  358. $param['pay_order_id'] = $order_data['order_num'];
  359. $param['pay_status'] = 2;
  360. $param['pay_cash'] = $price;
  361. $param['pay_msg'] = '';
  362. $this->success($param);
  363. return array
  364. (
  365. 'order' => '',
  366. 'order_num' => $order_data['order_num'],
  367. 'order_id' => $id,
  368. 'type' => 'test',
  369. );
  370. }
  371. $param = array
  372. (
  373. 'project_id' => 2,
  374. 'channel_id' => 1,
  375. 'system_source' => 5,
  376. 'account_id' => 7,
  377. 'uid' => $uid,
  378. 'name' => $order_data['name'],
  379. 'openid' => $openid,
  380. 'cash' => $price,
  381. 'product_id' => $id,
  382. 'order_id' => $order_data['order_num'],
  383. 'refer' => $refer,
  384. );
  385. $receipt = Dever::input('receipt');
  386. if ($receipt) {
  387. $param['receipt'] = $receipt;
  388. }
  389. $result = Dever::load('pay/api.pay', $param);
  390. //$result['type'] = 'test';
  391. //$result['order'] = '';
  392. $result['order_num'] = $order_data['order_num'];
  393. $result['order_id'] = $id;
  394. return $result;
  395. }
  396. # 再次发起支付
  397. public function raction($id, $refer = '')
  398. {
  399. $order = Dever::db('shop/buy_order')->find($id);
  400. if (!$order) {
  401. Dever::alert('订单信息错误');
  402. }
  403. if ($order['status'] > 1) {
  404. Dever::alert('订单已支付或已取消');
  405. }
  406. $param = array
  407. (
  408. 'project_id' => 2,
  409. 'channel_id' => 1,
  410. 'system_source' => 5,
  411. 'account_id' => 7,
  412. 'uid' => -1,
  413. 'name' => $order['name'],
  414. 'cash' => $order['price'],
  415. 'product_id' => $id,
  416. 'order_id' => $order['order_num'],
  417. 'refer' => $refer,
  418. );
  419. $receipt = Dever::input('receipt');
  420. if ($receipt) {
  421. $param['receipt'] = $receipt;
  422. }
  423. $result = Dever::load('pay/api.pay', $param);
  424. $result['order_num'] = $order['order_num'];
  425. $result['order_id'] = $id;
  426. return $result;
  427. }
  428. # 查询是否支付
  429. public function cpay($id, $system_source = 5, $refer = '')
  430. {
  431. $order = Dever::db('shop/buy_order')->find($id);
  432. if (!$order) {
  433. Dever::alert('订单信息错误');
  434. }
  435. $result['order_num'] = $order['order_num'];
  436. $result['order_id'] = $id;
  437. if ($order['status'] > 1 && $order['status'] <= 6) {
  438. # 已支付
  439. $result['status'] = 1;
  440. } else {
  441. # 待支付
  442. $result['status'] = 2;
  443. }
  444. return $result;
  445. }
  446. # 支付成功回调 安全加密 设置token
  447. public function success_secure_api_token()
  448. {
  449. $project_id = Dever::input('pay_project_id');
  450. $info = Dever::db('pay/project')->one($project_id);
  451. if ($info) {
  452. return $info['key'];
  453. }
  454. return 'shop_buy_dever_2020';
  455. }
  456. # 支付成功回调 安全加密
  457. public function success_secure_api($param = array())
  458. {
  459. $this->success($param);
  460. }
  461. # 支付成功回调
  462. public function success($param = array())
  463. {
  464. $send = $param ? $param : Dever::preInput('pay_');
  465. $product_id = $send['pay_product_id'];
  466. $order_id = $send['pay_order_id'];
  467. $status = $send['pay_status'];
  468. $cash = $send['pay_cash'];
  469. $msg = $send['pay_msg'];
  470. $order = Dever::db('shop/buy_order')->one(array('id' => $product_id, 'time' => time()));
  471. if ($order && $order['status'] == 1 && $status == 2) {
  472. $update['status'] = 2;
  473. $update['where_id'] = $order['id'];
  474. Dever::db('shop/buy_order')->update($update);
  475. # 生成结算单
  476. Dever::load('cash/lib/order')->up($order, 1, 1);
  477. }
  478. return 'ok';
  479. }
  480. # 生成订单号
  481. public function getOrderId()
  482. {
  483. $where['order_num'] = Dever::order('C');
  484. $state = Dever::db('shop/buy_order')->one($where);
  485. if (!$state) {
  486. return $where['order_num'];
  487. } else {
  488. return $this->getOrderId();
  489. }
  490. }
  491. # 添加配送信息
  492. public function updatePs($order, $status = 1)
  493. {
  494. $where['order_id'] = $order['id'];
  495. $data = Dever::db('shop/buy_order_ps')->find($where);
  496. if ($data) {
  497. $update['where_id'] = $data['id'];
  498. $update['status'] = $status;
  499. $update['ydate'] = time();
  500. Dever::db('shop/buy_order_ps')->update($update);
  501. }
  502. }
  503. # 确认收货
  504. public function finish($id, $order_id)
  505. {
  506. $data = $this->getView($id, $order_id, false);
  507. if ($data['status'] <= 4) {
  508. if ($data['refund_cash'] > 0) {
  509. $status = 6;
  510. } else {
  511. $status = 5;
  512. }
  513. $state = Dever::db('shop/buy_order')->update(array('where_id' => $data['id'], 'status' => $status, 'fdate' => time()));
  514. if ($state) {
  515. $this->updatePs($data, 3);
  516. # 增加库存
  517. $where['order_id'] = $data['id'];
  518. $where['status'] = 1;
  519. $goods = Dever::db('shop/buy_order_goods')->select($where);
  520. if ($goods) {
  521. Dever::load('shop/lib/goods')->oper($data, 1, 1, $goods);
  522. }
  523. # 生成结算单
  524. Dever::load('cash/lib/order')->up($data, 1, 2);
  525. }
  526. return 'ok';
  527. } else {
  528. Dever::alert('您没有权限');
  529. }
  530. }
  531. # 取消订单
  532. public function cancel($id, $order_id)
  533. {
  534. $data = $this->getView($id, $order_id, false);
  535. if ($data['status'] == 1) {
  536. $state = Dever::db('shop/buy_order')->update(array('where_id' => $data['id'], 'status' => 7, 'operdate' => time()));
  537. # 生成结算单
  538. if ($state) {
  539. if (isset($data['record_cash']) && $data['record_cash'] && $data['record_cash'] > 0) {
  540. # 还原钱包
  541. $shop = Dever::db('shop/info')->find($data['type_id']);
  542. if ($shop) {
  543. Dever::load('shop/lib/record')->pingtai($shop['id'], $shop['sid'],3, $data['id'], $data['record_cash'], round($shop['price'] + $data['record_cash'], 2), '订单退货返还');
  544. //Dever::load('cash/lib/order')->up($data, 1, 3);
  545. }
  546. }
  547. }
  548. return 'ok';
  549. } else {
  550. Dever::alert('当前订单状态不允许取消');
  551. }
  552. }
  553. # 查看订单状态
  554. public function orderStatus($id)
  555. {
  556. $config = Dever::db('shop/buy_order')->config;
  557. $info = Dever::db('shop/buy_order')->one($id);
  558. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'process' => 1));
  559. $status = $config['status'][$info['status']];
  560. if ($tk) {
  561. $status = '申请退款中';
  562. }
  563. return $status;
  564. }
  565. # 后台审核退款
  566. public function refund_commit_api()
  567. {
  568. $id = Dever::input('id');
  569. $process = Dever::input('process');
  570. return Dever::load('shop/lib/refund')->set('buy')->action($id, $process, false);
  571. }
  572. # 货物未发出,仅退款
  573. public function refund_apply_commit_api()
  574. {
  575. $order_id = Dever::input('order_id');
  576. $type = Dever::input('type');
  577. $type_id = Dever::input('type_id');
  578. $desc = Dever::input('desc');
  579. $pic = Dever::input('pic');
  580. return Dever::load('shop/lib/refund')->set('buy')->apply($type, $type_id, $order_id, false, 3, 0, $desc, $pic, 2);
  581. }
  582. # 货物已收到 全部退款 报损 需要审核
  583. public function refund_apply_info_commit_api()
  584. {
  585. $order_id = Dever::input('order_id');
  586. $status = Dever::input('status');
  587. $desc = Dever::input('desc');
  588. $pic = Dever::input('pic');
  589. $type = Dever::input('type');
  590. $type_id = Dever::input('type_id');
  591. return Dever::load('shop/lib/refund')->set('buy')->apply($type, $type_id, $order_id, false, $status, 0, $desc, $pic, 1);
  592. }
  593. # 部分退款信息 报损
  594. public function refund_apply_info_one_info()
  595. {
  596. $order_id = Dever::input('order_id');
  597. $order_goods_id = Dever::input('order_goods_id');
  598. $type = Dever::input('type');
  599. $type_id = Dever::input('type_id');
  600. return Dever::load('shop/lib/refund')->set('buy')->getInfo($type, $type_id, $order_id, $order_goods_id);
  601. }
  602. # 部分退款 报损
  603. public function refund_apply_info_one_commit_api()
  604. {
  605. $order_id = Dever::input('order_id');
  606. $order_goods_id = Dever::input('order_goods_id');
  607. $num = Dever::input('num', 0);
  608. $status = Dever::input('status');
  609. $desc = Dever::input('desc');
  610. $pic = Dever::input('pic');
  611. $type = Dever::input('type');
  612. $type_id = Dever::input('type_id');
  613. return Dever::load('shop/lib/refund')->set('buy')->apply($type, $type_id, $order_id, $order_goods_id, $status, $num, $desc, $pic, 1);
  614. }
  615. # 展示订单详情
  616. public function show()
  617. {
  618. $id = Dever::input('order_id');
  619. $type = 1;
  620. $type = Dever::input('search_option_type', $type);
  621. $type = Dever::input('type', $type);
  622. $noprice = Dever::input('noprice', 2);
  623. if ($type == 3) {
  624. $noprice = 1;
  625. }
  626. $info = Dever::db('shop/buy_order')->one($id);
  627. if (!$info) {
  628. return '无订单信息';
  629. }
  630. $result = array();
  631. $type_info = $this->show_base($info, $result, $type);
  632. $parent_order = $this->show_address($info, $result, $type, $type_info);
  633. $button = $this->show_button($info, $type, $parent_order, $noprice);
  634. $audit = Dever::input('audit');
  635. if ($audit == 1) {
  636. # 审核信息
  637. $this->show_audit($info, $result);
  638. } else {
  639. # 退款信息
  640. $this->show_refund($info, $result, $type);
  641. # 配送信息
  642. $this->show_ps($info, $result);
  643. # 商品信息
  644. $this->show_goods($info, $result, $type, $noprice);
  645. # 对账信息
  646. $this->show_dz($info, $result, $noprice);
  647. }
  648. $head = array
  649. (
  650. 'name' => '基本信息',
  651. 'btn' => $button,
  652. );
  653. $html = Dever::show($head, $result);
  654. return $html;
  655. }
  656. private function show_base($info, &$result, $type)
  657. {
  658. $config = Dever::db('shop/buy_order')->config;
  659. $status = $config['status'][$info['status']];
  660. $cdate = date('Y-m-d H:i', $info['cdate']);
  661. if ($info['operdate']) {
  662. $operdate = date('Y-m-d H:i', $info['operdate']);
  663. } else {
  664. $operdate = '';
  665. }
  666. if ($info['fdate']) {
  667. $fdate = date('Y-m-d H:i', $info['fdate']);
  668. } else {
  669. $fdate = '';
  670. }
  671. if ($type == 2 || $info['type'] == 2) {
  672. $info['price'] = $info['p_price'];
  673. }
  674. if ($info['refund_status'] == 2 && $info['status'] != 8) {
  675. $status .= '[申请报损中]';
  676. }
  677. $time = array();
  678. if ($type == 1) {
  679. $time[] = array('下单时间', $cdate);
  680. $time[] = array('订货时间', $operdate);
  681. } else {
  682. $time[] = array('订货时间', $operdate);
  683. }
  684. if ($fdate) {
  685. $time[] = array('完成时间', $fdate);
  686. }
  687. if ($info['type'] == 1) {
  688. $type_info = Dever::db('shop/info')->find($info['type_id']);
  689. } elseif ($info['type'] == 2) {
  690. $type_info = Dever::db('store/info')->find($info['type_id']);
  691. } elseif ($info['type'] == 3) {
  692. $type_info = Dever::db('store/info')->find($info['type_id']);
  693. }
  694. $result[$info['order_num']] = array
  695. (
  696. 'type' => 'info',
  697. 'content' => array
  698. (
  699. array
  700. (
  701. array('订单状态', $status),
  702. ),
  703. $time,
  704. )
  705. );
  706. return $type_info;
  707. }
  708. private function show_address($info, &$result, $type, $type_info)
  709. {
  710. $parent_order = array();
  711. if ($info['parent_type'] == 2) {
  712. $parent_order = Dever::db('shop/sell_order')->find($info['parent_order_id']);
  713. } elseif ($info['parent_type'] == 3) {
  714. $parent_order = Dever::db('agent/dh_order')->find($info['parent_order_id']);
  715. }
  716. if ($parent_order && isset($parent_order['uid']) && $parent_order['uid']) {
  717. if ($parent_order['address_id']) {
  718. $address = Dever::load('passport/address')->getOne($parent_order['uid'], $parent_order['address_id']);
  719. $result[$info['order_num']]['content'][] = array
  720. (
  721. array('收件人', $address['contact']),
  722. array('收件人手机', $address['mobile']),
  723. );
  724. $result[$info['order_num']]['content'][] = array
  725. (
  726. array('收货地址', $address['address_info']),
  727. array('门牌号', $address['house_number']),
  728. );
  729. } else {
  730. $user = Dever::db('passport/user')->find($parent_order['uid']);
  731. $result[$info['order_num']]['content'][] = array
  732. (
  733. array('收件人', $user['username']),
  734. array('收件人手机', $user['mobile']),
  735. );
  736. }
  737. } elseif ($parent_order && isset($parent_order['mid']) && $parent_order['mid']) {
  738. if ($parent_order['address_id']) {
  739. $address = Dever::load('agent/address')->getOne($parent_order['mid'], $parent_order['address_id']);
  740. $result[$info['order_num']]['content'][] = array
  741. (
  742. array('收件人', $address['contact']),
  743. array('收件人手机', $address['mobile']),
  744. );
  745. $result[$info['order_num']]['content'][] = array
  746. (
  747. array('收货地址', $address['address_info']),
  748. array('门牌号', $address['house_number']),
  749. );
  750. } else {
  751. $user = Dever::db('agent/member')->find($parent_order['mid']);
  752. $result[$info['order_num']]['content'][] = array
  753. (
  754. array('收件人', $user['mid']),
  755. array('收件人手机', $user['mobile']),
  756. );
  757. }
  758. } else {
  759. $result[$info['order_num']]['content'][] = array
  760. (
  761. array('配送至', $type_info['name']),
  762. array('配送地址', $type_info['address']),
  763. );
  764. $result[$info['order_num']]['content'][] = array
  765. (
  766. array('联系人', $type_info['truename']),
  767. array('联系电话', $type_info['mobile']),
  768. array('订单备注', $info['info']),
  769. );
  770. $result[$info['order_num']]['content'][] = array
  771. (
  772. array('资金账户抵扣', $info['record_cash']),
  773. );
  774. }
  775. if ($type == 1 && $info['source_id'] > 0) {
  776. if ($info['source_type'] == 2) {
  777. # 仓库
  778. $source_info = Dever::db('store/info')->find($info['source_id']);
  779. } elseif ($info['source_type'] == 3) {
  780. # 工厂
  781. $source_info = Dever::db('factory/info')->find($info['source_id']);
  782. }
  783. $result[$info['order_num']]['content'][] = array
  784. (
  785. array('供应商', $source_info['name']),
  786. array('联系人', $source_info['truename']),
  787. array('联系电话', $source_info['mobile']),
  788. );
  789. }
  790. return $parent_order;
  791. }
  792. private function show_button($info, $type, $parent_order, $noprice)
  793. {
  794. $button = array();
  795. if ($type == 2) {
  796. $config = Dever::load('factory/admin/auth.config');
  797. if ($info['status'] < 5) {
  798. $button[] = array
  799. (
  800. 'type' => 'link',
  801. 'link' => Dever::url('admin/order.print?id=' . $info['id'] . '&type=1', 'factory'),
  802. 'name' => '打印货单',
  803. );
  804. }
  805. if ($info['status'] == 3) {
  806. $button[] = array
  807. (
  808. 'type' => 'edit',
  809. 'link' => Dever::url('order_send?id=' . $info['id'], 'factory'),
  810. 'name' => '配送发货',
  811. );
  812. if ($parent_order && (isset($parent_order['uid']) && $parent_order['uid'] || isset($parent_order['mid']) && $parent_order['mid'])) {
  813. } else {
  814. $button[] = array
  815. (
  816. 'type' => 'cancel',
  817. 'link' => Dever::url('lib/buy.refund_apply_commit?json=1&order_id=' . $info['id'] . '&type_id=' . $info['source_id'], 'mshop'),
  818. 'name' => '取消订单',
  819. );
  820. }
  821. }
  822. $config['phone'] = '您的专属客服:' . $config['kf_name'] . ',联系电话:' . $config['phone'];
  823. $button[] = array
  824. (
  825. 'type' => 'alert',
  826. 'content' => $config['phone'],
  827. 'name' => '联系平台',
  828. );
  829. } elseif ($type != 1 && $noprice == 1) {
  830. $config = Dever::load('factory/admin/auth.config');
  831. $button[] = array
  832. (
  833. 'type' => 'link',
  834. 'link' => Dever::url('admin/order.print?id=' . $info['id'] . '&type=2', 'store'),
  835. 'name' => '打印货单',
  836. );
  837. /*
  838. if ($info['type'] == 2 && $info['source_type'] != 2 && in_array($info['status'], array(5,6,8))) {
  839. $button[] = array
  840. (
  841. 'type' => 'link',
  842. 'link' => Dever::url('lib/buy.excel_one?type='.$info['type'].'&type_id='.$info['type_id'].'&id='.$info['id'], 'mshop'),
  843. 'name' => '导出货单',
  844. );
  845. }
  846. */
  847. $store_type = Dever::input('store_type');
  848. if ($info['status'] == 3 && $store_type == 1) {
  849. $button[] = array
  850. (
  851. 'type' => 'edit',
  852. 'link' => Dever::url('order_send?id=' . $info['id'] . '&type=2', 'store'),
  853. 'name' => '配送发货',
  854. );
  855. if ($parent_order && ((isset($parent_order['uid']) && $parent_order['uid']) || (isset($parent_order['mid']) && $parent_order['mid']))) {
  856. } else {
  857. $button[] = array
  858. (
  859. 'type' => 'cancel',
  860. 'link' => Dever::url('lib/buy.refund_apply_commit?json=1&order_id=' . $info['id'] . '&type_id=' . $info['source_id'], 'mshop'),
  861. 'name' => '取消订单',
  862. );
  863. }
  864. }
  865. if ($info['status'] == 4 && $store_type != 1 && $info['type'] == 2) {
  866. $button[] = array
  867. (
  868. 'type' => 'action',
  869. 'link' => Dever::url('admin/factory_order.action_commit?order_id='.$info['id'], 'store'),
  870. 'name' => '确认收货',
  871. );
  872. }
  873. $config['phone'] = '您的专属客服:' . $config['kf_name'] . ',联系电话:' . $config['phone'];
  874. $button[] = array
  875. (
  876. 'type' => 'alert',
  877. 'content' => $config['phone'],
  878. 'name' => '联系平台',
  879. );
  880. }
  881. return $button;
  882. }
  883. public function getSelect($source, $source_id, $goods_id)
  884. {
  885. if ($goods_id) {
  886. $name = 'audit';
  887. $id = 'audit_' . $goods_id;
  888. } else {
  889. $name = 'audit_set';
  890. $id = 'audit';
  891. }
  892. $select = '<select class="'.$name.'" id="'.$id.'">';
  893. foreach ($source as $k => $v) {
  894. $yes = '';
  895. if ($source_id && $v['id'] == $source_id) {
  896. $yes = 'selected';
  897. }
  898. $value = $v['type'] . '_' . $v['id'];
  899. $select .= '<option value="'.$value.'" '.$yes.'>'.$v['name'].'</option>';
  900. }
  901. $select .= '</select>';
  902. return $select;
  903. }
  904. private function show_audit($info, &$result)
  905. {
  906. $info['type'] = 1;
  907. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $info['id'], 'status' => 1));
  908. $shop = Dever::db('shop/info')->find($info['type_id']);
  909. list($data, $source) = $this->assign($goods, $info, $shop, true);
  910. if ($info) {
  911. $head = array('序号', '商品编号', '商品名称', '供货商', '订货总数量', '商品计量单位', '订货单总金额');
  912. $body = array();
  913. $i = 0;
  914. if ($data) {
  915. foreach ($data as $k => $v) {
  916. foreach ($v as $v1) {
  917. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v1['goods_id'], $v1['sku_id']);
  918. if (isset($goods_info['sku'])) {
  919. $sku = '[' . $goods_info['sku']['string'] . ']';
  920. } else {
  921. $sku = '';
  922. }
  923. $d = array
  924. (
  925. $i+1,
  926. $goods_info['code'],
  927. $goods_info['name'] . $sku,
  928. $this->getSelect($source, $v1['source_id'], $v1['order_goods_id']),
  929. $v1['num'],
  930. $goods_info['unit'],
  931. $v1['p_price'] * $v1['num'],
  932. );
  933. $body[] = $d;
  934. $i++;
  935. }
  936. }
  937. }
  938. if ($goods) {
  939. foreach ($goods as $k => $v) {
  940. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  941. if (isset($goods_info['sku'])) {
  942. $sku = '[' . $goods_info['sku']['string'] . ']';
  943. } else {
  944. $sku = '';
  945. }
  946. $d = array
  947. (
  948. $i+1,
  949. $goods_info['code'],
  950. $goods_info['name'] . $sku,
  951. $this->getSelect($source, false, $v['id']),
  952. $v['num'],
  953. $goods_info['unit'],
  954. $v['p_price'] * $v['num'],
  955. );
  956. $body[] = $d;
  957. $i++;
  958. }
  959. }
  960. $btn = array();
  961. $btn[] = array(
  962. 'type' => 'click',
  963. 'content' => 'setAudit()',
  964. 'name' => '全部调整<script>function setAudit(){layer.confirm(\'确定进行此项操作吗?\', function(){var audit = $(\'.audit_set\').val();$(\'.audit\').each(function(){$(this).val(audit);layui.form.render();layer.closeAll()})})}</script>'
  965. );
  966. $result['商品拆单'] = array
  967. (
  968. 'type' => 'table',
  969. 'head' => array('name' => '供货商:' . $this->getSelect($source, false, false), 'btn' => $btn),
  970. 'content' => array
  971. (
  972. 'head' => $head,
  973. 'body' => $body,
  974. )
  975. );
  976. $url = Dever::url('lib/buy.audit_set_commit?json=1&order_id=' . $info['id'], 'mshop');
  977. $result['审核结果'] = array
  978. (
  979. 'type' => 'html',
  980. 'content' => '<script>function saveAudit(){var goods = [];var audit = 1; var audit_text = $(".update_audit_text").val();$(".update_audit").each(function(){if ($(this).get(0).checked){audit = $(this).val()}});$(\'.audit\').each(function(){var v = $(this).val();var id = $(this).attr("id");goods.push(v + "_" + id);});layer.confirm(\'确定进行此项操作吗?\', function(){layui.form.render();var send = {};send.goods = goods.join(",");send.audit = audit;send.audit_text = audit_text;console.info(send);$.post("'.$url.'", send, function(t){t = JSON.parse(t);showYes(t)})})}</script><style>.layui-form-radio{padding-right: 0px;margin: 0px 0px 0 0;margin-left:40px}</style><div class="layui-form-item show_input"><div classs="layui-col-lg6"><div><label style="font-size:14px;font-weight:bold;" class="">审核状态</label><div style="display:inline" class="am-btn-group"><input update_type="checked" class="checkbox-checkall-0_update_audit update_audit update_value validate[required]" type="radio" name="update_audit[]" value="2" checked="" show_id="audit" autocomplete="off" id="form-validation-field-1"> 审核通过 &nbsp;&nbsp;<input update_type="checked" class="autoload checkbox-checkall-0_update_audit update_audit update_value validate[required]" type="radio" name="update_audit[]" value="3" autocomplete="off" id="form-validation-field-0"> 审核未通过 &nbsp;&nbsp;</div></div></div></div><div class="layui-form-item" style="display: block;"><div classs="layui-col-lg6"><div><label style="font-size:14px;font-weight:bold;" class="">审核备注</label></div><div><textarea style="height:100px;" update_type="value" class="update_value update_audit_text form-control layui-input validate[required]" name="update_desc"></textarea></div></div></div> <div style="text-align:left" class="layui-layer-btn layui-layer-btn-"><a class="layui-btn layui-btn-primary" onclick="saveAudit()">保存</a><a class="layui-layer-btn1" style="display:none">取消</a></div>',
  981. );
  982. }
  983. }
  984. private function show_refund($info, &$result, $type)
  985. {
  986. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'type' => 1));
  987. $url = Dever::url('lib/buy.refund_commit?json=1', 'mshop');
  988. if (!$info['refund_cash']) {
  989. $info['refund_cash'] = 0;
  990. }
  991. if ($info['refund_cash'] > 0 && $tk) {
  992. if ($type == 2) {
  993. $info['refund_cash'] = $info['refund_p_cash'];
  994. }
  995. if ($tk['process'] == 1) {
  996. $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>';
  997. } else {
  998. $tk_process = Dever::db('shop/buy_order_refund')->config['process'];
  999. $process = $tk_process[$tk['process']];
  1000. }
  1001. $desc = array();
  1002. $desc['退款原因'] = $tk['desc'];
  1003. $desc['退款图片'] = '暂无';
  1004. if ($tk['pic']) {
  1005. $pic = explode(',', $tk['pic']);
  1006. $tk['pic'] = '';
  1007. foreach ($pic as $k => $v) {
  1008. $tk['pic'] .= '<a href="'.$v.'" target="_blank"><img src="'.$v.'" width="100"/></a>';
  1009. }
  1010. }
  1011. ;
  1012. $tk['cdate'] = date('Y-m-d H:i', $tk['cdate']);
  1013. $result['退款信息'] = array
  1014. (
  1015. 'type' => 'info',
  1016. 'content' => array
  1017. (
  1018. array
  1019. (
  1020. array('退款金额', $info['refund_cash']),
  1021. array('数量', $info['num']),
  1022. array('退款进度', $process),
  1023. ),
  1024. array
  1025. (
  1026. array('申请时间', $tk['cdate']),
  1027. array('申请原因', $tk['desc']),
  1028. array('图片', $tk['pic']),
  1029. ),
  1030. ),
  1031. );
  1032. }
  1033. }
  1034. # 获取配送信息
  1035. private function show_ps($info, &$result)
  1036. {
  1037. $info['ps_info'] = Dever::db('shop/buy_order_ps')->find(array('order_id' => $info['id']));
  1038. if ($info['ps_info']) {
  1039. $info['ps_info']['service'] = Dever::array_decode($info['ps_info']['service']);
  1040. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  1041. if ($info['ps_info']['ydate']) {
  1042. $info['ps_info']['ydate'] = date('Y-m-d H:i', $info['ps_info']['ydate']);
  1043. } else {
  1044. $info['ps_info']['ydate'] = '无';
  1045. }
  1046. $status = Dever::db('shop/buy_order_ps')->config['status'];
  1047. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  1048. $service = array();
  1049. if ($info['ps_info']['service']) {
  1050. if (isset($info['ps_info']['service']['serivce'])) {
  1051. $info['ps_info']['service'] = $info['ps_info']['service']['service'];
  1052. }
  1053. foreach ($info['ps_info']['service'] as $k => $v) {
  1054. if (isset($v['order_num']) && $v['order_num']) {
  1055. $info['ps_info']['service'][$k]['service_name'] = '供应商自送';
  1056. if (isset($v['service_id']) && $v['service_id'] > 0) {
  1057. $service_info = Dever::db('shop/service')->find($v['service_id']);
  1058. if ($service_info) {
  1059. $info['ps_info']['service'][$k]['service_name'] = $service_info['name'];
  1060. }
  1061. }
  1062. $service[] = array
  1063. (
  1064. array('名称', $info['ps_info']['service'][$k]['service_name']),
  1065. array('单号', isset($v['order_num']) ? $v['order_num'] : ''),
  1066. );
  1067. }
  1068. }
  1069. }
  1070. $result['物流信息'] = array
  1071. (
  1072. 'type' => 'info',
  1073. 'content' => array
  1074. (
  1075. array
  1076. (
  1077. array('发货时间', $info['ps_info']['cdate']),
  1078. array('收货时间', $info['ps_info']['ydate']),
  1079. array('查单', '<a href="https://www.kuaidi100.com/?from=openv" target="_blank">点此手动查单</a>'),
  1080. ),
  1081. ),
  1082. );
  1083. $result['物流信息']['content'] = array_merge($result['物流信息']['content'], $service);
  1084. }
  1085. }
  1086. # 获取商品清单
  1087. private function show_goods($info, &$result, $type, $noprice)
  1088. {
  1089. $body = array();
  1090. $body_total = array();
  1091. $body_total['price'] = 0;
  1092. $body_total['num'] = 0;
  1093. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $info['id']));
  1094. $goods_status = Dever::db('shop/buy_order_goods')->config['status'];
  1095. $tk_status = Dever::db('shop/buy_order_refund')->config['status'];
  1096. $refund_body = array();
  1097. $refund_body_total = array();
  1098. $refund_body_total['price'] = 0;
  1099. $refund_body_total['num'] = 0;
  1100. $url = Dever::url('lib/buy.refund_commit?json=1', 'mshop');
  1101. foreach ($goods as $k => $v) {
  1102. if ($type == 2) {
  1103. $v['price'] = $v['p_price'];
  1104. }
  1105. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  1106. $status = $goods_status[$v['status']];
  1107. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
  1108. if ($tk && $tk['process'] == 1) {
  1109. if ($type == 1) {
  1110. $status = '申请' . $tk_status[$tk['status']];
  1111. } else {
  1112. $status = '已上报';
  1113. }
  1114. if ($type == 1) {
  1115. $desc = array();
  1116. $desc['退款原因'] = $tk['desc'];
  1117. $desc['退款图片'] = '暂无';
  1118. if ($tk['pic']) {
  1119. $desc['退款图片'] = '';
  1120. $pic = explode(',', $tk['pic']);
  1121. foreach ($pic as $k1 => $v1) {
  1122. $desc['退款图片'] .= '<a href="'.$v1.'" target="_blank"><img src="'.$v1.'" width="100"/></a>';
  1123. }
  1124. }
  1125. $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>';
  1126. $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>';
  1127. }
  1128. } elseif ($type > 1 && (!$tk || ($tk && $tk['process'] == 3))) {
  1129. //$tui = Dever::url('lib/buy.refund_apply_info_one_commit?json=1', 'mshop');
  1130. if ($type == 2) {
  1131. $tui = Dever::url('order_tui?order_goods_id=' . $v['id'] . '&order_id=' . $info['id'] . '&type=13&type_id=' . $info['source_id'], 'factory');
  1132. } else {
  1133. if ($info['source_type'] == 3) {
  1134. $tui = Dever::url('order_tui?order_goods_id=' . $v['id'] . '&order_id=' . $info['id'] . '&type=13&type_id=' . $info['source_id'], 'store');
  1135. } else {
  1136. $tui = Dever::url('order_tui?order_goods_id=' . $v['id'] . '&order_id=' . $info['id'] . '&type=12&type_id=' . $info['source_id'], 'store');
  1137. }
  1138. }
  1139. $info = $this->refund_button($info);
  1140. if ($info['refund_button'] == 1) {
  1141. if ($info['status'] == 3) {
  1142. $status = '<a href="javascript:;" onclick="fastEdit($(this), \''.$tui.'\', \'缺货退款\', \'\', 1)" class="layui-btn">缺货退款</a>';
  1143. } elseif ($info['status'] == 4) {
  1144. $status = '<a href="javascript:;" onclick="fastEdit($(this), \''.$tui.'\', \'报损退款\', \'\', 1)" class="layui-btn">报损退款</a>';
  1145. }
  1146. }
  1147. }
  1148. if (isset($goods_info['sku'])) {
  1149. $sku = '[' . $goods_info['sku']['string'] . ']';
  1150. } else {
  1151. $sku = '';
  1152. }
  1153. $d = array
  1154. (
  1155. 'pic' => $goods_info['cover'],
  1156. 'name' => $goods_info['name'],
  1157. 'sku' => $sku,
  1158. 'price' => $v['price'],
  1159. 'num' => $v['num'] . $goods_info['unit'],
  1160. 'status' => $status,
  1161. );
  1162. if ($goods_info['price_type'] > 2) {
  1163. $d['goods'] = $goods_info['goods'];
  1164. }
  1165. if ($tk && $v['status'] == 3 && $tk['status'] != 3) {
  1166. $d['status'] = $tk_status[$tk['status']];
  1167. if ($noprice == 1) {
  1168. unset($d['price']);
  1169. }
  1170. $refund_body[] = $d;
  1171. $price = $v['price']*$v['num'];
  1172. $refund_body_total['price'] += $price;
  1173. $refund_body_total['num'] += $v['num'];
  1174. } else {
  1175. if ($noprice == 1) {
  1176. unset($d['price']);
  1177. }
  1178. $body[] = $d;
  1179. $price = $v['price']*$v['num'];
  1180. $body_total['price'] += $price;
  1181. $body_total['num'] += $v['num'];
  1182. }
  1183. }
  1184. if ($body) {
  1185. if ($refund_body_total['price']) {
  1186. $body_total['set_price'] = $body_total['price'] - $refund_body_total['price'];
  1187. }
  1188. if ($refund_body_total['num']) {
  1189. $body_total['set_num'] = $body_total['num'] - $refund_body_total['num'];
  1190. }
  1191. if ($noprice == 1) {
  1192. unset($body_total['price']);
  1193. unset($body_total['set_price']);
  1194. }
  1195. $result['商品清单'] = array
  1196. (
  1197. 'type' => 'list',
  1198. 'content' => $body,
  1199. 'total' => $body_total,
  1200. );
  1201. }
  1202. if ($refund_body) {
  1203. if ($noprice == 1) {
  1204. unset($refund_body_total['price']);
  1205. }
  1206. $result['缺货与报损商品'] = array
  1207. (
  1208. 'type' => 'list',
  1209. 'content' => $refund_body,
  1210. 'total' => $refund_body_total,
  1211. );
  1212. }
  1213. }
  1214. # 获取对账清单
  1215. private function show_dz($info, &$result, $noprice)
  1216. {
  1217. $cash_type = Dever::input('cash', 1);
  1218. $search_option_type = Dever::input('search_option_type');
  1219. if ($cash_type == 1) {
  1220. if ($search_option_type == 1) {
  1221. # 门店结算单
  1222. $cash_order = Dever::db('cash/order')->select(array('type' => $info['type'], 'type_id' => $info['type_id'], 'source_order_id' => $info['id']));
  1223. $col = 'cash';
  1224. } elseif ($search_option_type == 2) {
  1225. # 工厂结算单
  1226. $cash_order = Dever::db('cash/order')->select(array('source_type' => $info['source_type'], 'source_id' => $info['source_id'], 'source_order_id' => $info['id']));
  1227. $col = 'p_cash';
  1228. } elseif ($search_option_type == 3) {
  1229. # 仓库结算单
  1230. $cash_order = Dever::db('cash/order')->select(array('source_type' => $info['source_type'], 'source_id' => $info['source_id'], 'source_order_id' => $info['id']));
  1231. $col = 'num';
  1232. }
  1233. }
  1234. if (isset($cash_order) && $cash_order) {
  1235. $head = array('序号', '结算单号', '数量', '金额', '结算类型', '入账状态', '审核状态', '结算时间', '审核备注', '管理');
  1236. if ($search_option_type == 3 || $noprice == 1) {
  1237. unset($head[3]);
  1238. }
  1239. $body = array();
  1240. $config = Dever::db('cash/order')->config;
  1241. foreach ($cash_order as $k => $v) {
  1242. $cdate = date('Y-m-d H:i', $v['cdate']);
  1243. $fdate = $v['fdate'] ? date('Y-m-d H:i', $v['fdate']) : '';
  1244. $operdate = $v['operdate'] ? date('Y-m-d H:i', $v['operdate']) : '';
  1245. $cash = $v[$col];
  1246. $url = Dever::url('project/database/list?project=shop&table=buy_order_goods&order_id='.$v['source_order_id'].'&page_type=1', 'manage');
  1247. $v['source_order_num'] = '<a href="'.$url.'" style="color:blue">'.$v['source_order_num'].'</a>';
  1248. $m = '';
  1249. if ($v['audit'] == 1 && $v['audit_type'] == 2) {
  1250. $url = Dever::url('project/database/update?project=cash&table=order&where_id='.$v['id'].'&col=audit,desc', 'manage');
  1251. $m = '<a href="javascript:;" onclick="fastEdit($(this), \''.$url.'\', \'审核\', \'\', 1)" class="layui-btn">审核</a>';
  1252. }
  1253. $d = array
  1254. (
  1255. $k+1,
  1256. $v['order_num'],
  1257. $v['num'],
  1258. $cash,
  1259. $config['config_jstype'][$v['jstype']],
  1260. $config['config_status'][$v['status']],
  1261. $config['config_audit'][$v['audit']],
  1262. $operdate,
  1263. $v['desc'],
  1264. $m,
  1265. );
  1266. if ($search_option_type == 3 || $noprice == 1) {
  1267. unset($d[3]);
  1268. }
  1269. $body[] = $d;
  1270. }
  1271. $result['对账清单'] = array
  1272. (
  1273. 'type' => 'table',
  1274. 'content' => array
  1275. (
  1276. 'head' => $head,
  1277. 'body' => $body,
  1278. )
  1279. );
  1280. }
  1281. }
  1282. # 发货
  1283. /*
  1284. public function send_commit()
  1285. {
  1286. $order_id = Dever::input('order_id');
  1287. $order = Dever::db('shop/buy_order')->find($order_id);
  1288. if (!$order || ($order && $order['status'] != 3)) {
  1289. Dever::alert('当前订单状态无法发货');
  1290. }
  1291. $state = Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'status' => 4));
  1292. if ($state) {
  1293. $this->updatePs($order, 1);
  1294. }
  1295. return 'reload';
  1296. }*/
  1297. # 采购订单审核 拆单
  1298. /*
  1299. public function audit_commit()
  1300. {
  1301. $order_id = Dever::input('order_id');
  1302. $order = Dever::db('shop/buy_order')->find($order_id);
  1303. if (!$order || ($order && $order['status'] != 2)) {
  1304. Dever::alert('当前订单状态无法拆单');
  1305. }
  1306. if ($order['type'] != 1) {
  1307. Dever::alert('这不是门店的订单');
  1308. }
  1309. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $order_id));
  1310. $shop = Dever::db('shop/info')->find($order['type_id']);
  1311. # 获取门店分配的仓库和工厂
  1312. $shop_store = Dever::db('shop/store')->select(array('shop_id' => $shop['id']));
  1313. $store = array();
  1314. $distance = array();
  1315. # 按照距离排序
  1316. if ($shop_store) {
  1317. foreach ($shop_store as $k => $v) {
  1318. $info = Dever::db('store/info')->find($v['store_id']);
  1319. if ($info && $info['status'] == 1) {
  1320. $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
  1321. $store[] = $info;
  1322. }
  1323. }
  1324. if ($store) {
  1325. array_multisort($distance, SORT_ASC, SORT_NUMERIC, $store);
  1326. }
  1327. }
  1328. $shop_factory = Dever::db('shop/factory')->select(array('shop_id' => $shop['id']));
  1329. $factory = array();
  1330. $distance = array();
  1331. # 按照距离排序
  1332. if ($shop_factory) {
  1333. foreach ($shop_factory as $k => $v) {
  1334. $info = Dever::db('factory/info')->find($v['factory_id']);
  1335. if ($info && $info['status'] == 1) {
  1336. $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
  1337. $factory[] = $info;
  1338. }
  1339. }
  1340. if ($factory) {
  1341. array_multisort($distance, SORT_ASC, SORT_NUMERIC, $factory);
  1342. }
  1343. }
  1344. if (!$factory && !$store) {
  1345. Dever::alert('未分配仓库或者未分配工厂');
  1346. }
  1347. $this->order_num = 0;
  1348. $this->order_id = array();
  1349. $delete = array();
  1350. $yes = array();
  1351. foreach ($goods as $k => $v) {
  1352. # 开始找仓库
  1353. $find = $this->store($order['id'], $order['order_num'] . '_' . $this->order_num, $shop, $store, $v);
  1354. if (!$find[1]) {
  1355. # 仓库没有,找厂家
  1356. if ($factory) {
  1357. $find = $this->factory($order['id'], $order['order_num'] . '_' . $this->order_num, $shop, $factory, $v);
  1358. }
  1359. }
  1360. if ($find[1]) {
  1361. $delete[$v['id']] = $v;
  1362. } else {
  1363. $yes[$v['id']] = $v;
  1364. }
  1365. }
  1366. $num = count($this->order_id);
  1367. if ($num > 0) {
  1368. if ($num == 1) {
  1369. $key = array_keys($this->order_id);
  1370. $key = $key[0];
  1371. Dever::db('shop/buy_order')->delete($key);
  1372. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $key));
  1373. $p_price = 0;
  1374. foreach ($goods as $k => $v) {
  1375. $goods_info = Dever::db('shop/buy_order_goods')->find(array('order_id' => $order['id'], 'goods_id' => $v['goods_id']));
  1376. if ($goods_info) {
  1377. Dever::db('shop/buy_order_goods')->update(array('where_id' => $goods_info['id'], 'set_p_price' => $v['p_price']));
  1378. $p_price += $v['p_price'];
  1379. }
  1380. }
  1381. 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()));
  1382. Dever::db('shop/buy_order_goods')->delete(array('order_id' => $key));
  1383. $this->order_id[$order['id']] = array($this->order_id[$key][0], $this->order_id[$key][1]);
  1384. unset($this->order_id[$key]);
  1385. } else {
  1386. if (!$yes) {
  1387. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'state' => 2));
  1388. Dever::load('cash/lib/order')->up($order, 1, 3);
  1389. } else {
  1390. $price = 0;
  1391. $num = 0;
  1392. foreach ($delete as $k => $v) {
  1393. Dever::db('shop/buy_order_goods')->delete(array('id' => $v['id']));
  1394. $price += $v['price'];
  1395. $num += $v['num'];
  1396. }
  1397. $order['price'] = $order['price'] - $price;
  1398. $order['num'] = $order['num'] - $num;
  1399. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'price' => $order['price'], 'num' => $order['num']));
  1400. Dever::load('shop/lib/refund')->set('buy')->apply(1, $shop['id'], $order_id, false, 3, 0, '供货商库存不足');
  1401. Dever::load('cash/lib/order')->up($order, 1, 3);
  1402. }
  1403. }
  1404. } else {
  1405. # 没有找到,退款
  1406. Dever::alert('供货商库存不足,请联系供货商备货');
  1407. //Dever::load('shop/lib/refund')->set('buy')->apply(1, $shop['id'], $order_id, false, 3, 0, '供货商库存不足');
  1408. }
  1409. if ($this->order_id) {
  1410. foreach ($this->order_id as $k => $v) {
  1411. $this->sendMsg($v[0], $v[1], $k);
  1412. if ($k != $order['id']) {
  1413. $order_info = Dever::db('shop/buy_order')->find($k);
  1414. Dever::load('cash/lib/order')->up($order_info, 1, 1);
  1415. }
  1416. }
  1417. }
  1418. return 'ok';
  1419. }
  1420. */
  1421. # 采购订单审核 手动拆单
  1422. public function audit_set_commit_api()
  1423. {
  1424. Dever::config('base')->hook = true;
  1425. //{goods: "2_4_audit_615,2_4_audit_614,-1_-1_audit_617,-1_-1_audit_616,-1_-1_audit_613", audit: "1", audit_text: "1231"}
  1426. $id = Dever::input('order_id');
  1427. $goods = Dever::input('goods');
  1428. $audit = Dever::input('audit');
  1429. $audit_text = Dever::input('audit_text');
  1430. $info = Dever::db('shop/buy_order')->one($id);
  1431. if ($audit > 1 && $info && $info['status'] == 2) {
  1432. $shop = Dever::db('shop/info')->find($info['type_id']);
  1433. if ($audit == 2 && $goods) {
  1434. Dever::db('shop/buy_order')->update(array('where_id' => $info['id'], 'audit' => $audit, 'audit_desc' => $audit_text));
  1435. # 成功
  1436. if (strstr($info['order_num'], '_')) {
  1437. $temp = explode('_', $info['order_num']);
  1438. $info['order_num'] = $temp[0];
  1439. $info['order_index'] = $temp[1];
  1440. } else {
  1441. $info['order_index'] = 0;
  1442. }
  1443. $goods = explode(',', $goods);
  1444. $data = $other = array();
  1445. foreach ($goods as $k => $v) {
  1446. list($source_type, $source_id, $name, $goods_id) = explode('_', $v);
  1447. $gv = Dever::db('shop/buy_order_goods')->find($goods_id);
  1448. if ($source_id > 0) {
  1449. $key = $source_type . '_' . $source_id;
  1450. $data[$key][] = array
  1451. (
  1452. 'area' => $shop['area'],
  1453. 'order_id' => $info['id'],
  1454. 'order_type' => 1,
  1455. 'order_num' => $info['order_num'],
  1456. 'order_index' => $info['order_index'],
  1457. 'type' => 1,
  1458. 'type_id' => $shop['id'],
  1459. 'source_type' => $source_type,
  1460. 'source_id' => $source_id,
  1461. 'order_goods_id' => $gv['id'],
  1462. 'goods_id' => $gv['goods_id'],
  1463. 'sku_id' => $gv['sku_id'],
  1464. 'price' => $gv['price'],
  1465. 'p_price' => $gv['price'],
  1466. 'num' => $gv['num'],
  1467. );
  1468. } else {
  1469. $other[] = $gv;
  1470. }
  1471. }
  1472. $this->audit_action($data, $other, $info, $shop, 1);
  1473. } else {
  1474. # 退款
  1475. if ($goods) {
  1476. Dever::db('shop/buy_order')->update(array('where_id' => $info['id'], 'audit' => $audit, 'audit_desc' => $audit_text));
  1477. $shop = Dever::db('shop/info')->find($info['type_id']);
  1478. Dever::load('shop/lib/refund')->set('buy')->apply(1, $shop['id'], $id, false, 3, 0, '未通过审核');
  1479. } else {
  1480. Dever::db('shop/buy_order')->update(array('where_id' => $info['id'], 'audit' => $audit, 'audit_desc' => $audit_text, 'status' => 7));
  1481. }
  1482. }
  1483. }
  1484. return Dever::url('project/database/list&project=shop&table=buy_order&menu=shop&search_option_state=1&search_option_type=1&menu_id=84', 'manage');
  1485. }
  1486. # 采购订单审核 拆单
  1487. public function audit_commit()
  1488. {
  1489. $order_id = Dever::input('order_id');
  1490. $order = Dever::db('shop/buy_order')->find($order_id);
  1491. if (!$order || ($order && $order['status'] != 2)) {
  1492. Dever::alert('当前订单状态无法拆单');
  1493. }
  1494. if ($order['type'] != 1) {
  1495. Dever::alert('这不是门店的订单');
  1496. }
  1497. $order['type'] = 1;
  1498. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $order['id']));
  1499. $shop = Dever::db('shop/info')->find($order['type_id']);
  1500. $data = $this->assign($goods, $order, $shop);
  1501. $this->audit_action($data, $goods, $order, $shop);
  1502. return 'ok';
  1503. }
  1504. private function audit_action($data, $goods, $order, $shop, $state = 2)
  1505. {
  1506. $send = array();
  1507. if ($data) {
  1508. # 开始生成订单
  1509. $num = count($data);
  1510. if (!$goods && $num == 1) {
  1511. foreach ($data as $k => $v) {
  1512. $k = explode('_', $k);
  1513. $p_price = 0;
  1514. foreach ($v as $k1 => $v1) {
  1515. Dever::db('shop/buy_order_goods')->update(array('where_id' => $v1['order_goods_id'], 'set_p_price' => $v1['p_price']));
  1516. $p_price += ($v1['p_price'] * $v1['num']);
  1517. $send[$order['id']] = $v1;
  1518. }
  1519. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'status' => 3, 'source_type' => $k[0], 'source_id' => $k[1], 'p_price' => $p_price, 'operdate' => time()));
  1520. }
  1521. } elseif ($num >= 1) {
  1522. $i = $order['order_index'];
  1523. $price = 0;
  1524. $num = 0;
  1525. foreach ($data as $k => $v) {
  1526. foreach ($v as $k1 => $v1) {
  1527. $v1['order_num'] .= '_' . $i;
  1528. $state = $this->createOrder($v1);
  1529. $send[$state] = $v1;
  1530. Dever::db('shop/buy_order_goods')->delete(array('id' => $v1['order_goods_id'], 'clear' => true));
  1531. $price += ($v1['price'] * $v1['num']);
  1532. $num += $v1['num'];
  1533. }
  1534. $i++;
  1535. }
  1536. $price = $order['price'] - $price;
  1537. $num = $order['num'] - $num;
  1538. if ($num >= 0) {
  1539. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'price' => $price, 'num' => $num, 'audit' => 1, 'order_num' => $order['order_num'] . '_' . $i, 'clear' => true));
  1540. } else {
  1541. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'state' => 2));
  1542. Dever::load('cash/lib/order')->up($order, 1, 3);
  1543. }
  1544. }
  1545. } else {
  1546. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'audit' => 1));
  1547. if ($state == 1) {
  1548. Dever::alert('未选择供货商,该订单进入重新审核状态');
  1549. } else {
  1550. Dever::alert('供货商库存不足,请联系供货商备货,该订单进入重新审核状态');
  1551. }
  1552. }
  1553. if ($send) {
  1554. foreach ($send as $k => $v) {
  1555. $this->sendMsg($v['source_type'], $v['source_id'], $k);
  1556. if ($k != $order['id']) {
  1557. $order_info = Dever::db('shop/buy_order')->find(array('id' => $k, 'clear' => true));
  1558. Dever::load('cash/lib/order')->up($order_info, 1, 1);
  1559. }
  1560. }
  1561. }
  1562. if ($goods) {
  1563. $send = array();
  1564. foreach ($goods as $k => $v) {
  1565. $send[$v['order_id']] = $v;
  1566. }
  1567. if ($send) {
  1568. foreach ($send as $k => $v) {
  1569. $order_info = Dever::db('shop/buy_order')->find(array('id' => $k, 'clear' => true));
  1570. Dever::load('cash/lib/order')->up($order_info, 1, 1);
  1571. }
  1572. }
  1573. }
  1574. }
  1575. public function assign(&$goods, &$order, $shop, $state = false)
  1576. {
  1577. if (strstr($order['order_num'], '_')) {
  1578. $temp = explode('_', $order['order_num']);
  1579. $order['order_num'] = $temp[0];
  1580. $order['order_index'] = $temp[1];
  1581. } else {
  1582. $order['order_index'] = 0;
  1583. }
  1584. # 获取门店分配的仓库和工厂
  1585. $data = $source = array();
  1586. $source[] = array
  1587. (
  1588. 'type' => -1,
  1589. 'id' => -1,
  1590. 'name' => '未分配供货商',
  1591. );
  1592. $this->assign_store($data, $goods, $source, $order, $shop);
  1593. $this->assign_factory($data, $goods, $source, $order, $shop);
  1594. if ($state) {
  1595. return array($data, $source);
  1596. }
  1597. return $data;
  1598. }
  1599. private function assign_store(&$data, &$goods, &$source, $order, $shop)
  1600. {
  1601. $shop_store = Dever::db('shop/store')->select(array('shop_id' => $shop['id']));
  1602. $store = array();
  1603. $distance = array();
  1604. # 按照距离排序
  1605. if ($shop_store) {
  1606. foreach ($shop_store as $k => $v) {
  1607. $info = Dever::db('store/info')->find($v['store_id']);
  1608. if ($info && $info['status'] == 1) {
  1609. $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
  1610. $store[] = $info;
  1611. }
  1612. }
  1613. if ($store) {
  1614. array_multisort($distance, SORT_ASC, SORT_NUMERIC, $store);
  1615. foreach ($store as $k => $v) {
  1616. $store[$k]['type'] = 2;
  1617. $source[] = $store[$k];
  1618. if ($goods) {
  1619. foreach ($goods as $gk => $gv) {
  1620. $where = array();
  1621. $where['store_id'] = $v['id'];
  1622. $where['goods_id'] = $gv['goods_id'];
  1623. $where['sku_id'] = ($gv['sku_id'] && $gv['sku_id'] > 0) ? $gv['sku_id'] : -1;
  1624. $sku = Dever::db('store/goods_sku')->getOne($where);
  1625. if ($sku) {
  1626. if ($gv['num'] <= $sku['total']) {
  1627. # 库存符合
  1628. $data['2_' . $v['id']][] = array
  1629. (
  1630. 'area' => $shop['area'],
  1631. 'order_id' => $order['id'],
  1632. 'order_type' => $order['type'],
  1633. 'order_num' => $order['order_num'],
  1634. 'order_index' => $order['order_index'],
  1635. 'type' => 1,
  1636. 'type_id' => $shop['id'],
  1637. 'source_type' => 2,
  1638. 'source_id' => $v['id'],
  1639. 'source_name' => $v['name'],
  1640. 'order_goods_id' => $gv['id'],
  1641. 'goods_id' => $where['goods_id'],
  1642. 'sku_id' => $where['sku_id'],
  1643. 'price' => $gv['price'],
  1644. 'p_price' => $gv['price'],
  1645. 'num' => $gv['num'],
  1646. );
  1647. unset($goods[$gk]);
  1648. }
  1649. }
  1650. }
  1651. }
  1652. }
  1653. }
  1654. return $store;
  1655. }
  1656. return false;
  1657. }
  1658. private function assign_factory(&$data, &$goods, &$source, $order, $shop)
  1659. {
  1660. $shop_factory = Dever::db('shop/factory')->select(array('shop_id' => $shop['id']));
  1661. $factory = array();
  1662. $distance = array();
  1663. # 按照距离排序
  1664. if ($shop_factory && $goods) {
  1665. foreach ($shop_factory as $k => $v) {
  1666. $info = Dever::db('factory/info')->find($v['factory_id']);
  1667. if ($info && $info['status'] == 1) {
  1668. $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
  1669. $factory[] = $info;
  1670. }
  1671. }
  1672. if ($factory) {
  1673. array_multisort($distance, SORT_ASC, SORT_NUMERIC, $factory);
  1674. foreach ($factory as $k => $v) {
  1675. $factory[$k]['type'] = 3;
  1676. $source[] = $factory[$k];
  1677. if ($goods) {
  1678. foreach ($goods as $gk => $gv) {
  1679. $where = array();
  1680. $where['factory_id'] = $v['id'];
  1681. $where['goods_id'] = $gv['goods_id'];
  1682. $where['sku_id'] = ($gv['sku_id'] && $gv['sku_id'] > 0) ? $gv['sku_id'] : -1;
  1683. $sku = Dever::db('factory/goods_sku')->getOne($where);
  1684. if ($sku) {
  1685. $data['3_' . $v['id']][] = array
  1686. (
  1687. 'area' => $shop['area'],
  1688. 'order_id' => $order['id'],
  1689. 'order_type' => $order['type'],
  1690. 'order_num' => $order['order_num'],
  1691. 'order_index' => $order['order_index'],
  1692. 'type' => 1,
  1693. 'type_id' => $shop['id'],
  1694. 'source_type' => 3,
  1695. 'source_id' => $v['id'],
  1696. 'source_name' => $v['name'],
  1697. 'order_goods_id' => $gv['id'],
  1698. 'goods_id' => $where['goods_id'],
  1699. 'sku_id' => $where['sku_id'],
  1700. 'price' => $gv['price'],
  1701. 'p_price' => $sku['p_price'],
  1702. 'num' => $gv['num'],
  1703. );
  1704. unset($goods[$gk]);
  1705. }
  1706. }
  1707. }
  1708. }
  1709. }
  1710. return $factory;
  1711. }
  1712. return false;
  1713. }
  1714. /*
  1715. private function store($order_id, $order_num, $shop, $store, $goods, $state = 2)
  1716. {
  1717. $find = array();
  1718. $type_id = array();
  1719. $id = array();
  1720. if (!$store) {
  1721. return array(2, $type_id, $id);
  1722. }
  1723. foreach ($store as $k => $v) {
  1724. $where['store_id'] = $v['id'];
  1725. $where['goods_id'] = $goods['goods_id'];
  1726. $where['sku_id'] = ($goods['sku_id'] && $goods['sku_id'] > 0) ? $goods['sku_id'] : -1;
  1727. $sku = Dever::db('store/goods_sku')->getOne($where);
  1728. if ($sku) {
  1729. if ($goods['num'] <= $sku['total']) {
  1730. # 如果库存符合,直接下单即可,生成仓库订单
  1731. $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']);
  1732. if ($state) {
  1733. $type_id[] = $where['store_id'];
  1734. $id[] = $state;
  1735. $this->order_id[$state] = array(2, $where['store_id']);
  1736. break;
  1737. }
  1738. } elseif ($state == 1 && $sku['total'] > 0) {
  1739. # 如果库存不够,从这个仓库扣下一部分,剩余的从另外仓库处理
  1740. $goods['num'] = $goods['num'] - $sku['total'];
  1741. # 生成仓库订单
  1742. $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']);
  1743. if (!$state) {
  1744. $goods['num'] = $goods['num'] + $sku['total'];
  1745. } else {
  1746. $type_id[] = $where['store_id'];
  1747. $id[] = $state;
  1748. $this->order_id[$state] = array(2, $where['store_id']);
  1749. }
  1750. continue;
  1751. } else {
  1752. # 直接切换到下一个仓库处理
  1753. continue;
  1754. }
  1755. }
  1756. }
  1757. return array(2, $type_id, $id);
  1758. }
  1759. private function factory($order_id, $order_num, $shop, $factory, $goods)
  1760. {
  1761. $find = array();
  1762. $type_id = array();
  1763. $id = array();
  1764. if (!$factory) {
  1765. return array(3, $type_id, $id);
  1766. }
  1767. foreach ($factory as $k => $v) {
  1768. $where['factory_id'] = $v['id'];
  1769. $where['goods_id'] = $goods['goods_id'];
  1770. $where['sku_id'] = ($goods['sku_id'] && $goods['sku_id'] > 0) ? $goods['sku_id'] : -1;
  1771. $sku = Dever::db('factory/goods_sku')->getOne($where);
  1772. if ($sku && $sku['p_price'] > 0) {
  1773. $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']);
  1774. if ($state) {
  1775. $type_id[] = $where['factory_id'];
  1776. $id[] = $state;
  1777. $this->order_id[$state] = array(3, $where['factory_id']);
  1778. break;
  1779. }
  1780. } else {
  1781. continue;
  1782. }
  1783. }
  1784. return array(3, $type_id, $id);
  1785. }
  1786. */
  1787. # 发送消息
  1788. public function sendMsg($source_type, $source_id, $id)
  1789. {
  1790. if (Dever::project('message')) {
  1791. $msg_param['type'] = 1;//消息类型1是订单消息
  1792. $msg_param['id'] = $id;
  1793. $msg_param['source_id'] = $source_id;
  1794. $msg_param['source_type'] = $source_type;
  1795. $msg_param = Dever::json_encode($msg_param);
  1796. $project = $source_type + 1;
  1797. $msg = '您有一个新的订货单,请及时处理。';
  1798. Dever::load('message/lib/data')->push(-1, $source_id, '订货通知', $msg, 2, $project, false, $msg_param);
  1799. }
  1800. }
  1801. # 生成拆单的订单
  1802. /*
  1803. private function createOrder($order_id, $order_num, $type, $type_id, $source_type, $source_id, $price, $p_price, $num, $goods_id, $sku_id)
  1804. {
  1805. $where['parent_order_id'] = $order_id;
  1806. $where['type'] = $type;
  1807. $where['type_id'] = $type_id;
  1808. $where['source_type'] = $source_type;
  1809. $where['source_id'] = $source_id;
  1810. $where['r'] = $goods_id . '_' . microtime() . '_' . mt_rand(1, 10000);
  1811. $info = Dever::db('shop/buy_order')->find($where);
  1812. if (!$info) {
  1813. $this->order_num++;
  1814. $data = $where;
  1815. $data['name'] = $order_id;
  1816. $data['order_num'] = $order_num;
  1817. $data['operdate'] = time();
  1818. $data['status'] = 3;
  1819. $data['price'] = $price * $num;
  1820. $data['p_price'] = $p_price * $num;
  1821. $data['num'] = $num;
  1822. $data['refund_cash'] = 0;
  1823. $data['refund_p_cash'] = 0;
  1824. $id = $state = Dever::db('shop/buy_order')->insert($data);
  1825. } else {
  1826. $id = $info['id'];
  1827. $data['where_id'] = $id;
  1828. $data['price'] = $info['price'] + ($price * $num);
  1829. $data['p_price'] = $info['p_price'] + ($p_price * $num);
  1830. $data['num'] = $info['num'] + $num;
  1831. $state = Dever::db('shop/buy_order')->update($data);
  1832. }
  1833. if ($state && $id > 0) {
  1834. $goods_info = Dever::db('shop/buy_order_goods')->find(array('order_id' => $id));
  1835. if (!$goods_info) {
  1836. $goods['order_id'] = $id;
  1837. $goods['goods_id'] = $goods_id;
  1838. $goods['sku_id'] = $sku_id;
  1839. $goods['price'] = $price;
  1840. $goods['p_price'] = $p_price;
  1841. $goods['num'] = $num;
  1842. Dever::db('shop/buy_order_goods')->insert($goods);
  1843. }
  1844. $state = $this->updateSell($source_type, $source_id, $goods_id, $sku_id, $num);
  1845. return $id;
  1846. }
  1847. return false;
  1848. }
  1849. */
  1850. public function createOrder($data, $order_cdate = false)
  1851. {
  1852. $where['parent_type'] = $data['order_type'];
  1853. $where['parent_order_id'] = $data['order_id'];
  1854. $where['type'] = $data['type'];
  1855. $where['type_id'] = $data['type_id'];
  1856. $where['source_type'] = $data['source_type'];
  1857. $where['source_id'] = $data['source_id'];
  1858. $where['clear'] = true;
  1859. $info = Dever::db('shop/buy_order')->find($where);
  1860. $goods_info = array();
  1861. $goods['goods_id'] = $data['goods_id'];
  1862. $goods['sku_id'] = $data['sku_id'];
  1863. $goods['clear'] = true;
  1864. if (!$info) {
  1865. $insert = $where;
  1866. if (isset($data['area'])) {
  1867. $insert['area'] = $data['area'] . ',' . $where['type_id'];
  1868. }
  1869. $insert['name'] = $data['order_id'];
  1870. $insert['order_num'] = $data['order_num'];
  1871. $insert['operdate'] = time();
  1872. $insert['status'] = 3;
  1873. $insert['price'] = $data['price'] * $data['num'];
  1874. $insert['p_price'] = $data['p_price'] * $data['num'];
  1875. $insert['num'] = $data['num'];
  1876. $insert['refund_cash'] = 0;
  1877. $insert['refund_p_cash'] = 0;
  1878. if ($order_cdate) {
  1879. $insert['cdate'] = $order_cdate;
  1880. }
  1881. $id = $state = Dever::db('shop/buy_order')->insert($insert);
  1882. if ($id) {
  1883. $goods['order_id'] = $id;
  1884. $goods_info = Dever::db('shop/buy_order_goods')->find($goods);
  1885. }
  1886. } else {
  1887. $id = $info['id'];
  1888. $goods['order_id'] = $id;
  1889. $goods_info = Dever::db('shop/buy_order_goods')->find($goods);
  1890. $state = false;
  1891. if (!$goods_info) {
  1892. $update['where_id'] = $id;
  1893. $update['price'] = $info['price'] + ($data['price'] * $data['num']);
  1894. $update['p_price'] = $info['p_price'] + ($data['p_price'] * $data['num']);
  1895. $update['num'] = $info['num'] + $data['num'];
  1896. $state = Dever::db('shop/buy_order')->update($update);
  1897. }
  1898. }
  1899. if ($state && $id > 0 && !$goods_info) {
  1900. $goods['price'] = $data['price'];
  1901. $goods['p_price'] = $data['p_price'];
  1902. $goods['num'] = $data['num'];
  1903. Dever::db('shop/buy_order_goods')->insert($goods);
  1904. $state = $this->updateSell($data['source_type'], $data['source_id'], $data['goods_id'], $data['sku_id'], $data['num']);
  1905. return $id;
  1906. }
  1907. return false;
  1908. }
  1909. # 减少库存,增加销量
  1910. public function updateSell($source_type, $source_id, $goods_id, $sku_id, $num)
  1911. {
  1912. if ($source_type == 2) {
  1913. $data['store_id'] = $source_id;
  1914. $data['goods_id'] = $goods_id;
  1915. $data['sku_id'] = $sku_id;
  1916. $data['num'] = $num;
  1917. return Dever::load('store/lib/goods')->oper($source_id, 1, 2, array($data));
  1918. } else {
  1919. $up = array();
  1920. $up['where_factory_id'] = $source_id;
  1921. $up['where_goods_id'] = $goods_id;
  1922. $up['sell_num'] = $num;
  1923. $state = Dever::db('factory/goods')->incSell($up);
  1924. if ($state) {
  1925. if ($sku_id > 0) {
  1926. } else {
  1927. $sku_id = -1;
  1928. }
  1929. $upSku = $up;
  1930. $upSku['where_sku_id'] = $sku_id;
  1931. $state = Dever::db('factory/goods_sku')->incSell($upSku);
  1932. }
  1933. return $state;
  1934. }
  1935. }
  1936. # 打印订单单
  1937. public function printer($user)
  1938. {
  1939. $id = Dever::input('id');
  1940. $type = Dever::input('type', 1);
  1941. $factory_config = Dever::db('main/factory_config')->find();
  1942. $main_config = Dever::db('main/config')->find();
  1943. $config = Dever::db('shop/buy_order')->config;
  1944. $info = Dever::db('shop/buy_order')->one($id);
  1945. //$source_info = Dever::db('factory/info')->find($info['source_id']);
  1946. if ($type == 2) {
  1947. # 仓库
  1948. $member = Dever::db('store/member')->find($user['id']);
  1949. $name = '仓库名称';
  1950. } else {
  1951. # 工厂
  1952. $member = Dever::db('factory/member')->find($user['id']);
  1953. $name = '工厂名称';
  1954. }
  1955. if ($info['type'] == 1) {
  1956. $type_info = Dever::db('shop/info')->find($info['type_id']);
  1957. } elseif ($info['type'] == 2) {
  1958. $type_info = Dever::db('store/info')->find($info['type_id']);
  1959. } elseif ($info['type'] == 3) {
  1960. $type_info = Dever::db('factory/info')->find($info['type_id']);
  1961. }
  1962. if ($info['source_type'] == 1) {
  1963. $source_info = Dever::db('shop/info')->find($info['source_id']);
  1964. } elseif ($info['source_type'] == 2) {
  1965. $source_info = Dever::db('store/info')->find($info['source_id']);
  1966. } elseif ($info['source_type'] == 3) {
  1967. $source_info = Dever::db('factory/info')->find($info['source_id']);
  1968. }
  1969. $cdate = date('Y-m-d H:i', $info['cdate']);
  1970. if ($info['operdate']) {
  1971. $operdate = date('Y-m-d H:i', $info['operdate']);
  1972. } else {
  1973. $operdate = '';
  1974. }
  1975. if ($info['fdate']) {
  1976. $fdate = date('Y-m-d H:i', $info['fdate']);
  1977. } else {
  1978. $fdate = '';
  1979. }
  1980. $status = $config['status'][$info['status']];
  1981. $info['ps_info'] = Dever::db('shop/buy_order_ps')->find(array('order_id' => $info['id']));
  1982. if ($info['ps_info']) {
  1983. $info['ps_info']['service'] = Dever::array_decode($info['ps_info']['service']);
  1984. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  1985. if ($info['ps_info']['ydate']) {
  1986. $info['ps_info']['ydate'] = date('Y-m-d H:i', $info['ps_info']['ydate']);
  1987. } else {
  1988. $info['ps_info']['ydate'] = '无';
  1989. }
  1990. $ps_status = Dever::db('shop/buy_order_ps')->config['status'];
  1991. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  1992. }
  1993. $pdf = Dever::load('pdf/lib/base')->init();
  1994. $pdf->hr('-', $main_config['name']);
  1995. $pdf->br()->font(20)->center('订货单号:' . $info['order_num']);
  1996. $pdf->font(10);
  1997. $pdf->br();
  1998. if ($type == 2 && $info['type'] == 2 && $info['source_type'] != 2) {
  1999. $pdf->br()->left($name . ':' . $type_info['name'], 80)->left('制单人:' . $member['name'], 60)->left('制单时间:' . date('Y-m-d H:i'), 40);
  2000. $pdf->hr();
  2001. $pdf->br()->left('订单状态:' . $status, 140)->left('订货时间:' . $operdate, 40);
  2002. $pdf->hr();
  2003. if (!$info['ps_info']) {
  2004. $pdf->left('发货供应商:' . $source_info['name']);
  2005. } else {
  2006. //$pdf->br()->left('发货供应商:' . $source_info['name'], 140)->left('发货时间:' . $info['ps_info']['cdate'], 40);
  2007. $pdf->br()->left('发货供应商:' . $source_info['name'], 80)->left('发货时间:' . $info['ps_info']['cdate'], 60)->left('收货时间:' . $info['ps_info']['ydate'], 40);
  2008. }
  2009. if ($info['ps_info'] && $info['ps_info']['service']) {
  2010. if (isset($info['ps_info']['service']['serivce'])) {
  2011. $info['ps_info']['service'] = $info['ps_info']['service']['service'];
  2012. }
  2013. foreach ($info['ps_info']['service'] as $k => $v) {
  2014. if (isset($v['order_num']) && $v['order_num']) {
  2015. $info['ps_info']['service'][$k]['service_name'] = '供应商自送';
  2016. if (isset($v['service_id']) && $v['service_id'] > 0) {
  2017. $service = Dever::db('shop/service')->find($v['service_id']);
  2018. if ($service) {
  2019. $info['ps_info']['service'][$k]['service_name'] = $service['name'];
  2020. }
  2021. }
  2022. $pdf->br()->left('物流信息:' . $info['ps_info']['service'][$k]['service_name'], 80)->left('物流单号:' . $info['ps_info']['service'][$k]['order_num'], 40);
  2023. }
  2024. }
  2025. }
  2026. $pdf->hr();
  2027. } else {
  2028. $pdf->br()->left($name . ':' . $source_info['name'], 80)->left('制单人:' . $member['name'], 60)->left('制单时间:' . date('Y-m-d H:i'), 40);
  2029. $pdf->hr();
  2030. $pdf->br()->left('订单状态:' . $status, 80)->left('订货时间:' . $operdate, 80);
  2031. $pdf->hr();
  2032. $parent_order = array();
  2033. if ($info['parent_type'] == 2) {
  2034. $parent_order = Dever::db('shop/sell_order')->find($info['parent_order_id']);
  2035. } elseif ($info['parent_type'] == 3) {
  2036. $parent_order = Dever::db('agent/dh_order')->find($info['parent_order_id']);
  2037. }
  2038. if ($parent_order && isset($parent_order['uid']) && $parent_order['uid']) {
  2039. if ($parent_order['address_id']) {
  2040. $address = Dever::load('passport/address')->getOne($parent_order['uid'], $parent_order['address_id']);
  2041. $pdf->br()->left('收件人:' . $address['contact'], 80)->left('收件人手机:' . $address['mobile'], 80);
  2042. $pdf->left('收件地址:' . $address['address_info'] . ',' . $address['house_number']);
  2043. } else {
  2044. $user = Dever::db('passport/user')->find($parent_order['uid']);
  2045. $pdf->br()->left('收件人:' . $user['username'], 80)->left('收件人手机:' . $user['mobile'], 80);
  2046. }
  2047. } elseif ($parent_order && isset($parent_order['mid']) && $parent_order['mid']) {
  2048. if ($parent_order['address_id']) {
  2049. $address = Dever::load('agent/address')->getOne($parent_order['mid'], $parent_order['address_id']);
  2050. $pdf->br()->left('收件人:' . $address['contact'], 80)->left('收件人手机:' . $address['mobile'], 80);
  2051. $pdf->left('收件地址:' . $address['address_info'] . ',' . $address['house_number']);
  2052. } else {
  2053. $user = Dever::db('agent/member')->find($parent_order['mid']);
  2054. $pdf->br()->left('收件人:' . $user['name'], 80)->left('收件人手机:' . $user['mobile'], 80);
  2055. }
  2056. } else {
  2057. $pdf->br()->left('配送至:' . $type_info['name'], 80)->left('联系方式:' . $type_info['truename'] . ',' . $type_info['mobile'], 80);
  2058. $pdf->left('配送地址:' . $type_info['address']);
  2059. }
  2060. $pdf->hr();
  2061. if ($info['ps_info'] && $info['ps_info']['service']) {
  2062. if (isset($info['ps_info']['service']['serivce'])) {
  2063. $info['ps_info']['service'] = $info['ps_info']['service']['service'];
  2064. }
  2065. $pdf->br()->left('发货时间:' . $info['ps_info']['cdate'], 80)->left('收货时间:' . $info['ps_info']['ydate'], 80);
  2066. foreach ($info['ps_info']['service'] as $k => $v) {
  2067. if (isset($v['order_num']) && $v['order_num']) {
  2068. $info['ps_info']['service'][$k]['service_name'] = '供应商自送';
  2069. if (isset($v['service_id']) && $v['service_id'] > 0) {
  2070. $service = Dever::db('shop/service')->find($v['service_id']);
  2071. if ($service) {
  2072. $info['ps_info']['service'][$k]['service_name'] = $service['name'];
  2073. }
  2074. }
  2075. $pdf->br()->left('物流信息:' . $info['ps_info']['service'][$k]['service_name'], 80)->left('物流单号:' . $info['ps_info']['service'][$k]['order_num'], 80);
  2076. }
  2077. }
  2078. /*
  2079. $pdf->br()->left('发货时间:' . $info['ps_info']['cdate'], 80)->left('配货员:' . $info['ps_info']['phy'], 70)->left('打包员:' . $info['ps_info']['dby'], 40);
  2080. if ($info['ps_info']['zl'] && $info['ps_info']['tj']) {
  2081. $pdf->br()->left('包裹重量:' . $info['ps_info']['zl'] . '克', 80)->left('包裹体积:' . $info['ps_info']['tj'] . '立方厘米', 70)->left('包裹数量:' . $info['ps_info']['num'], 40);
  2082. } elseif (!$info['ps_info']['zl'] && $info['ps_info']['tj']) {
  2083. $pdf->br()->left('包裹体积:' . $info['ps_info']['tj'] . '立方厘米', 150)->left('包裹数量:' . $info['ps_info']['num'], 40);
  2084. } elseif ($info['ps_info']['zl'] && !$info['ps_info']['tj']) {
  2085. $pdf->br()->left('包裹重量:' . $info['ps_info']['zl'] . '克', 150)->left('包裹数量:' . $info['ps_info']['num'], 40);
  2086. } else {
  2087. $pdf->left('包裹数量:' . $info['ps_info']['num']);
  2088. }
  2089. $pdf->br()->left('物流信息:' . $info['ps_info']['service_name'], 40);
  2090. if ($info['ps_info']['order_num']) {
  2091. $pdf->left('物流单号:' . $info['ps_info']['order_num'], 70);
  2092. }
  2093. if ($info['ps_info']['ydate']) {
  2094. $pdf->left('到货确认时间:' . $info['ps_info']['ydate']);
  2095. }
  2096. */
  2097. $pdf->hr();
  2098. }
  2099. }
  2100. $body = array();
  2101. $body_total = array();
  2102. $body_total['price'] = 0;
  2103. $body_total['num'] = 0;
  2104. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $info['id']));
  2105. $goods_status = Dever::db('shop/buy_order_goods')->config['status'];
  2106. $refund_body = array();
  2107. $refund_body_total = array();
  2108. $refund_body_total['price'] = 0;
  2109. $refund_body_total['num'] = 0;
  2110. foreach ($goods as $k => $v) {
  2111. if ($type == 1) {
  2112. $v['price'] = $v['p_price'];
  2113. }
  2114. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  2115. $status = $goods_status[$v['status']];
  2116. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
  2117. if (isset($goods_info['sku'])) {
  2118. $sku = $goods_info['sku']['string'];
  2119. } else {
  2120. $sku = '无';
  2121. }
  2122. if ($goods_info['price_type'] > 2 && isset($goods_info['goods']) && is_array($goods_info['goods'])) {
  2123. foreach ($goods_info['goods'] as $v2) {
  2124. $p = $v2['price'];
  2125. if (is_numeric($v2['num']) && is_numeric($v2['price'])) {
  2126. $p = $v2['price']*$v2['num'];
  2127. }
  2128. $d = array
  2129. (
  2130. $v2['id'],
  2131. $v2['name'],
  2132. $sku,
  2133. $v2['price'],
  2134. 'x ' . $v2['num'],
  2135. );
  2136. if ($type == 2) {
  2137. unset($d[3]);
  2138. }
  2139. if ($tk && $v['status'] == 3 && $tk['status'] != 3) {
  2140. $refund_body[] = $d;
  2141. $price = $v['price']*$v['num'];
  2142. $refund_body_total['price'] += $price;
  2143. $refund_body_total['num'] += $v['num'];
  2144. } else {
  2145. $body[] = $d;
  2146. $price = $v['price']*$v['num'];
  2147. $body_total['price'] += $price;
  2148. $body_total['num'] += $v['num'];
  2149. }
  2150. }
  2151. } else {
  2152. $d = array
  2153. (
  2154. $goods_info['id'],
  2155. $goods_info['name'],
  2156. $sku,
  2157. $v['price'],
  2158. 'x ' . $v['num'],
  2159. );
  2160. if ($type == 2) {
  2161. unset($d[3]);
  2162. }
  2163. if ($tk && $v['status'] == 3 && $tk['status'] != 3) {
  2164. $refund_body[] = $d;
  2165. $price = $v['price']*$v['num'];
  2166. $refund_body_total['price'] += $price;
  2167. $refund_body_total['num'] += $v['num'];
  2168. } else {
  2169. $body[] = $d;
  2170. $price = $v['price']*$v['num'];
  2171. $body_total['price'] += $price;
  2172. $body_total['num'] += $v['num'];
  2173. }
  2174. }
  2175. }
  2176. $head = array(array('商品编号', 40), array('商品名称', 60), array('商品属性', 40), array('单价', 40), array('数量', 20));
  2177. if ($type == 2) {
  2178. unset($head[3]);
  2179. $head[1][1] = 70;
  2180. $head[2][1] = 60;
  2181. }
  2182. if ($body) {
  2183. $pdf->center('订单商品');
  2184. $pdf->br();
  2185. foreach ($head as $k => $v) {
  2186. $pdf->left($v[0], $v[1]);
  2187. }
  2188. foreach ($body as $k => $v) {
  2189. $pdf->br();
  2190. foreach ($head as $k1 => $v1) {
  2191. $pdf->left($v[$k1], $v1[1]);
  2192. }
  2193. }
  2194. $pdf->br();
  2195. if ($type == 2) {
  2196. $pdf->right('共'.$body_total['num'].'件商品');
  2197. } else {
  2198. $pdf->right('共'.$body_total['num'].'件商品,合计金额¥' . $body_total['price'] . '元');
  2199. }
  2200. $pdf->hr();
  2201. }
  2202. if ($refund_body) {
  2203. $pdf->center('缺货与报损商品');
  2204. $pdf->br();
  2205. foreach ($head as $k => $v) {
  2206. $pdf->left($v[0], $v[1]);
  2207. }
  2208. foreach ($refund_body as $k => $v) {
  2209. $pdf->br();
  2210. foreach ($head as $k1 => $v1) {
  2211. $pdf->left($v[$k1], $v1[1]);
  2212. }
  2213. }
  2214. $pdf->br();
  2215. if ($type == 2) {
  2216. $pdf->right('共'.$refund_body_total['num'].'件商品');
  2217. } else {
  2218. $pdf->right('共'.$refund_body_total['num'].'件商品,合计金额¥' . $refund_body_total['price'] . '元');
  2219. }
  2220. $pdf->hr();
  2221. }
  2222. $pdf->br(1);
  2223. $pdf->right('如遇任何问题请致电客服');
  2224. $pdf->br();
  2225. $pdf->font(20);
  2226. $pdf->left($main_config['name'], 160);
  2227. $pdf->font(10);
  2228. $pdf->right('电话:' . $factory_config['phone'], 30);
  2229. $pdf->br();
  2230. $pdf->left($main_config['site'], 160);
  2231. $pdf->font(10);
  2232. $pdf->right($main_config['worktime'], 30);
  2233. $pdf->out('订货单');
  2234. }
  2235. # 导出单个订单
  2236. public function excel_one_api()
  2237. {
  2238. $this->type = Dever::input('type');
  2239. $id = Dever::input('id');
  2240. $type_id = Dever::input('type_id');
  2241. $data = $this->getView($type_id, $id);
  2242. $this->excel(array($data));
  2243. }
  2244. # 导出订单
  2245. public function excel($data)
  2246. {
  2247. if (!$data) {
  2248. Dever::alert('无导出数据');
  2249. }
  2250. $file = '入库订单';
  2251. $type = Dever::input('search_option_type', $this->type);
  2252. $header = array('所属仓库', '订单号', '商品来源', '发货日期', '商品名称', '商品规格属性', '商品数量', '报损退款状态', '订单状态');
  2253. $table = 'store/info';
  2254. if ($type == 1) {
  2255. $header[0] = '所属门店';
  2256. $table = 'shop/info';
  2257. }
  2258. $body = array();
  2259. foreach ($data as $k => $v) {
  2260. if (!is_array($v)) {
  2261. continue;
  2262. }
  2263. foreach ($v['goods'] as $k1 => $v1) {
  2264. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v1['goods_id'], $v1['sku_id']);
  2265. if (isset($goods_info['sku'])) {
  2266. $sku = $goods_info['sku']['string'];
  2267. } else {
  2268. $sku = '';
  2269. }
  2270. if ($goods_info['price_type'] > 2 && isset($goods_info['goods']) && is_array($goods_info['goods'])) {
  2271. foreach ($goods_info['goods'] as $v2) {
  2272. $d = array
  2273. (
  2274. $v['type_info']['name'],
  2275. $v['order_num'],
  2276. $v['source_info']['name'],
  2277. $v['cdate'],
  2278. $v2['name'],
  2279. $sku,
  2280. $v2['num'],
  2281. $v1['status_name'],
  2282. $v['status_name'],
  2283. );
  2284. $body[] = $d;
  2285. }
  2286. } else {
  2287. $d = array
  2288. (
  2289. $v['type_info']['name'],
  2290. $v['order_num'],
  2291. $v['source_info']['name'],
  2292. $v['cdate'],
  2293. $goods_info['name'],
  2294. $sku,
  2295. $v1['num'],
  2296. $v1['status_name'],
  2297. $v['status_name'],
  2298. );
  2299. $body[] = $d;
  2300. }
  2301. }
  2302. }
  2303. Dever::excelExport($body, $header, $file);
  2304. }
  2305. }