Files
xk-api-yii/common/models/ProductOrder.php
2024-12-13 17:08:25 +08:00

248 lines
11 KiB
PHP

<?php
namespace common\models;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
class ProductOrder extends \common\modelsgii\ProductOrder
{
const EVENT_CREATED = 'orderCreated';
const EVENT_PAYED = 'orderPayed';
const EVENT_CANCELED = 'orderCanceled';
const EVENT_SEND = 'orderSend'; //发货3天后分账结算
public function behaviors()
{
return [
[
'class' => TimestampBehavior::class,
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at', 'updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
],
],
];
}
//关联产品
public function getOrderItems(){
return $this->hasMany(ProductOrderItems::class, ['product_order_id' => 'id']);
}
//关联门店
public function getStore()
{
return $this->hasOne(Store::class, ['id' => 'store_id']);
}
//关联医生
public function getDoctor()
{
return $this->hasOne(DoctorInfo::class, ['su_id' => 'su_id']);
}
//关联医生
public function getDoctorId()
{
return $this->hasOne(DoctorInfo::class, ['id' => 'su_id']);
}
//关联快递单号
public function getExpressNos()
{
return $this->hasOne(ExpressNos::class, ['id' => 'express_no_id']);
}
//关联已通过审核的处方
public function getApprovedPrescription(){
return $this->hasOne(Prescription::class, ['id' => 'p_id'])->where(['yii_prescription.status' => 1]);
}
//关联处方
public function getPrescription()
{
return $this->hasOne(Prescription::class, ['id' => 'p_id']);
}
//关联患者
public function getUserPatient()
{
return $this->hasOne(UserPatient::class, ['id' => 'up_id']);
}
//关联用户
public function getUser()
{
return $this->hasOne(User::class, ['id' => 'user_id']);
}
//
public function getProductOrderRefund()
{
return $this->hasOne(ProductOrderRefund::class, ['order_id' => 'id']);
}
//get transaction_id
public static function getSHH($order_no=null)
{
return PaymentProductOrder::find()->where(['is_pay'=>1,'order_no'=>$order_no])->andWhere(['not', ['transaction_id' => null]])->one();
}
//导出
public static function inventory($params,$store_id)
{
if($store_id){
$where['store_id'] = $store_id;
}
$where = $andWhere = [];
//统计时间范围
if (!empty($params['start_time']) && !empty($params['end_time'])) {
$start_time = strtotime($params['start_time']);
$end_time = strtotime($params['end_time']);
$andWhere = ['between', 'created_at', $start_time, $end_time];
}
if(isset($params['status']) && $params['status'] !== null && $params['status'] !== ""){
if($params['status'] == 3){
$where['status'] = [3,6,7];
}else{
$where['status'] = $params['status'];
}
}
if(isset($params['refund_status'])){
$where['refund_status'] = $params['after_status'];
}
// return $data;
//查询数据
$list = ProductOrder::find()->where($where)->andWhere($andWhere)->with(['prescription', 'doctor', 'doctorId','store'])->orderBy(['id' => SORT_DESC])->asArray()->all();
//把结果按照显示顺序存到返回的数组中
if (!empty($list)) {
foreach ($list as $key => $value) {
try {
$ProductOrder=self::getSHH($value['order_no']);
if (!empty($ProductOrder)) {
$inventory[$key]['epl_order_no'] = "\t".$ProductOrder['transaction_id']??'暂无';//易票联订单号
} else {
$inventory[$key]['epl_order_no'] = "\t".'暂无';//易票联订单号
}
$inventory[$key]['order_no'] = "\t" . $value['order_no'];//订单号
$inventory[$key]['store'] = "\t" . $value['store']['name'];//诊所名称
$content = $value['prescription'];
$item = [];
$prescriptionType = $content['prescription_type']?? 0;
if ($prescriptionType==1 ||$prescriptionType==3){//中药 配方颗粒
$repice = json_decode($content['content'], true)['repice'];
foreach ($repice as $val) {
if (!isset($item['drug_name'])) {
$item['drug_name'] = '';
}
if (!isset($item['specification'])) {
$item['specification'] = '';
}
if (!isset($item['source'])) {
$item['source'] = '';
}
if (!isset($item['unit_price'])) {
$item['unit_price'] = '';
}
foreach (json_decode($val['content'], true) as $vv) {
$item['drug_name'] .= array_key_exists('drug_name', $vv)? $vv['drug_name'] . '|': '获取失败' . '|';
$item['specification'] .= array_key_exists('specification', $vv)? $vv['specification'] . '|' : '无' . '|';
$item['source'] .= array_key_exists('source', $vv)? $vv['source'] . '|' : '无' . '|';
$item['unit_price'] .= $val['total_price'] . '|' ?? '无' . '|';
}
}
} else {//西药
if (isset($content['content'])) {
$repice = json_decode($content['content'], true)['repice']?? [];
} else {
$repice = [];
}
if(!empty($repice)){
foreach ($repice as $val) {
if (!isset($item['drug_name'])) {
$item['drug_name'] = '';
}
if (!isset($item['specification'])) {
$item['specification'] = '';
}
if (!isset($item['source'])) {
$item['source'] = '';
}
if (!isset($item['unit_price'])) {
$item['unit_price'] = '';
}
$contentItem = json_decode($val['content'], true);
$item['drug_name'] .= array_key_exists('drug_name', $contentItem)? $contentItem['drug_name'] . '|': '获取失败' . '|';
$item['specification'] .= array_key_exists('specification', $contentItem)? $contentItem['specification'] . '|' : '无' . '|';
$item['source'] .= array_key_exists('source', $contentItem)? $contentItem['source'] . '|' : '无' . '|';
$item['unit_price'] .= $val['total_price'] . '|' ?? '无' . '|';
}
}
}
$inventory[$key]['drug_name'] = $item['drug_name']?? '';
$inventory[$key]['specification'] = $item['specification']?? '';//规格
$inventory[$key]['source'] = $item['source']?? '';//厂家
$inventory[$key]['unit_price'] = $item['unit_price']?? '';//单价
$inventory[$key]['process_price'] = $value['process_price']?? '';
$inventory[$key]['treatement_price'] = $value['treatement_price']?? '';
$inventory[$key]['items_price'] = $value['items_price']?? '';
$inventory[$key]['market_price'] = $value['market_price']?? '';
$inventory[$key]['total_price'] = $value['total_pay_price']?? '';
$inventory[$key]['trans_expenses'] = $value['trans_expenses']?? '';
// $inventory[$key]['drugstore'] = $value['drugStore']['name'];
// $inventory[$key]['doctor'] = $value['doctor']['name']?? '无'. '-'. $value['su_id'];
$inventory[$key]['doctor'] = $value['doctor']['name']?? $value['doctorId']['name']?? '该医生已被删除'. $value['su_id'];
//订单状态
if ($value['status'] == 0) $inventory[$key]['status'] = '未支付';
if ($value['status'] == 1) $inventory[$key]['status'] = '待发货';
if ($value['status'] == 2) $inventory[$key]['status'] = '待收货';
if ($value['status'] == 4) $inventory[$key]['status'] = '已退款';
if ($value['status'] == 5) $inventory[$key]['status'] = '退款中';
if ($value['status'] == 3 || $value['status'] == 6 || $value['status'] == 7) $inventory[$key]['status'] = '已完成';
if ($value['status'] == 9) $inventory[$key]['status'] = '已取消';
//售后
if ($value['refund_status'] == 0) $inventory[$key]['after_status'] = '暂无';
if ($value['refund_status'] == 1) $inventory[$key]['after_status'] = '申请退款';
if ($value['refund_status'] == 2) $inventory[$key]['after_status'] = '同意退款';
if ($value['refund_status'] == 3) $inventory[$key]['after_status'] = '已退款';
if ($value['refund_status'] == 9) $inventory[$key]['after_status'] = '取消退款';
$inventory[$key]['created_at'] = date('Y-m-d H:i:s', $value['created_at']);//下单时间
$inventory[$key]['accept_name'] = $value['express_name'];// json_decode($value['address'])->name;
$inventory[$key]['accept_tel'] = $value['express_mobile'];//json_decode($value['address'])->mobile;
$inventory[$key]['accept_address'] = $value['express_region'] . $value['express_address'];//json_decode($value['address'])->region;
//支付时间
if ($value['pay_time']==0){
$inventory[$key]['pay_time'] = '暂无';
}else{
$inventory[$key]['pay_time'] = date('Y-m-d H:i:s', $value['pay_time']);
}
} catch (\Exception $e) {
ds([
'msg' => $e->getMessage(),
'line' => $e->getLine(),
'data' => $value
]);
}
}
} else {
$inventory[0]['product_order'] = '无数据导出';
}
return $inventory;
}
}