BaseExport.php 92 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017
  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,OG.STANDARD_PRICE,CS.NAME AS COUNTRY,CY.CODE AS CURRENCY, O.DEC_USER_ID AS ORDER_DEC_ID')
  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,OG.STANDARD_PRICE,CS.NAME AS COUNTRY,CY.CODE AS CURRENCY, O.DEC_USER_ID AS ORDER_DEC_ID')
  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. 'COUNTRY' => $columnData['COUNTRY'],
  1162. 'CURRENCY' => $columnData['CURRENCY'],
  1163. 'TEL' => "\t{$columnData['TEL']}",
  1164. 'PROVINCE' => $regionConfig[$columnData['PROVINCE']]['REGION_NAME'] ?? '',
  1165. 'CITY' => $regionConfig[$columnData['CITY']]['REGION_NAME'] ?? '',
  1166. 'COUNTY' => $regionConfig[$columnData['COUNTY']]['REGION_NAME'] ?? '',
  1167. 'ADDRESS' => $columnData['ADDRESS'],
  1168. 'PERIOD_NUM' => $columnData['PERIOD_NUM'],
  1169. 'ORDER_TYPE' => ($columnData['ORDER_TYPE'] == 'ZC') ? 'Welcome pack' : (in_array($columnData['PAY_TYPE'], ['cash', 'pay_stack']) ? 'Reselling': 'Points'),
  1170. 'CREATED_AT' => Date('Y-m-d H:i:s', $columnData['CREATED_AT']),
  1171. 'PAY_TYPE' => ShopGoods::payTypes()[$columnData['PAY_TYPE']]['name'] ?? ShopGoods::payTypes()['cash']['name'],
  1172. 'PAY_AT' => $columnData['PAY_AT'] > 0 ? Date('Y-m-d H:i:s', $columnData['PAY_AT']) : '',
  1173. 'DELIVERY_AT' => $columnData['DELIVERY_AT'] > 0 ? Date('Y-m-d H:i:s', $columnData['DELIVERY_AT']) : '',
  1174. 'REAL_PRICE' => $columnData['REAL_PRICE'] ?? 0,
  1175. 'REAL_PV' => $columnData['REAL_PV'] ?? 0,
  1176. 'PAY_FREIGHT' => $columnData['PAY_FREIGHT'] ?? 0,
  1177. 'TAX_RATE' => $columnData['TAX_RATE'],
  1178. 'TAX_AMOUNT' => Tool::calculateTax($columnData['REAL_PRICE'], $columnData['TAX_RATE'], $columnData['BUY_NUMS']),
  1179. 'EXPRESS_COMPANY' => $columnData['EXPRESS_COMPANY'],
  1180. 'ORDER_TRACK_NO' => $columnData['ORDER_TRACK_NO'],
  1181. 'EXPRESS_TYPE' => $columnData['EXPRESS_TYPE'] == 0 ? 'mailing ':' auto pick',
  1182. 'FRONT_REMARK' => $columnData['FRONT_REMARK'],
  1183. 'DELIVERY_STATUS_NAME' => Tool::paramConvert(\Yii::$app->params['deliveryStatus'])[$columnData['DELIVERY_STATUS']]['label'] ?? '',
  1184. ];
  1185. fputcsv($this->_fp, Tool::arrTextConvert($columnAccept));
  1186. unset($percent, $columnData, $columnAccept);
  1187. }
  1188. unset($list);
  1189. }
  1190. unset($list);
  1191. return 'finish';
  1192. }
  1193. /**
  1194. * 生成
  1195. * @return bool
  1196. * @throws Exception
  1197. * @throws InvalidConfigException
  1198. * @throws \yii\httpclient\Exception
  1199. */
  1200. public function generateBaOrderPDF()
  1201. {
  1202. $this->getParams();
  1203. if (!$this->params) {
  1204. throw new Exception('无法获取需要的参数');
  1205. }
  1206. $path = __DS__ . $this->getSaveBasePath() . __DS__ . $this->getSavePath();
  1207. $realFile = $path . __DS__ . $this->getFileName('.pdf');
  1208. $this->completed = false;
  1209. $this->getExportId();
  1210. $this->getUserId();
  1211. $fileNameUpdated = false;
  1212. // 获取列表数据及表头
  1213. $this->_listModel = new $this->listModelClass();
  1214. $this->_listModel->isExport = true;
  1215. // 查询订单数据
  1216. $orderQuery = BaOrder::find()
  1217. ->alias('O')
  1218. ->where($this->params['condition'], $this->params['params'])
  1219. ->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')
  1220. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'U.ID=O.USER_ID')
  1221. ->join('LEFT JOIN', BaOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  1222. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  1223. ->orderBy('O.CREATED_AT DESC');
  1224. // 订单中间表只查询待支付和支付失败的订单
  1225. $this->params['params'][':NOT_PAID'] = \Yii::$app->params['orderStatus']['notPaid']['value']; // 待支付
  1226. $this->params['params'][':FAIL_PAID'] = \Yii::$app->params['orderStatus']['failPaid']['value']; // 支付失败
  1227. $orderStandardQuery = BaApproachOrder::find()
  1228. ->alias('O')
  1229. ->where($this->params['condition'] . ' AND (O.STATUS = :NOT_PAID OR O.STATUS = :FAIL_PAID)', $this->params['params'])
  1230. ->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')
  1231. ->join('LEFT JOIN', BaUser::tableName() . ' AS U', 'U.ID=O.USER_ID')
  1232. ->join('LEFT JOIN', BaApproachOrderGoods::tableName() . ' AS OG', 'OG.ORDER_SN=O.SN')
  1233. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  1234. ->orderBy('O.CREATED_AT DESC');
  1235. $queryAll = $orderQuery->union($orderStandardQuery, true);
  1236. $query = (new Query())->from(['Q' => $queryAll])->select('Q.*')->distinct()->orderBy(['CREATED_AT' => SORT_DESC]);
  1237. $oderList = $query->all();
  1238. if ($oderList) {
  1239. $userId = '';
  1240. $userName = '';
  1241. $address = '';
  1242. $mobile = '';
  1243. $orderAt = '';
  1244. $orderDetails = '';
  1245. $orderSn = '';
  1246. $orderAmount = 0; // 合计总额
  1247. $orderNums = 0; // 合计总数
  1248. $totalTaxAmount = 0; // 合计税额
  1249. $totalAmount = 0;
  1250. foreach ($oderList as $key => $value) {
  1251. $provinceName = $value['PROVINCE'] ? Region::getCnName($value['PROVINCE']) : '';
  1252. $cityName = $value['CITY'] ? Region::getCnName($value['CITY']) : '';
  1253. $countyName = $value['COUNTY'] ? Region::getCnName($value['COUNTY']) : '';
  1254. $userId = $value['USER_NAME'];
  1255. $userName = $value['REAL_NAME'];
  1256. $address = $provinceName . $cityName . $countyName . $value['ADDRESS'];
  1257. $mobile = $value['MOBILE'];
  1258. $orderAt = Date::convert($value['CREATED_AT'],'Y-m-d H:i:s');
  1259. $orderSn = $value['SN'];
  1260. // 总价
  1261. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  1262. $orderAmount += $totalAmount;
  1263. $orderNums += $value['BUY_NUMS'];
  1264. // 税额
  1265. $taxAmount = Tool::calculateTax($value['REAL_PRICE'], $value['TAX_RATE'], $value['BUY_NUMS']);
  1266. $totalTaxAmount += $taxAmount;
  1267. $taxAmount = Tool::formatAmount($taxAmount);
  1268. $totalAmount = Tool::formatAmount($totalAmount);
  1269. // 订单详情
  1270. $orderDetails .= <<<EOT
  1271. <tr>
  1272. <td>{$value['SKU_CODE']}</td>
  1273. <td>{$value['GOODS_TITLE']}</td>
  1274. <td style="text-align: right;">{$value['REAL_PRICE']}</td>
  1275. <td>{$value['BUY_NUMS']}</td>
  1276. <td style="text-align: right;">{$value['TAX_RATE']}</td>
  1277. <td style="text-align: right;">{$taxAmount}</td>
  1278. <td style="text-align: right;">{$totalAmount}</td>
  1279. </tr>
  1280. EOT;
  1281. }
  1282. // 订单基本信息
  1283. $orderBase = <<<ORDER
  1284. <table border="1" style="table-layout: fixed; padding: 10px 20px;" width="100%">
  1285. <tr>
  1286. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">Member Code</td>
  1287. <td width="70%">{$userId}</td>
  1288. </tr>
  1289. <tr>
  1290. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">Member Name</td>
  1291. <td width="70%">{$userName}</td>
  1292. </tr>
  1293. <tr>
  1294. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">Member Address</td>
  1295. <td width="70%">{$address}</td>
  1296. </tr>
  1297. <tr>
  1298. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">Member Phone</td>
  1299. <td width="70%">{$mobile}</td>
  1300. </tr>
  1301. <tr>
  1302. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">Order Code</td>
  1303. <td width="70%">{$orderSn}</td>
  1304. </tr>
  1305. <tr>
  1306. <td width="30%" style="font-weight: bold; text-align: center; font-size: 14px;">Creation Time</td>
  1307. <td width="70%">{$orderAt}</td>
  1308. </tr>
  1309. <tr>
  1310. <td class="bg" style="font-weight: bold; font-size: 14px; text-align: center;">Order Detail</td>
  1311. <td class="bg"></td>
  1312. </tr>
  1313. </table>
  1314. ORDER;
  1315. $l['a_meta_charset'] = 'UTF-8';
  1316. $l['a_meta_dir'] = 'ltr';
  1317. $l['a_meta_language'] = 'zh';
  1318. $l['w_page'] = '页面';
  1319. $orderAmount = Tool::formatAmount($orderAmount);
  1320. $totalTaxAmount = Tool::formatAmount($totalTaxAmount);
  1321. $context = <<<ORDER
  1322. <!doctype html>
  1323. <html lang="en">
  1324. <head>
  1325. <meta charset="UTF-8" />
  1326. <title>Order detail</title>
  1327. <style>
  1328. table {
  1329. border-collapse: collapse;
  1330. }
  1331. table td, table th {
  1332. border: 1px solid #ccc;
  1333. padding: 5px 5px;
  1334. border-collapse: collapse;
  1335. }
  1336. /*td {*/
  1337. /* padding: 120px;*/
  1338. /*}*/
  1339. .bg {
  1340. background-color: #ccc;
  1341. }
  1342. </style>
  1343. </head>
  1344. <body>
  1345. <div class="content">
  1346. <p style="text-align: center; font-weight: bold; font-size: 22px;"><b>Order detail</b><br></p>
  1347. <div>
  1348. <div style="display: block; width: 100%;">
  1349. {$orderBase}
  1350. <table border="1" width="100%" style="padding: 10px 5px; text-align: center;">
  1351. <tr>
  1352. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">Product Code</th>
  1353. <th width="25%" style="font-size: 14px; font-weight: bold; text-align: center;">Product Name</th>
  1354. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">Product Price</th>
  1355. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">Qty</th>
  1356. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">Tax Rate</th>
  1357. <th width="10%" style="font-size: 14px; font-weight: bold; text-align: center;">Tax</th>
  1358. <th width="15%" style="font-size: 14px; font-weight: bold; text-align: center;">Total Amount</th>
  1359. </tr>
  1360. {$orderDetails}
  1361. <tr>
  1362. <td colspan="3">Total</td>
  1363. <td>{$orderNums}</td>
  1364. <td></td>
  1365. <td style="text-align: right;">{$totalTaxAmount}</td>
  1366. <td style="text-align: right;">{$orderAmount}</td>
  1367. </tr>
  1368. </table>
  1369. </div>
  1370. <div style="width: 100%; margin-top: 50px; height: 30px;">
  1371. <table width="100%" style="border: none; padding: 10px 20px; text-align: center;">
  1372. <tr style="border: none;">
  1373. <td width="70%" style="border: none;"></td>
  1374. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">Signature:</td>
  1375. </tr>
  1376. <tr style="border: none;">
  1377. <td width="70%" style="border: none;"></td>
  1378. <td width="30%" style="font-weight: bold; text-align: left; font-size: 14px; border: none;">Date:</td>
  1379. </tr>
  1380. </table>
  1381. </div>
  1382. </div>
  1383. </div>
  1384. </body>
  1385. </html>
  1386. ORDER;
  1387. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  1388. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  1389. // 设置打印模式
  1390. $pdf->SetCreator(PDF_CREATOR);
  1391. $pdf->SetAuthor('DaZe');
  1392. $pdf->SetTitle($orderSn);
  1393. $pdf->SetSubject('TCPDF Tutorial');
  1394. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  1395. // 是否显示页眉
  1396. $pdf->setPrintHeader(false);
  1397. // 设置页眉字体
  1398. $pdf->setHeaderFont(Array('dejavusans', '', '12'));
  1399. // 页眉距离顶部的距离
  1400. $pdf->SetHeaderMargin('5');
  1401. // 是否显示页脚
  1402. $pdf->setPrintFooter(false);
  1403. // 设置默认等宽字体
  1404. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  1405. // 设置行高
  1406. $pdf->setCellHeightRatio(1);
  1407. // 设置左、上、右的间距
  1408. $pdf->SetMargins('10', '0', '10');
  1409. // 设置是否自动分页 距离底部多少距离时分页
  1410. $pdf->SetAutoPageBreak(TRUE, '15');
  1411. // 设置图像比例因子
  1412. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  1413. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  1414. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  1415. $pdf->setLanguageArray($l);
  1416. }
  1417. $pdf->setFontSubsetting(true);
  1418. $pdf->AddPage();
  1419. // 设置字体
  1420. $pdf->SetFont('stsongstdlight', '', 10, '', true);
  1421. // $image_file = \Yii::$app->basePath . '/../frontendEle/src/static/img/ngds-logo.jpg';
  1422. // if (!file_exists($image_file)) {
  1423. // $image_file = \Yii::$app->runtimePath . '/../common/uploads/ngds-logo.jpg';
  1424. // }
  1425. $image_file = \Yii::$app->basePath . '/../ngds-logo.jpg';
  1426. $image = file_get_contents($image_file);
  1427. $pdf->Image('@' . $image, 15, 12, 20, 7, 'JPG');
  1428. $pdf->writeHTML($context);
  1429. $pdf->Output($realFile, 'F');
  1430. $this->_updateFirst($realFile, 1);
  1431. }
  1432. $this->complete();
  1433. return true;
  1434. }
  1435. /**
  1436. * 生成
  1437. * @return bool
  1438. * @throws Exception
  1439. * @throws InvalidConfigException
  1440. * @throws \yii\httpclient\Exception
  1441. */
  1442. public function actionOrderInvoiceExport() {
  1443. $this->getParams();
  1444. if (!$this->params) {
  1445. throw new Exception('无法获取需要的参数');
  1446. }
  1447. $path = __DS__ . $this->getSaveBasePath() . __DS__ . $this->getSavePath();
  1448. $realFile = $this->mkdir($path) . __DS__ . $this->getFileName('.pdf');
  1449. $this->completed = false;
  1450. $this->getExportId();
  1451. $this->getUserId();
  1452. $fileNameUpdated = false;
  1453. // 获取列表数据及表头
  1454. $this->_listModel = new $this->listModelClass();
  1455. $this->_listModel->isExport = true;
  1456. // 查询订单数据
  1457. // 使用新的查询对象,禁用查询缓存,确保获取最新数据
  1458. $query = OrderGoods::find()
  1459. ->alias('OG')
  1460. ->where($this->params['condition'], $this->params['params'])
  1461. ->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')
  1462. ->join('LEFT JOIN', ORDER::tableName() . ' AS O', 'OG.ORDER_SN=O.SN')
  1463. ->join('LEFT JOIN', User::tableName() . ' AS U', 'U.ID=O.USER_ID')
  1464. ->join('LEFT JOIN', ShopGoods::tableName() . ' AS SG', 'SG.ID=OG.GOODS_ID')
  1465. ->asArray()
  1466. ->noCache(); // 禁用查询缓存
  1467. // 添加详细的调试信息
  1468. $rawSql = $query->createCommand()->getRawSql();
  1469. $oderList = $query->all();
  1470. $sn = $oderList[0]['SN'];
  1471. // 同时使用原生SQL查询进行对比测试
  1472. $nativeConnection = Yii::$app->db;
  1473. $nativeQuery = "
  1474. SELECT
  1475. `OG`.`ID` AS `AID`,
  1476. `O`.*,
  1477. `U`.`REAL_NAME`,
  1478. `U`.`DEC_ID`,
  1479. `SG`.`CATEGORY_TYPE`,
  1480. `OG`.`REAL_PRICE`,
  1481. `OG`.`TAX_RATE`,
  1482. `OG`.`BUY_NUMS`,
  1483. `OG`.`SKU_CODE`,
  1484. `OG`.`GOODS_TITLE`,
  1485. `OG`.`REAL_PV`
  1486. FROM
  1487. `AR_ORDER_GOODS` `OG`
  1488. LEFT JOIN `AR_ORDER` `O` ON OG.ORDER_SN = O.SN
  1489. LEFT JOIN `AR_USER` `U` ON U.ID = O.USER_ID
  1490. LEFT JOIN `AR_SHOP_GOODS` `SG` ON SG.ID = OG.GOODS_ID
  1491. WHERE
  1492. O.IS_DELETE = 0
  1493. AND O.SN = '{$sn}'
  1494. ORDER BY
  1495. `O`.`CREATED_AT` DESC";
  1496. $nativeResults = $nativeConnection->createCommand($nativeQuery)->queryAll();
  1497. // 如果原生查询有结果但Yii查询没有,使用原生查询结果
  1498. if(count($nativeResults) > 0 && count($oderList) < count($nativeResults)) {
  1499. $oderList = $nativeResults;
  1500. }
  1501. if ($oderList) {
  1502. $userId = '';
  1503. $userName = '';
  1504. $address = '';
  1505. $mobile = $oderList[0]['MOBILE'];
  1506. $orderAt = Date::convert($oderList[0]['CREATED_AT'],'d/m/Y');
  1507. $orderDetails = '';
  1508. $orderDetails_bv = '';
  1509. $orderSn = $oderList[0]['SN'];;
  1510. $orderAmount = 0; // 合计总额
  1511. $orderNums = 0; // 合计总数
  1512. $totalTaxAmount = 0; // 合计税额
  1513. $totalAmount = 0;
  1514. $paymentStatus = 'Paid'; // 支付状态
  1515. $invoiceRemark = $oderList[0]['INVOICE_REMARK'];
  1516. $paymentMethod = $oderList[0]['METHOD']??'Cash'; // 支付方式
  1517. $currency = Currency::findOne($oderList[0]['CURRENCY_ID'])['CODE'] ?? '';;
  1518. $invoiceNo = $oderList[0]['INVOICE_NO'];
  1519. $provinceName = $oderList[0]['PROVINCE'] ? Region::getCnName($oderList[0]['PROVINCE']) : '';
  1520. $cityName = $oderList[0]['CITY'] ? Region::getCnName($oderList[0]['CITY']) : '';
  1521. $countyName = $oderList[0]['COUNTY'] ? Region::getCnName($oderList[0]['COUNTY']) : '';
  1522. $address = $provinceName . $cityName . $countyName . $oderList[0]['ADDRESS'];
  1523. $paymentStatus = \Yii::$app->params['orderStatus'][$oderList[0]['STATUS']]['label'] ?? 'Paid';
  1524. $orderTotalBv = 0;
  1525. $orderTotalTaxAmount = 0;
  1526. $orderTotalAmount = 0;
  1527. $orderTotal = 0;
  1528. $orderTotalQuantity = count($oderList);
  1529. $no = 0;
  1530. foreach ($oderList as $key => $value) {
  1531. $no += 1;
  1532. $userId = $value['USER_NAME'];
  1533. $userName = $value['REAL_NAME'];
  1534. // 总价
  1535. $totalAmount = $value['BUY_NUMS'] * $value['REAL_PRICE'];
  1536. $orderAmount += $totalAmount;
  1537. $orderNums += $value['BUY_NUMS'];
  1538. // 税额
  1539. $taxAmount = Tool::calculateTax($value['REAL_PRICE'], $value['TAX_RATE'], $value['BUY_NUMS']);
  1540. $totalTaxAmount += $taxAmount;
  1541. $totalBv = $value['BUY_NUMS'] * $value['REAL_PV'];
  1542. $orderTotalBv += $totalBv;
  1543. $orderTotalTaxAmount += $taxAmount;
  1544. $orderTotalAmount += $totalAmount - $taxAmount;
  1545. $orderTotal += $totalAmount;
  1546. $totalBvFormat = Tool::formatAmount($totalBv);
  1547. $taxableAmountFormat =Tool::formatAmount($taxAmount);
  1548. $vatAmountFormat = Tool::formatAmount($totalAmount - $taxAmount);
  1549. $totalFormat = Tool::formatAmount($totalAmount);
  1550. $vatRateFormat = Tool::formatAmount($value['TAX_RATE']) . '%';
  1551. $unitPriceFormat = Tool::formatAmount($value['REAL_PRICE']);
  1552. $unitBvFormat = Tool::formatAmount($value['REAL_PV']);
  1553. // 订单详情
  1554. //ek-3391 Taxable Amount和VAT Amount字段取值错误(两个取值取反了),需要修改 (2025/11/25 13:35 Ryan)
  1555. $orderDetails .= <<<EOT
  1556. <tr style="text-align: left; font-weight: normal; font-size: 12px; width: 100%; padding: 5px 5px;">
  1557. <td style="white-space: nowrap;">{$no}</td>
  1558. <td>{$value['SKU_CODE']}</td>
  1559. <td>{$value['GOODS_TITLE']}</td>
  1560. <td>{$value['BUY_NUMS']}</td>
  1561. <td>{$unitPriceFormat}</td>
  1562. <!-- <td>{$unitBvFormat}</td>-->
  1563. <!-- <td>{$totalBvFormat}</td>-->
  1564. <td colspan="2">{$vatAmountFormat}</td>
  1565. <td>{$vatRateFormat}</td>
  1566. <td colspan="2">{$taxableAmountFormat}</td>
  1567. <td>{$totalFormat}</td>
  1568. </tr>
  1569. EOT;
  1570. $orderDetails_bv .= <<<EOT
  1571. <tr style="text-align: left; font-weight: normal; font-size: 12px;">
  1572. <td colspan="7" class="table-noborder"></td>
  1573. <td>{$value['SKU_CODE']}</td>
  1574. <td>{$value['BUY_NUMS']}</td>
  1575. <td>{$unitBvFormat}</td>
  1576. <td>{$totalBvFormat}</td>
  1577. </tr>
  1578. EOT;
  1579. }
  1580. $memberCode = Yii::t('app', 'memberCode');
  1581. $memberName = Yii::t('app', 'memberName');
  1582. $memberAddress = Yii::t('app', 'memberAddress');
  1583. $memberPhone = Yii::t('app', 'memberPhone');
  1584. $orderCode = Yii::t('app', 'orderCode');
  1585. $taxInvoice = Yii::t('app', 'taxInvoice');
  1586. $productCode = Yii::t('app', 'productCode');
  1587. $productName = Yii::t('app', 'productName');
  1588. $productPrice = Yii::t('app', 'productPrice');
  1589. $quantity = Yii::t('app', 'qty');
  1590. $taxRate = Yii::t('app', 'taxRate');
  1591. $totalTax = Yii::t('app', 'totalTax');
  1592. $totalAmount = Yii::t('app', 'totalAmount');
  1593. $total = Yii::t('app', 'total');
  1594. $signature = Yii::t('app', 'signature');
  1595. $date = Yii::t('app', 'date');
  1596. $createAt = Yii::t('app', 'createAt');
  1597. $l['a_meta_charset'] = 'UTF-8';
  1598. $l['a_meta_dir'] = 'ltr';
  1599. $l['a_meta_language'] = 'zh';
  1600. $l['w_page'] = '页面';
  1601. $orderAmount = Tool::formatAmount($orderAmount);
  1602. $totalTaxAmount = Tool::formatAmount($totalTaxAmount);
  1603. $orderTotalBv = Tool::formatAmount($orderTotalBv);
  1604. $orderTotalTaxAmount = Tool::formatAmount($orderTotalTaxAmount);
  1605. $orderTotalAmount = Tool::formatAmount($orderTotalAmount);
  1606. $orderTotal = Tool::formatAmount($orderTotal);
  1607. $context = <<<ORDER
  1608. <!doctype html>
  1609. <html lang="en">
  1610. <head>
  1611. <meta charset="UTF-8" />
  1612. <title>{$taxInvoice}</title>
  1613. <style>
  1614. table {
  1615. border-collapse: collapse;
  1616. }
  1617. table td, table th {
  1618. border: 1px solid #ccc;
  1619. border-collapse: collapse;
  1620. }
  1621. .bg {
  1622. background-color: #ccc;
  1623. }
  1624. /* 无边框表格样式类 */
  1625. .table-noborder {
  1626. border-collapse: collapse; /* 合并边框(避免残留间隙) */
  1627. border-spacing: 0; /* 清除单元格之间的默认间隙 */
  1628. empty-cells: show; /* 不影响空单元格(可选,避免意外显示) */
  1629. }
  1630. /* 清除 table、th、td 的所有边框 */
  1631. .table-noborder,
  1632. .table-noborder th,
  1633. .table-noborder td {
  1634. border: none !important; /* !important 可选,用于覆盖冲突样式 */
  1635. outline: none; /* 清除可能的默认轮廓(可选) */
  1636. }
  1637. </style>
  1638. </head>
  1639. <body style="font-weight: normal; margin: 0; padding: 0; line-height: 1.3;">
  1640. <div class="content" style="line-height: 1.3; font-weight: normal;">
  1641. <div style="display: flex; justify-content: space-between; align-items: flex-start; width: 100%;">
  1642. <div style="width: 70%;">
  1643. <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>
  1644. <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>
  1645. <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>
  1646. <p style="text-align: left; font-size: 13px; margin: 0 !important; padding: 0 !important; line-height: 1.3 !important;">Phone: +971042296118</p>
  1647. <p style="text-align: left; font-size: 13px; margin: 0 !important; padding: 0 !important; line-height: 1.3 !important;">License No: 1314319</p>
  1648. <p style="text-align: left; font-size: 13px; margin: 0 !important; padding: 0 !important; line-height: 1.3 !important;">TRN No: 105034822400003</p>
  1649. </div>
  1650. <div style="width: 25%; text-align: right;">
  1651. <!-- 图像将通过TCPDF直接插入 -->
  1652. </div>
  1653. </div>
  1654. <hr />
  1655. <br>
  1656. <br>
  1657. <div style="width: 100%; line-height: 15px; margin-left: 0; margin-right: 0; margin-top: 0; margin-bottom: 0; padding: 0;">
  1658. <table class="table-noborder" width="100%">
  1659. <tr style="text-align: left; font-size: 12px; font-weight: normal; height: 50px; vertical-align: middle;">
  1660. <td>
  1661. Bill to
  1662. </td>
  1663. <td>
  1664. </td>
  1665. </tr>
  1666. <tr style="text-align: left; font-size: 12px; font-weight: normal; height: 50px; vertical-align: middle;">
  1667. <td>
  1668. Name of Distributor: {$userName}
  1669. </td>
  1670. <td>
  1671. Invoice No.: {$invoiceNo}
  1672. </td>
  1673. </tr>
  1674. <tr style="text-align: left; font-size: 12px; font-weight: normal; height: 50px; vertical-align: middle;">
  1675. <td>
  1676. Member ID: {$userId}
  1677. </td>
  1678. <td>
  1679. Order No.: {$orderSn}
  1680. </td>
  1681. </tr>
  1682. <tr style="text-align: left; font-size: 12px; font-weight: normal; height: 50px; vertical-align: middle;">
  1683. <td>
  1684. Address: {$address}
  1685. </td>
  1686. <td>
  1687. Date: {$orderAt}
  1688. </td>
  1689. </tr>
  1690. <tr style="text-align: left; font-size: 12px; font-weight: normal; height: 50px; vertical-align: middle;">
  1691. <td>
  1692. Contact: {$mobile}
  1693. </td>
  1694. <td>
  1695. Payment status: {$paymentStatus}
  1696. </td>
  1697. </tr>
  1698. </table>
  1699. <br />
  1700. <br />
  1701. <br />
  1702. <table style="table-layout: fixed;" width="100%">
  1703. <tr style="text-align: left; font-weight: normal; font-size: 12px;">
  1704. <th style="width: 5%; white-space: nowrap;">No.</th>
  1705. <th style="width: 10%; white-space: normal; word-break: break-word;">Product Code</th>
  1706. <th style="width: 19%; white-space: normal; word-break: break-word;">Product Name</th>
  1707. <th style="width: 8%; white-space: nowrap;">Quantity</th>
  1708. <th style="width: 10%; white-space: normal; word-break: break-word;">Unit Price ({$currency})</th>
  1709. <th style="width: 14%; white-space: normal; word-break: break-word;" colspan="2">Taxable Amount ({$currency})</th>
  1710. <th style="width: 8%; white-space: normal; word-break: break-word;">VAT Rate</th>
  1711. <th style="width: 16%; white-space: normal; word-break: break-word;" colspan="2">VAT Amount ({$currency})</th>
  1712. <th style="width: 10%; white-space: normal; word-break: break-word;">Total ({$currency})</th>
  1713. </tr>
  1714. {$orderDetails}
  1715. </table>
  1716. <table style="table-layout: fixed;" class="table-noborder">
  1717. <tr><td colspan="11"></td></tr>
  1718. <tr><td colspan="11"></td></tr>
  1719. </table>
  1720. <table style="table-layout: fixed;">
  1721. <tr style="text-align: left; font-weight: normal; font-size: 12px;">
  1722. <td class="table-noborder" style="width: 5%; white-space: nowrap;"></td>
  1723. <td style="width: 29%; text-align: left; background-color: lightslategray;" colspan="2">Grand Total</td>
  1724. <td style="width: 8%; text-align: left; background-color: lightslategray;">{$orderTotalQuantity}</td>
  1725. <td style="width: 10%; text-align: left; background-color: lightslategray;"></td>
  1726. <td style="width: 14%; text-align: left; background-color: lightslategray;" colspan="2">{$orderTotalAmount}</td>
  1727. <td style="width: 8%; text-align: left; background-color: lightslategray;"></td>
  1728. <td style="width: 16%; text-align: left; background-color: lightslategray;" colspan="2">{$orderTotalTaxAmount}</td>
  1729. <td style="width: 10%; text-align: left; background-color: lightslategray;">{$orderTotal}</td>
  1730. </tr>
  1731. </table>
  1732. <table style="table-layout: fixed;" class="table-noborder">
  1733. <tr>
  1734. <td colspan="11"></td>
  1735. </tr>
  1736. </table>
  1737. <table style="table-layout: fixed;" width="100%">
  1738. <tr style="text-align: left; font-weight: normal; font-size: 12px;">
  1739. <td colspan="7" class="table-noborder"></td>
  1740. <td style="text-align: left; background-color: lightgoldenrodyellow;" colspan="2" >Total Amount</td>
  1741. <td style="text-align: left; background-color: lightgoldenrodyellow;" colspan="2">{$currency}{$orderAmount}</td>
  1742. </tr>
  1743. </table>
  1744. <table style="table-layout: fixed;" class="table-noborder">
  1745. <tr>
  1746. <td colspan="11"></td>
  1747. </tr>
  1748. </table>
  1749. <table style="table-layout: fixed;" class="table-noborder">
  1750. <tr style="text-align: left; font-weight: normal; font-size: 12px;">
  1751. <td colspan="7"></td>
  1752. <td colspan="2">Method:</td>
  1753. <td colspan="2">{$paymentMethod}</td>
  1754. </tr>
  1755. <tr style="text-align: left; font-weight: normal; font-size: 12px;">
  1756. <td colspan="7"></td>
  1757. <td colspan="2" >Paid amount:</td>
  1758. <td colspan="2">{$currency}{$orderAmount}</td>
  1759. </tr>
  1760. </table>
  1761. <table style="table-layout: fixed;" class="table-noborder">
  1762. <tr>
  1763. <td colspan="11"></td>
  1764. </tr>
  1765. </table>
  1766. <table style="table-layout: fixed;">
  1767. <tr style="text-align: left; font-weight: normal; font-size: 12px; margin-top: 15px;">
  1768. <td colspan="7" class="table-noborder"></td>
  1769. <td>Product Code</td>
  1770. <td>Quantity</td>
  1771. <td>Unit BV</td>
  1772. <td>Total BV</td>
  1773. </tr>
  1774. $orderDetails_bv
  1775. </table>
  1776. <table style="table-layout: fixed;" class="table-noborder">
  1777. <tr><td colspan="11"></td></tr>
  1778. <tr><td colspan="11"></td></tr>
  1779. </table>
  1780. <table style="table-layout: fixed;">
  1781. <tr style="text-align: center; font-weight: normal; font-size: 12px;">
  1782. <td class="table-noborder" colspan="7"></td>
  1783. <td style="text-align: center; background-color: lightslategray;" colspan="2">Grand Total BV</td>
  1784. <td style="text-align: center; background-color: lightslategray;" colspan="2">{$orderTotalBv}</td>
  1785. </tr>
  1786. </table>
  1787. <table style="table-layout: fixed;">
  1788. <tr style="text-align: left; font-weight: normal; font-size: 12px; margin-top: 15px;">
  1789. <td colspan="2" class="table-noborder">Remark:</td>
  1790. <td colspan="9" class="table-noborder"></td>
  1791. </tr>
  1792. <tr style="text-align: left; font-weight: normal; font-size: 12px;">
  1793. <td colspan="6" class="table-noborder">{$invoiceRemark}</td>
  1794. </tr>
  1795. </table>
  1796. <table style="table-layout: fixed;" class="table-noborder">
  1797. <tr>
  1798. <td colspan="11"></td>
  1799. </tr>
  1800. <tr>
  1801. <td colspan="11"></td>
  1802. </tr>
  1803. <tr>
  1804. <td colspan="11"></td>
  1805. </tr>
  1806. <tr>
  1807. <td colspan="11"></td>
  1808. </tr>
  1809. </table>
  1810. <table style="table-layout: fixed;" class="table-noborder">
  1811. <tr style="text-align: left; font-weight: normal; font-size: 12px; margin-top: 15px;">
  1812. <td colspan="4">Issued By:</td>
  1813. <td colspan="4">Picked By:</td>
  1814. <td colspan="3">Received By:</td>
  1815. </tr>
  1816. <tr>
  1817. <td colspan="11"></td>
  1818. </tr>
  1819. </table>
  1820. </div>
  1821. </div>
  1822. </body>
  1823. </html>
  1824. ORDER;
  1825. require_once (\Yii::$app->vendorPath . '/tecnickcom/tcpdf/tcpdf.php');
  1826. $pdf = new \TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);
  1827. // 设置打印模式
  1828. $pdf->SetCreator(PDF_CREATOR);
  1829. $pdf->SetAuthor('DaZe');
  1830. $pdf->SetTitle($orderSn);
  1831. $pdf->SetSubject('TCPDF Tutorial');
  1832. $pdf->SetKeywords('TCPDF, PDF, example, test, guide');
  1833. // 是否显示页眉
  1834. $pdf->setPrintHeader(false);
  1835. // 设置页眉字体
  1836. $pdf->setHeaderFont(Array('Helvetica', '', '12'));
  1837. // 页眉距离顶部的距离
  1838. $pdf->SetHeaderMargin('5');
  1839. // 是否显示页脚
  1840. $pdf->setPrintFooter(false);
  1841. // 设置默认等宽字体
  1842. $pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);
  1843. // 设置行高
  1844. // $pdf->setCellHeightRatio(1);
  1845. // 设置左、上、右的间距
  1846. $pdf->SetMargins('10', '0', '10');
  1847. // 设置是否自动分页 距离底部多少距离时分页
  1848. $pdf->SetAutoPageBreak(TRUE, '15');
  1849. // 设置图像比例因子
  1850. $pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);
  1851. if (@file_exists(\Yii::$app->vendorPath . 'tecnickcom/tcpdf/examples/lang/eng.php')) {
  1852. require_once(\Yii::$app->vendorPath . '/tecnickcom/tcpdf/examples/lang/eng.php');
  1853. $pdf->setLanguageArray($l);
  1854. }
  1855. $pdf->setFontSubsetting(true);
  1856. $pdf->AddPage();
  1857. // 设置字体
  1858. $pdf->SetFont('Helvetica', '', 10, '', true);
  1859. // $image_file = \Yii::$app->basePath . '/../ngds-logo.jpg';
  1860. // $image = file_get_contents($image_file);
  1861. // $pdf->Image('@' . $image, 15, 12, 20, 7, 'JPG');
  1862. $pdf->writeHTML($context);
  1863. $result = $pdf->Output($realFile, 'F');
  1864. $this->_updateFirst($realFile, 1);
  1865. }
  1866. $this->complete();
  1867. return true;
  1868. }
  1869. }