PageRenderTime 28ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/b2b/core/api/taobao/3.1/api_b2b_3_1_fenxiao.php

http://phpfor.googlecode.com/
PHP | 825 lines | 646 code | 60 blank | 119 comment | 157 complexity | fc35ed9118428c574be8b5484a904e68 MD5 | raw file
  1. <?php
  2. include_once(CORE_DIR.'/api/shop_api_object.php');
  3. class api_b2b_3_1_fenxiao extends shop_api_object{
  4. var $app_error=array(
  5. 'error_param'=>array('no'=>'b_fenxiao_001','debug'=>'','level'=>'error','desc'=>'????','info'=>''),
  6. 'error_DB'=>array('no'=>'b_fenxiao_002','debug'=>'','level'=>'error','desc'=>'?????','info'=>'')
  7. );
  8. var $ome_node_id='';
  9. var $matrix='';
  10. /**
  11. * ???????????????????????????????????
  12. */
  13. function taobao_order_to_local($data){
  14. //?ome ??ome???
  15. if( !$this->ome_node_id ){
  16. $certificate = $this->system->loadModel('service/certificate');
  17. $this->ome_node_id = $certificate->getOmeNoteId();
  18. $this->ome_node_id and $this->matrix = $this->system->loadModel('system/matrix');
  19. }
  20. //??????
  21. $data['items'] = json_decode($data['items'],true);
  22. //?????????????????? tc_order_id, id, fenxiao_id?????????
  23. $order_id_range = array();
  24. if(!empty($data['tc_order_id'])){
  25. $order_id_range[] = $data['tc_order_id'];
  26. }
  27. if(!empty($data['id'])){
  28. $order_id_range[] = $data['id'];
  29. }
  30. if(!empty($data['fenxiao_id'])){
  31. $order_id_range[] = $data['fenxiao_id'];
  32. }
  33. $sql = 'SELECT dealer_order_id FROM sdb_orders WHERE order_source=\'taofenxiao\' and dealer_order_id IN ("'.join('","',$order_id_range).'")';
  34. $row_order_range = $this->db->selectrow($sql);
  35. if($row_order_range){
  36. //????id????????????????id??dealer_order_id
  37. $data['tc_order_id'] = $row_order_range['dealer_order_id'];
  38. }else{
  39. //??????id????
  40. if(!$data['tc_order_id'] && $data['id'] ){
  41. $data['tc_order_id'] = $data['id'];
  42. }else if( !$data['tc_order_id'] && !$data['id'] && $data['fenxiao_id'] ){
  43. $data['tc_order_id'] = $data['fenxiao_id'];
  44. }
  45. }
  46. $res = $this->check_order_param($data,$msg);
  47. if(!$res){
  48. $this->add_application_error('error_param','','???“ '.join(' ',$msg)).' ” ??';
  49. }
  50. //???? ???????? ALIPAY_SURETY(???????)
  51. if($data['pay_type'] != 'ALIPAY_SURETY'){
  52. $this->api_response('true',false,array('data_info'=>'The pay type not accept'));
  53. }
  54. //add liuguoqing ??????
  55. $res = $this->db->exec("insert into sdb_coincidence set type_id='".$data['tc_order_id']."',type='ORDER',time='".time()."'");
  56. if(!$res){
  57. $this->api_response('true',false,array('data_info'=>'success'));
  58. return true;
  59. }
  60. //add end
  61. //??????B2B???????????
  62. $row_member = $this->getMember($data);
  63. //???????? ??????
  64. $sql = 'SELECT * FROM sdb_orders WHERE order_source=\'taofenxiao\' and dealer_order_id = "'.$data['tc_order_id'].'"';
  65. $row_order = $this->db->selectrow($sql);
  66. //??ship_area
  67. $data['ship_area'] = $this->get_area($data['state'],$data['city'],$data['district']);
  68. //if(!$data['ship_area']){
  69. $data['address'] = $data['state'].' '.$data['city'].' '.$data['district'].' '.$data['address'];
  70. //}
  71. //error_log(print_r($data,true)."\n",3,HOME_DIR."/logs/taobao_order_to_local.log");
  72. if($row_order){
  73. //error_log(print_r($row_order,true)."\n",3,HOME_DIR."/logs/taobao_order_to_local.log");
  74. //??????
  75. $data['order_id'] = $row_order['order_id'];
  76. $data['ship_status'] = $row_order['ship_status'];
  77. $data['pay_status'] = $row_order['pay_status'];
  78. if($data['status'] == 'WAIT_BUYER_PAY' || $data['status'] == 'WAIT_CONFIRM' || $data['status'] == 'WAIT_CONFIRM_WAIT_SEND_GOODS'){
  79. //1.??? ??????
  80. if(strtotime($data['modified']) > $row_order['last_change_time']){
  81. $this->update_order_data($data,$row_member);
  82. }
  83. }
  84. if($data['status'] == 'WAIT_SELLER_SEND_GOODS' || $data['status'] == 'CONFIRM_WAIT_SEND_GOODS'){
  85. //2.??????: ?????,?????
  86. if(strtotime($data['modified']) > $row_order['last_change_time'] && $row_order['pay_status'] != 1){
  87. if($row_order['pay_status'] == 0){
  88. $this->update_order_data($data,$row_member);
  89. //??????OME
  90. if($this->ome_node_id){
  91. $this->matrix->createOrder($data['order_id']);
  92. }
  93. $this->create_payment($data,$row_member);
  94. }
  95. $this->update_order_status($data['order_id'],'PAY_WAIT_SEND');
  96. }
  97. }
  98. else if($data['status'] == 'WAIT_BUYER_CONFIRM_GOODS' || $data['status'] == 'WAIT_CONFIRM_SEND_GOODS' || $data['status'] == 'WAIT_CONFIRM_GOODS_CONFIRM' || $data['status'] == 'CONFIRM_WAIT_SEND_GOODS'){
  99. //3.??????:
  100. if(strtotime($data['modified']) > $row_order['last_change_time'] && $row_order['ship_status'] != 1){
  101. if($row_order['pay_status'] == 0){
  102. $this->update_order_data($data,$row_member);
  103. $this->create_payment($data,$row_member);
  104. }
  105. $this->update_order_status($row_order['order_id'],'SEND_WAIT_CONFIRM');
  106. //???????????
  107. if($this->ome_node_id){
  108. $this->matrix->update_ship_status($data['order_id'],1);
  109. }
  110. }
  111. }
  112. else if($data['status'] == 'TRADE_FINISHED'){
  113. //4.??? ??????
  114. if(strtotime($data['modified']) > $row_order['last_change_time'] && $row_order['status'] != 'finish'){
  115. if($row_order['pay_status'] == 0){
  116. $this->update_order_data($data,$row_member);
  117. }
  118. $this->update_order_status($row_order['order_id'],'FINISHED');
  119. }
  120. }
  121. else if($data['status'] == 'TRADE_CLOSED'){
  122. //5.??? ?? ????
  123. if(strtotime($data['modified']) > $row_order['last_change_time'] && $row_order['status'] != 'dead'){
  124. if($row_order['pay_status'] == 0){
  125. $this->update_order_data($data,$row_member);
  126. }
  127. $this->update_order_status($row_order['order_id'],'CLOSED');
  128. }
  129. }
  130. //5.??? ???? ????? ?????
  131. $refund_status = '';
  132. $refuneMoney = 0;
  133. $row_money = 0;
  134. $item_c = count($data['items']);//????
  135. $refu_c = 0;
  136. $refunding = false;
  137. foreach($data['items'] as $k=>$v){
  138. if($v['status'] == 'TRADE_REFUNDED'){
  139. $refu_c++;
  140. }
  141. if($v['status'] == 'TRADE_REFUNDING'){
  142. $refunding = true;
  143. }
  144. $refuneMoney = bcadd($refuneMoney,$v['refund_fee'],2);
  145. }
  146. //??????
  147. if($refu_c == 0 && $refunding){
  148. $refund_status = 'REFUNDING';//????????????????
  149. }elseif($refu_c > 0 && $refu_c < $item_c){
  150. $refund_status = 'PART_REFUND';//????
  151. }elseif($refu_c == $item_c){
  152. $refund_status = 'REFUNDED';//????
  153. }
  154. //???
  155. if($refund_status == 'PART_REFUND' || $refund_status == 'REFUNDED' || $refuneMoney>0){
  156. $sql = 'SELECT SUM(money) as money FROM sdb_refunds WHERE order_id = "'.$data['order_id'].'"';
  157. $row_refund = $this->db->selectrow($sql);
  158. if(!$row_refund){
  159. $this->create_refund($data,$row_member,$refuneMoney);
  160. }else if($row_refund['money'] < $refuneMoney){
  161. $money = bcsub($refuneMoney,$row_refund['money'],2);
  162. $this->create_refund($data,$row_member,$money);
  163. }
  164. }
  165. //????
  166. if($refund_status == 'REFUNDING'){
  167. $this->update_order_status($row_order['order_id'],'REFUNDING');
  168. }else if($refund_status == 'REFUNDED'){
  169. $this->update_order_status($row_order['order_id'],'REFUNDED');
  170. }else if($refund_status == 'PART_REFUND'){
  171. $this->update_order_status($row_order['order_id'],'PART_REFUND');
  172. }
  173. //???????????????????
  174. $sql = 'UPDATE sdb_orders SET memo = "'._addslashes($data['memo']).'" WHERE order_id = "'.$row_order['order_id'].'"';
  175. $this->db->exec($sql);
  176. //??????????
  177. if($this->ome_node_id){
  178. $messagedata['order_id'] = $row_order['order_id'];
  179. $messagedata['memo'] = $data['memo'];
  180. $this->matrix->update_order_buyer_message($messagedata);
  181. }
  182. }else{
  183. //???????,?????????
  184. if($data['status'] == 'WAIT_BUYER_PAY' || $data['status'] == 'WAIT_CONFIRM' || $data['status'] == 'WAIT_CONFIRM_WAIT_SEND_GOODS'){
  185. //1.????:?? 1??????????
  186. $this->createOrder('WAIT_PAY',$data,$row_member);
  187. }
  188. if($data['status'] == 'WAIT_SELLER_SEND_GOODS' || $data['status'] == 'CONFIRM_WAIT_SEND_GOODS'){
  189. //2.??????: ?? 1????????????? 2???
  190. $this->createOrder('PAY_WAIT_SEND',$data,$row_member);
  191. }
  192. }
  193. //add liuguoqing ??????
  194. $this->db->exec("delete from sdb_coincidence where type_id='".$data['tc_order_id']."' and type='ORDER' ");
  195. //add end
  196. //????????
  197. $this->logic($data['tc_order_id']);
  198. //????????
  199. $this->api_response('true',false,array('data_info'=>'success'));
  200. }
  201. //????????
  202. function logic($order_id){
  203. $area=$this->db->selectrow("select ship_area from sdb_orders where order_source=\'taofenxiao\' and dealer_order_id='$order_id'");
  204. $area_id=explode(':',$area['ship_area']);
  205. $area_id=$area_id[2];
  206. if(!is_numeric($area_id)){ //?????????????????????????
  207. return false;
  208. }
  209. $sql='select instr(a.areaid_group,",'.$area_id.',") as num,b.dt_name,b.dt_id from sdb_dly_h_area a,sdb_dly_type b where a.dt_id=b.dt_id and b.def="Y"';
  210. $rs=$this->db->select($sql);
  211. foreach($rs as $v){
  212. if($v['num']>0){
  213. $in_def=true;
  214. $ship_id=$v['dt_id'];
  215. $shipping=$v['dt_name'];
  216. break;
  217. }
  218. }
  219. if($in_def){ //????????????????
  220. $this->db->exec("update sdb_orders set shipping_id=".$ship_id.",shipping='".$shipping."' where order_source=\'taofenxiao\' and dealer_order_id='$order_id'");
  221. }
  222. else{ //??EMS??????
  223. $this->db->exec("update sdb_orders set shipping_id=1,shipping='????' where order_source=\'taofenxiao\' and dealer_order_id='$order_id'");
  224. }
  225. }
  226. function check_order_param($data,&$msg){
  227. $preg_money = '^[0-9]+(\.[0-9]+)?$';
  228. $msg = array();
  229. $flag = true;
  230. if(empty($data['tc_order_id'])){
  231. $msg[] = 'id';
  232. $flag = false;
  233. }
  234. if(empty($data['pay_type'])){
  235. $msg[] = 'pay_type';
  236. $flag = false;
  237. }
  238. if(empty($data['status'])){
  239. $msg[] = 'status';
  240. $flag = false;
  241. }
  242. if(empty($data['distributor_username'])){
  243. $msg[] = 'distributor_username';
  244. $flag = false;
  245. }
  246. if(!ereg($preg_money,$data['total_fee'])){
  247. $msg[] = 'total_fee';
  248. $flag = false;
  249. }
  250. if(!ereg($preg_money,$data['post_fee'])){
  251. $msg[] = 'post_fee';
  252. $flag = false;
  253. }
  254. if(empty($data['name'])){
  255. $msg[] = 'name';
  256. $flag = false;
  257. }
  258. if(empty($data['address'])){
  259. $msg[] = 'address';
  260. $flag = false;
  261. }
  262. // if(empty($data['state'])){
  263. // $msg[] = 'state';
  264. // $flag = false;
  265. // }
  266. // if(empty($data['city'])){
  267. // $msg[] = 'city';
  268. // $flag = false;
  269. // }
  270. // if(empty($data['district'])){
  271. // $msg[] = 'district';
  272. // $flag = false;
  273. // }
  274. if(empty($data['zip'])){
  275. $msg[] = 'zip';
  276. $flag = false;
  277. }
  278. if(empty($data['items'])){
  279. $msg[] = 'items';
  280. $flag = false;
  281. }
  282. foreach($data['items'] as $k=>$v){
  283. if(empty($v['title'])){
  284. $msg[] = 'items'.$k.'=>title';
  285. $flag = false;
  286. }
  287. if(!ereg($preg_money,$v['price'])){
  288. $msg[] = 'items'.$k.'=>price';
  289. $flag = false;
  290. }
  291. if(!ereg($preg_money,$v['total_fee'])){
  292. $msg[] = 'items'.$k.'=>total_fee';
  293. $flag = false;
  294. }
  295. }
  296. return $flag;
  297. }
  298. function getMember($data){//????? 1?????????2??????????
  299. /*$sql = 'SELECT * FROM sdb_members WHERE uname = "taofenxiao_'.$data['distributor_username'].'"';
  300. $row_member = $this->db->selectrow($sql);
  301. if(!$row_member){
  302. $sql = 'SELECT * FROM sdb_member_lv WHERE name = "????"';
  303. $row_member_lv = $this->db->selectrow($sql);
  304. $sql = 'INSERT INTO sdb_members SET
  305. member_lv_id = "'.$row_member_lv['member_lv_id'].'",
  306. uname = "taofenxiao_'.$data['distributor_username'].'",
  307. password = "'.md5('123456').'",
  308. role_type = "taofenxiao",
  309. dealer_purview="2",
  310. confirm=1';
  311. $res = $this->db->exec($sql);
  312. if(!$res){
  313. $this->add_application_error('error_DB');
  314. }
  315. $sql = 'SELECT * FROM sdb_members WHERE uname = "taofenxiao_'.$data['distributor_username'].'"';
  316. $row_member = $this->db->selectrow($sql);
  317. $sql = 'INSERT INTO sdb_pline_to_dealer SET member_id = "'.$row_member['member_id'].'",pline_id=1,last_modify='.time();
  318. $this->db->exec($sql);
  319. //?????????sdb_member_relations
  320. $sql = 'INSERT INTO sdb_member_relations SET name = "'.$data['distributor_username'].'",uname = "'.$row_member['uname'].'",member_id = "'.$row_member['member_id'].'",type = "taobao",time ='.time();
  321. $this->db->exec($sql);
  322. //???????
  323. $mdl_matrix = $this->system->loadModel('system/matrix');
  324. $mdl_matrix->getDistributors($data['distributor_username']);
  325. }
  326. return $row_member;*/
  327. $mdl_member = $this->system->loadModel('member/member');
  328. return $mdl_member->getTaoMember($data['distributor_username']);
  329. }
  330. function createOrder($type,$data,$row_member){
  331. //error_log(print_r($type,true).print_r($row_member,true)."\n\n",3,HOME_DIR."/logs/taobao_order_to_local.log");
  332. $mdl_order = $this->system->loadModel('trading/order');
  333. $mdl_payment = $this->system->loadModel('trading/payment');
  334. // $order_id = $mdl_order->gen_id();
  335. $order_id = $data['tc_order_id'];
  336. $tostr = '';
  337. $itemnum = 0;
  338. $item_amount = 0;
  339. $data['order_id'] = $order_id;
  340. //????????????? ????? ???
  341. $sql = 'SELECT * FROM sdb_payment_cfg WHERE custom_name LIKE "%???%"';
  342. $row_payment = $this->db->selectrow($sql);
  343. $payment = $row_payment?$row_payment['id']:0;
  344. $paymethod = $row_payment?$row_payment['custom_name']:'';
  345. //???sdb_order_items
  346. foreach($data['items'] as $product){
  347. //????
  348. $local_product = $this->match_product($product);
  349. //????
  350. if($local_product){
  351. //$this->freezStore($local_product['bn'],$product['num']);
  352. $sql = "INSERT INTO sdb_order_items SET
  353. order_id = '".$order_id."',
  354. product_id = '".$local_product['product_id']."',
  355. dly_status = 'storage',
  356. type_id = '".$local_product['type_id']."',
  357. bn = '".$local_product['bn']."',
  358. tao_name = '".$product['tao_name']."',
  359. name = '".$local_product['name']."(".$local_product['pdt_desc'].")',
  360. price = '".$product['price']."',
  361. amount = '".$product['total_fee']."',
  362. score = 0,
  363. nums = '".$product['num']."',
  364. sendnum = 0,
  365. addon = '".$local_product['addon']."',
  366. is_type = 'goods'";
  367. $res = $this->db->exec($sql);
  368. if(!$res){
  369. $this->add_application_error('error_DB');
  370. }
  371. //?????sdb_orders???
  372. $tostr.= $local_product['name'].'('.$local_product['pdt_desc'].')('.$product['num'].')';
  373. $item_amount=bcadd($item_amount,$product['total_fee'],2);
  374. $itemnum+=$product['num'];
  375. }else{//?????
  376. $sql = 'INSERT INTO sdb_order_items SET
  377. order_id = "'.$order_id.'",
  378. product_id = 0,
  379. dly_status = "storage",
  380. tao_name = "'.$product['tao_name'].'",
  381. price = "'.$product['price'].'",
  382. amount = "'.$product['total_fee'].'",
  383. score = 0,
  384. nums = "'.$product['num'].'",
  385. sendnum = 0,
  386. is_type = "goods"';
  387. $res = $this->db->exec($sql);
  388. if(!$res){
  389. $this->add_application_error('error_DB');
  390. }
  391. //?????sdb_orders???
  392. $itemnum+=$product['num'];
  393. $item_amount=bcadd($item_amount,$product['total_fee'],2);
  394. }
  395. }
  396. //??sdb_orders?
  397. $sql = 'INSERT INTO sdb_orders SET
  398. order_id = "'.$order_id.'",
  399. member_id = "'.$row_member['member_id'].'",
  400. confirm = "N",
  401. status = "active",
  402. pay_status = 0,
  403. ship_status = 0,
  404. payment = "'.$payment.'",
  405. tostr = "'.$tostr.'",
  406. itemnum = "'.$itemnum.'",
  407. last_change_time = '.strtotime($data['modified']).',
  408. acttime = '.strtotime($data['created']).',
  409. createtime = '.strtotime($data['created']).',
  410. ship_name = "'.$data['name'].'",
  411. ship_area = "'.$data['ship_area'].'",
  412. ship_addr = "'.$data['address'].'",
  413. ship_zip = "'.$data['zip'].'",
  414. ship_tel = "'.$data['phone'].'",
  415. ship_time = "????",
  416. ship_mobile = "'.$data['mobile_phone'].'",
  417. cost_item = "'.$item_amount.'",
  418. cost_freight = "'.$data['post_fee'].'",
  419. currency = "CNY",
  420. final_amount = "'.bcadd($data['total_fee'],$data['post_fee'],2).'",
  421. total_amount = "'.bcadd($data['total_fee'],$data['post_fee'],2).'",
  422. discount = "'.bcsub($data['total_fee'],$item_amount,2).'",
  423. memo = "'.$data['memo'].'",
  424. dealer_order_id = "'.$data['tc_order_id'].'",
  425. taobao_id = "'.$data['id'].'",
  426. expired_time = '.(strtotime($data['created'])+86400*3).',
  427. order_source = "taofenxiao"';
  428. $res = $this->db->exec($sql);
  429. if(!$res){
  430. $this->add_application_error('error_DB');
  431. }
  432. //?????????sdb_order_to_so
  433. $sql = 'INSERT INTO sdb_order_to_so SET order_id = "'.$order_id.'", so_id = "'.$data['tc_order_id'].'",member_id = "'.$row_member['member_id'].'"';
  434. $this->db->exec($sql);
  435. //??????????
  436. if($type == 'PAY_WAIT_SEND'){//2.??????: ?? 1????????????? 2???
  437. $this->update_order_status($data['order_id'],'PAY_WAIT_SEND');
  438. //??????OME
  439. if($this->ome_node_id){
  440. $this->matrix->createOrder($order_id);
  441. }
  442. $this->create_payment($data,$row_member);
  443. }
  444. $this->addLog($order_id,'????','??');
  445. }
  446. //?????
  447. function create_payment($data,$row_member){
  448. //????????????? ????? ???
  449. $mdl_payment = $this->system->loadModel('trading/payment');
  450. $sql = 'SELECT * FROM sdb_payment_cfg WHERE custom_name LIKE "%???%"';
  451. $row_payment = $this->db->selectrow($sql);
  452. $payment = $row_payment?$row_payment['id']:0;
  453. $paymethod = $row_payment?$row_payment['custom_name']:'';
  454. $payment_id = $mdl_payment->gen_id();
  455. $sql = 'INSERT INTO sdb_payments SET
  456. payment_id = "'.$payment_id.'",
  457. order_id = "'.$data['order_id'].'",
  458. member_id = "'.$row_member['member_id'].'",
  459. bank = "alipay",
  460. currency = "CNY",
  461. money = "'.$data['distributor_payment'].'",
  462. paycost = "0.000",
  463. cur_money = "'.$data['distributor_payment'].'",
  464. pay_type = "online",
  465. payment = "'.$payment.'",
  466. paymethod = "'.$paymethod.'",
  467. t_begin = "'.strtotime($data['modified']).'",
  468. t_end = "'.strtotime($data['modified']).'",
  469. status = "succ",
  470. memo = "????????"';
  471. $res = $this->db->exec($sql);
  472. if(!$res){
  473. $this->add_application_error('error_DB');
  474. }
  475. //????OME
  476. //???????OME
  477. if($this->ome_node_id){
  478. //????????????????
  479. $apilog = $this->system->loadModel('trading/apilog');
  480. $toOme=$apilog->getStatus($data['order_id']);
  481. if(!$toOme){//??????
  482. $this->matrix->createOrder($data['order_id']);
  483. }
  484. $this->matrix->send_payment_to_matrix($payment_id);
  485. }
  486. $this->addLog($data['order_id'],'????'.$data['distributor_payment'],'??');
  487. }
  488. //????
  489. function match_product(&$product){
  490. //??tao_name
  491. $tao_name = '';
  492. $tao_spec = array();
  493. $name_tmp = array();
  494. $name_tmp = explode('?',$product['sku_properties']);
  495. foreach($name_tmp as $v){
  496. $spec = explode('?',$v);
  497. $tao_spec[] = $spec[1];
  498. }
  499. $tao_name = $product['title'].'('.implode('?',$tao_spec).')';
  500. $product['tao_name'] = $tao_name;
  501. //1.?????????
  502. $sql = 'SELECT p.*,g.type_id,t.setting
  503. FROM sdb_taobao_products as tp
  504. LEFT JOIN sdb_products as p ON tp.bn = p.bn
  505. LEFT JOIN sdb_goods as g ON p.goods_id = g.goods_id
  506. LEFT JOIN sdb_goods_type as t ON g.type_id = t.type_id
  507. WHERE tp.name_hash = "'.md5($product['tao_name']).'"';
  508. $row_product1 = $this->db->selectrow($sql);
  509. if($row_product1){
  510. $props = unserialize($row_product1['props']);
  511. $setting = unserialize($row_product1['setting']);
  512. $addon = array_merge($setting,$props);
  513. $row_product1['addon'] = serialize($addon);
  514. return $row_product1;
  515. }
  516. //2.?????SKU???
  517. $sql = 'SELECT p.*,g.type_id,t.setting
  518. FROM sdb_products as p
  519. LEFT JOIN sdb_goods as g ON p.goods_id = g.goods_id
  520. LEFT JOIN sdb_goods_type as t ON g.type_id = t.type_id
  521. WHERE p.bn = "'.$product['sku_id'].'" or p.bn = "'.$product['sku_outer_id'].'" or p.bn = "'.$product['item_outer_id'].'"';
  522. $row_product2 = $this->db->selectrow($sql);
  523. if($row_product2){
  524. $props = unserialize($row_product2['props']);
  525. $setting = unserialize($row_product2['setting']);
  526. $addon = array_merge($setting,$props);
  527. $row_product2['addon'] = serialize($addon);
  528. return $row_product2;
  529. }
  530. return false;
  531. }
  532. function create_refund($data,$row_member,$money){
  533. $mdl_refund = $this->system->loadModel('trading/refund');
  534. $refund_id = $mdl_refund->gen_id();
  535. //????????????? ????? ???
  536. $mdl_payment = $this->system->loadModel('trading/payment');
  537. $sql = 'SELECT * FROM sdb_payment_cfg WHERE custom_name LIKE "%???%"';
  538. $row_payment = $this->db->selectrow($sql);
  539. $payment = $row_payment?$row_payment['id']:0;
  540. $paymethod = $row_payment?$row_payment['custom_name']:'';
  541. $sql = 'INSERT INTO sdb_refunds SET
  542. refund_id = "'.$refund_id.'",
  543. order_id = "'.$data['order_id'].'",
  544. member_id = "'.$row_member['member_id'].'",
  545. bank = "alipay",
  546. currency = "'.$refund_id.'",
  547. money = "'.$money.'",
  548. pay_type = "online",
  549. payment = "'.$payment.'",
  550. paymethod = "'.$paymethod.'",
  551. memo = "????????",
  552. status = "sent",
  553. title = "title",
  554. t_ready = "'.strtotime($data['modified']).'",
  555. t_sent = "'.strtotime($data['modified']).'"';
  556. $res = $this->db->exec($sql);
  557. if(!$res){
  558. $this->add_application_error('error_DB');
  559. }
  560. $this->addLog($data['order_id'],'????'.$money,'??');
  561. }
  562. function update_order_status($order_id,$status){
  563. if($status == 'PAY_WAIT_SEND'){
  564. $sql = 'UPDATE sdb_orders SET pay_status = 1,payed = final_amount WHERE order_id = "'.$order_id.'"';
  565. }
  566. if($status == 'SEND_WAIT_CONFIRM'){
  567. $sql = 'UPDATE sdb_orders SET pay_status = 1,payed = final_amount,ship_status = 1 WHERE order_id = "'.$order_id.'"';
  568. }
  569. if($status == 'PART_REFUND'){
  570. $sql = 'UPDATE sdb_orders SET pay_status = 4,payed = final_amount WHERE order_id = "'.$order_id.'"';
  571. }
  572. if($status == 'REFUNDED'){
  573. $sql = 'UPDATE sdb_orders SET pay_status = 5,payed = final_amount WHERE order_id = "'.$order_id.'"';
  574. }
  575. if($status == 'REFUNDING'){
  576. $sql = 'UPDATE sdb_orders SET pay_status = 6,payed = final_amount WHERE order_id = "'.$order_id.'"';
  577. }
  578. if($status == 'FINISHED'){
  579. $sql = 'UPDATE sdb_orders SET status = "finish" WHERE order_id = "'.$order_id.'"';
  580. $this->addLog($order_id,'????','??');
  581. }
  582. if($status == 'CLOSED'){
  583. $sql = 'UPDATE sdb_orders SET status = "dead" WHERE order_id = "'.$order_id.'"';
  584. $this->addLog($order_id,'????','??');
  585. }
  586. $res = $this->db->exec($sql);
  587. if(!$res){
  588. $this->add_application_error('error_DB');
  589. }
  590. }
  591. function update_order_data($data,$row_member){
  592. //?????
  593. //$this->callbak_store($data['order_id']);
  594. $order_id = $data['order_id'];
  595. $ship_status = $data['ship_status']?$data['ship_status']:0;
  596. $pay_status = $data['pay_status']?$data['pay_status']:0;
  597. //?????????????????????????? ????? ????order_id?????????????????????????
  598. $sql = 'DELETE FROM sdb_orders WHERE order_id = "'.$order_id.'"';
  599. $res = $this->db->exec($sql);
  600. if(!$res){
  601. $this->add_application_error('error_DB');
  602. }
  603. $sql = 'DELETE FROM sdb_order_items WHERE order_id = "'.$order_id.'"';
  604. $res = $this->db->exec($sql);
  605. if(!$res){
  606. $this->add_application_error('error_DB');
  607. }
  608. $mdl_payment = $this->system->loadModel('trading/payment');
  609. $tostr = '';
  610. $itemnum = 0;
  611. $item_amount = 0;
  612. //????????????? ????? ???
  613. $sql = 'SELECT * FROM sdb_payment_cfg WHERE custom_name LIKE "%???%"';
  614. $row_payment = $this->db->selectrow($sql);
  615. $payment = $row_payment?$row_payment['id']:0;
  616. $paymethod = $row_payment?$row_payment['custom_name']:'';
  617. //???sdb_order_items
  618. foreach($data['items'] as $product){
  619. if($product['status'] == 'TRADE_CLOSED') continue;//?????????????
  620. //????
  621. $local_product = $this->match_product($product);
  622. //????
  623. if($local_product){
  624. //????
  625. //$this->freezStore($local_product['bn'],$product['num']);
  626. $sql = "INSERT INTO sdb_order_items SET
  627. order_id = '".$order_id."',
  628. product_id = '".$local_product['product_id']."',
  629. dly_status = 'storage',
  630. type_id = '".$local_product['type_id']."',
  631. bn = '".$local_product['bn']."',
  632. tao_name = '".$product['title'].";".$product['sku_properties']."',
  633. name = '".$local_product['name']."(".$local_product['pdt_desc'].")',
  634. price = '".$product['price']."',
  635. amount = '".$product['total_fee']."',
  636. score = 0,
  637. nums = '".$product['num']."',
  638. sendnum = 0,
  639. addon = '".$local_product['addon']."',
  640. is_type = 'goods'";
  641. $res = $this->db->exec($sql);
  642. if(!$res){
  643. $this->add_application_error('error_DB');
  644. }
  645. //?????sdb_orders???
  646. $tostr.= $local_product['name'].'('.$local_product['pdt_desc'].')('.$product['num'].')';
  647. $itemnum+=$product['num'];
  648. $item_amount=bcadd($item_amount,$product['total_fee'],2);
  649. }else{//?????
  650. $sql = 'INSERT INTO sdb_order_items SET
  651. order_id = "'.$order_id.'",
  652. product_id = 0,
  653. dly_status = "storage",
  654. tao_name = "'.$product['title'].';'.$product['sku_properties'].'",
  655. price = "'.$product['price'].'",
  656. amount = "'.$product['total_fee'].'",
  657. score = 0,
  658. nums = "'.$product['num'].'",
  659. sendnum = 0,
  660. is_type = "goods"';
  661. $res = $this->db->exec($sql);
  662. if(!$res){
  663. $this->add_application_error('error_DB');
  664. }
  665. //?????sdb_orders???
  666. $itemnum+=$product['num'];
  667. $item_amount=bcadd($item_amount,$product['total_fee'],2);
  668. }
  669. }
  670. //??sdb_orders?
  671. $sql = 'INSERT INTO sdb_orders SET
  672. order_id = "'.$order_id.'",
  673. member_id = "'.$row_member['member_id'].'",
  674. confirm = "N",
  675. status = "active",
  676. pay_status = '.$pay_status.',
  677. ship_status = '.$ship_status.',
  678. payment = "'.$payment.'",
  679. tostr = "'.$tostr.'",
  680. itemnum = "'.$itemnum.'",
  681. last_change_time = '.strtotime($data['modified']).',
  682. acttime = '.strtotime($data['created']).',
  683. createtime = '.strtotime($data['created']).',
  684. ship_name = "'.$data['name'].'",
  685. ship_area = "'.$data['ship_area'].'",
  686. ship_addr = "'.$data['address'].'",
  687. ship_zip = "'.$data['zip'].'",
  688. ship_tel = "'.$data['phone'].'",
  689. ship_time = "????",
  690. ship_mobile = "'.$data['mobile_phone'].'",
  691. cost_item = "'.$item_amount.'",
  692. cost_freight = "'.$data['post_fee'].'",
  693. currency = "CNY",
  694. final_amount = "'.bcadd($data['total_fee'],$data['post_fee'],2).'",
  695. total_amount = "'.bcadd($data['total_fee'],$data['post_fee'],2).'",
  696. discount = "'.bcsub($data['total_fee'],$item_amount,2).'",
  697. memo = "'.$data['memo'].'",
  698. dealer_order_id = "'.$data['tc_order_id'].'",
  699. taobao_id = "'.$data['id'].'",
  700. expired_time = '.(strtotime($data['created'])+86400*3).',
  701. order_source = "taofenxiao"';
  702. $res = $this->db->exec($sql);
  703. if(!$res){
  704. $this->add_application_error('error_DB');
  705. }
  706. $this->addLog($order_id,'????','??');
  707. }
  708. function get_area($state,$city,$district){
  709. if($state){
  710. $sql = 'SELECT * FROM sdb_regions WHERE local_name = "'.$state.'" AND p_region_id is NULL';
  711. $row_state = $this->db->selectrow($sql);
  712. }
  713. if($city){
  714. $sql = 'SELECT * FROM sdb_regions WHERE local_name = "'.$city.'" AND p_region_id = '.$row_state['region_id'];
  715. $row_city = $this->db->selectrow($sql);
  716. }
  717. if($district){
  718. $sql = 'SELECT * FROM sdb_regions WHERE local_name = "'.$district.'" AND p_region_id = '.$row_city['region_id'];
  719. $row_district = $this->db->selectrow($sql);
  720. }
  721. if($row_state && $row_city && $row_district){
  722. return 'mainland:'.$state.'/'.$city.'/'.$district.':'.$row_district['region_id'];
  723. }elseif($row_state && $row_city){
  724. return 'mainland:'.$state.'/'.$city.':'.$row_city['region_id'];
  725. }elseif($row_state){
  726. return 'mainland:'.$state.':'.$row_state['region_id'];
  727. }else{
  728. return '';
  729. }
  730. }
  731. function addLog($order_id, $message, $behavior = '', $result = 'success'){
  732. $sql = 'INSERT INTO sdb_order_log SET
  733. order_id = "'.$order_id.'",
  734. behavior = "'.$behavior.'",
  735. result = "'.$result.'",
  736. log_text = "'._addslashes($message).'",
  737. acttime = "'.time().'"';
  738. $this->db->exec($sql);
  739. }
  740. function freezStore($bn,$num){
  741. $sql = 'SELECT * FROM sdb_products WHERE bn = "'.$bn.'"';
  742. $row_product = $this->db->selectrow($sql);
  743. //????
  744. if($row_product['freez']){
  745. $sql = 'UPDATE sdb_products SET freez=freez+'.$num.' WHERE bn = "'.$row_product['bn'].'"';
  746. }else{
  747. $sql = 'UPDATE sdb_products SET freez='.$num.' WHERE bn = "'.$row_product['bn'].'"';
  748. }
  749. $this->db->exec($sql);
  750. }
  751. function callbak_store($order_id){
  752. $sql = 'SELECT * FROM sdb_order_items WHERE order_id = "'.$order_id.'"';
  753. $items = $this->db->select($sql);
  754. foreach($items as $k=>$v){
  755. if($v['bn']){
  756. $sql = 'UPDATE sdb_products SET freez = freez-'.$v['nums'].' WHERE bn = "'.$v['bn'].'"';
  757. $this->db->exec($sql);
  758. }
  759. }
  760. }
  761. }
  762. ?>