Sale.php 247 KB

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