Sale.php 296 KB

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