BaseExport.php 92 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015
  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\Currency;
  20. use common\models\Export;
  21. use common\models\Order;
  22. use common\models\OrderGoods;
  23. use common\models\Region;
  24. use common\models\ShopGoods;
  25. use common\models\User;
  26. use console\helpers\Logger;
  27. use Yii;
  28. use yii\base\Exception;
  29. use yii\base\InvalidConfigException;
  30. use yii\base\StaticInstanceTrait;
  31. use yii\base\Component;
  32. use yii\db\Query;
  33. class BaseExport extends Component {
  34. use StaticInstanceTrait;
  35. public $moduleId;
  36. /**
  37. * @var string the model class name. This property must be set.
  38. */
  39. public $modelClass;
  40. /**
  41. * @var
  42. */
  43. public $listModelClass;
  44. /**
  45. * @var int
  46. */
  47. public $pageSize = 100;
  48. /**
  49. * @var int
  50. */
  51. public $totalCount = 0;
  52. /**
  53. * @var int
  54. */
  55. public $offset = 0;
  56. /**
  57. * @var int
  58. */
  59. public $limit = 100;
  60. /**
  61. * csv文件名称
  62. * @var null
  63. */
  64. public $fileName = null;
  65. /**
  66. * 保存的基本路径
  67. * @var null
  68. */
  69. public $saveBasePath = null;
  70. /**
  71. * 保存路径
  72. * @var null
  73. */
  74. public $savePath = null;
  75. /**
  76. * 正在导出的id
  77. * 来源于exporting_file表
  78. * @var null
  79. */
  80. public $exportId = null;
  81. /**
  82. * 操作人的id
  83. * @var null
  84. */
  85. public $userId = null;
  86. /**
  87. * 任务id
  88. * @var null
  89. */
  90. public $taskId = null;
  91. /**
  92. * 是否完成
  93. * @var bool
  94. */
  95. public $completed = false;
  96. /**
  97. * @var array
  98. */
  99. public $params = [];
  100. /**
  101. * 文件句柄
  102. * @var null
  103. */
  104. private $_fp = null;
  105. /**
  106. * 是否上传远程
  107. * @var bool
  108. */
  109. public $isRemote = true;
  110. /**
  111. * @var DataList
  112. */
  113. private $_listModel;
  114. public static function factory($taskId) {
  115. return new self([
  116. 'taskId' => $taskId
  117. ]
  118. );
  119. }
  120. /**
  121. *
  122. */
  123. public function init() {
  124. parent::init();
  125. $this->isRemote = \Yii::$app->params['isRemoteUpload'];
  126. }
  127. /**
  128. * 生成复杂的路径
  129. * @return string
  130. */
  131. public function pathCreator(...$params) {
  132. $hash = md5(implode('_', $params));
  133. $first = substr($hash, 0, 3);
  134. $second = substr($hash, 3, 2);
  135. $dir = $first . __DS__ . $second . __DS__;
  136. return $dir;
  137. }
  138. /**
  139. * 获取文件名
  140. * @return string|null
  141. * @throws \Exception
  142. */
  143. public function getFileName($ext = '.csv') {
  144. $this->fileName = date('YmdHis', Date::nowTime()) . random_int(1000, 9999) . $ext;
  145. return $this->fileName;
  146. }
  147. /**
  148. * 获取保存的路径
  149. * @return array|null
  150. */
  151. public function getSavePath() {
  152. $this->savePath = $this->pathCreator(date('Ymd', Date::nowTime()));
  153. return trim((string)$this->savePath, __DS__);
  154. }
  155. /**
  156. * @return bool|string|null
  157. */
  158. public function getSaveBasePath() {
  159. $this->saveBasePath = Yii::getAlias('@backendApi') . __DS__ . 'web' . __DS__ . 'upload' . __DS__ . (isset(\Yii::$app->params['excelLocalDir']) ? \Yii::$app->params['excelLocalDir'] : '');
  160. return trim((string)$this->saveBasePath, __DS__);
  161. }
  162. /**
  163. * 创建目录(递归创建)
  164. *
  165. * @param $dir
  166. * @return string|false
  167. */
  168. public function mkdir($dir) {
  169. if (!is_dir($dir)) {
  170. $this->mkdir(dirname($dir));
  171. if (!mkdir($dir, 0777))
  172. return false;
  173. @exec('chown -R www:www /'.$dir);
  174. @exec('chmod -R 777 /'.$dir);
  175. }
  176. return $dir;
  177. }
  178. /**
  179. * @return array|mixed
  180. */
  181. public function getParams() {
  182. $this->params = CacheHelper::getAsyncParams($this->taskId);
  183. // 设置语言
  184. Yii::$app->language = $this->params['language'] ?? 'en-US';
  185. Yii::$app->sourceLanguage = Yii::$app->language == 'zh-CN' ? 'en-US' : 'zh-CN';
  186. return $this->params;
  187. }
  188. /**
  189. * @return mixed|null
  190. */
  191. public function getUserId() {
  192. $this->userId = isset($this->params['userId']) ? $this->params['userId'] : null;
  193. return $this->userId;
  194. }
  195. /**
  196. * @return |null
  197. */
  198. public function getExportId() {
  199. $this->exportId = isset($this->params['exportId']) ? $this->params['exportId'] : null;
  200. return $this->userId;
  201. }
  202. /**
  203. * 生成
  204. * @return bool
  205. * @throws Exception
  206. * @throws InvalidConfigException
  207. * @throws \yii\httpclient\Exception
  208. */
  209. public function generate() {
  210. $this->getParams();
  211. if (!$this->params) {
  212. throw new Exception('无法获取需要的参数');
  213. }
  214. $path = $this->getSaveBasePath() . __DS__ . $this->getSavePath();
  215. $path = __DS__ . $path;
  216. $realFile = $this->mkdir($path) . __DS__ . $this->getFileName();
  217. $this->completed = false;
  218. $this->getExportId();
  219. $this->getUserId();
  220. $fileNameUpdated = false;
  221. $this->_fp = fopen($realFile, 'w');
  222. @exec('chown -R www:www /'.$realFile);
  223. @exec('chmod -R 777 /'.$realFile);
  224. // 获取列表数据及表头
  225. $this->_listModel = new $this->listModelClass();
  226. $this->_listModel->isExport = true;
  227. if(method_exists($this->_listModel, 'getExportHeaders')){
  228. if(method_exists($this->_listModel, 'exportPrepare')) {//导出数据提前设置参数
  229. $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]);
  230. }
  231. $headers = $this->_listModel->getExportHeaders($this->userId);
  232. fputcsv($this->_fp, $headers);
  233. unset($headers);
  234. $this->_updateFirst($realFile, 1);
  235. } else {
  236. throw new Exception($this->listModelClass.'的getExportHeaders方法不存在');
  237. }
  238. $this->_loopWriteData($realFile, $fileNameUpdated);
  239. $this->complete();
  240. return true;
  241. }
  242. /**
  243. * 循环写入数据
  244. * @param $realFile
  245. * @param $fileNameUpdated
  246. * @param int $page
  247. * @param int $counter
  248. * @return string
  249. * @throws Exception
  250. */
  251. private function _loopWriteData($realFile, &$fileNameUpdated, $page = 0, $counter = 0){
  252. if(method_exists($this->_listModel, 'getList')){
  253. $list = $this->_listModel->getList(['condition'=>$this->params['condition'], 'params'=> $this->params['params'] ?? [], 'others'=> $this->params['others'] ?? [], 'page'=>$page, 'pageSize'=>$this->pageSize, 'userId'=>$this->userId]);
  254. } else {
  255. throw new Exception($this->listModelClass.'的getList方法不存在');
  256. }
  257. if($page >= $list['totalPages']){
  258. return 'finish';
  259. }
  260. if(!empty($list['list'])){
  261. foreach($list['list'] as $columnData){
  262. fputcsv($this->_fp, Tool::arrTextConvert($columnData));
  263. $counter++;
  264. if ($list['totalCount'] < $counter) {
  265. // $counter = $list['totalCount'];
  266. return 'finish';
  267. }
  268. $percent = intval(($counter / $list['totalCount']) * 100);
  269. $this->_updatePercent($percent);
  270. // if (!$fileNameUpdated) {
  271. // $this->_updateFirst($realFile, $percent);
  272. // } else {
  273. // $this->_updatePercent($percent);
  274. // }
  275. // $fileNameUpdated = true;
  276. unset($percent, $columnData);
  277. }
  278. $page++;
  279. unset($list);
  280. return $this->_loopWriteData($realFile, $fileNameUpdated, $page, $counter);
  281. }
  282. unset($list);
  283. return 'finish';
  284. }
  285. /**
  286. * 循环写入数据
  287. */
  288. private function _loopWriteDataOrder()
  289. {
  290. $orderQuery = Order::find()
  291. ->alias('O')
  292. ->where($this->params['condition'], $this->params['params'])
  293. ->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')
  294. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  295. ->join('LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  296. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  297. ->orderBy('O.CREATED_AT DESC');
  298. // 订单中间表只查询待支付和支付失败的订单
  299. $this->params['params'][':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  300. $this->params['params'][':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  301. $orderStandardQuery = ApproachOrder::find()
  302. ->alias('O')
  303. ->where($this->params['condition'] . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $this->params['params'])
  304. ->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')
  305. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  306. ->join('LEFT JOIN', ApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  307. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  308. ->orderBy('O.CREATED_AT DESC');
  309. $queryAll = $orderQuery->union($orderStandardQuery, true);
  310. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  311. $lists = $query->all();
  312. if(!empty($lists)){
  313. $regionConfig = Cache::getRegionConfig();
  314. foreach($lists as $columnData) {
  315. $CREATE_USER_ID = Info::getUserIdByUserName($columnData['CREATE_USER']);
  316. $createUserName = Info::getUserRealNameByUserId($CREATE_USER_ID);
  317. $columnAccept = [
  318. 'USER_NAME' => $columnData['USER_NAME'],
  319. 'DEC_USER_NAME' => $columnData['DEC_USER_ID'],
  320. 'CREATE_USER' => $columnData['CREATE_USER'],
  321. 'CREATE_USER_NAME' => $createUserName,
  322. 'SN' => $columnData['SN'],
  323. 'STATUS' => Tool::paramConvert(\Yii::$app->params['orderStatus'])[$columnData['STATUS']]['label'] ?? '',
  324. 'SKU_CODE' => $columnData['SKU_CODE'],
  325. 'GOODS_TITLE' => Tool::mbSignConvert($columnData['GOODS_TITLE']) ,
  326. 'CONSIGNEE' => $columnData['CONSIGNEE'],
  327. 'MOBILE' => "\t{$columnData['MOBILE']}",
  328. 'TEL' => "\t{$columnData['TEL']}",
  329. 'PROVINCE' => $regionConfig[$columnData['PROVINCE']]['REGION_NAME'] ?? '',
  330. 'CITY' => $regionConfig[$columnData['CITY']]['REGION_NAME'] ?? '',
  331. 'COUNTY' => $regionConfig[$columnData['COUNTY']]['REGION_NAME'] ?? '',
  332. 'ADDRESS' => $columnData['ADDRESS'],
  333. 'PERIOD_NUM' => $columnData['PERIOD_NUM'],
  334. 'ORDER_TYPE' => ($columnData['ORDER_TYPE'] == 'ZC') ? 'Welcome pack' : (in_array($columnData['PAY_TYPE'], ['cash', 'pay_stack']) ? 'Repeat Purchase': 'Points'),
  335. // 'WAREHOUSE' => $columnData['WAREHOUSE'],
  336. 'CREATED_AT' => Date('Y-m-d H:i:s', $columnData['CREATED_AT']),
  337. 'PAY_TYPE' => ShopGoods::payTypes()[$columnData['PAY_TYPE']]['name'] ?? ShopGoods::payTypes()['cash']['name'],
  338. 'PAY_AT' => $columnData['PAY_AT'] > 0 ? Date('Y-m-d H:i:s', $columnData['PAY_AT']) : '',
  339. 'DELIVERY_AT' => $columnData['DELIVERY_AT'] > 0 ? Date('Y-m-d H:i:s', $columnData['DELIVERY_AT']) : '',
  340. 'BUY_NUMS' => $columnData['BUY_NUMS'],
  341. 'REAL_PRICE' => $columnData['REAL_PRICE'],
  342. 'TOTAL_PRICE' => $columnData['REAL_PRICE'] * $columnData['BUY_NUMS'],
  343. 'REAL_PV' => $columnData['REAL_PV'] * $columnData['BUY_NUMS'],
  344. 'STANDARD_PRICE' => $columnData['STANDARD_PRICE'] * $columnData['BUY_NUMS'],
  345. 'PAY_FREIGHT' => $columnData['PAY_FREIGHT'],
  346. 'TAX_RATE' => $columnData['TAX_RATE'],
  347. 'TAX_AMOUNT' => Tool::calculateTax($columnData['REAL_PRICE'], $columnData['TAX_RATE'], $columnData['BUY_NUMS']),
  348. 'EXPRESS_COMPANY' => $columnData['EXPRESS_COMPANY'],
  349. 'ORDER_TRACK_NO' => $columnData['ORDER_TRACK_NO'],
  350. 'EXPRESS_TYPE' => $columnData['EXPRESS_TYPE'] == 0 ? 'mailing ':' auto pick',
  351. 'FRONT_REMARK' => $columnData['FRONT_REMARK'],
  352. 'DELIVERY_STATUS_NAME' => Tool::paramConvert(\Yii::$app->params['deliveryStatus'])[$columnData['DELIVERY_STATUS']]['label'] ?? '',
  353. 'IS_AUTO' => $columnData['IS_AUTO'] == 1 ? 'Yes' : 'No',
  354. ];
  355. fputcsv($this->_fp, Tool::arrTextConvert($columnAccept));
  356. unset($percent, $columnData, $columnAccept);
  357. }
  358. unset($list);
  359. }
  360. unset($list);
  361. return 'finish';
  362. }
  363. /**
  364. * 完成
  365. * @return bool
  366. * @throws Exception
  367. * @throws InvalidConfigException
  368. * @throws \yii\httpclient\Exception
  369. */
  370. public function complete() {
  371. $this->_updatePercent(100);
  372. if ($this->completed) {
  373. return true;
  374. }
  375. if ($this->_fp) {
  376. fclose($this->_fp);
  377. }
  378. // 把导出的文件上传至静态文件服务器
  379. if ($this->isRemote) {
  380. $realFile = $this->getSaveBasePath() . __DS__ . $this->getSavePath() . __DS__ . $this->getFileName();
  381. $remoteUploadApi = RemoteUploadApi::instance();
  382. if ($uploadResult = $remoteUploadApi->upload($realFile)) {
  383. Export::updateAll(['REMOTE_URL' => $uploadResult['url'], 'IS_EXPORTING' => 0, 'EXPORT_PERCENT' => 100, 'ENDED_AT' => Date::nowTime()], 'ID=:ID', [':ID' => $this->exportId]);
  384. } else {
  385. $this->_errorHandle();
  386. throw new Exception('文件远程上传失败');
  387. }
  388. // 删除本地临时文件
  389. unlink($realFile);
  390. } else {
  391. Export::updateAll(['IS_EXPORTING' => 0, 'EXPORT_PERCENT' => 100, 'ENDED_AT' => Date::nowTime()], 'ID=:ID', [':ID' => $this->exportId]);
  392. }
  393. \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin(100, ['MODEL' => 'EXPORT', 'ID' => $this->exportId, 'FIELD' => 'EXPORT_PERCENT']);
  394. CacheHelper::deleteAsyncParams($this->taskId);
  395. $this->completed = true;
  396. }
  397. /**
  398. * @param $message
  399. */
  400. private function _errorNotice($message) {
  401. Yii::$app->swooleAsyncTimer->pushAsyncResultToAdmin($this->userId, $message, false);
  402. }
  403. /**
  404. * 首次更新
  405. * @param $exportId
  406. * @param $realName
  407. */
  408. private function _updateFirst($realName, $percent) {
  409. $fileName = str_replace($this->getSaveBasePath() . __DS__, '', $realName);
  410. Export::updateAll([
  411. 'FILE_NAME' => str_replace(__DS__, '/', $fileName),
  412. 'UPDATED_AT' => Date::nowTime(),
  413. 'EXPORT_PERCENT' => $percent,
  414. ], 'ID=:ID', [':ID' => $this->exportId]);
  415. }
  416. /**
  417. * 更新百分比
  418. * @param $exportId
  419. * @param $percent
  420. */
  421. private function _updatePercent($percent) {
  422. // 把数据写入数据库中
  423. Export::updateAll(['EXPORT_PERCENT' => $percent], 'ID=:ID', [':ID' => $this->exportId]);
  424. \Yii::$app->swooleAsyncTimer->pushAsyncPercentToAdmin($percent, ['MODEL' => 'EXPORT', 'ID' => $this->exportId, 'FIELD' => 'EXPORT_PERCENT']);
  425. }
  426. /**
  427. * 发生错误处理
  428. * @param $exportId
  429. */
  430. private function _errorHandle() {
  431. Export::updateAll(['IS_EXPORTING' => 0], 'ID=:ID', [':ID' => $this->exportId]);
  432. }
  433. /**
  434. * 导出逻辑
  435. * @param $filter
  436. * @param $listName
  437. * @param null $consoleRouter
  438. * @param null remark
  439. * @throws Exception
  440. */
  441. public function exportHandle($filter, $listName, $consoleRouter = null,$remark = null){
  442. $params = [
  443. 'moduleId' => $this->moduleId,
  444. 'listName' => $listName,
  445. 'action' => $consoleRouter ? $consoleRouter : Yii::$app->controller->id.'/'.Yii::$app->controller->action->id, // 这里这么写,是因为调用的异步路由和同步的控制器和方法是一样的,所以,只要不传默认调和同步一样的异步方法
  446. 'userId' => Yii::$app->user->id,
  447. 'remark' => $remark,
  448. ];
  449. $this->webToAsync($params,$filter);
  450. }
  451. /**
  452. * 页面到异步的请求
  453. * @param $params
  454. * @param array $extend
  455. * @return bool
  456. * @throws Exception
  457. */
  458. public function webToAsync($params, $extend = []) {
  459. $default = [
  460. 'moduleId' => null,
  461. 'listName' => null,
  462. 'remark' => null,
  463. 'action' => null,
  464. 'userId' => null,
  465. ];
  466. $params = Tool::deepParse($params, $default);
  467. if (!$params['moduleId'] || !$params['listName'] || !$params['action']) {
  468. throw new Exception('请设置moduleId,listName和action');
  469. }
  470. // 把文件对应的相关资料存入数据库中
  471. $export = new Export();
  472. $export->EXPORT_NAME = $params['listName'] . '_' . date('Exp_y-m-d H:i:s', Date::nowTime());
  473. $export->MODULE_NAME = $params['moduleId'];
  474. $export->REMARK = $params['remark'];
  475. $export->IS_EXPORTING = 1;
  476. $export->ADMIN_ID = \Yii::$app->user->id;
  477. $export->STARTED_AT = Date::nowTime();
  478. $export->CREATED_AT = Date::nowTime();
  479. $export->EXPORT_PERCENT = 0;
  480. $export->ENDED_AT = 0;
  481. $export->UPDATED_AT = 0;
  482. if (!$export->save()) {
  483. throw new Exception(Form::formatErrorsForApi($export->getErrors()));
  484. }
  485. $extend = array_merge($extend, [
  486. 'exportId' => $export->ID,
  487. 'userId' => $params['userId'],
  488. 'language' => Yii::$app->language,
  489. ]);
  490. // 异步处理添加任务
  491. $taskKey = \Yii::$app->swooleAsyncTimer->asyncHandle($params['action'], $extend);
  492. if($taskKey === false){
  493. // 删除刚刚添加的导出数据
  494. Export::deleteAll(['ID'=>$export->ID]);
  495. throw new Exception('导出失败,请求异步处理服务器失败');
  496. }
  497. return $taskKey;
  498. }
  499. /**
  500. * 生成
  501. * @return bool
  502. * @throws Exception
  503. * @throws InvalidConfigException
  504. * @throws \yii\httpclient\Exception
  505. */
  506. public function generateOrderExcel() {
  507. $this->getParams();
  508. if (!$this->params) {
  509. throw new Exception('无法获取需要的参数');
  510. }
  511. $path = $this->getSaveBasePath() . __DS__ . $this->getSavePath();
  512. $path = __DS__ . $path;
  513. $realFile = $this->mkdir($path) . __DS__ . $this->getFileName();
  514. $this->completed = false;
  515. $this->getExportId();
  516. $this->getUserId();
  517. $fileNameUpdated = false;
  518. $this->_fp = fopen($realFile, 'w');
  519. @exec('chown -R www:www /'.$realFile);
  520. @exec('chmod -R 777 /'.$realFile);
  521. // 获取列表数据及表头
  522. $this->_listModel = new $this->listModelClass();
  523. $this->_listModel->isExport = true;
  524. if(method_exists($this->_listModel, 'getExportHeaders')){
  525. if(method_exists($this->_listModel, 'exportPrepare')) {//导出数据提前设置参数
  526. $this->_listModel->exportPrepare(['condition' => $this->params['condition'], 'params' => $this->params['params'], 'others' => $this->params['others'] ?? [], 'page' => 0, 'pageSize' => 100000, 'userId' => $this->userId]);
  527. }
  528. $headers = $this->_listModel->getExportHeaders($this->userId);
  529. fputcsv($this->_fp, $headers);
  530. unset($headers);
  531. $this->_updateFirst($realFile, 1);
  532. } else {
  533. throw new Exception($this->listModelClass.'的getExportHeaders方法不存在');
  534. }
  535. $this->_loopWriteDataOrder();
  536. $this->complete();
  537. return true;
  538. }
  539. /**
  540. * 生成
  541. * @return bool
  542. * @throws Exception
  543. * @throws InvalidConfigException
  544. * @throws \yii\httpclient\Exception
  545. */
  546. public function generateOrderPDF() {
  547. $this->getParams();
  548. if (!$this->params) {
  549. throw new Exception('无法获取需要的参数');
  550. }
  551. $path = __DS__ . $this->getSaveBasePath() . __DS__ . $this->getSavePath();
  552. $realFile = $this->mkdir($path) . __DS__ . $this->getFileName('.pdf');
  553. $this->completed = false;
  554. $this->getExportId();
  555. $this->getUserId();
  556. $fileNameUpdated = false;
  557. // 获取列表数据及表头
  558. $this->_listModel = new $this->listModelClass();
  559. $this->_listModel->isExport = true;
  560. // 查询订单数据
  561. // $oderList = $this->_loopWriteDataPDF($realFile, $fileNameUpdated);
  562. $orderQuery = Order::find()
  563. ->alias('O')
  564. ->where($this->params['condition'], $this->params['params'])
  565. ->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')
  566. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  567. ->join('LEFT JOIN', OrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  568. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  569. ->orderBy('O.CREATED_AT DESC');
  570. // 订单中间表只查询待支付和支付失败的订单
  571. $this->params['params'][':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  572. $this->params['params'][':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  573. $orderStandardQuery = ApproachOrder::find()
  574. ->alias('O')
  575. ->where($this->params['condition'] . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $this->params['params'])
  576. ->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')
  577. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  578. ->join('LEFT JOIN', ApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  579. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  580. ->orderBy('O.CREATED_AT DESC');
  581. $queryAll = $orderQuery->union($orderStandardQuery, true);
  582. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  583. $oderList = $query->all();
  584. if ($oderList) {
  585. $userId = '';
  586. $userName = '';
  587. $address = '';
  588. $mobile = '';
  589. $orderAt = '';
  590. $orderDetails = '';
  591. $orderSn = '';
  592. $orderAmount = 0; // 合计总额
  593. $orderNums = 0; // 合计总数
  594. $totalTaxAmount = 0; // 合计税额
  595. $totalAmount = 0;
  596. foreach ($oderList as $key => $value) {
  597. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  598. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  599. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  600. $userId = $value['USER_NAME'];
  601. $userName = $value['REAL_NAME'];
  602. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  603. $mobile = $value['MOBILE'];
  604. $orderAt = Date::convert($value['CREATED_AT'],'Y-m-d H:i:s');
  605. $orderSn = $value['SN'];
  606. // 总价
  607. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  608. $orderAmount += $totalAmount;
  609. $orderNums += $value['BUY_NUMS'];
  610. // 税额
  611. $taxAmount = Tool::calculateTax($value['REAL_PRICE'], $value['TAX_RATE'], $value['BUY_NUMS']);
  612. $totalTaxAmount += $taxAmount;
  613. $taxAmount = Tool::formatAmount($taxAmount);
  614. $totalAmount = Tool::formatAmount($totalAmount);
  615. // 订单详情
  616. $orderDetails .= <<<EOT
  617. <tr>
  618. <td>{$value['SKU_CODE']}</td>
  619. <td>{$value['GOODS_TITLE']}</td>
  620. <td style="text-align: right;">{$value['REAL_PRICE']}</td>
  621. <td>{$value['BUY_NUMS']}</td>
  622. <td style="text-align: right;">{$value['TAX_RATE']}</td>
  623. <td style="text-align: right;">{$taxAmount}</td>
  624. <td style="text-align: right;">{$totalAmount}</td>
  625. </tr>
  626. EOT;
  627. }
  628. $memberCode = Yii::t('app', 'memberCode');
  629. $memberName = Yii::t('app', 'memberName');
  630. $memberAddress = Yii::t('app', 'memberAddress');
  631. $memberPhone = Yii::t('app', 'memberPhone');
  632. $orderCode = Yii::t('app', 'orderCode');
  633. $orderDetail = Yii::t('app', 'orderDetail');
  634. $productCode = Yii::t('app', 'productCode');
  635. $productName = Yii::t('app', 'productName');
  636. $productPrice = Yii::t('app', 'productPrice');
  637. $quantity = Yii::t('app', 'qty');
  638. $taxRate = Yii::t('app', 'taxRate');
  639. $totalTax = Yii::t('app', 'totalTax');
  640. $totalAmount = Yii::t('app', 'totalAmount');
  641. $total = Yii::t('app', 'total');
  642. $signature = Yii::t('app', 'signature');
  643. $date = Yii::t('app', 'date');
  644. $createAt = Yii::t('app', 'createAt');
  645. // 订单基本信息
  646. $orderBase = <<<ORDER
  647. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  648. <tr>
  649. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberCode}</td>
  650. <td width="70%">{$userId}</td>
  651. </tr>
  652. <tr>
  653. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberName}</td>
  654. <td width="70%">{$userName}</td>
  655. </tr>
  656. <tr>
  657. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberAddress}</td>
  658. <td width="70%">{$address}</td>
  659. </tr>
  660. <tr>
  661. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberPhone}</td>
  662. <td width="70%">{$mobile}</td>
  663. </tr>
  664. <tr>
  665. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$orderCode}</td>
  666. <td width="70%">{$orderSn}</td>
  667. </tr>
  668. <tr>
  669. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$createAt}</td>
  670. <td width="70%">{$orderAt}</td>
  671. </tr>
  672. <tr>
  673. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">{$orderDetail}</td>
  674. <td class="bg"></td>
  675. </tr>
  676. </table>
  677. ORDER;
  678. $l['a_meta_charset'] = 'UTF-8';
  679. $l['a_meta_dir'] = 'ltr';
  680. $l['a_meta_language'] = 'zh';
  681. $l['w_page'] = '页面';
  682. $orderAmount = Tool::formatAmount($orderAmount);
  683. $totalTaxAmount = Tool::formatAmount($totalTaxAmount);
  684. $context = <<<ORDER
  685. <!doctype html>
  686. <html lang="en">
  687. <head>
  688. <meta charset="UTF-8" />
  689. <title>{$orderDetail}</title>
  690. <style>
  691. table {
  692. border-collapse: collapse;
  693. }
  694. table td, table th {
  695. border: 1px solid #ccc;
  696. /*padding: 5px 5px;*/
  697. border-collapse: collapse;
  698. }
  699. /*td {*/
  700. /* padding: 120px;*/
  701. /*}*/
  702. .bg {
  703. background-color: #ccc;
  704. }
  705. </style>
  706. </head>
  707. <body>
  708. <div class="content">
  709. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>{$orderDetail}</b><br></p>
  710. <div>
  711. <div style="display: block; width: 100%;">
  712. {$orderBase}
  713. <table border="1" width="100%" style="padding: 10px 5px; text-align: center;">
  714. <tr>
  715. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productCode}</th>
  716. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productName}</th>
  717. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productPrice}</th>
  718. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">{$quantity}</th>
  719. <th width="12%" style="font-size: 14px; font-weight: bold; text-align: center;">{$taxRate}</th>
  720. <th width="13%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalTax}</th>
  721. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalAmount}</th>
  722. </tr>
  723. {$orderDetails}
  724. <tr>
  725. <td colspan="3">{$total}</td>
  726. <td style="text-align: right;">{$orderNums}</td>
  727. <td></td>
  728. <td style="text-align: right;">{$totalTaxAmount}</td>
  729. <td style="text-align: right;">{$orderAmount}</td>
  730. </tr>
  731. </table>
  732. </div>
  733. <div style="width: 100%; margin-top: 50px; height: 30px;">
  734. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  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;">{$signature}:</td>
  738. </tr>
  739. <tr style="border: none;">
  740. <td width="70%" style="border: none;"></td>
  741. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$date}:</td>
  742. </tr>
  743. </table>
  744. </div>
  745. </div>
  746. </div>
  747. </body>
  748. </html>
  749. ORDER;
  750. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  751. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  752. // 设置打印模式
  753. $pdf->SetCreator(PDF_CREATOR);
  754. $pdf->SetAuthor('DaZe');
  755. $pdf->SetTitle($orderSn);
  756. $pdf->SetSubject('TCPDF Tutorial');
  757. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  758. // 是否显示页眉
  759. $pdf->setPrintHeader(false);
  760. // 设置页眉字体
  761. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  762. // 页眉距离顶部的距离
  763. $pdf->SetHeaderMargin('5');
  764. // 是否显示页脚
  765. $pdf->setPrintFooter(false);
  766. // 设置默认等宽字体
  767. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  768. // 设置行高
  769. $pdf->setCellHeightRatio(1);
  770. // 设置左、上、右的间距
  771. $pdf->SetMargins('30', '70', '30');
  772. // 设置是否自动分页 距离底部多少距离时分页
  773. $pdf->SetAutoPageBreak(TRUE, '15');
  774. // 设置图像比例因子
  775. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  776. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  777. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  778. $pdf->setLanguageArray($l);
  779. }
  780. $pdf->setFontSubsetting(true);
  781. $pdf->AddPage();
  782. // 设置字体
  783. $pdf->SetFont('dejavusans', '', 10, '', true);
  784. // $image_file = \Yii::$app->basePath . '/../frontendEle/src/static/img/ngds-logo.jpg';
  785. // if (!file_exists($image_file)) {
  786. // $image_file = \Yii::$app->runtimePath . '/../common/uploads/ngds-logo.jpg';
  787. // }
  788. // $image_file = \Yii::$app->basePath . '/../ngds-logo.jpg';
  789. // $image = file_get_contents($image_file);
  790. // 将图像放在右上角,根据页面宽度和边距计算位置
  791. // $pdf->Image('@' . $image, $pdf->getPageWidth() - 45, 25, 30, 10, 'JPG');
  792. $pdf->writeHTML($context);
  793. $result = $pdf->Output($realFile, 'F');
  794. LoggerTool::debug(['result' => $result]);
  795. $this->_updateFirst($realFile, 1);
  796. }
  797. $this->complete();
  798. return true;
  799. }
  800. /**
  801. * 生成
  802. * @return bool
  803. * @throws Exception
  804. * @throws InvalidConfigException
  805. * @throws \yii\httpclient\Exception
  806. */
  807. public function generateDecOrderPDF() {
  808. $this->getParams();
  809. if (!$this->params) {
  810. throw new Exception('无法获取需要的参数');
  811. }
  812. $path = __DS__ . $this->getSaveBasePath() . __DS__ . $this->getSavePath();
  813. $realFile = $path . __DS__ . $this->getFileName('.pdf');
  814. $this->completed = false;
  815. $this->getExportId();
  816. $this->getUserId();
  817. $fileNameUpdated = false;
  818. // 获取列表数据及表头
  819. $this->_listModel = new $this->listModelClass();
  820. $this->_listModel->isExport = true;
  821. // 查询订单数据
  822. // $oderList = $this->_loopWriteDataPDF($realFile, $fileNameUpdated);
  823. $orderQuery = Order::find()
  824. ->alias('O')
  825. ->where($this->params['condition'], $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', OrderGoods::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. // 订单中间表只查询待支付和支付失败的订单
  832. $this->params['params'][':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  833. $this->params['params'][':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  834. $orderStandardQuery = ApproachOrder::find()
  835. ->alias('O')
  836. ->where($this->params['condition'] . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $this->params['params'])
  837. ->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')
  838. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  839. ->join('LEFT JOIN', ApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  840. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  841. ->orderBy('O.CREATED_AT DESC');
  842. $queryAll = $orderQuery->union($orderStandardQuery, true);
  843. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  844. $oderList = $query->all();
  845. if ($oderList) {
  846. $userId = '';
  847. $userName = '';
  848. $address = '';
  849. $mobile = '';
  850. $orderAt = '';
  851. $orderDetails = '';
  852. $orderSn = '';
  853. $orderAmount = 0; // 合计总额
  854. $orderNums = 0; // 合计总数
  855. $totalTaxAmount = 0; // 合计税额
  856. $totalAmount = 0;
  857. foreach ($oderList as $key => $value) {
  858. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  859. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  860. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  861. $userId = $value['USER_NAME'];
  862. $userName = $value['REAL_NAME'];
  863. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  864. $mobile = $value['MOBILE'];
  865. $orderAt = Date::convert($value['CREATED_AT'],'Y-m-d H:i:s');
  866. $orderSn = $value['SN'];
  867. // 总价
  868. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  869. $orderAmount += $totalAmount;
  870. $orderNums += $value['BUY_NUMS'];
  871. // 税额
  872. $taxAmount = Tool::calculateTax($value['REAL_PRICE'], $value['TAX_RATE'], $value['BUY_NUMS']);
  873. $totalTaxAmount += $taxAmount;
  874. $taxAmount = Tool::formatAmount($taxAmount);
  875. $totalAmount = Tool::formatAmount($totalAmount);
  876. // 订单详情
  877. $orderDetails .= <<<EOT
  878. <tr>
  879. <td>{$value['SKU_CODE']}</td>
  880. <td>{$value['GOODS_TITLE']}</td>
  881. <td style="text-align: right;">{$value['REAL_PRICE']}</td>
  882. <td>{$value['BUY_NUMS']}</td>
  883. <td style="text-align: right;">{$value['TAX_RATE']}</td>
  884. <td style="text-align: right;">{$taxAmount}</td>
  885. <td style="text-align: right;">{$totalAmount}</td>
  886. </tr>
  887. EOT;
  888. }
  889. $memberCode = Yii::t('app', 'memberCode');
  890. $memberName = Yii::t('app', 'memberName');
  891. $memberAddress = Yii::t('app', 'memberAddress');
  892. $memberPhone = Yii::t('app', 'memberPhone');
  893. $orderCode = Yii::t('app', 'orderCode');
  894. $orderDetail = Yii::t('app', 'orderDetail');
  895. $productCode = Yii::t('app', 'productCode');
  896. $productName = Yii::t('app', 'productName');
  897. $productPrice = Yii::t('app', 'productPrice');
  898. $quantity = Yii::t('app', 'qty');
  899. $taxRate = Yii::t('app', 'taxRate');
  900. $totalTax = Yii::t('app', 'totalTax');
  901. $totalAmount = Yii::t('app', 'totalAmount');
  902. $total = Yii::t('app', 'total');
  903. $signature = Yii::t('app', 'signature');
  904. $date = Yii::t('app', 'date');
  905. $createAt = Yii::t('app', 'createAt');
  906. // 订单基本信息
  907. $orderBase = <<<ORDER
  908. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  909. <tr>
  910. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberCode}</td>
  911. <td width="70%">{$userId}</td>
  912. </tr>
  913. <tr>
  914. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberName}</td>
  915. <td width="70%">{$userName}</td>
  916. </tr>
  917. <tr>
  918. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberAddress}</td>
  919. <td width="70%">{$address}</td>
  920. </tr>
  921. <tr>
  922. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$memberPhone}</td>
  923. <td width="70%">{$mobile}</td>
  924. </tr>
  925. <tr>
  926. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$orderCode}</td>
  927. <td width="70%">{$orderSn}</td>
  928. </tr>
  929. <tr>
  930. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">{$createAt}</td>
  931. <td width="70%">{$orderAt}</td>
  932. </tr>
  933. <tr>
  934. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">{$orderDetail}</td>
  935. <td class="bg"></td>
  936. </tr>
  937. </table>
  938. ORDER;
  939. $l['a_meta_charset'] = 'UTF-8';
  940. $l['a_meta_dir'] = 'ltr';
  941. $l['a_meta_language'] = 'zh';
  942. $l['w_page'] = '页面';
  943. $orderAmount = sprintf('%.2f', $orderAmount);
  944. $totalTaxAmount = sprintf('%.2f', $totalTaxAmount);
  945. $context = <<<ORDER
  946. <!doctype html>
  947. <html lang="en">
  948. <head>
  949. <meta charset="UTF-8" />
  950. <title>{$orderDetail}</title>
  951. <style>
  952. table {
  953. border-collapse: collapse;
  954. }
  955. table td, table th {
  956. border: 1px solid #ccc;
  957. /*padding: 5px 5px;*/
  958. border-collapse: collapse;
  959. }
  960. /*td {*/
  961. /* padding: 120px;*/
  962. /*}*/
  963. .bg {
  964. background-color: #ccc;
  965. }
  966. </style>
  967. </head>
  968. <body>
  969. <div class="content">
  970. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>{$orderDetail}</b><br></p>
  971. <div>
  972. <div style="display: block; width: 100%;">
  973. {$orderBase}
  974. <table border="1" width="100%" style="padding: 10px 5px; text-align: center;">
  975. <tr>
  976. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productCode}</th>
  977. <th width="20%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productName}</th>
  978. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$productPrice}</th>
  979. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">{$quantity}</th>
  980. <th width="12%" style="font-size: 14px; font-weight: bold; text-align: center;">{$taxRate}</th>
  981. <th width="13%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalTax}</th>
  982. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">{$totalAmount}</th>
  983. </tr>
  984. {$orderDetails}
  985. <tr>
  986. <td colspan="3">{$total}</td>
  987. <td style="text-align: right;">{$orderNums}</td>
  988. <td></td>
  989. <td style="text-align: right;">{$totalTaxAmount}</td>
  990. <td style="text-align: right;">{$orderAmount}</td>
  991. </tr>
  992. </table>
  993. </div>
  994. <div style="width: 100%; margin-top: 50px; height: 30px;">
  995. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  996. <tr style="border: none;">
  997. <td width="70%" style="border: none;"></td>
  998. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$signature}:</td>
  999. </tr>
  1000. <tr style="border: none;">
  1001. <td width="70%" style="border: none;"></td>
  1002. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">{$date}:</td>
  1003. </tr>
  1004. </table>
  1005. </div>
  1006. </div>
  1007. </div>
  1008. </body>
  1009. </html>
  1010. ORDER;
  1011. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  1012. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  1013. // 设置打印模式
  1014. $pdf->SetCreator(PDF_CREATOR);
  1015. $pdf->SetAuthor('DaZe');
  1016. $pdf->SetTitle($orderSn);
  1017. $pdf->SetSubject('TCPDF Tutorial');
  1018. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  1019. // 是否显示页眉
  1020. $pdf->setPrintHeader(false);
  1021. // 设置页眉字体
  1022. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  1023. // 页眉距离顶部的距离
  1024. $pdf->SetHeaderMargin('5');
  1025. // 是否显示页脚
  1026. $pdf->setPrintFooter(false);
  1027. // 设置默认等宽字体
  1028. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  1029. // 设置行高
  1030. $pdf->setCellHeightRatio(1);
  1031. // 设置左、上、右的间距
  1032. $pdf->SetMargins('10', '10', '10');
  1033. // 设置是否自动分页 距离底部多少距离时分页
  1034. $pdf->SetAutoPageBreak(TRUE, '15');
  1035. // 设置图像比例因子
  1036. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  1037. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  1038. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  1039. $pdf->setLanguageArray($l);
  1040. }
  1041. $pdf->setFontSubsetting(true);
  1042. $pdf->AddPage();
  1043. // 设置字体
  1044. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  1045. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  1046. // $image_file = \Yii::$app->basePath . '/../frontendEle/src/static/img/ngds-logo.jpg';
  1047. // if (!file_exists($image_file)) {
  1048. // $image_file = \Yii::$app->runtimePath . '/../common/uploads/ngds-logo.jpg';
  1049. // }
  1050. $image_file = \Yii::$app->basePath . '/../ngds-logo.jpg';
  1051. $image = file_get_contents($image_file);
  1052. $pdf->Image('@' . $image, 165, 5, 20, 10, 'JPG');
  1053. $pdf->writeHTML($context);
  1054. $pdf->Output($realFile, 'F');
  1055. $this->_updateFirst($realFile, 1);
  1056. }
  1057. $this->complete();
  1058. return true;
  1059. }
  1060. /**
  1061. * 循环写入数据
  1062. * @param $realFile
  1063. * @param $fileNameUpdated
  1064. * @param int $page
  1065. * @param int $counter
  1066. * @return array
  1067. * @throws Exception
  1068. */
  1069. private function _loopWriteDataPDF($realFile, &$fileNameUpdated, $page = 0, $counter = 0){
  1070. if(method_exists($this->_listModel, 'getList')){
  1071. $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]);
  1072. } else {
  1073. throw new Exception($this->listModelClass.'的getList方法不存在');
  1074. }
  1075. return $list['list'];
  1076. }
  1077. /**
  1078. * 生成
  1079. * @return bool
  1080. * @throws Exception
  1081. * @throws InvalidConfigException
  1082. * @throws \yii\httpclient\Exception
  1083. */
  1084. public function generateBaOrderExcel() {
  1085. $this->getParams();
  1086. if (!$this->params) {
  1087. throw new Exception('无法获取需要的参数');
  1088. }
  1089. $path = $this->getSaveBasePath() . __DS__ . $this->getSavePath();
  1090. $path = __DS__ . $path;
  1091. $realFile = $this->mkdir($path) . __DS__ . $this->getFileName();
  1092. $this->completed = false;
  1093. $this->getExportId();
  1094. $this->getUserId();
  1095. $this->_fp = fopen($realFile, 'w');
  1096. @exec('chown -R www:www /'.$realFile);
  1097. @exec('chmod -R 777 /'.$realFile);
  1098. // 获取列表数据及表头
  1099. $this->_listModel = new $this->listModelClass();
  1100. $this->_listModel->isExport = true;
  1101. if(method_exists($this->_listModel, 'getExportHeaders')){
  1102. if(method_exists($this->_listModel, 'exportPrepare')) {//导出数据提前设置参数
  1103. $this->_listModel->exportPrepare(['condition' => $this->params['condition'], 'params' => $this->params['params'], 'others' => $this->params['others'] ?? [], 'page' => 0, 'pageSize' => 100000, 'userId' => $this->userId]);
  1104. }
  1105. $headers = $this->_listModel->getExportHeaders($this->userId);
  1106. fputcsv($this->_fp, $headers);
  1107. unset($headers);
  1108. $this->_updateFirst($realFile, 1);
  1109. } else {
  1110. throw new Exception($this->listModelClass.'的getExportHeaders方法不存在');
  1111. }
  1112. $this->_loopWriteDataBaOrder();
  1113. $this->complete();
  1114. return true;
  1115. }
  1116. /**
  1117. * 循环写入数据
  1118. */
  1119. private function _loopWriteDataBaOrder()
  1120. {
  1121. $orderQuery = BaOrder::find()
  1122. ->alias('O')
  1123. ->where($this->params['condition'], $this->params['params'])
  1124. ->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')
  1125. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'U.ID=O.USER_ID')
  1126. ->join('LEFT JOIN', BaOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  1127. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  1128. ->orderBy('O.CREATED_AT DESC');
  1129. // 订单中间表只查询待支付和支付失败的订单
  1130. $this->params['params'][':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  1131. $this->params['params'][':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  1132. $orderStandardQuery = BaApproachOrder::find()
  1133. ->alias('O')
  1134. ->where($this->params['condition'] . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $this->params['params'])
  1135. ->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')
  1136. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'U.ID=O.USER_ID')
  1137. ->join('LEFT JOIN', BaApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  1138. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  1139. ->orderBy('O.CREATED_AT DESC');
  1140. $queryAll = $orderQuery->union($orderStandardQuery, true);
  1141. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  1142. $lists = $query->all();
  1143. if(!empty($lists)){
  1144. $regionConfig = Cache::getRegionConfig();
  1145. foreach($lists as $columnData) {
  1146. $CREATE_USER_ID = Info::getBaUserIdByUserName($columnData['CREATE_USER']);
  1147. $createUserName = Info::getBaUserRealNameByUserId($CREATE_USER_ID);
  1148. $decUserName = Info::getBaUserNameByUserId($columnData['DEC_ID']);
  1149. $columnAccept = [
  1150. 'USER_NAME' => $columnData['USER_NAME'],
  1151. 'DEC_USER_NAME' => $decUserName,
  1152. 'CREATE_USER' => $columnData['CREATE_USER'],
  1153. 'CREATE_USER_NAME' => $createUserName,
  1154. 'SN' => $columnData['SN'],
  1155. 'STATUS' => Tool::paramConvert(\Yii::$app->params['orderStatus'])[$columnData['STATUS']]['label'] ?? '',
  1156. 'SKU_CODE' => $columnData['SKU_CODE'],
  1157. 'GOODS_TITLE' => $columnData['GOODS_TITLE'],
  1158. 'BUY_NUMS' => $columnData['BUY_NUMS'],
  1159. 'CONSIGNEE' => $columnData['CONSIGNEE'],
  1160. 'MOBILE' => "\t{$columnData['MOBILE']}",
  1161. 'TEL' => "\t{$columnData['TEL']}",
  1162. 'PROVINCE' => $regionConfig[$columnData['PROVINCE']]['REGION_NAME'] ?? '',
  1163. 'CITY' => $regionConfig[$columnData['CITY']]['REGION_NAME'] ?? '',
  1164. 'COUNTY' => $regionConfig[$columnData['COUNTY']]['REGION_NAME'] ?? '',
  1165. 'ADDRESS' => $columnData['ADDRESS'],
  1166. 'PERIOD_NUM' => $columnData['PERIOD_NUM'],
  1167. 'ORDER_TYPE' => ($columnData['ORDER_TYPE'] == 'ZC') ? 'Welcome pack' : (in_array($columnData['PAY_TYPE'], ['cash', 'pay_stack']) ? 'Reselling': 'Points'),
  1168. 'CREATED_AT' => Date('Y-m-d H:i:s', $columnData['CREATED_AT']),
  1169. 'PAY_TYPE' => ShopGoods::payTypes()[$columnData['PAY_TYPE']]['name'] ?? ShopGoods::payTypes()['cash']['name'],
  1170. 'PAY_AT' => $columnData['PAY_AT'] > 0 ? Date('Y-m-d H:i:s', $columnData['PAY_AT']) : '',
  1171. 'DELIVERY_AT' => $columnData['DELIVERY_AT'] > 0 ? Date('Y-m-d H:i:s', $columnData['DELIVERY_AT']) : '',
  1172. 'REAL_PRICE' => $columnData['REAL_PRICE'] ?? 0,
  1173. 'REAL_PV' => $columnData['REAL_PV'] ?? 0,
  1174. 'PAY_FREIGHT' => $columnData['PAY_FREIGHT'] ?? 0,
  1175. 'TAX_RATE' => $columnData['TAX_RATE'],
  1176. 'TAX_AMOUNT' => Tool::calculateTax($columnData['REAL_PRICE'], $columnData['TAX_RATE'], $columnData['BUY_NUMS']),
  1177. 'EXPRESS_COMPANY' => $columnData['EXPRESS_COMPANY'],
  1178. 'ORDER_TRACK_NO' => $columnData['ORDER_TRACK_NO'],
  1179. 'EXPRESS_TYPE' => $columnData['EXPRESS_TYPE'] == 0 ? 'mailing ':' auto pick',
  1180. 'FRONT_REMARK' => $columnData['FRONT_REMARK'],
  1181. 'DELIVERY_STATUS_NAME' => Tool::paramConvert(\Yii::$app->params['deliveryStatus'])[$columnData['DELIVERY_STATUS']]['label'] ?? '',
  1182. ];
  1183. fputcsv($this->_fp, Tool::arrTextConvert($columnAccept));
  1184. unset($percent, $columnData, $columnAccept);
  1185. }
  1186. unset($list);
  1187. }
  1188. unset($list);
  1189. return 'finish';
  1190. }
  1191. /**
  1192. * 生成
  1193. * @return bool
  1194. * @throws Exception
  1195. * @throws InvalidConfigException
  1196. * @throws \yii\httpclient\Exception
  1197. */
  1198. public function generateBaOrderPDF()
  1199. {
  1200. $this->getParams();
  1201. if (!$this->params) {
  1202. throw new Exception('无法获取需要的参数');
  1203. }
  1204. $path = __DS__ . $this->getSaveBasePath() . __DS__ . $this->getSavePath();
  1205. $realFile = $path . __DS__ . $this->getFileName('.pdf');
  1206. $this->completed = false;
  1207. $this->getExportId();
  1208. $this->getUserId();
  1209. $fileNameUpdated = false;
  1210. // 获取列表数据及表头
  1211. $this->_listModel = new $this->listModelClass();
  1212. $this->_listModel->isExport = true;
  1213. // 查询订单数据
  1214. $orderQuery = BaOrder::find()
  1215. ->alias('O')
  1216. ->where($this->params['condition'], $this->params['params'])
  1217. ->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')
  1218. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'U.ID=O.USER_ID')
  1219. ->join('LEFT JOIN', BaOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  1220. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  1221. ->orderBy('O.CREATED_AT DESC');
  1222. // 订单中间表只查询待支付和支付失败的订单
  1223. $this->params['params'][':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  1224. $this->params['params'][':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  1225. $orderStandardQuery = BaApproachOrder::find()
  1226. ->alias('O')
  1227. ->where($this->params['condition'] . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $this->params['params'])
  1228. ->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')
  1229. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'U.ID=O.USER_ID')
  1230. ->join('LEFT JOIN', BaApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  1231. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  1232. ->orderBy('O.CREATED_AT DESC');
  1233. $queryAll = $orderQuery->union($orderStandardQuery, true);
  1234. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  1235. $oderList = $query->all();
  1236. if ($oderList) {
  1237. $userId = '';
  1238. $userName = '';
  1239. $address = '';
  1240. $mobile = '';
  1241. $orderAt = '';
  1242. $orderDetails = '';
  1243. $orderSn = '';
  1244. $orderAmount = 0; // 合计总额
  1245. $orderNums = 0; // 合计总数
  1246. $totalTaxAmount = 0; // 合计税额
  1247. $totalAmount = 0;
  1248. foreach ($oderList as $key => $value) {
  1249. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  1250. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  1251. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  1252. $userId = $value['USER_NAME'];
  1253. $userName = $value['REAL_NAME'];
  1254. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  1255. $mobile = $value['MOBILE'];
  1256. $orderAt = Date::convert($value['CREATED_AT'],'Y-m-d H:i:s');
  1257. $orderSn = $value['SN'];
  1258. // 总价
  1259. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  1260. $orderAmount += $totalAmount;
  1261. $orderNums += $value['BUY_NUMS'];
  1262. // 税额
  1263. $taxAmount = Tool::calculateTax($value['REAL_PRICE'], $value['TAX_RATE'], $value['BUY_NUMS']);
  1264. $totalTaxAmount += $taxAmount;
  1265. $taxAmount = Tool::formatAmount($taxAmount);
  1266. $totalAmount = Tool::formatAmount($totalAmount);
  1267. // 订单详情
  1268. $orderDetails .= <<<EOT
  1269. <tr>
  1270. <td>{$value['SKU_CODE']}</td>
  1271. <td>{$value['GOODS_TITLE']}</td>
  1272. <td style="text-align: right;">{$value['REAL_PRICE']}</td>
  1273. <td>{$value['BUY_NUMS']}</td>
  1274. <td style="text-align: right;">{$value['TAX_RATE']}</td>
  1275. <td style="text-align: right;">{$taxAmount}</td>
  1276. <td style="text-align: right;">{$totalAmount}</td>
  1277. </tr>
  1278. EOT;
  1279. }
  1280. // 订单基本信息
  1281. $orderBase = <<<ORDER
  1282. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  1283. <tr>
  1284. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">Member Code</td>
  1285. <td width="70%">{$userId}</td>
  1286. </tr>
  1287. <tr>
  1288. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">Member Name</td>
  1289. <td width="70%">{$userName}</td>
  1290. </tr>
  1291. <tr>
  1292. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">Member Address</td>
  1293. <td width="70%">{$address}</td>
  1294. </tr>
  1295. <tr>
  1296. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">Member Phone</td>
  1297. <td width="70%">{$mobile}</td>
  1298. </tr>
  1299. <tr>
  1300. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">Order Code</td>
  1301. <td width="70%">{$orderSn}</td>
  1302. </tr>
  1303. <tr>
  1304. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">Creation Time</td>
  1305. <td width="70%">{$orderAt}</td>
  1306. </tr>
  1307. <tr>
  1308. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">Order Detail</td>
  1309. <td class="bg"></td>
  1310. </tr>
  1311. </table>
  1312. ORDER;
  1313. $l['a_meta_charset'] = 'UTF-8';
  1314. $l['a_meta_dir'] = 'ltr';
  1315. $l['a_meta_language'] = 'zh';
  1316. $l['w_page'] = '页面';
  1317. $orderAmount = Tool::formatAmount($orderAmount);
  1318. $totalTaxAmount = Tool::formatAmount($totalTaxAmount);
  1319. $context = <<<ORDER
  1320. <!doctype html>
  1321. <html lang="en">
  1322. <head>
  1323. <meta charset="UTF-8" />
  1324. <title>Order detail</title>
  1325. <style>
  1326. table {
  1327. border-collapse: collapse;
  1328. }
  1329. table td, table th {
  1330. border: 1px solid #ccc;
  1331. padding: 5px 5px;
  1332. border-collapse: collapse;
  1333. }
  1334. /*td {*/
  1335. /* padding: 120px;*/
  1336. /*}*/
  1337. .bg {
  1338. background-color: #ccc;
  1339. }
  1340. </style>
  1341. </head>
  1342. <body>
  1343. <div class="content">
  1344. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>Order detail</b><br></p>
  1345. <div>
  1346. <div style="display: block; width: 100%;">
  1347. {$orderBase}
  1348. <table border="1" width="100%" style="padding: 10px 5px; text-align: center;">
  1349. <tr>
  1350. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">Product Code</th>
  1351. <th width="25%" style="font-size: 14px; font-weight: bold; text-align: center;">Product Name</th>
  1352. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">Product Price</th>
  1353. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">Qty</th>
  1354. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">Tax Rate</th>
  1355. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">Tax</th>
  1356. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">Total Amount</th>
  1357. </tr>
  1358. {$orderDetails}
  1359. <tr>
  1360. <td colspan="3">Total</td>
  1361. <td>{$orderNums}</td>
  1362. <td></td>
  1363. <td style="text-align: right;">{$totalTaxAmount}</td>
  1364. <td style="text-align: right;">{$orderAmount}</td>
  1365. </tr>
  1366. </table>
  1367. </div>
  1368. <div style="width: 100%; margin-top: 50px; height: 30px;">
  1369. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  1370. <tr style="border: none;">
  1371. <td width="70%" style="border: none;"></td>
  1372. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">Signature:</td>
  1373. </tr>
  1374. <tr style="border: none;">
  1375. <td width="70%" style="border: none;"></td>
  1376. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">Date:</td>
  1377. </tr>
  1378. </table>
  1379. </div>
  1380. </div>
  1381. </div>
  1382. </body>
  1383. </html>
  1384. ORDER;
  1385. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  1386. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  1387. // 设置打印模式
  1388. $pdf->SetCreator(PDF_CREATOR);
  1389. $pdf->SetAuthor('DaZe');
  1390. $pdf->SetTitle($orderSn);
  1391. $pdf->SetSubject('TCPDF Tutorial');
  1392. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  1393. // 是否显示页眉
  1394. $pdf->setPrintHeader(false);
  1395. // 设置页眉字体
  1396. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  1397. // 页眉距离顶部的距离
  1398. $pdf->SetHeaderMargin('5');
  1399. // 是否显示页脚
  1400. $pdf->setPrintFooter(false);
  1401. // 设置默认等宽字体
  1402. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  1403. // 设置行高
  1404. $pdf->setCellHeightRatio(1);
  1405. // 设置左、上、右的间距
  1406. $pdf->SetMargins('10', '0', '10');
  1407. // 设置是否自动分页 距离底部多少距离时分页
  1408. $pdf->SetAutoPageBreak(TRUE, '15');
  1409. // 设置图像比例因子
  1410. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  1411. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  1412. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  1413. $pdf->setLanguageArray($l);
  1414. }
  1415. $pdf->setFontSubsetting(true);
  1416. $pdf->AddPage();
  1417. // 设置字体
  1418. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  1419. // $image_file = \Yii::$app->basePath . '/../frontendEle/src/static/img/ngds-logo.jpg';
  1420. // if (!file_exists($image_file)) {
  1421. // $image_file = \Yii::$app->runtimePath . '/../common/uploads/ngds-logo.jpg';
  1422. // }
  1423. $image_file = \Yii::$app->basePath . '/../ngds-logo.jpg';
  1424. $image = file_get_contents($image_file);
  1425. $pdf->Image('@' . $image, 15, 12, 20, 7, 'JPG');
  1426. $pdf->writeHTML($context);
  1427. $pdf->Output($realFile, 'F');
  1428. $this->_updateFirst($realFile, 1);
  1429. }
  1430. $this->complete();
  1431. return true;
  1432. }
  1433. /**
  1434. * 生成
  1435. * @return bool
  1436. * @throws Exception
  1437. * @throws InvalidConfigException
  1438. * @throws \yii\httpclient\Exception
  1439. */
  1440. public function actionOrderInvoiceExport() {
  1441. $this->getParams();
  1442. if (!$this->params) {
  1443. throw new Exception('无法获取需要的参数');
  1444. }
  1445. $path = __DS__ . $this->getSaveBasePath() . __DS__ . $this->getSavePath();
  1446. $realFile = $this->mkdir($path) . __DS__ . $this->getFileName('.pdf');
  1447. $this->completed = false;
  1448. $this->getExportId();
  1449. $this->getUserId();
  1450. $fileNameUpdated = false;
  1451. // 获取列表数据及表头
  1452. $this->_listModel = new $this->listModelClass();
  1453. $this->_listModel->isExport = true;
  1454. // 查询订单数据
  1455. // 使用新的查询对象,禁用查询缓存,确保获取最新数据
  1456. $query = OrderGoods::find()
  1457. ->alias('OG')
  1458. ->where($this->params['condition'], $this->params['params'])
  1459. ->select('OG.ID AS AID,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')
  1460. ->join('LEFT JOIN', ORDER::tableName() . ' AS O', 'OG.ORDER_SN=O.SN')
  1461. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  1462. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  1463. ->asArray()
  1464. ->noCache(); // 禁用查询缓存
  1465. // 添加详细的调试信息
  1466. $rawSql = $query->createCommand()->getRawSql();
  1467. $oderList = $query->all();
  1468. $sn = $oderList[0]['SN'];
  1469. // 同时使用原生SQL查询进行对比测试
  1470. $nativeConnection = Yii::$app->db;
  1471. $nativeQuery = "
  1472. SELECT
  1473. `OG`.`ID` AS `AID`,
  1474. `O`.*,
  1475. `U`.`REAL_NAME`,
  1476. `U`.`DEC_ID`,
  1477. `SG`.`CATEGORY_TYPE`,
  1478. `OG`.`REAL_PRICE`,
  1479. `OG`.`TAX_RATE`,
  1480. `OG`.`BUY_NUMS`,
  1481. `OG`.`SKU_CODE`,
  1482. `OG`.`GOODS_TITLE`,
  1483. `OG`.`REAL_PV`
  1484. FROM
  1485. `AR_ORDER_GOODS` `OG`
  1486. LEFT JOIN `AR_ORDER` `O` ON OG.ORDER_SN = O.SN
  1487. LEFT JOIN `AR_USER` `U` ON U.ID = O.USER_ID
  1488. LEFT JOIN `AR_SHOP_GOODS` `SG` ON SG.ID = OG.GOODS_ID
  1489. WHERE
  1490. O.IS_DELETE = 0
  1491. AND O.SN = '{$sn}'
  1492. ORDER BY
  1493. `O`.`CREATED_AT` DESC";
  1494. $nativeResults = $nativeConnection->createCommand($nativeQuery)->queryAll();
  1495. // 如果原生查询有结果但Yii查询没有,使用原生查询结果
  1496. if(count($nativeResults) > 0 && count($oderList) < count($nativeResults)) {
  1497. $oderList = $nativeResults;
  1498. }
  1499. if ($oderList) {
  1500. $userId = '';
  1501. $userName = '';
  1502. $address = '';
  1503. $mobile = $oderList[0]['MOBILE'];
  1504. $orderAt = Date::convert($oderList[0]['CREATED_AT'],'d/m/Y');
  1505. $orderDetails = '';
  1506. $orderDetails_bv = '';
  1507. $orderSn = $oderList[0]['SN'];;
  1508. $orderAmount = 0; // 合计总额
  1509. $orderNums = 0; // 合计总数
  1510. $totalTaxAmount = 0; // 合计税额
  1511. $totalAmount = 0;
  1512. $paymentStatus = 'Paid'; // 支付状态
  1513. $invoiceRemark = $oderList[0]['INVOICE_REMARK'];
  1514. $paymentMethod = $oderList[0]['METHOD']??'Cash'; // 支付方式
  1515. $currency = Currency::findOne($oderList[0]['CURRENCY_ID'])['CODE'] ?? '';;
  1516. $invoiceNo = $oderList[0]['INVOICE_NO'];
  1517. $provinceName = $oderList[0]['PROVINCE'] ? Region::getCnName($oderList[0]['PROVINCE']) : '';
  1518. $cityName = $oderList[0]['CITY'] ? Region::getCnName($oderList[0]['CITY']) : '';
  1519. $countyName = $oderList[0]['COUNTY'] ? Region::getCnName($oderList[0]['COUNTY']) : '';
  1520. $address = $provinceName . $cityName . $countyName . $oderList[0]['ADDRESS'];
  1521. $paymentStatus = \Yii::$app->params['orderStatus'][$oderList[0]['STATUS']]['label'] ?? 'Paid';
  1522. $orderTotalBv = 0;
  1523. $orderTotalTaxAmount = 0;
  1524. $orderTotalAmount = 0;
  1525. $orderTotal = 0;
  1526. $orderTotalQuantity = count($oderList);
  1527. $no = 0;
  1528. foreach ($oderList as $key => $value) {
  1529. $no += 1;
  1530. $userId = $value['USER_NAME'];
  1531. $userName = $value['REAL_NAME'];
  1532. // 总价
  1533. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  1534. $orderAmount += $totalAmount;
  1535. $orderNums += $value['BUY_NUMS'];
  1536. // 税额
  1537. $taxAmount = Tool::calculateTax($value['REAL_PRICE'], $value['TAX_RATE'], $value['BUY_NUMS']);
  1538. $totalTaxAmount += $taxAmount;
  1539. $totalBv = $value['BUY_NUMS'] * $value['REAL_PV'];
  1540. $orderTotalBv += $totalBv;
  1541. $orderTotalTaxAmount += $taxAmount;
  1542. $orderTotalAmount += $totalAmount - $taxAmount;
  1543. $orderTotal += $totalAmount;
  1544. $totalBvFormat = Tool::formatAmount($totalBv);
  1545. $taxableAmountFormat =Tool::formatAmount($taxAmount);
  1546. $vatAmountFormat = Tool::formatAmount($totalAmount - $taxAmount);
  1547. $totalFormat = Tool::formatAmount($totalAmount);
  1548. $vatRateFormat = Tool::formatAmount($value['TAX_RATE']) . '%';
  1549. $unitPriceFormat = Tool::formatAmount($value['REAL_PRICE']);
  1550. $unitBvFormat = Tool::formatAmount($value['REAL_PV']);
  1551. // 订单详情
  1552. //ek-3391 Taxable Amount和VAT Amount字段取值错误(两个取值取反了),需要修改 (2025/11/25 13:35 Ryan)
  1553. $orderDetails .= <<<EOT
  1554. <tr style="text-align: left; font-weight: normal; font-size: 12px; width: 100%; padding: 5px 5px;">
  1555. <td style="white-space: nowrap;">{$no}</td>
  1556. <td>{$value['SKU_CODE']}</td>
  1557. <td>{$value['GOODS_TITLE']}</td>
  1558. <td>{$value['BUY_NUMS']}</td>
  1559. <td>{$unitPriceFormat}</td>
  1560. <!-- <td>{$unitBvFormat}</td>-->
  1561. <!-- <td>{$totalBvFormat}</td>-->
  1562. <td colspan="2">{$vatAmountFormat}</td>
  1563. <td>{$vatRateFormat}</td>
  1564. <td colspan="2">{$taxableAmountFormat}</td>
  1565. <td>{$totalFormat}</td>
  1566. </tr>
  1567. EOT;
  1568. $orderDetails_bv .= <<<EOT
  1569. <tr style="text-align: left; font-weight: normal; font-size: 12px;">
  1570. <td colspan="7" class="table-noborder"></td>
  1571. <td>{$value['SKU_CODE']}</td>
  1572. <td>{$value['BUY_NUMS']}</td>
  1573. <td>{$unitBvFormat}</td>
  1574. <td>{$totalBvFormat}</td>
  1575. </tr>
  1576. EOT;
  1577. }
  1578. $memberCode = Yii::t('app', 'memberCode');
  1579. $memberName = Yii::t('app', 'memberName');
  1580. $memberAddress = Yii::t('app', 'memberAddress');
  1581. $memberPhone = Yii::t('app', 'memberPhone');
  1582. $orderCode = Yii::t('app', 'orderCode');
  1583. $taxInvoice = Yii::t('app', 'taxInvoice');
  1584. $productCode = Yii::t('app', 'productCode');
  1585. $productName = Yii::t('app', 'productName');
  1586. $productPrice = Yii::t('app', 'productPrice');
  1587. $quantity = Yii::t('app', 'qty');
  1588. $taxRate = Yii::t('app', 'taxRate');
  1589. $totalTax = Yii::t('app', 'totalTax');
  1590. $totalAmount = Yii::t('app', 'totalAmount');
  1591. $total = Yii::t('app', 'total');
  1592. $signature = Yii::t('app', 'signature');
  1593. $date = Yii::t('app', 'date');
  1594. $createAt = Yii::t('app', 'createAt');
  1595. $l['a_meta_charset'] = 'UTF-8';
  1596. $l['a_meta_dir'] = 'ltr';
  1597. $l['a_meta_language'] = 'zh';
  1598. $l['w_page'] = '页面';
  1599. $orderAmount = Tool::formatAmount($orderAmount);
  1600. $totalTaxAmount = Tool::formatAmount($totalTaxAmount);
  1601. $orderTotalBv = Tool::formatAmount($orderTotalBv);
  1602. $orderTotalTaxAmount = Tool::formatAmount($orderTotalTaxAmount);
  1603. $orderTotalAmount = Tool::formatAmount($orderTotalAmount);
  1604. $orderTotal = Tool::formatAmount($orderTotal);
  1605. $context = <<<ORDER
  1606. <!doctype html>
  1607. <html lang="en">
  1608. <head>
  1609. <meta charset="UTF-8" />
  1610. <title>{$taxInvoice}</title>
  1611. <style>
  1612. table {
  1613. border-collapse: collapse;
  1614. }
  1615. table td, table th {
  1616. border: 1px solid #ccc;
  1617. border-collapse: collapse;
  1618. }
  1619. .bg {
  1620. background-color: #ccc;
  1621. }
  1622. /* 无边框表格样式类 */
  1623. .table-noborder {
  1624. border-collapse: collapse; /* 合并边框(避免残留间隙) */
  1625. border-spacing: 0; /* 清除单元格之间的默认间隙 */
  1626. empty-cells: show; /* 不影响空单元格(可选,避免意外显示) */
  1627. }
  1628. /* 清除 table、th、td 的所有边框 */
  1629. .table-noborder,
  1630. .table-noborder th,
  1631. .table-noborder td {
  1632. border: none !important; /* !important 可选,用于覆盖冲突样式 */
  1633. outline: none; /* 清除可能的默认轮廓(可选) */
  1634. }
  1635. </style>
  1636. </head>
  1637. <body style="font-weight: normal; margin: 0; padding: 0; line-height: 1.3;">
  1638. <div class="content" style="line-height: 1.3; font-weight: normal;">
  1639. <div style="display: flex; justify-content: space-between; align-items: flex-start; width: 100%;">
  1640. <div style="width: 70%;">
  1641. <p style="text-align: left; font-weight: bold; font-size: 16px; margin: 0 !important; padding: 0 !important; line-height: 1.3 !important;"><b>TAX INVOICE</b></p>
  1642. <p style="text-align: left; font-size: 14px; margin: 0 !important; padding: 0 !important; line-height: 1.3 !important;">ELKEN ARABIYA GENERAL TRADING CO. LLC</p>
  1643. <p style="text-align: left; font-size: 13px; margin: 0 !important; padding: 0 !important; line-height: 1.3 !important;">Shop No.2, Plot 356-1142, Juneirah Street, Umm Suqeim 1, Dubai, UAE</p>
  1644. <p style="text-align: left; font-size: 13px; margin: 0 !important; padding: 0 !important; line-height: 1.3 !important;">Phone: +971042296118</p>
  1645. <p style="text-align: left; font-size: 13px; margin: 0 !important; padding: 0 !important; line-height: 1.3 !important;">License No: 1314319</p>
  1646. <p style="text-align: left; font-size: 13px; margin: 0 !important; padding: 0 !important; line-height: 1.3 !important;">TRN No: 105034822400003</p>
  1647. </div>
  1648. <div style="width: 25%; text-align: right;">
  1649. <!-- 图像将通过TCPDF直接插入 -->
  1650. </div>
  1651. </div>
  1652. <hr />
  1653. <br>
  1654. <br>
  1655. <div style="width: 100%; line-height: 15px; margin-left: 0; margin-right: 0; margin-top: 0; margin-bottom: 0; padding: 0;">
  1656. <table class="table-noborder" width="100%">
  1657. <tr style="text-align: left; font-size: 12px; font-weight: normal; height: 50px; vertical-align: middle;">
  1658. <td>
  1659. Bill to
  1660. </td>
  1661. <td>
  1662. </td>
  1663. </tr>
  1664. <tr style="text-align: left; font-size: 12px; font-weight: normal; height: 50px; vertical-align: middle;">
  1665. <td>
  1666. Name of Distributor: {$userName}
  1667. </td>
  1668. <td>
  1669. Invoice No.: {$invoiceNo}
  1670. </td>
  1671. </tr>
  1672. <tr style="text-align: left; font-size: 12px; font-weight: normal; height: 50px; vertical-align: middle;">
  1673. <td>
  1674. Member ID: {$userId}
  1675. </td>
  1676. <td>
  1677. Order No.: {$orderSn}
  1678. </td>
  1679. </tr>
  1680. <tr style="text-align: left; font-size: 12px; font-weight: normal; height: 50px; vertical-align: middle;">
  1681. <td>
  1682. Address: {$address}
  1683. </td>
  1684. <td>
  1685. Date: {$orderAt}
  1686. </td>
  1687. </tr>
  1688. <tr style="text-align: left; font-size: 12px; font-weight: normal; height: 50px; vertical-align: middle;">
  1689. <td>
  1690. Contact: {$mobile}
  1691. </td>
  1692. <td>
  1693. Payment status: {$paymentStatus}
  1694. </td>
  1695. </tr>
  1696. </table>
  1697. <br />
  1698. <br />
  1699. <br />
  1700. <table style="table-layout: fixed;" width="100%">
  1701. <tr style="text-align: left; font-weight: normal; font-size: 12px;">
  1702. <th style="width: 5%; white-space: nowrap;">No.</th>
  1703. <th style="width: 10%; white-space: normal; word-break: break-word;">Product Code</th>
  1704. <th style="width: 19%; white-space: normal; word-break: break-word;">Product Name</th>
  1705. <th style="width: 8%; white-space: nowrap;">Quantity</th>
  1706. <th style="width: 10%; white-space: normal; word-break: break-word;">Unit Price ({$currency})</th>
  1707. <th style="width: 14%; white-space: normal; word-break: break-word;" colspan="2">Taxable Amount ({$currency})</th>
  1708. <th style="width: 8%; white-space: normal; word-break: break-word;">VAT Rate</th>
  1709. <th style="width: 16%; white-space: normal; word-break: break-word;" colspan="2">VAT Amount ({$currency})</th>
  1710. <th style="width: 10%; white-space: normal; word-break: break-word;">Total ({$currency})</th>
  1711. </tr>
  1712. {$orderDetails}
  1713. </table>
  1714. <table style="table-layout: fixed;" class="table-noborder">
  1715. <tr><td colspan="11"></td></tr>
  1716. <tr><td colspan="11"></td></tr>
  1717. </table>
  1718. <table style="table-layout: fixed;">
  1719. <tr style="text-align: left; font-weight: normal; font-size: 12px;">
  1720. <td class="table-noborder" style="width: 5%; white-space: nowrap;"></td>
  1721. <td style="width: 29%; text-align: left; background-color: lightslategray;" colspan="2">Grand Total</td>
  1722. <td style="width: 8%; text-align: left; background-color: lightslategray;">{$orderTotalQuantity}</td>
  1723. <td style="width: 10%; text-align: left; background-color: lightslategray;"></td>
  1724. <td style="width: 14%; text-align: left; background-color: lightslategray;" colspan="2">{$orderTotalAmount}</td>
  1725. <td style="width: 8%; text-align: left; background-color: lightslategray;"></td>
  1726. <td style="width: 16%; text-align: left; background-color: lightslategray;" colspan="2">{$orderTotalTaxAmount}</td>
  1727. <td style="width: 10%; text-align: left; background-color: lightslategray;">{$orderTotal}</td>
  1728. </tr>
  1729. </table>
  1730. <table style="table-layout: fixed;" class="table-noborder">
  1731. <tr>
  1732. <td colspan="11"></td>
  1733. </tr>
  1734. </table>
  1735. <table style="table-layout: fixed;" width="100%">
  1736. <tr style="text-align: left; font-weight: normal; font-size: 12px;">
  1737. <td colspan="7" class="table-noborder"></td>
  1738. <td style="text-align: left; background-color: lightgoldenrodyellow;" colspan="2" >Total Amount</td>
  1739. <td style="text-align: left; background-color: lightgoldenrodyellow;" colspan="2">{$currency}{$orderAmount}</td>
  1740. </tr>
  1741. </table>
  1742. <table style="table-layout: fixed;" class="table-noborder">
  1743. <tr>
  1744. <td colspan="11"></td>
  1745. </tr>
  1746. </table>
  1747. <table style="table-layout: fixed;" class="table-noborder">
  1748. <tr style="text-align: left; font-weight: normal; font-size: 12px;">
  1749. <td colspan="7"></td>
  1750. <td colspan="2">Method:</td>
  1751. <td colspan="2">{$paymentMethod}</td>
  1752. </tr>
  1753. <tr style="text-align: left; font-weight: normal; font-size: 12px;">
  1754. <td colspan="7"></td>
  1755. <td colspan="2" >Paid amount:</td>
  1756. <td colspan="2">{$currency}{$orderAmount}</td>
  1757. </tr>
  1758. </table>
  1759. <table style="table-layout: fixed;" class="table-noborder">
  1760. <tr>
  1761. <td colspan="11"></td>
  1762. </tr>
  1763. </table>
  1764. <table style="table-layout: fixed;">
  1765. <tr style="text-align: left; font-weight: normal; font-size: 12px; margin-top: 15px;">
  1766. <td colspan="7" class="table-noborder"></td>
  1767. <td>Product Code</td>
  1768. <td>Quantity</td>
  1769. <td>Unit BV</td>
  1770. <td>Total BV</td>
  1771. </tr>
  1772. $orderDetails_bv
  1773. </table>
  1774. <table style="table-layout: fixed;" class="table-noborder">
  1775. <tr><td colspan="11"></td></tr>
  1776. <tr><td colspan="11"></td></tr>
  1777. </table>
  1778. <table style="table-layout: fixed;">
  1779. <tr style="text-align: center; font-weight: normal; font-size: 12px;">
  1780. <td class="table-noborder" colspan="7"></td>
  1781. <td style="text-align: center; background-color: lightslategray;" colspan="2">Grand Total BV</td>
  1782. <td style="text-align: center; background-color: lightslategray;" colspan="2">{$orderTotalBv}</td>
  1783. </tr>
  1784. </table>
  1785. <table style="table-layout: fixed;">
  1786. <tr style="text-align: left; font-weight: normal; font-size: 12px; margin-top: 15px;">
  1787. <td colspan="2" class="table-noborder">Remark:</td>
  1788. <td colspan="9" class="table-noborder"></td>
  1789. </tr>
  1790. <tr style="text-align: left; font-weight: normal; font-size: 12px;">
  1791. <td colspan="6" class="table-noborder">{$invoiceRemark}</td>
  1792. </tr>
  1793. </table>
  1794. <table style="table-layout: fixed;" class="table-noborder">
  1795. <tr>
  1796. <td colspan="11"></td>
  1797. </tr>
  1798. <tr>
  1799. <td colspan="11"></td>
  1800. </tr>
  1801. <tr>
  1802. <td colspan="11"></td>
  1803. </tr>
  1804. <tr>
  1805. <td colspan="11"></td>
  1806. </tr>
  1807. </table>
  1808. <table style="table-layout: fixed;" class="table-noborder">
  1809. <tr style="text-align: left; font-weight: normal; font-size: 12px; margin-top: 15px;">
  1810. <td colspan="4">Issued By:</td>
  1811. <td colspan="4">Picked By:</td>
  1812. <td colspan="3">Received By:</td>
  1813. </tr>
  1814. <tr>
  1815. <td colspan="11"></td>
  1816. </tr>
  1817. </table>
  1818. </div>
  1819. </div>
  1820. </body>
  1821. </html>
  1822. ORDER;
  1823. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  1824. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  1825. // 设置打印模式
  1826. $pdf->SetCreator(PDF_CREATOR);
  1827. $pdf->SetAuthor('DaZe');
  1828. $pdf->SetTitle($orderSn);
  1829. $pdf->SetSubject('TCPDF Tutorial');
  1830. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  1831. // 是否显示页眉
  1832. $pdf->setPrintHeader(false);
  1833. // 设置页眉字体
  1834. $pdf->setHeaderFont(Array('Helvetica', '', '12'));
  1835. // 页眉距离顶部的距离
  1836. $pdf->SetHeaderMargin('5');
  1837. // 是否显示页脚
  1838. $pdf->setPrintFooter(false);
  1839. // 设置默认等宽字体
  1840. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  1841. // 设置行高
  1842. // $pdf->setCellHeightRatio(1);
  1843. // 设置左、上、右的间距
  1844. $pdf->SetMargins('10', '0', '10');
  1845. // 设置是否自动分页 距离底部多少距离时分页
  1846. $pdf->SetAutoPageBreak(TRUE, '15');
  1847. // 设置图像比例因子
  1848. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  1849. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  1850. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  1851. $pdf->setLanguageArray($l);
  1852. }
  1853. $pdf->setFontSubsetting(true);
  1854. $pdf->AddPage();
  1855. // 设置字体
  1856. $pdf->SetFont('Helvetica', '', 10, '', true);
  1857. // $image_file = \Yii::$app->basePath . '/../ngds-logo.jpg';
  1858. // $image = file_get_contents($image_file);
  1859. // $pdf->Image('@' . $image, 15, 12, 20, 7, 'JPG');
  1860. $pdf->writeHTML($context);
  1861. $result = $pdf->Output($realFile, 'F');
  1862. $this->_updateFirst($realFile, 1);
  1863. }
  1864. $this->complete();
  1865. return true;
  1866. }
  1867. }