PrintOrder.php 79 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919
  1. <?php
  2. /**
  3. * 重庆赤晓店信息科技有限公司
  4. * https://www.chixiaodian.com
  5. * Copyright (c) 2023 赤店商城 All rights reserved.
  6. */
  7. namespace app\utils;
  8. use app\constants\OptionSetting;
  9. use app\models\FoodFlag;
  10. use app\models\Goods;
  11. use app\models\Mch;
  12. use app\models\Md;
  13. use app\models\Option;
  14. use app\models\Order;
  15. use app\models\OrderDetail;
  16. use app\models\OrderForm;
  17. use app\models\OrderGoodsCancel;
  18. use app\models\OrderTransit;
  19. use app\models\Printer;
  20. use app\models\PrinterSetting;
  21. use app\models\PurchaseOrder;
  22. use app\modules\admin\models\SupplierForm;
  23. use yii\db\Exception;
  24. use yii\helpers\Json;
  25. use app\models\DeliveryInfo;
  26. class PrintOrder
  27. {
  28. public $store_id;
  29. public $mch_id;
  30. public $md_id;
  31. public $md;
  32. public $store_name;
  33. public $mch;
  34. public $order_id;
  35. /**
  36. * 打印设置
  37. * @var $setting
  38. */
  39. public $setting; // 打印设置
  40. /**
  41. * 打印机型号
  42. * @var $printer
  43. */
  44. public $printer;
  45. /**
  46. * 打印机配置
  47. * @var $printer_setting
  48. */
  49. public $printer_setting;
  50. /**
  51. * 订单类型 0--商城订单 1--秒杀订单 2--拼团订单 3--预约订单
  52. * @var $order_type
  53. */
  54. public $order_type;
  55. /**
  56. * 订单详情
  57. * @var $order
  58. */
  59. public $order;
  60. /**
  61. * 订单商品详情
  62. * @var $goods_list
  63. */
  64. public $goods_list;
  65. /**
  66. * 订单打印方式
  67. * @var $print_type
  68. */
  69. public $print_type;
  70. public $extra;
  71. public $type;
  72. public $supplier_id;
  73. public $is_refund = false;
  74. public function __construct($store_id, $order_id, $print_type, $order_type = 0, $md_id = 0, $type = 0, $mch_id = 0, $supplier_id = 0)
  75. {
  76. $this->store_id = $store_id;
  77. $this->mch_id = (int)$mch_id;
  78. $this->store_name = Option::get('name', $store_id, 'store')['value'];
  79. $md = Md::findOne($md_id);
  80. if ($md) {
  81. $this->md = $md;
  82. } else {
  83. $this->md = null;
  84. }
  85. $this->md_id = $md ? $md_id : -1;
  86. $this->order_id = $order_id;
  87. $this->print_type = $print_type;
  88. $this->order_type = $order_type;
  89. $this->type = $type;
  90. $this->supplier_id = $supplier_id;
  91. }
  92. public function print_order()
  93. {
  94. try {
  95. // 打印设置
  96. if (in_array((int)$this->type, [1, 2])) {
  97. $this->setting = PrinterSetting::findOne(['supplier_id' => $this->supplier_id ?: get_supplier_id()]);
  98. } else {
  99. $this->setting = PrinterSetting::findOne(['store_id' => $this->store_id, 'md_id' => $this->md_id]);
  100. if (!$this->setting) {
  101. $this->md_id = 0;
  102. $this->setting = PrinterSetting::findOne(['store_id' => $this->store_id, 'md_id' => $this->md_id]);
  103. }
  104. }
  105. if($this->mch_id){
  106. $this->setting = PrinterSetting::findOne(['mch_id' => $this->mch_id]);
  107. }
  108. if (!$this->setting) {
  109. \Yii::warning('未设置打印设置');
  110. throw new \Exception('未设置打印设置');
  111. }
  112. $type = json_decode($this->setting->type, true);
  113. if (!($type[$this->print_type] && $type[$this->print_type] == 1 || $this->print_type == 'reprint')) {
  114. \Yii::warning('打印设置打印方式');
  115. throw new \Exception('打印设置打印方式');
  116. }
  117. if (in_array((int)$this->type, [1, 2])) {
  118. $this->printer = Printer::findAll(['id' => explode(',',$this->setting->printer_id), 'is_delete' => 0]);
  119. } else {
  120. $this->printer = Printer::findAll(['id' => explode(',',$this->setting->printer_id), 'is_delete' => 0, 'md_id' => $this->md_id]);
  121. }
  122. if (count($this->printer) == 0) { // 打印机参数
  123. \Yii::warning('未找到设置的打印机');
  124. throw new \Exception('未找到设置的打印机');
  125. }
  126. // 整理订单打印信息 商城订单打印
  127. if (intval($this->type) === 0) {
  128. $order_id = $this->order_id;
  129. } else {
  130. $order_data = [
  131. 'order_id' => $this->order_id,
  132. 'status' => $this->type//1转单订单 2采购订单
  133. ];
  134. $domain = (new OptionSetting)->getCloudDomainName();
  135. $order_url = "/user/getOrder";
  136. $orderInfo = cloud_post($domain . $order_url,
  137. $order_data);
  138. $orderInfo = json_decode($orderInfo,true);
  139. if($orderInfo['code'] !== 0){
  140. return $orderInfo['msg'];
  141. }
  142. $orderInfo['data']['total_price'] = $orderInfo['data']['pay_price'];
  143. $orderInfo['data']['pay_type'] = 0;
  144. $goods_send_type = $orderInfo['data']['goods_send_type'];
  145. switch (intval($goods_send_type)) {
  146. case 1:
  147. $send_type = "集采";
  148. break;
  149. case 2:
  150. $send_type = "自主配送";
  151. break;
  152. case 3:
  153. $send_type = "代理配送";
  154. break;
  155. case 4:
  156. $send_type = "云仓库存";
  157. break;
  158. case 5:
  159. $send_type = "仓库同城";
  160. break;
  161. case 6:
  162. $send_type = "仓库快递";
  163. break;
  164. default:
  165. $send_type = "快递";
  166. break;
  167. }
  168. $orderInfo['data']['is_pay'] = intval($orderInfo['data']['status']) === 2 ? 0 : 1;
  169. $orderInfo['data']['created_at'] = $orderInfo['data']['pay_time'];
  170. $this->order = $orderInfo['data'];
  171. // 这里要根据商城订单的配送状态
  172. if($this->type){
  173. $orderT = OrderTransit::findOne(['cloud_order_id' => $this->order_id]);
  174. $storeOrder = Order::findOne($orderT->order_id);
  175. $this->order['md_id'] = $storeOrder->md_id;
  176. $this->order['is_offline'] = $storeOrder->is_offline;
  177. }
  178. $order_type = '供货商' . (intval($this->type) === 1 ? '转单订单' : '批发订单');
  179. $this->goods_list = $orderInfo['data']['goods_list'];
  180. }
  181. if (isset($order_id)) {
  182. $this->order = Order::find()->where(['id' => $order_id, 'is_delete' => 0])->asArray()->one();
  183. $this->goods_list = $this->getOrderGoodsList($this->order['id']);
  184. }
  185. if (intval($this->type) === 0) {
  186. if ($this->order['is_offline'] == 0) {
  187. if ($this->order['is_delivery'] == 0) {
  188. $send_type = '快递配送';
  189. } else {
  190. $send_type = '同城配送';
  191. }
  192. } else {
  193. $send_type = '到店自提';
  194. }
  195. $order_type = '商城订单';
  196. if (isset($this->order['order_origin']) && intval($this->order['order_origin']) === 4) {
  197. $send_type = '收银台';
  198. $order_type = '收银台订单';
  199. }
  200. }
  201. $this->extra = [
  202. 'order_type' => $order_type,
  203. 'send_type' => $send_type,
  204. ];
  205. switch ($this->order['pay_type']) {
  206. case 1:
  207. case 0:
  208. $this->extra['pay_type'] = "线上支付";
  209. break;
  210. case 2:
  211. $this->extra['pay_type'] = "货到付款";
  212. break;
  213. case 3:
  214. $this->extra['pay_type'] = "余额支付";
  215. break;
  216. default:
  217. $this->extra['pay_type'] = "";
  218. }
  219. foreach($this->printer as $printer){
  220. $this->handlePrint($printer);
  221. }
  222. return [
  223. 'code' => 0,
  224. 'msg' => '打印成功',
  225. 'data' => [],
  226. 'cc' => [$order_id, $this->order_id, $this->type, [], $this->goods_list, $this->order],
  227. ];
  228. } catch (\Exception $e) {
  229. return [
  230. 'code' => 1,
  231. 'msg' => $e->getMessage() . $e->getFile() . $e->getLine(),
  232. 'cc' => [$order_id, $this->order_id, $this->type, [], $this->goods_list, $this->order],
  233. 'data' => $e
  234. ];
  235. }
  236. }
  237. private function handlePrint($printer)
  238. {
  239. // 打印机配置
  240. $this->printer_setting = json_decode($printer->printer_setting, true);
  241. if ($printer->printer_type == 'kdt2') {
  242. $res = $this->printer_1();
  243. }
  244. if ($printer->printer_type == 'yilianyun-k4') {
  245. $res = $this->printer_2();
  246. }
  247. if ($printer->printer_type == 'feie') {
  248. $res = $this->printer_3();
  249. }
  250. if ($printer->printer_type == 'gp') {
  251. $res = $this->printer_4();
  252. }
  253. if ($printer->printer_type == 'xhy') {
  254. $res = $this->printer_5($printer);
  255. }
  256. if ($printer->printer_type == 'dqzn') {
  257. $res = $this->printer_6($printer);
  258. }
  259. if ($printer->printer_type == 'feie_label') {
  260. $res = $this->printer_7();
  261. }
  262. }
  263. /**
  264. * 365打印机
  265. */
  266. private function printer_1()
  267. {
  268. $big = [
  269. 1 => [
  270. 'start' => '',
  271. 'end' => ''
  272. ],
  273. 2 => [
  274. 'start' => "<B>",
  275. 'end' => "</B>"
  276. ],
  277. 3 => [
  278. 'start' => '<DB>',
  279. 'end' => '</DB>'
  280. ],
  281. ];
  282. $start = $big[$this->setting->big]['start'];
  283. $end = $big[$this->setting->big]['end'];
  284. date_default_timezone_set("Asia/Shanghai");
  285. $order = $this->order;
  286. $addtime = date('Y-m-d H:i:s', $order['created_at']);
  287. $goods_list = $this->goods_list;
  288. header("Content-Type: text/html;charset=utf-8");
  289. $DEVICE_NO = $this->printer_setting['name'];
  290. $key = $this->printer_setting['key'];
  291. $time = $this->printer_setting['time'];//打印联数
  292. $content = "";
  293. if ($this->mch && $this->mch->name){
  294. $title_name = $this->mch->name;
  295. }else{
  296. $title_name = $this->store_name;
  297. }
  298. $content .= "<CB>{$title_name}</CB><BR>";
  299. if (!empty($order['food_flag_id'])) {
  300. $FoodFlag = FoodFlag::findOne($order['food_flag_id']);
  301. $food_num = $FoodFlag->table_num;
  302. $content .= "桌号:{$food_num}\n";
  303. }
  304. if ($this->is_refund) {
  305. $content .= "订单类型:退款订单<BR>";
  306. } else {
  307. if ($order['food_code']) {
  308. $content .= "标识码:{$order['food_code']}<BR>";
  309. }
  310. $content .= "订单类型:{$this->extra['order_type']}<BR>";
  311. }
  312. $content .= "支付方式:{$this->extra['pay_type']}<BR>";
  313. $isPay = $order['is_pay'] == 1 ? '已支付' : '未支付';
  314. $content .= "支付状态:{$isPay}<BR>";
  315. $content .= "发货方式:{$this->extra['send_type']}<BR>";
  316. $content .= "订单号:{$order['order_no']}<BR>";
  317. $content .= "下单时间:{$addtime}<BR>";
  318. if ($this->setting->is_attr == 0) {
  319. $content .= "--------------------------------<BR>";
  320. $content .= "名称      单价 数量 金额<BR>";
  321. $content .= "--------------------------------<BR>";
  322. foreach ($goods_list as $k => $v) {
  323. $price = round($v['total_price'] / $v['num'], 2);
  324. $arr = $this->r_str_pad_1($v['name'], 7);
  325. foreach ($arr as $index => $value) {
  326. if ($index == 0) {
  327. $content .= $value . " " . str_pad($price, 5) . " " . str_pad($v['num'], 6) . " " . round($v['total_price'], 2) . '<BR>';
  328. } else {
  329. $content .= $value . '<BR>';
  330. }
  331. }
  332. }
  333. } else {
  334. /**
  335. * $content .= "--------------------------------<BR>";
  336. * $content .= "名称   规格  单价 数量 金额<BR>";
  337. * $content .= "--------------------------------<BR>";
  338. * foreach ($goods_list as $k => $v) {
  339. * $attr = json_decode($v['attr'], true);
  340. * $attr_arr = [];
  341. * foreach ($attr as $i => $j) {
  342. * $attr_arr[] = $j['attr_group_name'] . $j['attr_name'];
  343. * }
  344. * $attr_str = implode(',', $attr_arr);
  345. * $attr_arr_1 = $this->r_str_pad_1($attr_str, 4);
  346. * $price = round($v['total_price'] / $v['num'], 2);
  347. * $arr = $this->r_str_pad_1($v['name'], 4);
  348. * $a = count($attr_arr_1) >= count($arr) ? $attr_arr_1 : $arr;
  349. * foreach ($a as $index => $value) {
  350. * $string = "";
  351. * $string_1 = "";
  352. * if (isset($arr[$index])) {
  353. * $string = $arr[$index];
  354. * }
  355. * if (isset($attr_arr_1[$index])) {
  356. * if (!$string) {
  357. * $string = str_pad($string, 8);
  358. * }
  359. * $string_1 = $attr_arr_1[$index];
  360. * }
  361. * if ($index == 0) {
  362. * $content .= $string . " " . $string_1 . str_pad($price, 5) . " " . str_pad($v['num'], 3) . " " . round($v['total_price'], 2) . '<BR>';
  363. * } else {
  364. * $content .= $string . " " . $string_1 . '<BR>';
  365. * }
  366. * }
  367. * }
  368. * */
  369. $content .= "--------------------------------<BR>";
  370. $content .= "名称 数量 金额 <BR>";
  371. $content .= "--------------------------------<BR>";
  372. foreach ($goods_list as $k => $v) {
  373. $name = $v['name'];
  374. if (!empty($v['attr'])) {
  375. $attr = json_decode($v['attr'], true);
  376. $attr_arr = [];
  377. if ($attr['attr_list']) {
  378. $attr = $attr['attr_list'];
  379. }
  380. foreach ($attr as $i => $j) {
  381. $attr_arr[] = $j['attr_group_name'] . $j['attr_name'];
  382. }
  383. $attr_str = implode(',', $attr_arr);
  384. $name = $v['name'] . '(' . $attr_str . ')';
  385. }
  386. $name_arr = $this->r_str_pad_1($name, 8);
  387. foreach ($name_arr as $index => $value) {
  388. if ($index == count($name_arr) - 1) {
  389. $content .= $name_arr[$index] . " " . str_pad('×' . $v['num'], 7) . " " . round($v['total_price'], 2) . "<BR>";
  390. } else {
  391. $content .= $name_arr[$index] . '<BR>';
  392. }
  393. }
  394. }
  395. }
  396. $content .= "--------------------------------<BR>";
  397. if ($order['express_price']) {
  398. $content .= "运费:{$order['express_price']}元<BR>";
  399. }
  400. $content .= "总计:{$order['total_price']}元<BR>";
  401. if (isset($order['user_coupon_id'])) {
  402. $content .= "优惠券优惠:{$order['coupon_sub_price']}元<BR>";
  403. }
  404. if (isset($order['integral'])) {
  405. $integral = json_decode($order['integral'], true);
  406. if ($integral['forehead'] != 0) {
  407. $content .= "积分抵扣:{$integral['forehead']}元<BR>";
  408. }
  409. }
  410. if (isset($order['discount']) && $order['discount'] < 10) {
  411. $content .= "会员折扣:{$order['discount']}折<BR>";
  412. }
  413. if ($this->is_refund) {
  414. $content .= "实退:{$order['pay_price']}元<BR>";
  415. } else {
  416. $content .= $order['is_pay'] == 1 ? "实付:{$order['pay_price']}元<BR>" : "实付:0元<BR>";
  417. }
  418. if ($this->order_type != 3) {
  419. if ((isset($order['is_offline']) && $order['is_offline'] != 1) || (isset($order['offline']) && $order['offline'] != 2)) {
  420. $content .= "{$start}收货人:{$order['name']}<BR>";
  421. $content .= "收货地址:{$order['address']}<BR>";
  422. $content .= "收货人电话:{$order['mobile']}<BR>{$end}";
  423. $content .= "--------------------------------<BR>";
  424. } else {
  425. $content .= "{$start}联系人:{$order['name']}<BR>";
  426. $content .= "联系人电话:{$order['mobile']}<BR>{$end}";
  427. $content .= "--------------------------------<BR>";
  428. $content .= "门店信息<BR>";
  429. // 门店信息要根据订单上的md_id查询门店 不能根据get_md_id获取 也不能根据构造函数中传入的md_id 因为供货商打印 或者 平台打印 或者 商城打印的时候 是不会传md_id的
  430. $md = Md::findOne($order['md_id']);
  431. $content .= "{$md->name}<BR>";
  432. $content .= "{$md->mobile}<BR>";
  433. $content .= "{$md->address}<BR>";
  434. $content .= "--------------------------------<BR>";
  435. }
  436. }
  437. if ($order['remark']) {
  438. $content .= "备注:{$order['remark']}<BR>";
  439. } else {
  440. $orderForm = OrderForm::findAll([
  441. 'order_id' => $this->order['id'], 'store_id' => $this->store_id, 'is_delete' => 0
  442. ]);
  443. if ($orderForm) {
  444. /* @var $orderForm OrderForm[] */
  445. foreach ($orderForm as $item) {
  446. $content .= $item->key . ':' . $item->value . "<BR>";
  447. }
  448. }
  449. }
  450. if ($order['seller_comments']) {
  451. $content .= "商家备注:{$order['seller_comments']}<BR>";
  452. }
  453. $result = $this->sendSelfFormatOrderInfo($DEVICE_NO, $key, $time, $content);
  454. \Yii::warning('==>>' . $result);
  455. $result = Json::decode($result);
  456. if ($result['responseCode'] != 0) {
  457. throw new Exception($result['msg'], $result);
  458. }
  459. return $result;
  460. }
  461. /**
  462. * 365打印机
  463. */
  464. private function sendSelfFormatOrderInfo($device_no, $key, $times, $orderInfo)
  465. {
  466. // $times打印次数
  467. $selfMessage = array(
  468. 'deviceNo' => $device_no,
  469. 'printContent' => $orderInfo,
  470. 'key' => $key,
  471. 'times' => $times
  472. );
  473. $url = "http://open.printcenter.cn:8080/addOrder";
  474. $options = array(
  475. 'http' => array(
  476. 'header' => "Content-type: application/x-www-form-urlencoded ",
  477. 'method' => 'POST',
  478. 'content' => http_build_query($selfMessage),
  479. ),
  480. );
  481. $context = stream_context_create($options);
  482. $result = file_get_contents($url, false, $context);
  483. return $result;
  484. }
  485. /**
  486. * @param $order_id
  487. * @return mixed
  488. * 订单商品详情
  489. */
  490. public function getOrderGoodsList($order_id)
  491. {
  492. $order_detail_list = OrderDetail::find()->alias('od')
  493. ->leftJoin(['g' => Goods::tableName()], 'od.goods_id=g.id')
  494. ->where([
  495. 'od.is_delete' => 0,
  496. 'od.order_id' => $order_id,
  497. ])->select('od.*,g.name, g.product_type')->asArray()->all();
  498. $new_arr = [];
  499. foreach ($order_detail_list as $item) {
  500. $open = true;
  501. if (!empty($item['food_ext_goods'])) {
  502. $food_ext_goods = json_decode($item['food_ext_goods'], true);
  503. if (!empty($food_ext_goods)) {
  504. $open = false;
  505. foreach ($food_ext_goods as &$ext_item) {
  506. $ext_item['is_food_ext_goods'] = 1;
  507. $ext_item['total_price'] = $ext_item['price'];
  508. }
  509. $new_arr = array_merge($new_arr, [$item], $food_ext_goods);
  510. }
  511. }
  512. if ($open) {
  513. $orderGoodsCancel = OrderGoodsCancel::find()->where(['order_detail_id' => $item['id'], 'status' => [
  514. OrderGoodsCancel::STATUS_PASS,
  515. OrderGoodsCancel::STATUS_PAY,
  516. OrderGoodsCancel::STATUS_PAY_FAIL,
  517. ]])->groupBy('order_detail_id')
  518. ->select('SUM(num) as num, SUM(refund_price) as refund_price')
  519. ->asArray()->one();
  520. $this->order['total_price'] = bcsub($this->order['total_price'], $orderGoodsCancel['refund_price'], 2);
  521. $this->order['pay_price'] = bcsub($this->order['pay_price'], $orderGoodsCancel['refund_price'], 2);
  522. $item['total_price'] = bcsub($item['total_price'], $orderGoodsCancel['refund_price'], 2);
  523. $item['num'] = bcsub($item['num'], $orderGoodsCancel['num']);
  524. if ($item['num'] <= 0) {
  525. continue;
  526. }
  527. if ($item['total_price'] <= 0) {
  528. $item['total_price'] = 0;
  529. }
  530. $new_arr = array_merge($new_arr, [$item]);
  531. }
  532. }
  533. return $new_arr;
  534. }
  535. /**
  536. * 补齐空格
  537. * 截取七个中文字符长度
  538. */
  539. private function r_str_pad($input, $n = 7)
  540. {
  541. $string = "";
  542. $count = 0;
  543. $c_count = 0;
  544. for ($i = 0; $i < mb_strlen($input, 'UTF-8'); $i++) {
  545. $char = mb_substr($input, $i, 1, 'UTF-8');
  546. $string .= $char;
  547. if (strlen($char) == 3) {
  548. $count += 2;
  549. $c_count++;
  550. } else {
  551. $count += 1;
  552. }
  553. if ($count >= $n * 2) {
  554. break;
  555. }
  556. }
  557. if ($count < $n * 2) {
  558. $string = str_pad($string, $n * 2 + $c_count);
  559. }
  560. return $string;
  561. }
  562. /**
  563. * 补齐空格
  564. * 每n个中文字符长度为一个数组元素
  565. */
  566. private function r_str_pad_1($input, $n = 7)
  567. {
  568. $string = "";
  569. $count = 0;
  570. $c_count = 0;
  571. $arr = array();
  572. for ($i = 0; $i < mb_strlen($input, 'UTF-8'); $i++) {
  573. $char = mb_substr($input, $i, 1, 'UTF-8');
  574. $string .= $char;
  575. if (strlen($char) == 3) {
  576. $count += 2;
  577. $c_count++;
  578. } else {
  579. $count += 1;
  580. }
  581. if ($count >= $n * 2) {
  582. $arr[] = $string;
  583. $string = '';
  584. $count = 0;
  585. $c_count = 0;
  586. }
  587. }
  588. if ($count < $n * 2) {
  589. $string = str_pad($string, $n * 2 + $c_count);
  590. $arr[] = $string;
  591. }
  592. return $arr;
  593. }
  594. /**
  595. * 易联云打印机-k4
  596. */
  597. private function printer_2()
  598. {
  599. $big = [
  600. 1 => [
  601. 'start' => '',
  602. 'end' => ''
  603. ],
  604. 2 => [
  605. 'start' => '<FS>',
  606. 'end' => '</FS>'
  607. ],
  608. 3 => [
  609. 'start' => '<FS2>',
  610. 'end' => '</FS2>'
  611. ],
  612. ];
  613. $start = $big[$this->setting->big]['start'];
  614. $end = $big[$this->setting->big]['end'];
  615. date_default_timezone_set("Asia/Shanghai");
  616. $machine_code = $this->printer_setting['machine_code'];//终端编号
  617. $key = $this->printer_setting['key'];//终端密钥
  618. $client_id = $this->printer_setting['client_id'];//用户ID
  619. $client_key = $this->printer_setting['client_key'];//用户密钥
  620. $time = $this->printer_setting['time'];//打印联数
  621. $order = $this->order;
  622. $addtime = date('Y-m-d H:i:s', $order['created_at']);
  623. $goods_list = $this->goods_list;
  624. $print = new Yprint();
  625. $content = "<MN>{$time}</MN>";
  626. if ($this->mch && $this->mch->name) {
  627. $title_name = $this->mch->name;
  628. } else {
  629. $title_name = $this->store_name;
  630. }
  631. $content .= "<FB><center>{$title_name}</center></FB>\n";
  632. if (!empty($order['food_flag_id'])) {
  633. $FoodFlag = FoodFlag::findOne($order['food_flag_id']);
  634. $food_num = $FoodFlag->table_num;
  635. $content .= "桌号:{$food_num}\n";
  636. }
  637. if ($this->is_refund) {
  638. $content .= "订单类型:退款订单\n";
  639. } else {
  640. if ($order['food_code']) {
  641. $content .= "标识码:{$order['food_code']}\n";
  642. }
  643. $content .= "订单类型:{$this->extra['order_type']}\n";
  644. }
  645. $content .= "支付方式:{$this->extra['pay_type']}\n";
  646. $isPay = $order['is_pay'] == 1 ? '已支付' : '未支付';
  647. $content .= "支付状态:{$isPay}\n";
  648. $content .= "发货方式:{$this->extra['send_type']}\n";
  649. $content .= "订单号:{$order['order_no']}\n";
  650. $content .= "下单时间:{$addtime}\n";
  651. $content .= "--------------------------------\n";
  652. if ($this->setting->is_attr == 0) {
  653. $content .= "<table><tr><td>名称</td><td>单价</td><td>数量</td><td>金额</td></tr>";
  654. foreach ($goods_list as $k => $v) {
  655. $price = round($v['total_price'] / $v['num'], 2);
  656. $v['name'] = str_replace(',', ',', $v['name']);
  657. $arr = $this->r_str_pad_1($v['name'], 8);
  658. foreach ($arr as $index => $value) {
  659. if ($index == 0) {
  660. $content .= "<tr><td>" . $value . "</td><td>" . $price . "</td><td>" . $v['num'] . "</td><td>" . round($v['total_price'], 2) . "</td></tr>";
  661. } else {
  662. $content .= "<tr><td>" . $value . "</td></tr>";
  663. }
  664. }
  665. }
  666. $content .= "</table>";
  667. } else {
  668. /**
  669. * $content .= "<table><tr><td>名称</td><td>规格</td><td>数量</td><td>单价</td></tr>";
  670. * foreach ($goods_list as $k => $v) {
  671. * $attr = json_decode($v['attr'], true);
  672. * $attr_arr = [];
  673. * foreach ($attr as $i => $j) {
  674. * $attr_arr[] = $j['attr_group_name'] . $j['attr_name'];
  675. * }
  676. * $attr_str = implode(',', $attr_arr);
  677. * $attr_arr_1 = $this->r_str_pad_1($attr_str, 4);
  678. * $price = round($v['total_price'] / $v['num'], 2);
  679. * $v['name'] = str_replace(',', ',', $v['name']);
  680. * $arr = $this->r_str_pad_1($v['name'], 4);
  681. * $a = count($attr_arr_1) >= count($arr) ? $attr_arr_1 : $arr;
  682. * foreach ($a as $index => $value) {
  683. * $string = "";
  684. * $string_1 = "";
  685. * if (isset($arr[$index])) {
  686. * $string = $arr[$index];
  687. * }
  688. * if (isset($attr_arr_1[$index])) {
  689. * if (!$string) {
  690. * $string = str_pad($string, 8);
  691. * }
  692. * $string_1 = $attr_arr_1[$index];
  693. * }
  694. * if ($index == 0) {
  695. * $content .= "<tr><td>" . $string . "</td><td>" . $string_1 . "</td><td>" . $v['num'] . "</td><td>" . $price . "</td></tr>";
  696. * } else {
  697. * $content .= "<tr><td>" . $string . "</td><td>" . $string_1 . "</td></tr>";
  698. * }
  699. * }
  700. * }
  701. * $content .= "</table>";
  702. * */
  703. $content .= "<table><tr><td>名称</td><td>数量</td><td>总价</td></tr>";
  704. foreach ($goods_list as $k => $v) {
  705. $name = $v['name'];
  706. if (!empty($v['attr'])) {
  707. if (!is_array($v['attr'])) {
  708. $attr = json_decode($v['attr'], true);
  709. } else {
  710. $attr = $v['attr'];
  711. }
  712. if (intval($v['product_type']) === 0) {
  713. $attr_arr = [];
  714. if($attr['attr_list']){
  715. $attr = $attr['attr_list'];
  716. }
  717. foreach ($attr as $i => $j) {
  718. $attr_arr[] = ($j['attr_group_name'] ?? '') . ($j['attr_name'] ?? '');
  719. }
  720. $attr_str = implode(',', $attr_arr);
  721. $name = $v['name'] . '(' . $attr_str . ')';
  722. } elseif (intval($v['product_type']) === 1) {
  723. $name = $v['name'] . '(预约日期:' . $attr[0]['date'] . '-' . $attr[count($attr) - 1]['date'] . ')';
  724. } elseif (intval($v['product_type']) === 2) {
  725. $name = $v['name'] . '(预约时间:' . $attr['data'] . ' ' . $attr['time'] . ')';
  726. }
  727. }
  728. $name_arr = $this->r_str_pad_1($name, 6);
  729. foreach ($name_arr as $index => $value) {
  730. if ($index == count($name_arr) - 1) {
  731. $content .= "<tr><td>" . $name_arr[$index] . "</td><td>" . '×' . $v['num'] . "</td><td>" . round($v['total_price'], 2) . "</td></tr>";
  732. } else {
  733. $content .= "<tr><td>" . $name_arr[$index] . "</td><td></td><td></td></tr>";
  734. }
  735. }
  736. }
  737. $content .= "</table>";
  738. }
  739. $content .= "--------------------------------\n";
  740. if ($order['express_price']) {
  741. $content .= "运费:{$order['express_price']}元\n";
  742. }
  743. $content .= "总计:{$order['total_price']}元\n";
  744. if (isset($order['user_coupon_id'])) {
  745. $content .= "优惠券优惠:{$order['coupon_sub_price']}元\n";
  746. }
  747. if (isset($order['integral'])) {
  748. $integral = json_decode($order['integral'], true);
  749. if ($integral['forehead'] != 0) {
  750. $content .= "积分抵扣:{$integral['forehead']}元\n";
  751. }
  752. }
  753. if (isset($order['discount']) && $order['discount'] < 10) {
  754. $content .= "会员折扣:{$order['discount']}折\n";
  755. }
  756. if ($this->is_refund) {
  757. $content .= "实退:{$order['pay_price']}元\n";
  758. } else {
  759. $content .= $order['is_pay'] == 1 ? "实付:{$order['pay_price']}元\n" : "实付:0元\n";
  760. }
  761. if ($this->order_type != 3) {
  762. if ((isset($order['is_offline']) && $order['is_offline'] != 1) || (isset($order['offline']) && $order['offline'] != 2)) {
  763. $content .= "{$start}收货人:{$order['name']}\n";
  764. $content .= "收货地址:{$order['address']}\n";
  765. $content .= "收货人电话:{$order['mobile']}\n{$end}";
  766. $content .= "--------------------------------\n";
  767. } else {
  768. $content .= "{$start}联系人:{$order['name']}\n";
  769. $content .= "联系人电话:{$order['mobile']}\n{$end}";
  770. $content .= "--------------------------------\n";
  771. $content .= "门店信息\n";
  772. // 门店信息要根据订单上的md_id查询门店 不能根据get_md_id获取 也不能根据构造函数中传入的md_id 因为供货商打印 或者 平台打印 或者 商城打印的时候 是不会传md_id的
  773. $md = Md::findOne($order['md_id']);
  774. $content .= "{$md->name}\n";
  775. $content .= "{$md->mobile}\n";
  776. $content .= "{$md->address}\n";
  777. $content .= "--------------------------------\n";
  778. }
  779. }
  780. if ($order['remark']) {
  781. $content .= "备注:{$order['remark']}\n";
  782. } else {
  783. $orderForm = OrderForm::findAll([
  784. 'order_id' => $this->order['id'], 'store_id' => $this->store_id, 'is_delete' => 0
  785. ]);
  786. if ($orderForm) {
  787. /* @var $orderForm OrderForm[] */
  788. foreach ($orderForm as $item) {
  789. $content .= $item->key . ':' . $item->value . "\n";
  790. }
  791. }
  792. }
  793. if ($order['seller_comments']) {
  794. $content .= "商家备注:{$order['seller_comments']}<BR>";
  795. }
  796. $result = $print->action_print($client_id, $machine_code, $content, $client_key, $key);
  797. $result = Json::decode($result);
  798. switch ($result['state']) {
  799. case 1:
  800. return $result;
  801. break;
  802. case 2:
  803. throw new Exception('提交时间超时。验证你所提交的时间戳超过3分钟后拒绝接受', $result);
  804. break;
  805. case 3:
  806. throw new Exception('参数有误', $result);
  807. break;
  808. case 4:
  809. throw new Exception('sign加密验证失败', $result);
  810. break;
  811. default:
  812. throw new Exception('打印机设置错误', $result);
  813. break;
  814. }
  815. }
  816. /**
  817. * 补齐空格
  818. * 截取$n个中文字符长度
  819. */
  820. private function r_mb_str($input, $n)
  821. {
  822. $string = "";
  823. $count = 0;
  824. $c_count = 0;
  825. for ($i = 0; $i < mb_strlen($input, 'UTF-8'); $i++) {
  826. $char = mb_substr($input, $i, 1, 'UTF-8');
  827. $string .= $char;
  828. if (strlen($char) == 3) {
  829. $count += 2;
  830. $c_count++;
  831. } else {
  832. $count += 1;
  833. }
  834. if ($count >= 2 * $n) {
  835. break;
  836. }
  837. }
  838. if ($count < 2 * $n) {
  839. $string = str_pad($string, 2 * $n + $c_count);
  840. }
  841. return $string;
  842. }
  843. // 飞鹅打印机
  844. public function printer_3()
  845. {
  846. $big = [
  847. 1 => [
  848. 'start' => '',
  849. 'end' => ''
  850. ],
  851. 2 => [
  852. 'start' => '<B>',
  853. 'end' => '</B>'
  854. ],
  855. 3 => [
  856. 'start' => '<DB>',
  857. 'end' => '</DB>'
  858. ],
  859. ];
  860. $start = $big[$this->setting->big]['start'];
  861. $end = $big[$this->setting->big]['end'];
  862. date_default_timezone_set("Asia/Shanghai");
  863. // $order = Order::findOne(['id' => $this->order_id, 'is_delete' => 0]);
  864. $order = $this->order;
  865. $addtime = date('Y-m-d H:i:s', $order['created_at']);
  866. // $goods_list = $this->getOrderGoodsList($order->id);
  867. $goods_list = $this->goods_list;
  868. $time = $this->printer_setting['time'];//打印联数
  869. $content = "";
  870. if ($this->mch && $this->mch->name){
  871. $title_name = $this->mch->name;
  872. }else{
  873. $title_name = $this->store_name;
  874. }
  875. $content .= "<CB>{$title_name}</CB><BR>";
  876. if (!empty($order['food_flag_id'])) {
  877. $FoodFlag = FoodFlag::findOne($order['food_flag_id']);
  878. $food_num = $FoodFlag->table_num;
  879. $content .= "桌号:{$food_num}\n";
  880. }
  881. if ($this->is_refund) {
  882. $content .= "订单类型:退款订单<BR>";
  883. } else {
  884. if ($order['food_code']) {
  885. $content .= "标识码:{$order['food_code']}<BR>";
  886. }
  887. $content .= "订单类型:{$this->extra['order_type']}<BR>";
  888. }
  889. $content .= "支付方式:{$this->extra['pay_type']}<BR>";
  890. $isPay = $order['is_pay'] == 1 ? '已支付' : '未支付';
  891. $content .= "支付状态:{$isPay}<BR>";
  892. $content .= "发货方式:{$this->extra['send_type']}<BR>";
  893. $content .= "订单号:{$order['order_no']}<BR>";
  894. $content .= "下单时间:{$addtime}<BR>";
  895. if ($this->setting->is_attr == 0) {
  896. $content .= "--------------------------------<BR>";
  897. $content .= "名称      单价 数量 金额<BR>";
  898. $content .= "--------------------------------<BR>";
  899. // foreach ($goods_list as $k => $v) {
  900. // $price = round($v['total_price'] / $v['num'], 2);
  901. // $content .= $this->r_str_pad($v['name']) . " " . str_pad($price, 5) . " " . str_pad($v['num'], 4) . " " . round($v['total_price'], 2) . '<BR>';
  902. // }
  903. foreach ($goods_list as $k => $v) {
  904. $price = round($v['total_price'] / $v['num'], 2);
  905. $arr = $this->r_str_pad_1($v['name'], 7);
  906. foreach ($arr as $index => $value) {
  907. if ($index == 0) {
  908. $content .= $value . " " . str_pad($price, 5) . " " . str_pad($v['num'], 6) . " " . round($v['total_price'], 2) . '<BR>';
  909. } else {
  910. $content .= $value . '<BR>';
  911. }
  912. }
  913. }
  914. } else {
  915. /**
  916. * $content .= "--------------------------------<BR>";
  917. * $content .= "名称   规格  单价 数量 金额<BR>";
  918. * $content .= "--------------------------------<BR>";
  919. * foreach ($goods_list as $k => $v) {
  920. * $attr = json_decode($v['attr'], true);
  921. * $attr_arr = [];
  922. * foreach ($attr as $i => $j) {
  923. * $attr_arr[] = $j['attr_group_name'] . $j['attr_name'];
  924. * }
  925. * $attr_str = implode(',', $attr_arr);
  926. * $attr_arr_1 = $this->r_str_pad_1($attr_str, 4);
  927. * $price = round($v['total_price'] / $v['num'], 2);
  928. * $arr = $this->r_str_pad_1($v['name'], 4);
  929. * $a = count($attr_arr_1) >= count($arr) ? $attr_arr_1 : $arr;
  930. * foreach ($a as $index => $value) {
  931. * $string = "";
  932. * $string_1 = "";
  933. * if (isset($arr[$index])) {
  934. * $string = $arr[$index];
  935. * }
  936. * if (isset($attr_arr_1[$index])) {
  937. * if (!$string) {
  938. * $string = str_pad($string, 8);
  939. * }
  940. * $string_1 = $attr_arr_1[$index];
  941. * }
  942. * if ($index == 0) {
  943. * $content .= $string . " " . $string_1 . str_pad($price, 5) . " " . str_pad($v['num'], 3) . " " . round($v['total_price'], 2) . '<BR>';
  944. * } else {
  945. * $content .= $string . " " . $string_1 . '<BR>';
  946. * }
  947. * }
  948. * }
  949. * */
  950. $content .= "--------------------------------<BR>";
  951. $content .= "名称 数量 金额 <BR>";
  952. $content .= "--------------------------------<BR>";
  953. foreach ($goods_list as $k => $v) {
  954. $name = $v['name'];
  955. if (!empty($v['attr'])) {
  956. if (is_array($v['attr'])) {
  957. $attr = $v['attr'];
  958. } else {
  959. $attr = json_decode($v['attr'], true);
  960. }
  961. $attr_arr = [];
  962. if ($attr['attr_list']) {
  963. $attr = $attr['attr_list'];
  964. }
  965. foreach ($attr as $i => $j) {
  966. $attr_arr[] = ($j['attr_group_name'] ?? '') . $j['attr_name'];
  967. }
  968. $attr_str = implode(',', $attr_arr);
  969. $name = $v['name'] . '(' . $attr_str . ')';
  970. }
  971. $name_arr = $this->r_str_pad_1($name, 8);
  972. foreach ($name_arr as $index => $value) {
  973. if ($index == count($name_arr) - 1) {
  974. $content .= $name_arr[$index] . " " . str_pad('×' . $v['num'], 7) . " " . round($v['total_price'] ?? $v['price'], 2) . "<BR>";
  975. } else {
  976. $content .= $name_arr[$index] . '<BR>';
  977. }
  978. }
  979. }
  980. }
  981. $content .= "--------------------------------<BR>";
  982. if ($order['express_price']) {
  983. $content .= "运费:{$order['express_price']}元<BR>";
  984. }
  985. $content .= "总计:{$order['total_price']}元<BR>";
  986. if (isset($order['user_coupon_id'])) {
  987. $content .= "优惠券优惠:{$order['coupon_sub_price']}元<BR>";
  988. }
  989. if (isset($order['integral'])) {
  990. $integral = json_decode($order['integral'], true);
  991. if ($integral['forehead'] != 0) {
  992. $content .= "积分抵扣:{$integral['forehead']}元<BR>";
  993. }
  994. }
  995. // if (isset($order['discount']) && $order['discount'] < 10) {
  996. // $content .= "会员折扣:{$order['discount']}折<BR>";
  997. // }
  998. if ($this->is_refund) {
  999. $content .= "实退:{$order['pay_price']}元<BR>";
  1000. } else {
  1001. $content .= $order['is_pay'] == 1 ? "实付:{$order['pay_price']}元<BR>" : "实付: 0元<BR>";
  1002. }
  1003. // if ($this->order_type != 3) {
  1004. // if ((isset($order['is_offline']) && $order['is_offline'] != 1) || (isset($order['offline']) && $order['offline'] != 2)) {
  1005. //
  1006. // } else {
  1007. // $content .= "{$start}联系人:{$order['name']}<BR>";
  1008. // $content .= "联系人电话:{$order['mobile']}<BR>{$end}";
  1009. // $content .= "--------------------------------<BR>";
  1010. // $content .= "门店信息<BR>";
  1011. // $content .= "{$this->order->shop->name}<BR>";
  1012. // $content .= "{$this->order->shop->mobile}<BR>";
  1013. // $content .= "{$this->order->shop->address}<BR>";
  1014. // $content .= "--------------------------------<BR>";
  1015. // }
  1016. // }
  1017. if (isset($order['is_offline']) && $order['is_offline'] != 1) {
  1018. $content .= "{$start}收货人:{$order['name']}<BR>";
  1019. $content .= "收货地址:{$order['address']}<BR>";
  1020. $content .= "收货人电话:{$order['mobile']}<BR>{$end}";
  1021. $content .= "--------------------------------<BR>";
  1022. } else {
  1023. $content .= "{$start}联系人:{$order['name']}<BR>";
  1024. $content .= "联系人电话:{$order['mobile']}<BR>{$end}";
  1025. $content .= "--------------------------------<BR>";
  1026. // 门店信息要根据订单上的md_id查询门店 不能根据get_md_id获取 也不能根据构造函数中传入的md_id 因为供货商打印 或者 平台打印 或者 商城打印的时候 是不会传md_id的
  1027. $md = Md::findOne($order['md_id']);
  1028. $content .= "门店信息<BR>";
  1029. $content .= "门店名称:{$md->name}<BR>";
  1030. $content .= "门店联系人:{$md->contact}<BR>";
  1031. $content .= "门店电话:{$md->mobile}<BR>";
  1032. $content .= "门店地址:{$md->address}<BR>";
  1033. $content .= "--------------------------------<BR>";
  1034. }
  1035. if ($order['remark']) {
  1036. $content .= "备注:{$order['remark']}<BR>";
  1037. } else {
  1038. $orderForm = OrderForm::findAll([
  1039. 'order_id' => $this->order['id'], 'store_id' => $this->store_id, 'is_delete' => 0
  1040. ]);
  1041. if ($orderForm) {
  1042. /* @var $orderForm OrderForm[] */
  1043. foreach ($orderForm as $item) {
  1044. $content .= $item->key . ':' . $item->value . "<BR>";
  1045. }
  1046. }
  1047. }
  1048. if ($order['seller_comments']) {
  1049. $content .= "商家备注:{$order['seller_comments']}<BR>";
  1050. }
  1051. // var_dump($content);die;
  1052. $resule = $this->wp_print($this->printer_setting['sn'], $content, $time);
  1053. $resule = Json::decode($resule);
  1054. if ($resule['ret'] != 0) {
  1055. throw new Exception($resule['msg'], $resule);
  1056. }
  1057. return $resule;
  1058. }
  1059. // 飞鹅标签打印机
  1060. public function printer_7(){
  1061. date_default_timezone_set("Asia/Shanghai");
  1062. $order = $this->order;
  1063. $goods_list = $this->goods_list;
  1064. $time = $this->printer_setting['time'];//打印联数
  1065. //商品信息
  1066. $glist = array(
  1067. 'orderid'=>'001',
  1068. 'remark'=>'',
  1069. 'name'=>'',
  1070. 'tel'=>'',
  1071. 'z_number'=>'',
  1072. 'goods'=>array(
  1073. // '0'=>array('title'=>'可乐鸡翅'),
  1074. ),
  1075. );
  1076. if (!empty($order['food_flag_id'])) {
  1077. $FoodFlag = FoodFlag::findOne($order['food_flag_id']);
  1078. $food_num = $FoodFlag->table_num;
  1079. $glist['z_number'] = $food_num . '号桌';
  1080. }else{
  1081. $glist['z_number'] = " ";
  1082. }
  1083. if ($order['remark']) {
  1084. $glist['remark'] = $order['remark'];
  1085. }
  1086. if ($this->is_refund) {
  1087. $glist['remark'] = "**退款订单 {$order['pay_price']}元";
  1088. }
  1089. if ($order['food_code']) {
  1090. $glist['orderid'] = $order['food_code'];
  1091. }else{
  1092. $glist['orderid'] = substr($order['order_no'], -4);
  1093. }
  1094. foreach ($goods_list as $k => $v) {
  1095. if($v['is_food_ext_goods']){
  1096. continue;
  1097. }
  1098. $food_ext_goods_names = [];
  1099. if (!empty($v['food_ext_goods'])) {
  1100. $food_ext_goods = json_decode($v['food_ext_goods'], true);
  1101. if (!empty($food_ext_goods)) {
  1102. $food_ext_goods_names = array_column($food_ext_goods, 'name');
  1103. }
  1104. }
  1105. $name = $v['name'];
  1106. if (!empty($v['attr'])) {
  1107. if (is_array($v['attr'])) {
  1108. $attr = $v['attr'];
  1109. } else {
  1110. $attr = json_decode($v['attr'], true);
  1111. }
  1112. $attr_arr = [];
  1113. if ($attr['attr_list']) {
  1114. $attr = $attr['attr_list'];
  1115. }
  1116. foreach ($attr as $i => $j) {
  1117. $attr_arr[] = $j['attr_name'];
  1118. }
  1119. $attr_str = implode(',', $attr_arr);
  1120. }
  1121. for($i=1; $i<=$v['num']; $i++){
  1122. $glist['goods'][] = [
  1123. 'attr_str'=>$attr_str,
  1124. 'title'=>$name,
  1125. 'food_ext_goods_names'=>$food_ext_goods_names,
  1126. ];
  1127. }
  1128. }
  1129. $glist['name'] = $order['name'];
  1130. $glist['tel'] = $order['mobile'];
  1131. $num = count($glist['goods']);
  1132. $itemI = 1;
  1133. $contents = [];
  1134. foreach ($glist['goods'] as $key => $value) {
  1135. $content = '<TEXT x="0" y="10" font="12" w="1" h="1" r="0">'.$this->LR('#'.$glist['orderid'], $itemI.'/'.$num,23,$glist['z_number']) .'</TEXT>';
  1136. $glist['remark'] && $content .= '<TEXT x="0" y="40" font="12" w="1" h="1" r="0">'.$glist['remark'].'</TEXT>';
  1137. $content .= '<TEXT x="0" y="70" font="12" w="'. (mb_strlen($value["title"]) > 6 ? 1 : 2) .'" h="2" r="0">'.$this->LR('', '', (mb_strlen($value["title"]) > 6 ? 26 : 13), $value["title"]).'</TEXT>';
  1138. $value['attr_str'] && $content .= '<TEXT x="0" y="130" font="12" w="1" h="1" r="0">'.$this->LR('', '', 26, mb_substr($value['attr_str'], 0, 12)).'</TEXT>';
  1139. mb_strlen($value['attr_str']) > 12 && $content .= '<TEXT x="0" y="156" font="12" w="1" h="1" r="0">'.$this->LR('', '', 26, mb_substr($value['attr_str'], 12)).'</TEXT>';
  1140. if($value['food_ext_goods_names']){
  1141. $content .= '<TEXT x="0" y="185" font="12" w="1" h="1" r="0">'.$this->LR('', '', 26, implode(',', $value['food_ext_goods_names'])).'</TEXT>';
  1142. }
  1143. $content .= '<TEXT x="0" y="215" font="12" w="1" h="1" r="0">'. $this->LR(mb_substr($glist['name'], 0, 1) . '**', substr_replace($glist['tel'], '****', 3, 4), 23).'</TEXT>';//40mm宽度标签纸一行占用26个字符,50mm宽度标签纸请改成32个字符
  1144. $contents[] = [
  1145. 'content' => $content,
  1146. 'times' => $time,
  1147. ];
  1148. $itemI++;
  1149. }
  1150. \Yii::error($contents);
  1151. // var_dump($contents);die;
  1152. $resule = $this->wp_print_label($this->printer_setting['sn'], json_encode($contents), 1);
  1153. $resule = Json::decode($resule);
  1154. debug_log($resule, __CLASS__);
  1155. \Yii::error($resule);
  1156. if ($resule['ret'] != 0) {
  1157. throw new Exception($resule['msg'], $resule);
  1158. }
  1159. return $resule;
  1160. }
  1161. /**
  1162. * [统计字符串字节数补空格,实现左右排版对齐]
  1163. * @param [string] $str_left [左边字符串]
  1164. * @param [string] $str_right [右边字符串]
  1165. * @param [int] $length [输入当前纸张规格一行所支持的最大字母数量]
  1166. * 58mm的机器,一行打印16个汉字,32个字母;76mm的机器,一行打印22个汉字,33个字母,80mm的机器,一行打印24个汉字,48个字母
  1167. * 标签机宽度50mm,一行32个字母,宽度40mm,一行26个字母
  1168. * @return [string] [返回处理结果字符串]
  1169. */
  1170. public function LR($str_left,$str_right,$length,$str_center = ''){
  1171. if( empty($str_left) && empty($str_right) && empty($length) ) return '请输入正确的参数';
  1172. $kw = '';
  1173. $str_left_lenght = strlen(iconv("UTF-8", "GBK//IGNORE", $str_left));
  1174. $str_right_lenght = strlen(iconv("UTF-8", "GBK//IGNORE", $str_right));
  1175. $k = $length - ($str_left_lenght+$str_right_lenght);
  1176. for($q=0;$q<$k;$q++){
  1177. $kw .= ' ';
  1178. }
  1179. if($str_center){
  1180. $str_center_lenght = strlen(iconv("UTF-8", "GBK//IGNORE", $str_center));
  1181. if(strlen($kw) > $str_center_lenght){
  1182. $kk = (strlen($kw) - $str_center_lenght) / 2;
  1183. $kkw = '';
  1184. for($q=1;$q<$kk;$q++){
  1185. $kkw .= ' ';
  1186. }
  1187. $kw = $kkw . $str_center . $kkw;
  1188. }else{
  1189. $kw = $str_center;
  1190. }
  1191. }
  1192. return $str_left.$kw.$str_right;
  1193. }
  1194. /**
  1195. * 方法1
  1196. * 拼凑订单内容时可参考如下格式
  1197. * 根据打印纸张的宽度,自行调整内容的格式,可参考下面的样例格式
  1198. */
  1199. private function wp_print($printer_sn, $orderInfo, $times)
  1200. {
  1201. $user = $this->printer_setting['user'];
  1202. $ukey = $this->printer_setting['ukey'];
  1203. $time = time();
  1204. $ip = 'api.feieyun.cn';
  1205. $path = '/Api/Open/';
  1206. $content = array(
  1207. 'user' => $user,
  1208. 'stime' => $time,
  1209. 'sig' => sha1($user . $ukey . $time),
  1210. 'apiname' => 'Open_printMsg',
  1211. 'sn' => $printer_sn,
  1212. 'content' => $orderInfo,
  1213. 'times' => $times//打印次数
  1214. );
  1215. $client = new FeieYun($ip, 80);
  1216. if (!$client->post($path, $content)) {
  1217. return 'error';
  1218. } else {
  1219. //服务器返回的JSON字符串,建议要当做日志记录起来
  1220. return $client->getContent();
  1221. }
  1222. }
  1223. private function wp_print_label($printer_sn, $orderInfos, $times)
  1224. {
  1225. $user = $this->printer_setting['user'];
  1226. $ukey = $this->printer_setting['ukey'];
  1227. $time = time();
  1228. $ip = 'api.feieyun.cn';
  1229. $path = '/Api/Open/';
  1230. $content = array(
  1231. 'user' => $user,
  1232. 'stime' => $time,
  1233. 'sig' => sha1($user . $ukey . $time),
  1234. 'apiname' => 'Open_printLabelMsg',
  1235. 'sn' => $printer_sn,
  1236. 'contents' => $orderInfos,
  1237. 'times' => $times//打印次数
  1238. );
  1239. $client = new FeieYun($ip, 80);
  1240. if (!$client->post($path, $content)) {
  1241. return 'error';
  1242. } else {
  1243. //服务器返回的JSON字符串,建议要当做日志记录起来
  1244. return $client->getContent();
  1245. }
  1246. }
  1247. // 秒杀商品详情
  1248. public function getMsOrderGoodsList()
  1249. {
  1250. $ms_goods = MsGoods::findOne(['id' => $this->order['goods_id']]);
  1251. $list['name'] = $ms_goods->name;
  1252. $list['total_price'] = $this->order['pay_price'];
  1253. $list['num'] = $this->order['num'];
  1254. $list['attr'] = $this->order['attr'];
  1255. $new_list = [];
  1256. array_push($new_list, $list);
  1257. return $new_list;
  1258. }
  1259. // 拼团商品详情
  1260. public function getPtOrderGoodsList()
  1261. {
  1262. $order_detail_list = PtOrderDetail::find()->alias('od')
  1263. ->leftJoin(['g' => PtGoods::tableName()], 'od.goods_id=g.id')
  1264. ->where(['od.is_delete' => 0, 'od.order_id' => $this->order['id']])
  1265. ->select(['od.*', 'g.name'])->asArray()->all();
  1266. return $order_detail_list;
  1267. }
  1268. // 预约商品详情
  1269. public function getYyOrderGoodsList()
  1270. {
  1271. $order_detail_list = YyOrder::find()->alias('od')
  1272. ->leftJoin(['g' => YyGoods::tableName()], 'od.goods_id = g.id')
  1273. ->where(['od.is_delete' => 0, 'od.order_id' => $this->order['id']])
  1274. ->select(['od.*', 'g.name'])->asArray()->one();
  1275. $order_detail_list['num'] = 1;
  1276. return [$order_detail_list];
  1277. }
  1278. public function printer_4()
  1279. {
  1280. $big = $this->setting->big;
  1281. $order = $this->order;
  1282. $addtime = date('Y-m-d H:i:s', $order['created_at']);
  1283. $goods_list = $this->goods_list;
  1284. header("Content-Type: text/html;charset=utf-8");
  1285. $content = "";
  1286. if ($this->mch && $this->mch->name) {
  1287. $title_name = $this->mch->name;
  1288. } else {
  1289. $title_name = $this->store_name;
  1290. }
  1291. $content .= "{$title_name}<gpBr/>";
  1292. if (!empty($order['food_flag_id'])) {
  1293. $FoodFlag = FoodFlag::findOne($order['food_flag_id']);
  1294. $food_num = $FoodFlag->table_num;
  1295. $content .= "桌号:{$food_num}\n";
  1296. }
  1297. if ($this->is_refund) {
  1298. $content .= "订单类型:退款订单<gpBr/>";
  1299. } else {
  1300. if ($order['food_code']) {
  1301. $content .= "标识码:{$order['food_code']}<gpBr/>";
  1302. }
  1303. $content .= "订单类型:{$this->extra['order_type']}<gpBr/>";
  1304. }
  1305. $content .= "支付方式:{$this->extra['pay_type']}<gpBr/>";
  1306. $isPay = $order['is_pay'] == 1 ? '已支付' : '未支付';
  1307. $content .= "支付状态:{$isPay}<gpBr/>";
  1308. $content .= "发货方式:{$this->extra['send_type']}<gpBr/>";
  1309. $content .= "订单号:{$order['order_no']}<gpBr/>";
  1310. $content .= "下单时间:{$addtime}<gpBr/>";
  1311. if ($this->setting->is_attr == 0) {
  1312. $content .= "--------------------------------<gpBr/>";
  1313. $content .= "名称      单价 数量 金额<gpBr/>";
  1314. $content .= "--------------------------------<gpBr/>";
  1315. foreach ($goods_list as $k => $v) {
  1316. $price = round($v['total_price'] / $v['num'], 2);
  1317. $arr = $this->r_str_pad_1($v['name'], 7);
  1318. foreach ($arr as $index => $value) {
  1319. if ($index == 0) {
  1320. $content .= $value . " " . str_pad($price, 5) . " " . str_pad($v['num'], 6) . " " . round($v['total_price'], 2) . '<gpBr/>';
  1321. } else {
  1322. $content .= $value . '<gpBr/>';
  1323. }
  1324. }
  1325. }
  1326. } else {
  1327. $content .= "--------------------------------<gpBr/>";
  1328. $content .= "名称 数量 金额 <gpBr/>";
  1329. $content .= "--------------------------------<gpBr/>";
  1330. foreach ($goods_list as $k => $v) {
  1331. $name = $v['name'];
  1332. if (!empty($v['attr'])) {
  1333. $attr = json_decode($v['attr'], true);
  1334. $attr_arr = [];
  1335. if($attr['attr_list']){
  1336. $attr = $attr['attr_list'];
  1337. }
  1338. foreach ($attr as $i => $j) {
  1339. $attr_arr[] = $j['attr_group_name'] . $j['attr_name'];
  1340. }
  1341. $attr_str = implode(',', $attr_arr);
  1342. $name = $v['name'] . '(' . $attr_str . ')';
  1343. }
  1344. $name_arr = $this->r_str_pad_1($name, 8);
  1345. foreach ($name_arr as $index => $value) {
  1346. if ($index == count($name_arr) - 1) {
  1347. $content .= $name_arr[$index] . " " . str_pad('×' . $v['num'], 7) . " " . round($v['total_price'], 2) . "<gpBr/>";
  1348. } else {
  1349. $content .= $name_arr[$index] . '<gpBr/>';
  1350. }
  1351. }
  1352. }
  1353. }
  1354. $content .= "--------------------------------<gpBr/>";
  1355. if ($order['express_price']) {
  1356. $content .= "运费:{$order['express_price']}元<gpBr/>";
  1357. }
  1358. $content .= "总计:{$order['total_price']}元<gpBr/>";
  1359. if (isset($order['user_coupon_id'])) {
  1360. $content .= "优惠券优惠:{$order['coupon_sub_price']}元<gpBr/>";
  1361. }
  1362. if (isset($order['integral'])) {
  1363. $integral = json_decode($order['integral'], true);
  1364. if ($integral['forehead'] != 0) {
  1365. $content .= "积分抵扣:{$integral['forehead']}元<gpBr/>";
  1366. }
  1367. }
  1368. if (isset($order['discount']) && $order['discount'] < 10) {
  1369. $content .= "会员折扣:{$order['discount']}折<gpBr/>";
  1370. }
  1371. if ($this->is_refund) {
  1372. $content .= "实退:{$order['pay_price']}元<gpBr/>";
  1373. } else {
  1374. $content .= $order['is_pay'] == 1 ? "实付:{$order['pay_price']}元<gpBr/>" : "实付:0元<gpBr/>";
  1375. }
  1376. if ($this->order_type != 3) {
  1377. if ((isset($order['is_offline']) && $order['is_offline'] != 1) || (isset($order['offline']) && $order['offline'] != 2)) {
  1378. $content .= "<gpWord Align=0 Bold=0 Wsize={$big} Hsize={$big} Reverse=0 Underline=0>收货人:{$order['name']}</gpWord><gpBr/>";
  1379. $content .= "<gpWord Align=0 Bold=0 Wsize={$big} Hsize={$big} Reverse=0 Underline=0>收货地址:{$order['address']}</gpWord><gpBr/>";
  1380. $content .= "<gpWord Align=0 Bold=0 Wsize={$big} Hsize={$big} Reverse=0 Underline=0>收货人电话:{$order['mobile']}</gpWord><gpBr/>";
  1381. $content .= "--------------------------------<gpBr/>";
  1382. } else {
  1383. $content .= "<gpWord Align=0 Bold=0 Wsize={$big} Hsize={$big} Reverse=0 Underline=0>联系人:{$order['name']}</gpWord><gpBr/>";
  1384. $content .= "<gpWord Align=0 Bold=0 Wsize={$big} Hsize={$big} Reverse=0 Underline=0>联系人电话:{$order['mobile']}</gpWord><gpBr/>";
  1385. $content .= "--------------------------------<gpBr/>";
  1386. $content .= "门店信息<gpBr/>";
  1387. // 门店信息要根据订单上的md_id查询门店 不能根据get_md_id获取 也不能根据构造函数中传入的md_id 因为供货商打印 或者 平台打印 或者 商城打印的时候 是不会传md_id的
  1388. $md = Md::findOne($order['md_id']);
  1389. $content .= "{$md->name}<gpBr/>";
  1390. $content .= "{$md->mobile}<gpBr/>";
  1391. $content .= "{$md->address}<gpBr/>";
  1392. $content .= "--------------------------------<gpBr/>";
  1393. }
  1394. }
  1395. if ($order['remark']) {
  1396. $content .= "备注:{$order['remark']}<gpBr/>";
  1397. } else {
  1398. $orderForm = OrderForm::findAll([
  1399. 'order_id' => $this->order['id'], 'store_id' => $this->store_id, 'is_delete' => 0
  1400. ]);
  1401. if ($orderForm) {
  1402. /* @var $orderForm OrderForm[] */
  1403. foreach ($orderForm as $item) {
  1404. $content .= $item->key . ':' . $item->value . "<gpBr/>";
  1405. }
  1406. }
  1407. }
  1408. if ($order['seller_comments']) {
  1409. $content .= "商家备注:{$order['seller_comments']}<gpBr/>";
  1410. }
  1411. $content .= "<gpCut/>";
  1412. $res = $this->poscom($content);
  1413. $res = Json::decode($res);
  1414. if ($res['code'] != 0) {
  1415. throw new Exception($res['msg'], $res);
  1416. }
  1417. return $res;
  1418. }
  1419. public function printer_5($printer = null)
  1420. {
  1421. $big = [
  1422. 1 => [
  1423. 'start' => '',
  1424. 'end' => ''
  1425. ],
  1426. 2 => [
  1427. 'start' => "<B>",
  1428. 'end' => "</B>"
  1429. ],
  1430. 3 => [
  1431. 'start' => '<B2>',
  1432. 'end' => '</B2>'
  1433. ],
  1434. ];
  1435. $start = $big[$this->setting->big]['start'];
  1436. $end = $big[$this->setting->big]['end'];
  1437. date_default_timezone_set("Asia/Shanghai");
  1438. $order = $this->order;
  1439. $addtime = date('Y-m-d H:i:s', $order['created_at']);
  1440. $goods_list = $this->goods_list;
  1441. header("Content-Type: text/html;charset=utf-8");
  1442. $time = $this->printer_setting['time'];//打印联数
  1443. $content = "";
  1444. if ($this->mch && $this->mch->name){
  1445. $title_name = $this->mch->name;
  1446. }else{
  1447. $title_name = $this->store_name;
  1448. }
  1449. $content .= "<CB>{$title_name}<BR><BR></CB>";
  1450. if (!empty($order['food_flag_id'])) {
  1451. $FoodFlag = FoodFlag::findOne($order['food_flag_id']);
  1452. $food_num = $FoodFlag->table_num;
  1453. $content .= "桌号:{$food_num}\n";
  1454. }
  1455. if ($this->is_refund) {
  1456. $content .= "订单类型:退款订单<BR>";
  1457. } else {
  1458. if ($order['food_code']) {
  1459. $content .= "标识码:{$order['food_code']}<BR>";
  1460. }
  1461. $content .= "订单类型:{$this->extra['order_type']}<BR>";
  1462. }
  1463. $content .= "支付方式:{$this->extra['pay_type']}<BR>";
  1464. $isPay = $order['is_pay'] == 1 ? '已支付' : '未支付';
  1465. $content .= "支付状态:{$isPay}<BR>";
  1466. $content .= "发货方式:{$this->extra['send_type']}<BR>";
  1467. $content .= "订单号:{$order['order_no']}<BR>";
  1468. $content .= "下单时间:{$addtime}<BR>";
  1469. if ($this->setting->is_attr == 0) {
  1470. $content .= "--------------------------------<BR>";
  1471. $content .= "名称      单价 数量 金额<BR>";
  1472. $content .= "--------------------------------<BR>";
  1473. foreach ($goods_list as $k => $v) {
  1474. $price = round($v['total_price'] / $v['num'], 2);
  1475. $arr = $this->r_str_pad_1($v['name'], 7);
  1476. foreach ($arr as $index => $value) {
  1477. if ($index == 0) {
  1478. $content .= $value . " " . str_pad($price, 5) . " " . str_pad($v['num'], 6) . " " . round($v['total_price'], 2) . '<BR>';
  1479. } else {
  1480. $content .= $value . '<BR>';
  1481. }
  1482. }
  1483. }
  1484. } else {
  1485. $content .= "--------------------------------<BR>";
  1486. $content .= "名称 数量 金额 <BR>";
  1487. $content .= "--------------------------------<BR>";
  1488. foreach ($goods_list as $k => $v) {
  1489. $name = $v['name'];
  1490. if (!empty($v['attr'])) {
  1491. $attr = json_decode($v['attr'], true);
  1492. $attr_arr = [];
  1493. if($attr['attr_list']){
  1494. $attr = $attr['attr_list'];
  1495. }
  1496. foreach ($attr as $i => $j) {
  1497. $attr_arr[] = $j['attr_group_name'] . $j['attr_name'];
  1498. }
  1499. $attr_str = implode(',', $attr_arr);
  1500. $name = $v['name'] . '(' . $attr_str . ')';
  1501. }
  1502. $name_arr = $this->r_str_pad_1($name, 8);
  1503. foreach ($name_arr as $index => $value) {
  1504. if ($index == count($name_arr) - 1) {
  1505. $content .= $name_arr[$index] . " " . str_pad('×' . $v['num'], 7) . " " . round($v['total_price'], 2) . "<BR>";
  1506. } else {
  1507. $content .= $name_arr[$index] . '<BR>';
  1508. }
  1509. }
  1510. }
  1511. }
  1512. $content .= "--------------------------------<BR>";
  1513. if ($order['express_price']) {
  1514. if ($order['express_price'] == 0) {
  1515. $delivery_info = DeliveryInfo::findOne(['order_no' => $order['order_no']]);
  1516. if ($delivery_info && $delivery_info->fee > 0) {
  1517. $order['express_price'] = $delivery_info->fee;
  1518. }
  1519. }
  1520. $content .= "运费:{$order['express_price']}元<BR>";
  1521. }
  1522. $content .= "总计:{$order['total_price']}元<BR>";
  1523. if (isset($order['user_coupon_id'])) {
  1524. $content .= "优惠券优惠:{$order['coupon_sub_price']}元<BR>";
  1525. }
  1526. if (isset($order['integral'])) {
  1527. $integral = json_decode($order['integral'], true);
  1528. if ($integral['forehead'] != 0) {
  1529. $content .= "积分抵扣:{$integral['forehead']}元<BR>";
  1530. }
  1531. }
  1532. if (isset($order['discount']) && $order['discount'] < 10) {
  1533. $content .= "会员折扣:{$order['discount']}折<BR>";
  1534. }
  1535. if ($this->is_refund) {
  1536. $content .= "实退:{$order['pay_price']}元<BR>";
  1537. } else {
  1538. $content .= $order['is_pay'] == 1 ? "实付:{$order['pay_price']}元<BR>" : "实付:0元<BR>";
  1539. }
  1540. if ($this->order_type != 3) {
  1541. if ((isset($order['is_offline']) && $order['is_offline'] != 1) || (isset($order['offline']) && $order['offline'] != 2)) {
  1542. $content .= "{$start}收货人:{$order['name']}<BR>";
  1543. $content .= "收货地址:{$order['address']}<BR>";
  1544. $content .= "收货人电话:{$order['mobile']}<BR>{$end}";
  1545. $content .= "--------------------------------<BR>";
  1546. } else {
  1547. $content .= "{$start}联系人:{$order['name']}<BR>";
  1548. $content .= "联系人电话:{$order['mobile']}<BR>{$end}";
  1549. $content .= "--------------------------------<BR>";
  1550. $content .= "门店信息<BR>";
  1551. // 门店信息要根据订单上的md_id查询门店 不能根据get_md_id获取 也不能根据构造函数中传入的md_id 因为供货商打印 或者 平台打印 或者 商城打印的时候 是不会传md_id的
  1552. $md = Md::findOne($order['md_id']);
  1553. $content .= "{$md->name}<BR>";
  1554. $content .= "{$md->mobile}<BR>";
  1555. $content .= "{$md->address}<BR>";
  1556. $content .= "--------------------------------<BR>";
  1557. }
  1558. }
  1559. if ($order['content']) {
  1560. $content .= "备注:{$order['content']}<BR>";
  1561. } else {
  1562. $orderForm = OrderForm::findAll([
  1563. 'order_id' => $this->order['id'], 'store_id' => $this->store_id, 'is_delete' => 0
  1564. ]);
  1565. if ($orderForm) {
  1566. /* @var $orderForm OrderForm[] */
  1567. foreach ($orderForm as $item) {
  1568. $content .= $item->key . ':' . $item->value . "<BR>";
  1569. }
  1570. }
  1571. }
  1572. if ($order['seller_comments']) {
  1573. $content .= "商家备注:{$order['seller_comments']}<BR>";
  1574. }
  1575. try {
  1576. $result = (new XhyPrint($printer->id))->print($this->printer_setting['sn'], $content, $time);
  1577. \Yii::warning('==>>' . $result);
  1578. } catch (\Exception $e) {
  1579. throw new Exception($e->getMessage(), $result);
  1580. }
  1581. return $result;
  1582. }
  1583. public function printer_6($printer = null)
  1584. {
  1585. $big = [
  1586. 1 => [
  1587. 'start' => '',
  1588. 'end' => ''
  1589. ],
  1590. 2 => [
  1591. 'start' => "<font# bolder=0 height=2 width=2>",
  1592. 'end' => "</font#>"
  1593. ],
  1594. 3 => [
  1595. 'start' => '<font# bolder=0 height=3 width=3>',
  1596. 'end' => '</font#>'
  1597. ],
  1598. ];
  1599. $start = $big[$this->setting->big]['start'];
  1600. $end = $big[$this->setting->big]['end'];
  1601. date_default_timezone_set("Asia/Shanghai");
  1602. $order = $this->order;
  1603. $addtime = date('Y-m-d H:i:s', $order['created_at']);
  1604. $goods_list = $this->goods_list;
  1605. header("Content-Type: text/html;charset=utf-8");
  1606. $time = $this->printer_setting['time'];//打印联数
  1607. $content = "";
  1608. if ($this->mch && $this->mch->name){
  1609. $title_name = $this->mch->name;
  1610. }else{
  1611. $title_name = $this->store_name;
  1612. }
  1613. $content .= "<C><font# bolder=0 height=2 width=2>{$title_name}</font#></C><BR><BR>";
  1614. if (!empty($order['food_flag_id'])) {
  1615. $FoodFlag = FoodFlag::findOne($order['food_flag_id']);
  1616. $food_num = $FoodFlag->table_num;
  1617. $content .= "桌号:{$food_num}\n";
  1618. }
  1619. if ($this->is_refund) {
  1620. $content .= "订单类型:退款订单<BR>";
  1621. } else {
  1622. if ($order['food_code']) {
  1623. $content .= "标识码:{$order['food_code']}<BR>";
  1624. }
  1625. $content .= "订单类型:{$this->extra['order_type']}<BR>";
  1626. }
  1627. $content .= "支付方式:{$this->extra['pay_type']}<BR>";
  1628. $isPay = $order['is_pay'] == 1 ? '已支付' : '未支付';
  1629. $content .= "支付状态:{$isPay}<BR>";
  1630. $content .= "发货方式:{$this->extra['send_type']}<BR>";
  1631. $content .= "订单号:{$order['order_no']}<BR>";
  1632. $content .= "下单时间:{$addtime}<BR>";
  1633. if ($this->setting->is_attr == 0) {
  1634. $content .= "--------------------------------<BR>";
  1635. $content .= "名称      单价 数量 金额<BR>";
  1636. $content .= "--------------------------------<BR>";
  1637. foreach ($goods_list as $k => $v) {
  1638. $price = round($v['total_price'] / $v['num'], 2);
  1639. $arr = $this->r_str_pad_1($v['name'], 7);
  1640. foreach ($arr as $index => $value) {
  1641. if ($index == 0) {
  1642. $content .= $value . " " . str_pad($price, 5) . " " . str_pad($v['num'], 6) . " " . round($v['total_price'], 2) . '<BR>';
  1643. } else {
  1644. $content .= $value . '<BR>';
  1645. }
  1646. }
  1647. }
  1648. } else {
  1649. $content .= "--------------------------------<BR>";
  1650. $content .= "名称 数量 金额 <BR>";
  1651. $content .= "--------------------------------<BR>";
  1652. foreach ($goods_list as $k => $v) {
  1653. $name = $v['name'];
  1654. if (!empty($v['attr'])) {
  1655. $attr = json_decode($v['attr'], true);
  1656. $attr_arr = [];
  1657. if($attr['attr_list']){
  1658. $attr = $attr['attr_list'];
  1659. }
  1660. foreach ($attr as $i => $j) {
  1661. $attr_arr[] = $j['attr_group_name'] . $j['attr_name'];
  1662. }
  1663. $attr_str = implode(',', $attr_arr);
  1664. $name = $v['name'] . '(' . $attr_str . ')';
  1665. }
  1666. $name_arr = $this->r_str_pad_1($name, 8);
  1667. foreach ($name_arr as $index => $value) {
  1668. if ($index == count($name_arr) - 1) {
  1669. $content .= $name_arr[$index] . " " . str_pad('×' . $v['num'], 7) . " " . round($v['total_price'], 2) . "<BR>";
  1670. } else {
  1671. $content .= $name_arr[$index] . '<BR>';
  1672. }
  1673. }
  1674. }
  1675. }
  1676. $content .= "--------------------------------<BR>";
  1677. if ($order['express_price']) {
  1678. if ($order['express_price'] == 0) {
  1679. $delivery_info = DeliveryInfo::findOne(['order_no' => $order['order_no']]);
  1680. if ($delivery_info && $delivery_info->fee > 0) {
  1681. $order['express_price'] = $delivery_info->fee;
  1682. }
  1683. }
  1684. $content .= "运费:{$order['express_price']}元<BR>";
  1685. }
  1686. $content .= "总计:{$order['total_price']}元<BR>";
  1687. if (isset($order['user_coupon_id'])) {
  1688. $content .= "优惠券优惠:{$order['coupon_sub_price']}元<BR>";
  1689. }
  1690. if (isset($order['integral'])) {
  1691. $integral = json_decode($order['integral'], true);
  1692. if ($integral['forehead'] != 0) {
  1693. $content .= "积分抵扣:{$integral['forehead']}元<BR>";
  1694. }
  1695. }
  1696. if (isset($order['discount']) && $order['discount'] < 10) {
  1697. $content .= "会员折扣:{$order['discount']}折<BR>";
  1698. }
  1699. if ($this->is_refund) {
  1700. $content .= "实退:{$order['pay_price']}元<BR>";
  1701. } else {
  1702. $content .= $order['is_pay'] == 1 ? "实付:{$order['pay_price']}元<BR>" : "实付:0元<BR>";
  1703. }
  1704. if ($this->order_type != 3) {
  1705. if ((isset($order['is_offline']) && $order['is_offline'] != 1) || (isset($order['offline']) && $order['offline'] != 2)) {
  1706. $content .= "{$start}收货人:{$order['name']}{$end}<BR>";
  1707. $content .= "{$start}收货地址:{$order['address']}{$end}<BR>";
  1708. $content .= "{$start}收货人电话:{$order['mobile']}{$end}<BR>";
  1709. $content .= "--------------------------------<BR>";
  1710. } else {
  1711. $content .= "{$start}联系人:{$order['name']}{$end}<BR>";
  1712. $content .= "{$start}联系人电话:{$order['mobile']}{$end}<BR>";
  1713. $content .= "--------------------------------<BR>";
  1714. $content .= "门店信息<BR>";
  1715. // 门店信息要根据订单上的md_id查询门店 不能根据get_md_id获取 也不能根据构造函数中传入的md_id 因为供货商打印 或者 平台打印 或者 商城打印的时候 是不会传md_id的
  1716. $md = Md::findOne($order['md_id']);
  1717. $content .= "{$md->name}<BR>";
  1718. $content .= "{$md->mobile}<BR>";
  1719. $content .= "{$md->address}<BR>";
  1720. $content .= "--------------------------------<BR>";
  1721. }
  1722. }
  1723. if ($order['remark']) {
  1724. $content .= "备注:{$order['content']}<BR>";
  1725. } else {
  1726. $orderForm = OrderForm::findAll([
  1727. 'order_id' => $this->order['id'], 'store_id' => $this->store_id, 'is_delete' => 0
  1728. ]);
  1729. if ($orderForm) {
  1730. /* @var $orderForm OrderForm[] */
  1731. foreach ($orderForm as $item) {
  1732. $content .= $item->key . ':' . $item->value . "<BR>";
  1733. }
  1734. }
  1735. }
  1736. if ($order['seller_comments']) {
  1737. $content .= "商家备注:{$order['seller_comments']}<BR>";
  1738. }
  1739. try {
  1740. $result = (new DqznPrint($printer->id))->print($this->printer_setting['sn'], $content, $time);
  1741. \Yii::warning('==>>' . $result);
  1742. } catch (\Exception $e) {
  1743. throw new Exception($e->getMessage(), $result);
  1744. }
  1745. return $result;
  1746. }
  1747. public function poscom($msgDetail, $times = 1)
  1748. {
  1749. $reqTime = $this->getMillisecond();
  1750. $memberCode = $this->printer_setting['memberCode'];
  1751. $deviceNo = $this->printer_setting['deviceNo'];
  1752. $apiKey = $this->printer_setting['apiKey'];
  1753. $orderNo = $this->order['order_no'] . '-' . $times . '-' . $this->print_type;
  1754. $securityCode = md5($memberCode . $deviceNo . $orderNo . $reqTime . $apiKey);
  1755. $url = 'http://api.poscom.cn/apisc/sendMsg';
  1756. $content['charset'] = 'UTF-8';
  1757. $content['reqTime'] = $reqTime;
  1758. $content['memberCode'] = $memberCode;
  1759. $content['deviceNo'] = $deviceNo;
  1760. $content['securityCode'] = $securityCode;
  1761. $content['msgDetail'] = $msgDetail;
  1762. $content['msgNo'] = $orderNo;
  1763. $content['mode'] = 2;
  1764. $content['times'] = $this->printer_setting['time'] > 5 ? 5 : $this->printer_setting['time'];
  1765. $res = $this->request_post($url, $content);
  1766. return $res;
  1767. }
  1768. public function getMillisecond()
  1769. {
  1770. list($t1, $t2) = explode(' ', microtime());
  1771. return (float)sprintf('%.0f', (floatval($t1) + floatval($t2)) * 1000);
  1772. }
  1773. public function request_post($url = '', $post_data = array())
  1774. {
  1775. if (empty($url) || empty($post_data)) {
  1776. return false;
  1777. }
  1778. $postUrl = $url;
  1779. $curlPost = $post_data;
  1780. $ch = curl_init();//初始化curl
  1781. curl_setopt($ch, CURLOPT_URL, $postUrl);//抓取指定网页
  1782. curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
  1783. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
  1784. curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
  1785. curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
  1786. $data = curl_exec($ch);//运行curl
  1787. curl_close($ch);
  1788. return $data;
  1789. }
  1790. /**
  1791. * @param $order_id
  1792. * @return mixed
  1793. * 砍价订单商品详情
  1794. */
  1795. public function getBargainOrderGoodsList($order_id)
  1796. {
  1797. $order_detail_list = BargainOrderDetail::find()->alias('od')
  1798. ->leftJoin(['g' => BargainGoods::tableName()], 'od.goods_id=g.id')
  1799. ->where([
  1800. 'od.is_delete' => 0,
  1801. 'od.order_id' => $order_id,
  1802. ])->select('od.*,g.name')->asArray()->all();
  1803. return $order_detail_list;
  1804. }
  1805. // /**
  1806. // * @param $order_id
  1807. // * @return mixed
  1808. // * 当面付订单商品详情
  1809. // */
  1810. // public function getScanOrderGoodsList($order_id)
  1811. // {
  1812. // $order_detail_list = \app\plugins\scanCodePay\models\OrderDetail::find()->alias('od')
  1813. // ->leftJoin(['g' => Goods::tableName()], 'od.goods_id=g.id')
  1814. // ->where([
  1815. // 'od.is_delete' => 0,
  1816. // 'od.order_id' => $order_id,
  1817. // 'g.type' => 6
  1818. // ])->select('od.*,g.name')->asArray()->all();
  1819. // return $order_detail_list;
  1820. // }
  1821. }