Buy.php 85 KB

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