BaseExport.php 67 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515
  1. <?php
  2. namespace common\libs\export;
  3. use common\helpers\Cache;
  4. use common\helpers\Cache as CacheHelper;
  5. use common\helpers\Date;
  6. use common\helpers\Form;
  7. use common\helpers\http\RemoteUploadApi;
  8. use common\helpers\LoggerTool;
  9. use common\helpers\Tool;
  10. use common\helpers\user\Info;
  11. use common\libs\dataList\DataList;
  12. use common\models\ApproachOrder;
  13. use common\models\ApproachOrderGoods;
  14. use common\models\BaApproachOrder;
  15. use common\models\BaApproachOrderGoods;
  16. use common\models\BaOrder;
  17. use common\models\BaOrderGoods;
  18. use common\models\BaUser;
  19. use common\models\Export;
  20. use common\models\Order;
  21. use common\models\OrderGoods;
  22. use common\models\Region;
  23. use common\models\ShopGoods;
  24. use common\models\User;
  25. use Yii;
  26. use yii\base\Exception;
  27. use yii\base\InvalidConfigException;
  28. use yii\base\StaticInstanceTrait;
  29. use yii\base\Component;
  30. use yii\db\Query;
  31. class BaseExport extends Component {
  32. use StaticInstanceTrait;
  33. public $moduleId;
  34. /**
  35. * @var string the model class name. This property must be set.
  36. */
  37. public $modelClass;
  38. /**
  39. * @var
  40. */
  41. public $listModelClass;
  42. /**
  43. * @var int
  44. */
  45. public $pageSize = 100;
  46. /**
  47. * @var int
  48. */
  49. public $totalCount = 0;
  50. /**
  51. * @var int
  52. */
  53. public $offset = 0;
  54. /**
  55. * @var int
  56. */
  57. public $limit = 100;
  58. /**
  59. * csv文件名称
  60. * @var null
  61. */
  62. public $fileName = null;
  63. /**
  64. * 保存的基本路径
  65. * @var null
  66. */
  67. public $saveBasePath = null;
  68. /**
  69. * 保存路径
  70. * @var null
  71. */
  72. public $savePath = null;
  73. /**
  74. * 正在导出的id
  75. * 来源于exporting_file表
  76. * @var null
  77. */
  78. public $exportId = null;
  79. /**
  80. * 操作人的id
  81. * @var null
  82. */
  83. public $userId = null;
  84. /**
  85. * 任务id
  86. * @var null
  87. */
  88. public $taskId = null;
  89. /**
  90. * 是否完成
  91. * @var bool
  92. */
  93. public $completed = false;
  94. /**
  95. * @var array
  96. */
  97. public $params = [];
  98. /**
  99. * 文件句柄
  100. * @var null
  101. */
  102. private $_fp = null;
  103. /**
  104. * 是否上传远程
  105. * @var bool
  106. */
  107. public $isRemote = true;
  108. /**
  109. * @var DataList
  110. */
  111. private $_listModel;
  112. public static function factory($taskId) {
  113. return new self([
  114. 'taskId' => $taskId
  115. ]
  116. );
  117. }
  118. /**
  119. *
  120. */
  121. public function init() {
  122. parent::init();
  123. $this->isRemote = \Yii::$app->params['isRemoteUpload'];
  124. }
  125. /**
  126. * 生成复杂的路径
  127. * @return string
  128. */
  129. public function pathCreator(...$params) {
  130. $hash = md5(implode('_', $params));
  131. $first = substr($hash, 0, 3);
  132. $second = substr($hash, 3, 2);
  133. $dir = $first . __DS__ . $second . __DS__;
  134. return $dir;
  135. }
  136. /**
  137. * 获取文件名
  138. * @return string|null
  139. * @throws \Exception
  140. */
  141. public function getFileName($ext = '.csv') {
  142. $this->fileName = date('YmdHis', Date::nowTime()) . random_int(1000, 9999) . $ext;
  143. return $this->fileName;
  144. }
  145. /**
  146. * 获取保存的路径
  147. * @return array|null
  148. */
  149. public function getSavePath() {
  150. $this->savePath = $this->pathCreator(date('Ymd', Date::nowTime()));
  151. return trim($this->savePath, __DS__);
  152. }
  153. /**
  154. * @return bool|string|null
  155. */
  156. public function getSaveBasePath() {
  157. $this->saveBasePath = Yii::getAlias('@backendApi') . __DS__ . 'web' . __DS__ . 'upload' . __DS__ . \Yii::$app->params['excelLocalDir'];
  158. return trim($this->saveBasePath, __DS__);
  159. }
  160. /**
  161. * 创建目录(递归创建)
  162. *
  163. * @param $dir
  164. * @return string|false
  165. */
  166. public function mkdir($dir) {
  167. if (!is_dir($dir)) {
  168. $this->mkdir(dirname($dir));
  169. if (!mkdir($dir, 0777))
  170. return false;
  171. @exec('chown -R www:www /'.$dir);
  172. @exec('chmod -R 777 /'.$dir);
  173. }
  174. return $dir;
  175. }
  176. /**
  177. * @return array|mixed
  178. */
  179. public function getParams() {
  180. $this->params = CacheHelper::getAsyncParams($this->taskId);
  181. // 设置语言
  182. Yii::$app->language = $this->params['language'] ?? 'en-US';
  183. Yii::$app->sourceLanguage = Yii::$app->language == 'zh-CN' ? 'en-US' : 'zh-CN';
  184. return $this->params;
  185. }
  186. /**
  187. * @return mixed|null
  188. */
  189. public function getUserId() {
  190. $this->userId = isset($this->params['userId']) ? $this->params['userId'] : null;
  191. return $this->userId;
  192. }
  193. /**
  194. * @return |null
  195. */
  196. public function getExportId() {
  197. $this->exportId = isset($this->params['exportId']) ? $this->params['exportId'] : null;
  198. return $this->userId;
  199. }
  200. /**
  201. * 生成
  202. * @return bool
  203. * @throws Exception
  204. * @throws InvalidConfigException
  205. * @throws \yii\httpclient\Exception
  206. */
  207. public function generate() {
  208. $this->getParams();
  209. if (!$this->params) {
  210. throw new Exception('无法获取需要的参数');
  211. }
  212. $path = $this->getSaveBasePath() . __DS__ . $this->getSavePath();
  213. $path = __DS__ . $path;
  214. $realFile = $this->mkdir($path) . __DS__ . $this->getFileName();
  215. $this->completed = false;
  216. $this->getExportId();
  217. $this->getUserId();
  218. $fileNameUpdated = false;
  219. $this->_fp = fopen($realFile, 'w');
  220. @exec('chown -R www:www /'.$realFile);
  221. @exec('chmod -R 777 /'.$realFile);
  222. // 获取列表数据及表头
  223. $this->_listModel = new $this->listModelClass();
  224. $this->_listModel->isExport = true;
  225. if(method_exists($this->_listModel, 'getExportHeaders')){
  226. if(method_exists($this->_listModel, 'exportPrepare')) {//导出数据提前设置参数
  227. $this->_listModel->exportPrepare(['condition' => $this->params['condition'], 'params' => $this->params['params'], 'others' => isset($this->params['others']) ? $this->params['others'] : [], 'page' => 0, 'pageSize' => $this->pageSize, 'userId' => $this->userId]);
  228. }
  229. $headers = $this->_listModel->getExportHeaders($this->userId);
  230. fputcsv($this->_fp, $headers);
  231. unset($headers);
  232. $this->_updateFirst($realFile, 1);
  233. } else {
  234. throw new Exception($this->listModelClass.'的getExportHeaders方法不存在');
  235. }
  236. $this->_loopWriteData($realFile, $fileNameUpdated);
  237. $this->complete();
  238. return true;
  239. }
  240. /**
  241. * 循环写入数据
  242. * @param $realFile
  243. * @param $fileNameUpdated
  244. * @param int $page
  245. * @param int $counter
  246. * @return string
  247. * @throws Exception
  248. */
  249. private function _loopWriteData($realFile, &$fileNameUpdated, $page = 0, $counter = 0){
  250. if(method_exists($this->_listModel, 'getList')){
  251. $list = $this->_listModel->getList(['condition'=>$this->params['condition'], 'params'=> $this->params['params'] ?? [], 'others'=> $this->params['others'] ?? [], 'page'=>$page, 'pageSize'=>$this->pageSize, 'userId'=>$this->userId]);
  252. } else {
  253. throw new Exception($this->listModelClass.'的getList方法不存在');
  254. }
  255. if($page >= $list['totalPages']){
  256. return 'finish';
  257. }
  258. if(!empty($list['list'])){
  259. foreach($list['list'] as $columnData){
  260. fputcsv($this->_fp, Tool::arrTextConvert($columnData));
  261. $counter++;
  262. if ($list['totalCount'] < $counter) {
  263. // $counter = $list['totalCount'];
  264. return 'finish';
  265. }
  266. $percent = intval(($counter / $list['totalCount']) * 100);
  267. $this->_updatePercent($percent);
  268. // if (!$fileNameUpdated) {
  269. // $this->_updateFirst($realFile, $percent);
  270. // } else {
  271. // $this->_updatePercent($percent);
  272. // }
  273. // $fileNameUpdated = true;
  274. unset($percent, $columnData);
  275. }
  276. $page++;
  277. unset($list);
  278. return $this->_loopWriteData($realFile, $fileNameUpdated, $page, $counter);
  279. }
  280. unset($list);
  281. return 'finish';
  282. }
  283. /**
  284. * 循环写入数据
  285. */
  286. private function _loopWriteDataOrder()
  287. {
  288. $orderQuery = Order::find()
  289. ->alias('O')
  290. ->where($this->params['condition'], $this->params['params'])
  291. ->select('O.*,U.REAL_NAME,U.DEC_ID,SG.CATEGORY_TYPE,OG.REAL_PRICE,OG.TAX_RATE,OG.BUY_NUMS,OG.SKU_CODE,OG.GOODS_TITLE,OG.REAL_PV,OG.STANDARD_PRICE')
  292. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  293. ->join('LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  294. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  295. ->orderBy('O.CREATED_AT DESC');
  296. // 订单中间表只查询待支付和支付失败的订单
  297. $this->params['params'][':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  298. $this->params['params'][':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  299. $orderStandardQuery = ApproachOrder::find()
  300. ->alias('O')
  301. ->where($this->params['condition'] . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $this->params['params'])
  302. ->select('O.*,U.REAL_NAME,U.DEC_ID,SG.CATEGORY_TYPE,OG.REAL_PRICE,OG.TAX_RATE,OG.BUY_NUMS,OG.SKU_CODE,OG.GOODS_TITLE,OG.REAL_PV,OG.STANDARD_PRICE')
  303. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  304. ->join('LEFT JOIN', ApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  305. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  306. ->orderBy('O.CREATED_AT DESC');
  307. $queryAll = $orderQuery->union($orderStandardQuery, true);
  308. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  309. $lists = $query->all();
  310. if(!empty($lists)){
  311. $regionConfig = Cache::getRegionConfig();
  312. foreach($lists as $columnData) {
  313. $CREATE_USER_ID = Info::getUserIdByUserName($columnData['CREATE_USER']);
  314. $createUserName = Info::getUserRealNameByUserId($CREATE_USER_ID);
  315. $decUserName = Info::getUserNameByUserId($columnData['DEC_ID']);
  316. $columnAccept = [
  317. 'USER_NAME' => $columnData['USER_NAME'],
  318. 'DEC_USER_NAME' => $decUserName,
  319. 'CREATE_USER' => $columnData['CREATE_USER'],
  320. 'CREATE_USER_NAME' => $createUserName,
  321. 'SN' => $columnData['SN'],
  322. 'STATUS' => \Yii::$app->params['orderStatus'][$columnData['STATUS']]['label'] ?? '',
  323. 'SKU_CODE' => $columnData['SKU_CODE'],
  324. 'GOODS_TITLE' => Tool::mbSignConvert($columnData['GOODS_TITLE']) ,
  325. 'CONSIGNEE' => $columnData['CONSIGNEE'],
  326. 'MOBILE' => "\t{$columnData['MOBILE']}",
  327. 'TEL' => "\t{$columnData['TEL']}",
  328. 'PROVINCE' => $regionConfig[$columnData['PROVINCE']]['REGION_NAME'] ?? '',
  329. 'CITY' => $regionConfig[$columnData['CITY']]['REGION_NAME'] ?? '',
  330. 'COUNTY' => $regionConfig[$columnData['COUNTY']]['REGION_NAME'] ?? '',
  331. 'ADDRESS' => $columnData['ADDRESS'],
  332. 'PERIOD_NUM' => $columnData['PERIOD_NUM'],
  333. 'ORDER_TYPE' => ($columnData['ORDER_TYPE'] == 'ZC') ? 'Welcome pack' : (in_array($columnData['PAY_TYPE'], ['cash', 'pay_stack']) ? 'Repeat Purchase': 'Points'),
  334. // 'WAREHOUSE' => $columnData['WAREHOUSE'],
  335. 'CREATED_AT' => Date('Y-m-d H:i:s', $columnData['CREATED_AT']),
  336. 'PAY_TYPE' => ShopGoods::payTypes()[$columnData['PAY_TYPE']]['name'] ?? ShopGoods::payTypes()['cash']['name'],
  337. 'PAY_AT' => $columnData['PAY_AT'] > 0 ? Date('Y-m-d H:i:s', $columnData['PAY_AT']) : '',
  338. 'DELIVERY_AT' => $columnData['DELIVERY_AT'] > 0 ? Date('Y-m-d H:i:s', $columnData['DELIVERY_AT']) : '',
  339. 'BUY_NUMS' => $columnData['BUY_NUMS'],
  340. 'REAL_PRICE' => $columnData['REAL_PRICE'],
  341. 'TOTAL_PRICE' => $columnData['REAL_PRICE'] * $columnData['BUY_NUMS'],
  342. 'REAL_PV' => $columnData['REAL_PV'] * $columnData['BUY_NUMS'],
  343. 'STANDARD_PRICE' => $columnData['STANDARD_PRICE'] * $columnData['BUY_NUMS'],
  344. 'PAY_FREIGHT' => $columnData['PAY_FREIGHT'],
  345. 'TAX_RATE' => $columnData['TAX_RATE'],
  346. 'TAX_AMOUNT' => Tool::calculateTax($columnData['REAL_PRICE'], $columnData['TAX_RATE'], $columnData['BUY_NUMS']),
  347. 'EXPRESS_COMPANY' => $columnData['EXPRESS_COMPANY'],
  348. 'ORDER_TRACK_NO' => $columnData['ORDER_TRACK_NO'],
  349. 'EXPRESS_TYPE' => $columnData['EXPRESS_TYPE'] == 0 ? 'mailing ':' auto pick',
  350. 'FRONT_REMARK' => $columnData['FRONT_REMARK'],
  351. 'DELIVERY_STATUS_NAME' => \Yii::$app->params['deliveryStatus'][$columnData['DELIVERY_STATUS']]['label'] ?? '',
  352. 'IS_AUTO' => $columnData['IS_AUTO'] == 1 ? 'Yes' : 'No',
  353. ];
  354. fputcsv($this->_fp, Tool::arrTextConvert($columnAccept));
  355. unset($percent, $columnData, $columnAccept);
  356. }
  357. unset($list);
  358. }
  359. unset($list);
  360. return 'finish';
  361. }
  362. /**
  363. * 完成
  364. * @return bool
  365. * @throws Exception
  366. * @throws InvalidConfigException
  367. * @throws \yii\httpclient\Exception
  368. */
  369. public function complete() {
  370. $this->_updatePercent(100);
  371. if ($this->completed) {
  372. return true;
  373. }
  374. if ($this->_fp) {
  375. fclose($this->_fp);
  376. }
  377. // 把导出的文件上传至静态文件服务器
  378. if ($this->isRemote) {
  379. $realFile = $this->getSaveBasePath() . __DS__ . $this->getSavePath() . __DS__ . $this->getFileName();
  380. $remoteUploadApi = RemoteUploadApi::instance();
  381. if ($uploadResult = $remoteUploadApi->upload($realFile)) {
  382. Export::updateAll(['REMOTE_URL' => $uploadResult['url'], 'IS_EXPORTING' => 0, 'EXPORT_PERCENT' => 100, 'ENDED_AT' => Date::nowTime()], 'ID=:ID', [':ID' => $this->exportId]);
  383. } else {
  384. $this->_errorHandle();
  385. throw new Exception('文件远程上传失败');
  386. }
  387. // 删除本地临时文件
  388. unlink($realFile);
  389. } else {
  390. Export::updateAll(['IS_EXPORTING' => 0, 'EXPORT_PERCENT' => 100, 'ENDED_AT' => Date::nowTime()], 'ID=:ID', [':ID' => $this->exportId]);
  391. }
  392. \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin(100, ['MODEL' => 'EXPORT', 'ID' => $this->exportId, 'FIELD' => 'EXPORT_PERCENT']);
  393. CacheHelper::deleteAsyncParams($this->taskId);
  394. $this->completed = true;
  395. }
  396. /**
  397. * @param $message
  398. */
  399. private function _errorNotice($message) {
  400. Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($this->userId, $message, false);
  401. }
  402. /**
  403. * 首次更新
  404. * @param $exportId
  405. * @param $realName
  406. */
  407. private function _updateFirst($realName, $percent) {
  408. $fileName = str_replace($this->getSaveBasePath() . __DS__, '', $realName);
  409. Export::updateAll([
  410. 'FILE_NAME' => str_replace(__DS__, '/', $fileName),
  411. 'UPDATED_AT' => Date::nowTime(),
  412. 'EXPORT_PERCENT' => $percent,
  413. ], 'ID=:ID', [':ID' => $this->exportId]);
  414. }
  415. /**
  416. * 更新百分比
  417. * @param $exportId
  418. * @param $percent
  419. */
  420. private function _updatePercent($percent) {
  421. // 把数据写入数据库中
  422. Export::updateAll(['EXPORT_PERCENT' => $percent], 'ID=:ID', [':ID' => $this->exportId]);
  423. \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin($percent, ['MODEL' => 'EXPORT', 'ID' => $this->exportId, 'FIELD' => 'EXPORT_PERCENT']);
  424. }
  425. /**
  426. * 发生错误处理
  427. * @param $exportId
  428. */
  429. private function _errorHandle() {
  430. Export::updateAll(['IS_EXPORTING' => 0], 'ID=:ID', [':ID' => $this->exportId]);
  431. }
  432. /**
  433. * 导出逻辑
  434. * @param $filter
  435. * @param $listName
  436. * @param null $consoleRouter
  437. * @throws Exception
  438. */
  439. public function exportHandle($filter, $listName, $consoleRouter = null){
  440. $params = [
  441. 'moduleId' => $this->moduleId,
  442. 'listName' => $listName,
  443. 'action' => $consoleRouter ? $consoleRouter : Yii::$app->controller->id.'/'.Yii::$app->controller->action->id, // 这里这么写,是因为调用的异步路由和同步的控制器和方法是一样的,所以,只要不传默认调和同步一样的异步方法
  444. 'userId' => Yii::$app->user->id,
  445. ];
  446. $this->webToAsync($params,$filter);
  447. }
  448. /**
  449. * 页面到异步的请求
  450. * @param $params
  451. * @param array $extend
  452. * @return bool
  453. * @throws Exception
  454. */
  455. public function webToAsync($params, $extend = []) {
  456. $default = [
  457. 'moduleId' => null,
  458. 'listName' => null,
  459. 'remark' => null,
  460. 'action' => null,
  461. 'userId' => null,
  462. ];
  463. $params = Tool::deepParse($params, $default);
  464. if (!$params['moduleId'] || !$params['listName'] || !$params['action']) {
  465. throw new Exception('请设置moduleId,listName和action');
  466. }
  467. // 把文件对应的相关资料存入数据库中
  468. $export = new Export();
  469. $export->EXPORT_NAME = $params['listName'] . '_' . date('Exp_y-m-d H:i:s', Date::nowTime());
  470. $export->MODULE_NAME = $params['moduleId'];
  471. $export->REMARK = $params['remark'];
  472. $export->IS_EXPORTING = 1;
  473. $export->ADMIN_ID = \Yii::$app->user->id;
  474. $export->STARTED_AT = Date::nowTime();
  475. $export->CREATED_AT = Date::nowTime();
  476. $export->EXPORT_PERCENT = 0;
  477. $export->ENDED_AT = 0;
  478. $export->UPDATED_AT = 0;
  479. if (!$export->save()) {
  480. throw new Exception(Form::formatErrorsForApi($export->getErrors()));
  481. }
  482. $extend = array_merge($extend, [
  483. 'exportId' => $export->ID,
  484. 'userId' => $params['userId'],
  485. 'language' => Yii::$app->language,
  486. ]);
  487. // 异步处理添加任务
  488. $taskKey = \Yii::$app->swooleAsyncTimer->asyncHandle($params['action'], $extend);
  489. if($taskKey === false){
  490. // 删除刚刚添加的导出数据
  491. Export::deleteAll(['ID'=>$export->ID]);
  492. throw new Exception('导出失败,请求异步处理服务器失败');
  493. }
  494. return $taskKey;
  495. }
  496. /**
  497. * 生成
  498. * @return bool
  499. * @throws Exception
  500. * @throws InvalidConfigException
  501. * @throws \yii\httpclient\Exception
  502. */
  503. public function generateOrderExcel() {
  504. $this->getParams();
  505. if (!$this->params) {
  506. throw new Exception('无法获取需要的参数');
  507. }
  508. $path = $this->getSaveBasePath() . __DS__ . $this->getSavePath();
  509. $path = __DS__ . $path;
  510. $realFile = $this->mkdir($path) . __DS__ . $this->getFileName();
  511. $this->completed = false;
  512. $this->getExportId();
  513. $this->getUserId();
  514. $fileNameUpdated = false;
  515. $this->_fp = fopen($realFile, 'w');
  516. @exec('chown -R www:www /'.$realFile);
  517. @exec('chmod -R 777 /'.$realFile);
  518. // 获取列表数据及表头
  519. $this->_listModel = new $this->listModelClass();
  520. $this->_listModel->isExport = true;
  521. if(method_exists($this->_listModel, 'getExportHeaders')){
  522. if(method_exists($this->_listModel, 'exportPrepare')) {//导出数据提前设置参数
  523. $this->_listModel->exportPrepare(['condition' => $this->params['condition'], 'params' => $this->params['params'], 'others' => $this->params['others'] ?? [], 'page' => 0, 'pageSize' => 100000, 'userId' => $this->userId]);
  524. }
  525. $headers = $this->_listModel->getExportHeaders($this->userId);
  526. fputcsv($this->_fp, $headers);
  527. unset($headers);
  528. $this->_updateFirst($realFile, 1);
  529. } else {
  530. throw new Exception($this->listModelClass.'的getExportHeaders方法不存在');
  531. }
  532. $this->_loopWriteDataOrder();
  533. $this->complete();
  534. return true;
  535. }
  536. /**
  537. * 生成
  538. * @return bool
  539. * @throws Exception
  540. * @throws InvalidConfigException
  541. * @throws \yii\httpclient\Exception
  542. */
  543. public function generateOrderPDF() {
  544. $this->getParams();
  545. if (!$this->params) {
  546. throw new Exception('无法获取需要的参数');
  547. }
  548. $path = __DS__ . $this->getSaveBasePath() . __DS__ . $this->getSavePath();
  549. $realFile = $path . __DS__ . $this->getFileName('.pdf');
  550. $this->completed = false;
  551. $this->getExportId();
  552. $this->getUserId();
  553. $fileNameUpdated = false;
  554. // 获取列表数据及表头
  555. $this->_listModel = new $this->listModelClass();
  556. $this->_listModel->isExport = true;
  557. // 查询订单数据
  558. // $oderList = $this->_loopWriteDataPDF($realFile, $fileNameUpdated);
  559. $orderQuery = Order::find()
  560. ->alias('O')
  561. ->where($this->params['condition'], $this->params['params'])
  562. ->select('O.*,U.REAL_NAME,U.DEC_ID,SG.CATEGORY_TYPE,OG.REAL_PRICE,OG.TAX_RATE,OG.BUY_NUMS,OG.SKU_CODE,OG.GOODS_TITLE,OG.REAL_PV')
  563. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  564. ->join('LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  565. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  566. ->orderBy('O.CREATED_AT DESC');
  567. // 订单中间表只查询待支付和支付失败的订单
  568. $this->params['params'][':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  569. $this->params['params'][':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  570. $orderStandardQuery = ApproachOrder::find()
  571. ->alias('O')
  572. ->where($this->params['condition'] . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $this->params['params'])
  573. ->select('O.*,U.REAL_NAME,U.DEC_ID,SG.CATEGORY_TYPE,OG.REAL_PRICE,OG.TAX_RATE,OG.BUY_NUMS,OG.SKU_CODE,OG.GOODS_TITLE,OG.REAL_PV')
  574. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  575. ->join('LEFT JOIN', ApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  576. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  577. ->orderBy('O.CREATED_AT DESC');
  578. $queryAll = $orderQuery->union($orderStandardQuery, true);
  579. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  580. $oderList = $query->all();
  581. if ($oderList) {
  582. $userId = '';
  583. $userName = '';
  584. $address = '';
  585. $mobile = '';
  586. $orderAt = '';
  587. $orderDetails = '';
  588. $orderSn = '';
  589. $orderAmount = 0; // 合计总额
  590. $orderNums = 0; // 合计总数
  591. $totalTaxAmount = 0; // 合计税额
  592. $totalAmount = 0;
  593. foreach ($oderList as $key => $value) {
  594. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  595. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  596. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  597. $userId = $value['USER_NAME'];
  598. $userName = $value['REAL_NAME'];
  599. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  600. $mobile = $value['MOBILE'];
  601. $orderAt = Date::convert($value['CREATED_AT'],'Y-m-d H:i:s');
  602. $orderSn = $value['SN'];
  603. // 总价
  604. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  605. $orderAmount += $totalAmount;
  606. $orderNums += $value['BUY_NUMS'];
  607. // 税额
  608. $taxAmount = Tool::calculateTax($value['REAL_PRICE'], $value['TAX_RATE'], $value['BUY_NUMS']);
  609. $totalTaxAmount += $taxAmount;
  610. $taxAmount = Tool::formatAmount($taxAmount);
  611. $totalAmount = Tool::formatAmount($totalAmount);
  612. // 订单详情
  613. $orderDetails .= <<<EOT
  614. <tr>
  615. <td>{$value['SKU_CODE']}</td>
  616. <td>{$value['GOODS_TITLE']}</td>
  617. <td style="text-align: right;">{$value['REAL_PRICE']}</td>
  618. <td>{$value['BUY_NUMS']}</td>
  619. <td style="text-align: right;">{$value['TAX_RATE']}</td>
  620. <td style="text-align: right;">{$taxAmount}</td>
  621. <td style="text-align: right;">{$totalAmount}</td>
  622. </tr>
  623. EOT;
  624. }
  625. $memberCode = Yii::t('ctx', 'memberCode');
  626. $memberName = Yii::t('ctx', 'memberName');
  627. $memberAddress = Yii::t('ctx', 'memberAddress');
  628. $memberPhone = Yii::t('ctx', 'memberPhone');
  629. $orderCode = Yii::t('ctx', 'orderCode');
  630. $orderDetail = Yii::t('ctx', 'orderDetail');
  631. $productCode = Yii::t('ctx', 'productCode');
  632. $productName = Yii::t('ctx', 'productName');
  633. $productPrice = Yii::t('ctx', 'productPrice');
  634. $quantity = Yii::t('ctx', 'qty');
  635. $taxRate = Yii::t('ctx', 'taxRate');
  636. $totalTax = Yii::t('ctx', 'totalTax');
  637. $totalAmount = Yii::t('ctx', 'totalAmount');
  638. $total = Yii::t('ctx', 'total');
  639. $signature = Yii::t('ctx', 'signature');
  640. $date = Yii::t('ctx', 'date');
  641. $createAt = Yii::t('ctx', 'createAt');
  642. // 订单基本信息
  643. $orderBase = <<<ORDER
  644. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  645. <tr>
  646. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberCode}</td>
  647. <td width="70%">{$userId}</td>
  648. </tr>
  649. <tr>
  650. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberName}</td>
  651. <td width="70%">{$userName}</td>
  652. </tr>
  653. <tr>
  654. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberAddress}</td>
  655. <td width="70%">{$address}</td>
  656. </tr>
  657. <tr>
  658. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberPhone}</td>
  659. <td width="70%">{$mobile}</td>
  660. </tr>
  661. <tr>
  662. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$orderCode}</td>
  663. <td width="70%">{$orderSn}</td>
  664. </tr>
  665. <tr>
  666. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$createAt}</td>
  667. <td width="70%">{$orderAt}</td>
  668. </tr>
  669. <tr>
  670. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">{$orderDetail}</td>
  671. <td class="bg"></td>
  672. </tr>
  673. </table>
  674. ORDER;
  675. $l['a_meta_charset'] = 'UTF-8';
  676. $l['a_meta_dir'] = 'ltr';
  677. $l['a_meta_language'] = 'zh';
  678. $l['w_page'] = '页面';
  679. $orderAmount = Tool::formatAmount($orderAmount);
  680. $totalTaxAmount = Tool::formatAmount($totalTaxAmount);
  681. $context = <<<ORDER
  682. <!doctype html>
  683. <html lang="en">
  684. <head>
  685. <meta charset="UTF-8" />
  686. <title>{$orderDetail}</title>
  687. <style>
  688. table {
  689. border-collapse: collapse;
  690. }
  691. table td, table th {
  692. border: 1px solid #ccc;
  693. /*padding: 5px 5px;*/
  694. border-collapse: collapse;
  695. }
  696. /*td {*/
  697. /* padding: 120px;*/
  698. /*}*/
  699. .bg {
  700. background-color: #ccc;
  701. }
  702. </style>
  703. </head>
  704. <body>
  705. <div class="content">
  706. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>{$orderDetail}</b><br></p>
  707. <div>
  708. <div style="display: block; width: 100%;">
  709. {$orderBase}
  710. <table border="1" width="100%" style="padding: 10px 5px; text-align: center;">
  711. <tr>
  712. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productCode}</th>
  713. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productName}</th>
  714. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productPrice}</th>
  715. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">{$quantity}</th>
  716. <th width="12%" style="font-size: 14px; font-weight: bold; text-align: center;">{$taxRate}</th>
  717. <th width="13%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalTax}</th>
  718. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalAmount}</th>
  719. </tr>
  720. {$orderDetails}
  721. <tr>
  722. <td colspan="3">{$total}</td>
  723. <td style="text-align: right;">{$orderNums}</td>
  724. <td></td>
  725. <td style="text-align: right;">{$totalTaxAmount}</td>
  726. <td style="text-align: right;">{$orderAmount}</td>
  727. </tr>
  728. </table>
  729. </div>
  730. <div style="width: 100%; margin-top: 50px; height: 30px;">
  731. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  732. <tr style="border: none;">
  733. <td width="70%" style="border: none;"></td>
  734. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$signature}:</td>
  735. </tr>
  736. <tr style="border: none;">
  737. <td width="70%" style="border: none;"></td>
  738. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$date}:</td>
  739. </tr>
  740. </table>
  741. </div>
  742. </div>
  743. </div>
  744. </body>
  745. </html>
  746. ORDER;
  747. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  748. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  749. // 设置打印模式
  750. $pdf->SetCreator(PDF_CREATOR);
  751. $pdf->SetAuthor('DaZe');
  752. $pdf->SetTitle($orderSn);
  753. $pdf->SetSubject('TCPDF Tutorial');
  754. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  755. // 是否显示页眉
  756. $pdf->setPrintHeader(false);
  757. // 设置页眉字体
  758. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  759. // 页眉距离顶部的距离
  760. $pdf->SetHeaderMargin('5');
  761. // 是否显示页脚
  762. $pdf->setPrintFooter(false);
  763. // 设置默认等宽字体
  764. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  765. // 设置行高
  766. $pdf->setCellHeightRatio(1);
  767. // 设置左、上、右的间距
  768. $pdf->SetMargins('10', '0', '10');
  769. // 设置是否自动分页 距离底部多少距离时分页
  770. $pdf->SetAutoPageBreak(TRUE, '15');
  771. // 设置图像比例因子
  772. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  773. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  774. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  775. $pdf->setLanguageArray($l);
  776. }
  777. $pdf->setFontSubsetting(true);
  778. $pdf->AddPage();
  779. // 设置字体
  780. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  781. $image = file_get_contents(\Yii::$app->basePath . '/../frontendEle/src/static/img/ngds-logo.jpg');
  782. $pdf->Image('@' . $image, 15, 12, 20, 7, 'JPG');
  783. $pdf->writeHTML($context);
  784. $pdf->Output($realFile, 'F');
  785. $this->_updateFirst($realFile, 1);
  786. }
  787. $this->complete();
  788. return true;
  789. }
  790. /**
  791. * 生成
  792. * @return bool
  793. * @throws Exception
  794. * @throws InvalidConfigException
  795. * @throws \yii\httpclient\Exception
  796. */
  797. public function generateDecOrderPDF() {
  798. $this->getParams();
  799. if (!$this->params) {
  800. throw new Exception('无法获取需要的参数');
  801. }
  802. $path = __DS__ . $this->getSaveBasePath() . __DS__ . $this->getSavePath();
  803. $realFile = $path . __DS__ . $this->getFileName('.pdf');
  804. $this->completed = false;
  805. $this->getExportId();
  806. $this->getUserId();
  807. $fileNameUpdated = false;
  808. // 获取列表数据及表头
  809. $this->_listModel = new $this->listModelClass();
  810. $this->_listModel->isExport = true;
  811. // 查询订单数据
  812. // $oderList = $this->_loopWriteDataPDF($realFile, $fileNameUpdated);
  813. $orderQuery = Order::find()
  814. ->alias('O')
  815. ->where($this->params['condition'], $this->params['params'])
  816. ->select('O.*,U.REAL_NAME,U.DEC_ID,SG.CATEGORY_TYPE,OG.REAL_PRICE,OG.TAX_RATE,OG.BUY_NUMS,OG.SKU_CODE,OG.GOODS_TITLE,OG.REAL_PV')
  817. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  818. ->join('LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  819. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  820. ->orderBy('O.CREATED_AT DESC');
  821. // 订单中间表只查询待支付和支付失败的订单
  822. $this->params['params'][':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  823. $this->params['params'][':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  824. $orderStandardQuery = ApproachOrder::find()
  825. ->alias('O')
  826. ->where($this->params['condition'] . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $this->params['params'])
  827. ->select('O.*,U.REAL_NAME,U.DEC_ID,SG.CATEGORY_TYPE,OG.REAL_PRICE,OG.TAX_RATE,OG.BUY_NUMS,OG.SKU_CODE,OG.GOODS_TITLE,OG.REAL_PV')
  828. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  829. ->join('LEFT JOIN', ApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  830. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  831. ->orderBy('O.CREATED_AT DESC');
  832. $queryAll = $orderQuery->union($orderStandardQuery, true);
  833. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  834. $oderList = $query->all();
  835. if ($oderList) {
  836. $userId = '';
  837. $userName = '';
  838. $address = '';
  839. $mobile = '';
  840. $orderAt = '';
  841. $orderDetails = '';
  842. $orderSn = '';
  843. $orderAmount = 0; // 合计总额
  844. $orderNums = 0; // 合计总数
  845. $totalTaxAmount = 0; // 合计税额
  846. $totalAmount = 0;
  847. foreach ($oderList as $key => $value) {
  848. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  849. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  850. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  851. $userId = $value['USER_NAME'];
  852. $userName = $value['REAL_NAME'];
  853. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  854. $mobile = $value['MOBILE'];
  855. $orderAt = Date::convert($value['CREATED_AT'],'Y-m-d H:i:s');
  856. $orderSn = $value['SN'];
  857. // 总价
  858. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  859. $orderAmount += $totalAmount;
  860. $orderNums += $value['BUY_NUMS'];
  861. // 税额
  862. $taxAmount = Tool::calculateTax($value['REAL_PRICE'], $value['TAX_RATE'], $value['BUY_NUMS']);
  863. $totalTaxAmount += $taxAmount;
  864. $taxAmount = Tool::formatAmount($taxAmount);
  865. $totalAmount = Tool::formatAmount($totalAmount);
  866. // 订单详情
  867. $orderDetails .= <<<EOT
  868. <tr>
  869. <td>{$value['SKU_CODE']}</td>
  870. <td>{$value['GOODS_TITLE']}</td>
  871. <td style="text-align: right;">{$value['REAL_PRICE']}</td>
  872. <td>{$value['BUY_NUMS']}</td>
  873. <td style="text-align: right;">{$value['TAX_RATE']}</td>
  874. <td style="text-align: right;">{$taxAmount}</td>
  875. <td style="text-align: right;">{$totalAmount}</td>
  876. </tr>
  877. EOT;
  878. }
  879. $memberCode = Yii::t('app', 'memberCode');
  880. $memberName = Yii::t('app', 'memberName');
  881. $memberAddress = Yii::t('app', 'memberAddress');
  882. $memberPhone = Yii::t('app', 'memberPhone');
  883. $orderCode = Yii::t('app', 'orderCode');
  884. $orderDetail = Yii::t('app', 'orderDetail');
  885. $productCode = Yii::t('app', 'productCode');
  886. $productName = Yii::t('app', 'productName');
  887. $productPrice = Yii::t('app', 'productPrice');
  888. $quantity = Yii::t('app', 'qty');
  889. $taxRate = Yii::t('app', 'taxRate');
  890. $totalTax = Yii::t('app', 'totalTax');
  891. $totalAmount = Yii::t('app', 'totalAmount');
  892. $total = Yii::t('app', 'total');
  893. $signature = Yii::t('app', 'signature');
  894. $date = Yii::t('app', 'date');
  895. $createAt = Yii::t('app', 'createAt');
  896. // 订单基本信息
  897. $orderBase = <<<ORDER
  898. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  899. <tr>
  900. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberCode}</td>
  901. <td width="70%">{$userId}</td>
  902. </tr>
  903. <tr>
  904. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberName}</td>
  905. <td width="70%">{$userName}</td>
  906. </tr>
  907. <tr>
  908. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberAddress}</td>
  909. <td width="70%">{$address}</td>
  910. </tr>
  911. <tr>
  912. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberPhone}</td>
  913. <td width="70%">{$mobile}</td>
  914. </tr>
  915. <tr>
  916. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$orderCode}</td>
  917. <td width="70%">{$orderSn}</td>
  918. </tr>
  919. <tr>
  920. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$createAt}</td>
  921. <td width="70%">{$orderAt}</td>
  922. </tr>
  923. <tr>
  924. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">{$orderDetail}</td>
  925. <td class="bg"></td>
  926. </tr>
  927. </table>
  928. ORDER;
  929. $l['a_meta_charset'] = 'UTF-8';
  930. $l['a_meta_dir'] = 'ltr';
  931. $l['a_meta_language'] = 'zh';
  932. $l['w_page'] = '页面';
  933. $orderAmount = sprintf('%.2f', $orderAmount);
  934. $totalTaxAmount = sprintf('%.2f', $totalTaxAmount);
  935. $context = <<<ORDER
  936. <!doctype html>
  937. <html lang="en">
  938. <head>
  939. <meta charset="UTF-8" />
  940. <title>{$orderDetail}</title>
  941. <style>
  942. table {
  943. border-collapse: collapse;
  944. }
  945. table td, table th {
  946. border: 1px solid #ccc;
  947. /*padding: 5px 5px;*/
  948. border-collapse: collapse;
  949. }
  950. /*td {*/
  951. /* padding: 120px;*/
  952. /*}*/
  953. .bg {
  954. background-color: #ccc;
  955. }
  956. </style>
  957. </head>
  958. <body>
  959. <div class="content">
  960. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>{$orderDetail}</b><br></p>
  961. <div>
  962. <div style="display: block; width: 100%;">
  963. {$orderBase}
  964. <table border="1" width="100%" style="padding: 10px 5px; text-align: center;">
  965. <tr>
  966. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productCode}</th>
  967. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productName}</th>
  968. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productPrice}</th>
  969. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">{$quantity}</th>
  970. <th width="12%" style="font-size: 14px; font-weight: bold; text-align: center;">{$taxRate}</th>
  971. <th width="13%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalTax}</th>
  972. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalAmount}</th>
  973. </tr>
  974. {$orderDetails}
  975. <tr>
  976. <td colspan="3">{$total}</td>
  977. <td style="text-align: right;">{$orderNums}</td>
  978. <td></td>
  979. <td style="text-align: right;">{$totalTaxAmount}</td>
  980. <td style="text-align: right;">{$orderAmount}</td>
  981. </tr>
  982. </table>
  983. </div>
  984. <div style="width: 100%; margin-top: 50px; height: 30px;">
  985. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  986. <tr style="border: none;">
  987. <td width="70%" style="border: none;"></td>
  988. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$signature}:</td>
  989. </tr>
  990. <tr style="border: none;">
  991. <td width="70%" style="border: none;"></td>
  992. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$date}:</td>
  993. </tr>
  994. </table>
  995. </div>
  996. </div>
  997. </div>
  998. </body>
  999. </html>
  1000. ORDER;
  1001. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  1002. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  1003. // 设置打印模式
  1004. $pdf->SetCreator(PDF_CREATOR);
  1005. $pdf->SetAuthor('DaZe');
  1006. $pdf->SetTitle($orderSn);
  1007. $pdf->SetSubject('TCPDF Tutorial');
  1008. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  1009. // 是否显示页眉
  1010. $pdf->setPrintHeader(false);
  1011. // 设置页眉字体
  1012. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  1013. // 页眉距离顶部的距离
  1014. $pdf->SetHeaderMargin('5');
  1015. // 是否显示页脚
  1016. $pdf->setPrintFooter(false);
  1017. // 设置默认等宽字体
  1018. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  1019. // 设置行高
  1020. $pdf->setCellHeightRatio(1);
  1021. // 设置左、上、右的间距
  1022. $pdf->SetMargins('10', '10', '10');
  1023. // 设置是否自动分页 距离底部多少距离时分页
  1024. $pdf->SetAutoPageBreak(TRUE, '15');
  1025. // 设置图像比例因子
  1026. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  1027. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  1028. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  1029. $pdf->setLanguageArray($l);
  1030. }
  1031. $pdf->setFontSubsetting(true);
  1032. $pdf->AddPage();
  1033. // 设置字体
  1034. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  1035. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  1036. $image = file_get_contents(\Yii::$app->basePath . '/../frontendEle/src/static/img/ngds-logo.jpg');
  1037. $pdf->Image('@' . $image, 165, 5, 20, 10, 'JPG');
  1038. $pdf->writeHTML($context);
  1039. $pdf->Output($realFile, 'F');
  1040. $this->_updateFirst($realFile, 1);
  1041. }
  1042. $this->complete();
  1043. return true;
  1044. }
  1045. /**
  1046. * 循环写入数据
  1047. * @param $realFile
  1048. * @param $fileNameUpdated
  1049. * @param int $page
  1050. * @param int $counter
  1051. * @return array
  1052. * @throws Exception
  1053. */
  1054. private function _loopWriteDataPDF($realFile, &$fileNameUpdated, $page = 0, $counter = 0){
  1055. if(method_exists($this->_listModel, 'getList')){
  1056. $list = $this->_listModel->getList(['condition'=>$this->params['condition'], 'params'=>isset($this->params['params']) ? $this->params['params'] : [], 'others'=>isset($this->params['others']) ? $this->params['others'] : [], 'page'=>$page, 'pageSize'=>$this->pageSize, 'userId'=>$this->userId]);
  1057. } else {
  1058. throw new Exception($this->listModelClass.'的getList方法不存在');
  1059. }
  1060. return $list['list'];
  1061. }
  1062. /**
  1063. * 生成
  1064. * @return bool
  1065. * @throws Exception
  1066. * @throws InvalidConfigException
  1067. * @throws \yii\httpclient\Exception
  1068. */
  1069. public function generateBaOrderExcel() {
  1070. $this->getParams();
  1071. if (!$this->params) {
  1072. throw new Exception('无法获取需要的参数');
  1073. }
  1074. $path = $this->getSaveBasePath() . __DS__ . $this->getSavePath();
  1075. $path = __DS__ . $path;
  1076. $realFile = $this->mkdir($path) . __DS__ . $this->getFileName();
  1077. $this->completed = false;
  1078. $this->getExportId();
  1079. $this->getUserId();
  1080. $this->_fp = fopen($realFile, 'w');
  1081. @exec('chown -R www:www /'.$realFile);
  1082. @exec('chmod -R 777 /'.$realFile);
  1083. // 获取列表数据及表头
  1084. $this->_listModel = new $this->listModelClass();
  1085. $this->_listModel->isExport = true;
  1086. if(method_exists($this->_listModel, 'getExportHeaders')){
  1087. if(method_exists($this->_listModel, 'exportPrepare')) {//导出数据提前设置参数
  1088. $this->_listModel->exportPrepare(['condition' => $this->params['condition'], 'params' => $this->params['params'], 'others' => $this->params['others'] ?? [], 'page' => 0, 'pageSize' => 100000, 'userId' => $this->userId]);
  1089. }
  1090. $headers = $this->_listModel->getExportHeaders($this->userId);
  1091. fputcsv($this->_fp, $headers);
  1092. unset($headers);
  1093. $this->_updateFirst($realFile, 1);
  1094. } else {
  1095. throw new Exception($this->listModelClass.'的getExportHeaders方法不存在');
  1096. }
  1097. $this->_loopWriteDataBaOrder();
  1098. $this->complete();
  1099. return true;
  1100. }
  1101. /**
  1102. * 循环写入数据
  1103. */
  1104. private function _loopWriteDataBaOrder()
  1105. {
  1106. $orderQuery = BaOrder::find()
  1107. ->alias('O')
  1108. ->where($this->params['condition'], $this->params['params'])
  1109. ->select('O.*,U.REAL_NAME,U.DEC_ID,SG.CATEGORY_TYPE,OG.REAL_PRICE,OG.TAX_RATE,OG.BUY_NUMS,OG.SKU_CODE,OG.GOODS_TITLE,OG.REAL_PV')
  1110. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'U.ID=O.USER_ID')
  1111. ->join('LEFT JOIN', BaOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  1112. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  1113. ->orderBy('O.CREATED_AT DESC');
  1114. // 订单中间表只查询待支付和支付失败的订单
  1115. $this->params['params'][':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  1116. $this->params['params'][':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  1117. $orderStandardQuery = BaApproachOrder::find()
  1118. ->alias('O')
  1119. ->where($this->params['condition'] . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $this->params['params'])
  1120. ->select('O.*,U.REAL_NAME,U.DEC_ID,SG.CATEGORY_TYPE,OG.REAL_PRICE,OG.TAX_RATE,OG.BUY_NUMS,OG.SKU_CODE,OG.GOODS_TITLE,OG.REAL_PV')
  1121. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'U.ID=O.USER_ID')
  1122. ->join('LEFT JOIN', BaApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  1123. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  1124. ->orderBy('O.CREATED_AT DESC');
  1125. $queryAll = $orderQuery->union($orderStandardQuery, true);
  1126. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  1127. $lists = $query->all();
  1128. if(!empty($lists)){
  1129. $regionConfig = Cache::getRegionConfig();
  1130. foreach($lists as $columnData) {
  1131. $CREATE_USER_ID = Info::getBaUserIdByUserName($columnData['CREATE_USER']);
  1132. $createUserName = Info::getBaUserRealNameByUserId($CREATE_USER_ID);
  1133. $decUserName = Info::getBaUserNameByUserId($columnData['DEC_ID']);
  1134. $columnAccept = [
  1135. 'USER_NAME' => $columnData['USER_NAME'],
  1136. 'DEC_USER_NAME' => $decUserName,
  1137. 'CREATE_USER' => $columnData['CREATE_USER'],
  1138. 'CREATE_USER_NAME' => $createUserName,
  1139. 'SN' => $columnData['SN'],
  1140. 'STATUS' => \Yii::$app->params['orderStatus'][$columnData['STATUS']]['label'] ?? '',
  1141. 'SKU_CODE' => $columnData['SKU_CODE'],
  1142. 'GOODS_TITLE' => $columnData['GOODS_TITLE'],
  1143. 'BUY_NUMS' => $columnData['BUY_NUMS'],
  1144. 'CONSIGNEE' => $columnData['CONSIGNEE'],
  1145. 'MOBILE' => "\t{$columnData['MOBILE']}",
  1146. 'TEL' => "\t{$columnData['TEL']}",
  1147. 'PROVINCE' => $regionConfig[$columnData['PROVINCE']]['REGION_NAME'] ?? '',
  1148. 'CITY' => $regionConfig[$columnData['CITY']]['REGION_NAME'] ?? '',
  1149. 'COUNTY' => $regionConfig[$columnData['COUNTY']]['REGION_NAME'] ?? '',
  1150. 'ADDRESS' => $columnData['ADDRESS'],
  1151. 'PERIOD_NUM' => $columnData['PERIOD_NUM'],
  1152. 'ORDER_TYPE' => ($columnData['ORDER_TYPE'] == 'ZC') ? 'Welcome pack' : (in_array($columnData['PAY_TYPE'], ['cash', 'pay_stack']) ? 'Reselling': 'Points'),
  1153. 'CREATED_AT' => Date('Y-m-d H:i:s', $columnData['CREATED_AT']),
  1154. 'PAY_TYPE' => ShopGoods::payTypes()[$columnData['PAY_TYPE']]['name'] ?? ShopGoods::payTypes()['cash']['name'],
  1155. 'PAY_AT' => $columnData['PAY_AT'] > 0 ? Date('Y-m-d H:i:s', $columnData['PAY_AT']) : '',
  1156. 'DELIVERY_AT' => $columnData['DELIVERY_AT'] > 0 ? Date('Y-m-d H:i:s', $columnData['DELIVERY_AT']) : '',
  1157. 'REAL_PRICE' => $columnData['REAL_PRICE'] ?? 0,
  1158. 'REAL_PV' => $columnData['REAL_PV'] ?? 0,
  1159. 'PAY_FREIGHT' => $columnData['PAY_FREIGHT'] ?? 0,
  1160. 'TAX_RATE' => $columnData['TAX_RATE'],
  1161. 'TAX_AMOUNT' => Tool::calculateTax($columnData['REAL_PRICE'], $columnData['TAX_RATE'], $columnData['BUY_NUMS']),
  1162. 'EXPRESS_COMPANY' => $columnData['EXPRESS_COMPANY'],
  1163. 'ORDER_TRACK_NO' => $columnData['ORDER_TRACK_NO'],
  1164. 'EXPRESS_TYPE' => $columnData['EXPRESS_TYPE'] == 0 ? 'mailing ':' auto pick',
  1165. 'FRONT_REMARK' => $columnData['FRONT_REMARK'],
  1166. 'DELIVERY_STATUS_NAME' => \Yii::$app->params['deliveryStatus'][$columnData['DELIVERY_STATUS']]['label'] ?? '',
  1167. ];
  1168. fputcsv($this->_fp, Tool::arrTextConvert($columnAccept));
  1169. unset($percent, $columnData, $columnAccept);
  1170. }
  1171. unset($list);
  1172. }
  1173. unset($list);
  1174. return 'finish';
  1175. }
  1176. /**
  1177. * 生成
  1178. * @return bool
  1179. * @throws Exception
  1180. * @throws InvalidConfigException
  1181. * @throws \yii\httpclient\Exception
  1182. */
  1183. public function generateBaOrderPDF()
  1184. {
  1185. $this->getParams();
  1186. if (!$this->params) {
  1187. throw new Exception('无法获取需要的参数');
  1188. }
  1189. $path = __DS__ . $this->getSaveBasePath() . __DS__ . $this->getSavePath();
  1190. $realFile = $path . __DS__ . $this->getFileName('.pdf');
  1191. $this->completed = false;
  1192. $this->getExportId();
  1193. $this->getUserId();
  1194. $fileNameUpdated = false;
  1195. // 获取列表数据及表头
  1196. $this->_listModel = new $this->listModelClass();
  1197. $this->_listModel->isExport = true;
  1198. // 查询订单数据
  1199. $orderQuery = BaOrder::find()
  1200. ->alias('O')
  1201. ->where($this->params['condition'], $this->params['params'])
  1202. ->select('O.*,U.REAL_NAME,U.DEC_ID,SG.CATEGORY_TYPE,OG.REAL_PRICE,OG.TAX_RATE,OG.BUY_NUMS,OG.SKU_CODE,OG.GOODS_TITLE,OG.REAL_PV')
  1203. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'U.ID=O.USER_ID')
  1204. ->join('LEFT JOIN', BaOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  1205. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  1206. ->orderBy('O.CREATED_AT DESC');
  1207. // 订单中间表只查询待支付和支付失败的订单
  1208. $this->params['params'][':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  1209. $this->params['params'][':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  1210. $orderStandardQuery = BaApproachOrder::find()
  1211. ->alias('O')
  1212. ->where($this->params['condition'] . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $this->params['params'])
  1213. ->select('O.*,U.REAL_NAME,U.DEC_ID,SG.CATEGORY_TYPE,OG.REAL_PRICE,OG.TAX_RATE,OG.BUY_NUMS,OG.SKU_CODE,OG.GOODS_TITLE,OG.REAL_PV')
  1214. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'U.ID=O.USER_ID')
  1215. ->join('LEFT JOIN', BaApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  1216. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  1217. ->orderBy('O.CREATED_AT DESC');
  1218. $queryAll = $orderQuery->union($orderStandardQuery, true);
  1219. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  1220. $oderList = $query->all();
  1221. if ($oderList) {
  1222. $userId = '';
  1223. $userName = '';
  1224. $address = '';
  1225. $mobile = '';
  1226. $orderAt = '';
  1227. $orderDetails = '';
  1228. $orderSn = '';
  1229. $orderAmount = 0; // 合计总额
  1230. $orderNums = 0; // 合计总数
  1231. $totalTaxAmount = 0; // 合计税额
  1232. $totalAmount = 0;
  1233. foreach ($oderList as $key => $value) {
  1234. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  1235. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  1236. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  1237. $userId = $value['USER_NAME'];
  1238. $userName = $value['REAL_NAME'];
  1239. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  1240. $mobile = $value['MOBILE'];
  1241. $orderAt = Date::convert($value['CREATED_AT'],'Y-m-d H:i:s');
  1242. $orderSn = $value['SN'];
  1243. // 总价
  1244. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  1245. $orderAmount += $totalAmount;
  1246. $orderNums += $value['BUY_NUMS'];
  1247. // 税额
  1248. $taxAmount = Tool::calculateTax($value['REAL_PRICE'], $value['TAX_RATE'], $value['BUY_NUMS']);
  1249. $totalTaxAmount += $taxAmount;
  1250. $taxAmount = Tool::formatAmount($taxAmount);
  1251. $totalAmount = Tool::formatAmount($totalAmount);
  1252. // 订单详情
  1253. $orderDetails .= <<<EOT
  1254. <tr>
  1255. <td>{$value['SKU_CODE']}</td>
  1256. <td>{$value['GOODS_TITLE']}</td>
  1257. <td style="text-align: right;">{$value['REAL_PRICE']}</td>
  1258. <td>{$value['BUY_NUMS']}</td>
  1259. <td style="text-align: right;">{$value['TAX_RATE']}</td>
  1260. <td style="text-align: right;">{$taxAmount}</td>
  1261. <td style="text-align: right;">{$totalAmount}</td>
  1262. </tr>
  1263. EOT;
  1264. }
  1265. // 订单基本信息
  1266. $orderBase = <<<ORDER
  1267. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  1268. <tr>
  1269. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">Member Code</td>
  1270. <td width="70%">{$userId}</td>
  1271. </tr>
  1272. <tr>
  1273. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">Member Name</td>
  1274. <td width="70%">{$userName}</td>
  1275. </tr>
  1276. <tr>
  1277. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">Member Address</td>
  1278. <td width="70%">{$address}</td>
  1279. </tr>
  1280. <tr>
  1281. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">Member Phone</td>
  1282. <td width="70%">{$mobile}</td>
  1283. </tr>
  1284. <tr>
  1285. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">Order Code</td>
  1286. <td width="70%">{$orderSn}</td>
  1287. </tr>
  1288. <tr>
  1289. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">Creation Time</td>
  1290. <td width="70%">{$orderAt}</td>
  1291. </tr>
  1292. <tr>
  1293. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">Order Detail</td>
  1294. <td class="bg"></td>
  1295. </tr>
  1296. </table>
  1297. ORDER;
  1298. $l['a_meta_charset'] = 'UTF-8';
  1299. $l['a_meta_dir'] = 'ltr';
  1300. $l['a_meta_language'] = 'zh';
  1301. $l['w_page'] = '页面';
  1302. $orderAmount = Tool::formatAmount($orderAmount);
  1303. $totalTaxAmount = Tool::formatAmount($totalTaxAmount);
  1304. $context = <<<ORDER
  1305. <!doctype html>
  1306. <html lang="en">
  1307. <head>
  1308. <meta charset="UTF-8" />
  1309. <title>Order detail</title>
  1310. <style>
  1311. table {
  1312. border-collapse: collapse;
  1313. }
  1314. table td, table th {
  1315. border: 1px solid #ccc;
  1316. padding: 5px 5px;
  1317. border-collapse: collapse;
  1318. }
  1319. /*td {*/
  1320. /* padding: 120px;*/
  1321. /*}*/
  1322. .bg {
  1323. background-color: #ccc;
  1324. }
  1325. </style>
  1326. </head>
  1327. <body>
  1328. <div class="content">
  1329. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>Order detail</b><br></p>
  1330. <div>
  1331. <div style="display: block; width: 100%;">
  1332. {$orderBase}
  1333. <table border="1" width="100%" style="padding: 10px 5px; text-align: center;">
  1334. <tr>
  1335. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">Product Code</th>
  1336. <th width="25%" style="font-size: 14px; font-weight: bold; text-align: center;">Product Name</th>
  1337. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">Product Price</th>
  1338. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">Qty</th>
  1339. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">Tax Rate</th>
  1340. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">Tax</th>
  1341. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">Total Amount</th>
  1342. </tr>
  1343. {$orderDetails}
  1344. <tr>
  1345. <td colspan="3">Total</td>
  1346. <td>{$orderNums}</td>
  1347. <td></td>
  1348. <td style="text-align: right;">{$totalTaxAmount}</td>
  1349. <td style="text-align: right;">{$orderAmount}</td>
  1350. </tr>
  1351. </table>
  1352. </div>
  1353. <div style="width: 100%; margin-top: 50px; height: 30px;">
  1354. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  1355. <tr style="border: none;">
  1356. <td width="70%" style="border: none;"></td>
  1357. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">Signature:</td>
  1358. </tr>
  1359. <tr style="border: none;">
  1360. <td width="70%" style="border: none;"></td>
  1361. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">Date:</td>
  1362. </tr>
  1363. </table>
  1364. </div>
  1365. </div>
  1366. </div>
  1367. </body>
  1368. </html>
  1369. ORDER;
  1370. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  1371. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  1372. // 设置打印模式
  1373. $pdf->SetCreator(PDF_CREATOR);
  1374. $pdf->SetAuthor('DaZe');
  1375. $pdf->SetTitle($orderSn);
  1376. $pdf->SetSubject('TCPDF Tutorial');
  1377. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  1378. // 是否显示页眉
  1379. $pdf->setPrintHeader(false);
  1380. // 设置页眉字体
  1381. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  1382. // 页眉距离顶部的距离
  1383. $pdf->SetHeaderMargin('5');
  1384. // 是否显示页脚
  1385. $pdf->setPrintFooter(false);
  1386. // 设置默认等宽字体
  1387. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  1388. // 设置行高
  1389. $pdf->setCellHeightRatio(1);
  1390. // 设置左、上、右的间距
  1391. $pdf->SetMargins('10', '0', '10');
  1392. // 设置是否自动分页 距离底部多少距离时分页
  1393. $pdf->SetAutoPageBreak(TRUE, '15');
  1394. // 设置图像比例因子
  1395. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  1396. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  1397. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  1398. $pdf->setLanguageArray($l);
  1399. }
  1400. $pdf->setFontSubsetting(true);
  1401. $pdf->AddPage();
  1402. // 设置字体
  1403. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  1404. $image = file_get_contents(\Yii::$app->basePath . '/../frontendEle/src/static/img/ngds-logo.jpg');
  1405. $pdf->Image('@' . $image, 15, 12, 20, 7, 'JPG');
  1406. $pdf->writeHTML($context);
  1407. $pdf->Output($realFile, 'F');
  1408. $this->_updateFirst($realFile, 1);
  1409. }
  1410. $this->complete();
  1411. return true;
  1412. }
  1413. }