| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283 |
- <?php
- namespace common\models\order;
- use common\models\base\BaseActiveRecord;
- use common\enums\StatusEnum;
- use common\models\user\User;
- use common\components\export\CsvTool;
- use common\enums\DownloadEnum;
- use common\models\common\Download;
- use Exception;
- use Yii;
- use yii\helpers\Json;
- /**
- * This is the model class for table "{{%order_invoice}}".
- *
- * @property int $id
- * @property int $user_id 用户id
- * @property int $mall_id 商城ID
- * @property int $order_id 订单ID
- * @property string $order_no 订单编号
- * @property float|null $pay_money 订单实付金额
- * @property int $type 发票类型
- * @property int $head_type 抬头类型
- * @property string $head_name 发票抬头
- * @property string $account 开户账户
- * @property string $email 邮箱号
- * @property string $address 地址
- * @property string $mobile 电话
- * @property string $tax_no 税号
- * @property int $status 状态[-1:删除;0:禁用;1启用]
- * @property int $created_at
- * @property int $updated_at
- */
- class OrderInvoice extends BaseActiveRecord
- {
- /**
- * {@inheritdoc}
- */
- public static function tableName()
- {
- return '{{%order_invoice}}';
- }
- /**
- * {@inheritdoc}
- */
- public function rules()
- {
- return [
- [['user_id', 'mall_id', 'order_id', 'type', 'head_type', 'status', 'created_at', 'updated_at','invoice_status'], 'integer'],
- [['pay_money'], 'number'],
- [['order_no', 'head_name', 'account', 'email', 'tax_no','bank_name'], 'string', 'max' => 100],
- [['address','remark'], 'string', 'max' => 255],
- [['mobile'], 'string', 'max' => 20],
- ];
- }
- /**
- * {@inheritdoc}
- */
- public function attributeLabels()
- {
- return [
- 'id' => 'ID',
- 'user_id' => 'User ID',
- 'mall_id' => 'Mall ID',
- 'order_id' => 'Order ID',
- 'order_no' => 'Order No',
- 'pay_money' => 'Pay Money',
- 'type' => 'Type',
- 'head_type' => 'Head Type',
- 'head_name' => 'Head Name',
- 'account' => 'Account',
- 'email' => 'Email',
- 'address' => 'Address',
- 'mobile' => 'Mobile',
- 'tax_no' => 'Tax No',
- 'status' => 'Status',
- 'created_at' => 'Created At',
- 'updated_at' => 'Updated At',
- ];
- }
- public function getUser(): \yii\db\ActiveQuery
- {
- return $this->hasOne(User::class, ['id' => 'user_id']);
- }
- /**
- * 保存数据
- * @param array $params
- * @return bool|UserWallet
- */
- public static function setData(array $params){
- try{
- if(!empty($params['id'])){
- $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]);
- if(empty($model)) throw new \Exception('数据不存在或已删除');
- }else{
- $model = new self();
- $model->mall_id = $params['mall_id'];
- $model->loadDefaultValues();
- }
- if(!$model->load($params,'') || !$model->save()){
- throw new \Exception($model->getErrorMessage());
- }
- return $model->toArray();
- }catch(\Exception $e){
- self::$static_error = $e->getMessage();
- return false;
- }
- }
- /**
- * 导出字段
- */
- public static function exportFieldsList($field = null)
- {
- $fields = [
- 'user_id' => '用户ID',
- 'nickname' => '昵称',
- 'mobile' => '手机号',
- 'order_no' => '订单编号',
- 'pay_money' => '发票金额',
- 'type' => '发票类型',
- 'head_type' => '抬头类型',
- 'head_name' => '发票抬头',
- 'bank_name' => '开户银行',
- 'account' => '开户账户',
- 'tax_no' => '税号',
- 'email' => '邮箱号',
- 'address' => '企业地址',
- 'mobile' => '电话',
- 'invoice_status' => '开票状态',
- 'created_at' => '申请时间',
- 'remark' => '备注',
- ];
- return $fields;
- }
- /**
- * 导出字段
- */
- public static function fieldsList($field = null)
- {
- $fields = [
- 'user_id' => '用户ID',
- 'nickname' => '昵称',
- 'mobile' => '手机号',
- 'order_no' => '订单编号',
- 'pay_money' => '发票金额',
- 'type' => '发票类型',
- 'head_type' => '抬头类型',
- 'head_name' => '发票抬头',
- 'bank_name' => '开户银行',
- 'account' => '开户账户',
- 'tax_no' => '税号',
- 'email' => '邮箱号',
- 'address' => '企业地址',
- 'mobile' => '电话',
- 'invoice_status' => '开票状态',
- 'created_at' => '申请时间',
- 'remark' => '备注',
- ];
- if ($field === null) return $fields;
- $temp = [];
- foreach ($field as $val) {
- if (isset($fields[$val])) $temp[$val] = $fields[$val];
- }
- return $temp;
- }
- /**
- * @name:
- * @msg: 处理导出数据,这个方法是异步队列处理时使用的,请别修改
- * @param {array} $data
- * @return bool
- */
- public static function exportHandle(array $data)
- {
- $download_id = $data['download_id'] ?? 0;
- $download = Download::findOne(['id' => $download_id]);
- if ($download) {
- try {
- $params = json_decode($download->params, true);
- $filename = $download->name;
- $type = $download->type;
- $fields = $params['fields'];
- $fields_arr = self::fieldsList();
- $have_select_field_arr = [];
- foreach ($fields as $value) {
- $have_select_field_arr[] = $fields_arr[$value];
- }
- $csv = new CsvTool();
- $csv->filename = $filename;
- $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
- $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
- $csv->header = $have_select_field_arr;
- $model = self::find();
- $model->where(['mall_id' => $params['mall_id']]);
- $model->andWhere(['>=', 'status', StatusEnum::DISABLED]);
- $user_id_arr = [];
- //拼接查询条件
- if (!empty($params['keyword'])) {
- $condition['where'] = [[
- 'or',
- ['like', 'nickname', $params['keyword']],
- ['like', 'mobile', $params['keyword']],
- ]];
- $user_list = User::lists($condition);
- $user_id_arr = array_column($user_list, 'id');
- }
- if ($user_id_arr) {
- $model->andWhere(['in', 'user_id', $user_id_arr]);
- }
- if ($params['user_id']) {
- $model->andWhere(['=', 'user_id', $params['user_id']]);
- }
- if ($params['order_no']) {
- $model->andWhere(['=', 'order_no', $params['order_no']]);
- }
- if ($params['head_type'] != -1) {
- $model->andWhere(['=', 'head_type', $params['head_type']]);
- }
- if ($params['invoice_status'] != -1) {
- $model->andWhere(['=', 'invoice_status', $params['invoice_status']]);
- }
- $model->with(['user'])->orderBy('created_at desc');
- //遍历组装数据
- foreach ($model->asArray()->batch() as $list) {
- //查询上级昵称数组
- $csv->data = array_map(function ($model) use ($fields) {
- $row = [];
- //遍历所选字段
- foreach ($fields as $field) {
- if (in_array($field, ['created_at'])) {
- $row[] = !empty($model[$field]) ? date('Y-m-d H:i:s', $model[$field]) : '';
- } elseif (in_array($field, ['nickname', 'mobile'])) {
- $row[] = !empty($model['user'][$field]) ? $model['user'][$field] : '';
- } elseif (in_array($field, ['type'])) {
- $text = '';
- if($model['type'] == 1) $text = '增值税电子普通发票';
- $row[] = $text;
- }elseif (in_array($field, ['head_type'])) {
- $text = '';
- if($model['head_type'] == 1) $text = '个人/事业单位';
- if($model['head_type'] == 2) $text = '企业';
- $row[] = $text;
- }elseif (in_array($field, ['invoice_status'])) {
- $text = '';
- if($model['invoice_status'] == 0) $text = '待开票';
- if($model['invoice_status'] == 1) $text = '已开票';
- if($model['invoice_status'] == 2) $text = '已驳回';
- $row[] = $text;
- } else {
- $row[] = !empty($model[$field]) ? $model[$field] : '';
- }
- }
- return $row;
- }, $list);
- $csv->export();
- }
- $csv->updateDown($download, $params['mall_id']);
- return true;
- } catch (\Exception $e) {
- $download->status = 3;
- $res = $download->save();
- var_dump($e->getMessage());
- self::setStaticError($e->getMessage());
- return false;
- }
- }
- }
- }
|