Files
qa-ifish7/web/Application/Shop/Model/ShopOrderModel.class.php
2021-03-13 18:42:01 +08:00

306 lines
11 KiB
PHP

<?php
namespace Shop\Model;
use Think\Model\RelationModel;
class ShopOrderModel extends RelationModel{
private $order_status_config=array(
/*
* status_code为订单状态码
* set_table 为查询的对象表
* sql_where 为查询的对象表的判断条件
*/
/*******************以1开头的是正常购买状态************************/
//待付款
array(
"status_code"=>101,
"status_name"=>"待付款",
"set_table"=>"shop_order",
"sql_where"=>array(
"is_pay"=>0,
),
),
//待付款
array(
"status_code"=>111,
"status_name"=>"对公转账",
"set_table"=>"shop_order",
"sql_where"=>array(
"is_pay"=>0,
"is_offline_pay"=>1,
),
),
//待发货
array(
"status_code"=>102,
"status_name"=>"待发货",
"set_table"=>"shop_order",
"sql_where"=>array(
"is_pay"=>1,
"is_delivery"=>0,
),
),
//待收货
array(
"status_code"=>103,
"status_name"=>"待收货",
"set_table"=>"shop_order",
"sql_where"=>array(
"is_delivery"=>1,
"is_receipt"=>0,
),
),
//已收货
array(
"status_code"=>104,
"status_name"=>"已收货",
"set_table"=>"shop_order",
"sql_where"=>array(
"is_delivery"=>1,
"is_receipt"=>1,
),
),
/*******************以2为开头的为取消订单************************/
//取消订单(包含用户取消,超时取消)
array(
"status_code"=>201,
"status_name"=>"已取消",
"set_table"=>"shop_order",
"sql_where"=>array(
"is_cancel"=>1,
),
),
/*******************以3为开头的为退货状态 状态表为副表:order_refund(退款表)************************/
//用户申请退款中
array(
"status_code"=>301,
"status_name"=>"申请退款中",
"set_table"=>"shop_order_refund",
"sql_where"=>array(
"is_refund"=>1,
"is_rerund_agree"=>0,//(is_rerund_agree为1表示管理员同意,为0表示还未操作但不代表不同意)
"is_rerund_disagree"=>0,
),
),
//平台同意退款
array(
"status_code"=>302,
"status_name"=>"平台同意退款",
"set_table"=>"shop_order_refund",
"sql_where"=>array(
"is_refund"=>1,
"is_rerund_agree"=>1,
),
),
//费用已原路返还
array(
"status_code"=>303,
"status_name"=>"退款成功,费用已原路返还",
"set_table"=>"shop_order_refund",
"sql_where"=>array(
"is_refund"=>1,
"is_rerund_agree"=>1,
"is_back_money"=>1,
),
),
//平台不同意退款
array(
"status_code"=>311,
"status_name"=>"平台不同意退款",
"set_table"=>"shop_order_refund",
"sql_where"=>array(
"is_refund"=>1,
"is_rerund_agree"=>0,
"is_rerund_disagree"=>1,
),
),
//用户取消退款
array(
"status_code"=>321,
"status_name"=>"用户取消退款",
"set_table"=>"shop_order_refund",
"sql_where"=>array(
"is_refund_cancel"=>1,
),
),
);
//关联模型
Protected $_link=array(
//收货地址表
"shop_order_address"=>array(
'mapping_type' => self::HAS_ONE,
'parent_key' =>"order_id",
'foreign_key' =>"order_id",
),
//发票信息
"shop_order_invoice"=>array(
'mapping_type' => self::HAS_ONE,
'parent_key' =>"order_id",
'foreign_key' =>"order_id",
),
//收票地址表
"shop_order_invoice_address"=>array(
'mapping_type' => self::HAS_ONE,
'parent_key' =>"order_id",
'foreign_key' =>"order_id",
),
//商品详情表
"shop_order_detail"=>array(
'mapping_type' => self::HAS_MANY,
'parent_key' =>"order_id",
'foreign_key' =>"order_id",
),
//退款表
"shop_order_refund"=>array(
'mapping_type' => self::HAS_ONE,
'parent_key' =>"order_id",
'foreign_key' =>"order_id",
),
//日志表
"shop_order_log"=>array(
'mapping_type' => self::HAS_MANY,
'parent_key' =>"order_id",
'foreign_key' =>"order_id",
),
//会员表
"member" => array(
'mapping_type' => self::BELONGS_TO,
"parent_key" => 'member_id', //parent_key一定是主键,否则会列不出子表的数据
'foreign_key' => 'member_id',
),
);
/*设置订单状态
* $order(Array) 是一个订单信息的所有字段,包含关联的数据表
*/
public function setOrderState($order_id){
//这里一定要将数组倒序一下,订单最初状态判断条件很少。否则会造成判断不准确
$order_status_config = array_reverse($this->order_status_config);
foreach($order_status_config as $v){
$sql_where=$v["sql_where"];
$sql_where["order_id"]=$order_id;
//这里一定是配置里的表名,因为退款表是shop_order_refund表
$count=M($v["set_table"])->where($sql_where)->count();
if($count){
M("shop_order")->save(array(
"order_id"=>$order_id,
"status_code"=>$v["status_code"],
"status_name"=>$v["status_name"],
));
break;
}
}
}
//前台的增加订单
public function add_order($data,$sh_address,$order_invoice,$sp_address){
$member_id = session("member.member_id");
// 创建订单 计算金额
$money=0;
foreach($data as $k=>$v){
$money += ($v[cms_product]['price2']?$v[cms_product]['price2']:$v[cms_product]['price'])* $v["quantity"];
}
$data_order["money"] = number_format($money,2, '.', '');//这里第4个参数一定要加一个空,否则金额超出1000会转换为类似于2,155.00的,会增加一个千分位,存入数据只保存了前面2
$data_order["pay_money"] = number_format($money,2, '.', '');
$data_order["order_num"] = date("YmdHis").$member_id.rand(1000,9999);
$data_order["member_id"] = $member_id;
$data_order["addtime"] = time();
$data_order["remark"] = I("remark");
//收货地址表
$data_order["shop_order_address"] = D("shop_order_address")->create($sh_address);
//发票表
if($order_invoice){
$data_order["shop_order_invoice"] = D("shop_order_invoice")->create($order_invoice);
}
//收票地址表
if($order_invoice){
$data_order["shop_order_invoice_address"] = D("shop_order_invoice_address")->create($sp_address);
}
//商品详情表
foreach ($data as $k=>$v){
$price=($v[cms_product]['price2']?$v[cms_product]['price2']:$v[cms_product]['price']);
$money=($v[cms_product]['price2']?$v[cms_product]['price2']:$v[cms_product]['price']) * $v["quantity"];
$data_order["shop_order_detail"][$k]["thumb"] = $v["cms_product"]["thumb"];
$data_order["shop_order_detail"][$k]["title"] = $v["cms_product"]["title"];
$data_order["shop_order_detail"][$k]["type_no"] = $v["cms_product"]["type_no"];
$data_order["shop_order_detail"][$k]["price"] = number_format($price,2);
$data_order["shop_order_detail"][$k]["product_id"] = $v["cms_product"]["id"];
$data_order["shop_order_detail"][$k]["catid"] = $v["cat"]["catid"];
$data_order["shop_order_detail"][$k]["quantity"] = $v["quantity"];
$data_order["shop_order_detail"][$k]["money"] = number_format($money,2);
//更新订单销量
M("cms_product")->where(array("id"=>$v["cms_product"]["id"]))->setInc('sales',$v["quantity"]);
//
}
$order_id = $this->relation(true)->add($data_order);
//订单日志
$this->log($order_id,"用户创建订单");
//修改订单状态
$this->setOrderState($order_id);
// 删除购物车数据
$ids = session("confirmation_order_ids");
$ids[] = 0;
$where["id"] = array("in",$ids);
M("shop_cart")->where($where)->delete();
session("confirmation_order_ids",null);
return $order_id;
}
//取消订单
public function cancelOrder($order_id,$content){
$this->save(array(
"order_id"=>$order_id,
"is_cancel"=>1,
));
//订单日志
$this->log($order_id,$content);
//更新状态
$this->setOrderState($order_id);
}
//确认收货
public function confirmReceipt($order_id,$content){
$this->save(array(
"order_id"=>$order_id,
"is_receipt"=>1,
));
//订单日志
$this->log($order_id,$content);
//更新状态
$this->setOrderState($order_id);
}
//删除订单
public function deleteOrder($order_id){
$this->relation(true)->delete($order_id);
}
//订单日志
public function log($order_id,$content){
M("shop_order_log")->add(array(
"addtime"=>time(),
"order_id"=>$order_id,
"content"=>$content,
));
}
//自动收货
public function autoReceipt(){
$auto_time=C("shop.auto_receipt_time");
$auto_time=$auto_time?$auto_time:5;
$auto_time=$auto_time*3600*24;
$ago_time=time()-$auto_time;
$data=$this->where(array(
"status_code"=>103,
"delivery_time"=>array("lt",$ago_time),
))->getField("order_id",true);
foreach($data as $v){
$this->confirmReceipt($v,"超时系统自动收货");
}
}
//自动取消
public function autoCancelOrder(){
$auto_time=C("shop.order_timeout");
//$auto_time=$auto_time?$auto_time:60;
$auto_time=$auto_time*3600;
$ago_time=time()-$auto_time;
$data=$this->where(array(
"status_code"=>101,
"addtime"=>array("lt",$ago_time),
))->getField("order_id",true);
foreach($data as $v){
$this->cancelOrder($v,"超时系统取消订单");
}
}
}