Sale.php 218 KB

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