InvoiceExtendController.php 41 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. <?php
  2. /**
  3. * 开票功能控制器
  4. * @author CP
  5. * @version 1.0
  6. * @namespace invoice
  7. * @package InvoiceExtentController
  8. */
  9. class InvoiceExtendController extends DooController {
  10. public $staff;
  11. public $NEW;
  12. public $data;
  13. private $INVOICEKEY = "APPROVAL";
  14. private $INVOICECOLLECTPATH = "protected/cache/invoiceCollect/";
  15. private $ACTION = '';
  16. private $appId = 'wx3ee46531947a1384';
  17. private $appSecret = 'awGWzy3J5q_96VXKBLA2BQ9RpiT0LiPXnkMF-xV6h5o'; // awGWzy3J5q_96VXKBLA2BXU6mVCGynPGD-sxUCsdNDE //'PeiQVH8tbQE7pM9pVO4AIPlX9FFNpwu7O6Iy6VFcAVtpjTA3Qn42dbNmktBFkUHo';
  18. public function beforeRun($resource, $action) {
  19. Doo::loadModel ( "invoiceManage" );
  20. $invoiceManage = new invoiceManage ();
  21. Doo::loadClass ( 'XDeode' );
  22. $XDeode = new XDeode ( 5 );
  23. $sid = $XDeode->decode ( $_COOKIE ["staff"] );
  24. header ( 'Content-Type:text/html;charset=utf-8' );
  25. $moldList = $invoiceManage->getInvoiceByStaff ( $sid );
  26. if (empty ( $moldList )) {
  27. $flag = false;
  28. $uGroups = 'ordinary';
  29. if (Doo::acl ()->isAllowed ( $uGroups, $resource, $action )) {
  30. $flag = true;
  31. }
  32. } else {
  33. $flag = false;
  34. array_push ( $moldList, array (
  35. 'mold' => 'ordinary'
  36. ) );
  37. // 删除审批人后-加入权限问题-暂时关闭
  38. $fileInvoice = file_get_contents ( "protected/config/invoice/invoice.ini" );
  39. $fiML = json_decode ( $fileInvoice, true );
  40. if (empty ( $fiML ))
  41. $fiML = array ();
  42. foreach ( $fiML as $key => $value ) {
  43. $sidList = explode ( ',', $value );
  44. foreach ( $sidList as $k => $v ) {
  45. if ($v == $sid) {
  46. array_push ( $moldList, array (
  47. 'mold' => $key
  48. ) );
  49. break;
  50. }
  51. }
  52. }
  53. foreach ( $moldList as $value ) {
  54. if (Doo::acl ()->isAllowed ( $value ['mold'], $resource, $action )) {
  55. $flag = true;
  56. break;
  57. }
  58. }
  59. }
  60. /*
  61. * Doo::acl()->isAllowed($uGroups['groups'], $resource, $action);
  62. */
  63. // 单独判断办事处汇总的访问权限 invoiceAggregateCategoryDetail,invoiceAggregateStaffDetail
  64. if ($action == 'invoiceAggregateCategoryDetail' || $action == 'invoiceAggregateStaffDetail') {
  65. Doo::loadModel ( "invoiceCategoryManage" );
  66. $invoiceCategoryManage = new invoiceCategoryManage ();
  67. $icm = $invoiceCategoryManage->getInvoiceCMByStaff ( $sid );
  68. if (empty ( $icm ))
  69. die ( 'illegal request' );
  70. }
  71. // 单独判断应收款访问权限
  72. if ($action == 'invoiceAggregateCompanyDetail') {
  73. Doo::loadModel ( "invoiceCompanyManage" );
  74. $invoiceCompanyManage = new invoiceCompanyManage ();
  75. $icm = $invoiceCompanyManage->getInvoiceCMByStaff ( $sid );
  76. if (empty ( $icm ))
  77. die ( 'illegal request' );
  78. }
  79. // 主权限获得与判断
  80. Doo::loadModel ( 'staff' );
  81. $staff = new staff ();
  82. $detail = $staff->getStaffBySid ( $_COOKIE ["staff"] );
  83. $accessModular = 'INVOICEEXTEND';
  84. $accessAuthority = 'INVOICE';
  85. if (empty ( $detail ['cldAccessArray'] ))
  86. die ( 'illegal request' );
  87. else {
  88. if (in_array ( $accessAuthority, $detail ['cldAccessArray'] )) {
  89. if (Doo::acl ()->isAllowed ( $accessModular, $resource, $action )) {
  90. $flag = true;
  91. } else {
  92. $flag = false;
  93. }
  94. } else {
  95. die ( 'illegal request' );
  96. }
  97. }
  98. if (! $flag)
  99. die ( 'illegal request' );
  100. }
  101. function __construct() {
  102. if (isset ( $_COOKIE ["staff"] )) {
  103. if (! empty ( $_COOKIE ["staff"] )) {
  104. Doo::loadModel ( 'staff' );
  105. $staff = new staff ();
  106. $uriPartsOrig = explode ( '/', $_SERVER ['REQUEST_URI'] );
  107. $this->ACTION = $uriPartsOrig [1];
  108. $this->data ['new'] = $this->isInvoiceNew ();
  109. $this->staff = $staff->getUserByIdList ( $_COOKIE ["staff"] );
  110. $this->data ['isStaffCompleteMsg'] = false;
  111. $detail = $staff->checkStaffInfoIsComplete ( $_COOKIE ["staff"] );
  112. if (! empty ( $detail ))
  113. $this->data ['isStaffCompleteMsg'] = true;
  114. return "/";
  115. }
  116. }
  117. Doo::loadCore ( 'uri/DooUriRouter' );
  118. $router = new DooUriRouter ();
  119. $routeRs = $router->execute ( Doo::app ()->route, Doo::conf ()->SUBFOLDER );
  120. if ($routeRs ['1'] != "login") {
  121. header ( 'Content-Type:text/html;charset=utf-8' );
  122. @header ( "Location: /login" );
  123. }
  124. }
  125. function invoiceSettlementChange() {
  126. $iidKey = $this->get_args ( 'iidKey' ) ? $this->get_args ( 'iidKey' ) : "";
  127. $trainId = $this->get_args ( 'trainId' ) ? $this->get_args ( 'trainId' ) : 0;
  128. $settlementType = $this->get_args ( 'settlementType' ) ? $this->get_args ( 'settlementType' ) : "";
  129. if (empty ( $this->staff [0] )) {
  130. die ( 'illegal request-Please login' );
  131. }
  132. if (! empty ( $iidKey ) && ! empty ( $settlementType )) {
  133. Doo::loadModel ( 'invoice' );
  134. $invoice = new invoice ();
  135. Doo::loadClass ( 'XDeode' );
  136. $XDeode = new XDeode ( 5 );
  137. Doo::loadModel ( 'invoiceTraining' );
  138. $invoiceTraining = new invoiceTraining ();
  139. $iid = $XDeode->decode ( $iidKey );
  140. $invoiceDetail = $invoice->getInvoiceByIid ( $iid );
  141. if (empty ( $invoiceDetail )) {
  142. die ( 'illegal request-invoiceData NULL' );
  143. }
  144. $trainId = $XDeode->decode ( $trainId );
  145. $itDetail = $invoiceTraining->getInvoiceTrainingByItid ( $trainId );
  146. $st = '';
  147. if ($settlementType == 'SOFTWARE') {
  148. $st = 0;
  149. $trainId = 0;
  150. $operation = ' 结算方式 改 软件销售';
  151. } elseif ($settlementType == 'TRAINING') {
  152. $st = 1;
  153. $operation = ' 结算方式 改 ' . $itDetail ['trainName'];
  154. if (empty ( $trainId ) && ! is_numeric ( $trainId )) {
  155. die ( 'illegal request-trainingIdError' );
  156. }
  157. } else {
  158. die ( 'illegal request-settlementError' );
  159. }
  160. $invoice->settlementType = $st;
  161. $invoice->trainId = $trainId;
  162. $invoice->update ( array (
  163. 'where' => 'iid = ' . $iid
  164. ) );
  165. Doo::loadModel ( 'invoiceStore' );
  166. $invoiceStore = new invoiceStore ();
  167. // 重置结算方式的相关数据
  168. $isSuccess = $invoiceStore->delete ( array (
  169. 'where' => 'iid=' . $iid,
  170. 'limit' => 1
  171. ) );
  172. // if($isSuccess){
  173. // $detailIT = $invoiceTraining->getInvoiceTrainingByItid ( $invoiceDetail['trainId'] );
  174. // if (!empty($detailIT)){
  175. // $item = array (
  176. // 'itid' => $invoiceDetail['trainId'],
  177. // 'invoiceTotal' => $detailIT ['invoiceTotal'] - 1,
  178. // );
  179. // $itid = $invoiceTraining->setInvoiceTrainByCondition ( $item );
  180. // }
  181. // }
  182. // 如果是培训班结算 创建培训班发票到培训班备份中
  183. if (! empty ( $st ) && $st == 1) {
  184. $isid = $invoiceStore->setInvoiceToInvoiceStore ( $iid );
  185. $itemIS = array (
  186. 'isid' => $isid,
  187. 'ipSource' => 1,
  188. 'apStatus' => 1,
  189. 'iid' => $iid
  190. );
  191. $invoiceStore->setInvoiceStoreByCondition ( $itemIS );
  192. // 更新培训班总数量
  193. $item = array (
  194. 'itid' => $trainId,
  195. 'submitStatus' => 1
  196. );
  197. $itid = $invoiceTraining->setInvoiceTrainByCondition ( $item );
  198. }
  199. Doo::loadModel ( 'invoiceOperationLog' );
  200. $invoiceOperationLog = new invoiceOperationLog ();
  201. // 操作日志
  202. $invoiceOperationLog->setInvoiceOperationLog ( $item );
  203. $item = array (
  204. 'date' => date ( "Y-m-d H:i:s" ),
  205. 'operation' => $operation,
  206. 'status' => 1,
  207. 'img' => $this->staff [0] ['avatar'],
  208. 'username' => $this->staff [0] ['username'],
  209. 'uid' => $this->staff [0] ['sid'],
  210. 'category' => $this->staff [0] ['category'],
  211. 'iid' => $iid
  212. );
  213. $invoiceOperationLog->setInvoiceOperationLog ( $item );
  214. }
  215. return "/invoiceApprovalAchieve";
  216. }
  217. function invoiceUntreadAdd() {
  218. session_start ();
  219. Doo::loadClass ( 'XDeode' );
  220. $XDeode = new XDeode ( 5 );
  221. $iidKey = isset ( $this->params ['iidKey'] ) ? $this->params ['iidKey'] : "";
  222. $iid = $XDeode->decode ( $iidKey );
  223. if (! is_numeric ( $iid ))
  224. die ( 'illegal request' );
  225. Doo::loadModel ( 'invoice' );
  226. $invoice = new invoice ();
  227. Doo::loadModel ( 'invoiceOperationLog' );
  228. $invoiceOperationLog = new invoiceOperationLog ();
  229. $invoiceDetail = $invoice->getInvoiceByIid ( $iid );
  230. $operation = $invoiceOperationLog->getInvoiceOperationByDropped ( $iid );
  231. // 获得继承发票金额合计
  232. $restrictPrice = $invoiceDetail ['invoicePrice'];
  233. if (! empty ( $invoiceDetail ['inheritIid'] )) {
  234. $restrictList = $invoice->getInvoiceInIid ( $invoiceDetail ['inheritIid'] );
  235. foreach ( $restrictList as $key => $value ) {
  236. $restrictPrice -= $value ['invoicePrice'];
  237. }
  238. }
  239. if ($restrictPrice < 0) {
  240. $restrictPrice = 0;
  241. }
  242. $invoiceDetail ['mailItemsJson'] = json_decode ( $invoiceDetail ['mailItemsJson'], true );
  243. $token = $this->set_token ();
  244. $_SESSION ['token_' . $this->staff [0] ['sidKey']] = $token;
  245. $this->data ['token'] = $token;
  246. $this->data ['restrictPrice'] = floatval ( $restrictPrice );
  247. $this->data ['operation'] = $operation;
  248. $this->data ['invoiceDetail'] = $invoiceDetail;
  249. $this->data ['memu'] = "invoice";
  250. $this->data ['staff'] = $this->staff;
  251. $this->data ['receiptMemu'] = 'invoice';
  252. $this->render ( "/invoice/invoiceUntreadAdd", $this->data );
  253. }
  254. function invoiceUntreadAddDo() {
  255. $iidKey = $this->get_args ( 'iidKey' ) ? $this->get_args ( 'iidKey' ) : "";
  256. Doo::loadModel ( 'invoice' );
  257. $invoice = new invoice ();
  258. $iid = $invoice->authcode ( $iidKey );
  259. if (! empty ( $iid ) && ! is_numeric ( $iid ))
  260. die ( 'illegal request' );
  261. $cid = $this->get_args ( 'cid' ) && is_numeric ( $this->get_args ( 'cid' ) ) ? $this->get_args ( 'cid' ) : 0;
  262. $invoiceType = $this->get_args ( 'invoiceType' ) && is_numeric ( $this->get_args ( 'invoiceType' ) ) ? $this->get_args ( 'invoiceType' ) : 0;
  263. $doPost = $this->get_args ( 'doPost' ) && is_numeric ( $this->get_args ( 'doPost' ) ) ? $this->get_args ( 'doPost' ) : 0;
  264. // $invoicePrice = $this->get_args ( 'invoicePrice' ) ? $this->get_args ( 'invoicePrice' ) : "";
  265. $invoiceQuantity = $this->get_args ( 'invoiceQuantity' ) && is_numeric ( $this->get_args ( 'invoiceQuantity' ) ) ? $this->get_args ( 'invoiceQuantity' ) : 0;
  266. $invoiceUnitPrice = $this->get_args ( 'invoiceUnitPrice' ) && is_numeric ( $this->get_args ( 'invoiceUnitPrice' ) ) ? $this->get_args ( 'invoiceUnitPrice' ) : 0;
  267. $invoiceElement = $this->get_args ( 'invoiceElement' ) ? $this->get_args ( 'invoiceElement' ) : "";
  268. $invoiceTitle = $this->get_args ( 'invoiceTitle' ) ? $this->get_args ( 'invoiceTitle' ) : "";
  269. $invoiceCompany = $this->get_args ( 'invoiceCompany' ) ? $this->get_args ( 'invoiceCompany' ) : "";
  270. $TIN = $this->get_args ( 'TIN' ) ? $this->get_args ( 'TIN' ) : "";
  271. $address = $this->get_args ( 'address' ) ? $this->get_args ( 'address' ) : "";
  272. $phone = $this->get_args ( 'phone' ) ? $this->get_args ( 'phone' ) : "";
  273. $bank = $this->get_args ( 'bank' ) ? $this->get_args ( 'bank' ) : "";
  274. $bankAccount = $this->get_args ( 'bankAccount' ) ? $this->get_args ( 'bankAccount' ) : "";
  275. $recipients = $this->get_args ( 'recipients' ) ? $this->get_args ( 'recipients' ) : "";
  276. $recipientsPhone = $this->get_args ( 'recipientsPhone' ) ? $this->get_args ( 'recipientsPhone' ) : "";
  277. $recipientsAddress = $this->get_args ( 'recipientsAddress' ) ? $this->get_args ( 'recipientsAddress' ) : "";
  278. $remark = $this->get_args ( 'remark' ) ? $this->get_args ( 'remark' ) : "";
  279. $TINT = $this->get_args ( 'TINT' ) ? $this->get_args ( 'TINT' ) : "";
  280. $addressT = $this->get_args ( 'addressT' ) ? $this->get_args ( 'addressT' ) : "";
  281. $phoneT = $this->get_args ( 'phoneT' ) ? $this->get_args ( 'phoneT' ) : "";
  282. $bankT = $this->get_args ( 'bankT' ) ? $this->get_args ( 'bankT' ) : "";
  283. $bankAccountT = $this->get_args ( 'bankAccountT' ) ? $this->get_args ( 'bankAccountT' ) : "";
  284. $softLock = $this->get_args ( 'softLock' ) ? $this->get_args ( 'softLock' ) : 0;
  285. $softLockNum = $this->get_args ( 'softLockNum' ) ? $this->get_args ( 'softLockNum' ) : 0;
  286. $contract = $this->get_args ( 'contract' ) ? $this->get_args ( 'contract' ) : 0;
  287. $contractNum = $this->get_args ( 'contractNum' ) ? $this->get_args ( 'contractNum' ) : 0;
  288. $instructions = $this->get_args ( 'instructions' ) ? $this->get_args ( 'instructions' ) : 0;
  289. $instructionsNum = $this->get_args ( 'instructionsNum' ) ? $this->get_args ( 'instructionsNum' ) : 0;
  290. $remittance = $this->get_args ( 'remittance' ) ? $this->get_args ( 'remittance' ) : 0;
  291. $mailItems = $this->get_args ( 'mailItems' ) ? $this->get_args ( 'mailItems' ) : "";
  292. $token = $this->get_args ( 'token' ) ? $this->get_args ( 'token' ) : "";
  293. if (empty ( $token )) {
  294. die ( 'illegal request-token' );
  295. } else {
  296. session_start ();
  297. if ($_SESSION ['token_' . $this->staff [0] ['sidKey']] != $token) {
  298. header ( 'Content-Type:text/html;charset=utf-8' );
  299. die ( '请勿重复申请发票' );
  300. } else {
  301. $_SESSION ['token_' . $this->staff [0] ['sidKey']] = '';
  302. }
  303. }
  304. $temporary = $invoiceQuantity * $invoiceUnitPrice;
  305. if ($invoiceType == 1) {
  306. if ($temporary > 100000) {
  307. header ( 'Content-Type:text/html;charset=utf-8' );
  308. die ( '专用发票总金额暂时不能超过100000.00(十万)元。' );
  309. // die ( 'illegal request-Please priceError' );
  310. }
  311. }
  312. if (empty ( $this->staff [0] ))
  313. die ( 'illegal request-Please login' );
  314. if (! empty ( $cid ) && ! empty ( $invoiceQuantity ) && ! empty ( $invoiceUnitPrice ) && ! empty ( $invoiceElement )) {
  315. Doo::loadModel ( 'L_category' );
  316. $lCategory = new L_category ();
  317. Doo::loadModel ( 'invoiceManage' );
  318. $invoiceManage = new invoiceManage ();
  319. Doo::loadModel ( 'invoiceOperationLog' );
  320. $invoiceOperationLog = new invoiceOperationLog ();
  321. Doo::loadClass ( 'XDeode' );
  322. $XDeode = new XDeode ( 5 );
  323. $categoryDetil = $lCategory->getOne ( array (
  324. 'SELECT' => 'title',
  325. 'where' => 'cid=' . $cid,
  326. 'asArray' => true
  327. ) );
  328. $invoicePrice = $invoiceQuantity * $invoiceUnitPrice;
  329. $invoiceManageList = $invoiceManage->getInvoiceByAll ();
  330. $invoiceManageDetail = $invoiceManage->getInvoiceByMold ( "发票审批" );
  331. // print_r(current($invoiceManageDetail ['staffList']));
  332. // die;
  333. $moldManage = array ();
  334. foreach ( $invoiceManageList as $key => $value ) {
  335. if ($value ['mold'] == '发票打印')
  336. $moldManage [$value ['mold']] = $value ['staffList'];
  337. elseif ($value ['mold'] == '发票邮寄')
  338. $moldManage [$value ['mold']] = $value ['staffList'];
  339. elseif ($value ['mold'] == '发票退票')
  340. $moldManage [$value ['mold']] = $value ['staffList'];
  341. }
  342. $pendingApprovalsSid = current ( $invoiceManageDetail ['staffList'] ) [0];
  343. // 培训班ID--新增关联-发票关系 发票IID关联
  344. $invoice = new invoice ();
  345. $invoiceDetail = $invoice->getInvoiceByIid ( $iid );
  346. // 退票后,重新开票金额的限制
  347. $restrictPrice = $invoiceDetail ['invoicePrice'];
  348. if (! empty ( $invoiceDetail ['inheritIid'] )) {
  349. $restrictList = $invoice->getInvoiceInIid ( $invoiceDetail ['inheritIid'] );
  350. foreach ( $restrictList as $key => $value ) {
  351. $restrictPrice -= $value ['invoicePrice'];
  352. }
  353. }
  354. if ($restrictPrice < 0) {
  355. header ( 'Content-Type:text/html;charset=utf-8' );
  356. die ( '开票金额超出原退票金额-0' );
  357. } else {
  358. if ($restrictPrice < $temporary) {
  359. header ( 'Content-Type:text/html;charset=utf-8' );
  360. die ( '开票金额超出原退票金额-1' );
  361. }
  362. }
  363. $trainId = $invoiceDetail ['trainId'];
  364. $st = 1;
  365. if (empty ( $trainId ) && ! is_numeric ( $trainId )) {
  366. die ( 'illegal request-trainingIdError' );
  367. }
  368. $invoice = new invoice ();
  369. $item = array (
  370. 'cid' => $cid,
  371. 'categoryName' => $categoryDetil ['title'],
  372. 'invoiceElement' => $invoiceElement,
  373. 'invoiceQuantity' => $invoiceQuantity,
  374. 'invoiceUnitPrice' => $invoiceUnitPrice,
  375. 'invoicePrice' => $invoicePrice,
  376. 'invoiceType' => $invoiceType,
  377. 'settlementType' => $st,
  378. 'doPost' => $doPost,
  379. 'status' => 1,
  380. 'invoiceSerial' => "#F" . date ( 'Ymd' ) . str_pad ( mt_rand ( 1, 99999 ), 5, '0', STR_PAD_LEFT ),
  381. 'date' => date ( "Y-m-d H:i:s" ),
  382. 'updateTime' => date ( "Y-m-d H:i:s" ),
  383. 'sid' => $this->staff [0] ['sid'],
  384. 'userName' => $this->staff [0] ['username'],
  385. 'invoiceManage' => $invoiceManageDetail ['staff'],
  386. 'pendingApprovals' => $pendingApprovalsSid,
  387. 'moldManage' => json_encode ( $moldManage ),
  388. 'remark' => $remark,
  389. 'trainId' => $trainId,
  390. 'parentUntreadIid' => $trainId
  391. );
  392. if ($invoiceType == 0)
  393. $item += array (
  394. 'invoiceTitle' => $invoiceTitle,
  395. 'TIN' => $TINT,
  396. 'address' => $addressT,
  397. 'phone' => $phoneT,
  398. 'bank' => $bankT,
  399. 'bankAccount' => $bankAccountT
  400. );
  401. else {
  402. $item += array (
  403. 'invoiceCompany' => $invoiceCompany,
  404. 'TIN' => $TIN,
  405. 'address' => $address,
  406. 'phone' => $phone,
  407. 'bank' => $bank,
  408. 'bankAccount' => $bankAccount
  409. );
  410. }
  411. if ($doPost == 1) {
  412. $mailList = array (
  413. 'invoice' => 1,
  414. 'mailItems' => $mailItems
  415. );
  416. if (! empty ( $softLock ))
  417. $mailList += array (
  418. 'softLock' => $softLockNum
  419. );
  420. else
  421. $mailList += array (
  422. 'softLock' => 0
  423. );
  424. if (! empty ( $contract ))
  425. $mailList += array (
  426. 'contract' => $contractNum
  427. );
  428. else
  429. $mailList += array (
  430. 'contract' => 0
  431. );
  432. if (! empty ( $instructions ))
  433. $mailList += array (
  434. 'instructions' => $instructionsNum
  435. );
  436. else
  437. $mailList += array (
  438. 'instructions' => 0
  439. );
  440. if (! empty ( $remittance ))
  441. $mailList += array (
  442. 'remittance' => $remittance
  443. );
  444. else
  445. $mailList += array (
  446. 'remittance' => ''
  447. );
  448. $mailString = '发票x1张,备注:' . $mailItems;
  449. if (! empty ( $softLock ))
  450. $mailString .= ',软件锁x' . $softLockNum . '个';
  451. if (! empty ( $contract ))
  452. $mailString .= ',合同x' . $contractNum . '份';
  453. if (! empty ( $instructions ))
  454. $mailString .= ',说明书x' . $instructionsNum . '本';
  455. if (! empty ( $remittance ))
  456. $mailString .= ',汇款账号单(张)';
  457. $item += array (
  458. 'recipients' => $recipients,
  459. 'recipientsPhone' => $recipientsPhone,
  460. 'recipientsAddress' => $recipientsAddress,
  461. 'mailItems' => $mailString,
  462. 'mailItemsJson' => json_encode ( $mailList )
  463. );
  464. }
  465. $iid = $invoice->addInvoice ( $item );
  466. $invoice = new invoice ();
  467. if (! empty ( $invoiceDetail ['inheritIid'] )) {
  468. $inheritIid = $invoiceDetail ['inheritIid'] . ',' . $iid;
  469. } else {
  470. $inheritIid = $iid;
  471. }
  472. $item = array (
  473. 'iid' => $invoiceDetail ['iid'],
  474. 'inheritIid' => $inheritIid
  475. );
  476. $invoice->setInvoiceByCondition ( $item );
  477. // 如果是培训班结算 创建培训班发票到培训班备份中
  478. if (! empty ( $st ) && $st == 1) {
  479. Doo::loadModel ( 'invoiceStore' );
  480. $invoiceStore = new invoiceStore ();
  481. Doo::loadModel ( 'invoiceTraining' );
  482. $invoiceTraining = new invoiceTraining ();
  483. $isid = $invoiceStore->setInvoiceToInvoiceStore ( $iid );
  484. $itemIS = array (
  485. 'isid' => $isid,
  486. 'ipSource' => 1,
  487. 'apStatus' => 1,
  488. 'iid' => $iid
  489. );
  490. $invoiceStore->setInvoiceStoreByCondition ( $itemIS );
  491. // 更新培训班总数量
  492. $detailIT = $invoiceTraining->getInvoiceTrainingByItid ( $trainId );
  493. $item = array (
  494. 'itid' => $trainId,
  495. 'invoiceTotal' => $detailIT ['invoiceTotal'] + 1,
  496. 'submitStatus' => 1
  497. );
  498. $itid = $invoiceTraining->setInvoiceTrainByCondition ( $item );
  499. }
  500. // 操作日志
  501. $item = array (
  502. 'date' => date ( "Y-m-d H:i:s" ),
  503. 'operation' => "创建",
  504. 'status' => 1,
  505. 'img' => $this->staff [0] ['avatar'],
  506. 'username' => $this->staff [0] ['username'],
  507. 'uid' => $this->staff [0] ['sid'],
  508. 'category' => $this->staff [0] ['category'],
  509. 'iid' => $iid
  510. );
  511. $invoiceOperationLog->setInvoiceOperationLog ( $item );
  512. $item = array (
  513. 'date' => date ( "Y-m-d H:i:s" ),
  514. 'operation' => "提交审批",
  515. 'status' => 1,
  516. 'img' => $this->staff [0] ['avatar'],
  517. 'username' => $this->staff [0] ['username'],
  518. 'uid' => $this->staff [0] ['sid'],
  519. 'category' => $this->staff [0] ['category'],
  520. 'iid' => $iid
  521. );
  522. $invoiceOperationLog->setInvoiceOperationLog ( $item );
  523. }
  524. return "/invoiceUntreadED";
  525. }
  526. //发票作废
  527. function invoiceInvalid() {
  528. $invalidId = $this->get_args ( 'invalidId' ) ? $this->get_args ( 'invalidId' ) : "";
  529. $date = $this->get_args ( 'date' ) ? $this->get_args ( 'date' ) : "MONTHO";
  530. Doo::loadModel ( 'invoice' );
  531. $invoice = new invoice ();
  532. Doo::loadClass ( 'XDeode' );
  533. $XDeode = new XDeode ( 5 );
  534. Doo::loadModel ( 'invoiceStatistics' );
  535. $iid = $XDeode->decode ( $invalidId );
  536. if (! is_numeric ( $iid )){
  537. die ( 'illegal request-id' );
  538. }
  539. $invoiceDetail = $invoice->getInvoiceByIid ( $iid );
  540. if (empty ( $invoiceDetail )){
  541. die ( 'illegal request-emptyData' );
  542. }
  543. if ( $invoiceDetail['untreadStatus']==2&&$invoiceDetail['status']==2&&$invoiceDetail['invalid']==0 ){
  544. $invoice = new invoice ();
  545. $invoice->iid = $iid;
  546. $invoice->invalid = 1;
  547. $invoice->update ();
  548. $invoiceStatistics = new invoiceStatistics ();
  549. $item = array (
  550. 'date' => $invoiceDetail['date'] ,
  551. 'priceClass' => 1,
  552. 'cid' => $invoiceDetail ['cid'],
  553. 'staff' => $invoiceDetail ['sid'],
  554. 'invoicePrice' => -$invoiceDetail ['invoicePrice']
  555. );
  556. $invoiceStatistics->setInvoiceStatisticsByCondition ( $item );
  557. return "/invoiceUntreadAchieve/1/".$date;
  558. }else{
  559. die ( 'illegal request-status' );
  560. }
  561. }
  562. //发票撤销作废
  563. function invoiceUndoInvalid() {
  564. $invalidId = $this->get_args ( 'invalidId' ) ? $this->get_args ( 'invalidId' ) : "";
  565. $date = $this->get_args ( 'date' ) ? $this->get_args ( 'date' ) : "MONTHO";
  566. Doo::loadModel ( 'invoice' );
  567. $invoice = new invoice ();
  568. Doo::loadClass ( 'XDeode' );
  569. $XDeode = new XDeode ( 5 );
  570. Doo::loadModel ( 'invoiceStatistics' );
  571. $iid = $XDeode->decode ( $invalidId );
  572. if (! is_numeric ( $iid )){
  573. die ( 'illegal request-id' );
  574. }
  575. $invoiceDetail = $invoice->getInvoiceByIid ( $iid );
  576. if (empty ( $invoiceDetail )){
  577. die ( 'illegal request-emptyData' );
  578. }
  579. if ( $invoiceDetail['untreadStatus']==2&&$invoiceDetail['status']==2&&$invoiceDetail['isDelete']==0&&$invoiceDetail['invalid']==1 ){
  580. $invoice = new invoice ();
  581. $invoice->iid = $iid;
  582. $invoice->invalid = 0;
  583. $invoice->update ();
  584. $invoiceStatistics = new invoiceStatistics ();
  585. $item = array (
  586. 'date' => $invoiceDetail['date'] ,
  587. 'priceClass' => 1,
  588. 'cid' => $invoiceDetail ['cid'],
  589. 'staff' => $invoiceDetail ['sid'],
  590. 'invoicePrice' => $invoiceDetail ['invoicePrice']
  591. );
  592. $invoiceStatistics->setInvoiceStatisticsByCondition ( $item );
  593. return "/invoiceUntreadAchieve/1/".$date;
  594. }else{
  595. die ( 'illegal request-status-发票可能已删除或未退票' );
  596. }
  597. }
  598. //发票坏账
  599. function invoiceBadDebt(){
  600. $invalidId = $this->get_args ( 'iidKey' ) ? $this->get_args ( 'iidKey' ) : "";
  601. $status = $this->get_args ( 'status' ) ? $this->get_args ( 'status' ) : "";
  602. Doo::loadModel ( 'invoice' );
  603. $invoice = new invoice ();
  604. Doo::loadClass ( 'XDeode' );
  605. $XDeode = new XDeode ( 5 );
  606. $iid = $XDeode->decode ( $invalidId );
  607. if (! is_numeric ( $iid )){
  608. die ( 'illegal request-id' );
  609. }
  610. $invoiceDetail = $invoice->getInvoiceByIid ( $iid );
  611. if (empty ( $invoiceDetail )){
  612. die ( 'illegal request-emptyData' );
  613. }
  614. if ($invoiceDetail['untreadStatus']==0&&$invoiceDetail['status']==2&&$invoiceDetail['isDelete']==0&&$invoiceDetail['badDebt']==0 ){
  615. $invoice = new invoice ();
  616. $invoice->iid = $iid;
  617. $invoice->badDebt = 1;
  618. $invoice->update ();
  619. // Doo::loadModel ( 'invoiceStatistics' );
  620. // $invoiceStatistics = new invoiceStatistics ();
  621. // $item = array (
  622. // 'date' => $invoiceDetail['date'] ,
  623. // 'priceClass' => 1,
  624. // 'cid' => $invoiceDetail ['cid'],
  625. // 'staff' => $invoiceDetail ['sid'],
  626. // 'invoicePrice' => $invoiceDetail ['invoicePrice']
  627. // );
  628. // $invoiceStatistics->setInvoiceStatisticsByCondition ( $item );
  629. if(empty($status)){
  630. return "/invoiceAS";
  631. }else{
  632. return "/invoiceAS/1/".$status;
  633. }
  634. }else{
  635. die ( 'illegal request-status' );
  636. }
  637. }
  638. /**
  639. * 旧输入导入
  640. */
  641. function _GetFileEXT($filename) {
  642. $pics = explode ( '.', $filename );
  643. $num = count ( $pics );
  644. return $pics [$num - 1];
  645. }
  646. /**
  647. * 生成token
  648. * @return string
  649. */
  650. function set_token() {
  651. $str = md5 ( uniqid ( md5 ( microtime ( true ) ), true ) ); // 生成一个不会重复的字符串
  652. $str = sha1 ( $str ); // 加密
  653. return $str;
  654. }
  655. /**
  656. * 获取get或者POST值
  657. *
  658. * @param string $name 属性名称
  659. * @return fixed 值
  660. */
  661. function get_args($name) {
  662. if (isset ( $_GET [$name] )) {
  663. if (is_array ( $_GET [$name] ))
  664. return $_GET [$name];
  665. else
  666. return addslashes ( $_GET [$name] );
  667. } elseif (isset ( $_POST [$name] )) {
  668. if (is_array ( $_POST [$name] ))
  669. return $_POST [$name];
  670. else
  671. return addslashes ( $_POST [$name] );
  672. } else
  673. return false;
  674. }
  675. function SafeFilter(&$arr) {
  676. $ra = Array (
  677. '/([\x00-\x08,\x0b-\x0c,\x0e-\x19])/',
  678. '/script/',
  679. '/javascript/',
  680. '/vbscript/',
  681. '/expression/',
  682. '/applet/',
  683. '/meta/',
  684. '/xml/',
  685. '/blink/',
  686. '/link/',
  687. '/style/',
  688. '/embed/',
  689. '/object/',
  690. '/frame/',
  691. '/layer/',
  692. '/title/',
  693. '/bgsound/',
  694. '/base/',
  695. '/onload/',
  696. '/onunload/',
  697. '/onchange/',
  698. '/onsubmit/',
  699. '/onreset/',
  700. '/onselect/',
  701. '/onblur/',
  702. '/onfocus/',
  703. '/onabort/',
  704. '/onkeydown/',
  705. '/onkeypress/',
  706. '/onkeyup/',
  707. '/onclick/',
  708. '/ondblclick/',
  709. '/onmousedown/',
  710. '/onmousemove/',
  711. '/onmouseout/',
  712. '/onmouseover/',
  713. '/onmouseup/',
  714. '/onunload/'
  715. );
  716. if (is_array ( $arr )) {
  717. foreach ( $arr as $key => $value ) {
  718. if (! is_array ( $value )) {
  719. if (! get_magic_quotes_gpc ()) // 不对magic_quotes_gpc转义过的字符使用addslashes(),避免双重转义。
  720. {
  721. $value = addslashes ( $value ); // 给单引号(')、双引号(")、反斜线(\)与 NUL(NULL 字符)加上反斜线转义
  722. }
  723. $value = preg_replace ( $ra, '', $value ); // 删除非打印字符,粗暴式过滤xss可疑字符串
  724. $arr [$key] = htmlentities ( strip_tags ( $value ) ); // 去除 HTML 和 PHP 标记并转换为 HTML 实体
  725. } else {
  726. SafeFilter ( $arr [$key] );
  727. }
  728. }
  729. }
  730. }
  731. /**
  732. * Escape String
  733. *
  734. * @access public
  735. * @param string
  736. * @param bool whether or not the string will be used in a LIKE condition
  737. * @return string
  738. */
  739. function escape_str($str, $like = FALSE) {
  740. if (is_array ( $str )) {
  741. foreach ( $str as $key => $val ) {
  742. $str [$key] = $this->escape_str ( $val, $like );
  743. }
  744. return $str;
  745. }
  746. var_dump ( Doo::db ()->getDbObject () );
  747. die ();
  748. if (function_exists ( 'mysql_real_escape_string' ) and is_resource ( Doo::db ()->getDbObject () )) { // Doo::db ()->getDbObject()
  749. $str = mysql_real_escape_string ( $str, Doo::db ()->getDbObject () );
  750. } elseif (function_exists ( 'mysql_escape_string' )) {
  751. $str = mysql_escape_string ( $str );
  752. } else {
  753. $str = addslashes ( $str );
  754. }
  755. // escape LIKE condition wildcards
  756. if ($like === TRUE) {
  757. $str = str_replace ( array (
  758. '%',
  759. '_'
  760. ), array (
  761. '\\%',
  762. '\\_'
  763. ), $str );
  764. }
  765. return $str;
  766. }
  767. /**
  768. * 获得分页数据
  769. * @param unknown_type $table
  770. * @param unknown_type $condition
  771. * @param unknown_type $on_page
  772. * @param unknown_type $page_size
  773. */
  774. function get_page($table = "", $condition = "", $on_page = 1, $page_size = 20, $action = "", $get = "", $other = "page") {
  775. $page_c = "";
  776. $page ['previous'] = $this->get_previous ( $on_page );
  777. $page ['on_page'] = $on_page;
  778. $total_count = $this->get_table_count ( $table, $condition );
  779. $total = intval ( $total_count / $page_size );
  780. $page ['total_page'] = ($total_count % $page_size) == 0 ? $total : $total + 1;
  781. $page ['total_data'] = $total_count;
  782. $page ['next'] = $on_page == $page ['total_page'] ? $page ['total_page'] : $on_page + 1;
  783. $i = 1;
  784. $page_max = 1;
  785. $page_width = 3;
  786. if ($on_page >= $page_width) {
  787. $page_max = intval ( $on_page / $page_width ) + 1;
  788. $i = intval ( $on_page / $page_width ) * $page_width - 1;
  789. }
  790. for(; $i <= $page ['total_page']; $i ++) {
  791. if ($i == $on_page)
  792. $page_c .= '<li class="active"><a href="javascript:void(0)">' . $i . '</a></li>';
  793. else
  794. $page_c .= '<li><a href="/' . $action . '/' . $i . $get . '">' . $i . '</a></li>';
  795. if ($i == ($page_width * $page_max))
  796. break;
  797. }
  798. $page ['page'] = $page_c;
  799. $page ['lower'] = (-- $on_page) * $page_size;
  800. return $page;
  801. }
  802. /**
  803. * 获取上一页
  804. * @param unknown_type $on_page
  805. */
  806. function get_previous($on_page = 1) {
  807. return $on_page != 0 ? $on_page - 1 : $on_page;
  808. }
  809. /**
  810. * 获取总页数
  811. * @param unknown_type $table
  812. * @param unknown_type $condition
  813. */
  814. public function get_table_count($table = "", $condition = "") {
  815. // $sql = "select count(*) as count from " . $table . " where 1 " . $condition;
  816. $sql = "select count(*) as count from " . $table . " where " . $condition;
  817. // echo $sql;die;
  818. $query = Doo::db ()->query ( $sql );
  819. $result = $query->fetch ();
  820. return $result ['count'];
  821. }
  822. /**
  823. * 获得分页数据
  824. * @param unknown_type $table
  825. * @param unknown_type $condition
  826. * @param unknown_type $on_page
  827. * @param unknown_type $page_size
  828. */
  829. function getPageBySql($sql = "", $on_page = 1, $page_size = 20, $action = "", $get = "", $other = "page") {
  830. $page_c = "";
  831. $page ['previous'] = $this->get_previous ( $on_page );
  832. $page ['on_page'] = $on_page;
  833. $total_count = $this->getTableCountBySql ( $sql );
  834. $total = intval ( $total_count / $page_size );
  835. $page ['total_page'] = ($total_count % $page_size) == 0 ? $total : $total + 1;
  836. $page ['total_data'] = $total_count;
  837. $page ['next'] = $on_page == $page ['total_page'] ? $page ['total_page'] : $on_page + 1;
  838. $i = 1;
  839. $page_max = 1;
  840. $page_width = 3;
  841. if ($on_page >= $page_width) {
  842. $page_max = intval ( $on_page / $page_width ) + 1;
  843. $i = intval ( $on_page / $page_width ) * $page_width - 1;
  844. }
  845. for(; $i <= $page ['total_page']; $i ++) {
  846. if ($i == $on_page)
  847. $page_c .= '<li class="active"><a href="javascript:void(0)">' . $i . '</a></li>';
  848. else
  849. $page_c .= '<li><a href="/' . $action . '/' . $i . $get . '">' . $i . '</a></li>';
  850. if ($i == ($page_width * $page_max))
  851. break;
  852. }
  853. $page ['page'] = $page_c;
  854. $page ['lower'] = (-- $on_page) * $page_size;
  855. return $page;
  856. }
  857. /**
  858. * 获得总页数 sql
  859. * @param string $type
  860. * @return boolean[]
  861. */
  862. public function getTableCountBySql($sql = "") {
  863. $query = Doo::db ()->query ( $sql );
  864. $result = $query->fetchAll ();
  865. return count ( $result );
  866. }
  867. function isInvoiceNew($type = "GLOBAL") {
  868. Doo::loadModel ( 'invoice' );
  869. $invoice = new invoice ();
  870. Doo::loadModel ( 'invoiceReceivables' );
  871. $invoiceReceivables = new invoiceReceivables ();
  872. Doo::loadModel ( 'staff' );
  873. $staff = new staff ();
  874. $st = $staff->getUserByIdList ( $_COOKIE ["staff"] );
  875. // 我的发票待处理
  876. $pendingInvoice = $invoice->getOne ( array ( // or (status=2 and printStatus=1 and untreadStatus=0 ) or (status=2 and printStatus=1 and untreadStatus=2 )
  877. 'where' => "(status=3 or status=4) and isDelete=0 and sid=" . $st [0] ['sid'],
  878. 'desc' => 'iid',
  879. 'asArray' => TRUE
  880. ) );
  881. // 我的收款待入账
  882. /*
  883. * $myClaim = $invoiceReceivables->getOne ( array ( 'where' => " receivablesStaff like '" . $st [0] ['sid'] . "-%' and receivablesCategory like '" . $st [0] ['cid'] . ":%' and bindStatus=0", 'desc' => 'irid', 'asArray' => TRUE ) );
  884. */
  885. // 发票审批
  886. $approval = $invoice->getOne ( array (
  887. 'where' => 'status=1 and pendingApprovals=' . $st [0] ['sid'],
  888. 'desc' => 'iid',
  889. 'asArray' => true
  890. ) );
  891. // 发票邮寄
  892. $post = $invoice->getOne ( array (
  893. 'where' => "postStatus=0 and printStatus=1 and isDelete=0 and doPost=1 and status=2",
  894. 'asArray' => TRUE
  895. ) );
  896. // 发票打印
  897. $print = $invoice->getOne ( array (
  898. 'where' => "status=2 and printStatus=0 and untreadStatus=0 and moldManage like '%[\"" . $st [0] ['sid'] . "\",%'",
  899. 'desc' => 'iid',
  900. 'asArray' => TRUE
  901. ) );
  902. // 发票退票 printStatus=1 and
  903. $untread = $invoice->getOne ( array (
  904. 'where' => " untreadStatus=1 and status=2 ",
  905. 'asArray' => TRUE
  906. ) );
  907. $new = array (
  908. 'GLOBAL' => FALSE,
  909. 'pendingInvoice' => FALSE,
  910. // 'myClaim' => FALSE,
  911. 'approval' => FALSE,
  912. 'post' => FALSE,
  913. 'print' => FALSE,
  914. 'untread' => FALSE
  915. );
  916. // || ! empty ( $myClaim )
  917. if (! empty ( $pendingInvoice ) || ! empty ( $approval ) || ! empty ( $post ) || ! empty ( $print ) || ! empty ( $untread ))
  918. $new ['GLOBAL'] = TRUE;
  919. if (! empty ( $pendingInvoice ))
  920. $new ['pendingInvoice'] = TRUE;
  921. // if (! empty ( $myClaim ))
  922. // $new ['myClaim'] = TRUE;
  923. if (! empty ( $approval ))
  924. $new ['approval'] = TRUE;
  925. if (! empty ( $post ))
  926. $new ['post'] = TRUE;
  927. if (! empty ( $print ))
  928. $new ['print'] = TRUE;
  929. if (! empty ( $untread ))
  930. $new ['untread'] = TRUE;
  931. return $new;
  932. }
  933. function setWXMsg($rid, $typeMsg = '') {
  934. Doo::loadModel ( 'invoice' );
  935. $invoice = new invoice ();
  936. $invoiceDetail = $invoice->getOne ( array (
  937. 'where' => 'iid=' . $rid,
  938. 'asArray' => true
  939. ) );
  940. if (! empty ( $invoiceDetail )) {
  941. Doo::loadModel ( 'staff' );
  942. $staff = new staff ();
  943. $staffmsg = $staff->getOne ( array (
  944. 'where' => "sid='" . $invoiceDetail ['sid'] . "'",
  945. 'asArray' => TRUE
  946. ) );
  947. if (! empty ( $staffmsg ['wxid'] )) {
  948. require_once (SITE_PATH . '/protected/class/client.php');
  949. $client = new client ( 'http://m.cld.smartcost.com.cn/' );
  950. $user = $staffmsg ['wxid'];
  951. $expCom = explode ( ":", $invoiceDetail ['expressCompany'] );
  952. if (empty ( $expCom ))
  953. $expCom [0] = '';
  954. $msg = "";
  955. if (empty ( $typeMsg ))
  956. $msg = '太好了,您的发票' . $invoiceDetail ['invoiceSerial'] . ' 已邮寄 ' . $expCom [0] . ' ,快递号: <a href="https://m.kuaidi100.com/result.jsp?nu=' . $invoiceDetail ['expressNumber'] . '" >' . $invoiceDetail ['expressNumber'] . '</a>';
  957. elseif ($typeMsg == "PRINT")
  958. $msg = '太好了,您的发票已打印完成。';
  959. $result = $client->SendMsg ( $user, $msg, 'text' );
  960. }
  961. }
  962. }
  963. /**
  964. * 获取微信部门
  965. * @param string $category
  966. * @return number|mixed
  967. */
  968. function getToparty($category = '') {
  969. $token = $this->getAccessToken (); //
  970. $result = json_decode ( $this->httpGet ( "https://qyapi.weixin.qq.com/cgi-bin/department/list?access_token={$token}&id=5" ), true );
  971. $toparty = 0;
  972. foreach ( $result ['department'] as $key => $value ) {
  973. if ($value ['name'] == $category)
  974. $toparty = $value ['id'];
  975. }
  976. return $toparty;
  977. }
  978. /**
  979. * 获得某部门下的用户
  980. * @param string $toparty
  981. */
  982. function getUserByToparty($toparty = '') {
  983. $token = $this->getAccessToken (); //
  984. $result = json_decode ( $this->httpGet ( "https://qyapi.weixin.qq.com/cgi-bin/user/simplelist?access_token={$token}&department_id={$toparty}" ), true );
  985. $userList = array ();
  986. foreach ( $result ['userlist'] as $key => $value ) {
  987. array_push ( $userList, "'" . $value ['userid'] . "'" );
  988. }
  989. return $userList;
  990. }
  991. function msgSendToparty($msg, $title, $url, $toparty, $text = '') {
  992. $token = $this->getAccessToken ();
  993. if ($text == 'text') {
  994. $data = '{
  995. "touser": "' . $toparty . '",
  996. "toparty": "",
  997. "totag": "",
  998. "msgtype": "text",
  999. "agentid": 1000002,
  1000. "text": {
  1001. "content": "' . $msg . '"
  1002. },
  1003. "safe":0
  1004. }';
  1005. }
  1006. $result = json_decode ( $this->httpPOST ( "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={$token}", $data ) );
  1007. if ($result->errmsg != 'ok') {
  1008. return 0;
  1009. } else {
  1010. return 1;
  1011. }
  1012. }
  1013. /**
  1014. * 获得可以发送微信用户列表
  1015. * @param string $category
  1016. * @return string
  1017. */
  1018. function getWxidByCategory($category = "") {
  1019. if (empty ( $category ))
  1020. return "";
  1021. Doo::loadModel ( 'staff' );
  1022. $staff = new staff ();
  1023. $toparty = $this->getToparty ( $category );
  1024. $wxList = array ();
  1025. $wxString = "";
  1026. if (! empty ( $toparty )) {
  1027. $userList = $this->getUserByToparty ( $toparty );
  1028. $userCon = implode ( ',', $userList );
  1029. // echo $userCon;
  1030. $sidList = $staff->getStaffByWxid ( $userCon );
  1031. foreach ( $sidList as $key => $value ) {
  1032. $falg = $this->isSendNotification ( $value ['sid'], 'receivables' );
  1033. if ($falg) {
  1034. array_push ( $wxList, $value ['wxid'] );
  1035. }
  1036. }
  1037. $wxString = implode ( '|', $wxList );
  1038. }
  1039. return $wxString;
  1040. }
  1041. /**
  1042. * 读取发送信息规则判断是否需要发送
  1043. * @param number $sid
  1044. * @param string $sendType
  1045. * @return boolean
  1046. */
  1047. function isSendNotification($sid = 0, $sendType = '') {
  1048. Doo::loadModel ( "notificationConf" );
  1049. $notificationConf = new notificationConf ();
  1050. $detail = $notificationConf->getNotificationConfBySid ( $sid );
  1051. $conf = array ();
  1052. $sendFlag = true;
  1053. if (! empty ( $detail )) {
  1054. $conf = json_decode ( $detail ['conf'], true );
  1055. if (! empty ( $sendType )) {
  1056. if (isset ( $conf [$sendType] )) {
  1057. if ($conf [$sendType] == "CLOSE")
  1058. $sendFlag = false;
  1059. elseif ($conf [$sendType] == "Weekend") {
  1060. $day = date ( "w" );
  1061. if (1 <= $day && 5 >= $day)
  1062. $sendFlag = false;
  1063. } elseif ($conf [$sendType] == "OneToFriday") {
  1064. $day = date ( "w" );
  1065. if (0 == $day || 6 == $day)
  1066. $sendFlag = false;
  1067. }
  1068. }
  1069. } else
  1070. $sendFlag = false;
  1071. }
  1072. return $sendFlag;
  1073. }
  1074. /**
  1075. * 根据规则发送信息
  1076. * @param unknown $msg
  1077. * @param unknown $title
  1078. * @param unknown $url
  1079. * @param unknown $user
  1080. * @param string $text
  1081. * @return number
  1082. */
  1083. private function msgSend($msg, $title, $url, $user, $sid = 0, $sendType = '') {
  1084. $token = $this->getAccessToken ();
  1085. $falg = $this->isSendNotification ( $sid, $sendType );
  1086. // if ($text == 'text') {
  1087. // $data = '{
  1088. // "touser": "' . $user . '",
  1089. // "toparty": "",
  1090. // "totag": "",
  1091. // "msgtype": "text",
  1092. // "agentid": 1000002,
  1093. // "text": {
  1094. // "content": "' . $msg . '"
  1095. // },
  1096. // "safe":0
  1097. // }';
  1098. // } else {
  1099. $data = '{
  1100. "touser": "' . $user . '",
  1101. "toparty": "",
  1102. "totag": "",
  1103. "msgtype": "news",
  1104. "agentid": 1000002,
  1105. "news": {
  1106. "articles":[
  1107. {
  1108. "title": "' . $title . '",
  1109. "description": "' . $msg . '",
  1110. "url": "' . $url . '",
  1111. "picurl": ""
  1112. }
  1113. ]
  1114. }
  1115. }';
  1116. // }
  1117. if ($falg) {
  1118. $result = json_decode ( $this->httpPOST ( "https://qyapi.weixin.qq.com/cgi-bin/message/send?access_token={$token}", $data ) );
  1119. if ($result->errmsg != 'ok') {
  1120. return 0;
  1121. } else {
  1122. return 1;
  1123. }
  1124. } else
  1125. return 0;
  1126. }
  1127. private function getAccessToken() {
  1128. // access_token 应该全局存储与更新,以下代码以写入到文件中做示例
  1129. // $file = './access_token.txt';
  1130. // $data = json_decode ( file_get_contents ( $file ) );
  1131. // if ($data->expire_time < time () or ! $data->expire_time) {
  1132. // 如果是企业号用以下URL获取access_token
  1133. $url = "https://qyapi.weixin.qq.com/cgi-bin/gettoken?corpid=$this->appId&corpsecret=$this->appSecret";
  1134. // $url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=$this->appId&secret=$this->appSecret";
  1135. $res = json_decode ( $this->httpGet ( $url ) );
  1136. $access_token = $res->access_token;
  1137. // echo $access_token;
  1138. // if ($access_token) {
  1139. // $data->expire_time = time () + 7000;
  1140. // $data->access_token = $access_token;
  1141. // $fp = fopen ( $file, "w" );
  1142. // fwrite ( $fp, json_encode ( $data ) );
  1143. // fclose ( $fp );
  1144. // }
  1145. // } else {
  1146. // $access_token = $data->access_token;
  1147. // }
  1148. return $access_token;
  1149. }
  1150. private function httpGet($url) {
  1151. $curl = curl_init ();
  1152. curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true );
  1153. curl_setopt ( $curl, CURLOPT_TIMEOUT, 500 );
  1154. // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
  1155. // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
  1156. curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, true );
  1157. curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false );
  1158. curl_setopt ( $curl, CURLOPT_URL, $url );
  1159. $res = curl_exec ( $curl );
  1160. curl_close ( $curl );
  1161. return $res;
  1162. }
  1163. private function httpPost($url, $data) {
  1164. $curl = curl_init ();
  1165. curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true );
  1166. curl_setopt ( $curl, CURLOPT_TIMEOUT, 500 );
  1167. // 为保证第三方服务器与微信服务器之间数据传输的安全性,所有微信接口采用https方式调用,必须使用下面2行代码打开ssl安全校验。
  1168. // 如果在部署过程中代码在此处验证失败,请到 http://curl.haxx.se/ca/cacert.pem 下载新的证书判别文件。
  1169. curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER, true );
  1170. curl_setopt ( $curl, CURLOPT_SSL_VERIFYHOST, false );
  1171. curl_setopt ( $curl, CURLOPT_URL, $url );
  1172. curl_setopt ( $curl, CURLOPT_POST, 1 );
  1173. curl_setopt ( $curl, CURLOPT_POSTFIELDS, $data );
  1174. $res = curl_exec ( $curl );
  1175. curl_close ( $curl );
  1176. return $res;
  1177. }
  1178. }