Sale.php 269 KB

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