Sale.php 248 KB

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