Procuremen.php 157 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319
  1. <?php
  2. namespace app\admin\controller;
  3. use app\common\controller\Backend;
  4. use app\common\library\AliyunOss;
  5. use PHPMailer\PHPMailer\PHPMailer;
  6. use think\Config;
  7. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  8. use PhpOffice\PhpSpreadsheet\Style\Alignment;
  9. use PhpOffice\PhpSpreadsheet\Style\Border;
  10. use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
  11. use think\Db;
  12. use think\Log;
  13. class Procuremen extends Backend
  14. {
  15. /**
  16. * 顶部快速搜索(多列 OR LIKE);字段须为真实列名且带别名 a/b,列不宜过多以免 SQL 异常
  17. */
  18. protected $searchFields = 'a.ID,b.CCYDH,b.CYJMC,a.CDXMC,a.cGzzxMc';
  19. /**
  20. * Procuremen模型对象
  21. * @var \app\admin\model\Procuremen
  22. */
  23. protected $model = null;
  24. /**
  25. * @var array<int, array<string, mixed>> 演练模式下收集的下发/短信预览(供接口返回)
  26. */
  27. protected $notifyDryRunPreview = [];
  28. /**
  29. * @var array
  30. */
  31. protected $noNeedRight = [
  32. 'review', 'reviewCompanies', 'outward_detail', 'export_month_outward', 'snapshotToProcure',
  33. 'pick', 'audit', 'confirm', 'pickreview', 'picksubmit', 'pickadd', 'auditissue', 'auditsubmit',
  34. ];
  35. public function _initialize()
  36. {
  37. parent::_initialize();
  38. $this->model = new \app\admin\model\Procuremen;
  39. }
  40. /**
  41. * 手机端外发明细首页直达链接(登录后打开对应明细,免搜索)
  42. * 配置 application/extra/mproc.php:mobile_base_url、mobile_index_path
  43. *
  44. * @param int $purchaseOrderDetailId purchase_order_detail 主键
  45. */
  46. protected function buildMprocMobileOrderUrl($purchaseOrderDetailId)
  47. {
  48. static $mprocCfgLoaded = false;
  49. if (!$mprocCfgLoaded) {
  50. $mprocCfgLoaded = true;
  51. if (is_file(APP_PATH . 'extra/mproc.php')) {
  52. Config::load(APP_PATH . 'extra/mproc.php', 'mproc');
  53. }
  54. }
  55. $eid = (int)$purchaseOrderDetailId;
  56. $base = $this->resolveMprocMobilePublicBaseUrl();
  57. if ($base === '') {
  58. throw new \Exception('无法生成手机端链接,请检查站点访问地址或 application/extra/mproc.php 中的 mobile_base_url');
  59. }
  60. $path = trim((string)Config::get('mproc.mobile_index_path'));
  61. if ($path === '') {
  62. $path = '/index.php/index/index/index';
  63. } else {
  64. $path = '/' . ltrim($path, '/');
  65. }
  66. $query = ['main_tab' => 'orders'];
  67. if ($eid > 0) {
  68. $query['focus_eid'] = $eid;
  69. }
  70. return $base . $path . '?' . http_build_query($query, '', '&', PHP_QUERY_RFC3986);
  71. }
  72. /**
  73. * 邮件/短信外链:须为带点的公网域名
  74. */
  75. protected function procuremenOutboundBaseUrlLooksValid($baseUrl)
  76. {
  77. $u = trim((string)$baseUrl);
  78. if ($u === '' || !preg_match('#^https?://#i', $u)) {
  79. return false;
  80. }
  81. $host = parse_url($u, PHP_URL_HOST);
  82. if (!is_string($host) || $host === '') {
  83. return false;
  84. }
  85. if (strcasecmp($host, 'localhost') === 0) {
  86. return false;
  87. }
  88. if (preg_match('/^(127\.|10\.|192\.168\.|172\.(1[6-9]|2\d|3[01])\.)/', $host)) {
  89. return false;
  90. }
  91. if (strpos($host, '.') === false) {
  92. return false;
  93. }
  94. return true;
  95. }
  96. /**
  97. * 解析用于外发邮件/短信的手机端站点根
  98. */
  99. protected function resolveMprocMobilePublicBaseUrl()
  100. {
  101. $candidates = [];
  102. $candidates[] = trim((string)Config::get('mproc.mobile_base_url'));
  103. $candidates[] = trim((string)Config::get('site.indexurl'));
  104. $cdn = trim((string)Config::get('site.cdnurl'));
  105. if ($cdn !== '' && preg_match('#^https?://#i', $cdn)) {
  106. $candidates[] = rtrim($cdn, '/');
  107. }
  108. $reqBase = rtrim($this->request->scheme() . '://' . $this->request->host(), '/');
  109. $candidates[] = $reqBase;
  110. foreach ($candidates as $c) {
  111. if ($c === '') {
  112. continue;
  113. }
  114. $c = rtrim($c, '/');
  115. if ($this->procuremenOutboundBaseUrlLooksValid($c)) {
  116. return $c;
  117. }
  118. }
  119. // 本地联调:127.0.0.1、内网 IP、虚拟主机等无法用「公网域名」规则时,退回当前请求根地址
  120. $reqBase = rtrim($this->request->scheme() . '://' . $this->request->host(), '/');
  121. if ($reqBase !== '' && preg_match('#^https?://#i', $reqBase)) {
  122. return $reqBase;
  123. }
  124. Log::write('外发邮件手机链接:配置地址失效', 'warning');
  125. return '';
  126. }
  127. /**
  128. * 排除 mcyd.dputrecord 空串、零日期等非法值,避免 MySQL 1525(DATE_FORMAT/BETWEEN 遇 '' 报错)
  129. */
  130. protected function whereMcydDputrecordValid($query, $alias = 'a')
  131. {
  132. $c = $alias . '.dStamp';
  133. // 先转成 CHAR 再判断,避免 DATETIME 列里存 '' / 0000-00-00 时参与日期函数报错
  134. return $query->whereRaw(
  135. $c . ' IS NOT NULL'
  136. . ' AND TRIM(CAST(' . $c . ' AS CHAR(32))) <> \'\''
  137. . ' AND TRIM(CAST(' . $c . ' AS CHAR(32))) NOT LIKE \'0000-00-00%\''
  138. . ' AND TRIM(CAST(' . $c . ' AS CHAR(32))) REGEXP \'^[12][0-9]{3}-[01][0-9]-[0-3][0-9]\''
  139. );
  140. }
  141. /**
  142. * 获取 Redis 中 procuremen_redis
  143. */
  144. protected function ProcuremenRedis(): array
  145. {
  146. try {
  147. $redis = redis();
  148. $raw = $redis->get('procuremen_redis');
  149. if ($raw === false || $raw === '') {
  150. return [];
  151. }
  152. $decoded = json_decode($raw, true);
  153. if (!is_array($decoded) || !isset($decoded['data']) || !is_array($decoded['data'])) {
  154. return [];
  155. }
  156. return $decoded['data'];
  157. } catch (\Throwable $e) {
  158. return [];
  159. }
  160. }
  161. /**
  162. * 左侧菜单
  163. */
  164. protected function GetIndexYearMonths()
  165. {
  166. $ymSet = [];
  167. //获取此方法调用缓存数据
  168. foreach ($this->ProcuremenRedis() as $r) {
  169. if (!is_array($r)) {
  170. continue;
  171. }
  172. $ds = trim((string)($r['dputrecord'] ?? ''));
  173. if ($ds === '' || stripos($ds, '0000-00-00') === 0) {
  174. continue;
  175. }
  176. if (!preg_match('/^([12]\d{3})-(\d{1,2})-(\d{1,2})/', $ds, $m)) {
  177. continue;
  178. }
  179. $mo = (int)$m[2];
  180. if ($mo < 1 || $mo > 12) {
  181. continue;
  182. }
  183. $ymSet[$m[1] . '-' . str_pad((string)$mo, 2, '0', STR_PAD_LEFT)] = true;
  184. }
  185. $ymList = array_keys($ymSet);
  186. rsort($ymList, SORT_STRING);
  187. $byYear = [];
  188. foreach ($ymList as $ym) {
  189. $y = substr($ym, 0, 4);
  190. $mo = (int)substr($ym, 5, 2);
  191. if (!isset($byYear[$y])) {
  192. $byYear[$y] = [];
  193. }
  194. $byYear[$y][] = ['ym' => $ym, 'label' => $mo . '月'];
  195. }
  196. krsort($byYear, SORT_NUMERIC);
  197. foreach ($byYear as $y => $items) {
  198. usort($byYear[$y], function ($a, $b) {
  199. return strcmp($b['ym'], $a['ym']);
  200. });
  201. }
  202. $sidebar = [];
  203. foreach ($byYear as $y => $months) {
  204. $sidebar[] = ['year' => $y, 'months' => $months];
  205. }
  206. return $sidebar;
  207. }
  208. /**
  209. * 列表阶段:pick=下发(通知) audit=确认供应商 confirm=采购确认
  210. */
  211. protected function resolveProcuremenListStage(): string
  212. {
  213. $action = strtolower((string)$this->request->action());
  214. if (in_array($action, ['pick', 'audit', 'confirm'], true)) {
  215. return $action;
  216. }
  217. $tab = trim((string)$this->request->request('wff_tab', 'pick'));
  218. return in_array($tab, ['pick', 'audit', 'confirm'], true) ? $tab : 'pick';
  219. }
  220. protected function assignProcuremenListPage(string $stage, string $pageTitle): void
  221. {
  222. $rootTrue = rtrim((string)$this->request->root(true), '/');
  223. $indexPhpRoot = preg_replace('#/[^/]+\.php$#i', '/index.php', $rootTrue);
  224. if ($indexPhpRoot === $rootTrue && !preg_match('#/index\.php$#i', $rootTrue)) {
  225. $indexPhpRoot = $rootTrue . '/index.php';
  226. }
  227. $this->view->assign('procuremenRedisApi', $indexPhpRoot . '/api/procuremen/getprocuremen');
  228. $this->view->assign('defaultYm', date('Y-m'));
  229. $this->view->assign('sidebarYearMonths', $this->GetIndexYearMonths());
  230. $this->view->assign('procuremenStage', $stage);
  231. $this->view->assign('procuremenPageTitle', $pageTitle);
  232. }
  233. /** 外发下发(选工序+供应商,发短信邮件通知报价) */
  234. public function pick()
  235. {
  236. $this->request->filter(['strip_tags', 'trim']);
  237. if (!$this->request->isAjax()) {
  238. $this->assignProcuremenListPage('pick', '外发下发');
  239. return $this->view->fetch('procuremen/index');
  240. }
  241. return $this->index();
  242. }
  243. /** 确认供应商(下发通知后,从报价中选定一家,进入采购确认) */
  244. public function audit()
  245. {
  246. $this->request->filter(['strip_tags', 'trim']);
  247. if (!$this->request->isAjax()) {
  248. $this->assignProcuremenListPage('audit', '确认供应商');
  249. return $this->view->fetch('procuremen/index');
  250. }
  251. return $this->index();
  252. }
  253. /** 采购确认(定标) */
  254. public function confirm()
  255. {
  256. $this->request->filter(['strip_tags', 'trim']);
  257. if (!$this->request->isAjax()) {
  258. $this->assignProcuremenListPage('confirm', '采购确认');
  259. return $this->view->fetch('procuremen/index');
  260. }
  261. return $this->index();
  262. }
  263. /**
  264. * 列表页(兼容旧菜单 procuremen/index,等同外发下发)
  265. */
  266. public function index()
  267. {
  268. $this->request->filter(['strip_tags', 'trim']);
  269. if (!$this->request->isAjax()) {
  270. $this->assignProcuremenListPage('pick', '外发下发');
  271. return $this->view->fetch('procuremen/index');
  272. }
  273. if ($this->request->request('keyField')) {
  274. return $this->selectpage();
  275. }
  276. list(, $sort, $order, $offset, $limit) = $this->buildparams();
  277. $limit = max(10, min(500, (int)$limit));
  278. $ym = $this->request->request('ym', date('Y-m'));
  279. $ym = is_string($ym) ? trim($ym) : '';
  280. if (!preg_match('/^\d{4}-\d{2}$/', $ym)) {
  281. $ym = date('Y-m');
  282. }
  283. $monthStart = $ym . '-01 00:00:00';
  284. $monthEnd = date('Y-m-t 23:59:59', strtotime($monthStart));
  285. $search = trim((string)$this->request->get('search', ''));
  286. $hasActiveSearch = ($search !== '');
  287. if (!$hasActiveSearch) {
  288. $filterStr = $this->request->get('filter', '');
  289. if ($filterStr !== '' && $filterStr !== '[]' && $filterStr !== '{}') {
  290. $arr = json_decode($filterStr, true);
  291. if (is_array($arr) && count($arr) > 0) {
  292. foreach ($arr as $v) {
  293. if (is_array($v)) {
  294. if (array_filter($v, function ($x) {
  295. return $x !== '' && $x !== null;
  296. })) {
  297. $hasActiveSearch = true;
  298. break;
  299. }
  300. } elseif ($v !== '' && $v !== null) {
  301. $hasActiveSearch = true;
  302. break;
  303. }
  304. }
  305. }
  306. }
  307. }
  308. $applyMonthRange = !$hasActiveSearch;
  309. $filterArr = (array)json_decode($this->request->get('filter', ''), true);
  310. $opArr = (array)json_decode($this->request->get('op', ''), true);
  311. /* wff_tab / 菜单:pick=待下发 audit=待确认供应商 confirm=待采购确认 */
  312. $wffTab = $this->resolveProcuremenListStage();
  313. try {
  314. if ($wffTab === 'audit') {
  315. try {
  316. $dbRows = Db::table('purchase_order')->where('wflow_status', 1)->select();
  317. } catch (\Throwable $e) {
  318. $dbRows = [];
  319. }
  320. $pool = $this->procuremenPoolFromPurchaseOrderDbRows(is_array($dbRows) ? $dbRows : []);
  321. } elseif ($wffTab === 'confirm') {
  322. try {
  323. $dbRows = Db::table('purchase_order')
  324. ->where('status', 0)
  325. ->where(function ($q) {
  326. $q->where('wflow_status', 2)
  327. ->whereOr(function ($q2) {
  328. $q2->where('wflow_status', 0)->whereRaw(
  329. 'EXISTS (SELECT 1 FROM purchase_order_detail d WHERE d.scydgy_id = purchase_order.scydgy_id LIMIT 1)'
  330. );
  331. });
  332. })
  333. ->select();
  334. } catch (\Throwable $e) {
  335. $dbRows = [];
  336. }
  337. $pool = $this->procuremenPoolFromPurchaseOrderDbRows(is_array($dbRows) ? $dbRows : []);
  338. } else {
  339. $pool = $this->ProcuremenRedis();
  340. if (!is_array($pool)) {
  341. $pool = [];
  342. }
  343. // 下发列表:已通知供应商(有明细)或已进入审核/确认/完结的工序不再显示
  344. $hideSet = $this->loadPickHiddenScydgySet();
  345. if ($pool !== [] && $hideSet !== []) {
  346. $pool = array_values(array_filter($pool, function ($r) use ($hideSet) {
  347. if (!is_array($r)) {
  348. return false;
  349. }
  350. $id = (int)($r['ID'] ?? $r['id'] ?? 0);
  351. return $id <= 0 || !isset($hideSet[$id]);
  352. }));
  353. }
  354. $manualPool = $this->loadPickManualOrderRows();
  355. if ($manualPool !== []) {
  356. $pool = array_merge($pool, $manualPool);
  357. }
  358. if ($pool === []) {
  359. return json([
  360. 'total' => 0,
  361. 'rows' => [],
  362. 'msg' => '暂无缓存数据',
  363. ]);
  364. }
  365. }
  366. $filtered = $this->filterProcuremenIndexPool(
  367. $pool,
  368. $monthStart,
  369. $monthEnd,
  370. $applyMonthRange,
  371. $search,
  372. $filterArr,
  373. $opArr
  374. );
  375. /* 确认供应商:同一订单号合并为一行(下发时一并提交的多道工序只显示一次) */
  376. if ($wffTab === 'audit') {
  377. $filtered = $this->collapseProcuremenPoolByOrder($filtered);
  378. }
  379. $sortField = 'ID';
  380. if (is_string($sort) && $sort !== '') {
  381. $parts = explode(',', $sort);
  382. $sortField = preg_replace('/^[ab]\./i', '', trim($parts[0]));
  383. if ($sortField === '') {
  384. $sortField = 'ID';
  385. }
  386. }
  387. $ord = strtoupper((string)$order) === 'ASC' ? 1 : -1;
  388. $nFiltered = count($filtered);
  389. if ($nFiltered > 1) {
  390. if (in_array($sortField, ['pick_time', 'dputrecord', 'dStamp', 'createtime'], true)) {
  391. usort($filtered, function ($a, $b) use ($sortField, $ord) {
  392. $ta = $this->procuremenRowListSortTime($a, $sortField);
  393. $tb = $this->procuremenRowListSortTime($b, $sortField);
  394. if ($ta === $tb) {
  395. return ((int)($a['ID'] ?? 0) <=> (int)($b['ID'] ?? 0)) * $ord;
  396. }
  397. if ($ta === '') {
  398. return 1;
  399. }
  400. if ($tb === '') {
  401. return -1;
  402. }
  403. return strcmp($ta, $tb) * $ord;
  404. });
  405. } elseif ($sortField === 'ID') {
  406. $sortKeys = [];
  407. foreach ($filtered as $i => $row) {
  408. $sortKeys[$i] = (int)($row['ID'] ?? 0);
  409. }
  410. $dir = $ord === 1 ? SORT_ASC : SORT_DESC;
  411. array_multisort($sortKeys, $dir, SORT_NUMERIC, $filtered);
  412. } else {
  413. usort($filtered, function ($a, $b) use ($sortField, $ord) {
  414. $va = $a[$sortField] ?? null;
  415. $vb = $b[$sortField] ?? null;
  416. $sa = (string)$va;
  417. $sb = (string)$vb;
  418. if ($sa === $sb) {
  419. return 0;
  420. }
  421. return strcmp($sa, $sb) * $ord;
  422. });
  423. }
  424. }
  425. $offset = max(0, (int)$offset);
  426. $limit = max(1, (int)$limit);
  427. $rows = array_slice($filtered, $offset, $limit);
  428. foreach ($rows as &$rw) {
  429. if (!is_array($rw)) {
  430. continue;
  431. }
  432. $rid = (int)($rw['ID'] ?? 0);
  433. $rw['_iss_out'] = ($wffTab !== 'pick');
  434. }
  435. unset($rw);
  436. if ($wffTab === 'confirm' && count($rows) > 0) {
  437. $idList = [];
  438. foreach ($rows as $rw) {
  439. if (!is_array($rw)) {
  440. continue;
  441. }
  442. $id = (int)($rw['ID'] ?? 0);
  443. if ($id > 0) {
  444. $idList[$id] = true;
  445. }
  446. }
  447. $sumMap = $this->OrderDetailSummary(array_keys($idList));
  448. foreach ($rows as &$rw) {
  449. if (!is_array($rw)) {
  450. continue;
  451. }
  452. $rid = (int)($rw['ID'] ?? 0);
  453. $s = isset($sumMap[$rid]) ? $sumMap[$rid] : ['cnt' => 0, 'amt' => 0, 'deliv' => 0];
  454. $rw['po_detail_count'] = $s['cnt'];
  455. $rw['po_amount_fill_cnt'] = $s['amt'];
  456. $rw['po_delivery_fill_cnt'] = $s['deliv'];
  457. }
  458. unset($rw);
  459. }
  460. if ($wffTab === 'audit' && count($rows) > 0) {
  461. $idList = [];
  462. foreach ($rows as $rw) {
  463. if (!is_array($rw)) {
  464. continue;
  465. }
  466. if (!empty($rw['_order_merge_rows']) && is_array($rw['_order_merge_rows'])) {
  467. foreach ($rw['_order_merge_rows'] as $mr) {
  468. $mid = (int)($mr['ID'] ?? 0);
  469. if ($mid > 0) {
  470. $idList[$mid] = true;
  471. }
  472. }
  473. }
  474. $id = (int)($rw['ID'] ?? 0);
  475. if ($id > 0) {
  476. $idList[$id] = true;
  477. }
  478. }
  479. $quoteBucket = $this->loadQuotedSupplierBucketByScydgyIds(array_keys($idList));
  480. foreach ($rows as &$rw) {
  481. if (!is_array($rw)) {
  482. continue;
  483. }
  484. $mergeIds = [];
  485. if (!empty($rw['_order_merge_rows']) && is_array($rw['_order_merge_rows'])) {
  486. foreach ($rw['_order_merge_rows'] as $mr) {
  487. $mid = (int)($mr['ID'] ?? 0);
  488. if ($mid > 0) {
  489. $mergeIds[$mid] = true;
  490. }
  491. }
  492. }
  493. $rid = (int)($rw['ID'] ?? 0);
  494. if ($rid > 0) {
  495. $mergeIds[$rid] = true;
  496. }
  497. $buckets = [];
  498. foreach (array_keys($mergeIds) as $mid) {
  499. if (isset($quoteBucket[$mid])) {
  500. $buckets[] = $quoteBucket[$mid];
  501. }
  502. }
  503. $rw['picked_supplier_name'] = $buckets !== []
  504. ? $this->formatQuotedSupplierLines($this->mergeQuotedSupplierBuckets($buckets))
  505. : '';
  506. }
  507. unset($rw);
  508. }
  509. if ($wffTab === 'all' && count($rows) > 0) {
  510. $this->mergePurchaseOrder($rows);
  511. }
  512. return json([
  513. 'total' => count($filtered),
  514. 'rows' => $rows,
  515. ]);
  516. } catch (\Throwable $e) {
  517. return json([
  518. 'total' => 0,
  519. 'rows' => [],
  520. 'msg' => $e->getMessage(),
  521. ]);
  522. }
  523. }
  524. /**
  525. * 已外发/不可再审核判定(与「未发」列表隐藏规则一致,避免仅保存数量却被当成已下发)
  526. * - purchase_order.status 为 0 或 1:已审核下发或已完结
  527. * - 或存在 purchase_order_detail:已向供应商下过明细
  528. * 仅写入 purchase_order 且 status 为空:视为未发(如只填本次数量、最高限价)
  529. *
  530. * @return array<int, true> scydgy_id => true
  531. */
  532. protected function loadIssuedScydgySet()
  533. {
  534. $issuedSet = [];
  535. try {
  536. $poList = Db::table('purchase_order')
  537. ->whereRaw('(`status` = 0 OR `status` = 1)')
  538. ->column('scydgy_id');
  539. if (is_array($poList)) {
  540. foreach ($poList as $ids) {
  541. $n = (int)$ids;
  542. if ($n > 0) {
  543. $issuedSet[$n] = true;
  544. }
  545. }
  546. }
  547. } catch (\Throwable $e) {
  548. }
  549. try {
  550. $detList = Db::table('purchase_order_detail')->group('scydgy_id')->column('scydgy_id');
  551. if (is_array($detList)) {
  552. foreach ($detList as $ids) {
  553. $n = (int)$ids;
  554. if ($n > 0) {
  555. $issuedSet[$n] = true;
  556. }
  557. }
  558. }
  559. } catch (\Throwable $e) {
  560. }
  561. return $issuedSet;
  562. }
  563. /**
  564. * 外发下发列表需隐藏的工序 scydgy_id(已下发通知或已进入后续流程)
  565. *
  566. * @return array<int, true>
  567. */
  568. protected function loadPickHiddenScydgySet(): array
  569. {
  570. $hideSet = [];
  571. try {
  572. $poIds = Db::table('purchase_order')
  573. ->where(function ($q) {
  574. $q->where('wflow_status', '>=', 1)->whereOr('status', 1);
  575. })
  576. ->column('scydgy_id');
  577. if (is_array($poIds)) {
  578. foreach ($poIds as $pid) {
  579. $k = (int)$pid;
  580. if ($k > 0) {
  581. $hideSet[$k] = true;
  582. }
  583. }
  584. }
  585. } catch (\Throwable $e) {
  586. }
  587. try {
  588. $detIds = Db::table('purchase_order_detail')->column('scydgy_id');
  589. if (is_array($detIds)) {
  590. foreach ($detIds as $pid) {
  591. $k = (int)$pid;
  592. if ($k > 0) {
  593. $hideSet[$k] = true;
  594. }
  595. }
  596. }
  597. } catch (\Throwable $e) {
  598. }
  599. try {
  600. $timeIds = Db::table('purchase_order')
  601. ->whereNotNull('pick_time')
  602. ->where('pick_time', '<>', '')
  603. ->where('pick_time', '<>', '0000-00-00 00:00:00')
  604. ->column('scydgy_id');
  605. if (is_array($timeIds)) {
  606. foreach ($timeIds as $pid) {
  607. $k = (int)$pid;
  608. if ($k > 0) {
  609. $hideSet[$k] = true;
  610. }
  611. }
  612. }
  613. } catch (\Throwable $e) {
  614. }
  615. return $hideSet;
  616. }
  617. /**
  618. * 外发下发列表:手工新增工序(purchase_order.scydgy_id 为负数,与 ERP 缓存行区分)
  619. *
  620. * @return array<int, array<string, mixed>>
  621. */
  622. protected function loadPickManualOrderRows(): array
  623. {
  624. try {
  625. $dbRows = Db::table('purchase_order')->where('scydgy_id', '<', 0)->order('scydgy_id', 'asc')->select();
  626. } catch (\Throwable $e) {
  627. return [];
  628. }
  629. if (!is_array($dbRows) || $dbRows === []) {
  630. return [];
  631. }
  632. $pool = [];
  633. foreach ($dbRows as $dbRow) {
  634. if (!is_array($dbRow)) {
  635. continue;
  636. }
  637. $sid = (int)($dbRow['scydgy_id'] ?? 0);
  638. if ($sid >= 0) {
  639. continue;
  640. }
  641. $wf = (int)($dbRow['wflow_status'] ?? 0);
  642. if ($wf >= 1) {
  643. continue;
  644. }
  645. $st = trim((string)($dbRow['status'] ?? ''));
  646. if ($st === '1' || $st === '0') {
  647. continue;
  648. }
  649. try {
  650. $detCnt = (int)Db::table('purchase_order_detail')->where('scydgy_id', $sid)->count();
  651. if ($detCnt > 0) {
  652. continue;
  653. }
  654. } catch (\Throwable $e) {
  655. }
  656. $r = $this->buildListRowFromPurchaseOrderDbRow($dbRow);
  657. $r['_is_manual'] = 1;
  658. $pool[] = $r;
  659. }
  660. return $pool;
  661. }
  662. /**
  663. * 列表排序用时间:下发时间 pick_time 优先,其次 createtime / dputrecord / dStamp
  664. */
  665. protected function procuremenRowListSortTime(array $row, string $primary = 'dputrecord'): string
  666. {
  667. $keys = array_values(array_unique([$primary, 'pick_time', 'createtime', 'dputrecord', 'dStamp']));
  668. foreach ($keys as $k) {
  669. $t = trim((string)($row[$k] ?? ''));
  670. if ($t !== '' && stripos($t, '0000-00-00') !== 0) {
  671. return $t;
  672. }
  673. }
  674. return '';
  675. }
  676. /**
  677. * 分配手工新增工序行 ID(负数递减)
  678. */
  679. protected function allocateManualScydgyId(): int
  680. {
  681. try {
  682. $min = Db::table('purchase_order')->where('scydgy_id', '<', 0)->min('scydgy_id');
  683. $min = (int)$min;
  684. return $min < 0 ? $min - 1 : -1;
  685. } catch (\Throwable $e) {
  686. return -1;
  687. }
  688. }
  689. /**
  690. * 工序行主键:ERP 为正 scydgy.ID;手工新增为负 purchase_order.scydgy_id
  691. */
  692. protected function extractScydgyRowId(array $row): int
  693. {
  694. return (int)($row['ID'] ?? $row['id'] ?? $row['scydgy_id'] ?? 0);
  695. }
  696. protected function isValidScydgyRowId(int $id): bool
  697. {
  698. return $id !== 0;
  699. }
  700. protected function isManualScydgyRowId(int $id): bool
  701. {
  702. return $id < 0;
  703. }
  704. /**
  705. * 采购确认已选定供应商:明细 purchase_order_detail.status = 1 的工序行 scydgy_id 集合
  706. *
  707. * @return array<int, true>
  708. */
  709. protected function loadScydgyIdsWithPickedSupplierDetail(): array
  710. {
  711. $set = [];
  712. try {
  713. $rows = Db::table('purchase_order_detail')->where('status', 1)->field('scydgy_id')->select();
  714. } catch (\Throwable $e) {
  715. $rows = [];
  716. }
  717. if (!is_array($rows)) {
  718. return $set;
  719. }
  720. foreach ($rows as $r) {
  721. if (!is_array($r)) {
  722. continue;
  723. }
  724. $sid = (int)($r['scydgy_id'] ?? $r['SCYDGY_ID'] ?? 0);
  725. if ($sid > 0) {
  726. $set[$sid] = true;
  727. }
  728. }
  729. return $set;
  730. }
  731. /**
  732. * 每个工序行对应已选中供应商名称(取 status=1 的首条明细 company_name)
  733. *
  734. * @param int[] $scydgyIds
  735. * @return array<int, string> scydgy_id => company_name
  736. */
  737. protected function loadPickedSupplierCompanyByScydgyIds(array $scydgyIds): array
  738. {
  739. $out = [];
  740. $scydgyIds = array_values(array_unique(array_filter(array_map('intval', $scydgyIds))));
  741. if ($scydgyIds === []) {
  742. return $out;
  743. }
  744. try {
  745. $rows = Db::table('purchase_order_detail')
  746. ->where('scydgy_id', 'in', $scydgyIds)
  747. ->where('status', 1)
  748. ->field('scydgy_id,company_name')
  749. ->order('id', 'asc')
  750. ->select();
  751. } catch (\Throwable $e) {
  752. try {
  753. $rows = Db::table('purchase_order_detail')
  754. ->where('scydgy_id', 'in', $scydgyIds)
  755. ->where('status', 1)
  756. ->field('scydgy_id,company_name')
  757. ->order('ID', 'asc')
  758. ->select();
  759. } catch (\Throwable $e2) {
  760. $rows = [];
  761. }
  762. }
  763. if (!is_array($rows)) {
  764. return $out;
  765. }
  766. foreach ($rows as $r) {
  767. if (!is_array($r)) {
  768. continue;
  769. }
  770. $sid = (int)($r['scydgy_id'] ?? $r['SCYDGY_ID'] ?? 0);
  771. if (!$this->isValidScydgyRowId($sid) || isset($out[$sid])) {
  772. continue;
  773. }
  774. $name = trim((string)($r['company_name'] ?? ''));
  775. $out[$sid] = $name;
  776. }
  777. return $out;
  778. }
  779. /**
  780. * 审核列表:工序行已通知/已报价供应商(purchase_order_detail 有加工金额视为已报价)
  781. *
  782. * @param int[] $scydgyIds
  783. * @return array<int, array{all: array<string, bool>, quoted: array<string, bool>}>
  784. */
  785. protected function loadQuotedSupplierBucketByScydgyIds(array $scydgyIds): array
  786. {
  787. $out = [];
  788. $scydgyIds = array_values(array_unique(array_filter(array_map('intval', $scydgyIds))));
  789. if ($scydgyIds === []) {
  790. return $out;
  791. }
  792. try {
  793. $rows = Db::table('purchase_order_detail')
  794. ->where('scydgy_id', 'in', $scydgyIds)
  795. ->field('scydgy_id,company_name,amount')
  796. ->select();
  797. } catch (\Throwable $e) {
  798. $rows = [];
  799. }
  800. if (!is_array($rows)) {
  801. return $out;
  802. }
  803. foreach ($rows as $r) {
  804. if (!is_array($r)) {
  805. continue;
  806. }
  807. $sid = (int)($r['scydgy_id'] ?? 0);
  808. $cn = trim((string)($r['company_name'] ?? ''));
  809. if (!$this->isValidScydgyRowId($sid) || $cn === '') {
  810. continue;
  811. }
  812. if (!isset($out[$sid])) {
  813. $out[$sid] = ['all' => [], 'quoted' => []];
  814. }
  815. $out[$sid]['all'][$cn] = true;
  816. $am = trim((string)($r['amount'] ?? ''));
  817. if ($am !== '' && $am !== '0' && $am !== '0.00') {
  818. $out[$sid]['quoted'][$cn] = true;
  819. }
  820. }
  821. return $out;
  822. }
  823. /**
  824. * @param array<int, array{all: array<string, bool>, quoted: array<string, bool>}> $buckets
  825. * @return array{all: array<string, bool>, quoted: array<string, bool>}
  826. */
  827. protected function mergeQuotedSupplierBuckets(array $buckets): array
  828. {
  829. $merged = ['all' => [], 'quoted' => []];
  830. foreach ($buckets as $b) {
  831. if (!is_array($b)) {
  832. continue;
  833. }
  834. foreach ($b['all'] ?? [] as $cn => $_) {
  835. $merged['all'][$cn] = true;
  836. }
  837. foreach ($b['quoted'] ?? [] as $cn => $_) {
  838. $merged['quoted'][$cn] = true;
  839. }
  840. }
  841. return $merged;
  842. }
  843. /**
  844. * @param array{all: array<string, bool>, quoted: array<string, bool>} $bucket
  845. */
  846. protected function formatQuotedSupplierLines(array $bucket): string
  847. {
  848. $all = array_keys($bucket['all'] ?? []);
  849. if ($all === []) {
  850. return '';
  851. }
  852. sort($all, SORT_STRING);
  853. $lines = [];
  854. foreach ($all as $cn) {
  855. $status = !empty($bucket['quoted'][$cn]) ? '已报价' : '未报价';
  856. $lines[] = $cn . '(' . $status . ')';
  857. }
  858. return implode("\n", $lines);
  859. }
  860. /**
  861. * @param int[] $scydgyIds
  862. * @return array<int, string>
  863. */
  864. protected function loadQuotedSupplierSummaryByScydgyIds(array $scydgyIds): array
  865. {
  866. $out = [];
  867. foreach ($this->loadQuotedSupplierBucketByScydgyIds($scydgyIds) as $sid => $bucket) {
  868. $text = $this->formatQuotedSupplierLines($bucket);
  869. if ($text !== '') {
  870. $out[$sid] = $text;
  871. }
  872. }
  873. return $out;
  874. }
  875. /**
  876. * 审核弹窗:按供应商汇总报价明细
  877. *
  878. * @param array{ccydh:string, pos:array, merge_rows:array} $bundle
  879. * @return array<int, array<string, mixed>>
  880. */
  881. protected function loadAuditSupplierQuoteGroups(array $bundle): array
  882. {
  883. $sids = [];
  884. foreach ($bundle['pos'] ?? [] as $po) {
  885. if (!is_array($po)) {
  886. continue;
  887. }
  888. $sid = (int)($po['scydgy_id'] ?? 0);
  889. if ($sid > 0) {
  890. $sids[$sid] = true;
  891. }
  892. }
  893. if ($sids === []) {
  894. return [];
  895. }
  896. $gymcMap = [];
  897. foreach ($bundle['merge_rows'] ?? [] as $mr) {
  898. if (!is_array($mr)) {
  899. continue;
  900. }
  901. $gid = (int)($mr['ID'] ?? 0);
  902. if ($gid > 0) {
  903. $gymcMap[$gid] = trim((string)($mr['CGYMC'] ?? ''));
  904. }
  905. }
  906. foreach ($bundle['pos'] ?? [] as $po) {
  907. if (!is_array($po)) {
  908. continue;
  909. }
  910. $gid = (int)($po['scydgy_id'] ?? 0);
  911. if ($gid > 0) {
  912. $nm = trim((string)($po['CGYMC'] ?? ''));
  913. if ($nm !== '') {
  914. $gymcMap[$gid] = $nm;
  915. }
  916. }
  917. }
  918. try {
  919. $details = Db::table('purchase_order_detail')
  920. ->where('scydgy_id', 'in', array_keys($sids))
  921. ->order('company_name', 'asc')
  922. ->order('id', 'asc')
  923. ->select();
  924. } catch (\Throwable $e) {
  925. try {
  926. $details = Db::table('purchase_order_detail')
  927. ->where('scydgy_id', 'in', array_keys($sids))
  928. ->order('company_name', 'asc')
  929. ->order('ID', 'asc')
  930. ->select();
  931. } catch (\Throwable $e2) {
  932. $details = [];
  933. }
  934. }
  935. if (!is_array($details)) {
  936. $details = [];
  937. }
  938. $byCompany = [];
  939. foreach ($details as $d) {
  940. if (!is_array($d)) {
  941. continue;
  942. }
  943. $cn = trim((string)($d['company_name'] ?? ''));
  944. if ($cn === '') {
  945. continue;
  946. }
  947. if (!isset($byCompany[$cn])) {
  948. $ph = trim((string)($d['phone'] ?? ''));
  949. $byCompany[$cn] = [
  950. 'name' => $cn,
  951. 'email' => trim((string)($d['email'] ?? '')),
  952. 'phone' => $ph,
  953. 'username' => $this->resolveCustomerContactName($ph, $cn),
  954. 'has_quote' => false,
  955. 'lines' => [],
  956. ];
  957. }
  958. $sid = (int)($d['scydgy_id'] ?? 0);
  959. $am = trim((string)($d['amount'] ?? ''));
  960. $gymc = $gymcMap[$sid] ?? trim((string)($d['CGYMC'] ?? $d['cgymc'] ?? ''));
  961. if ($gymc === '') {
  962. $gymc = '工序';
  963. }
  964. $deliveryRaw = trim((string)($d['delivery'] ?? ''));
  965. $deliveryShow = $this->formatDeliveryYmd($deliveryRaw);
  966. if ($deliveryShow === '' && $deliveryRaw !== '') {
  967. $deliveryShow = $deliveryRaw;
  968. }
  969. $amountFilled = ($am !== '' && $am !== '0' && $am !== '0.00');
  970. $deliveryFilled = ($deliveryShow !== '');
  971. $byCompany[$cn]['lines'][] = [
  972. 'cgymc' => $gymc,
  973. 'amount' => $am,
  974. 'amount_show' => $amountFilled ? $am : '',
  975. 'amount_filled' => $amountFilled,
  976. 'delivery' => $deliveryRaw,
  977. 'delivery_show' => $deliveryFilled ? $deliveryShow : '',
  978. 'delivery_filled' => $deliveryFilled,
  979. 'status_name' => trim((string)($d['status_name'] ?? '')),
  980. 'is_quoted' => $amountFilled,
  981. 'quote_label' => $amountFilled ? '已报价' : '未报价',
  982. ];
  983. }
  984. foreach ($byCompany as $cn => $g) {
  985. $total = count($g['lines']);
  986. $quoted = 0;
  987. foreach ($g['lines'] as $ln) {
  988. $am = trim((string)($ln['amount'] ?? ''));
  989. if ($am !== '' && $am !== '0' && $am !== '0.00') {
  990. $quoted++;
  991. }
  992. }
  993. $byCompany[$cn]['has_quote'] = $total > 0 && $quoted === $total;
  994. }
  995. return array_values($byCompany);
  996. }
  997. /**
  998. * purchase_order 表行 → 列表/弹窗用的工序行(用表内已有字段,不依赖 row_json)
  999. *
  1000. * @param array<string, mixed> $dbRow
  1001. * @param array<int, mixed> $dStampMap scydgy_id => dStamp
  1002. */
  1003. protected function buildListRowFromPurchaseOrderDbRow(array $dbRow, array $dStampMap = []): array
  1004. {
  1005. $sid = (int)($dbRow['scydgy_id'] ?? 0);
  1006. $r = [
  1007. 'ID' => $sid,
  1008. 'CCYDH' => $dbRow['CCYDH'] ?? '',
  1009. 'CYJMC' => $dbRow['CYJMC'] ?? '',
  1010. 'CDXMC' => $dbRow['CDXMC'] ?? '',
  1011. 'CGYBH' => $dbRow['CGYBH'] ?? '',
  1012. 'CGYMC' => $dbRow['CGYMC'] ?? '',
  1013. 'CDW' => $dbRow['CDW'] ?? '',
  1014. 'NGZL' => $dbRow['NGZL'] ?? '',
  1015. 'CDF' => $dbRow['CDF'] ?? '',
  1016. 'cGzzxMc' => $dbRow['cGzzxMc'] ?? '',
  1017. 'MBZ' => $dbRow['MBZ'] ?? '',
  1018. 'bwjg' => $dbRow['bwjg'] ?? '',
  1019. 'iStatus' => $dbRow['iStatus'] ?? '',
  1020. 'dputrecord' => $dbRow['dputrecord'] ?? '',
  1021. 'cywyxm' => $dbRow['cywyxm'] ?? '',
  1022. 'This_quantity' => $dbRow['This_quantity'] ?? $dbRow['this_quantity'] ?? '',
  1023. 'ceilingPrice' => $dbRow['ceilingPrice'] ?? $dbRow['ceiling_price'] ?? '',
  1024. 'dStamp' => $dbRow['dStamp'] ?? '',
  1025. 'pick_time' => $dbRow['pick_time'] ?? '',
  1026. ];
  1027. if ($sid > 0 && isset($dStampMap[$sid])) {
  1028. $t = trim((string)$dStampMap[$sid]);
  1029. if ($t !== '' && stripos($t, '0000-00-00') !== 0) {
  1030. $r['dStamp'] = $dStampMap[$sid];
  1031. }
  1032. }
  1033. $dsOut = trim((string)($r['dStamp'] ?? ''));
  1034. if (($dsOut === '' || stripos($dsOut, '0000-00-00') === 0) && !empty($dbRow['createtime'])) {
  1035. $ct = $dbRow['createtime'];
  1036. if (is_numeric($ct) && (int)$ct > 946684800) {
  1037. $r['dStamp'] = date('Y-m-d H:i:s', (int)$ct);
  1038. } elseif (is_string($ct) && trim($ct) !== '' && stripos(trim($ct), '0000-00-00') !== 0) {
  1039. $r['dStamp'] = trim($ct);
  1040. }
  1041. }
  1042. // 列表月份筛选读 dputrecord;手工行无 ERP 提交日时回退 dStamp/createtime
  1043. $dpOut = trim((string)($r['dputrecord'] ?? ''));
  1044. if ($dpOut === '' || stripos($dpOut, '0000-00-00') === 0) {
  1045. $fallback = trim((string)($r['dStamp'] ?? ''));
  1046. if ($fallback !== '' && stripos($fallback, '0000-00-00') !== 0) {
  1047. $r['dputrecord'] = $fallback;
  1048. }
  1049. }
  1050. if (array_key_exists('id', $dbRow)) {
  1051. $r['purchase_order_id'] = (int)$dbRow['id'];
  1052. }
  1053. $r['createtime'] = $this->formatProcuremenDetailTime($dbRow['createtime'] ?? null);
  1054. $pickNm = trim((string)($dbRow['pick_company_name'] ?? ''));
  1055. if ($pickNm !== '') {
  1056. $r['pick_company_name'] = $pickNm;
  1057. $r['picked_supplier_name'] = $pickNm;
  1058. }
  1059. return $r;
  1060. }
  1061. /**
  1062. * 与列表「已下发 / 已选中 / 已完结」同源:将 purchase_order 行还原为工序列表行
  1063. *
  1064. * @return array<int, array<string, mixed>>
  1065. */
  1066. protected function procuremenPoolFromPurchaseOrderDbRows(array $dbRows): array
  1067. {
  1068. $pool = [];
  1069. if (!is_array($dbRows) || $dbRows === []) {
  1070. return $pool;
  1071. }
  1072. $dStampMap = [];
  1073. $sidList = [];
  1074. foreach ($dbRows as $tmpRow) {
  1075. if (is_array($tmpRow) && isset($tmpRow['scydgy_id'])) {
  1076. $sid = (int)$tmpRow['scydgy_id'];
  1077. if ($sid > 0) {
  1078. $sidList[$sid] = true;
  1079. }
  1080. }
  1081. }
  1082. if ($sidList !== []) {
  1083. try {
  1084. $dStampMap = Db::table('scydgy')->where('ID', 'in', array_keys($sidList))->column('dStamp', 'ID');
  1085. } catch (\Throwable $e) {
  1086. $dStampMap = [];
  1087. }
  1088. }
  1089. foreach ($dbRows as $dbRow) {
  1090. if (!is_array($dbRow)) {
  1091. continue;
  1092. }
  1093. $pool[] = $this->buildListRowFromPurchaseOrderDbRow($dbRow, $dStampMap);
  1094. }
  1095. return $pool;
  1096. }
  1097. /**
  1098. * 已下发列表汇总:按工序行 ID 统计 purchase_order_detail 条数、已填金额条数、已填货期条数
  1099. *
  1100. * @param int[] $scydgyIds
  1101. * @return array<int, array{cnt:int, amt:int, deliv:int}>
  1102. */
  1103. protected function OrderDetailSummary(array $scydgyIds)
  1104. {
  1105. $out = [];
  1106. $scydgyIds = array_values(array_unique(array_filter(array_map('intval', $scydgyIds))));
  1107. if ($scydgyIds === []) {
  1108. return $out;
  1109. }
  1110. $list = [];
  1111. try {
  1112. $list = Db::table('purchase_order_detail')
  1113. ->where('scydgy_id', 'in', $scydgyIds)
  1114. ->field('id,scydgy_id,amount,delivery')
  1115. ->select();
  1116. } catch (\Throwable $e) {
  1117. $list = [];
  1118. }
  1119. if (!is_array($list)) {
  1120. return $out;
  1121. }
  1122. foreach ($list as $r) {
  1123. if (!is_array($r)) {
  1124. continue;
  1125. }
  1126. $ids = (int)($r['scydgy_id'] ?? 0);
  1127. if (!$this->isValidScydgyRowId($ids)) {
  1128. continue;
  1129. }
  1130. if (!isset($out[$ids])) {
  1131. $out[$ids] = ['cnt' => 0, 'amt' => 0, 'deliv' => 0];
  1132. }
  1133. $out[$ids]['cnt']++;
  1134. $am = $r['amount'] ?? null;
  1135. if ($am !== null && $am !== '') {
  1136. if (!(is_string($am) && trim($am) === '')) {
  1137. $out[$ids]['amt']++;
  1138. }
  1139. }
  1140. $dv = $r['delivery'] ?? null;
  1141. if ($dv !== null && trim((string)$dv) !== '') {
  1142. $out[$ids]['deliv']++;
  1143. }
  1144. }
  1145. return $out;
  1146. }
  1147. /**
  1148. * 列表筛选:月份按 dputrecord(提交日期)、快速搜索、Bootstrap Table filter
  1149. *
  1150. * @return array
  1151. */
  1152. protected function filterProcuremenIndexPool(array $pool, $monthStart, $monthEnd, $applyMonthRange, $search, array $filterArr, array $opArr)
  1153. {
  1154. $strContains = function ($haystack, $needle) {
  1155. if ($needle === '') {
  1156. return false;
  1157. }
  1158. $haystack = (string)$haystack;
  1159. if (function_exists('mb_stripos')) {
  1160. return mb_stripos($haystack, $needle, 0, 'UTF-8') !== false;
  1161. }
  1162. return stripos($haystack, $needle) !== false;
  1163. };
  1164. $stripAlias = function ($f) {
  1165. return preg_replace('/^[ab]\./i', '', (string)$f);
  1166. };
  1167. $searchColNames = [];
  1168. foreach (explode(',', $this->searchFields) as $colRaw) {
  1169. $c = $stripAlias(trim($colRaw));
  1170. if ($c !== '') {
  1171. $searchColNames[] = $c;
  1172. }
  1173. }
  1174. $filtered = [];
  1175. foreach ($pool as $r) {
  1176. if (!is_array($r)) {
  1177. continue;
  1178. }
  1179. $ds = isset($r['dputrecord']) ? trim((string)$r['dputrecord']) : '';
  1180. if ($ds === '' || stripos($ds, '0000-00-00') === 0) {
  1181. $ds = isset($r['dStamp']) ? trim((string)$r['dStamp']) : '';
  1182. }
  1183. $isManual = !empty($r['_is_manual']);
  1184. if (!$isManual && ($ds === '' || stripos($ds, '0000-00-00') === 0
  1185. || !preg_match('/^([12]\d{3})-(\d{1,2})-(\d{1,2})/', $ds, $m))) {
  1186. continue;
  1187. }
  1188. if ($applyMonthRange && !$isManual) {
  1189. $mo = (int)$m[2];
  1190. if ($mo < 1 || $mo > 12) {
  1191. continue;
  1192. }
  1193. $ymRow = $m[1] . '-' . str_pad((string)$mo, 2, '0', STR_PAD_LEFT);
  1194. $rowMonthStart = $ymRow . '-01 00:00:00';
  1195. $rowMonthEnd = date('Y-m-t 23:59:59', strtotime($rowMonthStart));
  1196. if (strcmp($rowMonthEnd, $monthStart) < 0 || strcmp($rowMonthStart, $monthEnd) > 0) {
  1197. continue;
  1198. }
  1199. }
  1200. if ($search !== '') {
  1201. $hitSearch = false;
  1202. foreach ($searchColNames as $c) {
  1203. $cell = isset($r[$c]) ? (string)$r[$c] : '';
  1204. if ($cell !== '' && $strContains($cell, $search)) {
  1205. $hitSearch = true;
  1206. break;
  1207. }
  1208. }
  1209. if (!$hitSearch) {
  1210. continue;
  1211. }
  1212. }
  1213. foreach ($filterArr as $fk => $fv) {
  1214. if (!preg_match('/^[a-zA-Z0-9_\-\.]+$/', (string)$fk)) {
  1215. continue;
  1216. }
  1217. if (is_array($fv)) {
  1218. continue;
  1219. }
  1220. if ($fv === '' && $fv !== '0' && $fv !== 0) {
  1221. continue;
  1222. }
  1223. $sym = strtoupper(trim((string)($opArr[$fk] ?? '=')));
  1224. $sym = str_replace(['LIKE %...%', 'NOT LIKE %...%'], ['LIKE', 'NOT LIKE'], $sym);
  1225. $field = $stripAlias($fk);
  1226. $cell = array_key_exists($field, $r) ? $r[$field] : null;
  1227. $cellStr = trim((string)$cell);
  1228. switch ($sym) {
  1229. case '=':
  1230. if ((string)$cell !== (string)$fv) {
  1231. continue 3;
  1232. }
  1233. break;
  1234. case '<>':
  1235. if ((string)$cell === (string)$fv) {
  1236. continue 3;
  1237. }
  1238. break;
  1239. case 'LIKE':
  1240. case 'NOT LIKE':
  1241. $needle = trim((string)$fv);
  1242. $hit = ($needle !== '' && $strContains($cellStr, $needle));
  1243. if ($sym === 'LIKE' && !$hit) {
  1244. continue 3;
  1245. }
  1246. if ($sym === 'NOT LIKE' && $hit) {
  1247. continue 3;
  1248. }
  1249. break;
  1250. case '>':
  1251. case '>=':
  1252. case '<':
  1253. case '<=':
  1254. if (!is_numeric($cell) && $cellStr === '') {
  1255. continue 3;
  1256. }
  1257. $cv = is_numeric($cell) ? (float)$cell : (float)$cellStr;
  1258. $vv = (float)$fv;
  1259. if ($sym === '>' && !($cv > $vv)) {
  1260. continue 3;
  1261. }
  1262. if ($sym === '>=' && !($cv >= $vv)) {
  1263. continue 3;
  1264. }
  1265. if ($sym === '<' && !($cv < $vv)) {
  1266. continue 3;
  1267. }
  1268. if ($sym === '<=' && !($cv <= $vv)) {
  1269. continue 3;
  1270. }
  1271. break;
  1272. case 'BETWEEN':
  1273. case 'NOT BETWEEN':
  1274. case 'BETWEEN TIME':
  1275. case 'NOT BETWEEN TIME':
  1276. $rawR = is_array($fv) ? implode(',', $fv) : (string)$fv;
  1277. $rawR = str_replace(' - ', ',', $rawR);
  1278. $arr = array_slice(array_map('trim', explode(',', $rawR)), 0, 2);
  1279. if (count($arr) < 2 || $arr[0] === '' || $arr[1] === '') {
  1280. break;
  1281. }
  1282. $lo = $arr[0];
  1283. $hi = $arr[1];
  1284. $in = ($cellStr >= $lo && $cellStr <= $hi);
  1285. $isNot = (strpos($sym, 'NOT') !== false);
  1286. if ($isNot ? $in : !$in) {
  1287. continue 3;
  1288. }
  1289. break;
  1290. case 'NULL':
  1291. case 'IS NULL':
  1292. if ($cell !== null && $cell !== '') {
  1293. continue 3;
  1294. }
  1295. break;
  1296. case 'NOT NULL':
  1297. case 'IS NOT NULL':
  1298. if ($cell === null || $cell === '') {
  1299. continue 3;
  1300. }
  1301. break;
  1302. default:
  1303. break;
  1304. }
  1305. }
  1306. $filtered[] = $r;
  1307. }
  1308. return $filtered;
  1309. }
  1310. /**
  1311. * 查询订单 purchase_order 数据查有无记录,无则插、有则改(全部写在本方法内)
  1312. */
  1313. public function snapshotToProcure()
  1314. {
  1315. if (!$this->request->isPost() || !$this->request->isAjax()) {
  1316. $this->error(__('Invalid parameters'));
  1317. }
  1318. $row = json_decode($this->request->post('row_json', ''), true);
  1319. if (!is_array($row)) {
  1320. $this->error(__('Invalid parameters'));
  1321. }
  1322. try {
  1323. $ids = $this->extractScydgyRowId($row);
  1324. if (!$this->isValidScydgyRowId($ids)) {
  1325. throw new \Exception('无效的行主键 ID');
  1326. }
  1327. $exists = Db::table('purchase_order')->where('scydgy_id', $ids)->find();
  1328. $data = [
  1329. 'scydgy_id' => $ids,
  1330. 'CCYDH' => $row['CCYDH'] ?? null,
  1331. 'CYJMC' => $row['CYJMC'] ?? null,
  1332. 'CDXMC' => $row['CDXMC'] ?? null,
  1333. 'CGYBH' => $row['CGYBH'] ?? null,
  1334. 'CGYMC' => $row['CGYMC'] ?? null,
  1335. 'CDW' => $row['CDW'] ?? null,
  1336. 'NGZL' => $row['NGZL'] ?? null,
  1337. 'CDF' => $row['CDF'] ?? null,
  1338. 'cGzzxMc' => $row['cGzzxMc'] ?? null,
  1339. 'MBZ' => $row['MBZ'] ?? null,
  1340. 'bwjg' => $row['bwjg'] ?? null,
  1341. 'iStatus' => $row['iStatus'] ?? null,
  1342. 'dStamp' => $row['dStamp'] ?? null,
  1343. 'dputrecord' => $row['dputrecord'] ?? null,
  1344. 'cywyxm' => $row['cywyxm'] ?? null,
  1345. 'This_quantity' => $row['This_quantity'] ?? $row['this_quantity'] ?? null,
  1346. 'ceilingPrice' => $row['ceilingPrice'] ?? $row['ceiling_price'] ?? null,
  1347. ];
  1348. $qtySnap = trim((string)($data['This_quantity'] ?? ''));
  1349. $priceSnap = trim((string)($data['ceilingPrice'] ?? ''));
  1350. if (!$exists && $qtySnap === '' && $priceSnap === '') {
  1351. $this->success('操作成功');
  1352. return;
  1353. }
  1354. if ($exists) {
  1355. $upd = $data;
  1356. unset($upd['scydgy_id'], $upd['status']);
  1357. Db::table('purchase_order')->where('scydgy_id', $ids)->update($upd);
  1358. } else {
  1359. // 新增:不写 status(空/走库默认);仅写创建时间
  1360. $data['createtime'] = date('Y-m-d H:i:s');
  1361. Db::table('purchase_order')->insert($data);
  1362. }
  1363. } catch (\Throwable $e) {
  1364. $this->error($e->getMessage());
  1365. }
  1366. // 操作记录仅在「审核提交」时写入(review POST + addOrderLog),此处同步不写日志,避免与下发记录重复
  1367. $this->success('操作成功');
  1368. }
  1369. /**
  1370. * 未发列表
  1371. * 「完结」或「仅保存本次数量/最高限价」:有则改、无则插
  1372. * POST finish=1(默认):并置 status=1;finish=0:更新时不改 status;新增不写 status。
  1373. */
  1374. public function completeDirectly()
  1375. {
  1376. if (!$this->request->isPost() || !$this->request->isAjax()) {
  1377. $this->error(__('参数错误'));
  1378. }
  1379. $row = json_decode($this->request->post('row_json', ''), true);
  1380. if (!is_array($row)) {
  1381. $this->error(__('Invalid parameters'));
  1382. }
  1383. $finishRaw = $this->request->post('finish', '1');
  1384. $asComplete = ($finishRaw === '1' || $finishRaw === 1 || $finishRaw === true);
  1385. try {
  1386. // 获取工序行 ID,对应 purchase_order.scydgy_id;有则改、无则插
  1387. $ids = $this->extractScydgyRowId($row);
  1388. if (!$this->isValidScydgyRowId($ids)) {
  1389. throw new \Exception('无效的行主键 ID');
  1390. }
  1391. $exists = Db::table('purchase_order')->where('scydgy_id', $ids)->find();
  1392. $data = [
  1393. 'scydgy_id' => $ids,
  1394. 'CCYDH' => $row['CCYDH'] ?? null,
  1395. 'CYJMC' => $row['CYJMC'] ?? null,
  1396. 'CDXMC' => $row['CDXMC'] ?? null,
  1397. 'CGYBH' => $row['CGYBH'] ?? null,
  1398. 'CGYMC' => $row['CGYMC'] ?? null,
  1399. 'CDW' => $row['CDW'] ?? null,
  1400. 'NGZL' => $row['NGZL'] ?? null,
  1401. 'CDF' => $row['CDF'] ?? null,
  1402. 'cGzzxMc' => $row['cGzzxMc'] ?? null,
  1403. 'MBZ' => $row['MBZ'] ?? null,
  1404. 'bwjg' => $row['bwjg'] ?? null,
  1405. 'iStatus' => $row['iStatus'] ?? null,
  1406. 'dStamp' => $row['dStamp'] ?? null,
  1407. 'dputrecord' => $row['dputrecord'] ?? null,
  1408. 'cywyxm' => $row['cywyxm'] ?? null,
  1409. 'This_quantity' => $row['This_quantity'] ?? $row['this_quantity'] ?? null,
  1410. 'ceilingPrice' => $row['ceilingPrice'] ?? $row['ceiling_price'] ?? null,
  1411. ];
  1412. if ($asComplete) {
  1413. // 主表 status 为 varchar 时统一写 '1',档案 procuremenarchive 按 status=1 查询
  1414. $data['status'] = '1';
  1415. }
  1416. $qtyCd = trim((string)($data['This_quantity'] ?? ''));
  1417. $priceCd = trim((string)($data['ceilingPrice'] ?? ''));
  1418. if (!$asComplete && !$exists && $qtyCd === '' && $priceCd === '') {
  1419. $this->success('操作成功');
  1420. return;
  1421. }
  1422. if ($exists) {
  1423. $upd = $data;
  1424. unset($upd['scydgy_id']);
  1425. if (!$asComplete) {
  1426. unset($upd['status']);
  1427. }
  1428. Db::table('purchase_order')->where('scydgy_id', $ids)->update($upd);
  1429. } else {
  1430. // 新增:不写 status;完结时 $data 已含 status=1
  1431. $data['createtime'] = date('Y-m-d H:i:s');
  1432. Db::table('purchase_order')->insert($data);
  1433. }
  1434. } catch (\Throwable $e) {
  1435. $this->error($e->getMessage());
  1436. }
  1437. $poIdLog = null;
  1438. try {
  1439. $rpo = Db::table('purchase_order')->where('scydgy_id', $ids)->find();
  1440. if (is_array($rpo)) {
  1441. $tid = (int)($rpo['id'] ?? $rpo['ID'] ?? 0);
  1442. $poIdLog = $tid > 0 ? $tid : null;
  1443. }
  1444. } catch (\Throwable $e) {
  1445. $poIdLog = null;
  1446. }
  1447. $q = isset($data['This_quantity']) ? trim((string)$data['This_quantity']) : '';
  1448. $p = isset($data['ceilingPrice']) ? trim((string)$data['ceilingPrice']) : '';
  1449. if ($asComplete) {
  1450. $this->addOrderLog(
  1451. $ids,
  1452. 'mark_complete',
  1453. '点击「完结」,已标记为已完结',
  1454. $poIdLog
  1455. );
  1456. try {
  1457. $pdfPublicPath = (string)$this->savePurchaseConfirmDetailPdf($ids, (int)($poIdLog ?? 0));
  1458. if ($pdfPublicPath === '') {
  1459. Log::write('完结存证PDF/OSS未生成 scydgy_id=' . $ids, 'notice');
  1460. }
  1461. } catch (\Throwable $e) {
  1462. Log::write('完结存证PDF异常 scydgy_id=' . $ids . ' ' . $e->getMessage(), 'error');
  1463. }
  1464. } else {
  1465. $this->addOrderLog(
  1466. $ids,
  1467. 'save_qty_price',
  1468. '保存本次数量、最高限价:本次数量「' . ($q !== '' ? $q : '') . '」,最高限价「' . ($p !== '' ? $p : '') . '」',
  1469. $poIdLog
  1470. );
  1471. }
  1472. $this->success("操作成功");
  1473. }
  1474. /**
  1475. * 未发列表:从 purchase_order 合并已填的本次数量、最高限价(若表中有对应列)
  1476. *
  1477. * @param array<int, array> $rows 引用传递当前页行
  1478. */
  1479. protected function mergePurchaseOrder(array &$rows)
  1480. {
  1481. if ($rows === []) {
  1482. return;
  1483. }
  1484. $ids = [];
  1485. foreach ($rows as $rw) {
  1486. if (!is_array($rw)) {
  1487. continue;
  1488. }
  1489. $id = (int)($rw['ID'] ?? 0);
  1490. if ($id > 0) {
  1491. $ids[$id] = true;
  1492. }
  1493. }
  1494. $idList = array_keys($ids);
  1495. if ($idList === []) {
  1496. return;
  1497. }
  1498. try {
  1499. $list = Db::table('purchase_order')
  1500. ->where('scydgy_id', 'in', $idList)
  1501. ->field('scydgy_id,This_quantity,ceilingPrice')
  1502. ->select();
  1503. } catch (\Throwable $e) {
  1504. return;
  1505. }
  1506. if (!is_array($list)) {
  1507. return;
  1508. }
  1509. $byId = [];
  1510. foreach ($list as $dbRow) {
  1511. if (!is_array($dbRow) || !isset($dbRow['scydgy_id'])) {
  1512. continue;
  1513. }
  1514. $byId[(int)$dbRow['scydgy_id']] = $dbRow;
  1515. }
  1516. foreach ($rows as &$rw) {
  1517. if (!is_array($rw)) {
  1518. continue;
  1519. }
  1520. $sid = (int)($rw['ID'] ?? 0);
  1521. if (!$this->isValidScydgyRowId($sid) || !isset($byId[$sid])) {
  1522. continue;
  1523. }
  1524. $db = $byId[$sid];
  1525. if (array_key_exists('This_quantity', $db) && $db['This_quantity'] !== null && $db['This_quantity'] !== '') {
  1526. $rw['This_quantity'] = $db['This_quantity'];
  1527. }
  1528. if (array_key_exists('ceilingPrice', $db) && $db['ceilingPrice'] !== null && $db['ceilingPrice'] !== '') {
  1529. $rw['ceilingPrice'] = $db['ceilingPrice'];
  1530. } elseif (array_key_exists('ceiling_price', $db) && $db['ceiling_price'] !== null && $db['ceiling_price'] !== '') {
  1531. $rw['ceilingPrice'] = $db['ceiling_price'];
  1532. }
  1533. }
  1534. unset($rw);
  1535. }
  1536. /**
  1537. * 采购确认:明细选中 status=1、未选 status=0;同时将 purchase_order 主表 status 置为 1
  1538. */
  1539. public function purchaseConfirmPick()
  1540. {
  1541. if (!$this->request->isPost() || !$this->request->isAjax()) {
  1542. $this->error(__('Invalid parameters'));
  1543. }
  1544. $scydgyId = trim((string)$this->request->post('scydgy_id', ''));
  1545. $sid = (int)$scydgyId;
  1546. if (!$this->isValidScydgyRowId($sid)) {
  1547. $this->error('参数无效');
  1548. }
  1549. $parseIdList = function ($raw) {
  1550. if (is_array($raw)) {
  1551. $arr = $raw;
  1552. } else {
  1553. $decoded = json_decode((string)$raw, true);
  1554. $arr = is_array($decoded) ? $decoded : [];
  1555. }
  1556. return array_values(array_unique(array_filter(array_map('intval', $arr))));
  1557. };
  1558. $selectedRaw = $this->request->post('selected_ids', null);
  1559. if ($selectedRaw === null || $selectedRaw === '') {
  1560. $legacy = (int)$this->request->post('selected_id', 0);
  1561. $selectedIds = $legacy > 0 ? [$legacy] : [];
  1562. } else {
  1563. $selectedIds = $parseIdList($selectedRaw);
  1564. }
  1565. $unselectedIds = $parseIdList($this->request->post('unselected_ids', '[]'));
  1566. if ($selectedIds === []) {
  1567. $this->error('请至少勾选一条明细');
  1568. }
  1569. $inter = array_intersect($selectedIds, $unselectedIds);
  1570. if ($inter !== []) {
  1571. $this->error('选中与未选中 ID 不能重复');
  1572. }
  1573. $allIds = $this->purchaseOrderDetail($sid);
  1574. if ($allIds === []) {
  1575. $this->error('未找到该工序行的下发明细');
  1576. }
  1577. foreach ($selectedIds as $id) {
  1578. if (!in_array($id, $allIds, true)) {
  1579. $this->error('选中 ID 不属于当前工序行');
  1580. }
  1581. }
  1582. foreach ($unselectedIds as $id) {
  1583. if (!in_array($id, $allIds, true)) {
  1584. $this->error('未选中 ID 不属于当前工序行');
  1585. }
  1586. }
  1587. $union = array_values(array_unique(array_merge($selectedIds, $unselectedIds)));
  1588. sort($union, SORT_NUMERIC);
  1589. $expect = $allIds;
  1590. sort($expect, SORT_NUMERIC);
  1591. if ($union !== $expect) {
  1592. $this->error('请提交当前工序下全部明细 ID(选中 + 未选中)');
  1593. }
  1594. $purchaseOrderId = (int)$this->request->post('purchase_order_id', 0);
  1595. Db::startTrans();
  1596. try {
  1597. Db::table('purchase_order_detail')->where('scydgy_id', $sid)->update(['status' => 0]);
  1598. $aff = 0;
  1599. try {
  1600. $aff = (int)Db::table('purchase_order_detail')->where('scydgy_id', $sid)->where('id', 'in', $selectedIds)->update(['status' => 1]);
  1601. } catch (\Throwable $e) {
  1602. $aff = (int)Db::table('purchase_order_detail')->where('scydgy_id', $sid)->where('ID', 'in', $selectedIds)->update(['status' => 1]);
  1603. }
  1604. if ($aff < 1) {
  1605. throw new \Exception('更新选中状态失败');
  1606. }
  1607. if ($purchaseOrderId > 0) {
  1608. $poAff = (int)Db::table('purchase_order')
  1609. ->where('id', $purchaseOrderId)
  1610. ->where('scydgy_id', $sid)
  1611. ->update(['status' => 1]);
  1612. if ($poAff < 1) {
  1613. throw new \Exception('主表订单不存在或与工序行不匹配');
  1614. }
  1615. } else {
  1616. $poAff = (int)Db::table('purchase_order')->where('scydgy_id', $sid)->update(['status' => '1']);
  1617. if ($poAff < 1) {
  1618. throw new \Exception('未找到');
  1619. }
  1620. }
  1621. Db::commit();
  1622. } catch (\Throwable $e) {
  1623. Db::rollback();
  1624. $this->error($e->getMessage());
  1625. }
  1626. $poIdFinal = $purchaseOrderId;
  1627. if ($poIdFinal <= 0) {
  1628. try {
  1629. $rpo = Db::table('purchase_order')->where('scydgy_id', $sid)->find();
  1630. if (is_array($rpo)) {
  1631. $poIdFinal = (int)($rpo['id'] ?? $rpo['ID'] ?? 0);
  1632. }
  1633. } catch (\Throwable $e) {
  1634. $poIdFinal = 0;
  1635. }
  1636. }
  1637. $pickNames = [];
  1638. foreach ($selectedIds as $pid) {
  1639. $dr = $this->purchaseOrderDetai($sid, $pid);
  1640. $nm = trim((string)($dr['company_name'] ?? ''));
  1641. $pickNames[] = $nm !== '' ? $nm : ('明细#' . $pid);
  1642. }
  1643. $this->addOrderLog(
  1644. $sid,
  1645. 'purchase_confirm',
  1646. '采购确认:已选中供应商「' . implode('、', $pickNames) . '」;',
  1647. $poIdFinal > 0 ? $poIdFinal : null
  1648. );
  1649. $ccydh = '';
  1650. $cyjmc = '';
  1651. try {
  1652. $po = Db::table('purchase_order')->where('scydgy_id', $sid)->find();
  1653. if (is_array($po)) {
  1654. $ccydh = trim((string)($po['CCYDH'] ?? ''));
  1655. $cyjmc = trim((string)($po['CYJMC'] ?? ''));
  1656. }
  1657. } catch (\Throwable $e) {
  1658. }
  1659. if ($ccydh === '' || $cyjmc === '') {
  1660. $any = $this->purchaseOrderDetai($sid, $selectedIds[0] ?? 0);
  1661. if (is_array($any)) {
  1662. if ($ccydh === '') {
  1663. $ccydh = trim((string)($any['CCYDH'] ?? ''));
  1664. }
  1665. if ($cyjmc === '') {
  1666. $cyjmc = trim((string)($any['CYJMC'] ?? ''));
  1667. }
  1668. }
  1669. }
  1670. $sendSmsSafe = function ($phone, $content) {
  1671. $phone = trim((string)$phone);
  1672. if ($phone === '') {
  1673. return;
  1674. }
  1675. try {
  1676. $this->smsbao($phone, $content);
  1677. } catch (\Throwable $e) {
  1678. Log::write('采购确认短信失败 phone=' . $phone . ' ' . $e->getMessage(), 'error');
  1679. }
  1680. };
  1681. //批量发送手机短信:采购确认通过
  1682. foreach ($selectedIds as $pid) {
  1683. $dr = $this->purchaseOrderDetai($sid, $pid);
  1684. if (!is_array($dr)) {
  1685. continue;
  1686. }
  1687. $cname = trim((string)($dr['company_name'] ?? ''));
  1688. $ph = trim((string)($dr['phone'] ?? ''));
  1689. $sms = $this->renderNotifyTemplate('confirm_ok', [
  1690. 'company_name' => $cname,
  1691. 'contact_name' => $this->resolveCustomerContactName($ph, $cname),
  1692. 'phone' => $ph,
  1693. 'ccydh' => $ccydh,
  1694. 'cyjmc' => $cyjmc,
  1695. ]);
  1696. $sendSmsSafe((string)($dr['phone'] ?? ''), $sms);
  1697. }
  1698. //批量发送手机短信:采购确认未通过
  1699. foreach ($unselectedIds as $pid) {
  1700. $dr = $this->purchaseOrderDetai($sid, $pid);
  1701. if (!is_array($dr)) {
  1702. continue;
  1703. }
  1704. $cname = trim((string)($dr['company_name'] ?? ''));
  1705. $ph = trim((string)($dr['phone'] ?? ''));
  1706. $sms = $this->renderNotifyTemplate('confirm_fail', [
  1707. 'company_name' => $cname,
  1708. 'contact_name' => $this->resolveCustomerContactName($ph, $cname),
  1709. 'phone' => $ph,
  1710. 'ccydh' => $ccydh,
  1711. 'cyjmc' => $cyjmc,
  1712. ]);
  1713. $sendSmsSafe((string)($dr['phone'] ?? ''), $sms);
  1714. }
  1715. $line = sprintf(
  1716. 'purchaseConfirmPick scydgy_id=%s purchase_order_id=%d selected_ids=%s unselected_ids=%s',
  1717. $scydgyId,
  1718. $purchaseOrderId,
  1719. json_encode($selectedIds, JSON_UNESCAPED_UNICODE),
  1720. json_encode($unselectedIds, JSON_UNESCAPED_UNICODE)
  1721. );
  1722. Log::write($line, 'notice');
  1723. $pdfPublicPath = '';
  1724. try {
  1725. $pdfPublicPath = (string)$this->savePurchaseConfirmDetailPdf($sid, $poIdFinal);
  1726. } catch (\Throwable $e) {
  1727. Log::write('采购确认PDF异常: ' . $e->getMessage(), 'error');
  1728. }
  1729. $this->success('操作成功', '', [
  1730. 'scydgy_id' => $scydgyId,
  1731. 'purchase_order_id' => $purchaseOrderId,
  1732. 'selected_ids' => $selectedIds,
  1733. 'unselected_ids' => $unselectedIds,
  1734. 'purchase_confirm_pdf' => $pdfPublicPath,
  1735. ]);
  1736. }
  1737. /**
  1738. * @return int[]
  1739. */
  1740. protected function purchaseOrderDetail(int $scydgyId): array
  1741. {
  1742. if (!$this->isValidScydgyRowId($scydgyId)) {
  1743. return [];
  1744. }
  1745. try {
  1746. $list = Db::table('purchase_order_detail')->where('scydgy_id', $scydgyId)->select();
  1747. } catch (\Throwable $e) {
  1748. return [];
  1749. }
  1750. if (!is_array($list)) {
  1751. return [];
  1752. }
  1753. $ids = [];
  1754. foreach ($list as $r) {
  1755. if (!is_array($r)) {
  1756. continue;
  1757. }
  1758. $pk = (int)($r['id'] ?? $r['ID'] ?? 0);
  1759. if ($pk > 0) {
  1760. $ids[] = $pk;
  1761. }
  1762. }
  1763. return array_values(array_unique($ids));
  1764. }
  1765. /**
  1766. * 获取当前登录用户信息 [id, 展示名]
  1767. */
  1768. protected function GetUseName(): array
  1769. {
  1770. $id = 0;
  1771. $name = '';
  1772. try {
  1773. if ($this->auth && $this->auth->isLogin()) {
  1774. $u = $this->auth->getUserInfo();
  1775. if (is_array($u)) {
  1776. $id = (int)($u['id'] ?? 0);
  1777. $name = trim((string)($u['nickname'] ?? ''));
  1778. if ($name === '') {
  1779. $name = trim((string)($u['username'] ?? ''));
  1780. }
  1781. }
  1782. }
  1783. } catch (\Throwable $e) {
  1784. }
  1785. if ($name === '') {
  1786. $name = '未知用户';
  1787. }
  1788. return [$id, $name];
  1789. }
  1790. /**
  1791. * 外发采购操作日志(表未建时仅写 runtime 日志,不中断业务)
  1792. */
  1793. protected function addOrderLog(int $scydgyId, string $action, string $content, ?int $purchaseOrderId = null): void
  1794. {
  1795. if (!$this->isValidScydgyRowId($scydgyId) || $content === '') {
  1796. return;
  1797. }
  1798. list($adminId, $adminName) = $this->GetUseName();
  1799. $cut = function ($s, $max) {
  1800. if (function_exists('mb_substr')) {
  1801. return mb_substr($s, 0, $max, 'UTF-8');
  1802. }
  1803. return strlen($s) <= $max ? $s : substr($s, 0, $max);
  1804. };
  1805. try {
  1806. Db::table('purchase_order_oper_log')->insert([
  1807. 'scydgy_id' => $scydgyId,
  1808. 'purchase_order_id' => $purchaseOrderId,
  1809. 'admin_id' => $adminId,
  1810. 'admin_name' => $cut($adminName, 64),
  1811. 'action' => $cut((string)$action, 64),
  1812. 'content' => $cut($content, 1000),
  1813. // 表结构 createtime 为 int Unix 时间戳;日期字符串会导致插入失败或时间为 0,详情「操作记录」为空
  1814. 'createtime' => time(),
  1815. ]);
  1816. } catch (\Throwable $e) {
  1817. Log::write('procuremen addOrderLog: ' . $e->getMessage(), 'error');
  1818. }
  1819. }
  1820. /**
  1821. * 按主键取一条
  1822. */
  1823. protected function purchaseOrderDetai(int $scydgyId, int $pk): array
  1824. {
  1825. if (!$this->isValidScydgyRowId($scydgyId) || $pk <= 0) {
  1826. return [];
  1827. }
  1828. try {
  1829. $one = Db::table('purchase_order_detail')->where('scydgy_id', $scydgyId)->where('id', $pk)->find();
  1830. if (is_array($one)) {
  1831. return $one;
  1832. }
  1833. } catch (\Throwable $e) {
  1834. }
  1835. try {
  1836. $one = Db::table('purchase_order_detail')->where('scydgy_id', $scydgyId)->where('ID', $pk)->find();
  1837. if (is_array($one)) {
  1838. return $one;
  1839. }
  1840. } catch (\Throwable $e) {
  1841. }
  1842. return [];
  1843. }
  1844. /**
  1845. * 主表/明细时间字段统一为可读字符串(用于详情进度)
  1846. */
  1847. protected function formatProcuremenDetailTime($v): string
  1848. {
  1849. if ($v === null || $v === '') {
  1850. return '';
  1851. }
  1852. if (is_numeric($v) && (int)$v > 946684800) {
  1853. return date('Y-m-d H:i:s', (int)$v);
  1854. }
  1855. $s = trim((string)$v);
  1856. if ($s !== '' && stripos($s, '0000-00-00') !== 0) {
  1857. return $s;
  1858. }
  1859. return '';
  1860. }
  1861. /**
  1862. * 交货日期仅展示 YYYY-MM-DD(详情表、列表展示用)
  1863. */
  1864. protected function formatDeliveryYmd($v): string
  1865. {
  1866. if ($v === null || $v === '') {
  1867. return '';
  1868. }
  1869. $s = trim((string)$v);
  1870. if ($s === '' || preg_match('/^0000-00-00/i', $s)) {
  1871. return '';
  1872. }
  1873. if (preg_match('/^(\d{4}-\d{2}-\d{2})/', $s, $m)) {
  1874. return $m[1];
  1875. }
  1876. $ts = strtotime($s);
  1877. return ($ts !== false && $ts > 0) ? date('Y-m-d', $ts) : $s;
  1878. }
  1879. /**
  1880. * 供应商已接单:金额与交货日期均已填写(与列表汇总逻辑一致)
  1881. */
  1882. protected function detailRowSupplierAccepted(array $r): bool
  1883. {
  1884. $am = $r['amount'] ?? null;
  1885. $okAmt = $am !== null && $am !== '' && !(is_string($am) && trim($am) === '');
  1886. $dv = isset($r['delivery']) ? trim((string)$r['delivery']) : '';
  1887. $okDel = $dv !== '' && !preg_match('/^0000-00-00/i', $dv);
  1888. return $okAmt && $okDel;
  1889. }
  1890. /**
  1891. * 详情弹层:进度步骤 + 订单摘要 + 下发明细(已下发 / 已完结均可用)
  1892. *
  1893. * @param array $main purchase_order 一行
  1894. * @param array $details purchase_order_detail 多行(已预处理 createtime_text)
  1895. * @return array{steps: array, orderSummary: array, orderSummaryGrid: array, detailRows: array}
  1896. */
  1897. protected function buildProcuremenDetailsViewData(array $main, array $details): array
  1898. {
  1899. $issueCnt = count($details);
  1900. $acceptCnt = 0;
  1901. $pickedName = '';
  1902. $pickedTime = '';
  1903. foreach ($details as $dr) {
  1904. if (!is_array($dr)) {
  1905. continue;
  1906. }
  1907. if ($this->detailRowSupplierAccepted($dr)) {
  1908. $acceptCnt++;
  1909. }
  1910. if ((int)($dr['status'] ?? 0) === 1) {
  1911. if ($pickedName === '') {
  1912. $pickedName = trim((string)($dr['company_name'] ?? ''));
  1913. }
  1914. if ($pickedTime === '') {
  1915. $pickedTime = trim((string)($dr['createtime_text'] ?? ''));
  1916. if ($pickedTime === '') {
  1917. $pickedTime = $this->formatProcuremenDetailTime($dr['createtime'] ?? null);
  1918. }
  1919. }
  1920. }
  1921. }
  1922. $hasMain = $main !== [];
  1923. $poTime = $this->formatProcuremenDetailTime($main['createtime'] ?? null);
  1924. $mainStatus = isset($main['status']) ? (int)$main['status'] : 0;
  1925. $supplierTime = '';
  1926. if ($acceptCnt > 0) {
  1927. foreach ($details as $dr) {
  1928. if (!is_array($dr) || !$this->detailRowSupplierAccepted($dr)) {
  1929. continue;
  1930. }
  1931. $t = $this->formatProcuremenDetailTime($dr['updatetime'] ?? $dr['createtime'] ?? null);
  1932. if ($t !== '' && ($supplierTime === '' || strcmp($t, $supplierTime) < 0)) {
  1933. $supplierTime = $t;
  1934. }
  1935. }
  1936. }
  1937. $doneTime = '';
  1938. if ($mainStatus === 1) {
  1939. $doneTime = $this->formatProcuremenDetailTime($main['updatetime'] ?? null);
  1940. if ($doneTime === '') {
  1941. foreach ($details as $dr) {
  1942. if (!is_array($dr)) {
  1943. continue;
  1944. }
  1945. $t = $this->formatProcuremenDetailTime($dr['updatetime'] ?? $dr['createtime'] ?? null);
  1946. if ($t !== '' && ($doneTime === '' || strcmp($t, $doneTime) > 0)) {
  1947. $doneTime = $t;
  1948. }
  1949. }
  1950. }
  1951. if ($doneTime === '') {
  1952. $doneTime = $poTime;
  1953. }
  1954. }
  1955. $step1Done = $hasMain;
  1956. $step2Done = $hasMain;
  1957. $step3Done = $acceptCnt > 0;
  1958. $step4Done = false;
  1959. foreach ($details as $dr) {
  1960. if (is_array($dr) && (int)($dr['status'] ?? 0) === 1) {
  1961. $step4Done = true;
  1962. break;
  1963. }
  1964. }
  1965. $step5Done = $mainStatus === 1;
  1966. // 主表已完结:中间环节按业务视为已全部完成(不显示灰色「未到达」)
  1967. if ($mainStatus === 1) {
  1968. $step3Done = true;
  1969. $step4Done = true;
  1970. if ($supplierTime === '') {
  1971. $fillT = $doneTime !== '' ? $doneTime : $poTime;
  1972. if ($fillT !== '') {
  1973. $supplierTime = $fillT;
  1974. }
  1975. }
  1976. if ($pickedTime === '') {
  1977. $fillT = $doneTime !== '' ? $doneTime : $poTime;
  1978. if ($fillT !== '') {
  1979. $pickedTime = $fillT;
  1980. }
  1981. }
  1982. }
  1983. $steps = [
  1984. [
  1985. 'title' => '未发',
  1986. 'subtitle' => '',
  1987. 'time' => $step1Done ? '—' : '',
  1988. 'done' => $step1Done,
  1989. ],
  1990. [
  1991. 'title' => '已发未结束',
  1992. 'subtitle' => '',
  1993. 'time' => $step2Done ? $poTime : '',
  1994. 'done' => $step2Done,
  1995. ],
  1996. [
  1997. 'title' => '供应商接单',
  1998. 'subtitle' => '下发 ' . $issueCnt . ' / 接单 ' . $acceptCnt,
  1999. 'time' => $step3Done ? $supplierTime : '',
  2000. 'done' => $step3Done,
  2001. ],
  2002. [
  2003. 'title' => '采购确认',
  2004. 'subtitle' => $pickedName !== '' ? ('选中供应商:' . $pickedName) : '',
  2005. 'time' => $step4Done ? ($pickedTime !== '' ? $pickedTime : '') : '',
  2006. 'done' => $step4Done,
  2007. ],
  2008. [
  2009. 'title' => '已完结',
  2010. 'subtitle' => '',
  2011. 'time' => $step5Done ? $doneTime : '',
  2012. 'done' => $step5Done,
  2013. ],
  2014. ];
  2015. $currentIdx = count($steps) - 1;
  2016. foreach ($steps as $i => $s) {
  2017. if (!$s['done']) {
  2018. $currentIdx = $i;
  2019. break;
  2020. }
  2021. }
  2022. $nSteps = count($steps);
  2023. foreach ($steps as $idx => &$s) {
  2024. $s['current'] = ($idx === $currentIdx);
  2025. if ($idx === 0) {
  2026. $s['pdf_left_bg'] = '';
  2027. } else {
  2028. $s['pdf_left_bg'] = !empty($steps[$idx - 1]['done']) ? '#1890ff' : '#e0e0e0';
  2029. }
  2030. if ($idx >= $nSteps - 1) {
  2031. $s['pdf_right_bg'] = '';
  2032. } else {
  2033. $s['pdf_right_bg'] = !empty($s['done']) ? '#1890ff' : '#e0e0e0';
  2034. }
  2035. }
  2036. unset($s);
  2037. $labelMap = [
  2038. 'CCYDH' => '订单号',
  2039. 'CYJMC' => '印件名称',
  2040. 'CGYMC' => '工序名称',
  2041. 'CDW' => '单位',
  2042. 'NGZL' => '工作量',
  2043. 'CDF' => '单价',
  2044. 'cGzzxMc' => '工作中心',
  2045. 'MBZ' => '备注',
  2046. 'This_quantity' => '本次数量',
  2047. 'ceilingPrice' => '最高限价',
  2048. ];
  2049. $orderSummary = [];
  2050. foreach ($labelMap as $key => $lab) {
  2051. if (!array_key_exists($key, $main)) {
  2052. continue;
  2053. }
  2054. $val = $main[$key];
  2055. if ($val === null) {
  2056. $val = '';
  2057. } elseif (!is_scalar($val)) {
  2058. $val = json_encode($val, JSON_UNESCAPED_UNICODE);
  2059. } else {
  2060. $val = (string)$val;
  2061. }
  2062. $orderSummary[] = ['label' => $lab, 'value' => $val];
  2063. }
  2064. $orderSummaryGrid = [];
  2065. $n = count($orderSummary);
  2066. for ($i = 0; $i < $n; $i += 2) {
  2067. $left = $orderSummary[$i];
  2068. $hasRight = ($i + 1) < $n;
  2069. $orderSummaryGrid[] = [
  2070. 'l1' => $left['label'],
  2071. 'v1' => $left['value'],
  2072. 'l2' => $hasRight ? $orderSummary[$i + 1]['label'] : '',
  2073. 'v2' => $hasRight ? $orderSummary[$i + 1]['value'] : '',
  2074. ];
  2075. }
  2076. return [
  2077. 'steps' => $steps,
  2078. 'orderSummary' => $orderSummary,
  2079. 'orderSummaryGrid' => $orderSummaryGrid,
  2080. 'detailRows' => $details,
  2081. ];
  2082. }
  2083. /**
  2084. * 加载并 assign 外发采购「详情」弹窗所需变量(与 {@see details()} 页面一致,供模板 / PDF 共用)。
  2085. *
  2086. * @return array{ok: bool, ccydh: string} ok 表示存在主表或至少一条下发明细(否则 PDF 无可写内容)
  2087. */
  2088. protected function prepareProcuremenDetailsView(string $ids): array
  2089. {
  2090. $main = [];
  2091. try {
  2092. $one = Db::table('purchase_order')->where('scydgy_id', $ids)->find();
  2093. $main = is_array($one) ? $one : [];
  2094. } catch (\Throwable $e) {
  2095. $main = [];
  2096. }
  2097. $details = [];
  2098. try {
  2099. $details = Db::table('purchase_order_detail')->where('scydgy_id', $ids)->order('id', 'asc')->select();
  2100. } catch (\Throwable $e) {
  2101. $details = [];
  2102. }
  2103. if (!is_array($details)) {
  2104. $details = [];
  2105. }
  2106. foreach ($details as &$r) {
  2107. if (is_array($r) && isset($r['ID']) && !isset($r['id'])) {
  2108. $r['id'] = $r['ID'];
  2109. }
  2110. if (isset($r['createtime'])) {
  2111. if (is_numeric($r['createtime']) && (int)$r['createtime'] > 946684800) {
  2112. $r['createtime_text'] = date('Y-m-d H:i:s', (int)$r['createtime']);
  2113. } else {
  2114. $r['createtime_text'] = (string)$r['createtime'];
  2115. }
  2116. } else {
  2117. $r['createtime_text'] = '';
  2118. }
  2119. $r['delivery_ymd'] = $this->formatDeliveryYmd($r['delivery'] ?? null);
  2120. }
  2121. unset($r);
  2122. $operLogs = [];
  2123. try {
  2124. $operLogs = Db::table('purchase_order_oper_log')->where('scydgy_id', $ids)->order('id', 'asc')->select();
  2125. } catch (\Throwable $e) {
  2126. $operLogs = [];
  2127. }
  2128. if (!is_array($operLogs)) {
  2129. $operLogs = [];
  2130. }
  2131. foreach ($operLogs as &$lg) {
  2132. if (!is_array($lg)) {
  2133. continue;
  2134. }
  2135. $ct = isset($lg['createtime']) ? (int)$lg['createtime'] : 0;
  2136. $lg['createtime_text'] = $ct > 946684800 ? date('Y-m-d H:i:s', $ct) : '';
  2137. }
  2138. unset($lg);
  2139. $bundle = $this->buildProcuremenDetailsViewData($main, $details);
  2140. $ccydh = isset($main['CCYDH']) ? trim((string)$main['CCYDH']) : '';
  2141. if ($ccydh === '') {
  2142. foreach ($details as $r) {
  2143. if (!is_array($r)) {
  2144. continue;
  2145. }
  2146. $ccydh = trim((string)($r['CCYDH'] ?? ''));
  2147. if ($ccydh !== '') {
  2148. break;
  2149. }
  2150. }
  2151. }
  2152. $this->view->assign('ccydh', $ccydh);
  2153. $this->view->assign('steps', $bundle['steps']);
  2154. $this->view->assign('orderSummary', $bundle['orderSummary']);
  2155. $this->view->assign('orderSummaryGrid', $bundle['orderSummaryGrid']);
  2156. $this->view->assign('detailRows', $bundle['detailRows']);
  2157. $this->view->assign('operLogs', $operLogs);
  2158. return [
  2159. 'ok' => $main !== [] || $details !== [],
  2160. 'ccydh' => $ccydh,
  2161. ];
  2162. }
  2163. /**
  2164. * 详情弹层:状态进度 + 订单信息 + 下发明细(列表「已下发」「已完结」均可打开)
  2165. */
  2166. public function details()
  2167. {
  2168. $ids = $this->request->param('ids', $this->request->param('id', ''));
  2169. if (is_array($ids)) {
  2170. $ids = isset($ids[0]) ? $ids[0] : '';
  2171. }
  2172. $ids = trim((string)$ids);
  2173. if ($ids === '') {
  2174. $this->error(__('Invalid parameters'));
  2175. }
  2176. $this->prepareProcuremenDetailsView($ids);
  2177. /* 弹层内不套 default 布局,避免出现「控制台 / Control panel」整块标题区 */
  2178. $restoreLayout = !empty($this->layout) ? ('layout/' . $this->layout) : false;
  2179. $this->view->engine->layout(false);
  2180. try {
  2181. return $this->view->fetch('procuremen/details_dialog_shell');
  2182. } finally {
  2183. if ($restoreLayout) {
  2184. $this->view->engine->layout($restoreLayout);
  2185. }
  2186. }
  2187. }
  2188. /**
  2189. * 解析合并审核工序行(订单号须一致,且均未外发)
  2190. *
  2191. * @return array<int, array<string, mixed>>
  2192. */
  2193. protected function parseReviewMergeRows(string $rowJson, string $mergeRowsJson): array
  2194. {
  2195. $primary = json_decode($rowJson, true);
  2196. if (!is_array($primary)) {
  2197. $this->error(__('Invalid parameters'));
  2198. }
  2199. $merge = json_decode($mergeRowsJson, true);
  2200. if (!is_array($merge) || $merge === []) {
  2201. $merge = [$primary];
  2202. }
  2203. $seen = [];
  2204. $out = [];
  2205. foreach ($merge as $r) {
  2206. if (!is_array($r)) {
  2207. continue;
  2208. }
  2209. $id = (int)($r['ID'] ?? $r['id'] ?? 0);
  2210. if ($id <= 0 || isset($seen[$id])) {
  2211. continue;
  2212. }
  2213. $seen[$id] = true;
  2214. $out[] = $r;
  2215. }
  2216. if ($out === []) {
  2217. $this->error('无效的工序行');
  2218. }
  2219. $ccydh = null;
  2220. foreach ($out as $r) {
  2221. $dh = trim((string)($r['CCYDH'] ?? ''));
  2222. if ($ccydh === null) {
  2223. $ccydh = $dh;
  2224. } elseif ($dh !== $ccydh) {
  2225. $this->error('合并审核要求所选行的订单号一致');
  2226. }
  2227. }
  2228. foreach ($out as $r) {
  2229. $id = (int)($r['ID'] ?? $r['id'] ?? 0);
  2230. if ($id <= 0) {
  2231. continue;
  2232. }
  2233. try {
  2234. $po = Db::table('purchase_order')->where('scydgy_id', $id)->find();
  2235. } catch (\Throwable $e) {
  2236. $po = null;
  2237. }
  2238. if (!is_array($po)) {
  2239. continue;
  2240. }
  2241. $wf = (int)($po['wflow_status'] ?? 0);
  2242. $st = $po['status'] ?? null;
  2243. if ($wf >= 1 || $st === 1 || $st === '1') {
  2244. $gymc = trim((string)($r['CGYMC'] ?? ''));
  2245. $this->error('工序「' . ($gymc !== '' ? $gymc : ('#' . $id)) . '」已进入审批流程,不能重复下发');
  2246. }
  2247. try {
  2248. $detCnt = (int)Db::table('purchase_order_detail')->where('scydgy_id', $id)->count();
  2249. } catch (\Throwable $e) {
  2250. $detCnt = 0;
  2251. }
  2252. if ($detCnt > 0 && $wf < 1) {
  2253. $gymc = trim((string)($r['CGYMC'] ?? ''));
  2254. $this->error('工序「' . ($gymc !== '' ? $gymc : ('#' . $id)) . '」已存在下发明细,请走采购确认或联系管理员');
  2255. }
  2256. }
  2257. return $out;
  2258. }
  2259. /**
  2260. * 规范化下发/确认时勾选的供应商
  2261. *
  2262. * @param array<int, mixed> $companies
  2263. * @return array<int, array<string, string>>
  2264. */
  2265. protected function normalizePickCompanies(array $companies): array
  2266. {
  2267. $out = [];
  2268. foreach ($companies as $c) {
  2269. if (!is_array($c)) {
  2270. continue;
  2271. }
  2272. $name = trim((string)($c['name'] ?? $c['company_name'] ?? ''));
  2273. if ($name === '') {
  2274. continue;
  2275. }
  2276. $out[] = [
  2277. 'name' => $name,
  2278. 'company_name' => $name,
  2279. 'username' => trim((string)($c['username'] ?? '')),
  2280. 'email' => trim((string)($c['email'] ?? '')),
  2281. 'phone' => trim((string)($c['phone'] ?? '')),
  2282. 'company_type' => trim((string)($c['company_type'] ?? $c['category'] ?? '')),
  2283. ];
  2284. }
  2285. return $out;
  2286. }
  2287. /**
  2288. * 确认供应商列表:按订单号 CCYDH 合并为一行(一单一行,工序汇总)
  2289. *
  2290. * @param array<int, array<string, mixed>> $pool
  2291. * @return array<int, array<string, mixed>>
  2292. */
  2293. protected function collapseProcuremenPoolByOrder(array $pool): array
  2294. {
  2295. if ($pool === []) {
  2296. return [];
  2297. }
  2298. $groups = [];
  2299. foreach ($pool as $row) {
  2300. if (!is_array($row)) {
  2301. continue;
  2302. }
  2303. $dh = trim((string)($row['CCYDH'] ?? ''));
  2304. $key = $dh !== '' ? $dh : ('_id_' . (int)($row['ID'] ?? 0));
  2305. if (!isset($groups[$key])) {
  2306. $groups[$key] = [];
  2307. }
  2308. $groups[$key][] = $row;
  2309. }
  2310. $out = [];
  2311. foreach ($groups as $ccydh => $rows) {
  2312. if ($rows === []) {
  2313. continue;
  2314. }
  2315. usort($rows, function ($a, $b) {
  2316. return ((int)($a['ID'] ?? 0)) <=> ((int)($b['ID'] ?? 0));
  2317. });
  2318. $head = $rows[0];
  2319. $gymcList = [];
  2320. foreach ($rows as $r) {
  2321. $g = trim((string)($r['CGYMC'] ?? ''));
  2322. if ($g !== '' && !in_array($g, $gymcList, true)) {
  2323. $gymcList[] = $g;
  2324. }
  2325. }
  2326. $merged = $head;
  2327. $merged['order_key'] = strpos((string)$ccydh, '_id_') === 0 ? '' : (string)$ccydh;
  2328. $merged['process_count'] = count($rows);
  2329. $merged['_order_merge_rows'] = $rows;
  2330. if (count($rows) > 1) {
  2331. $merged['CGYMC'] = implode('、', $gymcList);
  2332. }
  2333. $mergedPickTime = '';
  2334. foreach ($rows as $r) {
  2335. $t = $this->procuremenRowListSortTime($r, 'pick_time');
  2336. if ($t !== '' && ($mergedPickTime === '' || strcmp($t, $mergedPickTime) > 0)) {
  2337. $mergedPickTime = $t;
  2338. }
  2339. }
  2340. if ($mergedPickTime !== '') {
  2341. $merged['pick_time'] = $mergedPickTime;
  2342. }
  2343. $out[] = $merged;
  2344. }
  2345. usort($out, function ($a, $b) {
  2346. $ta = $this->procuremenRowListSortTime($a, 'pick_time');
  2347. $tb = $this->procuremenRowListSortTime($b, 'pick_time');
  2348. if ($ta === $tb) {
  2349. $ida = (int)($a['purchase_order_id'] ?? 0);
  2350. $idb = (int)($b['purchase_order_id'] ?? 0);
  2351. if ($ida !== $idb) {
  2352. return $idb <=> $ida;
  2353. }
  2354. return strcmp((string)($b['CCYDH'] ?? ''), (string)($a['CCYDH'] ?? ''));
  2355. }
  2356. if ($ta === '') {
  2357. return 1;
  2358. }
  2359. if ($tb === '') {
  2360. return -1;
  2361. }
  2362. return strcmp($tb, $ta);
  2363. });
  2364. return $out;
  2365. }
  2366. /**
  2367. * 待确认供应商订单下全部工序(同一 CCYDH,wflow_status=1)
  2368. *
  2369. * @return array{ccydh:string, pos:array<int,array>, merge_rows:array<int,array>}
  2370. */
  2371. protected function loadAuditOrderBundleByScydgyId(string $scydgyId): array
  2372. {
  2373. $scydgyId = trim($scydgyId);
  2374. $empty = ['ccydh' => '', 'pos' => [], 'merge_rows' => []];
  2375. if ($scydgyId === '') {
  2376. return $empty;
  2377. }
  2378. try {
  2379. $anchor = Db::table('purchase_order')->where('scydgy_id', $scydgyId)->find();
  2380. } catch (\Throwable $e) {
  2381. $anchor = null;
  2382. }
  2383. if (!is_array($anchor) || (int)($anchor['wflow_status'] ?? 0) !== 1) {
  2384. return $empty;
  2385. }
  2386. $ccydh = trim((string)($anchor['CCYDH'] ?? ''));
  2387. if ($ccydh === '') {
  2388. return ['ccydh' => '', 'pos' => [$anchor], 'merge_rows' => $this->scydgyRowsForPurchaseOrders([$anchor])];
  2389. }
  2390. try {
  2391. $pos = Db::table('purchase_order')->where('CCYDH', $ccydh)->where('wflow_status', 1)->order('scydgy_id', 'asc')->select();
  2392. } catch (\Throwable $e) {
  2393. $pos = [$anchor];
  2394. }
  2395. if (!is_array($pos) || $pos === []) {
  2396. $pos = [$anchor];
  2397. }
  2398. return [
  2399. 'ccydh' => $ccydh,
  2400. 'pos' => $pos,
  2401. 'merge_rows' => $this->scydgyRowsForPurchaseOrders($pos),
  2402. ];
  2403. }
  2404. /**
  2405. * @param array<int, array<string, mixed>> $purchaseOrders
  2406. * @return array<int, array<string, mixed>>
  2407. */
  2408. protected function scydgyRowsForPurchaseOrders(array $purchaseOrders): array
  2409. {
  2410. $pool = $this->procuremenPoolFromPurchaseOrderDbRows($purchaseOrders);
  2411. $byId = [];
  2412. foreach ($pool as $r) {
  2413. if (!is_array($r)) {
  2414. continue;
  2415. }
  2416. $sid = (int)($r['ID'] ?? $r['id'] ?? 0);
  2417. if ($this->isValidScydgyRowId($sid)) {
  2418. $byId[$sid] = $r;
  2419. }
  2420. }
  2421. $out = [];
  2422. foreach ($purchaseOrders as $po) {
  2423. if (!is_array($po)) {
  2424. continue;
  2425. }
  2426. $sid = (int)($po['scydgy_id'] ?? 0);
  2427. if ($this->isValidScydgyRowId($sid) && isset($byId[$sid])) {
  2428. $out[] = $byId[$sid];
  2429. }
  2430. }
  2431. return $out;
  2432. }
  2433. /**
  2434. * 审核弹窗工序表展示行(与下发弹窗列一致,同订单合并订单号/印件名称单元格)
  2435. *
  2436. * @param array<int, array<string, mixed>> $mergeRows
  2437. * @return array<int, array<string, mixed>>
  2438. */
  2439. protected function buildAuditProcessDisplayRows(array $mergeRows): array
  2440. {
  2441. $list = array_values($mergeRows);
  2442. $n = count($list);
  2443. $orderKey0 = $n > 0 ? trim((string)($list[0]['CCYDH'] ?? '')) : '';
  2444. $nameKey0 = $n > 0 ? trim((string)($list[0]['CYJMC'] ?? '')) : '';
  2445. $mergeSame = $n > 1 && $orderKey0 !== '';
  2446. if ($mergeSame) {
  2447. foreach ($list as $rr) {
  2448. if (!is_array($rr)) {
  2449. $mergeSame = false;
  2450. break;
  2451. }
  2452. if (trim((string)($rr['CCYDH'] ?? '')) !== $orderKey0
  2453. || trim((string)($rr['CYJMC'] ?? '')) !== $nameKey0) {
  2454. $mergeSame = false;
  2455. break;
  2456. }
  2457. }
  2458. }
  2459. $out = [];
  2460. foreach ($list as $idx => $r) {
  2461. if (!is_array($r)) {
  2462. continue;
  2463. }
  2464. $qty = $r['This_quantity'] ?? $r['this_quantity'] ?? '';
  2465. if (is_scalar($qty) && trim((string)$qty) === '') {
  2466. $gzl = $r['NGZL'] ?? '';
  2467. if (is_scalar($gzl) && trim((string)$gzl) !== '') {
  2468. $qty = $gzl;
  2469. }
  2470. }
  2471. $price = $r['ceilingPrice'] ?? $r['ceiling_price'] ?? '';
  2472. $out[] = [
  2473. 'seq' => $idx + 1,
  2474. 'CCYDH' => trim((string)($r['CCYDH'] ?? '')),
  2475. 'CYJMC' => trim((string)($r['CYJMC'] ?? '')),
  2476. 'CGYMC' => trim((string)($r['CGYMC'] ?? '')),
  2477. 'CDW' => trim((string)($r['CDW'] ?? '')),
  2478. 'NGZL' => $r['NGZL'] ?? '',
  2479. 'This_quantity' => is_scalar($qty) ? trim((string)$qty) : '',
  2480. 'ceilingPrice' => is_scalar($price) ? trim((string)$price) : '',
  2481. 'CDF' => trim((string)($r['CDF'] ?? '')),
  2482. 'show_order_cells' => !$mergeSame || $idx === 0,
  2483. 'order_rowspan' => $mergeSame ? $n : 1,
  2484. ];
  2485. }
  2486. return $out;
  2487. }
  2488. /**
  2489. * 短信用工序明细(仅工序行,订单号/印件名称请在模版里用 {ccydh} {cyjmc} 自行排版)
  2490. */
  2491. protected function buildProcessLinesPlain(array $mergeRows): string
  2492. {
  2493. $lines = [];
  2494. $i = 1;
  2495. foreach ($mergeRows as $r) {
  2496. if (!is_array($r)) {
  2497. continue;
  2498. }
  2499. $gymc = trim((string)($r['CGYMC'] ?? ''));
  2500. $dw = trim((string)($r['CDW'] ?? ''));
  2501. $gzl = trim((string)($r['NGZL'] ?? ''));
  2502. $qty = trim((string)($r['This_quantity'] ?? $r['this_quantity'] ?? ''));
  2503. $parts = [$i . '.工序名称:' . ($gymc !== '' ? $gymc : '—')];
  2504. if ($dw !== '') {
  2505. $parts[] = '单位:' . $dw;
  2506. }
  2507. if ($gzl !== '') {
  2508. $parts[] = '工作量:' . $gzl;
  2509. }
  2510. if ($qty !== '') {
  2511. $parts[] = '本次数量:' . $qty;
  2512. }
  2513. $lines[] = implode(' ', $parts);
  2514. $i++;
  2515. }
  2516. return implode("\n", $lines);
  2517. }
  2518. /**
  2519. * 各工序手机端链接(纯文本,供短信模版 {platform_links})
  2520. *
  2521. * @param array<int, array{cgymc:string,url:string}> $detailLinks
  2522. */
  2523. protected function buildPlatformLinksPlain(array $detailLinks): string
  2524. {
  2525. $lines = [];
  2526. $i = 1;
  2527. foreach ($detailLinks as $lk) {
  2528. if (!is_array($lk)) {
  2529. continue;
  2530. }
  2531. $url = trim((string)($lk['url'] ?? ''));
  2532. if ($url === '') {
  2533. continue;
  2534. }
  2535. $label = trim((string)($lk['cgymc'] ?? ''));
  2536. if ($label === '') {
  2537. $label = '工序' . $i;
  2538. }
  2539. $lines[] = $label . ':' . $url;
  2540. $i++;
  2541. }
  2542. return implode("\n", $lines);
  2543. }
  2544. /**
  2545. * 各工序手机端链接(HTML,供邮件模版 {platform_links_html})
  2546. *
  2547. * @param array<int, array{cgymc:string,url:string}> $detailLinks
  2548. */
  2549. protected function buildPlatformLinksHtml(array $detailLinks): string
  2550. {
  2551. $parts = [];
  2552. $i = 1;
  2553. foreach ($detailLinks as $lk) {
  2554. if (!is_array($lk)) {
  2555. continue;
  2556. }
  2557. $url = trim((string)($lk['url'] ?? ''));
  2558. if ($url === '') {
  2559. continue;
  2560. }
  2561. $label = trim((string)($lk['cgymc'] ?? ''));
  2562. if ($label === '') {
  2563. $label = '工序' . $i;
  2564. }
  2565. $urlEsc = htmlspecialchars($url, ENT_QUOTES, 'UTF-8');
  2566. $parts[] = htmlspecialchars($label, ENT_QUOTES, 'UTF-8')
  2567. . ' — <a href="' . $urlEsc . '">点击查看</a>';
  2568. $i++;
  2569. }
  2570. return implode("<br>\n", $parts);
  2571. }
  2572. /**
  2573. * 发件配置:host/port 等来自 config.php;addr、pass 必须来自 purchase_email 表
  2574. *
  2575. * @return array<string, mixed>
  2576. */
  2577. protected function loadMailerConfig(): array
  2578. {
  2579. try {
  2580. $cfg = \app\admin\model\Purchaseemail::getActiveMailerConfig();
  2581. } catch (\Throwable $e) {
  2582. $cfg = [];
  2583. }
  2584. return is_array($cfg) ? $cfg : [];
  2585. }
  2586. protected function loadNotifyTemplateRow(string $scene): ?array
  2587. {
  2588. try {
  2589. $row = Db::table('purchase_sms_template')
  2590. ->where('scene', $scene)
  2591. ->where(function ($q) {
  2592. $q->where('status', 1)->whereOr('status', '1')->whereOr('status', 'normal');
  2593. })
  2594. ->order('id', 'asc')
  2595. ->find();
  2596. } catch (\Throwable $e) {
  2597. return null;
  2598. }
  2599. if (!is_array($row) || $row === []) {
  2600. return null;
  2601. }
  2602. return [
  2603. 'title' => trim((string)($row['title'] ?? '')),
  2604. 'content' => trim((string)($row['content'] ?? '')),
  2605. ];
  2606. }
  2607. /**
  2608. * 邮件用工序明细 HTML(仅工序块,其它字段请在模版里用 {ccydh} 等变量)
  2609. */
  2610. protected function buildProcessLinesHtml(array $mergeRows): string
  2611. {
  2612. if (count($mergeRows) <= 1) {
  2613. $r = $mergeRows[0] ?? [];
  2614. if (!is_array($r)) {
  2615. return '';
  2616. }
  2617. $gymc = trim((string)($r['CGYMC'] ?? ''));
  2618. $dw = trim((string)($r['CDW'] ?? ''));
  2619. $gzl = trim((string)($r['NGZL'] ?? ''));
  2620. $qty = trim((string)($r['This_quantity'] ?? $r['this_quantity'] ?? ''));
  2621. $html = '工序名称:' . htmlspecialchars($gymc !== '' ? $gymc : '—', ENT_QUOTES, 'UTF-8') . '<br>';
  2622. if ($dw !== '') {
  2623. $html .= '单位:' . htmlspecialchars($dw, ENT_QUOTES, 'UTF-8') . '<br>';
  2624. }
  2625. if ($gzl !== '') {
  2626. $html .= '工作量:' . htmlspecialchars($gzl, ENT_QUOTES, 'UTF-8') . '<br>';
  2627. }
  2628. if ($qty !== '') {
  2629. $html .= '本次数量:' . htmlspecialchars($qty, ENT_QUOTES, 'UTF-8') . '<br>';
  2630. }
  2631. return $html;
  2632. }
  2633. $rows = '';
  2634. $i = 1;
  2635. foreach ($mergeRows as $r) {
  2636. if (!is_array($r)) {
  2637. continue;
  2638. }
  2639. $gymc = htmlspecialchars(trim((string)($r['CGYMC'] ?? '')), ENT_QUOTES, 'UTF-8');
  2640. $dw = htmlspecialchars(trim((string)($r['CDW'] ?? '')), ENT_QUOTES, 'UTF-8');
  2641. $gzl = htmlspecialchars(trim((string)($r['NGZL'] ?? '')), ENT_QUOTES, 'UTF-8');
  2642. $qty = htmlspecialchars(trim((string)($r['This_quantity'] ?? $r['this_quantity'] ?? '')), ENT_QUOTES, 'UTF-8');
  2643. $rows .= '<tr><td style="padding:4px 8px;border:1px solid #ddd;text-align:center;">' . $i . '</td>'
  2644. . '<td style="padding:4px 8px;border:1px solid #ddd;">' . ($gymc !== '' ? $gymc : '—') . '</td>'
  2645. . '<td style="padding:4px 8px;border:1px solid #ddd;">' . ($dw !== '' ? $dw : '—') . '</td>'
  2646. . '<td style="padding:4px 8px;border:1px solid #ddd;">' . ($gzl !== '' ? $gzl : '—') . '</td>'
  2647. . '<td style="padding:4px 8px;border:1px solid #ddd;">' . ($qty !== '' ? $qty : '—') . '</td></tr>';
  2648. $i++;
  2649. }
  2650. if ($rows === '') {
  2651. return '';
  2652. }
  2653. return '<table cellspacing="0" cellpadding="0" style="border-collapse:collapse;margin:8px 0;font-size:14px;">'
  2654. . '<thead><tr style="background:#f5f5f5;">'
  2655. . '<th style="padding:6px 8px;border:1px solid #ddd;">序号</th>'
  2656. . '<th style="padding:6px 8px;border:1px solid #ddd;">工序名称</th>'
  2657. . '<th style="padding:6px 8px;border:1px solid #ddd;">单位</th>'
  2658. . '<th style="padding:6px 8px;border:1px solid #ddd;">工作量</th>'
  2659. . '<th style="padding:6px 8px;border:1px solid #ddd;">本次数量</th>'
  2660. . '</tr></thead><tbody>' . $rows . '</tbody></table>';
  2661. }
  2662. /**
  2663. * 写入/更新 purchase_order(单道工序行)
  2664. */
  2665. protected function upsertPurchaseOrderFromRow(array $row, string $sysRqDb, ?int $wflowStatus = null): void
  2666. {
  2667. $ids = $this->extractScydgyRowId($row);
  2668. if (!$this->isValidScydgyRowId($ids)) {
  2669. throw new \Exception('无效的行主键 ID');
  2670. }
  2671. $exists = Db::table('purchase_order')->where('scydgy_id', $ids)->find();
  2672. $data = [
  2673. 'scydgy_id' => $ids,
  2674. 'CCYDH' => $row['CCYDH'] ?? null,
  2675. 'CYJMC' => $row['CYJMC'] ?? null,
  2676. 'CDXMC' => $row['CDXMC'] ?? null,
  2677. 'CGYBH' => $row['CGYBH'] ?? null,
  2678. 'CGYMC' => $row['CGYMC'] ?? null,
  2679. 'CDW' => $row['CDW'] ?? null,
  2680. 'NGZL' => $row['NGZL'] ?? null,
  2681. 'CDF' => $row['CDF'] ?? null,
  2682. 'cGzzxMc' => $row['cGzzxMc'] ?? null,
  2683. 'MBZ' => $row['MBZ'] ?? null,
  2684. 'bwjg' => $row['bwjg'] ?? null,
  2685. 'iStatus' => $row['iStatus'] ?? null,
  2686. 'dStamp' => $row['dStamp'] ?? null,
  2687. 'dputrecord' => $row['dputrecord'] ?? null,
  2688. 'cywyxm' => $row['cywyxm'] ?? null,
  2689. 'This_quantity' => $row['This_quantity'] ?? $row['this_quantity'] ?? null,
  2690. 'ceilingPrice' => $row['ceilingPrice'] ?? $row['ceiling_price'] ?? null,
  2691. 'status' => 0,
  2692. 'sys_rq' => $sysRqDb,
  2693. ];
  2694. if ($wflowStatus !== null) {
  2695. $data['wflow_status'] = $wflowStatus;
  2696. }
  2697. if ($exists) {
  2698. $upd = $data;
  2699. unset($upd['scydgy_id']);
  2700. Db::table('purchase_order')->where('scydgy_id', $ids)->update($upd);
  2701. } else {
  2702. $data['createtime'] = date('Y-m-d H:i:s');
  2703. Db::table('purchase_order')->insert($data);
  2704. }
  2705. }
  2706. /**
  2707. * 外发下发 — 写入下发明细并拼好短信/邮件内容(不发)
  2708. *
  2709. * @return array<string, mixed>
  2710. */
  2711. protected function issueBuildSupplierNotifyBundle(array $c, array $mergeRows, array $ctx): array
  2712. {
  2713. $toDb = function ($value) {
  2714. if ($value === null) {
  2715. return null;
  2716. }
  2717. if (is_scalar($value)) {
  2718. return $value;
  2719. }
  2720. return json_encode($value, JSON_UNESCAPED_UNICODE);
  2721. };
  2722. $toEmail = isset($c['email']) ? trim((string)$c['email']) : '';
  2723. $companyName = isset($c['name']) ? trim((string)$c['name']) : '外协单位';
  2724. $phone = isset($c['phone']) ? trim((string)$c['phone']) : '';
  2725. if ($toEmail === '' || !filter_var($toEmail, FILTER_VALIDATE_EMAIL)) {
  2726. throw new \Exception($companyName . ' 未填写有效邮箱,无法发送邮件,未写入数据');
  2727. }
  2728. if ($phone === '') {
  2729. throw new \Exception(($companyName !== '' ? $companyName : '外协单位') . ' 未填写手机号,无法发送短信,未写入数据');
  2730. }
  2731. $detailLinks = [];
  2732. foreach ($mergeRows as $row) {
  2733. if (!is_array($row)) {
  2734. continue;
  2735. }
  2736. $sid = $this->extractScydgyRowId($row);
  2737. $one = [
  2738. 'scydgy_id' => $toDb($sid),
  2739. 'CCYDH' => $toDb($row['CCYDH'] ?? null),
  2740. 'CYJMC' => $toDb($row['CYJMC'] ?? null),
  2741. 'company_name' => isset($c['name']) ? (string)$c['name'] : null,
  2742. 'email' => isset($c['email']) ? (string)$c['email'] : null,
  2743. 'phone' => isset($c['phone']) ? (string)$c['phone'] : null,
  2744. 'createtime' => date('Y-m-d H:i:s'),
  2745. 'status' => 0,
  2746. 'status_name' => '未提交',
  2747. ];
  2748. Db::table('purchase_order_detail')->insert($one);
  2749. $detailId = (int)Db::getLastInsID();
  2750. $mprocUrl = $this->buildMprocMobileOrderUrl($detailId);
  2751. $detailLinks[] = [
  2752. 'detail_id' => $detailId,
  2753. 'cgymc' => trim((string)($row['CGYMC'] ?? '')),
  2754. 'url' => $mprocUrl,
  2755. ];
  2756. }
  2757. $platformUrl = isset($detailLinks[0]['url']) ? trim((string)$detailLinks[0]['url']) : '';
  2758. $platformLinksPlain = $this->buildPlatformLinksPlain($detailLinks);
  2759. $platformLinksHtml = $this->buildPlatformLinksHtml($detailLinks);
  2760. $ccydh = (string)($ctx['ccydh'] ?? '');
  2761. $cyjmc = (string)($ctx['cyjmc'] ?? '');
  2762. $sysRqNotify = (string)($ctx['deadline'] ?? '');
  2763. $processPlain = (string)($ctx['process_plain'] ?? '');
  2764. $isMerge = !empty($ctx['is_merge']);
  2765. $contactName = trim((string)($c['username'] ?? ''));
  2766. if ($contactName === '') {
  2767. $contactName = $this->resolveCustomerContactName($phone, $companyName);
  2768. }
  2769. $notifyVars = [
  2770. 'company_name' => $companyName,
  2771. 'contact_name' => $contactName,
  2772. 'phone' => $phone,
  2773. 'email' => $toEmail,
  2774. 'ccydh' => $ccydh,
  2775. 'cyjmc' => $cyjmc,
  2776. 'cgymc' => $isMerge ? '' : (string)($ctx['cgymc_single'] ?? ''),
  2777. 'category' => trim((string)($c['category'] ?? $c['company_type'] ?? '')),
  2778. 'deadline' => $sysRqNotify,
  2779. 'process_lines' => $processPlain,
  2780. 'process_lines_html' => (string)($ctx['process_html'] ?? ''),
  2781. 'platform_url' => $platformUrl,
  2782. 'platform_links' => $platformLinksPlain,
  2783. 'platform_links_html' => $platformLinksHtml,
  2784. ];
  2785. $smsContent = $this->renderNotifyTemplate('review_sms', $notifyVars);
  2786. $mailPlain = $this->renderNotifyTemplate('review_email', $notifyVars);
  2787. $mailBody = $this->plainTextToHtmlEmailBody($mailPlain);
  2788. return [
  2789. 'company_name' => $companyName,
  2790. 'phone' => $phone,
  2791. 'email' => $toEmail,
  2792. 'sms_content' => $smsContent,
  2793. 'mail_plain' => $mailPlain,
  2794. 'mail_body' => $mailBody,
  2795. 'notify_vars' => $notifyVars,
  2796. 'detail_links' => $detailLinks,
  2797. ];
  2798. }
  2799. /**
  2800. * 外发下发 — 发送短信(一家供应商)
  2801. *
  2802. * @param array<string, mixed> $bundle issueBuildSupplierNotifyBundle 返回值
  2803. */
  2804. protected function issueSendSupplierSms(array $bundle): void
  2805. {
  2806. $companyName = (string)($bundle['company_name'] ?? '');
  2807. $phone = (string)($bundle['phone'] ?? '');
  2808. $smsContent = (string)($bundle['sms_content'] ?? '');
  2809. $this->logIssueNotifyDebug('短信', [
  2810. 'company' => $companyName,
  2811. 'phone' => $phone,
  2812. 'sms_content' => $smsContent,
  2813. ]);
  2814. if ($this->isProcuremenNotifyDryRun()) {
  2815. $this->recordNotifyDryRunPreview([
  2816. 'scene' => 'issue_sms',
  2817. 'company_name' => $companyName,
  2818. 'phone' => $phone,
  2819. 'sms_content' => $smsContent,
  2820. ]);
  2821. return;
  2822. }
  2823. $this->smsbao($phone, $smsContent);
  2824. }
  2825. /**
  2826. * 外发下发 — 发送邮件(一家供应商)
  2827. *
  2828. * @param array<string, mixed> $bundle
  2829. * @param array<string, mixed> $mailConfig
  2830. */
  2831. protected function issueSendSupplierEmail(array $bundle, array $mailConfig, string $mailSubject): void
  2832. {
  2833. $companyName = (string)($bundle['company_name'] ?? '');
  2834. $toEmail = (string)($bundle['email'] ?? '');
  2835. $mailPlain = (string)($bundle['mail_plain'] ?? '');
  2836. $mailBody = (string)($bundle['mail_body'] ?? '');
  2837. $this->logIssueNotifyDebug('邮件', [
  2838. 'company' => $companyName,
  2839. 'email' => $toEmail,
  2840. 'mail_subject' => $mailSubject,
  2841. 'mail_body' => $mailPlain,
  2842. ]);
  2843. if ($this->isProcuremenNotifyDryRun()) {
  2844. $this->recordNotifyDryRunPreview([
  2845. 'scene' => 'issue_email',
  2846. 'company_name' => $companyName,
  2847. 'email' => $toEmail,
  2848. 'mail_subject' => $mailSubject,
  2849. 'mail_body' => $mailPlain,
  2850. 'sms_content' => (string)($bundle['sms_content'] ?? ''),
  2851. 'notify_vars' => $bundle['notify_vars'] ?? [],
  2852. 'detail_links' => $bundle['detail_links'] ?? [],
  2853. ]);
  2854. return;
  2855. }
  2856. $mail = new PHPMailer(true);
  2857. $mail->isSMTP();
  2858. $mail->Host = $mailConfig['host'];
  2859. $mail->SMTPAuth = true;
  2860. $mail->Username = $mailConfig['addr'];
  2861. $mail->Password = $mailConfig['pass'];
  2862. $mail->SMTPSecure = $mailConfig['security'];
  2863. $mail->Port = $mailConfig['port'];
  2864. $mail->CharSet = $mailConfig['charset'];
  2865. $mail->setFrom($mailConfig['addr'], $mailConfig['name']);
  2866. $mail->addAddress($toEmail, $companyName);
  2867. $mail->isHTML(true);
  2868. $mail->Subject = $mailSubject;
  2869. $mail->Body = $mailBody;
  2870. $mail->send();
  2871. }
  2872. /**
  2873. * 外发下发通知上下文(订单号、工序明细等,供短信/邮件共用)
  2874. *
  2875. * @param array<int, array<string, mixed>> $mergeRows
  2876. * @return array<string, mixed>
  2877. */
  2878. protected function buildIssueNotifyContext(array $mergeRows, string $sysRqNotify): array
  2879. {
  2880. $head = $mergeRows[0] ?? [];
  2881. $isMerge = count($mergeRows) > 1;
  2882. return [
  2883. 'ccydh' => isset($head['CCYDH']) ? (string)$head['CCYDH'] : '',
  2884. 'cyjmc' => isset($head['CYJMC']) ? (string)$head['CYJMC'] : '',
  2885. 'cgymc_single' => trim((string)($head['CGYMC'] ?? '')),
  2886. 'is_merge' => $isMerge,
  2887. 'process_plain' => $this->buildProcessLinesPlain($mergeRows),
  2888. 'process_html' => $this->buildProcessLinesHtml($mergeRows),
  2889. 'deadline' => $sysRqNotify,
  2890. ];
  2891. }
  2892. /**
  2893. * 调试:app_debug 或 notify_dry_run 时把短信/邮件内容写入 runtime/log
  2894. *
  2895. * @param array<string, mixed> $data
  2896. */
  2897. protected function logIssueNotifyDebug(string $type, array $data): void
  2898. {
  2899. if (!Config::get('app_debug') && !$this->isProcuremenNotifyDryRun()) {
  2900. return;
  2901. }
  2902. Log::write('[外发下发-' . $type . '] ' . json_encode($data, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), 'notice');
  2903. }
  2904. /**
  2905. * 外发下发弹窗(选多家供应商并通知)
  2906. */
  2907. public function pickReview()
  2908. {
  2909. if ($this->request->isPost()) {
  2910. $this->error('请使用弹窗内「确认下发」提交');
  2911. }
  2912. $this->view->assign('pickMode', 1);
  2913. return $this->view->fetch('procuremen/review');
  2914. }
  2915. /**
  2916. * 外发下发 — 手工新增工序(弹窗)
  2917. */
  2918. public function pickAdd()
  2919. {
  2920. if ($this->request->isPost()) {
  2921. $params = $this->request->post('row/a');
  2922. if (!is_array($params)) {
  2923. $this->error(__('Parameter %s can not be empty', ''));
  2924. }
  2925. $ccydh = trim((string)($params['CCYDH'] ?? ''));
  2926. $cyjmc = trim((string)($params['CYJMC'] ?? ''));
  2927. $cgymc = trim((string)($params['CGYMC'] ?? ''));
  2928. if ($ccydh === '') {
  2929. $this->error('请填写订单号');
  2930. }
  2931. if ($cyjmc === '') {
  2932. $this->error('请填写印件名称');
  2933. }
  2934. if ($cgymc === '') {
  2935. $this->error('请填写工序名称');
  2936. }
  2937. $now = date('Y-m-d H:i:s');
  2938. $sid = $this->allocateManualScydgyId();
  2939. $data = [
  2940. 'scydgy_id' => $sid,
  2941. 'CCYDH' => $ccydh,
  2942. 'CYJMC' => $cyjmc,
  2943. 'CGYMC' => $cgymc,
  2944. 'CDW' => trim((string)($params['CDW'] ?? '')),
  2945. 'NGZL' => trim((string)($params['NGZL'] ?? '')),
  2946. 'CDF' => trim((string)($params['CDF'] ?? '')),
  2947. 'cGzzxMc' => trim((string)($params['cGzzxMc'] ?? '')),
  2948. 'MBZ' => trim((string)($params['MBZ'] ?? '')),
  2949. 'cywyxm' => trim((string)($params['cywyxm'] ?? '')),
  2950. 'This_quantity' => trim((string)($params['This_quantity'] ?? '')),
  2951. 'ceilingPrice' => trim((string)($params['ceilingPrice'] ?? '')),
  2952. 'wflow_status' => 0,
  2953. 'createtime' => $now,
  2954. 'dStamp' => $now,
  2955. 'dputrecord' => $now,
  2956. ];
  2957. try {
  2958. Db::table('purchase_order')->insert($data);
  2959. } catch (\Throwable $e) {
  2960. $this->error($e->getMessage());
  2961. }
  2962. $poId = (int)Db::getLastInsID();
  2963. $this->addOrderLog($sid, 'manual_add', '手工新增外发工序', $poId > 0 ? $poId : null);
  2964. $this->success('新增成功', '', ['scydgy_id' => $sid]);
  2965. }
  2966. return $this->view->fetch('procuremen/pick_add');
  2967. }
  2968. /**
  2969. * 外发下发提交 — 弹窗「确认下发」→ POST procuremen/picksubmit
  2970. */
  2971. public function pickSubmit()
  2972. {
  2973. if (!$this->request->isPost()) {
  2974. $this->error(__('Invalid parameters'));
  2975. }
  2976. // ---------- 接口订单数据 ----------
  2977. $rowJson = $this->request->post('row_json', '');
  2978. $mergeRowsJson = $this->request->post('merge_rows_json', '');
  2979. $companiesJson = $this->request->post('companies_json', '[]');
  2980. $mergeRows = $this->parseReviewMergeRows($rowJson, $mergeRowsJson);
  2981. $companies = json_decode($companiesJson, true);
  2982. if (!is_array($companies)) {
  2983. $this->error('供应商数据无效');
  2984. }
  2985. $issueCompanies = $this->normalizePickCompanies($companies);
  2986. if ($issueCompanies === []) {
  2987. $this->error('请至少选择一家合作供应商');
  2988. }
  2989. $issueNameLabels = [];
  2990. foreach ($issueCompanies as $pc) {
  2991. $issueNameLabels[] = $pc['name'];
  2992. }
  2993. $issueNamesSummary = implode('、', $issueNameLabels);
  2994. $sysRq = trim((string)$this->request->post('sys_rq', ''));
  2995. if ($sysRq === '') {
  2996. $this->error('请选择截止时间');
  2997. }
  2998. $sysRqTs = strtotime($sysRq);
  2999. if ($sysRqTs === false || $sysRqTs <= 0) {
  3000. $this->error('截止时间格式无效');
  3001. }
  3002. $sysRqDb = date('Y-m-d H:i:s', $sysRqTs);
  3003. $sysRqNotify = date('Y-m-d H:i', $sysRqTs);
  3004. $ctx = $this->buildIssueNotifyContext($mergeRows, $sysRqNotify);
  3005. $mailTplRow = $this->loadNotifyTemplateRow('review_email');
  3006. if ($mailTplRow === null || trim((string)($mailTplRow['title'] ?? '')) === '') {
  3007. $this->error($this->notifyTemplateMissingMessage('review_email', true));
  3008. }
  3009. $mailSubject = trim((string)$mailTplRow['title']);
  3010. $mailConfig = $this->loadMailerConfig();
  3011. if (!$this->isProcuremenNotifyDryRun()) {
  3012. if (empty($mailConfig['host'])) {
  3013. $this->error('邮件 SMTP 未配置,请检查 config.php 中 Mailer.host');
  3014. }
  3015. if (empty($mailConfig['addr']) || empty($mailConfig['pass'])) {
  3016. $this->error('发件邮箱或授权码未配置,请至后台「邮箱配置」填写');
  3017. }
  3018. }
  3019. $isMerge = count($mergeRows) > 1;
  3020. $logMsg = ($isMerge ? '合并下发(' . count($mergeRows) . ' 道工序)' : '外发下发')
  3021. . ',通知供应商(' . count($issueCompanies) . ' 家):' . $issueNamesSummary;
  3022. $notifyBundles = [];
  3023. Db::startTrans();
  3024. try {
  3025. $this->notifyDryRunPreview = [];
  3026. // ---------- 存入数据库 ----------
  3027. // purchase_order 主表(工序行 + 截止时间 + wflow_status=1)
  3028. foreach ($mergeRows as $row) {
  3029. if (!is_array($row)) {
  3030. continue;
  3031. }
  3032. $this->upsertPurchaseOrderFromRow($row, $sysRqDb, 1);
  3033. }
  3034. // purchase_order_detail 下发明细 + 拼好短信/邮件文案(含手机端链接)
  3035. foreach ($issueCompanies as $c) {
  3036. if (!is_array($c)) {
  3037. continue;
  3038. }
  3039. $notifyBundles[] = $this->issueBuildSupplierNotifyBundle($c, $mergeRows, $ctx);
  3040. }
  3041. $issueTime = date('Y-m-d H:i:s');
  3042. foreach ($mergeRows as $row) {
  3043. if (!is_array($row)) {
  3044. continue;
  3045. }
  3046. $sid = $this->extractScydgyRowId($row);
  3047. if (!$this->isValidScydgyRowId($sid)) {
  3048. continue;
  3049. }
  3050. $upd = [
  3051. 'wflow_status' => 1,
  3052. 'status' => 0,
  3053. 'pick_time' => $issueTime,
  3054. 'pick_company_name' => '',
  3055. 'sys_rq' => $sysRqDb,
  3056. ];
  3057. Db::table('purchase_order')->where('scydgy_id', $sid)->update($upd);
  3058. }
  3059. // // ---------- 短信通知 ----------
  3060. // foreach ($notifyBundles as $bundle) {
  3061. // $this->issueSendSupplierSms($bundle);
  3062. // }
  3063. // // ---------- 邮箱通知 ----------
  3064. // foreach ($notifyBundles as $bundle) {
  3065. // $this->issueSendSupplierEmail($bundle, $mailConfig, $mailSubject);
  3066. // }
  3067. Db::commit();
  3068. } catch (\Throwable $e) {
  3069. Db::rollback();
  3070. $this->error($e->getMessage());
  3071. }
  3072. // ---------- 操作日志记录 ----------
  3073. foreach ($mergeRows as $row) {
  3074. if (!is_array($row)) {
  3075. continue;
  3076. }
  3077. $ids = $this->extractScydgyRowId($row);
  3078. if (!$this->isValidScydgyRowId($ids)) {
  3079. continue;
  3080. }
  3081. $poIdLog = null;
  3082. try {
  3083. $rpo = Db::table('purchase_order')->where('scydgy_id', $ids)->find();
  3084. if (is_array($rpo)) {
  3085. $tid = (int)($rpo['id'] ?? $rpo['ID'] ?? 0);
  3086. $poIdLog = $tid > 0 ? $tid : null;
  3087. }
  3088. } catch (\Throwable $e) {
  3089. }
  3090. $this->addOrderLog($ids, 'issue_submit', $logMsg, $poIdLog);
  3091. }
  3092. if ($this->isProcuremenNotifyDryRun()) {
  3093. $this->success('【演练模式】已写入数据,未发送短信/邮件', '', [
  3094. 'notify_dry_run' => true,
  3095. 'notify_preview' => $this->notifyDryRunPreview,
  3096. ]);
  3097. }
  3098. $this->success('已下发短信与邮件,请供应商报价后至「确认供应商」选定一家');
  3099. }
  3100. /**
  3101. * 确认供应商弹窗:订单信息 + 供应商报价,选定一家
  3102. */
  3103. public function auditIssue()
  3104. {
  3105. $ids = $this->request->param('ids', $this->request->param('id', ''));
  3106. if (is_array($ids)) {
  3107. $ids = isset($ids[0]) ? $ids[0] : '';
  3108. }
  3109. $ids = trim((string)$ids);
  3110. if ($ids === '') {
  3111. $this->error(__('Invalid parameters'));
  3112. }
  3113. if ($this->request->isPost()) {
  3114. $this->error('请使用弹窗内「确认审核」提交');
  3115. }
  3116. $bundle = $this->loadAuditOrderBundleByScydgyId($ids);
  3117. $pos = $bundle['pos'];
  3118. if ($pos === []) {
  3119. $this->error('该订单不在待确认供应商状态');
  3120. }
  3121. $po = $pos[0];
  3122. $supplierGroups = $this->loadAuditSupplierQuoteGroups($bundle);
  3123. if ($supplierGroups === []) {
  3124. $this->error('暂无供应商报价明细,请确认已下发且供应商已填报');
  3125. }
  3126. $this->view->assign('po', $po);
  3127. $this->view->assign('scydgyId', $ids);
  3128. $this->view->assign('orderCcydh', $bundle['ccydh']);
  3129. $mergeRows = $bundle['merge_rows'];
  3130. $this->view->assign('processRows', $mergeRows);
  3131. $this->view->assign('processDisplayRows', $this->buildAuditProcessDisplayRows($mergeRows));
  3132. $this->view->assign('processCount', count($mergeRows));
  3133. $this->view->assign('supplierGroups', $supplierGroups);
  3134. $this->view->assign('supplierGroupsJson', json_encode($supplierGroups, JSON_UNESCAPED_UNICODE));
  3135. return $this->view->fetch('procuremen/audit_issue');
  3136. }
  3137. /**
  3138. * 确认供应商提交(选定一家,进入采购确认,不发通知)
  3139. */
  3140. public function auditSubmit()
  3141. {
  3142. if (!$this->request->isPost()) {
  3143. $this->error(__('Invalid parameters'));
  3144. }
  3145. $scydgyId = trim((string)$this->request->post('scydgy_id', ''));
  3146. if ($scydgyId === '') {
  3147. $this->error('参数无效');
  3148. }
  3149. $bundle = $this->loadAuditOrderBundleByScydgyId($scydgyId);
  3150. $pos = $bundle['pos'];
  3151. $mergeRows = $bundle['merge_rows'];
  3152. if ($pos === [] || $mergeRows === []) {
  3153. $this->error('该订单不在待确认供应商状态');
  3154. }
  3155. $selRaw = $this->request->post('company_json', '');
  3156. $sel = json_decode(is_string($selRaw) ? $selRaw : '', true);
  3157. if (!is_array($sel)) {
  3158. $this->error('请选择一家供应商');
  3159. }
  3160. $selNorm = $this->normalizePickCompanies([$sel]);
  3161. if ($selNorm === []) {
  3162. $this->error('请选择有效的供应商');
  3163. }
  3164. $chosen = $selNorm[0];
  3165. $chosenName = $chosen['name'];
  3166. $supplierGroups = $this->loadAuditSupplierQuoteGroups($bundle);
  3167. $matched = null;
  3168. foreach ($supplierGroups as $g) {
  3169. if (!is_array($g)) {
  3170. continue;
  3171. }
  3172. if (($g['name'] ?? '') === $chosenName) {
  3173. $matched = $g;
  3174. break;
  3175. }
  3176. }
  3177. if ($matched === null) {
  3178. $this->error('所选供应商不在本单报价列表中');
  3179. }
  3180. $contactName = trim((string)($chosen['username'] ?? ''));
  3181. if ($contactName === '') {
  3182. $contactName = $this->resolveCustomerContactName(
  3183. trim((string)($chosen['phone'] ?? '')),
  3184. $chosenName
  3185. );
  3186. }
  3187. $ccydhLog = trim((string)($bundle['ccydh'] ?? ''));
  3188. $procCnt = count($mergeRows);
  3189. $logMsg = '确认供应商(订单' . ($ccydhLog !== '' ? $ccydhLog : '')
  3190. . ($procCnt > 1 ? ',' . $procCnt . ' 道工序' : '')
  3191. . '),选定供应商:' . $chosenName;
  3192. Db::startTrans();
  3193. try {
  3194. foreach ($pos as $poRow) {
  3195. if (!is_array($poRow)) {
  3196. continue;
  3197. }
  3198. $sid = (int)($poRow['scydgy_id'] ?? 0);
  3199. if (!$this->isValidScydgyRowId($sid)) {
  3200. continue;
  3201. }
  3202. Db::table('purchase_order')->where('scydgy_id', $sid)->update([
  3203. 'wflow_status' => 2,
  3204. 'status' => 0,
  3205. 'pick_company_name' => $chosenName,
  3206. ]);
  3207. }
  3208. Db::commit();
  3209. } catch (\Throwable $e) {
  3210. Db::rollback();
  3211. $this->error($e->getMessage());
  3212. }
  3213. foreach ($pos as $poRow) {
  3214. if (!is_array($poRow)) {
  3215. continue;
  3216. }
  3217. $sid = (int)($poRow['scydgy_id'] ?? 0);
  3218. if (!$this->isValidScydgyRowId($sid)) {
  3219. continue;
  3220. }
  3221. $poIdLog = (int)($poRow['id'] ?? $poRow['ID'] ?? 0);
  3222. $this->addOrderLog($sid, 'audit_confirm', $logMsg, $poIdLog > 0 ? $poIdLog : null);
  3223. }
  3224. $this->success('已确认供应商,请至第三步「采购确认」定标并发送通过/未通过短信');
  3225. }
  3226. /**
  3227. * 审核弹窗(旧入口,POST 已关闭)
  3228. */
  3229. public function review()
  3230. {
  3231. if ($this->request->isPost()) {
  3232. $this->error('请使用第一步「外发下发」通知供应商,再在「确认供应商」中选定一家');
  3233. }
  3234. $this->view->assign('pickMode', 0);
  3235. return $this->view->fetch('procuremen/review');
  3236. }
  3237. /**
  3238. * 审核弹窗获取公司列表
  3239. */
  3240. public function reviewCompanies()
  3241. {
  3242. if (!$this->request->isAjax()) {
  3243. $this->error(__('Invalid parameters'));
  3244. }
  3245. $list = [];
  3246. try {
  3247. // 仅「正常」外协(customer.status=1);0=禁止登录,与手机端 mprocCustomerUserActive 一致
  3248. $rows = Db::table('customer')
  3249. ->where(function ($q) {
  3250. $q->where('status', 1)
  3251. ->whereOr('status', '1')
  3252. ->whereOr('status', '')
  3253. ->whereNull('status');
  3254. })
  3255. ->order('id', 'desc')
  3256. ->select();
  3257. } catch (\Throwable $e) {
  3258. $this->success('', '', []);
  3259. return;
  3260. }
  3261. if (!is_array($rows)) {
  3262. $this->success('', '', []);
  3263. return;
  3264. }
  3265. $detailColCandidates = [
  3266. 'detail', 'mingxi', 'remark', 'memo', 'notes', 'description',
  3267. 'company_detail', 'company_desc', 'address',
  3268. ];
  3269. foreach ($rows as $row) {
  3270. if (!is_array($row)) {
  3271. continue;
  3272. }
  3273. $norm = [];
  3274. foreach ($row as $k => $v) {
  3275. $norm[is_string($k) ? strtolower($k) : $k] = $v;
  3276. }
  3277. $row = $norm;
  3278. $st = $row['status'] ?? '';
  3279. if ($st !== '' && $st !== null && $st !== 1 && $st !== '1') {
  3280. continue;
  3281. }
  3282. $id = isset($row['id']) ? (string)$row['id'] : '';
  3283. $companyName = '';
  3284. foreach (['company_name', 'name'] as $nk) {
  3285. if (!empty($row[$nk])) {
  3286. $companyName = trim((string)$row[$nk]);
  3287. break;
  3288. }
  3289. }
  3290. $username = '';
  3291. foreach (['username', 'contact', 'linkman', 'contacts'] as $uk) {
  3292. if (isset($row[$uk]) && trim((string)$row[$uk]) !== '') {
  3293. $username = trim((string)$row[$uk]);
  3294. break;
  3295. }
  3296. }
  3297. $email = isset($row['email']) ? trim((string)$row['email']) : '';
  3298. $phone = isset($row['phone']) ? trim((string)$row['phone']) : '';
  3299. if ($phone === '' && isset($row['account'])) {
  3300. $phone = trim((string)$row['account']);
  3301. }
  3302. if ($phone === '' && isset($row['mobile'])) {
  3303. $phone = trim((string)$row['mobile']);
  3304. }
  3305. /* 邮箱、手机号均为空则无法发短信/邮件,不在下发弹窗展示 */
  3306. if ($email === '' && $phone === '') {
  3307. continue;
  3308. }
  3309. $category = '';
  3310. foreach (['company_type', 'category', 'type_name'] as $ck) {
  3311. if (isset($row[$ck]) && trim((string)$row[$ck]) !== '') {
  3312. $category = trim((string)$row[$ck]);
  3313. break;
  3314. }
  3315. }
  3316. $detail = '';
  3317. foreach ($detailColCandidates as $dk) {
  3318. if (!isset($row[$dk])) {
  3319. continue;
  3320. }
  3321. $dv = trim((string)$row[$dk]);
  3322. if ($dv !== '') {
  3323. $detail = $dv;
  3324. break;
  3325. }
  3326. }
  3327. $list[] = [
  3328. 'id' => $id,
  3329. 'name' => $companyName,
  3330. 'company_name' => $companyName,
  3331. 'username' => $username,
  3332. 'email' => $email,
  3333. 'phone' => $phone,
  3334. 'category' => $category,
  3335. 'company_type' => $category,
  3336. 'detail' => $detail,
  3337. ];
  3338. }
  3339. $this->success('', '', $list);
  3340. }
  3341. /**
  3342. * 采购确认-下发明细弹窗:
  3343. * 查 purchase_order_detail;ids 为工序行 scydgy.ID,对应明细 scydgy_id
  3344. */
  3345. public function outward_detail()
  3346. {
  3347. $ids = $this->request->param('ids', $this->request->param('id', ''));
  3348. if (is_array($ids)) {
  3349. $ids = isset($ids[0]) ? $ids[0] : '';
  3350. }
  3351. $ids = trim((string)$ids);
  3352. if ($ids === '') {
  3353. $this->error(__('Invalid parameters'));
  3354. }
  3355. $wffTab = trim((string)$this->request->param('wff_tab', 'all'));
  3356. if (!in_array($wffTab, ['all', 'pending', 'picked', 'done'], true)) {
  3357. $wffTab = 'all';
  3358. }
  3359. $headTitle = $wffTab === 'pending' ? '采购确认(明细)' : '下发明细';
  3360. $rows = [];
  3361. try {
  3362. $rows = Db::table('purchase_order_detail')->where('scydgy_id', $ids)->order('id', 'desc')->select();
  3363. } catch (\Throwable $e) {
  3364. $rows = [];
  3365. }
  3366. foreach ($rows as &$r) {
  3367. if (is_array($r) && isset($r['ID']) && !isset($r['id'])) {
  3368. $r['id'] = $r['ID'];
  3369. }
  3370. if (isset($r['createtime'])) {
  3371. if (is_numeric($r['createtime']) && (int)$r['createtime'] > 946684800) {
  3372. $r['createtime_text'] = date('Y-m-d H:i:s', (int)$r['createtime']);
  3373. } else {
  3374. $r['createtime_text'] = (string)$r['createtime'];
  3375. }
  3376. } else {
  3377. $r['createtime_text'] = '';
  3378. }
  3379. }
  3380. unset($r);
  3381. $purchaseOrderId = 0;
  3382. try {
  3383. $po = Db::table('purchase_order')->where('scydgy_id', $ids)->find();
  3384. if (is_array($po)) {
  3385. $purchaseOrderId = (int)($po['id'] ?? $po['ID'] ?? 0);
  3386. }
  3387. } catch (\Throwable $e) {
  3388. $purchaseOrderId = 0;
  3389. }
  3390. $this->view->assign('rows', $rows ?: []);
  3391. $this->view->assign('rowCount', count($rows ?: []));
  3392. $this->view->assign('scydgyId', $ids);
  3393. $this->view->assign('purchaseOrderId', $purchaseOrderId);
  3394. $this->view->assign('headTitle', $headTitle);
  3395. $this->view->assign('showPurchaseConfirm', ($wffTab === 'pending' || $wffTab === 'confirm') ? 1 : 0);
  3396. $this->view->assign('detailColspan', $wffTab === 'pending' ? 10 : 9);
  3397. /* 采购确认(pending)需在 iframe 内加载 require-backend,以便勾选与提交;其它 tab 仅只读表格,用轻量壳避免整站 JS/CSS 二次初始化导致弹窗极慢 */
  3398. if ($wffTab === 'pending') {
  3399. return $this->view->fetch();
  3400. }
  3401. $restoreLayout = !empty($this->layout) ? ('layout/' . $this->layout) : false;
  3402. $this->view->engine->layout(false);
  3403. try {
  3404. $bodyHtml = $this->view->fetch('procuremen/outward_detail');
  3405. $this->view->assign('dialogBody', $bodyHtml);
  3406. return $this->view->fetch('procuremen/outward_detail_lite_shell');
  3407. } finally {
  3408. if ($restoreLayout) {
  3409. $this->view->engine->layout($restoreLayout);
  3410. }
  3411. }
  3412. }
  3413. /**
  3414. * 按月份导出:已完结且采购确认已选定供应商(明细 status=1),与列表「已完结」∩「已选中」一致。
  3415. * 表头固定 8 列(与历史「外发明细」模板一致):序号、传票号、传票名称、外加工单位、订法、客户名称、工序、加工金额。
  3416. */
  3417. public function export_month_outward()
  3418. {
  3419. $this->request->filter(['strip_tags', 'trim']);
  3420. $ym = trim((string)$this->request->get('ym', date('Y-m')));
  3421. if (!preg_match('/^\d{4}-\d{2}$/', $ym)) {
  3422. $ym = date('Y-m');
  3423. }
  3424. $monthStart = $ym . '-01 00:00:00';
  3425. $monthEnd = date('Y-m-t 23:59:59', strtotime($monthStart));
  3426. $dbRows = [];
  3427. try {
  3428. $dbRows = Db::table('purchase_order')->where('status', 1)->select();
  3429. } catch (\Throwable $e) {
  3430. $dbRows = [];
  3431. }
  3432. if (!is_array($dbRows)) {
  3433. $dbRows = [];
  3434. }
  3435. $pickedSidSet = $this->loadScydgyIdsWithPickedSupplierDetail();
  3436. $dbRows = array_values(array_filter($dbRows, function ($dbRow) use ($pickedSidSet) {
  3437. if (!is_array($dbRow)) {
  3438. return false;
  3439. }
  3440. $sid = (int)($dbRow['scydgy_id'] ?? 0);
  3441. if ($sid <= 0 && isset($dbRow['SCYDGY_ID'])) {
  3442. $sid = (int)$dbRow['SCYDGY_ID'];
  3443. }
  3444. return $sid > 0 && isset($pickedSidSet[$sid]);
  3445. }));
  3446. $pool = $this->procuremenPoolFromPurchaseOrderDbRows($dbRows);
  3447. $filtered = $this->filterProcuremenIndexPool($pool, $monthStart, $monthEnd, true, '', [], []);
  3448. $mainBySid = [];
  3449. foreach ($filtered as $rw) {
  3450. if (!is_array($rw)) {
  3451. continue;
  3452. }
  3453. $rid = (int)($rw['ID'] ?? 0);
  3454. if ($rid > 0) {
  3455. $mainBySid[$rid] = $rw;
  3456. }
  3457. }
  3458. if ($mainBySid === []) {
  3459. $detailRows = [];
  3460. } else {
  3461. try {
  3462. $detailRows = Db::table('purchase_order_detail')
  3463. ->where('scydgy_id', 'in', array_keys($mainBySid))
  3464. ->where('status', 1)
  3465. ->order('CCYDH', 'asc')
  3466. ->order('company_name', 'asc')
  3467. ->order('id', 'asc')
  3468. ->select();
  3469. } catch (\Throwable $e) {
  3470. try {
  3471. $detailRows = Db::table('purchase_order_detail')
  3472. ->where('scydgy_id', 'in', array_keys($mainBySid))
  3473. ->where('status', 1)
  3474. ->order('CCYDH', 'asc')
  3475. ->order('company_name', 'asc')
  3476. ->order('ID', 'asc')
  3477. ->select();
  3478. } catch (\Throwable $e2) {
  3479. $detailRows = [];
  3480. }
  3481. }
  3482. }
  3483. if (!is_array($detailRows)) {
  3484. $detailRows = [];
  3485. }
  3486. $exportLines = [];
  3487. foreach ($detailRows as $dr) {
  3488. if (!is_array($dr)) {
  3489. continue;
  3490. }
  3491. $sid = (int)($dr['scydgy_id'] ?? $dr['SCYDGY_ID'] ?? 0);
  3492. if ($sid <= 0 || !isset($mainBySid[$sid])) {
  3493. continue;
  3494. }
  3495. $m = $mainBySid[$sid];
  3496. $exportLines[] = [
  3497. 'CCYDH' => (string)($dr['CCYDH'] ?? $m['CCYDH'] ?? ''),
  3498. 'CYJMC' => (string)($dr['CYJMC'] ?? $m['CYJMC'] ?? ''),
  3499. 'company_name' => (string)($dr['company_name'] ?? ''),
  3500. 'CDF' => (string)($m['CDF'] ?? ''),
  3501. 'cGzzxMc' => (string)($m['cGzzxMc'] ?? ''),
  3502. 'gx' => $this->procuremenExportGxText($m),
  3503. 'detail' => $dr,
  3504. ];
  3505. }
  3506. $groups = [];
  3507. foreach ($exportLines as $line) {
  3508. $k = $line['CCYDH'] . "\x1f" . $line['company_name'];
  3509. if (!isset($groups[$k])) {
  3510. $groups[$k] = [];
  3511. }
  3512. $groups[$k][] = $line;
  3513. }
  3514. ksort($groups, SORT_STRING);
  3515. $spreadsheet = new Spreadsheet();
  3516. $sheet = $spreadsheet->getActiveSheet();
  3517. $sheet->setTitle('外发明细');
  3518. $mon = (int)substr($ym, 5, 2);
  3519. $sheet->mergeCells('A1:H1');
  3520. $sheet->setCellValue('A1', $mon . '月外发明细');
  3521. $sheet->getStyle('A1')->getFont()->setBold(true)->setSize(14);
  3522. $sheet->getStyle('A1')->getAlignment()
  3523. ->setHorizontal(Alignment::HORIZONTAL_CENTER)
  3524. ->setVertical(Alignment::VERTICAL_CENTER);
  3525. $headers = ['序号', '传票号', '传票名称', '外加工单位', '订法', '客户名称', '工序', '加工金额'];
  3526. $col = 'A';
  3527. foreach ($headers as $h) {
  3528. $sheet->setCellValue($col . '2', $h);
  3529. $col++;
  3530. }
  3531. $sheet->getStyle('A2:H2')->getFont()->setBold(true);
  3532. $sheet->getStyle('A2:H2')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  3533. $rowNum = 3;
  3534. $sumSubtotalCounts = 0;
  3535. $grandAmount = 0.0;
  3536. if ($groups === []) {
  3537. $sheet->mergeCells('A3:H3');
  3538. $sheet->setCellValue('A3', '(当月暂无符合条件的外发明细)');
  3539. $sheet->getStyle('A3')->getAlignment()->setHorizontal(Alignment::HORIZONTAL_CENTER);
  3540. $rowNum = 4;
  3541. } else {
  3542. foreach ($groups as $items) {
  3543. if ($items === []) {
  3544. continue;
  3545. }
  3546. $groupLineCount = count($items);
  3547. $subAmount = 0.0;
  3548. $i = 0;
  3549. foreach ($items as $line) {
  3550. $i++;
  3551. $dr = $line['detail'];
  3552. $amt = $this->procuremenExportAmount($dr);
  3553. $subAmount += $amt;
  3554. $sheet->setCellValue('A' . $rowNum, $i);
  3555. $sheet->setCellValue('B' . $rowNum, $line['CCYDH']);
  3556. $sheet->setCellValue('C' . $rowNum, $line['CYJMC']);
  3557. $sheet->setCellValue('D' . $rowNum, $line['company_name']);
  3558. $sheet->setCellValue('E' . $rowNum, $line['CDF']);
  3559. $sheet->setCellValue('F' . $rowNum, $line['cGzzxMc']);
  3560. $sheet->setCellValue('G' . $rowNum, $line['gx']);
  3561. $sheet->setCellValue('H' . $rowNum, $amt);
  3562. $sheet->getStyle('H' . $rowNum)->getNumberFormat()->setFormatCode('"¥"#,##0.00');
  3563. $rowNum++;
  3564. }
  3565. $sumSubtotalCounts += $groupLineCount;
  3566. $grandAmount += $subAmount;
  3567. $sheet->setCellValue('A' . $rowNum, $groupLineCount);
  3568. $sheet->mergeCells('G' . $rowNum . ':H' . $rowNum);
  3569. $sheet->setCellValue('G' . $rowNum, '¥ ' . number_format($subAmount, 2, '.', ','));
  3570. $sheet->getStyle('G' . $rowNum)->getAlignment()
  3571. ->setHorizontal(Alignment::HORIZONTAL_RIGHT)
  3572. ->setVertical(Alignment::VERTICAL_CENTER);
  3573. $rowNum++;
  3574. }
  3575. }
  3576. $sheet->setCellValue('A' . $rowNum, '总计');
  3577. $sheet->setCellValue('B' . $rowNum, $sumSubtotalCounts);
  3578. $sheet->mergeCells('G' . $rowNum . ':H' . $rowNum);
  3579. $sheet->setCellValue('G' . $rowNum, '¥ ' . number_format($grandAmount, 2, '.', ','));
  3580. $sheet->getStyle('A' . $rowNum . ':H' . $rowNum)->getFont()->setBold(true);
  3581. $sheet->getStyle('G' . $rowNum)->getAlignment()->setHorizontal(Alignment::HORIZONTAL_RIGHT);
  3582. $lastRow = $rowNum;
  3583. $sheet->getStyle('A1:H' . $lastRow)->applyFromArray([
  3584. 'borders' => [
  3585. 'allBorders' => [
  3586. 'borderStyle' => Border::BORDER_THIN,
  3587. 'color' => ['rgb' => '000000'],
  3588. ],
  3589. ],
  3590. ]);
  3591. $sheet->getStyle('A3:H' . $lastRow)->getAlignment()->setVertical(Alignment::VERTICAL_CENTER);
  3592. if ($lastRow >= 3) {
  3593. $sheet->getStyle('C3:C' . $lastRow)->getAlignment()->setWrapText(true)->setVertical(Alignment::VERTICAL_TOP);
  3594. $sheet->getStyle('D3:D' . $lastRow)->getAlignment()->setWrapText(true)->setVertical(Alignment::VERTICAL_TOP);
  3595. $sheet->getStyle('F3:F' . $lastRow)->getAlignment()->setWrapText(true)->setVertical(Alignment::VERTICAL_TOP);
  3596. $sheet->getStyle('G3:G' . $lastRow)->getAlignment()->setWrapText(true)->setVertical(Alignment::VERTICAL_TOP);
  3597. }
  3598. $sheet->getColumnDimension('A')->setWidth(7);
  3599. $sheet->getColumnDimension('B')->setWidth(16);
  3600. $sheet->getColumnDimension('C')->setWidth(52);
  3601. $sheet->getColumnDimension('D')->setWidth(32);
  3602. $sheet->getColumnDimension('E')->setWidth(12);
  3603. $sheet->getColumnDimension('F')->setWidth(44);
  3604. $sheet->getColumnDimension('G')->setWidth(26);
  3605. $sheet->getColumnDimension('H')->setWidth(13);
  3606. $sheet->getRowDimension(1)->setRowHeight(28);
  3607. $fileBase = '外发明细_' . str_replace('-', '', $ym);
  3608. $filename = $fileBase . '.xlsx';
  3609. try {
  3610. list($adminId, $adminName) = $this->GetUseName();
  3611. Db::table('purchase_month_export_log')->insert([
  3612. 'ym' => $ym,
  3613. 'admin_id' => (int)$adminId,
  3614. 'admin_name' => mb_substr((string)$adminName, 0, 64, 'UTF-8'),
  3615. 'row_count' => count($exportLines),
  3616. 'total_amount' => round($grandAmount, 2),
  3617. 'createtime' => time(),
  3618. ]);
  3619. } catch (\Throwable $e) {
  3620. Log::write('month export log: ' . $e->getMessage(), 'error');
  3621. }
  3622. if (ob_get_length()) {
  3623. ob_end_clean();
  3624. }
  3625. $asciiName = 'outward_' . str_replace('-', '', $ym) . '.xlsx';
  3626. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  3627. header('Content-Disposition: attachment;filename="' . $asciiName . '"; filename*=UTF-8\'\'' . rawurlencode($filename));
  3628. header('Cache-Control: max-age=0');
  3629. $writer = new Xlsx($spreadsheet);
  3630. $writer->save('php://output');
  3631. $spreadsheet->disconnectWorksheets();
  3632. unset($spreadsheet);
  3633. exit;
  3634. }
  3635. /**
  3636. * 导出用:工序列文案
  3637. */
  3638. protected function procuremenExportGxText(array $r)
  3639. {
  3640. $a = trim((string)($r['CDXMC'] ?? ''));
  3641. $b = trim((string)($r['CGYMC'] ?? ''));
  3642. if ($a !== '' && $b !== '') {
  3643. return $a . ':' . $b;
  3644. }
  3645. return $a !== '' ? $a : $b;
  3646. }
  3647. /**
  3648. * 导出用:加工金额(表中有 amount/jje/jgje 等则读取,否则 0)
  3649. */
  3650. protected function procuremenExportAmount(array $r)
  3651. {
  3652. foreach (['amount', 'jje', 'jgje', 'processing_amount'] as $k) {
  3653. if (!array_key_exists($k, $r)) {
  3654. continue;
  3655. }
  3656. $v = $r[$k];
  3657. if ($v === null || $v === '') {
  3658. continue;
  3659. }
  3660. if (is_numeric($v)) {
  3661. return (float)$v;
  3662. }
  3663. $v = preg_replace('/[^\d\.\-]/', '', (string)$v);
  3664. if ($v !== '' && is_numeric($v)) {
  3665. return (float)$v;
  3666. }
  3667. }
  3668. return 0.0;
  3669. }
  3670. /**
  3671. * 订单号等用于 PDF 文件名片段(去掉路径非法字符)
  3672. */
  3673. protected function sanitizePurchaseConfirmPdfOrderKey(string $ccydh): string
  3674. {
  3675. $s = trim($ccydh);
  3676. if ($s === '') {
  3677. return 'ORDER';
  3678. }
  3679. $s = preg_replace('@[\\\\/:*?"<>|\\s]+@u', '_', $s);
  3680. $s = trim($s, '._-');
  3681. if ($s === '') {
  3682. return 'ORDER';
  3683. }
  3684. if (function_exists('mb_substr')) {
  3685. return mb_substr($s, 0, 80, 'UTF-8');
  3686. }
  3687. return strlen($s) <= 80 ? $s : substr($s, 0, 80);
  3688. }
  3689. /**
  3690. * 采购确认 PDF 相对路径(与 OSS 对象键一致,无前导斜杠):
  3691. * xinhua/年/月/日/scydgy_id/订单号_scydgy_id.pdf
  3692. *
  3693. * @return array{objectKey: string, webPath: string}
  3694. */
  3695. protected function buildPurchaseConfirmPdfPaths(int $scydgyId, string $ccydhRaw): array
  3696. {
  3697. $sid = (int)$scydgyId;
  3698. $safeOrder = $this->sanitizePurchaseConfirmPdfOrderKey($ccydhRaw);
  3699. $y = date('Y');
  3700. $m = date('m');
  3701. $d = date('d');
  3702. $basename = $safeOrder . '_' . $sid . '.pdf';
  3703. $objectKey = 'xinhua/' . $y . '/' . $m . '/' . $d . '/' . $sid . '/' . $basename;
  3704. return [
  3705. 'objectKey' => $objectKey,
  3706. 'webPath' => '/' . $objectKey,
  3707. ];
  3708. }
  3709. /**
  3710. * 采购确认成功后:用与「详情」弹窗相同的模板片段渲染 HTML,再存为 PDF(改 details_fragment 后 PDF 同步变化)。
  3711. * 优先上传至阿里云 OSS(application/config.php 的 oss 节点);失败或未配置时回退到 public 下与 objectKey 相同目录结构。
  3712. * 成功后将相对路径写入 purchase_order.pdf_url(形如 /xinhua/年/月/日/scydgy_id/订单号_scydgy_id.pdf)。
  3713. *
  3714. * @return string OSS 返回 https 完整 URL;本地回退为以 / 开头的 Web 路径;失败返回空串
  3715. */
  3716. protected function savePurchaseConfirmDetailPdf(int $scydgyId, int $purchaseOrderId): string
  3717. {
  3718. if (!$this->isValidScydgyRowId($scydgyId)) {
  3719. return '';
  3720. }
  3721. $ids = trim((string)$scydgyId);
  3722. $prep = $this->prepareProcuremenDetailsView($ids);
  3723. if (!$prep['ok']) {
  3724. return '';
  3725. }
  3726. $ccydh = $prep['ccydh'];
  3727. $paths = $this->buildPurchaseConfirmPdfPaths((int)$scydgyId, $ccydh);
  3728. $objectKey = $paths['objectKey'];
  3729. $webPath = $paths['webPath'];
  3730. $meta = sprintf('工序行ID %s | 主表订单ID %d | PDF生成时间 %s', $ids, (int)$purchaseOrderId, date('Y-m-d H:i:s'));
  3731. $this->view->assign([
  3732. 'pdf_export' => 1,
  3733. 'pdfMetaLine' => $meta,
  3734. ]);
  3735. // 关闭后台 layout,避免 default 布局里的「控制台」面包屑等被打进 PDF
  3736. $restoreLayout = !empty($this->layout) ? ('layout/' . $this->layout) : false;
  3737. $this->view->engine->layout(false);
  3738. try {
  3739. $html = $this->view->fetch('procuremen/details_pdf_shell');
  3740. } catch (\Throwable $e) {
  3741. if ($restoreLayout) {
  3742. $this->view->engine->layout($restoreLayout);
  3743. }
  3744. Log::write('采购确认PDF模板渲染失败: ' . $e->getMessage(), 'error');
  3745. $this->view->assign(['pdf_export' => '', 'pdfMetaLine' => '']);
  3746. return '';
  3747. }
  3748. if ($restoreLayout) {
  3749. $this->view->engine->layout($restoreLayout);
  3750. }
  3751. $this->view->assign(['pdf_export' => '', 'pdfMetaLine' => '']);
  3752. $tempDir = ROOT_PATH . 'runtime' . DIRECTORY_SEPARATOR . 'mpdf_tmp';
  3753. if (!is_dir($tempDir)) {
  3754. @mkdir($tempDir, 0755, true);
  3755. }
  3756. $tempPdf = $tempDir . DIRECTORY_SEPARATOR . uniqid('pc_pdf_', true) . '.pdf';
  3757. try {
  3758. $mpdf = new \Mpdf\Mpdf([
  3759. 'mode' => 'utf-8',
  3760. 'format' => 'A4',
  3761. 'margin_left' => 12,
  3762. 'margin_right' => 12,
  3763. 'margin_top' => 14,
  3764. 'margin_bottom' => 14,
  3765. 'tempDir' => $tempDir,
  3766. 'autoScriptToLang' => true,
  3767. 'autoLangToFont' => true,
  3768. ]);
  3769. // 与当前站点 HTTP_HOST 不同的占位 base,使 basepathIsLocal=false,避免 mPDF CssManager
  3770. // 在 parse_url 得到有 scheme 无 host 时对 $tr['host'] 触发「Undefined index: host」(日志已复现)。
  3771. $mpdf->SetBasePath('http://127.0.0.1/');
  3772. $mpdf->WriteHTML($html);
  3773. $mpdf->Output($tempPdf, \Mpdf\Output\Destination::FILE);
  3774. } catch (\Throwable $e) {
  3775. Log::write('采购确认PDF写入失败: ' . $e->getMessage(), 'error');
  3776. if (is_file($tempPdf)) {
  3777. @unlink($tempPdf);
  3778. }
  3779. return '';
  3780. }
  3781. $ossUrl = AliyunOss::uploadLocalFile($tempPdf, $objectKey);
  3782. if ($ossUrl !== '') {
  3783. @unlink($tempPdf);
  3784. $this->persistPurchaseOrderPdfUrl((int)$scydgyId, $webPath);
  3785. return $ossUrl;
  3786. }
  3787. $pi = pathinfo($objectKey);
  3788. $dirRel = isset($pi['dirname']) ? (string)$pi['dirname'] : 'xinhua';
  3789. $baseFile = isset($pi['basename']) ? (string)$pi['basename'] : '';
  3790. if ($baseFile === '') {
  3791. @unlink($tempPdf);
  3792. return '';
  3793. }
  3794. $dir = ROOT_PATH . 'public' . DIRECTORY_SEPARATOR . str_replace('/', DIRECTORY_SEPARATOR, $dirRel);
  3795. if (!is_dir($dir) && !@mkdir($dir, 0755, true)) {
  3796. Log::write('采购确认PDF目录创建失败: ' . $dir, 'error');
  3797. @unlink($tempPdf);
  3798. return '';
  3799. }
  3800. $fullPath = $dir . DIRECTORY_SEPARATOR . $baseFile;
  3801. if (!@copy($tempPdf, $fullPath)) {
  3802. Log::write('采购确认PDF复制到本地失败: ' . $fullPath, 'error');
  3803. @unlink($tempPdf);
  3804. return '';
  3805. }
  3806. @unlink($tempPdf);
  3807. $this->persistPurchaseOrderPdfUrl((int)$scydgyId, $webPath);
  3808. return $webPath;
  3809. }
  3810. /**
  3811. * 采购确认 PDF 生成成功后写入 purchase_order.pdf_url(列不存在时仅记日志)
  3812. */
  3813. protected function persistPurchaseOrderPdfUrl(int $scydgyId, string $webPath): void
  3814. {
  3815. if ($scydgyId <= 0 || $webPath === '') {
  3816. return;
  3817. }
  3818. try {
  3819. Db::table('purchase_order')->where('scydgy_id', $scydgyId)->update(['pdf_url' => $webPath]);
  3820. } catch (\Throwable $e) {
  3821. Log::write('purchase_order.pdf_url 更新失败 scydgy_id=' . $scydgyId . ' ' . $e->getMessage(), 'notice');
  3822. }
  3823. }
  3824. /**
  3825. * 整理短信变量:去空格;联系人姓名为空时用公司名称
  3826. *
  3827. * @param array<string, string> $vars
  3828. * @return array<string, string>
  3829. */
  3830. protected function normalizeSmsTemplateVars(array $vars): array
  3831. {
  3832. $out = [];
  3833. foreach ($vars as $k => $v) {
  3834. $out[(string)$k] = trim((string)$v);
  3835. }
  3836. if (($out['contact_name'] ?? '') === '' && ($out['company_name'] ?? '') !== '') {
  3837. $out['contact_name'] = $out['company_name'];
  3838. }
  3839. return $out;
  3840. }
  3841. /**
  3842. * 按手机号或公司名称查 customer 表联系人姓名
  3843. */
  3844. protected function resolveCustomerContactName(string $phone, string $companyName): string
  3845. {
  3846. $phone = trim($phone);
  3847. $companyName = trim($companyName);
  3848. try {
  3849. if ($phone !== '' && preg_match('/^1\d{10}$/', $phone)) {
  3850. $row = Db::table('customer')
  3851. ->where(function ($q) use ($phone) {
  3852. $q->where('phone', $phone)->whereOr('account', $phone);
  3853. })
  3854. ->order('id', 'asc')
  3855. ->find();
  3856. if (is_array($row)) {
  3857. $nm = trim((string)($row['username'] ?? ''));
  3858. if ($nm !== '') {
  3859. return $nm;
  3860. }
  3861. }
  3862. }
  3863. if ($companyName !== '') {
  3864. $row = Db::table('customer')->where('company_name', $companyName)->order('id', 'asc')->find();
  3865. if (is_array($row)) {
  3866. return trim((string)($row['username'] ?? ''));
  3867. }
  3868. }
  3869. } catch (\Throwable $e) {
  3870. }
  3871. return '';
  3872. }
  3873. /**
  3874. * 模版纯文本转邮件 HTML:换行转 &lt;br&gt;,保留已替换进的 &lt;a&gt; 等标签
  3875. */
  3876. protected function plainTextToHtmlEmailBody(string $text): string
  3877. {
  3878. $text = str_replace(["\r\n", "\r"], "\n", (string)$text);
  3879. $parts = explode("\n", $text);
  3880. $out = [];
  3881. foreach ($parts as $line) {
  3882. if (preg_match('/<a\s+[^>]*href=/i', $line)) {
  3883. $out[] = $line;
  3884. } else {
  3885. $out[] = htmlspecialchars($line, ENT_QUOTES, 'UTF-8');
  3886. }
  3887. }
  3888. return implode("<br>\n", $out);
  3889. }
  3890. /**
  3891. * 短信场景:去掉链接类变量,避免误填 URL
  3892. *
  3893. * @param array<string, string> $vars
  3894. * @return array<string, string>
  3895. */
  3896. protected function stripLinkVarsForSmsScene(string $scene, array $vars): array
  3897. {
  3898. if (!in_array($scene, ['review_sms', 'confirm_ok', 'confirm_fail'], true)) {
  3899. return $vars;
  3900. }
  3901. foreach (['platform_url', 'platform_links', 'platform_links_html', 'process_lines_html'] as $k) {
  3902. $vars[$k] = '';
  3903. }
  3904. return $vars;
  3905. }
  3906. /**
  3907. * 读取通知模版并替换变量;无模版、正文为空或 status≠1 时抛异常(不使用代码内兜底文案)
  3908. *
  3909. * @param array<string, string> $vars
  3910. */
  3911. protected function renderNotifyTemplate(string $scene, array $vars): string
  3912. {
  3913. $vars = $this->normalizeSmsTemplateVars($vars);
  3914. $vars = $this->stripLinkVarsForSmsScene($scene, $vars);
  3915. $row = $this->loadNotifyTemplateRow($scene);
  3916. if ($row === null) {
  3917. throw new \Exception($this->notifyTemplateMissingMessage($scene));
  3918. }
  3919. $tpl = trim((string)($row['content'] ?? ''));
  3920. if ($tpl === '') {
  3921. throw new \Exception($this->notifyTemplateMissingMessage($scene));
  3922. }
  3923. foreach ($vars as $k => $v) {
  3924. $tpl = str_replace('{' . $k . '}', (string)$v, $tpl);
  3925. }
  3926. return $tpl;
  3927. }
  3928. protected function notifyTemplateMissingMessage(string $scene, bool $titleRequired = false): string
  3929. {
  3930. $map = [
  3931. 'review_email' => '外发下发-邮箱',
  3932. 'review_sms' => '外发下发-短信',
  3933. 'confirm_ok' => '采购确认-通过',
  3934. 'confirm_fail' => '采购确认-未通过',
  3935. ];
  3936. $label = $map[$scene] ?? $scene;
  3937. if ($titleRequired) {
  3938. return "通知模版「{$label}」({$scene})未配置、已禁用或缺少邮件标题,请在后台「短信模版配置」维护后再操作";
  3939. }
  3940. return "通知模版「{$label}」({$scene})未配置、已禁用或正文为空,请在后台「短信模版配置」维护后再操作";
  3941. }
  3942. /** @deprecated 使用 renderNotifyTemplate */
  3943. protected function renderSmsTemplate(string $scene, array $vars): string
  3944. {
  3945. return $this->renderNotifyTemplate($scene, $vars);
  3946. }
  3947. /**
  3948. * 是否开启外发通知演练(不发真实短信/邮件)
  3949. */
  3950. protected function isProcuremenNotifyDryRun(): bool
  3951. {
  3952. return (bool)Config::get('procuremen_notify_dry_run');
  3953. }
  3954. /**
  3955. * @param array<string, mixed> $payload
  3956. */
  3957. protected function recordNotifyDryRunPreview(array $payload): void
  3958. {
  3959. $this->notifyDryRunPreview[] = $payload;
  3960. Log::write('[外发通知演练] ' . json_encode($payload, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES), 'notice');
  3961. }
  3962. /**
  3963. * 短信宝发送;失败抛异常(供事务回滚,避免「已入库但未通知」与「通知失败仍入库」)
  3964. * @throws \Exception
  3965. */
  3966. protected function smsbao($phone, $content)
  3967. {
  3968. if ($this->isProcuremenNotifyDryRun()) {
  3969. $this->recordNotifyDryRunPreview([
  3970. 'scene' => 'sms_only',
  3971. 'phone' => $phone,
  3972. 'sms_content' => $content,
  3973. ]);
  3974. return;
  3975. }
  3976. $statusStr = [
  3977. '0' => '短信发送成功',
  3978. '-1' => '参数不全',
  3979. '-2' => '服务器空间不支持,请确认支持curl或者fsocket,联系您的空间商解决或者更换空间!',
  3980. '30' => '密码错误',
  3981. '40' => '账号不存在',
  3982. '41' => '余额不足',
  3983. '42' => '帐户已过期',
  3984. '43' => 'IP地址限制',
  3985. '50' => '内容含有敏感词',
  3986. ];
  3987. $smsapi = 'http://api.smsbao.com/';
  3988. $user = 'zhuwei123';
  3989. $pass = md5('1d1e605c101e4c1f8a156c6d7b19f126');
  3990. $sendurl = $smsapi . 'sms?u=' . $user . '&p=' . $pass . '&m=' . $phone . '&c=' . urlencode($content);
  3991. $result = @file_get_contents($sendurl);
  3992. if ($result === false) {
  3993. \think\Log::record('smsbao 请求失败 phone=' . $phone, 'error');
  3994. throw new \Exception('短信接口请求失败,请检查网络或稍后再试(未写入数据)');
  3995. }
  3996. $result = trim((string)$result);
  3997. if ($result !== '0') {
  3998. $msg = isset($statusStr[$result]) ? $statusStr[$result] : ('返回码 ' . $result);
  3999. \think\Log::record('smsbao 发送失败 phone=' . $phone . ' code=' . $result . ' ' . $msg, 'error');
  4000. throw new \Exception('短信发送失败:' . $msg . '(' . $phone . '),未写入数据');
  4001. }
  4002. }
  4003. }