Sale.php 296 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826382738283829383038313832383338343835383638373838383938403841384238433844384538463847384838493850385138523853385438553856385738583859386038613862386338643865386638673868386938703871387238733874387538763877387838793880388138823883388438853886388738883889389038913892389338943895389638973898389939003901390239033904390539063907390839093910391139123913391439153916391739183919392039213922392339243925392639273928392939303931393239333934393539363937393839393940394139423943394439453946394739483949395039513952395339543955395639573958395939603961396239633964396539663967396839693970397139723973397439753976397739783979398039813982398339843985398639873988398939903991399239933994399539963997399839994000400140024003400440054006400740084009401040114012401340144015401640174018401940204021402240234024402540264027402840294030403140324033403440354036403740384039404040414042404340444045404640474048404940504051405240534054405540564057405840594060406140624063406440654066406740684069407040714072407340744075407640774078407940804081408240834084408540864087408840894090409140924093409440954096409740984099410041014102410341044105410641074108410941104111411241134114411541164117411841194120412141224123412441254126412741284129413041314132413341344135413641374138413941404141414241434144414541464147414841494150415141524153415441554156415741584159416041614162416341644165416641674168416941704171417241734174417541764177417841794180418141824183418441854186418741884189419041914192419341944195419641974198419942004201420242034204420542064207420842094210421142124213421442154216421742184219422042214222422342244225422642274228422942304231423242334234423542364237423842394240424142424243424442454246424742484249425042514252425342544255425642574258425942604261426242634264426542664267426842694270427142724273427442754276427742784279428042814282428342844285428642874288428942904291429242934294429542964297429842994300430143024303430443054306430743084309431043114312431343144315431643174318431943204321432243234324432543264327432843294330433143324333433443354336433743384339434043414342434343444345434643474348434943504351435243534354435543564357435843594360436143624363436443654366436743684369437043714372437343744375437643774378437943804381438243834384438543864387438843894390439143924393439443954396439743984399440044014402440344044405440644074408440944104411441244134414441544164417441844194420442144224423442444254426442744284429443044314432443344344435443644374438443944404441444244434444444544464447444844494450445144524453445444554456445744584459446044614462446344644465446644674468446944704471447244734474447544764477447844794480448144824483448444854486448744884489449044914492449344944495449644974498449945004501450245034504450545064507450845094510451145124513451445154516451745184519452045214522452345244525452645274528452945304531453245334534453545364537453845394540454145424543454445454546454745484549455045514552455345544555455645574558455945604561456245634564456545664567456845694570457145724573457445754576457745784579458045814582458345844585458645874588458945904591459245934594459545964597459845994600460146024603460446054606460746084609461046114612461346144615461646174618461946204621462246234624462546264627462846294630463146324633463446354636463746384639464046414642464346444645464646474648464946504651465246534654465546564657465846594660466146624663466446654666466746684669467046714672467346744675467646774678467946804681468246834684468546864687468846894690469146924693469446954696469746984699470047014702470347044705470647074708470947104711471247134714471547164717471847194720472147224723472447254726472747284729473047314732473347344735473647374738473947404741474247434744474547464747474847494750475147524753475447554756475747584759476047614762476347644765476647674768476947704771477247734774477547764777477847794780478147824783478447854786478747884789479047914792479347944795479647974798479948004801480248034804480548064807480848094810481148124813481448154816481748184819482048214822482348244825482648274828482948304831483248334834483548364837483848394840484148424843484448454846484748484849485048514852485348544855485648574858485948604861486248634864486548664867486848694870487148724873487448754876487748784879488048814882488348844885488648874888488948904891489248934894489548964897489848994900490149024903490449054906490749084909491049114912491349144915491649174918491949204921492249234924492549264927492849294930493149324933493449354936493749384939494049414942494349444945494649474948494949504951495249534954495549564957495849594960496149624963496449654966496749684969497049714972497349744975497649774978497949804981498249834984498549864987498849894990499149924993499449954996499749984999500050015002500350045005500650075008500950105011501250135014501550165017501850195020502150225023502450255026502750285029503050315032503350345035503650375038503950405041504250435044504550465047504850495050505150525053505450555056505750585059506050615062506350645065506650675068506950705071507250735074507550765077507850795080508150825083508450855086508750885089509050915092509350945095509650975098509951005101510251035104510551065107510851095110511151125113511451155116511751185119512051215122512351245125512651275128512951305131513251335134513551365137513851395140514151425143514451455146514751485149515051515152515351545155515651575158515951605161516251635164516551665167516851695170517151725173517451755176517751785179518051815182518351845185518651875188518951905191519251935194519551965197519851995200520152025203520452055206520752085209521052115212521352145215521652175218521952205221522252235224522552265227522852295230523152325233523452355236523752385239524052415242524352445245524652475248524952505251525252535254525552565257525852595260526152625263526452655266526752685269527052715272527352745275527652775278527952805281528252835284528552865287528852895290529152925293529452955296529752985299530053015302530353045305530653075308530953105311531253135314531553165317531853195320532153225323532453255326532753285329533053315332533353345335533653375338533953405341534253435344534553465347534853495350535153525353535453555356535753585359536053615362536353645365536653675368536953705371537253735374537553765377537853795380538153825383538453855386538753885389539053915392539353945395539653975398539954005401540254035404540554065407540854095410541154125413541454155416541754185419542054215422542354245425542654275428542954305431543254335434543554365437543854395440544154425443544454455446544754485449545054515452545354545455545654575458545954605461546254635464546554665467546854695470547154725473547454755476547754785479548054815482548354845485548654875488548954905491549254935494549554965497549854995500550155025503550455055506550755085509551055115512551355145515551655175518551955205521552255235524552555265527552855295530553155325533553455355536553755385539554055415542554355445545554655475548554955505551555255535554555555565557555855595560556155625563556455655566556755685569557055715572557355745575557655775578557955805581558255835584558555865587558855895590559155925593559455955596559755985599560056015602560356045605560656075608560956105611561256135614561556165617561856195620562156225623562456255626562756285629563056315632563356345635563656375638563956405641564256435644564556465647564856495650565156525653565456555656565756585659566056615662566356645665566656675668566956705671567256735674567556765677567856795680568156825683568456855686568756885689
  1. <?php
  2. namespace app\admin\controller;
  3. use app\admin\model\ConsultBids;
  4. use app\admin\model\ConsultTemp;
  5. use app\admin\model\DataGroup as DataGroupModel;
  6. use app\admin\model\GoodLog;
  7. use app\admin\model\GoodPlatform;
  8. use app\admin\model\GoodStockInfo;
  9. use app\admin\model\GoodTax;
  10. use app\admin\model\OrderOutChild;
  11. use app\admin\model\OrderTax;
  12. use app\admin\model\ProcessOrder;
  13. use app\admin\model\SaleCgdPrice;
  14. use app\bbc\model\BbcFill;
  15. use app\model\TaxCategory;
  16. use Exception;
  17. use think\App;
  18. use think\facade\Db;
  19. use app\admin\model\ActionLog;
  20. use think\facade\Validate;
  21. //销售单
  22. class Sale extends Base
  23. {
  24. public $noble = [];
  25. public function __construct(App $app)
  26. {
  27. parent::__construct($app);
  28. $this->noble = \think\facade\Config::get("noble");
  29. }
  30. public function create()
  31. {
  32. $orderCode = makeNo("QR");
  33. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code'] !== "" ? trim($this->post['customer_code']) : "";
  34. if ($customer_code == "") {
  35. return error_show(1002, "客户customer_code不能为空");
  36. }
  37. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  38. $userCommon = \app\admin\common\User::getIns();
  39. $tmp = $userCommon->handle('getCodeAndName',['code'=>[$customer_code,$supplierNo]]);
  40. if(!isset($tmp['code']) || $tmp['code']!=0) return json_show($tmp['code'],$tmp['message'],$tmp['data']);
  41. if(!isset($tmp['data'][$customer_code])) return json_show(1004, "未找到客户数据");
  42. if(!isset($tmp['data'][$supplierNo])) return json_show(1004, '未找到平台供应商数据');
  43. $goodtype = isset($this->post['goodtype']) && $this->post['goodtype'] !== "" ? intval($this->post['goodtype']) : "1";
  44. $order_type = isset($this->post['order_type']) && $this->post['order_type'] !== "" ? intval($this->post['order_type']) : "1";
  45. $sendtype = isset($this->post['sendtype']) && $this->post['sendtype'] !== "" ? intval($this->post['sendtype']) : "";
  46. if ($sendtype === '') {
  47. return error_show(1004, "参数sendtype不能为空");
  48. }
  49. $platform_id = isset($this->post['platform_id']) && $this->post['platform_id'] !== "" ? intval($this->post['platform_id']) : "";
  50. if ($platform_id == '') {
  51. return error_show(1004, "参数platform_id不能为空");
  52. }
  53. $platforminfo = Db::name("platform")->where(["id"=> $platform_id,"is_del"=>0])->findOrEmpty();
  54. if (empty($platforminfo)) return error_show(1004, "平台信息未找到");
  55. if ($platforminfo['status'] != 1) return error_show(1004, "平台信息已禁用");
  56. if($platforminfo['is_select_pay_rate']==1 && channel_is_company($platform_id,$supplierNo))return error_show(1004, "平台渠道包含当前业务公司 {$tmp['data'][$supplierNo]}");
  57. $platform_order = isset($this->post['platform_order']) && $this->post['platform_order'] !== "" ? trim($this->post['platform_order']) : "";
  58. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  59. if ($good_code == '') {
  60. return error_show(1004, "参数good_code不能为空");
  61. }
  62. $good_num = isset($this->post['good_num']) && $this->post['good_num'] !== "" ? intval($this->post['good_num']) : "";
  63. if ($good_num === '') {
  64. return error_show(1004, "参数good_num不能为空");
  65. }
  66. $arrtime = isset($this->post['arrtime']) && $this->post['arrtime'] != "" ? $this->post['arrtime'] : "";
  67. if ($arrtime == "") {
  68. return error_show(1004, "参数arrtime不能为空");
  69. }
  70. $manager = isset($this->post['manager']) && $this->post['manager'] != '' ? $this->post['manager'] :$this->uname;
  71. $managerid = isset($this->post['managerid']) && $this->post['managerid'] != '' ? $this->post['managerid'] :$this->uid;
  72. $paytime = isset($this->post['paytime']) && $this->post['paytime'] != "" ? $this->post['paytime'] : "";
  73. $workNo = isset($this->post['workNo']) && $this->post['workNo'] != "" ? trim($this->post['workNo']) : "";
  74. $ct = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  75. ->where(['a.skuCode' => $good_code])->field("b.*,a.skuCode,a.platform_code,a.plat_code,a.is_fixed")
  76. ->findOrEmpty();
  77. if (empty($ct)) {
  78. return error_show(1002, "未找到商品数据");
  79. }
  80. $holder_id=0;
  81. $holder_name='';
  82. $supplier_name=$ct['supplierName'];
  83. if($ct['is_combind']==0){
  84. $supplier_temp =$userCommon->handle('sInfo',['code'=>$ct['supplierNo']]);
  85. if($supplier_temp['code']!=0 || empty($supplier_temp['data']) ) return error_show(1002,'采购单供应商不存在');
  86. $holder_id=$supplier_temp['data']['personid']??0;
  87. $holder_name=$supplier_temp['data']['person']??'';
  88. $supplier_name=$supplier_temp['data']['name']??'';
  89. }
  90. $goodinfo = $ct;
  91. $is_stock = $ct['is_stock'];
  92. $spuCode = $ct['spuCode'];
  93. $skuCode = $ct['skuCode'];
  94. $is_activity = isset($this->post['is_activity']) && $this->post['is_activity'] !== "" ? $this->post['is_activity'] : "";
  95. $actcode = isset($this->post['act_code']) && $this->post['act_code'] !== "" ? trim($this->post['act_code']) : "";
  96. if ($order_type == 1) {
  97. if ($is_activity === "") {
  98. return error_show(1002, "参数is_activity不能为空");
  99. }
  100. if ($is_activity == 1 && $actcode == "") {
  101. return error_show(1002, "参数act_code不能为空");
  102. }
  103. }
  104. $origin = Db::name('good_nake')->where([['spuCode', '=', $spuCode], ['min_num', '<=', $good_num], ['is_del', '=', 0]])->order('min_num desc')->find();
  105. $origin_price = $origin['nake_total']??0;
  106. $cgd_origin_price = $origin['supplier_origin_price']??0;
  107. if ($goodinfo['is_stock'] == 1) {
  108. $stock_num = Db::name("good")->where(["spuCode"=> $spuCode])->value("usable_stock",0);
  109. if($stock_num < $good_num){
  110. return error_show(1003, "商品可用库存数量不足");
  111. }
  112. $origin_price = 0;
  113. }
  114. $sale_price = isset($this->post['good_price']) && $this->post['good_price'] !== "" ? floatval($this->post['good_price']) : '';
  115. $ct['cgd_gold_price'] = 0;
  116. if ($goodtype == 1) {
  117. if ($is_activity == 1) {
  118. $act = Db::name("activity_info")->alias("a")->leftJoin("good_activity b", "a.activity_code=b.activity_code")
  119. ->where(["a.skuCode" => $skuCode, "a.activity_code" => $actcode, "a.is_del" => 0, "a.status" => 1, "b.status" => 6, "b.is_del" => 0])
  120. ->find();
  121. if ($act == false) {
  122. return error_show(1003, "未找到相关活动价");
  123. }
  124. if ($act['moq_num'] > $good_num) {
  125. return error_show(1003, "商品不满足活动价起订量{$act['moq_num']}");
  126. }
  127. if ($act['activity_stock'] < $good_num) {
  128. return error_show(1003, "商品活动库存剩余{$act['activity_stock']}");
  129. }
  130. $sale_price = $sale_price !== "" ? $sale_price : $act['activity_price'];
  131. $origin_price = $act['cost_price'];
  132. } else {
  133. if ($origin == false) {
  134. return error_show(1003, '未找到相关阶梯成本价格');
  135. }
  136. $good = Db::name("good_ladder")->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1])->where([["min_num", "<=", $good_num]])->order("min_num desc")->find();
  137. if ($good == false) {
  138. return error_show(1003, "未找到相关阶梯价格");
  139. }
  140. $sale_price = $sale_price !== "" ? $sale_price : $good['sale_price'];
  141. if ($ct['is_gold_price'] == 1 && $is_stock != 1) {
  142. $gold = Db::name("gold_price1")
  143. ->field('id,price')
  144. ->where(["type" => $ct['noble_metal'], "is_del" => 0, "status" => 1])
  145. ->order("addtime desc")
  146. ->find();
  147. if($ct['is_fixed']==0) $origin['nake_fee']=0;
  148. //$saleprice(最终售价) = (打样费/购买数量 + 开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价+物流费)/(1-成本售价/100);
  149. $sale_price = $sale_price !== "" ? $sale_price : ($ct['demo_fee'] / $good_num + $ct['open_fee'] / $good_num
  150. + $ct['noble_weight'] * $gold["price"] + $good['cost_fee'] * $ct['noble_weight'] + $origin['package_fee'] + $origin['mark_fee'] + $origin['cert_fee'] + $origin['nake_fee'] + $origin['delivery_fee']);
  151. $ct['cgd_gold_price'] = $gold["price"];
  152. }
  153. }
  154. } else {
  155. if ($origin == false) {
  156. return error_show(1003, '未找到相关阶梯成本价格');
  157. }
  158. $sale_price = 0;
  159. }
  160. $cgd = [
  161. "supplierNo" => $ct['supplierNo'],
  162. "supplierName" => $supplier_name,
  163. "companyNo" => $supplierNo,
  164. "companyName" => $tmp['data'][$supplierNo]??"",
  165. "orderCode" => $orderCode,
  166. "spuCode" => $ct['spuCode'],
  167. "skuCode" => $ct['skuCode'],
  168. "good_name" => $ct['good_name'],
  169. "sale_price" => $origin_price,
  170. "total_fee" => $origin_price * $good_num,
  171. "pakge_fee" => isset($origin['package_fee']) ? $origin['package_fee'] : 0,
  172. "cert_fee" => isset($origin['cert_fee']) ? $origin['cert_fee'] : 0,
  173. "open_fee" => $ct['open_fee'],
  174. "cost_fee" => isset($origin['cost_fee']) ? $origin['cost_fee'] : 0,
  175. "mark_fee" => isset($origin['mark_fee']) ? $origin['mark_fee'] : 0,
  176. "nake_fee" => isset($origin['nake_fee']) ? $origin['nake_fee'] : 0,
  177. "delivery_fee" => isset($origin['delivery_fee']) ? $origin['delivery_fee'] : 0,
  178. "demo_fee" => $ct['demo_fee'],
  179. "good_num" => $good_num,
  180. "good_type" => $goodtype,
  181. "weight" => $ct['noble_weight'],
  182. "gold_price" => isset($ct['cgd_gold_price']) ? $ct['cgd_gold_price'] : 0,
  183. "order_type" => $is_stock == 1 ? 1 : 2,
  184. "order_source" => 1,//1直接下单
  185. "createrid" =>$holder_id,
  186. "creater" =>$holder_name,
  187. 'send_way' => 2,
  188. "open_type"=>$ct['open_type'],
  189. 'good_createrid' => $goodinfo['createrid'],
  190. 'good_creater' => $goodinfo['creater'],//商品创建人
  191. 'supplier_origin_price'=>$cgd_origin_price,//供应商采购成本
  192. 'cgd_supplier_code'=>$goodinfo['cgd_supplier_code'],//采购供应商编号
  193. 'cgd_supplier_name'=>$goodinfo['cgd_supplier_name'],//采购供应商编号
  194. 'cgd_apply_id'=>$goodinfo['cgd_apply_id'],//竞单人
  195. 'cgd_apply_name'=>$goodinfo['cgd_apply_name'],//竞单人
  196. ];
  197. $addrlist = isset($this->post['addrlist']) && $this->post['addrlist'] !== "" ? $this->post['addrlist'] : "";
  198. $send_num = 0;
  199. if ($sendtype == 1) {
  200. if ($addrlist == "" || empty($addrlist) || !is_array($addrlist)) {
  201. return error_show(1004, "参数addrlist不能为空");
  202. }
  203. $send_num = array_sum(array_column($addrlist, "receipt_quantity"));
  204. }
  205. if ($send_num > $good_num) {
  206. return error_show(1004, "发货数量超出订单总数量");
  207. }
  208. $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
  209. $proof_id = isset($this->post['proof_id']) && $this->post['proof_id'] !== "" ? intval($this->post['proof_id']) : 0;
  210. $rm = $this->uid;
  211. $ri = $this->uname;
  212. Db::startTrans();
  213. try {
  214. $goodStock = Db::name("good")->where(["spuCode"=> $spuCode])->lock(true)->field("spuCode,usable_stock")
  215. ->findOrEmpty();
  216. if(empty($goodStock)) throw new Exception("未找到商品数据");
  217. $data = [
  218. "orderCode" => $orderCode,
  219. "good_code" => $spuCode,
  220. "skuCode" => $skuCode,
  221. "customer_code" => $customer_code,
  222. "customerName" =>$tmp['data'][$customer_code]??"",
  223. "good_name" => isset($goodinfo['good_name']) && $goodinfo['good_name'] !== '' ? $goodinfo['good_name'] : '',
  224. "good_num" => $good_num,
  225. "cat_id" => $goodinfo['cat_id'],
  226. "apply_id" => $rm,
  227. "apply_name" => $ri,
  228. "origin_price" => $origin_price,
  229. "sale_price" => $sale_price,
  230. "post_fee" => 0,
  231. "status" => 0,
  232. "send_num" => 0,
  233. "wsend_num" => $good_num,
  234. "send_status" => 1,
  235. "good_type" => $goodtype,
  236. "send_type" => $sendtype,
  237. "supplierNo" => $supplierNo,
  238. "supplierName" => $tmp['data'][$supplierNo]??"",
  239. "supName" => $supplier_name,
  240. "supNo" => $ct['supplierNo'],
  241. "is_del" => 0,
  242. "zxNo" => "",
  243. "platform_order" => $platform_order,
  244. "platform_id" => $platform_id,
  245. "remark" => $remark,
  246. "is_stock" => $is_stock,
  247. "is_activity" => $is_activity === "" ? 0 : $is_activity,
  248. "activity_code" => $actcode,
  249. "order_type" => $is_stock == 1 ? 1 : 2,
  250. "order_source" => 1,//1直接下单
  251. 'good_weight' => $ct['noble_weight'],
  252. 'gold_price' => isset($ct['cgd_gold_price']) ? $ct['cgd_gold_price'] : 0,
  253. 'cost_price' => isset($good['cost_fee']) ? $good['cost_fee'] : 0,
  254. 'diff_weight' => 0,
  255. 'diff_fee' => 0,
  256. "workNo" => $workNo,
  257. "use_order"=>$this->post['useid']??0,
  258. 'open_type'=>$ct['open_type'],
  259. "addtime" => date("Y-m-d H:i:s"),
  260. "updatetime" => date("Y-m-d H:i:s"),
  261. 'total_price' => round($sale_price * $good_num, 2),
  262. 'proof_id' => $proof_id,
  263. 'cgderid' => $holder_id,
  264. 'cgder' => $holder_name,//采购员(供应商负责人)
  265. 'good_createrid' => $goodinfo['createrid'],
  266. 'good_creater' => $goodinfo['creater'],//商品创建人
  267. 'manager' => $manager,
  268. 'managerid' => $managerid,//商品创建人
  269. ];
  270. $paytime == "" ? "" : $data['paytime'] = $paytime;
  271. $datainfo = Db::name('sale')->insert($data, true);
  272. if ($datainfo > 0) {
  273. $taxInfo = OrderTax::SpuCat($spuCode);
  274. if (!empty($taxInfo)) {
  275. (new \app\admin\model\OrderTax)->saveAll(array_map(function ($item)use($orderCode){
  276. $temp=[];
  277. $temp['code']=$orderCode;
  278. $temp['spuCode']=$item['spuCode'];
  279. $temp['tax_code']=$item['cat_code'];
  280. $temp['tax_name']=$item['cat_name'];
  281. $temp['tax_short_name']=$item['short_name'];
  282. $temp['inv_tax']=$item['tax'];
  283. $temp['inv_good_name']=$item['inv_good_name'];
  284. return $temp;
  285. },$taxInfo));
  286. }
  287. //修改状态,添加待办
  288. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  289. "order_code" => $orderCode,//销售单code
  290. "status" => 0,//这里的status是之前的值
  291. "action_remark" => '',//备注
  292. "action_type" => "create"//新建create,编辑edit,更改状态status
  293. ], "XSQRD", 0, $this->post);
  294. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  295. "order_type" => 'XSQRD',
  296. "order_code" => $orderCode,//销售单code
  297. "order_id" => $datainfo,
  298. "order_status" => 0,
  299. "before_status" => 0,
  300. 'holder_id' => $data['apply_id']
  301. ]);
  302. if ($is_activity == 1) {
  303. $actup = [
  304. "activity_stock" => $act['activity_stock'] - $good_num,
  305. "updatetime" => date("Y-m-d H:i:s")
  306. ];
  307. $actupp = Db::name("activity_info")->where(["skuCode" => $skuCode, "activity_code" => $actcode,
  308. "is_del" => 0, "status" => 1])->save($actup);
  309. if ($actupp == false) {
  310. throw new Exception("活动库存修改失败");
  311. }
  312. }
  313. $outstatus = 0;
  314. $standing_book_da = ['sale_id' => $datainfo, 'customer_code' => $customer_code, 'skuCode' => $skuCode, 'updatetime' => date('Y-m-d H:i:s')];
  315. if ($is_stock == 0) {
  316. $bol = $this->createCgd($cgd, $standing_book_da);
  317. if ($bol == false) {
  318. throw new Exception("订单创建失败");
  319. }
  320. } else {
  321. $outstatus = 1;
  322. $stockUp =[
  323. "usable_stock"=>$goodStock['usable_stock']-$good_num,
  324. "updatetime"=>date("Y-m-d H:i:s"),
  325. ];
  326. $goodStockUp = Db::name("good")->where($goodStock)->update($stockUp);
  327. if($goodStockUp==false) throw new Exception("商品可用库存更新失败");
  328. SaleCgdPrice::GetPrice($datainfo);
  329. }
  330. if ($sendtype == 1) {
  331. $i=1;
  332. foreach ($addrlist as $value) {
  333. $temp = [];
  334. $addrs = [];
  335. if ($value['addr_code'] !== '' && is_array($value['addr_code']) && !empty($value['addr_code'])) {
  336. $addrs['provice_code'] = $value['addr_code'][0];
  337. $addrs['city_code'] = $value['addr_code'][1];
  338. $addrs['area_code'] = $value['addr_code'][2];
  339. $addr = json_encode($addrs);
  340. } else {
  341. $addr = isset($value['addr_code']) ? $value['addr_code'] : '';
  342. }
  343. if($value['receipt_quantity']<=0) throw new Exception('地址发货数量不能小于1');
  344. $temp['orderCode'] = $orderCode;
  345. $temp['contactor'] = $value['contactor'];
  346. $temp['mobile'] = $value['mobile'];
  347. $temp['addr'] = $value['addr'];
  348. $temp['addr_code'] = $addr;
  349. $temp['customer_code'] = $customer_code;
  350. $temp['receipt_quantity'] = $value['receipt_quantity'];
  351. $temp['post_fee'] = 0;
  352. $temp['is_del'] = 0;
  353. $temp['addtime'] = date("Y-m-d H:i:s");
  354. $temp['updatetime'] = date("Y-m-d H:i:s");
  355. $temp['arrive_time'] = $arrtime;
  356. $vmp = Db::name('order_addr')->insert($temp, true);
  357. if ($vmp > 0) {
  358. $num = $value['receipt_quantity'];
  359. $outCode = makeNo("DF");
  360. //改变编码规则,将原来的outCode后两位换成序列号
  361. //str_pad字符串填充
  362. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  363. $i++;
  364. if($is_stock!=1) {
  365. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  366. ->lock(true)->find();
  367. if ($order == false) {
  368. throw new Exception("未找到可以发货得采购单数据");
  369. }
  370. $tep = [
  371. "cgdNo" => $order['cgdNo'],
  372. "outCode" => $outCode,
  373. "send_num" => $num,
  374. "status" => 1,
  375. "addtime" => date("Y-m-d H:i:s"),
  376. "updatetime" => date("Y-m-d H:i:s")
  377. ];
  378. $sen = Db::name("order_send")->save($tep);
  379. if ($sen == false) {
  380. throw new Exception("发货地址添加创建失败");
  381. }
  382. $cgdinfo = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  383. if ($cgdinfo == false) {
  384. throw new Exception("未匹配到采购数据");
  385. }
  386. }
  387. $out = [
  388. "orderCode" => $orderCode,
  389. "outCode" => $outCode,
  390. "apply_id" => $rm,
  391. "apply_name" => $ri,
  392. "addrid" => $vmp,
  393. "post_name" => "",
  394. "post_code" => "",
  395. "post_fee" => 0,
  396. "sendtime" => date("Y-m-d H:i:s"),
  397. "send_num" => $num,
  398. "check_num" => 0,
  399. "error_num" => 0,
  400. "wsm_code" => $is_stock == 1 ?'':$cgdinfo['wsm_code'],
  401. "order_type" => $is_stock == 1 ? 1 : 2,
  402. "send_status" => $outstatus,
  403. "status" => $outstatus,
  404. "addtime" => date("Y-m-d H:i:s"),
  405. "updatetime" => date("Y-m-d H:i:s")
  406. ];
  407. $ou = Db::name("order_out")->insertGetId($out);
  408. if ($ou == false) {
  409. throw new Exception("发货地址添加创建失败");
  410. // return error_show(1002, "发货地址添加创建失败");
  411. } else {
  412. if($is_stock==1) OrderOutChild::makeChild($outCode);//库存品拆单
  413. //修改状态,添加待办
  414. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  415. "order_code" => $outCode,//出库单号
  416. "status" => $out['status'],//这里的status是之前的值
  417. "action_remark" => '',//备注
  418. "action_type" => "create"//新建create,编辑edit,更改状态status
  419. ], "CKD", $out['status'], $out);
  420. if($out['status']==1 ){
  421. //节点1待库管发货
  422. if($data['is_stock']==0){
  423. //非库存品,推给供应商负责人
  424. // $supp = Db::name('supplier')
  425. // ->field('person,personid')
  426. // ->where('code',$ct['supplierNo'])
  427. // ->findOrEmpty();
  428. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], [
  429. "order_type" => 'CKD',
  430. "order_code" => $outCode,//出库单号
  431. "order_id" => $ou,
  432. "order_status" => $out['status'],
  433. "before_status" => $out['status'],
  434. 'wait_id'=>$holder_id,
  435. 'wait_name'=>$holder_name,
  436. ]);
  437. }else{
  438. //库存品,推给31库管和41库管-张凯旋
  439. $uids = Db::name('user_role')
  440. ->where(['is_del' => 0, 'roleid' => config('app.wsm_cgder_role'), 'status' => 1])
  441. ->column('uid');
  442. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], [
  443. "order_type" => 'CKD',
  444. "order_code" => $outCode,//出库单号
  445. "order_id" => $ou,
  446. "order_status" => $out['status'],
  447. "before_status" => $out['status'],
  448. 'handle_user_list' => implode(',', $uids),
  449. ]);
  450. }
  451. }else{
  452. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], [
  453. "order_type" => 'CKD',
  454. "order_code" => $outCode,//出库单号
  455. "order_id" => $ou,
  456. "order_status" => $out['status'],
  457. "before_status" => $out['status'],
  458. 'holder_id'=>$out['apply_id'],
  459. ]);
  460. }
  461. // $standing_book_da['outCode'] = $outCode;
  462. if (isset($standing_book_da['outCode'])) $standing_book_da['outCode'][] = $outCode;
  463. else $standing_book_da['outCode'] = [$outCode];
  464. }
  465. }
  466. else {
  467. throw new Exception("发货地址添加创建失败");
  468. // return error_show(1002, "发货地址添加创建失败");
  469. }
  470. }
  471. }
  472. //处理台账
  473. if (isset($standing_book_da['outCode'])) $standing_book_da['outCode'] = implode(',', $standing_book_da['outCode']);//发货单号拼接
  474. Db::name('standing_book')->insert(array_merge($standing_book_da, ['addtime' => date('Y-m-d H:i:s'), 'updatetime' => date('Y-m-d H:i:s'), 'standBookNo' => makeNo('IO')]));
  475. Db::commit();
  476. return app_show(0, "订单创建成功", ["order_code" => $orderCode]);
  477. }else{
  478. throw new Exception("订单创建失败");
  479. }
  480. } catch (\Exception $e) {
  481. Db::rollback();
  482. return error_show(1005, $e->getMessage());
  483. }
  484. }
  485. //采购单
  486. public function createCgd($data, array &$standing_book_da = [])
  487. {
  488. $cgdCode = makeNo("CG");
  489. $wsm = Db::name("warehouse_info")->where(["supplierNo" => $data["supplierNo"], "companyNo" => $data['companyNo'], "wsm_type" => 2, "is_del" => 0])->find();
  490. if ($wsm == false) {
  491. $wsm_code = makeNo("WSM");
  492. $inwsm = [
  493. "wsm_code" => $wsm_code,
  494. "name" => $data['supplierName'],
  495. "wsm_type" => 2,
  496. "supplierNo" => $data['supplierNo'],
  497. "supplierName" => $data['supplierName'],
  498. "addr" => "",
  499. "addrs_code" => "",
  500. "contactor" => $data['createrid'] ,
  501. "contactor_name" => $data['creater'] ,
  502. "mobile" => "",
  503. "position" => "",
  504. "companyNo" => $data['companyNo'],
  505. "companyName" => $data['companyName'],
  506. "status" => 1,
  507. "is_del" => 0,
  508. "addtime" => date("Y-m-d H:i:s"),
  509. "updatetime" => date("Y-m-d H:i:s"),
  510. 'createrid' => $this->uid,
  511. 'creater' => $this->uname,
  512. 'updaterid' => $this->uid,
  513. 'updater' => $this->uname,
  514. ];
  515. $in = Db::name("warehouse_info")->insert($inwsm);
  516. if ($in == false) {
  517. throw new Exception("虚拟供应商仓库创建失败");
  518. }
  519. } else {
  520. $wsm_code = $wsm['wsm_code'];
  521. }
  522. //判断该供应商是否开通了供应商账号
  523. $from_tag=checkHasAccountBySupplierNos([$data['supplierNo']]);
  524. $cg = [
  525. "cgdNo" => $cgdCode,
  526. "bkcode" => "",
  527. "wsm_code" => $wsm_code,
  528. "cgder" => $data['creater'],
  529. "cgder_id" => $data['createrid'],
  530. "spuCode" => $data['spuCode'],
  531. "skuCode" => $data['skuCode'],
  532. "good_name" => $data['good_name'],
  533. "good_num" => $data['good_num'],
  534. "good_price" => $data['sale_price'],
  535. "total_fee" => round($data['sale_price'] * $data['good_num'], 2),
  536. "origin_total" => round($data['sale_price'] * $data['good_num'], 2),
  537. "pakge_fee" => $data['pakge_fee'],
  538. "cert_fee" => $data['cert_fee'],
  539. "open_fee" => $data['open_fee'],
  540. "teach_fee" => $data['cost_fee'],
  541. "mark_fee" => $data['mark_fee'],
  542. "demo_fee" => $data['demo_fee'],
  543. "nake_fee" => $data['nake_fee'],
  544. "weight" => $data['weight'],
  545. "delivery_fee" => $data['delivery_fee'],
  546. "gold_price" => $data['gold_price'],
  547. "diff_weight" => "0",
  548. "diff_fee" => "0",
  549. "supplierNo" => $data['supplierNo'],
  550. "supplier_name" => $data['supplierName'],
  551. "companyNo" => $data['companyNo'],
  552. "companyName" => $data['companyName'],
  553. "send_status" => 1,
  554. "send_num" => 0,
  555. "wsend_num" => $data['good_num'],
  556. "remark" => "",
  557. "status" => 0,//0初始化
  558. "lasttime" => date("Y-m-d H:i:s"),
  559. "is_del" => 0,
  560. 'open_type'=>$data['open_type'],
  561. "order_type" => $data['order_type'],
  562. "order_source" => $data['order_source'],
  563. "good_type" => $data['good_type'],
  564. "addtime" => date("Y-m-d H:i:s"),
  565. "updatetime" => date("Y-m-d H:i:s"),
  566. 'good_createrid' => $data['good_createrid'],
  567. 'good_creater' => $data['good_creater'],//商品创建人
  568. 'from_tag' => isset($from_tag[$data['supplierNo']]) ? 2 : 1,//来源标签:1采销(默认),2供应商端
  569. 'supplier_origin_price'=>$data['supplier_origin_price'],//供应商采购成本
  570. 'cgd_supplier_code'=>$data['cgd_supplier_code'],//采购供应商编号
  571. 'cgd_supplier_name'=>$data['cgd_supplier_name'],//采购供应商编号
  572. 'cgd_apply_id'=>$data['cgd_apply_id'],//竞单人
  573. 'cgd_apply_name'=>$data['cgd_apply_name'],//竞单人
  574. ];
  575. $up = Db::name("purchease_order")->insert($cg, true);
  576. if ($up) {
  577. $taxInfo = OrderTax::SpuCat($data['spuCode'],1,1);
  578. if (!empty($taxInfo)) {
  579. (new \app\admin\model\OrderTax)->saveAll(array_map(function ($item)use($cgdCode){
  580. $temp=[];
  581. $temp['code']=$cgdCode;
  582. $temp['spuCode']=$item['spuCode'];
  583. $temp['tax_code']=$item['cat_code'];
  584. $temp['tax_name']=$item['cat_name'];
  585. $temp['tax_short_name']=$item['short_name'];
  586. $temp['inv_tax']=$item['tax'];
  587. $temp['inv_good_name']=$item['inv_good_name'];
  588. return $temp;
  589. },$taxInfo));
  590. }
  591. //修改状态,添加待办
  592. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  593. "order_code" => $cg['cgdNo'],//销售单code
  594. "status" => 0,//这里的status是之前的值
  595. "action_remark" => '',//备注
  596. "action_type" => "create"//新建create,编辑edit,更改状态status
  597. ], "CGD", $cg['status'], $cg);
  598. //当节点是0待与供应商确认,推给供应商负责人
  599. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  600. "order_type" => 'CGD',
  601. "order_code" => $cg['cgdNo'],//销售单code
  602. "order_id" => $up,
  603. "order_status" => $cg['status'],
  604. "before_status" => 0,
  605. 'holder_id' => $data['createrid'],
  606. 'wait_id'=>$data['createrid'],
  607. 'wait_name'=>$data['creater'],
  608. ]);
  609. $standing_book_da = array_merge($standing_book_da, [
  610. 'orderCode' => $data['orderCode'],
  611. 'cgdNo' => $cgdCode,
  612. 'spuCode' => $data['spuCode'],
  613. 'skuCode' => $data['skuCode'],
  614. 'order_type' => $data['order_type'],
  615. 'order_source' => $data['order_source'],
  616. 'supplierNo' => $data['supplierNo'],
  617. "companyNo" => $data['companyNo'],
  618. ]);
  619. $rela = [
  620. "orderCode" => $data['orderCode'],
  621. "cgdNo" => $cgdCode,
  622. "spuCode" => $data['spuCode'],
  623. "good_num" => $data['good_num'],
  624. "wsend_num" => $data['good_num'],
  625. "send_num" => 0,
  626. "wait_num" => 0,
  627. "status" => 1,
  628. "source" => 2
  629. ];
  630. $re = Db::name("order_num")->save($rela);
  631. if ($re == false) {
  632. throw new Exception("采购单销售单关联失败");
  633. } else {
  634. $stokc = Db::name("good_stock")->where(['spuCode' => $data['spuCode'], "wsm_code" => $wsm_code, "is_del" => 0])->find();
  635. if ($stokc == false) {
  636. $stokc = [
  637. "spuCode" => $data['spuCode'],
  638. "wsm_code" => $wsm_code,
  639. "wait_in_stock" => $data['good_num'],
  640. "wait_out_stock" => 0,
  641. "usable_stock" => 0,
  642. "intra_stock" => 0,
  643. "total_stock" => 0,
  644. "status" => 1,
  645. "addtime" => date("Y-m-d H:i:s"),
  646. "updatetime" => date("Y-m-d H:i:s")
  647. ];
  648. } else {
  649. $stokc['wait_in_stock'] += $data['good_num'];
  650. $stokc['updatetime'] = date("Y-m-d H:i:s");
  651. }
  652. $stoc = Db::name("good_stock")->save($stokc);
  653. if ($stoc == false) {
  654. throw new Exception("商品待入库存更新失败");
  655. }
  656. $good_data[] = ['good_log_code' => $cgdCode, "stock_id" => isset($stoc['id']) ? $stoc['id'] : Db::name("good_stock")->getLastInsID(), "type" => 1,
  657. 'stock' => $data['good_num'], "stock_name" => "wait_in_stock"];
  658. GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname], $good_data, "CGD");
  659. return true;
  660. }
  661. } else {
  662. throw new Exception("采购单生成失败");
  663. }
  664. }
  665. public function addAddr()
  666. {
  667. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] != "" ? trim($this->post['orderCode']) : "";
  668. $etid = Db::name("sale")->where(["orderCode" => $orderCode, "is_del" => 0])->find();
  669. if (empty($etid)) {
  670. return error_show(1002, "未找到数据");
  671. }
  672. $retrun = Db::name("sale_return")->where([["orderCode", "=", $orderCode], ["is_del", "=", 0], ["status", "in", [1, 2, 3,
  673. 7, 9,8, 10, 11, 12]]])->count('id');
  674. if ($retrun > 0) {
  675. return error_show(1005, "存在未完成退货订单数据");
  676. }
  677. $arrtime = isset($this->post['arrtime']) && $this->post['arrtime'] != "" ? $this->post['arrtime'] : "";
  678. $rm = $this->uid;//isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  679. $ri = $this->uname;//isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  680. $addrlist = isset($this->post['addrlist']) && $this->post['addrlist'] !== "" ? $this->post['addrlist'] : "";
  681. if ($addrlist == "" || empty($addrlist) || !is_array($addrlist)) {
  682. return error_show(1004, "参数addrlist不能为空");
  683. }
  684. Db::startTrans();
  685. try {
  686. $i = 1;
  687. $total_num = array_sum(array_column($addrlist, "receipt_quantity"));
  688. $goodStock_num = Db::name('good_stock')->alias("a")
  689. ->leftJoin("warehouse_info b","a.wsm_code=b.wsm_code")
  690. ->where(['a.is_del' => 0, 'a.spuCode' =>$etid['good_code'],"b.wsm_type"=>[2,5]])->sum('usable_stock');
  691. if ($goodStock_num < $total_num && $etid['is_stock'] == 1) throw new Exception("库存品库存数不足发货");
  692. foreach ($addrlist as $value) {
  693. $temp = [];
  694. $addrs = [];
  695. if ($value['addr_code'] !== '' && is_array($value['addr_code']) && !empty($value['addr_code'])) {
  696. $addrs['provice_code'] = $value['addr_code'][0];
  697. $addrs['city_code'] = $value['addr_code'][1];
  698. $addrs['area_code'] = $value['addr_code'][2];
  699. $addr = json_encode($addrs);
  700. } else {
  701. $addr = isset($value['addr_code']) && $value['addr_code'] !== '' ? $value['addr_code'] : '';
  702. }
  703. if($value['receipt_quantity']<=0) throw new Exception('地址发货数量不能小于1');
  704. $temp['orderCode'] = $orderCode;
  705. $temp['contactor'] = $value['contactor'];
  706. $temp['mobile'] = $value['mobile'];
  707. $temp['addr'] = $value['addr'];
  708. $temp['addr_code'] = $addr;
  709. $temp['customer_code'] = $etid['customer_code'];
  710. $temp['receipt_quantity'] = $value['receipt_quantity'];
  711. $temp['post_fee'] = 0;
  712. $temp['is_del'] = 0;
  713. $temp['addtime'] = date("Y-m-d H:i:s");
  714. $temp['updatetime'] = date("Y-m-d H:i:s");
  715. if ($arrtime != "") {
  716. $temp['arrive_time'] = $arrtime;
  717. }
  718. $vmp = Db::name('order_addr')->insert($temp, true);
  719. if ($vmp > 0) {
  720. $num = $value['receipt_quantity'];
  721. $outCode = makeNo("DF");
  722. //改变编码规则,将原来的outCode后两位换成序列号
  723. //str_pad字符串填充
  724. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  725. $i++;
  726. //维护商品所在仓库的库存 --- start
  727. if ($etid['is_stock'] != 1) {
  728. //查找关联采购单
  729. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->findOrEmpty();
  730. if (empty($order)) {
  731. throw new Exception("未找到可以发货得采购单数据");
  732. }
  733. $tep = [
  734. "cgdNo" => $order['cgdNo'],
  735. "outCode" => $outCode,
  736. "send_num" => $num,
  737. "status" => 1,
  738. "addtime" => date("Y-m-d H:i:s"),
  739. "updatetime" => date("Y-m-d H:i:s")
  740. ];
  741. $sen = Db::name("order_send")->save($tep);
  742. if ($sen == false) {
  743. throw new Exception("发货地址添加创建失败");
  744. }
  745. $cgdinfo = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  746. if ($cgdinfo == false) {
  747. throw new Exception("采购单数据不存在");
  748. }
  749. if ($goodStock_num == 0) {
  750. $status = 0;
  751. } else {
  752. if (($goodStock_num - $num) < 0) throw new Exception("非库存品可用库存不足");
  753. $goodStock_num -= $num;
  754. $status = 1;
  755. }
  756. } else {
  757. if ($goodStock_num == 0 || ($goodStock_num - $num) < 0) throw new Exception("库存品可用库存不足");
  758. $goodStock_num -= $num;
  759. $status = 1;
  760. }
  761. $out = [
  762. "orderCode" => $orderCode,
  763. "outCode" => $outCode,
  764. "apply_id" => $rm,
  765. "apply_name" => $ri,
  766. "addrid" => $vmp,
  767. "post_name" => "",
  768. "post_code" => "",
  769. "post_fee" => 0,
  770. "sendtime" => date("Y-m-d H:i:s"),
  771. "send_num" => $num,
  772. "check_num" => 0,
  773. "error_num" => 0,
  774. "wsm_code" => $cgdinfo['wsm_code'] ?? '',
  775. "order_type" => $etid['order_type'],
  776. "send_status" => $status,
  777. "status" => $status,
  778. "addtime" => date("Y-m-d H:i:s"),
  779. "updatetime" => date("Y-m-d H:i:s")
  780. ];
  781. $ou = Db::name("order_out")->insertGetId($out);
  782. if ($ou == false) throw new Exception("发货单创建失败");
  783. else {
  784. if ($out['send_status'] == 1) OrderOutChild::makeChild($outCode);
  785. //修改状态,添加待办
  786. // ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  787. // "order_code" => $outCode,//出库单号
  788. // "status" => 0,//这里的status是之前的值
  789. // "action_remark" => '',//备注
  790. // "action_type" => "create"//新建create,编辑edit,更改状态status
  791. // ], "CKD", 0, $out);
  792. //区分是否是库存品订单
  793. $process = [];
  794. // if($status == 1){
  795. // //库存品,推给库管和库管-张凯旋
  796. // if($etid['is_stock']==1){
  797. // $roleid = config('app.wsm_cgder_role');
  798. // $uids = Db::name('user_role')
  799. // ->where('is_del', 0)
  800. // ->whereIn('roleid', $roleid)
  801. // ->column('uid');
  802. // $process=[
  803. // "order_type" => 'CKD',
  804. // "order_code" => $outCode,//出库单号
  805. // "order_id" => $ou,
  806. // "order_status" => $status,
  807. // "before_status" => 0,
  808. //// 'holder_id'=>$out['apply_id'],
  809. // 'handle_user_list' => implode(',', $uids)
  810. // ];
  811. // }else{
  812. // //推给供应商负责人
  813. //// $supplier = Db::name('supplier')
  814. //// ->field('person,personid')
  815. //// ->where('code',$cgdinfo['supplierNo'])
  816. //// ->findOrEmpty();
  817. //
  818. // $supplier_temp =$userCommon->handle("sInfo",["code"=>$cgdinfo['supplierNo']]);
  819. // if($supplier_temp['code']!=0 || empty($supplier_temp['data']) ) return error_show(1002,"采购单供应商不存在");
  820. // $holder_id=$supplier_temp['data']['personid']??0;
  821. // $holder_name=$supplier_temp['data']['person']??'';
  822. // $supplier_name=$supplier_temp['data']['name']??'';
  823. // $process = [
  824. // "order_type" => 'CKD',
  825. // "order_code" => $outCode,//出库单号
  826. // "order_id" => $ou,
  827. // "order_status" => $status,
  828. // "before_status" => 0,
  829. // 'wait_id'=>$holder_id,
  830. // 'wait_name'=>$holder_name,
  831. // 'holder_id'=>$holder_id,
  832. // ];
  833. // }
  834. // }
  835. // if(empty($process)) $process=[
  836. // "order_type" => 'CKD',
  837. // "order_code" => $outCode,//出库单号
  838. // "order_id" => $ou,
  839. // "order_status" => $status,
  840. // "before_status" => 0,
  841. // 'holder_id'=>$out['apply_id'],
  842. // ];
  843. //
  844. // ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri],$process);
  845. //处理台账-既然是根据orderCode更新的数据,那么肯定是有数据的,只需要更新就行了
  846. Db::execute("UPDATE `wsm_standing_book` SET `outCode`=CONCAT(IFNULL(`outCode`,''),',{$outCode}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `orderCode`='{$orderCode}'");
  847. }
  848. //维护商品所在仓库的库存 --- end
  849. } else {
  850. Db::rollback();
  851. return error_show(1002, "发货地址添加创建失败");
  852. }
  853. }
  854. // $usable = $GoodModel->where($goodStock)->update(["usable_stock"=>$goodStock_num,"updatetime"=>date("Y-m-d H:i:s")]);
  855. // if($usable==false) throw new Exception("商品可用库存数更新失败");
  856. Db::commit();
  857. return app_show(0, "发货地址添加完成");
  858. } catch (\Exception $e) {
  859. Db::rollback();
  860. return error_show(1002, $e->getMessage());
  861. }
  862. }
  863. public function consultOrder()
  864. {
  865. $bidNo = isset($this->post['bidNo']) && $this->post['bidNo'] != "" ? trim($this->post['bidNo']) : "";
  866. if ($bidNo == "") {
  867. return error_show(1003, "参数bidNo不能为空");
  868. }
  869. $zxinfo = Db::name("consult_bids")->where(["bidNo" => $bidNo, "is_del" => 0])->find();
  870. if ($zxinfo == false) {
  871. return error_show(1003, "未找到咨询单商品信息");
  872. }
  873. if ($zxinfo['tax_status'] !=1) {
  874. return error_show(1003, "咨询单商品财务未审核通过");
  875. }
  876. $bargaininfo =Db::name("bargain_order")->where(["bidsNo" => $bidNo,"is_del"=>0,"status"=>0])->findOrEmpty();
  877. $zx = Db::name("consult_info")->where(["infoNo" => $zxinfo['infoNo'], "is_del" => 0])->find();
  878. if ($zx == false) {
  879. return error_show(1003, "未找到咨询单信息");
  880. }
  881. $manager = isset($this->post['manager']) && $this->post['manager'] != '' ? $this->post['manager'] :$this->uname;
  882. $managerid = isset($this->post['managerid']) && $this->post['managerid'] != '' ? $this->post['managerid'] :$this->uid;
  883. $zxorder = Db::name("consult_order")->where(["zxNo" => $zx["zxNo"], "is_del" => 0])->find();
  884. if ($zxorder == false) {
  885. return error_show(1003, "未找到咨询单信息");
  886. }
  887. $good_num = isset($this->post['good_num']) && $this->post['good_num'] != "" ? intval($this->post['good_num']) : "";
  888. if ($good_num === "") {
  889. return error_show(1003, "参数 good_num 不能为空");
  890. }
  891. $sale_price = isset($this->post['sale_price']) && $this->post['sale_price'] != "" ? floatval($this->post['sale_price']) : "";
  892. if ($sale_price === "") {
  893. return error_show(1003, "参数 sale_price 销售单价 不能为空");
  894. }
  895. if ($sale_price < $zxinfo['sale_price']) {
  896. return error_show(1003, "修改的销售单价不能低于原来的销售单价");
  897. }
  898. $goodtype = isset($this->post['goodtype']) && $this->post['goodtype'] !== '' ? intval($this->post['goodtype']) : '1';
  899. $sendtype = isset($this->post['sendtype']) && $this->post['sendtype'] != "" ? intval($this->post['sendtype']) : "";
  900. if ($sendtype == "") {
  901. return error_show(1003, "参数sendtype不能为空");
  902. }
  903. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] != "" ? trim($this->post['companyNo']) : "";
  904. if ($companyNo == "") {
  905. return error_show(1003, "参数 companyNo 不能为空");
  906. }
  907. $userCommon = \app\admin\common\User::getIns();
  908. // }
  909. $remark = isset($this->post['remark']) && $this->post['remark'] != "" ? trim($this->post['remark']) : "";
  910. $tmp = $userCommon->handle('sInfo',['code'=>$zxinfo['supplierNo']]);
  911. if(!isset($tmp['code']) || $tmp['code']!=0) return json_show($tmp['code'],$tmp['message'],$tmp['data']);
  912. $companyinfo = $userCommon->handle('getCodeAndName',['code'=>[$companyNo,$zxorder['khNo']]]);
  913. if(!isset($companyinfo['code']) || $companyinfo['code']!=0) return json_show($companyinfo['code'],$companyinfo['message'],$companyinfo['data']);
  914. $supplier_temp_info = $tmp['data'];
  915. $platforminfo = Db::name("platform")->where(["id"=> $zxorder['platform_code'],"is_del"=>0])->findOrEmpty();
  916. if (empty($platforminfo)) return error_show(1004, "平台信息未找到");
  917. if ($platforminfo['status'] != 1) return error_show(1004, "平台信息已禁用");
  918. if($platforminfo['is_select_pay_rate']==1 && channel_is_company($zxorder['platform_code'],$companyNo))return error_show(1004, "平台渠道含有 {$companyinfo['data'][$companyNo]}");
  919. $orderCode = makeNo("QR");
  920. $spuCode = $zxinfo['spuCode'];
  921. $skuCode = "";
  922. $is_stock = 0;
  923. $cgd = [
  924. "supplierNo" => $zxinfo['supplierNo'],
  925. "supplierName" => $supplier_temp_info['name'],
  926. "companyNo" => $companyNo,
  927. "companyName" => $companyinfo['data'][$companyNo]??"",
  928. "spuCode" => $spuCode,
  929. "skuCode" => $skuCode,
  930. "orderCode" => $orderCode,
  931. "good_name" => $zxinfo['good_name'],
  932. "sale_price" => $zxinfo['total_fee'],
  933. "total_fee" => round($zxinfo['total_fee'] * $good_num, 2),
  934. "pakge_fee" => $zxinfo['pakge_fee'],
  935. "cert_fee" => $zxinfo['cert_fee'],
  936. "open_fee" => $zxinfo['open_fee'],
  937. "cost_fee" => $zxinfo['cost_fee'],
  938. "mark_fee" => $zxinfo['mark_fee'],
  939. "demo_fee" => $zxinfo['demo_fee'],
  940. "nake_fee" => $zxinfo['nake_fee'],
  941. "delivery_fee" => $zxinfo['delivery_fee'],
  942. "good_num" => $good_num,
  943. "createrid" =>$supplier_temp_info['personid'],
  944. "creater" =>$supplier_temp_info['person'],
  945. "good_type" => 1,
  946. "open_type"=>$zxinfo["open_type"],
  947. "weight" => $zxinfo['good_weight'],
  948. "gold_price" => $zxinfo['gold_price'],
  949. "is_diff" => $zxinfo['is_diff'],
  950. "order_type" => 3,
  951. "order_source" => 2,//咨询
  952. 'send_way' => $zxinfo['send_way'],
  953. 'good_createrid' => $zxinfo['createrid'],
  954. 'good_creater' => $zxinfo['creater'],//商品创建人id
  955. 'supplier_origin_price'=>$zxinfo['supplier_origin_price'],//供应商采购成本
  956. 'cgd_supplier_code'=>$zxinfo['cgd_supplier_code'],//采购供应商编号
  957. 'cgd_supplier_name'=>$zxinfo['cgd_supplier_name'],//采购供应商编号
  958. 'cgd_apply_id'=>$zxinfo['cgd_apply_id'],//竞单人
  959. 'cgd_apply_name'=>$zxinfo['cgd_apply_name'],//竞单人
  960. ];
  961. $rm = $this->uid;//isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  962. $ri = $this->uname;//isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  963. $va = isset($this->post['order_addr']) && $this->post['order_addr'] !== "" ? $this->post['order_addr'] : "";
  964. $send_num = 0;
  965. if ($sendtype == 1) {
  966. if ($va == "") {
  967. return error_show(1002, "参数order_addr不能为空");
  968. }
  969. $send_num = array_sum(array_column($va, "receipt_quantity"));
  970. }
  971. if ($good_num < $send_num) {
  972. return error_show(1002, "发货数量不能超过购买数量");
  973. }
  974. $arrtime = isset($this->post['arrtime']) && $this->post['arrtime'] != "" ? $this->post['arrtime'] : "";
  975. if ($arrtime == "") {
  976. return error_show(1002, "参数arrtime不能为空");
  977. }
  978. //新加字段
  979. $platform_order = isset($this->post['platform_order']) && $this->post['platform_order'] != "" ? $this->post['platform_order'] : "";
  980. $workNo = isset($this->post['workNo']) && $this->post['workNo'] != "" ? $this->post['workNo'] : "";
  981. $proof_type = isset($this->post['proof_type']) && $this->post['proof_type'] != "" ? intval($this->post['proof_type']) : 0;
  982. $proof_url = isset($this->post['proof_url']) && $this->post['proof_url'] != "" ? trim($this->post['proof_url']) : '';
  983. if($proof_url=="")return error_show(1002, '参数 proof_url 不能为空');
  984. if($proof_type==0)return error_show(1002, '参数 proof_type 不能为空');
  985. Db::startTrans();
  986. try {
  987. $zx = Db::name("consult_info")->where(["infoNo" => $zxinfo['infoNo'], "is_del" => 0])->lock(true)->find();
  988. if ($zx == false) {
  989. Db::rollback();
  990. return error_show(1003, "未找到咨询单信息");
  991. }
  992. if ($companyNo != $zxorder['companyNo']) {
  993. $zxord = Db::name("consult_order")->where($zxorder)->update(["companyNo" => $companyNo, "updatetime" => date("Y-m-d H:i:s")]);
  994. if ($zxord == false) {
  995. Db::rollback();
  996. return error_show(1003, "咨询单更新业务公司失败");
  997. }
  998. }
  999. if(!empty($bargaininfo)){
  1000. $bargaininfo['status']=4;
  1001. $bargaininfo['updatetime']=date("Y-m-d H:i:s");
  1002. $branip =Db::name("bargain_order")->save($bargaininfo);
  1003. if($branip==false) throw new Exception("未完成的议价单取消失败");
  1004. }
  1005. $bidsup =[
  1006. "status"=>6,
  1007. "updatetime"=>date("Y-m-d H:i:s")
  1008. ];
  1009. $updateinfp =Db::name("consult_bids")->where(["bidNo" => $bidNo, "is_del" => 0])->update($bidsup);
  1010. if($updateinfp==false) throw new Exception("反馈单转单失败");
  1011. $data = [
  1012. "orderCode" => $orderCode,
  1013. "good_code" => $spuCode,
  1014. "skuCode" => $skuCode,
  1015. "customer_code" => $zxorder['khNo'],
  1016. "customerName" => $companyinfo['data'][$zxorder['khNo']]??"",
  1017. "good_name" => isset($zxinfo['good_name']) && $zxinfo['good_name'] !== '' ? $zxinfo['good_name'] : '',
  1018. "good_num" => $good_num,
  1019. "cat_id" => $zxinfo['cat_id'],
  1020. "apply_id" => $rm,
  1021. "apply_name" => $ri,
  1022. "origin_price" => $zxinfo['total_fee'],
  1023. "sale_price" => $sale_price,//销售单价支持修改
  1024. "post_fee" => 0,
  1025. "status" => 0,
  1026. "send_num" => 0,
  1027. "wsend_num" => $good_num,
  1028. "send_status" => 1,
  1029. "good_type" =>$goodtype,
  1030. "send_type" => $sendtype,
  1031. "supplierNo" => $companyNo,
  1032. "supplierName" =>$companyinfo['data'][$companyNo]??"",
  1033. "is_del" => 0,
  1034. "supNo" => $zxinfo['supplierNo'],
  1035. "supName" =>$supplier_temp_info['name'],
  1036. "zxNo" => $bidNo,
  1037. "platform_order" => $platform_order,
  1038. "platform_id" => $zxorder['platform_code'],
  1039. "remark" => $remark,
  1040. "arrive_time" => $arrtime,
  1041. "is_stock" => 0,
  1042. "is_activity" => 0,
  1043. "activity_code" => '',
  1044. "proof_id" => 0,
  1045. "order_type" => 3,
  1046. "order_source" => 2,//2咨询
  1047. 'good_weight' => $zxinfo['good_weight'],
  1048. 'gold_price' => $zxinfo['gold_price'],
  1049. 'cost_price' => $zxinfo['sale_cost_fee'],
  1050. 'diff_weight' => 0,
  1051. 'open_type'=>$zxinfo['open_type'],
  1052. 'diff_fee' => 0,
  1053. 'use_order'=>$this->post['useid']??0,
  1054. "addtime" => date("Y-m-d H:i:s"),
  1055. "updatetime" => date("Y-m-d H:i:s"),
  1056. 'total_price' => round($sale_price * $good_num, 2),
  1057. 'workNo' => $workNo,
  1058. 'cgderid' => $supplier_temp_info['personid'],
  1059. 'cgder' => $supplier_temp_info['person'],//采购员(采购单对应的供应商负责人)
  1060. 'good_createrid' => $zxinfo['createrid'],
  1061. 'good_creater' => $zxinfo['creater'],//商品创建人id
  1062. "manager"=>$manager,
  1063. "managerid"=>$managerid,
  1064. ];
  1065. $datainfo = Db::name('sale')->insert($data, true);
  1066. if ($datainfo > 0) {
  1067. $standing_book_data = [
  1068. 'sale_id' => $datainfo,
  1069. 'infoNo' => $zxinfo['infoNo'],
  1070. 'updatetime' => date('Y-m-d H:i:s')
  1071. ];
  1072. //修改状态,添加待办
  1073. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  1074. "order_code" => $orderCode,//销售单code
  1075. "status" => 0,//这里的status是之前的值
  1076. "action_remark" => '',//备注
  1077. "action_type" => "create"//新建create,编辑edit,更改状态status
  1078. ], "XSQRD", $data['status'], $data);
  1079. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  1080. "order_type" => 'XSQRD',
  1081. "order_code" => $orderCode,//销售单code
  1082. "order_id" => $datainfo,
  1083. "order_status" => $data['status'],
  1084. "before_status" => 0,
  1085. 'holder_id' => $data['apply_id'],
  1086. ]);
  1087. $old_zx_status = $zx['status'];
  1088. $sa = Db::name("consult_info")->where($zx)->save(["status" => 5, "updatetime" => date("Y-m-d H:i:s")]);
  1089. if ($sa == false) {
  1090. Db::rollback();
  1091. return error_show(1002, "咨询单修改失败");
  1092. } else {
  1093. Db::name('consult_bids')
  1094. ->where([["infoNo" ,"=", $zxinfo['infoNo']],["is_del","=",0],["status","<>",6]])
  1095. ->update(["tax_status"=>3]);
  1096. //修改状态,添加待办
  1097. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  1098. "order_code" => $zx['infoNo'],//咨询单详情编号
  1099. "status" => $old_zx_status,//这里的status是之前的值
  1100. "action_remark" => '',//备注
  1101. "action_type" => "status"//新建create,编辑edit,更改状态status
  1102. ], "ZXD", 5, $zx);
  1103. }
  1104. $limt = [
  1105. "spuCode" => $zxinfo['spuCode'],
  1106. "good_name" => $zxinfo['good_name'],
  1107. "brand_id" => $zxinfo['brand_id'],
  1108. "good_unit" => $zxinfo['unit_id'],
  1109. "cat_id" => $zxinfo['cat_id'],
  1110. "good_type" => 0,
  1111. "moq" => 1,
  1112. "customized" => $zxinfo['work_day'],
  1113. "tax" => $zxinfo['tax'],
  1114. "supplierNo" => $zxinfo["supplierNo"],
  1115. "supplierName" =>$supplier_temp_info['name'],
  1116. "is_auth" => 0,
  1117. "craft_desc" => $zxinfo['good_name'],
  1118. "good_remark" => "",
  1119. "platform_id" => $zxorder['platform_id'],
  1120. "good_img" => $zxinfo['good_img'],
  1121. "good_thumb_img" => "",
  1122. "good_info_img" => "",
  1123. "specinfo" => $zxinfo['specinfo'],
  1124. "work_day" => $zxinfo['work_day'],
  1125. "noble_metal" => $zxinfo['metal_id'],
  1126. "is_gold_price" => $zxinfo['is_gold_price'],
  1127. "config" => $zxinfo['config'],
  1128. "other_config" => $zxinfo['other_config'],
  1129. "weight" => $zxinfo['weight'],
  1130. "good_weight" => $zxinfo['good_weight'],
  1131. "is_diff" => $zxinfo['is_diff'],
  1132. "supply_area" => $zxinfo['supply_area'],
  1133. "pay_way" => $zxinfo['pay_way'],
  1134. "send_way" => $zxinfo['send_way'],
  1135. "companyNo" => $companyNo,
  1136. "companyName" =>$companyinfo['data'][$companyNo]??"",
  1137. 'sale_price' => $sale_price,
  1138. "status" => 1,
  1139. "is_combind" => $zxinfo['is_combind'],
  1140. "open_type"=>$zxinfo['open_type'],
  1141. "is_del" => 0,
  1142. 'order_type' => 3,
  1143. 'order_source' => 2,
  1144. "createrid" => $zxinfo['createrid'],
  1145. "creater" => $zxinfo['creater'],
  1146. "order_createrid" => $this->uid,
  1147. "order_creater" => $this->uname,
  1148. "addtime" => date("Y-m-d H:i:s"),
  1149. "updatetime" => date("Y-m-d H:i:s"),
  1150. 'proof_type' => $proof_type,//凭证类型
  1151. 'proof_url' => $proof_url,//凭证文件
  1152. ];
  1153. $good = Db::name("good_zixun")->insert($limt);
  1154. if (!$good) {
  1155. Db::rollback();
  1156. return error_show(1006, "咨询商品录入失败");
  1157. }
  1158. if($zxinfo['is_combind']==1){
  1159. $marage=[
  1160. "parent_code"=>$zxinfo['spuCode'],
  1161. "parent_name"=>$zxinfo['good_name'],
  1162. 'order_type' => 3,
  1163. 'order_source' => 2,
  1164. 'good_type' => 0,
  1165. 'moq' => 1,
  1166. 'good_thumb_img' => '',
  1167. 'good_info_img' => '',
  1168. "is_auth" => 0,
  1169. 'good_remark' => '',
  1170. 'createrid' => $zxinfo['createrid'],
  1171. 'creater' => $zxinfo['creater'],
  1172. 'order_createrid' => $this->uid,
  1173. 'order_creater' => $this->uname,
  1174. 'proof_type' => $proof_type,//凭证类型
  1175. 'proof_url' => $proof_url,//凭证文件
  1176. 'platform_id' => $zxorder['platform_id'],
  1177. ];
  1178. (new ConsultTemp())->CreateGood($zxinfo['bidNo'],$marage);
  1179. }
  1180. if($zxinfo['open_type']==1|| $zxinfo['tax_id']>0){
  1181. $data = [
  1182. 'spuCode'=>$zxinfo['spuCode'],
  1183. 'companyNo'=>$companyNo,
  1184. 'tax_id'=>$zxinfo['tax_id'],
  1185. 'inv_good_name'=>$zxinfo['inv_good_name'],
  1186. 'open_type'=>$zxinfo['open_type'],
  1187. 'tax'=>$zxinfo['tax'],
  1188. "out_tax_id"=>$zxinfo['out_tax_id'],
  1189. 'out_tax'=> str_replace("%","",$zxinfo['out_tax']),
  1190. 'cgd_tax_id'=>$zxinfo['cgd_tax_id'],
  1191. 'cgd_tax'=>str_replace('%','',$zxinfo['cgd_tax']),
  1192. "status"=>2,
  1193. 'createrid' => $zxinfo['createrid'],
  1194. 'creater' => $zxinfo['creater'],
  1195. ];
  1196. GoodTax::create($data);
  1197. }
  1198. $taxInfo = OrderTax::SpuCat($spuCode);
  1199. if (!empty($taxInfo)) {
  1200. (new \app\admin\model\OrderTax)->saveAll(array_map(function ($item)use($orderCode){
  1201. $temp=[];
  1202. $temp['code']=$orderCode;
  1203. $temp['spuCode']=$item['spuCode'];
  1204. $temp['tax_code']=$item['cat_code'];
  1205. $temp['tax_name']=$item['cat_name'];
  1206. $temp['tax_short_name']=$item['short_name'];
  1207. $temp['inv_tax']=$item['tax'];
  1208. $temp['inv_good_name']=$item['inv_good_name'];
  1209. return $temp;
  1210. },$taxInfo));
  1211. }
  1212. $bol = $this->createCgd($cgd, $standing_book_data);
  1213. if ($bol == false) {
  1214. Db::rollback();
  1215. return error_show(1002, '咨询订单创建失败');
  1216. }
  1217. if (!empty($va)) {
  1218. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  1219. ->find();
  1220. if ($order == false) {
  1221. Db::rollback();
  1222. return error_show(1006, "采购单关联数据未找到");
  1223. }
  1224. $i = 1;
  1225. foreach ($va as $value) {
  1226. $temp = [];
  1227. $addrs = [];
  1228. if ($value['addr_code'] !== '' && is_array($value['addr_code']) && !empty($value['addr_code'])) {
  1229. $addrs['provice_code'] = $value['addr_code'][0];
  1230. $addrs['city_code'] = $value['addr_code'][1];
  1231. $addrs['area_code'] = $value['addr_code'][2];
  1232. $addr = json_encode($addrs);
  1233. } else {
  1234. $addr = isset($value['addr_code']) ? $value['addr_code'] : '';
  1235. }
  1236. if($value['receipt_quantity']<=0) throw new Exception("地址发货数量不能小于1");
  1237. $temp['orderCode'] = $orderCode;
  1238. $temp['contactor'] = $value['contactor'];
  1239. $temp['mobile'] = $value['mobile'];
  1240. $temp['addr'] = $value['addr'];
  1241. $temp['addr_code'] = $addr;
  1242. $temp['customer_code'] = $zxorder['khNo'];
  1243. $temp['receipt_quantity'] = $value['receipt_quantity'];
  1244. $temp['post_fee'] = 0;
  1245. $temp['is_del'] = 0;
  1246. $temp['addtime'] = date("Y-m-d H:i:s");
  1247. $temp['updatetime'] = date("Y-m-d H:i:s");
  1248. $temp['arrive_time'] = $arrtime;
  1249. $vmp = Db::name('order_addr')->insert($temp, true);
  1250. if ($vmp > 0) {
  1251. $outCode = makeNo("DF");
  1252. //改变编码规则,将原来的outCode后两位换成序列号
  1253. //str_pad字符串填充
  1254. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  1255. $i++;
  1256. $out = [
  1257. "orderCode" => $orderCode,
  1258. "outCode" => $outCode,
  1259. "apply_id" => $rm,
  1260. "apply_name" => $ri,
  1261. "addrid" => $vmp,
  1262. "post_name" => "",
  1263. "post_code" => "",
  1264. "post_fee" => 0,
  1265. "sendtime" => date("Y-m-d H:i:s"),
  1266. "send_num" => $value['receipt_quantity'],
  1267. "check_num" => 0,
  1268. "error_num" => 0,
  1269. "wsm_code" => "",
  1270. "order_type" => 3,
  1271. "status" => 0,
  1272. "addtime" => date("Y-m-d H:i:s"),
  1273. "updatetime" => date("Y-m-d H:i:s")
  1274. ];
  1275. $ou = Db::name("order_out")->insertGetId($out);
  1276. if ($ou == false) {
  1277. Db::rollback();
  1278. return error_show(1002, "发货地址添加创建失败");
  1279. } else {
  1280. // OrderOutChild::makeChild($outCode); //非库存品下单无法立即分单
  1281. //修改状态,添加待办
  1282. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  1283. "order_code" => $outCode,//出库单号
  1284. "status" => 0,//这里的status是之前的值
  1285. "action_remark" => '',//备注
  1286. "action_type" => "create"//新建create,编辑edit,更改状态status
  1287. ], "CKD", 0, $out);
  1288. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], [
  1289. "order_type" => 'CKD',
  1290. "order_code" => $outCode,//出库单号
  1291. "order_id" => $ou,
  1292. "order_status" => 0,
  1293. "before_status" => 0,
  1294. 'holder_id'=>$out['apply_id'],
  1295. 'wait_id'=>$supplier_temp_info['personid'],
  1296. 'wait_name'=>$supplier_temp_info['person'],
  1297. ]);
  1298. //将发货编号添加到台账中
  1299. $standing_book_data['outCode'][] = $outCode;
  1300. }
  1301. $tep = [
  1302. "cgdNo" => $order['cgdNo'],
  1303. "outCode" => $outCode,
  1304. "send_num" => $value['receipt_quantity'],
  1305. "status" => 1,
  1306. "addtime" => date("Y-m-d H:i:s"),
  1307. "updatetime" => date("Y-m-d H:i:s")
  1308. ];
  1309. $sen = Db::name("order_send")->save($tep);
  1310. if ($sen == false) {
  1311. Db::rollback();
  1312. return error_show(1002, "发货地址添加创建失败");
  1313. }
  1314. } else {
  1315. Db::rollback();
  1316. return error_show(1002, "发货地址添加创建失败");
  1317. }
  1318. }
  1319. }
  1320. //处理台账
  1321. if (isset($standing_book_data['outCode'])) {
  1322. Db::execute("UPDATE `wsm_standing_book` SET `outCode`=CONCAT(IFNULL(`outCode`,''),'," . implode(',', $standing_book_data['outCode']) . "'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `infoNo`='{$standing_book_data['infoNo']}'");
  1323. unset($standing_book_data['outCode']);
  1324. }
  1325. Db::name('standing_book')->where('infoNo', $standing_book_data['infoNo'])->update($standing_book_data);
  1326. Db::commit();
  1327. return error_show(0, "咨询订单创建成功", ["order_code" => $orderCode]);
  1328. }
  1329. Db::rollback();
  1330. return error_show(1002, "咨询订单创建失败");
  1331. } catch (\Exception $e) {
  1332. Db::rollback();
  1333. return error_show(1003, $e->getMessage());
  1334. }
  1335. }
  1336. /**
  1337. * @return \think\response\Json|void
  1338. * @throws \think\db\exception\DataNotFoundException
  1339. * @throws \think\db\exception\DbException
  1340. * @throws \think\db\exception\ModelNotFoundException
  1341. */
  1342. public function projectCreate()
  1343. {
  1344. $planNo = isset($this->post['planNo']) && $this->post['planNo'] != "" ? trim($this->post['planNo']) : "";
  1345. if ($planNo == "") {
  1346. return error_show(1004, "参数planNo不能为空");
  1347. }
  1348. $plan = Db::name("project_plan")->where(["planNo" => $planNo, "is_del" => 0])->find();
  1349. if ($plan == false) {
  1350. return error_show(1004, "未找到方案信息");
  1351. }
  1352. $feedbackid = Db::name("project_plan_rela")->where(["planNo" => $planNo, "is_del" => 0])->column("feedback_id,sale_price,sale_num");
  1353. if ($feedbackid == false) {
  1354. return error_show(1004, "未找到方案反馈信息");
  1355. }
  1356. $project = Db::name("project")->where(["projectNo" => $plan['projectNo'], "is_del" => 0])->find();
  1357. if ($project == false) {
  1358. return error_show(1004, "未找到方案信息");
  1359. }
  1360. $arrtime = isset($this->post['arrtime']) && $this->post['arrtime'] != "" ? $this->post['arrtime'] : "";
  1361. if ($arrtime == "") {
  1362. return error_show(1002, "参数arrtime不能为空");
  1363. }
  1364. $planinfo = isset($this->post['planinfo']) && !empty($this->post['planinfo']) ? $this->post['planinfo'] : "";
  1365. if ($planinfo == "") {
  1366. return error_show(1002, "参数planinfo不能为空");
  1367. }
  1368. $userCommon = \app\admin\common\User::getIns();
  1369. $names = $userCommon->handle('getCodeAndName', ['code' =>[$project['companyNo'],$project['khNo']]]);
  1370. if($names['code']!=0 || empty($names['data']) ) return error_show($names['code'],$names['message']);
  1371. $platforminfo = Db::name("platform")->where(["id"=> $project['platform_id'],"is_del"=>0])->findOrEmpty();
  1372. if (empty($platforminfo)) return error_show(1004, "平台信息未找到");
  1373. if ($platforminfo['status'] != 1) return error_show(1004, "平台信息已禁用");
  1374. if($platforminfo['is_select_pay_rate']==1 && channel_is_company($project['platform_id'],$project['companyNo']))return error_show(1004, "平台渠道含有 {$names['data'][$project['companyNo']]}");
  1375. $rm = $this->uid;//isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  1376. $ri = $this->uname;//isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  1377. $manager = isset($this->post['manager']) && $this->post['manager'] != '' ? $this->post['manager'] :$this->uname;
  1378. $managerid = isset($this->post['managerid']) && $this->post['managerid'] != '' ? $this->post['managerid'] :$this->uid;
  1379. Db::startTrans();
  1380. try {
  1381. $plan = Db::name("project_plan")->where(["planNo" => $planNo, "is_del" => 0])->lock(true)->find();
  1382. if ($plan == false) {
  1383. throw new Exception("未找到方案信息");
  1384. // return error_show(1004, "未找到方案信息");
  1385. }
  1386. $feedid = array_column($feedbackid, "feedback_id");
  1387. $feedbacks = Db::name("project_feedback")
  1388. ->where(["id" => $feedid, "projectNo" => $plan['projectNo'], "is_del" => 0])
  1389. ->column("*","id");
  1390. foreach ($planinfo as $key => $value) {
  1391. $good = [];
  1392. $feedback = $feedbacks[$value['feedback_id']]??[];
  1393. if (empty($feedback)) {
  1394. throw new Exception("反馈数据未找到");
  1395. // return error_show(1002, "反馈数据未找到");
  1396. }
  1397. $key = array_search($feedback['id'], $feedid);
  1398. $price = $feedbackid[$key];
  1399. $good['spuCode'] = $feedback['spuCode'];
  1400. $good['skuCode'] = $feedback['skuCode'];
  1401. $good['good_num'] = $price['sale_num'];
  1402. $good['sendtype'] = $value['sendtype'];
  1403. $good['sale_price'] = $price['sale_price'];
  1404. $good['origin_price'] = $feedback['origin_price'];
  1405. $good['companyNo'] = $project['companyNo'];
  1406. $good['companyName'] =$names['data'][$project['companyNo']]??'';
  1407. $good['customerNo'] = $project['khNo'];
  1408. $good['customerName'] =$names['data'][$project['khNo']]??'';
  1409. $good['pgNo'] = $feedback['pgNo'];
  1410. $good['platform_id'] = $project['platform_id'];
  1411. $good['arrtime'] = $arrtime;
  1412. $good['rm'] = $rm;
  1413. $good['ri'] = $ri;
  1414. $good['use_order']=$this->post['useid']??0;
  1415. $good['manager'] = $manager;
  1416. $good['managerid'] = $managerid;
  1417. $good['send_num'] = 0;
  1418. $standing_book_data = ['projectNo' => $plan['projectNo'], 'updatetime' => date('Y-m-d H:i:s')];
  1419. if ($value['sendtype'] == 1) {
  1420. if (!isset($value['addrlist']) || empty($value['addrlist'])) {
  1421. throw new Exception("订单地址不能为空");
  1422. // return error_show(1002, "订单地址不能为空");
  1423. }
  1424. $good['addrlist'] = $value['addrlist'];
  1425. $good['send_num'] = array_sum(array_column($value['addrlist'], "receipt_quantity"));
  1426. }
  1427. if ($feedback['data_source'] == 1) {
  1428. $feed = $this->project_good($good, $standing_book_data);
  1429. if ($feed !== true) {
  1430. throw new Exception("订单生成失败");
  1431. // return error_show(1002, $msg);
  1432. }
  1433. } else {
  1434. $feed = $this->project_zx($good, $standing_book_data);
  1435. if ($feed == false) {
  1436. throw new Exception("咨询订单生成失败");
  1437. // return error_show(1002, "咨询订单生成失败");
  1438. }
  1439. }
  1440. $old_fk_status = $feedback['status'];
  1441. $fed = Db::name("project_feedback")->where($feedback)->save(['status' => 2, "updatetime" => date("Y-m-d H:i:s")]);
  1442. if ($fed == false) {
  1443. throw new Exception("项目反馈更新失败");
  1444. // return error_show(1002, "项目订单生成失败");
  1445. }
  1446. //处理台账(一个项目会生成多个销售订单,第一个更新,其余的新增)
  1447. //项目创建时已有供应商和企业,不能修改
  1448. if (isset($standing_book_data['companyNo'])) unset($standing_book_data['companyNo']);
  1449. if (isset($standing_book_data['customer_code'])) unset($standing_book_data['customer_code']);
  1450. if (isset($standing_book_data['outCode'])) $standing_book_data['outCode'] = implode(',', $standing_book_data['outCode']);
  1451. //普通商品
  1452. if ($feedback['data_source'] == 1) {
  1453. if ($key == 0) Db::name('standing_book')->where('projectNo', $plan['projectNo'])->update($standing_book_data);
  1454. else {
  1455. $temp = Db::name('standing_book')->where('projectNo', $plan['projectNo'])->find();
  1456. if (!empty($temp)) {
  1457. unset($temp['id']);
  1458. $temp = array_merge($temp, $standing_book_data);
  1459. $temp['standBookNo'] = makeNo('IO');
  1460. Db::name('standing_book')->insert($temp);
  1461. }
  1462. }
  1463. } else {
  1464. //项目类竞价单
  1465. Db::name('standing_book')->where(['projectNo' => $plan['projectNo'], 'infoNo' => $standing_book_data['infoNo']])->update($standing_book_data);
  1466. }
  1467. }
  1468. $old_plan_status = $plan['status'];
  1469. // $plan['status']=2;
  1470. // $plan['updatetime']=date("Y-m-d H:i:s");
  1471. $in = Db::name("project_plan")->where($plan)->save(['status' => 2, "updatetime" => date("Y-m-d H:i:s")]);
  1472. if ($in == false) {
  1473. throw new Exception("项目方案状态更新失败");
  1474. // return error_show(1002, "项目订单生成失败");
  1475. } else {
  1476. //修改状态,添加待办,只记录动作
  1477. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  1478. "order_code" => $planNo,//编码
  1479. "status" => $old_plan_status,//这里的status是之前的值
  1480. "action_remark" => '',//备注
  1481. "action_type" => "edit"//新建create,编辑edit,更改状态status
  1482. ], "PRP", 2, $plan);
  1483. }
  1484. $old_project_status = $project["status"];
  1485. // $project["status"]=6;
  1486. // $project['updatetime']=date("Y-m-d H:i:s");
  1487. $pr = Db::name("project")->where($project)->save(["updatetime" => date("Y-m-d H:i:s"), "status" => 6]);
  1488. if ($pr == false) {
  1489. throw new Exception("项目状态更新失败");
  1490. // return error_show(1002, "项目订单生成失败");
  1491. } else {
  1492. //修改状态,添加待办
  1493. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  1494. "order_code" => $project['projectNo'],//项目编码
  1495. "status" => $old_project_status,//这里的status是之前的值
  1496. "action_remark" => '',//备注
  1497. "action_type" => "status"//新建create,编辑edit,更改状态status
  1498. ], "PRO", 6, $project);
  1499. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  1500. "order_type" => 'PRO',
  1501. "order_code" => $project['projectNo'],
  1502. "order_id" => $project['id'],
  1503. "order_status" => 6,
  1504. "before_status" => $old_project_status,
  1505. 'holder_id'=>$project['createrid']
  1506. ]);
  1507. }
  1508. //修改状态,添加待办,只记录动作
  1509. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  1510. "order_code" => $project['projectNo'],//编码,这里是project的编码
  1511. "status" => 1,//这里的status是之前的值
  1512. "action_remark" => '',//备注
  1513. "action_type" => "create"//新建create,编辑edit,更改状态status
  1514. ], "PRT", 2, $this->post);
  1515. $process = ["order_code" => $project['projectNo'], "order_id" => $project['id'], "order_status" => 2, "order_type" => "PRT", "before_status" => 1];
  1516. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], $process);
  1517. Db::commit();
  1518. return app_show(0, "咨询订单生成成功");
  1519. } catch (\Exception $e) {
  1520. Db::rollback();
  1521. return error_show(1002, $e->getMessage());
  1522. }
  1523. }
  1524. /**
  1525. * @param $data
  1526. * @return false
  1527. * @throws \think\db\exception\DataNotFoundException
  1528. * @throws \think\db\exception\DbException
  1529. * @throws \think\db\exception\ModelNotFoundException
  1530. *
  1531. */
  1532. public function project_zx($data, array &$standing_book_data = [])
  1533. {
  1534. $goodinfo = Db::name("consult_bids")->where(["spuCode" => $data['spuCode'], "is_del" => 0])->find();
  1535. if ($goodinfo == false) {
  1536. throw new Exception("咨询商品未找到");
  1537. }
  1538. if ($goodinfo['status'] !=7) {
  1539. throw new Exception('咨询单商品财务未审核');
  1540. }
  1541. $bargaininfo =Db::name("bargain_order")->where(["bidsNo" => $goodinfo["bidNo"],"is_del"=>0,"status"=>0])
  1542. ->findOrEmpty();
  1543. if(!empty($bargaininfo)){
  1544. $bargaininfo['status']=4;
  1545. $bargaininfo['updatetime']=date("Y-m-d H:i:s");
  1546. $branip =Db::name("bargain_order")->save($bargaininfo);
  1547. if($branip==false) throw new Exception("未完成的议价单取消失败");
  1548. }
  1549. $orderCode = makeNo("QR");
  1550. $userCommon= \app\admin\common\User::getIns();
  1551. $supplier_temp =$userCommon->handle("sInfo",["code"=>$goodinfo['supplierNo']]);
  1552. if($supplier_temp['code']!=0 || empty($supplier_temp['data']) ) throw new Exception("采购单供应商不存在");
  1553. $holder_id=$supplier_temp['data']['personid']??0;
  1554. $holder_name=$supplier_temp['data']['person']??'';
  1555. $supplierName=$supplier_temp['data']['name']??'';
  1556. $cgd = [
  1557. "supplierNo" => $goodinfo['supplierNo'],
  1558. "supplierName" => $supplierName,
  1559. "companyNo" => $data['companyNo'],
  1560. "companyName" => $data['companyName'],
  1561. "spuCode" => $data['spuCode'],
  1562. "skuCode" => "",
  1563. "orderCode" => $orderCode,
  1564. "good_name" => $goodinfo['good_name'],
  1565. "sale_price" => $goodinfo['total_fee'],
  1566. "total_fee" => round($goodinfo['total_fee'] * $data['good_num'], 2),
  1567. "pakge_fee" => $goodinfo['pakge_fee'],
  1568. "cert_fee" => $goodinfo['cert_fee'],
  1569. "open_fee" => $goodinfo['open_fee'],
  1570. "cost_fee" => $goodinfo['cost_fee'],
  1571. "mark_fee" => $goodinfo['mark_fee'],
  1572. "demo_fee" => $goodinfo['demo_fee'],
  1573. "nake_fee" => $goodinfo['nake_fee'],
  1574. "good_num" => $data['good_num'],
  1575. "delivery_fee" => $goodinfo['delivery_fee'],
  1576. "createrid" =>$holder_id,
  1577. "creater" =>$holder_name,
  1578. "weight" => $goodinfo['good_weight'],
  1579. "gold_price" => $goodinfo['gold_price'],
  1580. 'open_type'=>$goodinfo['open_type'],
  1581. "good_type" => 1,
  1582. "order_type" => 3,//3咨询商品
  1583. "order_source" => 3,//项目
  1584. "send_way" => $goodinfo['send_way'],
  1585. 'good_createrid' => $goodinfo['createrid'],
  1586. 'good_creater' => $goodinfo['creater'],//商品创建人
  1587. 'supplier_origin_price'=>$goodinfo['supplier_origin_price'],//供应商采购成本
  1588. 'cgd_supplier_code'=>$goodinfo['cgd_supplier_code'],//采购供应商编号
  1589. 'cgd_supplier_name'=>$goodinfo['cgd_supplier_name'],//采购供应商编号
  1590. 'cgd_apply_id'=>$goodinfo['cgd_apply_id'],//竞单人
  1591. 'cgd_apply_name'=>$goodinfo['cgd_apply_name'],//竞单人
  1592. ];
  1593. $datas = [
  1594. "orderCode" => $orderCode,
  1595. "good_code" => $data['spuCode'],
  1596. "skuCode" => "",
  1597. "customer_code" => $data['customerNo'],
  1598. "customerName" => $data['customerName'],
  1599. "good_name" => isset($goodinfo['good_name']) && $goodinfo['good_name'] !== '' ? $goodinfo['good_name'] : '',
  1600. "good_num" => $data['good_num'],
  1601. "cat_id" => $goodinfo['cat_id'],
  1602. "apply_id" => $data['rm'],
  1603. "apply_name" => $data['ri'],
  1604. "origin_price" => $goodinfo['total_fee'],
  1605. "sale_price" => $data['sale_price'],
  1606. "post_fee" => 0,
  1607. "status" => 0,
  1608. "send_num" => 0,
  1609. "wsend_num" => $data['good_num'],
  1610. "send_status" => 1,
  1611. "good_type" => 1,
  1612. "send_type" => $data['sendtype'],
  1613. "supplierNo" => $data['companyNo'],
  1614. "supplierName" => $data['companyName'],
  1615. "supNo" =>$goodinfo['supplierNo'],
  1616. "supName" => $supplierName,
  1617. "is_del" => 0,
  1618. "zxNo" => $data["pgNo"],
  1619. "platform_order" => "",
  1620. "platform_id" => $data['platform_id'],
  1621. 'open_type'=>$goodinfo['open_type'],
  1622. "remark" => "",
  1623. "is_stock" => 0,
  1624. "is_activity" => 0,
  1625. "activity_code" => '',
  1626. "proof_id" => 0,
  1627. "order_type" => 3,//咨询商品
  1628. "order_source" => 3,//项目
  1629. 'use_order'=>$data['useid']??0,
  1630. 'good_weight' => $goodinfo['good_weight'],
  1631. 'gold_price' => $goodinfo['gold_price'],
  1632. 'cost_price' => $goodinfo['sale_cost_fee'],
  1633. 'diff_weight' => 0,
  1634. 'diff_fee' => 0,
  1635. "addtime" => date("Y-m-d H:i:s"),
  1636. "updatetime" => date("Y-m-d H:i:s"),
  1637. 'total_price' => round($data['sale_price'] * $data['good_num'], 2),
  1638. 'cgderid' => $holder_id,
  1639. 'cgder' => $holder_name,//采购员(供应商负责人)
  1640. 'good_createrid' => $goodinfo['createrid'],
  1641. 'good_creater' => $goodinfo['creater'],//商品创建人
  1642. "manager"=>$data['manager'],
  1643. "managerid"=>$data['managerid'],
  1644. ];
  1645. $datainfo = Db::name('sale')->insert($datas, true);
  1646. if ($datainfo > 0) {
  1647. $standing_book_data['sale_id'] = $datainfo;
  1648. $standing_book_data['customer_code'] = $datas['customer_code'];
  1649. $standing_book_data['infoNo'] = $goodinfo['infoNo'];
  1650. //修改状态,添加待办
  1651. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  1652. "order_code" => $orderCode,//项目编码
  1653. "status" => 0,//这里的status是之前的值
  1654. "action_remark" => '',//备注
  1655. "action_type" => "status"//新建create,编辑edit,更改状态status
  1656. ], "XSQRD", 0, $datas);
  1657. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  1658. "order_type" => 'XSQRD',
  1659. "order_code" => $orderCode,
  1660. "order_id" => $datainfo,
  1661. "order_status" => 0,
  1662. "before_status" => 0,
  1663. 'holder_id' => $datas['apply_id']
  1664. ]);
  1665. $bidstatus = $goodinfo['status'];
  1666. $goodinfo['status'] = 6;
  1667. $goodinfo['updatetime'] = date("Y-m-d H:i:s");
  1668. $sa = Db::name("consult_bids")->save($goodinfo);
  1669. if ($sa == false) {
  1670. throw new Exception("咨询反馈单更新失败");
  1671. }
  1672. $goodinfo['status'] = 5;
  1673. $goodinfo['updatetime'] = date("Y-m-d H:i:s");
  1674. $saa = Db::name("consult_info")->where(["infoNo"=>$goodinfo['infoNo']])->update(["status"=>5,
  1675. "updatetime"=>date("Y-m-d H:i:s")]);
  1676. if ($saa == false) {
  1677. throw new Exception("咨询单更新失败");
  1678. }
  1679. //修改状态,添加待办
  1680. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  1681. "order_code" => $goodinfo['bidNo'],//项目编码
  1682. "status" => $bidstatus,//这里的status是之前的值
  1683. "action_remark" => '',//备注
  1684. "action_type" => "status"//新建create,编辑edit,更改状态status
  1685. ], "FKD", 6, $datas);
  1686. $zxorder = Db::name("consult_order")->where(["zxNo" => $goodinfo["zxNo"], "is_del" => 0])->find();
  1687. if ($zxorder == false) {
  1688. throw new Exception("咨询单为找到");
  1689. }
  1690. $limt = [
  1691. "spuCode" => $goodinfo['spuCode'],
  1692. "good_name" => $goodinfo['good_name'],
  1693. "brand_id" => $goodinfo['brand_id'],
  1694. "good_unit" => $goodinfo['unit_id'],
  1695. "cat_id" => $goodinfo['cat_id'],
  1696. "good_type" => 0,
  1697. "moq" => 1,
  1698. "customized" => $goodinfo['work_day'],
  1699. "tax" => $goodinfo['tax'],
  1700. "supplierNo" => $goodinfo["supplierNo"],
  1701. "supplierName" => $supplierName,
  1702. "is_auth" => 0,
  1703. "craft_desc" => $goodinfo['good_name'],
  1704. "good_remark" => "",
  1705. "good_img" => $goodinfo['good_img'],
  1706. "good_thumb_img" => "",
  1707. "good_info_img" => "",
  1708. "platform_id" => $zxorder['platform_id'],
  1709. "specinfo" => $goodinfo['specinfo'],
  1710. "work_day" => $goodinfo['work_day'],
  1711. "noble_metal" => $goodinfo['metal_id'],
  1712. "is_gold_price" => $goodinfo['is_gold_price'],
  1713. "good_weight" => $goodinfo['good_weight'],
  1714. "companyNo" => $data['companyNo'],
  1715. "companyName" => $data['companyName'],
  1716. "config" => $goodinfo['config'],
  1717. "other_config" => $goodinfo['other_config'],
  1718. "weight" => $goodinfo['weight'],
  1719. "is_diff" => $goodinfo['is_diff'],
  1720. "supply_area" => $goodinfo['supply_area'],
  1721. "pay_way" => $goodinfo['pay_way'],
  1722. "send_way" => $goodinfo['send_way'],
  1723. 'sale_price' => $data['sale_price'],
  1724. 'open_type'=>$goodinfo['open_type'],
  1725. "status" => 1,
  1726. "is_del" => 0,
  1727. "order_type" => 3,
  1728. 'order_source' => 3,
  1729. 'order_createrid' => $data['rm'],
  1730. 'order_creater' => $data['ri'],
  1731. "createrid" => $goodinfo['createrid'],
  1732. "creater" => $goodinfo['creater'],
  1733. "addtime" => date("Y-m-d H:i:s"),
  1734. "updatetime" => date("Y-m-d H:i:s")
  1735. ];
  1736. $good = Db::name("good_zixun")->insert($limt);
  1737. if (!$good) {
  1738. throw new Exception("咨询商品录入失败");
  1739. }
  1740. if($goodinfo['is_combind']==1){
  1741. $marage=[
  1742. 'parent_code'=>$goodinfo['spuCode'],
  1743. 'parent_name'=>$goodinfo['good_name'],
  1744. 'order_type' => 3,
  1745. 'order_source' =>3,
  1746. 'good_type' => 0,
  1747. 'moq' => 1,
  1748. 'good_thumb_img' => '',
  1749. 'good_info_img' => '',
  1750. 'is_auth' => 0,
  1751. 'good_remark' => '',
  1752. 'createrid' => $goodinfo['createrid'],
  1753. 'creater' => $goodinfo['creater'],
  1754. 'order_createrid' => $data['rm'],
  1755. 'order_creater' => $data['ri'],
  1756. 'platform_id' => $zxorder['platform_id'],
  1757. ];
  1758. (new ConsultTemp())->CreateGood($goodinfo['bidNo'],$marage);
  1759. }
  1760. if($goodinfo['open_type']==1|| $goodinfo['tax_id']>0){
  1761. $data = [
  1762. 'spuCode'=>$goodinfo['spuCode'],
  1763. 'companyNo'=>$data['companyNo'],
  1764. 'tax_id'=>$goodinfo['tax_id'],
  1765. 'open_type'=>$goodinfo['open_type'],
  1766. 'inv_good_name'=>$goodinfo['inv_good_name'],
  1767. 'tax'=>$goodinfo['tax'],
  1768. 'out_tax_id'=>$goodinfo['out_tax_id'],
  1769. 'out_tax'=>str_replace('%','',$goodinfo['out_tax']),
  1770. 'cgd_tax_id'=>$goodinfo['cgd_tax_id'],
  1771. 'cgd_tax'=>str_replace('%','',$goodinfo['cgd_tax']),
  1772. 'status'=>2,
  1773. 'createrid' => $goodinfo['createrid'],
  1774. 'creater' => $goodinfo['creater'],
  1775. ];
  1776. GoodTax::create($data);
  1777. }
  1778. $taxInfo = OrderTax::SpuCat( $data['spuCode']);
  1779. if (!empty($taxInfo)) {
  1780. (new \app\admin\model\OrderTax)->saveAll(array_map(function ($item)use($orderCode){
  1781. $temp=[];
  1782. $temp['code']=$orderCode;
  1783. $temp['spuCode']=$item['spuCode'];
  1784. $temp['tax_code']=$item['cat_code'];
  1785. $temp['tax_name']=$item['cat_name'];
  1786. $temp['tax_short_name']=$item['short_name'];
  1787. $temp['inv_tax']=$item['tax'];
  1788. $temp['inv_good_name']=$item['inv_good_name'];
  1789. return $temp;
  1790. },$taxInfo));
  1791. }
  1792. $bol = $this->createCgd($cgd, $standing_book_data);
  1793. if ($bol == false) {
  1794. throw new Exception('采购单生成失败');
  1795. }
  1796. if ($data['sendtype'] == 1 && !empty($data['addrlist'])) {
  1797. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])
  1798. ->find();
  1799. if ($order == false) {
  1800. throw new Exception("关联采购单信息未找到");
  1801. }
  1802. $i = 1;
  1803. foreach ($data['addrlist'] as $value) {
  1804. $temp = [];
  1805. $addrs = [];
  1806. if ($value['addr_code'] !== '' && is_array($value['addr_code']) && !empty($value['addr_code'])) {
  1807. $addrs['provice_code'] = $value['addr_code'][0];
  1808. $addrs['city_code'] = $value['addr_code'][1];
  1809. $addrs['area_code'] = $value['addr_code'][2];
  1810. $addr = json_encode($addrs);
  1811. } else {
  1812. $addr = isset($value['addr_code']) ? $value['addr_code'] : '';
  1813. }
  1814. if($value['receipt_quantity']<=0) throw new Exception('地址发货数量不能小于1');
  1815. $temp['orderCode'] = $orderCode;
  1816. $temp['contactor'] = $value['contactor'];
  1817. $temp['mobile'] = $value['mobile'];
  1818. $temp['addr'] = $value['addr'];
  1819. $temp['addr_code'] = $addr;
  1820. $temp['customer_code'] = $data['companyNo'];
  1821. $temp['receipt_quantity'] = $value['receipt_quantity'];
  1822. $temp['post_fee'] = 0;
  1823. $temp['is_del'] = 0;
  1824. $temp['addtime'] = date("Y-m-d H:i:s");
  1825. $temp['updatetime'] = date("Y-m-d H:i:s");
  1826. $temp['arrive_time'] = $data['arrtime'];
  1827. $vmp = Db::name('order_addr')->insert($temp, true);
  1828. if ($vmp > 0) {
  1829. $outCode = makeNo("DF");
  1830. //改变编码规则,将原来的outCode后两位换成序列号
  1831. //str_pad字符串填充
  1832. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  1833. $i++;
  1834. $out = [
  1835. "orderCode" => $orderCode,
  1836. "outCode" => $outCode,
  1837. "apply_id" => $data['rm'],
  1838. "apply_name" => $data['ri'],
  1839. "addrid" => $vmp,
  1840. "post_name" => "",
  1841. "post_code" => "",
  1842. "post_fee" => 0,
  1843. "sendtime" => date("Y-m-d H:i:s"),
  1844. "send_num" => $value['receipt_quantity'],
  1845. "check_num" => 0,
  1846. "error_num" => 0,
  1847. "wsm_code" => "",
  1848. "order_type" => 3,//3咨询商品
  1849. "status" => 0,
  1850. "addtime" => date("Y-m-d H:i:s"),
  1851. "updatetime" => date("Y-m-d H:i:s")
  1852. ];
  1853. $ou = Db::name("order_out")->insertGetId($out);
  1854. if ($ou == false) {
  1855. throw new Exception("发货单生成失败");
  1856. }
  1857. // OrderOutChild::makeChild($outCode);
  1858. //修改状态,添加待办
  1859. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  1860. "order_code" => $outCode,//项目编码
  1861. "status" => 0,//这里的status是之前的值
  1862. "action_remark" => '',//备注
  1863. "action_type" => "status"//新建create,编辑edit,更改状态status
  1864. ], "CKD", 0, $out);
  1865. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  1866. "order_type" => 'CKD',
  1867. "order_code" => $outCode,
  1868. "order_id" => $ou,
  1869. "order_status" => 0,
  1870. "before_status" => 0,
  1871. 'holder_id'=>$data['rm'],
  1872. 'wait_id'=>$holder_id,
  1873. 'wait_name'=>$holder_name,
  1874. ]);
  1875. $tep = [
  1876. "cgdNo" => $order['cgdNo'],
  1877. "outCode" => $outCode,
  1878. "send_num" => $value['receipt_quantity'],
  1879. "status" => 1,
  1880. "addtime" => date("Y-m-d H:i:s"),
  1881. "updatetime" => date("Y-m-d H:i:s")
  1882. ];
  1883. $sen = Db::name("order_send")->save($tep);
  1884. if ($sen == false) {
  1885. throw new Exception("发货单关联采购单生成失败");
  1886. }
  1887. $standing_book_data['outCode'][] = $outCode;
  1888. } else {
  1889. throw new Exception("地址录入失败");
  1890. }
  1891. }
  1892. }
  1893. }
  1894. return true;
  1895. }
  1896. /**
  1897. * @param $data
  1898. * @return bool
  1899. * @throws \think\db\exception\DataNotFoundException
  1900. * @throws \think\db\exception\DbException
  1901. * @throws \think\db\exception\ModelNotFoundException
  1902. */
  1903. public function project_good($data, array &$standing_book_data = [])
  1904. {
  1905. $good = Db::name('good')
  1906. ->alias("a")
  1907. ->leftJoin("good_platform b", "a.spuCode=b.spuCode")
  1908. ->where(["b.skuCode" => $data['skuCode'], "a.is_del" => 0, "b.is_del" => 0])
  1909. ->field("a.*,b.skuCode,b.platform_code,b.is_fixed")->find();
  1910. if ($good == false) {
  1911. // return 4; //商品信息未找到
  1912. throw new Exception("商品信息未找到");
  1913. }
  1914. $goodlass = Db::name("good_ladder")->where(["skuCode" => $data['skuCode'], "is_del" => 0, "status" => 1])->where([["min_num", "<=", $data['good_num']]])->order("min_num desc")->find();
  1915. if ($goodlass == false) {
  1916. // /return 4;//商品信息未找到
  1917. throw new Exception("商品售价信息未找到");
  1918. }
  1919. $sale_price = $goodlass['sale_price'];
  1920. $origin = Db::name('good_nake')->where([['spuCode', '=', $data['spuCode']], ['min_num', '<=', $data['good_num']],
  1921. ['is_del', '=', 0]])->order('min_num desc')->find();
  1922. if ($origin == false && $good['is_stock'] ==0) {
  1923. throw new Exception('商品成本售价信息未找到');
  1924. }
  1925. $origin_price = $good['is_stock'] == 0?($origin['nake_total']??0):0;
  1926. $cgd_origin_price = $origin['supplier_origin_price']??0;
  1927. if ($good['is_gold_price'] == 1 && $good['is_stock'] == 0) {
  1928. $gold = Db::name("gold_price1")
  1929. ->field('id,price')
  1930. ->where(["type" => $good['noble_metal'], "is_del" => 0, "status" => 1])
  1931. ->order("addtime desc")
  1932. ->find();
  1933. if($good['is_fixed']==0) $origin['nake_fee']=0;
  1934. //$saleprice(最终售价) = (打样费/购买数量 + 开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价+物流费)/(1-成本售价/100);
  1935. $sale_price = $good['demo_fee'] / $data['good_num'] + $good['open_fee'] / $data['good_num'] + $good['noble_weight'] * $gold["price"] + $goodlass['cost_fee'] * $good['noble_weight'] + $origin['package_fee'] + $origin['mark_fee'] + $origin['cert_fee'] + $origin['nake_fee'] + $origin['delivery_fee'];
  1936. $good['cgd_gold_price'] = $gold["price"];
  1937. }
  1938. $orderCode = makeNo("QR");
  1939. $supplier_temp_info =[];
  1940. if($good['supplierNo']!=''){
  1941. $userCommon= \app\admin\common\User::getIns();
  1942. $supplier_temp =$userCommon->handle('sInfo',['code'=>$good['supplierNo']]);
  1943. if($supplier_temp['code']!=0 || empty($supplier_temp['data']) ) throw new Exception('供应商不存在');
  1944. $supplier_temp_info = $supplier_temp['data'];
  1945. }
  1946. $datas = [
  1947. "orderCode" => $orderCode,
  1948. "good_code" => $data['spuCode'],
  1949. "skuCode" => $data['skuCode'],
  1950. "customer_code" => $data['customerNo'],
  1951. "customerName" => $data['customerName'],
  1952. "good_name" => isset($good['good_name']) && $good['good_name'] !== '' ? $good['good_name'] : '',
  1953. "good_num" => $data['good_num'],
  1954. "cat_id" => $good['cat_id'],
  1955. "apply_id" => $data['rm'],
  1956. "apply_name" => $data['ri'],
  1957. "origin_price" => $origin_price,
  1958. "sale_price" => $sale_price,
  1959. "post_fee" => 0,
  1960. "status" => 0,
  1961. "send_num" => 0,
  1962. "wsend_num" => $data['good_num'],
  1963. "send_status" => 1,
  1964. "good_type" => 1,
  1965. "send_type" => $data['sendtype'],
  1966. "supplierNo" => $data['companyNo'],
  1967. "supplierName" => $data['companyName'],
  1968. "supName" => $good['supplierName'],
  1969. "supNo" => $good['supplierNo'],
  1970. 'use_order'=>$data['useid']??0,
  1971. "is_del" => 0,
  1972. "zxNo" => $data["pgNo"],
  1973. "platform_order" => "",
  1974. "platform_id" => $data['platform_id'],
  1975. "remark" => "",
  1976. "is_stock" => $good['is_stock'],
  1977. 'open_type'=>$good['open_type'],
  1978. "is_activity" => 0,
  1979. "activity_code" => '',
  1980. "proof_id" => 0,
  1981. "order_type" => $good['is_stock'] == 1 ? 1 : 2,//1备库,2非库存品
  1982. 'order_source' => 3,//3项目
  1983. 'good_weight' => $good['noble_weight'],
  1984. 'gold_price' => isset($good['cgd_gold_price']) ? $good['cgd_gold_price'] : 0,
  1985. 'cost_price' => $goodlass['cost_fee'],
  1986. 'diff_weight' => 0,
  1987. 'diff_fee' => 0,
  1988. "addtime" => date("Y-m-d H:i:s"),
  1989. "updatetime" => date("Y-m-d H:i:s"),
  1990. 'total_price' => round($sale_price * $data['good_num'], 2),
  1991. 'cgderid' => $supplier_temp_info['personid']??"0",
  1992. 'cgder' => $supplier_temp_info['person']??"",//采购员(供应商负责人)
  1993. 'good_createrid' => $good['createrid'],
  1994. 'good_creater' => $good['creater'],//商品创建人
  1995. "manager"=>$data['manager'],
  1996. "managerid"=>$data['managerid'],
  1997. ];
  1998. $datainfo = Db::name('sale')->insert($datas, true);
  1999. if ($datainfo > 0) {
  2000. $standing_book_data['sale_id'] = $datainfo;
  2001. //修改状态,添加待办
  2002. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  2003. "order_code" => $orderCode,//项目编码
  2004. "status" => 0,//这里的status是之前的值
  2005. "action_remark" => '',//备注
  2006. "action_type" => "status"//新建create,编辑edit,更改状态status
  2007. ], "XSQRD", 0, $datas);
  2008. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  2009. "order_type" => 'XSQRD',
  2010. "order_code" => $orderCode,
  2011. "order_id" => $datainfo,
  2012. "order_status" => 0,
  2013. "before_status" => 0,
  2014. 'holder_id' => $datas['apply_id']
  2015. ]);
  2016. $cgd = [
  2017. "supplierNo" => $good['supplierNo'],
  2018. "supplierName" =>$good['supplierName'],
  2019. "companyNo" => $data['companyNo'],
  2020. "companyName" => $data['companyName'],
  2021. "orderCode" => $orderCode,
  2022. "spuCode" => $good['spuCode'],
  2023. "skuCode" => $good['skuCode'],
  2024. "good_name" => $good['good_name'],
  2025. "sale_price" => $origin_price,
  2026. "total_fee" => $origin_price * $data['good_num'],
  2027. "pakge_fee" => isset($origin['package_fee']) ? $origin['package_fee'] : 0,
  2028. "cert_fee" => isset($origin['cert_fee']) ? $origin['cert_fee'] : 0,
  2029. "open_fee" => $good['open_fee'],
  2030. "cost_fee" => isset($origin['cost_fee']) ? $origin['cost_fee'] : 0,
  2031. "mark_fee" => isset($origin['mark_fee']) ? $origin['mark_fee'] : 0,
  2032. "demo_fee" => $good['demo_fee'],
  2033. "nake_fee" => isset($origin['nake_fee']) ? $origin['nake_fee'] : 0,
  2034. "delivery_fee" => isset($origin['delivery_fee']) ? $origin['delivery_fee'] : 0,
  2035. "good_num" => $data['good_num'],
  2036. "good_type" => 1,
  2037. "weight" => $good['noble_weight'],
  2038. "gold_price" => isset($good['cgd_gold_price']) ? $good['cgd_gold_price'] : 0,
  2039. "createrid" => $supplier_temp_info['personid']??0,
  2040. "creater" => $supplier_temp_info['person']??"",
  2041. "order_type" => $good['is_stock'] == 1 ? 1 : 2,
  2042. 'open_type'=>$good['open_type'],
  2043. 'order_source' => 3,//3项目
  2044. 'send_way' => 2,
  2045. 'good_createrid' => $good['createrid'],
  2046. 'good_creater' => $good['creater'],//商品创建人
  2047. 'supplier_origin_price'=>$cgd_origin_price,//供应商采购成本
  2048. 'cgd_supplier_code'=>$good['cgd_supplier_code'],//采购供应商编号
  2049. 'cgd_supplier_name'=>$good['cgd_supplier_name'],//采购供应商编号
  2050. 'cgd_apply_id'=>$good['cgd_apply_id'],//竞单人
  2051. 'cgd_apply_name'=>$good['cgd_apply_name'],//竞单人
  2052. ];
  2053. $taxInfo = OrderTax::SpuCat( $data['spuCode']);
  2054. if (!empty($taxInfo)) {
  2055. (new \app\admin\model\OrderTax)->saveAll(array_map(function ($item)use($orderCode){
  2056. $temp=[];
  2057. $temp['code']=$orderCode;
  2058. $temp['spuCode']=$item['spuCode'];
  2059. $temp['tax_code']=$item['cat_code'];
  2060. $temp['tax_name']=$item['cat_name'];
  2061. $temp['tax_short_name']=$item['short_name'];
  2062. $temp['inv_tax']=$item['tax'];
  2063. $temp['inv_good_name']=$item['inv_good_name'];
  2064. return $temp;
  2065. },$taxInfo));
  2066. }
  2067. if ($good['is_stock'] == 0) {
  2068. $bol = $this->createCgd($cgd, $standing_book_data);
  2069. if ($bol == false) {
  2070. // return 3;//关联采购失败
  2071. throw new Exception("采购单生成失败");
  2072. }
  2073. } else {
  2074. if($good['usable_stock'] < $data['good_num']){
  2075. throw new Exception("可用库存数不足");
  2076. }
  2077. $stockUp =[
  2078. "usable_stock"=>$good['usable_stock']-$data['good_num'],
  2079. "updatetime"=>date("Y-m-d H:i:s"),
  2080. ];
  2081. $goodStockUp = Db::name("good")->where(['spuCode'=>$good['spuCode'],"usable_stock"=>$good['usable_stock']])->update
  2082. ($stockUp);
  2083. if($goodStockUp==false) throw new Exception("商品可用库存更新失败");
  2084. SaleCgdPrice::GetPrice($datainfo);
  2085. }
  2086. if ($data['sendtype'] == 1 && !empty($data['addrlist'])) {
  2087. $i = 1;
  2088. foreach ($data['addrlist'] as $value) {
  2089. $temp = [];
  2090. $addrs = [];
  2091. if ($value['addr_code'] !== '' && is_array($value['addr_code']) && !empty($value['addr_code'])) {
  2092. $addrs['provice_code'] = $value['addr_code'][0];
  2093. $addrs['city_code'] = $value['addr_code'][1];
  2094. $addrs['area_code'] = $value['addr_code'][2];
  2095. $addr = json_encode($addrs);
  2096. } else {
  2097. $addr = isset($value['addr_code']) ? $value['addr_code'] : '';
  2098. }
  2099. if($value['receipt_quantity']<=0) throw new Exception('地址发货数量不能小于1');
  2100. $temp['orderCode'] = $orderCode;
  2101. $temp['contactor'] = $value['contactor'];
  2102. $temp['mobile'] = $value['mobile'];
  2103. $temp['addr'] = $value['addr'];
  2104. $temp['addr_code'] = $addr;
  2105. $temp['customer_code'] = $data['companyNo'];
  2106. $temp['receipt_quantity'] = $value['receipt_quantity'];
  2107. $temp['post_fee'] = 0;
  2108. $temp['is_del'] = 0;
  2109. $temp['addtime'] = date("Y-m-d H:i:s");
  2110. $temp['updatetime'] = date("Y-m-d H:i:s");
  2111. $temp['arrive_time'] = $data['arrtime'];
  2112. $vmp = Db::name('order_addr')->insert($temp, true);
  2113. if ($vmp > 0) {
  2114. $standing_book_data['customer_code'] = $temp['customer_code'];
  2115. $num = $value['receipt_quantity'];
  2116. $outCode = makeNo("DF");
  2117. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  2118. $i++;
  2119. if ($good['is_stock'] != 1) {
  2120. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])
  2121. ->find();
  2122. if ($order == false) {
  2123. throw new Exception("关联采购单信息未找到");
  2124. }
  2125. $tep = [
  2126. "cgdNo" => $order['cgdNo'],
  2127. "outCode" => $outCode,
  2128. "send_num" => $num,
  2129. "status" => 1,
  2130. "addtime" => date("Y-m-d H:i:s"),
  2131. "updatetime" => date("Y-m-d H:i:s")
  2132. ];
  2133. $sen = Db::name("order_send")->save($tep);
  2134. if ($sen == false) {
  2135. throw new Exception("发货单关联采购单信息失败");
  2136. }
  2137. $cgdinfo = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  2138. if ($cgdinfo == false) {
  2139. throw new Exception("采购单信息未找到");
  2140. }
  2141. }
  2142. $out = [
  2143. "orderCode" => $orderCode,
  2144. "outCode" => $outCode,
  2145. "apply_id" => $data['rm'],
  2146. "apply_name" => $data['ri'],
  2147. "addrid" => $vmp,
  2148. "post_name" => "",
  2149. "post_code" => "",
  2150. "post_fee" => 0,
  2151. "sendtime" => date("Y-m-d H:i:s"),
  2152. "send_num" => $num,
  2153. "check_num" => 0,
  2154. "error_num" => 0,
  2155. "wsm_code" => $good['is_stock'] == 1 ?'':$cgdinfo['wsm_code'],
  2156. "order_type" => 1,
  2157. "status" => $good['is_stock'] == 1 ? 1 : 0,
  2158. "addtime" => date("Y-m-d H:i:s"),
  2159. "updatetime" => date("Y-m-d H:i:s")
  2160. ];
  2161. $ou = Db::name("order_out")->insertGetId($out);
  2162. if ($ou == false) {
  2163. throw new Exception("发货单生成失败");
  2164. } else {
  2165. if($good['is_stock'] == 1)OrderOutChild::makeChild($outCode);//非库存品无法立即分单
  2166. $standing_book_data['outCode'][] = $outCode;
  2167. //修改状态,添加待办
  2168. if($good['is_stock'] == 1){
  2169. $roleid = config('app.wsm_cgder_role');
  2170. $uids = Db::name('user_role')
  2171. ->where('is_del', 0)
  2172. ->whereIn('roleid', $roleid)
  2173. ->column('uid');
  2174. $handle_user_list=implode(',', $uids);
  2175. }
  2176. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], [
  2177. "order_code" => $outCode,//出库单号
  2178. "status" => 0,//这里的status是之前的值
  2179. "action_remark" => '',//备注
  2180. "action_type" => "create"//新建create,编辑edit,更改状态status
  2181. ], "CKD", 0, $out);
  2182. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  2183. "order_type" => 'CKD',
  2184. "order_code" => $outCode,//出库单号
  2185. "order_id" => $ou,
  2186. "order_status" => $good['is_stock'] == 1 ? 1 : 0,
  2187. "before_status" => 0,
  2188. 'holder_id'=>$out['apply_id'],
  2189. 'handle_user_list' =>$handle_user_list??""
  2190. ]);
  2191. }
  2192. } else {
  2193. throw new Exception("地址录入失败");
  2194. }
  2195. }
  2196. }
  2197. // }
  2198. }
  2199. return true;
  2200. }
  2201. public function list()
  2202. {
  2203. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  2204. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  2205. $where[] = ['s.is_del', "=", 0];
  2206. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  2207. if ($orderCode !== "") {
  2208. $where[] = ['s.orderCode', "like", "%$orderCode%"];
  2209. }
  2210. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !== "" ? trim($this->post['apply_name']) : "";
  2211. if ($apply_name !== "") {
  2212. $where[] = ['s.apply_name', "like", "%$apply_name%"];
  2213. }
  2214. $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim($this->post['good_name']) : "";
  2215. if ($good_name !== "") {
  2216. $where[] = ['s.good_name', "like", "%$good_name%"];
  2217. }
  2218. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  2219. if ($good_code !== "") {
  2220. $where[] = ['s.good_code', "like", "%$good_code%"];
  2221. }
  2222. $platform_order = isset($this->post['platform_order']) && $this->post['platform_order'] !== '' ? trim
  2223. ($this->post['platform_order']) : '';
  2224. if ($platform_order !== '') {
  2225. $where[] = ['s.platform_order', 'like', "%$platform_order%"];
  2226. }
  2227. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code'] !== "" ? trim($this->post['customer_code']) : "";
  2228. if ($customer_code !== "") {
  2229. $where[] = ['s.customer_code', "like", "%$customer_code%"];
  2230. }
  2231. $customer_name = isset($this->post['customer_name']) && $this->post['customer_name'] !== '' ? trim($this->post['customer_name']) : '';
  2232. if ($customer_name !== '') {
  2233. $where[] = ['s.customerName', 'like', "%$customer_name%"];
  2234. }
  2235. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  2236. if ($supplierNo !== "") {
  2237. $where[] = ['s.supplierNo', "=", $supplierNo];
  2238. }
  2239. $relaComNo = trim($this->post['relaComNo']??'');
  2240. if ($relaComNo !== "") {
  2241. $where[] = ['s.supplierNo', "=", $relaComNo];
  2242. }
  2243. $ordertype = isset($this->post['order_type']) && $this->post['order_type'] !== "" ? intval($this->post['order_type']) : "";
  2244. if ($ordertype !== "") {
  2245. $where[] = ['s.order_type', "=", $ordertype];
  2246. }
  2247. $status = isset($this->post['status']) && $this->post['status'] !== "" ? trim($this->post['status']) : "";
  2248. if ($status !== "") {
  2249. $where[] = ['s.status', "=", $status];
  2250. }
  2251. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  2252. if ($start != "") {
  2253. $where[] = ["s.addtime", '>=', $start];
  2254. }
  2255. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  2256. if ($end != "") {
  2257. $end .= " 23:59:59";
  2258. $where[] = ["s.addtime", '<=', $end];
  2259. }
  2260. $use_type = $this->post['use_type']??'';
  2261. if($use_type!=='') $where[]=['e.use_type','=',$use_type];
  2262. $platform_id = $this->post['platform_id']??'';
  2263. if($platform_id!=='') $where[]=['s.platform_id','=',$platform_id];
  2264. $role = $this->checkDataShare();
  2265. $hand = resign_hand_user($this->uid,0);
  2266. if (!empty($role[DataGroupModel::$type_全部])) {
  2267. $arr= array_unique(array_merge($hand,$role[DataGroupModel::$type_全部]));
  2268. $where[] = ['s.apply_id', 'in',$arr];
  2269. }
  2270. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  2271. if ($company_name !== "") $where[] = ["s.apply_id", 'in', get_company_item_user_by_name($company_name)];
  2272. $order_source = isset($this->post['order_source']) && $this->post['order_source'] !== "" ? trim($this->post['order_source']) : "";
  2273. if ($order_source !== "") $where[] = ["s.order_source", '=', $order_source];
  2274. $count = Db::name('sale')
  2275. ->alias('s')
  2276. ->leftJoin('order_num b','b.orderCode=s.orderCode')
  2277. // ->leftJoin('order_use c','c.id=s.use_order')
  2278. ->leftJoin('purchease_order d','d.cgdNo=b.cgdNo')
  2279. ->leftJoin('platform e','e.id=s.platform_id')
  2280. ->where($where)
  2281. ->count();
  2282. $total = ceil($count / $size);
  2283. $page = $page >= $total ? $total : $page;
  2284. $list = Db::name('sale')
  2285. ->alias('s')
  2286. ->field('s.*,b.cgdNo,s.use_order useage,d.status cgd_status,e.use_type,e.platform_name')
  2287. ->leftJoin('order_num b','b.orderCode=s.orderCode')
  2288. // ->leftJoin('order_use c','c.id=s.use_order')
  2289. ->leftJoin('purchease_order d','d.cgdNo=b.cgdNo')
  2290. ->leftJoin('platform e','e.id=s.platform_id')
  2291. ->order("s.addtime desc")
  2292. ->page($page, $size)
  2293. ->where($where)
  2294. ->select()
  2295. ->toArray();
  2296. $all_createrid = array_unique(array_column($list, 'apply_id'));
  2297. $item = get_company_name_by_uid($all_createrid);
  2298. $useAddr = \app\admin\model\OrderUse::where(["is_del"=>1])->column("order_use","id");
  2299. $data = [];
  2300. foreach ($list as $value) {
  2301. $value['useage'] = $useAddr[$value['useage']]??"";
  2302. if ($value['order_type'] == 3 ||$value['order_type']==4) {
  2303. $var = Db::name("good_zixun")->where(["spuCode" => $value['good_code'], "is_del" => 0])->find();
  2304. $value["speclist"] = isset($var['specinfo']) && $var['specinfo'] != "" ? json_decode($var['specinfo'], true) : "";
  2305. } else {
  2306. $var = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  2307. ->where(['a.skuCode' => $value['skuCode']])->find();
  2308. $spec = Db::name("good_spec")->where(["spuCode" => $var['spuCode'], "is_del" => 0])->select()->toArray();
  2309. $speclist = [];
  2310. if (!empty($spec)) {
  2311. foreach ($spec as $val) {
  2312. $temp = [];
  2313. $temp['id'] = $val['id'];
  2314. $temp['spuCode'] = $val['spuCode'];
  2315. $temp['spec_id'] = $val['spec_id'];
  2316. $temp['spec_value_id'] = $val['spec_value_id'];
  2317. $sp = Db::name("specs")->where(["id" => $val['spec_id']])->find();
  2318. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  2319. $spv = Db::name("spec_value")->where(["id" => $val['spec_value_id']])->find();
  2320. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  2321. $speclist[] = $temp;
  2322. }
  2323. }
  2324. $value["speclist"] = empty($speclist) ? [] : $speclist;
  2325. }
  2326. $value['can'] = isset($var['cat_id']) && $var['cat_id'] != 0 ? made($var['cat_id']) : [];
  2327. $value['good_num'] -= $value['th_num'];
  2328. $value['send_num'] -= $value['th_num'];
  2329. $value['total_price'] = round($value['total_price'] - $value['th_fee'], 2);
  2330. $value['company_name'] = $item[$value['apply_id']]??'';
  2331. $value['infoNo'] = ConsultBids::where("bidNo",$value['zxNo'])->value("infoNo","");
  2332. //是否具有编辑权限
  2333. $value['is_allow_update'] = 0;
  2334. if (in_array($this->roleid, [1, 33]) || in_array($value['apply_id'], $role[DataGroupModel::$type_可编辑])) $value['is_allow_update'] = 1;
  2335. $data[] = $value;
  2336. }
  2337. return app_show(0, "获取成功", ['count' => $count, 'list' => $data]);
  2338. }
  2339. public function edit()
  2340. {
  2341. // $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2342. $id = isset($this->post['id']) && $this->post['id'] !== "" ? trim($this->post['id']) : "";
  2343. if ($id == "") {
  2344. return error_show(1002, "参数id不能为空");
  2345. }
  2346. $etid = Db::name("sale")->where(["id" => $id, "is_del" => 0])->find();
  2347. if (empty($etid)) {
  2348. return error_show(1002, "未找到数据");
  2349. }
  2350. // $orderCode=isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  2351. // if($orderCode==""){
  2352. // return error_show(1002,"销售单code不能为空");
  2353. // }
  2354. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  2355. if ($good_code == "") {
  2356. return error_show(1002, "商品code不能为空");
  2357. }
  2358. $ct = Db::name('good')->alias('a')->join('good_type b', 'b.good_code=a.good_code', 'left')
  2359. ->where(['b.type_code' => $good_code])->find();
  2360. if ($ct == "") {
  2361. return error_show(1002, "未找到商品数据");
  2362. }
  2363. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code'] !== "" ? trim($this->post['customer_code']) : "";
  2364. if ($customer_code == "") {
  2365. return error_show(1002, "客户code不能为空");
  2366. }
  2367. // $customer = Db::name("customer_info")->where(["companyNo" => $customer_code])->find();
  2368. // if ($customer == false) {
  2369. // return error_show(1004, "未找到客户数据");
  2370. // }
  2371. $supplierNo = $this->post['supplierNo'] && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  2372. if ($supplierNo == "") {
  2373. return error_show(1002, "参数supplierNo不能为空");
  2374. }
  2375. // $supplier = Db::name("supplier")->where(["code" => $supplierNo])->find();
  2376. // if ($supplier == false) {
  2377. // return error_show(1004, "未找到平台供应商数据");
  2378. // }
  2379. // $apply_id = GetUserInfo($token);
  2380. // if (empty($apply_id) || $apply_id['code'] != 0) {
  2381. // return error_show(1002, "申请人数据不存在");
  2382. // }
  2383. $rm = $this->uid;//isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  2384. $ri = $this->uname;//isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  2385. // $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim
  2386. //($this->post['good_name']) : "";
  2387. // $good_num = isset($this->post['good_num']) && $this->post['good_num'] !== "" ? intval
  2388. //($this->post['good_num']) : "";
  2389. // $origin_price = isset($this->post['origin_price']) && $this->post['origin_price'] !== "" ? intval
  2390. // ($this->post['origin_price']) : "";
  2391. // $sale_price = isset($this->post['sale_price']) && $this->post['sale_price'] !== "" ? trim($this->post['sale_price']) : "";
  2392. // $total_price = isset($this->post['total_price']) && $this->post['total_price'] !== "" ? trim($this->post['total_price']) : "";
  2393. // $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !== "" ? trim($this->post['post_fee']) : "";
  2394. // $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  2395. $vr = isset($this->post['order_addr']) && $this->post['order_addr'] !== "" ? $this->post['order_addr'] : "";
  2396. if ($vr == "") {
  2397. return error_show(1002, "参数order_addr不能为空");
  2398. }
  2399. $vi = isset($this->post['good_stock']) && $this->post['good_stock'] !== "" ? $this->post['good_stock'] : "";
  2400. if ($vi == "") {
  2401. return error_show(1002, "参数good_stock不能为空");
  2402. }
  2403. $good_num = intval(array_sum(array_column($vi, "num")));
  2404. Db::startTrans();
  2405. try {
  2406. $datn = [
  2407. "id" => $id,
  2408. "good_code" => $good_code,
  2409. "customer_code" => $customer_code,
  2410. "good_num" => $good_num,
  2411. "supplierNo" => $supplierNo,
  2412. "apply_id" => $rm,
  2413. "apply_name" => $ri,
  2414. "is_del" => 0,
  2415. "updatetime" => date("Y-m-d H:i:s")
  2416. ];
  2417. $datninfo = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->save($datn);
  2418. if ($datninfo > 0) {
  2419. $stn = ["order_code" => $etid['orderCode'], "status" => $etid['status'], "action_remark" => '', "action_type" => "edit"];
  2420. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], $stn, "XSQRD", $etid['status'], $datn);
  2421. // $order=["order_type"=>'XSQRD',"order_code"=>$etid['orderCode'],"order_id"=>$id,"order_status"=>$etid['status'],"before_status"=> 0];
  2422. // ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname],$order);
  2423. //更新台账
  2424. Db::name('standing_book')
  2425. ->where('orderCode', $etid['orderCode'])
  2426. ->update([
  2427. 'customer_code' => $customer_code,
  2428. 'supplierNo' => $supplierNo,
  2429. 'updatetime' => date("Y-m-d H:i:s")
  2430. ]);
  2431. foreach ($vi as $ion) {
  2432. $iten = [];
  2433. isset($ion['id']) && $ion['id'] !== "" ? $iten['id'] = $ion['id'] : '';
  2434. $iten['orderCode'] = $etid['orderCode'];
  2435. $iten['wsm_code'] = $ion['wsm_code'];
  2436. $iten['stock_id'] = 0;
  2437. $iten['num'] = $ion['num'];
  2438. $iten['status'] = 1;
  2439. $iten['sale_price'] = $ct['original_price'];
  2440. isset($ion['id']) && $ion['id'] !== "" ? '' : $iten['addtime'] = date("Y-m-d H:i:s");
  2441. $iten['updatetime'] = date("Y-m-d H:i:s");
  2442. $vp = Db::name('sale_info')->save($iten);
  2443. if ($vp == false) {
  2444. Db::rollback();
  2445. return error_show(1002, "更新失败");
  2446. }
  2447. }
  2448. foreach ($vr as $value) {
  2449. $temp = [];
  2450. isset($value['id']) && $value['id'] !== "" ? $temp['id'] = $value['id'] : '';
  2451. $temp['orderCode'] = $etid['orderCode'];
  2452. $temp['contactor'] = $value['contactor'];
  2453. $temp['mobile'] = $value['mobile'];
  2454. $temp['addr'] = $value['addr'];
  2455. $temp['addr_code'] = $value['addr_code'];
  2456. $temp['customer_code'] = $customer_code;
  2457. $temp['receipt_quantity'] = $value['receipt_quantity'];
  2458. $temp['post_fee'] = 0;
  2459. $temp['is_del'] = $value['is_del'];
  2460. isset($value['id']) && $value['id'] !== "" ? '' : $temp['addtime'] = date("Y-m-d H:i:s");
  2461. $temp['updatetime'] = date("Y-m-d H:i:s");
  2462. $temp['arrive_time'] = date("Y-m-d H:i:s");
  2463. $dat = Db::name('order_addr')->save($temp);
  2464. if ($dat == false) {
  2465. Db::rollback();
  2466. return error_show(1002, "更新失败");
  2467. }
  2468. }
  2469. Db::commit();
  2470. return error_show(0, "更新成功");
  2471. }
  2472. Db::rollback();
  2473. return error_show(1002, "更新失败");
  2474. } catch (Exception $e) {
  2475. Db::rollback();
  2476. return error_show(1005, $e->getMessage());
  2477. }
  2478. }
  2479. public function info()
  2480. {
  2481. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2482. if ($id == "") {
  2483. return error_show(1002, "参数id不能为空");
  2484. }
  2485. $einfo = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->find();
  2486. if (empty($einfo)) {
  2487. return error_show(1002, "未找到销售订单数据");
  2488. }
  2489. if ($einfo['order_type'] == 3 || $einfo['order_type']==4) {
  2490. $goon = Db::name("good_zixun")->where(["spuCode" => $einfo['good_code'], "is_del" => 0])->find();
  2491. } else {
  2492. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')->where(['a.skuCode' => $einfo['skuCode']])->find();
  2493. }
  2494. if ($goon == false) {
  2495. return error_show(1003, "未找到商品数据");
  2496. } else {
  2497. $taxInfo=OrderTax::where(['code'=>$einfo['orderCode']])->findOrEmpty();
  2498. $einfo['tax_name'] = $taxInfo['tax_name']??'';
  2499. $einfo['tax_short_name'] =$taxInfo['tax_short_name']??'';
  2500. $einfo['tax_code'] =$taxInfo['tax_code']??'';
  2501. $einfo['tax']=$taxInfo['inv_tax']??'';
  2502. $einfo['inv_good_name']=$taxInfo['inv_good_name']??'';
  2503. $goon['exclusive'] = isset($goon['is_exclusive']) ? makeExcluse($goon['is_exclusive']) : "";
  2504. $unit = Db::name("unit")->where(["id" => $goon['good_unit']])->find();
  2505. $goon['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  2506. $spec = Db::name("good_spec")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->select()->toArray();
  2507. $speclist = [];
  2508. if (!empty($spec)) {
  2509. foreach ($spec as $value) {
  2510. $temp = [];
  2511. $temp['id'] = $value['id'];
  2512. $temp['spuCode'] = $value['spuCode'];
  2513. $temp['spec_id'] = $value['spec_id'];
  2514. $temp['spec_value_id'] = $value['spec_value_id'];
  2515. $temp['is_del'] = $value['is_del'];
  2516. $sp = Db::name("specs")->where(["id" => $value['spec_id']])->find();
  2517. $temp['spec_name'] = isset($sp["spec_name"]) ? $sp["spec_name"] : "";
  2518. $spv = Db::name("spec_value")->where(["id" => $value['spec_value_id']])->find();
  2519. $temp['spec_value'] = isset($spv["spec_value"]) ? $spv["spec_value"] : "";
  2520. $speclist[] = $temp;
  2521. }
  2522. }
  2523. $goon["speclist"] = empty($speclist) ? [] : $speclist;
  2524. $proof = Db::name("good_proof")->where(["spuCode" => $goon['spuCode'], "is_del" => 0])->order("updatetime desc")->field("id,proof_type,proof_url")->find();
  2525. $goon['proof'] = isset($proof) && $proof != false ? $proof : [];
  2526. $goon['origin_place_cn'] = "";
  2527. $goon['delivery_place_cn'] = "";
  2528. if (isset($goon['delivery_place']) && $goon['delivery_place'] !== "") {
  2529. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  2530. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $goon['delivery_place']);
  2531. $goon['delivery_place_cn'] = GetAddr(json_encode($place));
  2532. }
  2533. if (isset($goon['delivery_place']) && $goon['origin_place'] !== "") {
  2534. $place = ["provice_code" => "", "city_code" => "", "area_code" => ""];
  2535. list($place['provice_code'], $place['city_code'], $place['area_code']) = explode(",", $goon['origin_place']);
  2536. $goon['origin_place_cn'] = GetAddr(json_encode($place));
  2537. }
  2538. if ($goon['brand_id'] != 0) {
  2539. $brand = Db::name("brand")->where(["id" => $goon['brand_id']])->find();
  2540. $goon["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  2541. } else {
  2542. $goon["brand_name"] = "";
  2543. $goon["brand_id"] = "";
  2544. }
  2545. $userCommon = \app\admin\common\User::getIns();
  2546. $names = $userCommon->handle('getCodeAndName',['code'=>[
  2547. $goon['supplierNo'],
  2548. $goon['companyNo'],
  2549. $einfo['customer_code'],
  2550. $einfo['supplierNo']
  2551. ]]);
  2552. $goon['supplierName'] = $names['data'][$goon['supplierNo']]??'';//isset($supplier['name']) ? $supplier['name'] : "";
  2553. $goon['noble_name'] = isset($goon['noble_metal']) && $goon['noble_metal'] != 0 ? $this->noble[$goon['noble_metal']] : "";
  2554. $goon['company'] = $names['data'][$goon['companyNo']]??'';//isset($company['company']) ? $company['company'] : "";
  2555. }
  2556. $int = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  2557. // $in = Db::name('customer_info')->where(['companyNo' => $einfo['customer_code']])->field('companyName')->find();
  2558. $einfo['supplierName'] = '';
  2559. if ($einfo['supplierNo'] != "") {
  2560. $einfo['supplierName'] = $names['data'][$einfo['supplierNo']]??'';//isset($supplier['company']) ? $supplier['company'] : "";
  2561. }
  2562. $addr_sum = Db::name('order_addr')->where(['orderCode' => $einfo['orderCode'],"is_del"=>0])->sum('receipt_quantity');
  2563. $einfo['addr_sum']=$addr_sum;
  2564. $addrs = [];
  2565. $einfo['useage'] = "";
  2566. if ($einfo['use_order'] != 0) {
  2567. $use = Db::name("order_use")->where(["id" => $einfo['use_order']])->find();
  2568. $einfo['useage'] = isset($use['order_use']) ? $use['order_use'] : "";
  2569. } else {
  2570. $einfo['use_order'] = '';
  2571. }
  2572. $einfo['platform_name'] = '';
  2573. if ($einfo['platform_id'] != 0) {
  2574. $plat = Db::name("platform")->where(['id' => $einfo['platform_id']])->find();
  2575. $einfo['platform_name'] = isset($plat['platform_name']) ? $plat['platform_name'] : "";
  2576. }
  2577. $einfo['companyName'] = $names['data'][$einfo['customer_code']]??'';//isset($in['companyName']) ? $in['companyName'] : "";
  2578. $einfo['addrs'] = $addrs;
  2579. $einfo['can'] = $int;
  2580. $einfo['goodinfo'] = $goon;
  2581. $einfo['good_num'] -= $einfo['th_num'];
  2582. $einfo['send_num'] -= $einfo['th_num'];
  2583. $einfo['total_price'] = round($einfo['total_price'] - $einfo['th_fee'], 2);
  2584. //处理凭证
  2585. if ($einfo['order_type'] == 1 || $einfo['order_type'] == 2) {
  2586. $proof = Db::name('good_proof')
  2587. ->field('id,proof_type,proof_url')
  2588. ->where(['id' => $einfo['proof_id'], 'is_del' => 0])
  2589. ->find();
  2590. $einfo['proof_type'] = isset($proof['proof_type']) ? $proof['proof_type'] : '';
  2591. $einfo['proof_url'] = isset($proof['proof_url']) ? $proof['proof_url'] : '';
  2592. } elseif ($einfo['order_type'] == 3 ||$einfo['order_type']==4) {
  2593. $einfo['proof_type'] = isset($goon['proof_type']) ? $goon['proof_type'] : '';
  2594. $einfo['proof_url'] = isset($goon['proof_url']) ? $goon['proof_url'] : '';
  2595. } else {
  2596. $einfo['proof_type'] = '';
  2597. $einfo['proof_url'] = '';
  2598. }
  2599. if (empty($einfo)) {
  2600. return error_show(1002, "未找到销售订单数据");
  2601. } else {
  2602. return app_show(0, "获取成功", $einfo);
  2603. }
  2604. }
  2605. public function del()
  2606. {
  2607. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2608. if ($id === "") {
  2609. return error_show(1002, "参数id不能为空");
  2610. }
  2611. $ed = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->find();
  2612. if (empty($ed)) {
  2613. return error_show(1002, "未找到销售订单数据");
  2614. }
  2615. $str = Db::name('sale')->update(['id' => $id, 'is_del' => 1, 'updatetime' => date("Y-m-d H:i:s")]);
  2616. if ($str) {
  2617. $stc = ["order_code" => $id, "status" => $ed['status'], "action_remark" => '', "action_type" => "delete"];
  2618. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], $stc, "XSQRD", $ed['status'], ['id' => $id, 'is_del' => 1, 'updatetime' => date("Y-m-d H:i:s")]);
  2619. $order = ["order_type" => 'XSQRD', "order_code" => $ed['orderCode'], "order_id" => $id, "order_status" => $ed['status'],'holder_id' => $ed['apply_id']
  2620. ];
  2621. ProcessOrder::workdel($order);
  2622. return error_show(0, "删除成功");
  2623. } else {
  2624. return error_show(1002, "删除失败");
  2625. }
  2626. }
  2627. //废弃接口
  2628. public function status()
  2629. {
  2630. // $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2631. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  2632. if ($id == "") {
  2633. return error_show(1002, "参数id不能为空");
  2634. }
  2635. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  2636. if ($status == "") {
  2637. return error_show(1002, "订单状态不能为空");
  2638. }
  2639. $dio = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->find();
  2640. if (empty($dio)) {
  2641. return error_show(1002, "销售单信息未找到");
  2642. }
  2643. if ($status == 1 && $dio['order_type'] == 2) {
  2644. $zxinfo = Db::name("consult")->where(["zxNo" => $dio['zxNo'], "is_del" => 0])->find();
  2645. if ($zxinfo == false) {
  2646. return error_show(1004, "未找到咨询单数据");
  2647. }
  2648. $gold['price'] = 0;
  2649. if ($zxinfo['zx_type'] == 2) {
  2650. if ($zxinfo['metals'] == "") {
  2651. return error_show(1004, "未找到咨询单贵金属类型数据");
  2652. }
  2653. $metals = $zxinfo['metals'] == '18K' ? 1 : ($zxinfo['metals'] == '24K' ? 2 : 3);
  2654. $gold = Db::name("gold_price")->where(["type" => $metals])->order("addtime desc")->find();
  2655. if (empty($gold)) {
  2656. return error_show(1004, "未找到咨询单贵金属类型数据");
  2657. }
  2658. $dio['gold_price'] = $gold['price'];
  2659. }
  2660. $wsm = Db::name("warehouse_info")->where(["supplierNo" => $zxinfo['gysNo'], "wsm_type" => 2])->find();
  2661. if (empty($wsm)) {
  2662. return error_show(1002, "供应商仓库未找到");
  2663. }
  2664. }
  2665. // $apply_id = GetUserInfo($token);
  2666. // if (empty($apply_id) || $apply_id['code'] != 0) {
  2667. // return error_show(1002, "申请人数据不存在");
  2668. // }
  2669. $rm = $this->uid;
  2670. $ri = $this->uname;
  2671. Db::startTrans();
  2672. try {
  2673. $vmp = $dio['status'];
  2674. $dio['status'] = $status;
  2675. $dio['updatetime'] = date("Y-m-d H:i:s");
  2676. $st = Db::name('sale')->save($dio);
  2677. if ($st) {
  2678. $stn = ["order_code" => $dio['orderCode'], "status" => $vmp, "action_remark" => '', "action_type" => "status"];
  2679. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], $stn, $dio['order_type'] == 2 ? "ZXQRD" : "XSQRD", $dio['status'], $dio);
  2680. $order = ["order_type" => "XSQRD", "order_code" => $dio['orderCode'], "order_id" => $id, "order_status" => $dio['status'], "before_status" => 1,'holder_id' => $dio['apply_id']];
  2681. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], $order);
  2682. if ($status == 3 && $dio['order_type'] == 1) {
  2683. $dn = Db::name('sale_info')->where(['orderCode' => $st['orderCode']])->select();
  2684. foreach ($dn as $value) {
  2685. $dm = Db::name('good_stock')->where(['wsm_code' => $value['wsm_code'], 'good_type_code' => $st['good_code']])->find();
  2686. if ($dm == "") {
  2687. Db::rollback();
  2688. return error_show(1003, "未找到商品数据");
  2689. }
  2690. if ($value['num'] > $dm['usable_stock']) {
  2691. Db::rollback();
  2692. return error_show(1002, "超出库存数量");
  2693. }
  2694. $dm['usable_stock'] -= $value['num'];
  2695. $dm['wait_out_stock'] += $value['num'];
  2696. $send = Db::name("order_out")->where(["wsm_code" => $value['wsm_code'], "is_del" => 0])->sum("send_num");
  2697. $value['send_num'] = $send ?? 0;
  2698. $value['wsend_num'] = $value['num'] - $value['send_num'];
  2699. $Db = Db::name('good_stock')->update($dm);
  2700. $Db['updatetime'] = date('Y-m-d H:i:s');
  2701. if ($Db == false) {
  2702. Db::rollback();
  2703. return error_show(1002, "状态更新失败");
  2704. }
  2705. }
  2706. }
  2707. if ($status == 1 && $dio['order_type'] == 2) {
  2708. $cgd = makeNo("CG");
  2709. $clll = [
  2710. "cgdNo" => $cgd,
  2711. "bkcode" => $dio['orderCode'],
  2712. "wsm_code" => $wsm['wsm_code'],
  2713. "cgder_id" => Db::name('depart_user')->where('nickname', $zxinfo['saler'])->value('uid') ?? 0,//重名以及多个账号的话…………
  2714. "cgder" => $zxinfo['saler'],
  2715. "good_code" => "GD-" . $zxinfo['cpNo'],
  2716. "good_name" => $zxinfo['cpName'],
  2717. "good_type_code" => $zxinfo['cpNo'],
  2718. "good_num" => $dio['good_num'],
  2719. "good_price" => $zxinfo['total_fee'],
  2720. "total_fee" => round($zxinfo['total_fee'] * $dio['good_num'], 2),
  2721. "pakge_fee" => $zxinfo['package_fee'],
  2722. "cert_fee" => $zxinfo['cert_fee'],
  2723. "open_fee" => $zxinfo['open_fee'],
  2724. "delivery_fee" => $zxinfo['delivery_fee'],
  2725. "mark_fee" => $zxinfo['mark_fee'],
  2726. "teach_fee" => $zxinfo['cost_fee'],
  2727. "demo_fee" => $zxinfo['demo_fee'],
  2728. "nake_fee" => $zxinfo['bare_fee'],
  2729. "weight" => isset($zxinfo['weight']) ? $zxinfo['weight'] : 0,
  2730. "supplierNo" => $zxinfo['gysNo'],
  2731. "supplier_name" => $zxinfo['gysname'],
  2732. "gold_price" => $gold['price'],
  2733. "send_num" => 0,
  2734. "wsend_num" => $dio['good_num'],
  2735. "remark" => '',
  2736. "lasttime" => date("Y-m-d H:i:s"),
  2737. "is_del" => 0,
  2738. "status" => $dio['send_type'] == 1 ? 3 : 0,
  2739. "order_type" => 2,
  2740. "order_source" => 3,//3项目,废弃接口???
  2741. "addtime" => date("Y-m-d H:i:s"),
  2742. "updatetime" => date("Y-m-d H:i:s")
  2743. ];
  2744. $cgin = Db::name("purchease_order")->insert($clll);
  2745. // var_dump(Db::name("purchease_order")->getLastSql(),$clll);
  2746. if ($cgin == false) {
  2747. $stb = ["order_code" => $cgd, "status" => $clll['status'], "action_remark" => '', "action_type" => "create"];
  2748. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], $stb, "xsd", $clll['status'], $stb);
  2749. Db::rollback();
  2750. return error_show(1002, "咨询采购单创建商品失败");
  2751. }
  2752. if ($dio['send_type'] == 1) {
  2753. $addr = Db::name('order_addr')->where(["orderCode" => $dio['orderCode'], 'is_del' => 0])->select();
  2754. $i = 1;
  2755. foreach ($addr as $value) {
  2756. $outCode = makeNo("DF");
  2757. //改变编码规则,将原来的outCode后两位换成序列号
  2758. //str_pad字符串填充
  2759. $outCode = substr($outCode, 0, -2) . str_pad($i, 2, '0', STR_PAD_LEFT);
  2760. $i++;
  2761. $data = [
  2762. "wsm_code" => $wsm['wsm_code'],
  2763. "orderCode" => $dio['orderCode'],
  2764. "outCode" => $outCode,
  2765. "order_type" => $dio['order_type'],
  2766. "apply_id" => $rm,
  2767. "apply_name" => $ri,
  2768. "addrid" => $value['id'],
  2769. "post_name" => '',
  2770. "post_code" => '',
  2771. "post_fee" => '',
  2772. "sendtime" => date("Y-m-d H:i:s"),
  2773. "send_num" => $value['receipt_quantity'],
  2774. "check_num" => 0,
  2775. "error_num" => 0,
  2776. "status" => 0,
  2777. "addtime" => date("Y-m-d H:i:s"),
  2778. "updatetime" => date("Y-m-d H:i:s")
  2779. ];
  2780. $datainfo = Db::name('order_out')->insertGetId($data);
  2781. if (!$datainfo) {
  2782. $sti = ["order_code" => $dio['orderCode'], "status" => 0, "action_remark" => '', "action_type" => "create"];
  2783. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], $sti, "xsd", 0, $sti);
  2784. Db::rollback();
  2785. return error_show(1003, "创建失败");
  2786. } else {
  2787. //修改状态,添加待办
  2788. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  2789. "order_code" => $outCode,//出库单号
  2790. "status" => 0,//这里的status是之前的值
  2791. "action_remark" => '',//备注
  2792. "action_type" => "create"//新建create,编辑edit,更改状态status
  2793. ], "CKD", 0, $data);
  2794. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], [
  2795. "order_type" => 'CKD',
  2796. "order_code" => $outCode,//出库单号
  2797. "order_id" => $datainfo,
  2798. "order_status" => 0,
  2799. "before_status" => 0,
  2800. 'holder_id'=>$data['apply_id'],
  2801. ]);
  2802. }
  2803. }
  2804. $wsm_in_code = makeNo("CF");
  2805. $orin = [
  2806. "wsm_in_code" => $wsm_in_code,
  2807. "cgdNo" => $cgd,
  2808. "wsm_code" => $wsm['wsm_code'],
  2809. "wsm_reaper" => "",
  2810. "send_num" => $dio['good_num'],
  2811. "post_company" => '',
  2812. "post_code" => '',
  2813. "post_fee" => '',
  2814. "sendtime" => date("Y-m-d H:i:s"),
  2815. "apply_id" => $rm,
  2816. "apply_name" => $ri,
  2817. "wait_num" => 0,
  2818. "status" => 4,
  2819. "addtime" => date("Y-m-d H:i:s"),
  2820. "updatetime" => date("Y-m-d H:i:s")
  2821. ];
  2822. $win = Db::name("purchease_in")->insert($orin);
  2823. if (!$win) {
  2824. $stv = ["order_code" => $wsm_in_code, "status" => 4, "action_remark" => '', "action_type" => "create"];
  2825. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], $stv, "xsd", 4, $stv);
  2826. Db::rollback();
  2827. return error_show(1003, "创建失败");
  2828. }
  2829. //维护台账
  2830. Db::execute("UPDATE `wsm_standing_book` SET `wsm_in_code`=CONCAT(IFNULL(`wsm_in_code`,''),',{$wsm_in_code}'),`updatetime`='" . date('Y-m-d H:i:s') . "' WHERE `cgdNo`='{$cgd}'");
  2831. $good = Db::name("good_stock")->where(["wsm_code" => $wsm['wsm_code'], "good_type_code" => $zxinfo['cpNo'], "is_del" => 0])->find();
  2832. if (empty($good)) {
  2833. $good = [
  2834. "good_type_code" => $zxinfo['cpNo'],
  2835. "wsm_code" => $wsm['wsm_code'],
  2836. "usable_stock" => 0,
  2837. "wait_out_stock" => 0,
  2838. "total_stock" => 0,
  2839. "addtime" => date("Y-m-d H:i:s"),
  2840. "updatetime" => date("Y-m-d H:i:s"),
  2841. ];
  2842. }
  2843. $good['wait_out_stock'] += $dio['good_num'];
  2844. $good['total_stock'] = $good['usable_stock'] + $good['wait_out_stock'];
  2845. $good['updatetime'] = date("Y-m-d H:i:s");
  2846. $upd = Db::name("good_stock")->save($good);
  2847. if (!$upd) {
  2848. $ste = ["order_code" => $zxinfo['cpNo'], "status" => 0, "action_remark" => '', "action_type" => "create"];
  2849. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], $ste, "xsd", 0, $ste);
  2850. Db::rollback();
  2851. return error_show(1003, "创建失败");
  2852. }
  2853. }
  2854. }
  2855. }
  2856. Db::commit();
  2857. return error_show(0, "订单状态更新成功");
  2858. } catch (Exception $e) {
  2859. Db::rollback();
  2860. return error_show(1005, $e->getMessage());
  2861. }
  2862. }
  2863. public function fee()
  2864. {
  2865. $id = isset($this->post['id']) && $this->post['id'] !== "" ? trim($this->post['id']) : "";
  2866. if ($id == "") {
  2867. return error_show(1002, "参数id不能为空");
  2868. }
  2869. $etid = Db::name("sale")->where(["id" => $id, "is_del" => 0])->find();
  2870. if (empty($etid)) {
  2871. return error_show(1002, "未找到数据");
  2872. }
  2873. $vr = isset($this->post['order_addr']) && $this->post['order_addr'] !== "" ? $this->post['order_addr'] : "";
  2874. if ($vr == "") {
  2875. return error_show(1002, "参数order_addr不能为空");
  2876. }
  2877. Db::startTrans();
  2878. try {
  2879. $data = [
  2880. "id" => $id,
  2881. "status" => 2,
  2882. "is_del" => 0,
  2883. "updatetime" => date("Y-m-d H:i:s")
  2884. ];
  2885. $datainfo = Db::name('sale')->where(['id' => $id, 'is_del' => 0])->save($data);
  2886. if ($datainfo) {
  2887. $stx = ["order_code" => $etid['orderCode'], "status" => $etid['status'], "action_remark" => '', "action_type" => "edit"];
  2888. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], $stx, "XSQRD", 2, $data);
  2889. $order = ["order_type" => "XSQRD", "order_code" => $etid['orderCode'], "order_id" => $id, "order_status" => $data['status'], "before_status" => 0,'holder_id'=>$etid['apply_id']];
  2890. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], $order);
  2891. foreach ($vr as $value) {
  2892. $tm = [];
  2893. $tm['id'] = $value['id'];
  2894. $tm['orderCode'] = $etid['orderCode'];
  2895. $tm['post_fee'] = $value['post_fee'];
  2896. $tm['updatetime'] = date("Y-m-d H:i:s");
  2897. $dn = Db::name('order_addr')->save($tm);
  2898. if ($dn == false) {
  2899. Db::rollback();
  2900. return error_show(1002, "更新失败");
  2901. }
  2902. }
  2903. Db::commit();
  2904. return error_show(0, "更新成功");
  2905. }
  2906. Db::rollback();
  2907. return error_show(1003, "更新失败");
  2908. } catch (Exception $e) {
  2909. Db::rollback();
  2910. return error_show(1005, $e->getMessage());
  2911. }
  2912. }
  2913. // 废弃
  2914. public function out()
  2915. {
  2916. // $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  2917. $outCode = makeNo("DF");
  2918. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  2919. if ($orderCode == "") {
  2920. return error_show(1002, "参数orderCoder不能为空");
  2921. }
  2922. $der = Db::name('sale')->where(['orderCode' => $orderCode])->find();
  2923. if ($der == "") {
  2924. return error_show(1002, "未找到出库订单单号");
  2925. }
  2926. // $wsm_code = isset($this->post['wsm_code']) && $this->post['wsm_code'] !== "" ? trim($this->post['wsm_code']) : "";
  2927. // if ($wsm_code == "") {
  2928. // return error_show(1002, "参数wsm_code不能为空");
  2929. // }
  2930. // $wsm = Db::name('sale_info')->where(['wsm_code' => $wsm_code, 'orderCode' => $der['orderCode']])->find();
  2931. // if ($wsm == "") {
  2932. // return error_show(1002, "未找到仓库编码");
  2933. // }
  2934. $post_name = isset($this->post['post_name']) && $this->post['post_name'] !== "" ? trim($this->post['post_name']) : "";
  2935. if ($post_name == "") {
  2936. return error_show(1002, "物流公司不能为空");
  2937. }
  2938. $post_code = isset($this->post['post_code']) && $this->post['post_code'] !== "" ? trim($this->post['post_code']) : "";
  2939. if ($post_code == "") {
  2940. return error_show(1002, "物流单号不能为空");
  2941. }
  2942. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] !== "" ? intval($this->post['post_fee']) : "";
  2943. if ($post_fee == "") {
  2944. return error_show(1002, "物流费不能为空");
  2945. }
  2946. $send_num = isset($this->post['send_num']) && $this->post['send_num'] !== "" ? $this->post['send_num'] : "";
  2947. if ($send_num == "") {
  2948. return error_show(1002, "发货数量不能为空");
  2949. }
  2950. if ($send_num > $der['wsend_num']) {
  2951. return error_show(1002, "超出可发货数量");
  2952. }
  2953. // $apply_id = GetUserInfo($token);
  2954. // if (empty($apply_id) || $apply_id['code'] != 0) {
  2955. // return error_show(1002, "申请人数据不存在");
  2956. // }
  2957. $rm = $this->uid;
  2958. $ri = $this->uname;
  2959. $addrid = isset($this->post['addrid']) && $this->post['addrid'] !== "" ? intval($this->post['addrid']) : "";
  2960. $dr = Db::name('order_addr')->where(['id' => $addrid, 'is_del' => 0])->find();
  2961. if (empty($dr)) {
  2962. return error_show(1003, "收货人信息未找到");
  2963. }
  2964. if ($dr['orderCode'] !== $orderCode) {
  2965. return error_show(1002, "订单code不存在");
  2966. }
  2967. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !== "" ? $this->post['sendtime'] : date("Y-m-d H:i:s");
  2968. // $check_num = isset($this->post['check_num']) && $this->post['check_num'] !=="" ? $this->post['check_num'] :"";
  2969. // $error_num = isset($this->post['error_num']) && $this->post['error_num'] !=="" ? $this->post['error_num'] :"";
  2970. $status = isset($this->post['status']) && $this->post['status'] !== "" ? $this->post['status'] : "1";
  2971. //$order_type = isset($this->post['order_type']) && $this->post['order_type'] !=="" ? $this->post['order_type'] :"1";
  2972. //$dn=Db::name('sale_info')->where(['wsm_code'=>$outCode])->find();
  2973. Db::startTrans();
  2974. try {
  2975. $data = [
  2976. "wsm_code" => "",
  2977. "orderCode" => $orderCode,
  2978. "outCode" => $outCode,
  2979. "order_type" => $der['order_type'],
  2980. "apply_id" => $rm,
  2981. "apply_name" => $ri,
  2982. "addrid" => $dr['id'],
  2983. "post_name" => $post_name,
  2984. "post_code" => $post_code,
  2985. "post_fee" => $post_fee,
  2986. "sendtime" => $sendtime,
  2987. "send_num" => $send_num,
  2988. "check_num" => 0,
  2989. "error_num" => 0,
  2990. "status" => $status,
  2991. "addtime" => date("Y-m-d H:i:s"),
  2992. "updatetime" => date("Y-m-d H:i:s")
  2993. ];
  2994. $datainfo = Db::name('order_out')->insert($data, true);
  2995. if ($datainfo > 0) {
  2996. $stx = ["order_code" => $outCode, "status" => $status, "action_remark" => '', "action_type" => "create"];
  2997. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], $stx, $der['order_type'] == 1 ? "CKD" : 'ZXCKD', $status, $data);
  2998. //是否库存品,待办数据推送到的人有区别,
  2999. //库存品——>推到库管,非库存品——>推送供应商负责人
  3000. $order=[];
  3001. if($data['status'] == 1){
  3002. if($der['is_stock'] == 1){
  3003. //库存品,推给库管和库管-张凯旋
  3004. $roleid = config('app.wsm_cgder_role');
  3005. $uids = Db::name('user_role')
  3006. ->where('is_del', 0)
  3007. ->whereIn('roleid', $roleid)
  3008. ->column('uid');
  3009. $order = ["order_type" => "CKD", "order_code" => $outCode, "order_id" => $datainfo, "order_status" => $data['status'], "before_status" => 0, 'handle_user_list' => implode(',', $uids)];
  3010. }else{
  3011. //推给供应商负责人 ###todo::接口废弃
  3012. $supplier = Db::name('supplier')
  3013. ->alias('a')
  3014. ->field('a.id,a.person,a.personid')
  3015. ->leftJoin('purchease_order b', 'b.is_del=0 AND b.supplierNo=a.code')
  3016. ->leftJoin('order_num c', 'c.cgdNo=b.cgdNo')
  3017. ->where('c.orderCode', $orderCode)
  3018. ->findOrEmpty();
  3019. $order = ["order_type" => "CKD", "order_code" => $outCode, "order_id" => $datainfo, "order_status" => $data['status'], "before_status" => 0, 'holder_id' => $data['apply_id'], 'wait_id' => $supplier['personid'] ?? 0, 'wait_name' => $supplier['person'] ?? ''];
  3020. }
  3021. }
  3022. if(empty($order)) $order = ["order_type" => "CKD", "order_code" => $outCode, "order_id" => $datainfo, "order_status" => $data['status'], "before_status" => 0, 'holder_id' => $data['apply_id']];
  3023. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], $order);
  3024. if ($status == 1) {
  3025. $item = $der['status'];
  3026. $der['send_num'] += $send_num;
  3027. $der['wsend_num'] -= $send_num;//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  3028. $der['send_status'] = $der['send_num'] == 0 ? 1 : $der['wsend_num'] == 0 ? 3 : 2;
  3029. $der['status'] = $der['send_num'] == 0 ? 3 : $der['wsend_num'] == 0 ? 5 : 4;
  3030. $der['updatetime'] = date("Y-m-d H:i:s");
  3031. $si = Db::name('sale')->save($der);
  3032. if ($si == false) {
  3033. Db::rollback();
  3034. return error_show(1003, "更新失败");
  3035. }
  3036. $stx = ["order_code" => $orderCode, "status" => $item, "action_remark" => '', "action_type" => "edit"];
  3037. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], $stx, "XSQRD", $der['status'], $der);
  3038. $order = ["order_type" => "XSQRD", "order_code" => $orderCode, "order_id" => $der['id'], "order_status" => $data['status'], "before_status" => 0,'holder_id'=>$der['apply_id']];
  3039. ProcessOrder::AddProcess(['id'=>$rm,'nickname'=>$ri], $order);
  3040. }
  3041. Db::commit();
  3042. return error_show(0, "创建成功");
  3043. } else {
  3044. Db::rollback();
  3045. return error_show(1003, "创建失败");
  3046. }
  3047. } catch (Exception $e) {
  3048. Db::rollback();
  3049. return error_show(1005, $e->getMessage());
  3050. }
  3051. }
  3052. // 废弃
  3053. public function customer()
  3054. {
  3055. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  3056. if (empty($outCode)) {
  3057. return error_show(1002, "销售订单编号不能为空");
  3058. }
  3059. $codeinfo = Db::name('order_out')->where(['outCode' => $outCode])->find();
  3060. if ($codeinfo == "") {
  3061. return error_show(1003, "未找到订单数据");
  3062. }
  3063. // $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !=="" ? trim($this->post['orderCode']) :"";
  3064. // if($orderCode==""){
  3065. // return error_show(1002,"参数orderCoder不能为空");
  3066. // }
  3067. $dr = Db::name('sale')->where(['orderCode' => $codeinfo['orderCode']])->find();
  3068. if ($dr == "") {
  3069. return error_show(1002, "未找到销售订单");
  3070. }
  3071. $status = isset($this->post['status']) && $this->post['status'] !== "" ? $this->post['status'] : "";
  3072. if ($status == "") {
  3073. return error_show(10002, "状态不能为空");
  3074. }
  3075. Db::startTrans();
  3076. try {
  3077. $time = $codeinfo['status'];
  3078. $codeinfo['status'] = $status;
  3079. $codeinfo['updatetime'] = date("Y-m-d H:i:s");
  3080. $cust = Db::name('order_out')->save($codeinfo);
  3081. if ($cust) {
  3082. $stx = ["order_code" => $outCode, "status" => $time, "action_remark" => '', "action_type" => "status"];
  3083. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], $stx, "CKD", $codeinfo['status'], $codeinfo);
  3084. $order = [];
  3085. if ($status == 1) {
  3086. //推给供应商负责人#todo::接口废弃
  3087. if ($dr['is_stock'] == 0) {
  3088. $supplier = Db::name('supplier')
  3089. ->alias('a')
  3090. ->field('a.id,a.person,a.personid')
  3091. ->leftJoin('purchease_order b', 'b.is_del=0 AND b.supplierNo=a.code')
  3092. ->leftJoin('order_num c', 'c.cgdNo=b.cgdNo')
  3093. ->where('c.orderCode', $codeinfo['orderCode'])
  3094. ->findOrEmpty();
  3095. $order = ["order_type" => "CKD", "order_code" => $outCode, "order_id" => $codeinfo['id'], "order_status" => $codeinfo['status'], "before_status" => 0, 'holder_id' => $codeinfo['apply_id'], 'wait_id' => $supplier['personid'] ?? 0, 'wait_name' => $supplier['person'] ?? ''];
  3096. } else{
  3097. $roleid = config('app.wsm_cgder_role');
  3098. $uids = Db::name('user_role')
  3099. ->where('is_del', 0)
  3100. ->whereIn('roleid', $roleid)
  3101. ->column('uid');
  3102. $order = ["order_type" => "CKD", "order_code" => $outCode, "order_id" => $codeinfo['id'], "order_status" => $codeinfo['status'], "before_status" => 0, 'handle_user_list' => implode(',', $uids)];
  3103. }
  3104. }
  3105. if (empty($order)) $order = ["order_type" => "CKD", "order_code" => $outCode, "order_id" => $codeinfo['id'], "order_status" => $codeinfo['status'], "before_status" => 0, 'holder_id' => $codeinfo['apply_id']];
  3106. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], $order);
  3107. if ($status == 1) {
  3108. $stokc = Db::name("good_stock")->where(['spuCode' => $codeinfo['spuCode'], "wsm_code" => $codeinfo['wsm_code'],
  3109. "is_del" => 0])->find();
  3110. if ($stokc == false) {
  3111. Db::rollback();
  3112. return error_show(1002, "未找库存到数据");
  3113. } else {
  3114. if ($codeinfo['send_num'] > $stokc['usable_stock']) {
  3115. Db::rollback();
  3116. return error_show(1002, "超出库存数量");
  3117. }
  3118. $stokc['wait_out_stock'] += $codeinfo['send_num'];
  3119. $stokc['usable_stock'] -= $codeinfo['send_num'];
  3120. $stokc['updatetime'] = date("Y-m-d H:i:s");
  3121. }
  3122. $stoc = Db::name("good_stock")->save($stokc);
  3123. if ($stoc == false) {
  3124. Db::rollback();
  3125. return error_show(1002, "库存更新失败");
  3126. }
  3127. $item = $dr['status'];
  3128. $dr['send_num'] += $codeinfo['send_num'];
  3129. $dr['wsend_num'] -= $codeinfo['send_num'];//($der['send_num'])?$der['wsend_num'] =="" ? $der['send']
  3130. $dr['send_status'] = $dr['send_num'] == 0 ? 1 : $dr['wsend_num'] == 0 ? 3 : 2;
  3131. $dr['status'] = $dr['send_num'] == 0 ? 3 : $dr['wsend_num'] == 0 ? 5 : 4;
  3132. $dr['updatetime'] = date("Y-m-d H:i:s");
  3133. $si = Db::name('sale')->save($dr);
  3134. if ($si == false) {
  3135. Db::rollback();
  3136. return error_show(1003, "更新失败");
  3137. }
  3138. $stx = ["order_code" => $dr['orderCode'], "status" => $item, "action_remark" => '', "action_type" => "edit"];
  3139. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], $stx, $dr['order_type'] == 2 ? "ZXQRD" : "XSQRD", $dr['status'],$dr);
  3140. $order = ["order_type" => "XSQRD", "order_code" => $dr['orderCode'], "order_id" => $dr['id'], "order_status" => $dr['status'], "before_status" => 0, 'holder_id' => $dr['apply_id']];
  3141. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], $order);
  3142. //商品变动日志表,good_log_code字段存储销售订单编号
  3143. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $stoc['id'], "type" => 1, 'stock' => $codeinfo['send_num'], "stock_name" => "wait_out_stock"];
  3144. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $stoc['id'], "type" => 2, 'stock' => $codeinfo['send_num'], "stock_name" => "usable_stock"];
  3145. GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname], $good_data, 'CKD');
  3146. }
  3147. if ($status == 3) {
  3148. $var = Db::name('good_stock')->where(['wsm_code' => $codeinfo['wsm_code'], 'good_type_code' => $dr['good_code']])->find();
  3149. if ($var == false) {
  3150. Db::rollback();
  3151. return error_show(1002, "未找到商品数据");
  3152. }
  3153. if ($codeinfo['send_num'] > $var['intra_stock']) {
  3154. Db::rollback();
  3155. return error_show(1003, "超出在途库存数量");
  3156. }
  3157. $var['intra_stock'] -= $codeinfo['send_num'];
  3158. $var['updatetime'] = date("Y-m-d H:i:s");
  3159. $stre = Db::name('good_stock')->save($var);
  3160. //商品变动日志表,good_log_code字段存储销售订单编号
  3161. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $var['id'], "type" => 2, 'stock' => $codeinfo['send_num'], "stock_name" => "intra_stock"];
  3162. GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname], $good_data, $codeinfo['order_type'] == 2 ? "ZXCKD" : "CKD");
  3163. if ($stre == false) {
  3164. Db::rollback();
  3165. return error_show(1002, "状态更新失败");
  3166. }
  3167. }
  3168. Db::commit();
  3169. return error_show(0, "出库订单更新成功");
  3170. } else {
  3171. Db::rollback();
  3172. return error_show(1003, "出库订单更新失败");
  3173. }
  3174. } catch (Exception $e) {
  3175. Db::rollback();
  3176. return error_show(1005, $e->getMessage());
  3177. }
  3178. }
  3179. public function difflist()
  3180. {
  3181. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  3182. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  3183. $where = [];
  3184. $cgdNo = isset($this->post['orderCode']) && $this->post['orderCode'] != "" ? trim($this->post['orderCode']) : "";
  3185. if ($cgdNo != "") {
  3186. $where[] = ['a.orderCode', "like", "%$cgdNo%"];
  3187. }
  3188. $diffNo = isset($this->post['diffCode']) && $this->post['diffCode'] != "" ? trim($this->post['diffCode']) : "";
  3189. if ($diffNo != "") {
  3190. $where[] = ['a.diffCode', "like", "%$diffNo%"];
  3191. }
  3192. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "";
  3193. if ($status !== "") {
  3194. $where[] = ['a.status', "=", $status];
  3195. }
  3196. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  3197. if ($start != "") {
  3198. $where[] = ["a.addtime", '>=', $start];
  3199. }
  3200. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  3201. if ($end != "") {
  3202. $where[] = ["a.addtime", '<=', $end];
  3203. }
  3204. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
  3205. if ($companyNo != "") {
  3206. $where[] = ['a.companyNo', "like", "%$companyNo%"];
  3207. }
  3208. $customer_code = isset($this->post['customer_code']) && $this->post['customer_code'] !== "" ? trim($this->post['customer_code']) : "";
  3209. if ($customer_code != "") {
  3210. $where[] = ['a.customerNo', "like", "%$customer_code%"];
  3211. }
  3212. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  3213. if ($supplierNo != "") {
  3214. $where[] = ['a.supplierNo', "like", "%$supplierNo%"];
  3215. }
  3216. $relaComNo = trim($this->post['relaComNo'] ?? '');
  3217. if ($relaComNo !== "") {
  3218. $where[] = ['a.companyNo', "=", $relaComNo];
  3219. }
  3220. $order_source = isset($this->post['order_source']) && $this->post['order_source'] != "" ? trim($this->post['order_source']) : "";
  3221. if ($order_source !== "") {
  3222. $where[] = ['c.order_source', "=", $order_source];
  3223. }
  3224. $is_stock = isset($this->post['is_stock']) && $this->post['is_stock'] != "" ? trim($this->post['is_stock']) : "";
  3225. if ($is_stock !== "") {
  3226. $where[] = ['c.is_stock', "=", $is_stock];
  3227. }
  3228. // $role=$this->checkRole();
  3229. // if(!empty($role['write'])){
  3230. // $where[]=["apply_id","in",$role['write']];
  3231. // }
  3232. // $role = $this->checkDataShare();
  3233. // if (!empty($role[DataGroupModel::$type_全部])) $where[] = ['c.apply_id', 'in', $role[DataGroupModel::$type_全部]];
  3234. $role = $this->checkDataShare();
  3235. $hand = resign_hand_user($this->uid, 0);
  3236. if (!empty($role[DataGroupModel::$type_全部])) {
  3237. $arr = array_unique(array_merge($hand, $role[DataGroupModel::$type_全部]));
  3238. $where[] = ['c.apply_id', 'in', $arr];
  3239. }
  3240. // if(!empty($role['platform']) ){
  3241. // $where[]=["c.platform_id","in",$role['platform']];
  3242. // }
  3243. $count = Db::name('sale_diff')
  3244. ->alias('a')
  3245. ->join("sale c", "c.orderCode=a.orderCode", "left")
  3246. // ->join("customer_info v", "v.companyNo=c.customer_code", "left")
  3247. ->where($where)
  3248. ->count();
  3249. $total = ceil($count / $size);
  3250. $page = $page >= $total ? intval($total) : $page;
  3251. $list = Db::name('sale_diff')
  3252. ->alias('a')
  3253. ->field("a.*,c.skuCode,c.order_type,c.apply_id,c.order_source,c.is_stock")
  3254. ->join("sale c", "c.orderCode=a.orderCode", "left")
  3255. // ->join("customer_info v", "v.companyNo=c.customer_code", "left")
  3256. ->where($where)
  3257. ->page($page, $size)
  3258. ->order("a.addtime desc")
  3259. ->select()
  3260. ->toArray();
  3261. $data = [];
  3262. // $userCommon = \app\admin\common\User::getIns();
  3263. // $names = $userCommon->handle('getCodeAndName',['code'=>array_merge(array_column($list,'supplierNo'),array_column($list,'customer_code'))]);
  3264. foreach ($list as $value) {
  3265. if (in_array($value['order_type'], [3, 4])) {
  3266. $goon = Db::name("good_zixun")->where(["spuCode" => $value['good_code'], "is_del" => 0])->find();
  3267. } else {
  3268. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  3269. ->where(['a.skuCode' => $value['skuCode']])->find();
  3270. }
  3271. $value['can'] = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  3272. // $wsm = Db::name("business")->where(['companyNo' => $value['supplierNo']])->find();
  3273. // $value['supplierNo'] = $value['supplierNo'];//isset($wsm['companyNo']) ? $wsm['companyNo'] : "";
  3274. // $value['supplierName'] = $names['data'][$value['supplierNo']] ?? '';//isset($wsm['company']) ? $wsm['company'] : "";
  3275. // $value['customer_name'] = $names['data'][$value['customer_code']] ?? '';//isset($wsm['company']) ? $wsm['company'] : "";
  3276. //是否具有编辑权限
  3277. $value['is_allow_update'] = 0;
  3278. if (in_array($this->roleid, [1, 33]) || in_array($value['apply_id'], $role[DataGroupModel::$type_可编辑])) $value['is_allow_update'] = 1;
  3279. $data[] = $value;
  3280. }
  3281. return app_show(0, "获取成功", ['list' => $data, 'count' => $count]);
  3282. }
  3283. /**
  3284. * @return \think\response\Json|void
  3285. * @throws \think\db\exception\DataNotFoundException
  3286. * @throws \think\db\exception\DbException
  3287. * @throws \think\db\exception\ModelNotFoundException
  3288. */
  3289. //工差单审核
  3290. public function diffcheck()
  3291. {
  3292. // $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  3293. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  3294. if ($id === "") {
  3295. return error_show(1004, "参数id不能为空");
  3296. }
  3297. $info = Db::name("sale_diff")->where(["id" => $id])->find();
  3298. if (empty($info)) {
  3299. return error_show(1004, "订单数据未找到");
  3300. }
  3301. $sale = Db::name("sale")->where(["orderCode" => $info["orderCode"]])->find();
  3302. if ($sale == false) {
  3303. return error_show(1004, "订单数据未找到");
  3304. }
  3305. $cgd = Db::name("purchease_diff")->where(['id' => $info['cgd_diffid']])->find();
  3306. if ($cgd == false) {
  3307. return error_show(1004, "采购工差单数据未找到");
  3308. }
  3309. $is_act = isset($this->post['is_act']) && $this->post['is_act'] !== "" ? intval($this->post['is_act']) : "";
  3310. if ($is_act === '') {
  3311. return error_show(1004, "参数is_act不能为空");
  3312. }
  3313. $customer_remark = isset($this->post['customer_remark']) && $this->post['customer_remark'] !== "" ? trim($this->post['customer_remark']) : "";
  3314. if ($customer_remark === '') {
  3315. return error_show(1004, "参数customer_remark不能为空");
  3316. }
  3317. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "2";
  3318. $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
  3319. $item = $info['status'];
  3320. $info['is_act'] = $is_act;
  3321. $info['customer_remark'] = $customer_remark;
  3322. $info['status'] = $status;
  3323. $info['remark'] = $remark;
  3324. $info['updatetime'] = date("Y-m-d H:i:s");
  3325. Db::startTrans();
  3326. try {
  3327. $up = Db::name("sale_diff")->save($info);
  3328. if ($up) {
  3329. $stx = ["order_code" => $info["orderCode"], "status" => $item, "action_remark" => '', "action_type" => "edit"];
  3330. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], $stx, "XSGCD", $info['status'], $info);
  3331. $order = ["order_type" => "XSGCD", "order_code" => $info["orderCode"], "order_id" => $info['id'], "order_status" => $info['status'], "before_status" => $item,'holder_id'=>$sale['apply_id']];
  3332. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], $order);
  3333. $upda = [
  3334. "status" => $status,
  3335. "remark" => $remark,
  3336. "updatetime" => date("Y-m-d H:i:s"),
  3337. ];
  3338. $old_purchease_diff = Db::name("purchease_diff")
  3339. ->field('id,cgdNo,status')
  3340. ->where(["id" => $info['cgd_diffid']])
  3341. ->find();
  3342. $cgdup = Db::name("purchease_diff")->where(["id" => $info['cgd_diffid']])->save($upda);
  3343. if ($cgdup) {
  3344. if ($status == 2) {
  3345. if ($customer_remark == 1) {
  3346. $sale['diff_weight'] = $info['diff_weight'];
  3347. $sale['diff_fee'] = $info['diff_price'];
  3348. $sale['updatetime'] = date("Y-m-d H:i:s");
  3349. $sal = Db::name("sale")->save($sale);
  3350. if ($sal == false) {
  3351. Db::rollback();
  3352. return error_show(1003, "订单更新工差失败");
  3353. }
  3354. }
  3355. $cgdup = Db::name("purchease_order")->where(["cgdNo" => $cgd['cgdNo']])->save
  3356. (["diff_weight" => $cgd['diff_weight'], "diff_fee" => $cgd['diff_price'], "updatetime" => date("Y-m-d H:i:s")]);
  3357. if ($cgdup == false) {
  3358. Db::rollback();
  3359. return error_show(1003, "采购订单更新工差失败");
  3360. }
  3361. }
  3362. $a = ["order_code" => $old_purchease_diff["cgdNo"], "status" => $old_purchease_diff['status'], "action_remark" => '', "action_type" => "status"];
  3363. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], $a, "CGGCD", $upda['status'], $info);
  3364. $b = ["order_type" => "CGGCD", "order_code" => $old_purchease_diff["cgdNo"], "order_id" => $old_purchease_diff['id'], "order_status" => $upda['status'], "before_status" => $old_purchease_diff['status'], 'holder_id' => $cgd['apply_id']];
  3365. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], $b);
  3366. Db::commit();
  3367. return app_show(0, "更新成功");
  3368. }
  3369. }
  3370. Db::rollback();
  3371. return error_show(1003, "更新失败");
  3372. } catch (\Exception $e) {
  3373. Db::rollback();
  3374. return error_show(1003, $e->getMessage());
  3375. }
  3376. }
  3377. /**
  3378. * @return \think\response\Json|void
  3379. * @throws \think\db\exception\DataNotFoundException
  3380. * @throws \think\db\exception\DbException
  3381. * @throws \think\db\exception\ModelNotFoundException
  3382. */
  3383. public function diffstatus()
  3384. {
  3385. // $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  3386. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  3387. if ($id === "") {
  3388. return error_show(1004, "参数id不能为空");
  3389. }
  3390. $info = Db::name("sale_diff")->where(["id" => $id])->find();
  3391. if ($info == false) {
  3392. return error_show(1004, "工差单数据未找到");
  3393. }
  3394. $sale = Db::name("sale")->where(["orderCode" => $info["orderCode"]])->find();
  3395. if ($sale == false) {
  3396. return error_show(1004, "订单数据未找到");
  3397. }
  3398. $cgd = Db::name("purchease_diff")->where(['id' => $info['cgd_diffid']])->find();
  3399. if ($cgd == false) {
  3400. return error_show(1004, "采购工差单数据未找到");
  3401. }
  3402. $status = isset($this->post['status']) && $this->post['status'] !== "" ? intval($this->post['status']) : "3";
  3403. $remark = isset($this->post['remark']) && $this->post['remark'] !== "" ? trim($this->post['remark']) : "";
  3404. $ite = $info['status'];
  3405. Db::startTrans();
  3406. try {
  3407. $upda = [
  3408. "status" => $status,
  3409. "remark" => $remark,
  3410. "updatetime" => date("Y-m-d H:i:s"),
  3411. ];
  3412. $up = Db::name("sale_diff")->where($info)->save($upda);
  3413. if ($up) {
  3414. $stx = ["order_code" => $info["orderCode"], "status" => $ite, "action_remark" => '', "action_type" => "status"];
  3415. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], $stx, "XSGCD", $status, $info);
  3416. $order = ["order_type" => "XSGCD", "order_code" => $info["orderCode"], "order_id" => $info['id'],"order_status" => $status, "before_status" => $ite,'holder_id'=>$sale['apply_id']];
  3417. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], $order);
  3418. $cgdup = Db::name("purchease_diff")->where(["id" => $info['cgd_diffid']])->save($upda);
  3419. if ($cgdup) {
  3420. if ($status == 2 && $info['customer_remark'] == 1) {
  3421. $sale['diff_weight'] = $info['diff_weight'];
  3422. $sale['diff_fee'] = $info['diff_price'];
  3423. $sale['updatetime'] = date("Y-m-d H:i:s");
  3424. $sal = Db::name("sale")->save($sale);
  3425. if ($sal == false) {
  3426. Db::rollback();
  3427. return error_show(1003, "订单更新工差失败");
  3428. }
  3429. }
  3430. Db::commit();
  3431. return app_show(0, "更新成功");
  3432. }
  3433. }
  3434. Db::rollback();
  3435. return error_show(1003, "更新失败");
  3436. } catch (\Exception $e) {
  3437. Db::rollback();
  3438. return error_show(1003, $e->getMessage());
  3439. }
  3440. }
  3441. /**
  3442. * @return \think\response\Json|void
  3443. * @throws \think\db\exception\DataNotFoundException
  3444. * @throws \think\db\exception\DbException
  3445. * @throws \think\db\exception\ModelNotFoundException
  3446. */
  3447. public function diffinfo()
  3448. {
  3449. $id = isset($this->post['id']) && $this->post['id'] !== "" ? intval($this->post['id']) : "";
  3450. if ($id === "") {
  3451. return error_show(1004, "参数id不能为空");
  3452. }
  3453. $info = Db::name('sale_diff')->where(["id" => $id])->find();
  3454. if (empty($info)) {
  3455. return error_show(1004, "工差订单数据未找到");
  3456. }
  3457. $einfo = Db::name('sale')->where(['orderCode' => $info['orderCode'], 'is_del' => 0])->find();
  3458. if ($einfo == false) {
  3459. return error_show(1002, "未找到销售订单数据");
  3460. }
  3461. if (in_array($einfo['order_type'], [3, 4])) {
  3462. $goon = Db::name("good_zixun")->where(["spuCode" => $einfo['good_code'], "is_del" => 0])->find();
  3463. } else {
  3464. $goon = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  3465. ->where(['a.skuCode' => $einfo['skuCode']])->find();
  3466. }
  3467. if ($goon == false) {
  3468. return error_show(1002, "未找到商品数据");
  3469. }
  3470. $info['skuCode'] = isset($goon['skuCode']) ? $goon['skuCode'] : "";
  3471. $info['spuCode'] = isset($goon['spuCode']) ? $goon['spuCode'] : "";
  3472. $info['order_type'] = isset($einfo['order_type']) ? $einfo['order_type'] : "";
  3473. $info['can'] = isset($info['cat_id']) && $info['cat_id'] !== 0 ? made($info['cat_id']) : [];
  3474. return app_show(0, "获取成功", $info);
  3475. }
  3476. //发货申请单列表
  3477. public function saleout()
  3478. {
  3479. $page = isset($this->post['page']) && $this->post['page'] !== "" ? intval($this->post['page']) : "1";
  3480. $size = isset($this->post['size']) && $this->post['size'] !== "" ? intval($this->post['size']) : "10";
  3481. $where = [["a.is_del", "=", 0]];
  3482. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  3483. if ($orderCode !== "") {
  3484. $where[] = ['a.orderCode', "like", "%$orderCode%"];
  3485. }
  3486. $order_type = isset($this->post['order_type']) && $this->post['order_type'] !== "" ? trim($this->post['order_type']) : "";
  3487. if ($order_type !== "") {
  3488. $where[] = ['a.order_type', "=", $order_type];
  3489. }
  3490. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  3491. if ($outCode !== "") {
  3492. $where[] = ['a.outCode', "like", "%$outCode%"];
  3493. }
  3494. $apply_name = isset($this->post['apply_name']) && $this->post['apply_name'] !== "" ? trim($this->post['apply_name']) : "";
  3495. if ($apply_name !== "") {
  3496. $where[] = ['a.apply_name', "like", "%$apply_name%"];
  3497. }
  3498. $good_code = isset($this->post['good_code']) && $this->post['good_code'] !== "" ? trim($this->post['good_code']) : "";
  3499. if ($good_code !== "") {
  3500. $where[] = ['b.good_code', "like", "%$good_code%"];
  3501. }
  3502. $good_name = isset($this->post['good_name']) && $this->post['good_name'] !== "" ? trim($this->post['good_name']) : "";
  3503. if ($good_name !== "") {
  3504. $where[] = ['b.good_name', "like", "%$good_name%"];
  3505. }
  3506. $supplier_name = isset($this->post['supplier_name']) && $this->post['supplier_name'] !== "" ? trim($this->post['supplier_name']) : "";
  3507. if ($supplier_name !== "") {
  3508. $where[] = ['b.supName', "like", "%$supplier_name%"];
  3509. }
  3510. $status = isset($this->post['status']) && $this->post['status'] !== "" ? trim($this->post['status']) : "";
  3511. if ($status !== "") {
  3512. $where[] = ['a.status', "=", $status];
  3513. }
  3514. $send_status = isset($this->post['send_status']) && $this->post['send_status'] !== "" ? trim($this->post['send_status']) : "";
  3515. if ($send_status !== "") {
  3516. $where[] = ['a.send_status', "=", $send_status];
  3517. }
  3518. $start = isset($this->post['start']) && $this->post['start'] !== "" ? $this->post['start'] : "";
  3519. if ($start != "") {
  3520. $where[] = ["a.addtime", '>=', $start . " 00:00:00"];
  3521. }
  3522. $end = isset($this->post['end']) && $this->post['end'] !== "" ? $this->post['end'] : "";
  3523. if ($end != "") {
  3524. $where[] = ["a.addtime", '<=', $end . " 23:59:59"];
  3525. }
  3526. $companyNo = isset($this->post['companyNo']) && $this->post['companyNo'] !== "" ? trim($this->post['companyNo']) : "";
  3527. if ($companyNo != "") {
  3528. $where[] = ['b.supplierNo', "like", "%$companyNo%"];
  3529. }
  3530. $customer_code = isset($this->post['customerCode']) && $this->post['customerCode'] !== "" ? trim($this->post['customerCode']) : "";
  3531. if ($customer_code != "") {
  3532. $where[] = ['b.customer_code', "like", "%$customer_code%"];
  3533. }
  3534. $relaComNo = isset($this->post['relaComNo']) && $this->post['relaComNo'] !== "" ? trim($this->post['relaComNo']) : "";
  3535. if ($relaComNo != '') $where[] = ['b.supplierNo', '=', $relaComNo];
  3536. $supplierNo = isset($this->post['supplierNo']) && $this->post['supplierNo'] !== "" ? trim($this->post['supplierNo']) : "";
  3537. if ($supplierNo != '') {
  3538. //sale b 表里存的supplierNo值是GS开头的,即业务公司编码
  3539. //所以此处应该根据供应商编码筛选商品spuCode,然后筛选发货单
  3540. $spuCode = Db::name('good_basic')
  3541. ->where(['is_del' => 0, 'supplierNo' => $supplierNo])
  3542. ->column('spuCode');
  3543. $where[] = ['b.good_code', 'in', $spuCode];
  3544. }
  3545. $cgdNo = isset($this->post['cgdNo']) && $this->post['cgdNo'] !== "" ? $this->post['cgdNo'] : "";
  3546. if ($cgdNo != "") {
  3547. $where[] = ["os.cgdNo", 'like', '%' . $cgdNo . '%'];
  3548. }
  3549. $condition = '';
  3550. $level= isset($this->post['level']) && $this->post['level'] !== '' ? $this->post['level'] : '0';
  3551. //只有level2的账号过滤数据权限
  3552. if ($level=='0'&& $this->level == 2) {
  3553. // //如果是供应商负责人,根据供应商筛选
  3554. // $person_supplier = Db::connect('mysql_sys')
  3555. // ->name('supplier')
  3556. // ->where(['is_del' => 0, 'personid' =>$this->uid])
  3557. // ->column('code');
  3558. //
  3559. // if(!empty($person_supplier)) $condition = "b.supNo in ('" . implode('\',\'', $person_supplier) . "')";
  3560. // else{
  3561. //不是供应商负责人,根据数据权限筛选申请人
  3562. $role = $this->checkDataShare();
  3563. if (!empty($role[DataGroupModel::$type_全部])) $condition = " a.apply_id in (" . implode(',', $role[DataGroupModel::$type_全部]) . ")";
  3564. // }
  3565. // $hand = resign_hand_user($this->uid, 0);
  3566. // $uidarr = implode(",", $hand);
  3567. // //库管只能看到库存品订单,供应商负责人只能看到非库存品订单
  3568. // if (!in_array($this->roleid, [1, 33])) {
  3569. // //库管看到所有的库存品发货申请单
  3570. // if (in_array($this->roleid, config('app.wsm_cgder_role'))) $condition .= " b.is_stock=1";
  3571. // else {
  3572. // $role = $this->checkDataShare();
  3573. // if (!empty($role[DataGroupModel::$type_全部])) {
  3574. // $arr = array_unique(array_merge($hand, $role[DataGroupModel::$type_全部]));
  3575. // if ($condition != '') $condition .= " or ";
  3576. // $condition .= " a.apply_id in (" . implode(',', $arr) . ")";
  3577. // }
  3578. // }
  3579. //// $personid = Db::name('supplier')->field('id')->where(['is_del' => 0, 'personid' => $hand])->findOrEmpty();
  3580. // if (!empty($hand)) {
  3581. // if ($condition != '') $condition .= " or ";
  3582. //// $condition .= "(b.is_stock=0 and sip.personid in ($uidarr))";
  3583. //
  3584. // $person_supplier = Db::connect('mysql_sys')
  3585. // ->name('supplier')
  3586. // ->where(['is_del'=>0,'personid'=>$uidarr])
  3587. // ->column('code');
  3588. //
  3589. // $condition .= "(b.is_stock=0 and wpo.supplierNo in ('" . implode('\',\'', $person_supplier) . "'))";
  3590. // }
  3591. // }
  3592. }
  3593. // if(!empty($role['platform']) ){
  3594. // $where[]=["b.platform_id","in",$role['platform']];
  3595. // }
  3596. $company_name = isset($this->post['company_name']) && $this->post['company_name'] !== "" ? trim($this->post['company_name']) : "";
  3597. if ($company_name !== "") $where[] = ["a.apply_id", 'in', get_company_item_user_by_name($company_name)];
  3598. //发货时间筛选
  3599. $start_sendtime = isset($this->post['start_sendtime']) && $this->post['start_sendtime'] !== "" ? $this->post['start_sendtime'] : "";
  3600. $end_sendtime = isset($this->post['end_sendtime']) && $this->post['end_sendtime'] !== "" ? $this->post['end_sendtime'] : "";
  3601. if ($start_sendtime != "" && $end_sendtime != "") {
  3602. $where[] = ["a.sendtime", 'between', [$start_sendtime . " 00:00:00", $end_sendtime . " 23:59:59"]];
  3603. $where[] = ["a.status", '>=', 2];//搜索发货时间时,要指定状态为已发货及之后的状态值(0待发货,1待库管发货,2已发货待收货,3已收货,4已全部退货',)
  3604. }
  3605. $order_source = isset($this->post['order_source']) && $this->post['order_source'] !== "" ? intval($this->post['order_source']) : "";
  3606. if ($order_source !== "") $where[] = ["b.order_source", '=', $order_source];
  3607. $use_type = $this->post['use_type']??'';
  3608. if($use_type!=='') $where[]=['p.use_type','=',$use_type];
  3609. $count = Db::name('order_out')
  3610. ->alias('a')
  3611. ->join("sale b", "b.orderCode=a.orderCode", "left")
  3612. // ->join("customer_info v", "v.companyNo=b.customer_code", "left")
  3613. ->join("warehouse_info n", "n.wsm_code=a.wsm_code", "left")
  3614. ->leftJoin("order_send os", "os.outCode=a.outCode")
  3615. ->leftJoin("platform p", "p.id=b.platform_id")
  3616. // ->leftJoin("purchease_order wpo", "wpo.cgdNo=os.cgdNo")
  3617. // ->leftJoin('supplier sip', 'sip.code=wpo.supplierNo')
  3618. ->where($where)
  3619. ->where($condition)
  3620. ->count('a.id');
  3621. $total = ceil($count / $size);
  3622. $page = $page >= $total ? $total : $page;
  3623. $list = Db::name('order_out')
  3624. ->alias('a')
  3625. ->join("sale b", "b.orderCode=a.orderCode", "left")
  3626. // ->join("customer_info v", "v.companyNo=b.customer_code", "left")
  3627. ->join("warehouse_info n", "n.wsm_code=a.wsm_code AND n.is_del=0", "left")
  3628. ->leftJoin("order_send os", "os.outCode=a.outCode")
  3629. ->leftJoin("platform p", "p.id=b.platform_id")
  3630. // ->leftJoin("purchease_order wpo", "wpo.cgdNo=os.cgdNo")
  3631. // ->leftJoin('supplier sip', 'sip.code=wpo.supplierNo')
  3632. ->field("a.*,b.order_type,b.order_source,b.good_name,b.good_code,b.skuCode,b.customer_code,b.supplierNo companyNo,b.supplierName companyName,b.origin_price,b.sale_price,b.total_price,os.cgdNo,b.supNo supplierNo,n.supplierNo wsm_supplierNo,p.use_type")
  3633. ->where($where)
  3634. ->where($condition)
  3635. ->order("addtime desc")
  3636. ->page($page, $size)
  3637. ->select()
  3638. ->toArray();
  3639. $all_createrid = array_column($list, 'apply_id');
  3640. $item = get_company_name_by_uid($all_createrid);
  3641. $user = \app\admin\common\User::getIns();
  3642. $names = $user->handle("getCodeAndName", ["code" => array_merge( array_column($list, "supplierNo"), array_column($list, "wsm_supplierNo"))]);
  3643. // $userCommon = \app\admin\common\User::getIns();
  3644. // $names = $userCommon->handle('getCodeAndName',['code'=>array_column($list,'customer_code')]);
  3645. $has_account = checkHasAccountBySupplierNos(array_unique(array_column($list,'wsm_supplierNo')));
  3646. $data = [];
  3647. foreach ($list as $value) {
  3648. // $value['companyName'] = $names['data'][$value['customer_code']] ?? '';
  3649. if ($value['order_type'] == 1 && $value['wsm_code'] == '') {
  3650. // $wsmcode = Db::name("good")->alias("a")
  3651. // ->leftJoin("supplier b","a.supplierNo=b.code")
  3652. // ->where(["a.spuCode"=>$value['good_code']])->field("'' wsm_name,b.name,b.code")->find();
  3653. $value['wsm_supplierNo'] = $value['supplierNo'];
  3654. $value['wsm_supplier'] = $names['data'][$value['supplierNo']] ?? "";
  3655. } else {
  3656. // $wsmcode = Db::name("warehouse_info")->alias("k")->leftJoin("supplier c", "k.supplierNo=c.code")
  3657. // ->where(["k.wsm_code" => $value['wsm_code']])->field("k.name as wsm_name,c.name,c.code")->find();
  3658. $value['wsm_supplier'] = $names['data'][$value['wsm_supplierNo']] ?? "";
  3659. }
  3660. $addr = Db::name("order_addr")->where(["id" => $value['addrid']])->find();
  3661. if ($addr) {
  3662. $addinfo = $addr['addr_code'] != '' ? json_decode($addr['addr_code'], true) ?? $addr['addr_code'] : '';
  3663. if (is_string($addinfo) && $addinfo != '') {
  3664. $addinfo = ["provice_code" => '', "city_code" => '', "area_code" => ''];
  3665. list($addinfo['provice_code'], $addinfo['city_code'], $addinfo['area_code']) = explode(",", $addr['addr_code']);
  3666. }
  3667. $addr['addr_cn'] = GetAddr(json_encode($addinfo));
  3668. }
  3669. if (in_array($value['order_type'], [3, 4])) {
  3670. $goon = Db::name("good_zixun")->field('id,cat_id')->where(["spuCode" => $value['good_code'], "is_del" => 0])->find();
  3671. } else {
  3672. $goon = Db::name('good_platform')->field('a.id,b.cat_id')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')->where(['a.skuCode' => $value['skuCode']])->find();
  3673. }
  3674. $value['wsm_has_account'] = (int)isset($has_account[$value['wsm_supplierNo']]);
  3675. $value['addr'] = isset($addr['addr']) ? $addr['addr_cn'] . $addr['addr'] : "";
  3676. $value['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : "";
  3677. $value['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : "";
  3678. $value['can'] = isset($goon['cat_id']) && $goon['cat_id'] != 0 ? made($goon['cat_id']) : [];
  3679. $value['company_name'] = $item[$value['apply_id']] ?? '';
  3680. $value['sendtime'] = $value['status'] < 2 ? '' : $value['sendtime'];
  3681. //是否具有编辑权限
  3682. // $value['is_allow_update'] = 0;
  3683. // if (in_array($this->roleid, [1, 33]) || in_array($value['apply_id'], $role[DataGroupModel::$type_可编辑])) $value['is_allow_update'] = 1;
  3684. $data[] = $value;
  3685. }
  3686. return app_show(0, "获取成功", ['list' => $data, 'count' => $count]);
  3687. }
  3688. public function outinfo()
  3689. {
  3690. $outCode = isset($this->post['outCode']) && $this->post['outCode'] !== "" ? trim($this->post['outCode']) : "";
  3691. if ($outCode == "") {
  3692. return error_show(1002, "参数outcode不能为空");
  3693. }
  3694. $codeinfo = Db::name("order_out")->where(['outCode' => $outCode, "is_del" => 0])->find();
  3695. if (empty($codeinfo)) {
  3696. return error_show(1002, "未找到出库数据");
  3697. }
  3698. $item = Db::name("sale")->where(['orderCode' => $codeinfo['orderCode']])->find();
  3699. if ($item['order_type'] == 3 ||$item['order_type'] == 4 ) {
  3700. $goodinfo = Db::name("good_zixun")->where(["spuCode" => $item['good_code'], "is_del" => 0])->find();
  3701. if ($goodinfo == false) {
  3702. return error_show(1004, "未找到商品数据");
  3703. }
  3704. } else {
  3705. $goodinfo = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  3706. ->where(['a.skuCode' => $item['skuCode']])->find();
  3707. if ($goodinfo == false) {
  3708. return error_show(1002, "未找到商品数据");
  3709. }
  3710. }
  3711. $int = isset($goodinfo['cat_id']) && $goodinfo['cat_id'] != 0 ? made($goodinfo['cat_id']) : [];
  3712. $addr = Db::name("order_addr")->where(["id" => $codeinfo['addrid']])->find();
  3713. if($addr){
  3714. $addinfo =$addr['addr_code']!=''?json_decode($addr['addr_code'],true)??$addr['addr_code']: '' ;
  3715. if(is_string($addinfo)&&$addinfo!=''){
  3716. $addinfo=["provice_code"=>'',"city_code"=>'',"area_code"=>''];
  3717. list($addinfo['provice_code'],$addinfo['city_code'],$addinfo['area_code']) = explode(",",$addr['addr_code']);
  3718. }
  3719. $addr['addr_cn'] =GetAddr(json_encode($addinfo));
  3720. }
  3721. $codeinfo['good_name'] = isset($item['good_name']) ? $item['good_name'] : "";
  3722. $codeinfo['good_num'] = isset($item['good_num']) ? $item['good_num'] : "";
  3723. $codeinfo['good_code'] = isset($item['good_code']) ? $item['good_code'] : "";
  3724. $codeinfo['origin_price'] = isset($item['origin_price']) ? $item['origin_price'] : "";
  3725. $codeinfo['sale_price'] = isset($item['sale_price']) ? $item['sale_price'] : "";
  3726. $codeinfo['total_price'] = isset($item['total_price']) ? $item['total_price'] : "";
  3727. $codeinfo['skuCode'] = isset($item['skuCode']) ? $item['skuCode'] : "";
  3728. $codeinfo['addr'] = $addr['addr_cn']??'';
  3729. $codeinfo['addr'] .= isset($addr['addr']) ? $addr['addr'] : "";
  3730. $codeinfo['addr_code'] = isset($addr['addr_code']) ? $addr['addr_code'] : "";
  3731. $codeinfo['contactor'] = isset($addr['contactor']) ? $addr['contactor'] : "";
  3732. $codeinfo['mobile'] = isset($addr['mobile']) ? $addr['mobile'] : "";
  3733. $codeinfo['sendtime'] = $codeinfo['status'] < 2 ? '' : $codeinfo['sendtime'];
  3734. $codeinfo['can'] = $int;
  3735. $codeinfo['order_source'] = $item['order_source'];
  3736. $orderReturn = Db::name("order_return")->where(["outCode" => $outCode, "is_del" => 0])->order("id desc")->find();
  3737. $codeinfo['order_return'] = $orderReturn;
  3738. $codeinfo['platform_order'] = $item['platform_order'];
  3739. //获取销售订单对应的供应商是否有启用账号
  3740. $codeinfo['has_account'] = 0;
  3741. $supplierNo = Db::name('order_num')
  3742. ->alias('a')
  3743. ->leftJoin('purchease_order b', 'b.cgdNo=a.cgdNo')
  3744. ->where('a.orderCode', $codeinfo['orderCode'])
  3745. ->value('b.supplierNo');
  3746. if ($supplierNo) {
  3747. $has_account = checkHasAccountBySupplierNos([$supplierNo]);
  3748. $codeinfo['has_account'] = (int)isset($has_account[$supplierNo]);
  3749. }
  3750. $saleinfo=Db::name("sale_info")->where([["orderCode","=",$codeinfo['orderCode']],["num",">",0]])->select()
  3751. ->toArray();
  3752. if(!empty($saleinfo)){
  3753. $stockidArr=array_column($saleinfo,"stockid");
  3754. $wsm=Db::name("good_stock")->alias("a")
  3755. ->leftJoin("warehouse_info b","a.wsm_code=b.wsm_code")
  3756. ->where("a.id","in",$stockidArr)
  3757. ->column("a.wsm_code,name","a.id");
  3758. foreach ($saleinfo as &$item){
  3759. $item['wsm_code'] = $wsm[$item['stockid']]['wsm_code'];
  3760. $item['wsm_name'] = $wsm[$item['stockid']]['name'];
  3761. }
  3762. }
  3763. $codeinfo["saleinfo"] = $saleinfo;
  3764. return app_show(0, "获取成功", $codeinfo);
  3765. }
  3766. public function outadd()
  3767. {
  3768. // $token = isset($this->post['token']) && $this->post['token'] !== "" ? trim($this->post['token']) : "";
  3769. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] !== "" ? trim($this->post['orderCode']) : "";
  3770. if ($orderCode == "") {
  3771. return error_show(1002, "参数orderCoder不能为空");
  3772. }
  3773. $retrun =Db::name("sale_return")->where([["orderCode","=",$orderCode],["is_del","=",0],["status","in",[1,2,3,
  3774. 7,9,10,8,11,12]]])->count();
  3775. if($retrun>0){
  3776. return error_show(1005,"存在未完成退货订单数据");
  3777. }
  3778. $der = Db::name('sale')->where(['orderCode' => $orderCode, "is_del" => 0])->find();
  3779. if ($der == "") {
  3780. return error_show(1002, "未找到出库订单单号");
  3781. }
  3782. $send_num = isset($this->post['send_num']) && $this->post['send_num'] !== "" ? $this->post['send_num'] : "";
  3783. if ($send_num == "") {
  3784. return error_show(1002, "发货数量不能为空");
  3785. }
  3786. $wsed = Db::name("order_out")->where(["orderCode" => $orderCode])->where([["status", "<", 2], ["is_del", "=", 0]])->sum
  3787. ('send_num');
  3788. if ($send_num > $der['wsend_num'] - $wsed) {
  3789. return error_show(1002, "超出可发货数量");
  3790. }
  3791. $contactor = isset($this->post['contactor']) && $this->post['contactor'] != "" ? trim($this->post['contactor']) : "";
  3792. if ($contactor == "") {
  3793. return error_show(1002, "参数contactor不能为空");
  3794. }
  3795. $mobile = isset($this->post['mobile']) && $this->post['mobile'] != "" ? trim($this->post['mobile']) : "";
  3796. if ($mobile == "") {
  3797. return error_show(1002, "参数mobile不能为空");
  3798. }
  3799. $addr = isset($this->post['addr']) && $this->post['addr'] != "" ? trim($this->post['addr']) : "";
  3800. if ($addr == "") {
  3801. return error_show(1002, "参数addr不能为空");
  3802. }
  3803. $addr_code = isset($this->post['addr_code']) && $this->post['addr_code'] != "" ? trim($this->post['addr_code']) : "";
  3804. // if ($addr_code == "") {
  3805. // return error_show(1002, "参数addr_code不能为空");
  3806. // }
  3807. // $apply_id = GetUserInfo($token);
  3808. // if (empty($apply_id) || $apply_id['code'] != 0) {
  3809. // return error_show(1002, "申请人数据不存在");
  3810. // }
  3811. $rm = $this->uid;//isset($apply_id["data"]['id']) ? $apply_id["data"]['id'] : "";
  3812. $ri = $this->uname;//isset($apply_id["data"]['nickname']) ? $apply_id["data"]['nickname'] : "";
  3813. $sendtime = isset($this->post['sendtime']) && $this->post['sendtime'] !== "" ? $this->post['sendtime'] : date("Y-m-d H:i:s");
  3814. Db::startTrans();
  3815. try {
  3816. $addrlst = [
  3817. "orderCode" => $orderCode,
  3818. "addr" => $addr,
  3819. "addr_code" => $addr_code!=''?json_encode($addr_code):'',
  3820. "contactor" => $contactor,
  3821. "mobile" => $mobile,
  3822. "customer_code" => $der['customer_code'],
  3823. "post_fee" => $der['post_fee'],
  3824. "arrive_time" => $sendtime,
  3825. "receipt_quantity" => $send_num,
  3826. "is_del" => 0,
  3827. "addtime" => date("Y-m-d H:i:s"),
  3828. "updatetime" => date("Y-m-d H:i:s")
  3829. ];
  3830. $addrid = Db::name("order_addr")->insert($addrlst, true);
  3831. if ($addrid > 0) {
  3832. $outCode = makeNo("DF");
  3833. $order=[];
  3834. if($der['order_type']!=1){
  3835. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  3836. ->lock(true)->find();
  3837. if ($order == false) {
  3838. Db::rollback();
  3839. return error_show(1004, "未找到可以发货得采购单数据");
  3840. }
  3841. $cgd = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  3842. if ($cgd == false) {
  3843. Db::rollback();
  3844. return error_show(1002, "未找到对应的采购单数据");
  3845. }
  3846. $order['wsend_num'] -= $send_num;
  3847. $order['send_num'] += $send_num;
  3848. $or = Db::name("order_num")->save($order);
  3849. if ($or == false) {
  3850. Db::rollback();
  3851. return error_show(1002, "发货地址添加创建失败");
  3852. }
  3853. $tep = [
  3854. "cgdNo" => $order['cgdNo'],
  3855. "outCode" => $outCode,
  3856. "send_num" => $send_num,
  3857. "status" => 1,
  3858. "addtime" => date("Y-m-d H:i:s"),
  3859. "updatetime" => date("Y-m-d H:i:s")
  3860. ];
  3861. $sen = Db::name("order_send")->save($tep);
  3862. if ($sen == false) {
  3863. Db::rollback();
  3864. return error_show(1002, "发货地址添加创建失败");
  3865. }
  3866. }else{
  3867. $order = Db::name("order_num")->where(["orderCode" => $orderCode, "status" => 1])->where([["wsend_num", ">=", 0]])
  3868. ->lock(true)->find();
  3869. if ($order!= false) {
  3870. $cgd = Db::name("purchease_order")->where(["cgdNo" => $order['cgdNo']])->find();
  3871. if ($cgd == false) {
  3872. Db::rollback();
  3873. return error_show(1002, "未找到对应的采购单数据");
  3874. }
  3875. $order['wsend_num'] -= $send_num;
  3876. $order['send_num'] += $send_num;
  3877. $or = Db::name("order_num")->save($order);
  3878. if ($or == false) {
  3879. Db::rollback();
  3880. return error_show(1002, "发货地址添加创建失败");
  3881. }
  3882. $tep = [
  3883. "cgdNo" => $order['cgdNo'],
  3884. "outCode" => $outCode,
  3885. "send_num" => $send_num,
  3886. "status" => 1,
  3887. "addtime" => date("Y-m-d H:i:s"),
  3888. "updatetime" => date("Y-m-d H:i:s")
  3889. ];
  3890. $sen = Db::name("order_send")->save($tep);
  3891. if ($sen == false) {
  3892. Db::rollback();
  3893. return error_show(1002, "发货地址添加创建失败");
  3894. }
  3895. }else{
  3896. $order['cgdNo']='';
  3897. $cgd=[];
  3898. }
  3899. }
  3900. $data = [
  3901. "wsm_code" => isset($cgd['wsm_code']) ? $cgd['wsm_code'] : "",
  3902. "orderCode" => $orderCode,
  3903. "outCode" => $outCode,
  3904. "order_type" => $der['order_type'],
  3905. "apply_id" => $rm,
  3906. "apply_name" => $ri,
  3907. "addrid" => $addrid,
  3908. "post_name" => "",
  3909. "post_code" => "",
  3910. "post_fee" => "",
  3911. "sendtime" => $sendtime,
  3912. "send_num" => $send_num,
  3913. "check_num" => 0,
  3914. "error_num" => 0,
  3915. "send_status" => 1,
  3916. "status" => 1,
  3917. "addtime" => date("Y-m-d H:i:s"),
  3918. "updatetime" => date("Y-m-d H:i:s")
  3919. ];
  3920. if ($der['is_stock'] == 0) {
  3921. //维护商品所在仓库的库存 --- start
  3922. $temp = Db::name('good_stock')
  3923. ->field('id,usable_stock,wait_out_stock')
  3924. ->where(['spuCode' => $der['good_code'], 'wsm_code' => $cgd['wsm_code']])
  3925. ->find();
  3926. if (($temp == false || ($temp['usable_stock'] - $send_num) < 0) && $der['send_type'] == 1) {
  3927. Db::rollback();
  3928. return error_show(1004, '库存不足');
  3929. } else {
  3930. if (($temp == false || ($temp['usable_stock'] - $send_num) < 0) && $der['send_type'] == 2) {
  3931. $data['status'] = 0;
  3932. } else {
  3933. //这个地方不需要更新,因为后面尝试拆单的时候会维护库存数
  3934. // $up = Db::name('good_stock')
  3935. // ->where('id', $temp['id'])
  3936. // ->update([
  3937. // 'usable_stock' => $temp['usable_stock'] - $send_num,
  3938. // 'wait_out_stock' => $temp['wait_out_stock'] + $send_num,
  3939. // 'updatetime' => date('Y-m-d H:i:s'),
  3940. // ]);
  3941. // if ($up == false) {
  3942. // Db::rollback();
  3943. // return error_show(1004, '库存更新失败');
  3944. // }
  3945. // ::todo
  3946. // $good_data[] = ['good_log_code' => $outCode, "stock_id" => $temp['id'], "type" => 1, 'stock' => $send_num, "stock_name" => "wait_out_stock"];
  3947. // $good_data[] = ['good_log_code' => $outCode, "stock_id" => $temp['id'], "type" => 2, 'stock' => $send_num, "stock_name" => "usable_stock"];
  3948. // GoodLog::LogAdd(['id'=>$this->uid,'nickname'=>$this->uname], $good_data, "CKD");
  3949. }
  3950. }
  3951. }
  3952. $datainfo = Db::name('order_out')->insert($data, true);
  3953. if ($datainfo > 0) {
  3954. //修改状态,添加待办
  3955. ActionLog::logAdd(['id'=>$rm,'nickname'=>$ri], [
  3956. "order_code" => $data['outCode'],//出库单号
  3957. "status" => $data['status'],//这里的status是之前的值
  3958. "action_remark" => '',//备注
  3959. "action_type" => "create"//新建create,编辑edit,更改状态status
  3960. ], "CKD", $data['status'], $data);
  3961. //是否库存品,待办数据推送到的人有区别,
  3962. $process = [];
  3963. if ($data['status'] == 1) {
  3964. if ($data['send_status'] == 1) OrderOutChild::makeChild($outCode);//尝试拆单
  3965. if ($der['is_stock'] == 1) {
  3966. //库存品,推给库管和库管-张凯旋
  3967. $roleid = config('app.wsm_cgder_role');
  3968. $uids = Db::name('user_role')
  3969. ->where('is_del', 0)
  3970. ->whereIn('roleid', $roleid)
  3971. ->column('uid');
  3972. $process = ["order_type" => 'CKD', "order_code" => $data['outCode'], "order_id" => $datainfo, "order_status" => $data['status'], "before_status" => 0, 'holder_id' => $data['apply_id'], 'handle_user_list' => implode(',', $uids)];
  3973. } else{
  3974. //推给供应商负责人
  3975. // $supplier = Db::name('supplier')
  3976. // ->field('person,personid')
  3977. // ->where('code', $cgd['supplierNo'])
  3978. // ->findOrEmpty();
  3979. $userCommon= \app\admin\common\User::getIns();
  3980. $supplier_temp =$userCommon->handle("sInfo",["code"=>$cgd['supplierNo']]);
  3981. if($supplier_temp['code']!=0 || empty($supplier_temp['data']) ) return error_show(1002,"采购单供应商不存在");
  3982. $holder_id=$supplier_temp['data']['personid']??0;
  3983. $holder_name=$supplier_temp['data']['personid']??0;
  3984. $process = ["order_type" => 'CKD', "order_code" => $data['outCode'], "order_id" =>
  3985. $datainfo, "order_status" => $data['status'], "before_status" => 0, 'holder_id' => $data['apply_id'], 'wait_id' => $holder_id, 'wait_name' => $holder_name,];
  3986. }
  3987. }
  3988. if (empty($process)) $process = ["order_type" => 'CKD', "order_code" => $data['outCode'], "order_id" => $datainfo, "order_status" => $data['status'], "before_status" => 0, 'holder_id' => $data['apply_id']];
  3989. ProcessOrder::AddProcess(['id' => $rm, 'nickname' => $ri], $process);
  3990. //维护台账记录
  3991. Db::name('standing_book')
  3992. ->where('orderCode', $orderCode)
  3993. ->update([
  3994. 'order_type' => $der['order_type'],
  3995. 'order_source' => $der['order_source'],
  3996. 'cgdNo' => $order['cgdNo'],
  3997. 'customer_code' => $der['customer_code'],
  3998. 'updatetime' => date('Y-m-d H:i:s'),
  3999. ]);
  4000. Db::execute("UPDATE `wsm_standing_book` SET `outCode`=CONCAT(IFNULL(`outCode`,''),',{$outCode}') WHERE `cgdNo`='{$order['cgdNo']}'");
  4001. Db::commit();
  4002. return app_show(0, "出库单新建成功");
  4003. }
  4004. }
  4005. Db::rollback();
  4006. return error_show(1004, "出库单新建失败");
  4007. } catch (Exception $e) {
  4008. Db::rollback();
  4009. return error_show(1005, $e->getMessage());
  4010. }
  4011. }
  4012. //库管发货
  4013. public function outSend()
  4014. {
  4015. $outCode = isset($this->post['outCode']) && $this->post['outCode'] != "" ? trim($this->post['outCode']) : "";
  4016. if ($outCode == "") {
  4017. return error_show(1004, "参数outCode不能为空");
  4018. }
  4019. $outinfo = Db::name("order_out")->where(["outCode" => $outCode, "is_del" => 0])->find();
  4020. if ($outinfo == false) {
  4021. return error_show(1004, "发货数据未找到");
  4022. }
  4023. if($outinfo['status']!=1){
  4024. return error_show(1004, "发货状态有误");
  4025. }
  4026. $einfo = Db::name('sale')->where(['orderCode' => $outinfo['orderCode'], 'is_del' => 0])->find();
  4027. if (empty($einfo)) {
  4028. return error_show(1002, "未找到销售订单数据");
  4029. }
  4030. //判断供应商是否开通账号,若开通的话,则只能由供应商账号操作
  4031. if ($this->level != 3) {
  4032. $temp = checkHasAccountBySupplierNos([$einfo['supNo']]);
  4033. if (isset($temp[$einfo['supNo']])) return json_show(1004, '非供应商账号不能操作');
  4034. }
  4035. $is_reurn = Db::name("sale_return")->where(['orderCode' => $outinfo['orderCode'], 'is_del' => 0])->where("status", "in", [1,2,3,7,9,10,8,11,12])->find();
  4036. if ($is_reurn != false) {
  4037. return error_show(1002, "销售单存在退货未处理完成");
  4038. }
  4039. $post_name = isset($this->post['post_name']) && $this->post['post_name'] != "" ? trim($this->post['post_name']) : "";
  4040. if ($post_name == "") {
  4041. return error_show(1004, "参数post_name不能为空");
  4042. }
  4043. $post_code = isset($this->post['post_code']) && $this->post['post_code'] != "" ? trim($this->post['post_code']) : "";
  4044. if ($post_code == "") {
  4045. return error_show(1004, "参数post_code不能为空");
  4046. }
  4047. $remark = isset($this->post['remark']) && $this->post['remark'] != "" ? trim($this->post['remark']) : "";
  4048. $post_fee = isset($this->post['post_fee']) && $this->post['post_fee'] != "" ? floatval($this->post['post_fee']) : "";
  4049. $outinfo['post_name'] = $post_name;
  4050. $outinfo['post_code'] = $post_code;
  4051. $outinfo['post_fee'] = $post_fee;
  4052. $outinfo['sendtime'] = date("Y-m-d H:i:s");
  4053. $old_outinfo_status = $outinfo['status'];
  4054. $outinfo['status'] = 2;
  4055. $outinfo['updatetime'] = date("Y-m-d H:i:s");
  4056. $outinfo['remark'] = $remark;
  4057. if ($einfo['wsend_num'] < $outinfo['send_num']) {
  4058. return error_show(1004, "订单未发货数量不足");
  4059. }
  4060. Db::startTrans();
  4061. try {
  4062. $up = Db::name("order_out")->where(["outCode"=>$outCode,"status"=>1,"is_del"=>0])->update($outinfo);
  4063. // $user_info = GetUserInfo($this->post['token']);
  4064. $uid = $this->uid;//isset($user_info['data']['id']) ? $user_info['data']['id'] : 0;
  4065. $uname = $this->uname;//isset($user_info['data']['nickname']) ? $user_info['data']['nickname'] : '';
  4066. if ($einfo['is_stock'] == 1) {
  4067. //由于存在一个单子对应多个仓库的情况,因此库存品的判断暂时无法进行,待分仓功能完成后再考虑
  4068. //如果是库存品,只能由库管操作
  4069. // $contactor = Db::name('warehouse_info')
  4070. // ->where(['wsm_code' => $outinfo['wsm_code'], 'is_del' => 0])
  4071. // ->value('contactor');
  4072. // $own_roleid = Db::name('user_role')->where(['is_del'=>0,'uid'=>$uid,'status'=>1])->value('roleid',0);
  4073. // if ($uid != $contactor) throw new \think\Exception('库存品只能由库管人员操作');
  4074. } elseif ($einfo['is_stock'] == 0) {
  4075. //非库存品和采返商品只能由供应商负责人操作
  4076. if ($einfo['order_type'] == 3 || $einfo['order_type'] ==4 ) {
  4077. $supplierNo = Db::name('good_zixun')
  4078. // ->leftJoin("supplier b", "a.supplierNo=b.code")
  4079. ->where(['spuCode' => $einfo['good_code'], 'is_del' => 0])
  4080. ->value('supplierNo');
  4081. } else {
  4082. $supplierNo = Db::name('good')
  4083. // ->alias("a")
  4084. // ->leftJoin("supplier b", "a.supplierNo=b.code")
  4085. ->where(['spuCode' => $einfo['good_code'], 'is_del' => 0])
  4086. ->value('supplierNo');
  4087. }
  4088. // $userCommon = \app\admin\common\User::getIns();
  4089. // $temp = $userCommon->handle('sInfo', ['code' => $supplierNo]);
  4090. $temp = get_personid_by_supplierNo([$supplierNo]);
  4091. if (($this->level == 2) && ($uid != $temp[$supplierNo])) throw new \think\Exception('非库存品和采返商品只能由供应商负责人操作');
  4092. }
  4093. if ($up) {
  4094. //修改状态,添加待办
  4095. ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], [
  4096. "order_code" => $outinfo['outCode'],//出库单号
  4097. "status" => $old_outinfo_status,//这里的status是之前的值
  4098. "action_remark" => '',//备注
  4099. "action_type" => "edit"//新建create,编辑edit,更改状态status
  4100. ], "CKD", $outinfo['status'], $outinfo);
  4101. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], [
  4102. "order_type" => 'CKD',
  4103. "order_code" => $outinfo['outCode'],//出库单号
  4104. "order_id" => $outinfo['id'],
  4105. "order_status" => $outinfo['status'],
  4106. "before_status" => $old_outinfo_status,
  4107. 'wait_id'=>$outinfo['apply_id'],
  4108. 'wait_name'=>$outinfo['apply_name'],
  4109. 'holder_id'=>$outinfo['apply_id'],
  4110. ]);
  4111. if($einfo['wsend_num']<$outinfo['send_num']){
  4112. Db::rollback();
  4113. return error_show(1002, "订单待付货数量不足");
  4114. }
  4115. $orderstatus = $einfo['status'];
  4116. $einfo['send_num'] += $outinfo['send_num'];
  4117. $einfo['wsend_num'] -= $outinfo['send_num'];
  4118. $einfo['status'] = $einfo['wsend_num'] == 0 ? 2 : ($einfo['send_num'] == 0 ? 0 : 1);
  4119. $einfo['send_status'] = $einfo['wsend_num'] == 0 ? 3 : ($einfo['send_num'] == 0 ? 1 : 2);
  4120. $einfo['updatetime'] = date("Y-m-d H:i:s");
  4121. $saleup = Db::name("sale")->save($einfo);
  4122. if ($saleup == false) {
  4123. Db::rollback();
  4124. return error_show(1002, "销售单状态更新失败");
  4125. }
  4126. // }
  4127. //修改状态,添加待办
  4128. ActionLog::logAdd(['id' => $uid, 'nickname' => $uname], [
  4129. "order_code" => $einfo['orderCode'],//出库单号
  4130. "status" => $orderstatus,//这里的status是之前的值
  4131. "action_remark" => '',//备注
  4132. "action_type" => "edit"//新建create,编辑edit,更改状态status
  4133. ], "XSQRD", $einfo['status'], $einfo);
  4134. ProcessOrder::AddProcess(['id' => $uid, 'nickname' => $uname], [
  4135. "order_type" => 'XSQRD',
  4136. "order_code" => $einfo['orderCode'],//出库单号
  4137. "order_id" => $einfo['id'],
  4138. "order_status" => $einfo['status'],
  4139. "before_status" => $orderstatus,
  4140. 'holder_id' => $einfo['apply_id']
  4141. ]);
  4142. if($einfo["order_type"]!=1 ){
  4143. $stokc = Db::name("good_stock")->where(['spuCode' => $einfo['good_code'], "wsm_code" => $outinfo['wsm_code'], "is_del" => 0])->find();
  4144. if ($stokc == false) {
  4145. Db::rollback();
  4146. return error_show(1002, "未找到库存数据");
  4147. } else {
  4148. if ($outinfo['send_num'] > $stokc['wait_out_stock']) {
  4149. Db::rollback();
  4150. return error_show(1002, "超出库存数量");
  4151. }
  4152. $stokc['wait_out_stock'] -= $outinfo['send_num'];
  4153. $stokc['total_stock'] = $stokc['wait_out_stock'] + $stokc['usable_stock'];
  4154. // $stokc['intra_stock']+=$outinfo['send_num'];
  4155. $stokc['updatetime'] = date("Y-m-d H:i:s");
  4156. }
  4157. $stoc = Db::name("good_stock")->save($stokc);
  4158. if ($stoc == false) {
  4159. Db::rollback();
  4160. return error_show(1002, "库存更新失败");
  4161. }
  4162. // $outsend =Db::name("order_send")->where(["outCode"=> $outCode])->findOrEmpty();
  4163. // if(empty($outsend)){
  4164. // Db::rollback();
  4165. // return error_show(1002, "未找到关联采购单");
  4166. // }
  4167. // $cgdinfo= Db::name("purchease_order")->where(["cgdNo"=>$outsend['cgdNo'],"is_del"=>0])
  4168. // ->findOrEmpty();
  4169. // $bn_code =makeNo("BN");
  4170. // $bnin=GoodStockInfo::AddBn($stokc["id"],$bn_code,$outinfo['send_num'],$cgdinfo['good_price']??0);
  4171. // if($bnin==false){
  4172. // Db::rollback();
  4173. // return error_show(1004,"库存bn数据新增失败");
  4174. // }
  4175. // $bnup =GoodStockInfo::OrderBn($outinfo['orderCode'],$stokc["id"],$outinfo['send_num'],0);
  4176. // if($bnup==false){
  4177. // Db::rollback();
  4178. // return error_show(1004,"库存bn库存数更新失败");
  4179. // }
  4180. // $outsend['bnCode']=$bn_code;
  4181. // $outsend['updatetime']=date("Y-m-d H:i:s");
  4182. // $ps=Db::name("order_send")->save($outsend);
  4183. // if($ps==false){
  4184. // Db::rollback();
  4185. // return error_show(1004,"库存bn库存数更新失败");
  4186. // }
  4187. $good_data[] = ['good_log_code' => $outCode, "stock_id" => $stokc['id'], "type" => 2, 'stock' => $outinfo['send_num'], "stock_name" => "wait_out_stock"];
  4188. GoodLog::LogAdd(['id' => $uid, 'nickname' => $uname], $good_data, "CKD");
  4189. }else{
  4190. // $saleinfo =Db::name("sale_info")->where(["orderCode"=>$outinfo['orderCode']])->select()->toArray();
  4191. // if (!empty($saleinfo)){
  4192. // $bnup =SaleInfo::SendStock($outinfo['orderCode'],$outinfo['send_num']);
  4193. // if($bnup==false){
  4194. // Db::rollback();
  4195. // return error_show(1004,"库存bn库存数更新失败");
  4196. // }
  4197. // }else{
  4198. $stokc = Db::name("good_stock")->where(['spuCode' => $einfo['good_code'], "wsm_code" => $outinfo['wsm_code'], "is_del" => 0])->find();
  4199. if ($stokc == false) {
  4200. Db::rollback();
  4201. return error_show(1002, "未找到库存数据");
  4202. } else {
  4203. if ($outinfo['send_num'] > $stokc['wait_out_stock']) {
  4204. Db::rollback();
  4205. return error_show(1002, "超出库存数量");
  4206. }
  4207. $stokc['wait_out_stock'] -= $outinfo['send_num'];
  4208. $stokc['total_stock'] = $stokc['wait_out_stock'] + $stokc['usable_stock'];
  4209. // $stokc['intra_stock']+=$outinfo['send_num'];
  4210. $stokc['updatetime'] = date("Y-m-d H:i:s");
  4211. }
  4212. $stoc = Db::name("good_stock")->save($stokc);
  4213. if ($stoc == false) {
  4214. Db::rollback();
  4215. return error_show(1002, "库存更新失败");
  4216. }
  4217. // }
  4218. }
  4219. // Cache::store("redis")->handler()->lPush("SENDOUT", $outCode);
  4220. Db::commit();
  4221. } else {
  4222. Db::rollback();
  4223. return error_show(1004, "发货失败");
  4224. }
  4225. //如果是有赞订单的话,将发货信息推到有赞
  4226. //有赞信息有可能推送失败(比如超过72小时,不允许多次修改等),所以不应该和这里的事务放到一起
  4227. if ($einfo['order_source'] == 5) {
  4228. $res = curl_request(config('app.yz_domain') . 'api/yz_out_send', ['orderCode' => $einfo['orderCode'], 'out_stype' => $post_name, 'post_code' => $post_code, 'uid' => $uid, 'uname' => $uname, 'order_out' => $outCode]);
  4229. $res = json_decode($res, true);
  4230. if ($res['code'] != 0) return app_show(0, '发货成功,' . $res['message']);
  4231. }
  4232. return app_show(0, "发货成功");
  4233. } catch (\Exception $e) {
  4234. Db::rollback();
  4235. return error_show(1004, $e->getMessage());
  4236. }
  4237. }
  4238. public function RelaCgd($outinfo, array &$standing_book_da = [])
  4239. {
  4240. $cgd = Db::name("order_bk")->where([["spuCode", "=", $outinfo['spuCode']], ["is_del", "=", 0], ["balance_num", ">=", $outinfo['good_num']], ['companyNo', "=", $outinfo['companyNo']]])->lock(true)->find();
  4241. if ($cgd == false) {
  4242. return false;
  4243. }
  4244. $good = Db::name("good")->where(["spuCode" => $outinfo['spuCode'], "is_del" => 0])->find();
  4245. if ($good == false) {
  4246. return false;
  4247. }
  4248. $cgdinfo = Db::name("purchease_order")->where(['cgdNo' => $cgd['cgdNo'], "is_del" => 0])->find();
  4249. if ($cgdinfo == false) {
  4250. return false;
  4251. }
  4252. $QrdCgd = [
  4253. "cgdNo" => makeNo("CG"),
  4254. "bkcode" => $cgdinfo['bkcode'],
  4255. 'wsm_code' => $cgdinfo['wsm_code'],
  4256. "cgder_id" => $cgdinfo['cgder_id'],
  4257. "cgder" => $cgdinfo['cgder'],
  4258. "spuCode" => $cgdinfo['spuCode'],
  4259. "good_name" => $cgdinfo['good_name'],
  4260. "good_num" => $outinfo['good_num'],
  4261. "good_price" => $cgdinfo['good_price'],
  4262. "total_fee" => round($cgdinfo['good_price'] * $outinfo['good_num'], 2),
  4263. "pakge_fee" => $cgdinfo['pakge_fee'],
  4264. "cert_fee" => $cgdinfo['cert_fee'],
  4265. "open_fee" => $cgdinfo['open_fee'],
  4266. "delivery_fee" => $cgdinfo['delivery_fee'],
  4267. "mark_fee" => $cgdinfo['mark_fee'],
  4268. "teach_fee" => $cgdinfo['teach_fee'],
  4269. "nake_fee" => $cgdinfo['nake_fee'],
  4270. "demo_fee" => $cgdinfo['demo_fee'],
  4271. "weight" => $cgdinfo['weight'],
  4272. "diff_weight" => $cgdinfo['diff_weight'],
  4273. "diff_fee" => $cgdinfo['diff_fee'],
  4274. "gold_price" => $cgdinfo['gold_price'],
  4275. "supplierNo" => $cgdinfo['supplierNo'],
  4276. "supplier_name" => $cgdinfo['supplier_name'],
  4277. "companyNo" => $cgdinfo['companyNo'],
  4278. "send_status" => 3,
  4279. "send_num" => $outinfo['good_num'],
  4280. "wsend_num" => 0,
  4281. "remark" => $cgdinfo['remark'],
  4282. "status" => 3,
  4283. "lasttime" => $cgdinfo['lasttime'],
  4284. "is_del" => 0,
  4285. "order_type" => $outinfo['order_type'],
  4286. "order_source" => $outinfo['order_source'],
  4287. "good_type" => $cgdinfo['good_type'],
  4288. "addtime" => date("Y-m-d H:i:s"),
  4289. "updatetime" => date("Y-m-d H:i:s"),
  4290. 'good_createrid' => $good['createrid'],
  4291. 'good_creater' => $good['creater'],//商品创建人
  4292. ];
  4293. $insetrCgd = Db::name("purchease_order")->insert($QrdCgd);
  4294. if ($insetrCgd == false) {
  4295. return false;
  4296. } else {
  4297. $standing_book_da = array_merge($standing_book_da, [
  4298. 'orderCode' => $outinfo['orderCode'],
  4299. 'cgdNo' => $cgdinfo['cgdNo'],
  4300. 'spuCode' => $outinfo['spuCode'],
  4301. 'order_type' => $QrdCgd['order_type'],
  4302. 'order_source' => $QrdCgd['order_source'],
  4303. 'supplierNo' => $cgdinfo['supplierNo'],
  4304. 'companyNo' => $cgdinfo['companyNo'],
  4305. 'bk_code' => $cgdinfo['bkcode'],
  4306. 'purchease_id' => Db::name('purchease')->where('bk_code', $cgdinfo['bkcode'])->value('id'),
  4307. ]);
  4308. }
  4309. if ($good['is_gold_price'] == 1 && $good['is_stock'] == 1) {
  4310. $gold = Db::name("gold_price1")
  4311. ->field('id,price')
  4312. ->where(["type" => $good['noble_metal'], "is_del" => 0, "status" => 1])
  4313. ->order("addtime desc")
  4314. ->find();
  4315. //$saleprice(最终售价) = (打样费/购买数量 + 开模费/购买数量 + 商品重量* 最新金价 + 工艺费* 商品重量+包装费+加标费+证书费+产品裸价+物流费)/(1-成本售价/100);
  4316. $sale_price = $good['demo_fee'] / $outinfo['good_num'] + $good['open_fee'] / $outinfo['good_num'] + $good['noble_weight'] * $gold["price"] + $outinfo['cost_fee'] * $good['noble_weight'] + $cgdinfo['pakge_fee'] + $cgdinfo['mark_fee'] + $cgdinfo['cert_fee'] + $cgdinfo['nake_fee'] + $cgdinfo['delivery_fee'];
  4317. $ct['cgd_gold_price'] = $gold["price"];
  4318. $updat = [
  4319. "sale_price" => $sale_price,
  4320. "total_price" => round($sale_price * $outinfo['good_num'], 2),
  4321. "origin_price" => $cgdinfo['good_price'],
  4322. "gold_price" => $gold["price"],
  4323. ];
  4324. $upsale = Db::name("sale")->where(["orderCode" => $outinfo['orderCode']])->update($updat);
  4325. if ($upsale == false) {
  4326. return false;
  4327. }
  4328. }
  4329. $merge_num = Db::name("purchease_order")->where(["bkcode" => $cgdinfo['bkcode'], "order_type" => 1, "is_del" => 0])
  4330. ->where("order_source", "<>", 0)
  4331. ->field("sum(send_num)-sum(th_num) as num")->find();
  4332. $cgd['balance_num'] = $cgd['total_num'] - $merge_num['num'];
  4333. $cgd['merge_num'] = $merge_num['num'];
  4334. $cgd['updatetime'] = date("Y-m-d H:i:s");
  4335. $up = Db::name("order_bk")->save($cgd);
  4336. if ($up == false) {
  4337. return false;
  4338. }
  4339. $data = [
  4340. "orderCode" => $outinfo['orderCode'],
  4341. "cgdNo" => $QrdCgd['cgdNo'],
  4342. "spuCode" => $outinfo['spuCode'],
  4343. "companyNo" => $outinfo['companyNo'],
  4344. "good_num" => $outinfo['good_num'],
  4345. "wsend_num" => $outinfo['good_num'],
  4346. "send_num" => 0,
  4347. "wait_num" => $outinfo['good_num'],
  4348. "status" => 1,
  4349. "source" => 1,
  4350. ];
  4351. $order = Db::name("order_num")->save($data);
  4352. if ($order == false) {
  4353. return false;
  4354. }
  4355. return true;
  4356. }
  4357. public function addother()
  4358. {
  4359. $orderCode = isset($this->post['orderCode']) && $this->post['orderCode'] != "" ? trim($this->post['orderCode']) : "";
  4360. if ($orderCode == "") {
  4361. return error_show(1004, "参数orderCode不能为空");
  4362. }
  4363. $order = Db::name("sale")->where(["orderCode" => $orderCode, "is_del" => 0])->find();
  4364. if ($order == false) {
  4365. return error_show(1004, "未找到订单数据");
  4366. }
  4367. $paytime = isset($this->post['paytime']) && $this->post['paytime'] != "" ? $this->post['paytime'] : "";
  4368. $workNo = isset($this->post['workNo']) && $this->post['workNo'] != "" ? trim($this->post['workNo']) : "";
  4369. $platform_order = isset($this->post['platform_order']) && $this->post['platform_order'] != "" ? trim($this->post['platform_order']) : "";
  4370. $paytime == "" ? "" : $order['paytime'] = $paytime;
  4371. $order['workNo'] = $workNo;
  4372. $order['platform_order'] = $platform_order;
  4373. $order['updatetime'] = date("Y-m-d H:i:s");
  4374. $sa = Db::name("sale")->save($order);
  4375. if ($sa) {
  4376. return app_show(0, "更新成功");
  4377. } else {
  4378. return error_show(1004, "更新失败");
  4379. }
  4380. }
  4381. public function getPrice()
  4382. {
  4383. $skuCode = isset($this->post['skuCode']) && $this->post['skuCode'] != "" ? trim($this->post['skuCode']) : "";
  4384. if ($skuCode === "") {
  4385. return error_show(1003, "参数skuCode不能为空");
  4386. }
  4387. $is_activity = isset($this->post['is_activity']) && $this->post['is_activity'] !== "" ? intval($this->post['is_activity'])
  4388. : "";
  4389. if ($is_activity === "") {
  4390. return error_show(1003, "参数is_activity不能为空");
  4391. }
  4392. $sale_num = isset($this->post['sale_num']) && $this->post['sale_num'] !== "" ? intval($this->post['sale_num']) : "";
  4393. if ($sale_num === "") {
  4394. return error_show(1003, "参数sale_num不能为空");
  4395. }
  4396. $ct = Db::name('good_platform')->alias('a')->join('good b', 'b.spuCode=a.spuCode', 'left')
  4397. ->where(['a.skuCode' => $skuCode])->find();
  4398. if ($ct == false) {
  4399. return error_show(1002, "未找到商品数据");
  4400. }
  4401. $stock = 0;
  4402. if ($ct['is_stock'] == 1) {
  4403. // $good_stock = Db::name("good_stock")->alias("a")->leftJoin("warehouse_info b", "a.wsm_code=b.wsm_code")->where
  4404. // (["spuCode" => $ct['spuCode'], "a.is_del" => 0, "a.status" => 1, "b.wsm_type" => 5])->field("a.id,a.usable_stock,a.wait_out_stock")->find();
  4405. // $good_stock = Db::name("good_stock")->where(["spuCode" => $ct['spuCode'], "is_del" => 0])->find();
  4406. $stock = Db::name("good")->where(["spuCode"=>$ct['spuCode']])->value("usable_stock",0);
  4407. }
  4408. if ($is_activity == 1) {
  4409. $act = Db::name("activity_info")->alias("a")->leftJoin("good_activity b", "a.activity_code=b.activity_code")
  4410. ->where(["a.skuCode" => $skuCode, "a.is_del" => 0, "a.status" => 1, "b.status" => 6, "b.is_del" => 0])->find();
  4411. if ($act == false) {
  4412. return error_show(1003, "未找到相关活动价");
  4413. }
  4414. if ($act['moq_num'] > $sale_num) {
  4415. return error_show(1003, "商品不满足活动价起订量{$act['moq_num']}");
  4416. }
  4417. if ($act['activity_stock'] < $sale_num) {
  4418. return error_show(1003, "商品活动库存剩余{$act['activity_stock']}");
  4419. }
  4420. return app_show(0, "获取成功", ['sale_price' => $act['activity_price'], "stock" => $act['activity_stock']]);
  4421. } else {
  4422. //good_nake 成本阶梯 good_ladder销售阶梯
  4423. $origin = Db::name("good_nake")->where([["spuCode", "=", $ct['spuCode']], ["is_del", "=", 0]])->order("min_num asc")->find();
  4424. if (!$origin) {
  4425. return error_show(1003, "没有找到成本数据");
  4426. }
  4427. $good = Db::name("good_ladder")->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1])->order("min_num asc")->find();
  4428. if (!$good) {
  4429. return error_show(1003, "未找到相关阶梯价格");
  4430. }
  4431. if (!isset($origin['min_num'])) $origin['min_num'] = 0;
  4432. //如果是库存品的话,只取销售的库存
  4433. if ($ct['is_stock'] == 1) $lastnum = $good['min_num'];
  4434. else $lastnum = max($origin['min_num'] ?? 0, $good['min_num']);
  4435. if ($sale_num < $lastnum) {
  4436. return error_show(1003, "商品不满足起订量{$lastnum}");
  4437. }
  4438. $good_temp = Db::name("good_ladder")->where(["skuCode" => $skuCode, "is_del" => 0, "status" => 1])->where('min_num', '<=', $sale_num)->order("min_num desc")->find();
  4439. if (!$good_temp) {
  4440. return error_show(1003, "未找到相关阶梯价格");
  4441. }
  4442. $cat_top_list = made($ct['cat_id']);
  4443. $cat_top_id = isset($cat_top_list[0]['id']) ? $cat_top_list[0]['id'] : 0;
  4444. if ($ct['is_gold_price'] == 1 && $cat_top_id == 6) {
  4445. $gold = Db::name("gold_price1")
  4446. ->field('id,price')
  4447. ->where(["type" => $ct['noble_metal'], "is_del" => 0, "status" => 1])
  4448. ->order("addtime desc")
  4449. ->find();
  4450. if($ct['is_fixed']==0) $origin['nake_fee']=0;
  4451. $saleprice = $ct['demo_fee'] / $sale_num + $ct['open_fee'] / $sale_num + $ct['noble_weight'] * $gold["price"] + $good_temp['cost_fee'] * $ct['noble_weight'] + $origin['package_fee'] + $origin['mark_fee'] + $origin['cert_fee'] + $origin['nake_fee'] + $origin['delivery_fee'];
  4452. return app_show(0, "获取成功", ['sale_price' => $saleprice, "stock" => $stock]);
  4453. }
  4454. return app_show(0, "获取成功", ['sale_price' => $good_temp['sale_price'], "stock" => $stock]);
  4455. }
  4456. }
  4457. public function saleuse()
  4458. {
  4459. $orderCode = isset($this->post['orderCode']) && !empty($this->post['orderCode']) ? $this->post['orderCode'] : "";
  4460. if ($orderCode == "") {
  4461. return error_show(1003, "参数orderCode不能为空");
  4462. }
  4463. $order = Db::name("sale")->where(["orderCode" => $orderCode, "is_del" => 0])->select()->toArray();
  4464. if (empty($order)) {
  4465. return error_show(1003, "订单数据未找到");
  4466. }
  4467. $useid = isset($this->post['useid']) && $this->post['useid'] != "" ? intval($this->post['useid']) : "";
  4468. if ($useid == "") {
  4469. return error_show(1003, "参数useid不能为空");
  4470. }
  4471. $use = Db::name("order_use")->where(["id" => $useid, "is_del" => 0])->find();
  4472. if ($use == false) {
  4473. return error_show(1003, "用途数据未找到");
  4474. }
  4475. $asve = ['use_order' => $useid, "updatetime" => date("Y-m-d H:i:s")];
  4476. $up = Db::name("sale")->where(["orderCode" => $orderCode, "is_del" => 0])->update($asve);
  4477. if ($up > 0) {
  4478. return app_show(0, "更新成功");
  4479. } else {
  4480. return error_show(1003, "更新失败");
  4481. }
  4482. }
  4483. public function goodzxinfo()
  4484. {
  4485. $orderCode = isset($this->post['spuCode']) && $this->post['spuCode'] != '' ? trim($this->post['spuCode']) : "";
  4486. if ($orderCode == "") {
  4487. return error_show(1003, "参数spuCode不能为空");
  4488. }
  4489. $order = Db::name("good_zixun")->where(["spuCode" => $orderCode, "is_del" => 0])->find();
  4490. if (empty($order)) {
  4491. return error_show(1003, "订单数据未找到");
  4492. }
  4493. $unit = Db::name("unit")->where(["id" => $order['good_unit']])->find();
  4494. if ($order['brand_id'] != 0) {
  4495. $brand = Db::name("brand")->where(["id" => $order['brand_id']])->find();
  4496. $order["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  4497. } else {
  4498. $order["brand_name"] = "";
  4499. $order["brand_id"] = "";
  4500. }
  4501. $order['specinfo'] = json_decode($order['specinfo'], true);
  4502. $order['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  4503. $order['cat_info'] = made($order['cat_id'], []);
  4504. $order['noble_name'] = isset($order['noble_metal']) && $order['noble_metal'] != 0 ? $this->noble[$order['noble_metal']] : "";
  4505. if ($order['is_gold_price'] == 1) {
  4506. $price = Db::name("gold_price1")->where(["type" => $order['noble_metal'], "status" => 1, "is_del" => 0])->order("addtime desc")->find();
  4507. }
  4508. $order['tax_info']=GoodTax::with(['Tax','OutTax',"CgdTax"])->where(['spuCode'=>$orderCode])->findOrEmpty();
  4509. $depart = get_company_name_by_uid($order['order_createrid']);
  4510. $userCommon = \app\admin\common\User::getIns();
  4511. $supplier = $userCommon->handle('sInfo',['code'=>$order['supplierNo']]);
  4512. $order['supplier_name'] = isset($supplier['data']['name']) ? $supplier['data']['name'] : "";
  4513. $order['cgder'] = isset($supplier['data']['person']) ? $supplier['data']['person'] : "";
  4514. $order['cgderid'] = isset($supplier['data']['personid']) ? $supplier['data']['personid'] : "";
  4515. $order['order_creater_depart'] = $depart;
  4516. $is_online= Db::name('good_platform')->where(['spuCode'=>$order['spuCode']])->findOrEmpty();
  4517. $order['is_online'] = empty($is_online)?0:1;
  4518. $value['is_basic']= Db::name('good_basic')->where(['spuCode'=>$order['spuCode']])->value("status",null);
  4519. $palt = Db::name("platform")->where(["id" => $order['platform_id']])->find();
  4520. $order['platform_name'] = isset($palt['platform_name']) ? $palt['platform_name'] : "";
  4521. $names = $userCommon->handle('getCodeAndName',['code'=>$order['companyNo']]);
  4522. $order['company'] = $names['data'][$order['companyNo']]??'';
  4523. $order["gold_price"] = isset($price['price']) ? $price['price'] : 0;
  4524. $order["noble_weight"] = isset($order['weight']) ? $order['weight'] : 0;
  4525. return app_show(0, "获取成功", $order);
  4526. }
  4527. //获取采反商品详情,当数据不在good_zixun中,模仿goodzxinfo
  4528. public function getGoodZxInfoByNotZixun()
  4529. {
  4530. $param = $this->request->only(['spuCode'], 'post', 'trim');
  4531. $val = Validate::rule(['spuCode' => 'require']);
  4532. if (!$val->check($param)) return error_show(1004, $val->getError());
  4533. $order = Db::name('consult_bids')
  4534. ->field(true)
  4535. ->where(['spuCode' => $param['spuCode'], 'is_del' => 0])
  4536. ->find();
  4537. if (empty($order)) return error_show(1003, "数据未找到");
  4538. $unit = Db::name("unit")->where(["id" => $order['unit_id']])->find();
  4539. if ($order['brand_id'] != 0) {
  4540. $brand = Db::name("brand")->where(["id" => $order['brand_id']])->find();
  4541. $order["brand_name"] = isset($brand['brand_name']) ? $brand['brand_name'] : "";
  4542. } else {
  4543. $order["brand_name"] = "";
  4544. $order["brand_id"] = "";
  4545. }
  4546. $order['specinfo'] = json_decode($order['specinfo'], true);
  4547. $order['unit'] = isset($unit['unit']) ? $unit['unit'] : '';
  4548. $order['cat_info'] = made($order['cat_id'], []);
  4549. $order['noble_name'] = isset($order['metal_id']) && $order['metal_id'] != 0 ? $this->noble[$order['metal_id']] : "";
  4550. if ($order['is_gold_price'] == 1) {
  4551. $price = Db::name("gold_price1")->where(["type" => $order['metal_id'], "status" => 1, "is_del" => 0])->order("addtime desc")->find();
  4552. }
  4553. $userCommon = \app\admin\common\User::getIns();
  4554. $supplier_temp = $userCommon->handle('getCodeAndName', ['code' => $order['supplierNo']]);
  4555. $order['supplier_name'] = $supplier_temp['data'][$order['supplierNo']]??"";
  4556. $order["gold_price"] = isset($price['price']) ? $price['price'] : 0;
  4557. $order["noble_weight"] = isset($order['weight']) ? $order['weight'] : 0;
  4558. $tax = TaxCategory::where(['id'=>[$order['tax_id'],$order['out_tax_id'],$order['cgd_tax_id']]])->column('short_name,cat_name,cat_code,merge_code','id');
  4559. $order['in_info'] = $tax[$order['tax_id']]??[];
  4560. $order['out_info'] = $tax[$order['out_tax_id']]??[];
  4561. $order['cgd_info'] = $tax[$order['cgd_tax_id']]??[];
  4562. $order['child'] =[];
  4563. if($order['is_combind']==1){
  4564. $order['child'] =ConsultTemp::where(['bidNo'=>$order['bidNo']])->select()->each(function(&$item){
  4565. $tax = TaxCategory::where(['id'=>[$item['tax_id'],$item['out_tax_id'],$item['cgd_tax_id']]])->column('short_name,cat_name,cat_code,merge_code','id');
  4566. $item['in_info'] = $tax[$item['tax_id']]??[];
  4567. $item['out_info'] = $tax[$item['out_tax_id']]??[];
  4568. $item['cgd_info'] = $tax[$item['cgd_tax_id']]??[];
  4569. });
  4570. }
  4571. $good_info = Db::name('good_basic')
  4572. ->field('id,createrid,creater')
  4573. ->where('is_del',0)
  4574. ->where('spuCode',$order['spuCode'])
  4575. ->findOrEmpty();
  4576. //补充商品创建人字段
  4577. // 2022-9-7号确认 creater创建人即采购单的采购员 咨询cgder 只是供应商负责人
  4578. $order['good_createrid']=$good_info['createrid']??$order['createrid'];
  4579. $order['good_creater']=$good_info['creater']??$order['creater'];
  4580. return app_show(0, "获取成功", $order);
  4581. }
  4582. //发货申请单导出
  4583. public function exportSaleOut()
  4584. {
  4585. $outCodes = $this->request->post('outCodes', [], 'trim');
  4586. if (empty($outCodes)) return error_show(1004, '要导出的发货申请单编号不能为空');
  4587. $i = 1;
  4588. $send_type = [1 => '直接发货', 2 => '延时发货'];
  4589. $status = [0 => '待发货', 1 => '待库管发货', 2 => '已发货待收货', 3 => '已收货', 4 => '已全部退货', 5 => '取消发货单'];
  4590. $Cgdstatus = [0 => '待与供应商确认', 1 => '待入库', 2 => '部分入库', 3 => '入库完成', 4 => '取消采购单'];
  4591. $list = Db::name("order_out")
  4592. ->alias('a')
  4593. ->field('"" as 序号,a.addtime as 创建时间,po.cgdNo as 采购单编号,po.status as 采购单状态,po.cgder as 采购员,b.orderCode as 确认单号,b.addtime as 确认单时间,
  4594. b.good_code as 产品编号,b.skuCode as 商品上线编码,"" 平台商品编码,b.good_name as 产品名称,"" as 规格,"" as 单位,b.supName as 供应商名称,b.supNo as 供应商编号,
  4595. a.post_fee as 物流费,b.send_type as 发货方式,b.remark as 确认单备注,po.good_num as 采购数量,a.send_num 发货数量,a.status 发货单状态,
  4596. oa.contactor as 收货人,oa.mobile as 联系方式,oa.addr as 收货地址,oa.addr_code,oa.arrive_time as 到货时间,a.outCode as 发货申请单号,po.order_type,
  4597. b.platform_order 平台订单号,a.post_name 发货申请单物流公司,a.post_code 发货申请单物流单号,a.post_fee 发货申请单物流费用,a.remark 发货申请单备注,a.apply_name 申请人,b.cat_id 商品分类')
  4598. ->whereIn('a.outCode', $outCodes)
  4599. ->order("a.addtime desc")
  4600. ->withAttr('序号', function () use (&$i) {
  4601. return $i++;
  4602. })->withAttr('发货单状态', function ($val) use ($status) {
  4603. return isset($status[$val]) ? $status[$val] : '';
  4604. })->withAttr('发货方式', function ($val) use ($send_type) {
  4605. return isset($send_type[$val]) ? $send_type[$val] : '';
  4606. })->withAttr('采购单状态', function ($val) use ($Cgdstatus) {
  4607. return isset($Cgdstatus[$val]) ? $Cgdstatus[$val] : '';
  4608. })->where("a.is_del", "=", 0)
  4609. ->leftJoin("sale b", "b.orderCode=a.orderCode")
  4610. ->leftJoin("order_addr oa", "oa.id=a.addrid AND oa.is_del=0")
  4611. ->leftJoin('order_send os', 'os.outCode=a.outCode ')
  4612. ->leftJoin("purchease_order po", "po.cgdNo=os.cgdNo AND po.is_del=0")
  4613. ->select()
  4614. ->toArray();
  4615. $userCommon = \app\admin\common\User::getIns();
  4616. $skuArr =array_unique(array_column($list,"商品上线编码"));
  4617. $platCode= (new GoodPlatform())->where(["skuCode"=>$skuArr])->column("plat_code","skuCode");
  4618. foreach ($list as &$value) {
  4619. $value['平台商品编码']= $platCode[$value['商品上线编码']]??"";
  4620. if ($value['order_type'] == 3 || $value['order_type'] == 4) {
  4621. $good = Db::name("good_zixun")
  4622. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  4623. ->field('id,specinfo,good_unit,supplierNo')
  4624. ->find();
  4625. $specinfo = isset($good['specinfo']) ? json_decode($good['specinfo'], true) : [];
  4626. $speclist = [];
  4627. foreach ($specinfo as $val) {
  4628. $speclist[] = $val['spec_name'] . ':' . $val['spec_value_name'];
  4629. }
  4630. } else {
  4631. $good = Db::name('good_basic')
  4632. ->where(['spuCode' => $value['产品编号']])
  4633. ->field('good_unit,supplierNo')
  4634. ->find();
  4635. $spec = Db::name("good_spec")
  4636. ->field('id,spec_id,spec_value_id')
  4637. ->where(["spuCode" => $value['产品编号'], "is_del" => 0])
  4638. ->select()
  4639. ->toArray();
  4640. $speclist = [];
  4641. if (!empty($spec)) {
  4642. foreach ($spec as $val) {
  4643. $speclist[] = Db::name("specs")->where(["id" => $val['spec_id']])->value('spec_name', '') . ':' . Db::name("spec_value")->where(["id" => $val['spec_value_id']])->value('spec_value', '');
  4644. }
  4645. }
  4646. }
  4647. $supplier_temp = $userCommon->handle('sInfo', ['code' =>$value['供应商编号']]);
  4648. $supplier=$supplier_temp['data']??[];
  4649. $value['供应商编号'] = $good['supplierNo']??"";
  4650. // $supplier= Db::name("supplier")->where(["code"=>$value['供应商编号']])->field("name,person")->find();
  4651. $value['供应商名称'] = $supplier['name']??"";
  4652. $value['采购员'] = $supplier['person']??"";
  4653. $good_unit = isset($good['good_unit']) ? $good['good_unit'] : 0;
  4654. $value['规格'] = empty($speclist) ? '' : implode(',', $speclist);;
  4655. $value['单位'] = $good_unit ? Db::name('unit')->where(['id' => $good_unit, 'is_del' => 0])->value('unit', '') : '';
  4656. $value['商品分类'] = implode('/', array_column(made($value['商品分类']), 'name'));
  4657. $value['发货申请单物流单号'] = ' ' . $value['发货申请单物流单号'];//添加空格防止将数字转为科学计数法(比如,韵达的快递单号就是纯数字)
  4658. $addinfo =$value['addr_code']!=''?json_decode($value['addr_code'],true)??$value['addr_code']: '' ;
  4659. if(is_string($addinfo)&&$addinfo!=''){
  4660. $addinfo=["provice_code"=>'',"city_code"=>'',"area_code"=>''];
  4661. list($addinfo['provice_code'],$addinfo['city_code'],$addinfo['area_code']) = explode(",",$value['addr_code']);
  4662. }
  4663. $addr_cn =GetAddr(json_encode($addinfo));
  4664. $value['收货地址'] = $addr_cn.$value['收货地址'];
  4665. unset($value['order_type']);
  4666. unset($value['addr_code']);
  4667. }
  4668. if (empty($list)) $list[] = '没有可供导出的数据';
  4669. $headerArr = array_keys($list[0]);
  4670. excelSave('发货申请单导出' . date('YmdHis'), $headerArr, $list);
  4671. }
  4672. //发货申请单 签收(目前只改动状态,后面要实时查询物流信息,以判断是否签收)
  4673. public function saleoutReceipt()
  4674. {
  4675. $outCode = $this->request->post('outCode', '', 'trim');
  4676. if (empty($outCode)) return error_show(1004, '发货申请单编号不能为为空');
  4677. Db::startTrans();
  4678. try {
  4679. $rs = Db::name('order_out')
  4680. ->field('id,orderCode,apply_id')
  4681. ->where(['outCode' => $outCode, 'status' => 2, "is_del" => 0])
  4682. ->find();
  4683. if (empty($rs)) throw new Exception('该出库单不存在或不允许确认收货');
  4684. //发货单收货
  4685. $res_order_out = Db::name('order_out')
  4686. ->where(['id' => $rs['id'], 'status' => 2, "is_del" => 0])
  4687. ->update(['status' => 3, 'updatetime' => date('Y-m-d H:i:s')]);
  4688. //发货工单不做更新,发货工单只给库管人员看,不关心是否收货,只保留 待发货/已发货/已取消 这几个状态就可以了
  4689. //查询该发货单所属的销售单,是否还有未收货的其他发货单
  4690. $other_res_sale = Db::name('order_out')
  4691. ->where(['orderCode' => $rs['orderCode'], 'status' => 2, "is_del" => 0])
  4692. ->where('id', '<>', $rs['id'])
  4693. ->find();
  4694. if (empty($other_res_sale)) {
  4695. //所属销售单也确认收货
  4696. Db::name('sale')
  4697. ->where(['orderCode' => $rs['orderCode'], 'status' => 0])
  4698. ->update(['status' => 2, 'updatetime' => date('Y-m-d H:i:s')]);
  4699. }
  4700. //修改状态,添加待办
  4701. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
  4702. "order_code" => $outCode,
  4703. "status" => 2,//这里的status是之前的值
  4704. "action_remark" => '',//备注
  4705. "action_type" => "status"//新建create,编辑edit,更改状态status
  4706. ], "CKD", 3, $rs);
  4707. //将待办数据变为已办
  4708. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  4709. "order_code" => $outCode,
  4710. "order_id" => $rs['id'],
  4711. "order_status" => 3,
  4712. "order_type" => 'CKD',
  4713. "before_status" => 2,
  4714. 'holder_id' => $rs['apply_id']
  4715. ]);
  4716. if ($res_order_out) {
  4717. Db::commit();
  4718. return app_show(0, '收货完成');
  4719. } else {
  4720. Db::rollback();
  4721. error_show(1005, '收货失败');
  4722. }
  4723. } catch (\Exception $e) {
  4724. Db::rollback();
  4725. return error_show(1005, $e->getMessage());
  4726. }
  4727. }
  4728. //库管批量发货
  4729. public function outSendBatchByImport()
  4730. {
  4731. $param = $this->request->only(['list', 'token'], 'post', 'trim');
  4732. if (empty($param['list'])) return error_show(1005, 'list参数不能为空');
  4733. //只允许处理所属供应商下的采购单关联的发货单
  4734. $suppliers = Db::name('order_out')
  4735. ->alias('a')
  4736. ->leftJoin('order_num b', 'b.orderCode=a.orderCode')
  4737. ->leftJoin('purchease_order c', 'c.cgdNo=b.cgdNo')
  4738. ->where([
  4739. ['a.is_del', '=', 0],
  4740. ['c.order_type', '<>', 1],
  4741. ['a.outCode', 'in', array_column($param['list'], 'outCode')],
  4742. ])->column('c.supplierNo', 'a.outCode');
  4743. if ($this->level != 3) {
  4744. //检查这些供应商编码是否开通了账号
  4745. $temp_res = checkHasAccountBySupplierNos(array_values($suppliers));
  4746. // ->whereIn('supplierNo', array_values($suppliers))
  4747. // ->column('supplierNo');
  4748. // $temp = [];
  4749. // foreach ($suppliers as $k => $v) {
  4750. // if (in_array($v, $temp_res)) $temp[] = $k;
  4751. // }
  4752. if (!empty($temp_res)) return json_show(1003, '不允许处理已开通账号的供应商的单子', array_keys($temp_res));
  4753. }
  4754. // $user = GetUserInfo($param['token']);
  4755. $createrid = $this->uid;//$user['data']['id']??0;//$this->uid;
  4756. $creater = $this->uname;//$user['data']['nickname']??0;//$this->uname;
  4757. // $own_roleid = Db::name('user_role')->where(['is_del'=>0,'uid'=>$createrid,'status'=>1])->value('roleid',0);
  4758. //所有发货单信息
  4759. $order_out_infos = Db::name("order_out")
  4760. ->where('status', 1)
  4761. ->where('is_del', 0)
  4762. ->whereIn('outCode', array_column($param['list'], 'outCode'))
  4763. ->column('id,status,send_num,orderCode,wsm_code,apply_id,apply_name', 'outCode');
  4764. //所有库管人员信息
  4765. // $wsm_infos=Db::name('warehouse_info')
  4766. // ->where(['is_del'=>0,'wsm_code'=>array_column($order_out_infos,'wsm_code')])
  4767. // ->column('contactor','wsm_code');
  4768. //所有商品信息及供应商编码
  4769. $spuCodes=Db::name('sale')
  4770. ->where(['is_del'=>0,'orderCode'=>array_column($order_out_infos,'orderCode')])
  4771. ->column('good_code');
  4772. $supplierNo1 = Db::name('good_zixun')
  4773. ->where(['spuCode' => $spuCodes, 'is_del' => 0])
  4774. ->column('supplierNo','spuCode');
  4775. $supplierNo2 = Db::name('good')
  4776. ->where(['spuCode' => $spuCodes, 'is_del' => 0])
  4777. ->column('supplierNo','spuCode');
  4778. $supplierNos = array_unique(array_merge($supplierNo1,$supplierNo2));
  4779. $userCommon = \app\admin\common\User::getIns();
  4780. $temp = $userCommon->handle('sGetList',['more_code'=>array_values($supplierNos)]);
  4781. $supplier_personid = array_column($temp['data']['list'],'personid','code');
  4782. //处理数据
  4783. Db::startTrans();
  4784. try {
  4785. $good_data = $temp_out_codes = $yz_data = [];
  4786. foreach ($param['list'] as $value) {
  4787. if (!isset($order_out_infos[$value['outCode']])) throw new Exception($value['outCode'] . '该发货单未找到或状态不允许发货');
  4788. //每次循环都需要重新查询对应数据,为了数据及时更新
  4789. $sale_infos = Db::name("sale")
  4790. ->where('orderCode', $order_out_infos[$value['outCode']]['orderCode'])
  4791. ->where('is_del', 0)
  4792. ->column('id,send_num,wsend_num,status,good_code,order_source,order_type,apply_id,apply_name,is_stock,cgderid,orderCode', 'orderCode');
  4793. if (!isset($sale_infos[$order_out_infos[$value['outCode']]['orderCode']])) throw new Exception($value['outCode'] . '该发货单对应的销售单未找到');
  4794. //判断发货单是否重复
  4795. if (isset($temp_out_codes[$value['outCode']])) {
  4796. Db::rollback();
  4797. return app_show(1005, '下列发货申请单号重复', [$value]);
  4798. } else $temp_out_codes[$value['outCode']] = $value['outCode'];
  4799. if($this->level==2) {
  4800. //level2账号的话,库存品只能由库管操作,非库存品只能由供应商负责人操作,level3账号不限制
  4801. if ($sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['is_stock'] == 1) {
  4802. //由于存在一个单子对应多个仓库的情况,因此库存品的判断暂时无法进行,待分仓功能完成后再考虑
  4803. // if($wsm_infos[$order_out_infos[$value['outCode']]['wsm_code']]??0 != $createrid) throw new \think\Exception($value['outCode'] . '发货单只能由库管操作');
  4804. // if (!in_array($own_roleid, config('app.wsm_cgder_role'))) {
  4805. // Db::rollback();
  4806. // return app_show(1003, $value['outCode'] . '发货单只能由库管操作');
  4807. // }
  4808. } else {
  4809. // if ($sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['order_type'] == 3 || $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['order_type'] == 4) {
  4810. // $supplierNo = Db::name('good_zixun')
  4811. //// ->leftJoin("supplier b", "a.supplierNo=b.code")
  4812. // ->where(['spuCode' => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['good_code'], 'is_del' => 0])
  4813. // ->value('supplierNo');
  4814. // } else {
  4815. // $supplierNo = Db::name('good')
  4816. //// ->alias("a")
  4817. //// ->leftJoin("supplier b", "a.supplierNo=b.code")
  4818. // ->where(['spuCode' =>$sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['good_code'], 'is_del' => 0])
  4819. // ->value('supplierNo');
  4820. // }
  4821. if ($createrid != $supplier_personid[$supplierNos[$sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['good_code']]]) throw new \think\Exception($value['outCode'] . '发货单只能由供应商负责人操作');
  4822. // Db::rollback();
  4823. // return app_show(1003, $value['outCode'] . '发货单只能由供应商负责人操作');
  4824. }
  4825. }
  4826. //【发货单】
  4827. //查询已有发货单信息及状态
  4828. //更新发货单
  4829. $up = Db::name("order_out")
  4830. ->where(['id' => $order_out_infos[$value['outCode']]['id'], 'outCode' => $value['outCode'], 'status' => 1, "is_del" => 0])
  4831. ->update([
  4832. 'post_name' => isset($value['post_name']) ? $value['post_name'] : '',
  4833. 'post_code' => $value['post_code'],
  4834. 'post_fee' => $value['post_fee'],
  4835. 'sendtime' => date('Y-m-d H:i:s'),
  4836. 'status' => 2,
  4837. 'updatetime' => date('Y-m-d H:i:s'),
  4838. 'remark' => isset($value['remark']) ? $value['remark'] : '',
  4839. ]);
  4840. if ($up) {
  4841. //修改状态,添加待办
  4842. ActionLog::logAdd(['id' => $createrid, 'nickname' => $creater], [
  4843. "order_code" => $value['outCode'],//出库单号
  4844. "status" => $order_out_infos[$value['outCode']]['status'],//这里的status是之前的值
  4845. "action_remark" => '',//备注
  4846. "action_type" => "edit"//新建create,编辑edit,更改状态status
  4847. ], "CKD", 2, $order_out_infos[$value['outCode']]);
  4848. ProcessOrder::AddProcess(['id' => $createrid, 'nickname' => $creater], [
  4849. "order_type" => 'CKD',
  4850. "order_code" => $value['outCode'],//出库单号
  4851. "order_id" => $order_out_infos[$value['outCode']]['id'],
  4852. "order_status" => 2,
  4853. "before_status" => $order_out_infos[$value['outCode']]['status'],
  4854. 'wait_id' => $order_out_infos[$value['outCode']]['apply_id'],
  4855. 'wait_name' => $order_out_infos[$value['outCode']]['apply_name'],
  4856. 'holder_id' => $order_out_infos[$value['outCode']]['apply_id'],
  4857. ]);
  4858. //【销售单】
  4859. $einfo = [];
  4860. $einfo['send_num'] = $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['send_num'] + $order_out_infos[$value['outCode']]['send_num'];
  4861. $einfo['wsend_num'] = $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['wsend_num'] - $order_out_infos[$value['outCode']]['send_num'];
  4862. $einfo['status'] = $einfo['wsend_num'] == 0 ? 2 : ($einfo['send_num'] == 0 ? 0 : 1);
  4863. $einfo['send_status'] = $einfo['wsend_num'] == 0 ? 3 : ($einfo['send_num'] == 0 ? 1 : 2);
  4864. $einfo['updatetime'] = date("Y-m-d H:i:s");
  4865. $saleup = Db::name("sale")
  4866. ->where('id', $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['id'])
  4867. ->update($einfo);
  4868. if ($saleup == false) throw new Exception($value['outCode'] . '销售单状态更新失败');
  4869. //修改状态,添加待办
  4870. ActionLog::logAdd(['id' => $createrid, 'nickname' => $creater], [
  4871. "order_code" => $order_out_infos[$value['outCode']]['orderCode'],//销售单号
  4872. "status" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['status'],//这里的status是之前的值
  4873. "action_remark" => '',//备注
  4874. "action_type" => "edit"//新建create,编辑edit,更改状态status
  4875. ], "XSQRD", $einfo['status'], $einfo);
  4876. ProcessOrder::AddProcess(['id' => $createrid, 'nickname' => $creater], [
  4877. "order_type" => 'XSQRD',
  4878. "order_code" => $order_out_infos[$value['outCode']]['orderCode'],//出库单号
  4879. "order_id" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['id'],
  4880. "order_status" => $einfo['status'],
  4881. "before_status" => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['status'],
  4882. 'holder_id' => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['apply_id']
  4883. ]);
  4884. if($sale_infos[$order_out_infos[$value['outCode']]['orderCode']]["order_type"]!=1){
  4885. //【库存】
  4886. $stock = Db::name("good_stock")
  4887. ->field('id,wait_out_stock,usable_stock,total_stock,updatetime')
  4888. ->where([
  4889. "is_del" => 0,
  4890. 'spuCode' => $sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['good_code'],
  4891. "wsm_code" => $order_out_infos[$value['outCode']]['wsm_code'],
  4892. ])->find();
  4893. if ($stock == false) throw new Exception($value['outCode'] . '未找到库存数据');
  4894. else {
  4895. if ($order_out_infos[$value['outCode']]['send_num'] > $stock['wait_out_stock']) throw new Exception($value['outCode'] . '超出库存数量');
  4896. $stock['wait_out_stock'] -= $order_out_infos[$value['outCode']]['send_num'];
  4897. $stock['total_stock'] = $stock['wait_out_stock'] + $stock['usable_stock'];
  4898. $stock['updatetime'] = date("Y-m-d H:i:s");
  4899. $stock_rs = Db::name("good_stock")
  4900. ->where('id', $stock['id'])
  4901. ->update($stock);
  4902. if ($stock_rs == false) throw new Exception($value['outCode'] . '库存更新失败');
  4903. // $outsend =Db::name("order_send")->where(["outCode"=> $value['outCode']])->findOrEmpty();
  4904. // if(empty($outsend)){
  4905. // Db::rollback();
  4906. // return error_show(1002, "未找到关联采购单");
  4907. // }
  4908. // $cgdinfo= Db::name("purchease_order")->where(["cgdNo"=>$outsend['cgdNo'],"is_del"=>0])
  4909. // ->findOrEmpty();
  4910. // $bn_code =makeNo("BN");
  4911. // $bnin=GoodStockInfo::AddBn($stock["id"],$bn_code,$order_out_infos[$value['outCode']]['send_num'],$cgdinfo['good_price']??0);
  4912. // if($bnin==false){
  4913. // Db::rollback();
  4914. // return error_show(1004,"库存bn数据新增失败");
  4915. // }
  4916. // $bnup =GoodStockInfo::OrderBn($order_out_infos[$value['outCode']]['orderCode'],$stock["id"],$order_out_infos[$value['outCode']]['send_num'],0);
  4917. // if($bnup==false){
  4918. // Db::rollback();
  4919. // return error_show(1004,"库存bn库存数更新失败");
  4920. // }
  4921. // $outsend['bnCode']=$bn_code;
  4922. // $outsend['updatetime']=date("Y-m-d H:i:s");
  4923. // $ps=Db::name("order_send")->save($outsend);
  4924. // if($ps==false){
  4925. // Db::rollback();
  4926. // return error_show(1004,"库存bn库存数更新失败");
  4927. // }
  4928. }
  4929. $good_data[] = [
  4930. 'good_log_code' => $value['outCode'],
  4931. 'stock_id' => $stock['id'],
  4932. 'type' => 2,
  4933. 'stock' => $order_out_infos[$value['outCode']]['send_num'],
  4934. 'stock_name' => 'wait_out_stock'
  4935. ];
  4936. }else{
  4937. // $bnup =SaleInfo::SendStock($order_out_infos[$value['outCode']]['orderCode'],$order_out_infos[$value['outCode']]['send_num']);
  4938. // if($bnup==false){
  4939. // Db::rollback();
  4940. // return error_show(1004,"库存bn库存数更新失败");
  4941. // }
  4942. // $saleinfo =Db::name("sale_info")->where(["orderCode"=>$order_out_infos[$value['outCode']]['orderCode']])->select()->toArray();
  4943. // if (!empty($saleinfo)){
  4944. // $bnup =SaleInfo::SendStock($order_out_infos[$value['outCode']]['orderCode'],$order_out_infos[$value['outCode']]['send_num']);
  4945. // if($bnup==false){
  4946. // Db::rollback();
  4947. // return error_show(1004,"库存bn库存数更新失败");
  4948. // }
  4949. // }else{
  4950. $stokc = Db::name("good_stock")->where(['spuCode' =>$sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['good_code'], "wsm_code" => $order_out_infos[$value['outCode']]['wsm_code'], "is_del" => 0])->find();
  4951. if ($stokc == false) {
  4952. Db::rollback();
  4953. return error_show(1002, "未找到库存数据");
  4954. } else {
  4955. if ($order_out_infos[$value['outCode']]['send_num'] > $stokc['wait_out_stock']) {
  4956. Db::rollback();
  4957. return error_show(1002, "超出库存数量");
  4958. }
  4959. $stokc['wait_out_stock'] -= $order_out_infos[$value['outCode']]['send_num'];
  4960. $stokc['total_stock'] = $stokc['wait_out_stock'] + $stokc['usable_stock'];
  4961. // $stokc['intra_stock']+=$outinfo['send_num'];
  4962. $stokc['updatetime'] = date("Y-m-d H:i:s");
  4963. }
  4964. $stoc = Db::name("good_stock")->save($stokc);
  4965. if ($stoc == false) {
  4966. Db::rollback();
  4967. return error_show(1002, "库存更新失败");
  4968. }
  4969. // }
  4970. }
  4971. } else throw new Exception($value['outCode'] . '发货失败');
  4972. // Cache::store("redis")->handler()->lPush("SENDOUT",$value['outCode']);
  4973. //如果是有赞订单的话,将发货信息推到有赞
  4974. if($sale_infos[$order_out_infos[$value['outCode']]['orderCode']]['order_source']==5){
  4975. $yz_data[]=[
  4976. 'orderCode'=>$sale_infos[$order_out_infos[$value['outCode']]['orderCode']],
  4977. 'out_stype'=>isset($value['post_name']) ? $value['post_name'] : '',
  4978. 'post_code'=>$value['post_code'],
  4979. 'uid'=>$createrid,
  4980. 'uname'=>$creater,
  4981. 'order_out'=>$value['outCode'],
  4982. ];
  4983. }
  4984. }
  4985. if (!empty($good_data)) GoodLog::LogAdd(['id' => $createrid, 'nickname' => $creater], $good_data, "CKD");
  4986. Db::commit();
  4987. //如果是有赞订单的话,将发货信息推到有赞
  4988. //有赞信息有可能推送失败(比如超过72小时,不允许多次修改等),所以不应该和这里的事务放到一起
  4989. if ($yz_data) {
  4990. foreach ($yz_data as $yz) {
  4991. // $res = curl_request(config('app.yz_domain') . 'api/yz_out_send', ['orderCode' => $einfo['orderCode'], 'out_stype' => $post_name, 'post_code' => $post_code, 'uid' => $uid, 'uname' => $uname, 'order_out' => $outCode]);
  4992. curl_request(config('app.yz_domain') . 'api/yz_out_send', $yz);
  4993. }
  4994. // $res = json_decode($res, true);
  4995. // if ($res['code'] != 0) return app_show(0, '发货成功,' . $res['message']);
  4996. }
  4997. return app_show(0, '发货成功');
  4998. } catch (Exception $exception) {
  4999. Db::rollback();
  5000. return error_show(1004, $exception->getMessage());
  5001. }
  5002. }
  5003. //获取物流信息
  5004. public function getLogisticsInformation()
  5005. {
  5006. $post_code = $this->request->post('post_code', '', 'trim');
  5007. if (empty($post_code)) return error_show(1004, '快递单号不能为空');
  5008. $postCode = explode(",", $post_code);
  5009. $rs = Db::name('express_data')
  5010. ->where('post_code', $postCode[0])
  5011. ->where('status', ">=", 0)
  5012. ->withAttr('post_data', function ($val) {
  5013. return json_decode($val, true);
  5014. })->findOrEmpty();
  5015. settype($rs, 'object');//格式统一转为对象
  5016. return app_show(0, '请求成功', $rs);
  5017. }
  5018. //订单取消
  5019. //未发货状态添加撤销功能 要求1.库存品 未发货 直接撤销 2.除了库存品以外的商品类型,采购单未入库的 可撤销。
  5020. public function orderCancel()
  5021. {
  5022. $orderCode = $this->request->post('orderCode', '', 'trim');
  5023. $val = Validate::rule(['orderCode' => 'require|array|max:100']);
  5024. if ($val->check(['orderCode' => $orderCode]) == false) return json_show(1004, $val->getError());
  5025. $list = Db::name('sale')
  5026. ->alias('a')
  5027. ->field('a.id sale_id,a.good_code,a.platform_order,a.good_name,a.wsend_num,a.good_num,a.total_price,a.order_type,
  5028. a.status sale_status,a.orderCode,a.cat_id,a.good_createrid,c.id cgd_id,c.status cgd_status,c.cgdNo,c.cgder_id,
  5029. a.addtime,c.wsm_code,a.order_source,a.other_orderNo')
  5030. ->leftJoin('order_num b', 'b.orderCode=a.orderCode')
  5031. ->leftJoin('purchease_order c', 'c.cgdNo=b.cgdNo')
  5032. ->where(['a.is_del' => 0, 'a.orderCode' => $orderCode])
  5033. ->select()
  5034. ->toArray();
  5035. Db::startTrans();
  5036. try {
  5037. $date = date('Y-m-d H:i:s');
  5038. $th=[];
  5039. $cgdId=[];
  5040. foreach ($list as $sale) {
  5041. if ($sale['sale_status'] == 3) throw new \think\Exception($sale['orderCode'] . '已取消,无法重复操作');
  5042. if ($sale['order_type'] == 1) {
  5043. if ($sale['sale_status'] != 0) throw new \think\Exception($sale['orderCode'] . '已发货,无法取消');
  5044. $good = Db::name("good")->where(["spuCode" => $sale['good_code']])->findOrEmpty();
  5045. if (empty($good)) throw new Exception('未找到商品信息');
  5046. $stock = Db::name("good")->where($good)
  5047. ->inc("usable_stock",$sale['wsend_num'])
  5048. ->update();
  5049. if ($stock == false) throw new Exception('商品库存更新失败');
  5050. if(strtotime($sale['addtime'])< strtotime('2023-04-17 00:00:00')){
  5051. $saleinfo = Db::name('sale_info')
  5052. ->where([['orderCode', '=',$sale['orderCode']], ['num', '>', 0]])
  5053. ->select()
  5054. ->toArray();
  5055. if (empty($saleinfo)) {
  5056. $ordernum = Db::name('order_num')->where(['orderCode' =>$sale['orderCode']])->findOrEmpty();
  5057. if (empty($ordernum)) throw new Exception('未找到关联采购单');
  5058. $cgd = Db::name('purchease_order')->where(['cgdNo' => $ordernum['cgdNo'], 'is_del' => 0])->findOrEmpty();
  5059. if (empty($cgd)) throw new Exception('未找到采购单数据');
  5060. $bn = makeNo('BN');
  5061. $stock = Db::name('good_stock')
  5062. ->where(['spuCode' => $sale['good_code'], 'wsm_code' => $cgd['wsm_code'], 'is_del' => 0, 'status' => 1])
  5063. ->findOrEmpty();
  5064. if ($stock == false) throw new Exception('商品库存数据未找到');
  5065. $stock['usable_stock'] += $sale['wsend_num'];
  5066. $stock['wait_out_stock'] -= $sale['wsend_num'];
  5067. $stock['updatetime'] = $date;
  5068. $st_up = Db::name('good_stock')->save($stock);
  5069. if ($st_up == false) throw new Exception('可售商品入库失败');
  5070. $yp = GoodStockInfo::AddBn($stock['id'], $bn, $sale['wsend_num'], $cgd['good_price']);
  5071. if ($yp == false) throw new Exception('商品批次退货入库失败');
  5072. }
  5073. else {
  5074. $tempnum = $sale['wsend_num'];
  5075. foreach ($saleinfo as $va) {
  5076. if ($tempnum == 0) break;
  5077. $stock = Db::name('good_stock')->where(['spuCode' => $sale['good_code'], 'id' => $va['stockid']])
  5078. ->findOrEmpty();
  5079. if ($stock == false) throw new Exception('商品库存数据未找到');
  5080. if ($va['num'] >= $tempnum) {
  5081. $tnm = $tempnum;
  5082. $va['num'] -= $tempnum;
  5083. $va['th_num'] += $tempnum;
  5084. $tempnum = 0;
  5085. } else {
  5086. $tnm = $va['num'];
  5087. $tempnum -= $va['num'];
  5088. $va['th_num'] += $va['num'];
  5089. $va['num'] = 0;
  5090. }
  5091. $stock['usable_stock'] += $tnm;
  5092. $stock['wait_out_stock'] -= $tnm;
  5093. $stock['updatetime'] = $date;
  5094. $st_up = Db::name('good_stock')->save($stock);
  5095. if ($st_up == false) throw new Exception('可售商品入库失败');
  5096. $ps = GoodStockInfo::AddBn($va['stockid'], $va['bnCode'], $tnm);
  5097. if ($ps == false) throw new Exception('商品批次退货入库失败');
  5098. $ret = GoodStockInfo::ReturnBn($sale['orderCode'], $va['id'], $tnm);
  5099. if ($ret == false) throw new Exception('商品批次退货入库失败');
  5100. $va['updatetime'] = $date;
  5101. $sal = Db::name('sale_info')->save($va);
  5102. if ($sal == false) throw new Exception('商品批次退货入库失败');
  5103. }
  5104. }
  5105. }
  5106. SaleCgdPrice::RePrice($sale['sale_id'],$sale['wsend_num']);
  5107. }
  5108. else{
  5109. if ($sale['order_source'] != 9){
  5110. $good = Db::name('good_stock')->where(['spuCode' => $sale['good_code'],'wsm_code'=>$sale['wsm_code']])
  5111. ->findOrEmpty();
  5112. if (empty($good)) throw new Exception('未找到商品信息');
  5113. if($sale['cgd_status']==1){
  5114. $stock = Db::name('good_stock')->where($good)
  5115. ->inc('wait_in_stock',$sale['wsend_num'])
  5116. ->update();
  5117. if ($stock == false) throw new Exception('商品库存更新失败');
  5118. }
  5119. if($sale['cgd_status']==3){
  5120. $wait_out= Db::name('order_out_child')
  5121. ->where(['is_del' => 0, 'orderCode' => $sale['orderCode'],'status'=>1])
  5122. ->sum('num');
  5123. $usable = $sale['wsend_num'] - intval($wait_out);
  5124. if($usable>0){
  5125. $stock = Db::name('good_stock')->where($good)
  5126. ->inc('usable_stock',$usable)
  5127. ->update();
  5128. if ($stock == false) throw new Exception('商品库存更新失败');
  5129. }
  5130. }
  5131. }
  5132. }
  5133. if($sale['order_type'] == 4){
  5134. $fill =Db::name("filing")->where(["filingCode"=>$sale["platform_order"]])->findOrEmpty();
  5135. if(empty($fill)) throw new Exception($sale['orderCode'] . '未找到对应得报备单');
  5136. $fullData=[
  5137. 'transfer_num'=>$fill['transfer_num']-$sale['wsend_num'],
  5138. 'wait_num'=>$fill['wait_num']+$sale['wsend_num'],
  5139. 'status'=>$fill['wait_num']+$sale['wsend_num']==0?4:
  5140. ($fill['transfer_num']-$sale['wsend_num']==0?2:3)];
  5141. $fillup=Db::name('filing')
  5142. ->where($fill)
  5143. ->data($fullData)
  5144. ->update();
  5145. $stn = ['order_code' => $fill['filingCode'], 'status' =>$fullData['status'], 'action_remark' => '', 'action_type' => 'status'];
  5146. ActionLog::logAdd(['id'=>$this->uid,'nickname'=>$this->uname], $stn, 'BBD', $fill['status'],$fullData);
  5147. if($fillup==false)throw new Exception($sale['platform_order'] . '报备单更新失败');
  5148. ProcessOrder::AddProcess(['id'=>$this->uid,'nickname'=>$this->uname], [
  5149. 'order_type' => 'BBD',
  5150. 'order_code' => $fill['filingCode'],//出库单号
  5151. 'order_id' => $fill['id'],
  5152. 'order_status' =>$fullData['status'],
  5153. 'before_status' => $fill['status'],
  5154. 'wait_id'=>0,
  5155. 'wait_name'=>0,
  5156. ]);
  5157. }
  5158. if($sale['order_source'] == 11){
  5159. $fill =(new BbcFill())->where(['fillCode'=>$sale['other_orderNo']])->findOrEmpty();
  5160. if($fill->isEmpty()) throw new Exception($sale['orderCode'] . '未找到对应得业务报备单');
  5161. if($fill->transfer_num <$sale['wsend_num'])throw new Exception($sale['orderCode'] . '对应得业务报备单已转单数量不足');
  5162. $fill->transfer_num -=$sale['wsend_num'];
  5163. $fill->status=$fill->transfer_num >0?2:1;
  5164. $upd=$fill->save();
  5165. if($upd==false)throw new \Exception('业务报备单更新失败');
  5166. }
  5167. $fininfo =GetFin("admin/orderischeck",["token"=>$this->post['token'],"orderCode"=>$sale['orderCode'], "isCgd"=>1]);
  5168. if(!isset($fininfo['code'])||$fininfo['code']!=0 ){
  5169. Db::rollback();
  5170. return json_show(10005, $fininfo['message']??'结算校验失败',$fininfo['data']??[]);
  5171. }
  5172. $thtemp=[
  5173. "orderCode"=>$sale["orderCode"],
  5174. "th_type"=>4,//取消订单
  5175. "th_num"=>$sale["good_num"],
  5176. "th_fee"=>$sale["total_price"],
  5177. "thCode"=>$sale["orderCode"],
  5178. "cat_id"=>$sale["cat_id"],
  5179. "good_name"=>$sale["good_name"],
  5180. "spuCode"=>$sale["good_code"],
  5181. "apply_id"=>$this->uid,
  5182. "apply_name"=>$this->uname,
  5183. "addtime"=>$date
  5184. ];
  5185. $th[]=$thtemp;
  5186. //修改状态,添加待办
  5187. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
  5188. "order_code" => $sale['orderCode'],//销售单号
  5189. "status" => $sale['sale_status'],//这里的status是之前的值
  5190. "action_remark" => '',//备注
  5191. "action_type" => "status"//新建create,编辑edit,更改状态status
  5192. ], "XSQRD", 3, ['orderCode' => $orderCode]);
  5193. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  5194. "order_type" => 'XSQRD',
  5195. "order_code" => $sale['orderCode'],
  5196. "order_id" => $sale['sale_id'],
  5197. "order_status" => 3,
  5198. "before_status" => $sale['sale_status'],
  5199. 'holder_id' => $sale['good_createrid'],
  5200. ]);
  5201. if ($sale['cgdNo']) {
  5202. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
  5203. "order_code" => $sale['cgdNo'],//销售单号
  5204. "status" => $sale['cgd_status'],//这里的status是之前的值
  5205. "action_remark" => '',//备注
  5206. "action_type" => "status"//新建create,编辑edit,更改状态status
  5207. ], "CGD", 4, ['orderCode' => $sale['cgdNo']]);
  5208. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  5209. "order_type" => 'CGD',
  5210. "order_code" => $sale['cgdNo'],
  5211. "order_id" => $sale['cgd_id'],
  5212. "order_status" => 4,
  5213. "before_status" => $sale['cgd_status'],
  5214. 'holder_id' => $sale['cgder_id'],
  5215. ]);
  5216. $cgdId[]=$sale['cgd_id'];
  5217. }
  5218. }
  5219. $orderup= Db::name('sale')
  5220. ->where(['is_del' => 0, 'id' => array_column($list, 'sale_id')])
  5221. ->update([
  5222. 'status' => 3,
  5223. 'th_num' => Db::raw("good_num"),
  5224. 'th_fee' =>Db::raw("total_price"),
  5225. 'send_num' =>Db::raw("good_num"),
  5226. 'wsend_num' =>0,
  5227. 'send_status' =>3,
  5228. 'updatetime' => $date
  5229. ]);
  5230. if($orderup==false) throw new \Exception("订单状态更新失败");
  5231. $thin= Db::name("th_data")->insertAll($th);
  5232. if($thin==false) throw new \Exception('订单取消失败');
  5233. if(!empty($cgdId)){
  5234. $cgdup=Db::name('purchease_order')
  5235. ->where(['is_del' => 0, 'id' => $cgdId])
  5236. ->update([
  5237. 'status' => 4,
  5238. 'th_num' => Db::raw('wsend_num+send_num'),
  5239. 'th_fee' =>Db::raw('total_fee'),
  5240. 'send_num' =>Db::raw('wsend_num+send_num'),
  5241. 'wsend_num' =>0,
  5242. 'send_status' =>3,
  5243. 'updatetime' => $date]);
  5244. if($cgdup==false) throw new \Exception('采购单取消失败');
  5245. }
  5246. $out_list = Db::name('order_out')
  5247. ->where(['is_del' => 0, 'orderCode' => $orderCode])
  5248. ->select()
  5249. ->toArray();
  5250. if (!empty($out_list)) {
  5251. $sen= Db::name('order_out')
  5252. ->where(['is_del' => 0, 'orderCode' => $orderCode])
  5253. ->update(['status' => 5, 'updatetime' => $date]);
  5254. if($sen==false) throw new \Exception('发货单取消失败');
  5255. foreach ($out_list as $out) {
  5256. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
  5257. "order_code" => $out['outCode'],//销售单号
  5258. "status" => $out['status'],//这里的status是之前的值
  5259. "action_remark" => '',//备注
  5260. "action_type" => "status"//新建create,编辑edit,更改状态status
  5261. ], "CKD", 5, $out);
  5262. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  5263. "order_type" => 'CKD',
  5264. "order_code" => $out['outCode'],
  5265. "order_id" => $out['id'],
  5266. "order_status" => 5,
  5267. "before_status" => $out['status'],
  5268. 'holder_id' => $out['apply_id'],
  5269. ]);
  5270. }
  5271. }
  5272. $ooc_list = Db::name('order_out_child')
  5273. ->where(['is_del' => 0, 'orderCode' => $orderCode])
  5274. ->select()
  5275. ->toArray();
  5276. if (!empty($ooc_list)) {
  5277. $child= Db::name('order_out_child')
  5278. ->where(['is_del' => 0, 'orderCode' => $orderCode])
  5279. ->update(['status' => 5, 'updatetime' => $date]);
  5280. if ($child == false) throw new Exception('发货工单更新失败');
  5281. foreach ($ooc_list as $ooc) {
  5282. $stockinfo = Db::name("good_stock")->where(["spuCode" => $ooc['spuCode'], "wsm_code" => $ooc['wsm_code']])->findOrEmpty();
  5283. if (empty($stockinfo)) throw new Exception('未找到库存信息');
  5284. $stockup = [
  5285. "usable_stock" =>$ooc['order_type']==1? $stockinfo['usable_stock'] + $ooc['num']:$stockinfo['usable_stock'],
  5286. "wait_out_stock" => $stockinfo['wait_out_stock'] - $ooc['num'],
  5287. "updatetime" => $date
  5288. ];
  5289. $ysup = Db::name("good_stock")->where($stockinfo)->update($stockup);
  5290. if ($ysup == false) throw new Exception('库存信息更新失败');
  5291. if($ooc['order_type']==1)GoodStockInfo::ChildReturnBn($ooc['outChildCode'], $stockinfo['id'], $ooc['num']);//处理bn数据
  5292. ActionLog::logAdd(['id' => $this->uid, 'nickname' => $this->uname], [
  5293. "order_code" => $ooc['outChildCode'],//销售单号
  5294. "status" => $ooc['status'],//这里的status是之前的值
  5295. "action_remark" => '',//备注
  5296. "action_type" => "status"//新建create,编辑edit,更改状态status
  5297. ], "FHGD", 5, $ooc);
  5298. ProcessOrder::AddProcess(['id' => $this->uid, 'nickname' => $this->uname], [
  5299. "order_type" => 'FHGD',
  5300. "order_code" => $ooc['outChildCode'],
  5301. "order_id" => $ooc['id'],
  5302. "order_status" => 5,
  5303. "before_status" => $ooc['status'],
  5304. 'holder_id' => $ooc['apply_id'],
  5305. ]);
  5306. }
  5307. }
  5308. Db::commit();
  5309. return json_show(0, '操作成功');
  5310. } catch (\think\Exception $exception) {
  5311. Db::rollback();
  5312. return json_show(1004, $exception->getMessage());
  5313. }catch (Exception $exception){
  5314. Db::rollback();
  5315. return json_show(1004, $exception->getMessage());
  5316. }
  5317. }
  5318. }