Buy.php 97 KB

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