OrderInvoice.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. <?php
  2. namespace common\models\order;
  3. use common\models\base\BaseActiveRecord;
  4. use common\enums\StatusEnum;
  5. use common\models\user\User;
  6. use common\components\export\CsvTool;
  7. use common\enums\DownloadEnum;
  8. use common\models\common\Download;
  9. use Exception;
  10. use Yii;
  11. use yii\helpers\Json;
  12. /**
  13. * This is the model class for table "{{%order_invoice}}".
  14. *
  15. * @property int $id
  16. * @property int $user_id 用户id
  17. * @property int $mall_id 商城ID
  18. * @property int $order_id 订单ID
  19. * @property string $order_no 订单编号
  20. * @property float|null $pay_money 订单实付金额
  21. * @property int $type 发票类型
  22. * @property int $head_type 抬头类型
  23. * @property string $head_name 发票抬头
  24. * @property string $account 开户账户
  25. * @property string $email 邮箱号
  26. * @property string $address 地址
  27. * @property string $mobile 电话
  28. * @property string $tax_no 税号
  29. * @property int $status 状态[-1:删除;0:禁用;1启用]
  30. * @property int $created_at
  31. * @property int $updated_at
  32. */
  33. class OrderInvoice extends BaseActiveRecord
  34. {
  35. /**
  36. * {@inheritdoc}
  37. */
  38. public static function tableName()
  39. {
  40. return '{{%order_invoice}}';
  41. }
  42. /**
  43. * {@inheritdoc}
  44. */
  45. public function rules()
  46. {
  47. return [
  48. [['user_id', 'mall_id', 'order_id', 'type', 'head_type', 'status', 'created_at', 'updated_at','invoice_status'], 'integer'],
  49. [['pay_money'], 'number'],
  50. [['order_no', 'head_name', 'account', 'email', 'tax_no','bank_name'], 'string', 'max' => 100],
  51. [['address','remark'], 'string', 'max' => 255],
  52. [['mobile'], 'string', 'max' => 20],
  53. ];
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function attributeLabels()
  59. {
  60. return [
  61. 'id' => 'ID',
  62. 'user_id' => 'User ID',
  63. 'mall_id' => 'Mall ID',
  64. 'order_id' => 'Order ID',
  65. 'order_no' => 'Order No',
  66. 'pay_money' => 'Pay Money',
  67. 'type' => 'Type',
  68. 'head_type' => 'Head Type',
  69. 'head_name' => 'Head Name',
  70. 'account' => 'Account',
  71. 'email' => 'Email',
  72. 'address' => 'Address',
  73. 'mobile' => 'Mobile',
  74. 'tax_no' => 'Tax No',
  75. 'status' => 'Status',
  76. 'created_at' => 'Created At',
  77. 'updated_at' => 'Updated At',
  78. ];
  79. }
  80. public function getUser(): \yii\db\ActiveQuery
  81. {
  82. return $this->hasOne(User::class, ['id' => 'user_id']);
  83. }
  84. /**
  85. * 保存数据
  86. * @param array $params
  87. * @return bool|UserWallet
  88. */
  89. public static function setData(array $params){
  90. try{
  91. if(!empty($params['id'])){
  92. $model = self::findOne(['and',['id' => $params['id'],'mall_id' => $params['mall_id']],['<>','status',StatusEnum::DELETE]]);
  93. if(empty($model)) throw new \Exception('数据不存在或已删除');
  94. }else{
  95. $model = new self();
  96. $model->mall_id = $params['mall_id'];
  97. $model->loadDefaultValues();
  98. }
  99. if(!$model->load($params,'') || !$model->save()){
  100. throw new \Exception($model->getErrorMessage());
  101. }
  102. return $model->toArray();
  103. }catch(\Exception $e){
  104. self::$static_error = $e->getMessage();
  105. return false;
  106. }
  107. }
  108. /**
  109. * 导出字段
  110. */
  111. public static function exportFieldsList($field = null)
  112. {
  113. $fields = [
  114. 'user_id' => '用户ID',
  115. 'nickname' => '昵称',
  116. 'mobile' => '手机号',
  117. 'order_no' => '订单编号',
  118. 'pay_money' => '发票金额',
  119. 'type' => '发票类型',
  120. 'head_type' => '抬头类型',
  121. 'head_name' => '发票抬头',
  122. 'bank_name' => '开户银行',
  123. 'account' => '开户账户',
  124. 'tax_no' => '税号',
  125. 'email' => '邮箱号',
  126. 'address' => '企业地址',
  127. 'mobile' => '电话',
  128. 'invoice_status' => '开票状态',
  129. 'created_at' => '申请时间',
  130. 'remark' => '备注',
  131. ];
  132. return $fields;
  133. }
  134. /**
  135. * 导出字段
  136. */
  137. public static function fieldsList($field = null)
  138. {
  139. $fields = [
  140. 'user_id' => '用户ID',
  141. 'nickname' => '昵称',
  142. 'mobile' => '手机号',
  143. 'order_no' => '订单编号',
  144. 'pay_money' => '发票金额',
  145. 'type' => '发票类型',
  146. 'head_type' => '抬头类型',
  147. 'head_name' => '发票抬头',
  148. 'bank_name' => '开户银行',
  149. 'account' => '开户账户',
  150. 'tax_no' => '税号',
  151. 'email' => '邮箱号',
  152. 'address' => '企业地址',
  153. 'mobile' => '电话',
  154. 'invoice_status' => '开票状态',
  155. 'created_at' => '申请时间',
  156. 'remark' => '备注',
  157. ];
  158. if ($field === null) return $fields;
  159. $temp = [];
  160. foreach ($field as $val) {
  161. if (isset($fields[$val])) $temp[$val] = $fields[$val];
  162. }
  163. return $temp;
  164. }
  165. /**
  166. * @name:
  167. * @msg: 处理导出数据,这个方法是异步队列处理时使用的,请别修改
  168. * @param {array} $data
  169. * @return bool
  170. */
  171. public static function exportHandle(array $data)
  172. {
  173. $download_id = $data['download_id'] ?? 0;
  174. $download = Download::findOne(['id' => $download_id]);
  175. if ($download) {
  176. try {
  177. $params = json_decode($download->params, true);
  178. $filename = $download->name;
  179. $type = $download->type;
  180. $fields = $params['fields'];
  181. $fields_arr = self::fieldsList();
  182. $have_select_field_arr = [];
  183. foreach ($fields as $value) {
  184. $have_select_field_arr[] = $fields_arr[$value];
  185. }
  186. $csv = new CsvTool();
  187. $csv->filename = $filename;
  188. $csv->file_dirname = DownloadEnum::getTypeStorageDirMap($type);
  189. $csv->export_mode = CsvTool::EXPORT_MODE_ASYNC;
  190. $csv->header = $have_select_field_arr;
  191. $model = self::find();
  192. $model->where(['mall_id' => $params['mall_id']]);
  193. $model->andWhere(['>=', 'status', StatusEnum::DISABLED]);
  194. $user_id_arr = [];
  195. //拼接查询条件
  196. if (!empty($params['keyword'])) {
  197. $condition['where'] = [[
  198. 'or',
  199. ['like', 'nickname', $params['keyword']],
  200. ['like', 'mobile', $params['keyword']],
  201. ]];
  202. $user_list = User::lists($condition);
  203. $user_id_arr = array_column($user_list, 'id');
  204. }
  205. if ($user_id_arr) {
  206. $model->andWhere(['in', 'user_id', $user_id_arr]);
  207. }
  208. if ($params['user_id']) {
  209. $model->andWhere(['=', 'user_id', $params['user_id']]);
  210. }
  211. if ($params['order_no']) {
  212. $model->andWhere(['=', 'order_no', $params['order_no']]);
  213. }
  214. if ($params['head_type'] != -1) {
  215. $model->andWhere(['=', 'head_type', $params['head_type']]);
  216. }
  217. if ($params['invoice_status'] != -1) {
  218. $model->andWhere(['=', 'invoice_status', $params['invoice_status']]);
  219. }
  220. $model->with(['user'])->orderBy('created_at desc');
  221. //遍历组装数据
  222. foreach ($model->asArray()->batch() as $list) {
  223. //查询上级昵称数组
  224. $csv->data = array_map(function ($model) use ($fields) {
  225. $row = [];
  226. //遍历所选字段
  227. foreach ($fields as $field) {
  228. if (in_array($field, ['created_at'])) {
  229. $row[] = !empty($model[$field]) ? date('Y-m-d H:i:s', $model[$field]) : '';
  230. } elseif (in_array($field, ['nickname', 'mobile'])) {
  231. $row[] = !empty($model['user'][$field]) ? $model['user'][$field] : '';
  232. } elseif (in_array($field, ['type'])) {
  233. $text = '';
  234. if($model['type'] == 1) $text = '增值税电子普通发票';
  235. $row[] = $text;
  236. }elseif (in_array($field, ['head_type'])) {
  237. $text = '';
  238. if($model['head_type'] == 1) $text = '个人/事业单位';
  239. if($model['head_type'] == 2) $text = '企业';
  240. $row[] = $text;
  241. }elseif (in_array($field, ['invoice_status'])) {
  242. $text = '';
  243. if($model['invoice_status'] == 0) $text = '待开票';
  244. if($model['invoice_status'] == 1) $text = '已开票';
  245. if($model['invoice_status'] == 2) $text = '已驳回';
  246. $row[] = $text;
  247. } else {
  248. $row[] = !empty($model[$field]) ? $model[$field] : '';
  249. }
  250. }
  251. return $row;
  252. }, $list);
  253. $csv->export();
  254. }
  255. $csv->updateDown($download, $params['mall_id']);
  256. return true;
  257. } catch (\Exception $e) {
  258. $download->status = 3;
  259. $res = $download->save();
  260. var_dump($e->getMessage());
  261. self::setStaticError($e->getMessage());
  262. return false;
  263. }
  264. }
  265. }
  266. }