Buy.php 97 KB

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