MReceipt.Class.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. <?php
  2. namespace JinDouYun\Model\System;
  3. use JinDouYun\Controller\Common\CloudPrint;
  4. use JinDouYun\Model\Order\MOrder;
  5. use Mall\Framework\Core\ErrorCode;
  6. use Mall\Framework\Core\ResultWrapper;
  7. use Mall\Framework\Core\StatusCode;
  8. /**
  9. * 单据打印小票
  10. * Class MReceiptService
  11. * @package JinDouYun\Model\System
  12. */
  13. class MReceipt
  14. {
  15. /**
  16. * @var integer 企业id
  17. */
  18. private $onlineEnterpriseId;
  19. /**
  20. * @var integer 用户id
  21. */
  22. private $onlineUserId;
  23. /**
  24. * @var CloudPrint 小票云打印
  25. */
  26. private $objCloudPrint;
  27. /**
  28. * MReceiptService constructor.
  29. * @param $onlineEnterpriseId
  30. * @param $onlineUserId
  31. */
  32. public function __construct($onlineEnterpriseId,$onlineUserId)
  33. {
  34. $this->onlineEnterpriseId = $onlineEnterpriseId;
  35. $this->onlineUserId = $onlineUserId;
  36. $this->objCloudPrint = new CloudPrint();
  37. }
  38. /**
  39. * @param int $id
  40. * @param int $type
  41. * @return ResultWrapper
  42. * @throws \Exception
  43. */
  44. public function toPrint(int $id,int $type)
  45. {
  46. //1.单据信息
  47. $objectInfo = self::objectInfo($id,$type);
  48. if (!$objectInfo->isSuccess()){
  49. return ResultWrapper::fail($objectInfo->getData(),$objectInfo->getErrorCode());
  50. }
  51. $objectInfo = $objectInfo->getData();
  52. //2.转化格式封装数据
  53. $pattern = (new MReceiptAdapter)->arrayToPattern($objectInfo,$type);
  54. //3.调用打印服务
  55. $result = $this->objCloudPrint->sendMsg($pattern,$this->onlineEnterpriseId);
  56. if (!$result->isSuccess()){
  57. file_put_contents('/www/wwwroot/logs/api.junhailan.com/MReceipt_error.log', date('Y-m-d H:i:s') . '错误原因: ' . $result->getData(). PHP_EOL, FILE_APPEND);
  58. return ResultWrapper::fail('打印失败,'.$result->getData(),ErrorCode::$paramError);
  59. }
  60. return ResultWrapper::success("操作成功");
  61. }
  62. /**
  63. * 获取打印单据的信息
  64. * @param int $id
  65. * @param int $type
  66. * @return ResultWrapper
  67. * @throws \Exception
  68. */
  69. public function objectInfo(int $id,int $type)
  70. {
  71. $dbResult = false;
  72. switch ($type){
  73. case StatusCode::$orderType['salesSlip']:
  74. //销售单
  75. $objMOrder = new MOrder($this->onlineUserId,$this->onlineEnterpriseId);
  76. $dbResult = $objMOrder->getOrderInfoById($id);
  77. break;
  78. case StatusCode::$orderType['saleOrder']:
  79. //订单单
  80. $objMOrder = new MOrder($this->onlineUserId,$this->onlineEnterpriseId);
  81. $dbResult = $objMOrder->getOrderInfoById($id);
  82. break;
  83. default:
  84. return ResultWrapper::fail("尚未完善。。。",ErrorCode::$paramError);
  85. break;
  86. }
  87. if (!$dbResult->isSuccess()){
  88. return ResultWrapper::fail($dbResult->getData(),$dbResult->getErrorCode());
  89. }
  90. return ResultWrapper::success($dbResult->getData());
  91. }
  92. }