BaseExport.php 67 KB

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