Buy.php 85 KB

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