123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- <?php
- namespace JinDouYun\Model\System;
- use JinDouYun\Controller\Common\Logger;
- use JinDouYun\Dao\System\DAdminSetting;
- use JinDouYun\Dao\System\DReceiptTemplate;
- use JinDouYun\Model\MBaseModel;
- use Mall\Framework\Core\ErrorCode;
- use Mall\Framework\Core\ResultWrapper;
- use Mall\Framework\Core\StatusCode;
- /**
- * Description:
- * Class MReceiptTemplate
- * @package JinDouYun\Model\System
- */
- class MReceiptTemplate extends MBaseModel
- {
- private $onlineEnterpriseId;
- private $onlineUserId;
- private $objDReceiptTemplate;
- private $objDAdminSetting;
- /**
- * MReceiptTemplate constructor.
- * @param $onlineEnterpriseId
- * @param $onlineUserId
- */
- public function __construct($onlineEnterpriseId, $onlineUserId)
- {
- parent::__construct($onlineEnterpriseId, $onlineUserId);
- $this->onlineEnterpriseId = $onlineEnterpriseId;
- $this->onlineUserId = $onlineUserId;
- $this->objDReceiptTemplate = new DReceiptTemplate();
- $this->objDAdminSetting = new DAdminSetting();
- self::init();
- }
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2020/8/26
- * Time: 9:51 上午
- */
- public function init()
- {
- $adminLists = $this->objDAdminSetting->select(
- [
- 'type' => StatusCode::$adminSettingType['printTemplate'],'signId' => [StatusCode::$orderType['saleOrder'],
- StatusCode::$orderType['saleOut'],StatusCode::$orderType['saleReturn'],StatusCode::$orderType['purchaseIn']]
- ],
- 'content,title,signId'
- );
- if ($adminLists === false){
- Logger::logs(E_USER_ERROR,'sql',__CLASS__,__LINE__,$this->objDAdminSetting->error());
- return ResultWrapper::fail($this->objDAdminSetting->error(),ErrorCode::$dberror);
- }
- if (empty($adminLists)){
- return ResultWrapper::success('没有数据需要操作');
- }
- $receiptTemplateLists = $this->objDReceiptTemplate->select(['enterpriseId' => $this->onlineEnterpriseId],'id,receiptType');
- if ($receiptTemplateLists === false){
- Logger::logs(E_USER_ERROR,'sql',__CLASS__,__LINE__,$this->objDReceiptTemplate->error());
- return ResultWrapper::fail($this->objDReceiptTemplate->error(),ErrorCode::$dberror);
- }
- $insert = [];
- if (empty($receiptTemplateLists)){
- foreach ($adminLists as $value){
- $insert[] = [
- 'enterpriseId' => $this->onlineEnterpriseId,
- 'title' => $value['title'],
- 'fieldsData' => $value['content'],
- 'receiptType' => $value['signId'],
- 'createTime' => time(),
- 'operatorName' => '系统'
- ];
- }
- unset($value);
- }else{
- $map = [];
- foreach ($receiptTemplateLists as $value){
- $map[$value['receiptType']] = $value;
- }
- unset($value);
- foreach ($adminLists as $value){
- if (!isset($map[$value['signId']])){
- $insert[] = [
- 'enterpriseId' => $this->onlineEnterpriseId,
- 'title' => $value['title'],
- 'fieldsData' => $value['content'],
- 'receiptType' => $value['signId'],
- 'createTime' => time(),
- 'operatorName' => '系统'
- ];
- }
- }
- unset($value);
- }
- if (empty($insert)){
- return ResultWrapper::success('没有数据需要操作');
- }
- $result = $this->objDReceiptTemplate->insert($insert,true);
- if ($result === false){
- Logger::logs(E_USER_ERROR,'sql',__CLASS__,__LINE__,$this->objDReceiptTemplate->error());
- return ResultWrapper::fail($this->objDReceiptTemplate->error(),ErrorCode::$dberror);
- }
- return ResultWrapper::success(true);
- }
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2020/8/26
- * Time: 9:51 上午
- */
- public function getAll()
- {
- $list = $this->objDReceiptTemplate->select(['enterpriseId'=>$this->onlineEnterpriseId],'*');
- if ($list === false){
- return ResultWrapper::fail($this->objDReceiptTemplate->error(),ErrorCode::$dberror);
- }
- foreach ($list as &$value){
- $value['printTypeMsg'] = '网页打印';
- $value['fieldsData'] = empty($value['fieldsData']) ? $value['fieldsData'] : json_decode($value['fieldsData'],true);
- }
- return ResultWrapper::success($list);
- }
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2020/8/26
- * Time: 10:39 上午
- * @param array $params
- * @param int $id
- * @return ResultWrapper
- */
- public function save(array $params,int $id)
- {
- $updateResult = $this->objDReceiptTemplate->update($params,['id'=>$id]);
- if ($updateResult === false){
- return ResultWrapper::fail($this->objDReceiptTemplate->error(),ErrorCode::$dberror);
- }
- return ResultWrapper::success('操作成功');
- }
- /**
- * Doc: (des="")
- * User: XMing
- * Date: 2020/8/26
- * Time: 11:04 上午
- * @param int $id
- * @return ResultWrapper
- */
- public function getInfo(int $id)
- {
- $result = $this->objDReceiptTemplate->get(['id' => $id,'enterpriseId' => $this->onlineEnterpriseId]);
- if ($result === false){
- return ResultWrapper::fail($this->objDReceiptTemplate->error(),ErrorCode::$dberror);
- }
- $result['describe'] = htmlspecialchars_decode($result['describe'],ENT_QUOTES);;
- if (empty($result['fieldsData'])){
- $result['fieldsData'] = [];
- }else{
- $params = json_decode($result['fieldsData'],true);
- $formatResult = self::formatSort($params,$result['receiptType']);
- if (!$formatResult->isSuccess()){
- return ResultWrapper::fail($formatResult->getData(),$formatResult->getErrorCode());
- }
- $result['fieldsData'] = $formatResult->getData();
- }
- return ResultWrapper::success($result);
- }
- private function formatSort(array $params,$type)
- {
- $orderMapSort = [
- 'basic'=>[
- 'enterprise' => 0,
- 'operatorName' => 1,
- 'orderNo' => 2,
- 'createTime' => 3,
- 'customerName' => 4,
- 'customerMobile' => 5,
- 'deliveryType' => 6,
- 'receivedName' => 7,
- 'receivedMobile' => 8,
- 'customerAddress'=> 9,
- 'remark' => 10,
- 'printing'=> 11 //是否选择打印标
- ],
- 'goods' => [
- 'goodsId'=> 0,
- 'images'=> 1,
- 'goodsName' => 2,
- 'unitName' => 3,
- 'specName' => 4,
- 'skuValue' => 5,
- 'buyNum' => 6,
- 'unitPrice' => 7,
- 'totalPrice' => 8,
- 'barCode' => 9,
- 'reservoirArea'=> 10,
- 'location'=>11
- ],
- 'fiance' => [
- 'accountName'=>0,
- 'receivable' => 1,
- 'orderPreferential' => 2,
- 'netReceipts' => 3,
- 'currentShortage'=> 4,
- 'upOwe' => 5,
- 'totalDeficit' => 6
- ],
- 'other'=>[
- 'shopName'=>0,
- 'shopMobile'=>1,
- 'shopAddress'=>2,
- 'shopDescribe'=>3,
- 'signature'=>4,
- 'printingNum'=>5
- ],
- ];
- $outMapSort = [
- 'basic' => [
- 'enterprise' => 0,
- 'operatorName' => 1,
- 'orderNo' => 2,
- 'createTime' => 3,
- 'customerName' => 4,
- 'customerMobile' => 5,
- 'deliveryType' => 6,
- 'receivedName' => 7,
- 'receivedMobile' => 8,
- 'customerAddress'=> 9,
- 'remark' => 10
- ],
- 'goods' => [
- 'goodsId' => 0,
- 'images'=> 1,
- 'goodsName' => 2,
- 'unitName' => 3,
- 'specName' => 4,
- 'skuValue' => 5,
- 'buyNum' => 6,
- 'outNum' => 7,
- 'unitPrice' => 8,
- 'originTotalPrice' => 9,
- 'totalPrice' => 10,
- 'strageCode' => 11,
- 'barCode' => 12,
- 'reservoirArea'=> 13,
- 'location'=>14
- ],
- 'other'=>[
- 'shopName'=>0,
- 'shopMobile'=>1,
- 'shopAddress'=>2,
- 'shopDescribe'=>3,
- 'signature'=>4,
- 'printingNum'=>5
- ],
- 'fiance' => [
- ],
- ];
- $data = $params['templateInfo'];
- switch ($type){
- case StatusCode::$orderType['saleOrder']:
- $allMap = [];
- foreach ($data as $key => $row){
- if (!isset($orderMapSort[$key])){
- return ResultWrapper::fail('格式化打印模版错误',ErrorCode::$paramError);
- }
- $sortMap = $orderMapSort[$key];
- $nowMap = [];
- foreach ($row as $item){
- $alias = $item['alias'];
- $nowMap[$sortMap[$alias]] = $item;
- }
- ksort($nowMap);
- $allMap[$key] = array_values($nowMap);
- }
- $params['templateInfo'] = $allMap;
- break;
- case StatusCode::$orderType['saleOut']:
- $allMap = [];
- foreach ($data as $key => $row){
- if (!isset($outMapSort[$key])){
- return ResultWrapper::fail('格式化打印模版错误',ErrorCode::$paramError);
- }
- $sortMap = $outMapSort[$key];
- $nowMap = [];
- foreach ($row as $item){
- $alias = $item['alias'];
- $nowMap[$sortMap[$alias]] = $item;
- }
- ksort($nowMap);
- $allMap[$key] = array_values($nowMap);
- }
- $params['templateInfo'] = $allMap;
- break;
- }
- return ResultWrapper::success($params);
- }
- /**
- * Doc: (des="获取对应类型模版")
- * User: XMing
- * Date: 2020/8/29
- * Time: 4:51 下午
- * @param int $id
- * @return ResultWrapper
- */
- public function getTemplateInfo(int $id)
- {
- $result = $this->objDReceiptTemplate->get(['receiptType'=>$id,'enterpriseId'=>$this->onlineEnterpriseId]);
- if ($result === false){
- return ResultWrapper::fail($this->objDReceiptTemplate->error(),ErrorCode::$dberror);
- }
- $result['describe'] = empty($result['describe'])?"": htmlspecialchars_decode($result['describe'],ENT_QUOTES);
-
- if (empty($result['fieldsData'])){
- $result['fieldsData'] = [];
- }else{
- $result['fieldsData'] = json_decode($result['fieldsData'],true);
- }
- return ResultWrapper::success($result);
- }
- }
|