PurchaseOrderClass.php 106 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259
  1. <?php
  2. namespace bizGhs\order\classes;
  3. use biz\ghs\classes\GhsClass;
  4. use biz\shop\classes\MainClass;
  5. use biz\shop\classes\ShopCapitalClass;
  6. use biz\shop\classes\ShopClass;
  7. use biz\shop\classes\ShopExtClass;
  8. use biz\sj\classes\SjClass;
  9. use biz\stat\classes\StatCgClass;
  10. use biz\stat\classes\StatCgGhsClass;
  11. use biz\stat\classes\StatOutClass;
  12. use biz\wx\classes\WxMessageClass;
  13. use bizGhs\base\classes\BaseClass;
  14. use bizGhs\book\classes\BookCustomClass;
  15. use bizGhs\book\classes\BookItemClass;
  16. use bizGhs\book\classes\BookItemCustomClass;
  17. use bizGhs\book\classes\BookItemGhsClass;
  18. use bizGhs\cg\classes\CgOrderItemClass;
  19. use bizGhs\cg\classes\CgOrderItemSendClass;
  20. use bizGhs\clear\classes\ClearClass;
  21. use bizGhs\clear\classes\OrderCgClearClass;
  22. use bizGhs\custom\classes\AccountMoneyClass;
  23. use bizGhs\custom\classes\CustomClass;
  24. use bizGhs\ghs\classes\GhsBalanceChangeClass;
  25. use bizGhs\item\classes\CostChangeClass;
  26. use bizGhs\item\classes\ItemClassClass;
  27. use bizGhs\order\traits\OrderTrait;
  28. use bizGhs\shop\classes\ShopAdminClass;
  29. use bizGhs\stock\classes\OnStockRecordClass;
  30. use bizGhs\stock\classes\StockRecordClass;
  31. use bizGhs\product\classes\ProductClass;
  32. use common\components\dict;
  33. use common\components\imgUtil;
  34. use common\components\noticeUtil;
  35. use common\components\orderSn;
  36. use common\components\printUtil;
  37. use common\components\stringUtil;
  38. use common\components\util;
  39. use Yii;
  40. //采购单 lqh 2021.1.20
  41. class PurchaseOrderClass extends BaseClass
  42. {
  43. use OrderTrait;
  44. public static $baseFile = '\bizGhs\order\models\PurchaseOrder';
  45. const PURCHASE_ORDER_STATUS_WAIT = 1; //待确认
  46. const PURCHASE_ORDER_STATUS_UN_SEND = 2; //待配送,待发货
  47. const PURCHASE_ORDER_STATUS_SENDING = 3; //配送中,待入库
  48. const PURCHASE_ORDER_STATUS_COMPLETE = 4; //已入库
  49. const PURCHASE_ORDER_STATUS_CANCEL = 5; //已取消
  50. public static $statusMap = [
  51. self::PURCHASE_ORDER_STATUS_WAIT => '待确认',
  52. self::PURCHASE_ORDER_STATUS_UN_SEND => '待配送',
  53. self::PURCHASE_ORDER_STATUS_SENDING => '待入库',
  54. self::PURCHASE_ORDER_STATUS_COMPLETE => '已入库',
  55. self::PURCHASE_ORDER_STATUS_CANCEL => '已取消',
  56. ];
  57. const DEBT_UNKNOWN = 0;
  58. const DEBT_YES = 1;
  59. const DEBT_NO = 2;
  60. /**
  61. * 挂账成立时写入原欠/剩余待结(两边相等,单位元)
  62. * @param object $order 采购单
  63. * @param string|float|null $amount 应付;空则用 actPrice
  64. */
  65. public static function initDebtPrices($order, $amount = null)
  66. {
  67. $pay = $amount !== null
  68. ? bcadd((string)$amount, '0', 2)
  69. : bcadd((string)($order->actPrice ?? '0'), '0', 2);
  70. $order->debtPrice = $pay;
  71. $order->remainDebtPrice = $pay;
  72. }
  73. /**
  74. * 入库/改价后校准:尚未部分消欠时两边同步为当前 actPrice
  75. */
  76. public static function syncDebtPricesIfFullPending($order)
  77. {
  78. if (intval($order->debt ?? 0) !== self::DEBT_YES) {
  79. return;
  80. }
  81. $debtPrice = bcadd((string)($order->debtPrice ?? '0'), '0', 2);
  82. $remain = bcadd((string)($order->remainDebtPrice ?? '0'), '0', 2);
  83. // 已有部分消欠(remain < debtPrice)时不覆盖原欠
  84. if (bccomp($debtPrice, '0', 2) > 0 && bccomp($debtPrice, $remain, 2) !== 0) {
  85. return;
  86. }
  87. $pay = bcadd((string)($order->actPrice ?? '0'), '0', 2);
  88. $order->debtPrice = $pay;
  89. $order->remainDebtPrice = $pay;
  90. }
  91. /**
  92. * 减少本单剩余待结;减到 ≤0 则标已结清。不改 debtPrice。
  93. * 消欠成功时若 clearId==0 则写 1(结账痕迹,供隔天售后判定)。
  94. * @return bool 是否本单因此从待结变为已结清
  95. */
  96. public static function reduceRemainDebtPrice($order, $amount)
  97. {
  98. $cut = bcadd((string)$amount, '0', 2);
  99. if (bccomp($cut, '0', 2) <= 0) {
  100. return false;
  101. }
  102. if (intval($order->debt ?? 0) !== self::DEBT_YES) {
  103. return false;
  104. }
  105. $remain = bcadd((string)($order->remainDebtPrice ?? '0'), '0', 2);
  106. // 历史单未回填 remain 时回落 actPrice
  107. if (bccomp($remain, '0', 2) <= 0) {
  108. $remain = bcadd((string)($order->actPrice ?? '0'), '0', 2);
  109. }
  110. $newRemain = bcsub($remain, $cut, 2);
  111. if (bccomp($newRemain, '0', 2) < 0) {
  112. $newRemain = '0.00';
  113. }
  114. $order->remainDebtPrice = $newRemain;
  115. $cleared = false;
  116. $saveAttrs = ['remainDebtPrice', 'debt'];
  117. if (bccomp($newRemain, '0', 2) <= 0) {
  118. $order->debt = self::DEBT_NO;
  119. $cleared = true;
  120. }
  121. // 售后返充 FIFO 消欠也算结账痕迹
  122. if (intval($order->clearId ?? 0) === 0) {
  123. $order->clearId = 1;
  124. $saveAttrs[] = 'clearId';
  125. }
  126. $order->save(false, $saveAttrs);
  127. return $cleared;
  128. }
  129. /**
  130. * 售后加余额后 FIFO 消采购挂账:只减 remainDebtPrice,不再加余额。
  131. * @param int $ghsId 买方供货商
  132. * @param string|float $amount 本次入账金额
  133. * @return string 实际消欠合计
  134. * @deprecated 采购售后返充请用 fifoClearRemainAfterCreditWithBill(会建结账单)
  135. */
  136. public static function fifoClearRemainAfterCredit($ghsId, $amount)
  137. {
  138. $pool = bcadd((string)$amount, '0', 2);
  139. if (bccomp($pool, '0', 2) <= 0 || intval($ghsId) <= 0) {
  140. return '0.00';
  141. }
  142. $orderList = self::getAllByCondition(
  143. ['ghsId' => intval($ghsId), 'debt' => self::DEBT_YES],
  144. 'id asc',
  145. '*',
  146. null,
  147. true
  148. );
  149. if (empty($orderList)) {
  150. return '0.00';
  151. }
  152. $used = '0.00';
  153. $clearedCount = 0;
  154. foreach ($orderList as $order) {
  155. $left = bcsub($pool, $used, 2);
  156. if (bccomp($left, '0', 2) <= 0) {
  157. break;
  158. }
  159. $remain = bcadd((string)($order->remainDebtPrice ?? '0'), '0', 2);
  160. if (bccomp($remain, '0', 2) <= 0) {
  161. $remain = bcadd((string)($order->actPrice ?? '0'), '0', 2);
  162. }
  163. if (bccomp($remain, '0', 2) <= 0) {
  164. continue;
  165. }
  166. $cut = bccomp($remain, $left, 2) <= 0 ? $remain : $left;
  167. if (self::reduceRemainDebtPrice($order, $cut)) {
  168. $clearedCount++;
  169. }
  170. $used = bcadd($used, $cut, 2);
  171. }
  172. if ($clearedCount > 0) {
  173. $ghs = GhsClass::getLockById(intval($ghsId));
  174. if (!empty($ghs)) {
  175. $ghs->debtNum = max(0, intval($ghs->debtNum ?? 0) - $clearedCount);
  176. $ghs->debt = bccomp((string)($ghs->balance ?? '0'), '0', 2) < 0
  177. ? GhsClass::DEBT_YES
  178. : (intval($ghs->debtNum) > 0 ? GhsClass::DEBT_YES : GhsClass::DEBT_NO);
  179. $ghs->save(false, ['debtNum', 'debt']);
  180. $customId = intval($ghs->customId ?? 0);
  181. if ($customId > 0) {
  182. $custom = CustomClass::getLockById($customId);
  183. if (!empty($custom)) {
  184. $custom->debtNum = max(0, intval($custom->debtNum ?? 0) - $clearedCount);
  185. $custom->isDebt = bccomp((string)($custom->balance ?? '0'), '0', 2) < 0
  186. ? CustomClass::IS_DEBT_YES
  187. : (intval($custom->debtNum) > 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO);
  188. $custom->save(false, ['debtNum', 'isDebt']);
  189. }
  190. }
  191. }
  192. }
  193. return $used;
  194. }
  195. /**
  196. * 【用途】采购售后返充后消欠并生成结账单,回写余额流水 clear 字段。
  197. * 【为什么】对齐销售侧:先结当前售后采购单(可部分结),剩余再按 id FIFO。
  198. *
  199. * @param object $ghs 买方供货商(已锁、余额已加完)
  200. * @param string|float $amount 本次返充金额
  201. * @param object $refund 采购售后单
  202. * @param int $sjId
  203. * @param int $shopId
  204. * @param object|null $preferOrder 当前售后关联采购单,空则整笔 FIFO
  205. * @return array{clearId:int,clearSn:string,clearAmount:string}
  206. */
  207. public static function fifoClearRemainAfterCreditWithBill($ghs, $amount, $refund, $sjId, $shopId, $preferOrder = null)
  208. {
  209. $empty = ['clearId' => 0, 'clearSn' => '', 'clearAmount' => '0.00'];
  210. $pool = bcadd((string)$amount, '0', 2);
  211. $ghsId = intval($ghs->id ?? 0);
  212. if (bccomp($pool, '0', 2) <= 0 || $ghsId <= 0) {
  213. return $empty;
  214. }
  215. $orderList = self::getAllByCondition(
  216. ['ghsId' => $ghsId, 'debt' => self::DEBT_YES],
  217. 'id asc',
  218. '*',
  219. null,
  220. true
  221. );
  222. if (empty($orderList)) {
  223. return $empty;
  224. }
  225. $plan = [];
  226. $used = '0.00';
  227. $preferOrderId = !empty($preferOrder) ? intval($preferOrder->id ?? 0) : 0;
  228. // 1)优先结当前售后采购单(可部分结)
  229. if ($preferOrderId > 0) {
  230. foreach ($orderList as $order) {
  231. if (intval($order->id ?? 0) !== $preferOrderId) {
  232. continue;
  233. }
  234. $remain = bcadd((string)($order->remainDebtPrice ?? '0'), '0', 2);
  235. if (bccomp($remain, '0', 2) <= 0) {
  236. $remain = bcadd((string)($order->actPrice ?? '0'), '0', 2);
  237. }
  238. if (bccomp($remain, '0', 2) <= 0) {
  239. break;
  240. }
  241. $cut = bccomp($remain, $pool, 2) <= 0 ? $remain : $pool;
  242. $plan[] = [
  243. 'cgId' => $preferOrderId,
  244. 'clearAmount' => $cut,
  245. 'orderSn' => (string)($order->orderSn ?? ''),
  246. ];
  247. $used = bcadd($used, $cut, 2);
  248. break;
  249. }
  250. }
  251. // 2)剩余金额按 id 升序 FIFO(跳过已优先的本单)
  252. foreach ($orderList as $order) {
  253. $left = bcsub($pool, $used, 2);
  254. if (bccomp($left, '0', 2) <= 0) {
  255. break;
  256. }
  257. $oid = intval($order->id ?? 0);
  258. if ($preferOrderId > 0 && $oid === $preferOrderId) {
  259. continue;
  260. }
  261. $remain = bcadd((string)($order->remainDebtPrice ?? '0'), '0', 2);
  262. if (bccomp($remain, '0', 2) <= 0) {
  263. $remain = bcadd((string)($order->actPrice ?? '0'), '0', 2);
  264. }
  265. if (bccomp($remain, '0', 2) <= 0) {
  266. continue;
  267. }
  268. $cut = bccomp($remain, $left, 2) <= 0 ? $remain : $left;
  269. $plan[] = [
  270. 'cgId' => $oid,
  271. 'clearAmount' => $cut,
  272. 'orderSn' => (string)($order->orderSn ?? ''),
  273. ];
  274. $used = bcadd($used, $cut, 2);
  275. }
  276. if (empty($plan)) {
  277. return $empty;
  278. }
  279. $clear = ClearClass::settlePurchaseFifoAfterCredit($ghs, $plan, [
  280. 'shopId' => intval($shopId),
  281. 'sjId' => intval($sjId),
  282. 'staffId' => intval($refund->shopAdminId ?? 0),
  283. 'staffName' => (string)($refund->shopAdminName ?? ''),
  284. 'payWay' => dict::getDict('payWay', 'balancePay'),
  285. 'remark' => '采购售后返充余额自动销欠,售后单:' . ($refund->refundSn ?? ''),
  286. ]);
  287. if (empty($clear)) {
  288. return $empty;
  289. }
  290. $clearId = intval($clear->id ?? 0);
  291. $clearSn = (string)($clear->orderSn ?? '');
  292. $clearAmount = bcadd((string)($clear->actPrice ?? $used), '0', 2);
  293. ClearClass::patchGhsBalanceChangeClearAudit($ghsId, intval($refund->id ?? 0), $clear, $clearAmount);
  294. return [
  295. 'clearId' => $clearId,
  296. 'clearSn' => $clearSn,
  297. 'clearAmount' => $clearAmount,
  298. ];
  299. }
  300. //寄付与到付
  301. const YF_PAY_WAY_JF = 1;
  302. const YF_PAY_WAY_DF = 2;
  303. //稍后入库和直接入库
  304. const IN_TYPE_LATER = 0;
  305. const IN_TYPE_NOW = 1;
  306. /**
  307. * 采购挂账:买方 xhGhs 净 balance 减少,并记余额变动(合并后模型,不再写欠款字段)
  308. */
  309. public static function applyPurchaseDebtOnBuyerGhs($ghs, $amount, $purchase, $sjId, $shopId)
  310. {
  311. AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
  312. $ghsNewBalance = bcsub($ghs->balance ?? '0.00', $amount, 2);
  313. $ghs->debt = GhsClass::DEBT_YES;
  314. $ghs->balance = $ghsNewBalance;
  315. $ghs->debtNum += 1;
  316. $ghs->expendAmount = bcadd($ghs->expendAmount ?? '0.00', $amount, 2);
  317. $ghs->expendNum += 1;
  318. $ghs->save(false, ['debt', 'balance', 'debtNum', 'expendAmount', 'expendNum']);
  319. $orderSn = $purchase->orderSn ?? '';
  320. $capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
  321. GhsBalanceChangeClass::add([
  322. 'ghsId' => $ghs->id ?? 0,
  323. 'relateId' => $purchase->id ?? 0,
  324. 'ptStyle' => 2,
  325. 'capitalType' => $capitalType,
  326. 'amount' => $amount,
  327. 'balance' => $ghsNewBalance,
  328. 'staffId' => $purchase->shopAdminId ?? 0,
  329. 'staffName' => $purchase->shopAdminName ?? '',
  330. 'io' => 0,
  331. 'event' => '新增采购 ' . $orderSn,
  332. 'sjId' => $sjId,
  333. 'shopId' => $shopId,
  334. ], true);
  335. return $ghs;
  336. }
  337. /**
  338. * 采购挂账:供货方 custom 净 balance 同步减少(双边关系)
  339. */
  340. public static function applyPurchaseDebtOnSupplierCustom($ghs, $amount, $ghsMain = null)
  341. {
  342. $customId = $ghs->customId ?? 0;
  343. $custom = CustomClass::getLockById($customId);
  344. if (empty($custom)) {
  345. util::fail('没有找到客户信息');
  346. }
  347. AccountMoneyClass::ensureCustomMoneyReady($custom, true);
  348. $custom->isDebt = CustomClass::IS_DEBT_YES;
  349. $custom->balance = bcsub($custom->balance ?? '0.00', $amount, 2);
  350. if ($custom->debtNum == 0 && $ghsMain !== null) {
  351. $ghsMain->mayGatheringNum += 1;
  352. }
  353. $custom->debtNum += 1;
  354. $custom->buyNum += 1;
  355. $custom->buyAmount = bcadd($custom->buyAmount ?? '0.00', $amount, 2);
  356. $custom->save();
  357. return $custom;
  358. }
  359. /**
  360. * 采购售后退款:买方 xhGhs 净 balance 增加 + 流水;供货方 custom 同步。
  361. * @param bool $doFifo 是否用本次金额 FIFO 消其它挂账;当天已减本单 remain 时传 false,隔天传 true
  362. */
  363. public static function applyPurchaseRefundOnAccounts($ghs, $amount, $refund, $order, $sjId, $shopId, $doFifo = true)
  364. {
  365. AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
  366. $ghsNewBalance = bcadd($ghs->balance ?? '0.00', $amount, 2);
  367. $ghs->balance = $ghsNewBalance;
  368. // 本单刚因当天售后结清时减 debtNum(隔天 FIFO 内部自行处理)
  369. $clearOrderDebt = !empty($order->_purchaseDebtClearedByRefund);
  370. $orderSaveAttrs = [];
  371. if (!$clearOrderDebt && intval($order->debt ?? 0) === self::DEBT_YES
  372. && bccomp((string)($order->actPrice ?? '1'), '0', 2) <= 0
  373. && bccomp((string)($order->remainDebtPrice ?? '1'), '0', 2) <= 0) {
  374. $order->debt = self::DEBT_NO;
  375. $order->remainDebtPrice = '0.00';
  376. $orderSaveAttrs = ['debt', 'remainDebtPrice'];
  377. $clearOrderDebt = true;
  378. }
  379. // 采购售后返充余额:本单 clearId==0 打结账痕迹 1(与销售侧语义一致)
  380. if (intval($order->clearId ?? 0) === 0) {
  381. $order->clearId = 1;
  382. $orderSaveAttrs[] = 'clearId';
  383. }
  384. if (!empty($orderSaveAttrs)) {
  385. $order->save(false, array_values(array_unique($orderSaveAttrs)));
  386. }
  387. if ($clearOrderDebt) {
  388. $ghs->debtNum = max(0, intval($ghs->debtNum ?? 0) - 1);
  389. }
  390. $ghs->debt = bccomp($ghsNewBalance, '0', 2) < 0 ? GhsClass::DEBT_YES : GhsClass::DEBT_NO;
  391. $ghs->save(false, ['balance', 'debtNum', 'debt']);
  392. $orderSn = $order->orderSn ?? '';
  393. $refundSn = $refund->refundSn ?? '';
  394. $capitalType = dict::getDict('capitalType', 'ghsCgOrderRefund', 'id');
  395. GhsBalanceChangeClass::add([
  396. 'ghsId' => $ghs->id ?? 0,
  397. 'relateId' => $refund->id ?? 0,
  398. 'ptStyle' => 2,
  399. 'capitalType' => $capitalType,
  400. 'amount' => $amount,
  401. 'balance' => $ghsNewBalance,
  402. 'io' => 1,
  403. 'staffId' => $refund->shopAdminId ?? 0,
  404. 'staffName' => $refund->shopAdminName ?? '',
  405. 'event' => '采购单 ' . $orderSn . ' 申请售后 ' . $refundSn,
  406. 'sjId' => $sjId,
  407. 'shopId' => $shopId,
  408. ], true);
  409. $customId = $ghs->customId ?? 0;
  410. if ($customId > 0) {
  411. $custom = CustomClass::getLockById($customId);
  412. if (!empty($custom)) {
  413. AccountMoneyClass::ensureCustomMoneyReady($custom, true);
  414. $customNewBalance = bcadd($custom->balance ?? '0.00', $amount, 2);
  415. $custom->balance = $customNewBalance;
  416. if ($clearOrderDebt) {
  417. $custom->debtNum = max(0, intval($custom->debtNum ?? 0) - 1);
  418. }
  419. $custom->isDebt = bccomp($customNewBalance, '0', 2) < 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO;
  420. $saveAttrs = ['balance', 'isDebt'];
  421. if ($clearOrderDebt) {
  422. $saveAttrs[] = 'debtNum';
  423. }
  424. $custom->save(false, $saveAttrs);
  425. }
  426. }
  427. // 隔天等:入账后消挂账并生成结账单(优先当前售后采购单);当天已减本单 remain 则不再消,避免重复
  428. if ($doFifo) {
  429. self::fifoClearRemainAfterCreditWithBill($ghs, $amount, $refund, $sjId, $shopId, $order);
  430. }
  431. return $ghs;
  432. }
  433. /**
  434. * 采购结账:买方 xhGhs 净 balance 增加 + 流水
  435. */
  436. public static function applyPurchaseClearOnBuyerGhs($ghs, $amount, $clear, $orderCount)
  437. {
  438. AccountMoneyClass::ensureGhsMoneyReady($ghs, true);
  439. $ghsNewBalance = bcadd($ghs->balance ?? '0.00', $amount, 2);
  440. $ghs->balance = $ghsNewBalance;
  441. $ghs->debtNum -= $orderCount;
  442. $ghs->debt = bccomp($ghsNewBalance, '0', 2) < 0 ? GhsClass::DEBT_YES : GhsClass::DEBT_NO;
  443. $ghs->save(false, ['balance', 'debtNum', 'debt']);
  444. $capitalType = dict::getDict('capitalType', 'ghsCgOrderClear', 'id');
  445. $clearId = intval($clear->id ?? 0);
  446. $clearSn = (string)($clear->orderSn ?? '');
  447. GhsBalanceChangeClass::add([
  448. 'ghsId' => $ghs->id ?? 0,
  449. 'relateId' => $clearId,
  450. 'ptStyle' => 2,
  451. 'capitalType' => $capitalType,
  452. 'amount' => $amount,
  453. 'balance' => $ghsNewBalance,
  454. 'staffId' => $clear->customShopAdminId ?? 0,
  455. 'staffName' => $clear->customShopAdminName ?? '',
  456. 'io' => 1,
  457. 'event' => '采购单结账 ' . $clearSn,
  458. 'sjId' => $clear->sjId ?? 0,
  459. 'shopId' => $clear->shopId ?? 0,
  460. // 直接挂结账审计,列表无需再猜 relateId
  461. 'clearId' => $clearId,
  462. 'clearSn' => $clearSn,
  463. 'clearAmount' => bcadd((string)$amount, '0', 2),
  464. ], true);
  465. return $ghs;
  466. }
  467. /**
  468. * 采购结账:供货方 custom 净 balance 同步增加
  469. */
  470. public static function applyPurchaseClearOnSupplierCustom($ghs, $amount, $orderCount, $ghsMain = null)
  471. {
  472. $customId = $ghs->customId ?? 0;
  473. $custom = CustomClass::getLockById($customId);
  474. if (empty($custom)) {
  475. util::fail('没有找到客户信息');
  476. }
  477. AccountMoneyClass::ensureCustomMoneyReady($custom, true);
  478. $customNewBalance = bcadd($custom->balance ?? '0.00', $amount, 2);
  479. $custom->balance = $customNewBalance;
  480. $custom->debtNum -= $orderCount;
  481. $custom->isDebt = bccomp($customNewBalance, '0', 2) < 0 ? CustomClass::IS_DEBT_YES : CustomClass::IS_DEBT_NO;
  482. $custom->save(false, ['balance', 'debtNum', 'isDebt']);
  483. if ($custom->debtNum == 0 && $ghsMain !== null) {
  484. $ghsMain->mayGatheringNum -= 1;
  485. $ghsMain->save();
  486. }
  487. return $custom;
  488. }
  489. public static function assign($cg, $shop, $params)
  490. {
  491. $bookSn = $cg->bookSn ?? 0;
  492. $orderSn = $cg->orderSn ?? '';
  493. $cgId = $cg->id ?? 0;
  494. $mainId = $cg->mainId ?? 0;
  495. $ghsId = $cg->ghsId ?? 0;
  496. $ghsName = $cg->ghsName ?? '';
  497. $cgItemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  498. if (empty($cgItemList)) {
  499. return [];
  500. }
  501. $ids = [];
  502. foreach ($cgItemList as $cgItem) {
  503. $ids[] = $cgItem->productId;
  504. }
  505. $productInfoList = ProductClass::getAllByCondition(['id' => ['in', $ids]], null, '*', 'id', true);
  506. $hasChange = 0;
  507. foreach ($cgItemList as $cgItem) {
  508. $cgItemId = $cgItem->id ?? 0;
  509. $cgItemNum = $cgItem->itemNum ?? 0;
  510. $productId = $cgItem->productId ?? 0;
  511. $ptItemId = $cgItem->itemId ?? 0;
  512. $assignSeat = $cgItem->assignSeat;
  513. $noAssignHasDelNeedCg = $cgItem->noAssignHasDelNeedCg;
  514. //不需要分配货位号的花材,已经减待采数
  515. if ($assignSeat == 0 && $noAssignHasDelNeedCg == 1) {
  516. continue;
  517. }
  518. if ($cgItemNum <= 0) {
  519. continue;
  520. }
  521. $currentItemInfo = $productInfoList[$productId] ?? null;
  522. if (empty($currentItemInfo)) {
  523. util::fail('花材信息缺失');
  524. }
  525. $bookItemRes = BookItemClass::getByCondition(['bookSn' => $bookSn, 'itemId' => $productId], true);
  526. if (empty($bookItemRes)) {
  527. //没有预订花材则不需要分配货位
  528. continue;
  529. }
  530. $bookItemCustomList = BookItemCustomClass::getAllByCondition(['bookSn' => $bookSn, 'itemId' => $productId], null, '*', null, true);
  531. if (empty($bookItemCustomList)) {
  532. //没有预订的花材和客户则不需要分配货位
  533. continue;
  534. }
  535. $customIds = [];
  536. foreach ($bookItemCustomList as $bookItemCustomRes) {
  537. $customIds[] = $bookItemCustomRes->customId;
  538. }
  539. $customList = CustomClass::getAllByCondition(['id' => ['in', $customIds]], null, '*', 'id', true);
  540. foreach ($bookItemCustomList as $bookItemCustomRes) {
  541. $customId = $bookItemCustomRes->customId ?? 0;
  542. $bookCustomRes = BookCustomClass::getByCondition(['bookSn' => $bookSn, 'customId' => $customId], true);
  543. if (empty($bookCustomRes)) {
  544. noticeUtil::push("采购单 {$orderSn} 分配货位时,发现 bookCustom 数据有问题", '15280215347');
  545. util::fail("bookCustom 数据有问题");
  546. }
  547. $confirmSendNum = $cgItem->confirmSendNum ?? 0;
  548. $remainCouldSendNum = bcsub($cgItemNum, $confirmSendNum, 2);
  549. if ($remainCouldSendNum <= 0) {
  550. //剩余应分配数量不足则退出本循环
  551. break;
  552. }
  553. $needCgNum = $bookItemCustomRes->needCgNum ?? 0;
  554. if ($needCgNum <= 0) {
  555. //此客户花材,没有要采购的数量,则找下一个
  556. continue;
  557. }
  558. if ($needCgNum >= $remainCouldSendNum) {
  559. $remainNeedSendNum = $remainCouldSendNum;
  560. } else {
  561. $remainNeedSendNum = $needCgNum;
  562. }
  563. if ($remainNeedSendNum <= 0) {
  564. //剩余要分配数量不足则退出本循环
  565. break;
  566. }
  567. //待采数量减少
  568. $params['currentChangeType'] = 'addCg';
  569. $params['customName'] = $bookItemCustomRes->customName ?? '';
  570. $params['customId'] = $bookItemCustomRes->customId ?? '';
  571. $params['customNamePy'] = $bookItemCustomRes->customNamePy ?? '';
  572. if ($assignSeat == 1) {
  573. $bookItemCustomRes = BookItemCustomClass::delNeedCgNum($bookItemCustomRes, $remainNeedSendNum, 0, $cg, $shop, $currentItemInfo, $params);
  574. $bookCustomRes = BookCustomClass::delNeedCgNum($bookCustomRes, $remainNeedSendNum, 0, $cg, $shop, $currentItemInfo, $params);
  575. }
  576. $bookItemRes = BookItemClass::delNeedCgNum($bookItemRes, $remainNeedSendNum, 0, $cg, $shop, $currentItemInfo, $params);
  577. $hasChange = 1;
  578. if ($assignSeat == 1) {
  579. //路上数量增加
  580. $bookItemCustomRes = BookItemCustomClass::addRoadNum($bookItemCustomRes, $remainNeedSendNum, $cg, $shop, $currentItemInfo, $params);
  581. $bookCustomRes = BookCustomClass::addRoadNum($bookCustomRes, $remainNeedSendNum, $cg, $shop, $currentItemInfo, $params);
  582. $bookItemRes = BookItemClass::addRoadNum($bookItemRes, $remainNeedSendNum, $cg, $shop, $currentItemInfo, $params);
  583. //确认已分配数量增加
  584. $cgItem->confirmSendNum = bcadd($cgItem->confirmSendNum, $remainNeedSendNum);
  585. $cgItem->save();
  586. $sendInfo = CgOrderItemSendClass::getByCondition(['parentId' => $cgItemId, 'customId' => $customId, 'itemId' => $productId], true);
  587. $bookItemCustomId = $bookItemCustomRes->id ?? 0;
  588. $currentCustom = $customList[$customId] ?? null;
  589. if (empty($currentCustom)) {
  590. util::fail('客户信息缺失,编号545');
  591. }
  592. if (empty($sendInfo)) {
  593. $sendData = [
  594. 'mainId' => $mainId,
  595. 'parentOrderId' => $cgId,
  596. 'parentId' => $cgItemId,
  597. 'bookGhsGiveId' => 0,
  598. 'name' => $currentItemInfo->name ?? '',
  599. 'py' => $currentItemInfo->py ?? '',
  600. 'itemId' => $productId,
  601. 'ptItemId' => $ptItemId,
  602. 'customId' => $customId,
  603. 'customName' => $currentCustom->name ?? '',
  604. 'customPy' => $currentCustom->py ?? '',
  605. 'seatSn' => $currentCustom->seatSn ?? 0,
  606. 'num' => 0,
  607. 'bookItemCustomId' => $bookItemCustomId,
  608. 'status' => 0,
  609. 'staffId' => $currentItemInfo->cgStaffId ?? 0,
  610. 'staffName' => $currentItemInfo->cgStaffName ?? '',
  611. ];
  612. $sendInfo = CgOrderItemSendClass::add($sendData, true);
  613. }
  614. $sendInfo->num = bcadd($sendInfo->num, $remainNeedSendNum);
  615. $sendInfo->save();
  616. $sendInfoId = $sendInfo->id ?? 0;
  617. $giveGhs = BookItemGhsClass::getByCondition(['parentId' => $bookItemCustomId, 'ghsId' => $ghsId, 'itemId' => $productId,], true);
  618. if (empty($giveGhs)) {
  619. $giveGhsData = [
  620. 'mainId' => $mainId,
  621. 'bookSn' => $bookSn,
  622. 'parentId' => $bookItemCustomId,
  623. 'cgSendId' => $sendInfoId,
  624. 'name' => $currentItemInfo->name ?? '',
  625. 'py' => $currentItemInfo->py ?? '',
  626. 'itemId' => $productId,
  627. 'ptItemId' => $ptItemId,
  628. 'ghsId' => $ghsId,
  629. 'ghsName' => $ghsName,
  630. 'cgId' => $cgId,
  631. 'cgItemId' => $cgItemId,
  632. 'customId' => $customId,
  633. 'customName' => $currentCustom->name ?? '',
  634. 'staffId' => $currentItemInfo->cgStaffId ?? 0,
  635. 'staffName' => $currentItemInfo->cgStaffName ?? '',
  636. 'status' => 0,
  637. ];
  638. $giveGhs = BookItemGhsClass::add($giveGhsData, true);
  639. }
  640. $giveGhs->num = bcadd($giveGhs->num, $remainNeedSendNum);
  641. $giveGhs->save();
  642. $giveGhsId = $giveGhs->id ?? 0;
  643. $sendInfo->bookGhsGiveId = $giveGhsId;
  644. $sendInfo->save();
  645. } else {
  646. $cgItem->noAssignHasDelNeedCg = 1;
  647. $cgItem->save();
  648. break;
  649. }
  650. }
  651. }
  652. return ['hasChange' => $hasChange];
  653. }
  654. //取消采购入库 ssh 20231218
  655. public static function cancel($cg, $shop, $params)
  656. {
  657. $cg->status = self::PURCHASE_ORDER_STATUS_CANCEL;
  658. $cg->save();
  659. $orderSn = $cg->orderSn ?? '';
  660. $mainId = $cg->mainId ?? 0;
  661. $sjId = $cg->sjId ?? 0;
  662. $shopId = $cg->shopId ?? 0;
  663. $ghsName = $cg->ghsName ?? '';
  664. $itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  665. if (empty($itemList)) {
  666. util::fail('没有找到花材');
  667. }
  668. foreach ($itemList as $cgKey => $cgItem) {
  669. $productId = $cgItem['productId'] ?? 0;
  670. $currentNum = $cgItem['itemNum'] ?? 0;
  671. $ptItemId = $cgItem['itemId'] ?? 0;
  672. $presell = $cgItem['presell'] ?? 0;
  673. $product = ProductClass::getLockById($productId);
  674. if (empty($product)) {
  675. util::fail('没有找到花材哦');
  676. }
  677. if ($presell == 0) {
  678. //减少路上库存
  679. $onRecord = [];
  680. $onRecord['sjId'] = $sjId;
  681. $onRecord['shopId'] = $shopId;
  682. $onRecord['mainId'] = $mainId;
  683. $onRecord['orderSn'] = $orderSn;
  684. $onRecord['productId'] = $productId;
  685. $onRecord['itemId'] = $ptItemId;
  686. $onRecord['relateName'] = $ghsName;
  687. $onRecord['itemNum'] = $currentNum;
  688. $oldOnStock = $product->onStock ?? 0;
  689. $onRecord['oldStock'] = $oldOnStock;
  690. $onRecord['newStock'] = bcsub($oldOnStock, $currentNum);
  691. $onRecord['ptStyle'] = 2;
  692. OnStockRecordClass::cancelCg($onRecord);
  693. ProductClass::decreaseOnStockByItemNum($productId, $currentNum);
  694. }
  695. }
  696. $bookSn = $cg->bookSn ?? 0;
  697. if ($shop->pfLevel == 1 && !empty($shop->bookSn)) {
  698. if ($shop->bookSn == $bookSn) {
  699. //取消分配的货位
  700. $key = 'ghs_cg_order_scan_seat_' . $cg->id;
  701. $noLock = util::lock($key);
  702. if ($noLock) {
  703. CgOrderItemClass::cancelAssign($cg, $shop, $params);
  704. util::unlock($key);
  705. } else {
  706. util::fail('正在分配货位,请1秒后再取消');
  707. }
  708. }
  709. }
  710. }
  711. public static function printGroup($current, $content)
  712. {
  713. //58mm的机器,一行打印16个汉字,32个字母
  714. $bigPrice = $current['bigPrice'] ?? '';
  715. $bigPrice = floatval($bigPrice);
  716. $bigUnit = $current['bigUnit'] ?? '扎';
  717. $aboutPrice = $current['aboutPrice'] ?? 0;
  718. $aboutString = $aboutPrice == 1 ? '约' : '';
  719. $name = $current['name'] ?? '';
  720. if (floatval($bigPrice) == 0.01) {
  721. $name = $name . '(送)';
  722. }
  723. $count = $current['itemNum'] ?? '';
  724. $count = floatval($count);
  725. $content .= '<B>' . $name . ' ' . $count . $bigUnit . '</B><BR>';
  726. $totalPrice = $current['totalPrice'] ?? 0;
  727. $content .= $count . $bigUnit . 'x' . $aboutString . $bigPrice . '元' . '=' . $totalPrice . '元<BR>';
  728. if (isset($current['refundNum']) && $current['refundNum'] > 0) {
  729. $content .= '<B>已退' . $current['refundNum'] . $bigUnit . '</B><BR>';
  730. }
  731. $content .= '--------------------------------<BR>';
  732. return $content;
  733. }
  734. public static function addPrintNum($cg, $num = 1)
  735. {
  736. $cg->printNum += $num;
  737. $cg->save();
  738. if ($cg->printNum > 1) {
  739. $mainId = $cg->mainId ?? 0;
  740. if (getenv('YII_ENV') == 'production') {
  741. //小向花卉采购单第二次打通要通知老板
  742. if ($mainId == 23390) {
  743. WxMessageClass::ghsCgRepeatPrintInform($cg);
  744. }
  745. if ($mainId == 50) {
  746. //WxMessageClass::ghsCgRepeatPrintInform($cg);
  747. }
  748. } else {
  749. if ($mainId == 644) {
  750. //WxMessageClass::ghsCgRepeatPrintInform($cg);
  751. }
  752. }
  753. }
  754. return $cg;
  755. }
  756. //打印小票 ssh 20230608
  757. public static function printTicket($orderInfo, $firstPrint = true, $order = 1)
  758. {
  759. $ghsId = $orderInfo->ghsId ?? 0;
  760. $debtCount = PurchaseOrderClass::getCount(['ghsId' => $ghsId, 'debt' => PurchaseOrderClass::DEBT_YES]);
  761. $debtAmount = PurchaseOrderClass::sum(['ghsId' => $ghsId, 'debt' => PurchaseOrderClass::DEBT_YES], 'actPrice');
  762. $orderSn = $orderInfo->orderSn ?? '';
  763. $itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*');
  764. $shopId = $orderInfo->shopId ?? 0;
  765. $mainId = $orderInfo->mainId ?? 0;
  766. $shop = ShopClass::getById($shopId, true);
  767. $telephone = $shop->telephone ?? '';
  768. $sjId = $shop->sjId ?? 0;
  769. $sj = SjClass::getById($sjId, true);
  770. $sjName = $sj->name ?? '';
  771. $shopName = $shop->shopName ?? '';
  772. $currentName = $shopName == '首店' ? $sjName : $sjName . ' ' . $shopName;
  773. if (getenv('YII_ENV') == 'production') {
  774. //小向的采购不允许打小票
  775. if (in_array($mainId, [23390])) {
  776. return true;
  777. }
  778. }
  779. $content = "<CB>" . $currentName . "</CB>";
  780. $content .= "<CB>采购单</CB>";
  781. $content .= '--------------------------------<BR>';
  782. $content .= '<C>供货商</C><BR>';
  783. $content .= '<CB>' . $orderInfo->ghsName . '</CB><BR>';
  784. $shortOrderSn = substr($orderSn, -4);
  785. $content .= '<CB>核销码:' . $shortOrderSn . '</CB><BR>';
  786. $seatSn = $orderInfo->seatSn ?? '';
  787. if (!empty($seatSn)) {
  788. $content .= '<CB>货位号:' . $seatSn . '</CB><BR>';
  789. }
  790. $currentTime = date("Y-m-d H:i", strtotime($orderInfo->entryTime));
  791. $content .= '<C>' . $currentTime . '</C><BR>';
  792. if (isset($orderInfo->remark) && !empty($orderInfo->remark)) {
  793. $content .= '<BR>';
  794. $content .= '备注:<BR>';
  795. $content .= '<B>' . $orderInfo->remark . '</B><BR>';
  796. }
  797. $content .= '<BR>';
  798. $content .= '商品 数量/单价 金额 <BR>';
  799. $content .= '--------------------------------<BR>';
  800. if (isset($itemList) && !empty($itemList)) {
  801. foreach ($itemList as $current) {
  802. $content = self::printGroup($current, $content);
  803. }
  804. }
  805. $kindNum = $orderInfo->kind ?? 0;
  806. $bigNum = $orderInfo->itemNum ? floatval($orderInfo->itemNum) : 0;
  807. if (!empty($itemList)) {
  808. $kindNum = 0;
  809. $bigNum = 0;
  810. foreach ($itemList as $row) {
  811. $itemNumRow = floatval($row['itemNum'] ?? 0);
  812. $refundNumRow = floatval($row['refundNum'] ?? 0);
  813. $netNum = max(0, $itemNumRow - $refundNumRow);
  814. if ($netNum > 0) {
  815. $kindNum++;
  816. $bigNum += $netNum;
  817. }
  818. }
  819. }
  820. $content .= '<BR>********************************<BR>';
  821. $content .= "<CB>共{$kindNum}种";
  822. $content .= " 数量:";
  823. if ($bigNum > 0) {
  824. $content .= "{$bigNum}";
  825. }
  826. $content .= "</CB>";
  827. $content .= '********************************<BR><BR>';
  828. if (isset($orderInfo['itemPrice']) && $orderInfo['itemPrice'] > 0) {
  829. $content .= '商品金额:' . floatval($orderInfo['itemPrice']) . '元<BR>';
  830. }
  831. if (isset($orderInfo['sendCost']) && $orderInfo['sendCost'] > 0) {
  832. $content .= ' 运费:' . floatval($orderInfo['sendCost']) . '元<BR>';
  833. }
  834. if (isset($orderInfo['packCost']) && $orderInfo['packCost'] > 0) {
  835. $content .= ' 打包费:' . floatval($orderInfo['packCost']) . '元<BR>';
  836. }
  837. $content .= '合计金额:' . floatval($orderInfo['prePrice']) . '元<BR>';
  838. if (isset($orderInfo['discountAmount']) && !empty($orderInfo['discountAmount']) && $orderInfo['discountAmount'] > 0) {
  839. $content .= '优惠扣除:-' . floatval($orderInfo['discountAmount']) . '元<BR>';
  840. }
  841. if (isset($orderInfo['tkPrice']) && $orderInfo['tkPrice'] > 0) {
  842. $content .= '退款金额:' . floatval($orderInfo['tkPrice']) . '元<BR>';
  843. }
  844. $content .= '<B>订单金额:' . floatval($orderInfo['realPrice']) . '</B><BR>';
  845. if (isset($orderInfo['debt']) && $orderInfo['debt'] == 1) {
  846. $content .= '实付金额:0元<BR>';
  847. } else {
  848. $content .= '实付金额:' . floatval($orderInfo['realPrice']) . '元<BR>';
  849. }
  850. // if ($debtAmount > 0) {
  851. // $content .= '累计待结:' . $debtCount . '笔 ' . floatval($debtAmount) . '元<BR>';
  852. // }
  853. $content .= '--------------------------------<BR>';
  854. $content .= '订单编号:' . $orderInfo['orderSn'] . '<BR>';
  855. if (!empty($telephone)) {
  856. $content .= '联系电话:' . $telephone . '<BR>';
  857. }
  858. if (isset($orderInfo['shopAdminName']) && !empty($orderInfo['shopAdminName'])) {
  859. $content .= '录入人员:' . $orderInfo['shopAdminName'] . '<BR>';
  860. }
  861. $content .= "<BR>";
  862. $bookSn = $shop->bookSn ?? 0;
  863. if (!empty($bookSn)) {
  864. $content .= "<CB>扫码查货位号</CB>";
  865. $content .= "<BR>";
  866. $host = Yii::$app->params['ghsHost'];
  867. $orderSn = $orderInfo->orderSn ?? '';
  868. $salt = $orderInfo->salt ?? '';
  869. $content .= "<QR>" . $host . "/#/pagesPurchase/info?orderSn=" . $orderSn . "&salt=" . $salt . "</QR>";
  870. }
  871. $content .= "<BR>";
  872. $ext = ShopExtClass::getByCondition(['shopId' => $shopId]);
  873. $printSn = $ext['wcPrintSn'] ?? '';
  874. if ($firstPrint) {
  875. if (isset($orderInfo->fistWcPrintNum) && $orderInfo->fistWcPrintNum > 1) {
  876. if ($orderInfo->fistWcPrintNum == 2) {
  877. $printSn = $ext['wcPrintSn2'] ?? '';
  878. //noticeUtil::push("使用第2台机器打印 {$printSn}", '15280215347');
  879. }
  880. if ($orderInfo->fistWcPrintNum == 3) {
  881. $printSn = $ext['wcPrintSn3'] ?? '';
  882. //noticeUtil::push("使用第3台机器打印 {$printSn}", '15280215347');
  883. }
  884. }
  885. } else {
  886. if ($order > 1) {
  887. if ($order == 2) {
  888. $printSn = $ext['wcPrintSn2'] ?? '';
  889. //noticeUtil::push("使用第2台机器打印 {$printSn}", '15280215347');
  890. }
  891. if ($order == 3) {
  892. $printSn = $ext['wcPrintSn3'] ?? '';
  893. //noticeUtil::push("使用第3台机器打印 {$printSn}", '15280215347');
  894. }
  895. }
  896. }
  897. if (!empty($printSn)) {
  898. self::addPrintNum($orderInfo);
  899. $p = new printUtil($printSn);
  900. $p->printMsg($content, $shop);
  901. }
  902. }
  903. public static function putIn($cg, $data)
  904. {
  905. if ($cg->status == self::PURCHASE_ORDER_STATUS_COMPLETE) {
  906. util::fail('已经入过库了');
  907. }
  908. if ($cg->status == self::PURCHASE_ORDER_STATUS_CANCEL) {
  909. util::fail('已取消');
  910. }
  911. $orderSn = $cg->orderSn ?? '';
  912. $mainId = $cg->mainId ?? 0;
  913. $sjId = $cg->sjId ?? 0;
  914. $shopId = $cg->shopId ?? 0;
  915. $staffId = $data['staffId'] ?? 0;
  916. $staffName = $data['staffName'] ?? '';
  917. $adminId = $data['adminId'] ?? 0;
  918. $shop = ShopClass::getLockById($shopId);
  919. if (empty($shop)) {
  920. util::fail('没有找到门店26');
  921. }
  922. $ghsName = $cg->ghsName ?? '';
  923. $itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  924. if (empty($itemList)) {
  925. util::fail('没有找到花材');
  926. }
  927. $bookSn = $shop->bookSn ?? 0;
  928. $pfLevel = $shop->pfLevel ?? 0;
  929. if ($pfLevel == 1 && !empty($bookSn)) {
  930. if ($cg->bookSn == $bookSn) {
  931. //入库前再确认分配货位
  932. $key = 'ghs_cg_order_scan_seat_' . $cg->id;
  933. $noLock = util::lock($key);
  934. if ($noLock) {
  935. //可以获得锁,没有在分配货位,可以进行货位分配
  936. $params = ['staffId' => $staffId, 'staffName' => $staffName];
  937. self::assign($cg, $shop, $params);
  938. util::unlock($key);
  939. //分配货位后上架和入库
  940. $shop = $data['shop'] ?? null;
  941. CgOrderItemClass::assignToPutOn($cg, $shop, $params);
  942. } else {
  943. util::fail('正在分配货位,请1秒后再提交');
  944. }
  945. }
  946. }
  947. foreach ($itemList as $cgKey => $cgItem) {
  948. $productId = $cgItem['productId'] ?? 0;
  949. $currentNum = $cgItem['itemNum'] ?? 0;
  950. $ptItemId = $cgItem['itemId'] ?? 0;
  951. $unitCost = $cgItem['cost'] ?? 0;
  952. $presell = $cgItem['presell'] ?? 0;
  953. $currentCost = $cgItem['cost'] ?? 0;
  954. $product = ProductClass::getLockById($productId);
  955. if (empty($product)) {
  956. util::fail('没有找到花材哦');
  957. }
  958. //有新入库且 limitBuy=0,则清空限购记录缓存
  959. if ($product->limitBuy == 0) {
  960. ProductClass::clearLimitBuyCache($productId);
  961. \bizHd\product\classes\ProductClass::clearLimitBuyCache($productId);
  962. }
  963. //库存为0时重置特价
  964. if (intval($product->stock) == 0) {
  965. $product->discountPrice = 0;
  966. $product->skDiscountPrice = 0;
  967. $product->hjDiscountPrice = 0;
  968. }
  969. if ($presell == 0) {
  970. //加库存和流水记录
  971. $stockInfo = ProductClass::addStockByItemNum($productId, $currentNum, ['cost' => $currentCost]);
  972. $record = [];
  973. $record['sjId'] = $sjId;
  974. $record['shopId'] = $shopId;
  975. $record['mainId'] = $mainId;
  976. $record['orderSn'] = $orderSn;
  977. $record['productId'] = $productId;
  978. $record['itemId'] = $ptItemId;
  979. $record['relateName'] = $ghsName;
  980. $record['itemNum'] = $currentNum;
  981. $record['oldStock'] = $stockInfo['oldStock'];
  982. $record['newStock'] = $stockInfo['newStock'];
  983. $record['totalCost'] = $stockInfo['totalCost'] ?? 0;
  984. $record['totalStock'] = $stockInfo['totalStock'] ?? 0;
  985. $record['avCost'] = $stockInfo['avCost'] ?? 0;
  986. $record['changeCost'] = $stockInfo['changeCost'] ?? 0;
  987. $record['unitCost'] = $stockInfo['unitCost'] ?? 0;
  988. StockRecordClass::addPurchaseOrderRecord($record);
  989. //减少路上库存
  990. $onRecord = [];
  991. $onRecord['sjId'] = $sjId;
  992. $onRecord['shopId'] = $shopId;
  993. $onRecord['mainId'] = $mainId;
  994. $onRecord['orderSn'] = $orderSn;
  995. $onRecord['productId'] = $productId;
  996. $onRecord['itemId'] = $ptItemId;
  997. $onRecord['relateName'] = $ghsName;
  998. $onRecord['itemNum'] = $currentNum;
  999. $oldOnStock = $product->onStock ?? 0;
  1000. $onRecord['oldStock'] = $oldOnStock;
  1001. $onRecord['newStock'] = bcsub($oldOnStock, $currentNum);
  1002. $onRecord['ptStyle'] = 2;
  1003. OnStockRecordClass::addInStockOrderRecord($onRecord);
  1004. ProductClass::decreaseOnStockByItemNum($productId, $currentNum);
  1005. }
  1006. $product->cost = $unitCost;
  1007. $product->priceLabel = ProductClass::PRICE_LABEL_AUTO;
  1008. $product->save();
  1009. //成本变动记录
  1010. $changeData = [
  1011. 'ptStyle' => 2,
  1012. 'sjId' => $sjId,
  1013. 'mainId' => $mainId,
  1014. 'itemId' => $productId,
  1015. 'ptItemId' => $ptItemId,
  1016. 'cost' => $unitCost,
  1017. 'staffId' => $staffId,
  1018. 'staffName' => $staffName,
  1019. 'name' => $product->name ?? '',
  1020. 'cover' => $product->cover ?? '',
  1021. 'targetId' => $order->id ?? 0,
  1022. 'event' => '采购',
  1023. ];
  1024. CostChangeClass::addData($changeData);
  1025. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  1026. //直营门店的成本价也要一起改掉
  1027. $allShop = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  1028. if (!empty($allShop)) {
  1029. $currentItemId = $product->itemId ?? 0;
  1030. foreach ($allShop as $currentShop) {
  1031. $currentId = $currentShop->mainId ?? 0;
  1032. if ($currentId == $mainId) {
  1033. continue;
  1034. }
  1035. if (isset($currentShop->join) && $currentShop->join == 1) {
  1036. continue;
  1037. }
  1038. $currentProduct = ProductClass::getByCondition(['mainId' => $currentId, 'itemId' => $currentItemId], true);
  1039. if (!empty($currentProduct)) {
  1040. $currentProduct->cost = $unitCost;
  1041. $currentProduct->save();
  1042. $currentStaff = ShopAdminClass::getByCondition(['mainId' => $currentProduct->mainId, 'adminId' => $adminId], true);
  1043. //成本变动记录
  1044. $changeData = [
  1045. 'ptStyle' => 2,
  1046. 'sjId' => $currentProduct->sjId ?? 0,
  1047. 'mainId' => $currentProduct->mainId ?? 0,
  1048. 'itemId' => $currentProduct->id ?? 0,
  1049. 'ptItemId' => $currentProduct->itemId ?? 0,
  1050. 'cost' => $unitCost,
  1051. 'staffId' => $currentStaff->id ?? 0,
  1052. 'staffName' => $currentStaff->name ?? '',
  1053. 'name' => $currentProduct->name ?? '',
  1054. 'cover' => $currentProduct->cover ?? '',
  1055. 'targetId' => $order->id ?? 0,
  1056. 'event' => $shop->shopName . '采购',
  1057. ];
  1058. CostChangeClass::addData($changeData);
  1059. }
  1060. }
  1061. }
  1062. }
  1063. }
  1064. //门店支出增加
  1065. $currentPrice = $cg->actPrice ?? 0;
  1066. $mainId = $shop->mainId ?? 0;
  1067. $main = MainClass::getLockById($mainId);
  1068. if (empty($main)) {
  1069. util::fail('没有资产信息');
  1070. }
  1071. //支出增加
  1072. $currentTotalExpend = bcadd($main->totalExpend, $currentPrice, 2);
  1073. $main->totalExpend = $currentTotalExpend;
  1074. $main->save();
  1075. //采购增加
  1076. $main->cgFinish += 1;
  1077. $main->totalPurchaseOrder += 1;
  1078. $currentTotalPurchase = bcadd($main->totalPurchase, $currentPrice, 2);
  1079. $main->totalPurchase = $currentTotalPurchase;
  1080. $main->save();
  1081. //供货商资产增加
  1082. $ghsId = $cg->ghsId ?? 0;
  1083. $ghs = GhsClass::getLockById($ghsId);
  1084. if (empty($ghs)) {
  1085. util::fail('没有找到供货商');
  1086. }
  1087. self::applyPurchaseDebtOnBuyerGhs($ghs, $currentPrice, $cg, $sjId, $shopId);
  1088. //采购统计
  1089. $cgNum = $cg->itemNum ?? 0;
  1090. StatCgClass::replace($main, $shop, $currentPrice, $cgNum);
  1091. //采购按供货商统计
  1092. StatCgGhsClass::ghsReplace($cg);
  1093. //门店应收客户款增加
  1094. $ghsShopId = $ghs->shopId;
  1095. $ghsShop = ShopClass::getLockById($ghsShopId);
  1096. if (empty($ghsShop)) {
  1097. util::fail('没有找到门店27');
  1098. }
  1099. $ghsMainId = $ghsShop->mainId ?? 0;
  1100. $ghsMain = \bizGhs\shop\classes\MainClass::getLockById($ghsMainId);
  1101. if (empty($ghsMain)) {
  1102. util::fail('没有main信息22');
  1103. }
  1104. $currentMayGathering = bcadd($ghsMain->mayGathering, $currentPrice, 2);
  1105. $ghsMain->mayGathering = $currentMayGathering;
  1106. self::applyPurchaseDebtOnSupplierCustom($ghs, $currentPrice, $ghsMain);
  1107. $ghsMain->save();
  1108. //当天和当月支出统计
  1109. StatOutClass::updateOrInsert($main, $shop, $currentPrice);
  1110. //支出流水
  1111. $payWay = dict::getDict('payWay', 'unknown');
  1112. $capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
  1113. $event = '采购';
  1114. $mainId = $shop->mainId ?? 0;
  1115. $capitalData = [
  1116. 'capitalType' => $capitalType,
  1117. 'io' => 0,
  1118. 'totalExpend' => $currentTotalExpend,
  1119. 'payWay' => $payWay,
  1120. 'amount' => $currentPrice,
  1121. 'sjId' => $sjId,
  1122. 'shopId' => $shopId,
  1123. 'event' => $event,
  1124. 'mainId' => $mainId
  1125. ];
  1126. ShopCapitalClass::addCapital($capitalData);
  1127. $status = self::PURCHASE_ORDER_STATUS_COMPLETE;
  1128. $cg->debt = self::DEBT_YES;
  1129. // 挂账成立:写入原欠/剩余待结
  1130. self::initDebtPrices($cg, $currentPrice);
  1131. $cg->status = $status;
  1132. $update = ['status' => $status];
  1133. if (isset($data['entryTime']) && !empty($data['entryTime'])) {
  1134. $cg->entryTime = $data['entryTime'];
  1135. $update['entryTime'] = $data['entryTime'];
  1136. }
  1137. PurchaseOrderItemClass::updateByCondition(['orderSn' => $orderSn], $update);
  1138. $cg->save();
  1139. return $cg;
  1140. }
  1141. //添加采购单 lqh 2021.1.20
  1142. public static function addOrder($data)
  1143. {
  1144. $sjId = $data['sjId'] ?? 0;
  1145. $shopId = $data['shopId'] ?? 0;
  1146. $mainId = $data['mainId'] ?? 0;
  1147. $ghsName = $data['ghsName'] ?? '';
  1148. $staffId = $data['staffId'] ?? 0;
  1149. $staffName = $data['staffName'] ?? '';
  1150. $inType = $data['inType'] ?? self::IN_TYPE_NOW;
  1151. $adminId = $data['adminId'] ?? 0;
  1152. $orderSn = orderSn::getGhsPurchaseSn();
  1153. $data['orderSn'] = $orderSn;
  1154. $checkCode = substr($orderSn, -4);
  1155. $data['checkCode'] = $checkCode;
  1156. $status = $inType == self::IN_TYPE_NOW ? self::PURCHASE_ORDER_STATUS_COMPLETE : self::PURCHASE_ORDER_STATUS_SENDING;
  1157. $data['status'] = $status;
  1158. //数据结构 [{itemId:0,bigNum:0,smallNum:0,productId:12,itemPrice:1,weight:1}]
  1159. $ghsItemInfo = $data['itemInfo'];
  1160. $ghsItemInfo = self::mergeItemInfo($ghsItemInfo);
  1161. //总共多少扎
  1162. $totalBigNum = 0;
  1163. //总共多少支
  1164. $totalSmallNum = 0;
  1165. //总数量
  1166. $totalNum = 0;
  1167. $kindNum = 0;
  1168. $totalItemPrice = 0;
  1169. $totalWeight = 0;
  1170. $data['confirm'] = 1;
  1171. if (getenv('YII_ENV') == 'production') {
  1172. if (in_array($mainId, [23390])) {
  1173. $data['confirm'] = 0;
  1174. }
  1175. } else {
  1176. if (in_array($mainId, [644])) {
  1177. $data['confirm'] = 0;
  1178. }
  1179. }
  1180. $productIds = array_column($ghsItemInfo, 'productId');
  1181. $productIds = array_unique(array_filter($productIds));
  1182. $productList = ProductClass::getByIds($productIds, null, 'id');
  1183. foreach ($ghsItemInfo as $v) {
  1184. $ratioType = $v['ratioType'] ?? 1;
  1185. $ratio = $v['ratio'] ?? 1;
  1186. $currentProductId = $v['productId'] ?? 0;
  1187. $currentTotal = $v['totalPrice'] ?? 0;
  1188. $currentAboutPrice = $v['aboutPrice'] ?? 0;
  1189. $unitWeight = $productList[$currentProductId]['weight'] ?? 0;
  1190. if (isset($v['itemPrice']) == false || $v['itemPrice'] <= 0) {
  1191. util::fail('请填写价格');
  1192. }
  1193. if (isset($v['bigNum']) && $v['bigNum'] > 0) {
  1194. $thisBigNum = $v['bigNum'];
  1195. $totalBigNum = bcadd($totalBigNum, $thisBigNum, 2);
  1196. $totalNum = bcadd($totalNum, $thisBigNum, 2);
  1197. if ($currentAboutPrice == 1) {
  1198. if ($currentTotal <= 0) {
  1199. util::fail('请填写总金额');
  1200. }
  1201. $currentTotalPrice = $currentTotal;
  1202. } else {
  1203. $currentTotalPrice = bcmul($thisBigNum, $v['itemPrice'], 2);
  1204. }
  1205. $weight = bcmul($unitWeight, $thisBigNum, 2);
  1206. } else {
  1207. if (isset($v['smallNum']) == false || $v['smallNum'] <= 0) {
  1208. util::fail('请填写数量');
  1209. }
  1210. if ($ratioType == 1) {
  1211. //util::fail('小单位采购,单位比必须固定');
  1212. }
  1213. if ($ratio <= 1) {
  1214. //util::fail('小单位采购,单位比必须大于1');
  1215. }
  1216. $thisSmallNum = $v['smallNum'];
  1217. $changeToBigNum = bcdiv($thisSmallNum, $ratio, 2);
  1218. $totalNum = bcadd($totalNum, $changeToBigNum, 2);
  1219. $totalSmallNum = bcadd($totalSmallNum, $thisSmallNum, 2);
  1220. if ($currentAboutPrice == 1) {
  1221. if ($currentTotal <= 0) {
  1222. util::fail('请填写总金额');
  1223. }
  1224. $currentTotalPrice = $currentTotal;
  1225. } else {
  1226. $currentTotalPrice = bcmul($thisSmallNum, $v['itemPrice'], 2);
  1227. }
  1228. $weight = bcmul($unitWeight, $changeToBigNum, 2);
  1229. }
  1230. $kindNum++;
  1231. $totalItemPrice = bcadd($totalItemPrice, $currentTotalPrice, 2);
  1232. $totalWeight = bcadd($totalWeight, $weight, 2);
  1233. }
  1234. $data['bigNum'] = $totalBigNum;
  1235. $data['smallNum'] = $totalSmallNum;
  1236. $data['itemNum'] = $totalNum;
  1237. $data['kind'] = $kindNum;
  1238. $data['price'] = $totalItemPrice;
  1239. $data['totalWeight'] = $totalWeight;
  1240. //计算订单总价
  1241. $data['packingCharge'] = isset($data['packingCharge']) && $data['packingCharge'] > 0 ? $data['packingCharge'] : 0;
  1242. $data['shortCharge'] = isset($data['shortCharge']) && $data['shortCharge'] > 0 ? $data['shortCharge'] : 0;
  1243. $data['longCharge'] = isset($data['longCharge']) && $data['longCharge'] > 0 ? $data['longCharge'] : 0;
  1244. $data['pickCharge'] = isset($data['pickCharge']) && $data['pickCharge'] > 0 ? $data['pickCharge'] : 0;
  1245. $data['localCharge'] = isset($data['localCharge']) && $data['localCharge'] > 0 ? $data['localCharge'] : 0;
  1246. $cgModel = $data['cgModel'] ?? 0;
  1247. if ($cgModel == 1) {
  1248. //简约采购模式
  1249. $customAvgKgWeight = $data['avgKgWeight'] ?? 0;
  1250. $data['longCharge'] = bcmul($totalWeight, $customAvgKgWeight, 2);
  1251. unset($data['modifyPrice']);
  1252. } else {
  1253. //正常采购模式不要使用传过来的每公斤运费
  1254. unset($data['avgKgWeight']);
  1255. }
  1256. $data['entryTime'] = isset($data['entryTime']) && !empty($data['entryTime']) ? $data['entryTime'] . ' 12:00:00' : date('Y-m-d H:i:s');
  1257. $packingCharge = $data['packingCharge'] ?? 0;
  1258. $shortCharge = $data['shortCharge'] ?? 0;
  1259. $longCharge = $data['longCharge'] ?? 0;
  1260. $pickCharge = $data['pickCharge'] ?? 0;
  1261. $localCharge = $data['localCharge'] ?? 0;
  1262. //提货费和本地运费
  1263. $paidPrice = bcadd($localCharge, $pickCharge, 2);
  1264. $yfPayWay = $data['yfPayWay'] ?? self::YF_PAY_WAY_JF;
  1265. //打包费
  1266. $currentPrice = bcadd($totalItemPrice, $packingCharge, 2);
  1267. //寄付才算长途和短途运费
  1268. if ($yfPayWay == self::YF_PAY_WAY_JF) {
  1269. $currentPrice = bcadd($currentPrice, $shortCharge, 2);
  1270. $currentPrice = bcadd($currentPrice, $longCharge, 2);
  1271. } elseif ($yfPayWay == self::YF_PAY_WAY_DF) {
  1272. $paidPrice = bcadd($paidPrice, $shortCharge, 2);
  1273. $paidPrice = bcadd($paidPrice, $longCharge, 2);
  1274. } else {
  1275. util::fail('没有找到这个运费支付方式');
  1276. }
  1277. // $paidPrice 字段说明,此字段表示收货方已付款金额,包括 $localCharge本地运费、 $pickCharge提货费、 $shortCharge短途运费(到付时算,如果不是到付,不算在$paidPrice里)、$longCharge长途运费(到付时算,如果不是到付,不算在$paidPrice里)
  1278. //请搜索关键词 cg_item_cost ,有多处相似情况要考虑
  1279. $data['prePrice'] = $currentPrice;
  1280. if (isset($data['modifyPrice']) && !empty($data['modifyPrice']) && $data['modifyPrice'] > 0) {
  1281. //报损减免后金额
  1282. $modifyPrice = $data['modifyPrice'] ?? 0;
  1283. if ($modifyPrice > $currentPrice) {
  1284. util::fail("应付金额大于总金额,应付金额:{$modifyPrice},总金额:{$currentPrice}");
  1285. }
  1286. if ($modifyPrice < $currentPrice) {
  1287. $data['discountAmount'] = bcsub($currentPrice, $modifyPrice, 2);
  1288. $data['discountType'] = dict::getDict('discountType', 'discount');
  1289. $currentPrice = $modifyPrice;
  1290. }
  1291. }
  1292. $data['orderPrice'] = $currentPrice;
  1293. $data['actPrice'] = $currentPrice;
  1294. $data['realPrice'] = $currentPrice;
  1295. $data['paidPrice'] = $paidPrice;
  1296. //总运费
  1297. $totalFreight = bcadd($packingCharge, $shortCharge, 2);
  1298. $totalFreight = bcadd($totalFreight, $longCharge, 2);
  1299. $totalFreight = bcadd($totalFreight, $pickCharge, 2);
  1300. $totalFreight = bcadd($totalFreight, $localCharge, 2);
  1301. $data['totalFreight'] = $totalFreight;
  1302. $connection = Yii::$app->db;
  1303. $transaction = $connection->beginTransaction();
  1304. try {
  1305. //总运费/总公斤数,计算每公斤的运费
  1306. if ($totalWeight > 0) {
  1307. $avgKgWeight = bcdiv($totalFreight, $totalWeight, 3);
  1308. } else {
  1309. $avgKgWeight = 0;
  1310. }
  1311. $avgKgWeight = round($avgKgWeight, 2);
  1312. $data['avgKgWeight'] = $avgKgWeight;
  1313. $data['salt'] = stringUtil::charsShuffleLowerCase(8);
  1314. $data['cgStyle'] = dict::getDict('cgStyle', 'ghs');
  1315. $order = self::add($data, true);
  1316. $shop = ShopClass::getLockById($shopId);
  1317. if (empty($shop)) {
  1318. util::fail('没有找到门店28');
  1319. }
  1320. //组装详情表数据
  1321. $batchData = self::groupOrderItem($ghsItemInfo, $orderSn);
  1322. //加库存
  1323. foreach ($batchData as $currentKey => $v) {
  1324. $itemId = $v['itemId'];
  1325. $productId = $v['productId'];
  1326. //单位采购价
  1327. $unitCgPrice = $v['bigPrice'] ?? 0;
  1328. $presell = $productList[$productId]['presell'] ?? 0;
  1329. //普通入库增加库存,预售入库不增加库存 ssh 20220109
  1330. if ($presell == 0) {
  1331. //增加路上库存
  1332. $onRecord = [];
  1333. $onRecord['sjId'] = $sjId;
  1334. $onRecord['shopId'] = $shopId;
  1335. $onRecord['mainId'] = $mainId;
  1336. $onRecord['orderSn'] = $orderSn;
  1337. $onRecord['productId'] = $productId;
  1338. $onRecord['itemId'] = $itemId;
  1339. $onRecord['relateName'] = $ghsName;
  1340. $onRecord['itemNum'] = $v['itemNum'];
  1341. $oldOnStock = $v['onStock'] ?? 0;
  1342. $onRecord['oldStock'] = $oldOnStock;
  1343. $newOnStock = bcadd($oldOnStock, $v['itemNum']);
  1344. $onRecord['newStock'] = $newOnStock;
  1345. $onRecord['ptStyle'] = 2;
  1346. OnStockRecordClass::addPurchaseOrderRecord($onRecord);
  1347. ProductClass::addOnStockByItemNum($productId, $v['itemNum']);
  1348. if ($inType == self::IN_TYPE_NOW) {
  1349. //增加库存
  1350. $stockInfo = ProductClass::addStockByItemNum($productId, $v['itemNum'], ['cost' => $unitCgPrice]);
  1351. $record = [];
  1352. $record['sjId'] = $sjId;
  1353. $record['shopId'] = $shopId;
  1354. $record['mainId'] = $mainId;
  1355. $record['orderSn'] = $orderSn;
  1356. $record['productId'] = $productId;
  1357. $record['itemId'] = $itemId;
  1358. $record['relateName'] = $ghsName;
  1359. $record['itemNum'] = $v['itemNum'];
  1360. $record['oldStock'] = $stockInfo['oldStock'];
  1361. $record['newStock'] = $stockInfo['newStock'];
  1362. $record['unitCost'] = $stockInfo['unitCost'];
  1363. $record['changeCost'] = $stockInfo['changeCost'];
  1364. $record['totalStock'] = $stockInfo['totalStock'];
  1365. $record['totalCost'] = $stockInfo['totalCost'];
  1366. $record['avCost'] = $stockInfo['avCost'];
  1367. StockRecordClass::addPurchaseOrderRecord($record);
  1368. //减少路上库存
  1369. $onRecord = [];
  1370. $onRecord['sjId'] = $sjId;
  1371. $onRecord['shopId'] = $shopId;
  1372. $onRecord['mainId'] = $mainId;
  1373. $onRecord['orderSn'] = $orderSn;
  1374. $onRecord['productId'] = $productId;
  1375. $onRecord['itemId'] = $itemId;
  1376. $onRecord['relateName'] = $ghsName;
  1377. $onRecord['itemNum'] = $v['itemNum'];
  1378. $onRecord['oldStock'] = $newOnStock;
  1379. $onRecord['newStock'] = bcsub($newOnStock, $v['itemNum']);
  1380. $onRecord['ptStyle'] = 2;
  1381. OnStockRecordClass::addInStockOrderRecord($onRecord);
  1382. ProductClass::decreaseOnStockByItemNum($productId, $v['itemNum']);
  1383. }
  1384. }
  1385. $productWeight = $productList[$productId]['weight'] ?? 0;
  1386. $unitFreight = bcmul($avgKgWeight, $productWeight, 3);
  1387. $unitCost = bcadd($unitCgPrice, $unitFreight, 3);
  1388. $unitCost = round($unitCost, 2);
  1389. $batchData[$currentKey]['bigFreight'] = $unitFreight;
  1390. $batchData[$currentKey]['cost'] = $unitCost;
  1391. $currentCover = $productList[$productId]['cover'] ?? '';
  1392. $currentName = $productList[$productId]['name'] ?? '';
  1393. $batchData[$currentKey]['cover'] = $currentCover;
  1394. $batchData[$currentKey]['name'] = $currentName;
  1395. $currentBigUnit = $productList[$productId]['bigUnit'] ?? '';
  1396. $currentSmallUnit = $productList[$productId]['smallUnit'] ?? '';
  1397. $currentRatio = $productList[$productId]['ratio'] ?? 0;
  1398. $currentRatioType = $productList[$productId]['ratioType'] ?? 0;
  1399. $currentClassId = $productList[$productId]['classId'] ?? 0;
  1400. $batchData[$currentKey]['bigUnit'] = $currentBigUnit;
  1401. $batchData[$currentKey]['smallUnit'] = $currentSmallUnit;
  1402. $batchData[$currentKey]['ratio'] = $currentRatio;
  1403. $batchData[$currentKey]['ratioType'] = $currentRatioType;
  1404. $batchData[$currentKey]['presell'] = $presell;
  1405. $batchData[$currentKey]['classId'] = $currentClassId;
  1406. $batchData[$currentKey]['entryTime'] = $order->entryTime ?? date("Y-m-d H:i:s");
  1407. $batchData[$currentKey]['addTime'] = $order->addTime ?? date("Y-m-d H:i:s");
  1408. $batchData[$currentKey]['status'] = $order->status ?? 1;
  1409. $batchData[$currentKey]['cgStaffId'] = $order->cgStaffId ?? 0;
  1410. $batchData[$currentKey]['cgStaffName'] = $order->cgStaffName ?? '';
  1411. $batchData[$currentKey]['staffId'] = $order->shopAdminId ?? 0;
  1412. $batchData[$currentKey]['staffName'] = $order->shopAdminName ?? '';
  1413. if ($inType == self::IN_TYPE_NOW) {
  1414. $product = ProductClass::getLockById($productId);
  1415. $product->cost = $unitCost;
  1416. $product->priceLabel = ProductClass::PRICE_LABEL_AUTO;
  1417. $product->save();
  1418. //成本变动记录
  1419. $changeData = [
  1420. 'ptStyle' => 2,
  1421. 'sjId' => $sjId,
  1422. 'mainId' => $mainId,
  1423. 'itemId' => $productId,
  1424. 'ptItemId' => $itemId,
  1425. 'cost' => $unitCost,
  1426. 'staffId' => $staffId,
  1427. 'staffName' => $staffName,
  1428. 'name' => $product->name ?? '',
  1429. 'cover' => $product->cover ?? '',
  1430. 'targetId' => $order->id ?? 0,
  1431. 'event' => '采购',
  1432. ];
  1433. CostChangeClass::addData($changeData);
  1434. if (isset($shop->default) && $shop->default == 1 && isset($shop->dataSync) && $shop->dataSync == 1) {
  1435. //直营门店的成本价也要一起改掉
  1436. $allShop = ShopClass::getAllByCondition(['sjId' => $sjId], null, '*', null, true);
  1437. if (!empty($allShop)) {
  1438. $currentItemId = $product->itemId ?? 0;
  1439. foreach ($allShop as $currentShop) {
  1440. $currentId = $currentShop->mainId ?? 0;
  1441. if ($currentId == $mainId) {
  1442. continue;
  1443. }
  1444. if (isset($currentShop->join) && $currentShop->join == 1) {
  1445. continue;
  1446. }
  1447. $currentProduct = ProductClass::getByCondition(['mainId' => $currentId, 'itemId' => $currentItemId], true);
  1448. if (!empty($currentProduct)) {
  1449. $currentProduct->cost = $unitCost;
  1450. $currentProduct->save();
  1451. $currentStaff = ShopAdminClass::getByCondition(['mainId' => $currentProduct->mainId, 'adminId' => $adminId], true);
  1452. //成本变动记录
  1453. $changeData = [
  1454. 'ptStyle' => 2,
  1455. 'sjId' => $currentProduct->sjId ?? 0,
  1456. 'mainId' => $currentProduct->mainId ?? 0,
  1457. 'itemId' => $currentProduct->id ?? 0,
  1458. 'ptItemId' => $currentProduct->itemId ?? 0,
  1459. 'cost' => $unitCost,
  1460. 'staffId' => $currentStaff->id ?? 0,
  1461. 'staffName' => $currentStaff->name ?? '',
  1462. 'name' => $currentProduct->name ?? '',
  1463. 'cover' => $currentProduct->cover ?? '',
  1464. 'targetId' => $order->id ?? 0,
  1465. 'event' => $shop->shopName . '采购',
  1466. ];
  1467. CostChangeClass::addData($changeData);
  1468. }
  1469. }
  1470. }
  1471. }
  1472. }
  1473. }
  1474. //写入详情表
  1475. PurchaseOrderItemClass::batchAddOrderItem($batchData);
  1476. if ($inType == self::IN_TYPE_NOW) {
  1477. //门店支出增加
  1478. $mainId = $shop->mainId ?? 0;
  1479. $main = MainClass::getLockById($mainId);
  1480. if (empty($main)) {
  1481. util::fail('没有资产信息');
  1482. }
  1483. //支出增加
  1484. $currentTotalExpend = bcadd($main->totalExpend, $currentPrice, 2);
  1485. $main->totalExpend = $currentTotalExpend;
  1486. $main->save();
  1487. //采购增加
  1488. $main->cgFinish += 1;
  1489. $main->totalPurchaseOrder += 1;
  1490. $currentTotalPurchase = bcadd($main->totalPurchase, $currentPrice, 2);
  1491. $main->totalPurchase = $currentTotalPurchase;
  1492. $main->save();
  1493. //供货商资产增加
  1494. $ghsId = $data['ghsId'] ?? 0;
  1495. $ghs = GhsClass::getLockById($ghsId);
  1496. if (empty($ghs)) {
  1497. util::fail('没有找到供货商');
  1498. }
  1499. self::applyPurchaseDebtOnBuyerGhs($ghs, $currentPrice, $order, $sjId, $shopId);
  1500. //采购统计
  1501. $cgNum = $order->itemNum ?? 0;
  1502. StatCgClass::replace($main, $shop, $currentPrice, $cgNum);
  1503. //采购按供货商统计
  1504. StatCgGhsClass::ghsReplace($order);
  1505. //门店应收客户款增加
  1506. $ghsShopId = $ghs->shopId;
  1507. $ghsShop = ShopClass::getLockById($ghsShopId);
  1508. if (empty($ghsShop)) {
  1509. util::fail('没有找到门店29');
  1510. }
  1511. $ghsMainId = $ghsShop->mainId ?? 0;
  1512. $ghsMain = \bizGhs\shop\classes\MainClass::getLockById($ghsMainId);
  1513. if (empty($ghsMain)) {
  1514. util::fail('没有main信息23');
  1515. }
  1516. $currentMayGathering = bcadd($ghsMain->mayGathering, $currentPrice, 2);
  1517. $ghsMain->mayGathering = $currentMayGathering;
  1518. self::applyPurchaseDebtOnSupplierCustom($ghs, $currentPrice, $ghsMain);
  1519. $ghsMain->save();
  1520. $order->debt = self::DEBT_YES;
  1521. // 稍后入库→入库完成:校准/写入挂账原欠与剩余待结
  1522. self::initDebtPrices($order, $currentPrice);
  1523. $order->save(false, ['debt', 'debtPrice', 'remainDebtPrice']);
  1524. //当天和当月支出统计
  1525. StatOutClass::updateOrInsert($main, $shop, $currentPrice);
  1526. //支出流水
  1527. $payWay = dict::getDict('payWay', 'unknown');
  1528. $capitalType = dict::getDict('capitalType', 'ghsPurchase', 'id');
  1529. $sjId = $order->sjId ?? 0;
  1530. $shopId = $order->shopId ?? 0;
  1531. $event = '采购';
  1532. $mainId = $shop->mainId ?? 0;
  1533. $capitalData = [
  1534. 'capitalType' => $capitalType,
  1535. 'io' => 0,
  1536. 'totalExpend' => $currentTotalExpend,
  1537. 'payWay' => $payWay,
  1538. 'amount' => $currentPrice,
  1539. 'sjId' => $sjId,
  1540. 'shopId' => $shopId,
  1541. 'event' => $event,
  1542. 'mainId' => $mainId
  1543. ];
  1544. ShopCapitalClass::addCapital($capitalData);
  1545. }
  1546. $transaction->commit();
  1547. return $order;
  1548. } catch (\Exception $exception) {
  1549. $transaction->rollBack();
  1550. util::fail('保存失败:' . $exception->getMessage());
  1551. }
  1552. return false;
  1553. }
  1554. //采购单列表 lqh 2021.1.19
  1555. public static function getOrderList($where)
  1556. {
  1557. $data = self::getList('*', $where, 'addTime DESC');
  1558. $list = $data['list'] ?? [];
  1559. $data['list'] = self::groupSupplierList($list);
  1560. $data['list'] = self::groupAdminList($data['list']);
  1561. $data['list'] = self::groupStatusName($data['list']);
  1562. //订单列表页需要显示花材
  1563. if (!empty($list)) {
  1564. foreach ($list as $key => $order) {
  1565. $orderSn = $order['orderSn'] ?? '';
  1566. $itemList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, 'id,name,itemNum,refundNum,aboutPrice,bigPrice');
  1567. if (!empty($itemList)) {
  1568. $data['list'][$key]['itemList'] = $itemList;
  1569. }
  1570. }
  1571. }
  1572. return $data;
  1573. }
  1574. //获取订单详情信息 lqh 2021.1.20
  1575. public static function getOrderDetail($orderSn, $shopId)
  1576. {
  1577. $where = ['orderSn' => $orderSn];
  1578. $orderData = self::getByCondition($where);
  1579. if (empty($orderData)) {
  1580. util::fail('订单不存在');
  1581. }
  1582. if (isset($orderData['shopId']) == false || $orderData['shopId'] != $shopId) {
  1583. util::fail('无法访问,编号669');
  1584. }
  1585. $orderInfo = PurchaseOrderItemClass::getOrderItemDetail($orderSn);
  1586. if ($orderInfo) {
  1587. foreach ($orderInfo as $key => $val) {
  1588. $id = $val['id'] ?? 0;
  1589. $shortCover = $val['cover'] ?? '';
  1590. $orderInfo[$key]['ShortCover'] = $shortCover;
  1591. $orderInfo[$key]['itemNum'] = $val['itemNum'] ? floatval($val['itemNum']) : 0;
  1592. $orderInfo[$key]['bigPrice'] = $val['bigPrice'] ? floatval($val['bigPrice']) : 0;
  1593. $orderInfo[$key]['totalPrice'] = $val['totalPrice'] ? floatval($val['totalPrice']) : 0;
  1594. $orderInfo[$key]['cover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_130,w_130";
  1595. $orderInfo[$key]['bigCover'] = imgUtil::groupImg($shortCover) . "?x-oss-process=image/resize,m_fill,h_700,w_700";
  1596. $sendList = CgOrderItemSendClass::getAllByCondition(['parentId' => $id], null, '*');
  1597. if (!empty($sendList)) {
  1598. foreach ($sendList as $kk => $vv) {
  1599. //前端使用
  1600. $sendList[$kk]['currentRefundNum'] = '';
  1601. }
  1602. }
  1603. $orderInfo[$key]['sendList'] = $sendList;
  1604. $num = $val['itemNum'] ?? 0;
  1605. $confirmSendNum = $val['confirmSendNum'] ?? 0;
  1606. $orderInfo[$key]['noSeatNum'] = 0;
  1607. if ($num > $confirmSendNum) {
  1608. $orderInfo[$key]['noSeatNum'] = floatval(bcsub($num, $confirmSendNum, 2));
  1609. }
  1610. }
  1611. }
  1612. $orderData = self::groupAdmin($orderData);
  1613. //供货商信息
  1614. $ghsId = $orderData['ghsId'] ?? 0;
  1615. $ghsInfo = GhsClass::getGhsInfo($ghsId);
  1616. $orderData['supplierName'] = $ghsInfo['name'] ?? '';
  1617. $orderData['supplierMobile'] = $ghsInfo['mobile'] ?? '';
  1618. $orderData['supplierAddress'] = $ghsInfo['address'] ?? '';
  1619. $orderData['debtAmount'] = $ghsInfo['debtAmount'] ?? 0;
  1620. $orderData['itemInfo'] = $orderInfo;
  1621. $orderData['statusName'] = self::getStatusName($orderData['status']);
  1622. //1寄付 2到付
  1623. $yfPayWay = $orderData['yfPayWay'] ?? 1;
  1624. $actPrice = $orderData['actPrice'] ?? 0;
  1625. $shortCharge = $orderData['shortCharge'] ?? 0;
  1626. $longCharge = $orderData['longCharge'] ?? 0;
  1627. $pickCharge = $orderData['pickCharge'] ?? 0;
  1628. $localCharge = $orderData['localCharge'] ?? 0;
  1629. $totalCgCost = $actPrice;
  1630. if ($yfPayWay == 2) {
  1631. $totalCgCost = bcadd($totalCgCost, $shortCharge, 2);
  1632. $totalCgCost = bcadd($totalCgCost, $longCharge, 2);
  1633. }
  1634. $totalCgCost = bcadd($totalCgCost, $pickCharge, 2);
  1635. $totalCgCost = bcadd($totalCgCost, $localCharge, 2);
  1636. $orderData['totalCgCost'] = $totalCgCost;
  1637. $shop = ShopClass::getById($shopId, true);
  1638. $shopBookSn = $shop->bookSn ?? 0;
  1639. $bookSn = $orderData['bookSn'] ?? 0;
  1640. //是否需要提醒更新货位
  1641. $needRemindRefreshSeat = 0;
  1642. $inBooking = 0;
  1643. if (!empty($shopBookSn) && $shopBookSn == $bookSn) {
  1644. //在预订状态,用于采购单售后时判断是否也要退货位上花材
  1645. $inBooking = 1;
  1646. if (isset($orderData['status']) && $orderData['status'] == 3) {
  1647. $needRemindRefreshSeat = 1;
  1648. }
  1649. }
  1650. $orderData['needRemindRefreshSeat'] = $needRemindRefreshSeat;
  1651. $orderData['inBooking'] = $inBooking;
  1652. // 结账信息列表:对齐销售单详情 orderClearList(部分/多次销均展示金额)
  1653. $cgId = intval($orderData['id'] ?? 0);
  1654. $saleId = intval($orderData['saleId'] ?? 0);
  1655. $orderData['orderClearList'] = OrderCgClearClass::listPaidClearRecordsForPurchase($cgId, $saleId);
  1656. return $orderData;
  1657. }
  1658. // 组装 orderItem lqh 2021.1.25
  1659. public static function groupOrderItem($ghsItemInfo, $orderSn)
  1660. {
  1661. $productData = self::getProductMapData($ghsItemInfo);
  1662. $batchData = [];
  1663. foreach ($ghsItemInfo as $v) {
  1664. $tmp = [];
  1665. $productId = $v['productId'];
  1666. $currentAboutPrice = $v['aboutPrice'] ?? 0;
  1667. $currentTotal = $v['totalPrice'] ?? 0;
  1668. $assignSeat = $v['assignSeat'] ?? 1;
  1669. $itemId = $productData[$productId]['itemId'];
  1670. $tmp['orderSn'] = $orderSn;
  1671. $tmp['assignSeat'] = $assignSeat;
  1672. $tmp['itemId'] = $itemId;
  1673. $tmp['belongCost'] = $productData[$productId]['belongCost'] ?? 0;
  1674. $tmp['productId'] = $v['productId'];
  1675. $tmp['itemStock'] = $productData[$productId]['stock'] ?? 0;
  1676. $tmp['onStock'] = $productData[$productId]['onStock'] ?? 0;
  1677. $tmp['aboutPrice'] = $currentAboutPrice;
  1678. if (isset($v['bigNum']) && $v['bigNum'] > 0) {
  1679. $tmp['itemNum'] = $v['bigNum'];
  1680. $tmp['bigPrice'] = $v['itemPrice'];
  1681. if ($currentAboutPrice == 1) {
  1682. $totalPrice = $currentTotal;
  1683. } else {
  1684. $totalPrice = bcmul($v['itemPrice'], $tmp['itemNum'], 2);
  1685. }
  1686. $tmp['totalPrice'] = $totalPrice;
  1687. } else {
  1688. $smallNum = $v['smallNum'] ?? 0;
  1689. if ($smallNum <= 0) {
  1690. util::fail('没有数量');
  1691. }
  1692. $ratioType = $v['ratioType'] ?? 1;
  1693. if ($ratioType == 1) {
  1694. //util::fail('小单位,单位比必须固定的哈');
  1695. }
  1696. $ratio = $v['ratio'] ?? 1;
  1697. if ($ratio <= 1) {
  1698. //util::fail('小单位,单位比必须大于1的哦');
  1699. }
  1700. $changeToBigNum = bcdiv($smallNum, $ratio, 2);
  1701. $tmp['itemNum'] = $changeToBigNum;
  1702. $bigPrice = bcmul($v['itemPrice'], $ratio, 2);
  1703. $tmp['bigPrice'] = $bigPrice;
  1704. if ($currentAboutPrice == 1) {
  1705. $totalPrice = $currentTotal;
  1706. } else {
  1707. $totalPrice = bcmul($v['itemPrice'], $smallNum, 2);
  1708. }
  1709. $tmp['totalPrice'] = $totalPrice;
  1710. }
  1711. $batchData[] = $tmp;
  1712. }
  1713. return $batchData;
  1714. }
  1715. // 组装状态码 lqh 2021.1.30
  1716. public static function groupStatusName($list)
  1717. {
  1718. foreach ($list as $k => $val) {
  1719. $status = $val['status'] ?? 0;
  1720. $list[$k]['statusName'] = self::getStatusName($status);
  1721. }
  1722. return $list;
  1723. }
  1724. // 获取状态码名称
  1725. public static function getStatusName($status)
  1726. {
  1727. return self::$statusMap[$status] ?? '';
  1728. }
  1729. //获取各个状态下的订单数量 allNum unPayNum unSendNum sendingNum finishNum
  1730. public static function statNum($shopId)
  1731. {
  1732. $data = [
  1733. 'allNum' => 0,
  1734. 'unPayNum' => 0,
  1735. 'unSendNum' => 0,
  1736. 'sendingNum' => 0,
  1737. 'finishNum' => 0,
  1738. ];
  1739. $res = self::getAllList("status", ['shopId' => $shopId]);
  1740. foreach ($res as $v) {
  1741. $data['allNum']++;
  1742. $status = $v['status'];
  1743. switch ($status) {
  1744. case self::PURCHASE_ORDER_STATUS_WAIT:
  1745. //待支付
  1746. $data['unPayNum']++;
  1747. break;
  1748. case self::PURCHASE_ORDER_STATUS_UN_SEND:
  1749. $data['unSendNum']++;
  1750. break;
  1751. case self::PURCHASE_ORDER_STATUS_SENDING:
  1752. $data['sendingNum']++;
  1753. break;
  1754. case self::PURCHASE_ORDER_STATUS_COMPLETE:
  1755. $data['finishNum']++;
  1756. break;
  1757. }
  1758. }
  1759. return $data;
  1760. }
  1761. //采购数,采购金额统计(根据时间,今日,昨天,7天,30天)
  1762. public static function statOrderInfo($shopId, $statType)
  1763. {
  1764. $start = '';
  1765. $end = '';
  1766. switch ($statType) {
  1767. case "today":
  1768. $start = date('Y-m-d');
  1769. $end = $start;
  1770. break;
  1771. case "yesterday" :
  1772. $start = date('Y-m-d', strtotime("-1 day"));
  1773. $end = $start;
  1774. break;
  1775. case "seven":
  1776. $start = date('Y-m-d', strtotime("-6 day"));
  1777. $end = date('Y-m-d');
  1778. break;
  1779. case "thirty":
  1780. $start = date('Y-m-d', strtotime("-29 day"));
  1781. $end = date('Y-m-d');
  1782. break;
  1783. default:
  1784. }
  1785. $where = [];
  1786. $where['shopId'] = $shopId;
  1787. $where['addTime'] = ['between', [$start, $end]];
  1788. $res = self::getAllByCondition($where, null, "id,actPrice");
  1789. $totalAmount = 0;
  1790. $totalNum = count($res);
  1791. foreach ($res as $v) {
  1792. $actPrice = $v['actPrice'] ?? 0;
  1793. $totalAmount = bcadd($totalAmount, $actPrice, 2);
  1794. }
  1795. return [
  1796. 'totalAmount' => $totalAmount,
  1797. 'totalNum' => $totalNum,
  1798. ];
  1799. }
  1800. public static function exportDueList($list, $mainId)
  1801. {
  1802. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1803. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1804. $objPHPExcel = new \PHPExcel();
  1805. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1806. ->setLastModifiedBy("Maarten Balliauw")
  1807. ->setTitle("Office 2007 XLSX Document")
  1808. ->setSubject("Office 2007 XLSX Document")
  1809. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1810. ->setKeywords("office 2007 openxml php")
  1811. ->setCategory("file");
  1812. $ghsTitle = '供货商';
  1813. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1814. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1815. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1816. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1817. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1818. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1819. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1820. $objPHPExcel->getActiveSheet()->setCellValue('A1', 'ID');
  1821. $objPHPExcel->getActiveSheet()->setCellValue('B1', '名称');
  1822. $objPHPExcel->getActiveSheet()->setCellValue('C1', '未扣隔天金额');
  1823. $objPHPExcel->getActiveSheet()->setCellValue('D1', '隔天售后金额');
  1824. $objPHPExcel->getActiveSheet()->setCellValue('E1', '已扣隔天金额');
  1825. $objPHPExcel->getActiveSheet()->setCellValue('F1', '订单数');
  1826. //设置宽度
  1827. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(25);
  1828. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(18);
  1829. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(18);
  1830. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(18);
  1831. $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(15);
  1832. //加粗
  1833. foreach (['A1', 'B1', 'C1', 'D1', 'E1', 'F1'] as $cell) {
  1834. $objPHPExcel->getActiveSheet()->getStyle($cell)->getFont()->setSize(9)->setBold(true);
  1835. $objPHPExcel->getActiveSheet()->getStyle($cell)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1836. }
  1837. $baseRow = 2;
  1838. foreach ($list as $key => $custom) {
  1839. $i = $baseRow + $key;
  1840. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['id']);
  1841. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['name']);
  1842. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['originAmount'] ?? $custom['amount']);
  1843. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['nextDayAmount'] ?? 0);
  1844. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['amount']);
  1845. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['num'] ?? 0);
  1846. foreach (['A', 'B', 'C', 'D', 'E', 'F'] as $col) {
  1847. $objPHPExcel->getActiveSheet()->getStyle($col . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1848. }
  1849. }
  1850. $fileName = '应付统计-' . date("m-d");
  1851. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1852. $objPHPExcel->setActiveSheetIndex(0);
  1853. $dir = './priceTable/' . $mainId;
  1854. if (file_exists($dir) == false) {
  1855. mkdir($dir, 0777, true);
  1856. }
  1857. $date = $fileName;
  1858. $file = $date . '.xls';
  1859. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1860. if (file_exists($dir . '/' . $file)) {
  1861. unlink($dir . '/' . $file);
  1862. }
  1863. $objWriter->save($dir . '/' . $file);
  1864. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1865. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1866. }
  1867. public static function exportOrderData($respond, $mainId)
  1868. {
  1869. $orderList = $respond['list'] ?? [];
  1870. if (empty($orderList)) {
  1871. util::fail('没有数据需要导出');
  1872. }
  1873. $orderSnList = array_column($orderList, 'orderSn');
  1874. $orderItemData = PurchaseOrderItemClass::getAllByCondition(['orderSn' => ['in', $orderSnList]], null, '*');
  1875. $orderItemList = [];
  1876. if (!empty($orderItemData)) {
  1877. foreach ($orderItemData as $itemInfo) {
  1878. $orderSn = $itemInfo['orderSn'] ?? '';
  1879. $orderItemList[$orderSn][] = $itemInfo;
  1880. }
  1881. }
  1882. foreach ($orderList as $key => $orderInfo) {
  1883. $orderSn = $orderInfo['orderSn'] ?? '';
  1884. $status = $orderInfo['status'] ?? 0;
  1885. //增加项
  1886. $actPrice = $orderInfo['actPrice'] ?? 0;
  1887. if ($status != 4) {
  1888. unset($orderList[$key]);
  1889. continue;
  1890. }
  1891. //增加项
  1892. if ($actPrice <= 0) {
  1893. unset($orderList[$key]);
  1894. continue;
  1895. }
  1896. $itemList = $orderItemList[$orderSn] ?? [];
  1897. $orderList[$key]['itemList'] = $itemList;
  1898. }
  1899. $classList = ItemClassClass::getAllByCondition(['mainId' => $mainId], null, '*', 'id');
  1900. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1901. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1902. $objPHPExcel = new \PHPExcel();
  1903. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1904. ->setLastModifiedBy("Maarten Balliauw")
  1905. ->setTitle("Office 2007 XLSX Document")
  1906. ->setSubject("Office 2007 XLSX Document")
  1907. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1908. ->setKeywords("office 2007 openxml php")
  1909. ->setCategory("file");
  1910. $ghsTitle = '供货商';
  1911. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1912. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1913. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1914. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1915. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1916. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1917. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1918. $objPHPExcel->getActiveSheet()->setCellValue('A1', '订单号');
  1919. $objPHPExcel->getActiveSheet()->setCellValue('B1', '创建时间');
  1920. $objPHPExcel->getActiveSheet()->setCellValue('C1', '入库时间');
  1921. $objPHPExcel->getActiveSheet()->setCellValue('D1', '订单金额');
  1922. $objPHPExcel->getActiveSheet()->setCellValue('E1', '退款金额');
  1923. $objPHPExcel->getActiveSheet()->setCellValue('F1', '实际金额');
  1924. $objPHPExcel->getActiveSheet()->setCellValue('G1', '供货商名称');
  1925. $objPHPExcel->getActiveSheet()->setCellValue('H1', '录入人员');
  1926. $objPHPExcel->getActiveSheet()->setCellValue('I1', '采购人员');
  1927. $objPHPExcel->getActiveSheet()->setCellValue('J1', '状态');
  1928. $objPHPExcel->getActiveSheet()->setCellValue('K1', '打印次数');
  1929. $objPHPExcel->getActiveSheet()->setCellValue('L1', '结清状态');
  1930. $objPHPExcel->getActiveSheet()->setCellValue('M1', '备注');
  1931. $objPHPExcel->getActiveSheet()->setCellValue('N1', '花材Id');
  1932. $objPHPExcel->getActiveSheet()->setCellValue('O1', '花材名称');
  1933. $objPHPExcel->getActiveSheet()->setCellValue('P1', '单价');
  1934. $objPHPExcel->getActiveSheet()->setCellValue('Q1', '数量');
  1935. $objPHPExcel->getActiveSheet()->setCellValue('R1', '已退数量');
  1936. $objPHPExcel->getActiveSheet()->setCellValue('S1', '金额');
  1937. $objPHPExcel->getActiveSheet()->setCellValue('T1', '分类');
  1938. //设置宽度
  1939. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
  1940. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  1941. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  1942. $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
  1943. $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
  1944. $objPHPExcel->getActiveSheet()->getColumnDimension('O')->setWidth(20);
  1945. $objPHPExcel->getActiveSheet()->getColumnDimension('T')->setWidth(20);
  1946. //加粗
  1947. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1948. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1949. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1950. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1951. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1952. $objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(9)->setBold(true);
  1953. $objPHPExcel->getActiveSheet()->getStyle('G1')->getFont()->setSize(9)->setBold(true);
  1954. $objPHPExcel->getActiveSheet()->getStyle('H1')->getFont()->setSize(9)->setBold(true);
  1955. $objPHPExcel->getActiveSheet()->getStyle('I1')->getFont()->setSize(9)->setBold(true);
  1956. $objPHPExcel->getActiveSheet()->getStyle('J1')->getFont()->setSize(9)->setBold(true);
  1957. $objPHPExcel->getActiveSheet()->getStyle('K1')->getFont()->setSize(9)->setBold(true);
  1958. $objPHPExcel->getActiveSheet()->getStyle('L1')->getFont()->setSize(9)->setBold(true);
  1959. $objPHPExcel->getActiveSheet()->getStyle('M1')->getFont()->setSize(9)->setBold(true);
  1960. $objPHPExcel->getActiveSheet()->getStyle('N1')->getFont()->setSize(9)->setBold(true);
  1961. $objPHPExcel->getActiveSheet()->getStyle('O1')->getFont()->setSize(9)->setBold(true);
  1962. $objPHPExcel->getActiveSheet()->getStyle('P1')->getFont()->setSize(9)->setBold(true);
  1963. $objPHPExcel->getActiveSheet()->getStyle('Q1')->getFont()->setSize(9)->setBold(true);
  1964. $objPHPExcel->getActiveSheet()->getStyle('R1')->getFont()->setSize(9)->setBold(true);
  1965. $objPHPExcel->getActiveSheet()->getStyle('S1')->getFont()->setSize(9)->setBold(true);
  1966. $objPHPExcel->getActiveSheet()->getStyle('T1')->getFont()->setSize(9)->setBold(true);
  1967. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1968. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1969. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1970. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1971. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1972. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1973. $objPHPExcel->getActiveSheet()->getStyle('G1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1974. $objPHPExcel->getActiveSheet()->getStyle('H1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1975. $objPHPExcel->getActiveSheet()->getStyle('I1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1976. $objPHPExcel->getActiveSheet()->getStyle('J1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1977. $objPHPExcel->getActiveSheet()->getStyle('K1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1978. $objPHPExcel->getActiveSheet()->getStyle('L1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1979. $objPHPExcel->getActiveSheet()->getStyle('M1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1980. $objPHPExcel->getActiveSheet()->getStyle('N1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1981. $objPHPExcel->getActiveSheet()->getStyle('O1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1982. $objPHPExcel->getActiveSheet()->getStyle('P1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1983. $objPHPExcel->getActiveSheet()->getStyle('Q1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1984. $objPHPExcel->getActiveSheet()->getStyle('R1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1985. $objPHPExcel->getActiveSheet()->getStyle('S1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1986. $objPHPExcel->getActiveSheet()->getStyle('T1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1987. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1988. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1989. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1990. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1991. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1992. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1993. $objPHPExcel->getActiveSheet()->getStyle('G1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1994. $objPHPExcel->getActiveSheet()->getStyle('H1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1995. $objPHPExcel->getActiveSheet()->getStyle('I1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1996. $objPHPExcel->getActiveSheet()->getStyle('J1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1997. $objPHPExcel->getActiveSheet()->getStyle('K1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1998. $objPHPExcel->getActiveSheet()->getStyle('L1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1999. $objPHPExcel->getActiveSheet()->getStyle('M1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2000. $objPHPExcel->getActiveSheet()->getStyle('N1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2001. $objPHPExcel->getActiveSheet()->getStyle('O1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2002. $objPHPExcel->getActiveSheet()->getStyle('P1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2003. $objPHPExcel->getActiveSheet()->getStyle('Q1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2004. $objPHPExcel->getActiveSheet()->getStyle('R1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2005. $objPHPExcel->getActiveSheet()->getStyle('S1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2006. $objPHPExcel->getActiveSheet()->getStyle('T1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  2007. $baseRow = 2;
  2008. $x = 0;
  2009. foreach ($orderList as $key => $orderData) {
  2010. $currentList = $orderData['itemList'] ?? [];
  2011. if (!empty($currentList)) {
  2012. foreach ($currentList as $k => $orderInfo) {
  2013. $i = $baseRow + $x;
  2014. $x++;
  2015. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $orderData['orderSn']);
  2016. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $orderData['addTime']);
  2017. if ($orderData['status'] == 4) {
  2018. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $orderData['entryTime']);
  2019. } else {
  2020. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, '');
  2021. }
  2022. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $orderData['orderPrice']);
  2023. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $orderData['tkPrice']);
  2024. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $orderData['actPrice']);
  2025. $objPHPExcel->getActiveSheet()->setCellValue('G' . $i, $orderData['ghsName']);
  2026. $objPHPExcel->getActiveSheet()->setCellValue('H' . $i, $orderData['shopAdminName']);
  2027. $objPHPExcel->getActiveSheet()->setCellValue('I' . $i, $orderData['cgStaffName']);
  2028. $map = [1 => '待确认', 2 => '待发货', 3 => '待入库', 4 => '已入库', 5 => '已取消'];
  2029. $statusName = isset($orderData['status']) && isset($map[$orderData['status']]) ? $map[$orderData['status']] : '未知';
  2030. $objPHPExcel->getActiveSheet()->setCellValue('J' . $i, $statusName);
  2031. $objPHPExcel->getActiveSheet()->setCellValue('K' . $i, $orderData['printNum'] ?? 0);
  2032. $debtName = '';
  2033. if ($orderData['debt'] == 1) {
  2034. $debtName = '未结清';
  2035. }
  2036. if ($orderData['debt'] == 2) {
  2037. $debtName = '已结清';
  2038. }
  2039. $objPHPExcel->getActiveSheet()->setCellValue('L' . $i, $debtName);
  2040. $objPHPExcel->getActiveSheet()->setCellValue('M' . $i, $orderData['remark']);
  2041. $objPHPExcel->getActiveSheet()->setCellValue('N' . $i, $orderInfo['productId']);
  2042. $objPHPExcel->getActiveSheet()->setCellValue('O' . $i, $orderInfo['name']);
  2043. $objPHPExcel->getActiveSheet()->setCellValue('P' . $i, $orderInfo['bigPrice']);
  2044. $remainNum = bcsub($orderInfo['itemNum'], $orderInfo['refundNum']);
  2045. $price = bcmul($remainNum, $orderInfo['bigPrice'], 2);
  2046. $price = floatval($price);
  2047. $objPHPExcel->getActiveSheet()->setCellValue('Q' . $i, $remainNum);
  2048. $objPHPExcel->getActiveSheet()->setCellValue('R' . $i, $orderInfo['refundNum']);
  2049. $objPHPExcel->getActiveSheet()->setCellValue('S' . $i, $price);
  2050. $classId = $orderInfo['classId'] ?? 0;
  2051. $className = isset($classList[$classId]) && isset($classList[$classId]['name']) ? $classList[$classId]['name'] : '';
  2052. $objPHPExcel->getActiveSheet()->setCellValue('T' . $i, $className);
  2053. //居左
  2054. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2055. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2056. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2057. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2058. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2059. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2060. $objPHPExcel->getActiveSheet()->getStyle('G' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2061. $objPHPExcel->getActiveSheet()->getStyle('H' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2062. $objPHPExcel->getActiveSheet()->getStyle('I' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2063. $objPHPExcel->getActiveSheet()->getStyle('J' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2064. $objPHPExcel->getActiveSheet()->getStyle('K' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2065. $objPHPExcel->getActiveSheet()->getStyle('L' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2066. $objPHPExcel->getActiveSheet()->getStyle('M' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2067. $objPHPExcel->getActiveSheet()->getStyle('N' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2068. $objPHPExcel->getActiveSheet()->getStyle('O' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2069. $objPHPExcel->getActiveSheet()->getStyle('P' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2070. $objPHPExcel->getActiveSheet()->getStyle('Q' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2071. $objPHPExcel->getActiveSheet()->getStyle('R' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2072. $objPHPExcel->getActiveSheet()->getStyle('S' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2073. $objPHPExcel->getActiveSheet()->getStyle('T' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  2074. }
  2075. }
  2076. }
  2077. $fileName = '采购单明细-' . time();
  2078. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  2079. $objPHPExcel->setActiveSheetIndex(0);
  2080. $dir = './priceTable/' . $mainId;
  2081. if (file_exists($dir) == false) {
  2082. mkdir($dir, 0777, true);
  2083. }
  2084. $date = $fileName;
  2085. $file = $date . '.xls';
  2086. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  2087. if (file_exists($dir . '/' . $file)) {
  2088. unlink($dir . '/' . $file);
  2089. }
  2090. $objWriter->save($dir . '/' . $file);
  2091. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  2092. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  2093. }
  2094. /**
  2095. * 按 productId 升序排列,用于库存行锁顺序(降低多商品并发死锁)
  2096. * @param array $list
  2097. */
  2098. private static function sortRowsByProductIdAsc(&$list)
  2099. {
  2100. usort($list, function ($left, $right) {
  2101. $leftId = isset($left['productId']) ? intval($left['productId']) : 0;
  2102. $rightId = isset($right['productId']) ? intval($right['productId']) : 0;
  2103. if ($leftId == $rightId) {
  2104. return 0;
  2105. }
  2106. return $leftId < $rightId ? -1 : 1;
  2107. });
  2108. }
  2109. }