Sale.php 267 KB

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