Sale.php 203 KB

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