Sale.php 266 KB

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