Buy.php 100 KB

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