Buy.php 82 KB

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