Buy.php 82 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260
  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. $store_type = Dever::input('store_type');
  715. if ($info['status'] == 3 && $info['type'] == 1 && $store_type == 1) {
  716. $button[] = array
  717. (
  718. 'type' => 'edit',
  719. 'link' => Dever::url('order_send?id=' . $info['id'] . '&type=2', 'store'),
  720. 'name' => '配送发货',
  721. );
  722. }
  723. if ($info['status'] == 4 && $info['type'] == 2) {
  724. $button[] = array
  725. (
  726. 'type' => 'action',
  727. 'link' => Dever::url('admin/factory_order.action_commit?order_id='.$info['id'], 'store'),
  728. 'name' => '确认收货',
  729. );
  730. }
  731. $config['phone'] = '您的专属客服:' . $config['kf_name'] . ',联系电话:' . $config['phone'];
  732. $button[] = array
  733. (
  734. 'type' => 'alert',
  735. 'content' => $config['phone'],
  736. 'name' => '联系平台',
  737. );
  738. }
  739. $url = Dever::url('lib/buy.refund_commit?json=1', 'mshop');
  740. if (!$info['refund_cash']) {
  741. $info['refund_cash'] = 0;
  742. }
  743. if ($info['refund_cash'] > 0 && $tk) {
  744. if ($type == 2) {
  745. $info['refund_cash'] = $info['refund_p_cash'];
  746. }
  747. if ($tk['process'] == 1) {
  748. $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>';
  749. } else {
  750. $process = $tk_process[$tk['process']];
  751. }
  752. $desc = array();
  753. $desc['退款原因'] = $tk['desc'];
  754. $desc['退款图片'] = '暂无';
  755. if ($tk['pic']) {
  756. $pic = explode(',', $tk['pic']);
  757. $tk['pic'] = '';
  758. foreach ($pic as $k => $v) {
  759. $tk['pic'] .= '<a href="'.$v.'" target="_blank"><img src="'.$v.'" width="100"/></a>';
  760. }
  761. }
  762. ;
  763. $tk['cdate'] = date('Y-m-d H:i', $tk['cdate']);
  764. $result['退款信息'] = array
  765. (
  766. 'type' => 'info',
  767. 'content' => array
  768. (
  769. array
  770. (
  771. array('退款金额', $info['refund_cash']),
  772. array('数量', $info['num']),
  773. array('退款进度', $process),
  774. ),
  775. array
  776. (
  777. array('申请时间', $tk['cdate']),
  778. array('申请原因', $tk['desc']),
  779. array('图片', $tk['pic']),
  780. ),
  781. ),
  782. );
  783. }
  784. $info['ps_info'] = Dever::db('shop/buy_order_ps')->find(array('order_id' => $info['id']));
  785. if ($info['ps_info']) {
  786. $info['ps_info']['service'] = Dever::array_decode($info['ps_info']['service']);
  787. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  788. if ($info['ps_info']['ydate']) {
  789. $info['ps_info']['ydate'] = date('Y-m-d H:i', $info['ps_info']['ydate']);
  790. } else {
  791. $info['ps_info']['ydate'] = '无';
  792. }
  793. $status = Dever::db('shop/buy_order_ps')->config['status'];
  794. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  795. $service = array();
  796. if ($info['ps_info']['service']) {
  797. foreach ($info['ps_info']['service'] as $k => $v) {
  798. $info['ps_info']['service'][$k]['service_name'] = '供应商自送';
  799. if ($v['service_id'] > 0) {
  800. $service_info = Dever::db('shop/service')->find($v['service_id']);
  801. $info['ps_info']['service'][$k]['service_name'] = $service_info['name'];
  802. }
  803. $service[] = array
  804. (
  805. array('名称', $info['ps_info']['service'][$k]['service_name']),
  806. array('单号', $v['order_num']),
  807. );
  808. }
  809. }
  810. $result['物流信息'] = array
  811. (
  812. 'type' => 'info',
  813. 'content' => array
  814. (
  815. array
  816. (
  817. array('发货时间', $info['ps_info']['cdate']),
  818. array('收货时间', $info['ps_info']['ydate']),
  819. array('查单', '<a href="https://www.kuaidi100.com/?from=openv" target="_blank">点此手动查单</a>'),
  820. ),
  821. ),
  822. );
  823. $result['物流信息']['content'] = array_merge($result['物流信息']['content'], $service);
  824. /*
  825. if ($noprice == 1) {
  826. $result['物流信息'] = array
  827. (
  828. 'type' => 'info',
  829. 'content' => array
  830. (
  831. array
  832. (
  833. array('名称', $info['ps_info']['service_name']),
  834. array('单号', $info['ps_info']['order_num']),
  835. ),
  836. array
  837. (
  838. array('数量', $info['ps_info']['num']),
  839. array('重量', $info['ps_info']['zl']),
  840. array('体积', $info['ps_info']['tj']),
  841. ),
  842. array
  843. (
  844. array('发货时间', $info['ps_info']['cdate']),
  845. array('收货时间', $info['ps_info']['ydate']),
  846. ),
  847. ),
  848. );
  849. } else {
  850. $result['物流信息'] = array
  851. (
  852. 'type' => 'info',
  853. 'content' => array
  854. (
  855. array
  856. (
  857. array('名称', $info['ps_info']['service_name']),
  858. array('单号', $info['ps_info']['order_num']),
  859. array('费用', $info['ps_info']['price']),
  860. ),
  861. array
  862. (
  863. array('配货员', $info['ps_info']['phy']),
  864. array('打包员', $info['ps_info']['dby']),
  865. ),
  866. array
  867. (
  868. array('数量', $info['ps_info']['num']),
  869. array('重量', $info['ps_info']['zl']),
  870. array('体积', $info['ps_info']['tj']),
  871. ),
  872. array
  873. (
  874. array('发货时间', $info['ps_info']['cdate']),
  875. array('收货时间', $info['ps_info']['ydate']),
  876. ),
  877. ),
  878. );
  879. }
  880. */
  881. }
  882. $body = array();
  883. $body_total = array();
  884. $body_total['price'] = 0;
  885. $body_total['num'] = 0;
  886. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $info['id']));
  887. $goods_status = Dever::db('shop/buy_order_goods')->config['status'];
  888. $refund_body = array();
  889. $refund_body_total = array();
  890. $refund_body_total['price'] = 0;
  891. $refund_body_total['num'] = 0;
  892. foreach ($goods as $k => $v) {
  893. if ($type == 2) {
  894. $v['price'] = $v['p_price'];
  895. }
  896. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  897. $status = $goods_status[$v['status']];
  898. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
  899. if ($tk && $tk['process'] == 1) {
  900. if ($type == 1) {
  901. $status = '申请' . $tk_status[$tk['status']];
  902. } else {
  903. $status = '已上报';
  904. }
  905. if ($type == 1) {
  906. $desc = array();
  907. $desc['退款原因'] = $tk['desc'];
  908. $desc['退款图片'] = '暂无';
  909. if ($tk['pic']) {
  910. $desc['退款图片'] = '';
  911. $pic = explode(',', $tk['pic']);
  912. foreach ($pic as $k1 => $v1) {
  913. $desc['退款图片'] .= '<a href="'.$v1.'" target="_blank"><img src="'.$v1.'" width="100"/></a>';
  914. }
  915. }
  916. $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>';
  917. $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>';
  918. }
  919. } elseif ($type > 1 && (!$tk || ($tk && $tk['process'] == 3))) {
  920. //$tui = Dever::url('lib/buy.refund_apply_info_one_commit?json=1', 'mshop');
  921. if ($type == 2) {
  922. $tui = Dever::url('order_tui?order_goods_id=' . $v['id'] . '&order_id=' . $info['id'] . '&type=13&type_id=' . $info['source_id'], 'factory');
  923. } else {
  924. if ($info['source_type'] == 3) {
  925. $tui = Dever::url('order_tui?order_goods_id=' . $v['id'] . '&order_id=' . $info['id'] . '&type=13&type_id=' . $info['source_id'], 'store');
  926. } else {
  927. $tui = Dever::url('order_tui?order_goods_id=' . $v['id'] . '&order_id=' . $info['id'] . '&type=12&type_id=' . $info['source_id'], 'store');
  928. }
  929. }
  930. if ($info['status'] == 3) {
  931. $status = '<a href="javascript:;" onclick="fastEdit($(this), \''.$tui.'\', \'缺货退款\', \'\', 1)" class="layui-btn">缺货退款</a>';
  932. } elseif ($info['status'] == 4) {
  933. $status = '<a href="javascript:;" onclick="fastEdit($(this), \''.$tui.'\', \'报损退款\', \'\', 1)" class="layui-btn">报损退款</a>';
  934. }
  935. }
  936. if (isset($goods_info['sku'])) {
  937. $sku = '[' . $goods_info['sku']['string'] . ']';
  938. } else {
  939. $sku = '';
  940. }
  941. $d = array
  942. (
  943. 'pic' => $goods_info['cover'],
  944. 'name' => $goods_info['name'],
  945. 'sku' => $sku,
  946. 'price' => $v['price'],
  947. 'num' => $v['num'],
  948. 'status' => $status,
  949. );
  950. if ($tk && $v['status'] == 3 && $tk['status'] != 3) {
  951. $d['status'] = $tk_status[$tk['status']];
  952. if ($noprice == 1) {
  953. unset($d['price']);
  954. }
  955. $refund_body[] = $d;
  956. $price = $v['price']*$v['num'];
  957. $refund_body_total['price'] += $price;
  958. $refund_body_total['num'] += $v['num'];
  959. } else {
  960. if ($noprice == 1) {
  961. unset($d['price']);
  962. }
  963. $body[] = $d;
  964. $price = $v['price']*$v['num'];
  965. $body_total['price'] += $price;
  966. $body_total['num'] += $v['num'];
  967. }
  968. }
  969. if ($body) {
  970. if ($refund_body_total['price']) {
  971. $body_total['set_price'] = $body_total['price'] - $refund_body_total['price'];
  972. }
  973. if ($refund_body_total['num']) {
  974. $body_total['set_num'] = $body_total['num'] - $refund_body_total['num'];
  975. }
  976. if ($noprice == 1) {
  977. unset($body_total['price']);
  978. unset($body_total['set_price']);
  979. }
  980. $result['商品清单'] = array
  981. (
  982. 'type' => 'list',
  983. 'content' => $body,
  984. 'total' => $body_total,
  985. );
  986. }
  987. if ($refund_body) {
  988. if ($noprice == 1) {
  989. unset($refund_body_total['price']);
  990. }
  991. $result['缺货与报损商品'] = array
  992. (
  993. 'type' => 'list',
  994. 'content' => $refund_body,
  995. 'total' => $refund_body_total,
  996. );
  997. }
  998. $search_option_type = Dever::input('search_option_type');
  999. if ($cash_type == 1) {
  1000. if ($search_option_type == 1) {
  1001. # 门店结算单
  1002. $cash_order = Dever::db('cash/order')->select(array('type' => $info['type'], 'type_id' => $info['type_id'], 'source_order_id' => $info['id']));
  1003. $col = 'cash';
  1004. } elseif ($search_option_type == 2) {
  1005. # 工厂结算单
  1006. $cash_order = Dever::db('cash/order')->select(array('source_type' => $info['source_type'], 'source_id' => $info['source_id'], 'source_order_id' => $info['id']));
  1007. $col = 'p_cash';
  1008. } elseif ($search_option_type == 3) {
  1009. # 仓库结算单
  1010. $cash_order = Dever::db('cash/order')->select(array('source_type' => $info['source_type'], 'source_id' => $info['source_id'], 'source_order_id' => $info['id']));
  1011. $col = 'num';
  1012. }
  1013. }
  1014. if (isset($cash_order) && $cash_order) {
  1015. $head = array('序号', '结算单号', '数量', '金额', '结算类型', '入账状态', '审核状态', '结算时间', '审核未通过原因', '管理');
  1016. if ($search_option_type == 3 || $noprice == 1) {
  1017. unset($head[3]);
  1018. }
  1019. $body = array();
  1020. $config = Dever::db('cash/order')->config;
  1021. foreach ($cash_order as $k => $v) {
  1022. $cdate = date('Y-m-d H:i', $v['cdate']);
  1023. $fdate = $v['fdate'] ? date('Y-m-d H:i', $v['fdate']) : '';
  1024. $operdate = $v['operdate'] ? date('Y-m-d H:i', $v['operdate']) : '';
  1025. $cash = $v[$col];
  1026. $url = Dever::url('project/database/list?project=shop&table=buy_order_goods&order_id='.$v['source_order_id'].'&page_type=1', 'manage');
  1027. $v['source_order_num'] = '<a href="'.$url.'" style="color:blue">'.$v['source_order_num'].'</a>';
  1028. $m = '';
  1029. if ($v['audit'] == 1 && $v['audit_type'] == 2) {
  1030. $url = Dever::url('project/database/update?project=cash&table=order&where_id='.$v['id'].'&col=audit,desc', 'manage');
  1031. $m = '<a href="javascript:;" onclick="fastEdit($(this), \''.$url.'\', \'审核\', \'\', 1)" class="layui-btn">审核</a>';
  1032. }
  1033. $d = array
  1034. (
  1035. $k+1,
  1036. $v['order_num'],
  1037. $v['num'],
  1038. $cash,
  1039. $config['config_jstype'][$v['jstype']],
  1040. $config['config_status'][$v['status']],
  1041. $config['config_audit'][$v['audit']],
  1042. $operdate,
  1043. $v['desc'],
  1044. $m,
  1045. );
  1046. if ($search_option_type == 3 || $noprice == 1) {
  1047. unset($d[3]);
  1048. }
  1049. $body[] = $d;
  1050. }
  1051. $result['对账清单'] = array
  1052. (
  1053. 'type' => 'table',
  1054. 'content' => array
  1055. (
  1056. 'head' => $head,
  1057. 'body' => $body,
  1058. )
  1059. );
  1060. }
  1061. $head = array
  1062. (
  1063. 'name' => '基本信息',
  1064. 'btn' => $button,
  1065. );
  1066. $html = Dever::show($head, $result);
  1067. return $html;
  1068. }
  1069. # 发货
  1070. /*
  1071. public function send_commit()
  1072. {
  1073. $order_id = Dever::input('order_id');
  1074. $order = Dever::db('shop/buy_order')->find($order_id);
  1075. if (!$order || ($order && $order['status'] != 3)) {
  1076. Dever::alert('当前订单状态无法发货');
  1077. }
  1078. $state = Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'status' => 4));
  1079. if ($state) {
  1080. $this->updatePs($order, 1);
  1081. }
  1082. return 'reload';
  1083. }*/
  1084. # 采购订单审核 拆单
  1085. /*
  1086. public function audit_commit()
  1087. {
  1088. $order_id = Dever::input('order_id');
  1089. $order = Dever::db('shop/buy_order')->find($order_id);
  1090. if (!$order || ($order && $order['status'] != 2)) {
  1091. Dever::alert('当前订单状态无法拆单');
  1092. }
  1093. if ($order['type'] != 1) {
  1094. Dever::alert('这不是门店的订单');
  1095. }
  1096. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $order_id));
  1097. $shop = Dever::db('shop/info')->find($order['type_id']);
  1098. # 获取门店分配的仓库和工厂
  1099. $shop_store = Dever::db('shop/store')->select(array('shop_id' => $shop['id']));
  1100. $store = array();
  1101. $distance = array();
  1102. # 按照距离排序
  1103. if ($shop_store) {
  1104. foreach ($shop_store as $k => $v) {
  1105. $info = Dever::db('store/info')->find($v['store_id']);
  1106. if ($info && $info['status'] == 1) {
  1107. $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
  1108. $store[] = $info;
  1109. }
  1110. }
  1111. if ($store) {
  1112. array_multisort($distance, SORT_ASC, SORT_NUMERIC, $store);
  1113. }
  1114. }
  1115. $shop_factory = Dever::db('shop/factory')->select(array('shop_id' => $shop['id']));
  1116. $factory = array();
  1117. $distance = array();
  1118. # 按照距离排序
  1119. if ($shop_factory) {
  1120. foreach ($shop_factory as $k => $v) {
  1121. $info = Dever::db('factory/info')->find($v['factory_id']);
  1122. if ($info && $info['status'] == 1) {
  1123. $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
  1124. $factory[] = $info;
  1125. }
  1126. }
  1127. if ($factory) {
  1128. array_multisort($distance, SORT_ASC, SORT_NUMERIC, $factory);
  1129. }
  1130. }
  1131. if (!$factory && !$store) {
  1132. Dever::alert('未分配仓库或者未分配工厂');
  1133. }
  1134. $this->order_num = 0;
  1135. $this->order_id = array();
  1136. $delete = array();
  1137. $yes = array();
  1138. foreach ($goods as $k => $v) {
  1139. # 开始找仓库
  1140. $find = $this->store($order['id'], $order['order_num'] . '_' . $this->order_num, $shop, $store, $v);
  1141. if (!$find[1]) {
  1142. # 仓库没有,找厂家
  1143. if ($factory) {
  1144. $find = $this->factory($order['id'], $order['order_num'] . '_' . $this->order_num, $shop, $factory, $v);
  1145. }
  1146. }
  1147. if ($find[1]) {
  1148. $delete[$v['id']] = $v;
  1149. } else {
  1150. $yes[$v['id']] = $v;
  1151. }
  1152. }
  1153. $num = count($this->order_id);
  1154. if ($num > 0) {
  1155. if ($num == 1) {
  1156. $key = array_keys($this->order_id);
  1157. $key = $key[0];
  1158. Dever::db('shop/buy_order')->delete($key);
  1159. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $key));
  1160. $p_price = 0;
  1161. foreach ($goods as $k => $v) {
  1162. $goods_info = Dever::db('shop/buy_order_goods')->find(array('order_id' => $order['id'], 'goods_id' => $v['goods_id']));
  1163. if ($goods_info) {
  1164. Dever::db('shop/buy_order_goods')->update(array('where_id' => $goods_info['id'], 'set_p_price' => $v['p_price']));
  1165. $p_price += $v['p_price'];
  1166. }
  1167. }
  1168. 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()));
  1169. Dever::db('shop/buy_order_goods')->delete(array('order_id' => $key));
  1170. $this->order_id[$order['id']] = array($this->order_id[$key][0], $this->order_id[$key][1]);
  1171. unset($this->order_id[$key]);
  1172. } else {
  1173. if (!$yes) {
  1174. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'state' => 2));
  1175. Dever::load('cash/lib/order')->up($order, 1, 3);
  1176. } else {
  1177. $price = 0;
  1178. $num = 0;
  1179. foreach ($delete as $k => $v) {
  1180. Dever::db('shop/buy_order_goods')->delete(array('id' => $v['id']));
  1181. $price += $v['price'];
  1182. $num += $v['num'];
  1183. }
  1184. $order['price'] = $order['price'] - $price;
  1185. $order['num'] = $order['num'] - $num;
  1186. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'price' => $order['price'], 'num' => $order['num']));
  1187. Dever::load('shop/lib/refund')->set('buy')->apply(1, $shop['id'], $order_id, false, 3, 0, '供货商库存不足');
  1188. Dever::load('cash/lib/order')->up($order, 1, 3);
  1189. }
  1190. }
  1191. } else {
  1192. # 没有找到,退款
  1193. Dever::alert('供货商库存不足,请联系供货商备货');
  1194. //Dever::load('shop/lib/refund')->set('buy')->apply(1, $shop['id'], $order_id, false, 3, 0, '供货商库存不足');
  1195. }
  1196. if ($this->order_id) {
  1197. foreach ($this->order_id as $k => $v) {
  1198. $this->sendMsg($v[0], $v[1], $k);
  1199. if ($k != $order['id']) {
  1200. $order_info = Dever::db('shop/buy_order')->find($k);
  1201. Dever::load('cash/lib/order')->up($order_info, 1, 1);
  1202. }
  1203. }
  1204. }
  1205. return 'ok';
  1206. }
  1207. */
  1208. # 采购订单审核 拆单
  1209. public function audit_commit()
  1210. {
  1211. $order_id = Dever::input('order_id');
  1212. $order = Dever::db('shop/buy_order')->find($order_id);
  1213. if (!$order || ($order && $order['status'] != 2)) {
  1214. Dever::alert('当前订单状态无法拆单');
  1215. }
  1216. if ($order['type'] != 1) {
  1217. Dever::alert('这不是门店的订单');
  1218. }
  1219. $order['type'] = 1;
  1220. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $order['id']));
  1221. $shop = Dever::db('shop/info')->find($order['type_id']);
  1222. $data = $this->assign($goods, $order, $shop);
  1223. $send = array();
  1224. if ($data) {
  1225. # 开始生成订单
  1226. $num = count($data);
  1227. if (!$goods && $num == 1) {
  1228. foreach ($data as $k => $v) {
  1229. $k = explode('_', $k);
  1230. $p_price = 0;
  1231. foreach ($v as $k1 => $v1) {
  1232. Dever::db('shop/buy_order_goods')->update(array('where_id' => $v1['order_goods_id'], 'set_p_price' => $v1['p_price']));
  1233. $p_price += $v1['p_price'];
  1234. $send[$order['id']] = $v1;
  1235. }
  1236. 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()));
  1237. }
  1238. } elseif ($num >= 1) {
  1239. $i = $order['order_index'];
  1240. $price = 0;
  1241. $num = 0;
  1242. foreach ($data as $k => $v) {
  1243. foreach ($v as $k1 => $v1) {
  1244. $v1['order_num'] .= '_' . $i;
  1245. $state = $this->createOrder($v1);
  1246. $send[$state] = $v1;
  1247. Dever::db('shop/buy_order_goods')->delete(array('id' => $v1['order_goods_id'], 'clear' => true));
  1248. $price += $v1['price'];
  1249. $num += $v1['num'];
  1250. }
  1251. $i++;
  1252. }
  1253. $order['price'] = $order['price'] - $price;
  1254. $order['num'] = $order['num'] - $num;
  1255. 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));
  1256. }
  1257. } else {
  1258. Dever::db('shop/buy_order')->update(array('where_id' => $order['id'], 'audit' => 1));
  1259. Dever::alert('供货商库存不足,请联系供货商备货,该订单进入重新审核状态');
  1260. }
  1261. if ($send) {
  1262. foreach ($send as $k => $v) {
  1263. $this->sendMsg($v['source_type'], $v['source_id'], $k);
  1264. if ($k != $order['id']) {
  1265. $order_info = Dever::db('shop/buy_order')->find(array('id' => $k, 'clear' => true));
  1266. Dever::load('cash/lib/order')->up($order_info, 1, 1);
  1267. }
  1268. }
  1269. }
  1270. if ($goods) {
  1271. $send = array();
  1272. foreach ($goods as $k => $v) {
  1273. $send[$v['order_id']] = $v;
  1274. }
  1275. if ($send) {
  1276. foreach ($send as $k => $v) {
  1277. $order_info = Dever::db('shop/buy_order')->find(array('id' => $k, 'clear' => true));
  1278. Dever::load('cash/lib/order')->up($order_info, 1, 1);
  1279. }
  1280. }
  1281. }
  1282. return 'ok';
  1283. }
  1284. public function assign(&$goods, &$order, $shop)
  1285. {
  1286. if (strstr($order['order_num'], '_')) {
  1287. $temp = explode('_', $order['order_num']);
  1288. $order['order_num'] = $temp[0];
  1289. $order['order_index'] = $temp[1];
  1290. } else {
  1291. $order['order_index'] = 0;
  1292. }
  1293. # 获取门店分配的仓库和工厂
  1294. $data = array();
  1295. $this->assign_store($data, $goods, $order, $shop);
  1296. $this->assign_factory($data, $goods, $order, $shop);
  1297. return $data;
  1298. }
  1299. private function assign_store(&$data, &$goods, $order, $shop)
  1300. {
  1301. $shop_store = Dever::db('shop/store')->select(array('shop_id' => $shop['id']));
  1302. $store = array();
  1303. $distance = array();
  1304. # 按照距离排序
  1305. if ($shop_store) {
  1306. foreach ($shop_store as $k => $v) {
  1307. $info = Dever::db('store/info')->find($v['store_id']);
  1308. if ($info && $info['status'] == 1) {
  1309. $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
  1310. $store[] = $info;
  1311. }
  1312. }
  1313. if ($store) {
  1314. array_multisort($distance, SORT_ASC, SORT_NUMERIC, $store);
  1315. foreach ($store as $k => $v) {
  1316. if ($goods) {
  1317. foreach ($goods as $gk => $gv) {
  1318. $where = array();
  1319. $where['store_id'] = $v['id'];
  1320. $where['goods_id'] = $gv['goods_id'];
  1321. $where['sku_id'] = ($gv['sku_id'] && $gv['sku_id'] > 0) ? $gv['sku_id'] : -1;
  1322. $sku = Dever::db('store/goods_sku')->getOne($where);
  1323. if ($sku) {
  1324. if ($gv['num'] <= $sku['total']) {
  1325. # 库存符合
  1326. $data['2_' . $v['id']][] = array
  1327. (
  1328. 'area' => $shop['area'],
  1329. 'order_id' => $order['id'],
  1330. 'order_type' => $order['type'],
  1331. 'order_num' => $order['order_num'],
  1332. 'order_index' => $order['order_index'],
  1333. 'type' => 1,
  1334. 'type_id' => $shop['id'],
  1335. 'source_type' => 2,
  1336. 'source_id' => $v['id'],
  1337. 'source_name' => $v['name'],
  1338. 'order_goods_id' => $gv['id'],
  1339. 'goods_id' => $where['goods_id'],
  1340. 'sku_id' => $where['sku_id'],
  1341. 'price' => $gv['price'],
  1342. 'p_price' => $gv['price'],
  1343. 'num' => $gv['num'],
  1344. );
  1345. unset($goods[$gk]);
  1346. }
  1347. }
  1348. }
  1349. }
  1350. }
  1351. }
  1352. }
  1353. }
  1354. private function assign_factory(&$data, &$goods, $order, $shop)
  1355. {
  1356. $shop_factory = Dever::db('shop/factory')->select(array('shop_id' => $shop['id']));
  1357. $factory = array();
  1358. $distance = array();
  1359. # 按照距离排序
  1360. if ($shop_factory && $goods) {
  1361. foreach ($shop_factory as $k => $v) {
  1362. $info = Dever::db('factory/info')->find($v['factory_id']);
  1363. if ($info && $info['status'] == 1) {
  1364. $distance[] = $info['distance'] = Dever::distance($shop['lng'], $shop['lat'], $info['lng'], $info['lat']);
  1365. $factory[] = $info;
  1366. }
  1367. }
  1368. if ($factory) {
  1369. array_multisort($distance, SORT_ASC, SORT_NUMERIC, $factory);
  1370. foreach ($factory as $k => $v) {
  1371. if ($goods) {
  1372. foreach ($goods as $gk => $gv) {
  1373. $where = array();
  1374. $where['factory_id'] = $v['id'];
  1375. $where['goods_id'] = $gv['goods_id'];
  1376. $where['sku_id'] = ($gv['sku_id'] && $gv['sku_id'] > 0) ? $gv['sku_id'] : -1;
  1377. $sku = Dever::db('factory/goods_sku')->getOne($where);
  1378. if ($sku) {
  1379. $data['3_' . $v['id']][] = array
  1380. (
  1381. 'area' => $shop['area'],
  1382. 'order_id' => $order['id'],
  1383. 'order_type' => $order['type'],
  1384. 'order_num' => $order['order_num'],
  1385. 'order_index' => $order['order_index'],
  1386. 'type' => 1,
  1387. 'type_id' => $shop['id'],
  1388. 'source_type' => 3,
  1389. 'source_id' => $v['id'],
  1390. 'source_name' => $v['name'],
  1391. 'order_goods_id' => $gv['id'],
  1392. 'goods_id' => $where['goods_id'],
  1393. 'sku_id' => $where['sku_id'],
  1394. 'price' => $gv['price'],
  1395. 'p_price' => $sku['p_price'],
  1396. 'num' => $gv['num'],
  1397. );
  1398. unset($goods[$gk]);
  1399. }
  1400. }
  1401. }
  1402. }
  1403. }
  1404. }
  1405. }
  1406. /*
  1407. private function store($order_id, $order_num, $shop, $store, $goods, $state = 2)
  1408. {
  1409. $find = array();
  1410. $type_id = array();
  1411. $id = array();
  1412. if (!$store) {
  1413. return array(2, $type_id, $id);
  1414. }
  1415. foreach ($store as $k => $v) {
  1416. $where['store_id'] = $v['id'];
  1417. $where['goods_id'] = $goods['goods_id'];
  1418. $where['sku_id'] = ($goods['sku_id'] && $goods['sku_id'] > 0) ? $goods['sku_id'] : -1;
  1419. $sku = Dever::db('store/goods_sku')->getOne($where);
  1420. if ($sku) {
  1421. if ($goods['num'] <= $sku['total']) {
  1422. # 如果库存符合,直接下单即可,生成仓库订单
  1423. $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']);
  1424. if ($state) {
  1425. $type_id[] = $where['store_id'];
  1426. $id[] = $state;
  1427. $this->order_id[$state] = array(2, $where['store_id']);
  1428. break;
  1429. }
  1430. } elseif ($state == 1 && $sku['total'] > 0) {
  1431. # 如果库存不够,从这个仓库扣下一部分,剩余的从另外仓库处理
  1432. $goods['num'] = $goods['num'] - $sku['total'];
  1433. # 生成仓库订单
  1434. $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']);
  1435. if (!$state) {
  1436. $goods['num'] = $goods['num'] + $sku['total'];
  1437. } else {
  1438. $type_id[] = $where['store_id'];
  1439. $id[] = $state;
  1440. $this->order_id[$state] = array(2, $where['store_id']);
  1441. }
  1442. continue;
  1443. } else {
  1444. # 直接切换到下一个仓库处理
  1445. continue;
  1446. }
  1447. }
  1448. }
  1449. return array(2, $type_id, $id);
  1450. }
  1451. private function factory($order_id, $order_num, $shop, $factory, $goods)
  1452. {
  1453. $find = array();
  1454. $type_id = array();
  1455. $id = array();
  1456. if (!$factory) {
  1457. return array(3, $type_id, $id);
  1458. }
  1459. foreach ($factory as $k => $v) {
  1460. $where['factory_id'] = $v['id'];
  1461. $where['goods_id'] = $goods['goods_id'];
  1462. $where['sku_id'] = ($goods['sku_id'] && $goods['sku_id'] > 0) ? $goods['sku_id'] : -1;
  1463. $sku = Dever::db('factory/goods_sku')->getOne($where);
  1464. if ($sku && $sku['p_price'] > 0) {
  1465. $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']);
  1466. if ($state) {
  1467. $type_id[] = $where['factory_id'];
  1468. $id[] = $state;
  1469. $this->order_id[$state] = array(3, $where['factory_id']);
  1470. break;
  1471. }
  1472. } else {
  1473. continue;
  1474. }
  1475. }
  1476. return array(3, $type_id, $id);
  1477. }
  1478. */
  1479. # 发送消息
  1480. public function sendMsg($source_type, $source_id, $id)
  1481. {
  1482. if (Dever::project('message')) {
  1483. $msg_param['type'] = 1;//消息类型1是订单消息
  1484. $msg_param['id'] = $id;
  1485. $msg_param['source_id'] = $source_id;
  1486. $msg_param['source_type'] = $source_type;
  1487. $msg_param = Dever::json_encode($msg_param);
  1488. $project = $source_type + 1;
  1489. $msg = '您有一个新的订货单,请及时处理。';
  1490. Dever::load('message/lib/data')->push(-1, $source_id, '订货通知', $msg, 2, $project, false, $msg_param);
  1491. }
  1492. }
  1493. # 生成拆单的订单
  1494. /*
  1495. private function createOrder($order_id, $order_num, $type, $type_id, $source_type, $source_id, $price, $p_price, $num, $goods_id, $sku_id)
  1496. {
  1497. $where['parent_order_id'] = $order_id;
  1498. $where['type'] = $type;
  1499. $where['type_id'] = $type_id;
  1500. $where['source_type'] = $source_type;
  1501. $where['source_id'] = $source_id;
  1502. $where['r'] = $goods_id . '_' . microtime() . '_' . mt_rand(1, 10000);
  1503. $info = Dever::db('shop/buy_order')->find($where);
  1504. if (!$info) {
  1505. $this->order_num++;
  1506. $data = $where;
  1507. $data['name'] = $order_id;
  1508. $data['order_num'] = $order_num;
  1509. $data['operdate'] = time();
  1510. $data['status'] = 3;
  1511. $data['price'] = $price * $num;
  1512. $data['p_price'] = $p_price * $num;
  1513. $data['num'] = $num;
  1514. $data['refund_cash'] = 0;
  1515. $data['refund_p_cash'] = 0;
  1516. $id = $state = Dever::db('shop/buy_order')->insert($data);
  1517. } else {
  1518. $id = $info['id'];
  1519. $data['where_id'] = $id;
  1520. $data['price'] = $info['price'] + ($price * $num);
  1521. $data['p_price'] = $info['p_price'] + ($p_price * $num);
  1522. $data['num'] = $info['num'] + $num;
  1523. $state = Dever::db('shop/buy_order')->update($data);
  1524. }
  1525. if ($state && $id > 0) {
  1526. $goods_info = Dever::db('shop/buy_order_goods')->find(array('order_id' => $id));
  1527. if (!$goods_info) {
  1528. $goods['order_id'] = $id;
  1529. $goods['goods_id'] = $goods_id;
  1530. $goods['sku_id'] = $sku_id;
  1531. $goods['price'] = $price;
  1532. $goods['p_price'] = $p_price;
  1533. $goods['num'] = $num;
  1534. Dever::db('shop/buy_order_goods')->insert($goods);
  1535. }
  1536. $state = $this->updateSell($source_type, $source_id, $goods_id, $sku_id, $num);
  1537. return $id;
  1538. }
  1539. return false;
  1540. }
  1541. */
  1542. public function createOrder($data)
  1543. {
  1544. $where['parent_type'] = $data['order_type'];
  1545. $where['parent_order_id'] = $data['order_id'];
  1546. $where['type'] = $data['type'];
  1547. $where['type_id'] = $data['type_id'];
  1548. $where['source_type'] = $data['source_type'];
  1549. $where['source_id'] = $data['source_id'];
  1550. $where['clear'] = true;
  1551. $info = Dever::db('shop/buy_order')->find($where);
  1552. $goods_info = array();
  1553. $goods['goods_id'] = $data['goods_id'];
  1554. $goods['sku_id'] = $data['sku_id'];
  1555. $goods['clear'] = true;
  1556. if (!$info) {
  1557. $insert = $where;
  1558. if (isset($data['area'])) {
  1559. $insert['area'] = $data['area'] . ',' . $where['type_id'];
  1560. }
  1561. $insert['name'] = $data['order_id'];
  1562. $insert['order_num'] = $data['order_num'];
  1563. $insert['operdate'] = time();
  1564. $insert['status'] = 3;
  1565. $insert['price'] = $data['price'] * $data['num'];
  1566. $insert['p_price'] = $data['p_price'] * $data['num'];
  1567. $insert['num'] = $data['num'];
  1568. $insert['refund_cash'] = 0;
  1569. $insert['refund_p_cash'] = 0;
  1570. $id = $state = Dever::db('shop/buy_order')->insert($insert);
  1571. if ($id) {
  1572. $goods['order_id'] = $id;
  1573. $goods_info = Dever::db('shop/buy_order_goods')->find($goods);
  1574. }
  1575. } else {
  1576. $id = $info['id'];
  1577. $goods['order_id'] = $id;
  1578. $goods_info = Dever::db('shop/buy_order_goods')->find($goods);
  1579. $state = false;
  1580. if (!$goods_info) {
  1581. $update['where_id'] = $id;
  1582. $update['price'] = $info['price'] + ($data['price'] * $data['num']);
  1583. $update['p_price'] = $info['p_price'] + ($data['p_price'] * $data['num']);
  1584. $update['num'] = $info['num'] + $data['num'];
  1585. $state = Dever::db('shop/buy_order')->update($update);
  1586. }
  1587. }
  1588. if ($state && $id > 0 && !$goods_info) {
  1589. $goods['price'] = $data['price'];
  1590. $goods['p_price'] = $data['p_price'];
  1591. $goods['num'] = $data['num'];
  1592. Dever::db('shop/buy_order_goods')->insert($goods);
  1593. $state = $this->updateSell($data['source_type'], $data['source_id'], $data['goods_id'], $data['sku_id'], $data['num']);
  1594. return $id;
  1595. }
  1596. return false;
  1597. }
  1598. # 减少库存,增加销量
  1599. public function updateSell($source_type, $source_id, $goods_id, $sku_id, $num)
  1600. {
  1601. if ($source_type == 2) {
  1602. $data['store_id'] = $source_id;
  1603. $data['goods_id'] = $goods_id;
  1604. $data['sku_id'] = $sku_id;
  1605. $data['num'] = $num;
  1606. return Dever::load('store/lib/goods')->oper($source_id, 1, 2, array($data));
  1607. } else {
  1608. $up = array();
  1609. $up['where_factory_id'] = $source_id;
  1610. $up['where_goods_id'] = $goods_id;
  1611. $up['sell_num'] = $num;
  1612. $state = Dever::db('factory/goods')->incSell($up);
  1613. if ($state) {
  1614. if ($sku_id > 0) {
  1615. } else {
  1616. $sku_id = -1;
  1617. }
  1618. $upSku = $up;
  1619. $upSku['where_sku_id'] = $sku_id;
  1620. $state = Dever::db('factory/goods_sku')->incSell($upSku);
  1621. }
  1622. return $state;
  1623. }
  1624. }
  1625. # 打印订单单
  1626. public function printer($user)
  1627. {
  1628. $id = Dever::input('id');
  1629. $type = Dever::input('type', 1);
  1630. $factory_config = Dever::db('main/factory_config')->find();
  1631. $main_config = Dever::db('main/config')->find();
  1632. $config = Dever::db('shop/buy_order')->config;
  1633. $info = Dever::db('shop/buy_order')->one($id);
  1634. //$source_info = Dever::db('factory/info')->find($info['source_id']);
  1635. if ($type == 2) {
  1636. # 仓库
  1637. $member = Dever::db('store/member')->find($user['id']);
  1638. $name = '仓库名称';
  1639. } else {
  1640. # 工厂
  1641. $member = Dever::db('factory/member')->find($user['id']);
  1642. $name = '工厂名称';
  1643. }
  1644. if ($info['type'] == 1) {
  1645. $type_info = Dever::db('shop/info')->find($info['type_id']);
  1646. } elseif ($info['type'] == 2) {
  1647. $type_info = Dever::db('store/info')->find($info['type_id']);
  1648. } elseif ($info['type'] == 3) {
  1649. $type_info = Dever::db('factory/info')->find($info['type_id']);
  1650. }
  1651. if ($info['source_type'] == 1) {
  1652. $source_info = Dever::db('shop/info')->find($info['source_id']);
  1653. } elseif ($info['source_type'] == 2) {
  1654. $source_info = Dever::db('store/info')->find($info['source_id']);
  1655. } elseif ($info['source_type'] == 3) {
  1656. $source_info = Dever::db('factory/info')->find($info['source_id']);
  1657. }
  1658. $cdate = date('Y-m-d H:i', $info['cdate']);
  1659. if ($info['operdate']) {
  1660. $operdate = date('Y-m-d H:i', $info['operdate']);
  1661. } else {
  1662. $operdate = '';
  1663. }
  1664. if ($info['fdate']) {
  1665. $fdate = date('Y-m-d H:i', $info['fdate']);
  1666. } else {
  1667. $fdate = '';
  1668. }
  1669. $status = $config['status'][$info['status']];
  1670. $info['ps_info'] = Dever::db('shop/buy_order_ps')->find(array('order_id' => $info['id']));
  1671. if ($info['ps_info']) {
  1672. $info['ps_info']['service'] = Dever::array_decode($info['ps_info']['service']);
  1673. $info['ps_info']['cdate'] = date('Y-m-d H:i', $info['ps_info']['cdate']);
  1674. if ($info['ps_info']['ydate']) {
  1675. $info['ps_info']['ydate'] = date('Y-m-d H:i', $info['ps_info']['ydate']);
  1676. } else {
  1677. $info['ps_info']['ydate'] = '无';
  1678. }
  1679. $ps_status = Dever::db('shop/buy_order_ps')->config['status'];
  1680. $info['ps_info']['status_name'] = $status[$info['ps_info']['status']];
  1681. }
  1682. $pdf = Dever::load('pdf/lib/base')->init();
  1683. $pdf->hr('-', $main_config['name']);
  1684. $pdf->br()->font(20)->center('订货单号:' . $info['order_num']);
  1685. $pdf->font(10);
  1686. $pdf->br();
  1687. if ($type == 2 && $info['type'] == 2 && $info['source_type'] != 2) {
  1688. $pdf->br()->left($name . ':' . $type_info['name'], 80)->left('制单人:' . $member['name'], 60)->left('制单时间:' . date('Y-m-d H:i'), 40);
  1689. $pdf->hr();
  1690. $pdf->br()->left('订单状态:' . $status, 140)->left('订货时间:' . $operdate, 40);
  1691. $pdf->hr();
  1692. if (!$info['ps_info']) {
  1693. $pdf->left('发货供应商:' . $source_info['name']);
  1694. } else {
  1695. //$pdf->br()->left('发货供应商:' . $source_info['name'], 140)->left('发货时间:' . $info['ps_info']['cdate'], 40);
  1696. $pdf->br()->left('发货供应商:' . $source_info['name'], 80)->left('发货时间:' . $info['ps_info']['cdate'], 60)->left('收货时间:' . $info['ps_info']['ydate'], 40);
  1697. }
  1698. if ($info['ps_info'] && $info['ps_info']['service']) {
  1699. foreach ($info['ps_info']['service'] as $k => $v) {
  1700. $info['ps_info']['service'][$k]['service_name'] = '供应商自送';
  1701. if ($v['service_id'] > 0) {
  1702. $service = Dever::db('shop/service')->find($v['service_id']);
  1703. $info['ps_info']['service'][$k]['service_name'] = $service['name'];
  1704. }
  1705. $pdf->br()->left('物流信息:' . $info['ps_info']['service'][$k]['service_name'], 80)->left('物流单号:' . $info['ps_info']['service'][$k]['order_num'], 40);
  1706. }
  1707. }
  1708. $pdf->hr();
  1709. } else {
  1710. $pdf->br()->left($name . ':' . $source_info['name'], 80)->left('制单人:' . $member['name'], 60)->left('制单时间:' . date('Y-m-d H:i'), 40);
  1711. $pdf->hr();
  1712. $pdf->br()->left('订单状态:' . $status, 80)->left('订货时间:' . $operdate, 80);
  1713. $pdf->hr();
  1714. $parent_order = array();
  1715. if ($info['parent_type'] == 2) {
  1716. $parent_order = Dever::db('shop/sell_order')->find($info['parent_order_id']);
  1717. }
  1718. if ($parent_order && $parent_order['uid']) {
  1719. if ($parent_order['address_id']) {
  1720. $address = Dever::load('passport/address')->getOne($parent_order['uid'], $parent_order['address_id']);
  1721. $pdf->br()->left('收件人:' . $address['contact'], 80)->left('收件人手机:' . $address['mobile'], 80);
  1722. $pdf->left('收件地址:' . $address['address_info'] . ',' . $address['house_number']);
  1723. } else {
  1724. $user = Dever::db('passport/user')->find($parent_order['uid']);
  1725. $pdf->br()->left('收件人:' . $user['username'], 80)->left('收件人手机:' . $user['mobile'], 80);
  1726. }
  1727. } else {
  1728. $pdf->br()->left('配送至:' . $type_info['name'], 80)->left('联系方式:' . $type_info['truename'] . ',' . $type_info['mobile'], 80);
  1729. $pdf->left('配送地址:' . $type_info['address']);
  1730. }
  1731. $pdf->hr();
  1732. if ($info['ps_info'] && $info['ps_info']['service']) {
  1733. $pdf->br()->left('发货时间:' . $info['ps_info']['cdate'], 80)->left('收货时间:' . $info['ps_info']['ydate'], 80);
  1734. foreach ($info['ps_info']['service'] as $k => $v) {
  1735. $info['ps_info']['service'][$k]['service_name'] = '供应商自送';
  1736. if ($v['service_id'] > 0) {
  1737. $service = Dever::db('shop/service')->find($v['service_id']);
  1738. $info['ps_info']['service'][$k]['service_name'] = $service['name'];
  1739. }
  1740. $pdf->br()->left('物流信息:' . $info['ps_info']['service'][$k]['service_name'], 80)->left('物流单号:' . $info['ps_info']['service'][$k]['order_num'], 80);
  1741. }
  1742. /*
  1743. $pdf->br()->left('发货时间:' . $info['ps_info']['cdate'], 80)->left('配货员:' . $info['ps_info']['phy'], 70)->left('打包员:' . $info['ps_info']['dby'], 40);
  1744. if ($info['ps_info']['zl'] && $info['ps_info']['tj']) {
  1745. $pdf->br()->left('包裹重量:' . $info['ps_info']['zl'] . '克', 80)->left('包裹体积:' . $info['ps_info']['tj'] . '立方厘米', 70)->left('包裹数量:' . $info['ps_info']['num'], 40);
  1746. } elseif (!$info['ps_info']['zl'] && $info['ps_info']['tj']) {
  1747. $pdf->br()->left('包裹体积:' . $info['ps_info']['tj'] . '立方厘米', 150)->left('包裹数量:' . $info['ps_info']['num'], 40);
  1748. } elseif ($info['ps_info']['zl'] && !$info['ps_info']['tj']) {
  1749. $pdf->br()->left('包裹重量:' . $info['ps_info']['zl'] . '克', 150)->left('包裹数量:' . $info['ps_info']['num'], 40);
  1750. } else {
  1751. $pdf->left('包裹数量:' . $info['ps_info']['num']);
  1752. }
  1753. $pdf->br()->left('物流信息:' . $info['ps_info']['service_name'], 40);
  1754. if ($info['ps_info']['order_num']) {
  1755. $pdf->left('物流单号:' . $info['ps_info']['order_num'], 70);
  1756. }
  1757. if ($info['ps_info']['ydate']) {
  1758. $pdf->left('到货确认时间:' . $info['ps_info']['ydate']);
  1759. }
  1760. */
  1761. $pdf->hr();
  1762. }
  1763. }
  1764. $body = array();
  1765. $body_total = array();
  1766. $body_total['price'] = 0;
  1767. $body_total['num'] = 0;
  1768. $goods = Dever::db('shop/buy_order_goods')->select(array('order_id' => $info['id']));
  1769. $goods_status = Dever::db('shop/buy_order_goods')->config['status'];
  1770. $refund_body = array();
  1771. $refund_body_total = array();
  1772. $refund_body_total['price'] = 0;
  1773. $refund_body_total['num'] = 0;
  1774. foreach ($goods as $k => $v) {
  1775. if ($type == 1) {
  1776. $v['price'] = $v['p_price'];
  1777. }
  1778. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v['goods_id'], $v['sku_id']);
  1779. $status = $goods_status[$v['status']];
  1780. $tk = Dever::db('shop/buy_order_refund')->find(array('order_id' => $info['id'], 'order_goods_id' => $v['id'], 'type' => 2));
  1781. if (isset($goods_info['sku'])) {
  1782. $sku = $goods_info['sku']['string'];
  1783. } else {
  1784. $sku = '无';
  1785. }
  1786. $d = array
  1787. (
  1788. $goods_info['id'],
  1789. $goods_info['name'],
  1790. $sku,
  1791. $v['price'],
  1792. 'x ' . $v['num'],
  1793. );
  1794. if ($type == 2) {
  1795. unset($d[3]);
  1796. }
  1797. if ($tk && $v['status'] == 3 && $tk['status'] != 3) {
  1798. $refund_body[] = $d;
  1799. $price = $v['price']*$v['num'];
  1800. $refund_body_total['price'] += $price;
  1801. $refund_body_total['num'] += $v['num'];
  1802. } else {
  1803. $body[] = $d;
  1804. $price = $v['price']*$v['num'];
  1805. $body_total['price'] += $price;
  1806. $body_total['num'] += $v['num'];
  1807. }
  1808. }
  1809. $head = array(array('商品编号', 40), array('商品名称', 60), array('商品属性', 40), array('单价', 40), array('数量', 20));
  1810. if ($type == 2) {
  1811. unset($head[3]);
  1812. $head[1][1] = 70;
  1813. $head[2][1] = 60;
  1814. }
  1815. if ($body) {
  1816. $pdf->center('订单商品');
  1817. $pdf->br();
  1818. foreach ($head as $k => $v) {
  1819. $pdf->left($v[0], $v[1]);
  1820. }
  1821. foreach ($body as $k => $v) {
  1822. $pdf->br();
  1823. foreach ($head as $k1 => $v1) {
  1824. $pdf->left($v[$k1], $v1[1]);
  1825. }
  1826. }
  1827. $pdf->br();
  1828. if ($type == 2) {
  1829. $pdf->right('共'.$body_total['num'].'件商品');
  1830. } else {
  1831. $pdf->right('共'.$body_total['num'].'件商品,合计金额¥' . $body_total['price'] . '元');
  1832. }
  1833. $pdf->hr();
  1834. }
  1835. if ($refund_body) {
  1836. $pdf->center('缺货与报损商品');
  1837. $pdf->br();
  1838. foreach ($head as $k => $v) {
  1839. $pdf->left($v[0], $v[1]);
  1840. }
  1841. foreach ($refund_body as $k => $v) {
  1842. $pdf->br();
  1843. foreach ($head as $k1 => $v1) {
  1844. $pdf->left($v[$k1], $v1[1]);
  1845. }
  1846. }
  1847. $pdf->br();
  1848. if ($type == 2) {
  1849. $pdf->right('共'.$refund_body_total['num'].'件商品');
  1850. } else {
  1851. $pdf->right('共'.$refund_body_total['num'].'件商品,合计金额¥' . $refund_body_total['price'] . '元');
  1852. }
  1853. $pdf->hr();
  1854. }
  1855. $pdf->br(1);
  1856. $pdf->right('如遇任何问题请致电客服');
  1857. $pdf->br();
  1858. $pdf->font(20);
  1859. $pdf->left($main_config['name'], 160);
  1860. $pdf->font(10);
  1861. $pdf->right('电话:' . $factory_config['phone'], 30);
  1862. $pdf->br();
  1863. $pdf->left($main_config['site'], 160);
  1864. $pdf->font(10);
  1865. $pdf->right($main_config['worktime'], 30);
  1866. $pdf->out('订货单');
  1867. }
  1868. # 导出单个订单
  1869. public function excel_one_api()
  1870. {
  1871. $this->type = Dever::input('type');
  1872. $id = Dever::input('id');
  1873. $type_id = Dever::input('type_id');
  1874. $data = $this->getView($type_id, $id);
  1875. $this->excel(array($data));
  1876. }
  1877. # 导出订单
  1878. public function excel($data)
  1879. {
  1880. if (!$data) {
  1881. Dever::alert('无导出数据');
  1882. }
  1883. $file = '入库订单';
  1884. $type = Dever::input('search_option_type', $this->type);
  1885. $header = array('所属仓库', '订单号', '商品来源', '发货日期', '商品名称', '商品规格属性', '商品数量', '报损退款状态', '订单状态');
  1886. $table = 'store/info';
  1887. if ($type == 1) {
  1888. $header[0] = '所属门店';
  1889. $table = 'shop/info';
  1890. }
  1891. $body = array();
  1892. foreach ($data as $k => $v) {
  1893. if (!is_array($v)) {
  1894. continue;
  1895. }
  1896. foreach ($v['goods'] as $k1 => $v1) {
  1897. $goods_info = Dever::load('goods/lib/info')->getInfoBySku($v1['goods_id'], $v1['sku_id']);
  1898. if (isset($goods_info['sku'])) {
  1899. $sku = $goods_info['sku']['string'];
  1900. } else {
  1901. $sku = '';
  1902. }
  1903. $d = array
  1904. (
  1905. $v['type_info']['name'],
  1906. $v['order_num'],
  1907. $v['source_info']['name'],
  1908. $v['cdate'],
  1909. $goods_info['name'],
  1910. $sku,
  1911. $v1['num'],
  1912. $v1['status_name'],
  1913. $v['status_name'],
  1914. );
  1915. $body[] = $d;
  1916. }
  1917. }
  1918. Dever::excelExport($body, $header, $file);
  1919. }
  1920. }