| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907 |
- <?php
- /**
- * 重庆赤晓店信息科技有限公司
- * https://www.chixiaodian.com
- * Copyright (c) 2023 赤店商城 All rights reserved.
- */
- namespace app\modules\admin\models;
- // use app\models\Form;
- // use app\models\Option;
- use app\jobs\CreatedStbzOrderTranJob;
- use app\models\Address;
- use app\models\AgentFrontBind;
- use app\models\AgentFrontErpInventory;
- use app\models\AgentFrontErpInventoryLog;
- use app\models\AgentGoodsInstallLog;
- use app\models\CloudGoodsUpdateLog;
- use app\models\DeliveryInfo;
- use app\models\District;
- use app\models\FrontDeliveryOrder;
- use app\models\FrontExpressOrder;
- use app\models\LocalDeliveryFreight;
- use app\models\Md;
- use app\models\OrderGoodsCancel;
- use app\models\OrderRefund;
- use app\models\SaasUser;
- use app\models\Store;
- use app\models\StoreForbiddenDirectory;
- use app\models\StoreShareMoney;
- use app\modules\client\models\v1\order\OrderForm;
- use app\utils\CurlHelper;
- use app\utils\PrintOrder;
- use Yii;
- use Exception;
- use yii\base\Model;
- use app\models\Admin;
- use app\models\Supplier;
- use app\models\StoreCloud;
- use app\models\Goods;
- use app\models\Option;
- use app\models\CloudGoodsBind;
- use app\models\OrderDetail;
- use app\models\Order;
- use app\models\OrderTransit;
- use app\models\Share;
- use app\models\Attr;
- use yii\base\BaseObject;
- use yii\helpers\Json;
- use app\jobs\BatchDownGoodsJob;
- use app\constants\OptionSetting;
- use yii\data\Pagination;
- use app\models\Purchase;
- use app\models\PurchaseOrder;
- use app\models\PurchaseOrderDetail;
- class MerchantForm extends Model
- {
- public $name;
- public $password;
- public $tel;
- public $logo;
- public $type;
- public $certificate;
- public $id_card_reverse;
- public $id_card_front;
- public $page;
- public $limit;
- public $token_stroe_cloud_id = 0;
- public $supplier_id; //云库供货商id
- public $cloud_user_id; //云库会员id
- public $price; //变动金额
- public $desc; //详情
- public $status; //状态
- public $cash_id; //提现ID
- public $pay_type; //打款方式 1 微信打款 2 支付宝打款 3 银行卡打款 4 其他
- public $service_charge; //手续费
- public $user_id;
- public $cloud_store_id; //云仓商户id
- public $attr_id; //属性id 多规格'12-21'
- public $goods_id; //商品id
- public $province_id; //省
- public $city_id; //市
- public $district_id; //区
- public $address; //详细地址
- public $goods_info; //商品详情
- public $mch_id; //商户id
- public $start_time;
- public $end_time;
- public $mch_order_no; //订单号
- public $order_no; //订单号
- public $order_id; //订单id
- public $cloud_goods_id; //云仓商品id
- public $cloud_attr_id; //云仓属性id
- public $cloud_supplier_id; //云仓供货商id
- public $apply_data;
- public $user_send_express;
- public $user_send_express_no;
- public $sort;
- public $origin;
- public $mobile;
- public $goods_name;
- public $sale_sort;
- public $price_sort;
- public $is_distribution;
- public $cat_id;
- public $parent_id;
- public $store_id;
- public $pic_list;
- public $low_price;
- public $high_price;
- public $send_type;
- private $domain = '';
- /**
- * @var mixed
- */
- public $order_type;
- public $ids;
- public $is_pay;
- public $order_refund_id;//退款订单id
- public $attr;
- public $mch_cancel_id;
- public $goods_num;
- public $md_id;
- public function __construct($config = [])
- {
- parent::__construct($config);
- $this->domain = (new OptionSetting)->getCloudDomainName();
- }
- /**
- * @return array the validation rules.
- */
- public function rules()
- {
- return [
- [['name', 'certificate', 'id_card_reverse','id_card_front','logo','desc','address', 'ids'], 'string', 'max' => 255],
- [['password'], 'string', 'max'=>32],
- ['is_pay', 'default', 'value' => 1],
- [['tel','price','service_charge', 'low_price', 'high_price'],'number'],
- [['order_id','district_id','city_id','province_id','goods_id','user_id','pay_type','cash_id','type','page','limit','cloud_user_id','cloud_store_id','status','cloud_goods_id','cloud_attr_id','cloud_supplier_id', 'sort', 'origin', 'mobile', 'sale_sort', 'price_sort', 'is_distribution', 'cat_id', 'order_type', 'parent_id', 'store_id', 'send_type', 'is_pay'],'integer'],
- [['goods_info','order_no','mch_order_no','start_time','end_time','attr_id', 'user_send_express', 'user_send_express_no', 'goods_name', 'pic_list'],'string'],
- [['apply_data', 'supplier_id', 'order_refund_id'], 'safe']
- ];
- }
- //订单确认收货
- public function mchOrderConfirm(){
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $message = null;
- $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- $mch_order_confirm_url = "/cloud/mch/mchOrderConfirm";
- $mch_order_confirm_data = [];
- $mch_order_confirm_data['access_token'] = $cloud_store_token; //获取供货商的token信息
- $mch_order_confirm_data['order_id'] = $this->order_id; //订单id
- $mchOrderConfirmInfo = cloud_post($this->domain.$mch_order_confirm_url,$mch_order_confirm_data);
- $mchOrderConfirmInfo = json_decode($mchOrderConfirmInfo,true);
- if($mchOrderConfirmInfo['code'] != 0){
- $code = 1;//失败
- return $this->outPutData($code,$mchOrderConfirmInfo['msg']);
- }else{
- return $this->outPutData($mchOrderConfirmInfo['code'],$mchOrderConfirmInfo['msg']);
- }
- }
- //订单取消申请
- public function mchOrderCancel(){
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $message = null;
- $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- $mch_order_cancel_url = "/cloud/mch/mchOrderCancel";
- $mch_order_cancel_data = [];
- $mch_order_cancel_data['access_token'] = $cloud_store_token; //获取供货商的token信息
- $mch_order_cancel_data['order_id'] = $this->order_id; //订单id
- $this->order_type = 1;
- $res = $this->stbzRrfundOrderHandle($cloud_store_token);
- debug_log('订单未完成,胜天半子售后' . json_encode($res));
- if ($res['code'] === 0) {
- $mch_order_cancel_data['stbz_refund_id'] = $res['data'];
- }
- if ($res['code'] === 1) {
- return $res;
- }
- $mchOrderCancelInfo = cloud_post($this->domain.$mch_order_cancel_url,$mch_order_cancel_data);
- $mchOrderCancelInfo = json_decode($mchOrderCancelInfo,true);
- if($mchOrderCancelInfo['code'] != 0){
- $code = 1;//失败
- return $this->outPutData($code,$mchOrderCancelInfo['msg']);
- }else{
- return $this->outPutData($mchOrderCancelInfo['code'],$mchOrderCancelInfo['msg']);
- }
- }
- //供货单汇总
- public function mchOrderSummary(){
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $message = null;
- $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- $mch_order_list_url = "/cloud/mch/orderShow";
- $mch_order_list_data = [];
- $mch_order_list_data['access_token'] = $cloud_store_token; //获取供货商的token信息
- $mch_order_list_data['page'] = $this->page; //默认为 1
- $mch_order_list_data['limit'] = $this->limit; //默认20
- $mch_order_list_data['start_time'] = strtotime($this->start_time); //开始时间
- $mch_order_list_data['end_time'] = strtotime($this->end_time); //结束时间
- $mchOrderListInfo = cloud_post($this->domain.$mch_order_list_url,$mch_order_list_data);
- $mchOrderListInfo = json_decode($mchOrderListInfo,true);
- if($mchOrderListInfo['code'] != 0){
- $code = 1;//失败
- return $this->outPutData($code,$mchOrderListInfo['msg']);
- }else{
- $msg = "获取成功";
- return $this->outPutData($mchOrderListInfo['code'],$msg,$mchOrderListInfo['data']);
- }
- }
- //供货单列表
- public function mchOrderList(){
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $message = null;
- $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- $mch_order_list_url = "/cloud/mch/mchOrderList";
- $mch_order_list_data = [];
- $mch_order_list_data['access_token'] = $cloud_store_token; //获取供货商的token信息
- $mch_order_list_data['page'] = $this->page; //默认为 1
- $mch_order_list_data['limit'] = $this->limit; //默认20
- $mch_order_list_data['status'] = $this->status; //-1 全部 0 待供货商确认 1 供货商已发货 2 已确认收货
- $mch_order_list_data['mch_id'] = $this->mch_id; //商户ID
- $mch_order_list_data['start_time'] = strtotime($this->start_time); //开始时间
- $mch_order_list_data['end_time'] = strtotime($this->end_time); //结束时间
- $mchOrderListInfo = cloud_post($this->domain.$mch_order_list_url,$mch_order_list_data);
- $mchOrderListInfo = json_decode($mchOrderListInfo,true);
- if($mchOrderListInfo['code'] != 0){
- $code = 1;//失败
- return $this->outPutData($code,$mchOrderListInfo['msg']);
- }else{
- $msg = "获取成功";
- return $this->outPutData($mchOrderListInfo['code'],$msg,$mchOrderListInfo['data']);
- }
- }
- //创建供货单
- public function mchSetSubmitOrder(){
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $message = null;
- $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- $mch_set_submit_order_url = "/cloud/mch/mchSetSubmitOrder";
- $mch_set_submit_order_data = [];
- $mch_set_submit_order_data['access_token'] = $cloud_store_token; //获取供货商的token信息
- $mch_set_submit_order_data['province_id'] = $this->province_id; //省
- $mch_set_submit_order_data['city_id'] = $this->city_id; //市
- $mch_set_submit_order_data['district_id'] = $this->district_id; //区
- $mch_set_submit_order_data['address'] = $this->address; //地址详情
- $mch_set_submit_order_data['name'] = $this->name; //联系人
- $mch_set_submit_order_data['mobile'] = $this->tel; //联系电话
- $mch_set_submit_order_data['goods_info'] = $this->goods_info; //商品数据
- $mchSetSubmitOrderInfo = cloud_post($this->domain.$mch_set_submit_order_url,$mch_set_submit_order_data);
- $mchSetSubmitOrderInfo = json_decode($mchSetSubmitOrderInfo,true);
- if($mchSetSubmitOrderInfo['code'] != 0){
- $code = 1;//失败
- return $this->outPutData($code,$mchSetSubmitOrderInfo['msg']);
- }else{
- $msg = "创建成功";
- return $this->outPutData($mchSetSubmitOrderInfo['code'],$msg,$mchSetSubmitOrderInfo['data']);
- }
- }
- //订单确认收货
- public function mchPurchaseOrderConfirm($token_store_cloud_id = 0, $store_id = 0, $order_id = 0){
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $message = null;
- $cloud_store_token = get_merchant_token($token_store_cloud_id ?: $this->token_stroe_cloud_id , $store_id ?: 0, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- $platform_profit_strategy = Option::get('platform_profit_strategy', 0, 'store')['value'];
- $platform_profit_strategy = json_decode($platform_profit_strategy, true);
- $change_rate = 0;
- if (!empty($platform_profit_strategy)) {
- if (intval($platform_profit_strategy['service_charge_model'])) {
- $change_rate = 1;
- }
- }
- $mch_purchase_order_confirm_url = "/cloud/mch/mchPurchaseOrderConfirm";
- $mch_purchase_order_confirm_data = [];
- $mch_purchase_order_confirm_data['access_token'] = $cloud_store_token; //获取供货商的token信息
- $mch_purchase_order_confirm_data['order_id'] = $order_id ?: $this->order_id; //订单id
- $mch_purchase_order_confirm_data['change_rate'] = $change_rate;
- $mchPurchaseOrderConfirmInfo = cloud_post($this->domain.$mch_purchase_order_confirm_url,$mch_purchase_order_confirm_data);
- $mchPurchaseOrderConfirmInfo = json_decode($mchPurchaseOrderConfirmInfo,true);
- if($mchPurchaseOrderConfirmInfo['code'] != 0){
- $code = 1;//失败
- return $this->outPutData($code,$mchPurchaseOrderConfirmInfo['msg']);
- }else{
- return $this->outPutData($mchPurchaseOrderConfirmInfo['code'],$mchPurchaseOrderConfirmInfo['msg']);
- }
- }
- //订单取消申请
- public function mchPurchaseSendcancel(){
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $message = null;
- $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- $mch_purchase_send_cancel_url = "/cloud/mch/mchPurchaseSendcancel";
- $mch_purchase_send_cancel_data = [];
- $mch_purchase_send_cancel_data['access_token'] = $cloud_store_token; //获取供货商的token信息
- $mch_purchase_send_cancel_data['order_id'] = $this->order_id; //订单id
- $mchPurchaseSendcancelInfo = cloud_post($this->domain.$mch_purchase_send_cancel_url,$mch_purchase_send_cancel_data);
- $mchPurchaseSendcancelInfo = json_decode($mchPurchaseSendcancelInfo,true);
- if($mchPurchaseSendcancelInfo['code'] != 0){
- $code = 1;//失败
- return $this->outPutData($code,$mchPurchaseSendcancelInfo['msg']);
- }else{
- return $this->outPutData($mchPurchaseSendcancelInfo['code'],$mchPurchaseSendcancelInfo['msg']);
- }
- }
- //订单申请售后
- public function mchOrderRefundApply()
- {
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $order_refund_id = $this->order_refund_id;//商城售后订单ID
- $message = null;
- // var_dump($this->token_stroe_cloud_id, $this->store_id);die;
- $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, $this->store_id, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- $mch_order_refund_apply_url = "/cloud/mch/mchOrderRefundApply";
- $mch_order_refund_apply_data = [];
- $data = $this->apply_data;
- if (is_string($data['pic_list'])) {
- $data['pic_list'] = explode(',', $data['pic_list']);
- }
- $refund_data = [];
- if (!intval($this->order_type)) {
- $purchaseOrder = OrderTransit::findOne(['cloud_order_id' => $this->order_id]);
- if ($purchaseOrder) {
- $cancel_examine_info = json_decode($purchaseOrder->cancel_examine_info, true) ?? [];
- $cancel_examine_detail_id = array_column($cancel_examine_info, 'store_order_detail_id');
- if ($order_refund_id) {
- $orderRefund = OrderRefund::findOne($order_refund_id);
- $order_detail_id_arr = json_decode($orderRefund->order_detail_id, true);
- foreach ($order_detail_id_arr as $order_detail_id) {
- $orderDetail = OrderDetail::findOne($order_detail_id);
- if (in_array($order_detail_id, $cancel_examine_detail_id)) {
- continue;
- }
- if ($orderDetail) {
- $goods_info = json_decode($orderDetail->goods_info, true);
- $attr = json_decode($orderDetail->attr, true);
- $attr_id = array_column($attr, 'attr_id');
- sort($attr_id);
- $goods_info['attr'] = json_decode($goods_info['attr'], true);
- foreach ($goods_info['attr'] as $attr_item) {
- $item_attr_id = array_column($attr_item['attr_list'], 'attr_id');
- sort($item_attr_id);
- if (!array_diff($item_attr_id, $attr_id)) {
- $refund_data[] = [
- 'cloud_goods_id' => $goods_info['cloud_goods_id'],
- 'goods_no' => $attr_item['no'],
- 'store_order_detail_id' => $order_detail_id
- ];
- break;
- }
- }
- }
- }
- $purchaseOrder->cancel_examine_info = json_encode(array_merge($cancel_examine_info, $refund_data));
- // $purchaseOrder->cancel_examine_info = json_encode($refund_data);
- $purchaseOrder->save();
- // $refund_data = json_decode($purchaseOrder->cancel_examine_info, true);
- }
- }
- }
- $mch_order_refund_apply_data['access_token'] = $cloud_store_token; //获取供货商的token信息
- $mch_order_refund_apply_data['order_id'] = $this->order_id;
- $mch_order_refund_apply_data['refund_data'] = json_encode($refund_data);
- $mch_order_refund_apply_data['type'] = $data['type'];
- $mch_order_refund_apply_data['desc'] = $data['desc'];
- $mch_order_refund_apply_data['pic_list'] = json_encode($data['pic_list']);
- $mch_order_refund_apply_data['video'] = $data['video'];
- // $mch_order_refund_apply_data['refund_price'] = $data['refund_price'];//老板说取消自定义退款金额
- $mch_order_refund_apply_data['order_type'] = $this->order_type;
- $res = $this->stbzRrfundOrderHandle($cloud_store_token, $data);
- // $res = ['code' => 0, 'data' => 52859];
- if ($res['code'] === 0) {
- $mch_order_refund_apply_data['stbz_refund_id'] = $res['data'];
- }
- if ($res['code'] === 1) {
- return $res;
- }
- $mchOrderRefundApplyInfo = cloud_post($this->domain.$mch_order_refund_apply_url,$mch_order_refund_apply_data);
- $mchOrderRefundApplyInfo = json_decode($mchOrderRefundApplyInfo,true);
- if($mchOrderRefundApplyInfo['code'] != 0){
- $code = 1;//失败
- return $this->outPutData($code,$mchOrderRefundApplyInfo['msg']);
- }else{
- if ((int)$this->order_type === 1) {
- $PurchaseOrder = PurchaseOrder::findOne(['cloud_order_id' => $this->order_id]);
- } else {
- $PurchaseOrder = OrderTransit::findOne(['cloud_order_id' => $this->order_id]);
- }
- $PurchaseOrder->cancel_examine = 1;
- $PurchaseOrder->cancel_examine_time = (string)time();
- if (!$PurchaseOrder->save()) {
- return $this->outPutData(1, '订单状态更新失败' . json_encode($PurchaseOrder->errors));
- };
- return $this->outPutData($mchOrderRefundApplyInfo['code'],$mchOrderRefundApplyInfo['msg']);
- }
- }
- //订单申请售后(邮寄产品)
- public function mchOrderRefundSend($order_refund_detail_info = '')
- {
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $message = null;
- $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- $refund_data = [];
- if ($order_refund_detail_info) {
- $order_refund_detail_info = json_decode($order_refund_detail_info, true);
- foreach ($order_refund_detail_info as $order_refund_detail_item) {
- $orderTransit = OrderTransit::findOne(['cloud_order_id' => $this->order_id, 'is_delete' => 0]);
- $cancel_examine_info = json_decode($orderTransit->cancel_examine_info, true);
- $store_order_detail_arr = array_column($cancel_examine_info, 'store_order_detail_id');
- $result = array_search($order_refund_detail_item, $store_order_detail_arr);
- if ($result !== false) {
- $refund_data[] = $cancel_examine_info[$result];
- }
- }
- }
- $mch_order_refund_send_url = "/cloud/mch/mchOrderRefundSend";
- $mch_order_refund_send_data = [];
- $data = $this->apply_data;
- $mch_order_refund_send_data['access_token'] = $cloud_store_token; //获取供货商的token信息
- $mch_order_refund_send_data['order_id'] = $this->order_id;
- $mch_order_refund_send_data['refund_data'] = json_encode($refund_data);
- $mch_order_refund_send_data['user_send_express'] = $this->user_send_express;
- $mch_order_refund_send_data['user_send_express_no'] = $this->user_send_express_no;
- $mch_order_refund_send_data['order_type'] = $this->order_type;
- $mch_order_refund_send_data['send_pic_list'] = json_encode($this->pic_list);
- $order_data = [
- 'order_id' => $this->order_id,
- 'status' => $this->order_type
- ];
- $order_url = "/user/getRefundOrder";
- $domain = $this->domain;
- $orderInfo = cloud_post($domain. $order_url,
- $order_data);
- $orderInfo = json_decode($orderInfo,true);
- // if(input_params('debug')){
- // var_dump($orderInfo);die;
- // }
- if($orderInfo['code'] == 0){
- if ($orderInfo['data']['stbz_refund_id']) {
- $urls = [];
- $pic_lists = $this->pic_list;
- if (!empty($pic_lists)) {
- $order_url = "/user/getOrder";
- $orderInfo = cloud_post($domain. $order_url,
- $order_data);
- $orderInfo = json_decode($orderInfo,true);
- if($orderInfo['code'] !== 0){
- return $orderInfo['msg'];
- }
- //上传凭证
- foreach ($pic_lists as &$pic_list) {
- $save_path = $this->saveTempImage($pic_list);
- $image = file_get_contents($save_path);
- $pic_list = base64_encode($image);
- }
- $params = array_merge($order_data, [
- 'pictures' => $pic_lists
- ]);
- $checkData = \stbz_client()->getApiResponse('post', '/v2/afterSale/picture', $params);
- $checkData = \json_decode($checkData, true);
- if ((int)$checkData['code'] === 1) {
- $urls = $checkData['data']['urls'];
- }
- }
- $company = '';
- $logistic = \stbz_client()->getApiResponse('get', '/v2/logistic/firms');
- $returnData = \json_decode($logistic, true);
- if ($returnData['code'] == 1) {
- if($returnData['data']){
- foreach($returnData['data'] as $gitem){
- if($this->user_send_express == $gitem['name']){
- $company = $gitem['code'];
- break;
- }
- }
- }
- }
- $params = [
- 'id' => $orderInfo['data']['stbz_refund_id'],
- 'sn' => $this->user_send_express_no,
- 'company' => $company,
- 'vouchers' => $urls
- ];
- $checkData = \stbz_client()->getApiResponse('patch', '/v2/afterSale', $params);
- $checkData = \json_decode($checkData, true);
- debug_log('邮寄产品' . json_encode($params) . json_encode($checkData));
- if ((int)$checkData['code'] !== 1) {
- return [
- 'code' => 1,
- 'msg' => $checkData['msg'],
- 'd' => [$checkData, $params],
- ];
- }
- }
- }
- $mchOrderRefundSendInfo = cloud_post($this->domain.$mch_order_refund_send_url,$mch_order_refund_send_data);
- $mchOrderRefundSendInfo = json_decode($mchOrderRefundSendInfo,true);
- if($mchOrderRefundSendInfo['code'] != 0){
- $code = 1;//失败
- return $this->outPutData($code,$mchOrderRefundSendInfo['msg']);
- }else{
- return $this->outPutData($mchOrderRefundSendInfo['code'],$mchOrderRefundSendInfo['msg']);
- }
- }
- //售后列表
- public function mchOrderRefundList()
- {
- try {
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $message = null;
- $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- $mch_order_refund_send_url = "/cloud/mch/mchOrderRefundList";
- $mch_order_refund_send_data['access_token'] = $cloud_store_token;
- $mch_order_refund_send_data['id'] = $this->order_id;
- $mch_order_refund_send_data['page'] = $this->page; //默认为 1
- $mch_order_refund_send_data['limit'] = $this->limit; //默认20
- $mch_order_refund_send_data['status'] = $this->status;
- $mchOrderRefundSendInfo = cloud_post($this->domain.$mch_order_refund_send_url,$mch_order_refund_send_data);
- $mchOrderRefundSendInfo = json_decode($mchOrderRefundSendInfo,true);
- if($mchOrderRefundSendInfo['code'] != 0){
- $code = 1;//失败
- return $this->outPutData($code,$mchOrderRefundSendInfo['msg']);
- }else{
- return $this->outPutData($mchOrderRefundSendInfo['code'],$mchOrderRefundSendInfo['msg'],$mchOrderRefundSendInfo['data']);
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function mchGetOrderGoods()
- {
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $message = null;
- $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- $mch_purchase_order_list_url = "/cloud/mch/mchGetOrderGoods";
- $mch_purchase_order_list_data = [];
- $mch_purchase_order_list_data['access_token'] = $cloud_store_token;//获取供货商的token信息
- $mch_purchase_order_list_data['status'] = $this->status;//-1 全部 0 待供货商确认 1 供货商已发货 2 已确认收货
- //$mch_purchase_order_list_data['mch_id'] = $this->mch_id; //商户ID
- if($this->page) $mch_purchase_order_list_data['page'] = $this->page; //默认为 1
- if($this->limit) $mch_purchase_order_list_data['limit'] = $this->limit; //默认20
- if($this->start_time) $mch_purchase_order_list_data['start_time'] = strtotime($this->start_time); //开始时间
- if($this->end_time) $mch_purchase_order_list_data['end_time'] = strtotime($this->end_time); //结束时间
- if($this->order_no) $mch_purchase_order_list_data['order_no'] = $this->order_no; //订单号
- if($this->origin) $mch_purchase_order_list_data['origin'] = $this->origin; //订单号
- if($this->mobile) $mch_purchase_order_list_data['mobile'] = $this->mobile; //订单号
- if($this->goods_name) $mch_purchase_order_list_data['goods_name'] = $this->goods_name; //订单号
- $mchPurchaseOrderListInfo = cloud_post($this->domain.$mch_purchase_order_list_url,$mch_purchase_order_list_data);
- $mchPurchaseOrderListInfo = json_decode($mchPurchaseOrderListInfo,true);
- if($mchPurchaseOrderListInfo['code'] != 0){
- $code = 1;//失败
- return $this->outPutData($code,$mchPurchaseOrderListInfo['msg']);
- }else{
- $msg = "获取成功";
- return $this->outPutData($mchPurchaseOrderListInfo['code'],$msg,$mchPurchaseOrderListInfo['data']);
- }
- }
- //转单列表(无页码限制)
- public function getPurchaseOrderList(){
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $order_id = $this->order_id;
- $orderTransit = OrderTransit::find()->where(['order_id' => $order_id])->select('cloud_order_id')->asArray()->all();
- $cloud_order_id = array_column($orderTransit, 'cloud_order_id');
- $order_data_url = "/user/getPurchaseOrder";
- $order_data = [];
- if (!$cloud_order_id) {
- return [
- 'code' => 1,
- 'msg' => '未查找到订单信息'
- ];
- }
- $order_data['order_ids'] = implode(',', $cloud_order_id);
- $order_data_info = cloud_post($this->domain . $order_data_url, $order_data);
- $order_data_info = json_decode($order_data_info, true);
- if($order_data_info['code'] != 0){
- $code = 1;//失败
- return $this->outPutData($code, $order_data_info['msg']);
- }else{
- $msg = "获取成功";
- return $this->outPutData($order_data_info['code'], $msg, $order_data_info['data']);
- }
- }
- //转单列表
- public function mchPurchaseOrderList(){
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $message = null;
- $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- $mch_purchase_order_list_url = "/cloud/mch/mchPurchaseOrderList";
- $mch_purchase_order_list_data = [];
- $mch_purchase_order_list_data['access_token'] = $cloud_store_token;//获取供货商的token信息
- $mch_purchase_order_list_data['status'] = $this->status;//-1 全部 0 待供货商确认 1 供货商已发货 2 已确认收货
- //$mch_purchase_order_list_data['mch_id'] = $this->mch_id; //商户ID
- if($this->page) $mch_purchase_order_list_data['page'] = $this->page; //默认为 1
- if($this->limit) $mch_purchase_order_list_data['limit'] = $this->limit; //默认20
- if($this->start_time) $mch_purchase_order_list_data['start_time'] = strtotime($this->start_time); //开始时间
- if($this->end_time) $mch_purchase_order_list_data['end_time'] = strtotime($this->end_time); //结束时间
- if($this->order_no) $mch_purchase_order_list_data['order_no'] = $this->order_no; //订单号
- if($this->origin) $mch_purchase_order_list_data['origin'] = $this->origin; //订单号
- if($this->mobile) $mch_purchase_order_list_data['mobile'] = $this->mobile; //订单号
- if($this->goods_name) $mch_purchase_order_list_data['goods_name'] = $this->goods_name; //订单号
- if($this->mch_order_no){
- $orderIds = Order::find()->where(['store_id' => $this->store_id])->andWhere(['like', 'order_no', $this->mch_order_no])->select('id')->column();
- if($orderIds){
- $mch_purchase_order_list_data['mch_order_ids'] = implode(',', $orderIds);
- }
- }
- $mchPurchaseOrderListInfo = cloud_post($this->domain.$mch_purchase_order_list_url,$mch_purchase_order_list_data);
- $mchPurchaseOrderListInfo = json_decode($mchPurchaseOrderListInfo,true);
- if($mchPurchaseOrderListInfo['code'] != 0){
- $code = 1;//失败
- return $this->outPutData($code,$mchPurchaseOrderListInfo['msg']);
- }else{
- $msg = "获取成功";
- foreach ($mchPurchaseOrderListInfo['data']['list'] as &$item) {
- //TODO 胜天半子
- $token = get_merchant_token();
- $result = $this->stbzRrfundOrderHandleInfo($token, $item['id']);
- $reasonsType = null;
- $returnType = null;
- $packageType = null;
- $serviceType = null;
- if ($result['code'] === 0) {
- $reasonsType = $result['data']['reasonsType'];
- $returnType = $result['data']['returnType'];
- $packageType = $result['data']['packageType'];
- $serviceType = $result['data']['serviceType'];
- }
- $item['reasonsType'] = $reasonsType;
- $item['returnType'] = $returnType;
- $item['packageType'] = $packageType;
- $item['serviceType'] = $serviceType;
- $item['is_can_cancel'] = 0;
- $item['order'] = Order::findOne($item['platform_order_no']);
- //
- $order_transit = OrderTransit::findOne(['cloud_order_id' => $item['id'], 'is_delete' => 0]);
- if ($order_transit) {
- $order_refund = OrderRefund::findOne(['order_id' => $order_transit->order_id]);
- if (!$order_refund) {
- $open = false;
- //显示可售后按钮 且 用户未申请售后的不显示取消售后按钮
- if ($item['is_show_refund'] === 1) {
- $open = true;
- $item['is_show_refund'] = 0;
- }
- $order = Order::findOne($order_transit->order_id);
- if ($order) {
- if (intval($order->trade_status) === 1 && $open && intval($item['is_show_refund']) === 0 && in_array($item['status'], [0, 1])) {
- $item['is_can_cancel'] = 1;
- if (intval($item['is_cancel']) === 1) {
- $item['is_can_cancel'] = 0;
- $item['is_show_refund'] = 0;
- $item['status'] = '-1';
- }
- }
- }
- }
- }
- }
- return $this->outPutData($mchPurchaseOrderListInfo['code'],$msg,$mchPurchaseOrderListInfo['data']);
- }
- }
- //检测转单
- public function mchGetPurchaseOrder($type = 0){
- if(!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $message = null;
- $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, $this->store_id, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- $order = Order::findOne($this->order_id);
- if (empty($order)) {
- return [
- 'code' => 1,
- 'msg' => '订单不存在'
- ];
- }
- if (intval($order->apply_delete) === 1) {
- return [
- 'code' => 1,
- 'msg' => '订单正在申请取消 不可转单'
- ];
- }
- if (intval($order->is_offline) === 1) {
- //如果订单自提就发货到商家
- $store = Store::findOne($order->store_id);
- if ($store) {
- $this->province_id = $store->province_id;
- $this->city_id = $store->city_id;
- $this->district_id = $store->district_id;
- $this->address = $store->address;
- $this->name = $store->name;
- $this->tel = $store->contact_tel;
- }
- if ($order->md_id > 0) {
- $md = Md::findOne(['id' => $order->md_id, 'is_delete' => 0]);
- if ($md) {
- $this->province_id = $md->province;
- $this->city_id = $md->city;
- $this->district_id = $md->district;
- $this->address = $md->address;
- $this->name = $md->name;
- $this->tel = $md->mobile;
- }
- }
- }
- $OrderDetail = OrderDetail::find()->alias('od')->where(['od.order_id' => $this->order_id])
- ->leftJoin(['g' => Goods::tableName()], 'od.goods_id = g.id')
- ->select('od.id, od.id order_d_id, od.goods_id, od.num, od.total_price price, od.pic pic_url, od.attr, g.attr attrs,
- g.name, g.is_front_delivery, g.is_front_centralize, g.cloud_supplier_id, g.cloud_goods_id')
- ->asArray()->all();
- $checkMdFrontBindArr = [];
- $checkMdFrontBindArr['send'] = 'express';
- if ($order->is_offline) {
- $checkMdFrontBindArr['send'] = 'shop';
- }
- if ($order->is_delivery) {
- $checkMdFrontBindArr['send'] = 'delivery';
- }
- $goods_info = [];
- $price = 0;
- foreach ($OrderDetail as $index => $item) {
- $item['id'] = $item['goods_id'];
- $attr = array_column(json_decode($item['attr'], true), 'attr_id');
- sort($attr);
- $attrs = json_decode($item['attrs'], true);
- foreach ($attrs as $value) {
- $goods_attr = array_column($value['attr_list'], 'attr_id');
- sort($goods_attr);
- if (!array_diff($goods_attr, $attr)) {
- $item['attr'] = json_encode($value);
- continue;
- }
- }
- unset($OrderDetail[$index]['attrs'], $OrderDetail[$index]['goods_id']);
- $goods = CloudGoodsBind::find()->where(['goods_id' => $item['id']])->select('cloud_goods_id id, cloud_supplier_id')->asArray()->one();
- if (!empty($goods)) {
- $item['id'] = $goods['id'];
- $goods_info[] = $item;
- $order_detail_ids[] = $item['order_d_id'];
- $price += $item['price'];
-
- $supplier = \app\models\Supplier::findOne(['cloud_supplier_id' => $goods['cloud_supplier_id']]);
- if($supplier){
- $store_goods = Goods::findOne($item['goods_id']);
- \app\modules\admin\models\jushuitan\JuShuiTanForm::syncSupplierJstGoodsQty($supplier['id'], $goods['id'], $this->store_id, $store_goods);
- } else {
- return [
- 'code' => 1,
- 'msg' => '供货商已经被删除'
- ];
- }
- }
- $item_attr = json_decode($item['attr'], true);
- $checkMdFrontBindArr['goods_list'][] = [
- 'goods_name' => $item['name'],
- 'is_front_delivery' => $item['is_front_delivery'],
- 'cloud_supplier_id' => $item['cloud_supplier_id'],
- 'cloud_goods_id' => $item['cloud_goods_id'],
- 'goods_id' => $item['goods_id'],
- 'goods_no' => $item_attr['no'],
- 'is_front_centralize' => $item['is_front_centralize']
- ];
- }
- $checkMdFrontBindResult = OrderTransit::checkMdFrontBind($checkMdFrontBindArr, $order->md_id);
- if ($checkMdFrontBindResult['code'] !== 0) {
- return $checkMdFrontBindResult;
- }
- $mch_get_purchase_order_url = "/cloud/mch/mchSetPurchaseOrder";
- $mch_get_purchase_order_data = [];
- $mch_get_purchase_order_data['access_token'] = $cloud_store_token; //获取供货商的token信息
- $mch_get_purchase_order_data['province_id'] = $this->province_id; //省
- $mch_get_purchase_order_data['city_id'] = $this->city_id; //市
- $mch_get_purchase_order_data['district_id'] = $this->district_id; //区
- $mch_get_purchase_order_data['address'] = $this->address; //地址详情
- $mch_get_purchase_order_data['name'] = $this->name; //联系人
- $mch_get_purchase_order_data['mobile'] = $this->tel; //联系电话
- $mch_get_purchase_order_data['goods_info'] = json_encode($goods_info); //商品数据
- $mch_get_purchase_order_data['platform_order_no'] = $this->order_id; //订单id
- $mch_get_purchase_order_data['type'] = 0; //创建
- $mch_get_purchase_order_data['is_use_platform_mch'] = $order->is_use_platform_mch; //是否是平台收款账户收款
- $mchGetPurchaseOrderInfo = cloud_post($this->domain.$mch_get_purchase_order_url,$mch_get_purchase_order_data);
- $mchGetPurchaseOrderInfo = json_decode($mchGetPurchaseOrderInfo,true);
- if($mchGetPurchaseOrderInfo['code'] != 0){
- $code = 1;//失败
- $order->trans_status = 2;
- $order->trans_error = $mchGetPurchaseOrderInfo['msg'];
- $order->save();
- return $this->outPutData($code,$mchGetPurchaseOrderInfo['msg']);
- }else{
- $total_price = 0;
- foreach($mchGetPurchaseOrderInfo['data'] as $key=>$val){
- $orderTransitInfo = OrderTransit::find()->where(['order_id'=>$val['platform_order_no'],'cloud_supplier_id'=>$val['supplier']['id'],'is_delete'=>0])->one();
- if($orderTransitInfo){
- $mchGetPurchaseOrderInfo['data'][$key]['cloud_order_status'] = true; //已转单成功
- }else{
- $mchGetPurchaseOrderInfo['data'][$key]['cloud_order_status'] = false; //未转单
- }
- $total_price += $val['pay_price'];
- }
- //检测未进件时候使用
- if ($type) {
- if ($order && $order->is_use_platform_mch) {
- $store = Store::findOne($order->store_id);
- if ($store) {
- $store_price = Store::getMoney($store);
- $profit = bcmul($order->pay_price, ($store->transfer_profit / 100), 3);
- $order_pay_price = bcsub($order->pay_price, $profit, 3);
- if ($order_pay_price < $total_price) {
- return [
- 'code' => 2,
- 'msg' => '商品价格可抵扣金额不足'
- ];
- }
- }
- }
- }
- $msg = "获取成功";
- return $this->outPutData($mchGetPurchaseOrderInfo['code'],$msg,$mchGetPurchaseOrderInfo['data']);
- }
- }
- //创建转单
- public function mchSetPurchaseOrder(){
- checkReplay($this->order_id);
- $order = Order::findOne($this->order_id);
- $is_use_platform_mch = $order->is_use_platform_mch;
- // 主要为供应链系统
- // 赤店新增逻辑 未进件情况下转单需要从商城余额抵扣金额 订单转单的时候不扣除云仓余额,修改给商家结算的金额即可
- // 转单的 如果商城订单价格没云仓订单价格高 商家可结算金额不足 那给商家结算时需要提示充值云仓余额
- // 调用此接口时先调用检测 检测提示可抵扣金额不足 就直接以已进件的方式进行扣除云仓金额
- $type = 0;
- $self_mini = \app\models\Option::get('self_mini', $this->store_id ?? get_store_id(), 'store', 0)['value'];
- if ($is_use_platform_mch && \Yii::$app->prod_is_dandianpu() && !$self_mini) {
- $type = 1;
- }
- if (!$type) {
- $is_use_platform_mch = 0;
- }
- $result = $this->mchGetPurchaseOrder($type);
- if ($result['code'] !== 0) {
- if ($is_use_platform_mch && $result['code'] === 2) {
- $is_use_platform_mch = 0;
- } else {
- return $result;
- }
- }
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $order_detail_ids = []; //对应order_detail id
- $OrderDetail = OrderDetail::find()->alias('od')->where(['od.order_id' => $this->order_id])
- ->leftJoin(['g' => Goods::tableName()], 'od.goods_id = g.id')
- ->select('od.id order_d_id, od.goods_id, od.num, od.total_price price, od.pic pic_url, od.attr, g.attr attrs, od.goods_name')
- ->asArray()->all();
- foreach ($OrderDetail as $order_detail_index => $order_detail_item) {
- $orderGoodsCancelNum = OrderGoodsCancel::find()->where([
- 'order_id' => $this->order_id,
- 'order_detail_id' => $order_detail_item['order_d_id'],
- 'status' => [
- OrderGoodsCancel::STATUS_PASS,
- OrderGoodsCancel::STATUS_PAY,
- OrderGoodsCancel::STATUS_PAY_FAIL
- ]
- ])->groupBy('order_detail_id')->select('SUM(num)')->scalar();
- $order_detail_item['num'] = bcsub($order_detail_item['num'], $orderGoodsCancelNum);
- if ($order_detail_item['num'] <= 0) {
- unset($OrderDetail[$order_detail_index]);
- }
- }
- $OrderDetail = array_values($OrderDetail);
- $goods_info = [];
- // $supplier_arr = [];
- $price = 0;
- foreach ($OrderDetail as $index => $item) {
- //购买的规格
- $attr = array_column(json_decode($item['attr'], true), 'attr_id');
- sort($attr);
- //商品规格
- $attrs = json_decode($item['attrs'], true);
- foreach ($attrs as $value) {
- $goods_attr = array_column($value['attr_list'], 'attr_id');
- sort($goods_attr);
- if (!array_diff($goods_attr, $attr)) {
- $item['attr'] = json_encode($value);
- continue;
- }
- }
- unset($OrderDetail[$index]['attrs']);
- $goods = CloudGoodsBind::find()->where(['goods_id' => $item['goods_id']])->select('cloud_goods_id id, cloud_supplier_id')->asArray()->one();
- // $supplier_arr[] = $goods['cloud_supplier_id'];
- if (!empty($goods)) {
- $item['id'] = $goods['id'];
- $goods_info[] = $item;
- $order_detail_ids[] = $item['order_d_id'];
- $price += $item['price'];
-
- $supplier = \app\models\Supplier::findOne(['cloud_supplier_id' => $goods['cloud_supplier_id']]);
- if($supplier){
- $store_goods = Goods::findOne($item['goods_id']);
- \app\modules\admin\models\jushuitan\JuShuiTanForm::syncSupplierJstGoodsQty($supplier['id'], $goods['id'], $this->store_id, $store_goods);
- } else {
- return [
- 'code' => 1,
- 'msg' => '供货商已经被删除'
- ];
- }
- }
- }
- //一个订单一个供货商只能创建一次转单
- $transitInfo = OrderTransit::find()->where(['order_id'=>$this->order_id, 'is_delete'=>0])->one();
- if($transitInfo) {
- $code = 1;//失败
- $msg = "订单已转单";
- return $this->outPutData($code,$msg);
- }
- //验证订单数据
- $orderInfo = Order::find()->with(['detail' => function ($query) {
- $query->select("id,goods_id,attr,order_id,num")->asArray();
- }])->where(['is_delete' => 0, 'id' => $this->order_id])->asArray()->one();
- if(!$orderInfo || !$orderInfo['detail']){
- $code = 1;//失败
- $msg = "订单数据有误";
- return $this->outPutData($code,$msg);
- }
- $address_data = json_decode($order->address_data, true);
- $this->address = $address_data['detail'];
- $message = null;
- $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, $this->store_id, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- $spu = [];
- foreach ($goods_info as $goods) {
- $goods_url = "/goods/getAttrParams";
- $param = [
- 'id' => $goods['id']
- ];
- //请求接口
- $domain = $this->domain;
- $goodsInfo = cloud_post($domain . $goods_url, $param);
- $goodsInfo = json_decode($goodsInfo, true);
- if ((int)$goodsInfo['code'] !== 0) {
- return [
- 'code' => 1,
- 'msg' => $goodsInfo['msg']
- ];
- }
- if ($goodsInfo['data']['stbz_goods_id'] > 0) {
- $attr = json_decode($goods['attr'], true);
- $spu[] = [
- 'number' => $goods['num'],
- 'sku' => $attr['no'],
- ];
- }
- }
- if (intval($order->is_offline) === 1) {
- //如果订单自提就发货到商家
- $store = Store::findOne($order->store_id);
- if ($store) {
- $this->province_id = $store->province_id;
- $this->city_id = $store->city_id;
- $this->district_id = $store->district_id;
- $this->address = $store->address;
- }
- if ($order->md_id > 0) {
- $md = Md::findOne(['id' => $order->md_id, 'is_delete' => 0]);
- if ($md) {
- $this->province_id = $md->province;
- $this->city_id = $md->city;
- $this->district_id = $md->district;
- $this->address = $md->address;
- }
- }
- }
- if (intval($order->is_delivery) === 1) {
- //如果订单自提就发货到商家
- $store = Store::findOne($order->store_id);
- if ($store) {
- $this->province_id = $store->province_id;
- $this->city_id = $store->city_id;
- $this->district_id = $store->district_id;
- $this->address = $store->address;
- }
- }
- if (!empty($spu)) {
- [$province, $city, $district] = District::find()->where(['id' => [$this->province_id, $this->city_id, $this->district_id]])
- ->select('name')->column();
- if (empty($province) || empty($city) || empty($district) || empty($this->address)) {
- return [
- 'code' => 1,
- 'msg' => '地址信息错误'
- ];
- }
- $params = [
- 'spu' => $spu,
- 'address' => [
- 'consignee' => $orderInfo['name'],
- 'phone' => $orderInfo['mobile'],
- 'province' => $province,
- 'city' => $city,
- 'area' => $district,
- 'street' => '',
- 'description' => $this->address,
- ]
- ];
- debug_log('胜天半子商品信息' . json_encode($params));
- $checkData = \stbz_client()->getApiResponse('post', '/v2/order/beforeCheck', $params);
- $checkData = \json_decode($checkData, true);
- debug_log('胜天半子下单检测' . json_encode($checkData));
- if ($checkData['code'] != 1) {
- $order->trans_status = 2;
- $order->trans_error = $checkData['msg'];
- $order->save();
- return $checkData;
- }
- }
- $platform_profit_strategy = Option::get('platform_profit_strategy', 0, 'store')['value'];
- $platform_profit_strategy = json_decode($platform_profit_strategy, true);
- $change_rate = 0;
- if (!empty($platform_profit_strategy)) {
- if (intval($platform_profit_strategy['service_charge_model'])) {
- $change_rate = 1;
- }
- }
- $mch_set_purchase_order_url = "/cloud/mch/mchSetPurchaseOrder";
- $mch_set_purchase_order_data = [];
- $mch_set_purchase_order_data['access_token'] = $cloud_store_token; //获取供货商的token信息
- $mch_set_purchase_order_data['province_id'] = $this->province_id; //省
- $mch_set_purchase_order_data['city_id'] = $this->city_id; //市
- $mch_set_purchase_order_data['district_id'] = $this->district_id; //区
- $mch_set_purchase_order_data['address'] = $this->address; //地址详情
- $mch_set_purchase_order_data['name'] = $this->name; //联系人
- $mch_set_purchase_order_data['mobile'] = $this->tel; //联系电话
- $mch_set_purchase_order_data['goods_info'] = json_encode($goods_info); //商品数据
- $mch_set_purchase_order_data['platform_order_no'] = $this->order_id; //订单id
- $mch_set_purchase_order_data['type'] = 1; //创建
- $mch_set_purchase_order_data['user_id'] = $order->user_id; //用户id
- $mch_set_purchase_order_data['order_no'] = $order->order_no; //订单号
- $mch_set_purchase_order_data['platform_md_id'] = $order->md_id; //订单号
- $mch_set_purchase_order_data['is_use_platform_mch'] = $is_use_platform_mch; //是否是平台收款账户收款
- $mch_set_purchase_order_data['change_rate'] = $change_rate;
- $mchSetPurchaseOrderInfo = cloud_post($this->domain.$mch_set_purchase_order_url,$mch_set_purchase_order_data);
- debug_log('转单' . $mchSetPurchaseOrderInfo);
- $mchSetPurchaseOrderInfo = json_decode($mchSetPurchaseOrderInfo,true);
- if($mchSetPurchaseOrderInfo['code'] != 0){
- $order->trans_status = 2;
- $order->trans_error = $mchSetPurchaseOrderInfo['msg'];
- $order->save();
- $code = 1;//失败
- return $this->outPutData($code, $mchSetPurchaseOrderInfo['msg']);
- } else {
- $arr = [];
- if ($mchSetPurchaseOrderInfo['data']['order_data']) {
- $order_detail = OrderDetail::find()->alias('od')
- ->leftJoin(['g' => Goods::tableName()], 'od.goods_id = g.id')
- ->where(['od.order_id' => $order->id])->select('od.id')->asArray()->all();
- $order_detail_ids = array_column($order_detail, 'id');
- $total_price = 0;
- foreach ($mchSetPurchaseOrderInfo['data']['order_data'] as $index => $order_data) {
- foreach ($order_data as $key => $value) {
- $cloud_order_goods_id = array_column($value['goods_list'], 'id');
- $order_detail_ = OrderDetail::find()->alias('od')
- ->leftJoin(['g' => Goods::tableName()], 'od.goods_id = g.id')
- ->where(['od.order_id' => $order->id, 'g.cloud_goods_id' => $cloud_order_goods_id])
- ->select('od.id, g.name, g.cloud_goods_id')->asArray()->all();
- $order_detail_ids_ = array_column($order_detail_, 'id');
- $orderTransit = new OrderTransit();
- $orderTransit->province_id = $this->province_id;
- $orderTransit->city_id = $this->city_id;
- $orderTransit->district_id = $this->district_id;
- $orderTransit->address = $this->address;
- $orderTransit->name = $this->name;
- $orderTransit->tel = $this->tel;
- $orderTransit->goods_info = json_encode($goods_info);
- $orderTransit->order_id = $order->id;
- $orderTransit->cloud_supplier_id = $value['supplier_id'];
- $orderTransit->created_at = time();
- $orderTransit->order_detail_ids = json_encode($order_detail_ids);
- $orderTransit->cloud_order_id = $value['id'];
- $orderTransit->cloud_order_no = $value['order_no'];
- $orderTransit->deleted_at = 0;
- $orderTransit->order_price = $value['pay_price'];
- $orderTransit->is_use_platform_mch = $is_use_platform_mch;
- $orderTransit->store_id = $order->store_id;
- $orderTransit->goods_send_type = $value['goods_send_type'];
- $orderTransit->save();
- debug_log(json_encode($orderTransit->errors));
- $supplier = Supplier::findOne(['cloud_supplier_id' => $value['supplier_id'], 'is_delete' => 0]);
- if (in_array(intval($value['goods_send_type']), [5, 6])) {
- //获取商城供货商
- $supplierAgentFrontBind = AgentFrontBind::find()->where([
- 'is_delete' => 0,
- 'status' => 1,
- 'type' => AgentFrontBind::TYPE_SUPPLIER,
- 'type_id' => $supplier->id
- ])->select('front_agent_admin_id')->column();
- debug_log('供货商绑定仓库');
- debug_log($value['supplier_id']);
- debug_log($supplierAgentFrontBind);
- $md_id = $order->md_id;
- if ($md_id <= 0 || !$order->is_offline) {
- $mdAgentFrontBind = AgentFrontBind::find()->where([
- 'type' => AgentFrontBind::TYPE_STORE,
- 'type_id' => $order->store_id,
- 'is_delete' => 0,
- 'status' => 1,
- 'front_agent_admin_id' => $supplierAgentFrontBind,
- 'is_cloud_delivery' => AgentFrontBind::IS_CLOUD_DELIVERY_TRUE
- ])->select('md_id, front_agent_admin_id')->orderBy('md_id ASC')
- ->asArray()->one();
- if (!$mdAgentFrontBind) {
- $address_data = json_decode($order->address_data ?: '', true) ?: [];
- $mdAgentFrontBindArr = AgentFrontBind::find()->where([
- 'type' => AgentFrontBind::TYPE_STORE,
- 'type_id' => $order->store_id,
- 'is_delete' => 0,
- 'status' => 1,
- 'front_agent_admin_id' => $supplierAgentFrontBind
- ])->select('md_id, front_agent_admin_id')->orderBy('md_id ASC')
- ->asArray()->all();
- $mdAgentFrontBind = $mdAgentFrontBindArr[0];
- debug_log(['mdAgentFrontBindArr' => $mdAgentFrontBindArr, 'address' => $address_data]);
- if (!empty($address_data)) {
- //如果订单中存在经纬度信息 就查询经纬度距离最近的仓库
- $front_agent_admin_id_arr = array_column($mdAgentFrontBindArr, 'front_agent_admin_id');
- debug_log(['front_agent_admin_id_arr' => $front_agent_admin_id_arr]);
- $admin_table = Admin::tableName();
- $admin_id_str = implode(',', $front_agent_admin_id_arr);
- $sql = "SELECT id, acos(cos({$address_data['latitude']}*pi()/180 )*cos(lat*pi()/180)*cos({$address_data['longitude']}*pi()/180 -lng*pi()/180)+sin({$address_data['latitude']}*pi()/180 )*sin(lat*pi()/180))*6370996.81 as
- distance FROM {$admin_table} WHERE `is_delete`=0 AND `id`in ({$admin_id_str}) ORDER BY distance ASC, id ASC";
- $admin_info = \Yii::$app->db->createCommand($sql)->queryOne();
- debug_log(['admin_info_arr' => \Yii::$app->db->createCommand($sql)->queryAll()]);
- if ($admin_info) {
- //如果能查询到就获得到一个仓库信息 获取不到就还是使用门店正序排列查询
- $mdAgentFrontBind = AgentFrontBind::find()->where([
- 'type' => AgentFrontBind::TYPE_STORE,
- 'type_id' => $order->store_id,
- 'is_delete' => 0,
- 'status' => 1,
- 'front_agent_admin_id' => $admin_info['id']
- ])->select('md_id, front_agent_admin_id')->asArray()->one();
- }
- }
- }
- } else {
- $mdAgentFrontBind = AgentFrontBind::find()->where([
- 'md_id' => $md_id,
- 'is_delete' => 0,
- 'status' => 1,
- 'front_agent_admin_id' => $supplierAgentFrontBind
- ])->select('md_id, front_agent_admin_id')
- ->orderBy('md_id ASC')->asArray()->one();
- }
- if (!$mdAgentFrontBind) {
- debug_log('未找到供货商与门店绑定的同一个仓库');
- goto mdAgentFrontBindEND;
- }
- //扣减仓库库存
- foreach ($value['goods_list'] as $cloud_goods_item) {
- $cloud_goods_attr = json_decode($cloud_goods_item['attr'], true);
- $cloud_goods_attr_id = array_column($cloud_goods_attr['attr_list'], 'attr_id');
- sort($cloud_goods_attr_id);
- //转单后扣减仓库库存 调用供货商修改库存接口 数量传0用作更新商城库存
- $erpInventoryArray = AgentFrontErpInventory::find()->where(['goods_id' => $cloud_goods_item['id'], 'front_agent_admin_id' => $mdAgentFrontBind['front_agent_admin_id']])
- ->asArray()->all();
- foreach ($erpInventoryArray as $erpInventory) {
- $attr_ids = explode(',', $erpInventory['attr_ids']);
- sort($attr_ids);
- if (!array_diff($cloud_goods_attr_id, $attr_ids)) {
- $ei = AgentFrontErpInventory::findOne($erpInventory['id']);
- if ($ei) {
- AgentFrontErpInventory::logSave($ei, -$cloud_goods_item['num'], AgentFrontErpInventoryLog::LOG_TYPE_ORDER, $value['id'], $value['order_no']);
- }
- break;
- }
- }
- }
- if (intval($value['goods_send_type']) === 5) {
- $delivery_info = DeliveryInfo::findOne(['order_no' => $order->order_no]);
- if (!$delivery_info) {
- //不存在应该是门店自提订单
- $goods_list = [];
- foreach ($order_detail_ as $order_goods_item) {
- foreach ($value['goods_list'] as $cloud_goods_item) {
- if ($order_goods_item['cloud_goods_id'] == $cloud_goods_item['id']) {
- $attr = json_decode($cloud_goods_item['attr'], true);
- $goods_list[] = [
- 'name' => $order_goods_item['goods_name'],
- 'price' => $attr['price'],
- 'pic' => $attr['pic'],
- 'num' => $attr['num'],
- 'weight' => $attr['weight'],
- ];
- }
- }
- }
- $type = LocalDeliveryFreight::TYPE_LOCAL;
- //将门店地址传入
- $mdInfo = Md::findOne($order->md_id);
- $address = [
- 'latitude' => $mdInfo->latitude,
- 'longitude' => $mdInfo->longitude
- ];
- $getFreight = LocalDeliveryFreight::getFreight($order->store_id, $address, '', $type, $goods_list, 0, DeliveryInfo::IS_FRONT_DELIVERY_YSE, $mdAgentFrontBind['front_agent_admin_id']);
- if ($getFreight['code'] != 0) {
- return $getFreight;
- }
- $freight = $getFreight['data'];
- // 存预下单表
- $delivery_info = new DeliveryInfo();
- $delivery_info->store_id = $order->store_id;
- $delivery_info->order_no = $order->order_no;
- $delivery_info->fee = $freight;
- $delivery_info->delivery_type = 0;
- $delivery_info->created_at = time();
- $delivery_info->is_local = DeliveryInfo::IS_LOCAL_YSE;
- $delivery_info->is_store_delivery_type = 0;
- $delivery_info->is_front_delivery = DeliveryInfo::IS_FRONT_DELIVERY_YSE;//是否是仓库配送订单
- $serial_num = DeliveryInfo::find()->where(['>', 'created_at', strtotime(date('Y-m-d'))])->orderBy('serial_num desc')->asArray()->one()['serial_num'];
- $delivery_info->serial_num = $serial_num > 0 ? $serial_num + 1 : 1;
- if (!$delivery_info->save()) {
- debug_log('云仓转单后仓库配送添加同城配送订单失败' . json_encode($delivery_info->errors, JSON_UNESCAPED_UNICODE));
- goto mdAgentFrontBindEND;
- }
- $setting = json_decode(Option::get(OptionSetting::LOCAL_DELIVERY_SETTING, 0, '', '{}')['value'], true);
- $delivery_time = date('Y-m-d H:i:s', time() + 90 * 60);
- if (!empty($setting['default_time']) && !empty($setting['default_time']['value'])) {
- $delivery_time = date('Y-m-d H:i:s', time() + ($setting['default_time']['value'] * 60));
- }
- $order->delivery_time = strtotime($delivery_time);
- $order->save();
- }
- //增加仓库配送订单
- $frontDeliveryOrder = new FrontDeliveryOrder();
- $frontDeliveryOrder->front_agent_admin_id = $mdAgentFrontBind['front_agent_admin_id'];
- $frontDeliveryOrder->cloud_order_id = $value['id'];
- $frontDeliveryOrder->delivery_info_id = $delivery_info->id;
- $frontDeliveryOrder->order_id = $order->id;
- $frontDeliveryOrder->store_id = $order->store_id;
- $frontDeliveryOrder->cloud_supplier_id = $value['supplier_id'];
- if (!$frontDeliveryOrder->save()) {
- debug_log('云仓转单后仓库配送添加仓库配送订单记录失败' . json_encode($frontDeliveryOrder->errors, JSON_UNESCAPED_UNICODE));
- }
- }
- if (intval($value['goods_send_type']) === 6) {
- //增加仓库配送订单
- $frontExpressOrder = new FrontExpressOrder();
- $frontExpressOrder->front_agent_admin_id = $mdAgentFrontBind['front_agent_admin_id'];
- $frontExpressOrder->cloud_order_id = $value['id'];
- $frontExpressOrder->order_id = $order->id;
- $frontExpressOrder->store_id = $order->store_id;
- $frontExpressOrder->cloud_supplier_id = $value['supplier_id'];
- if (!$frontExpressOrder->save()) {
- debug_log('云仓转单后仓库添加仓库快递订单记录失败' . json_encode($frontExpressOrder->errors, JSON_UNESCAPED_UNICODE));
- }
- }
- }
- mdAgentFrontBindEND:
- $arr[] = $orderTransit->id;
- $supplier_form = new SupplierForm();
- $supplier_form->order_id = (string)$value['id'];
- $auto = 0;
- if ($this->store_id) {
- $auto = 1;
- }
- $res = $supplier_form->supplierPurchaseOrderList($orderTransit->cloud_supplier_id, $auto);
- debug_log('supplierPurchaseOrderList' . json_encode($res));
- if ($res['code'] === 0) {
- $cloud_order = $res['data']['list'][0];
- queue_push(new CreatedStbzOrderTranJob(['data' => $cloud_order]));
- } else {
- return $res;
- }
- $total_price += $value['pay_price'];
- try {
- $printer_order = new PrintOrder(0, $value['id'], 'pay', 0, 0, 1, 0, $supplier->id);
- $printer_order->print_order();
- } catch (\Exception $e) {
- debug_log([
- 'event' => 'mchSetPurchaseOrder',
- 'message' => $e->getMessage(),
- 'line' => $e->getLine(),
- 'file' => $e->getFile()
- ], 'cloud_printer.log');
- }
- }
- }
- // $del_order = Order::findOne($old_order['id']);
- // $del_order->is_delete = 1;
- // $del_order->save();
- }
- debug_log('order_transit_id' . json_encode($arr));
- debug_log('order_detail_ids' . json_encode($order_detail_ids));
- if(count($order_detail_ids) > 0 ){
- foreach ($order_detail_ids as $index => $value) {
- $OrderDetail = OrderDetail::find()->where(["id"=>$value,"is_delete"=>0])->one();
- if($OrderDetail){
- $OrderDetail->order_transit_id = $arr[$index] ? : ($arr[0] ?: 0);
- $OrderDetail->save();
- }
- }
- }
- //上门安装逻辑
- $res = $this->isNeedInstall($this->order_id, 0, true);
- if ($res['code'] !== 0) {
- return $res;
- }
- $OrderTransit = OrderTransit::findOne(['order_id' => $order->id]);
- if ($OrderTransit) {
- $order->trans_status = 1;
- }
- $order->trans_error = '';
- $order->save();
- $msg = "创建成功";
- return $this->outPutData($mchSetPurchaseOrderInfo['code'],$msg,$mchSetPurchaseOrderInfo['data']);
- }
- }
- //获取云仓产品更新记录
- public function mchGetCloudGoodsUpdateLog() {
- try {
- $where = [
- 'cgl.store_id' => get_store_id(),
- 'g.is_delete' => 0
- ];
- if (in_array($this->status, [1, 2])) {
- $where['is_update'] = (int)$this->status === 1 ? 1 : 0;
- }
- $query = CloudGoodsUpdateLog::find()->alias('cgl')
- ->leftJoin(['g' => Goods::tableName()], 'cgl.cloud_goods_id = g.cloud_goods_id')
- ->leftJoin(['s' => Supplier::tableName()], 'cgl.cloud_supplier_id = s.cloud_supplier_id')
- ->select('cgl.id, g.cloud_goods_id, g.name, g.cover_pic, s.supplier_name, s.logo, cgl.is_audit, cgl.is_update, s.phone, cgl.created_at, cgl.updated_at')
- ->where($where)
- ->orderBy(['cgl.created_at' => SORT_DESC, 'cgl.updated_at' => SORT_DESC])->groupBy(['g.cloud_goods_id', 'cgl.is_update', 'cgl.is_audit']);
- // var_dump($query->createCommand()->getRawSql());die;
- $list = pagination_make($query);
- foreach ($list['list'] as &$value) {
- $value['is_audit'] *= 1;
- $value['is_update'] *= 1;
- }
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'data' => $list['list'],
- 'pageNo' => $list['pageNo'],
- 'totalCount' => $list['totalCount']
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //获取绑定商品列表
- public function mchGetGoodsBindingList(){
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $message = null;
- $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- $mch_get_goods_bind_url = "/cloud/mch/mchGetGoodsBinding";
- $mch_get_goods_bind_data = [];
- $mch_get_goods_bind_data['access_token'] = $cloud_store_token; //获取供货商的token信息
- if($this->page) $mch_get_goods_bind_data['page'] = $this->page; //默认为 1
- if($this->limit) $mch_get_goods_bind_data['limit'] = $this->limit; //默认20
- if($this->goods_id) $mch_get_goods_bind_data['goods_id'] = $this->goods_id; //订单号
- $mchGetGoodsBindingInfo = cloud_post($this->domain.$mch_get_goods_bind_url,$mch_get_goods_bind_data);
- $mchGetGoodsBindingInfo = json_decode($mchGetGoodsBindingInfo,true);
- if($mchGetGoodsBindingInfo['code'] != 0){
- $code = 1;//失败
- return $this->outPutData($code,$mchGetGoodsBindingInfo['msg']);
- }else{
- $msg = "获取成功";
- //组装本地商城数据
- if(count($mchGetGoodsBindingInfo['data'])>0){
- $shop_goods_ids = $shop_goods_attr_ids = [];
- foreach($mchGetGoodsBindingInfo['data']['list'] as $key=>$val){
- if($val['goods_id']){
- foreach(explode("-",$val['goods_id']) as $shopKey=>$shopVal){
- if($shopKey == 0){
- $shop_goods_ids[] = $shopVal;
- $mchGetGoodsBindingInfo['data']['list'][$key]['shop_goods_id'] = $shopVal;
- }else{
- $shop_goods_attr_ids[] = $shopVal;
- $mchGetGoodsBindingInfo['data']['list'][$key]['shop_goods_attr_ids'][] = $shopVal;
- }
- }
- }
- }
- $goodsInfo = Goods::find()->where(['id'=>array_unique($shop_goods_ids),'is_delete'=>0])->all();
- $newGoodsInfo = $newAttrInfo = [];
- foreach($goodsInfo as $val){
- $newGoodsInfo[$val->attributes['id']] = $val->attributes;
- }
- $attrInfo = Attr::find()->where(['id'=>array_unique($shop_goods_attr_ids),'is_delete'=>0])->all();
- foreach($attrInfo as $val){
- $newAttrInfo[$val->attributes['id']] = $val->attributes;
- }
- foreach($mchGetGoodsBindingInfo['data']['list'] as $key=>$val){
- if(isset($newGoodsInfo[$mchGetGoodsBindingInfo['data']['list'][$key]['shop_goods_id']])){
- $mchGetGoodsBindingInfo['data']['list'][$key]['shop_goods_info'] = $newGoodsInfo[$mchGetGoodsBindingInfo['data']['list'][$key]['shop_goods_id']];
- }
- if(isset($mchGetGoodsBindingInfo['data']['list'][$key]['shop_goods_attr_ids']) ){
- foreach($mchGetGoodsBindingInfo['data']['list'][$key]['shop_goods_attr_ids'] as $attrVal){
- if(isset($newAttrInfo[$attrVal])){
- $mchGetGoodsBindingInfo['data']['list'][$key]['shop_goods_attr_info'][] = $newAttrInfo[$attrVal];
- }
- }
- }
- }
- }
- return $this->outPutData($mchGetGoodsBindingInfo['code'],$msg,$mchGetGoodsBindingInfo['data']);
- }
- }
- // //获取绑定商品列表
- // public function mchSetGoodsBindingList(){
- // $store_id = get_store_id();
- // $query = CloudGoodsBind::find()->where(['store_id'=>$store_id,'is_delete'=>0]);
- // $count = $query->count();
- // //使用总数来创建一个分页对象
- // $pagination = new Pagination(['totalCount' => $count]);
- // //使用分页对象来填充 limit 子句并取得文章数据
- // $articles = $query->offset($this->page-1)
- // ->limit($this->limit)
- // ->all();
- // $attr_ids = [];
- // $goods_ids = [];
- // foreach($articles as $val){
- // $attrInfo = explode("-",$val->attributes['attr_id']);
- // $attr_ids = array_merge($attr_ids,$attrInfo);
- // $goods_ids[] = $val->attributes['goods_id'];
- // }
- // $goodsInfo = Goods::find()->where(['id'=>$goods_ids,'is_delete'=>0])->all();
- // $attrInfo = Attr::find()->where(['id'=>$attr_ids,'is_delete'=>0])->all();
- // $newAttrInfo = [];
- // foreach($attrInfo as $attrVal){
- // $newAttrInfo[$attrVal->attributes['id']] = $attrVal->attributes;
- // }
- // $dataInfo = [];
- // $i = 0;
- // foreach($articles as $key=>$val){
- // $dataInfo[$i]['goods_id'] = $val->attributes['goods_id'];
- // $dataInfo[$i]['cloud_attr_id'] = $val->attributes['cloud_attr_id'];
- // foreach(explode("-",$val->attributes['attr_id']) as $attrVal){
- // $temp =[];
- // $temp[] = $newAttrInfo[$attrVal];
- // }
- // $dataInfo[$i]['attr'] = $temp;
- // foreach($goodsInfo as $goodsVal){
- // if($val->attributes['goods_id'] == $goodsVal->attributes['id']){
- // $dataInfo[$i]['goods_info'] = $goodsVal->attributes;
- // }
- // }
- // $i++;
- // }
- // $messageInfo = [];
- // $messageInfo['count'] = $count;
- // $messageInfo['page'] = $this->page;
- // $messageInfo['limit'] = $this->limit;
- // $messageInfo['data'] = $dataInfo;
- // $code = 0;//失败
- // $msg = '获取绑定信息列表';
- // return $this->outPutData($code,$msg,$messageInfo);
- // }
- //导入商品
- public function mchGoodsImport($goodsInfo,$goods_id, $store_id = 0){
- try {
- if(!$goodsInfo){
- $code = 1;//失败
- $msg = '导入失败';
- return $this->outPutData($code,$msg);
- }
- $store_id = !empty($store_id) ? $store_id : get_store_id();
- $cloudBind = CloudGoodsBind::findOne(['cloud_goods_id' => $goods_id, 'store_id' => $store_id, 'is_delete' => 0]) ?: new CloudGoodsBind();
- $message = null;
- $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, $store_id, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- $mch_set_goods_bind_url = "/cloud/mch/mchSetGoodsBinding";
- foreach($goodsInfo as $val){
- $mch_set_goods_bind_data = [];
- $mch_set_goods_bind_data['access_token'] = $cloud_store_token; //获取供货商的token信息
- $mch_set_goods_bind_data['goods_id'] = $goods_id;
- $mch_set_goods_bind_data['status'] = 1; //1 绑定 0 解除绑定
- $mchSetGoodsBindInfo =cloud_post($this->domain.$mch_set_goods_bind_url,$mch_set_goods_bind_data);
- $mchSetGoodsBindInfo = json_decode($mchSetGoodsBindInfo,true);
- if($mchSetGoodsBindInfo['code'] == 0){
- $cloudBind->cloud_goods_id = $goods_id;
- $cloudBind->cloud_supplier_id = $val['cloud_supplier_id'];
- $cloudBind->store_id = $store_id;
- $cloudBind->goods_id = $val['goods_id'];
- $cloudBind->created_at = time();
- if (!$cloudBind->save()) {
- $code = 1;//失败
- $msg = '导入失败:' . $cloudBind->errors;
- return $this->outPutData($code,$msg);
- };
- } else {
- $code = 1;//失败
- $msg = '导入失败:' . $mchSetGoodsBindInfo['msg'];
- return $this->outPutData($code,$msg);
- }
- }
- $log = CloudGoodsUpdateLog::findOne(['cloud_goods_id' => $goods_id, 'store_id' => (!empty($store_id) ? $store_id : get_store_id()), 'is_audit' => 1, 'is_update' => 0]);
- if ($log) {
- $log->is_update = 1;
- $log->updated_at = time();
- $log->save();
- }
- $code = 0;//失败
- $msg = '导入成功';
- return $this->outPutData($code, $msg);
- } catch (\Exception $e) {
- debug_log($e->getMessage());
- return $this->outPutData(1, $e->getMessage());
- }
- }
- //绑定商品
- public function mchSetGoodsBinding(){
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $goodsInfo =Goods::find()->where(['id' => $this->goods_id,'is_delete'=>0])->one();
- if(!$goodsInfo || !$goodsInfo->attributes['attr']){
- $code = 1;//失败
- $msg = '商城商品信息不存在';
- return $this->outPutData($code,$msg);
- }
- $attrInfo = json_decode($goodsInfo->attributes['attr'],true);
- $is_ok = false;
- foreach($attrInfo as $attr){
- //拼接多规格商品
- $good_attr = '';
- foreach($attr['attr_list'] as $attrVal){
- $good_attr .= $attrVal['attr_id']."-";
- }
- if(substr($good_attr,0,-1) == $this->attr_id) $is_ok = true;
- }
- if(!$is_ok){
- $code = 1;//失败
- $msg = '商城商品信息不存在';
- return $this->outPutData($code,$msg);
- }
- //创建关系
- $bindInfo = CloudGoodsBind::find()->where(['goods_id'=>$this->goods_id,'attr_id'=>$this->attr_id,'is_delete'=>0])->one();
- if($this->status==1 && $bindInfo){
- $code = 1;//失败
- $msg = '商城商品信息只能绑定一个商品';
- return $this->outPutData($code,$msg);
- }
- $message = null;
- $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- $mch_set_goods_bind_url = "/cloud/mch/mchSetGoodsBinding";
- $mch_set_goods_bind_data = [];
- $mch_set_goods_bind_data['access_token'] = $cloud_store_token; //获取供货商的token信息
- $mch_set_goods_bind_data['goods_id'] = $this->goods_id."-".$this->attr_id; //本地商城的唯一id(商品ID商品属性ID)
- $mch_set_goods_bind_data['attr_id'] = $this->cloud_attr_id; //规格ID
- $mch_set_goods_bind_data['status'] = $this->status; //1 绑定 0 解除绑定
- $t = Yii::$app->db->beginTransaction();
- if($this->status==1){ //新增逻辑
- $cloudBind = new CloudGoodsBind();
- $cloudBind->cloud_goods_id = $this->cloud_goods_id;
- $cloudBind->cloud_attr_id = $this->cloud_attr_id;
- $cloudBind->cloud_supplier_id = $this->cloud_supplier_id;
- $cloudBind->store_id = get_store_id();
- $cloudBind->goods_id = $this->goods_id;
- $cloudBind->attr_id = $this->attr_id;
- $cloudBind->created_at = time();
- $cloudBind->save();
- }else{ //删除绑定逻辑
- if(!$bindInfo){
- $code = 1;//失败
- $msg = '不存在绑定商品信息';
- return $this->outPutData($code,$msg);
- }
- $bindInfo->is_delete = 1;
- $bindInfo->deleted_at = time();
- $bindInfo->save();
- }
- $mchSetGoodsBindInfo = cloud_post($this->domain.$mch_set_goods_bind_url,$mch_set_goods_bind_data);
- $mchSetGoodsBindInfo = json_decode($mchSetGoodsBindInfo,true);
- if($mchSetGoodsBindInfo['code'] != 0){
- $t->rollBack();
- $code = 1;//失败
- return $this->outPutData($code,$mchSetGoodsBindInfo['msg']);
- }else{
- $t->commit();
- return $this->outPutData($mchSetGoodsBindInfo['code'],$mchSetGoodsBindInfo['msg']);
- }
- }
- //余额记录
- public function mchGetBalanceLog(){
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $message = null;
- $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- $get_balance_log_url = "/cloud/mch/mchGetBalanceLog";
- $get_balance_log_data = [];
- $get_balance_log_data['access_token'] = $cloud_store_token; //获取供货商的token信息
- if($this->page) $get_balance_log_data['page'] = $this->page; //计费方式【1=>按重计费、2=>按件计费】
- if($this->limit) $get_balance_log_data['limit'] = $this->limit;
- if($this->sort) $get_balance_log_data['sort'] = $this->sort;
- debug_log($this->sort);
- $getBalanceLogInfo = cloud_post($this->domain.$get_balance_log_url,$get_balance_log_data);
- $getBalanceLogInfo = json_decode($getBalanceLogInfo,true);
- if($getBalanceLogInfo['code'] != 0){
- $code = 1; //失败
- return $this->outPutData($code,$getBalanceLogInfo['msg']);
- }else{
- $msg = '获取成功';
- return $this->outPutData($getBalanceLogInfo['code'],$msg,$getBalanceLogInfo['data']);
- }
- }
- //商品列表
- public function mchGetGoodsList(){
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- $message = null;
- $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- if ($this->token_stroe_cloud_id) {
- $store_cloud = StoreCloud::findOne(['cloud_store_id' => $this->token_stroe_cloud_id]);
- } else {
- $store_id = $this->store_id ?: get_store_id();
- $store_cloud = StoreCloud::findOne(['store_id' => $store_id, 'is_delete' => 0]);
- }
- if ($store_cloud && $store_cloud->cloud_platform_list) {
- $cloud_platform_list = json_decode($store_cloud->cloud_platform_list, true);
- if($this->supplier_id) {
- $supplier_id = explode(',', $this->supplier_id);
- }
- if (!empty($supplier_id)) {
- $supplier_id = array_map('intval', $supplier_id);
- } else {
- $supplier_id = $cloud_platform_list;
- }
- if (!empty($cloud_platform_list)) {
- $cloud_platform_list = array_map('intval', $cloud_platform_list);
- $supplier_id = array_intersect($cloud_platform_list, $supplier_id);
- if (!empty($supplier_id)) {
- $this->supplier_id = implode(',', $supplier_id);
- } else {
- $this->supplier_id = "0";
- }
- }
- }
- $mch_get_goods_list_url = "/cloud/mch/mchGetGoodsList";
- $mch_get_goods_list_data = [];
- $mch_get_goods_list_data['access_token'] = $cloud_store_token; //获取供货商的token信息
- $mch_get_goods_list_data['page'] = $this->page; //默认为 1
- $mch_get_goods_list_data['limit'] = $this->limit; //默认20
- if($this->cat_id) $mch_get_goods_list_data['cat_id'] = $this->cat_id;
- if($this->parent_id) $mch_get_goods_list_data['parent_id'] = $this->parent_id;
- if($this->name) $mch_get_goods_list_data['name'] = $this->name; //名称
- if($this->supplier_id) $mch_get_goods_list_data['supplier_id'] = $this->supplier_id; //供货商ID
- $mch_get_goods_list_data['sort'] = $this->sort;
- if($this->status) $mch_get_goods_list_data['status'] = $this->status; //-1 全部 0 下架 1 上架
- if($this->price_sort) $mch_get_goods_list_data['sort'] = $this->price_sort;
- if($this->sale_sort) $mch_get_goods_list_data['sort'] = 3;
- if($this->high_price) $mch_get_goods_list_data['high_price'] = $this->high_price;
- if($this->low_price) $mch_get_goods_list_data['low_price'] = $this->low_price;
- if(intval($this->send_type) !== -1 && $this->send_type !== null) $mch_get_goods_list_data['send_type'] = $this->send_type;
- $mch_get_goods_list_data['is_distribution'] = $this->is_distribution;
- $mchGetGoodsListInfo = cloud_post($this->domain.$mch_get_goods_list_url,$mch_get_goods_list_data);
- $mchGetGoodsListInfo = json_decode($mchGetGoodsListInfo,true);
- if($mchGetGoodsListInfo['code'] != 0){
- $code = 1;//失败
- return $this->outPutData($code,$mchGetGoodsListInfo['msg']);
- }else{
- //获取议价比
- // $content = Option::get('cloud', 0, 'saas');
- // $content = json_decode($content['value'], true);
- $rate = Store::find()->where(['id' => $this->store_id ?: get_store_id()])->select('rate')->scalar() ?: 0;
- //填充
- if (!empty($mchGetGoodsListInfo['data']['list'])) {
- foreach ($mchGetGoodsListInfo['data']['list'] as &$item) {
- // if (!empty($item['original_price'])) {
- // $item['sale_price'] = $item['original_price'];
- // } else {
- $item['store_id'] = $this->store_id ?: get_store_id();
- $item['cost_price'] = $item['price']; //将供货商售价作为成本价
- $sale_price_type = Option::get('sale_price_type', $this->store_id ?: get_store_id(), 'store')['value'];
- $item['sale_price'] = intval($sale_price_type) === 0 ? sprintf('%.2f', ($item['platform_negotiated_price'] + ($item['platform_negotiated_price']) * ($rate / 100))) : $item['original_price'];
- // $item['price'] = $item['sale_price'] = sprintf("%.2f", $item['price'] + ($item['price'] * ($rate / 100)));
- $item['sale_price'] = round($item['sale_price'], 2);
- if (in_array(intval($this->is_distribution), [0, 1, 2])) {
- $item['price'] = $item['platform_negotiated_price'];
- $item['original_price'] = $item['sale_price'];
- }
- $item['price_rate'] = '0.00';
- $price_rate = sprintf('%.2f', ($item['original_price'] - $item['price']));
- if ($price_rate >= 0) {
- $item['price_rate'] = sprintf('%.2f', $price_rate);
- }
- // }
- if (!empty($item['attrs'])) {
- foreach ($item['attrs'] as &$attr_item) {
- //生成零售价
- // if (!empty($attr_item['original_price'])) {
- // $sale_price = $attr_item['original_price'];
- // } else {
- $attr_item['price'] = $attr_item['price'] ?: '0.00';
- $attr_item['cost_price'] = $attr_item['price']; //将供货商售价作为成本价
- if ($sale_price_type) {
- $sale_price = $attr_item['original_price'];
- } else {
- $sale_price = sprintf('%.2f', ($attr_item['platform_negotiated_price'] + ($attr_item['platform_negotiated_price']) * ($rate / 100)));
- }
- $attr_item['sale_price'] = $sale_price;
- // }
- $attr_item = array_merge($attr_item, ['sale_price' => $sale_price, 'rate' => $rate]);
- }
- }
- }
- }
- $msg = "获取成功";
- return $this->outPutData($mchGetGoodsListInfo['code'], $msg, $mchGetGoodsListInfo['data']);
- }
- }
- public function setSaasStoreCloudId($saas_id = 0) {
- $item = get_saas_purchase_store_cloud($saas_id);
- $this->token_stroe_cloud_id = $item ? $item->id : 0;
- }
- //创建供货单
- public function purchasePrePurchaseOrder($cartList, $address){
- $orderInfo = (object)[];
- if($address){
- $orderInfo = (object)($address);
- $orderInfo->address = $address['detail'];
- }
- $orderInfo->is_pay = $this->is_pay;
- $goodsInfo = [];
- foreach($cartList['goods_list'] as $i => $v){
- foreach ($v['cart'] as $cartI => $_c) {
- $_goodsInfo = [
- 'attr' => $_c['attr_list'],
- 'goods_id' => $v['goods']['id'],
- 'num' => $_c['num'],
- 'goods_name' => $v['goods']['name'],
- 'total_price' => $_c['price'] * $_c['num'],
- ];
- $goodsInfo[] = $_goodsInfo;
- }
- }
- return $this->_purchaseSetPurchaseOrder($orderInfo, $goodsInfo);
- }
-
- //创建供货单
- public function purchaseSetPurchaseOrder($orderIds = []){
- if (!$this->validate()) {
- return $this->getErrorSummary(false)[0];
- }
- if(!$orderIds){
- $orderIds[] = $this->order_id;
- }
- $goods_info = [];
- foreach ($orderIds as $order_id){
- $orderInfo = PurchaseOrder::findOne([
- 'id' => $order_id,
- ]);
- if(!$orderInfo){
- \Yii::error('-----------PurchaseOrder订单数据有误' . $order_id);
- $code = 1;//失败
- $msg = "订单数据有误";
- return $this->outPutData($code,$msg);
- }
- $OrderDetail = PurchaseOrderDetail::find()->where(['order_id' => $order_id])->select('id order_d_id, goods_id, num, total_price price, pic pic_url, attr')->asArray()->all();
- foreach ($OrderDetail as $item) {
- $goods_info[] = $item;
- }
- }
- debug_log('orderIds' . json_encode($orderIds));
- $res = $this->_purchaseSetPurchaseOrder($orderInfo, $goods_info, $orderIds);
- debug_log('运行结束' . json_encode($res));
- return $res;
- }
- private function _purchaseSetPurchaseOrder($orderInfo, $goods_info, $orderIds = []){
- $new_goods_info = [];
- foreach($goods_info as $item){
- $goodsId = $item['goods_id'];
- if(empty($new_goods_info[$goodsId])){
- $new_goods_info[$goodsId] = ['id' => $goodsId];
- }
- $new_goods_info[$goodsId]['attr'][] = [
- 'attr_list' => is_array($item['attr']) ? $item['attr'] : json_decode($item['attr']),
- 'num' => $item['num'],
- ];
- $new_goods_info[$goodsId]['num'] += $item['num'];
- }
- // $this->setSaasStoreCloudId();
- $message = null;
- $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- $mch_set_purchase_order_url = "/cloud/mch/mchSetSubmitOrder";
- $mch_set_purchase_order_data = [];
- $mch_set_purchase_order_data['access_token'] = $cloud_store_token; //获取供货商的token信息
- $mch_set_purchase_order_data['province_id'] = $orderInfo->province_id; //省
- $mch_set_purchase_order_data['city_id'] = $orderInfo->city_id; //市
- $mch_set_purchase_order_data['district_id'] = $orderInfo->district_id; //区
- $mch_set_purchase_order_data['address'] = $orderInfo->address; //地址详情
- $mch_set_purchase_order_data['name'] = $orderInfo->name; //联系人
- $mch_set_purchase_order_data['mobile'] = $orderInfo->mobile; //联系电话
- $mch_set_purchase_order_data['remark'] = $orderInfo->remark;
- $mch_set_purchase_order_data['goods_info'] = json_encode($new_goods_info); //商品数据
- $mch_set_purchase_order_data['pay_type'] = $orderInfo->pay_type == 3 ? 0 : 1;
- $mch_set_purchase_order_data['type'] = $this->type; //1;
- $mch_set_purchase_order_data['is_pay'] = $orderInfo->is_pay ?? 1;
- $mch_set_purchase_order_data['order_id'] = $orderInfo->cloud_order_id ?? 0;
- $mchSetPurchaseOrderInfo = cloud_post($this->domain.$mch_set_purchase_order_url,$mch_set_purchase_order_data);
- $mchSetPurchaseOrderInfo = json_decode($mchSetPurchaseOrderInfo,true);
- // \Yii::error('-----------PurchaseOrder转供货单' . Json::encode($mch_set_purchase_order_data));
- // \Yii::error('-----------PurchaseOrder转供货单' . Json::encode($mchSetPurchaseOrderInfo));
- if($mchSetPurchaseOrderInfo['code'] != 0){
- \Yii::error('-----------PurchaseOrder转供货单失败' . Json::encode($mchSetPurchaseOrderInfo));
- $code = 1;//失败
- return $this->outPutData($code,$mchSetPurchaseOrderInfo['msg']);
- }else{
- if($mch_set_purchase_order_data['type'] == 1 && $mchSetPurchaseOrderInfo['data']){
- foreach ($orderIds as $index => $orderId) {
- $status = 0;
- foreach ($mchSetPurchaseOrderInfo['order_data'] as $order_datum) {
- foreach ($order_datum as $item) {
- if (intval($item['old_order_id']) === intval($orderId)) {
- if (intval($item['status']) === 2) {
- $status = 3;
- }
- }
- $print_type = 'pay';
- if (intval($item['status']) === 2) {
- $print_type = 'order';
- }
- try {
- $supplier = Supplier::findOne(['cloud_supplier_id' => $item['supplier']]);
- $printer_order = new PrintOrder(0, $item['id'], $print_type, 0, 0, 2, 0, $supplier->id);
- $printer_order->print_order();
- } catch (\Exception $e) {
- debug_log([
- 'event' => '_purchaseSetPurchaseOrder',
- 'message' => $e->getMessage(),
- 'line' => $e->getLine(),
- 'file' => $e->getFile()
- ], 'cloud_printer.log');
- }
- }
- }
- PurchaseOrder::updateAll(['cloud_order_id' => $mchSetPurchaseOrderInfo['data'][$index], 'trade_status' => $status], ['id' => $orderId]);
- }
- }
- $msg = "创建成功";
- return [
- 'code' => $mchSetPurchaseOrderInfo['code'],
- 'msg' => $msg,
- 'data' => $mchSetPurchaseOrderInfo['data'],
- 'order_data' => $mchSetPurchaseOrderInfo['order_data']
- ];
- }
- }
- public function getAgentOrderDistributionList() {
- $message = null;
- $cloud_store_token = get_merchant_token($this->token_stroe_cloud_id, 0, $message);
- if (!$cloud_store_token && $message) {
- return $message;
- }
- $mch_set_purchase_order_url = "/cloud/mch/getAgentOrderDistributionList";
- $data = [];
- $data['access_token'] = $cloud_store_token;
- $data['goods_name'] = $this->name;
- $data['status'] = $this->status;
- $data['page'] = $this->page; //默认为 1
- $data['limit'] = $this->limit; //默认20
- $mchSetPurchaseOrderInfo = cloud_post($this->domain.$mch_set_purchase_order_url,$data);
- $mchSetPurchaseOrderInfo = json_decode($mchSetPurchaseOrderInfo,true);
- if($mchSetPurchaseOrderInfo['code'] != 0){
- $code = 1;//失败
- return $this->outPutData($code,$mchSetPurchaseOrderInfo['msg']);
- }else{
- $msg = "获取成功";
- if (!empty($mchSetPurchaseOrderInfo['data'])) {
- foreach ($mchSetPurchaseOrderInfo['data']['data'] as &$data) {
- $admin_id = $data['admin_id'];
- $data['admin_name'] = Admin::findOne(['id' => $admin_id])->name;
- }
- }
- return $this->outPutData($mchSetPurchaseOrderInfo['code'], $msg, $mchSetPurchaseOrderInfo['data']);
- }
- }
- //胜天半子售后操作
- public function stbzRrfundOrderHandle($cloud_store_token, $refund_data = null) {
- try {
- //获取订单信息 售后前置检测 +
- if ((int)$this->order_type === 1) {
- $order_url = "/cloud/mch/mchOrderList";
- } else {
- $order_url = "/cloud/mch/mchPurchaseOrderList";
- }
- $order_data['access_token'] = $cloud_store_token;
- $order_data['id'] = $this->order_id;
- $domain = $this->domain;
- $orderInfo = cloud_post($domain. $order_url,
- $order_data);
- $orderInfo = json_decode($orderInfo,true);
- if ($orderInfo['code'] !== 0) {
- return [
- 'code' => 2,
- 'msg' => $orderInfo['msg']
- ];
- }
- $data = $orderInfo['data']['list'][0];
- if (!empty($data['goods_list'])) {
- foreach ($data['goods_list'] as $datum) {
- $attr = json_decode($datum['attr'], true);
- if ($datum['stbz_goods_id'] && $attr['no']) {
- $params = [
- 'orderSn' => $data['order_no'],
- 'sku' => $attr['no']
- ];
- $checkData = \stbz_client()->getApiResponse('post', '/v2/afterSale/beforeCheck', $params);
- debug_log('胜天半子售后' . json_encode($checkData));
- $checkData = \json_decode($checkData, true);
- if ((int)$checkData['code'] === 1) {
- $checkData = $checkData['data'];
- $num = $checkData['num'];
- if ($num <= 0) {
- return [
- 'code' => 1,
- 'msg' => '可售后商品数量为0个'
- ];
- }
- $serviceType = $checkData['serviceType'];
- $pickType = $checkData['pickType'];
- $packageType = $checkData['packageType'];
- $returnType = $checkData['returnType'];
- $reasonsType = $checkData['reasonsType'];
- //售后类型 20换货 50退款退货
- $serviceTypeCode = '0';
- //目前订单未完成禁止售后
- if ((int)$data['status'] === 0) {
- $serviceTypeCode = '40';
- } else {
- if ((int)$refund_data['type'] === 1) {
- $serviceTypeCodeArr = array_column($serviceType, 'code');
- // $serviceTypeRes = array_search('50', $serviceType);
- $serviceTypeCode = '50';
- if (!in_array('50', $serviceTypeCodeArr)) {
- $serviceTypeCode = '40';
- if (!in_array('40', $serviceTypeCodeArr)) {
- return [
- 'code' => 1,
- 'msg' => '当前订单状态异常,不可申请退款退货'
- ];
- }
- }
- } else {
- foreach ($serviceType as $service) {
- if ($service['code'] == '20') {
- $serviceTypeCode = '20';
- }
- }
- if (!$serviceTypeCode) {
- return [
- 'code' => 1,
- 'msg' => '当前订单状态异常,不可申请换货'
- ];
- }
- }
- }
- //售后原因
- $reasonsTypeCode = 0;
- if ((int)$data['status'] === 0) {
- $reasonsTypeCode = '6';
- } else {
- if (!empty($reasonsType)) {
- if (!empty($refund_data['reasonsTypeCode'])) {
- foreach ($reasonsType as $reason) {
- if ($reason['code'] == $refund_data['reasonsTypeCode']) {
- $reasonsTypeCode = $refund_data['reasonsTypeCode'];
- }
- }
- if (!$reasonsTypeCode) {
- return [
- 'code' => 1,
- 'msg' => '售后原因编码错误'
- ];
- }
- } else {
- return [
- 'code' => 1,
- 'msg' => '售后原因编码错误'
- ];
- }
- }
- }
- //返件类型
- $returnTypeCode = 0;
- if (!empty($returnType)) {
- if (!empty($refund_data['returnTypeCode'])) {
- foreach ($returnType as $return) {
- if ($return['code'] == $refund_data['reasonsTypeCode']) {
- $returnTypeCode = $refund_data['reasonsTypeCode'];
- }
- }
- if (!$returnTypeCode) {
- return [
- 'code' => 1,
- 'msg' => '返件类型编码错误'
- ];
- }
- } else {
- return [
- 'code' => 1,
- 'msg' => '返件类型编码错误'
- ];
- }
- }
- //包装情况
- $packageTypeCode = 0;
- if (!empty($packageType)) {
- if (!empty($refund_data['packageTypeCode'])) {
- foreach ($packageType as $package) {
- if ($package['code'] == $refund_data['packageTypeCode']) {
- $packageTypeCode = $refund_data['packageTypeCode'];
- }
- }
- if (!$packageTypeCode) {
- return [
- 'code' => 1,
- 'msg' => '包装情况编码错误'
- ];
- }
- } else {
- return [
- 'code' => 1,
- 'msg' => '包装情况编码错误'
- ];
- }
- }
- $pickTypeCode = '40'; //自主发货
- $urls = [];
- if ($refund_data['pic_list']) {
- //上传凭证
- foreach ($refund_data['pic_list'] as &$pic_list) {
- $save_path = $this->saveTempImage($pic_list);
- $image = file_get_contents($save_path);
- $pic_list = base64_encode($image);
- }
- $params = array_merge($params, [
- 'pictures' => $refund_data['pic_list']
- ]);
- $checkData = \stbz_client()->getApiResponse('post', '/v2/afterSale/picture', $params);
- $checkData = \json_decode($checkData, true);
- if ((int)$checkData['code'] === 1) {
- $urls = $checkData['data']['urls'];
- }
- }
- $data['address_data'] = json_decode($data['address_data'], true);
- //申请售后
- $refund_params = [
- 'orderSn' => $params['orderSn'],
- 'sku' => $params['sku'],
- 'num' => $num,
- 'goodsFee' => $data['goods_price'],
- 'logisticFee' => $data['express_price'],
- 'serviceTypeCode' => $serviceTypeCode,
- 'pickTypeCode' => $pickTypeCode,
- 'packageTypeCode' => $packageTypeCode,
- 'returnTypeCode' => $returnTypeCode,
- 'reasonsTypeCode' => $reasonsTypeCode,
- 'reasonsDescription' => $refund_data['desc'] ?: '不想要了',
- 'vouchers' => $urls,
- 'userInfo' => [
- 'consignee' => $data['name'],
- 'phone' => $data['mobile'],
- 'province' => $data['address_data']['province'],
- 'city' => $data['address_data']['city'],
- 'area' => $data['address_data']['district'],
- 'street' => '',
- 'description' => $data['address_data']['address'],
- ]
- ];
- //已经发货,且为换货
- if ((int)$refund_data['type'] === 0 && (int)$data['status'] !== 0) {
- //如果是换货时,移除退货金额 ,将运费清0
- unset($refund_params['goodsFee']);
- $refund_params['logisticFee'] = 0;
- }
- //退款退货 或 未发货
- if ((int)$refund_data['type'] === 1 || (int)$data['status'] === 0) {
- //如果是退款退货时,移除收件人信息
- unset($refund_params['userInfo']);
- }
- $checkData = \stbz_client()->getApiResponse('post', '/v2/afterSale', $refund_params);
- $checkData = \json_decode($checkData, true);
- if ((int)$checkData['code'] === 1) {
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => $checkData['data']['id']
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => $checkData['msg']
- ];
- }
- } else {
- return [
- 'code' => 1,
- 'msg' => $checkData['msg']
- ];
- }
- }
- }
- }
- return [
- 'code' => 2,
- 'msg' => '参数错误'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function stbzRrfundOrderHandleInfo($cloud_store_token, $id) {
- try {
- $order_url = "/cloud/mch/mchPurchaseOrderList";
- $order_data['access_token'] = $cloud_store_token;
- $order_data['id'] = $id;
- $domain = (new OptionSetting)->getCloudDomainName();
- $orderInfo = cloud_post($domain. $order_url,
- $order_data);
- $orderInfo = json_decode($orderInfo,true);
- if ($orderInfo['code'] !== 0) {
- return [
- 'code' => 0,
- 'msg' => $orderInfo['msg']
- ];
- }
- $data = $orderInfo['data']['list'][0];
- if (!empty($data['goods_list'])) {
- foreach ($data['goods_list'] as $datum) {
- $attr = json_decode($datum['attr'], true);
- if ($datum['stbz_goods_id'] && $attr['no']) {
- $params = [
- 'orderSn' => $data['order_no'],
- 'sku' => $attr['no']
- ];
- $checkData = \stbz_client()->getApiResponse('post', '/v2/afterSale/beforeCheck', $params);
- $checkData = \json_decode($checkData, true);
- if ($checkData['code'] === 1) {
- $checkData = $checkData['data'];
- $num = $checkData['num'];
- $reasonsType = $checkData['reasonsType'];
- $returnType = $checkData['returnType'];
- $packageType = $checkData['packageType'];
- $serviceType = $checkData['serviceType'];
- return [
- 'code' => 0,
- 'msg' => 'success',
- 'data' => [
- 'reasonsType' => $reasonsType,
- 'returnType' => $returnType,
- 'packageType' => $packageType,
- 'serviceType' => $serviceType
- ]
- ];
- } else {
- return [
- 'code' => 1,
- 'msg' => $checkData['msg']
- ];
- }
- }
- }
- }
- return [
- 'code' => 0,
- 'data' => [
- 'reasonsType' => null,
- 'returnType' => null,
- 'packageType' => null
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //普通商品支付完成后需要上门安装逻辑
- public function isNeedInstall($order_id, $type = 0, $submit = false) {
- try {
- if (intval($type)) {
- $order = PurchaseOrder::findOne($order_id);
- } else {
- $order = Order::findOne($order_id);
- }
- if ($order) {
- $address = [
- 'province_id' => $order->province_id,
- 'city_id' => $order->city_id,
- 'district_id' => $order->district_id,
- ];
- if (intval($type)) {
- $order_goods = PurchaseOrderDetail::find()->where(['order_id' => $order_id, 'is_delete' => 0])
- ->select('goods_id as cloud_goods_id, id order_detail_id')->asArray()->all();
- $store_address = $address;
- } else {
- $md_id = $order->md_id;
- $md = Md::findOne($md_id);
- $store_address = [
- 'province_id' => $md->province,
- 'city_id' => $md->city,
- 'district_id' => $md->district,
- ];
- $order_goods = OrderDetail::find()->alias('od')
- ->leftJoin(['g' => Goods::tableName()], 'g.id = od.goods_id')
- ->where(['od.order_id' => $order_id])->select('g.id, g.cloud_goods_id, od.id order_detail_id')->asArray()->all();
- }
- if (!empty($order_goods)) {
- $form = new OrderForm();
- foreach ($order_goods as $item) {
- $cloud_goods_id = 0;
- if (intval($type)) {
- $cloud_goods_id = $item['cloud_goods_id'];
- }
- $is_install = $this->goodsIsInstall($item['id'] ?? 0, $cloud_goods_id);
- if ($is_install !== 2) {
- $address = $is_install === 1 ? $address : $store_address;
- $result = $form->goodsIsGoodsAgent($address, $item['id'] ?? 0, $cloud_goods_id);
- if ($result['code'] === 0 && $submit) {
- $model = new AgentGoodsInstallLog();
- $model->goods_agent_admin_id = $result['data']->id;
- $model->order_detail_id = $item['order_detail_id'];
- $model->is_need_install = (int)(intval($is_install) === 1);
- $model->order_id = $order_id;
- $model->order_type = intval($type);
- if (!$model->save()) {
- throw new \Exception(json_encode($model->errors));
- }
- } else {
- return $result;
- }
- }
- }
- }
- }
- return [
- 'code' => 0,
- 'msg' => '操作完成'
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => '转单完成后需要上门安装逻辑操作异常' . $e->getMessage() . $e->getLine() . $e->getFile()
- ];
- }
- }
- //判断是否需为代理配送且要上门安装
- public function goodsIsInstall($goods_id, $cloud_goods_id = 0) {
- try {
- $goods = Goods::findOne($goods_id);
- if (empty($cloud_goods_id)) {
- if (empty($goods->cloud_goods_id)) {
- return 2;
- }
- }
- // $goods_url = "/goods/getAttrParams";
- // $param = [
- // 'id' => $cloud_goods_id
- // ];
- // //请求接口
- // $domain = (new OptionSetting)->getCloudDomainName();
- // $goodsInfo = cloud_post($domain . $goods_url, $param);
- // $goodsInfo = json_decode($goodsInfo, true);
- // if ((int)$goodsInfo['code'] === 0) {
- // $goodsInfo = $goodsInfo['data'];
- // //需要安装
- // if ((int)$goodsInfo['send_type'] === 3 && (int)$goodsInfo['is_need_install'] === 1) {
- // return 1;
- // }
- // //不需要安装
- // if ((int)$goodsInfo['send_type'] === 3) {
- // return 3;
- // }
- // }
- if ($goods->agent_goods_status > 0) {
- return intval($goods->agent_goods_status) === 1 ? 3 : 1;
- }
- return 2;
- } catch (\Exception $e) {
- return 2;
- }
- }
- //
- public function setCloudMerchantConfig($reduce_type = null, $store_cloud_id = 0) {
- try {
- if (!$this->validate()) {
- return [
- 'code' => 1,
- 'msg' => $this->getErrorSummary(false)[0],
- ];
- }
- //获取平台token
- $cloud_token = get_merchant_token($store_cloud_id);
- $getGoodsCateUrl = "/cloud/mch/setMchStore";
- $getGoodsCateData['access_token'] = $cloud_token;
- if (isset($reduce_type)) {
- $getGoodsCateData['reduce_type'] = $reduce_type;
- }
- $getGoodsCateInfo = cloud_post($this->domain.$getGoodsCateUrl,$getGoodsCateData);
- $getGoodsCateInfo = json_decode($getGoodsCateInfo,true);
- if($getGoodsCateInfo['code'] != 0){
- throw new \Exception($getGoodsCateInfo['msg']);
- }
- return $this->outPutData(0, $getGoodsCateInfo['msg'], $getGoodsCateInfo['data']);
- } catch (\Exception $e) {
- return $this->outPutData(1, $e->getMessage());
- }
- }
- //转单取消订单
- public function purchaseOrderCancel() {
- try {
- //获取平台token
- $cloud_token = get_merchant_token(0, $this->store_id);
- $orderDataUrl = "/cloud/mch/delPurchaseOrder";
- $orderData['access_token'] = $cloud_token;
- $orderData['order_id'] = $this->order_id;
- $orderTransit = OrderTransit::findOne(['cloud_order_id' => $this->order_id, 'is_delete' => 0]);
- if (!$orderTransit) {
- throw new \Exception('订单不存在');
- }
- if ($orderTransit->order_id) {
- if ($orderTransit->is_use_platform_mch) {
- $orderData['is_use_platform_mch'] = $orderTransit->is_use_platform_mch;
- }
- }
- //这里没太明白为啥云仓都已经退款了还是要调用申请售后的逻辑
- // if($orderTransit){
- //// $form = new \app\modules\admin\models\MerchantForm();
- // $form = $this;
- // $form->order_id = $orderTransit->cloud_order_id;
- // $form->apply_data = [];
- // $form->apply_data['type'] = 1;
- // $form->apply_data['desc'] = 'cancel order before send';
- // $form->apply_data['pic_list'] = [];
- // $mchOrderRefundApply = $form->mchOrderRefundApply();
- // if($mchOrderRefundApply['code']){
- // return $mchOrderRefundApply;
- // }
- // }
- $getGoodsCateInfo = cloud_post($this->domain . $orderDataUrl, $orderData);
- $getGoodsCateInfo = json_decode($getGoodsCateInfo,true);
- if ($getGoodsCateInfo['code'] != 0) {
- throw new \Exception($getGoodsCateInfo['msg']);
- }
- if (isset($getGoodsCateInfo['data']['order_detail'])) {
- foreach ($getGoodsCateInfo['data']['order_detail'] as $order_detail_item) {
- if (in_array($order_detail_item['send_type'], [5, 6])) {
- //增加仓库库存
- $cloud_goods_attr = json_decode($order_detail_item['attr'], true);
- $cloud_goods_attr_id = array_column($cloud_goods_attr['attr_list'], 'attr_id');
- sort($cloud_goods_attr_id);
- //转单后扣减仓库库存 调用供货商修改库存接口 数量传0用作更新商城库存
- $erpInventoryArray = AgentFrontErpInventory::find()->where(['goods_id' => $order_detail_item['id']])
- ->asArray()->all();
- foreach ($erpInventoryArray as $erpInventory) {
- $attr_ids = explode(',', $erpInventory['attr_ids']);
- sort($attr_ids);
- if (!array_diff($cloud_goods_attr_id, $attr_ids)) {
- $ei = AgentFrontErpInventory::findOne($erpInventory['id']);
- if ($ei) {
- AgentFrontErpInventory::logSave($ei, $order_detail_item['num'], AgentFrontErpInventoryLog::LOG_TYPE_ORDER, $this->order_id, $orderTransit->cloud_order_no);
- }
- break;
- }
- }
- }
- }
- }
- // if (intval($orderTransit->is_use_platform_mch)) {
- // $store_info = Store::findOne($orderTransit->store_id);
- // Store::addMoney($store_info, $orderTransit->order_price, '商城云仓订单取消');
- // }
- $orderTransit->status = -1;
- $orderTransit->save();
- try {
- $supplier = Supplier::findOne(['cloud_supplier_id' => $orderTransit->cloud_supplier_id]);
- $printer_order = new PrintOrder(0, $this->order_id, 'confirm', 0, 0, 1, 0, $supplier->id);
- $printer_order->print_order();
- } catch (\Exception $e) {
- debug_log([
- 'event' => 'purchaseOrderCancel',
- 'message' => $e->getMessage(),
- 'line' => $e->getLine(),
- 'file' => $e->getFile()
- ], 'cloud_printer.log');
- }
- return [
- 'code' => 0,
- 'msg' => "操作成功"
- ];
- } catch (\Exception $e) {
- return [
- 'code' => $e->getCode() ?: 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //转单单个商品取消
- public function purchaseOrderCancelGoods() {
- try {
- //获取平台token
- $cloud_token = get_merchant_token(0, $this->store_id);
- $orderDataUrl = "/cloud/mch/orderCancelGoods";
- $orderData['access_token'] = $cloud_token;
- $orderData['order_id'] = $this->order_id;
- $orderData['goods_id'] = $this->goods_id;
- $orderData['goods_attr'] = $this->attr;
- $orderData['mch_cancel_id'] = $this->mch_cancel_id;
- $orderData['goods_num'] = $this->goods_num;
- $orderData['platform_md_id'] = $this->md_id;
- $orderTransit = OrderTransit::findOne(['cloud_order_id' => $this->order_id, 'is_delete' => 0]);
- if (!$orderTransit) {
- throw new \Exception('订单不存在');
- }
- if ($orderTransit->order_id) {
- if ($orderTransit->is_use_platform_mch) {
- $orderData['is_use_platform_mch'] = $orderTransit->is_use_platform_mch;
- }
- }
- $getGoodsCateInfo = cloud_post($this->domain . $orderDataUrl, $orderData);
- $getGoodsCateInfo = json_decode($getGoodsCateInfo,true);
- if ($getGoodsCateInfo['code'] != 0 || !isset($getGoodsCateInfo['code'])) {
- throw new \Exception($getGoodsCateInfo['msg'] ?? '返回值错误');
- }
- return [
- 'code' => 0,
- 'msg' => "操作成功"
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- public function getCloudForbiddenGoods() {
- try {
- $store_id = $this->store_id;
- $storeForbiddenDirectory = StoreForbiddenDirectory::findOne(['store_id' => $store_id, 'is_delete' => 0]);
- if (empty($storeForbiddenDirectory->cloud_cat_id)) {
- return [
- 'code' => 1,
- 'msg' => ''
- ];
- }
- $cloud_cat_id = $storeForbiddenDirectory->cloud_cat_id;
- $orderDataUrl = "/cloud/mch/getCatGoods";
- $orderData['access_token'] = get_merchant_token(0, $store_id);
- $orderData['cat_id'] = $cloud_cat_id;
- $getGoodsCateInfo = cloud_post($this->domain . $orderDataUrl, $orderData);
- $getGoodsCateInfo = json_decode($getGoodsCateInfo,true);
- if ($getGoodsCateInfo['code'] != 0) {
- throw new \Exception($getGoodsCateInfo['msg']);
- }
- return [
- 'code' => 0,
- 'data' => [
- 'goods_id' => $getGoodsCateInfo['data']['goods_id']
- ]
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //同步商城订单售后状态1禁止发货 0可以发货
- public function syncMchOrderRefundStatus($status) {
- try {
- $order_id = $this->order_id;
- $orderTransit = OrderTransit::findOne(['order_id' => $order_id, 'is_delete' => 0, 'cancel_examine' => 0]);
- if (!$orderTransit) {
- return false;
- }
- $cloud_order_id = $orderTransit->cloud_order_id;
- $store_id = $orderTransit->store_id;
- $sync_url = "/cloud/mch/syncMchOrderRefundStatus";
- $sync_data = [];
- $sync_data['access_token'] = get_merchant_token(0, $store_id);
- $sync_data['order_id'] = $cloud_order_id;
- $sync_data['status'] = $status;
- $sync_info = cloud_post($this->domain . $sync_url, $sync_data);
- $sync_info = json_decode($sync_info,true);
- if ($sync_info['code'] != 0) {
- throw new \Exception($sync_info['msg']);
- }
- return true;
- } catch (\Exception $e) {
- return false;
- }
- }
- //取消售后订单
- public function orderRefundCancel($order_id = 0) {
- try {
- $orderTransit = OrderTransit::findOne(['order_id' => $order_id, 'is_delete' => 0]);
- if (!$orderTransit) {
- return [
- 'code' => 0,
- 'msg' => '非云仓订单'
- ];
- }
- $cloud_order_id = $orderTransit->cloud_order_id;
- $store_id = $orderTransit->store_id;
- $refundCancelUrl = "/cloud/mch/mchOrderRefundCancel";
- $refundCancelData = [];
- $refundCancelData['access_token'] = get_merchant_token(0, $store_id);
- $refundCancelData['order_id'] = $cloud_order_id;
- $refundCancelInfo = cloud_post($this->domain . $refundCancelUrl, $refundCancelData);
- $refundCancelInfo = json_decode($refundCancelInfo,true);
- if ($refundCancelInfo['code'] != 0) {
- throw new \Exception($refundCancelInfo['msg']);
- }
- return [
- 'code' => 0,
- 'msg' => "操作成功",
- 'data' => $refundCancelInfo
- ];
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- //返回数据
- private function outPutData($code,$msg,$data=[]){
- $putData = [];
- $putData['code'] = $code;
- $putData['msg'] = $msg;
- if($data) $putData['data'] = $data;
- return $putData;
- }
- //获取token方法
- private function getToken($name, $password) {
- $token = get_platform_token();
- $url = "/user/getToken";
- $data = [];
- $data['name'] = $name;
- $data['pwd'] = $password;
- $data['token'] = $token;
- $response = cloud_post($this->domain.$url,$data);
- return json_decode($response,true);
- }
- //获取网络图片到临时目录
- public function saveTempImage($url)
- {
- if (strpos($url, 'http') === false) {
- $url = 'http:' . trim($url);
- }
- if (!is_dir(\Yii::$app->runtimePath . '/image')) {
- mkdir(\Yii::$app->runtimePath . '/image');
- }
- $save_path = \Yii::$app->runtimePath . '/image/' . md5($url) . '.jpg';
- CurlHelper::download($url, $save_path);
- return $save_path;
- }
- public function getCloudGoodsInfo() {
- try {
- $goods_id = $this->goods_id;
- $goods = Goods::find()->where(['cloud_goods_id' => $goods_id, 'is_delete' => 0, 'store_id' => get_store_id()])->one();
- if ($goods) {
- return [
- 'code' => 0,
- 'msg' => '商品已存在',
- 'data' => [
- 'id' => $goods->id
- ]
- ];
- } else {
- return [
- 'code' => 0,
- 'msg' => '商品已存在',
- 'data' => []
- ];
- }
- } catch (\Exception $e) {
- return [
- 'code' => 1,
- 'msg' => $e->getMessage()
- ];
- }
- }
- }
|