Sale.php 258 KB

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