StatSaleClass.php 69 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344
  1. <?php
  2. namespace bizGhs\stat\classes;
  3. use biz\stat\classes\StatCgClass;
  4. use biz\stat\classes\StatStockInClass;
  5. use biz\stat\classes\StatStockOutClass;
  6. use bizGhs\cg\classes\CgRefundClass;
  7. use bizGhs\custom\classes\CustomClass;
  8. use bizGhs\expend\classes\ExpendClass;
  9. use bizGhs\item\classes\ItemClassClass;
  10. use bizGhs\order\classes\OrderItemClass;
  11. use bizGhs\order\classes\PurchaseOrderClass;
  12. use bizGhs\order\classes\PurchaseOrderItemClass;
  13. use bizGhs\order\classes\RefundOrderClass;
  14. use bizGhs\order\classes\StockWastageOrderClass;
  15. use bizGhs\product\classes\ProductClass;
  16. use bizGhs\staff\classes\SalaryClass;
  17. use bizHd\goods\classes\PlantCgClass;
  18. use bizHd\order\classes\OrderClass as HdOrderClass;
  19. use bizGhs\order\classes\OrderClass;
  20. use bizHd\purchase\classes\PurchaseClass;
  21. use bizHd\refund\classes\HdRefundClass;
  22. use common\components\arrayUtil;
  23. use common\components\dateUtil;
  24. use common\components\dict;
  25. use common\components\noticeUtil;
  26. use common\components\util;
  27. use Yii;
  28. use bizGhs\base\classes\BaseClass;
  29. class StatSaleClass extends BaseClass
  30. {
  31. public static $baseFile = '\bizGhs\stat\models\StatSale';
  32. public static function wastage($mainId)
  33. {
  34. $get = Yii::$app->request->get();
  35. $where = [];
  36. $where['mainId'] = $mainId;
  37. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  38. $startTime = $get['startTime'] ?? '';
  39. $endTime = $get['endTime'] ?? '';
  40. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  41. $start = $period['startTime'];
  42. $end = $period['endTime'];
  43. $currentStartDate = date('Y-m-d', strtotime($start));
  44. $currentEndDate = date('Y-m-d', strtotime($end));
  45. $currentStartTime = $currentStartDate . ' 00:00:00';
  46. $currentEndTime = $currentEndDate . ' 23:59:59';
  47. $where['addTime'] = ['between', [$currentStartTime, $currentEndTime]];
  48. $list = StockWastageOrderClass::getAllByCondition($where, null, '*');
  49. $totalPrice = 0;
  50. if (!empty($list)) {
  51. foreach ($list as $order) {
  52. $price = $order['price'] ?? 0;
  53. $totalPrice = bcadd($totalPrice, $price, 2);
  54. }
  55. }
  56. $totalPrice = floatval($totalPrice);
  57. return ['totalPrice' => $totalPrice];
  58. }
  59. //结账跟踪 ssh 20230503
  60. public static function OrderClear($mainId)
  61. {
  62. $get = Yii::$app->request->get();
  63. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  64. $timeStyle = $get['timeStyle'] ?? 0;
  65. if (isset($get['searchTime']) && !empty($get['searchTime'])) {
  66. $searchTime = $get['searchTime'];
  67. $startTime = $get['startTime'] ?? '';
  68. $endTime = $get['endTime'] ?? '';
  69. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  70. $start = $period['startTime'];
  71. $end = $period['endTime'];
  72. $currentStartDate = date('Y-m-d', strtotime($start));
  73. $currentEndDate = date('Y-m-d', strtotime($end));
  74. $currentStartTime = $currentStartDate . ' 00:00:00';
  75. $currentEndTime = $currentEndDate . ' 23:59:59';
  76. if ($timeStyle == 0) {
  77. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  78. } else {
  79. $where['clearTime'] = ['between', [$currentStartTime, $currentEndTime]];
  80. }
  81. } else {
  82. $payTime = 'today';
  83. $startTime = $get['startTime'] ?? '';
  84. $endTime = $get['endTime'] ?? '';
  85. $period = dateUtil::formatTime($payTime, $startTime, $endTime, true);
  86. $start = $period['startTime'];
  87. $end = $period['endTime'];
  88. $currentStartDate = date('Y-m-d', strtotime($start));
  89. $currentEndDate = date('Y-m-d', strtotime($end));
  90. $currentStartTime = $currentStartDate . ' 00:00:00';
  91. $currentEndTime = $currentEndDate . ' 23:59:59';
  92. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  93. }
  94. $customId = $get['customId'] ?? 0;
  95. if (!empty($customId)) {
  96. $where['customId'] = $customId;
  97. }
  98. $unClear = $get['unClear'] ?? 0;
  99. if ($unClear == 1) {
  100. $where['clearId'] = 0;
  101. $where['remainDebtPrice>'] = 0;
  102. }
  103. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  104. return ['list' => $ghsOrderList];
  105. }
  106. //客户下单榜 ssh 20230502
  107. public static function statCustomCg($mainId)
  108. {
  109. $get = Yii::$app->request->get();
  110. $where = [];
  111. $where['mainId'] = $mainId;
  112. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  113. $startTime = $get['startTime'] ?? '';
  114. $endTime = $get['endTime'] ?? '';
  115. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  116. $start = $period['startTime'];
  117. $end = $period['endTime'];
  118. $where['time'] = ['between', [$start, $end]];
  119. $currentStartDate = date('Y-m-d', strtotime($start));
  120. $currentEndDate = date('Y-m-d', strtotime($end));
  121. $currentStartTime = $currentStartDate . ' 00:00:00';
  122. $currentEndTime = $currentEndDate . ' 23:59:59';
  123. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  124. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  125. $customList = CustomClass::getAllByCondition(['ownMainId' => $mainId], null, 'id,name,py', 'id');
  126. $arr = [];
  127. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  128. $totalAmount = 0;
  129. $totalNum = 0;
  130. if (!empty($ghsOrderList)) {
  131. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  132. $actPrice = $ghsOrder['actPrice'] ?? 0;
  133. $orderPrice = $ghsOrder['orderPrice'] ?? 0;
  134. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  135. $customId = $ghsOrder['customId'] ?? 0;
  136. $status = $ghsOrder['status'] ?? 0;
  137. $book = $ghsOrder['book'] ?? 0;
  138. if ($status == 1 || $status == 5) {
  139. continue;
  140. }
  141. if ($book == 1 && $status == 2) {
  142. continue;
  143. }
  144. $customName = $customList[$customId]['name'] ?? '';
  145. $py = $customList[$customId]['py'] ?? '';
  146. $totalNum++;
  147. $totalAmount = bcadd($totalAmount, $actPrice, 2);
  148. if (isset($arr[$customId])) {
  149. $arr[$customId]['num']++;
  150. $arr[$customId]['amount'] = bcadd($arr[$customId]['amount'], $actPrice, 2);
  151. $arr[$customId]['orderPrice'] = bcadd($arr[$customId]['orderPrice'], $orderPrice, 2);
  152. $arr[$customId]['tkPrice'] = bcadd($arr[$customId]['tkPrice'], $tkPrice, 2);
  153. } else {
  154. $arr[$customId] = [
  155. 'customId' => $customId,
  156. 'customName' => $customName,
  157. 'py' => $py,
  158. 'num' => 1,
  159. 'amount' => $actPrice,
  160. 'levelName' => '',
  161. 'orderPrice' => $orderPrice,
  162. 'tkPrice' => $tkPrice,
  163. ];
  164. }
  165. }
  166. $arr = arrayUtil::arraySort($arr, 'amount');
  167. }
  168. $totalAmount = floatval($totalAmount);
  169. return ['list' => $arr, 'totalAmount' => $totalAmount, 'totalNum' => $totalNum];
  170. }
  171. public static function profile($mainId)
  172. {
  173. //收入:批发开单、零售开单(含门店、美团和微信客服)、出库
  174. //支出:采购、调拨入库、批发退款、零售退款、店租、水电、伙食
  175. $get = Yii::$app->request->get();
  176. $where = [];
  177. $where['mainId'] = $mainId;
  178. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  179. $startTime = $get['startTime'] ?? '';
  180. $endTime = $get['endTime'] ?? '';
  181. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  182. $start = $period['startTime'];
  183. $end = $period['endTime'];
  184. $where['time'] = ['between', [$start, $end]];
  185. $currentStartDate = date('Y-m-d', strtotime($start));
  186. $currentEndDate = date('Y-m-d', strtotime($end));
  187. $currentStartTime = $currentStartDate . ' 00:00:00';
  188. $currentEndTime = $currentEndDate . ' 23:59:59';
  189. //出库
  190. $stockOut = StatStockOutClass::getSum($where);
  191. //采购入库
  192. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  193. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  194. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  195. $cg = 0;
  196. if (!empty($cgList)) {
  197. //print_r($cgList);
  198. foreach ($cgList as $cgKey => $cgItem) {
  199. $actPrice = $cgItem['actPrice'] ?? 0;
  200. $paidPrice = $cgItem['paidPrice'] ?? 0;
  201. $total = bcadd($actPrice, $paidPrice, 2);
  202. //物流费用不算成本
  203. if (isset($cgItem['isCost']) && $cgItem['isCost'] == 0) {
  204. $shortCharge = $cgItem['shortCharge'] ?? 0;
  205. $longCharge = $cgItem['longCharge'] ?? 0;
  206. $pickCharge = $cgItem['pickCharge'] ?? 0;
  207. $localCharge = $cgItem['localCharge'] ?? 0;
  208. $a1 = bcadd($shortCharge, $longCharge, 2);
  209. $a2 = bcadd($pickCharge, $localCharge, 2);
  210. $a = bcadd($a1, $a2, 2);
  211. $total = bcsub($total, $a, 2);
  212. }
  213. //echo $total.' '.$cgItem->orderSn." \n<br/>";
  214. $cg = bcadd($cg, $total, 2);
  215. }
  216. }
  217. //die;
  218. $cg = floatval($cg);
  219. $lsCgWhere = ['mainId' => $mainId, 'status' => 4,'belongCost'=>1];
  220. $lsCgWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  221. $lsCgList = PurchaseClass::getAllByCondition($lsCgWhere, null, '*');
  222. $lsCgAmount = 0;
  223. if (!empty($lsCgList)) {
  224. foreach ($lsCgList as $lsCgItem) {
  225. $actPrice = $lsCgItem['actPrice'] ?? 0;
  226. $lsCgAmount = bcadd($lsCgAmount, $actPrice, 2);
  227. }
  228. }
  229. $lsCgAmount = floatval($lsCgAmount);
  230. $lzWhere = ['mainId' => $mainId, 'status' => 3];
  231. $lzWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  232. $lzCgList = PlantCgClass::getAllByCondition($lzWhere, null, '*');
  233. $lzCgAmount = 0;
  234. if (!empty($lzCgList)) {
  235. foreach ($lzCgList as $lzInfo) {
  236. $actPrice = $lzInfo['actPrice'] ?? 0;
  237. $lzCgAmount = bcadd($actPrice, $lzCgAmount, 2);
  238. }
  239. }
  240. $lzCgAmount = floatval($lzCgAmount);
  241. //调拨入库
  242. $stockIn = StatStockInClass::getSum($where);
  243. $totalOrderNum = 0;
  244. //批发开单
  245. $pfIncome = 0;
  246. $pfSendCost = 0;
  247. $pfPack = 0;
  248. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  249. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  250. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  251. if (!empty($ghsOrderList)) {
  252. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  253. $actPrice = $ghsOrder['actPrice'] ?? 0;
  254. $sendCost = $ghsOrder['sendCost'] ?? 0;
  255. $refundSendCost = $ghsOrder['refundSendCost'] ?? 0;
  256. $remainSendCost = bcsub($sendCost, $refundSendCost, 2);
  257. $packCost = $ghsOrder['packCost'] ?? 0;
  258. $refundPackCost = $ghsOrder['refundPackCost'] ?? 0;
  259. $remainPackCost = bcsub($packCost, $refundPackCost, 2);
  260. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  261. $pfSendCost = bcadd($pfSendCost, $remainSendCost, 2);
  262. $pfPack = bcadd($pfPack, $remainPackCost, 2);
  263. $totalOrderNum++;
  264. }
  265. }
  266. //零售开单
  267. $lsIncome = 0;
  268. $lsSendCost = 0;
  269. $lsServiceFee = 0;
  270. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  271. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  272. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  273. foreach ($hdOrderList as $hdKey => $hdOrder) {
  274. $actPrice = $hdOrder['mainPay'] ?? 0;
  275. if ($hdOrder['tkPrice'] && $hdOrder['tkPrice'] > 0) {
  276. $actPrice = bcsub($actPrice, $hdOrder['tkPrice'], 2);
  277. }
  278. $cash = $hdOrder['cash'] ?? 0;
  279. $serviceFee = $hdOrder['serviceFee'] ?? 0;
  280. $sendCost = $hdOrder['sendCost'] ?? 0;
  281. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  282. $lsIncome = bcadd($lsIncome, $cash, 2);
  283. //退款取消的先不计算成本
  284. if ($actPrice > 0) {
  285. $lsSendCost = bcadd($lsSendCost, $sendCost, 2);
  286. $lsServiceFee = bcadd($lsServiceFee, $serviceFee, 2);
  287. }
  288. $totalOrderNum++;
  289. }
  290. $condition = ['mainId' => $mainId, 'addTime' => ['between', [$currentStartTime, $currentEndTime]],];
  291. $salary = 0;
  292. $salaryList = SalaryClass::getAllByCondition($condition, null, '*', null, true);
  293. if (!empty($salaryList)) {
  294. foreach ($salaryList as $item) {
  295. $salary = bcadd($salary, $item->amount, 2);
  296. }
  297. }
  298. $condition = ['mainId' => $mainId, 'payTime' => ['between', [$currentStartTime, $currentEndTime]],];
  299. $expendList = ExpendClass::getAllByCondition($condition, null, '*', null, true);
  300. $expendTypeMap = dict::getDict('expendTypeMap');
  301. $expendAmount = [];
  302. if (!empty($expendList)) {
  303. foreach ($expendList as $item) {
  304. $type = $item->type ?? 0;
  305. $amount = $item->amount ?? 0;
  306. $name = $expendTypeMap[$type] ?? '暂无';
  307. if (isset($expendAmount[$type]['amount']) == false) {
  308. $expendAmount[$type]['amount'] = 0;
  309. }
  310. $expendAmount[$type]['name'] = $name;
  311. $expendAmount[$type]['amount'] = floatval(bcadd($expendAmount[$type]['amount'], $amount, 2));
  312. }
  313. }
  314. $income = [
  315. ['name' => '批发', 'id' => 'pf', 'amount' => floatval($pfIncome)],
  316. ['name' => '零售', 'id' => 'ls', 'amount' => floatval($lsIncome)],
  317. ['name' => '调拨出库', 'id' => 'allot', 'amount' => floatval($stockOut)],
  318. ];
  319. $expend = [
  320. ['name' => '批发运费', 'id' => 'pfSendCost', 'amount' => floatval($pfSendCost)],
  321. ['name' => '批发包装费', 'id' => 'pfSendCost', 'amount' => floatval($pfPack)],
  322. ['name' => '零售运费', 'id' => 'lsSendCost', 'amount' => floatval($lsSendCost)],
  323. ['name' => '美团手续费', 'id' => 'mtFee', 'amount' => floatval($lsServiceFee)],
  324. ['name' => '花材采购', 'id' => 'cg', 'amount' => floatval($cg)],
  325. ['name' => '绿植资材采购', 'id' => 'cg', 'amount' => floatval($lzCgAmount)],
  326. ['name' => '零售花材采购', 'id' => 'cg', 'amount' => floatval($lsCgAmount)],
  327. ['name' => '调拨入库', 'id' => 'allot', 'amount' => floatval($stockIn)],
  328. ['name' => '员工工资', 'id' => 'salary', 'amount' => floatval($salary)],
  329. ];
  330. if (!empty($expendAmount)) {
  331. foreach ($expendAmount as $it) {
  332. $expend[] = $it;
  333. }
  334. }
  335. $balance = 0;
  336. foreach ($income as $it) {
  337. $amount = $it['amount'] ?? 0;
  338. $balance = bcadd($amount, $balance, 2);
  339. }
  340. foreach ($expend as $it) {
  341. $amount = $it['amount'] ?? 0;
  342. $balance = bcsub($balance, $amount, 2);
  343. }
  344. return ['income' => $income, 'expend' => $expend, 'balance' => floatval($balance), 'totalOrderNum' => $totalOrderNum];
  345. }
  346. //按花材统计收入 ssh 20231111
  347. public static function incomeOutItem($mainId)
  348. {
  349. $get = Yii::$app->request->get();
  350. $export = $get['export'] ?? 0;
  351. $where = [];
  352. $where['mainId'] = $mainId;
  353. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  354. $startTime = $get['startTime'] ?? '';
  355. $endTime = $get['endTime'] ?? '';
  356. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  357. $start = $period['startTime'];
  358. $end = $period['endTime'];
  359. $where['time'] = ['between', [$start, $end]];
  360. $currentStartDate = date('Y-m-d', strtotime($start));
  361. $currentEndDate = date('Y-m-d', strtotime($end));
  362. $currentStartTime = $currentStartDate . ' 00:00:00';
  363. $currentEndTime = $currentEndDate . ' 23:59:59';
  364. $s = strtotime($currentStartTime);
  365. $e = strtotime($currentEndTime);
  366. $p = bcsub($e, $s);
  367. $xx = bcmul(86400, 90);
  368. if ($p > $xx) {
  369. util::fail('查询时间不能超过三个月');
  370. }
  371. $productList = ProductClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
  372. if (empty($productList)) {
  373. util::fail('没有找到花材');
  374. }
  375. $arr = [];
  376. //打包费和运费
  377. $packFreight = 0;
  378. $onlyRefundCg = 0;
  379. $onlyRefund = 0;
  380. $totalDiscountAmount = 0;
  381. $totalLabourCost = 0;
  382. //采购入库
  383. $tbCgIncome = [];
  384. $tbCgIncomeStaff = [];
  385. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  386. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  387. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  388. if (!empty($cgList)) {
  389. foreach ($cgList as $cgKey => $cgItem) {
  390. $packingCharge = $cgItem['packingCharge'] ?? 0;
  391. $shortCharge = $cgItem['shortCharge'] ?? 0;
  392. $longCharge = $cgItem['longCharge'] ?? 0;
  393. $pickCharge = $cgItem['pickCharge'] ?? 0;
  394. $localCharge = $cgItem['localCharge'] ?? 0;
  395. $packFreight = bcadd($packFreight, $packingCharge, 2);
  396. $packFreight = bcadd($packFreight, $shortCharge, 2);
  397. $packFreight = bcadd($packFreight, $longCharge, 2);
  398. $packFreight = bcadd($packFreight, $pickCharge, 2);
  399. $packFreight = bcadd($packFreight, $localCharge, 2);
  400. $tkPrice = $cgItem['tkPrice'] ?? 0;
  401. $orderSn = $cgItem['orderSn'] ?? '';
  402. $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  403. if (!empty($cgItemInfoList)) {
  404. foreach ($cgItemInfoList as $cgData) {
  405. $itemNum = $cgData->itemNum ?? 0;
  406. $productId = $cgData->productId ?? 0;
  407. $refundNum = $cgData->refundNum ?? 0;
  408. $remainNum = bcsub($itemNum, $refundNum);
  409. $bigPrice = $cgData->bigPrice ?? 0;
  410. $amount = bcmul($remainNum, $bigPrice, 2);
  411. $itemName = $productList[$productId]['name'] ?? '已删除';
  412. $arr[$productId]['name'] = $itemName;
  413. $arr[$productId]['id'] = $productId;
  414. if (isset($arr[$productId]['expend'])) {
  415. $arr[$productId]['expend'] = bcadd($amount, $arr[$productId]['expend'], 2);
  416. $arr[$productId]['outNum'] = bcadd($arr[$productId]['outNum'], $remainNum, 2);
  417. } else {
  418. $arr[$productId]['expend'] = $amount;
  419. $arr[$productId]['outNum'] = $remainNum;
  420. $arr[$productId]['inNum'] = 0;
  421. $arr[$productId]['income'] = 0;
  422. }
  423. }
  424. if ($tkPrice > 0) {
  425. $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  426. if (!empty($cgRefundOrderList)) {
  427. foreach ($cgRefundOrderList as $cgRefundOrder) {
  428. if ($cgRefundOrder->refundType == 2) {
  429. $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
  430. }
  431. }
  432. }
  433. }
  434. }
  435. }
  436. }
  437. //批发开单
  438. $pfIncome = 0;
  439. $tbSaleIncome = [];
  440. $tbSaleIncomeStaff = [];
  441. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  442. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  443. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  444. if (!empty($ghsOrderList)) {
  445. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  446. $book = $ghsOrder['book'] ?? 0;
  447. $status = $ghsOrder['status'] ?? 0;
  448. if ($book == 1 && $status == 2) {
  449. //预订单待发货的不算收入
  450. continue;
  451. }
  452. if ($status == 0 || $status == 5) {
  453. continue;
  454. }
  455. $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
  456. $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
  457. $orderSn = $ghsOrder['orderSn'] ?? '';
  458. $actPrice = $ghsOrder['actPrice'] ?? 0;
  459. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  460. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  461. $labourCost = $ghsOrder['labourCost'] ?? 0;
  462. $discountAmount = $ghsOrder['discountAmount'] ?? 0;
  463. $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
  464. $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
  465. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  466. if (!empty($orderItemList)) {
  467. foreach ($orderItemList as $orderItem) {
  468. $refundNum = $orderItem->refundNum ?? 0;
  469. $name = $orderItem->name ?? '';
  470. $productId = $orderItem->productId ?? 0;
  471. $num = $orderItem->xhNum ?? 0;
  472. $remainNum = bcsub($num, $refundNum);
  473. $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
  474. $amount = bcmul($remainNum, $xhUnitPrice, 2);
  475. $itemName = $productList[$productId]['name'] ?? '已删除';
  476. $arr[$productId]['name'] = $itemName;
  477. $arr[$productId]['id'] = $productId;
  478. if (isset($arr[$productId]['income'])) {
  479. $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
  480. $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
  481. } else {
  482. $arr[$productId]['expend'] = 0;
  483. $arr[$productId]['outNum'] = 0;
  484. $arr[$productId]['inNum'] = $remainNum;
  485. $arr[$productId]['income'] = $amount;
  486. }
  487. //单独统计
  488. $belongCost = $orderItem->belongCost ?? 0;
  489. if ($belongCost == 1) {
  490. if (isset($tbSaleIncome[$productId])) {
  491. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  492. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  493. } else {
  494. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  495. }
  496. if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
  497. $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
  498. $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
  499. } else {
  500. $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
  501. }
  502. }
  503. }
  504. if ($tkPrice > 0) {
  505. $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  506. if (!empty($refundList)) {
  507. foreach ($refundList as $refund) {
  508. if ($refund->refundType == 1) {
  509. //退货退款前面已经考虑,这里不再处理 OK
  510. } else {
  511. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  512. }
  513. }
  514. }
  515. }
  516. }
  517. }
  518. }
  519. //零售开单
  520. $lsIncome = 0;
  521. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  522. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  523. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  524. foreach ($hdOrderList as $hdKey => $hdOrder) {
  525. $actPrice = $hdOrder['mainPay'] ?? 0;
  526. $cash = $hdOrder['cash'] ?? 0;
  527. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  528. $lsIncome = bcadd($lsIncome, $cash, 2);
  529. $orderSn = $hdOrder['orderSn'] ?? '';
  530. $tkPrice = $hdOrder['tkPrice'] ?? 0;
  531. $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  532. if (!empty($orderItemList)) {
  533. foreach ($orderItemList as $orderItem) {
  534. $unitPrice = $orderItem->unitPrice ?? 0;
  535. $refundNum = $orderItem->refundNum ?? 0;
  536. $num = $orderItem->num ?? 0;
  537. $productId = $orderItem->itemId ?? 0;
  538. $remainNum = bcsub($num, $refundNum);
  539. $amount = bcmul($remainNum, $unitPrice, 2);
  540. $itemName = $productList[$productId]['name'] ?? '已删除';
  541. $arr[$productId]['name'] = $itemName;
  542. $arr[$productId]['id'] = $productId;
  543. if (isset($arr[$productId]['income'])) {
  544. $arr[$productId]['income'] = bcadd($amount, $arr[$productId]['income'], 2);
  545. $arr[$productId]['inNum'] = bcadd($arr[$productId]['inNum'], $remainNum, 2);
  546. } else {
  547. $arr[$productId]['expend'] = 0;
  548. $arr[$productId]['outNum'] = 0;
  549. $arr[$productId]['inNum'] = $remainNum;
  550. $arr[$productId]['income'] = $amount;
  551. }
  552. }
  553. if ($tkPrice > 0) {
  554. $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  555. if (!empty($refundList)) {
  556. foreach ($refundList as $refund) {
  557. if ($refund->refundType == 2) {
  558. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  559. }
  560. }
  561. }
  562. }
  563. }
  564. }
  565. $totalIncome = 0;
  566. $totalExpend = 0;
  567. if (!empty($arr)) {
  568. foreach ($arr as $key => $val) {
  569. $income = $val['income'] ?? 0;
  570. $income = floatval($income);
  571. $totalIncome = bcadd($totalIncome, $income, 2);
  572. $expend = $val['expend'] ?? 0;
  573. $expend = floatval($expend);
  574. $totalExpend = bcadd($totalExpend, $expend, 2);
  575. $profit = bcsub($income, $expend, 2);
  576. $arr[$key]['profit'] = floatval($profit);
  577. $arr[$key]['income'] = floatval($income);
  578. $arr[$key]['expend'] = floatval($expend);
  579. }
  580. }
  581. //总成本算上物流打包等费用
  582. $totalExpend = bcadd($totalExpend, $packFreight, 2);
  583. //总收入算上采购仅退款
  584. $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
  585. //总收入加上人工费
  586. $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
  587. //总收入减掉优惠金额
  588. $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
  589. //总成本算上销售仅退款
  590. $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
  591. $totalProfit = bcsub($totalIncome, $totalExpend, 2);
  592. $totalIncome = floatval($totalIncome);
  593. $totalExpend = floatval($totalExpend);
  594. $totalProfit = floatval($totalProfit);
  595. $arr = array_values($arr);
  596. $arr = arrayUtil::arraySort($arr, 'profit');
  597. if ($export == 1) {
  598. $statData = [
  599. ['id' => 0, 'name' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'inNum' => 0, 'outNum' => 0],
  600. ['id' => 0, 'name' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'inNum' => 0, 'outNum' => 0],
  601. ['id' => 0, 'name' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'inNum' => 0, 'outNum' => 0],
  602. ['id' => 0, 'name' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'inNum' => 0, 'outNum' => 0],
  603. ['id' => 0, 'name' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'inNum' => 0, 'outNum' => 0],
  604. ['id' => 0, 'name' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'inNum' => 0, 'outNum' => 0],
  605. ];
  606. $list = array_merge($arr, $statData);
  607. self::exportIncomeOutItem($list, $mainId);
  608. }
  609. util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
  610. 'totalIncome' => $totalIncome,
  611. 'totalExpend' => $totalExpend,
  612. 'totalProfit' => $totalProfit,
  613. 'tbSaleIncome' => $tbSaleIncome,
  614. 'tbCgIncome' => $tbCgIncome,
  615. 'tbCgIncomeStaff' => $tbCgIncomeStaff,
  616. 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
  617. 'totalLabourCost' => $totalLabourCost,
  618. 'totalDiscountAmount' => $totalDiscountAmount,
  619. ]);
  620. }
  621. //按分类统计收入 ssh 20231111
  622. public static function classProfile($mainId)
  623. {
  624. $get = Yii::$app->request->get();
  625. $export = $get['export'] ?? 0;
  626. $where = [];
  627. $where['mainId'] = $mainId;
  628. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  629. $startTime = $get['startTime'] ?? '';
  630. $endTime = $get['endTime'] ?? '';
  631. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  632. $start = $period['startTime'];
  633. $end = $period['endTime'];
  634. $where['time'] = ['between', [$start, $end]];
  635. $currentStartDate = date('Y-m-d', strtotime($start));
  636. $currentEndDate = date('Y-m-d', strtotime($end));
  637. $currentStartTime = $currentStartDate . ' 00:00:00';
  638. $currentEndTime = $currentEndDate . ' 23:59:59';
  639. $s = strtotime($currentStartTime);
  640. $e = strtotime($currentEndTime);
  641. $p = bcsub($e, $s);
  642. $xx = bcmul(86400, 90);
  643. if ($p > $xx) {
  644. util::fail('查询时间不能超过三个月');
  645. }
  646. $classList = ItemClassClass::getAllByCondition(['mainId' => $mainId], null, 'id,name', 'id');
  647. if (empty($classList)) {
  648. util::fail('没有找到花材');
  649. }
  650. $arr = [];
  651. //打包费和运费
  652. $packFreight = 0;
  653. $onlyRefundCg = 0;
  654. $onlyRefund = 0;
  655. $totalDiscountAmount = 0;
  656. $totalLabourCost = 0;
  657. //采购入库
  658. $tbCgIncome = [];
  659. $tbCgIncomeStaff = [];
  660. $cgWhere = ['mainId' => $mainId, 'status' => 4];
  661. $cgWhere['entryTime'] = ['between', [$currentStartTime, $currentEndTime]];
  662. $cgList = PurchaseOrderClass::getAllByCondition($cgWhere, null, '*');
  663. if (!empty($cgList)) {
  664. foreach ($cgList as $cgKey => $cgItem) {
  665. $packingCharge = $cgItem['packingCharge'] ?? 0;
  666. $shortCharge = $cgItem['shortCharge'] ?? 0;
  667. $longCharge = $cgItem['longCharge'] ?? 0;
  668. $pickCharge = $cgItem['pickCharge'] ?? 0;
  669. $localCharge = $cgItem['localCharge'] ?? 0;
  670. $cgStaffId = $cgItem['cgStaffId'] ?? 0;
  671. $cgStaffName = $cgItem['cgStaffName'] ?? '';
  672. $actPrice = $cgItem['actPrice'] ?? 0;
  673. if ($actPrice <= 0) {
  674. continue;
  675. }
  676. $packFreight = bcadd($packFreight, $packingCharge, 2);
  677. $packFreight = bcadd($packFreight, $shortCharge, 2);
  678. $packFreight = bcadd($packFreight, $longCharge, 2);
  679. $packFreight = bcadd($packFreight, $pickCharge, 2);
  680. $packFreight = bcadd($packFreight, $localCharge, 2);
  681. $tkPrice = $cgItem['tkPrice'] ?? 0;
  682. $orderSn = $cgItem['orderSn'] ?? '';
  683. $cgItemInfoList = PurchaseOrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  684. if (!empty($cgItemInfoList)) {
  685. foreach ($cgItemInfoList as $cgData) {
  686. $itemNum = $cgData->itemNum ?? 0;
  687. $productId = $cgData->productId ?? 0;
  688. $name = $cgData->name ?? '';
  689. $refundNum = $cgData->refundNum ?? 0;
  690. $remainNum = bcsub($itemNum, $refundNum);
  691. $bigPrice = $cgData->bigPrice ?? 0;
  692. $amount = bcmul($remainNum, $bigPrice, 2);
  693. $classId = $cgData->classId;
  694. $className = $classList[$classId]['name'] ?? '已删除';
  695. if ($className == '运费') {
  696. //noticeUtil::push("{$cgData->orderSn}运费项", '15280215347');
  697. }
  698. $arr[$classId]['className'] = $className;
  699. $arr[$classId]['classId'] = $classId;
  700. if (isset($arr[$classId]['expend'])) {
  701. $arr[$classId]['expend'] = bcadd($amount, $arr[$classId]['expend'], 2);
  702. $arr[$classId]['cgNum'] = bcadd($arr[$classId]['cgNum'], $remainNum);
  703. } else {
  704. $arr[$classId]['expend'] = $amount;
  705. $arr[$classId]['income'] = 0;
  706. $arr[$classId]['cgNum'] = $remainNum;
  707. $arr[$classId]['saleNum'] = 0;
  708. }
  709. //单独统计
  710. $belongCost = $cgData->belongCost ?? 0;
  711. if ($belongCost == 1) {
  712. if (isset($tbCgIncome[$productId])) {
  713. $tbCgIncome[$productId]['num'] = bcadd($tbCgIncome[$productId]['num'], $remainNum);
  714. $tbCgIncome[$productId]['price'] = bcadd($tbCgIncome[$productId]['price'], $amount, 2);
  715. } else {
  716. $tbCgIncome[$productId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount];
  717. }
  718. if (isset($tbCgIncomeStaff[$productId]) && isset($tbCgIncomeStaff[$productId][$cgStaffId])) {
  719. $tbCgIncomeStaff[$productId][$cgStaffId]['num'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['num'], $remainNum);
  720. $tbCgIncomeStaff[$productId][$cgStaffId]['price'] = bcadd($tbCgIncomeStaff[$productId][$cgStaffId]['price'], $amount, 2);
  721. } else {
  722. $tbCgIncomeStaff[$productId][$cgStaffId] = ['num' => $remainNum, 'name' => $name, 'price' => $amount, 'staffName' => $cgStaffName,];
  723. }
  724. }
  725. }
  726. if ($tkPrice > 0) {
  727. $cgRefundOrderList = CgRefundClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  728. if (!empty($cgRefundOrderList)) {
  729. foreach ($cgRefundOrderList as $cgRefundOrder) {
  730. if ($cgRefundOrder->refundType == 2) {
  731. $onlyRefundCg = bcadd($cgRefundOrder->refundPrice, $onlyRefundCg, 2);
  732. }
  733. }
  734. }
  735. }
  736. }
  737. }
  738. }
  739. //批发开单
  740. $pfIncome = 0;
  741. $tbSaleIncome = [];
  742. $tbSaleIncomeStaff = [];
  743. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  744. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  745. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  746. if (!empty($ghsOrderList)) {
  747. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  748. $book = $ghsOrder['book'] ?? 0;
  749. $status = $ghsOrder['status'] ?? 0;
  750. if ($book == 1 && $status == 2) {
  751. //预订单待发货的不算收入
  752. continue;
  753. }
  754. if ($status == 0 || $status == 5) {
  755. continue;
  756. }
  757. $shopAdminId = $ghsOrder['shopAdminId'] ?? 0;
  758. $shopAdminName = $ghsOrder['shopAdminName'] ?? '';
  759. $orderSn = $ghsOrder['orderSn'] ?? '';
  760. $actPrice = $ghsOrder['actPrice'] ?? 0;
  761. if ($actPrice <= 0) {
  762. continue;
  763. }
  764. $tkPrice = $ghsOrder['tkPrice'] ?? 0;
  765. $pfIncome = bcadd($pfIncome, $actPrice, 2);
  766. $labourCost = $ghsOrder['labourCost'] ?? 0;
  767. $discountAmount = $ghsOrder['discountAmount'] ?? 0;
  768. $totalLabourCost = bcadd($labourCost, $totalLabourCost, 2);
  769. $totalDiscountAmount = bcadd($totalDiscountAmount, $discountAmount, 2);
  770. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  771. if (!empty($orderItemList)) {
  772. foreach ($orderItemList as $orderItem) {
  773. $refundNum = $orderItem->refundNum ?? 0;
  774. $name = $orderItem->name ?? '';
  775. $productId = $orderItem->productId ?? 0;
  776. $num = $orderItem->xhNum ?? 0;
  777. $remainNum = bcsub($num, $refundNum);
  778. $classId = $orderItem->classId ?? 0;
  779. $className = $classList[$classId]['name'] ?? '已删除';
  780. $xhUnitPrice = $orderItem->xhUnitPrice ?? 0;
  781. $amount = bcmul($remainNum, $xhUnitPrice, 2);
  782. $arr[$classId]['className'] = $className;
  783. $arr[$classId]['classId'] = $classId;
  784. if (isset($arr[$classId]['income'])) {
  785. $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
  786. $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
  787. } else {
  788. $arr[$classId]['expend'] = 0;
  789. $arr[$classId]['income'] = $amount;
  790. $arr[$classId]['saleNum'] = $remainNum;
  791. $arr[$classId]['cgNum'] = 0;
  792. }
  793. //单独统计
  794. $belongCost = $orderItem->belongCost ?? 0;
  795. if ($belongCost == 1) {
  796. if (isset($tbSaleIncome[$productId])) {
  797. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  798. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  799. } else {
  800. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  801. }
  802. if (isset($tbSaleIncomeStaff[$productId]) && isset($tbSaleIncomeStaff[$productId][$shopAdminId])) {
  803. $tbSaleIncomeStaff[$productId][$shopAdminId]['num'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['num'], $remainNum);
  804. $tbSaleIncomeStaff[$productId][$shopAdminId]['price'] = bcadd($tbSaleIncomeStaff[$productId][$shopAdminId]['price'], $amount, 2);
  805. } else {
  806. $tbSaleIncomeStaff[$productId][$shopAdminId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount, 'staffName' => $shopAdminName];
  807. }
  808. }
  809. }
  810. if ($tkPrice > 0) {
  811. $refundList = RefundOrderClass::getAllByCondition(['relateOrderSn' => $orderSn], null, '*', null, true);
  812. if (!empty($refundList)) {
  813. foreach ($refundList as $refund) {
  814. if ($refund->refundType == 1) {
  815. //退货退款前面已经考虑,这里不再处理 OK
  816. } else {
  817. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  818. }
  819. }
  820. }
  821. }
  822. }
  823. }
  824. }
  825. //零售开单
  826. $lsIncome = 0;
  827. $hdWhere = ['mainId' => $mainId, 'status' => ['in', [HdOrderClass::ORDER_STATUS_UN_SEND, HdOrderClass::ORDER_STATUS_SENDING, HdOrderClass::ORDER_STATUS_COMPLETE]]];
  828. $hdWhere['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  829. $hdOrderList = HdOrderClass::getAllByCondition($hdWhere, null, '*');
  830. foreach ($hdOrderList as $hdKey => $hdOrder) {
  831. $actPrice = $hdOrder['mainPay'] ?? 0;
  832. $cash = $hdOrder['cash'] ?? 0;
  833. if ($actPrice <= 0 && $cash <= 0) {
  834. continue;
  835. }
  836. $lsIncome = bcadd($actPrice, $lsIncome, 2);
  837. $lsIncome = bcadd($lsIncome, $cash, 2);
  838. $orderSn = $hdOrder['orderSn'] ?? '';
  839. $tkPrice = $hdOrder['tkPrice'] ?? 0;
  840. $orderItemList = \bizHd\order\classes\OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  841. if (!empty($orderItemList)) {
  842. foreach ($orderItemList as $orderItem) {
  843. $classId = $orderItem->classId ?? 0;
  844. $unitPrice = $orderItem->unitPrice ?? 0;
  845. $refundNum = $orderItem->refundNum ?? 0;
  846. $num = $orderItem->num ?? 0;
  847. $productId = $orderItem->itemId ?? 0;
  848. $name = $orderItem->name ?? '';
  849. $remainNum = bcsub($num, $refundNum);
  850. $amount = bcmul($remainNum, $unitPrice, 2);
  851. $className = $classList[$classId]['name'] ?? '已删除';
  852. $arr[$classId]['className'] = $className;
  853. $arr[$classId]['classId'] = $classId;
  854. if (isset($arr[$classId]['income'])) {
  855. $arr[$classId]['income'] = bcadd($amount, $arr[$classId]['income'], 2);
  856. $arr[$classId]['saleNum'] = bcadd($arr[$classId]['saleNum'], $remainNum);
  857. } else {
  858. $arr[$classId]['expend'] = 0;
  859. $arr[$classId]['income'] = $amount;
  860. $arr[$classId]['saleNum'] = $remainNum;
  861. $arr[$classId]['cgNum'] = 0;
  862. }
  863. $belongCost = $orderItem->belongCost ?? 0;
  864. if ($belongCost == 1) {
  865. if (isset($tbSaleIncome[$productId])) {
  866. $tbSaleIncome[$productId]['num'] = bcadd($tbSaleIncome[$productId]['num'], $remainNum);
  867. $tbSaleIncome[$productId]['price'] = bcadd($tbSaleIncome[$productId]['price'], $amount, 2);
  868. } else {
  869. $tbSaleIncome[$productId] = ['name' => $name, 'num' => $remainNum, 'price' => $amount];
  870. }
  871. }
  872. }
  873. if ($tkPrice > 0) {
  874. $refundList = HdRefundClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  875. if (!empty($refundList)) {
  876. foreach ($refundList as $refund) {
  877. if ($refund->refundType == 2) {
  878. $onlyRefund = bcadd($onlyRefund, $refund->refundPrice, 2);
  879. }
  880. }
  881. }
  882. }
  883. }
  884. }
  885. $totalIncome = 0;
  886. $totalExpend = 0;
  887. if (!empty($arr)) {
  888. foreach ($arr as $key => $val) {
  889. $income = $val['income'] ?? 0;
  890. $income = floatval($income);
  891. $totalIncome = bcadd($totalIncome, $income, 2);
  892. $expend = $val['expend'] ?? 0;
  893. $expend = floatval($expend);
  894. $totalExpend = bcadd($totalExpend, $expend, 2);
  895. $profit = bcsub($income, $expend, 2);
  896. $arr[$key]['profit'] = floatval($profit);
  897. $arr[$key]['income'] = floatval($income);
  898. $arr[$key]['expend'] = floatval($expend);
  899. }
  900. }
  901. //总成本算上物流打包等费用
  902. $totalExpend = bcadd($totalExpend, $packFreight, 2);
  903. //总收入算上采购仅退款
  904. $totalIncome = bcadd($totalIncome, $onlyRefundCg, 2);
  905. //总收入加上人工费
  906. $totalIncome = bcadd($totalIncome, $totalLabourCost, 2);
  907. //总收入减掉优惠金额
  908. $totalIncome = bcsub($totalIncome, $totalDiscountAmount, 2);
  909. //总成本算上销售仅退款
  910. $totalExpend = bcadd($totalExpend, $onlyRefund, 2);
  911. $totalProfit = bcsub($totalIncome, $totalExpend, 2);
  912. $totalIncome = floatval($totalIncome);
  913. $totalExpend = floatval($totalExpend);
  914. $totalProfit = floatval($totalProfit);
  915. $arr = array_values($arr);
  916. $arr = arrayUtil::arraySort($arr, 'profit');
  917. if ($export == 1) {
  918. $statData = [
  919. ['classId' => 0, 'className' => '物流打包费', 'income' => 0, 'expend' => $packFreight, 'profit' => -$packFreight, 'saleNum' => 0, 'cgNum' => 0],
  920. ['classId' => 0, 'className' => '采购仅退款', 'income' => $onlyRefundCg, 'expend' => 0, 'profit' => $onlyRefundCg, 'saleNum' => 0, 'cgNum' => 0],
  921. ['classId' => 0, 'className' => '销售仅退款', 'income' => 0, 'expend' => $onlyRefund, 'profit' => -$onlyRefund, 'saleNum' => 0, 'cgNum' => 0],
  922. ['classId' => 0, 'className' => '附加人工费', 'income' => $totalLabourCost, 'expend' => 0, 'profit' => $totalLabourCost, 'saleNum' => 0, 'cgNum' => 0],
  923. ['classId' => 0, 'className' => '优惠金额', 'income' => -$totalDiscountAmount, 'expend' => 0, 'profit' => -$totalDiscountAmount, 'saleNum' => 0, 'cgNum' => 0],
  924. ['classId' => 0, 'className' => '【汇总金额】', 'income' => $totalIncome, 'expend' => $totalExpend, 'profit' => $totalProfit, 'saleNum' => 0, 'cgNum' => 0],
  925. ];
  926. $list = array_merge($arr, $statData);
  927. self::exportIncomeClass($list, $mainId);
  928. }
  929. util::success(['list' => $arr, 'packFreight' => $packFreight, 'onlyRefundCg' => $onlyRefundCg, 'onlyRefund' => $onlyRefund,
  930. 'totalIncome' => $totalIncome,
  931. 'totalExpend' => $totalExpend,
  932. 'totalProfit' => $totalProfit,
  933. 'tbSaleIncome' => $tbSaleIncome,
  934. 'tbCgIncome' => $tbCgIncome,
  935. 'tbCgIncomeStaff' => $tbCgIncomeStaff,
  936. 'tbSaleIncomeStaff' => $tbSaleIncomeStaff,
  937. 'totalLabourCost' => $totalLabourCost,
  938. 'totalDiscountAmount' => $totalDiscountAmount,
  939. ]);
  940. }
  941. public static function exportIncomeOutItem($list, $mainId)
  942. {
  943. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  944. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  945. $objPHPExcel = new \PHPExcel();
  946. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  947. ->setLastModifiedBy("Maarten Balliauw")
  948. ->setTitle("Office 2007 XLSX Document")
  949. ->setSubject("Office 2007 XLSX Document")
  950. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  951. ->setKeywords("office 2007 openxml php")
  952. ->setCategory("file");
  953. $ghsTitle = '供货商';
  954. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  955. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  956. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  957. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  958. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  959. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  960. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  961. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  962. $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售收入');
  963. $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售数量');
  964. $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购成本');
  965. $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购数量');
  966. $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
  967. //设置宽度
  968. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  969. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  970. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  971. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  972. //加粗
  973. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  974. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  975. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  976. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  977. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  978. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  979. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  980. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  981. $baseRow = 2;
  982. foreach ($list as $key => $custom) {
  983. $i = $baseRow + $key;
  984. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
  985. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['income']);
  986. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['inNum']);
  987. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['expend']);
  988. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['outNum']);
  989. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
  990. //居左
  991. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  992. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  993. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  994. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  995. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  996. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  997. }
  998. $fileName = '收支花材-' . date("m-d");
  999. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1000. $objPHPExcel->setActiveSheetIndex(0);
  1001. $dir = './priceTable/' . $mainId;
  1002. if (file_exists($dir) == false) {
  1003. mkdir($dir, 0777, true);
  1004. }
  1005. $date = $fileName;
  1006. $file = $date . '.xls';
  1007. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1008. if (file_exists($dir . '/' . $file)) {
  1009. unlink($dir . '/' . $file);
  1010. }
  1011. $objWriter->save($dir . '/' . $file);
  1012. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1013. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1014. }
  1015. public static function exportIncomeClass($list, $mainId)
  1016. {
  1017. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1018. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1019. $objPHPExcel = new \PHPExcel();
  1020. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1021. ->setLastModifiedBy("Maarten Balliauw")
  1022. ->setTitle("Office 2007 XLSX Document")
  1023. ->setSubject("Office 2007 XLSX Document")
  1024. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1025. ->setKeywords("office 2007 openxml php")
  1026. ->setCategory("file");
  1027. $ghsTitle = '供货商';
  1028. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1029. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1030. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1031. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1032. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1033. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1034. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1035. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  1036. $objPHPExcel->getActiveSheet()->setCellValue('B1', '销售数量');
  1037. $objPHPExcel->getActiveSheet()->setCellValue('C1', '销售收入');
  1038. $objPHPExcel->getActiveSheet()->setCellValue('D1', '采购数量');
  1039. $objPHPExcel->getActiveSheet()->setCellValue('E1', '采购成本');
  1040. $objPHPExcel->getActiveSheet()->setCellValue('F1', '利润');
  1041. //设置宽度
  1042. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(20);
  1043. //加粗
  1044. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1045. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1046. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1047. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1048. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1049. $objPHPExcel->getActiveSheet()->getStyle('F1')->getFont()->setSize(9)->setBold(true);
  1050. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1051. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1052. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1053. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1054. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1055. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1056. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1057. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1058. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1059. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1060. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1061. $objPHPExcel->getActiveSheet()->getStyle('F1')->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
  1062. $baseRow = 2;
  1063. foreach ($list as $key => $custom) {
  1064. $i = $baseRow + $key;
  1065. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['className']);
  1066. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['saleNum']);
  1067. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['income']);
  1068. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['cgNum']);
  1069. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['expend']);
  1070. $objPHPExcel->getActiveSheet()->setCellValue('F' . $i, $custom['profit']);
  1071. //居左
  1072. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1073. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1074. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1075. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1076. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1077. $objPHPExcel->getActiveSheet()->getStyle('F' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
  1078. }
  1079. $fileName = '收支分类-' . time();
  1080. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1081. $objPHPExcel->setActiveSheetIndex(0);
  1082. $dir = './priceTable/' . $mainId;
  1083. if (file_exists($dir) == false) {
  1084. mkdir($dir, 0777, true);
  1085. }
  1086. $date = $fileName;
  1087. $file = $date . '.xls';
  1088. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1089. if (file_exists($dir . '/' . $file)) {
  1090. unlink($dir . '/' . $file);
  1091. }
  1092. $objWriter->save($dir . '/' . $file);
  1093. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1094. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1095. }
  1096. public static function exportItemSale($list, $mainId)
  1097. {
  1098. $phpExcelFile = Yii::getAlias("@vendor/phpoffice/phpexcel/");
  1099. require_once($phpExcelFile . 'Classes/PHPExcel.php');
  1100. $objPHPExcel = new \PHPExcel();
  1101. $objPHPExcel->getProperties()->setCreator("Maarten Balliauw")
  1102. ->setLastModifiedBy("Maarten Balliauw")
  1103. ->setTitle("Office 2007 XLSX Document")
  1104. ->setSubject("Office 2007 XLSX Document")
  1105. ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
  1106. ->setKeywords("office 2007 openxml php")
  1107. ->setCategory("file");
  1108. $ghsTitle = '供货商';
  1109. $objPHPExcel->getActiveSheet()->getHeaderFooter()->setOddHeader(date('n月j日') . " " . $ghsTitle);
  1110. $objPHPExcel->getActiveSheet()->getPageMargins()->setTop(0.7);
  1111. $objPHPExcel->getActiveSheet()->getPageMargins()->setBottom(0.1);
  1112. $objPHPExcel->getActiveSheet()->getPageMargins()->setLeft(0.5);
  1113. $objPHPExcel->getActiveSheet()->getPageMargins()->setRight(0);
  1114. $objPHPExcel->getActiveSheet()->getPageMargins()->setHeader(0.1);
  1115. $objPHPExcel->getActiveSheet()->getPageMargins()->setFooter(0);
  1116. $objPHPExcel->getActiveSheet()->setCellValue('A1', '名称');
  1117. $objPHPExcel->getActiveSheet()->setCellValue('B1', '总数量');
  1118. $objPHPExcel->getActiveSheet()->setCellValue('C1', '总金额');
  1119. $objPHPExcel->getActiveSheet()->setCellValue('D1', '毛利');
  1120. $objPHPExcel->getActiveSheet()->setCellValue('E1', '毛利率');
  1121. //设置宽度
  1122. $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(30);
  1123. $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
  1124. $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(20);
  1125. $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(20);
  1126. $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(20);
  1127. //加粗
  1128. $objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(9)->setBold(true);
  1129. $objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setSize(9)->setBold(true);
  1130. $objPHPExcel->getActiveSheet()->getStyle('C1')->getFont()->setSize(9)->setBold(true);
  1131. $objPHPExcel->getActiveSheet()->getStyle('D1')->getFont()->setSize(9)->setBold(true);
  1132. $objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->setSize(9)->setBold(true);
  1133. $objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1134. $objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1135. $objPHPExcel->getActiveSheet()->getStyle('C1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1136. $objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1137. $objPHPExcel->getActiveSheet()->getStyle('E1')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1138. $baseRow = 2;
  1139. foreach ($list as $key => $custom) {
  1140. $i = $baseRow + $key;
  1141. $objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $custom['name']);
  1142. $objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $custom['num']);
  1143. $objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $custom['amount']);
  1144. $objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $custom['gross']);
  1145. $objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $custom['mll'] . '%');
  1146. //居左
  1147. $objPHPExcel->getActiveSheet()->getStyle('A' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1148. $objPHPExcel->getActiveSheet()->getStyle('B' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1149. $objPHPExcel->getActiveSheet()->getStyle('C' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1150. $objPHPExcel->getActiveSheet()->getStyle('D' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1151. $objPHPExcel->getActiveSheet()->getStyle('E' . $i)->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_LEFT);
  1152. }
  1153. $fileName = '花材销量-' . date("m-d");
  1154. $objPHPExcel->getActiveSheet()->setTitle($fileName);
  1155. $objPHPExcel->setActiveSheetIndex(0);
  1156. $dir = './priceTable/' . $mainId;
  1157. if (file_exists($dir) == false) {
  1158. mkdir($dir, 0777, true);
  1159. }
  1160. $date = $fileName;
  1161. $file = $date . '.xls';
  1162. $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
  1163. if (file_exists($dir . '/' . $file)) {
  1164. unlink($dir . '/' . $file);
  1165. }
  1166. $objWriter->save($dir . '/' . $file);
  1167. $fileUrl = Yii::$app->params['ghsHost'] . '/priceTable/' . $mainId . '/' . $file;
  1168. util::success(['file' => $fileUrl, 'shortFile' => $file]);
  1169. }
  1170. public static function send($mainId)
  1171. {
  1172. $get = Yii::$app->request->get();
  1173. $where = [];
  1174. $where['mainId'] = $mainId;
  1175. $searchTime = isset($get['searchTime']) && !empty($get['searchTime']) ? $get['searchTime'] : 'today';
  1176. $startTime = $get['startTime'] ?? '';
  1177. $endTime = $get['endTime'] ?? '';
  1178. $period = dateUtil::formatTime($searchTime, $startTime, $endTime, true);
  1179. $start = $period['startTime'];
  1180. $end = $period['endTime'];
  1181. $where['time'] = ['between', [$start, $end]];
  1182. $currentStartDate = date('Y-m-d', strtotime($start));
  1183. $currentEndDate = date('Y-m-d', strtotime($end));
  1184. $currentStartTime = $currentStartDate . ' 00:00:00';
  1185. $currentEndTime = $currentEndDate . ' 23:59:59';
  1186. $s = strtotime($currentStartTime);
  1187. $e = strtotime($currentEndTime);
  1188. $p = bcsub($e, $s);
  1189. $xx = bcmul(86400, 90);
  1190. if ($p > $xx) {
  1191. util::fail('查询时间不能超过三个月');
  1192. }
  1193. $where = ['mainId' => $mainId, 'status' => ['in', [OrderClass::ORDER_STATUS_UN_SEND, OrderClass::ORDER_STATUS_SENDING, OrderClass::ORDER_STATUS_COMPLETE]]];
  1194. $where['payTime'] = ['between', [$currentStartTime, $currentEndTime]];
  1195. $ghsOrderList = OrderClass::getAllByCondition($where, null, '*');
  1196. $arr = [];
  1197. if (!empty($ghsOrderList)) {
  1198. foreach ($ghsOrderList as $ghsKey => $ghsOrder) {
  1199. $book = $ghsOrder['book'] ?? 0;
  1200. $status = $ghsOrder['status'] ?? 0;
  1201. if ($book == 1 && $status == 2) {
  1202. //预订单待发货的不算收入
  1203. continue;
  1204. }
  1205. if ($status == 0 || $status == 5) {
  1206. continue;
  1207. }
  1208. $orderSn = $ghsOrder['orderSn'] ?? '';
  1209. $actPrice = $ghsOrder['actPrice'] ?? 0;
  1210. if ($actPrice <= 0) {
  1211. continue;
  1212. }
  1213. $sendStaffId = $ghsOrder['sendStaffId'] ?? 0;
  1214. $sendStaffName = isset($ghsOrder['sendStaffName']) && !empty($ghsOrder['sendStaffName']) ? $ghsOrder['sendStaffName'] : '未记名';
  1215. if (isset($arr[$sendStaffId])) {
  1216. $arr[$sendStaffId]['orderNum'] = bcadd($arr[$sendStaffId]['orderNum'], 1);
  1217. $arr[$sendStaffId]['amount'] = bcadd($arr[$sendStaffId]['amount'], $actPrice, 2);
  1218. } else {
  1219. $arr[$sendStaffId] = ['staffName' => $sendStaffName, 'orderNum' => 1, 'itemNum' => 0, 'amount' => $actPrice, 'staffId' => $sendStaffId];
  1220. }
  1221. $orderItemList = OrderItemClass::getAllByCondition(['orderSn' => $orderSn], null, '*', null, true);
  1222. if (!empty($orderItemList)) {
  1223. foreach ($orderItemList as $orderItem) {
  1224. $refundNum = $orderItem->refundNum ?? 0;
  1225. $num = $orderItem->xhNum ?? 0;
  1226. $remainNum = bcsub($num, $refundNum);
  1227. $arr[$sendStaffId]['itemNum'] = bcadd($arr[$sendStaffId]['itemNum'], $remainNum);
  1228. }
  1229. }
  1230. }
  1231. }
  1232. if (!empty($arr)) {
  1233. $arr = array_values($arr);
  1234. }
  1235. return $arr;
  1236. }
  1237. }