Files
xk-api-yii/common/models/oldDb/Ledger.php

90 lines
2.9 KiB
PHP

<?php
namespace common\models\oldDb;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
class Ledger extends \common\modelsgii\oldDb\Ledger
{
public function behaviors()
{
return [
[
'class' => TimestampBehavior::class,
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created_at','updated_at'],
ActiveRecord::EVENT_BEFORE_UPDATE => ['updated_at'],
],
],
];
}
//账户
public function getCashAccount()
{
return $this->hasOne(CashAccount::class,['user_id'=>'user_id']);
}
//产品订单
public function getProductOrder()
{
return $this->hasOne(ProductOrder::class,['id'=>'order_id']);
}
//药品
public function getDrug()
{
return $this->hasOne(Drug::class,['id'=>'drug_id']);
}
//挂号
public function getRegister()
{
return $this->hasOne(Register::class,['id'=>'order_id']);
}
//导出结算记录
public static function inventory($params,$store_id)
{
$where = $andWhere = [];
$where['status'] = [0,1];
//统计时间范围
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($store_id){
$where['user_id'] = $store_id;
}
$list=Ledger::find()->select('id,order_id,sum(money) as total_money,order_type,status,created_at')->where($where)->andWhere($andWhere)->groupBy('order_id,order_type')->asArray()->all();
//把结果按照显示顺序存到返回的数组中
if(!empty($list)){
foreach ($list as $key => $value){
//订单类型
if ($value['order_type']==1){
$order = ProductOrder::find()->where(['id' => $value['order_id']])->one();
$inventory[$key]['order_no'] =$order->order_no;
$inventory[$key]['order_type'] ='产品订单';
}else{
$order = Register::find()->where(['id' => $value['order_id']])->one();
$inventory[$key]['order_no'] =$order->order_no;
$inventory[$key]['order_type'] ='挂号订单';
}
$user = User::find()->where(['id' => $order->user_id])->one();
$inventory[$key]['nickname'] = $user->nickname;
$inventory[$key]['money'] = $value['total_money'];
$inventory[$key]['status'] = $value['status'] == 0?'待结算':'已结算';
$inventory[$key]['created_at'] = date('Y-m-d H:i:s',$value['created_at']);//下单时间
}
}else{
$inventory[0]['prescription_no']= '无数据导出';
}
return $inventory;
}
}