Sale.php 153 KB

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