Sale.php 277 KB

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