Sale.php 175 KB

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