Buy.php 83 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270
  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')->getOne($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')->getOne($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. if ($info['ps_info']['ydate']) {
  239. $info['ps_info']['ydate_day'] = date('Y-m-d', $info['ps_info']['ydate']);
  240. $info['ps_info']['ydate'] = date('Y-m-d H:i', $info['ps_info']['ydate']);
  241. }
  242. $status = Dever::db('shop/buy_order_ps')->config['status'];
  243. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  244. if ($info['ps_info']['service']) {
  245. foreach ($info['ps_info']['service'] as $k => $v) {
  246. $info['ps_info']['service'][$k]['service_name'] = '供应商自送';
  247. if ($v['service_id'] > 0) {
  248. $service = Dever::db('shop/service')->find($v['service_id']);
  249. $info['ps_info']['service'][$k]['service_name'] = $service['name'];
  250. }
  251. }
  252. }
  253. }
  254. return $info;
  255. }
  256. # 发起支付
  257. public function action($uid, $type, $type_id, $name, $num, $goods, $price, $info, $refer = '', $area = '')
  258. {
  259. $openid = false;
  260. if ($uid > 0) {
  261. $wechat = Dever::db('passport/wechat')->one(array('uid' => $uid, 'type' => 1, 'system_id' => 2));
  262. if (!$wechat) {
  263. Dever::alert('错误的用户信息');
  264. }
  265. $openid = $wechat['openid'];
  266. }
  267. $order_data['type'] = $type;
  268. $order_data['type_id'] = $type_id;
  269. if ($area) {
  270. $order_data['area'] = $area . ',' . $type_id;
  271. }
  272. $order_data['name'] = $name;
  273. $order_data['num'] = $num;
  274. $order_data['info'] = $info;
  275. $order_data['price'] = $price;
  276. $order_data['order_num'] = $this->getOrderId();
  277. $order_data['refund_cash'] = 0;
  278. $order_data['refund_p_cash'] = 0;
  279. $id = Dever::db('shop/buy_order')->insert($order_data);
  280. if (!$id) {
  281. Dever::alert('支付失败');
  282. }
  283. foreach($goods as $k => $v) {
  284. $data['order_id'] = $id;
  285. $data['goods_id'] = $v['id'];
  286. $data['sku_id'] = $v['sku_id'];
  287. $data['price'] = $v['price'];
  288. $data['num'] = $v['buy_num'];
  289. Dever::db('shop/buy_order_goods')->insert($data);
  290. }
  291. if ($price <= 0) {
  292. $param['pay_product_id'] = $id;
  293. $param['pay_order_id'] = $order_data['order_num'];
  294. $param['pay_status'] = 2;
  295. $param['pay_cash'] = $price;
  296. $param['pay_msg'] = '';
  297. $this->success($param);
  298. return array
  299. (
  300. 'order' => '',
  301. 'order_num' => $order_data['order_num'],
  302. 'order_id' => $id,
  303. 'type' => 'pay',
  304. );
  305. }
  306. $param = array
  307. (
  308. 'project_id' => 2,
  309. 'channel_id' => 1,
  310. 'system_source' => 5,
  311. 'account_id' => 7,
  312. 'uid' => $uid,
  313. 'name' => $order_data['name'],
  314. 'openid' => $openid,
  315. 'cash' => $price,
  316. 'product_id' => $id,
  317. 'order_id' => $order_data['order_num'],
  318. 'refer' => $refer,
  319. );
  320. $receipt = Dever::input('receipt');
  321. if ($receipt) {
  322. $param['receipt'] = $receipt;
  323. }
  324. $result = Dever::load('pay/api.pay', $param);
  325. //$result['type'] = 'test';
  326. //$result['order'] = '';
  327. $result['order_num'] = $order_data['order_num'];
  328. $result['order_id'] = $id;
  329. return $result;
  330. }
  331. # 再次发起支付
  332. public function raction($id, $refer = '')
  333. {
  334. $order = Dever::db('shop/buy_order')->find($id);
  335. if (!$order) {
  336. Dever::alert('订单信息错误');
  337. }
  338. if ($order['status'] > 1) {
  339. Dever::alert('订单已支付或已取消');
  340. }
  341. $param = array
  342. (
  343. 'project_id' => 2,
  344. 'channel_id' => 1,
  345. 'system_source' => 5,
  346. 'account_id' => 7,
  347. 'uid' => -1,
  348. 'name' => $order['name'],
  349. 'cash' => $order['price'],
  350. 'product_id' => $id,
  351. 'order_id' => $order['order_num'],
  352. 'refer' => $refer,
  353. );
  354. $receipt = Dever::input('receipt');
  355. if ($receipt) {
  356. $param['receipt'] = $receipt;
  357. }
  358. $result = Dever::load('pay/api.pay', $param);
  359. $result['order_num'] = $order['order_num'];
  360. $result['order_id'] = $id;
  361. return $result;
  362. }
  363. # 查询是否支付
  364. public function cpay($id, $system_source = 5, $refer = '')
  365. {
  366. $order = Dever::db('shop/buy_order')->find($id);
  367. if (!$order) {
  368. Dever::alert('订单信息错误');
  369. }
  370. $result['order_num'] = $order['order_num'];
  371. $result['order_id'] = $id;
  372. if ($order['status'] > 1 && $order['status'] <= 6) {
  373. # 已支付
  374. $result['status'] = 1;
  375. } else {
  376. # 待支付
  377. $result['status'] = 2;
  378. }
  379. return $result;
  380. }
  381. # 支付成功回调 安全加密 设置token
  382. public function success_secure_api_token()
  383. {
  384. $project_id = Dever::input('pay_project_id');
  385. $info = Dever::db('pay/project')->one($project_id);
  386. if ($info) {
  387. return $info['key'];
  388. }
  389. return 'shop_buy_dever_2020';
  390. }
  391. # 支付成功回调 安全加密
  392. public function success_secure_api($param = array())
  393. {
  394. $this->success($param);
  395. }
  396. # 支付成功回调
  397. public function success($param = array())
  398. {
  399. $send = $param ? $param : Dever::preInput('pay_');
  400. $product_id = $send['pay_product_id'];
  401. $order_id = $send['pay_order_id'];
  402. $status = $send['pay_status'];
  403. $cash = $send['pay_cash'];
  404. $msg = $send['pay_msg'];
  405. $order = Dever::db('shop/buy_order')->one(array('id' => $product_id, 'time' => time()));
  406. if ($order && $order['status'] == 1 && $status == 2) {
  407. $update['status'] = 2;
  408. $update['where_id'] = $order['id'];
  409. Dever::db('shop/buy_order')->update($update);
  410. # 生成结算单
  411. Dever::load('cash/lib/order')->up($order, 1, 1);
  412. }
  413. return 'ok';
  414. }
  415. # 生成订单号
  416. public function getOrderId()
  417. {
  418. $where['order_num'] = Dever::order('C');
  419. $state = Dever::db('shop/buy_order')->one($where);
  420. if (!$state) {
  421. return $where['order_num'];
  422. } else {
  423. return $this->getOrderId();
  424. }
  425. }
  426. # 添加配送信息
  427. public function updatePs($order, $status = 1)
  428. {
  429. $where['order_id'] = $order['id'];
  430. $data = Dever::db('shop/buy_order_ps')->find($where);
  431. if ($data) {
  432. $update['where_id'] = $data['id'];
  433. $update['status'] = $status;
  434. $update['ydate'] = time();
  435. Dever::db('shop/buy_order_ps')->update($update);
  436. }
  437. }
  438. # 确认收货
  439. public function finish($id, $order_id)
  440. {
  441. $data = $this->getView($id, $order_id, false);
  442. if ($data['status'] <= 4) {
  443. if ($data['refund_cash'] > 0) {
  444. $status = 6;
  445. } else {
  446. $status = 5;
  447. }
  448. $state = Dever::db('shop/buy_order')->update(array('where_id' => $data['id'], 'status' => $status, 'fdate' => time()));
  449. if ($state) {
  450. $this->updatePs($data, 3);
  451. # 增加库存
  452. $where['order_id'] = $data['id'];
  453. $where['status'] = 1;
  454. $goods = Dever::db('shop/buy_order_goods')->select($where);
  455. if ($goods) {
  456. Dever::load('shop/lib/goods')->oper($data, 1, 1, $goods);
  457. }
  458. # 生成结算单
  459. Dever::load('cash/lib/order')->up($data, 1, 2);
  460. }
  461. return 'ok';
  462. } else {
  463. Dever::alert('您没有权限');
  464. }
  465. }
  466. # 取消订单
  467. public function cancel($id, $order_id)
  468. {
  469. $data = $this->getView($id, $order_id, false);
  470. if ($data['status'] == 1) {
  471. $state = Dever::db('shop/buy_order')->update(array('where_id' => $data['id'], 'status' => 7, 'operdate' => time()));
  472. # 生成结算单
  473. if ($state) {
  474. //Dever::load('cash/lib/order')->up($data, 1, 3);
  475. }
  476. return 'ok';
  477. } else {
  478. Dever::alert('当前订单状态不允许取消');
  479. }
  480. }
  481. # 查看订单状态
  482. public function orderStatus($id)
  483. {
  484. $config = Dever::db('shop/buy_order')->config;
  485. $info = Dever::db('shop/buy_order')->one($id);
  486. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'process' => 1));
  487. $status = $config['status'][$info['status']];
  488. if ($tk) {
  489. $status = '申请退款中';
  490. }
  491. return $status;
  492. }
  493. # 后台审核退款
  494. public function refund_commit_api()
  495. {
  496. $id = Dever::input('id');
  497. $process = Dever::input('process');
  498. return Dever::load('shop/lib/refund')->set('buy')->action($id, $process, false);
  499. }
  500. # 货物未发出,仅退款
  501. public function refund_apply_commit_api()
  502. {
  503. $order_id = Dever::input('order_id');
  504. $type = Dever::input('type');
  505. $type_id = Dever::input('type_id');
  506. $desc = Dever::input('desc');
  507. $pic = Dever::input('pic');
  508. return Dever::load('shop/lib/refund')->set('buy')->apply($type, $type_id, $order_id, false, 3, 0, $desc, $pic, 2);
  509. }
  510. # 货物已收到 全部退款 报损 需要审核
  511. public function refund_apply_info_commit_api()
  512. {
  513. $order_id = Dever::input('order_id');
  514. $status = Dever::input('status');
  515. $desc = Dever::input('desc');
  516. $pic = Dever::input('pic');
  517. $type = Dever::input('type');
  518. $type_id = Dever::input('type_id');
  519. return Dever::load('shop/lib/refund')->set('buy')->apply($type, $type_id, $order_id, false, $status, 0, $desc, $pic, 1);
  520. }
  521. # 部分退款信息 报损
  522. public function refund_apply_info_one_info()
  523. {
  524. $order_id = Dever::input('order_id');
  525. $order_goods_id = Dever::input('order_goods_id');
  526. $type = Dever::input('type');
  527. $type_id = Dever::input('type_id');
  528. return Dever::load('shop/lib/refund')->set('buy')->getInfo($type, $type_id, $order_id, $order_goods_id);
  529. }
  530. # 部分退款 报损
  531. public function refund_apply_info_one_commit_api()
  532. {
  533. $order_id = Dever::input('order_id');
  534. $order_goods_id = Dever::input('order_goods_id');
  535. $num = Dever::input('num', 0);
  536. $status = Dever::input('status');
  537. $desc = Dever::input('desc');
  538. $pic = Dever::input('pic');
  539. $type = Dever::input('type');
  540. $type_id = Dever::input('type_id');
  541. return Dever::load('shop/lib/refund')->set('buy')->apply($type, $type_id, $order_id, $order_goods_id, $status, $num, $desc, $pic, 1);
  542. }
  543. # 展示订单详情
  544. public function show()
  545. {
  546. $id = Dever::input('order_id');
  547. $type = 1;
  548. $type = Dever::input('search_option_type', $type);
  549. $type = Dever::input('type', $type);
  550. $noprice = Dever::input('noprice', 2);
  551. if ($type == 3) {
  552. $noprice = 1;
  553. }
  554. $cash_type = Dever::input('cash', 1);
  555. $config = Dever::db('shop/buy_order')->config;
  556. $info = Dever::db('shop/buy_order')->one($id);
  557. $tk_status = Dever::db('shop/buy_order_refund')->config['status'];
  558. $tk_process = Dever::db('shop/buy_order_refund')->config['process'];
  559. $status = $config['status'][$info['status']];
  560. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'process' => 1));
  561. $cdate = date('Y-m-d H:i', $info['cdate']);
  562. if ($info['operdate']) {
  563. $operdate = date('Y-m-d H:i', $info['operdate']);
  564. } else {
  565. $operdate = '';
  566. }
  567. if ($info['fdate']) {
  568. $fdate = date('Y-m-d H:i', $info['fdate']);
  569. } else {
  570. $fdate = '';
  571. }
  572. if ($type == 2 || $info['type'] == 2) {
  573. $info['price'] = $info['p_price'];
  574. }
  575. if ($tk && $tk['process'] == 1) {
  576. $status = '申请' . $tk_status[$tk['status']];
  577. }
  578. $time = array();
  579. if ($type == 1) {
  580. $time[] = array('下单时间', $cdate);
  581. $time[] = array('订货时间', $operdate);
  582. } else {
  583. $time[] = array('订货时间', $operdate);
  584. }
  585. if ($fdate) {
  586. $time[] = array('完成时间', $fdate);
  587. }
  588. if ($info['type'] == 1) {
  589. $type_info = Dever::db('shop/info')->find($info['type_id']);
  590. } elseif ($info['type'] == 2) {
  591. $type_info = Dever::db('store/info')->find($info['type_id']);
  592. } elseif ($info['type'] == 3) {
  593. $type_info = Dever::db('store/info')->find($info['type_id']);
  594. }
  595. $result = array();
  596. $result[$info['order_num']] = array
  597. (
  598. 'type' => 'info',
  599. 'content' => array
  600. (
  601. array
  602. (
  603. array('订单状态', $status),
  604. ),
  605. $time,
  606. )
  607. );
  608. $parent_order = array();
  609. if ($info['parent_type'] == 2) {
  610. $parent_order = Dever::db('shop/sell_order')->find($info['parent_order_id']);
  611. }
  612. if ($parent_order && $parent_order['uid']) {
  613. if ($parent_order['address_id']) {
  614. $address = Dever::load('passport/address')->getOne($parent_order['uid'], $parent_order['address_id']);
  615. $result[$info['order_num']]['content'][] = array
  616. (
  617. array('收件人', $address['contact']),
  618. array('收件人手机', $address['mobile']),
  619. );
  620. $result[$info['order_num']]['content'][] = array
  621. (
  622. array('收货地址', $address['address_info']),
  623. array('门牌号', $address['house_number']),
  624. );
  625. } else {
  626. $user = Dever::db('passport/user')->find($parent_order['uid']);
  627. $result[$info['order_num']]['content'][] = array
  628. (
  629. array('收件人', $user['username']),
  630. array('收件人手机', $user['mobile']),
  631. );
  632. }
  633. } else {
  634. $result[$info['order_num']]['content'][] = array
  635. (
  636. array('配送至', $type_info['name']),
  637. array('配送地址', $type_info['address']),
  638. );
  639. $result[$info['order_num']]['content'][] = array
  640. (
  641. array('联系人', $type_info['truename']),
  642. array('联系电话', $type_info['mobile']),
  643. array('订单备注', $info['info']),
  644. );
  645. }
  646. if ($type == 1 && $info['source_id'] > 0) {
  647. if ($info['source_type'] == 2) {
  648. # 仓库
  649. $source_info = Dever::db('store/info')->find($info['source_id']);
  650. } elseif ($info['source_type'] == 3) {
  651. # 工厂
  652. $source_info = Dever::db('factory/info')->find($info['source_id']);
  653. }
  654. $result[$info['order_num']]['content'][] = array
  655. (
  656. array('供应商', $source_info['name']),
  657. array('联系人', $source_info['truename']),
  658. array('联系电话', $source_info['mobile']),
  659. );
  660. }
  661. $button = array();
  662. if ($type == 2) {
  663. $config = Dever::load('factory/admin/auth.config');
  664. if ($info['status'] < 5) {
  665. $button[] = array
  666. (
  667. 'type' => 'link',
  668. 'link' => Dever::url('admin/order.print?id=' . $info['id'] . '&type=1', 'factory'),
  669. 'name' => '打印货单',
  670. );
  671. }
  672. if ($info['status'] == 3) {
  673. $button[] = array
  674. (
  675. 'type' => 'edit',
  676. 'link' => Dever::url('order_send?id=' . $info['id'], 'factory'),
  677. 'name' => '配送发货',
  678. );
  679. if ($parent_order && $parent_order['uid']) {
  680. } else {
  681. $button[] = array
  682. (
  683. 'type' => 'cancel',
  684. 'link' => Dever::url('lib/buy.refund_apply_commit?json=1&order_id=' . $info['id'] . '&type_id=' . $info['source_id'], 'mshop'),
  685. 'name' => '取消订单',
  686. );
  687. }
  688. }
  689. $config['phone'] = '您的专属客服:' . $config['kf_name'] . ',联系电话:' . $config['phone'];
  690. $button[] = array
  691. (
  692. 'type' => 'alert',
  693. 'content' => $config['phone'],
  694. 'name' => '联系平台',
  695. );
  696. } elseif ($type != 1 && $noprice == 1) {
  697. $config = Dever::load('factory/admin/auth.config');
  698. $button[] = array
  699. (
  700. 'type' => 'link',
  701. 'link' => Dever::url('admin/order.print?id=' . $info['id'] . '&type=2', 'store'),
  702. 'name' => '打印货单',
  703. );
  704. /*
  705. if ($info['type'] == 2 && $info['source_type'] != 2 && in_array($info['status'], array(5,6,8))) {
  706. $button[] = array
  707. (
  708. 'type' => 'link',
  709. 'link' => Dever::url('lib/buy.excel_one?type='.$info['type'].'&type_id='.$info['type_id'].'&id='.$info['id'], 'mshop'),
  710. 'name' => '导出货单',
  711. );
  712. }
  713. */
  714. $store_type = Dever::input('store_type');
  715. if ($info['status'] == 3 && $store_type == 1) {
  716. $button[] = array
  717. (
  718. 'type' => 'edit',
  719. 'link' => Dever::url('order_send?id=' . $info['id'] . '&type=2', 'store'),
  720. 'name' => '配送发货',
  721. );
  722. if ($parent_order && $parent_order['uid']) {
  723. } else {
  724. $button[] = array
  725. (
  726. 'type' => 'cancel',
  727. 'link' => Dever::url('lib/buy.refund_apply_commit?json=1&order_id=' . $info['id'] . '&type_id=' . $info['source_id'], 'mshop'),
  728. 'name' => '取消订单',
  729. );
  730. }
  731. }
  732. if ($info['status'] == 4 && $info['type'] == 2) {
  733. $button[] = array
  734. (
  735. 'type' => 'action',
  736. 'link' => Dever::url('admin/factory_order.action_commit?order_id='.$info['id'], 'store'),
  737. 'name' => '确认收货',
  738. );
  739. }
  740. $config['phone'] = '您的专属客服:' . $config['kf_name'] . ',联系电话:' . $config['phone'];
  741. $button[] = array
  742. (
  743. 'type' => 'alert',
  744. 'content' => $config['phone'],
  745. 'name' => '联系平台',
  746. );
  747. }
  748. $url = Dever::url('lib/buy.refund_commit?json=1', 'mshop');
  749. if (!$info['refund_cash']) {
  750. $info['refund_cash'] = 0;
  751. }
  752. if ($info['refund_cash'] > 0 && $tk) {
  753. if ($type == 2) {
  754. $info['refund_cash'] = $info['refund_p_cash'];
  755. }
  756. if ($tk['process'] == 1) {
  757. $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>';
  758. } else {
  759. $process = $tk_process[$tk['process']];
  760. }
  761. $desc = array();
  762. $desc['退款原因'] = $tk['desc'];
  763. $desc['退款图片'] = '暂无';
  764. if ($tk['pic']) {
  765. $pic = explode(',', $tk['pic']);
  766. $tk['pic'] = '';
  767. foreach ($pic as $k => $v) {
  768. $tk['pic'] .= '<a href="'.$v.'" target="_blank"><img src="'.$v.'" width="100"/></a>';
  769. }
  770. }
  771. ;
  772. $tk['cdate'] = date('Y-m-d H:i', $tk['cdate']);
  773. $result['退款信息'] = array
  774. (
  775. 'type' => 'info',
  776. 'content' => array
  777. (
  778. array
  779. (
  780. array('退款金额', $info['refund_cash']),
  781. array('数量', $info['num']),
  782. array('退款进度', $process),
  783. ),
  784. array
  785. (
  786. array('申请时间', $tk['cdate']),
  787. array('申请原因', $tk['desc']),
  788. array('图片', $tk['pic']),
  789. ),
  790. ),
  791. );
  792. }
  793. $info['ps_info'] = Dever::db('shop/buy_order_ps')->find(array('order_id' => $info['id']));
  794. if ($info['ps_info']) {
  795. $info['ps_info']['service'] = Dever::array_decode($info['ps_info']['service']);
  796. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  797. if ($info['ps_info']['ydate']) {
  798. $info['ps_info']['ydate'] = date('Y-m-d H:i', $info['ps_info']['ydate']);
  799. } else {
  800. $info['ps_info']['ydate'] = '无';
  801. }
  802. $status = Dever::db('shop/buy_order_ps')->config['status'];
  803. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  804. $service = array();
  805. if ($info['ps_info']['service']) {
  806. foreach ($info['ps_info']['service'] as $k => $v) {
  807. $info['ps_info']['service'][$k]['service_name'] = '供应商自送';
  808. if ($v['service_id'] > 0) {
  809. $service_info = Dever::db('shop/service')->find($v['service_id']);
  810. $info['ps_info']['service'][$k]['service_name'] = $service_info['name'];
  811. }
  812. $service[] = array
  813. (
  814. array('名称', $info['ps_info']['service'][$k]['service_name']),
  815. array('单号', $v['order_num']),
  816. );
  817. }
  818. }
  819. $result['物流信息'] = array
  820. (
  821. 'type' => 'info',
  822. 'content' => array
  823. (
  824. array
  825. (
  826. array('发货时间', $info['ps_info']['cdate']),
  827. array('收货时间', $info['ps_info']['ydate']),
  828. array('查单', '<a href="https://www.kuaidi100.com/?from=openv" target="_blank">点此手动查单</a>'),
  829. ),
  830. ),
  831. );
  832. $result['物流信息']['content'] = array_merge($result['物流信息']['content'], $service);
  833. /*
  834. if ($noprice == 1) {
  835. $result['物流信息'] = array
  836. (
  837. 'type' => 'info',
  838. 'content' => array
  839. (
  840. array
  841. (
  842. array('名称', $info['ps_info']['service_name']),
  843. array('单号', $info['ps_info']['order_num']),
  844. ),
  845. array
  846. (
  847. array('数量', $info['ps_info']['num']),
  848. array('重量', $info['ps_info']['zl']),
  849. array('体积', $info['ps_info']['tj']),
  850. ),
  851. array
  852. (
  853. array('发货时间', $info['ps_info']['cdate']),
  854. array('收货时间', $info['ps_info']['ydate']),
  855. ),
  856. ),
  857. );
  858. } else {
  859. $result['物流信息'] = array
  860. (
  861. 'type' => 'info',
  862. 'content' => array
  863. (
  864. array
  865. (
  866. array('名称', $info['ps_info']['service_name']),
  867. array('单号', $info['ps_info']['order_num']),
  868. array('费用', $info['ps_info']['price']),
  869. ),
  870. array
  871. (
  872. array('配货员', $info['ps_info']['phy']),
  873. array('打包员', $info['ps_info']['dby']),
  874. ),
  875. array
  876. (
  877. array('数量', $info['ps_info']['num']),
  878. array('重量', $info['ps_info']['zl']),
  879. array('体积', $info['ps_info']['tj']),
  880. ),
  881. array
  882. (
  883. array('发货时间', $info['ps_info']['cdate']),
  884. array('收货时间', $info['ps_info']['ydate']),
  885. ),
  886. ),
  887. );
  888. }
  889. */
  890. }
  891. $body = array();
  892. $body_total = array();
  893. $body_total['price'] = 0;
  894. $body_total['num'] = 0;
  895. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $info['id']));
  896. $goods_status = Dever::db('shop/buy_order_goods')->config['status'];
  897. $refund_body = array();
  898. $refund_body_total = array();
  899. $refund_body_total['price'] = 0;
  900. $refund_body_total['num'] = 0;
  901. foreach ($goods as $k => $v) {
  902. if ($type == 2) {
  903. $v['price'] = $v['p_price'];
  904. }
  905. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  906. $status = $goods_status[$v['status']];
  907. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
  908. if ($tk && $tk['process'] == 1) {
  909. if ($type == 1) {
  910. $status = '申请' . $tk_status[$tk['status']];
  911. } else {
  912. $status = '已上报';
  913. }
  914. if ($type == 1) {
  915. $desc = array();
  916. $desc['退款原因'] = $tk['desc'];
  917. $desc['退款图片'] = '暂无';
  918. if ($tk['pic']) {
  919. $desc['退款图片'] = '';
  920. $pic = explode(',', $tk['pic']);
  921. foreach ($pic as $k1 => $v1) {
  922. $desc['退款图片'] .= '<a href="'.$v1.'" target="_blank"><img src="'.$v1.'" width="100"/></a>';
  923. }
  924. }
  925. $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>';
  926. $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>';
  927. }
  928. } elseif ($type > 1 && (!$tk || ($tk && $tk['process'] == 3))) {
  929. //$tui = Dever::url('lib/buy.refund_apply_info_one_commit?json=1', 'mshop');
  930. if ($type == 2) {
  931. $tui = Dever::url('order_tui?order_goods_id=' . $v['id'] . '&order_id=' . $info['id'] . '&type=13&type_id=' . $info['source_id'], 'factory');
  932. } else {
  933. if ($info['source_type'] == 3) {
  934. $tui = Dever::url('order_tui?order_goods_id=' . $v['id'] . '&order_id=' . $info['id'] . '&type=13&type_id=' . $info['source_id'], 'store');
  935. } else {
  936. $tui = Dever::url('order_tui?order_goods_id=' . $v['id'] . '&order_id=' . $info['id'] . '&type=12&type_id=' . $info['source_id'], 'store');
  937. }
  938. }
  939. if ($info['status'] == 3) {
  940. $status = '<a href="javascript:;" onclick="fastEdit($(this), \''.$tui.'\', \'缺货退款\', \'\', 1)" class="layui-btn">缺货退款</a>';
  941. } elseif ($info['status'] == 4) {
  942. $status = '<a href="javascript:;" onclick="fastEdit($(this), \''.$tui.'\', \'报损退款\', \'\', 1)" class="layui-btn">报损退款</a>';
  943. }
  944. }
  945. if (isset($goods_info['sku'])) {
  946. $sku = '[' . $goods_info['sku']['string'] . ']';
  947. } else {
  948. $sku = '';
  949. }
  950. $d = array
  951. (
  952. 'pic' => $goods_info['cover'],
  953. 'name' => $goods_info['name'],
  954. 'sku' => $sku,
  955. 'price' => $v['price'],
  956. 'num' => $v['num'],
  957. 'status' => $status,
  958. );
  959. if ($tk && $v['status'] == 3 && $tk['status'] != 3) {
  960. $d['status'] = $tk_status[$tk['status']];
  961. if ($noprice == 1) {
  962. unset($d['price']);
  963. }
  964. $refund_body[] = $d;
  965. $price = $v['price']*$v['num'];
  966. $refund_body_total['price'] += $price;
  967. $refund_body_total['num'] += $v['num'];
  968. } else {
  969. if ($noprice == 1) {
  970. unset($d['price']);
  971. }
  972. $body[] = $d;
  973. $price = $v['price']*$v['num'];
  974. $body_total['price'] += $price;
  975. $body_total['num'] += $v['num'];
  976. }
  977. }
  978. if ($body) {
  979. if ($refund_body_total['price']) {
  980. $body_total['set_price'] = $body_total['price'] - $refund_body_total['price'];
  981. }
  982. if ($refund_body_total['num']) {
  983. $body_total['set_num'] = $body_total['num'] - $refund_body_total['num'];
  984. }
  985. if ($noprice == 1) {
  986. unset($body_total['price']);
  987. unset($body_total['set_price']);
  988. }
  989. $result['商品清单'] = array
  990. (
  991. 'type' => 'list',
  992. 'content' => $body,
  993. 'total' => $body_total,
  994. );
  995. }
  996. if ($refund_body) {
  997. if ($noprice == 1) {
  998. unset($refund_body_total['price']);
  999. }
  1000. $result['缺货与报损商品'] = array
  1001. (
  1002. 'type' => 'list',
  1003. 'content' => $refund_body,
  1004. 'total' => $refund_body_total,
  1005. );
  1006. }
  1007. $search_option_type = Dever::input('search_option_type');
  1008. if ($cash_type == 1) {
  1009. if ($search_option_type == 1) {
  1010. # 门店结算单
  1011. $cash_order = Dever::db('cash/order')->select(array('type' => $info['type'], 'type_id' => $info['type_id'], 'source_order_id' => $info['id']));
  1012. $col = 'cash';
  1013. } elseif ($search_option_type == 2) {
  1014. # 工厂结算单
  1015. $cash_order = Dever::db('cash/order')->select(array('source_type' => $info['source_type'], 'source_id' => $info['source_id'], 'source_order_id' => $info['id']));
  1016. $col = 'p_cash';
  1017. } elseif ($search_option_type == 3) {
  1018. # 仓库结算单
  1019. $cash_order = Dever::db('cash/order')->select(array('source_type' => $info['source_type'], 'source_id' => $info['source_id'], 'source_order_id' => $info['id']));
  1020. $col = 'num';
  1021. }
  1022. }
  1023. if (isset($cash_order) && $cash_order) {
  1024. $head = array('序号', '结算单号', '数量', '金额', '结算类型', '入账状态', '审核状态', '结算时间', '审核未通过原因', '管理');
  1025. if ($search_option_type == 3 || $noprice == 1) {
  1026. unset($head[3]);
  1027. }
  1028. $body = array();
  1029. $config = Dever::db('cash/order')->config;
  1030. foreach ($cash_order as $k => $v) {
  1031. $cdate = date('Y-m-d H:i', $v['cdate']);
  1032. $fdate = $v['fdate'] ? date('Y-m-d H:i', $v['fdate']) : '';
  1033. $operdate = $v['operdate'] ? date('Y-m-d H:i', $v['operdate']) : '';
  1034. $cash = $v[$col];
  1035. $url = Dever::url('project/database/list?project=shop&table=buy_order_goods&order_id='.$v['source_order_id'].'&page_type=1', 'manage');
  1036. $v['source_order_num'] = '<a href="'.$url.'" style="color:blue">'.$v['source_order_num'].'</a>';
  1037. $m = '';
  1038. if ($v['audit'] == 1 && $v['audit_type'] == 2) {
  1039. $url = Dever::url('project/database/update?project=cash&table=order&where_id='.$v['id'].'&col=audit,desc', 'manage');
  1040. $m = '<a href="javascript:;" onclick="fastEdit($(this), \''.$url.'\', \'审核\', \'\', 1)" class="layui-btn">审核</a>';
  1041. }
  1042. $d = array
  1043. (
  1044. $k+1,
  1045. $v['order_num'],
  1046. $v['num'],
  1047. $cash,
  1048. $config['config_jstype'][$v['jstype']],
  1049. $config['config_status'][$v['status']],
  1050. $config['config_audit'][$v['audit']],
  1051. $operdate,
  1052. $v['desc'],
  1053. $m,
  1054. );
  1055. if ($search_option_type == 3 || $noprice == 1) {
  1056. unset($d[3]);
  1057. }
  1058. $body[] = $d;
  1059. }
  1060. $result['对账清单'] = array
  1061. (
  1062. 'type' => 'table',
  1063. 'content' => array
  1064. (
  1065. 'head' => $head,
  1066. 'body' => $body,
  1067. )
  1068. );
  1069. }
  1070. $head = array
  1071. (
  1072. 'name' => '基本信息',
  1073. 'btn' => $button,
  1074. );
  1075. $html = Dever::show($head, $result);
  1076. return $html;
  1077. }
  1078. # 发货
  1079. /*
  1080. public function send_commit()
  1081. {
  1082. $order_id = Dever::input('order_id');
  1083. $order = Dever::db('shop/buy_order')->find($order_id);
  1084. if (!$order || ($order && $order['status'] != 3)) {
  1085. Dever::alert('当前订单状态无法发货');
  1086. }
  1087. $state = Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'status' => 4));
  1088. if ($state) {
  1089. $this->updatePs($order, 1);
  1090. }
  1091. return 'reload';
  1092. }*/
  1093. # 采购订单审核 拆单
  1094. /*
  1095. public function audit_commit()
  1096. {
  1097. $order_id = Dever::input('order_id');
  1098. $order = Dever::db('shop/buy_order')->find($order_id);
  1099. if (!$order || ($order && $order['status'] != 2)) {
  1100. Dever::alert('当前订单状态无法拆单');
  1101. }
  1102. if ($order['type'] != 1) {
  1103. Dever::alert('这不是门店的订单');
  1104. }
  1105. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $order_id));
  1106. $shop = Dever::db('shop/info')->find($order['type_id']);
  1107. # 获取门店分配的仓库和工厂
  1108. $shop_store = Dever::db('shop/store')->select(array('shop_id' => $shop['id']));
  1109. $store = array();
  1110. $distance = array();
  1111. # 按照距离排序
  1112. if ($shop_store) {
  1113. foreach ($shop_store as $k => $v) {
  1114. $info = Dever::db('store/info')->find($v['store_id']);
  1115. if ($info && $info['status'] == 1) {
  1116. $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
  1117. $store[] = $info;
  1118. }
  1119. }
  1120. if ($store) {
  1121. array_multisort($distance, SORT_ASC, SORT_NUMERIC, $store);
  1122. }
  1123. }
  1124. $shop_factory = Dever::db('shop/factory')->select(array('shop_id' => $shop['id']));
  1125. $factory = array();
  1126. $distance = array();
  1127. # 按照距离排序
  1128. if ($shop_factory) {
  1129. foreach ($shop_factory as $k => $v) {
  1130. $info = Dever::db('factory/info')->find($v['factory_id']);
  1131. if ($info && $info['status'] == 1) {
  1132. $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
  1133. $factory[] = $info;
  1134. }
  1135. }
  1136. if ($factory) {
  1137. array_multisort($distance, SORT_ASC, SORT_NUMERIC, $factory);
  1138. }
  1139. }
  1140. if (!$factory && !$store) {
  1141. Dever::alert('未分配仓库或者未分配工厂');
  1142. }
  1143. $this->order_num = 0;
  1144. $this->order_id = array();
  1145. $delete = array();
  1146. $yes = array();
  1147. foreach ($goods as $k => $v) {
  1148. # 开始找仓库
  1149. $find = $this->store($order['id'], $order['order_num'] . '_' . $this->order_num, $shop, $store, $v);
  1150. if (!$find[1]) {
  1151. # 仓库没有,找厂家
  1152. if ($factory) {
  1153. $find = $this->factory($order['id'], $order['order_num'] . '_' . $this->order_num, $shop, $factory, $v);
  1154. }
  1155. }
  1156. if ($find[1]) {
  1157. $delete[$v['id']] = $v;
  1158. } else {
  1159. $yes[$v['id']] = $v;
  1160. }
  1161. }
  1162. $num = count($this->order_id);
  1163. if ($num > 0) {
  1164. if ($num == 1) {
  1165. $key = array_keys($this->order_id);
  1166. $key = $key[0];
  1167. Dever::db('shop/buy_order')->delete($key);
  1168. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $key));
  1169. $p_price = 0;
  1170. foreach ($goods as $k => $v) {
  1171. $goods_info = Dever::db('shop/buy_order_goods')->find(array('order_id' => $order['id'], 'goods_id' => $v['goods_id']));
  1172. if ($goods_info) {
  1173. Dever::db('shop/buy_order_goods')->update(array('where_id' => $goods_info['id'], 'set_p_price' => $v['p_price']));
  1174. $p_price += $v['p_price'];
  1175. }
  1176. }
  1177. 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()));
  1178. Dever::db('shop/buy_order_goods')->delete(array('order_id' => $key));
  1179. $this->order_id[$order['id']] = array($this->order_id[$key][0], $this->order_id[$key][1]);
  1180. unset($this->order_id[$key]);
  1181. } else {
  1182. if (!$yes) {
  1183. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'state' => 2));
  1184. Dever::load('cash/lib/order')->up($order, 1, 3);
  1185. } else {
  1186. $price = 0;
  1187. $num = 0;
  1188. foreach ($delete as $k => $v) {
  1189. Dever::db('shop/buy_order_goods')->delete(array('id' => $v['id']));
  1190. $price += $v['price'];
  1191. $num += $v['num'];
  1192. }
  1193. $order['price'] = $order['price'] - $price;
  1194. $order['num'] = $order['num'] - $num;
  1195. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'price' => $order['price'], 'num' => $order['num']));
  1196. Dever::load('shop/lib/refund')->set('buy')->apply(1, $shop['id'], $order_id, false, 3, 0, '供货商库存不足');
  1197. Dever::load('cash/lib/order')->up($order, 1, 3);
  1198. }
  1199. }
  1200. } else {
  1201. # 没有找到,退款
  1202. Dever::alert('供货商库存不足,请联系供货商备货');
  1203. //Dever::load('shop/lib/refund')->set('buy')->apply(1, $shop['id'], $order_id, false, 3, 0, '供货商库存不足');
  1204. }
  1205. if ($this->order_id) {
  1206. foreach ($this->order_id as $k => $v) {
  1207. $this->sendMsg($v[0], $v[1], $k);
  1208. if ($k != $order['id']) {
  1209. $order_info = Dever::db('shop/buy_order')->find($k);
  1210. Dever::load('cash/lib/order')->up($order_info, 1, 1);
  1211. }
  1212. }
  1213. }
  1214. return 'ok';
  1215. }
  1216. */
  1217. # 采购订单审核 拆单
  1218. public function audit_commit()
  1219. {
  1220. $order_id = Dever::input('order_id');
  1221. $order = Dever::db('shop/buy_order')->find($order_id);
  1222. if (!$order || ($order && $order['status'] != 2)) {
  1223. Dever::alert('当前订单状态无法拆单');
  1224. }
  1225. if ($order['type'] != 1) {
  1226. Dever::alert('这不是门店的订单');
  1227. }
  1228. $order['type'] = 1;
  1229. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $order['id']));
  1230. $shop = Dever::db('shop/info')->find($order['type_id']);
  1231. $data = $this->assign($goods, $order, $shop);
  1232. $send = array();
  1233. if ($data) {
  1234. # 开始生成订单
  1235. $num = count($data);
  1236. if (!$goods && $num == 1) {
  1237. foreach ($data as $k => $v) {
  1238. $k = explode('_', $k);
  1239. $p_price = 0;
  1240. foreach ($v as $k1 => $v1) {
  1241. Dever::db('shop/buy_order_goods')->update(array('where_id' => $v1['order_goods_id'], 'set_p_price' => $v1['p_price']));
  1242. $p_price += $v1['p_price'];
  1243. $send[$order['id']] = $v1;
  1244. }
  1245. 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()));
  1246. }
  1247. } elseif ($num >= 1) {
  1248. $i = $order['order_index'];
  1249. $price = 0;
  1250. $num = 0;
  1251. foreach ($data as $k => $v) {
  1252. foreach ($v as $k1 => $v1) {
  1253. $v1['order_num'] .= '_' . $i;
  1254. $state = $this->createOrder($v1);
  1255. $send[$state] = $v1;
  1256. Dever::db('shop/buy_order_goods')->delete(array('id' => $v1['order_goods_id'], 'clear' => true));
  1257. $price += $v1['price'];
  1258. $num += $v1['num'];
  1259. }
  1260. $i++;
  1261. }
  1262. $order['price'] = $order['price'] - $price;
  1263. $order['num'] = $order['num'] - $num;
  1264. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'price' => $order['price'], 'num' => $order['num'], 'audit' => 1, 'order_num' => $order['order_num'] . '_' . $i, 'clear' => true));
  1265. }
  1266. } else {
  1267. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'audit' => 1));
  1268. Dever::alert('供货商库存不足,请联系供货商备货,该订单进入重新审核状态');
  1269. }
  1270. if ($send) {
  1271. foreach ($send as $k => $v) {
  1272. $this->sendMsg($v['source_type'], $v['source_id'], $k);
  1273. if ($k != $order['id']) {
  1274. $order_info = Dever::db('shop/buy_order')->find(array('id' => $k, 'clear' => true));
  1275. Dever::load('cash/lib/order')->up($order_info, 1, 1);
  1276. }
  1277. }
  1278. }
  1279. if ($goods) {
  1280. $send = array();
  1281. foreach ($goods as $k => $v) {
  1282. $send[$v['order_id']] = $v;
  1283. }
  1284. if ($send) {
  1285. foreach ($send as $k => $v) {
  1286. $order_info = Dever::db('shop/buy_order')->find(array('id' => $k, 'clear' => true));
  1287. Dever::load('cash/lib/order')->up($order_info, 1, 1);
  1288. }
  1289. }
  1290. }
  1291. return 'ok';
  1292. }
  1293. public function assign(&$goods, &$order, $shop)
  1294. {
  1295. if (strstr($order['order_num'], '_')) {
  1296. $temp = explode('_', $order['order_num']);
  1297. $order['order_num'] = $temp[0];
  1298. $order['order_index'] = $temp[1];
  1299. } else {
  1300. $order['order_index'] = 0;
  1301. }
  1302. # 获取门店分配的仓库和工厂
  1303. $data = array();
  1304. $this->assign_store($data, $goods, $order, $shop);
  1305. $this->assign_factory($data, $goods, $order, $shop);
  1306. return $data;
  1307. }
  1308. private function assign_store(&$data, &$goods, $order, $shop)
  1309. {
  1310. $shop_store = Dever::db('shop/store')->select(array('shop_id' => $shop['id']));
  1311. $store = array();
  1312. $distance = array();
  1313. # 按照距离排序
  1314. if ($shop_store) {
  1315. foreach ($shop_store as $k => $v) {
  1316. $info = Dever::db('store/info')->find($v['store_id']);
  1317. if ($info && $info['status'] == 1) {
  1318. $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
  1319. $store[] = $info;
  1320. }
  1321. }
  1322. if ($store) {
  1323. array_multisort($distance, SORT_ASC, SORT_NUMERIC, $store);
  1324. foreach ($store as $k => $v) {
  1325. if ($goods) {
  1326. foreach ($goods as $gk => $gv) {
  1327. $where = array();
  1328. $where['store_id'] = $v['id'];
  1329. $where['goods_id'] = $gv['goods_id'];
  1330. $where['sku_id'] = ($gv['sku_id'] && $gv['sku_id'] > 0) ? $gv['sku_id'] : -1;
  1331. $sku = Dever::db('store/goods_sku')->getOne($where);
  1332. if ($sku) {
  1333. if ($gv['num'] <= $sku['total']) {
  1334. # 库存符合
  1335. $data['2_' . $v['id']][] = array
  1336. (
  1337. 'area' => $shop['area'],
  1338. 'order_id' => $order['id'],
  1339. 'order_type' => $order['type'],
  1340. 'order_num' => $order['order_num'],
  1341. 'order_index' => $order['order_index'],
  1342. 'type' => 1,
  1343. 'type_id' => $shop['id'],
  1344. 'source_type' => 2,
  1345. 'source_id' => $v['id'],
  1346. 'source_name' => $v['name'],
  1347. 'order_goods_id' => $gv['id'],
  1348. 'goods_id' => $where['goods_id'],
  1349. 'sku_id' => $where['sku_id'],
  1350. 'price' => $gv['price'],
  1351. 'p_price' => $gv['price'],
  1352. 'num' => $gv['num'],
  1353. );
  1354. unset($goods[$gk]);
  1355. }
  1356. }
  1357. }
  1358. }
  1359. }
  1360. }
  1361. }
  1362. }
  1363. private function assign_factory(&$data, &$goods, $order, $shop)
  1364. {
  1365. $shop_factory = Dever::db('shop/factory')->select(array('shop_id' => $shop['id']));
  1366. $factory = array();
  1367. $distance = array();
  1368. # 按照距离排序
  1369. if ($shop_factory && $goods) {
  1370. foreach ($shop_factory as $k => $v) {
  1371. $info = Dever::db('factory/info')->find($v['factory_id']);
  1372. if ($info && $info['status'] == 1) {
  1373. $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
  1374. $factory[] = $info;
  1375. }
  1376. }
  1377. if ($factory) {
  1378. array_multisort($distance, SORT_ASC, SORT_NUMERIC, $factory);
  1379. foreach ($factory as $k => $v) {
  1380. if ($goods) {
  1381. foreach ($goods as $gk => $gv) {
  1382. $where = array();
  1383. $where['factory_id'] = $v['id'];
  1384. $where['goods_id'] = $gv['goods_id'];
  1385. $where['sku_id'] = ($gv['sku_id'] && $gv['sku_id'] > 0) ? $gv['sku_id'] : -1;
  1386. $sku = Dever::db('factory/goods_sku')->getOne($where);
  1387. if ($sku) {
  1388. $data['3_' . $v['id']][] = array
  1389. (
  1390. 'area' => $shop['area'],
  1391. 'order_id' => $order['id'],
  1392. 'order_type' => $order['type'],
  1393. 'order_num' => $order['order_num'],
  1394. 'order_index' => $order['order_index'],
  1395. 'type' => 1,
  1396. 'type_id' => $shop['id'],
  1397. 'source_type' => 3,
  1398. 'source_id' => $v['id'],
  1399. 'source_name' => $v['name'],
  1400. 'order_goods_id' => $gv['id'],
  1401. 'goods_id' => $where['goods_id'],
  1402. 'sku_id' => $where['sku_id'],
  1403. 'price' => $gv['price'],
  1404. 'p_price' => $sku['p_price'],
  1405. 'num' => $gv['num'],
  1406. );
  1407. unset($goods[$gk]);
  1408. }
  1409. }
  1410. }
  1411. }
  1412. }
  1413. }
  1414. }
  1415. /*
  1416. private function store($order_id, $order_num, $shop, $store, $goods, $state = 2)
  1417. {
  1418. $find = array();
  1419. $type_id = array();
  1420. $id = array();
  1421. if (!$store) {
  1422. return array(2, $type_id, $id);
  1423. }
  1424. foreach ($store as $k => $v) {
  1425. $where['store_id'] = $v['id'];
  1426. $where['goods_id'] = $goods['goods_id'];
  1427. $where['sku_id'] = ($goods['sku_id'] && $goods['sku_id'] > 0) ? $goods['sku_id'] : -1;
  1428. $sku = Dever::db('store/goods_sku')->getOne($where);
  1429. if ($sku) {
  1430. if ($goods['num'] <= $sku['total']) {
  1431. # 如果库存符合,直接下单即可,生成仓库订单
  1432. $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']);
  1433. if ($state) {
  1434. $type_id[] = $where['store_id'];
  1435. $id[] = $state;
  1436. $this->order_id[$state] = array(2, $where['store_id']);
  1437. break;
  1438. }
  1439. } elseif ($state == 1 && $sku['total'] > 0) {
  1440. # 如果库存不够,从这个仓库扣下一部分,剩余的从另外仓库处理
  1441. $goods['num'] = $goods['num'] - $sku['total'];
  1442. # 生成仓库订单
  1443. $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']);
  1444. if (!$state) {
  1445. $goods['num'] = $goods['num'] + $sku['total'];
  1446. } else {
  1447. $type_id[] = $where['store_id'];
  1448. $id[] = $state;
  1449. $this->order_id[$state] = array(2, $where['store_id']);
  1450. }
  1451. continue;
  1452. } else {
  1453. # 直接切换到下一个仓库处理
  1454. continue;
  1455. }
  1456. }
  1457. }
  1458. return array(2, $type_id, $id);
  1459. }
  1460. private function factory($order_id, $order_num, $shop, $factory, $goods)
  1461. {
  1462. $find = array();
  1463. $type_id = array();
  1464. $id = array();
  1465. if (!$factory) {
  1466. return array(3, $type_id, $id);
  1467. }
  1468. foreach ($factory as $k => $v) {
  1469. $where['factory_id'] = $v['id'];
  1470. $where['goods_id'] = $goods['goods_id'];
  1471. $where['sku_id'] = ($goods['sku_id'] && $goods['sku_id'] > 0) ? $goods['sku_id'] : -1;
  1472. $sku = Dever::db('factory/goods_sku')->getOne($where);
  1473. if ($sku && $sku['p_price'] > 0) {
  1474. $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']);
  1475. if ($state) {
  1476. $type_id[] = $where['factory_id'];
  1477. $id[] = $state;
  1478. $this->order_id[$state] = array(3, $where['factory_id']);
  1479. break;
  1480. }
  1481. } else {
  1482. continue;
  1483. }
  1484. }
  1485. return array(3, $type_id, $id);
  1486. }
  1487. */
  1488. # 发送消息
  1489. public function sendMsg($source_type, $source_id, $id)
  1490. {
  1491. if (Dever::project('message')) {
  1492. $msg_param['type'] = 1;//消息类型1是订单消息
  1493. $msg_param['id'] = $id;
  1494. $msg_param['source_id'] = $source_id;
  1495. $msg_param['source_type'] = $source_type;
  1496. $msg_param = Dever::json_encode($msg_param);
  1497. $project = $source_type + 1;
  1498. $msg = '您有一个新的订货单,请及时处理。';
  1499. Dever::load('message/lib/data')->push(-1, $source_id, '订货通知', $msg, 2, $project, false, $msg_param);
  1500. }
  1501. }
  1502. # 生成拆单的订单
  1503. /*
  1504. private function createOrder($order_id, $order_num, $type, $type_id, $source_type, $source_id, $price, $p_price, $num, $goods_id, $sku_id)
  1505. {
  1506. $where['parent_order_id'] = $order_id;
  1507. $where['type'] = $type;
  1508. $where['type_id'] = $type_id;
  1509. $where['source_type'] = $source_type;
  1510. $where['source_id'] = $source_id;
  1511. $where['r'] = $goods_id . '_' . microtime() . '_' . mt_rand(1, 10000);
  1512. $info = Dever::db('shop/buy_order')->find($where);
  1513. if (!$info) {
  1514. $this->order_num++;
  1515. $data = $where;
  1516. $data['name'] = $order_id;
  1517. $data['order_num'] = $order_num;
  1518. $data['operdate'] = time();
  1519. $data['status'] = 3;
  1520. $data['price'] = $price * $num;
  1521. $data['p_price'] = $p_price * $num;
  1522. $data['num'] = $num;
  1523. $data['refund_cash'] = 0;
  1524. $data['refund_p_cash'] = 0;
  1525. $id = $state = Dever::db('shop/buy_order')->insert($data);
  1526. } else {
  1527. $id = $info['id'];
  1528. $data['where_id'] = $id;
  1529. $data['price'] = $info['price'] + ($price * $num);
  1530. $data['p_price'] = $info['p_price'] + ($p_price * $num);
  1531. $data['num'] = $info['num'] + $num;
  1532. $state = Dever::db('shop/buy_order')->update($data);
  1533. }
  1534. if ($state && $id > 0) {
  1535. $goods_info = Dever::db('shop/buy_order_goods')->find(array('order_id' => $id));
  1536. if (!$goods_info) {
  1537. $goods['order_id'] = $id;
  1538. $goods['goods_id'] = $goods_id;
  1539. $goods['sku_id'] = $sku_id;
  1540. $goods['price'] = $price;
  1541. $goods['p_price'] = $p_price;
  1542. $goods['num'] = $num;
  1543. Dever::db('shop/buy_order_goods')->insert($goods);
  1544. }
  1545. $state = $this->updateSell($source_type, $source_id, $goods_id, $sku_id, $num);
  1546. return $id;
  1547. }
  1548. return false;
  1549. }
  1550. */
  1551. public function createOrder($data)
  1552. {
  1553. $where['parent_type'] = $data['order_type'];
  1554. $where['parent_order_id'] = $data['order_id'];
  1555. $where['type'] = $data['type'];
  1556. $where['type_id'] = $data['type_id'];
  1557. $where['source_type'] = $data['source_type'];
  1558. $where['source_id'] = $data['source_id'];
  1559. $where['clear'] = true;
  1560. $info = Dever::db('shop/buy_order')->find($where);
  1561. $goods_info = array();
  1562. $goods['goods_id'] = $data['goods_id'];
  1563. $goods['sku_id'] = $data['sku_id'];
  1564. $goods['clear'] = true;
  1565. if (!$info) {
  1566. $insert = $where;
  1567. if (isset($data['area'])) {
  1568. $insert['area'] = $data['area'] . ',' . $where['type_id'];
  1569. }
  1570. $insert['name'] = $data['order_id'];
  1571. $insert['order_num'] = $data['order_num'];
  1572. $insert['operdate'] = time();
  1573. $insert['status'] = 3;
  1574. $insert['price'] = $data['price'] * $data['num'];
  1575. $insert['p_price'] = $data['p_price'] * $data['num'];
  1576. $insert['num'] = $data['num'];
  1577. $insert['refund_cash'] = 0;
  1578. $insert['refund_p_cash'] = 0;
  1579. $id = $state = Dever::db('shop/buy_order')->insert($insert);
  1580. if ($id) {
  1581. $goods['order_id'] = $id;
  1582. $goods_info = Dever::db('shop/buy_order_goods')->find($goods);
  1583. }
  1584. } else {
  1585. $id = $info['id'];
  1586. $goods['order_id'] = $id;
  1587. $goods_info = Dever::db('shop/buy_order_goods')->find($goods);
  1588. $state = false;
  1589. if (!$goods_info) {
  1590. $update['where_id'] = $id;
  1591. $update['price'] = $info['price'] + ($data['price'] * $data['num']);
  1592. $update['p_price'] = $info['p_price'] + ($data['p_price'] * $data['num']);
  1593. $update['num'] = $info['num'] + $data['num'];
  1594. $state = Dever::db('shop/buy_order')->update($update);
  1595. }
  1596. }
  1597. if ($state && $id > 0 && !$goods_info) {
  1598. $goods['price'] = $data['price'];
  1599. $goods['p_price'] = $data['p_price'];
  1600. $goods['num'] = $data['num'];
  1601. Dever::db('shop/buy_order_goods')->insert($goods);
  1602. $state = $this->updateSell($data['source_type'], $data['source_id'], $data['goods_id'], $data['sku_id'], $data['num']);
  1603. return $id;
  1604. }
  1605. return false;
  1606. }
  1607. # 减少库存,增加销量
  1608. public function updateSell($source_type, $source_id, $goods_id, $sku_id, $num)
  1609. {
  1610. if ($source_type == 2) {
  1611. $data['store_id'] = $source_id;
  1612. $data['goods_id'] = $goods_id;
  1613. $data['sku_id'] = $sku_id;
  1614. $data['num'] = $num;
  1615. return Dever::load('store/lib/goods')->oper($source_id, 1, 2, array($data));
  1616. } else {
  1617. $up = array();
  1618. $up['where_factory_id'] = $source_id;
  1619. $up['where_goods_id'] = $goods_id;
  1620. $up['sell_num'] = $num;
  1621. $state = Dever::db('factory/goods')->incSell($up);
  1622. if ($state) {
  1623. if ($sku_id > 0) {
  1624. } else {
  1625. $sku_id = -1;
  1626. }
  1627. $upSku = $up;
  1628. $upSku['where_sku_id'] = $sku_id;
  1629. $state = Dever::db('factory/goods_sku')->incSell($upSku);
  1630. }
  1631. return $state;
  1632. }
  1633. }
  1634. # 打印订单单
  1635. public function printer($user)
  1636. {
  1637. $id = Dever::input('id');
  1638. $type = Dever::input('type', 1);
  1639. $factory_config = Dever::db('main/factory_config')->find();
  1640. $main_config = Dever::db('main/config')->find();
  1641. $config = Dever::db('shop/buy_order')->config;
  1642. $info = Dever::db('shop/buy_order')->one($id);
  1643. //$source_info = Dever::db('factory/info')->find($info['source_id']);
  1644. if ($type == 2) {
  1645. # 仓库
  1646. $member = Dever::db('store/member')->find($user['id']);
  1647. $name = '仓库名称';
  1648. } else {
  1649. # 工厂
  1650. $member = Dever::db('factory/member')->find($user['id']);
  1651. $name = '工厂名称';
  1652. }
  1653. if ($info['type'] == 1) {
  1654. $type_info = Dever::db('shop/info')->find($info['type_id']);
  1655. } elseif ($info['type'] == 2) {
  1656. $type_info = Dever::db('store/info')->find($info['type_id']);
  1657. } elseif ($info['type'] == 3) {
  1658. $type_info = Dever::db('factory/info')->find($info['type_id']);
  1659. }
  1660. if ($info['source_type'] == 1) {
  1661. $source_info = Dever::db('shop/info')->find($info['source_id']);
  1662. } elseif ($info['source_type'] == 2) {
  1663. $source_info = Dever::db('store/info')->find($info['source_id']);
  1664. } elseif ($info['source_type'] == 3) {
  1665. $source_info = Dever::db('factory/info')->find($info['source_id']);
  1666. }
  1667. $cdate = date('Y-m-d H:i', $info['cdate']);
  1668. if ($info['operdate']) {
  1669. $operdate = date('Y-m-d H:i', $info['operdate']);
  1670. } else {
  1671. $operdate = '';
  1672. }
  1673. if ($info['fdate']) {
  1674. $fdate = date('Y-m-d H:i', $info['fdate']);
  1675. } else {
  1676. $fdate = '';
  1677. }
  1678. $status = $config['status'][$info['status']];
  1679. $info['ps_info'] = Dever::db('shop/buy_order_ps')->find(array('order_id' => $info['id']));
  1680. if ($info['ps_info']) {
  1681. $info['ps_info']['service'] = Dever::array_decode($info['ps_info']['service']);
  1682. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  1683. if ($info['ps_info']['ydate']) {
  1684. $info['ps_info']['ydate'] = date('Y-m-d H:i', $info['ps_info']['ydate']);
  1685. } else {
  1686. $info['ps_info']['ydate'] = '无';
  1687. }
  1688. $ps_status = Dever::db('shop/buy_order_ps')->config['status'];
  1689. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  1690. }
  1691. $pdf = Dever::load('pdf/lib/base')->init();
  1692. $pdf->hr('-', $main_config['name']);
  1693. $pdf->br()->font(20)->center('订货单号:' . $info['order_num']);
  1694. $pdf->font(10);
  1695. $pdf->br();
  1696. if ($type == 2 && $info['type'] == 2 && $info['source_type'] != 2) {
  1697. $pdf->br()->left($name . ':' . $type_info['name'], 80)->left('制单人:' . $member['name'], 60)->left('制单时间:' . date('Y-m-d H:i'), 40);
  1698. $pdf->hr();
  1699. $pdf->br()->left('订单状态:' . $status, 140)->left('订货时间:' . $operdate, 40);
  1700. $pdf->hr();
  1701. if (!$info['ps_info']) {
  1702. $pdf->left('发货供应商:' . $source_info['name']);
  1703. } else {
  1704. //$pdf->br()->left('发货供应商:' . $source_info['name'], 140)->left('发货时间:' . $info['ps_info']['cdate'], 40);
  1705. $pdf->br()->left('发货供应商:' . $source_info['name'], 80)->left('发货时间:' . $info['ps_info']['cdate'], 60)->left('收货时间:' . $info['ps_info']['ydate'], 40);
  1706. }
  1707. if ($info['ps_info'] && $info['ps_info']['service']) {
  1708. foreach ($info['ps_info']['service'] as $k => $v) {
  1709. $info['ps_info']['service'][$k]['service_name'] = '供应商自送';
  1710. if ($v['service_id'] > 0) {
  1711. $service = Dever::db('shop/service')->find($v['service_id']);
  1712. $info['ps_info']['service'][$k]['service_name'] = $service['name'];
  1713. }
  1714. $pdf->br()->left('物流信息:' . $info['ps_info']['service'][$k]['service_name'], 80)->left('物流单号:' . $info['ps_info']['service'][$k]['order_num'], 40);
  1715. }
  1716. }
  1717. $pdf->hr();
  1718. } else {
  1719. $pdf->br()->left($name . ':' . $source_info['name'], 80)->left('制单人:' . $member['name'], 60)->left('制单时间:' . date('Y-m-d H:i'), 40);
  1720. $pdf->hr();
  1721. $pdf->br()->left('订单状态:' . $status, 80)->left('订货时间:' . $operdate, 80);
  1722. $pdf->hr();
  1723. $parent_order = array();
  1724. if ($info['parent_type'] == 2) {
  1725. $parent_order = Dever::db('shop/sell_order')->find($info['parent_order_id']);
  1726. }
  1727. if ($parent_order && $parent_order['uid']) {
  1728. if ($parent_order['address_id']) {
  1729. $address = Dever::load('passport/address')->getOne($parent_order['uid'], $parent_order['address_id']);
  1730. $pdf->br()->left('收件人:' . $address['contact'], 80)->left('收件人手机:' . $address['mobile'], 80);
  1731. $pdf->left('收件地址:' . $address['address_info'] . ',' . $address['house_number']);
  1732. } else {
  1733. $user = Dever::db('passport/user')->find($parent_order['uid']);
  1734. $pdf->br()->left('收件人:' . $user['username'], 80)->left('收件人手机:' . $user['mobile'], 80);
  1735. }
  1736. } else {
  1737. $pdf->br()->left('配送至:' . $type_info['name'], 80)->left('联系方式:' . $type_info['truename'] . ',' . $type_info['mobile'], 80);
  1738. $pdf->left('配送地址:' . $type_info['address']);
  1739. }
  1740. $pdf->hr();
  1741. if ($info['ps_info'] && $info['ps_info']['service']) {
  1742. $pdf->br()->left('发货时间:' . $info['ps_info']['cdate'], 80)->left('收货时间:' . $info['ps_info']['ydate'], 80);
  1743. foreach ($info['ps_info']['service'] as $k => $v) {
  1744. $info['ps_info']['service'][$k]['service_name'] = '供应商自送';
  1745. if ($v['service_id'] > 0) {
  1746. $service = Dever::db('shop/service')->find($v['service_id']);
  1747. $info['ps_info']['service'][$k]['service_name'] = $service['name'];
  1748. }
  1749. $pdf->br()->left('物流信息:' . $info['ps_info']['service'][$k]['service_name'], 80)->left('物流单号:' . $info['ps_info']['service'][$k]['order_num'], 80);
  1750. }
  1751. /*
  1752. $pdf->br()->left('发货时间:' . $info['ps_info']['cdate'], 80)->left('配货员:' . $info['ps_info']['phy'], 70)->left('打包员:' . $info['ps_info']['dby'], 40);
  1753. if ($info['ps_info']['zl'] && $info['ps_info']['tj']) {
  1754. $pdf->br()->left('包裹重量:' . $info['ps_info']['zl'] . '克', 80)->left('包裹体积:' . $info['ps_info']['tj'] . '立方厘米', 70)->left('包裹数量:' . $info['ps_info']['num'], 40);
  1755. } elseif (!$info['ps_info']['zl'] && $info['ps_info']['tj']) {
  1756. $pdf->br()->left('包裹体积:' . $info['ps_info']['tj'] . '立方厘米', 150)->left('包裹数量:' . $info['ps_info']['num'], 40);
  1757. } elseif ($info['ps_info']['zl'] && !$info['ps_info']['tj']) {
  1758. $pdf->br()->left('包裹重量:' . $info['ps_info']['zl'] . '克', 150)->left('包裹数量:' . $info['ps_info']['num'], 40);
  1759. } else {
  1760. $pdf->left('包裹数量:' . $info['ps_info']['num']);
  1761. }
  1762. $pdf->br()->left('物流信息:' . $info['ps_info']['service_name'], 40);
  1763. if ($info['ps_info']['order_num']) {
  1764. $pdf->left('物流单号:' . $info['ps_info']['order_num'], 70);
  1765. }
  1766. if ($info['ps_info']['ydate']) {
  1767. $pdf->left('到货确认时间:' . $info['ps_info']['ydate']);
  1768. }
  1769. */
  1770. $pdf->hr();
  1771. }
  1772. }
  1773. $body = array();
  1774. $body_total = array();
  1775. $body_total['price'] = 0;
  1776. $body_total['num'] = 0;
  1777. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $info['id']));
  1778. $goods_status = Dever::db('shop/buy_order_goods')->config['status'];
  1779. $refund_body = array();
  1780. $refund_body_total = array();
  1781. $refund_body_total['price'] = 0;
  1782. $refund_body_total['num'] = 0;
  1783. foreach ($goods as $k => $v) {
  1784. if ($type == 1) {
  1785. $v['price'] = $v['p_price'];
  1786. }
  1787. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  1788. $status = $goods_status[$v['status']];
  1789. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
  1790. if (isset($goods_info['sku'])) {
  1791. $sku = $goods_info['sku']['string'];
  1792. } else {
  1793. $sku = '无';
  1794. }
  1795. $d = array
  1796. (
  1797. $goods_info['id'],
  1798. $goods_info['name'],
  1799. $sku,
  1800. $v['price'],
  1801. 'x ' . $v['num'],
  1802. );
  1803. if ($type == 2) {
  1804. unset($d[3]);
  1805. }
  1806. if ($tk && $v['status'] == 3 && $tk['status'] != 3) {
  1807. $refund_body[] = $d;
  1808. $price = $v['price']*$v['num'];
  1809. $refund_body_total['price'] += $price;
  1810. $refund_body_total['num'] += $v['num'];
  1811. } else {
  1812. $body[] = $d;
  1813. $price = $v['price']*$v['num'];
  1814. $body_total['price'] += $price;
  1815. $body_total['num'] += $v['num'];
  1816. }
  1817. }
  1818. $head = array(array('商品编号', 40), array('商品名称', 60), array('商品属性', 40), array('单价', 40), array('数量', 20));
  1819. if ($type == 2) {
  1820. unset($head[3]);
  1821. $head[1][1] = 70;
  1822. $head[2][1] = 60;
  1823. }
  1824. if ($body) {
  1825. $pdf->center('订单商品');
  1826. $pdf->br();
  1827. foreach ($head as $k => $v) {
  1828. $pdf->left($v[0], $v[1]);
  1829. }
  1830. foreach ($body as $k => $v) {
  1831. $pdf->br();
  1832. foreach ($head as $k1 => $v1) {
  1833. $pdf->left($v[$k1], $v1[1]);
  1834. }
  1835. }
  1836. $pdf->br();
  1837. if ($type == 2) {
  1838. $pdf->right('共'.$body_total['num'].'件商品');
  1839. } else {
  1840. $pdf->right('共'.$body_total['num'].'件商品,合计金额¥' . $body_total['price'] . '元');
  1841. }
  1842. $pdf->hr();
  1843. }
  1844. if ($refund_body) {
  1845. $pdf->center('缺货与报损商品');
  1846. $pdf->br();
  1847. foreach ($head as $k => $v) {
  1848. $pdf->left($v[0], $v[1]);
  1849. }
  1850. foreach ($refund_body as $k => $v) {
  1851. $pdf->br();
  1852. foreach ($head as $k1 => $v1) {
  1853. $pdf->left($v[$k1], $v1[1]);
  1854. }
  1855. }
  1856. $pdf->br();
  1857. if ($type == 2) {
  1858. $pdf->right('共'.$refund_body_total['num'].'件商品');
  1859. } else {
  1860. $pdf->right('共'.$refund_body_total['num'].'件商品,合计金额¥' . $refund_body_total['price'] . '元');
  1861. }
  1862. $pdf->hr();
  1863. }
  1864. $pdf->br(1);
  1865. $pdf->right('如遇任何问题请致电客服');
  1866. $pdf->br();
  1867. $pdf->font(20);
  1868. $pdf->left($main_config['name'], 160);
  1869. $pdf->font(10);
  1870. $pdf->right('电话:' . $factory_config['phone'], 30);
  1871. $pdf->br();
  1872. $pdf->left($main_config['site'], 160);
  1873. $pdf->font(10);
  1874. $pdf->right($main_config['worktime'], 30);
  1875. $pdf->out('订货单');
  1876. }
  1877. # 导出单个订单
  1878. public function excel_one_api()
  1879. {
  1880. $this->type = Dever::input('type');
  1881. $id = Dever::input('id');
  1882. $type_id = Dever::input('type_id');
  1883. $data = $this->getView($type_id, $id);
  1884. $this->excel(array($data));
  1885. }
  1886. # 导出订单
  1887. public function excel($data)
  1888. {
  1889. if (!$data) {
  1890. Dever::alert('无导出数据');
  1891. }
  1892. $file = '入库订单';
  1893. $type = Dever::input('search_option_type', $this->type);
  1894. $header = array('所属仓库', '订单号', '商品来源', '发货日期', '商品名称', '商品规格属性', '商品数量', '报损退款状态', '订单状态');
  1895. $table = 'store/info';
  1896. if ($type == 1) {
  1897. $header[0] = '所属门店';
  1898. $table = 'shop/info';
  1899. }
  1900. $body = array();
  1901. foreach ($data as $k => $v) {
  1902. if (!is_array($v)) {
  1903. continue;
  1904. }
  1905. foreach ($v['goods'] as $k1 => $v1) {
  1906. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v1['goods_id'], $v1['sku_id']);
  1907. if (isset($goods_info['sku'])) {
  1908. $sku = $goods_info['sku']['string'];
  1909. } else {
  1910. $sku = '';
  1911. }
  1912. $d = array
  1913. (
  1914. $v['type_info']['name'],
  1915. $v['order_num'],
  1916. $v['source_info']['name'],
  1917. $v['cdate'],
  1918. $goods_info['name'],
  1919. $sku,
  1920. $v1['num'],
  1921. $v1['status_name'],
  1922. $v['status_name'],
  1923. );
  1924. $body[] = $d;
  1925. }
  1926. }
  1927. Dever::excelExport($body, $header, $file);
  1928. }
  1929. }