Files
liche/api/controllers/plan/Order.php
T
2023-06-13 10:08:54 +08:00

1460 lines
73 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Order extends CI_Controller{
private $log_dir = "licheb";
private $log_file = "order_v2.log";
public function __construct()
{
parent::__construct();
$this->load->model('receiver/order/receiver_orders_model');
$this->load->model('receiver/order/receiver_orders_v2_model');
$this->load->model('receiver/order/receiver_order_status_model');
$this->load->model('receiver/order/receiver_order_datas_model');
$this->load->model('receiver/order/receiver_order_contracts_model');
$this->load->model('app/liche/app_liche_orders_model');
$this->load->library('receiver/order_datas_entity');
$this->load->library('receiver/orders_v2_entity');
}
//更新旧订单数据状态
public function up_old_status(){
$size = $this->input->get('size');
!$size && $size = 20;
$t1 = 'lc_receiver_orders_v2';
$t2 = 'lc_receiver_orders';
$fields = "$t1.*";
$where = [
"$t1.id<" => 10000,
"$t1.status" => 0,
"$t2.status" => 6
];
$this->db->from("$t1");
$this->db->join("$t2", "$t2.id=$t1.id",'left');
$this->db->select($fields);
$this->db->where($where);
$this->db->order_by("$t1.id asc");
$this->db->limit($size);
$rows = $this->db->get()->result_array();
if($rows){
foreach($rows as $key=>$val){
$res = $this->receiver_orders_v2_model->update(['status'=>1],['id'=>$val['id']]);
$str = "更新订单状态:{$val['id']}";
debug_log($str,$this->log_file,$this->log_dir);
}
}else{
echo "no data";
}
}
//过期未支付订单
public function out_time(){
if(time()<=strtotime('2022-04-20 00:00:00')){ //2022-4-20后开始执行
exit;
}
$s_time = date('Y-m-d 00:00:00',strtotime("-1 day"));//昨天开始时间
$e_time = date('Y-m-d 23:59:59',strtotime("-1 day"));//昨天结束时间
$last_id_key = "out_time_receiver_order_id";
$redis = &load_cache();
$last_id = $redis->get($last_id_key);
$where = [
'c_time<=' => strtotime($e_time),
'c_time>=' => strtotime($s_time),
'status' => 0
];
$last_id && $where['id>'] = $last_id;
$rows = $this->receiver_orders_v2_model->select($where,'id asc',1,30);
if($rows){
foreach($rows as $key=>$val){
$if_pay = $this->app_liche_orders_model->count(['o_id'=>$val['id'],'status'=>1]);
if(!$if_pay){ //不存在已支付订单
$this->receiver_orders_v2_model->update(['status'=>-1],['id'=>$val['id']]);
$this->app_liche_orders_model->update(['status'=>-1],['o_id'=>$val['id']]);
debug_log("订单过期:".$val['id'],$this->log_file,$this->log_dir);
}
$do_last_id = $val['id'];
}
$redis->save($last_id_key,$do_last_id,2*24*60*60);
}else{
debug_log("订单过期执行完毕",$this->log_file,$this->log_dir);
}
}
//更新保险图片
public function up_ins_img(){
$redis = load_cache('redis');
$this->load->library('qiniuorc');
$page = $this->input->get('page');
$size = $this->input->get('size');
!$page && $page = 1;
!$size && $size = 10;
$c_key = "TEMP_ORDER_INSIMG_LAST_ID";
$where = [
"ins_img <> ''" => null,
"insurance_img" => null,
"business_img" => null,
"o_id>=" => 1000,
];
$last_id = $redis->get($c_key);
if($this->input->get('last_id')){
echo $last_id;exit;
}
if($last_id){
$where['id<'] = $last_id;
}
$count = $this->receiver_order_datas_model->count($where);
$rows = $this->receiver_order_datas_model->select($where,'id desc',$page,$size,'id,o_id,ins_img,insurance_img,business_img,jsondata');
if(!$rows){
echo "finish";
}
foreach ($rows as $key => $val) {
$ins_img = json_decode($val['ins_img'],true);
$jsondata = json_decode($val['jsondata'],true);
$ins_info = $jsondata['ins_info'] ? $jsondata['ins_info'] : [];
$update = $business_img = $insurance_img = [];
if(is_array($ins_info)){
$new_ins_img = [];
foreach ($ins_img as $k => $v) {
$img_url = build_qiniu_image_url($v);
$ins_key = md5($img_url);
$o_ins_info = $ins_info[$ins_key]['data'];
if(count($o_ins_info['投保险种'])>3 && !$business_img){ //商业险
$business_img['img'] = $v;
$business_img['date'] = $o_ins_info['投保确认时间'];
$business_img['price'] = floatval($o_ins_info['合计保费']);
$business_img['product'] = $o_ins_info['保险公司'];
$update['business_img'] = json_encode($business_img,JSON_UNESCAPED_UNICODE);
}elseif(count($o_ins_info['投保险种'])==3 && !$insurance_img){ //强制险
$insurance_img['img'] = $v;
$insurance_img['date'] = $o_ins_info['投保确认时间'];
$insurance_img['price'] = floatval($o_ins_info['合计保费']);
$insurance_img['product'] = $o_ins_info['保险公司'];
$update['insurance_img'] = json_encode($insurance_img,JSON_UNESCAPED_UNICODE);
}else{
$new_ins_img[] = $v;
}
}
$update['ins_img'] = json_encode($new_ins_img,JSON_UNESCAPED_UNICODE);
}
if($business_img && $insurance_img){
$this->receiver_order_datas_model->update($update,['id'=>$val['id']]);
}
$last_id = $val['id'];
$update_str = json_encode($update,JSON_UNESCAPED_UNICODE);
echo "自增id:{$last_id},订单id:{$val['o_id']},更新数据:{$update_str}<br>";
}
$redis->save($c_key,$last_id,24*60*60);
$left = $count-$size;
echo "剩余:{$left}";
}
/**
* 获取飞书 tenant_access_token
* @param $app_id
* @param $app_secret
* @return string
*/
private function get_tenant_access_token($app_id="cli_a2483b8493fc500d", $app_secret="iO6Egjw09xZ6SJVaNVOUxqClAW8YXnXD"){
# https://open.feishu.cn/document/ukTMukTMukTM/uMTNz4yM1MjLzUzM
# https://open.feishu.cn/document/ukTMukTMukTM/ukDNz4SO0MjL5QzM/auth-v3/auth/tenant_access_token_internal
if (!$app_id || !$app_secret){
return null;
}
$redis = &load_cache('redis');
$redisKey = 'feishu_app_id_'.$app_id;
$tenant_access_token = $redis->get($redisKey);
if ($tenant_access_token){
return $tenant_access_token;
}
$url = "https://open.feishu.cn/open-apis/auth/v3/tenant_access_token/internal";
$data = array("app_id"=>$app_id, "app_secret"=>$app_secret);
$this->load->library('mycurl');
$res = $this->mycurl->httpPost($url, $data, $type='is_json');
$result = json_decode($res, true);
if ($result && $result['code'] == 0 && $tenant_access_token = $result['tenant_access_token']){
$expire = $result['expire'] ? $result['expire'] - 100 : 3600;
$redis->save($redisKey, $tenant_access_token, $expire);
}
return $tenant_access_token;
}
/**
* 获取飞书上传图片的image_key
* @param $img_path
* @return string
*/
private function get_upload_image_key($img_path='temp/c01.jpg'){
$image_key = '';
$tenant_access_token = $this->get_tenant_access_token();
if (!$tenant_access_token){
return $image_key;
#echo "no tenant_access_token";exit();
}
$url = "https://open.feishu.cn/open-apis/im/v1/images";
$header = ["Authorization: Bearer ".$tenant_access_token, "Content-Type: multipart/form-data"];
$file_path = $img_path;
$arr = explode('/', $img_path);
$postname = array_slice($arr, -1)[0]; #var_dump($arr[count($arr) - 1]);
#$data = ['image_type="message"', "image=@'".$file_path."'"];
#$data = ["image_type='message'", "image=@'".realpath($file_path)."'"];
$data = ["image_type"=>"message", "image"=>curl_file_create(realpath($file_path), 'image/jpeg', $postname)];
$this->load->library('mycurl');
$this->mycurl->setHeaders($header);
$res = $this->mycurl->httpPost($url, $data, $type='is_file', $file_path);
$result = json_decode($res, true);
$result && $result['code'] == 0 && $result['data'] && $image_key = $result['data']['image_key'];
return $image_key;
}
/**
* Notes:向飞书发送小狸播报
* https://liche-api-dev.xiaoyu.com/plan/order/send_aggs_biz_ding_piao_rpt?sd=1&debug=1
* https://api.liche.cn/plan/order/send_aggs_biz_ding_piao_rpt
*/
public function send_aggs_biz_ding_piao_rpt(){
$params = $this->input->get();
$hour = date('H.i');
$test = $params['debug'] ? true : false;
if ( !($hour >= 23.00 && $hour <= 23.10 || $params['sd'])) {
return false;
}
$redis = &load_cache('redis');
$redisKey = 'send_aggs_biz_ding_piao_rpt';
if (intval($this->input->get('rds'))) {
$redis->delete($redisKey);
}
if ($redis->get($redisKey)){
return false;
};
#$redis->save($redisKey, 1, 60 * 10); # 暂不考虑“执行时间过长时重复执行”
$today_start = $today_end = $total_start = $total_end = $title_today = $title_ding_today = $title_ding = $title_piao_today = $title_piao = '';
$params['today_start'] && $today_start = $params['today_start'];
$params['today_end'] && $today_end = $params['today_end'];
$params['total_start'] && $total_start = $params['total_start'];
$params['total_end'] && $total_end = $params['total_end'];
$params['title_today'] && $title_today = $params['title_today'];
$params['title_ding_today'] && $title_ding_today = $params['title_ding_today'];
$params['title_ding'] && $title_ding = $params['title_ding'];
$params['title_piao_today'] && $title_piao_today = $params['title_piao_today'];
$params['title_piao'] && $title_piao = $params['title_piao'];
$this->load->library('receiver/orders_v2_entity');
$data = $this->orders_v2_entity->aggs_biz_ding_piao_rpt($today_start, $today_end, $total_start, $total_end,
$title_today, $title_ding_today, $title_ding, $title_piao_today, $title_piao, $create_table_image=true);
if (!$data || !$data['biz_aggs']){
echo 'no data';exit();
}
$img_path = $data['img_path'] ? $data['img_path'] : '';
$img_path_ding = $data['img_path_ding'] ? $data['img_path_ding'] : '';
$img_path_piao = $data['img_path_piao'] ? $data['img_path_piao'] : '';
if (!$img_path && !$img_path_ding && !$img_path_piao ){
echo '执行结果:没有生成图片'; exit();
}
$img_path_arr = array();
#if ($img_path) echo '<img src="/' . $img_path . '"/>';echo '<br>';
if ($img_path_ding){
echo '<img src="/' . $img_path_ding . '"/>';echo '<br>';
$img_path_arr[] = $img_path_ding;
}
if ($img_path_piao){
echo '<img src="/' . $img_path_piao . '"/>';echo '<br>';
$img_path_arr[] = $img_path_piao;
}
if ($params['notsend']){
return true;
}
# https://open.feishu.cn/document/ukTMukTMukTM/ucTM5YjL3ETO24yNxkjN
# 测试图片 img_ecffc3b9-8f14-400f-a014-05eca1a4310g {"StatusCode":0,"StatusMessage":"success"}
$res = array();
foreach ($img_path_arr as $img_path){
$image_key = $this->get_upload_image_key($img_path);
if (!$image_key){
echo "执行结果:$img_path 没有生成 image_key<br>";
continue;
}
else{
echo "执行结果:$img_path 生成 image_key$image_key<br>";
}
$this->load->library('qyrobot', array('test'=>$test));
$msg = [
"msg_type" => "image",
"content" => [
"image_key" => $image_key,
]
];
$res_send = $this->qyrobot->send_fb($msg);
$res[] = $res_send;
}
$redis->save($redisKey, 1, 60 * 10);
$res['img_path'] = $img_path;
$res['image_key'] = $image_key;
if ($params['sd']) {
echo "执行结果:<br>";
echo json_encode($res, JSON_UNESCAPED_UNICODE);
}
}
/**更新订单的二手车标志
*/
private function update_if_usedcar(){
$sql = 'update lc_receiver_orders_v2 set if_usedcar = 1 where if_usedcar = 0 and item_id in (select id from lc_items where vin in (
"LDP42A96XN9024652",
"LDP42A968N9024648",
"LDP42A967N9022809",
"LDP42A961N9024653",
"LDP42A965N9021917",
"LDP42A96XN9023405",
"LDP42A960N9018777",
"LDP42A963N9018840",
"LDP42A963N9030437",
"LDP42A966N9030299",
"LDP42A966N9030206",
"LDP42A969N9026604",
"LDP42A961N9029710",
"LDP42A960N9026443",
"LDP42A968N9025251",
"LDP42A962N9024676",
"LDP42A963N9022659",
"LDP42A964N9027059",
"LDP42A96XN9023484",
"LDP42A969N9027042",
"LDP42A96XN9025039",
"LDP42A960N9029696",
"LDP42A961N9018920",
"LDP42A966N9023417",
"LDP42A967N9029727",
"LDP42A960N9030055",
"LDP42A969N9030099",
"LDP42A968N9030420",
"LDP42A963N9030082",
"LDP42A963N9030390",
"LDP42A964N9020998",
"LDP42A967N9026830",
"LDP42A969N9026750",
"LDP42A967N9029663",
"LDP42A967N9018890",
"LDP42A968N9026836",
"LDP42A963N9030020",
"LDP42A96XN9030256",
"LDP42A960N9024868",
"LDP42A963N9029384",
"LDP42A967N9027850",
"LDP42A968N9030398",
"LDP42A96XN9030242",
"LDP42A967N9030229",
"LDP42A96XN9030239",
"LDP42A961N9030260",
"LDP42A961N9030047",
"LDP42A966N9030058",
"LDP42A96XN9030032",
"LDP42A965N9030102",
"LDP42A968N9030238",
"LDP42A964N9030253",
"LDP42A963N9029854",
"LDP42A963N9030051",
"LDP42A961N9030100",
"LDP42A962N9022670",
"LDP42A963N9030230",
"LDP42A967N9030215",
"LDP42A960N9030248",
"LDP42A963N9030079",
"LFZ63AL59ND051213",
"LFZ71AK58ND121148",
"LFZ71AK53ND121056",
"LFZ71AK50ND123072",
"LFZ71AK51ND123310",
"LFZ71AK55ND123312",
"LFZ71AK54ND123317",
"LFZ71AK56ND123318",
"LFZ71AK55ND123049",
"LFZ71AK55ND123116",
"LFZ71AK5XND123113",
"LFZ71AK57ND123117",
"LFZ71AK52ND123302",
"LFZ71AK51ND120729",
"LFZ71AK55ND120586",
"LFZ71AK50ND120589",
"LFZ71AK5XND102083",
"LFZ63AL52ND051263",
"LFZ63AL55ND051256",
"LFZ63AL59ND052846",
"LFZ63AL51ND052355",
"LFZ71AK58ND120775",
"LFZ63AL56ND050486",
"LFZ63AL52ND050484",
"LUZAGAAA5NA136091",
"LUZAGAAA6NA136018",
"LUZAGBGA8NA170829",
"LUZAGAGA0NA170446",
"LUZAGAGA9NA170476",
"LUZAGAGAXNA170440",
"LUZAGBGA9NA170869",
"LUZAGBGA7NA170885",
"LUZAGBGA6NA170845",
"LUZAGAGA1NA163358",
"LUZAGAGA2NA139845",
"LUZAGAGAXNA139852",
"LUZAGAGA2NA139831",
"LUZAGAGA9NA133279",
"LUZAGAGAXNA133260",
"LUZAGAGA3NA123699",
"LUZAGAGA0NA139830",
"LUZAGAGA4NA139846",
"LUZAGAGA0NA139875",
"LUZAGBGA6NA140681",
"LUZAGBGA6NA140678",
"LUZAGBGA9NA140724",
"LUZAGBGA3NA132473",
"LUZAGBGA8NA139581",
"LUZAGBGA2NA140774",
"LUZAGBGA9NA140738",
"LUZAGBGA8NA171253",
"LUZAGBGA6NA139403",
"LUZAGAAA1NA107154",
"LUZAGAAA9NA156327",
"LUZBGAFB6NA076697"
))';
$this->receiver_orders_v2_model->db->query($sql);
$res = $this->receiver_orders_v2_model->db->affected_rows();
echo "update_if_usedcar rows: {$res}<br><br>";
}
/**
* 添加成本数据
* @return void
* https://liche-api-dev.xiaoyu.com/plan/order/cost?debug=1&id=102997
*/
public function cost(){
$this->load->model('receiver/order/receiver_order_bills_model', 'order_bills_model');
$this->load->model('receiver/order/receiver_order_loans_model','order_loans_model');
$this->load->model('receiver/order/receiver_order_loans0_model','order_loans0_model');
$this->load->model('receiver/order/receiver_order_datas_model');
$this->load->model("biz/biz_model");
$this->load->model('biz/biz_settle_srv_model');
$this->load->model('auto/auto_business_model');
$this->load->model('items/items_cost_model');
//$this->load->model('sys/sys_finance_model');
$this->load->model('sys/sys_supplier_model');
# 更新订单的二手车标志
$this->update_if_usedcar();
$page = $this->input->get('page');
$size = $this->input->get('size');
$id = $this->input->get('id');
$debug = $this->input->get('debug');
$recost = $this->input->get('recost');
!$page && $page = 1;
!$size && $size = 5;
!$debug && $where = [
'(
status = 1
or ( status = 0 and id in (select o_id from `lc_receiver_order_status` where (pid_status = 4 and `status` in (1,2)) or (pid_status = 3 and `status` = 1) group by o_id having count(distinct pid_status) = 2 ))
or (`status` =0 and id in (select o_id from `lc_receiver_order_status` where `pid_status` = 3 and `status` = 1 ) and biz_id in (select id from lc_biz where type <> 1))
)' => null,
'id not in (select o_id from lc_items_cost)' => null,
'item_id>' => 0,
'bill_time>' => '0000-00-00 00:00:00',
'id>=' => Orders_v2_entity::V2_START_ID
];
$id && $where['id'] = $id;
if ($debug){
if (!$id){
exit('debug下,只支持单个id参数');
}
$where['(
status = 1
or ( status = 0 and id in (select o_id from `lc_receiver_order_status` where (pid_status = 4 and `status` in (1,2)) or (pid_status = 3 and `status` = 1) group by o_id having count(distinct pid_status) = 2 ))
or (`status` =0 and id in (select o_id from `lc_receiver_order_status` where `pid_status` = 3 and `status` = 1 ) and biz_id in (select id from lc_biz where type <> 1))
)'] = null;
$time_server = time();
$times_db = $this->receiver_orders_v2_model->db->query("select UNIX_TIMESTAMP() as ut")->result_array()[0]['ut'];
$time_diff = $time_server - intval($times_db);
echo "time check: <br>";
echo " - server time(): {$time_server}<br>";
echo " - db UNIX_TIMESTAMP(): {$times_db}<br>";
echo " - times diff(second): {$time_diff}<br>";
echo "<br><br>";
}
$rows = $this->receiver_orders_v2_model->select($where,'',$page,$size);
if ($recost){
$bill_time = $this->input->get('bill_time');
$where = array(
'id in (select o_id from lc_items_cost)' => null,
);
$bill_time && $where["bill_time >= '{$bill_time}'"] = null;
$id && $where['id'] = $id;
$rows = $this->receiver_orders_v2_model->select($where,'',0,0);
echo "recost: <br>";
echo $this->receiver_orders_v2_model->db->last_query()."<br><br>";
}
if ($debug){
echo "debug mode start <br><br>get order_v2: {$id}<br>";
echo $this->receiver_orders_v2_model->db->last_query()."<br>";
var_dump($rows);
echo "<br>";
}
if($rows){
$supplier2 = $this->sys_supplier_model->select(['status' => 1, 'type' => 2], '', 0, 0, 'id,title,short');
/*//测试 保险公司名 匹配 保险供应商
var_dump($supplier2);
$product = '太平财产保险有限公司'; var_dump($product); var_dump($this->cost_insure_product($supplier2, $product));
$product = '太平保险'; var_dump($product); var_dump($this->cost_insure_product($supplier2, $product));
$product = '太平'; var_dump($product); var_dump($this->cost_insure_product($supplier2, $product));
$product = '中国太平洋财产保险股份有限公司'; var_dump($product); var_dump($this->cost_insure_product($supplier2, $product));
$product = '太平洋'; var_dump($product); var_dump($this->cost_insure_product($supplier2, $product));
$product = '大西洋保险'; var_dump($product); var_dump($this->cost_insure_product($supplier2, $product));
exit();*/
foreach ($rows as $item) {
$if_usedcar = intval($item['if_usedcar']);
//`if_insure` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否保险(0否 1是)',
//`if_num` tinyint(1) NOT NULL DEFAULT '0' COMMENT '是否上牌(0否 1是)',
// if_insure 与 if_num,后台编辑会导致反转,参考接口改用 item['srv_ids'] 判断处理,后序再考虑修复现有数据
$srv_if_insure = $srv_if_num = $srv_if_finance = 0;
$srv_insure = $srv_fee_carno = $srv_loan = 0;
$srv_ids = json_decode($item['srv_ids'],true);
foreach ($srv_ids as $ks) {
$ks['id'] == 1 && $srv_if_insure = 1;
$ks['id'] == 1 && $srv_insure = floatval($ks['price']); # $money_json['price_insure']
$ks['id'] == 2 && $srv_if_num = 1;
$ks['id'] == 2 && $srv_fee_carno = floatval($ks['price']); # $money_json['fee_carno']
$ks['id'] == 4 && $srv_if_finance = 1;
$ks['id'] == 4 && $srv_loan = floatval($ks['price']); # $money_json['price_finance']
}
if ($debug){
echo "finance_id: {$item['finance_id']}"."<br>";
echo "finance_id0: {$item['finance_id0']}"."<br>";
echo "biz_id: {$item['biz_id']}"."<br>";
echo "status: {$item['status']}"."<br>";
echo "if_usedcar: {$if_usedcar}"."<br>";
echo "if_num: {$item['if_num']}"."<br>";
echo "payway: {$item['payway']}"."<br>";
var_dump($item['payway']);echo"<br>";
echo "if_insure: {$item['if_insure']}"."<br>";
echo "srv_ids: {$item['srv_ids']}"."<br>";
echo "<br>"."=====srv_ids====="."<br>";
echo "srv_if_insure: {$srv_if_insure}"."<br>";
echo "srv_if_num: {$srv_if_num}"."<br>";
echo "srv_if_finance: {$srv_if_finance}"."<br>";
echo "srv_insure: {$srv_insure}"."<br>";
echo "srv_fee_carno: {$srv_fee_carno}"."<br>";
echo "srv_loan: {$srv_loan}"."<br>";
echo "<br><br>";
}
$biz = $this->biz_model->get(['id'=>$item['biz_id']]);
if ($debug){
echo "get biz: <br>";
echo $this->biz_model->db->last_query()."<br>";
var_dump($biz);
echo "<br>";
echo "biz_type: {$biz['type']}"."<br>";
echo "<br><br>";
}
$cost = $srv_json_cost = $cost_json_cost = $promotion_json_cost = array();
if ($recost || $debug){
$cost = $this->items_cost_model->get(array('o_id'=>$item['id']));
$srv_json_cost = json_decode($cost['srv_json'], true);
$cost_json_cost = $cost['cost_json'] ? json_decode($cost['cost_json'], true) : array();
$promotion_json_cost = $cost['promotion_json'] ? json_decode($cost['promotion_json'], true) : array();
echo "get cost: <br>";
echo $this->items_cost_model->db->last_query()."<br>";
if($debug){
echo("<br><br>");
}
}
$money_json = json_decode($item['money_json'],true);
$info_json = json_decode($item['info_json'],true);
$add_data = [
'o_id' => $item['id'],
'price' => $this->orders_v2_entity->total_price($item['id'],$item['money_json']),
'c_time' => time()
];
$item['item_id'] && $add_data['item_id'] = $item['item_id'];
$bill_info = $this->order_bills_model->bill_info($item['id']);
$bill_info['销货单位名称'] && $add_data['bill_name'] = $bill_info['销货单位名称'];
$bill_info['开票日期'] && $add_data['bill_time'] = $bill_info['开票日期'];
$bill_info['价税合计(小写)'] && $add_data['bill_price'] = str_replace('¥','',$bill_info['价税合计(小写)']);
if ($if_usedcar){
$bill_info['二手车市场'] && $add_data['bill_name'] = $bill_info['二手车市场'];
$bill_info['车价合计(小写)'] && $add_data['bill_price'] = str_replace('¥','',$bill_info['车价合计(小写)']);
}
elseif ($bill_info['发票类型'] == '增值税专用发票'){
$bill_info['销售方名称'] && $add_data['bill_name'] = $bill_info['销售方名称'];
# 开票日期:2023年04月27日
if ($tmp_value = $bill_info['开票日期']){
$tmp_value = str_replace("", "-", $tmp_value);
$tmp_value = str_replace("", "-", $tmp_value);
$tmp_value = str_replace("", "", $tmp_value);
$add_data['bill_time'] = $tmp_value;
};
$bill_info['小写金额'] && $add_data['bill_price'] = str_replace('¥','',$bill_info['小写金额']);
}
$add_data['price'] = $add_data['bill_price'] ? $add_data['bill_price'] : 0; #23-04-04: 这个实际售价可以改成默认开票价,不能默认成指导价
if ($debug){
echo "get bill_info: <br>";
echo $this->order_bills_model->db->last_query()."<br>";
var_dump($bill_info);
echo "<br>";
echo "bill_time: {$add_data['bill_time']}"."<br>";
echo "bill_name: {$add_data['bill_name']}"."<br>";
echo "bill_price: {$add_data['bill_price']}"."<br>";
echo "<br><br>";
}
$srv_json = [];
$recost && $srv_json = $srv_json_cost; // recost要保留原来有可能修改过的srv
$srv_json['srv_if_insure'] = $srv_if_insure;
$srv_json['srv_if_num'] = $srv_if_num;
$srv_json['srv_if_finance'] = $srv_if_finance;
//精品
$srv_json['price_fine_select'] = $money_json['price_fine_select'] ? $money_json['price_fine_select'] : 0;
$fines = $item['fines'] ? json_decode($item['fines'], true) : array();
$fine_ids = array();
foreach ($fines as $fine){
if ($fine['id'] && $fine['id'] > 0){
$fine_ids[] = $fine['id'];
}
}
$cost_json = array();
$recost && $cost_json = $cost_json_cost;
$cost_select_price = 0; // 根据cost.fines 计算成本
$this->load->model('receiver/receiver_fine_model');
if ($fine_ids){
//$where = ['status' => 1];
$where['biz_id'] = $item['biz_id'];
$ids = implode(',',$fine_ids);
$where = ["id in ($ids)" => null];
$sum = $this->receiver_fine_model->sum('price', $where);
$cost_select_price = $sum['price'] ? $sum['price'] : 0;
}
if ($cost_select_price){
// $recost下,如$cost_json['select_price'] && $cost_select_price 要recost
if ($recost){
if (!$cost_json['select_price']){
$cost_json['select_price'] = $cost_select_price;
}
}
else{
$cost_json['select_price'] = $cost_select_price;
}
}
$cost_json && $add_data['cost_json'] = json_encode($cost_json,JSON_UNESCAPED_UNICODE);
if ($recost || $debug){
if ($fine_ids){
echo $this->receiver_fine_model->db->last_query()."<br>";
}
echo "get cost_json origin: {$cost_json}<br>";var_dump($cost_json);echo("<br>");
echo "get fines: {$fines}<br>";var_dump($fines);echo("<br>");
echo "get fine_ids: {$fine_ids}<br>";var_dump($fine_ids);echo("<br>");
echo "get cost_select_price: {$cost_select_price}<br>";
echo "get cost_json new: {$cost_json}<br>";var_dump($cost_json);
if($debug){
echo("<br><br>");
}
}
$this->load->model('items/items_model');
$item_row = $this->items_model->get(['id'=>$item['item_id']]);
if ($debug){
echo $this->items_model->db->last_query()."<br>";
echo "buy_price: {$item_row['buy_price']}"."<br>";
echo "<br><br>";
}
//获取创建订单时商务政策
$where = [
'id' => $info_json['business_id'],
];
$b_row_one = $this->auto_business_model->get($where);
$srv_json['ori_price'] = $b_row_one['price_car'] ? $b_row_one['price_car'] : 0;
$srv_json['price_floor'] = $b_row_one['price_floor'] ? $b_row_one['price_floor'] : 0;
if ($biz['type']==1){
$srv_json['profix_car'] = $b_row_one['profix_car'];
}
elseif ($biz['type']==3){
#$srv_json['profix_car'] = $b_row_one['proxy_profix_car'];
#票折的单车利润 = 代理店结算价 - 采购成本
#返佣的单车利润 = 最低限价 - 采购成本 - 佣金
$buy_price = $item_row['buy_price'];
$srv_json['profix_car'] = $b_row_one['proxy_type'] == 1 ? $b_row_one['proxy_profix_price'] - $buy_price : $b_row_one['proxy_profix_lower'] - $buy_price - $b_row_one['proxy_profix_rebate'];
}
else{
$srv_json['profix_car'] = 0;
}
if ($debug){
echo "get 商务政策: <br>";
echo $this->auto_business_model->db->last_query()."<br>";
var_dump($b_row_one);
echo "<br>";
echo "ori_price: {$srv_json['ori_price']}"."<br>";
echo "price_floor: {$srv_json['price_floor']}"."<br>";
echo "profix_car: {$srv_json['profix_car']}"."<br>";
echo "<br><br>";
}
/*
水平
品牌店按现有逻辑 (1)
合伙店取商务政策的固定值 (2)
代理店没有水平业务 (3)
*/
if ($biz['type']==2){
$add_data['insurance_price'] = $b_row_one['profix_insure'] ? $b_row_one['profix_insure'] : 0.00;
$add_data['fee_carno_price'] = $b_row_one['profix_carno'] ? $b_row_one['profix_carno'] : 0.00;
$add_data['loan_price'] = $b_row_one['profix_loan'] ? $b_row_one['profix_loan'] : 0.00;
if ($debug){
echo "biz_type==2 (biz_type==3时均为0): "."<br>";
echo "insurance_price: {$add_data['insurance_price']}"."<br>";
echo "fee_carno_price: {$add_data['fee_carno_price']}"."<br>";
echo "loan_price: {$add_data['loan_price']}"."<br>";
echo "<br><br>";
}
}
elseif ($biz['type']==3 || $biz['type'] != 1){
$add_data['insurance_price'] = 0.00;
$add_data['fee_carno_price'] = 0.00;
$add_data['loan_price'] = 0.00;
}
//保险
$srv_json['insurance_price'] = $srv_json['business_price'] = 0;
$srv_json['insurance_product'] = $srv_json['business_product'] = '';
$img_data = $this->receiver_order_datas_model->get(['o_id'=>$item['id'],'status>='=>0]);
$insurance_img = json_decode($img_data['insurance_img'],true);
$business_img = json_decode($img_data['business_img'],true);
$accident_img = json_decode($img_data['accident_img'],true);
# 0612各保单支持返点系数,优先级高于水平业务配置
$insurance_rebate = $insurance_img['rebate'] ? $insurance_img['rebate'] : 0;
$business_rebate = $business_img['rebate'] ? $business_img['rebate'] : 0;
$accident_rebate = $accident_img['rebate'] ? $accident_img['rebate'] : 0;
if ($biz['type']==1 && $srv_if_insure){
$srv_json['insurance_price'] = $insurance_img['price'] ? $insurance_img['price'] : 0;
$srv_json['insurance_product'] = $insurance_img['product'] ? $insurance_img['product'] : '';
$srv_json['business_price'] = $business_img['price'] ? $business_img['price'] : 0;
$srv_json['business_product'] = $business_img['product'] ? $business_img['product'] : '';
$srv_json['accident_price'] = $accident_img['price'] ? $accident_img['price'] : 0;
$srv_json['accident_title'] = $accident_img['product'] ? $accident_img['product'] : '';
if ($debug){
echo "get order_datas - 保险 img_data: <br>";
echo $this->receiver_order_datas_model->db->last_query()."<br>";
var_dump($img_data);
echo "<br>";
echo "insurance_price: {$srv_json['insurance_price']}"."<br>";
echo "insurance_product: {$srv_json['insurance_product']}"."<br>";
echo "insurance_rebate: {$insurance_rebate}"."<br>";
echo "business_price: {$srv_json['business_price']}"."<br>";
echo "business_product: {$srv_json['business_product']}"."<br>";
echo "business_rebate: {$business_rebate}"."<br>";
echo "accident_price: {$srv_json['accident_price']}"."<br>";
echo "accident_title: {$srv_json['accident_title']}"."<br>";
echo "accident_rebate: {$accident_rebate}"."<br>";
echo "<br><br>";
}
}
# 23-04-06: 保险佣金计算做个特殊判断 - 漳州品牌店的佣金,需要再除以 1.06
$insurance_xishu = $biz['type']==1 && $biz['city_id'] == 350600 ? 1.06 : 1.0;
//强险
$srv_json['insurance_ins_price'] = $srv_json['insurance_fd'] = 0;
if($biz['type']==1 && $srv_if_insure && $insurance_img && $insurance_img['product']){
if (!$insurance_rebate){
$time = strtotime($insurance_img['date']);
$insure_product = $this->cost_insure_product($supplier2, $insurance_img['product']);
$where = [
'biz_id' => $item['biz_id'],
'title' => $insure_product,
'type' => 2,
'status' => 1,
"json_extract(jsondata, '$.type') = '2'" => null,
"json_extract(jsondata, '$.s_time') <= '{$time}'" => null,
"json_extract(jsondata, '$.e_time') >= '{$time}'" => null
];
$fd_row = $this->biz_settle_srv_model->get($where);
if ($debug){
echo "get 强险配置 : <br>";
if ($insure_product != $insurance_img['product']){
echo "{$insurance_img['product']} -> {$insure_product}"."<br>";
}
echo $this->biz_settle_srv_model->db->last_query()."<br>";
var_dump($fd_row);
echo "<br><br>";
}
if(!$fd_row){
$where = [
'biz_id' => $item['biz_id'],
'title' => $insure_product,
'type' => 2,
'status' => 1,
'is_def' => 1,
"json_extract(jsondata, '$.type') = '2'" => null,
];
$fd_row = $this->biz_settle_srv_model->get($where);
if ($debug){
echo "get 强险配置(默认) : <br>";
echo $this->biz_settle_srv_model->db->last_query()."<br>";
var_dump($fd_row);
echo "<br><br>";
}
}
$fd_jsondata = json_decode($fd_row['jsondata'],true);
$insurance_rebate = $fd_jsondata['rebate'] ? $fd_jsondata['rebate'] : 0;
}
$srv_json['insurance_fd'] = $insurance_rebate;
$qx_fd = $insurance_rebate/100/$insurance_xishu;
$srv_json['insurance_ins_price'] = round(floatval($insurance_img['price']) * $qx_fd, 2);
if ($debug){
echo "get 强险返点: <br>";
echo "insurance_fd: {$srv_json['insurance_fd']}"."<br>";
echo "insurance_xishu: {$insurance_xishu}"."<br>";
echo "insurance_fd 2: {$qx_fd}"."<br>";
echo "insurance_ins_price: {$srv_json['insurance_ins_price']}"."<br>";
echo "<br><br>";
}
}
//商业险
$srv_json['business_ins_price'] = $srv_json['business_fd'] = 0;
if($biz['type']==1 && $srv_if_insure && $business_img && $business_img['product']){
if (!$business_rebate){
$time = strtotime($business_img['date']);
$insure_product = $this->cost_insure_product($supplier2, $business_img['product']);
$where = [
'biz_id' => $item['biz_id'],
'title' => $insure_product,
'status' => 1,
'type' => 2,
"json_extract(jsondata, '$.type') = '1'" => null,
"json_extract(jsondata, '$.s_time') <= '{$time}'" => null,
"json_extract(jsondata, '$.e_time') >= '{$time}'" => null
];
$bis_fd_row = $this->biz_settle_srv_model->get($where);
if ($debug){
echo "get 商业险配置 : <br>";
if ($insure_product != $business_img['product']){
echo "{$business_img['product']} -> {$insure_product}"."<br>";
}
echo $this->biz_settle_srv_model->db->last_query()."<br>";
var_dump($bis_fd_row);
echo "<br><br>";
}
if(!$bis_fd_row){
$where = [
'biz_id' => $item['biz_id'],
'title' => $insure_product,
'status' => 1,
'type' => 2,
'is_def' => 1,
"json_extract(jsondata, '$.type') = '1'" => null,
];
$bis_fd_row = $this->biz_settle_srv_model->get($where);
if ($debug){
echo "get 商业险配置(默认) : <br>";
echo $this->biz_settle_srv_model->db->last_query()."<br>";
var_dump($bis_fd_row);
echo "<br><br>";
}
}
$bis_fd_jsondata = json_decode($bis_fd_row['jsondata'],true);
$business_rebate = $bis_fd_jsondata['rebate'] ? $bis_fd_jsondata['rebate'] : 0;
}
$srv_json['business_fd'] = $business_rebate;
$bis_fd = $business_rebate/100/$insurance_xishu;
$srv_json['business_ins_price'] = round(floatval($business_img['price'])*$bis_fd, 2);
if ($debug){
echo "get 商业险返点: <br>";
echo "business_fd: {$srv_json['business_fd']}"."<br>";
echo "insurance_xishu: {$insurance_xishu}"."<br>";
echo "business_fd 2: {$bis_fd}"."<br>";
echo "business_ins_price: {$srv_json['business_ins_price']}"."<br>";
echo "<br><br>";
}
}
//意外险
$srv_json['accident_ins_price'] = $srv_json['accident_fd'] = 0;
if($biz['type']==1 && $srv_if_insure && $accident_img && $accident_img['product']){
if (!$accident_rebate){
$time = strtotime($accident_img['date']);
$insure_product = $this->cost_insure_product($supplier2, $accident_img['product']);
$where = [
'biz_id' => $item['biz_id'],
'title' => $insure_product,
'status' => 1,
'type' => 2,
"json_extract(jsondata, '$.type') = '3'" => null,
"json_extract(jsondata, '$.s_time') <= '{$time}'" => null,
"json_extract(jsondata, '$.e_time') >= '{$time}'" => null
];
$bis_fd_row = $this->biz_settle_srv_model->get($where);
if ($debug){
echo "get 意外险配置 : <br>";
if ($insure_product != $accident_img['product']){
echo "{$accident_img['product']} -> {$insure_product}"."<br>";
}
echo $this->biz_settle_srv_model->db->last_query()."<br>";
var_dump($bis_fd_row);
echo "<br><br>";
}
if(!$bis_fd_row){
$where = [
'biz_id' => $item['biz_id'],
'title' => $insure_product,
'status' => 1,
'type' => 2,
'is_def' => 1,
"json_extract(jsondata, '$.type') = '3'" => null,
];
$bis_fd_row = $this->biz_settle_srv_model->get($where);
if ($debug){
echo "get 意外险配置(默认) : <br>";
echo $this->biz_settle_srv_model->db->last_query()."<br>";
var_dump($bis_fd_row);
echo "<br><br>";
}
}
$bis_fd_jsondata = json_decode($bis_fd_row['jsondata'],true);
$accident_rebate = $bis_fd_jsondata['rebate'] ? $bis_fd_jsondata['rebate'] : 0;
}
$srv_json['accident_fd'] = $accident_rebate;
$bis_fd = $accident_rebate/100/$insurance_xishu;
$srv_json['accident_ins_price'] = round(floatval($accident_img['price'])*$bis_fd, 2);
if ($debug){
echo "get 意外险返点: <br>";
echo "accident_fd: {$srv_json['accident_fd']}"."<br>";
echo "insurance_xishu: {$insurance_xishu}"."<br>";
echo "accident_fd 2: {$bis_fd}"."<br>";
echo "accident_ins_price: {$srv_json['accident_ins_price']}"."<br>";
echo "<br><br>";
}
}
//挂牌
$srv_json['fee_carno'] = $money_json['fee_carno'] ? $money_json['fee_carno'] : 0;
$srv_json['cb_fee_carno'] = 0;
if($biz['type']==1 && $srv_if_num){
# 23-04-03: 350600优先取默认,不再区分二手车
$free_row = array();
if(!$free_row && $biz['city_id']!=350600){
$where = [
'biz_id' => $item['biz_id'],
'type' => 1,
'status' => 1,
's_effect_time<=' => date('Y-m-d',strtotime($item['bill_time'])),
];
#$biz['city_id']==350600 && $if_usedcar && $where['title like "%二手车%"'] = null;
#$biz['city_id']==350600 && !$if_usedcar && $where['title not like "%二手车%"'] = null;
$free_row = $this->biz_settle_srv_model->get($where);
if ($debug){
echo "get 挂牌配置 : <br>";
echo $this->biz_settle_srv_model->db->last_query()."<br>";
var_dump($free_row);
echo "<br><br>";
}
}
if(!$free_row){
$where = ['biz_id'=>$item['biz_id'],'is_def'=>1,'status'=>1,'type'=>1];
#$biz['city_id']==350600 && $if_usedcar && $where['title like "%二手车%"'] = null;
#$biz['city_id']==350600 && !$if_usedcar && $where['title not like "%二手车%"'] = null;
$free_row = $this->biz_settle_srv_model->get($where);
if ($debug){
echo "get 挂牌配置(默认) : <br>";
echo $this->biz_settle_srv_model->db->last_query()."<br>";
var_dump($free_row);
echo "<br><br>";
}
}
$free_jsondata = json_decode($free_row['jsondata'],true);
$srv_json['cb_fee_carno'] = $free_jsondata['price'] ? $free_jsondata['price'] : 0;
if ($debug){
echo "挂牌费用 fee_carno: {$srv_json['fee_carno']}"."<br>";
echo "挂牌成本 cb_fee_carno: {$srv_json['cb_fee_carno']}"."<br>";
$fee_carno_price = $srv_json['fee_carno'] - $srv_json['cb_fee_carno'];
echo "挂牌收入合计 fee_carno_price: {$fee_carno_price}"."<br>";
echo "<br><br>";
}
}
//按揭信息
$srv_json['loan_product'] = '';
$srv_json['loan_srv_price'] = $srv_json['loan_num'] = $srv_json['loan_price'] = 0;
# 23-04-03: 按揭, 350600 代理店按所在城市的品牌店处理; 23-04-04: 加多一个城市,宁德
$biz_type_force = $biz_id_force = 0;
$city_ids_force = $this->receiver_orders_v2_model->get_city_ids_force(); # 要同步修改 Items_cost_model.php 中相关代码, Cost.php 相关代码
if ($biz['type']==3 and in_array($biz['city_id'], $city_ids_force)){
$biz_force = $this->biz_model->get(['city_id'=>$biz['city_id'], 'type'=>1, 'status'=>1]);
$biz_force && $biz_type_force = 1;
$biz_force && $biz_id_force = $biz_force['id'];
if ($debug){
echo "{$biz['city_id']} 代理店按所在城市的品牌店处理: <br>";
echo $this->biz_model->db->last_query()."<br>";
var_dump($biz_force);
echo "biz_type_force: {$biz_type_force}"."<br>";
echo "<br>";
}
}
# 23-04-04: 不用管有没有; 可以换后期补; 先确认有没有该项业务,其次再确认有没有值,要分开判断跟取数,不能混在一起
#if(($biz['type']==1 && $srv_if_finance || $biz_type_force) && $money_json['price_loan']>0){
if(($biz['type']==1 && $srv_if_finance || $biz_type_force) && $item['payway']==0){
$biz_type_force && $srv_loan = 0;
$biz_type_force && $srv_json['srv_if_finance'] = 1;
$srv_json['loan_price'] = $money_json['price_loan'] ? $money_json['price_loan'] : 0;
$loan_row = $this->order_loans_model->get(['o_id'=>$item['id']]);
if ($debug){
echo "get 按揭信息: <br>";
echo $this->order_loans_model->db->last_query()."<br>";
var_dump($loan_row);
echo "<br>";
}
$finance_id = $item['finance_id'];
$srv_json['loan_product'] = '';
$srv_json['loan_is_tiexi'] = $loan_row['is_tiexi'];
if ($finance_id){
//$finance_row = $this->sys_finance_model->get(array('id'=>$finance_id),'title');
$finance_row = $this->sys_supplier_model->get(array('id'=>$finance_id),'title');
$srv_json['loan_product'] = $finance_row ? $finance_row['title'] : '';
}
$srv_json['loan_num'] = $finance_num = $loan_row['num'] ? $loan_row['num'] : 36;
$srv_json['loan_num'] = $srv_json['loan_product'] ? $srv_json['loan_num'] : 0;
if ($debug){
if ($finance_id){
echo $this->sys_supplier_model->db->last_query()."<br>";
var_dump($finance_row);
echo "<br>";
}
echo "loan_price (money_json['price_loan']): {$srv_json['loan_price']}"."<br>";
echo "loan_product: {$srv_json['loan_product']}"."<br>";
echo "loan_num: {$srv_json['loan_num']}"."<br>";
echo "is_tiexi: {$loan_row['is_tiexi']}"."<br>";
echo "<br><br>";
}
$profix_loan = $loan_fd = 0;
if (!$loan_row['is_tiexi']){
$biz_id_param = $biz_id_force ? $biz_id_force : $item['biz_id'];
$where = [
'biz_id' => $biz_id_param,
'type' => 3,
'status' => 1,
's_effect_time<=' => date('Y-m-d',strtotime($item['bill_time'])),
"JSON_UNQUOTE(json_extract(jsondata, '$.supplier_id')) = '{$finance_id}'" => null,
"JSON_UNQUOTE(json_extract(jsondata, '$.finance_num')) = '{$finance_num}'" => null,
"JSON_UNQUOTE(json_extract(jsondata, '$.rebate')) > 0" => null,
];
$fd_row = $this->biz_settle_srv_model->get($where);
if ($debug){
echo "get 按揭配置 : <br>";
echo $this->biz_settle_srv_model->db->last_query()."<br>";
var_dump($fd_row);
echo "<br><br>";
}
if(!$fd_row){
$fd_row = $this->biz_settle_srv_model->get(['biz_id'=>$biz_id_param,'is_def'=>1,'status'=>1,'type'=>3,
"JSON_UNQUOTE(json_extract(jsondata, '$.supplier_id')) = '{$finance_id}'" => null,
"JSON_UNQUOTE(json_extract(jsondata, '$.finance_num')) = '{$finance_num}'" => null,
"JSON_UNQUOTE(json_extract(jsondata, '$.rebate')) > 0" => null,
]);
if ($debug){
echo "get 按揭配置(默认) : <br>";
echo $this->biz_settle_srv_model->db->last_query()."<br>";
var_dump($fd_row);
echo "<br><br>";
}
}
$fd_jsondata = json_decode($fd_row['jsondata'],true);
$loan_fd = $fd_jsondata['rebate'] && $money_json['price_loan'] ? $fd_jsondata['rebate']:0;
$profix_loan = round($money_json['price_loan']*$loan_fd/100, 2);
}
$srv_json['loan_srv_price'] = $profix_loan + $srv_loan;
# 23-04-04: 手续费 跟 服务费 分开
if (date('Y-m-d',strtotime($item['bill_time'])) >= date('Y-m-d',strtotime('2023-04-01'))){
$srv_json['loan_srv_price'] = $profix_loan;
$srv_json['loan_fd'] = $loan_fd;
$srv_json['loan_srv'] = $srv_loan;
if ($debug){
echo "是否大于2023-04-01: yes{$item['bill_time']}"."<br>";
}
}
if ($debug){
echo "是否贴息: is_tiexi: {$loan_row['is_tiexi']}"."<br>";
echo "按揭返点: loan_fd: {$loan_fd}"."<br>";
echo "按揭返点金额: profix_loan: {$profix_loan}"."<br>";
echo "按揭手续费: srv_loan: {$srv_loan}"."<br>";
echo "按揭综合服务费: loan_srv_price: {$srv_json['loan_srv_price']}"."<br>";
echo "<br><br>";
}
// 首付按揭
if ($money_json['price_loan0']){
$srv_json['loan_price0'] = $money_json['price_loan0'] ? $money_json['price_loan0'] : 0;
$loan_row = $this->order_loans0_model->get(['o_id'=>$item['id']]);
if ($debug){
echo "get 首付按揭-按揭信息: <br>";
echo $this->order_loans0_model->db->last_query()."<br>";
var_dump($loan_row);
echo "<br>";
}
$finance_id = $item['finance_id0'];
$srv_json['loan_product0'] = '';
$srv_json['loan_is_tiexi0'] = $loan_row['is_tiexi'];
if ($finance_id){
$finance_row = $this->sys_supplier_model->get(array('id'=>$finance_id),'title');
$srv_json['loan_product0'] = $finance_row ? $finance_row['title'] : '';
}
$srv_json['loan_num0'] = $finance_num = $loan_row['num'] ? $loan_row['num'] : 36;
$srv_json['loan_num0'] = $srv_json['loan_product0'] ? $srv_json['loan_num0'] : 0;
if ($debug){
if ($finance_id){
echo $this->sys_supplier_model->db->last_query()."<br>";
var_dump($finance_row);
echo "<br>";
}
echo "loan_price0 (money_json['price_loan0']): {$srv_json['loan_price0']}"."<br>";
echo "loan_product0: {$srv_json['loan_product0']}"."<br>";
echo "loan_num0: {$srv_json['loan_num0']}"."<br>";
echo "is_tiexi0: {$loan_row['is_tiexi0']}"."<br>";
echo "<br><br>";
}
$profix_loan = $loan_fd = 0;
if (!$loan_row['is_tiexi']){
$biz_id_param = $biz_id_force ? $biz_id_force : $item['biz_id'];
$where = [
'biz_id' => $biz_id_param,
'type' => 3,
'status' => 1,
's_effect_time<=' => date('Y-m-d',strtotime($item['bill_time'])),
"JSON_UNQUOTE(json_extract(jsondata, '$.supplier_id')) = '{$finance_id}'" => null,
"JSON_UNQUOTE(json_extract(jsondata, '$.finance_num')) = '{$finance_num}'" => null,
"JSON_UNQUOTE(json_extract(jsondata, '$.rebate')) > 0" => null,
];
$fd_row = $this->biz_settle_srv_model->get($where);
if ($debug){
echo "get 首付按揭-按揭配置 : <br>";
echo $this->biz_settle_srv_model->db->last_query()."<br>";
var_dump($fd_row);
echo "<br><br>";
}
if(!$fd_row){
$fd_row = $this->biz_settle_srv_model->get(['biz_id'=>$biz_id_param,'is_def'=>1,'status'=>1,'type'=>3,
"JSON_UNQUOTE(json_extract(jsondata, '$.supplier_id')) = '{$finance_id}'" => null,
"JSON_UNQUOTE(json_extract(jsondata, '$.finance_num')) = '{$finance_num}'" => null,
"JSON_UNQUOTE(json_extract(jsondata, '$.rebate')) > 0" => null,
]);
if ($debug){
echo "get 首付按揭-按揭配置(默认) : <br>";
echo $this->biz_settle_srv_model->db->last_query()."<br>";
var_dump($fd_row);
echo "<br><br>";
}
}
$fd_jsondata = json_decode($fd_row['jsondata'],true);
$loan_fd = $fd_jsondata['rebate'] && $money_json['price_loan0'] ? $fd_jsondata['rebate']:0;
$profix_loan = round($money_json['price_loan0']*$loan_fd/100, 2);
}
$srv_loan = 0; // 暂不支持“首付按揭”手续费
$srv_json['loan_srv_price0'] = $profix_loan + $srv_loan;
# 23-04-04: 手续费 跟 服务费 分开
if (date('Y-m-d',strtotime($item['bill_time'])) >= date('Y-m-d',strtotime('2023-04-01'))){
$srv_json['loan_srv_price0'] = $profix_loan;
$srv_json['loan_fd0'] = $loan_fd;
$srv_json['loan_srv0'] = $srv_loan;
if ($debug){
echo "是否大于2023-04-01: yes{$item['bill_time']}"."<br>";
}
}
if ($debug){
echo "是否贴息: is_tiexi0: {$loan_row['is_tiexi']}"."<br>";
echo "按揭返点: loan_fd0: {$loan_fd}"."<br>";
echo "按揭返点金额: profix_loan0: {$profix_loan}"."<br>";
echo "按揭手续费: srv_loan0: {$srv_loan}"."<br>";
echo "按揭综合服务费: loan_srv_price0: {$srv_json['loan_srv_price0']}"."<br>";
echo "<br><br>";
}
}
}
$sum = $this->app_liche_orders_model->sum('total_price', ['o_id'=>$item['id'],'status'=>1,'pay_price <>'=>0]);
$pay_price = $sum['total_price'] ? $sum['total_price'] : 0;
$need_price = $this->orders_v2_entity->recevable_price($item['id'],$srv_price=true,$price_loan=false);
$srv_json['if_pay'] = $pay_price>=$need_price ? 1 : 0;
if ($recost){
# 已标记为齐款的,不能设置为0
$srv_json_cost['if_pay'] && $srv_json['if_pay'] = 1;
}
# 预估返利 写入到 厂家补贴
if ($item_row['estimated_rebate']){
$promotion_json = $recost ? $promotion_json_cost : array();
$promotion_json['factory_price'] = $item_row['estimated_rebate'];
$add_data['promotion_json'] = json_encode($promotion_json,JSON_UNESCAPED_UNICODE);
if ($debug){
echo "estimated_rebate: {$item_row['estimated_rebate']}"."<br>";
echo "promotion_json_cost: ";var_dump($promotion_json_cost);echo "<br>";
echo "promotion_json: ";var_dump($promotion_json);echo "<br>";
echo "<br><br>";
}
}
$add_data['srv_json'] = json_encode($srv_json,JSON_UNESCAPED_UNICODE);
if ($debug){
echo "------------------------------"."<br>";
echo "pay_price: {$pay_price}"."<br>";
echo "need_price: {$need_price}"."<br>";
echo "if_pay(pay_price>=need_price ? 1 : 0): {$srv_json['if_pay']}"."<br>";
echo "<br><br>";
echo "get add_data : <br>";
var_dump($add_data);
if ($cost){
echo "<br><br>get cost data:<br>";
var_dump($cost);
echo "<br><br>";
echo "c_time: ".date("Y-m-d H:i:s",$cost['c_time'])."<br><br>";
echo "srv_json:<br>";
var_dump($srv_json_cost);
}
echo "<br><br>debug mode end<br><br>";
$fill_info = $this->orders_v2_entity->pdf_data($item);
echo "<b>fill_info(pdf_data)</b>:<br>";
var_dump($fill_info);
exit();
}
if ($recost){
unset($add_data['c_time']);
$id = $this->items_cost_model->update($add_data, array('o_id' => $item['id']));
$id && $id = $cost['id']; # 如有更新,强制为cost.id
}
else{
$id = $this->items_cost_model->add($add_data);
}
if(is_numeric($id)){
$this->items_cost_model->update_total($id);
if ($recost){
echo "更新成功订单id:{$item['id']}<br><br>";
}
else{
echo "添加成功订单id:{$item['id']}<br><br>";
}
}else{
if ($recost){
echo "忽略更新订单id:{$item['id']}<br><br>";
}
else{
echo "添加失败订单id:{$item['id']}<br><br>";
}
}
}
}else{
echo "数据不存在";
}
}
/**
* 修复成本数据的bill_name
* @return void
*/
public function cost_repair_bill_name(){
$this->load->model('receiver/order/receiver_order_bills_model', 'order_bills_model');
$this->load->model('items/items_cost_model');
$sql = "select *, (select if_usedcar from lc_receiver_orders_v2 where id = lc_items_cost.o_id) as if_usedcar from lc_items_cost";
$rows = $this->items_cost_model->db->query($sql)->result_array();
if (!$rows){
echo "数据不存在";exit();
}
foreach ($rows as $row){
$id = $row['id'];
$if_usedcar = intval($row['if_usedcar']);
$bill_info = $this->order_bills_model->bill_info($row['o_id']);
$bill_name = '';
$bill_info['销货单位名称'] && $bill_name = $bill_info['销货单位名称'];
$if_usedcar && $bill_info['二手车市场'] && $bill_name = $bill_info['二手车市场'];
if ($row['bill_name'] != $bill_name){
$upd_data['bill_name'] = $bill_name;
$res = $this->items_cost_model->update($upd_data, array('id'=>$id));
echo "{$id}, bill_name_old: {$row['bill_name']}, bill_name_new: {$bill_name}, update: {$res}<br>";
}
else{
echo "{$id}, bill_name_old: {$row['bill_name']}, bill_name_new: {$bill_name}, update: ignore<br>";
}
}
echo "repair ".count($rows)." rows ok!";
}
/**
* 修复成本数据的非品牌店的水平业务
* @return void
*/
private function cost_repair_spyw(){
$this->load->model("biz/biz_model");
$this->load->model('auto/auto_business_model');
$this->load->model('items/items_cost_model');
$where = [
'(status = 1 or ( status = 0 and id in (select o_id from `lc_receiver_order_status` where (pid_status = 4 and `status` in (1,2)) or (pid_status = 3 and `status` = 1) group by o_id having count(distinct pid_status) = 2 ) ))' => null,
'id in (select o_id from lc_items_cost)' => null,
'item_id>' => 0,
'id>=' => Orders_v2_entity::V2_START_ID,
'biz_id in (select id from lc_biz where type <> 1)' => null,
];
$rows = $this->receiver_orders_v2_model->select($where,'',0,0);
if (!$rows){
echo "数据不存在";exit();
}
foreach ($rows as $item) {
$o_id = $item['id'];
$cost = $this->items_cost_model->get(['o_id'=>$o_id]);
if (!$cost){
echo "{$o_id}, cost not found<br>";
}
$biz = $this->biz_model->get(['id'=>$item['biz_id']]);
if (!$biz){
echo "{$o_id}, biz not found, biz_id: {$item['biz_id']}<br>";
}
$info_json = json_decode($item['info_json'],true);
//获取创建订单时商务政策
$b_row_one = $this->auto_business_model->get(['id' => $info_json['business_id']]);
if (!$b_row_one){
echo "{$o_id}, business not found, business_id: {$item['business_id']}<br>";
}
/*
水平
品牌店按现有逻辑 (1)
合伙店取商务政策的固定值 (2)
代理店没有水平业务 (3)
*/
$add_data = [];
if ($biz['type']==2){
$add_data['insurance_price'] = $b_row_one['profix_insure'] ? $b_row_one['profix_insure'] : 0.00;
$add_data['fee_carno_price'] = $b_row_one['profix_carno'] ? $b_row_one['profix_carno'] : 0.00;
$add_data['loan_price'] = $b_row_one['profix_loan'] ? $b_row_one['profix_loan'] : 0.00;
}
elseif ($biz['type']==3 || $biz['type'] != 1){
$add_data['insurance_price'] = 0.00;
$add_data['fee_carno_price'] = 0.00;
$add_data['loan_price'] = 0.00;
}
echo "{$o_id}, biz_type: {$biz['type']}<br>";
$id = $cost['id'];
if ($add_data){
$res = $this->items_cost_model->update($add_data, array('id'=>$id));
$sql = $this->items_cost_model->db->last_query();
$res = is_numeric($res) ? is_numeric($res) : 0;
echo "{$o_id}, cost {$id} update: {$res}, ({$sql})<br>";
}
else{
echo "{$o_id}, cost {$id} update: ignore<br>";
}
$res = $this->items_cost_model->update_total($id);
echo "{$o_id}, cost {$id} update_total: {$res}<br><br>";
}
echo "repair ".count($rows)." rows ok!";
}
public function cost_repair_loan_product(){
$this->load->model('items/items_cost_model');
$sql = "
select c.id, c.o_id, c.srv_json, JSON_UNQUOTE(JSON_EXTRACT(c.srv_json,'$.loan_product')) as loan_product, o.payway, o.finance_id, f.title
from lc_items_cost c
left join lc_receiver_orders_v2 o on c.o_id = o.id
left join lc_sys_finance f on f.id = o.finance_id
where o.finance_id > 0
and JSON_UNQUOTE(JSON_EXTRACT(c.srv_json,'$.loan_product')) <> f.title
";
$rows = $this->items_cost_model->db->query($sql)->result_array();
if (!$rows){
echo "数据不存在";exit();
}
foreach ($rows as $row){
$id = $row['id'];
$o_id = $row['o_id'];
$loan_product = $row['title'];
if (!$loan_product){
echo "{$id}, o_id: {$o_id}, title not found <br>";
continue;
}
$srv_json = json_decode($row['srv_json'],true);
$srv_json['loan_product'] = $loan_product;
$upd_data['srv_json'] = json_encode($srv_json,JSON_UNESCAPED_UNICODE);
$res = $this->items_cost_model->update($upd_data, array('id'=>$id));
echo "{$id}, o_id: {$o_id}, update: {$res}<br>";
}
echo "repair ".count($rows)." rows ok!";
}
/**
* 保险供应商名转换
*/
private function cost_insure_product($supplier2, $product){
if (!$supplier2 || !$product){
return $product;
}
$product_parm = '';
foreach ($supplier2 as $supplier){
if ($product == $supplier['title']){
$product_parm = $supplier['title'];
break;
}
if ($supplier['short']){
$shorts = explode(',', $supplier['short']);
foreach ($shorts as $short){
if (strpos($product, $short) !== false){
$product_parm = $supplier['title'];
break;
}
}
}
/*if ($product == $supplier['title'] || $supplier['short'] && strpos($product,$supplier['short']) !== false){
$product_parm = $supplier['title'];
break;
}*/
}
!$product_parm && $product_parm = $product;
return $product_parm;
}
}