Sale.php 280 KB

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