Sale.php 220 KB

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