Sale.php 153 KB

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