Dhorder.php 65 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686
  1. <?php
  2. namespace Agent\Lib;
  3. use Dever;
  4. class Dhorder
  5. {
  6. # 获取配置
  7. public $config = array();
  8. # table
  9. public $table = 'agent/dh_order';
  10. public function __construct()
  11. {
  12. $this->config = Dever::db($this->table)->config;
  13. }
  14. # 设置订单的类型
  15. public function set($type, $view)
  16. {
  17. return $this;
  18. }
  19. # 获取公共的where
  20. public function where($id)
  21. {
  22. $where = array();
  23. $where['mid'] = $id;
  24. if (!$where) {
  25. Dever::alert('参数错误');
  26. }
  27. return $where;
  28. }
  29. # 获取订单列表
  30. public function getList($id)
  31. {
  32. $result = array();
  33. $where = $this->where($id);
  34. $mobile = Dever::input('mobile');
  35. if ($mobile) {
  36. $where['mobile'] = $mobile;
  37. }
  38. $status = Dever::input('status');
  39. if ($status) {
  40. $where['status'] = $status;
  41. }
  42. $result['order'] = Dever::db($this->table)->getAll($where);
  43. if ($result['order']) {
  44. foreach ($result['order'] as $k => $v) {
  45. $result['order'][$k] = $this->getInfo($v, false);
  46. }
  47. }
  48. return $result;
  49. }
  50. # 查看详情
  51. public function getView($id, $order_id, $show = true)
  52. {
  53. $where = $this->where($id);
  54. $where['id'] = $order_id;
  55. $result = Dever::db($this->table)->find($where);
  56. if (!$result) {
  57. Dever::alert('订单不存在');
  58. }
  59. if ($show) {
  60. $result = $this->getInfo($result);
  61. }
  62. return $result;
  63. }
  64. # 获取订单详细信息
  65. public function getInfo($info, $view = true)
  66. {
  67. $info['status_name'] = Dever::status($this->config['status'], $info['status']);
  68. $info['cdate'] = date('Y-m-d H:i', $info['cdate']);
  69. if ($info['operdate']) {
  70. $info['operdate'] = date('Y-m-d H:i', $info['operdate']);
  71. } else {
  72. $info['operdate'] = '';
  73. }
  74. if ($info['fdate']) {
  75. $info['fdate'] = date('Y-m-d H:i', $info['fdate']);
  76. } else {
  77. $info['fdate'] = '';
  78. }
  79. if ($view && $info['type'] == 1) {
  80. $info['ps_info'] = Dever::db('agent/dh_order_ps')->find(array('order_id' => $info['id']));
  81. if ($info['ps_info']) {
  82. $info['ps_info']['service'] = Dever::array_decode($info['ps_info']['service']);
  83. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  84. if ($info['ps_info']['qu_date']) {
  85. $info['ps_info']['qu_date'] = date('Y-m-d H:i', $info['ps_info']['qu_date']);
  86. }
  87. if ($info['ps_info']['qs_date']) {
  88. $info['ps_info']['qs_date'] = date('Y-m-d H:i', $info['ps_info']['qs_date']);
  89. }
  90. $status = Dever::db('agent/dh_order_ps')->config['status'];
  91. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  92. if ($info['ps_info']['service']) {
  93. foreach ($info['ps_info']['service'] as $k => $v) {
  94. if (isset($v['order_num']) && $v['order_num']) {
  95. $info['ps_info']['service'][$k]['service_name'] = '商家自送';
  96. if ($v['service_id'] > 0) {
  97. $service = Dever::db('shop/service')->find($v['service_id']);
  98. $info['ps_info']['service'][$k]['service_name'] = $service['name'];
  99. }
  100. } else {
  101. unset($info['ps_info']['service'][$k]);
  102. }
  103. }
  104. }
  105. }
  106. $goods_status = Dever::db('agent/dh_order_goods')->config['status'];
  107. if ($info['address_id']) {
  108. $info['address'] = Dever::db('agent/member_address')->find($info['address_id']);
  109. }
  110. $info['goods'] = Dever::db('agent/dh_order_goods')->select(array('order_id' => $info['id']));
  111. foreach ($info['goods'] as $k => $v) {
  112. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  113. if (isset($goods_info['sku'])) {
  114. $sku = $goods_info['sku']['string'];
  115. } else {
  116. $sku = '';
  117. }
  118. $info['goods'][$k] += array
  119. (
  120. 'pic' => $goods_info['cover'],
  121. 'name' => $goods_info['name'],
  122. 'sku' => $sku,
  123. 'price' => $goods_info['price'],
  124. );
  125. }
  126. }
  127. if ($info['type'] == 2) {
  128. $info['card'] = Dever::db('card/info')->find($info['type_id']);
  129. }
  130. if ($info['type'] == 3) {
  131. $info['prize'] = Dever::db('setting/prize')->find($info['type_id']);
  132. }
  133. return $info;
  134. }
  135. # 取消订单
  136. public function cancel($id, $order_id)
  137. {
  138. $data = $this->getView($id, $order_id, false);
  139. if ($data['status'] == 1) {
  140. $state = Dever::db('agent/dh_order')->update(array('where_id' => $data['id'], 'status' => 7, 'operdate' => time()));
  141. if ($state) {
  142. $this->updateSell($data, 2);
  143. }
  144. return 'ok';
  145. } else {
  146. Dever::alert('当前订单状态不允许取消');
  147. }
  148. }
  149. # 得到商品和总价
  150. public function goods($mid)
  151. {
  152. $goods_id = Dever::input('goods_id');
  153. if (!$goods_id) {
  154. Dever::alert('请传入商品');
  155. }
  156. $goods_id = explode(',', $goods_id);
  157. $num = Dever::input('num');
  158. if (!$num) {
  159. Dever::alert('请传入商品数量');
  160. }
  161. $num = explode(',', $num);
  162. $data['price'] = 0;
  163. $data['num'] = 0;
  164. $data['name'] = array();
  165. $count = count($goods_id);
  166. $data['list'] = array();
  167. $i = 0;
  168. foreach ($goods_id as $k => $v) {
  169. $n = isset($num[$k]) ? $num[$k] : 1;
  170. $data['list'][$i] = Dever::db('agent/member_goods')->find(array('mid' => $mid, 'id' => $v));
  171. if ($data['list'][$i]) {
  172. $data['list'][$i]['num'] = $n;
  173. # 2是库存不足
  174. $data['list'][$i]['ku_state'] = 1;
  175. $total = $data['list'][$i]['total_num'] - $data['list'][$i]['sell_num'];
  176. if ($total >= $n) {
  177. $data['list'][$i]['buy_num'] = $n;
  178. } else {
  179. $data['list'][$i]['buy_num'] = $total;
  180. }
  181. if ($data['list'][$i]['buy_num'] <= 0) {
  182. $data['list'][$i]['ku_state'] = 2;
  183. $data['list'][$i]['buy_num'] = 0;
  184. }
  185. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($data['list'][$i]['type_id'], $data['list'][$i]['sku_id']);
  186. if (isset($goods_info['sku'])) {
  187. $sku = $goods_info['sku']['string'];
  188. } else {
  189. $sku = '';
  190. }
  191. $data['list'][$i] += array
  192. (
  193. 'pic' => $goods_info['cover'],
  194. 'name' => $goods_info['name'],
  195. 'sku' => $sku,
  196. 'price' => $goods_info['price'],
  197. );
  198. if ($data['list'][$i]['ku_state'] == 1) {
  199. $data['num'] += $data['list'][$i]['buy_num'];
  200. $data['price'] += $goods_info['price'] * $data['list'][$i]['buy_num'];
  201. $data['name'] = '兑换商品';
  202. }
  203. $i++;
  204. }
  205. }
  206. if ($data['num'] <= 0) {
  207. Dever::alert('没有商品可以兑换');
  208. }
  209. return $data;
  210. }
  211. # 确认兑换
  212. public function action($user, $name, $num, $price, $goods, $address_id, $dh_type, $type, $type_id, $info, $refer = '')
  213. {
  214. # 获取平台店
  215. $shop = Dever::db('shop/info')->find(array('type' => 10));
  216. if (!$shop) {
  217. Dever::alert('没有平台店');
  218. }
  219. $order_data['shop_id'] = $shop['id'];
  220. $order_data['mid'] = $user['id'];
  221. $order_data['mobile'] = $user['mobile'];
  222. $order_data['address_id'] = $address_id;
  223. $order_data['name'] = $name;
  224. $order_data['num'] = $num;
  225. $order_data['info'] = $info;
  226. $order_data['price'] = $price;
  227. $order_data['dh_type'] = $dh_type;
  228. $order_data['type'] = $type;
  229. if ($type_id) {
  230. $order_data['type_id'] = $type_id;
  231. }
  232. if ($dh_type == 2) {
  233. $order_data['status'] = 5;
  234. } else {
  235. $order_data['status'] = 2;
  236. }
  237. $order_data['refund_cash'] = 0;
  238. $order_data['audit'] = 2;
  239. if ($type == 1) {
  240. $time = time();
  241. $order_data_where = $order_data;
  242. $order_data_where['start'] = $time - 60;
  243. $order_data_where['end'] = $time;
  244. $order_data_where['option']['start'] = array('yes-cdate', '>=');
  245. $order_data_where['option']['end'] = array('yes-cdate', '<=');
  246. $info = Dever::db('agent/dh_order')->find($order_data_where);
  247. } else {
  248. $info = false;
  249. }
  250. $order_data['order_num'] = $this->getOrderId();
  251. if (!$info) {
  252. $id = Dever::db('agent/dh_order')->insert($order_data);
  253. } else {
  254. return array
  255. (
  256. 'order' => '',
  257. 'order_num' => $info['order_num'],
  258. 'order_id' => $info['id'],
  259. 'type' => 'test',
  260. );
  261. }
  262. if (!$id) {
  263. Dever::alert('兑换失败');
  264. }
  265. if ($goods) {
  266. foreach($goods as $k => $v) {
  267. if ($v['ku_state'] == 1) {
  268. $data['mid'] = $user['id'];
  269. $data['order_id'] = $id;
  270. $data['shop_id'] = $shop['id'];
  271. $data['goods_id'] = $v['type_id'];
  272. $data['sku_id'] = $v['sku_id'];
  273. $data['price'] = $v['price'];
  274. $data['num'] = $v['buy_num'];
  275. Dever::db('agent/dh_order_goods')->insert($data);
  276. }
  277. }
  278. }
  279. # 减少库存 增加销量
  280. $order_data['id'] = $id;
  281. $this->updateSell($order_data);
  282. return array
  283. (
  284. 'order_num' => $order_data['order_num'],
  285. 'order_id' => $id,
  286. 'type' => 'dh',
  287. );
  288. }
  289. # 添加配送信息
  290. public function updatePs($order, $status = 1, $service_id = -1, $order_num = '', $cdate = false)
  291. {
  292. $order = Dever::db('agent/dh_order')->find($order['id']);
  293. $where['order_id'] = $order['id'];
  294. $where['clear'] = true;
  295. $data = Dever::db('agent/dh_order_ps')->find($where);
  296. $service = array();
  297. if ($service_id) {
  298. $service['service_id'] = $service_id;
  299. }
  300. if ($order_num) {
  301. $service['order_num'] = $order_num;
  302. }
  303. if (!$data) {
  304. $insert = $where;
  305. if ($service) {
  306. $insert['service'][] = $service;
  307. $insert['service'] = Dever::array_encode($insert['service']);
  308. }
  309. $insert['status'] = $status;
  310. if ($status == 2) {
  311. $insert['qu_date'] = time();
  312. }
  313. $insert['mid'] = $order['mid'];
  314. if ($cdate) {
  315. $insert['cdate'] = $cdate;
  316. }
  317. Dever::db('agent/dh_order_ps')->insert($insert);
  318. } else {
  319. $update = $where;
  320. $update['where_id'] = $data['id'];
  321. $update['status'] = $status;
  322. if ($status == 2) {
  323. $update['qu_date'] = time();
  324. }
  325. if ($status == 3) {
  326. $update['qs_date'] = time();
  327. }
  328. if ($service) {
  329. $data['service'] = Dever::array_decode($data['service']);
  330. $update['service'] = $data['service'];
  331. $update['service'][] = $service;
  332. $update['service'] = Dever::array_encode($update['service']);
  333. }
  334. $update['mid'] = $order['mid'];
  335. if ($cdate) {
  336. $update['cdate'] = $cdate;
  337. }
  338. Dever::db('agent/dh_order_ps')->update($update);
  339. }
  340. }
  341. # 减少库存,增加销量
  342. public function updateSell($order, $type = 1)
  343. {
  344. $method = 'incSell';
  345. $name = '兑换商品';
  346. $name_3 = '兑换名额';
  347. if ($type == 2) {
  348. $method = 'decSell';
  349. $name = '退回商品';
  350. $name_3 = '退回名额';
  351. }
  352. if ($order['type'] == 1) {
  353. $where['order_id'] = $order['id'];
  354. //$where['status'] = 3;
  355. $data = Dever::db('agent/dh_order_goods')->select($where);
  356. if ($data) {
  357. foreach ($data as $k => $v) {
  358. $up = array();
  359. $up['where_mid'] = $order['mid'];
  360. $up['where_type'] = $order['type'];
  361. $up['where_type_id'] = $v['goods_id'];
  362. $up['where_sku_id'] = $v['sku_id'];
  363. $up['clear'] = true;
  364. $info = Dever::db('agent/member_goods')->findData($up);
  365. $up['set_sell_num'] = $v['num'];
  366. Dever::db('agent/member_goods')->$method($up);
  367. if ($type == 2) {
  368. Dever::load('agent/lib/member_set')->log($up['where_mid'], '', $up['where_type'], $up['where_type_id'], $up['where_sku_id'], $up['set_sell_num'], $info['num'], $info['num']+$up['set_sell_num'], $info['name'], $name);
  369. } else {
  370. Dever::load('agent/lib/member_set')->log($up['where_mid'], '', $up['where_type'], $up['where_type_id'], $up['where_sku_id'], $up['set_sell_num'], $info['num'], $info['num']-$up['set_sell_num'], $info['name'], $name);
  371. }
  372. }
  373. }
  374. } elseif ($order['type'] == 3) {
  375. $up['where_mid'] = $order['mid'];
  376. $up['where_type'] = $order['type'];
  377. $up['where_type_id'] = $order['type_id'];
  378. $up['where_sku_id'] = -1;
  379. $info = Dever::db('agent/member_goods')->findData($up);
  380. $up['set_sell_num'] = $order['num'];
  381. Dever::db('agent/member_goods')->$method($up);
  382. if ($type == 2) {
  383. Dever::load('agent/lib/member_set')->log($up['where_mid'], '', $up['where_type'], $up['where_type_id'], $up['where_sku_id'], $up['set_sell_num'], $info['num'], $info['num']+$up['set_sell_num'], $info['name'], $name_3);
  384. } else {
  385. Dever::load('agent/lib/member_set')->log($up['where_mid'], '', $up['where_type'], $up['where_type_id'], $up['where_sku_id'], $up['set_sell_num'], $info['num'], $info['num']-$up['set_sell_num'], $info['name'], $name_3);
  386. }
  387. }
  388. }
  389. # 完成
  390. public function finish($info, $shop_id)
  391. {
  392. if ($info && $info['shop_id'] == $shop_id && $info['status'] <= 4) {
  393. $status = 5;
  394. $state = Dever::db('agent/dh_order')->update(array('where_id' => $info['id'], 'status' => $status, 'fdate' => time()));
  395. $this->updatePs($info, 3);
  396. $w['parent_type'] = 3;
  397. $w['parent_order_id'] = $info['id'];
  398. $w['status'] = 4;
  399. $buy_order = Dever::db('shop/buy_order')->select($w);
  400. if ($buy_order) {
  401. foreach ($buy_order as $k => $v) {
  402. Dever::load('mshop/lib/buy')->set(1,1)->finish($shop_id, $v['id']);
  403. }
  404. }
  405. return 'ok';
  406. } else {
  407. Dever::alert('您没有权限核销');
  408. }
  409. }
  410. # 平台订单已收货
  411. public function yes_api()
  412. {
  413. $shop_id = Dever::input('shop_id');
  414. $order_id = Dever::input('order_id');
  415. $info = Dever::db('agent/dh_order')->find(array('id' => $order_id));
  416. if ($info && $info['type'] == 10) {
  417. $this->finish($info, $shop_id);
  418. } else {
  419. Dever::alert('您没有权限核销');
  420. }
  421. return 'reload';
  422. }
  423. # 生成订单号
  424. public function getOrderId()
  425. {
  426. $where['order_num'] = Dever::order('DH');
  427. $state = Dever::db('agent/dh_order')->one($where);
  428. if (!$state) {
  429. return $where['order_num'];
  430. } else {
  431. return $this->getOrderId();
  432. }
  433. }
  434. # 展示订单详情
  435. public function show()
  436. {
  437. $id = Dever::input('order_id');
  438. $config = Dever::db('agent/dh_order')->config;
  439. $info = Dever::db('agent/dh_order')->one($id);
  440. if ($info['type'] > 1) {
  441. return;
  442. }
  443. $tk_status = Dever::db('agent/dh_order_refund')->config['status'];
  444. $tk_process = Dever::db('agent/dh_order_refund')->config['process'];
  445. $status = $config['status'][$info['status']];
  446. $tk = Dever::db('agent/dh_order_refund')->find(array('order_id' => $info['id'], 'type' => 1));
  447. $shop = Dever::db('shop/info')->find($info['shop_id']);
  448. $user = Dever::db('agent/member')->find($info['mid']);
  449. $cdate = date('Y-m-d H:i', $info['cdate']);
  450. if ($info['operdate']) {
  451. $opertime = date('Y-m-d H:i', $info['operdate']);
  452. } else {
  453. $opertime = '';
  454. }
  455. if ($info['fdate']) {
  456. $fdate = date('Y-m-d H:i', $info['fdate']);
  457. } else {
  458. $fdate = '';
  459. }
  460. if (!$user) {
  461. $user['username'] = '无';
  462. }
  463. if (!isset($user['name'])) {
  464. $user['truename'] = $user['name'];
  465. }
  466. if ($tk && $tk['process'] == 1) {
  467. $status = '申请' . $tk_status[$tk['status']];
  468. $process = $tk_process[$tk['process']];
  469. $process = '';
  470. }
  471. $time = array();
  472. $time[] = array('下单时间', $cdate);
  473. if ($opertime) {
  474. if ($info['status'] == 2) {
  475. $time[] = array('通知时间', $opertime);
  476. }
  477. if ($info['status'] == 7) {
  478. $time[] = array('取消时间', $opertime);
  479. }
  480. }
  481. if ($fdate) {
  482. $time[] = array('完成时间', $fdate);
  483. }
  484. $time[] = array('审核备注', $info['audit_desc']);
  485. $result = array();
  486. $result[$info['order_num']] = array
  487. (
  488. 'type' => 'info',
  489. 'content' => array
  490. (
  491. array
  492. (
  493. array('订单状态', $status),
  494. array('代理商姓名', $user['name']),
  495. array('联系电话', $info['mobile']),
  496. ),
  497. $time,
  498. )
  499. );
  500. if ($info['address_id'] > 0) {
  501. $address = Dever::load('agent/address')->getOne($info['mid'], $info['address_id']);
  502. if ($address) {
  503. $result['收货地址'] = array
  504. (
  505. 'type' => 'info',
  506. 'content' => array
  507. (
  508. array
  509. (
  510. array('联系人', $address['contact']),
  511. array('联系电话', $address['mobile']),
  512. ),
  513. array
  514. (
  515. array('收货地址', $address['address_info']),
  516. array('门牌号', $address['house_number']),
  517. ),
  518. ),
  519. );
  520. }
  521. }
  522. $info['ps_info'] = Dever::db('agent/dh_order_ps')->find(array('order_id' => $info['id']));
  523. if ($info['ps_info']) {
  524. $info['ps_info']['service'] = Dever::array_decode($info['ps_info']['service']);
  525. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  526. if ($info['ps_info']['qs_date']) {
  527. $info['ps_info']['qs_date'] = date('Y-m-d H:i', $info['ps_info']['qs_date']);
  528. } else {
  529. $info['ps_info']['qs_date'] = '无';
  530. }
  531. $status = Dever::db('agent/dh_order_ps')->config['status'];
  532. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  533. $service = array();
  534. if ($info['ps_info']['service']) {
  535. foreach ($info['ps_info']['service'] as $k => $v) {
  536. if (isset($v['order_num']) && $v['order_num']) {
  537. $info['ps_info']['service'][$k]['service_name'] = '商家自送';
  538. if ($v['service_id'] > 0) {
  539. $service_info = Dever::db('shop/service')->find($v['service_id']);
  540. $info['ps_info']['service'][$k]['service_name'] = $service_info['name'];
  541. }
  542. $service[] = array
  543. (
  544. array('名称', $info['ps_info']['service'][$k]['service_name']),
  545. array('单号', isset($v['order_num']) ? $v['order_num'] : ''),
  546. );
  547. }
  548. }
  549. }
  550. $result['物流信息'] = array
  551. (
  552. 'type' => 'info',
  553. 'content' => array
  554. (
  555. array
  556. (
  557. array('发货时间', $info['ps_info']['cdate']),
  558. array('收货时间', $info['ps_info']['qs_date']),
  559. array('查单', '<a href="https://www.kuaidi100.com/?from=openv" target="_blank">点此手动查单</a>'),
  560. ),
  561. ),
  562. );
  563. $result['物流信息']['content'] = array_merge($result['物流信息']['content'], $service);
  564. }
  565. if (!$info['refund_cash']) {
  566. $info['refund_cash'] = 0;
  567. }
  568. if ($info['refund_cash'] > 0 && $tk) {
  569. $process = $tk_process[$tk['process']];
  570. $tk['cdate'] = date('Y-m-d H:i', $tk['cdate']);
  571. $result['退款信息'] = array
  572. (
  573. 'type' => 'info',
  574. 'content' => array
  575. (
  576. array
  577. (
  578. array('退款金额', $info['refund_cash']),
  579. array('数量', $info['num']),
  580. array('退款进度', $process),
  581. ),
  582. array
  583. (
  584. array('申请时间', $tk['cdate']),
  585. array('退款原因', $tk['desc']),
  586. ),
  587. ),
  588. );
  589. }
  590. $body = $refund_body = array();
  591. $body_total = array();
  592. $body_total['price'] = 0;
  593. $body_total['num'] = 0;
  594. $refund_body_total = array();
  595. $refund_body_total['price'] = 0;
  596. $refund_body_total['num'] = 0;
  597. $goods = Dever::db('agent/dh_order_goods')->select(array('order_id' => $info['id']));
  598. $goods_status = Dever::db('agent/dh_order_goods')->config['status'];
  599. foreach ($goods as $k => $v) {
  600. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  601. $status = Dever::status($goods_status, $v['status']);
  602. $tk = Dever::db('agent/dh_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
  603. if ($tk && $tk['process'] == 1) {
  604. $status = '申请' . $tk_status[$tk['status']];
  605. //$status .= '&nbsp;&nbsp;[' . $tk_process[$tk['process']] . ']';
  606. }
  607. if (isset($goods_info['sku'])) {
  608. $sku = $goods_info['sku']['string'];
  609. } else {
  610. $sku = '无';
  611. }
  612. if ($info['status'] > 1 && $info['status'] < 7) {
  613. $tui = Dever::url('buy_order_tui?shop_id='.$v['shop_id'].'&order_goods_id=' . $v['id'] . '&order_id=' . $info['id'] . '&cash=' . $v['price'], 'shop');
  614. $status = '<a href="javascript:;" onclick="fastEdit($(this), \''.$tui.'\', \'退款\', \'\', 1)" class="layui-btn">退款</a>';
  615. $status = '';
  616. }
  617. $d = array
  618. (
  619. 'pic' => $goods_info['cover'],
  620. 'name' => $goods_info['name'],
  621. 'sku' => $sku,
  622. 'price' => $v['price'],
  623. 'num' => $v['num'],
  624. 'status' => $status,
  625. );
  626. if ($tk && $v['status'] == 3) {
  627. $d['status'] = $tk_status[$tk['status']];
  628. $refund_body[] = $d;
  629. $price = $v['price']*$v['num'];
  630. $refund_body_total['price'] += $price;
  631. $refund_body_total['num'] += $v['num'];
  632. } else {
  633. $body[] = $d;
  634. $price = $v['price']*$v['num'];
  635. $body_total['price'] += $price;
  636. $body_total['num'] += $v['num'];
  637. }
  638. }
  639. if ($body) {
  640. if ($refund_body_total['price']) {
  641. $body_total['set_price'] = $body_total['price'] - $refund_body_total['price'];
  642. }
  643. if ($refund_body_total['num']) {
  644. $body_total['set_num'] = $body_total['num'] - $refund_body_total['num'];
  645. }
  646. $result['商品清单'] = array
  647. (
  648. 'type' => 'list',
  649. 'content' => $body,
  650. 'total' => $body_total,
  651. );
  652. }
  653. if ($refund_body) {
  654. $result['退款商品'] = array
  655. (
  656. 'type' => 'list',
  657. 'content' => $refund_body,
  658. 'total' => $refund_body_total,
  659. );
  660. }
  661. # 平台门店采购单
  662. $buy_order = Dever::db('shop/buy_order')->getDataToId(array('type' => 1, 'type_id' => $info['shop_id'], 'parent_type' => 3, 'parent_order_id' => $info['id']));
  663. $source_order_id = array();
  664. if ($buy_order) {
  665. $source_order_id = array_keys($buy_order);
  666. }
  667. # 平台门店结算单
  668. if ($source_order_id) {
  669. $cash_order = Dever::db('cash/order')->getDataByOrderId(array('source_order_id' => $source_order_id));
  670. $col = 'cash';
  671. }
  672. if (isset($cash_order) && $cash_order) {
  673. $head = array('序号', '结算单号', '数量', '金额', '结算类型', '入账状态', '审核状态', '结算时间', '审核备注', '管理');
  674. $body = array();
  675. $config = Dever::db('cash/order')->config;
  676. foreach ($cash_order as $k => $v) {
  677. $cdate = date('Y-m-d H:i', $v['cdate']);
  678. $fdate = $v['fdate'] ? date('Y-m-d H:i', $v['fdate']) : '';
  679. $operdate = $v['operdate'] ? date('Y-m-d H:i', $v['operdate']) : '';
  680. $cash = $v[$col];
  681. $url = Dever::url('project/database/list?project=shop&table=buy_order_goods&order_id='.$v['source_order_id'].'&page_type=1', 'manage');
  682. $v['source_order_num'] = '<a href="'.$url.'" style="color:blue">'.$v['source_order_num'].'</a>';
  683. $m = '';
  684. if ($v['audit'] == 1 && $v['audit_type'] == 2) {
  685. $url = Dever::url('project/database/update?project=cash&table=order&where_id='.$v['id'].'&col=audit,desc', 'manage');
  686. $m = '<a href="javascript:;" onclick="fastEdit($(this), \''.$url.'\', \'审核\', \'\', 1)" class="layui-btn">审核</a>';
  687. }
  688. $d = array
  689. (
  690. $k+1,
  691. $v['order_num'],
  692. $v['num'],
  693. $cash,
  694. $config['config_jstype'][$v['jstype']],
  695. $config['config_status'][$v['status']],
  696. $config['config_audit'][$v['audit']],
  697. $operdate,
  698. $v['desc'],
  699. $m,
  700. );
  701. $body[] = $d;
  702. }
  703. $result['对账清单'] = array
  704. (
  705. 'type' => 'table',
  706. 'content' => array
  707. (
  708. 'head' => $head,
  709. 'body' => $body,
  710. )
  711. );
  712. }
  713. $head_btn[] = array
  714. (
  715. 'type' => 'link',
  716. 'link' => Dever::url('project/database/list?project=agent&table=dh_order&menu=agent&search_option_state=1', 'manage'),
  717. 'name' => '返回上一页',
  718. );
  719. if (Dever::load('manage/auth')->checkFunc('agent.dh_order', 'editdhaddress', '修改地址')) {
  720. if ($info['address_id']>0){
  721. $head_btn[] = array
  722. (
  723. 'type' => 'edit',
  724. 'link' => Dever::url('project/database/update?project=agent&table=member_address&search_option_info_id='.$info['id'].'&search_option_mid='.$info['mid'].'&search_option_contact='.$address['contact'].'&search_option_mobile='.$address['mobile'].'&search_option_area='.$address['area'].'&search_option_address='.$address['address'].'&search_option_house_number='.$address['house_number'].'&status=1&col=contact,mobile,area,address,house_number,mid,info_id', 'manage'),
  725. 'name' => '修改地址',
  726. );
  727. }
  728. }
  729. $audit = Dever::input('audit');
  730. if($audit==1){
  731. # 审核信息
  732. $this->show_audit($info, $result);
  733. }
  734. $head = array
  735. (
  736. 'name' => '基本信息',
  737. 'btn' => $head_btn,
  738. );
  739. $html = Dever::show($head, $result);
  740. return $html;
  741. }
  742. public function addressUpdate($id,$name,$data)
  743. {
  744. $info_id = Dever::param('info_id',$data);
  745. if ($info_id) {
  746. $info = Dever::db('agent/dh_order')->find($info_id);
  747. if ($info) {
  748. $where['where_id'] = $info['id'];
  749. $where['address_id'] = $id;
  750. Dever::db('agent/dh_order')->update($where);
  751. }
  752. }
  753. }
  754. private function show_audit($info, &$result)
  755. {
  756. $info['type'] = 3;
  757. $goods = Dever::db('agent/dh_order_goods')->select(array('order_id' => $info['id'], 'status' => 1));
  758. $shop = Dever::db('shop/info')->find($info['shop_id']);
  759. list($data, $source) = Dever::load('mshop/lib/buy')->assign($goods, $info, $shop, true);
  760. // print_R($data);die;
  761. if ($info) {
  762. $head = array('序号', '商品编号', '商品名称', '供货商', '订货总数量', '商品计量单位', '订货单总金额');
  763. $body = array();
  764. $i = 0;
  765. if ($data) {
  766. foreach ($data as $k => $v) {
  767. foreach ($v as $v1) {
  768. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v1['goods_id'], $v1['sku_id']);
  769. if (isset($goods_info['sku'])) {
  770. $sku = '[' . $goods_info['sku']['string'] . ']';
  771. } else {
  772. $sku = '';
  773. }
  774. $d = array
  775. (
  776. $i+1,
  777. $goods_info['code'],
  778. $goods_info['name'] . $sku,
  779. Dever::load('mshop/lib/buy')->getSelect($source, $v1['source_id'], $v1['order_goods_id']),
  780. $v1['num'],
  781. $goods_info['unit'],
  782. $v1['price'] * $v1['num'],
  783. );
  784. $body[] = $d;
  785. $i++;
  786. }
  787. }
  788. }
  789. if ($goods) {
  790. foreach ($goods as $k => $v) {
  791. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  792. if (isset($goods_info['sku'])) {
  793. $sku = '[' . $goods_info['sku']['string'] . ']';
  794. } else {
  795. $sku = '';
  796. }
  797. $d = array
  798. (
  799. $i+1,
  800. $goods_info['code'],
  801. $goods_info['name'] . $sku,
  802. Dever::load('mshop/lib/buy')->getSelect($source, false, $v['id']),
  803. $v['num'],
  804. $goods_info['unit'],
  805. $v['price'] * $v['num'],
  806. );
  807. $body[] = $d;
  808. $i++;
  809. }
  810. }
  811. $btn = array();
  812. $btn[] = array(
  813. 'type' => 'click',
  814. 'content' => 'setAudit()',
  815. 'name' => '全部调整<script>function setAudit(){layer.confirm(\'确定进行此项操作吗?\', function(){var audit = $(\'.audit_set\').val();$(\'.audit\').each(function(){$(this).val(audit);layui.form.render();layer.closeAll()})})}</script>'
  816. );
  817. $result['商品拆单'] = array
  818. (
  819. 'type' => 'table',
  820. 'head' => array('name' => '供货商:' . Dever::load('mshop/lib/buy')->getSelect($source, false, false), 'btn' => $btn),
  821. 'content' => array
  822. (
  823. 'head' => $head,
  824. 'body' => $body,
  825. )
  826. );
  827. $url = Dever::url('lib/dhorder.audit_set_commit?json=1&order_id=' . $info['id'], 'agent');
  828. // print_R($url);die;
  829. $result['审核结果'] = array
  830. (
  831. 'type' => 'html',
  832. '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>',
  833. );
  834. }
  835. }
  836. # 采购订单审核 手动拆单
  837. public function audit_set_commit_api()
  838. {
  839. Dever::config('base')->hook = true;
  840. //{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"}
  841. $id = Dever::input('order_id');
  842. $goods = Dever::input('goods');
  843. $audit = Dever::input('audit');
  844. $audit_text = Dever::input('audit_text');
  845. $info = Dever::db('agent/dh_order')->one($id);
  846. if ($audit > 1 && $info && $info['status'] == 2) {
  847. $shop = Dever::db('shop/info')->find($info['shop_id']);
  848. if ($audit == 2) {
  849. Dever::db('agent/dh_order')->update(array('where_id' => $info['id'], 'audit' => $audit, 'audit_desc' => $audit_text));
  850. # 成功
  851. if (strstr($info['order_num'], '_')) {
  852. $temp = explode('_', $info['order_num']);
  853. $info['order_num'] = $temp[0];
  854. $info['order_index'] = $temp[1];
  855. } else {
  856. $info['order_index'] = 0;
  857. }
  858. $goods = explode(',', $goods);
  859. $data = $other = array();
  860. foreach ($goods as $k => $v) {
  861. list($source_type, $source_id, $name, $goods_id) = explode('_', $v);
  862. $gv = Dever::db('agent/dh_order_goods')->find($goods_id);
  863. if ($source_id > 0) {
  864. $key = $source_type . '_' . $source_id;
  865. $data[$key][] = array
  866. (
  867. 'area' => $shop['area'],
  868. 'order_id' => $info['id'],
  869. 'order_type' => 3,
  870. 'order_num' => $info['order_num'],
  871. 'order_index' => $info['order_index'],
  872. 'type' => 1,
  873. 'type_id' => $shop['id'],
  874. 'source_type' => $source_type,
  875. 'source_id' => $source_id,
  876. 'order_goods_id' => $gv['id'],
  877. 'goods_id' => $gv['goods_id'],
  878. 'sku_id' => $gv['sku_id'],
  879. 'price' => $gv['price'],
  880. 'p_price' => $gv['price'],
  881. 'num' => $gv['num'],
  882. );
  883. } else {
  884. $other[] = $gv;
  885. }
  886. }
  887. $this->audit_action($data, $other, $info, $shop, 1);
  888. } else {
  889. Dever::db('agent/dh_order')->update(array('where_id' => $info['id'], 'audit' => $audit, 'audit_desc' => $audit_text, 'status' => 7));
  890. Dever::load('agent/lib/dhorder')->updateSell($info, 2);
  891. if (Dever::project('sms')) {
  892. $member = Dever::db('agent/member')->find($info['mid']);
  893. Dever::load('sms/api')->send('daili_goods_no', $member['mobile'], array('name' => $member['name'], 'content' => $audit_text));
  894. }
  895. }
  896. }
  897. return Dever::url('project/database/list&project=agent&table=dh_order&menu=agent&search_option_state=1&search_option_shop_type=2&menu_id=218', 'manage');
  898. }
  899. # 采购订单审核 拆单
  900. public function audit_commit()
  901. {
  902. $order_id = Dever::input('order_id');
  903. $order = Dever::db('agent/dh_order')->find($order_id);
  904. if (!$order || ($order && $order['status'] != 2)) {
  905. Dever::alert('当前订单状态无法拆单');
  906. }
  907. // if ($order['shop_type'] != 2) {
  908. // Dever::alert('这不是平台店的订单');
  909. // }
  910. $order['type'] = 3;
  911. $goods = Dever::db('agent/dh_order_goods')->select(array('order_id' => $order_id));
  912. $shop = Dever::db('shop/info')->find($order['shop_id']);
  913. $data = Dever::load('mshop/lib/buy')->assign($goods, $order, $shop);
  914. $this->audit_action($data, $goods, $order, $shop);
  915. return 'ok';
  916. }
  917. private function audit_action($data, $goods, $order, $shop, $state = 2)
  918. {
  919. $send = array();
  920. if (!$goods && $data) {
  921. # 开始生成订单
  922. $i = $order['order_index'];
  923. foreach ($data as $k => $v) {
  924. foreach ($v as $k1 => $v1) {
  925. $v1['order_num'] .= '_' . $i;
  926. $state = Dever::load('mshop/lib/buy')->createOrder($v1);
  927. $send[$state] = $v1;
  928. }
  929. $i++;
  930. }
  931. Dever::db('agent/dh_order')->update(array('where_id' => $order['id'], 'status' => 3));
  932. } else {
  933. Dever::db('agent/dh_order')->update(array('where_id' => $order['id'], 'audit' => 1));
  934. if ($goods) {
  935. $string = array();
  936. foreach ($goods as $k => $v) {
  937. $info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  938. if ($info['sku_name']) {
  939. $info['name'] .= '('.$info['sku_name'].')';
  940. }
  941. $string[] = $info['name'];
  942. }
  943. $string = implode('、', $string);
  944. if ($state == 1) {
  945. Dever::alert('商品:'.$string.'未选择供货商');
  946. } else {
  947. Dever::alert('商品:'.$string.'库存不足或厂家未生产,请联系供货商备货,该订单进入重新审核状态');
  948. }
  949. Dever::alert('商品:'.$string.'库存不足或厂家未生产,请联系供货商备货,该订单进入重新审核状态');
  950. } else {
  951. if ($state == 1) {
  952. Dever::alert('未选择供货商,该订单进入重新审核状态');
  953. } else {
  954. Dever::alert('供货商库存不足,请联系供货商备货,该订单进入重新审核状态');
  955. }
  956. }
  957. }
  958. if ($send) {
  959. foreach ($send as $k => $v) {
  960. Dever::load('mshop/lib/buy')->sendMsg($v['source_type'], $v['source_id'], $k);
  961. $order_info = Dever::db('shop/buy_order')->find(array('id' => $k, 'clear' => true));
  962. Dever::load('cash/lib/order')->up($order_info, 1, 1);
  963. }
  964. }
  965. }
  966. # 第三方确认
  967. public function tixian()
  968. {
  969. $param = array();
  970. ksort($param);
  971. $string = array();
  972. foreach ($param as $k => $v) {
  973. if (strstr($v, '&') || strstr($v, '@')) {
  974. $v = urlencode($v);
  975. }
  976. $string[] = $k . '=' . $v;
  977. }
  978. }
  979. # 发货
  980. public function notice($info)
  981. {
  982. $config = Dever::db('agent/dh_order')->config;
  983. $state = Dever::db('agent/dh_order')->update(array('where_id' => $info['id'], 'status' => 3, 'operdate' => time()));
  984. }
  985. # 采购订单审核 拆单
  986. /*
  987. public function audit_commit()
  988. {
  989. $order_id = Dever::input('order_id');
  990. $order = Dever::db('agent/dh_order')->find($order_id);
  991. if (!$order || ($order && $order['status'] != 2)) {
  992. Dever::alert('当前订单状态无法拆单');
  993. }
  994. $order['type'] = 3;
  995. $goods = Dever::db('agent/dh_order_goods')->select(array('order_id' => $order_id));
  996. $shop = Dever::db('shop/info')->find($order['shop_id']);
  997. $data = Dever::load('mshop/lib/buy')->assign($goods, $order, $shop);
  998. $send = array();
  999. if (!$goods && $data) {
  1000. # 开始生成订单
  1001. $i = $order['order_index'];
  1002. foreach ($data as $k => $v) {
  1003. foreach ($v as $k1 => $v1) {
  1004. $v1['order_num'] .= '_' . $i;
  1005. $state = Dever::load('mshop/lib/buy')->createOrder($v1);
  1006. $send[$state] = $v1;
  1007. }
  1008. $i++;
  1009. }
  1010. Dever::db('agent/dh_order')->update(array('where_id' => $order['id'], 'status' => 3));
  1011. } else {
  1012. Dever::db('agent/dh_order')->update(array('where_id' => $order['id'], 'audit' => 1));
  1013. if ($goods) {
  1014. $string = array();
  1015. foreach ($goods as $k => $v) {
  1016. $info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  1017. if ($info['sku_name']) {
  1018. $info['name'] .= '('.$info['sku_name'].')';
  1019. }
  1020. $string[] = $info['name'];
  1021. }
  1022. $string = implode('、', $string);
  1023. Dever::alert('商品:'.$string.'库存不足或厂家未生产,请联系供货商备货,该订单进入重新审核状态');
  1024. } else {
  1025. Dever::alert('供货商库存不足,请联系供货商备货,该订单进入重新审核状态');
  1026. }
  1027. }
  1028. if ($send) {
  1029. foreach ($send as $k => $v) {
  1030. Dever::load('mshop/lib/buy')->sendMsg($v['source_type'], $v['source_id'], $k);
  1031. $order_info = Dever::db('shop/buy_order')->find(array('id' => $k, 'clear' => true));
  1032. Dever::load('cash/lib/order')->up($order_info, 1, 1);
  1033. }
  1034. }
  1035. return 'ok';
  1036. }
  1037. */
  1038. public function goodsd_api($id){
  1039. $name = '';
  1040. $goods = Dever::db('agent/dh_order_goods')->state(array('order_id'=>$id));
  1041. foreach($goods as $k1 => $v1){
  1042. $goods_info[] = Dever::load('goods/lib/info')->getInfoBySku($v1['goods_id'], $v1['sku_id']);
  1043. }
  1044. if(isset($goods_info)){
  1045. foreach($goods_info as $v2){
  1046. $goods_name[] = $v2['name'];
  1047. }
  1048. $name = implode(',',$goods_name);
  1049. }
  1050. return $name;
  1051. }
  1052. #权益订单导出
  1053. public function out_dhorder_api($data){
  1054. $header = array('订单编号', '会员姓名', '会员等级', '会员手机号', '收货姓名(或自提人)', '联系电话', '收货地址', '商品名称', '商品数量', '商品原价','订单状态','下单时间', '付款时间', '发货时间','完成时间','卖家订单备注','权益发放时间','代理商终审时间');
  1055. $body = array();
  1056. foreach($data as $k => $v){
  1057. $member = Dever::db('agent/member')->find(array('id'=>$v['mid']));
  1058. $role = Dever::db('setting/role')->find(array('id'=>$member['role']));
  1059. $address = $res = array();
  1060. if ($v['address_id'] && $v['address_id'] > 0) {
  1061. $address = Dever::db('agent/member_address')->find(array('id'=>$v['address_id']));
  1062. if ($address) {
  1063. $res=Dever::load('agent/address')->getOne($address['mid'],$address['id']);
  1064. }
  1065. }
  1066. $fhdate = '';
  1067. $buy_order = Dever::db('shop/buy_order')->find(array('parent_id' => 3, 'parent_order_id' => $v['id']));
  1068. if ($buy_order) {
  1069. $ps = Dever::db('shop/buy_order_ps')->find(array('order_id' => $buy_order['id']));
  1070. $fhdate = ($ps && $ps['cdate']) ? date('Y-m-d H:i', $ps['cdate']) : '-';
  1071. }
  1072. $fdate = '';
  1073. if($v['fdate']){
  1074. $fdate = date('Y-m-d H:i',$v['fdate']);
  1075. }
  1076. $quanyi = '';
  1077. $member_goods = Dever::db('agent/member_goods')->getQone(array('type'=>$v['type'],'type_id'=>$v['type_id']));
  1078. if($member_goods && $member_goods['cdate']){
  1079. $quanyi = date('Y-m-d H:i',$member_goods['cdate']);
  1080. }
  1081. $order = Dever::db('agent/order')->find(array('mid'=>$member['id']));
  1082. $zhongshen = '';
  1083. $process = Dever::db('agent/order_process')->state(array('order_id'=>$order['id']));
  1084. foreach($process as $k1 => $v1){
  1085. if($v1['status'] == 2){
  1086. $zhongshen = date('Y-m-d H:i',$v1['cdate']);
  1087. }
  1088. }
  1089. $name = '';
  1090. if($v['type'] == 1){
  1091. $name = $this->goodsd_api($v['id']);
  1092. }elseif($v['type'] == 2){
  1093. $goods = Dever::db('card/info')->find(array('id'=>$v['type_id']));
  1094. if($goods){
  1095. $name = $goods['name'];
  1096. }
  1097. }elseif($v['type'] == 3){
  1098. $goods = Dever::db('setting/prize')->find(array('id'=>$v['type_id']));
  1099. if($goods){
  1100. $name = $goods['name'];
  1101. }
  1102. }
  1103. $goods_status = Dever::db('agent/dh_order')->config['status'];
  1104. $status = Dever::status($goods_status, $v['status']);
  1105. $d = array
  1106. (
  1107. $v['order_num'],
  1108. $member['name'],
  1109. $role['name'],
  1110. $member['mobile'],
  1111. isset($address['contact']) ? $address['contact'] : '',
  1112. isset($address['mobile']) ? $address['mobile'] : '',
  1113. (isset($res['address_info']) ? $res['address_info'] : '').(isset($address['house_number']) ? $address['house_number'] : ''),
  1114. $name,
  1115. $v['num'],
  1116. $v['price'],
  1117. $status,
  1118. date('Y-m-d H:i',$v['cdate']),
  1119. date('Y-m-d H:i',$v['cdate']),
  1120. $fhdate,
  1121. $fdate,
  1122. $v['audit_desc'],
  1123. $quanyi,
  1124. $zhongshen,
  1125. );
  1126. $body[] = $d;
  1127. }
  1128. $file = Dever::input('excel_name');
  1129. return Dever::excelExport($body, $header, $file);
  1130. }
  1131. #权益管理导出'5月已用面值',
  1132. public function out_member_goods_api($data){
  1133. $header = array('权益名称', '会员等级', '姓名', '手机号', '卡号','剩余数量', '总数量', '单件价值', '未兑换价值', '已兑换价值', '已用面值','备注','获取方式', '获取时间','代理商注册时间','礼品卡有效期');
  1134. $body = array();
  1135. foreach($data as $k => $v){
  1136. $member = Dever::db('agent/member')->find(array('id'=>$v['mid']));
  1137. $member_cdate = '-';
  1138. if($member && $member['cdate']){
  1139. $member_cdate = date('Y-m-d H:i',$member['cdate']);
  1140. }
  1141. $role = Dever::db('setting/role')->find(array('id'=>$member['role']));
  1142. $wd = ($v['total_num'] - $v['sell_num'])*$v['price'];
  1143. $yd = $v['sell_num']*$v['price'];
  1144. $card = '-';
  1145. $mianzhi = '-';
  1146. $money = '-';
  1147. $youxiao = '';
  1148. if($v['type'] == 2){
  1149. $code = Dever::db('card/code')->find($v['sku_id']);
  1150. $start = '1651334400';
  1151. $end = '1654012799';
  1152. if($code){
  1153. $card_info = Dever::db('card/info')->find($code['card_id']);
  1154. if ($card_info && $card_info['end_dh_day']) {
  1155. $youxiao = date('Y-m-d H:i',strtotime(+ $card_info['end_dh_day'].'day',$code['bdate']));
  1156. }
  1157. // $sell_order = Dever::db('shop/sell_order')->getSumCdate(array('start'=>$start,'end'=>$end,'card_code_id'=>$code['id'],'state'=>1));
  1158. // if($sell_order && $sell_order['total']){
  1159. // $money = $sell_order['total'];
  1160. // }
  1161. $card = $code['card']."\t";
  1162. $mianzhi = $code['use_cash'];
  1163. }
  1164. }
  1165. $desc = '';
  1166. $member_goods_log = Dever::db('agent/member_goods_log')->getOne(array('type'=>$v['type'],'type_id'=>$v['type_id'],'sku_id'=>$v['sku_id'],'state'=>1));
  1167. if($member_goods_log && $member_goods_log['desc']){
  1168. $desc = $member_goods_log['desc'];
  1169. }
  1170. $d = array
  1171. (
  1172. $v['name'],
  1173. $role['name'],
  1174. $member['name'],
  1175. $member['mobile'],
  1176. $card,
  1177. $v['total_num']-$v['sell_num'],
  1178. $v['total_num'],
  1179. $v['price'],
  1180. $wd,
  1181. $yd,
  1182. $mianzhi,
  1183. // $money,
  1184. $desc,
  1185. '',
  1186. date('Y-m-d H:i',$v['cdate']),
  1187. $member_cdate,
  1188. $youxiao,
  1189. );
  1190. $body[] = $d;
  1191. }
  1192. $file = Dever::input('excel_name');
  1193. return Dever::excelExport($body, $header, $file);
  1194. }
  1195. #未使用数据导出
  1196. public function out_member_nogoods($data)
  1197. {
  1198. $header = array('权益名称', '会员等级', '姓名', '手机号', '卡号','剩余数量', '总数量', '单件价值', '未兑换价值', '已兑换价值', '已用面值','备注','获取方式', '获取时间','代理商注册时间','礼品卡有效期');
  1199. $body = array();
  1200. foreach ($data as $k => $v) {
  1201. $member = Dever::db('agent/member')->find(array('id'=>$v['mid']));
  1202. $member_cdate = '-';
  1203. if($member && $member['cdate']){
  1204. $member_cdate = date('Y-m-d H:i',$member['cdate']);
  1205. }
  1206. $role = Dever::db('setting/role')->find(array('id'=>$member['role']));
  1207. $wd = ($v['total_num'] - $v['sell_num'])*$v['price'];
  1208. $yd = $v['sell_num']*$v['price'];
  1209. $card = '-';
  1210. $mianzhi = '-';
  1211. $money = '-';
  1212. $youxiao = '';
  1213. if ($v['type'] == 1) {
  1214. if ($v['total_num'] - $v['sell_num'] == 0) {
  1215. continue;
  1216. }
  1217. }
  1218. if($v['type'] == 2){
  1219. $code = Dever::db('card/code')->find($v['sku_id']);
  1220. if($code){
  1221. if ($code['use_cash'] == $yd && $v['sell_num'] != 0){
  1222. continue;
  1223. }
  1224. $card_info = Dever::db('card/info')->find($code['card_id']);
  1225. if ($card_info && $card_info['end_dh_day']) {
  1226. $youxiao = date('Y-m-d H:i',strtotime(+ $card_info['end_dh_day'].'day',$code['bdate']));
  1227. }
  1228. $card = $code['card']."\t";
  1229. $mianzhi = $code['use_cash'];
  1230. }
  1231. }
  1232. $desc = '';
  1233. $member_goods_log = Dever::db('agent/member_goods_log')->getOne(array('type'=>$v['type'],'type_id'=>$v['type_id'],'sku_id'=>$v['sku_id'],'state'=>1));
  1234. if($member_goods_log && $member_goods_log['desc']){
  1235. $desc = $member_goods_log['desc'];
  1236. }
  1237. $d = array
  1238. (
  1239. $v['name'],
  1240. $role['name'],
  1241. $member['name'],
  1242. $member['mobile'],
  1243. $card,
  1244. $v['total_num']-$v['sell_num'],
  1245. $v['total_num'],
  1246. $v['price'],
  1247. $wd,
  1248. $yd,
  1249. $mianzhi,
  1250. $desc,
  1251. '',
  1252. date('Y-m-d H:i',$v['cdate']),
  1253. $member_cdate,
  1254. $youxiao,
  1255. );
  1256. $body[] = $d;
  1257. }
  1258. $file = Dever::input('excel_name');
  1259. return Dever::excelExport($body, $header, $file);
  1260. }
  1261. public function head($type=false){
  1262. $data = Dever::db('agent/member_goods')->getProduct(array('type'=>'1,2','state'=>1));
  1263. $res = array();
  1264. foreach($data as $k => $v){
  1265. if($v['name'] && $v['total_num']>0 && $v['type_id']!=8){
  1266. $key = $v['type'] . '_' . $v['type_id'];
  1267. if ($v['type'] == 2) {
  1268. $card = Dever::db('card/info')->find($v['type_id']);
  1269. $v['name'] = $card['name'];
  1270. }
  1271. if ($v['type'] == 1) {
  1272. $res[0][$key] = array($v['name'], '价值');
  1273. } else {
  1274. $res[0][$key] = array($v['name'], '');
  1275. $res[2][$key] = $v['name'];
  1276. }
  1277. $res[1][$key] = $v['name'];
  1278. }
  1279. }
  1280. return $res;
  1281. }
  1282. public function out_equity_api($data){
  1283. $start = Dever::input('search_option_start_udate');
  1284. $end = Dever::input('search_option_end_udate');
  1285. $fstart = Dever::input('search_option_start_dever_auth');
  1286. $fend = Dever::input('search_option_end_dever_auth');
  1287. if($start && $end){
  1288. $where['start'] = $start;
  1289. $where['end'] = $end;
  1290. }else{
  1291. $where['start']=Dever::maketime(date('Y-m-01') . ' 00:00:00');
  1292. $where['end'] = strtotime(date('Y-m-d 23:59:59',
  1293. strtotime(date('Y-m-01')."+1 month - 1 day")));
  1294. }
  1295. $w = array();
  1296. if ($fstart) {
  1297. $w['start'] = $fstart;
  1298. }
  1299. if ($fend) {
  1300. $w['end'] = $fend;
  1301. }
  1302. $where['type'] = '1,2';
  1303. $res = $this->head(1);
  1304. $header = array('基础信息', '', '', '', '','', '', '', '');
  1305. $body = array();
  1306. $pg = Dever::input('pg');
  1307. if ($pg == 1) {
  1308. $body[] = array('代理商姓名', '角色', '电话', '区域','应收合计', '代理费', '产品费(商品)', '产品费(电器卡)','产品费(菜卡)');
  1309. }
  1310. // print_R($res[0]);die;
  1311. $i = 0;
  1312. foreach ($res[0] as $k => $v) {
  1313. if ($i == 0) {
  1314. $header[] = '发放权益';
  1315. } else {
  1316. $header[] = '';
  1317. }
  1318. isset($body[0]) && $body[0][] = $v[0];
  1319. if ($v[1]) {
  1320. isset($body[0]) && $body[0][] = $v[1];
  1321. $header[] = '';
  1322. }
  1323. $i++;
  1324. }
  1325. $i = 0;
  1326. foreach ($res[1] as $k => $v) {
  1327. isset($body[0]) && $body[0][] = $v;
  1328. if ($i == 0) {
  1329. $header[] = '兑换权益';
  1330. } else {
  1331. $header[] = '';
  1332. }
  1333. $i++;
  1334. }
  1335. $i = 0;
  1336. foreach ($res[2] as $k => $v) {
  1337. isset($body[0]) && $body[0][] = $v;
  1338. if ($i == 0) {
  1339. $header[] = '使用金额';
  1340. } else {
  1341. $header[] = '';
  1342. }
  1343. $i++;
  1344. }
  1345. $body_member = array();
  1346. // $data = Dever::db('agent/member_goods')->getSearchAll($where);
  1347. foreach ($data as $k => $v) {
  1348. $member_goods[] = Dever::db('agent/member_goods')->getTOne(array('mid'=>$v['id'],'type'=>'1,2'));
  1349. }
  1350. foreach($member_goods as $k => $v){
  1351. if($v){
  1352. $body_member[] = $v;
  1353. }
  1354. }
  1355. foreach ($body_member as $k => $v) {
  1356. $month = date('Y-m',$v['cdate']);
  1357. $member = Dever::db('agent/member')->find($v['mid']);
  1358. $role = Dever::db('setting/role')->find(array('id'=>$member['role']));
  1359. $area = '';
  1360. if($member['area']){
  1361. $area = Dever::load("area/api.string", $member['area']);
  1362. }
  1363. $order = Dever::db('agent/order')->getOrder(array('mid'=>$v['mid']));
  1364. // print_R($order);die;
  1365. // $order = Dever::db('agent/order')->find(array('mid'=>$v['mid']));
  1366. $heji = 0;
  1367. $product_cash = 0;
  1368. $dianqi_cash = 0;
  1369. $caipin_cash = 0;
  1370. $d = array
  1371. (
  1372. $member['name'],
  1373. $role['name'],
  1374. $member['mobile'],
  1375. $area,
  1376. 0,
  1377. // $order['agent_cash'],
  1378. // $month,
  1379. 0,
  1380. 0,
  1381. 0,
  1382. 0,
  1383. );
  1384. $i = count($d);
  1385. $goods = array();
  1386. foreach ($res[0] as $k1 => $v1) {
  1387. list($type, $type_id) = explode('_', $k1);
  1388. $goods[$k1] = Dever::db('agent/member_goods')->getTotal(array('end'=>$where['end'],'mid'=>$v['mid'],'type'=>$type, 'type_id' => $type_id));
  1389. $d[$i] = 0;
  1390. if ($goods[$k1]) {
  1391. if(strstr($v1[0],'卡')){
  1392. $d[$i] = $goods[$k1]['cash'] ? $goods[$k1]['cash'] : 0;
  1393. }else{
  1394. $d[$i] = $goods[$k1]['total_num'] ? $goods[$k1]['total_num'] : 0;
  1395. }
  1396. }
  1397. if ($v1[1]) {
  1398. $i++;
  1399. $d[$i] = 0;
  1400. if ($goods[$k1]) {
  1401. $d[$i] = $goods[$k1]['cash'] ? $goods[$k1]['cash'] : 0;
  1402. }
  1403. }
  1404. if ($type == 1) {
  1405. $product_cash += $goods[$k1]['cash'] ? $goods[$k1]['cash'] : 0;
  1406. }
  1407. if ($type == 2 ) {
  1408. if(strstr($v1[0], '器具')){
  1409. $dianqi_cash += $goods[$k1]['cash'] ? $goods[$k1]['cash'] : 0;
  1410. } else {
  1411. $caipin_cash += $goods[$k1]['cash'] ? $goods[$k1]['cash'] : 0;
  1412. }
  1413. }
  1414. $i++;
  1415. }
  1416. // $heji = $order['agent_cash'] + $product_cash + $dianqi_cash + $caipin_cash;
  1417. $d[4] = $order['agent_cash'] - $product_cash - $dianqi_cash - $caipin_cash;
  1418. $d[5] = $order['agent_cash'];
  1419. $d[6] = $product_cash;
  1420. $d[7] = $dianqi_cash;
  1421. $d[8] = $caipin_cash;
  1422. $w1 = $w;
  1423. $w1['mid'] = $v['mid'];
  1424. $dh_order_ps = Dever::db('agent/dh_order_ps')->getDui($w1);
  1425. foreach ($res[1] as $k1 => $v1) {
  1426. $d[$i] = 0;
  1427. list($type, $type_id) = explode('_', $k1);
  1428. if ($goods[$k1]) {
  1429. if ($type == 2 && strstr($v1,'卡')) {
  1430. $n = $w;
  1431. $n['mid'] = $v['mid'];
  1432. $n['type'] = $type;
  1433. $n['type_id'] = $type_id;
  1434. $dh_goods = Dever::db('agent/member_goods')->getDhTotal($n);
  1435. $d[$i] = $dh_goods['sell_num'] ? $dh_goods['cash']*$dh_goods['sell_num'] : 0;
  1436. } elseif($type == 1) {
  1437. if ($dh_order_ps) {
  1438. foreach($dh_order_ps as $a => $b) {
  1439. $dh_order_goods = Dever::db('agent/dh_order_goods')->find(array('order_id'=>$b['order_id'], 'goods_id' => $type_id, 'status' => 1));
  1440. if($dh_order_goods){
  1441. $d[$i] += $dh_order_goods['num'];
  1442. }
  1443. }
  1444. }
  1445. // $d[$i] = $goods[$k1]['sell_num'] ? $goods[$k1]['sell_num'] : 0;
  1446. }
  1447. }
  1448. $i++;
  1449. }
  1450. foreach ($res[2] as $k1 => $v1) {
  1451. list($type, $type_id) = explode('_', $k1);
  1452. $w1 = array();
  1453. $w1['mid'] = $v['mid'];
  1454. $w1['type'] = $type;
  1455. $w1['type_id'] = $type_id;
  1456. $sku = Dever::db('agent/member_goods')->getCardAll($w1);
  1457. $array = array_column($sku, 'sku_id');
  1458. $sku_id = implode(",",$array);
  1459. if($sku_id){
  1460. $w1 = $w;
  1461. $w1['card_code_id'] = $sku_id;
  1462. $w1['status'] = '4,5,6';
  1463. $sell_order[$k1] = Dever::db('shop/sell_order')->getCardAlls($w1);
  1464. }else{
  1465. $sell_order[$k1]['total'] = 0;
  1466. }
  1467. if($sell_order[$k1]){
  1468. $d[$i] = $sell_order[$k1]['total'] ? $sell_order[$k1]['total'] : 0 ;
  1469. }
  1470. $i++;
  1471. }
  1472. $body[] = $d;
  1473. }
  1474. $file = Dever::input('excel_name');
  1475. return Dever::excelExport($body, $header, $file);
  1476. }
  1477. }