/b2b/core/api/taobao/3.1/api_b2b_3_1_fenxiao.php
PHP | 825 lines | 646 code | 60 blank | 119 comment | 157 complexity | fc35ed9118428c574be8b5484a904e68 MD5 | raw file
- <?php
- include_once(CORE_DIR.'/api/shop_api_object.php');
- class api_b2b_3_1_fenxiao extends shop_api_object{
- var $app_error=array(
- 'error_param'=>array('no'=>'b_fenxiao_001','debug'=>'','level'=>'error','desc'=>'????','info'=>''),
- 'error_DB'=>array('no'=>'b_fenxiao_002','debug'=>'','level'=>'error','desc'=>'?????','info'=>'')
- );
- var $ome_node_id='';
- var $matrix='';
- /**
- * ???????????????????????????????????
- */
- function taobao_order_to_local($data){
- //?ome ??ome???
- if( !$this->ome_node_id ){
- $certificate = $this->system->loadModel('service/certificate');
- $this->ome_node_id = $certificate->getOmeNoteId();
- $this->ome_node_id and $this->matrix = $this->system->loadModel('system/matrix');
- }
- //??????
- $data['items'] = json_decode($data['items'],true);
- //?????????????????? tc_order_id, id, fenxiao_id?????????
- $order_id_range = array();
- if(!empty($data['tc_order_id'])){
- $order_id_range[] = $data['tc_order_id'];
- }
- if(!empty($data['id'])){
- $order_id_range[] = $data['id'];
- }
- if(!empty($data['fenxiao_id'])){
- $order_id_range[] = $data['fenxiao_id'];
- }
- $sql = 'SELECT dealer_order_id FROM sdb_orders WHERE order_source=\'taofenxiao\' and dealer_order_id IN ("'.join('","',$order_id_range).'")';
- $row_order_range = $this->db->selectrow($sql);
- if($row_order_range){
- //????id????????????????id??dealer_order_id
- $data['tc_order_id'] = $row_order_range['dealer_order_id'];
- }else{
- //??????id????
- if(!$data['tc_order_id'] && $data['id'] ){
- $data['tc_order_id'] = $data['id'];
- }else if( !$data['tc_order_id'] && !$data['id'] && $data['fenxiao_id'] ){
- $data['tc_order_id'] = $data['fenxiao_id'];
- }
- }
-
- $res = $this->check_order_param($data,$msg);
- if(!$res){
- $this->add_application_error('error_param','','??? '.join(' ',$msg)).' ??';
- }
-
- //???? ???????? ALIPAY_SURETY(???????)
- if($data['pay_type'] != 'ALIPAY_SURETY'){
- $this->api_response('true',false,array('data_info'=>'The pay type not accept'));
- }
-
- //add liuguoqing ??????
- $res = $this->db->exec("insert into sdb_coincidence set type_id='".$data['tc_order_id']."',type='ORDER',time='".time()."'");
- if(!$res){
- $this->api_response('true',false,array('data_info'=>'success'));
- return true;
- }
- //add end
-
- //??????B2B???????????
- $row_member = $this->getMember($data);
-
- //???????? ??????
- $sql = 'SELECT * FROM sdb_orders WHERE order_source=\'taofenxiao\' and dealer_order_id = "'.$data['tc_order_id'].'"';
- $row_order = $this->db->selectrow($sql);
-
- //??ship_area
- $data['ship_area'] = $this->get_area($data['state'],$data['city'],$data['district']);
- //if(!$data['ship_area']){
- $data['address'] = $data['state'].' '.$data['city'].' '.$data['district'].' '.$data['address'];
- //}
- //error_log(print_r($data,true)."\n",3,HOME_DIR."/logs/taobao_order_to_local.log");
- if($row_order){
- //error_log(print_r($row_order,true)."\n",3,HOME_DIR."/logs/taobao_order_to_local.log");
- //??????
- $data['order_id'] = $row_order['order_id'];
- $data['ship_status'] = $row_order['ship_status'];
- $data['pay_status'] = $row_order['pay_status'];
- if($data['status'] == 'WAIT_BUYER_PAY' || $data['status'] == 'WAIT_CONFIRM' || $data['status'] == 'WAIT_CONFIRM_WAIT_SEND_GOODS'){
- //1.??? ??????
- if(strtotime($data['modified']) > $row_order['last_change_time']){
- $this->update_order_data($data,$row_member);
- }
- }
- if($data['status'] == 'WAIT_SELLER_SEND_GOODS' || $data['status'] == 'CONFIRM_WAIT_SEND_GOODS'){
- //2.??????: ?????,?????
- if(strtotime($data['modified']) > $row_order['last_change_time'] && $row_order['pay_status'] != 1){
- if($row_order['pay_status'] == 0){
- $this->update_order_data($data,$row_member);
- //??????OME
- if($this->ome_node_id){
- $this->matrix->createOrder($data['order_id']);
- }
- $this->create_payment($data,$row_member);
- }
- $this->update_order_status($data['order_id'],'PAY_WAIT_SEND');
- }
- }
- 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'){
- //3.??????:
- if(strtotime($data['modified']) > $row_order['last_change_time'] && $row_order['ship_status'] != 1){
- if($row_order['pay_status'] == 0){
- $this->update_order_data($data,$row_member);
- $this->create_payment($data,$row_member);
- }
- $this->update_order_status($row_order['order_id'],'SEND_WAIT_CONFIRM');
- //???????????
- if($this->ome_node_id){
- $this->matrix->update_ship_status($data['order_id'],1);
- }
- }
- }
- else if($data['status'] == 'TRADE_FINISHED'){
- //4.??? ??????
- if(strtotime($data['modified']) > $row_order['last_change_time'] && $row_order['status'] != 'finish'){
- if($row_order['pay_status'] == 0){
- $this->update_order_data($data,$row_member);
- }
- $this->update_order_status($row_order['order_id'],'FINISHED');
- }
- }
- else if($data['status'] == 'TRADE_CLOSED'){
- //5.??? ?? ????
- if(strtotime($data['modified']) > $row_order['last_change_time'] && $row_order['status'] != 'dead'){
- if($row_order['pay_status'] == 0){
- $this->update_order_data($data,$row_member);
- }
- $this->update_order_status($row_order['order_id'],'CLOSED');
- }
- }
- //5.??? ???? ????? ?????
- $refund_status = '';
- $refuneMoney = 0;
- $row_money = 0;
- $item_c = count($data['items']);//????
- $refu_c = 0;
- $refunding = false;
- foreach($data['items'] as $k=>$v){
- if($v['status'] == 'TRADE_REFUNDED'){
- $refu_c++;
- }
- if($v['status'] == 'TRADE_REFUNDING'){
- $refunding = true;
- }
- $refuneMoney = bcadd($refuneMoney,$v['refund_fee'],2);
- }
- //??????
- if($refu_c == 0 && $refunding){
- $refund_status = 'REFUNDING';//????????????????
- }elseif($refu_c > 0 && $refu_c < $item_c){
- $refund_status = 'PART_REFUND';//????
- }elseif($refu_c == $item_c){
- $refund_status = 'REFUNDED';//????
- }
- //???
- if($refund_status == 'PART_REFUND' || $refund_status == 'REFUNDED' || $refuneMoney>0){
- $sql = 'SELECT SUM(money) as money FROM sdb_refunds WHERE order_id = "'.$data['order_id'].'"';
- $row_refund = $this->db->selectrow($sql);
-
- if(!$row_refund){
- $this->create_refund($data,$row_member,$refuneMoney);
- }else if($row_refund['money'] < $refuneMoney){
- $money = bcsub($refuneMoney,$row_refund['money'],2);
- $this->create_refund($data,$row_member,$money);
- }
- }
- //????
- if($refund_status == 'REFUNDING'){
- $this->update_order_status($row_order['order_id'],'REFUNDING');
- }else if($refund_status == 'REFUNDED'){
- $this->update_order_status($row_order['order_id'],'REFUNDED');
- }else if($refund_status == 'PART_REFUND'){
- $this->update_order_status($row_order['order_id'],'PART_REFUND');
- }
-
- //???????????????????
- $sql = 'UPDATE sdb_orders SET memo = "'._addslashes($data['memo']).'" WHERE order_id = "'.$row_order['order_id'].'"';
- $this->db->exec($sql);
-
- //??????????
- if($this->ome_node_id){
- $messagedata['order_id'] = $row_order['order_id'];
- $messagedata['memo'] = $data['memo'];
- $this->matrix->update_order_buyer_message($messagedata);
- }
-
- }else{
- //???????,?????????
- if($data['status'] == 'WAIT_BUYER_PAY' || $data['status'] == 'WAIT_CONFIRM' || $data['status'] == 'WAIT_CONFIRM_WAIT_SEND_GOODS'){
- //1.????:?? 1??????????
- $this->createOrder('WAIT_PAY',$data,$row_member);
- }
- if($data['status'] == 'WAIT_SELLER_SEND_GOODS' || $data['status'] == 'CONFIRM_WAIT_SEND_GOODS'){
- //2.??????: ?? 1????????????? 2???
- $this->createOrder('PAY_WAIT_SEND',$data,$row_member);
- }
- }
- //add liuguoqing ??????
- $this->db->exec("delete from sdb_coincidence where type_id='".$data['tc_order_id']."' and type='ORDER' ");
- //add end
-
- //????????
- $this->logic($data['tc_order_id']);
- //????????
-
- $this->api_response('true',false,array('data_info'=>'success'));
- }
-
- //????????
- function logic($order_id){
- $area=$this->db->selectrow("select ship_area from sdb_orders where order_source=\'taofenxiao\' and dealer_order_id='$order_id'");
- $area_id=explode(':',$area['ship_area']);
- $area_id=$area_id[2];
- if(!is_numeric($area_id)){ //?????????????????????????
- return false;
- }
- $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"';
- $rs=$this->db->select($sql);
- foreach($rs as $v){
- if($v['num']>0){
- $in_def=true;
- $ship_id=$v['dt_id'];
- $shipping=$v['dt_name'];
- break;
- }
- }
- if($in_def){ //????????????????
- $this->db->exec("update sdb_orders set shipping_id=".$ship_id.",shipping='".$shipping."' where order_source=\'taofenxiao\' and dealer_order_id='$order_id'");
- }
- else{ //??EMS??????
- $this->db->exec("update sdb_orders set shipping_id=1,shipping='????' where order_source=\'taofenxiao\' and dealer_order_id='$order_id'");
- }
- }
-
- function check_order_param($data,&$msg){
- $preg_money = '^[0-9]+(\.[0-9]+)?$';
- $msg = array();
- $flag = true;
- if(empty($data['tc_order_id'])){
- $msg[] = 'id';
- $flag = false;
- }
- if(empty($data['pay_type'])){
- $msg[] = 'pay_type';
- $flag = false;
- }
- if(empty($data['status'])){
- $msg[] = 'status';
- $flag = false;
- }
- if(empty($data['distributor_username'])){
- $msg[] = 'distributor_username';
- $flag = false;
- }
- if(!ereg($preg_money,$data['total_fee'])){
- $msg[] = 'total_fee';
- $flag = false;
- }
- if(!ereg($preg_money,$data['post_fee'])){
- $msg[] = 'post_fee';
- $flag = false;
- }
- if(empty($data['name'])){
- $msg[] = 'name';
- $flag = false;
- }
- if(empty($data['address'])){
- $msg[] = 'address';
- $flag = false;
- }
- // if(empty($data['state'])){
- // $msg[] = 'state';
- // $flag = false;
- // }
- // if(empty($data['city'])){
- // $msg[] = 'city';
- // $flag = false;
- // }
- // if(empty($data['district'])){
- // $msg[] = 'district';
- // $flag = false;
- // }
- if(empty($data['zip'])){
- $msg[] = 'zip';
- $flag = false;
- }
- if(empty($data['items'])){
- $msg[] = 'items';
- $flag = false;
- }
- foreach($data['items'] as $k=>$v){
- if(empty($v['title'])){
- $msg[] = 'items'.$k.'=>title';
- $flag = false;
- }
- if(!ereg($preg_money,$v['price'])){
- $msg[] = 'items'.$k.'=>price';
- $flag = false;
- }
- if(!ereg($preg_money,$v['total_fee'])){
- $msg[] = 'items'.$k.'=>total_fee';
- $flag = false;
- }
- }
-
- return $flag;
- }
-
-
- function getMember($data){//????? 1?????????2??????????
- /*$sql = 'SELECT * FROM sdb_members WHERE uname = "taofenxiao_'.$data['distributor_username'].'"';
- $row_member = $this->db->selectrow($sql);
- if(!$row_member){
- $sql = 'SELECT * FROM sdb_member_lv WHERE name = "????"';
- $row_member_lv = $this->db->selectrow($sql);
- $sql = 'INSERT INTO sdb_members SET
- member_lv_id = "'.$row_member_lv['member_lv_id'].'",
- uname = "taofenxiao_'.$data['distributor_username'].'",
- password = "'.md5('123456').'",
- role_type = "taofenxiao",
- dealer_purview="2",
- confirm=1';
- $res = $this->db->exec($sql);
- if(!$res){
- $this->add_application_error('error_DB');
- }
- $sql = 'SELECT * FROM sdb_members WHERE uname = "taofenxiao_'.$data['distributor_username'].'"';
- $row_member = $this->db->selectrow($sql);
-
- $sql = 'INSERT INTO sdb_pline_to_dealer SET member_id = "'.$row_member['member_id'].'",pline_id=1,last_modify='.time();
- $this->db->exec($sql);
-
- //?????????sdb_member_relations
- $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();
- $this->db->exec($sql);
-
- //???????
- $mdl_matrix = $this->system->loadModel('system/matrix');
- $mdl_matrix->getDistributors($data['distributor_username']);
- }
- return $row_member;*/
- $mdl_member = $this->system->loadModel('member/member');
- return $mdl_member->getTaoMember($data['distributor_username']);
- }
-
- function createOrder($type,$data,$row_member){
- //error_log(print_r($type,true).print_r($row_member,true)."\n\n",3,HOME_DIR."/logs/taobao_order_to_local.log");
-
- $mdl_order = $this->system->loadModel('trading/order');
- $mdl_payment = $this->system->loadModel('trading/payment');
- // $order_id = $mdl_order->gen_id();
- $order_id = $data['tc_order_id'];
- $tostr = '';
- $itemnum = 0;
- $item_amount = 0;
- $data['order_id'] = $order_id;
-
- //????????????? ????? ???
- $sql = 'SELECT * FROM sdb_payment_cfg WHERE custom_name LIKE "%???%"';
- $row_payment = $this->db->selectrow($sql);
- $payment = $row_payment?$row_payment['id']:0;
- $paymethod = $row_payment?$row_payment['custom_name']:'';
-
- //???sdb_order_items
- foreach($data['items'] as $product){
- //????
- $local_product = $this->match_product($product);
-
- //????
- if($local_product){
- //$this->freezStore($local_product['bn'],$product['num']);
-
- $sql = "INSERT INTO sdb_order_items SET
- order_id = '".$order_id."',
- product_id = '".$local_product['product_id']."',
- dly_status = 'storage',
- type_id = '".$local_product['type_id']."',
- bn = '".$local_product['bn']."',
- tao_name = '".$product['tao_name']."',
- name = '".$local_product['name']."(".$local_product['pdt_desc'].")',
- price = '".$product['price']."',
- amount = '".$product['total_fee']."',
- score = 0,
- nums = '".$product['num']."',
- sendnum = 0,
- addon = '".$local_product['addon']."',
- is_type = 'goods'";
- $res = $this->db->exec($sql);
- if(!$res){
- $this->add_application_error('error_DB');
- }
- //?????sdb_orders???
- $tostr.= $local_product['name'].'('.$local_product['pdt_desc'].')('.$product['num'].')';
- $item_amount=bcadd($item_amount,$product['total_fee'],2);
- $itemnum+=$product['num'];
- }else{//?????
- $sql = 'INSERT INTO sdb_order_items SET
- order_id = "'.$order_id.'",
- product_id = 0,
- dly_status = "storage",
- tao_name = "'.$product['tao_name'].'",
- price = "'.$product['price'].'",
- amount = "'.$product['total_fee'].'",
- score = 0,
- nums = "'.$product['num'].'",
- sendnum = 0,
- is_type = "goods"';
- $res = $this->db->exec($sql);
- if(!$res){
- $this->add_application_error('error_DB');
- }
- //?????sdb_orders???
- $itemnum+=$product['num'];
- $item_amount=bcadd($item_amount,$product['total_fee'],2);
- }
- }
-
- //??sdb_orders?
- $sql = 'INSERT INTO sdb_orders SET
- order_id = "'.$order_id.'",
- member_id = "'.$row_member['member_id'].'",
- confirm = "N",
- status = "active",
- pay_status = 0,
- ship_status = 0,
- payment = "'.$payment.'",
- tostr = "'.$tostr.'",
- itemnum = "'.$itemnum.'",
- last_change_time = '.strtotime($data['modified']).',
- acttime = '.strtotime($data['created']).',
- createtime = '.strtotime($data['created']).',
- ship_name = "'.$data['name'].'",
- ship_area = "'.$data['ship_area'].'",
- ship_addr = "'.$data['address'].'",
- ship_zip = "'.$data['zip'].'",
- ship_tel = "'.$data['phone'].'",
- ship_time = "????",
- ship_mobile = "'.$data['mobile_phone'].'",
- cost_item = "'.$item_amount.'",
- cost_freight = "'.$data['post_fee'].'",
- currency = "CNY",
- final_amount = "'.bcadd($data['total_fee'],$data['post_fee'],2).'",
- total_amount = "'.bcadd($data['total_fee'],$data['post_fee'],2).'",
- discount = "'.bcsub($data['total_fee'],$item_amount,2).'",
- memo = "'.$data['memo'].'",
- dealer_order_id = "'.$data['tc_order_id'].'",
- taobao_id = "'.$data['id'].'",
- expired_time = '.(strtotime($data['created'])+86400*3).',
- order_source = "taofenxiao"';
-
- $res = $this->db->exec($sql);
- if(!$res){
- $this->add_application_error('error_DB');
- }
-
- //?????????sdb_order_to_so
- $sql = 'INSERT INTO sdb_order_to_so SET order_id = "'.$order_id.'", so_id = "'.$data['tc_order_id'].'",member_id = "'.$row_member['member_id'].'"';
- $this->db->exec($sql);
- //??????????
- if($type == 'PAY_WAIT_SEND'){//2.??????: ?? 1????????????? 2???
- $this->update_order_status($data['order_id'],'PAY_WAIT_SEND');
- //??????OME
- if($this->ome_node_id){
- $this->matrix->createOrder($order_id);
- }
- $this->create_payment($data,$row_member);
- }
- $this->addLog($order_id,'????','??');
- }
-
- //?????
- function create_payment($data,$row_member){
-
- //????????????? ????? ???
- $mdl_payment = $this->system->loadModel('trading/payment');
- $sql = 'SELECT * FROM sdb_payment_cfg WHERE custom_name LIKE "%???%"';
- $row_payment = $this->db->selectrow($sql);
- $payment = $row_payment?$row_payment['id']:0;
- $paymethod = $row_payment?$row_payment['custom_name']:'';
- $payment_id = $mdl_payment->gen_id();
-
-
- $sql = 'INSERT INTO sdb_payments SET
- payment_id = "'.$payment_id.'",
- order_id = "'.$data['order_id'].'",
- member_id = "'.$row_member['member_id'].'",
- bank = "alipay",
- currency = "CNY",
- money = "'.$data['distributor_payment'].'",
- paycost = "0.000",
- cur_money = "'.$data['distributor_payment'].'",
- pay_type = "online",
- payment = "'.$payment.'",
- paymethod = "'.$paymethod.'",
- t_begin = "'.strtotime($data['modified']).'",
- t_end = "'.strtotime($data['modified']).'",
- status = "succ",
- memo = "????????"';
-
- $res = $this->db->exec($sql);
- if(!$res){
- $this->add_application_error('error_DB');
- }
- //????OME
- //???????OME
- if($this->ome_node_id){
- //????????????????
- $apilog = $this->system->loadModel('trading/apilog');
- $toOme=$apilog->getStatus($data['order_id']);
- if(!$toOme){//??????
- $this->matrix->createOrder($data['order_id']);
- }
- $this->matrix->send_payment_to_matrix($payment_id);
- }
- $this->addLog($data['order_id'],'????'.$data['distributor_payment'],'??');
- }
-
-
- //????
- function match_product(&$product){
-
- //??tao_name
- $tao_name = '';
- $tao_spec = array();
- $name_tmp = array();
- $name_tmp = explode('?',$product['sku_properties']);
- foreach($name_tmp as $v){
- $spec = explode('?',$v);
- $tao_spec[] = $spec[1];
- }
- $tao_name = $product['title'].'('.implode('?',$tao_spec).')';
- $product['tao_name'] = $tao_name;
-
- //1.?????????
- $sql = 'SELECT p.*,g.type_id,t.setting
- FROM sdb_taobao_products as tp
- LEFT JOIN sdb_products as p ON tp.bn = p.bn
- LEFT JOIN sdb_goods as g ON p.goods_id = g.goods_id
- LEFT JOIN sdb_goods_type as t ON g.type_id = t.type_id
- WHERE tp.name_hash = "'.md5($product['tao_name']).'"';
- $row_product1 = $this->db->selectrow($sql);
- if($row_product1){
- $props = unserialize($row_product1['props']);
- $setting = unserialize($row_product1['setting']);
- $addon = array_merge($setting,$props);
- $row_product1['addon'] = serialize($addon);
-
- return $row_product1;
- }
- //2.?????SKU???
- $sql = 'SELECT p.*,g.type_id,t.setting
- FROM sdb_products as p
- LEFT JOIN sdb_goods as g ON p.goods_id = g.goods_id
- LEFT JOIN sdb_goods_type as t ON g.type_id = t.type_id
- WHERE p.bn = "'.$product['sku_id'].'" or p.bn = "'.$product['sku_outer_id'].'" or p.bn = "'.$product['item_outer_id'].'"';
-
- $row_product2 = $this->db->selectrow($sql);
-
- if($row_product2){
- $props = unserialize($row_product2['props']);
- $setting = unserialize($row_product2['setting']);
- $addon = array_merge($setting,$props);
- $row_product2['addon'] = serialize($addon);
-
- return $row_product2;
- }
- return false;
- }
-
- function create_refund($data,$row_member,$money){
- $mdl_refund = $this->system->loadModel('trading/refund');
- $refund_id = $mdl_refund->gen_id();
-
- //????????????? ????? ???
- $mdl_payment = $this->system->loadModel('trading/payment');
- $sql = 'SELECT * FROM sdb_payment_cfg WHERE custom_name LIKE "%???%"';
- $row_payment = $this->db->selectrow($sql);
- $payment = $row_payment?$row_payment['id']:0;
- $paymethod = $row_payment?$row_payment['custom_name']:'';
-
-
- $sql = 'INSERT INTO sdb_refunds SET
- refund_id = "'.$refund_id.'",
- order_id = "'.$data['order_id'].'",
- member_id = "'.$row_member['member_id'].'",
- bank = "alipay",
- currency = "'.$refund_id.'",
- money = "'.$money.'",
- pay_type = "online",
- payment = "'.$payment.'",
- paymethod = "'.$paymethod.'",
- memo = "????????",
- status = "sent",
- title = "title",
- t_ready = "'.strtotime($data['modified']).'",
- t_sent = "'.strtotime($data['modified']).'"';
- $res = $this->db->exec($sql);
- if(!$res){
- $this->add_application_error('error_DB');
- }
- $this->addLog($data['order_id'],'????'.$money,'??');
- }
-
- function update_order_status($order_id,$status){
- if($status == 'PAY_WAIT_SEND'){
- $sql = 'UPDATE sdb_orders SET pay_status = 1,payed = final_amount WHERE order_id = "'.$order_id.'"';
- }
- if($status == 'SEND_WAIT_CONFIRM'){
- $sql = 'UPDATE sdb_orders SET pay_status = 1,payed = final_amount,ship_status = 1 WHERE order_id = "'.$order_id.'"';
- }
- if($status == 'PART_REFUND'){
- $sql = 'UPDATE sdb_orders SET pay_status = 4,payed = final_amount WHERE order_id = "'.$order_id.'"';
- }
- if($status == 'REFUNDED'){
- $sql = 'UPDATE sdb_orders SET pay_status = 5,payed = final_amount WHERE order_id = "'.$order_id.'"';
- }
- if($status == 'REFUNDING'){
- $sql = 'UPDATE sdb_orders SET pay_status = 6,payed = final_amount WHERE order_id = "'.$order_id.'"';
- }
- if($status == 'FINISHED'){
- $sql = 'UPDATE sdb_orders SET status = "finish" WHERE order_id = "'.$order_id.'"';
- $this->addLog($order_id,'????','??');
- }
- if($status == 'CLOSED'){
- $sql = 'UPDATE sdb_orders SET status = "dead" WHERE order_id = "'.$order_id.'"';
- $this->addLog($order_id,'????','??');
- }
- $res = $this->db->exec($sql);
- if(!$res){
- $this->add_application_error('error_DB');
- }
- }
-
- function update_order_data($data,$row_member){
- //?????
- //$this->callbak_store($data['order_id']);
-
- $order_id = $data['order_id'];
- $ship_status = $data['ship_status']?$data['ship_status']:0;
- $pay_status = $data['pay_status']?$data['pay_status']:0;
- //?????????????????????????? ????? ????order_id?????????????????????????
- $sql = 'DELETE FROM sdb_orders WHERE order_id = "'.$order_id.'"';
- $res = $this->db->exec($sql);
- if(!$res){
- $this->add_application_error('error_DB');
- }
- $sql = 'DELETE FROM sdb_order_items WHERE order_id = "'.$order_id.'"';
- $res = $this->db->exec($sql);
- if(!$res){
- $this->add_application_error('error_DB');
- }
-
- $mdl_payment = $this->system->loadModel('trading/payment');
- $tostr = '';
- $itemnum = 0;
- $item_amount = 0;
-
- //????????????? ????? ???
- $sql = 'SELECT * FROM sdb_payment_cfg WHERE custom_name LIKE "%???%"';
- $row_payment = $this->db->selectrow($sql);
- $payment = $row_payment?$row_payment['id']:0;
- $paymethod = $row_payment?$row_payment['custom_name']:'';
-
- //???sdb_order_items
- foreach($data['items'] as $product){
- if($product['status'] == 'TRADE_CLOSED') continue;//?????????????
- //????
- $local_product = $this->match_product($product);
- //????
- if($local_product){
- //????
- //$this->freezStore($local_product['bn'],$product['num']);
-
- $sql = "INSERT INTO sdb_order_items SET
- order_id = '".$order_id."',
- product_id = '".$local_product['product_id']."',
- dly_status = 'storage',
- type_id = '".$local_product['type_id']."',
- bn = '".$local_product['bn']."',
- tao_name = '".$product['title'].";".$product['sku_properties']."',
- name = '".$local_product['name']."(".$local_product['pdt_desc'].")',
- price = '".$product['price']."',
- amount = '".$product['total_fee']."',
- score = 0,
- nums = '".$product['num']."',
- sendnum = 0,
- addon = '".$local_product['addon']."',
- is_type = 'goods'";
- $res = $this->db->exec($sql);
- if(!$res){
- $this->add_application_error('error_DB');
- }
- //?????sdb_orders???
- $tostr.= $local_product['name'].'('.$local_product['pdt_desc'].')('.$product['num'].')';
- $itemnum+=$product['num'];
- $item_amount=bcadd($item_amount,$product['total_fee'],2);
- }else{//?????
- $sql = 'INSERT INTO sdb_order_items SET
- order_id = "'.$order_id.'",
- product_id = 0,
- dly_status = "storage",
- tao_name = "'.$product['title'].';'.$product['sku_properties'].'",
- price = "'.$product['price'].'",
- amount = "'.$product['total_fee'].'",
- score = 0,
- nums = "'.$product['num'].'",
- sendnum = 0,
- is_type = "goods"';
- $res = $this->db->exec($sql);
- if(!$res){
- $this->add_application_error('error_DB');
- }
- //?????sdb_orders???
- $itemnum+=$product['num'];
- $item_amount=bcadd($item_amount,$product['total_fee'],2);
- }
- }
-
- //??sdb_orders?
-
- $sql = 'INSERT INTO sdb_orders SET
- order_id = "'.$order_id.'",
- member_id = "'.$row_member['member_id'].'",
- confirm = "N",
- status = "active",
- pay_status = '.$pay_status.',
- ship_status = '.$ship_status.',
- payment = "'.$payment.'",
- tostr = "'.$tostr.'",
- itemnum = "'.$itemnum.'",
- last_change_time = '.strtotime($data['modified']).',
- acttime = '.strtotime($data['created']).',
- createtime = '.strtotime($data['created']).',
- ship_name = "'.$data['name'].'",
- ship_area = "'.$data['ship_area'].'",
- ship_addr = "'.$data['address'].'",
- ship_zip = "'.$data['zip'].'",
- ship_tel = "'.$data['phone'].'",
- ship_time = "????",
- ship_mobile = "'.$data['mobile_phone'].'",
- cost_item = "'.$item_amount.'",
- cost_freight = "'.$data['post_fee'].'",
- currency = "CNY",
- final_amount = "'.bcadd($data['total_fee'],$data['post_fee'],2).'",
- total_amount = "'.bcadd($data['total_fee'],$data['post_fee'],2).'",
- discount = "'.bcsub($data['total_fee'],$item_amount,2).'",
- memo = "'.$data['memo'].'",
- dealer_order_id = "'.$data['tc_order_id'].'",
- taobao_id = "'.$data['id'].'",
- expired_time = '.(strtotime($data['created'])+86400*3).',
- order_source = "taofenxiao"';
- $res = $this->db->exec($sql);
- if(!$res){
- $this->add_application_error('error_DB');
- }
- $this->addLog($order_id,'????','??');
- }
-
- function get_area($state,$city,$district){
- if($state){
- $sql = 'SELECT * FROM sdb_regions WHERE local_name = "'.$state.'" AND p_region_id is NULL';
- $row_state = $this->db->selectrow($sql);
- }
- if($city){
- $sql = 'SELECT * FROM sdb_regions WHERE local_name = "'.$city.'" AND p_region_id = '.$row_state['region_id'];
- $row_city = $this->db->selectrow($sql);
- }
- if($district){
- $sql = 'SELECT * FROM sdb_regions WHERE local_name = "'.$district.'" AND p_region_id = '.$row_city['region_id'];
- $row_district = $this->db->selectrow($sql);
- }
-
- if($row_state && $row_city && $row_district){
- return 'mainland:'.$state.'/'.$city.'/'.$district.':'.$row_district['region_id'];
- }elseif($row_state && $row_city){
- return 'mainland:'.$state.'/'.$city.':'.$row_city['region_id'];
- }elseif($row_state){
- return 'mainland:'.$state.':'.$row_state['region_id'];
- }else{
- return '';
- }
- }
-
- function addLog($order_id, $message, $behavior = '', $result = 'success'){
- $sql = 'INSERT INTO sdb_order_log SET
- order_id = "'.$order_id.'",
- behavior = "'.$behavior.'",
- result = "'.$result.'",
- log_text = "'._addslashes($message).'",
- acttime = "'.time().'"';
- $this->db->exec($sql);
- }
-
- function freezStore($bn,$num){
- $sql = 'SELECT * FROM sdb_products WHERE bn = "'.$bn.'"';
- $row_product = $this->db->selectrow($sql);
- //????
- if($row_product['freez']){
- $sql = 'UPDATE sdb_products SET freez=freez+'.$num.' WHERE bn = "'.$row_product['bn'].'"';
- }else{
- $sql = 'UPDATE sdb_products SET freez='.$num.' WHERE bn = "'.$row_product['bn'].'"';
- }
-
- $this->db->exec($sql);
- }
-
- function callbak_store($order_id){
- $sql = 'SELECT * FROM sdb_order_items WHERE order_id = "'.$order_id.'"';
- $items = $this->db->select($sql);
-
- foreach($items as $k=>$v){
- if($v['bn']){
- $sql = 'UPDATE sdb_products SET freez = freez-'.$v['nums'].' WHERE bn = "'.$v['bn'].'"';
- $this->db->exec($sql);
- }
- }
- }
- }
-
- ?>