Sale.php 232 KB

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