Sale.php 290 KB

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