MReceiptTemplate.Class.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. <?php
  2. namespace JinDouYun\Model\System;
  3. use JinDouYun\Controller\Common\Logger;
  4. use JinDouYun\Dao\System\DAdminSetting;
  5. use JinDouYun\Dao\System\DReceiptTemplate;
  6. use JinDouYun\Model\MBaseModel;
  7. use Mall\Framework\Core\ErrorCode;
  8. use Mall\Framework\Core\ResultWrapper;
  9. use Mall\Framework\Core\StatusCode;
  10. /**
  11. * Description:
  12. * Class MReceiptTemplate
  13. * @package JinDouYun\Model\System
  14. */
  15. class MReceiptTemplate extends MBaseModel
  16. {
  17. private $onlineEnterpriseId;
  18. private $onlineUserId;
  19. private $objDReceiptTemplate;
  20. private $objDAdminSetting;
  21. /**
  22. * MReceiptTemplate constructor.
  23. * @param $onlineEnterpriseId
  24. * @param $onlineUserId
  25. */
  26. public function __construct($onlineEnterpriseId, $onlineUserId)
  27. {
  28. parent::__construct($onlineEnterpriseId, $onlineUserId);
  29. $this->onlineEnterpriseId = $onlineEnterpriseId;
  30. $this->onlineUserId = $onlineUserId;
  31. $this->objDReceiptTemplate = new DReceiptTemplate();
  32. $this->objDAdminSetting = new DAdminSetting();
  33. self::init();
  34. }
  35. /**
  36. * Doc: (des="")
  37. * User: XMing
  38. * Date: 2020/8/26
  39. * Time: 9:51 上午
  40. */
  41. public function init()
  42. {
  43. $adminLists = $this->objDAdminSetting->select(
  44. [
  45. 'type' => StatusCode::$adminSettingType['printTemplate'],'signId' => [StatusCode::$orderType['saleOrder'],
  46. StatusCode::$orderType['saleOut'],StatusCode::$orderType['saleReturn'],StatusCode::$orderType['purchaseIn']]
  47. ],
  48. 'content,title,signId'
  49. );
  50. if ($adminLists === false){
  51. Logger::logs(E_USER_ERROR,'sql',__CLASS__,__LINE__,$this->objDAdminSetting->error());
  52. return ResultWrapper::fail($this->objDAdminSetting->error(),ErrorCode::$dberror);
  53. }
  54. if (empty($adminLists)){
  55. return ResultWrapper::success('没有数据需要操作');
  56. }
  57. $receiptTemplateLists = $this->objDReceiptTemplate->select(['enterpriseId' => $this->onlineEnterpriseId],'id,receiptType');
  58. if ($receiptTemplateLists === false){
  59. Logger::logs(E_USER_ERROR,'sql',__CLASS__,__LINE__,$this->objDReceiptTemplate->error());
  60. return ResultWrapper::fail($this->objDReceiptTemplate->error(),ErrorCode::$dberror);
  61. }
  62. $insert = [];
  63. if (empty($receiptTemplateLists)){
  64. foreach ($adminLists as $value){
  65. $insert[] = [
  66. 'enterpriseId' => $this->onlineEnterpriseId,
  67. 'title' => $value['title'],
  68. 'fieldsData' => $value['content'],
  69. 'receiptType' => $value['signId'],
  70. 'createTime' => time(),
  71. 'operatorName' => '系统'
  72. ];
  73. }
  74. unset($value);
  75. }else{
  76. $map = [];
  77. foreach ($receiptTemplateLists as $value){
  78. $map[$value['receiptType']] = $value;
  79. }
  80. unset($value);
  81. foreach ($adminLists as $value){
  82. if (!isset($map[$value['signId']])){
  83. $insert[] = [
  84. 'enterpriseId' => $this->onlineEnterpriseId,
  85. 'title' => $value['title'],
  86. 'fieldsData' => $value['content'],
  87. 'receiptType' => $value['signId'],
  88. 'createTime' => time(),
  89. 'operatorName' => '系统'
  90. ];
  91. }
  92. }
  93. unset($value);
  94. }
  95. if (empty($insert)){
  96. return ResultWrapper::success('没有数据需要操作');
  97. }
  98. $result = $this->objDReceiptTemplate->insert($insert,true);
  99. if ($result === false){
  100. Logger::logs(E_USER_ERROR,'sql',__CLASS__,__LINE__,$this->objDReceiptTemplate->error());
  101. return ResultWrapper::fail($this->objDReceiptTemplate->error(),ErrorCode::$dberror);
  102. }
  103. return ResultWrapper::success(true);
  104. }
  105. /**
  106. * Doc: (des="")
  107. * User: XMing
  108. * Date: 2020/8/26
  109. * Time: 9:51 上午
  110. */
  111. public function getAll()
  112. {
  113. $list = $this->objDReceiptTemplate->select(['enterpriseId'=>$this->onlineEnterpriseId],'*');
  114. if ($list === false){
  115. return ResultWrapper::fail($this->objDReceiptTemplate->error(),ErrorCode::$dberror);
  116. }
  117. foreach ($list as &$value){
  118. $value['printTypeMsg'] = '网页打印';
  119. $value['fieldsData'] = empty($value['fieldsData']) ? $value['fieldsData'] : json_decode($value['fieldsData'],true);
  120. }
  121. return ResultWrapper::success($list);
  122. }
  123. /**
  124. * Doc: (des="")
  125. * User: XMing
  126. * Date: 2020/8/26
  127. * Time: 10:39 上午
  128. * @param array $params
  129. * @param int $id
  130. * @return ResultWrapper
  131. */
  132. public function save(array $params,int $id)
  133. {
  134. $updateResult = $this->objDReceiptTemplate->update($params,['id'=>$id]);
  135. if ($updateResult === false){
  136. return ResultWrapper::fail($this->objDReceiptTemplate->error(),ErrorCode::$dberror);
  137. }
  138. return ResultWrapper::success('操作成功');
  139. }
  140. /**
  141. * Doc: (des="")
  142. * User: XMing
  143. * Date: 2020/8/26
  144. * Time: 11:04 上午
  145. * @param int $id
  146. * @return ResultWrapper
  147. */
  148. public function getInfo(int $id)
  149. {
  150. $result = $this->objDReceiptTemplate->get(['id' => $id,'enterpriseId' => $this->onlineEnterpriseId]);
  151. if ($result === false){
  152. return ResultWrapper::fail($this->objDReceiptTemplate->error(),ErrorCode::$dberror);
  153. }
  154. $result['describe'] = htmlspecialchars_decode($result['describe'],ENT_QUOTES);;
  155. if (empty($result['fieldsData'])){
  156. $result['fieldsData'] = [];
  157. }else{
  158. $params = json_decode($result['fieldsData'],true);
  159. $formatResult = self::formatSort($params,$result['receiptType']);
  160. if (!$formatResult->isSuccess()){
  161. return ResultWrapper::fail($formatResult->getData(),$formatResult->getErrorCode());
  162. }
  163. $result['fieldsData'] = $formatResult->getData();
  164. }
  165. return ResultWrapper::success($result);
  166. }
  167. private function formatSort(array $params,$type)
  168. {
  169. $orderMapSort = [
  170. 'basic'=>[
  171. 'enterprise' => 0,
  172. 'operatorName' => 1,
  173. 'orderNo' => 2,
  174. 'createTime' => 3,
  175. 'customerName' => 4,
  176. 'customerMobile' => 5,
  177. 'deliveryType' => 6,
  178. 'receivedName' => 7,
  179. 'receivedMobile' => 8,
  180. 'customerAddress'=> 9,
  181. 'remark' => 10,
  182. 'printing'=> 11 //是否选择打印标
  183. ],
  184. 'goods' => [
  185. 'goodsId'=> 0,
  186. 'images'=> 1,
  187. 'goodsName' => 2,
  188. 'unitName' => 3,
  189. 'specName' => 4,
  190. 'skuValue' => 5,
  191. 'buyNum' => 6,
  192. 'unitPrice' => 7,
  193. 'totalPrice' => 8,
  194. 'barCode' => 9,
  195. 'reservoirArea'=> 10,
  196. 'location'=>11
  197. ],
  198. 'fiance' => [
  199. 'accountName'=>0,
  200. 'receivable' => 1,
  201. 'orderPreferential' => 2,
  202. 'netReceipts' => 3,
  203. 'currentShortage'=> 4,
  204. 'upOwe' => 5,
  205. 'totalDeficit' => 6
  206. ],
  207. 'other'=>[
  208. 'shopName'=>0,
  209. 'shopMobile'=>1,
  210. 'shopAddress'=>2,
  211. 'shopDescribe'=>3,
  212. 'signature'=>4,
  213. 'printingNum'=>5
  214. ],
  215. ];
  216. $outMapSort = [
  217. 'basic' => [
  218. 'enterprise' => 0,
  219. 'operatorName' => 1,
  220. 'orderNo' => 2,
  221. 'createTime' => 3,
  222. 'customerName' => 4,
  223. 'customerMobile' => 5,
  224. 'deliveryType' => 6,
  225. 'receivedName' => 7,
  226. 'receivedMobile' => 8,
  227. 'customerAddress'=> 9,
  228. 'remark' => 10
  229. ],
  230. 'goods' => [
  231. 'goodsId' => 0,
  232. 'images'=> 1,
  233. 'goodsName' => 2,
  234. 'unitName' => 3,
  235. 'specName' => 4,
  236. 'skuValue' => 5,
  237. 'buyNum' => 6,
  238. 'outNum' => 7,
  239. 'unitPrice' => 8,
  240. 'originTotalPrice' => 9,
  241. 'totalPrice' => 10,
  242. 'strageCode' => 11,
  243. 'barCode' => 12,
  244. 'reservoirArea'=> 13,
  245. 'location'=>14
  246. ],
  247. 'other'=>[
  248. 'shopName'=>0,
  249. 'shopMobile'=>1,
  250. 'shopAddress'=>2,
  251. 'shopDescribe'=>3,
  252. 'signature'=>4,
  253. 'printingNum'=>5
  254. ],
  255. 'fiance' => [
  256. ],
  257. ];
  258. $data = $params['templateInfo'];
  259. switch ($type){
  260. case StatusCode::$orderType['saleOrder']:
  261. $allMap = [];
  262. foreach ($data as $key => $row){
  263. if (!isset($orderMapSort[$key])){
  264. return ResultWrapper::fail('格式化打印模版错误',ErrorCode::$paramError);
  265. }
  266. $sortMap = $orderMapSort[$key];
  267. $nowMap = [];
  268. foreach ($row as $item){
  269. $alias = $item['alias'];
  270. $nowMap[$sortMap[$alias]] = $item;
  271. }
  272. ksort($nowMap);
  273. $allMap[$key] = array_values($nowMap);
  274. }
  275. $params['templateInfo'] = $allMap;
  276. break;
  277. case StatusCode::$orderType['saleOut']:
  278. $allMap = [];
  279. foreach ($data as $key => $row){
  280. if (!isset($outMapSort[$key])){
  281. return ResultWrapper::fail('格式化打印模版错误',ErrorCode::$paramError);
  282. }
  283. $sortMap = $outMapSort[$key];
  284. $nowMap = [];
  285. foreach ($row as $item){
  286. $alias = $item['alias'];
  287. $nowMap[$sortMap[$alias]] = $item;
  288. }
  289. ksort($nowMap);
  290. $allMap[$key] = array_values($nowMap);
  291. }
  292. $params['templateInfo'] = $allMap;
  293. break;
  294. }
  295. return ResultWrapper::success($params);
  296. }
  297. /**
  298. * Doc: (des="获取对应类型模版")
  299. * User: XMing
  300. * Date: 2020/8/29
  301. * Time: 4:51 下午
  302. * @param int $id
  303. * @return ResultWrapper
  304. */
  305. public function getTemplateInfo(int $id)
  306. {
  307. $result = $this->objDReceiptTemplate->get(['receiptType'=>$id,'enterpriseId'=>$this->onlineEnterpriseId]);
  308. if ($result === false){
  309. return ResultWrapper::fail($this->objDReceiptTemplate->error(),ErrorCode::$dberror);
  310. }
  311. $result['describe'] = empty($result['describe'])?"": htmlspecialchars_decode($result['describe'],ENT_QUOTES);
  312. if (empty($result['fieldsData'])){
  313. $result['fieldsData'] = [];
  314. }else{
  315. $result['fieldsData'] = json_decode($result['fieldsData'],true);
  316. }
  317. return ResultWrapper::success($result);
  318. }
  319. }