Buy.php 100 KB

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