PrintJob.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. <?php
  2. namespace app\api\controller\ps;
  3. use app\common\controller\Api;
  4. use app\common\library\ps\FactoryContext;
  5. use app\common\library\ps\FactoryPerm;
  6. use app\common\library\ps\LodopPrinter;
  7. use app\common\library\ps\PaperSize;
  8. use app\common\library\ps\PrintPlanner;
  9. use app\common\library\ps\TemplateLicense;
  10. use app\common\model\ps\PrintJob as PrintJobModel;
  11. use app\common\model\ps\PrintSnapshot;
  12. use app\common\model\ps\Template;
  13. use fast\Random;
  14. use think\Db;
  15. use think\Exception;
  16. /**
  17. * 套打接口(测试环境只预览)
  18. */
  19. class PrintJob extends Api
  20. {
  21. protected $noNeedRight = '*';
  22. public function _initialize()
  23. {
  24. parent::_initialize();
  25. if (!FactoryContext::id()) {
  26. $this->error('账号未绑定工厂');
  27. }
  28. $map = [
  29. 'index' => 'print.view',
  30. 'prepare' => 'print.do',
  31. 'confirm' => 'print.do',
  32. 'del' => 'print.delete',
  33. ];
  34. $action = strtolower($this->request->action());
  35. if (isset($map[$action]) && FactoryPerm::deny($map[$action])) {
  36. $this->error('没有权限:' . FactoryPerm::label($map[$action]));
  37. }
  38. }
  39. /**
  40. * 打印历史
  41. */
  42. public function index()
  43. {
  44. $limit = $this->request->request('limit/d', 10);
  45. $list = (new PrintJobModel())->order('id desc')->paginate($limit);
  46. $this->success('', ['total' => $list->total(), 'rows' => $list->items()]);
  47. }
  48. /**
  49. * 预览套打数据:拆张、工序、填充字段。不锁定单次模板。
  50. */
  51. public function prepare()
  52. {
  53. $templateId = $this->request->param('template_id/d');
  54. $bizType = $this->request->param('biz_type', 'workorder');
  55. $bizId = $this->request->param('biz_id/d', 0);
  56. if (!$templateId) {
  57. $this->error(__('Invalid parameters'));
  58. }
  59. if ($bizId <= 0) {
  60. $no = trim((string)$this->request->param('no', $this->request->param('code', '')));
  61. $name = trim((string)$this->request->param('name', ''));
  62. if ($no === '' && $name === '') {
  63. $this->error('请填写工单或产品编号、名称');
  64. }
  65. }
  66. $template = Template::get($templateId);
  67. if (!$template) {
  68. $this->error(__('No Results were found'));
  69. }
  70. try {
  71. TemplateLicense::assertPrintable($template, FactoryContext::id());
  72. $plan = PrintPlanner::prepare($bizType, $bizId, $this->planOptions());
  73. } catch (Exception $e) {
  74. $this->error($e->getMessage());
  75. }
  76. $schemaJson = PrintPlanner::schemaJson($template);
  77. $schema = PrintPlanner::decodeSchema($schemaJson, $template['cover']);
  78. $htmlTpl = $this->request->param('html', '', null);
  79. if (trim((string)$htmlTpl) === '') {
  80. $htmlTpl = PrintPlanner::htmlFromSchema($schemaJson, $template['cover'], false);
  81. }
  82. $pages = PrintPlanner::renderPages($plan, $htmlTpl, $schema);
  83. $lodop = LodopPrinter::buildTask($pages[0]['html'] ?? '', $template['name'], $pages, 1, $this->printPage($template));
  84. $this->success('', [
  85. 'template_id' => (int)$template['id'],
  86. 'template_name' => $template['name'],
  87. 'template_cover' => $template['cover'] ? cdnurl($template['cover'], true) : '',
  88. 'template_version_id' => (int)$template['current_version_id'],
  89. 'schema_json' => PrintPlanner::schemaJson($template),
  90. 'qty_plan' => $plan['qty_plan'],
  91. 'sheet_qty' => $plan['sheet_qty'],
  92. 'plate_total' => $plan['plate_total'] ?? $plan['total_sheets'],
  93. 'sheet_from' => $plan['sheet_from'] ?? 1,
  94. 'sheet_to' => $plan['sheet_to'] ?? $plan['total_sheets'],
  95. 'total_sheets' => $plan['total_sheets'],
  96. 'processes' => $plan['processes'],
  97. 'fill' => $plan['fill'],
  98. 'sheets' => $plan['sheets'],
  99. 'pages' => $pages,
  100. 'lodop' => $lodop,
  101. ]);
  102. }
  103. /**
  104. * 确认套打:按拆张落快照,整单只锁定一次单次授权,返回 Lodop 预览任务
  105. */
  106. public function confirm()
  107. {
  108. $templateId = $this->request->param('template_id/d');
  109. $bizType = $this->request->param('biz_type', 'workorder');
  110. $bizId = $this->request->param('biz_id/d', 0);
  111. $copies = $this->request->param('copies/d', 1);
  112. $htmlTpl = $this->request->param('html', '', null);
  113. if (!$templateId) {
  114. $this->error(__('Invalid parameters'));
  115. }
  116. if ($bizId <= 0) {
  117. $no = trim((string)$this->request->param('no', $this->request->param('code', '')));
  118. $name = trim((string)$this->request->param('name', ''));
  119. if ($no === '' && $name === '') {
  120. $this->error('请填写工单或产品编号、名称');
  121. }
  122. }
  123. $factoryId = FactoryContext::id();
  124. $template = Template::get($templateId);
  125. if (!$template) {
  126. $this->error(__('No Results were found'));
  127. }
  128. $schemaJson = PrintPlanner::schemaJson($template);
  129. $schema = PrintPlanner::decodeSchema($schemaJson, $template['cover']);
  130. if (trim((string)$htmlTpl) === '') {
  131. $htmlTpl = PrintPlanner::htmlFromSchema($schemaJson, $template['cover'], false);
  132. }
  133. try {
  134. TemplateLicense::assertPrintable($template, $factoryId);
  135. $plan = PrintPlanner::prepare($bizType, $bizId, $this->planOptions());
  136. } catch (Exception $e) {
  137. $this->error($e->getMessage());
  138. }
  139. $postedPages = $this->request->param('pages/a');
  140. if (is_array($postedPages) && $postedPages) {
  141. $pages = [];
  142. foreach ($postedPages as $index => $item) {
  143. $html = is_array($item) ? (isset($item['html']) ? $item['html'] : '') : (string)$item;
  144. $sheet = $plan['sheets'][$index] ?? null;
  145. $fill = $sheet ? $sheet['fill'] : $plan['fill'];
  146. $pages[] = [
  147. 'no' => $sheet ? $sheet['no'] : ($index + 1),
  148. 'total' => $plan['total_sheets'],
  149. 'qty' => $sheet ? $sheet['qty'] : $plan['qty_plan'],
  150. 'title' => '流程单 ' . ($sheet ? $sheet['no_text'] : ($index + 1)),
  151. 'html' => $html,
  152. 'fill' => $fill,
  153. 'print_items' => PrintPlanner::isOverlay($schema)
  154. ? PrintPlanner::overlayPrintItems($schema, $fill, $plan['processes'])
  155. : [],
  156. ];
  157. }
  158. } else {
  159. $pages = PrintPlanner::renderPages($plan, $htmlTpl, $schema);
  160. }
  161. $lodop = LodopPrinter::buildTask($pages[0]['html'] ?? '', $template['name'], $pages, $copies, $this->printPage($template));
  162. $printNo = 'P' . date('YmdHis') . Random::numeric(4);
  163. Db::startTrans();
  164. try {
  165. $job = new PrintJobModel();
  166. $job->allowField(true)->save([
  167. 'operator_user_id' => $this->auth->id,
  168. 'print_no' => $printNo,
  169. 'biz_type' => $bizType,
  170. 'biz_id' => $bizId,
  171. 'template_id' => $templateId,
  172. 'template_version_id' => (int)$template['current_version_id'],
  173. 'copies' => $copies > 0 ? $copies : 1,
  174. 'lodop_mode' => $lodop['mode'],
  175. 'status' => 'previewed',
  176. ]);
  177. $snapshot = new PrintSnapshot();
  178. $snapshot->allowField(true)->save([
  179. 'print_job_id' => $job->id,
  180. 'fill_json' => json_encode([
  181. 'qty_plan' => $plan['qty_plan'],
  182. 'sheet_qty' => $plan['sheet_qty'],
  183. 'total_sheets' => $plan['total_sheets'],
  184. 'processes' => $plan['processes'],
  185. 'fill' => $plan['fill'],
  186. 'sheets' => $plan['sheets'],
  187. ], JSON_UNESCAPED_UNICODE),
  188. 'schema_json' => $schemaJson,
  189. 'lodop_html' => json_encode(array_column($pages, 'html'), JSON_UNESCAPED_UNICODE),
  190. ]);
  191. TemplateLicense::consumeOnConfirm($template, $factoryId);
  192. Db::commit();
  193. } catch (Exception $e) {
  194. Db::rollback();
  195. $this->error($e->getMessage());
  196. }
  197. $this->success('', [
  198. 'print_no' => $printNo,
  199. 'total_sheets' => $plan['total_sheets'],
  200. 'lodop' => $lodop,
  201. ]);
  202. }
  203. /**
  204. * 删除本厂打印记录
  205. */
  206. public function del()
  207. {
  208. $ids = $this->request->post('id');
  209. if (!$ids) {
  210. $this->error(__('Parameter %s can not be empty', 'id'));
  211. }
  212. $list = (new PrintJobModel())->where('id', 'in', $ids)->select();
  213. foreach ($list as $row) {
  214. PrintSnapshot::where('print_job_id', $row->id)->delete();
  215. $row->delete();
  216. }
  217. $this->success('已删除');
  218. }
  219. /**
  220. * @return array
  221. */
  222. protected function planOptions()
  223. {
  224. $extra = $this->request->param('extra/a');
  225. $options = [
  226. 'sheet_qty' => $this->request->param('sheet_qty', $this->request->param('tldx', 0)),
  227. 'no' => $this->request->param('no', ''),
  228. 'code' => $this->request->param('code', ''),
  229. 'name' => $this->request->param('name', ''),
  230. 'sheet_from' => $this->request->param('sheet_from/d', 0),
  231. 'sheet_to' => $this->request->param('sheet_to/d', 0),
  232. 'process_string' => $this->request->param('process_string', $this->request->param('string', '')),
  233. 'extra' => is_array($extra) ? $extra : [],
  234. ];
  235. $rawProcesses = $this->request->param('processes/a');
  236. if ($rawProcesses !== null) {
  237. $options['processes'] = is_array($rawProcesses) ? $rawProcesses : [];
  238. }
  239. $qty = $this->request->param('qty', null);
  240. if ($qty === null || $qty === '') {
  241. $qty = $this->request->param('num', null);
  242. }
  243. if ($qty !== null && $qty !== '') {
  244. $options['qty'] = $qty;
  245. }
  246. return $options;
  247. }
  248. /**
  249. * @param Template $template
  250. * @return array
  251. */
  252. protected function printPage($template)
  253. {
  254. $schema = PrintPlanner::decodeSchema(PrintPlanner::schemaJson($template), $template['cover']);
  255. $page = isset($schema['page']) && is_array($schema['page']) ? $schema['page'] : [];
  256. $resolved = PaperSize::resolve(
  257. (string)($template['paper_size'] ?: ($page['paper_size'] ?? 'A4')),
  258. (float)($template['page_w'] ?: ($page['w'] ?? 0)),
  259. (float)($template['page_h'] ?: ($page['h'] ?? 0))
  260. );
  261. $page['w'] = $resolved['w'];
  262. $page['h'] = $resolved['h'];
  263. $page['paper_size'] = $resolved['name'];
  264. $cover = (string)($schema['background'] ?? $template['cover'] ?? '');
  265. $page['background'] = $cover !== '' ? PrintPlanner::printImageSrc($cover) : '';
  266. return $page;
  267. }
  268. }