Sale.php 291 KB

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